Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

28.8 KB
Raw Blame
"""Direct Codex Responses streaming; Jarvis alone executes declared tools.

The account's Codex OAuth grant targets the Codex endpoint. It is deliberately
not sent to public Responses: that official SIWC flow needs a different grant.
There is no CLI turn, autonomous harness, hosted tool, or API-key fallback here.
"""

from __future__ import annotations

import json
import logging
import re
from collections.abc import AsyncIterator, Awaitable, Callable
from typing import Any
from uuid import uuid4

from dodaios.core.http_pool import HttpClientPool
from dodaios.core.protocols import BrainDelta, BrainRequest
from dodaios.live.subscription_auth import SubscriptionAuthError, SubscriptionCredentials

_BASE_URL = "https://chatgpt.com/backend-api/codex"
_MAX_EVENT_CHARS = 2 * 1024 * 1024
_MAX_CATALOG_BYTES = 4 * 1024 * 1024
_MAX_ERROR_BYTES = 64 * 1024
_REQUEST_FAILURES = {
    "invalid_function_parameters": "ChatGPT rejected a tool schema in this request.",
    "invalid_tool_schema": "ChatGPT rejected a tool schema in this request.",
    "context_length_exceeded": "This request exceeds the selected ChatGPT model's context limit.",
    "model_not_found": "The selected model is unavailable on this ChatGPT subscription.",
    "invalid_request_error": "ChatGPT rejected this request.",
}
#: A stream the backend ended as incomplete: retrying the same request
#: changes nothing.
_STOPPED_EARLY = frozenset({"max_output_tokens", "incomplete"})
log = logging.getLogger(__name__)
_SAFE_CODE = re.compile(r"^[a-zA-Z0-9_.-]{1,96}$")
_SAFE_EFFORT = re.compile(r"^[a-z][a-z0-9_-]{0,31}$")
_KNOWN_EFFORTS = frozenset({"none", "minimal", "low", "medium", "high", "xhigh", "max"})
# "No thinking" requests (small structured calls) map to the model's lightest
# advertised level: a Codex model without "none" answers 400 to it (live
# 2026-10-03, gpt-6.1-sol: every computer-use step failed before acting).
_LIGHTEST_FIRST = ("none", "minimal", "low", "medium", "high", "xhigh", "max")


class SubscriptionReasoningError(RuntimeError):
    def __init__(
        self,
        message: str,
        *,
        code: str = "subscription_unavailable",
        status: int = 0,
        retry_after: str | None = None,
        detail: str = "",
    ) -> None:
        super().__init__(message)
        self.code = code
        self.status = status
        self.retry_after = retry_after
        #: The backend's own failure (event type, code, short message),
        #: bounded and redacted: for the log only, never shown to a runtime.
        self.detail = detail


def _failure(
    status: int = 0,
    payload: Any = None,
    *,
    retry_after: str | None = None,
) -> SubscriptionReasoningError:
    error = payload.get("error", {}) if isinstance(payload, dict) else {}
    if not isinstance(error, dict):
        error = {}
    code = str(error.get("code") or "")
    if not _SAFE_CODE.fullmatch(code):
        code = "subscription_unavailable"
    if code == "rate_limit_exceeded":
        # A short burst limit, not the plan's allowance: a wait fixes it.
        status = 429
        message = (
            "The selected ChatGPT subscription is receiving too many requests "
            "right now (rate limit). Try again shortly."
        )
    elif status == 429 or code in {
        "usage_limit_reached",
        "subscription_sharing_usage_limit_exceeded",
    }:
        status = 429
        message = (
            "The selected ChatGPT subscription reached a usage limit. "
            "Try again after its allowance resets."
        )
        retry_after = retry_after or _reset_after(error)
    elif status in (401, 403):
        message = (
            "ChatGPT subscription access was rejected. "
            "Check the selected account and sign in again if needed."
        )
    elif code in _STOPPED_EARLY:
        message = (
            "ChatGPT stopped this request before it finished (incomplete). "
            "Resending it unchanged will not help; a tool schema it carries can cause this."
        )
    elif status in (400, 404, 413, 422):
        if code not in _REQUEST_FAILURES:
            code = "invalid_request_error"
        message = (
            f"{_REQUEST_FAILURES[code]} (HTTP {status}). "
            "Check the model, conversation and tool settings before trying again."
        )
    else:
        message = (
            "ChatGPT subscription reasoning failed. "
            "Try again later; no API billing fallback was used."
        )
    return SubscriptionReasoningError(message, code=code, status=status, retry_after=retry_after)


