- Showcase UI Reorganization: * Move web UI dashboard from root directory into apps/showcase_ui/ for cleaner project structure * Add 'artemis ui' CLI command (artemis/interfaces/cli/commands/ui.py) and launcher scripts for the interactive showcase console - Standalone MCP Server & Notifiers (mcp_server/): * Implement dedicated MCP server package with mobile automation tools (device_state, inspect_trace, task_manager, task_runner) * Add multi-channel notifiers (AgentAPI, Desktop notifications, Webhooks, File output, and Composite routing) * Introduce background task runner and trace store for autonomous mobile execution monitoring - Cross-Platform Runtime & Toolchain Support: * Add artemis/platform/ with OS-specific implementations for Linux, macOS (Darwin), and Windows * Add artemis/runtime/supervisor.py for resilient process and agent runtime supervision * Add artemis/toolchain/ with dependency resolution and SDK descriptors * Add cross-platform launcher scripts (start.sh, start.bat) and installer scripts (scripts/install_deps.*) - Core Enhancements & Diagnostics: * Enhance coordinate normalization, OCR API fallback, and perception pipeline * Add credential validation (credentials_validator.py) and system diagnostics * Update bilingual documentation (README.md, README_CN.md) and architecture diagrams - Test Suite Expansion: * Add integration tests for cloud gateway, remote ADB, and Artemis login * Add unit tests covering core diagnostics, platform detection, toolchain, credentials validator, optional OCR, and MCP tools
106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
# Copyright 2026 Google LLC
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import os
|
|
from typing import Any
|
|
|
|
import httpx
|
|
from artemis.config import settings
|
|
from artemis.utils.logger import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
_HTTP_CLIENT: httpx.AsyncClient | None = None
|
|
|
|
|
|
def get_http_client() -> httpx.AsyncClient:
|
|
global _HTTP_CLIENT
|
|
if _HTTP_CLIENT is None or _HTTP_CLIENT.is_closed:
|
|
_HTTP_CLIENT = httpx.AsyncClient(timeout=30.0)
|
|
return _HTTP_CLIENT
|
|
|
|
|
|
def is_ocr_configured() -> bool:
|
|
"""Checks if a valid Google Vision OCR API key is configured."""
|
|
ocr_secret = settings.get_api_key("ocr")
|
|
if ocr_secret and ocr_secret.get_secret_value().strip():
|
|
val = ocr_secret.get_secret_value().strip()
|
|
if val not in ("API_KEY", "your_google_cloud_vision_api_key_here"):
|
|
return True
|
|
for env_var in ("OCR_API_KEY", "VISION_API_KEY"):
|
|
val = os.environ.get(env_var)
|
|
if (
|
|
val
|
|
and val.strip()
|
|
and val.strip() not in ("API_KEY", "your_google_cloud_vision_api_key_here")
|
|
):
|
|
return True
|
|
return False
|
|
|
|
|
|
async def perform_ocr(
|
|
screenshot_b64: str,
|
|
client: httpx.AsyncClient | None = None,
|
|
) -> list[dict[str, Any]]:
|
|
"""Calls Google Vision API for text recognition on an image.
|
|
|
|
Args:
|
|
screenshot_b64: Base64 encoded screenshot image.
|
|
client: Optional persistent httpx.AsyncClient.
|
|
|
|
Returns:
|
|
A list of dictionaries containing detected text and position vertices.
|
|
"""
|
|
ocr_secret = settings.get_api_key("ocr")
|
|
api_key = (
|
|
(ocr_secret.get_secret_value() if ocr_secret else None)
|
|
or os.environ.get("OCR_API_KEY")
|
|
or os.environ.get("VISION_API_KEY")
|
|
)
|
|
if not api_key:
|
|
return []
|
|
|
|
url = f"https://vision.googleapis.com/v1/images:annotate?key={api_key}"
|
|
headers = {"Content-Type": "application/json"}
|
|
data = {
|
|
"requests": [
|
|
{
|
|
"image": {"content": screenshot_b64},
|
|
"features": [{"type": "TEXT_DETECTION"}],
|
|
}
|
|
]
|
|
}
|
|
|
|
active_client = client if client is not None else get_http_client()
|
|
response = await active_client.post(url, json=data, headers=headers, timeout=30.0)
|
|
return _parse_ocr_response(response)
|
|
|
|
|
|
def _parse_ocr_response(response: httpx.Response) -> list[dict[str, Any]]:
|
|
if response.status_code == 200:
|
|
res_json = response.json()
|
|
responses = res_json.get("responses", [])
|
|
if responses and "textAnnotations" in responses[0]:
|
|
annotations = responses[0]["textAnnotations"]
|
|
results = []
|
|
# Skip the first annotation (index 0) as it is the full-screen combined text
|
|
for ann in annotations[1:]:
|
|
desc = ann.get("description", "")
|
|
vertices = ann.get("boundingPoly", {}).get("vertices", [])
|
|
results.append({"text": desc, "position": vertices})
|
|
return results
|
|
else:
|
|
return []
|
|
else:
|
|
raise Exception(f"Vision API returned status {response.status_code}: {response.text}")
|