feat: add shared runtime with FastAPI job server and safety pipeline

This commit is contained in:
Space-Banane
2026-05-27 17:43:51 +02:00
parent 84b0df520c
commit 10355bf11a
14 changed files with 1516 additions and 157 deletions

57
src/runtime.py Normal file
View File

@@ -0,0 +1,57 @@
from __future__ import annotations
import logging
import threading
from pathlib import Path
from typing import Any, Callable
from openai import OpenAI
from .agent import ScreenJobAgent
from .models import AgentResult, RunArtifacts, RuntimeOptions
from .utils import setup_artifacts, setup_logger
try:
import pyautogui
except Exception as import_exc:
raise RuntimeError(
"pyautogui is required. Install dependencies with: pip install pyautogui pillow"
) from import_exc
def create_openai_client(api_key: str) -> OpenAI:
return OpenAI(api_key=api_key)
def run_job(
*,
api_key: str,
objective: str,
options: RuntimeOptions,
runs_base: Path,
no_failsafe: bool = False,
cancel_event: threading.Event | None = None,
event_callback: Callable[[dict[str, Any]], None] | None = None,
logger: logging.Logger | None = None,
) -> tuple[AgentResult, RunArtifacts]:
pyautogui.FAILSAFE = not no_failsafe
pyautogui.PAUSE = 0.05
artifacts = setup_artifacts(runs_base)
active_logger = logger or setup_logger(artifacts.log_file, verbose=True)
active_logger.info("ScreenJob booting. Artifacts: %s", str(artifacts.root_dir.resolve()))
active_logger.info("PyAutoGUI FAILSAFE=%s", pyautogui.FAILSAFE)
client = create_openai_client(api_key)
agent = ScreenJobAgent(
client=client,
logger=active_logger,
artifacts=artifacts,
options=options,
cancel_event=cancel_event,
event_callback=event_callback,
)
result = agent.run(objective)
active_logger.info("Run finished. completed=%s elapsed=%.2fs", result.completed, result.ended_at - result.started_at)
return result, artifacts