def _reset_after(error: dict[str, Any]) -> str | None:
    """Seconds until a spent allowance resets, from the backend's own fields.

    The usage-limit body names the reset as ``resets_in_seconds`` or as the
    epoch second ``resets_at``; nothing else of the body is read.
    """
    import time

    for field_name, relative in (("resets_in_seconds", True), ("resets_at", False)):
        raw = error.get(field_name)
        if isinstance(raw, bool) or not isinstance(raw, (int, float)):
            continue
        seconds = float(raw) if relative else float(raw) - time.time()
        if 0 <= seconds < 400 * 86400:
            return str(int(seconds))
    return None


#: Key- and token-shaped strings masked in a logged backend failure.
_SECRET = re.compile(
    r"(sk-[A-Za-z0-9_\-]{6,}|[Bb]earer\s+[A-Za-z0-9._\-]{10,}|eyJ[A-Za-z0-9._\-]{20,})"
)
_MAX_DETAIL_CHARS = 300


def _stream_failure(kind: str, event: dict[str, Any]) -> SubscriptionReasoningError:
    """A failure the backend reported inside an accepted (HTTP 200) stream.

    The three shapes carry their reason in different places: ``error`` at
    the top level, ``response.failed`` under ``response.error`` and
    ``response.incomplete`` under ``response.incomplete_details``. The code
    alone decides the wording; the backend's message only reaches the log.
    """
    response = event.get("response") if isinstance(event.get("response"), dict) else {}
    error: dict[str, Any] = {}
    code = ""
    if kind == "error" and not isinstance(event.get("error"), dict):
        # Top-level shape: its own "type" is the event kind, never a reason.
        error = event
        code = str(event.get("code") or "")
    else:
        nested = event.get("error") if kind == "error" else response.get("error")
        if isinstance(nested, dict):
            error = nested
            code = str(nested.get("code") or nested.get("type") or "")
    incomplete = response.get("incomplete_details")
    reason = incomplete.get("reason") if isinstance(incomplete, dict) else None
    if not code and (kind == "response.incomplete" or response.get("status") == "incomplete"):
        code = "max_output_tokens" if reason == "max_output_tokens" else "incomplete"
    message = error.get("message") if isinstance(error.get("message"), str) else ""
    message = _SECRET.sub("[redacted]", " ".join(message.split()))[:_MAX_DETAIL_CHARS]
    shown_code = code if _SAFE_CODE.fullmatch(code) else ("unsafe" if code else "none")
    detail = f"event={kind} code={shown_code}"
    if isinstance(reason, str) and _SAFE_CODE.fullmatch(reason):
        detail += f" reason={reason}"
    if message:
        detail += f" message={message!r}"
    log.warning("ChatGPT subscription stream failed: %s", detail)
    failure = _failure(payload={"error": {"code": code}})
    failure.detail = detail
    return failure


def _invalid_request(message: str) -> SubscriptionReasoningError:
    """A request this client refuses before sending it: never worth a retry."""
    return SubscriptionReasoningError(message, code="invalid_request_error", status=400)


async def _http_failure(response: Any) -> SubscriptionReasoningError:
    """Read bounded error metadata, never expose provider text or request content."""
    import httpx

    raw = bytearray()
    try:
        async for chunk in response.aiter_bytes():
            if len(raw) + len(chunk) > _MAX_ERROR_BYTES:
                raw.clear()
                break
            raw.extend(chunk)
    except httpx.HTTPError:
        # Headers already established rejection; a broken error body must not
        # turn a permanent 400 into a retryable network failure.
        raw.clear()
    payload: Any = None
    if raw:
        try:
            payload = json.loads(raw)
        except ValueError:
            pass  # HTML, malformed JSON and oversized bodies use the HTTP status alone.
    failure = _failure(response.status_code, payload)
    # Preserve throttling instructions for gateway backoff without copying any
    # other headers. Existing consumers may read this optional exception field.
    failure.retry_after = response.headers.get("retry-after") or failure.retry_after
    log.info(
        "ChatGPT subscription request rejected: status=%s category=%s",
        failure.status,
        failure.code if failure.code in _REQUEST_FAILURES else "subscription_unavailable",
    )
    return failure


