SurfSense/surfsense_local/backend/modules/agent/opencode_runtime.py
Anish Sarkar a67be6ad49 feat(local): call each remote model on its own route, and run the agent on a ChatGPT plan
A remote text model now answers on the route its manifest entry records:
/responses where the connection's provider marks it so, /chat/completions
otherwise. The credential stays the connection's: an API key, or a ChatGPT
plan's token. The plan's limits apply to the plan only.

- Unblock the 21 models recorded as served only on /responses (Neon,
  Sakana, Infer): they are no longer listed as unusable, and chat, titles,
  Studio and Try reach them through the Responses client with their key.
- Run the agent on a ChatGPT plan and on those models. opencode's provider
  becomes @ai-sdk/openai for a /responses model and calls the model
  endpoint's new POST /agent/model/v1/responses. The relay adds the plan's
  token (refreshed once on a 401), drops the fields a plan refuses, sends
  system turns as developer turns and wraps tools in one namespace.
- A plan's limit or a needed sign-in reaches an agent thread with the chat's
  kinds, and in a status opencode does not retry.
- Read a plan model's window from the plan's model list, for chat's history
  budget and the agent's limits.
- Sign in again on a connection still signed in through OpenAI's
  returning-user path, and refuse a callback for another client.
- Send a JSON schema to a Responses model as text.format.

API-key connections to OpenAI, custom endpoints and local models keep
/chat/completions. ADR 0049 records the decision and supersedes ADR 0038's
request body.

Not yet run against a real ChatGPT account, or against Neon, Sakana or Infer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 03:16:14 +05:30

127 lines
4.6 KiB
Python

"""An opencode running SurfSense's current configuration, ready for a turn."""
import asyncio
from dataclasses import dataclass
from typing import Any
import httpx
from fastapi.concurrency import run_in_threadpool
from sqlalchemy.orm import Session
from api.config import get_settings
from api.dependencies import transact
from modules.agent.model_call_route import selected_call_route
from modules.agent.model_reads_images import selected_model_reads_images
from modules.agent.model_window import selected_model_window
from modules.agent.opencode_client import OpencodeClient
from modules.agent.opencode_config import (
CONFIG_FILE,
PROVIDER,
PROVIDER_PACKAGES,
AgentSetup,
write_opencode_config,
)
from shared.config import get_agent_settings, get_storage_settings
# Electron checks the file every 2 s and opencode answers about 2 s after it
# starts; the rest is room for a slow disk on the first start of a session.
READY_SECONDS = 60.0
_POLL_SECONDS = 0.25
class AgentUnavailableError(RuntimeError):
"""opencode is not part of this install, or did not start."""
@dataclass
class ReadyAgent:
"""An opencode serving the configuration just written, and the model it names."""
client: OpencodeClient
model: str
async def ready_opencode(session: Session, *, launch_key: str) -> ReadyAgent:
"""Write opencode's configuration for the selected model, then wait until it serves it.
The opencode already running may hold an earlier configuration, with
another model or a launch key from a previous run; it is reloaded, and
counts only once it reports this one. The caller closes the client.
"""
settings = get_agent_settings()
if not settings.opencode_url or not settings.opencode_password:
raise AgentUnavailableError("the agent's runtime is not part of this install")
model, window = await selected_model_window(session)
setup = AgentSetup(
model=model,
window=window,
reads_images=await selected_model_reads_images(session),
endpoint_url=_endpoint_url(),
launch_key=launch_key,
route=await transact(session, selected_call_route),
)
await run_in_threadpool(
write_opencode_config, get_storage_settings().agent_dir / CONFIG_FILE, setup
)
client = OpencodeClient(settings.opencode_url, settings.opencode_password)
try:
await _until_serving(client, setup)
except BaseException:
await client.close()
raise
return ReadyAgent(client=client, model=model)
def connect_opencode() -> OpencodeClient:
"""A client for the opencode Electron runs, for a call that needs no new configuration.
Answering an approval or deleting a session reaches the server as it is; only
a turn writes the configuration first (`ready_opencode`).
"""
settings = get_agent_settings()
if not settings.opencode_url or not settings.opencode_password:
raise AgentUnavailableError("the agent's runtime is not part of this install")
return OpencodeClient(settings.opencode_url, settings.opencode_password)
def _endpoint_url() -> str:
"""This API's model endpoint, where opencode's only provider points."""
api = get_settings()
return f"http://{api.host}:{api.port}/agent/model/v1"
async def _until_serving(client: OpencodeClient, setup: AgentSetup) -> None:
"""Return once opencode reports `setup`, reloading it once if it holds an older one."""
loop = asyncio.get_running_loop()
deadline = loop.time() + READY_SECONDS
reloaded = False
while True:
try:
await client.require_pinned_version()
if _serves(await client.config(), setup):
return
if not reloaded:
await client.reload_config()
reloaded = True
except httpx.HTTPError:
pass # not yet started by Electron, or still starting
if loop.time() > deadline:
raise AgentUnavailableError(
"opencode did not start with SurfSense's configuration"
)
await asyncio.sleep(_POLL_SECONDS)
def _serves(config: dict[str, Any], setup: AgentSetup) -> bool:
"""Whether a loaded configuration is this one: the provider package, the launch
key, the model's window and image input."""
provider = config.get("provider", {}).get(PROVIDER, {})
model = provider.get("models", {}).get(setup.model, {})
return (
provider.get("npm") == PROVIDER_PACKAGES[setup.route]
and provider.get("options", {}).get("apiKey") == setup.launch_key
and model.get("limit", {}).get("context") == setup.window
and model.get("attachment", False) == setup.reads_images
)