class SubscriptionReasoning:
    """Streaming text/image inference and function calls, without side effects."""

    def __init__(
        self,
        credentials: Callable[..., Awaitable[SubscriptionCredentials]],
        *,
        http_pool: Any = None,
        session_id: str = "",
    ) -> None:
        self._credentials = credentials
        self._pool = http_pool or HttpClientPool(timeout_s=60.0)
        self._session_id = session_id or str(uuid4())
        self._model_efforts: dict[str, tuple[str, ...]] = {}
        self._catalog_slugs: frozenset[str] = frozenset()

    def _headers(self, credentials: SubscriptionCredentials) -> dict[str, str]:
        return {
            "Authorization": f"Bearer {credentials.access_token}",
            "ChatGPT-Account-Id": credentials.account_id,
            "Accept": "text/event-stream",
            "Content-Type": "application/json",
            "originator": "personal_jarvis",
            "session_id": self._session_id,
        }

    async def list_models(self) -> list[dict[str, Any]]:
        import httpx

        from dodaios import __version__

        for attempt in range(2):
            credentials = await self._credentials(force_refresh=bool(attempt))
            try:
                response = await self._pool.client().get(
                    f"{_BASE_URL}/models",
                    params={"client_version": __version__},
                    headers={**self._headers(credentials), "Accept": "application/json"},
                )
            except httpx.HTTPError as exc:
                raise _failure() from exc
            if response.status_code == 401 and attempt == 0:
                continue
            if response.status_code != 200:
                raise await _http_failure(response)
            if len(response.content) > _MAX_CATALOG_BYTES:
                raise SubscriptionReasoningError("The ChatGPT model catalog is too large.")
            try:
                payload = response.json()
            except ValueError as exc:
                raise SubscriptionReasoningError("The ChatGPT model catalog is invalid.") from exc
            rows = payload.get("models") if isinstance(payload, dict) else None
            if not isinstance(rows, list):
                raise SubscriptionReasoningError("The ChatGPT model catalog is unavailable.")
            models: list[dict[str, Any]] = []
            model_efforts: dict[str, tuple[str, ...]] = {}
            # Hidden rows count too: a model the picker does not list may
            # still be one this account can use.
            self._catalog_slugs = frozenset(
                row["slug"]
                for row in rows
                if isinstance(row, dict) and isinstance(row.get("slug"), str)
            )
            for row in rows:
                if not isinstance(row, dict) or row.get("visibility", "list") != "list":
                    continue
                slug = row.get("slug")
                if not isinstance(slug, str) or not slug.strip():
                    continue
                model: dict[str, Any] = {
                    "id": slug,
                    "label": str(row.get("display_name") or slug),
                }
                for source, target in (
                    ("context_window", "context_length"),
                    ("max_output_tokens", "max_output_tokens"),
                ):
                    value = row.get(source)
                    if isinstance(value, int) and not isinstance(value, bool) and value > 0:
                        model[target] = value
                levels = row.get("supported_reasoning_levels")
                if isinstance(levels, list):
                    efforts: list[str] = []
                    for level in levels:
                        effort = level.get("effort") if isinstance(level, dict) else level
                        if (
                            isinstance(effort, str)
                            and _SAFE_EFFORT.fullmatch(effort)
                            and effort not in efforts
                        ):
                            efforts.append(effort)
                    model["efforts"] = efforts
                    model_efforts[slug] = tuple(efforts)
                default = row.get("default_reasoning_level")
                if (
                    isinstance(default, str)
                    and _SAFE_EFFORT.fullmatch(default)
                    and ("efforts" not in model or default in model["efforts"])
                ):
                    model["default_effort"] = default
                models.append(model)
            self._model_efforts = model_efforts
            return models
        raise _failure(401)

    async def stream(
        self,
        *,
        model: str,
        input: list[dict[str, Any]],  # noqa: A002 - matches the Responses wire field
        instructions: str,
        tools: list[dict[str, Any]],
        reasoning_effort: str = "",
    ) -> AsyncIterator[dict[str, Any]]:
        import httpx

        if not model.strip():
            raise _invalid_request(
                "Select a ChatGPT subscription thinking model before starting voice."
            )
        if any(tool.get("type") != "function" for tool in tools):
            raise _invalid_request("Subscription reasoning accepts only Dodaios function tools.")
        if reasoning_effort and not _SAFE_EFFORT.fullmatch(reasoning_effort):
            raise _invalid_request("The subscription thinking effort is invalid.")
        if reasoning_effort in {"none", "minimal"}:
            reasoning_effort = await self._lightest_effort(model, reasoning_effort)
        if reasoning_effort and reasoning_effort not in _KNOWN_EFFORTS:
            # New efforts are capability-driven. A model catalog request is free
            # of inference and happens only when an unknown effort needs checking.
            if model not in self._model_efforts:
                await self.list_models()
            if reasoning_effort not in self._model_efforts.get(model, ()):
                raise _invalid_request(
                    "The selected ChatGPT model does not advertise this thinking effort."
                )
        supported = self._model_efforts.get(model)
        if reasoning_effort and supported is not None and reasoning_effort not in supported:
            raise _invalid_request(
                "The selected ChatGPT model does not support this thinking effort."
            )
        reasoning = {"summary": "auto"}
        if reasoning_effort:
            reasoning["effort"] = reasoning_effort
        body = {
            "model": model,
            "input": input,
            "instructions": instructions,
            "tools": tools,
            "tool_choice": "auto",
            "parallel_tool_calls": True,
            "store": False,
            "stream": True,
            "include": ["reasoning.encrypted_content"],
            "reasoning": reasoning,
        }
        for attempt in range(2):
            credentials = await self._credentials(force_refresh=bool(attempt))
            try:
                async with self._pool.client().stream(
                    "POST",
                    f"{_BASE_URL}/responses",
                    json=body,
                    headers=self._headers(credentials),
                ) as response:
                    if response.status_code == 401 and attempt == 0:
                        continue
                    if response.status_code != 200:
                        failure = await _http_failure(response)
                        if (
                            failure.status in (400, 404)
                            and failure.code == "invalid_request_error"
                            and await self._unlisted(model)
                        ):
                            # The backend refuses a model the account no longer
                            # has with a bare 400. Live 2026-10-13: a model
                            # saved under a plan that had since ended.
                            failure = _failure(
                                failure.status, {"error": {"code": "model_not_found"}}
                            )
                        raise failure
                    async for event in self._events(response):
                        yield event
                    return
            except httpx.HTTPError as exc:
                # Once a stream has started, never replay the request automatically.
                raise _failure() from exc
        raise _failure(401)

    async def _unlisted(self, model: str) -> bool:
        """True when the account's own catalog answers and does not carry ``model``.

        One catalog request, no inference, and only after a request was
        already refused.
        """
        try:
            await self.list_models()
        except (SubscriptionReasoningError, SubscriptionAuthError):  # the given refusal stands
            # No catalog, or the login went away meanwhile: the refusal the
            # backend already gave is the one reported.
            return False
        return bool(self._catalog_slugs) and model not in self._catalog_slugs

    async def snap_effort(self, model: str, requested: str) -> str:
        """The level ``model`` offers closest to ``requested`` ("" = its default).

        A runtime asks for the level its own settings name; a ChatGPT model
        offers only some (no ``max`` on one, no ``none`` on another). A level
        the model lacks becomes the highest one it offers below it, else its
        lightest, so the request is never refused for the level alone. Unknown
        capabilities (the catalog is unreachable) keep a known level as is and
        drop an unknown one to the model's default. One catalog request at
        most, no inference.
        """
        if not requested:
            return ""
        if model not in self._model_efforts:
            try:
                await self.list_models()
            except SubscriptionReasoningError:  # no catalog: the backend validates the level
                return requested if requested in _KNOWN_EFFORTS else ""
        supported = self._model_efforts.get(model, ())
        if not supported:
            return requested if requested in _KNOWN_EFFORTS else ""
        if requested in supported:
            return requested
        if requested in _LIGHTEST_FIRST:
            ceiling = _LIGHTEST_FIRST.index(requested)
            lower = [e for e in _LIGHTEST_FIRST[: ceiling + 1] if e in supported]
            if lower:
                return lower[-1]
        return next((e for e in _LIGHTEST_FIRST if e in supported), "")

    async def _lightest_effort(self, model: str, requested: str) -> str:
        """``requested`` when the model offers it, else its lightest level ("" = default)."""
        if model not in self._model_efforts:
            try:
                await self.list_models()
            except SubscriptionReasoningError:
                # Unknown capabilities: the model's own default is always valid.
                return ""
        supported = self._model_efforts.get(model, ())
        if requested in supported:
            return requested
        start = _LIGHTEST_FIRST.index(requested)
        return next((e for e in _LIGHTEST_FIRST[start:] if e in supported), "")

    async def _events(self, response: Any) -> AsyncIterator[dict[str, Any]]:
        parts: list[str] = []
        size = 0
        completed = False
        async for line in response.aiter_lines():
            if line.startswith("data:"):
                chunk = line[5:].lstrip()
                size += len(chunk)
                if size > _MAX_EVENT_CHARS:
                    raise SubscriptionReasoningError(
                        "ChatGPT returned an oversized reasoning event."
                    )
                parts.append(chunk)
            elif not line and parts:
                raw = "\n".join(parts)
                parts, size = [], 0
                if raw == "[DONE]":
                    break
                try:
                    event = json.loads(raw)
                except ValueError as exc:
                    raise SubscriptionReasoningError(
                        "ChatGPT returned an invalid reasoning event."
                    ) from exc
                if not isinstance(event, dict) or not isinstance(event.get("type"), str):
                    raise SubscriptionReasoningError("ChatGPT returned an invalid reasoning event.")
                kind = event["type"]
                if kind in {"error", "response.failed", "response.incomplete"}:
                    raise _stream_failure(kind, event)
                if kind == "response.completed":
                    result = event.get("response", {})
                    if (
                        not isinstance(result, dict)
                        or result.get("status", "completed") != "completed"
                    ):
                        raise _stream_failure("response.completed", {"response": result})
                    completed = True
                yield event
                if completed:
                    return
        if not completed:
            raise SubscriptionReasoningError(
                "ChatGPT reasoning ended before completion. The request was not retried."
            )

    async def aclose(self) -> None:
        await self._pool.aclose()


def _request_input(request: BrainRequest) -> tuple[str, list[dict[str, Any]]]:
    instructions = [request.system] if request.system else []
    items: list[dict[str, Any]] = []
    for message in request.messages:
        if message.role == "system":
            if isinstance(message.content, str):
                instructions.append(message.content)
            continue
        if message.role == "tool":
            if not message.tool_call_id:
                raise SubscriptionReasoningError("A tool result is missing its call identity.")
            items.append(
                {
                    "type": "function_call_output",
                    "call_id": message.tool_call_id,
                    "output": message.content
                    if isinstance(message.content, str)
                    else json.dumps(message.content),
                }
            )
            continue
        blocks: list[dict[str, Any]] = []
        text_type = "output_text" if message.role == "assistant" else "input_text"
        if isinstance(message.content, str):
            if message.content:
                blocks.append({"type": text_type, "text": message.content})
        else:
            for block in message.content:
                kind = block.get("type")
                if kind in {"text", "input_text", "output_text"}:
                    blocks.append({"type": text_type, "text": block.get("text", "")})
                elif kind in {"input_image", "image_url"} and message.role == "user":
                    image = block.get("image_url", "")
                    if isinstance(image, dict):
                        image = image.get("url", "")
                    if image:
                        blocks.append({"type": "input_image", "image_url": image})
                elif kind == "tool_use":
                    items.append(
                        {
                            "type": "function_call",
                            "call_id": block.get("id", ""),
                            "name": block.get("name", ""),
                            "arguments": json.dumps(block.get("input", {})),
                        }
                    )
                elif kind in {"reasoning", "function_call"}:
                    items.append(dict(block))
                else:
                    raise SubscriptionReasoningError(
                        "This message contains an unsupported subscription input."
                    )
        for image in message.images:
            if message.role != "user":
                raise SubscriptionReasoningError(
                    "Subscription images must belong to a user observation."
                )
            blocks.append(
                {"type": "input_image", "image_url": f"data:{image.mime};base64,{image.data_b64}"}
            )
        if blocks:
            items.append({"role": message.role, "content": blocks})
    return "\n\n".join(instructions), items


class SubscriptionReasoningBrain:
    """Per-operation Brain adapter for image analysis and Jarvis computer tools."""

    supports_vision = True
    supports_tools = True
    context_window = 128_000
    name = "openai-chatgpt-subscription"

    def __init__(self, reasoning: SubscriptionReasoning, model: str) -> None:
        self._reasoning = reasoning
        self._model = model

    def estimate_cost(self, req: BrainRequest) -> float:
        """No metered API-key billing; subscription allowance is reported separately."""
        return 0.0

    async def complete(self, req: BrainRequest) -> AsyncIterator[BrainDelta]:
        instructions, items = _request_input(req)
        tools: list[dict[str, Any]] = []
        for declaration in req.tools:
            if "function" in declaration:
                tools.append({"type": "function", **declaration["function"]})
            elif declaration.get("type") == "function":
                tools.append(dict(declaration))
            elif "name" in declaration and "input_schema" in declaration:
                tools.append(
                    {
                        "type": "function",
                        "name": declaration["name"],
                        "description": declaration.get("description", ""),
                        "parameters": declaration["input_schema"],
                    }
                )
            else:
                raise SubscriptionReasoningError(
                    "Subscription reasoning accepts only Dodaios function tools."
                )
        calls: list[dict[str, Any]] = []
        seen_calls: set[str] = set()
        usage: dict[str, int] | None = None
        async for event in self._reasoning.stream(
            model=self._model,
            input=items,
            instructions=instructions,
            tools=tools,
            reasoning_effort=req.reasoning_effort or "",
        ):
            kind = event["type"]
            if kind == "response.output_text.delta" and event.get("delta"):
                yield BrainDelta(content=str(event["delta"]))
            elif kind == "response.output_item.done":
                item = event.get("item", {})
                if item.get("type") != "function_call":
                    continue
                call_id = item.get("call_id")
                if not isinstance(call_id, str) or not call_id:
                    raise SubscriptionReasoningError(
                        "ChatGPT returned a tool call without an identity."
                    )
                if call_id in seen_calls:
                    continue
                try:
                    arguments = json.loads(item.get("arguments") or "{}")
                except ValueError as exc:
                    raise SubscriptionReasoningError(
                        "ChatGPT returned invalid tool arguments."
                    ) from exc
                if not isinstance(arguments, dict):
                    raise SubscriptionReasoningError("ChatGPT returned invalid tool arguments.")
                seen_calls.add(call_id)
                calls.append({"id": call_id, "name": item.get("name", ""), "input": arguments})
            elif kind == "response.completed":
                raw = event.get("response", {}).get("usage")
                if isinstance(raw, dict):
                    cached = int((raw.get("input_tokens_details") or {}).get("cached_tokens", 0))
                    usage = {
                        "input_tokens": max(0, int(raw.get("input_tokens", 0)) - cached),
                        "output_tokens": int(raw.get("output_tokens", 0)),
                        "cache_hit_tokens": cached,
                    }
        # BrainDelta has no reasoning-summary field: raw stream consumers receive
        # those events, while Brain callers receive only answer text and tools.
        for call in calls:
            yield BrainDelta(tool_call=call)
        yield BrainDelta(finish_reason="tool_calls" if calls else "stop")
        if usage is not None:
            yield BrainDelta(usage=usage)