Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
Refactor OpenAI Responses into modular adapter facade (#847)
Ali Khokhar8d2e5b95f793
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex b7eafc52..20c7c91f 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -67,6 +67,19 @@ The main ownership rule is that shared Anthropic and Responses protocol behaviorbelongs in [core/](core/). Provider modules should use neutral helpers ratherthan importing behavior from another provider-specific module.+## Customer-Facing Contract++FCC optimizes for installed user workflows, not internal compatibility. The+behavior that must be preserved is that `fcc-server`, `fcc-cloud`, `fcc-claude`,+`fcc-codex`, Claude Code, Codex, and configured messaging bridges run correctly+for real prompts against supported providers.++Internal modules, class designs, helper APIs, route implementations, and tests+are not stable contracts. Refactors may replace or remove them when doing so+simplifies the system, improves correctness, or better matches these+architecture boundaries. When tests primarily encode an obsolete internal shape,+update the tests to assert the customer-facing behavior instead.+## Design Pressure And Refactor TargetsThe current package boundaries are intentional, but several modules still carry@@ -215,11 +228,12 @@ sequenceDiagramProvider-->>Client: Anthropic SSE events```-OpenAI Responses uses the same core route. `create_response()` converts the-Responses payload into an Anthropic Messages payload with-[core/openai_responses/conversion.py](core/openai_responses/conversion.py),-reuses `create_message()`, then converts the final response or SSE stream back to-Responses format.+OpenAI Responses uses the same core route. `create_response()` delegates+Responses protocol work to the `OpenAIResponsesAdapter` in+[core/openai_responses/adapter.py](core/openai_responses/adapter.py). The adapter+converts the Responses payload into an Anthropic Messages payload, lets the+service reuse `create_message()`, then converts the final response or SSE stream+back to Responses format.## Model Routing@@ -334,11 +348,32 @@ hooks, client setup, and model listing.[core/openai_responses/](core/openai_responses/) owns OpenAI Responses support:+- the `OpenAIResponsesAdapter` facade used by the API layer;+- streaming-only `/v1/responses` support for Codex/FCC workflows;- Responses request conversion into Anthropic Messages payloads;-- Anthropic message response conversion into Responses objects;- Anthropic SSE conversion into Responses SSE;- OpenAI-compatible error envelopes.+The package intentionally does not implement the full OpenAI Responses surface.+FCC accepts omitted `stream` or `stream: true`; `stream: false` is rejected with+an OpenAI-shaped client error because installed FCC/Codex workflows only need+streaming. Request conversion, stream transformation, Anthropic SSE parsing,+Responses SSE event formatting, output item construction, tool identity mapping,+reasoning mapping, ID generation, and error envelope construction each live+behind the adapter boundary. `stream.py` is the public streaming entrypoint;+`stream_state.py` owns the block-indexed Responses output ledger used to+preserve streamed item order, pending block finalization, and terminal response+lifecycle events. API code should depend on the adapter, not on those internal+module owners directly. Responses output payloads stay OpenAI-shaped; Anthropic+terminal metadata is used internally only when it affects streamed behavior.++Responses custom tools are also boundary-owned. The adapter accepts native+Responses `custom` tool declarations, represents them internally as Anthropic+tools with a single string `input` field, and restores `custom_tool_call`,+`custom_tool_call_output`, and `response.custom_tool_call_input.*` shapes at the+Responses edge. Text or grammar format metadata is preserved as model guidance;+FCC does not validate custom-tool grammars.+Responses reasoning is handled as protocol conversion, not provider policy.`reasoning.effort = "none"` converts to a disabled Anthropic `thinking`request; any other explicit Responses reasoning request enables Anthropicdiff --git a/api/services.py b/api/services.pyindex df1985c4..d1d0ac81 100644--- a/api/services.py+++ b/api/services.py@@ -10,22 +10,12 @@ from typing import Anyfrom fastapi import HTTPExceptionfrom fastapi.responses import JSONResponse, StreamingResponsefrom loguru import logger-from pydantic import BaseModelfrom config.provider_catalog import PROVIDER_CATALOGfrom config.settings import Settingsfrom core.anthropic import get_token_count, get_user_facing_error_messagefrom core.anthropic.sse import ANTHROPIC_SSE_RESPONSE_HEADERS-from core.openai_responses import (- OPENAI_RESPONSES_SSE_HEADERS,- ResponsesConversionError,- anthropic_message_response_to_openai_response,- collect_openai_response_from_anthropic_sse,- iter_anthropic_sse_as_openai_responses,- iter_message_response_as_openai_responses,- openai_error_payload,- responses_request_to_anthropic_payload,-)+from core.openai_responses import OpenAIResponsesAdapterfrom core.trace import api_messages_request_snapshot, trace_event, traced_async_streamfrom providers.base import BaseProviderfrom providers.exceptions import InvalidRequestError, ProviderError@@ -72,7 +62,7 @@ def openai_responses_sse_streaming_response(return StreamingResponse(body,media_type="text/event-stream",- headers=OPENAI_RESPONSES_SSE_HEADERS,+ headers=OpenAIResponsesAdapter.sse_headers,)@@ -121,11 +111,13 @@ class ClaudeProxyService:provider_getter: ProviderGetter,model_router: ModelRouter | None = None,token_counter: TokenCounter = get_token_count,+ responses_adapter: OpenAIResponsesAdapter | None = None,):self._settings = settingsself._provider_getter = provider_getterself._model_router = model_router or ModelRouter(settings)self._token_counter = token_counter+ self._responses_adapter = responses_adapter or OpenAIResponsesAdapter()def create_message(self, request_data: MessagesRequest) -> object:"""Create a message response or streaming response."""@@ -250,14 +242,104 @@ class ClaudeProxyService:"""Create an OpenAI Responses-compatible response through the provider router."""request_payload = request_data.model_dump(mode="json", exclude_none=True)+ if request_data.stream is False:+ invalid_request = InvalidRequestError(+ "FCC /v1/responses supports streaming only; omit stream or set stream=true."+ )+ return JSONResponse(+ status_code=invalid_request.status_code,+ content=self._responses_adapter.error_payload(+ message=invalid_request.message,+ error_type=invalid_request.error_type,+ ),+ )+try:- anthropic_payload = responses_request_to_anthropic_payload(request_payload)- result = self.create_message(MessagesRequest(**anthropic_payload))- except ResponsesConversionError as exc:+ anthropic_payload = self._responses_adapter.to_anthropic_payload(+ request_payload+ )+ response_request = MessagesRequest(**anthropic_payload)+ _require_non_empty_messages(response_request.messages)+ routed = self._model_router.resolve_messages_request(response_request)++ if routed.resolved.provider_id in _OPENAI_CHAT_UPSTREAM_IDS:+ tool_err = openai_chat_upstream_server_tool_error(+ routed.request,+ web_tools_enabled=self._settings.enable_web_server_tools,+ )+ if tool_err is not None:+ raise InvalidRequestError(tool_err)++ provider = self._provider_getter(routed.resolved.provider_id)+ provider.preflight_stream(+ routed.request,+ thinking_enabled=routed.resolved.thinking_enabled,+ )++ trace_event(+ stage="routing",+ event="api.route.resolved",+ source="api",+ provider_id=routed.resolved.provider_id,+ provider_model=routed.resolved.provider_model,+ provider_model_ref=routed.resolved.provider_model_ref,+ gateway_model=routed.request.model,+ thinking_enabled=routed.resolved.thinking_enabled,+ wire_api="responses",+ )++ request_id = f"req_{uuid.uuid4().hex[:12]}"+ with logger.contextualize(request_id=request_id):+ trace_event(+ stage="ingress",+ event="api.responses.request.received",+ source="api",+ message_count=len(routed.request.messages),+ snapshot=api_messages_request_snapshot(routed.request),+ )++ if self._settings.log_raw_api_payloads:+ logger.debug(+ "FULL_RESPONSES_PAYLOAD [{}]: {}",+ request_id,+ request_payload,+ )++ input_tokens = self._token_counter(+ routed.request.messages,+ routed.request.system,+ routed.request.tools,+ )++ streamed = traced_async_stream(+ provider.stream_response(+ routed.request,+ input_tokens=input_tokens,+ request_id=request_id,+ thinking_enabled=routed.resolved.thinking_enabled,+ ),+ stage="egress",+ source="api",+ complete_event="api.responses.stream_completed",+ interrupted_event="api.responses.stream_interrupted",+ chunk_event=None,+ extra={+ "request_id": request_id,+ "provider_id": routed.resolved.provider_id,+ "gateway_model": routed.request.model,+ },+ )+ return openai_responses_sse_streaming_response(+ self._responses_adapter.iter_sse_from_anthropic(+ streamed,+ request_payload,+ )+ )+ except OpenAIResponsesAdapter.ConversionError as exc:invalid_request = InvalidRequestError(str(exc))return JSONResponse(status_code=invalid_request.status_code,- content=openai_error_payload(+ content=self._responses_adapter.error_payload(message=invalid_request.message,error_type=invalid_request.error_type,),@@ -265,39 +347,24 @@ class ClaudeProxyService:except ProviderError as exc:return JSONResponse(status_code=exc.status_code,- content=openai_error_payload(+ content=self._responses_adapter.error_payload(message=exc.message,error_type=exc.error_type,),)-- if request_data.stream is False:- if isinstance(result, StreamingResponse):- return await collect_openai_response_from_anthropic_sse(- result.body_iterator,- request_payload,- )- return anthropic_message_response_to_openai_response(- _model_dump_json(result),- request_payload,- )-- if isinstance(result, StreamingResponse):- return openai_responses_sse_streaming_response(- iter_anthropic_sse_as_openai_responses(- result.body_iterator,- request_payload,- )+ except Exception as e:+ _log_unexpected_service_exception(+ self._settings,+ e,+ context="CREATE_RESPONSE_ERROR",)-- return openai_responses_sse_streaming_response(- _iter_static_response_sse(- iter_message_response_as_openai_responses(- _model_dump_json(result),- request_payload,- )+ return JSONResponse(+ status_code=_http_status_for_unexpected_service_exception(e),+ content=self._responses_adapter.error_payload(+ message=get_user_facing_error_message(e),+ error_type="api_error",+ ),)- )def count_tokens(self, request_data: TokenCountRequest) -> TokenCountResponse:"""Count tokens for a request after applying configured model routing."""@@ -341,17 +408,3 @@ class ClaudeProxyService:status_code=_http_status_for_unexpected_service_exception(e),detail=get_user_facing_error_message(e),) from e---def _model_dump_json(value: object) -> dict[str, Any]:- if isinstance(value, BaseModel):- dumped = value.model_dump(mode="json")- return dumped if isinstance(dumped, dict) else {}- if isinstance(value, dict):- return {str(key): item for key, item in value.items()}- return {}---async def _iter_static_response_sse(chunks: list[str]) -> AsyncIterator[str]:- for chunk in chunks:- yield chunkdiff --git a/cli/adapters/base.py b/cli/adapters/base.pyindex bfea80a8..b6452dec 100644--- a/cli/adapters/base.py+++ b/cli/adapters/base.py@@ -32,6 +32,9 @@ class CliParseState:log_raw_cli_diagnostics: bool = Falsesession_id_extracted: bool = False+ responses_seen_output_keys: set[str] = field(default_factory=set)+ responses_current_output_scope: str | None = None+ responses_next_implicit_output_scope: int = 0class ClientCliAdapter(Protocol):diff --git a/cli/adapters/codex.py b/cli/adapters/codex.pyindex 25d8adad..d123357a 100644--- a/cli/adapters/codex.py+++ b/cli/adapters/codex.py@@ -4,7 +4,7 @@ from __future__ import annotationsimport jsonfrom collections.abc import Iterable, Mapping-from typing import Any+from typing import Any, castfrom loguru import logger@@ -101,10 +101,12 @@ class CodexCliAdapter:logger.info("Extracted Codex session ID: {}", extracted_id)yield {"type": "session_info", "session_id": extracted_id}- mapped = list(_codex_event_to_parser_events(event))+ mapped = list(_codex_event_to_parser_events(event, state))if mapped:yield from mappedreturn+ if event.get("type") in {"response.output_item.done", "response.completed"}:+ returnyield {"type": "raw", "content": line}@@ -240,12 +242,19 @@ class CodexCliAdapter:return args-def _codex_event_to_parser_events(event: dict[str, Any]) -> Iterable[dict[str, Any]]:+def _codex_event_to_parser_events(+ event: dict[str, Any], state: CliParseState+) -> Iterable[dict[str, Any]]:event_type = event.get("type")if event_type in {"error", "turn.failed"}:yield {"type": "error", "error": {"message": _event_message(event)}}return+ if event_type == "response.failed":+ _finish_response_scope(state)+ yield {"type": "error", "error": {"message": _event_message(event)}}+ returnif event_type == "response.output_text.delta":+ _mark_streamed_message_item_seen(event, state)yield {"type": "content_block_delta","index": 0,@@ -272,16 +281,31 @@ def _codex_event_to_parser_events(event: dict[str, Any]) -> Iterable[dict[str, Areturnif event_type == "response.output_item.done":item = event.get("item")- if isinstance(item, dict):+ if isinstance(item, dict) and _mark_response_item_unseen(+ item,+ state,+ response_scope=_response_scope(event, state),+ output_index=_optional_event_output_index(event),+ ):yield from _responses_item_to_parser_events(item)returnif event_type == "response.completed":response = event.get("response")output = response.get("output") if isinstance(response, dict) else None+ response_scope = _response_scope(event, state)if isinstance(output, list):- for item in output:- if isinstance(item, dict):- yield from _responses_item_to_parser_events(item)+ for output_index, item in enumerate(output):+ if not isinstance(item, dict):+ continue+ item_mapping = cast(Mapping[str, Any], item)+ if _mark_response_item_unseen(+ item_mapping,+ state,+ response_scope=response_scope,+ output_index=output_index,+ ):+ yield from _responses_item_to_parser_events(item_mapping)+ _finish_response_scope(state)return@@ -319,6 +343,21 @@ def _responses_item_to_parser_events(]},}+ return+ if item_type == "custom_tool_call":+ yield {+ "type": "assistant",+ "message": {+ "content": [+ {+ "type": "tool_use",+ "id": str(item.get("call_id", item.get("id", ""))),+ "name": str(item.get("name", "")),+ "input": {"input": _custom_tool_input_text(item.get("input"))},+ }+ ]+ },+ }def _event_message(event: Mapping[str, Any]) -> str:@@ -328,6 +367,11 @@ def _event_message(event: Mapping[str, Any]) -> str:return valueif isinstance(value, dict) and isinstance(value.get("message"), str):return str(value["message"])+ response = event.get("response")+ if isinstance(response, dict):+ error = response.get("error")+ if isinstance(error, dict) and isinstance(error.get("message"), str):+ return str(error["message"])return ""@@ -336,6 +380,104 @@ def _event_output_index(event: Mapping[str, Any]) -> int:return value if isinstance(value, int) else 0+def _optional_event_output_index(event: Mapping[str, Any]) -> int | None:+ value = event.get("output_index")+ return value if isinstance(value, int) else None+++def _mark_response_item_unseen(+ item: Mapping[str, Any],+ state: CliParseState,+ *,+ response_scope: str | None = None,+ output_index: int | None = None,+) -> bool:+ item_key = _response_item_key(+ item,+ response_scope=response_scope,+ output_index=output_index,+ )+ if item_key is None:+ return True+ if item_key in state.responses_seen_output_keys:+ return False+ state.responses_seen_output_keys.add(item_key)+ return True+++def _mark_streamed_message_item_seen(+ event: Mapping[str, Any], state: CliParseState+) -> None:+ item_id = event.get("item_id")+ if isinstance(item_id, str) and item_id:+ state.responses_seen_output_keys.add(f"message:{item_id}")+ output_index = _optional_event_output_index(event)+ if output_index is not None:+ state.responses_seen_output_keys.add(+ _response_output_index_key(+ "message",+ _response_scope(event, state),+ output_index,+ )+ )+++def _response_item_key(+ item: Mapping[str, Any],+ *,+ response_scope: str | None = None,+ output_index: int | None = None,+) -> str | None:+ item_type = item.get("type")+ if (+ item_type == "message"+ and response_scope is not None+ and output_index is not None+ ):+ return _response_output_index_key(item_type, response_scope, output_index)+ for key in ("id", "call_id"):+ value = item.get(key)+ if isinstance(value, str) and value:+ return f"{item_type}:{value}"+ return None+++def _response_scope(event: Mapping[str, Any], state: CliParseState) -> str:+ if state.responses_current_output_scope is not None:+ return state.responses_current_output_scope+ response_id = _event_response_id(event)+ if response_id is not None:+ scope = f"response:{response_id}"+ else:+ scope = f"implicit:{state.responses_next_implicit_output_scope}"+ state.responses_next_implicit_output_scope += 1+ state.responses_current_output_scope = scope+ return scope+++def _finish_response_scope(state: CliParseState) -> None:+ state.responses_current_output_scope = None+++def _event_response_id(event: Mapping[str, Any]) -> str | None:+ for key in ("response_id", "responseId"):+ value = event.get(key)+ if isinstance(value, str) and value:+ return value+ response = event.get("response")+ if isinstance(response, dict):+ value = response.get("id")+ if isinstance(value, str) and value:+ return value+ return None+++def _response_output_index_key(+ item_type: object, response_scope: str, output_index: int+) -> str:+ return f"{item_type}_output_index:{response_scope}:{output_index}"++def _safe_json_object(value: Any) -> dict[str, Any]:if isinstance(value, dict):return value@@ -348,6 +490,17 @@ def _safe_json_object(value: Any) -> dict[str, Any]:return parsed if isinstance(parsed, dict) else {}+def _custom_tool_input_text(value: Any) -> str:+ if value is None:+ return ""+ if isinstance(value, str):+ return value+ try:+ return json.dumps(value)+ except TypeError:+ return str(value)++def _base_codex_env(base_env: Mapping[str, str]) -> dict[str, str]:return {key: valuediff --git a/core/openai_responses/__init__.py b/core/openai_responses/__init__.pyindex 2c1aa58b..524caf07 100644--- a/core/openai_responses/__init__.py+++ b/core/openai_responses/__init__.py@@ -1,27 +1,5 @@-"""OpenAI Responses compatibility helpers."""+"""OpenAI Responses protocol adapter."""-from .conversion import (- ResponsesConversionError,- anthropic_message_response_to_openai_response,- openai_error_payload,- responses_request_to_anthropic_payload,-)-from .sse import (- OPENAI_RESPONSES_SSE_HEADERS,- collect_openai_response_from_anthropic_sse,- format_response_sse_event,- iter_anthropic_sse_as_openai_responses,- iter_message_response_as_openai_responses,-)+from .adapter import OpenAIResponsesAdapter-__all__ = [- "OPENAI_RESPONSES_SSE_HEADERS",- "ResponsesConversionError",- "anthropic_message_response_to_openai_response",- "collect_openai_response_from_anthropic_sse",- "format_response_sse_event",- "iter_anthropic_sse_as_openai_responses",- "iter_message_response_as_openai_responses",- "openai_error_payload",- "responses_request_to_anthropic_payload",-]+__all__ = ["OpenAIResponsesAdapter"]diff --git a/core/openai_responses/adapter.py b/core/openai_responses/adapter.pynew file mode 100644index 00000000..1b73ea90--- /dev/null+++ b/core/openai_responses/adapter.py@@ -0,0 +1,31 @@+"""Facade for OpenAI Responses protocol adaptation."""++from __future__ import annotations++from collections.abc import AsyncIterable, AsyncIterator, Mapping+from typing import Any, ClassVar++from .errors import ResponsesConversionError, openai_error_payload+from .events import OPENAI_RESPONSES_SSE_HEADERS+from .input import convert_request_to_anthropic_payload+from .stream import iter_responses_sse_from_anthropic+++class OpenAIResponsesAdapter:+ """Convert between OpenAI Responses and the proxy's Anthropic core path."""++ ConversionError: ClassVar[type[ResponsesConversionError]] = ResponsesConversionError+ sse_headers: ClassVar[dict[str, str]] = OPENAI_RESPONSES_SSE_HEADERS++ def to_anthropic_payload(self, request: Mapping[str, Any]) -> dict[str, Any]:+ return convert_request_to_anthropic_payload(request)++ def iter_sse_from_anthropic(+ self,+ chunks: AsyncIterable[Any],+ request: Mapping[str, Any],+ ) -> AsyncIterator[str]:+ return iter_responses_sse_from_anthropic(chunks, request)++ def error_payload(self, *, message: str, error_type: str) -> dict[str, Any]:+ return openai_error_payload(message=message, error_type=error_type)diff --git a/core/openai_responses/anthropic_sse.py b/core/openai_responses/anthropic_sse.pynew file mode 100644index 00000000..4233980d--- /dev/null+++ b/core/openai_responses/anthropic_sse.py@@ -0,0 +1,59 @@+"""Anthropic SSE parsing used by the Responses stream adapter."""++from __future__ import annotations++import json+from collections.abc import AsyncIterable, AsyncIterator+from dataclasses import dataclass+from typing import Any+++@dataclass(slots=True)+class AnthropicSseEvent:+ event: str+ data: dict[str, Any]+++async def iter_sse_events(+ chunks: AsyncIterable[Any],+) -> AsyncIterator[AnthropicSseEvent]:+ buffer = ""+ async for chunk in chunks:+ if isinstance(chunk, bytes):+ buffer += chunk.decode("utf-8", errors="replace")+ else:+ buffer += str(chunk)++ while "\n\n" in buffer:+ raw, buffer = buffer.split("\n\n", 1)+ event = parse_sse_event(raw)+ if event is not None:+ yield event++ if buffer.strip():+ event = parse_sse_event(buffer)+ if event is not None:+ yield event+++def parse_sse_event(raw: str) -> AnthropicSseEvent | None:+ event_type = ""+ data_parts: list[str] = []+ for line in raw.splitlines():+ stripped = line.rstrip("\r")+ if stripped.startswith("event:"):+ event_type = stripped.split(":", 1)[1].strip()+ elif stripped.startswith("data:"):+ data_parts.append(stripped.split(":", 1)[1].strip())+ if not event_type and not data_parts:+ return None+ data_text = "\n".join(data_parts)+ if data_text == "[DONE]":+ return None+ try:+ parsed = json.loads(data_text) if data_text else {}+ except json.JSONDecodeError:+ parsed = {"raw": data_text}+ if not isinstance(parsed, dict):+ parsed = {"value": parsed}+ return AnthropicSseEvent(event=event_type, data=parsed)diff --git a/core/openai_responses/conversion.py b/core/openai_responses/conversion.pydeleted file mode 100644index 31684e60..00000000--- a/core/openai_responses/conversion.py+++ /dev/null@@ -1,672 +0,0 @@-"""Convert between OpenAI Responses payloads and Anthropic-style payloads."""--from __future__ import annotations--import hashlib-import json-import re-import time-import uuid-from collections.abc import Mapping-from typing import Any--_MAX_ANTHROPIC_TOOL_NAME_LEN = 64-_NAMESPACE_TOOL_SEPARATOR = "__"-_UNSUPPORTED_PASSIVE_TOOL_TYPES = frozenset({"web_search", "image_generation"})-_INVALID_TOOL_NAME_CHARS = re.compile(r"[^A-Za-z0-9_-]+")---class ResponsesConversionError(ValueError):- """Raised when a Responses request cannot be converted deterministically."""---def responses_request_to_anthropic_payload(- request: Mapping[str, Any],-) -> dict[str, Any]:- """Convert an OpenAI Responses request into an Anthropic Messages payload."""-- system_parts: list[str] = []- if instructions := _optional_str(request.get("instructions")):- system_parts.append(instructions)-- messages: list[dict[str, Any]] = []- pending_reasoning: str | None = None- for item in _iter_input_items(request.get("input")):- pending_reasoning = _append_input_item(- item,- messages=messages,- system_parts=system_parts,- pending_reasoning=pending_reasoning,- )- _append_pending_reasoning(messages, pending_reasoning)-- if not messages:- raise ResponsesConversionError("Responses request input must contain a message")-- payload: dict[str, Any] = {- "model": _required_str(request.get("model"), "model"),- "messages": messages,- "stream": True,- }- if system_parts:- payload["system"] = "\n\n".join(system_parts)- _copy_if_present(request, payload, "temperature")- _copy_if_present(request, payload, "top_p")- if request.get("max_output_tokens") is not None:- payload["max_tokens"] = request["max_output_tokens"]- if isinstance(request.get("metadata"), dict):- payload["metadata"] = request["metadata"]-- if thinking := _responses_reasoning_to_thinking(request.get("reasoning")):- payload["thinking"] = thinking-- raw_tool_choice = request.get("tool_choice")- tools = _convert_tools(request.get("tools"))- if tools and raw_tool_choice != "none":- payload["tools"] = tools- tool_choice = _convert_tool_choice(raw_tool_choice)- if tool_choice is not None:- payload["tool_choice"] = tool_choice-- return payload---def anthropic_message_response_to_openai_response(- message: Mapping[str, Any],- request: Mapping[str, Any],- *,- response_id: str | None = None,- status: str = "completed",-) -> dict[str, Any]:- """Convert a complete Anthropic message response into a Responses object."""-- response_id = response_id or _new_response_id()- output: list[dict[str, Any]] = []- text_parts: list[str] = []-- def flush_text() -> None:- text = "".join(text_parts)- text_parts.clear()- if text:- output.append(_message_item(_new_message_item_id(), text, "completed"))-- for block in _message_content_blocks(message):- block_type = block.get("type")- if block_type == "text":- text_parts.append(str(block.get("text", "")))- elif block_type == "thinking":- flush_text()- output.append(- _reasoning_item(- _new_reasoning_item_id(),- str(block.get("thinking", "")),- "completed",- )- )- elif block_type == "redacted_thinking":- flush_text()- output.append(- _encrypted_reasoning_item(- _new_reasoning_item_id(),- str(block.get("data", "")),- "completed",- )- )- elif block_type == "tool_use":- flush_text()- namespace, name = responses_tool_identity_from_anthropic_name(- request, str(block.get("name", ""))- )- output.append(- _function_call_item(- block_id=str(block.get("id", "") or _new_call_id()),- name=name,- namespace=namespace,- arguments=json.dumps(block.get("input") or {}),- status="completed",- )- )- flush_text()-- return {- "id": response_id,- "object": "response",- "created_at": int(time.time()),- "status": status,- "model": str(request.get("model", message.get("model", ""))),- "output": output,- "parallel_tool_calls": bool(request.get("parallel_tool_calls", True)),- "tool_choice": request.get("tool_choice", "auto"),- "temperature": request.get("temperature"),- "top_p": request.get("top_p"),- "max_output_tokens": request.get("max_output_tokens"),- "usage": _openai_usage(message.get("usage")),- "error": None if status == "completed" else {},- }---def openai_error_payload(*, message: str, error_type: str) -> dict[str, Any]:- """Return an OpenAI-compatible error envelope."""-- return {- "error": {- "message": message,- "type": error_type,- "param": None,- "code": None,- }- }---def _append_input_item(- item: Any,- *,- messages: list[dict[str, Any]],- system_parts: list[str],- pending_reasoning: str | None,-) -> str | None:- if isinstance(item, str):- _append_pending_reasoning(messages, pending_reasoning)- messages.append({"role": "user", "content": item})- return None- if not isinstance(item, dict):- raise ResponsesConversionError(- f"Unsupported Responses input item: {type(item).__name__}"- )-- item_type = item.get("type")- if item_type in (None, "message") or "role" in item:- role = _required_str(item.get("role", "user"), "input.role")- if role == "assistant":- _append_message_item(- role,- item.get("content", ""),- messages,- system_parts,- reasoning_content=pending_reasoning,- )- return None- _append_pending_reasoning(messages, pending_reasoning)- _append_message_item(role, item.get("content", ""), messages, system_parts)- return None- if item_type == "function_call":- namespace = _optional_str(item.get("namespace"))- name = _required_str(item.get("name"), "function_call.name")- message = {- "role": "assistant",- "content": [- {- "type": "tool_use",- "id": _call_id_from_item(item),- "name": responses_tool_name_to_anthropic_name(- name, namespace=namespace- ),- "input": _parse_arguments(item.get("arguments")),- }- ],- }- if pending_reasoning:- message["reasoning_content"] = pending_reasoning- messages.append(message)- return None- if item_type == "function_call_output":- _append_pending_reasoning(messages, pending_reasoning)- messages.append(- {- "role": "user",- "content": [- {- "type": "tool_result",- "tool_use_id": _call_id_from_item(item),- "content": item.get("output", ""),- }- ],- }- )- return None- if item_type == "reasoning":- return _combine_reasoning(pending_reasoning, _reasoning_text_from_item(item))- if item_type in {"input_text", "output_text", "text"}:- _append_pending_reasoning(messages, pending_reasoning)- messages.append({"role": "user", "content": _text_from_part(item)})- return None-- raise ResponsesConversionError(- f"Unsupported Responses input item type: {item_type!r}"- )---def _append_message_item(- role: str,- content: Any,- messages: list[dict[str, Any]],- system_parts: list[str],- *,- reasoning_content: str | None = None,-) -> None:- normalized_role = "system" if role == "developer" else role- if normalized_role == "system":- text = _content_as_text(content)- if text:- system_parts.append(text)- return- if normalized_role not in {"user", "assistant"}:- raise ResponsesConversionError(f"Unsupported Responses message role: {role!r}")- message = {- "role": normalized_role,- "content": _convert_message_content(content),- }- if normalized_role == "assistant" and reasoning_content:- message["reasoning_content"] = reasoning_content- messages.append(message)---def _append_pending_reasoning(- messages: list[dict[str, Any]], pending_reasoning: str | None-) -> None:- if pending_reasoning:- messages.append(- {- "role": "assistant",- "content": "",- "reasoning_content": pending_reasoning,- }- )---def _iter_input_items(value: Any) -> list[Any]:- if value is None:- return []- if isinstance(value, list):- return value- return [value]---def _convert_message_content(content: Any) -> str | list[dict[str, Any]]:- if isinstance(content, str):- return content- if isinstance(content, list):- blocks: list[dict[str, Any]] = []- for part in content:- if isinstance(part, str):- blocks.append({"type": "text", "text": part})- continue- if not isinstance(part, dict):- raise ResponsesConversionError(- f"Unsupported Responses content part: {type(part).__name__}"- )- part_type = part.get("type")- if part_type in {"input_text", "output_text", "text"} or "text" in part:- blocks.append({"type": "text", "text": _text_from_part(part)})- continue- if part_type == "refusal":- blocks.append({"type": "text", "text": str(part.get("refusal", ""))})- continue- raise ResponsesConversionError(- f"Unsupported Responses content part type: {part_type!r}"- )- return blocks- if isinstance(content, dict):- return [{"type": "text", "text": _text_from_part(content)}]- raise ResponsesConversionError(- f"Unsupported Responses message content: {type(content).__name__}"- )---def _content_as_text(content: Any) -> str:- converted = _convert_message_content(content)- if isinstance(converted, str):- return converted- return "\n".join(str(block.get("text", "")) for block in converted)---def _text_from_part(part: Mapping[str, Any]) -> str:- if text := _optional_str(part.get("text")):- return text- if text := _optional_str(part.get("input_text")):- return text- if text := _optional_str(part.get("output_text")):- return text- return ""---def _reasoning_text_from_item(item: Mapping[str, Any]) -> str | None:- content_parts = _text_parts_from_items(- item.get("content"), item_type="reasoning_text"- )- if content_parts:- return "\n".join(content_parts)- summary_parts = _text_parts_from_items(- item.get("summary"), item_type="summary_text"- )- if summary_parts:- return "\n".join(summary_parts)- return None---def _text_parts_from_items(value: Any, *, item_type: str) -> list[str]:- if not isinstance(value, list):- return []- parts: list[str] = []- for item in value:- if isinstance(item, dict) and item.get("type") == item_type:- text = _optional_str(item.get("text"))- if text:- parts.append(text)- return parts---def _combine_reasoning(existing: str | None, addition: str | None) -> str | None:- if not addition:- return existing- if not existing:- return addition- return f"{existing}\n{addition}"---def _responses_reasoning_to_thinking(value: Any) -> dict[str, Any] | None:- if not isinstance(value, Mapping):- return None- if value.get("effort") == "none":- return {"type": "disabled", "enabled": False}- if any(item is not None for item in value.values()):- return {"type": "enabled", "enabled": True}- return None---def _convert_tools(value: Any) -> list[dict[str, Any]] | None:- if value is None:- return None- if not isinstance(value, list):- raise ResponsesConversionError("Responses tools must be a list")-- tools: list[dict[str, Any]] = []- for tool in value:- if not isinstance(tool, dict):- raise ResponsesConversionError(- f"Unsupported Responses tool: {type(tool).__name__}"- )- tool_type = tool.get("type")- if tool_type == "function":- tools.append(_convert_function_tool(tool, namespace=None))- continue- if tool_type == "namespace":- tools.extend(_convert_namespace_tool(tool))- continue- if tool_type in _UNSUPPORTED_PASSIVE_TOOL_TYPES:- continue- if tool_type != "function":- raise ResponsesConversionError(- f"Unsupported Responses tool type: {tool_type!r}"- )- return tools---def _convert_namespace_tool(tool: Mapping[str, Any]) -> list[dict[str, Any]]:- namespace = _required_str(tool.get("name"), "tool.namespace.name")- nested_tools = tool.get("tools")- if not isinstance(nested_tools, list):- raise ResponsesConversionError(- f"Responses namespace tool {namespace!r} tools must be a list"- )-- converted_tools: list[dict[str, Any]] = []- for nested_tool in nested_tools:- if not isinstance(nested_tool, dict):- raise ResponsesConversionError(- f"Unsupported Responses namespace tool: {type(nested_tool).__name__}"- )- nested_tool_type = nested_tool.get("type")- if nested_tool_type != "function":- raise ResponsesConversionError(- f"Unsupported Responses namespace tool type: {nested_tool_type!r}"- )- converted_tools.append(_convert_function_tool(nested_tool, namespace=namespace))- return converted_tools---def _convert_function_tool(- tool: Mapping[str, Any], *, namespace: str | None-) -> dict[str, Any]:- function = tool.get("function")- source = function if isinstance(function, dict) else tool- name = _required_str(source.get("name"), "tool.name")- schema = source.get("parameters")- if schema is None:- schema = {"type": "object", "properties": {}}- if not isinstance(schema, dict):- raise ResponsesConversionError(- f"Responses tool {name!r} parameters must be an object"- )- converted: dict[str, Any] = {- "name": responses_tool_name_to_anthropic_name(name, namespace=namespace),- "input_schema": schema,- }- if description := _optional_str(source.get("description")):- converted["description"] = description- return converted---def _convert_tool_choice(value: Any) -> dict[str, Any] | None:- if value is None or value == "auto":- return None- if value == "none":- return None- if value == "required":- return {"type": "any"}- if isinstance(value, dict):- choice_type = value.get("type")- if choice_type == "function":- namespace = _optional_str(value.get("namespace"))- name = _required_str(value.get("name"), "tool_choice.name")- return {- "type": "tool",- "name": responses_tool_name_to_anthropic_name(- name, namespace=namespace- ),- }- if choice_type in {"auto", "any", "tool"}:- return dict(value)- raise ResponsesConversionError(f"Unsupported Responses tool_choice: {value!r}")---def responses_tool_name_to_anthropic_name(- name: str, *, namespace: str | None = None-) -> str:- """Return a deterministic Anthropic tool name for a Responses tool identity."""-- if not namespace:- return name- combined = (- f"{_tool_name_part(namespace)}"- f"{_NAMESPACE_TOOL_SEPARATOR}"- f"{_tool_name_part(name)}"- )- if len(combined) <= _MAX_ANTHROPIC_TOOL_NAME_LEN:- return combined- digest = hashlib.sha1(combined.encode("utf-8")).hexdigest()[:8]- prefix_len = _MAX_ANTHROPIC_TOOL_NAME_LEN - len(digest) - 1- return f"{combined[:prefix_len]}_{digest}"---def responses_tool_identity_from_anthropic_name(- request: Mapping[str, Any], anthropic_name: str-) -> tuple[str | None, str]:- """Return the Responses namespace/name represented by an Anthropic tool name."""-- tools = request.get("tools")- if not isinstance(tools, list):- return None, anthropic_name- for tool in tools:- if not isinstance(tool, dict):- continue- tool_type = tool.get("type")- if tool_type == "function":- source = tool.get("function")- function = source if isinstance(source, dict) else tool- if (name := _optional_str(function.get("name"))) and (- responses_tool_name_to_anthropic_name(name) == anthropic_name- ):- return None, name- continue- if tool_type != "namespace":- continue- namespace = _optional_str(tool.get("name"))- nested_tools = tool.get("tools")- if not namespace or not isinstance(nested_tools, list):- continue- for nested_tool in nested_tools:- if not isinstance(nested_tool, dict):- continue- if nested_tool.get("type") != "function":- continue- source = nested_tool.get("function")- function = source if isinstance(source, dict) else nested_tool- if (name := _optional_str(function.get("name"))) and (- responses_tool_name_to_anthropic_name(name, namespace=namespace)- == anthropic_name- ):- return namespace, name- return None, anthropic_name---def _tool_name_part(value: str) -> str:- normalized = _INVALID_TOOL_NAME_CHARS.sub("_", value).strip("_")- return normalized or "tool"---def _parse_arguments(value: Any) -> dict[str, Any]:- if value is None or value == "":- return {}- if isinstance(value, dict):- return value- if not isinstance(value, str):- raise ResponsesConversionError("Responses function_call arguments must be JSON")- try:- parsed = json.loads(value)- except json.JSONDecodeError as exc:- raise ResponsesConversionError(- f"Responses function_call arguments are invalid JSON: {exc.msg}"- ) from exc- if not isinstance(parsed, dict):- raise ResponsesConversionError(- "Responses function_call arguments must decode to an object"- )- return parsed---def _call_id_from_item(item: Mapping[str, Any]) -> str:- for key in ("call_id", "id"):- if value := _optional_str(item.get(key)):- return value- return _new_call_id()---def _required_str(value: Any, field_name: str) -> str:- if isinstance(value, str) and value:- return value- raise ResponsesConversionError(- f"Responses field {field_name} must be a non-empty string"- )---def _optional_str(value: Any) -> str | None:- return value if isinstance(value, str) else None---def _copy_if_present(- source: Mapping[str, Any], target: dict[str, Any], field_name: str-) -> None:- if source.get(field_name) is not None:- target[field_name] = source[field_name]---def _message_content_blocks(message: Mapping[str, Any]) -> list[dict[str, Any]]:- content = message.get("content")- if not isinstance(content, list):- return []- return [block for block in content if isinstance(block, dict)]---def _message_item(item_id: str, text: str, status: str) -> dict[str, Any]:- return {- "id": item_id,- "type": "message",- "status": status,- "role": "assistant",- "content": [{"type": "output_text", "text": text, "annotations": []}],- }---def _reasoning_item(item_id: str, text: str, status: str) -> dict[str, Any]:- return {- "id": item_id,- "type": "reasoning",- "status": status,- "summary": [],- "content": [{"type": "reasoning_text", "text": text}],- }---def _encrypted_reasoning_item(- item_id: str, encrypted_content: str, status: str-) -> dict[str, Any]:- return {- "id": item_id,- "type": "reasoning",- "status": status,- "summary": [],- "encrypted_content": encrypted_content,- }---def _function_call_item(- *,- block_id: str,- name: str,- namespace: str | None,- arguments: str,- status: str,-) -> dict[str, Any]:- item = {- "id": block_id if block_id.startswith("fc_") else f"fc_{uuid.uuid4().hex[:24]}",- "type": "function_call",- "status": status,- "call_id": block_id,- "name": name,- "arguments": arguments,- }- if namespace:- item["namespace"] = namespace- return item---def _openai_usage(value: Any) -> dict[str, int] | None:- if not isinstance(value, Mapping):- return None- input_tokens = value.get("input_tokens")- output_tokens = value.get("output_tokens")- return {- "input_tokens": input_tokens if isinstance(input_tokens, int) else 0,- "output_tokens": output_tokens if isinstance(output_tokens, int) else 0,- "total_tokens": (- (input_tokens if isinstance(input_tokens, int) else 0)- + (output_tokens if isinstance(output_tokens, int) else 0)- ),- }---def _new_response_id() -> str:- return f"resp_{uuid.uuid4().hex}"---def _new_message_item_id() -> str:- return f"msg_{uuid.uuid4().hex}"---def _new_reasoning_item_id() -> str:- return f"rs_{uuid.uuid4().hex}"---def _new_call_id() -> str:- return f"call_{uuid.uuid4().hex[:24]}"diff --git a/core/openai_responses/errors.py b/core/openai_responses/errors.pynew file mode 100644index 00000000..c39985b6--- /dev/null+++ b/core/openai_responses/errors.py@@ -0,0 +1,22 @@+"""Errors and error envelopes for OpenAI Responses compatibility."""++from __future__ import annotations++from typing import Any+++class ResponsesConversionError(ValueError):+ """Raised when a Responses request cannot be converted deterministically."""+++def openai_error_payload(*, message: str, error_type: str) -> dict[str, Any]:+ """Return an OpenAI-compatible error envelope."""++ return {+ "error": {+ "message": message,+ "type": error_type,+ "param": None,+ "code": None,+ }+ }diff --git a/core/openai_responses/events.py b/core/openai_responses/events.pynew file mode 100644index 00000000..0cf16c95--- /dev/null+++ b/core/openai_responses/events.py@@ -0,0 +1,19 @@+"""OpenAI Responses SSE event formatting."""++from __future__ import annotations++import json+from collections.abc import Mapping+from typing import Any++OPENAI_RESPONSES_SSE_HEADERS: dict[str, str] = {+ "X-Accel-Buffering": "no",+ "Cache-Control": "no-cache",+ "Connection": "keep-alive",+}+++def format_response_sse_event(event_type: str, data: Mapping[str, Any]) -> str:+ """Format one OpenAI Responses SSE event."""++ return f"event: {event_type}\ndata: {json.dumps(data)}\n\n"diff --git a/core/openai_responses/ids.py b/core/openai_responses/ids.pynew file mode 100644index 00000000..a6019974--- /dev/null+++ b/core/openai_responses/ids.py@@ -0,0 +1,21 @@+"""Identifier helpers for OpenAI Responses payloads."""++from __future__ import annotations++import uuid+++def new_response_id() -> str:+ return f"resp_{uuid.uuid4().hex}"+++def new_message_item_id() -> str:+ return f"msg_{uuid.uuid4().hex}"+++def new_reasoning_item_id() -> str:+ return f"rs_{uuid.uuid4().hex}"+++def new_call_id() -> str:+ return f"call_{uuid.uuid4().hex[:24]}"diff --git a/core/openai_responses/input.py b/core/openai_responses/input.pynew file mode 100644index 00000000..d1abea13--- /dev/null+++ b/core/openai_responses/input.py@@ -0,0 +1,258 @@+"""Convert OpenAI Responses requests into Anthropic Messages payloads."""++from __future__ import annotations++from collections.abc import Mapping+from typing import Any++from .errors import ResponsesConversionError+from .reasoning import (+ combine_reasoning,+ reasoning_text_from_item,+ responses_reasoning_to_thinking,+)+from .tools import (+ call_id_from_item,+ convert_tool_choice,+ convert_tools,+ custom_tool_input_to_anthropic,+ optional_str,+ parse_arguments,+ required_str,+ responses_tool_name_to_anthropic_name,+)+++def convert_request_to_anthropic_payload(+ request: Mapping[str, Any],+) -> dict[str, Any]:+ """Convert an OpenAI Responses request into an Anthropic Messages payload."""++ system_parts: list[str] = []+ if instructions := optional_str(request.get("instructions")):+ system_parts.append(instructions)++ messages: list[dict[str, Any]] = []+ pending_reasoning: str | None = None+ for item in _iter_input_items(request.get("input")):+ pending_reasoning = _append_input_item(+ item,+ messages=messages,+ system_parts=system_parts,+ pending_reasoning=pending_reasoning,+ )+ _append_pending_reasoning(messages, pending_reasoning)++ if not messages:+ raise ResponsesConversionError("Responses request input must contain a message")++ payload: dict[str, Any] = {+ "model": required_str(request.get("model"), "model"),+ "messages": messages,+ "stream": True,+ }+ if system_parts:+ payload["system"] = "\n\n".join(system_parts)+ _copy_if_present(request, payload, "temperature")+ _copy_if_present(request, payload, "top_p")+ if request.get("max_output_tokens") is not None:+ payload["max_tokens"] = request["max_output_tokens"]+ if isinstance(request.get("metadata"), dict):+ payload["metadata"] = request["metadata"]++ if thinking := responses_reasoning_to_thinking(request.get("reasoning")):+ payload["thinking"] = thinking++ raw_tool_choice = request.get("tool_choice")+ tools = convert_tools(request.get("tools"))+ if tools and raw_tool_choice != "none":+ payload["tools"] = tools+ tool_choice = convert_tool_choice(raw_tool_choice)+ if tool_choice is not None:+ payload["tool_choice"] = tool_choice++ return payload+++def _append_input_item(+ item: Any,+ *,+ messages: list[dict[str, Any]],+ system_parts: list[str],+ pending_reasoning: str | None,+) -> str | None:+ if isinstance(item, str):+ _append_pending_reasoning(messages, pending_reasoning)+ messages.append({"role": "user", "content": item})+ return None+ if not isinstance(item, dict):+ raise ResponsesConversionError(+ f"Unsupported Responses input item: {type(item).__name__}"+ )++ item_type = item.get("type")+ if item_type in (None, "message") or "role" in item:+ role = required_str(item.get("role", "user"), "input.role")+ if role == "assistant":+ _append_message_item(+ role,+ item.get("content", ""),+ messages,+ system_parts,+ reasoning_content=pending_reasoning,+ )+ return None+ _append_pending_reasoning(messages, pending_reasoning)+ _append_message_item(role, item.get("content", ""), messages, system_parts)+ return None+ if item_type in {"function_call", "custom_tool_call"}:+ namespace = optional_str(item.get("namespace"))+ field_name = f"{item_type}.name"+ name = required_str(item.get("name"), field_name)+ if item_type == "custom_tool_call":+ tool_input = custom_tool_input_to_anthropic(item.get("input"))+ else:+ tool_input = parse_arguments(item.get("arguments"))+ message = {+ "role": "assistant",+ "content": [+ {+ "type": "tool_use",+ "id": call_id_from_item(item),+ "name": responses_tool_name_to_anthropic_name(+ name, namespace=namespace+ ),+ "input": tool_input,+ }+ ],+ }+ if pending_reasoning:+ message["reasoning_content"] = pending_reasoning+ messages.append(message)+ return None+ if item_type in {"function_call_output", "custom_tool_call_output"}:+ _append_pending_reasoning(messages, pending_reasoning)+ messages.append(+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": call_id_from_item(item),+ "content": item.get("output", ""),+ }+ ],+ }+ )+ return None+ if item_type == "reasoning":+ return combine_reasoning(pending_reasoning, reasoning_text_from_item(item))+ if item_type in {"input_text", "output_text", "text"}:+ _append_pending_reasoning(messages, pending_reasoning)+ messages.append({"role": "user", "content": _text_from_part(item)})+ return None++ raise ResponsesConversionError(+ f"Unsupported Responses input item type: {item_type!r}"+ )+++def _append_message_item(+ role: str,+ content: Any,+ messages: list[dict[str, Any]],+ system_parts: list[str],+ *,+ reasoning_content: str | None = None,+) -> None:+ normalized_role = "system" if role == "developer" else role+ if normalized_role == "system":+ text = _content_as_text(content)+ if text:+ system_parts.append(text)+ return+ if normalized_role not in {"user", "assistant"}:+ raise ResponsesConversionError(f"Unsupported Responses message role: {role!r}")+ message = {+ "role": normalized_role,+ "content": _convert_message_content(content),+ }+ if normalized_role == "assistant" and reasoning_content:+ message["reasoning_content"] = reasoning_content+ messages.append(message)+++def _append_pending_reasoning(+ messages: list[dict[str, Any]], pending_reasoning: str | None+) -> None:+ if pending_reasoning:+ messages.append(+ {+ "role": "assistant",+ "content": "",+ "reasoning_content": pending_reasoning,+ }+ )+++def _iter_input_items(value: Any) -> list[Any]:+ if value is None:+ return []+ if isinstance(value, list):+ return value+ return [value]+++def _convert_message_content(content: Any) -> str | list[dict[str, Any]]:+ if isinstance(content, str):+ return content+ if isinstance(content, list):+ blocks: list[dict[str, Any]] = []+ for part in content:+ if isinstance(part, str):+ blocks.append({"type": "text", "text": part})+ continue+ if not isinstance(part, dict):+ raise ResponsesConversionError(+ f"Unsupported Responses content part: {type(part).__name__}"+ )+ part_type = part.get("type")+ if part_type in {"input_text", "output_text", "text"} or "text" in part:+ blocks.append({"type": "text", "text": _text_from_part(part)})+ continue+ if part_type == "refusal":+ blocks.append({"type": "text", "text": str(part.get("refusal", ""))})+ continue+ raise ResponsesConversionError(+ f"Unsupported Responses content part type: {part_type!r}"+ )+ return blocks+ if isinstance(content, dict):+ return [{"type": "text", "text": _text_from_part(content)}]+ raise ResponsesConversionError(+ f"Unsupported Responses message content: {type(content).__name__}"+ )+++def _content_as_text(content: Any) -> str:+ converted = _convert_message_content(content)+ if isinstance(converted, str):+ return converted+ return "\n".join(str(block.get("text", "")) for block in converted)+++def _text_from_part(part: Mapping[str, Any]) -> str:+ if text := optional_str(part.get("text")):+ return text+ if text := optional_str(part.get("input_text")):+ return text+ if text := optional_str(part.get("output_text")):+ return text+ return ""+++def _copy_if_present(+ source: Mapping[str, Any], target: dict[str, Any], field_name: str+) -> None:+ if source.get(field_name) is not None:+ target[field_name] = source[field_name]diff --git a/core/openai_responses/items.py b/core/openai_responses/items.pynew file mode 100644index 00000000..d4e14484--- /dev/null+++ b/core/openai_responses/items.py@@ -0,0 +1,37 @@+"""Responses object and output item builders."""++from __future__ import annotations++from typing import Any+++def message_item(item_id: str, text: str, status: str) -> dict[str, Any]:+ return {+ "id": item_id,+ "type": "message",+ "status": status,+ "role": "assistant",+ "content": [{"type": "output_text", "text": text, "annotations": []}],+ }+++def reasoning_item(item_id: str, text: str, status: str) -> dict[str, Any]:+ return {+ "id": item_id,+ "type": "reasoning",+ "status": status,+ "summary": [],+ "content": [{"type": "reasoning_text", "text": text}],+ }+++def encrypted_reasoning_item(+ item_id: str, encrypted_content: str, status: str+) -> dict[str, Any]:+ return {+ "id": item_id,+ "type": "reasoning",+ "status": status,+ "summary": [],+ "encrypted_content": encrypted_content,+ }diff --git a/core/openai_responses/reasoning.py b/core/openai_responses/reasoning.pynew file mode 100644index 00000000..9f4ec45e--- /dev/null+++ b/core/openai_responses/reasoning.py@@ -0,0 +1,52 @@+"""Reasoning and thinking conversion helpers for OpenAI Responses."""++from __future__ import annotations++from collections.abc import Mapping+from typing import Any++from .tools import optional_str+++def reasoning_text_from_item(item: Mapping[str, Any]) -> str | None:+ content_parts = _text_parts_from_items(+ item.get("content"), item_type="reasoning_text"+ )+ if content_parts:+ return "\n".join(content_parts)+ summary_parts = _text_parts_from_items(+ item.get("summary"), item_type="summary_text"+ )+ if summary_parts:+ return "\n".join(summary_parts)+ return None+++def combine_reasoning(existing: str | None, addition: str | None) -> str | None:+ if not addition:+ return existing+ if not existing:+ return addition+ return f"{existing}\n{addition}"+++def responses_reasoning_to_thinking(value: Any) -> dict[str, Any] | None:+ if not isinstance(value, Mapping):+ return None+ if value.get("effort") == "none":+ return {"type": "disabled", "enabled": False}+ if any(item is not None for item in value.values()):+ return {"type": "enabled", "enabled": True}+ return None+++def _text_parts_from_items(value: Any, *, item_type: str) -> list[str]:+ if not isinstance(value, list):+ return []+ parts: list[str] = []+ for item in value:+ if isinstance(item, dict) and item.get("type") == item_type:+ text = optional_str(item.get("text"))+ if text:+ parts.append(text)+ return partsdiff --git a/core/openai_responses/sse.py b/core/openai_responses/sse.pydeleted file mode 100644index 9f67f0a8..00000000--- a/core/openai_responses/sse.py+++ /dev/null@@ -1,850 +0,0 @@-"""Translate Anthropic-style SSE streams into OpenAI Responses SSE streams."""--from __future__ import annotations--import json-import time-import uuid-from collections.abc import AsyncIterable, AsyncIterator, Mapping-from dataclasses import dataclass, field-from typing import Any--from .conversion import (- anthropic_message_response_to_openai_response,- responses_tool_identity_from_anthropic_name,-)--OPENAI_RESPONSES_SSE_HEADERS: dict[str, str] = {- "X-Accel-Buffering": "no",- "Cache-Control": "no-cache",- "Connection": "keep-alive",-}---def format_response_sse_event(event_type: str, data: Mapping[str, Any]) -> str:- """Format one OpenAI Responses SSE event."""-- return f"event: {event_type}\ndata: {json.dumps(data)}\n\n"---async def iter_anthropic_sse_as_openai_responses(- chunks: AsyncIterable[Any],- request: Mapping[str, Any],-) -> AsyncIterator[str]:- """Yield Responses SSE events translated from an Anthropic SSE stream."""-- transformer = _ResponsesStreamTransformer(request)- async for event in _iter_sse_events(chunks):- for chunk in transformer.process_anthropic_event(event):- yield chunk- if transformer.terminal:- return- for chunk in transformer.finish_if_needed():- yield chunk---async def collect_openai_response_from_anthropic_sse(- chunks: AsyncIterable[Any],- request: Mapping[str, Any],-) -> dict[str, Any]:- """Collect a translated Anthropic SSE stream into one Responses object."""-- transformer = _ResponsesStreamTransformer(request)- async for event in _iter_sse_events(chunks):- transformer.process_anthropic_event(event)- if transformer.terminal:- break- if transformer.final_response is not None:- return transformer.final_response- transformer.finish_if_needed()- return transformer.final_response or transformer.response_payload(- status="completed"- )---def iter_message_response_as_openai_responses(- message: Mapping[str, Any],- request: Mapping[str, Any],-) -> list[str]:- """Return Responses SSE chunks for a non-stream Anthropic message response."""-- response_id = _new_response_id()- response = _base_response(request, response_id=response_id, status="in_progress")- chunks = [- format_response_sse_event(- "response.created",- {"type": "response.created", "response": response},- )- ]- completed = anthropic_message_response_to_openai_response(- message, request, response_id=response_id- )- for output_index, item in enumerate(completed["output"]):- chunks.append(- format_response_sse_event(- "response.output_item.added",- {- "type": "response.output_item.added",- "output_index": output_index,- "item": _in_progress_item(item),- },- )- )- if item.get("type") == "message":- text = _message_item_text(item)- chunks.extend(_message_text_events(item, output_index, text))- elif item.get("type") == "reasoning":- text = _reasoning_item_text(item)- if text:- chunks.extend(_reasoning_text_events(item, output_index, text))- elif item.get("type") == "function_call":- arguments = str(item.get("arguments", ""))- if arguments:- chunks.append(- format_response_sse_event(- "response.function_call_arguments.delta",- {- "type": "response.function_call_arguments.delta",- "item_id": item.get("id"),- "output_index": output_index,- "delta": arguments,- },- )- )- chunks.append(- format_response_sse_event(- "response.function_call_arguments.done",- {- "type": "response.function_call_arguments.done",- "item_id": item.get("id"),- "output_index": output_index,- "arguments": arguments,- },- )- )- chunks.append(- format_response_sse_event(- "response.output_item.done",- {- "type": "response.output_item.done",- "output_index": output_index,- "item": item,- },- )- )- chunks.append(- format_response_sse_event(- "response.completed",- {"type": "response.completed", "response": completed},- )- )- return chunks---@dataclass(slots=True)-class _AnthropicSseEvent:- event: str- data: dict[str, Any]---@dataclass(slots=True)-class _ToolState:- output_index: int- item_id: str- call_id: str- name: str- namespace: str | None = None- argument_parts: list[str] = field(default_factory=list)---@dataclass(slots=True)-class _ReasoningState:- output_index: int- item_id: str- text_parts: list[str] = field(default_factory=list)- encrypted_content: str | None = None---class _ResponsesStreamTransformer:- def __init__(self, request: Mapping[str, Any]) -> None:- self._request = request- self._response_id = _new_response_id()- self._created_at = int(time.time())- self._output: list[dict[str, Any]] = []- self._text_item_id = _new_message_item_id()- self._text_started = False- self._text_done = False- self._text_output_index: int | None = None- self._text_block_indexes: set[int] = set()- self._text_parts: list[str] = []- self._tools_by_block_index: dict[int, _ToolState] = {}- self._reasoning_by_block_index: dict[int, _ReasoningState] = {}- self._stop_reason: str | None = None- self._usage: dict[str, int] | None = None- self._started = False- self.terminal = False- self.final_response: dict[str, Any] | None = None-- def process_anthropic_event(self, event: _AnthropicSseEvent) -> list[str]:- if self.terminal:- return []-- chunks = self._ensure_started()- if event.event == "content_block_start":- chunks.extend(self._handle_content_block_start(event.data))- elif event.event == "content_block_delta":- chunks.extend(self._handle_content_block_delta(event.data))- elif event.event == "content_block_stop":- chunks.extend(self._handle_content_block_stop(event.data))- elif event.event == "message_delta":- self._handle_message_delta(event.data)- elif event.event == "message_stop":- chunks.extend(self._complete_response())- elif event.event == "error":- chunks.extend(self._error_event(event.data))- return chunks-- def finish_if_needed(self) -> list[str]:- if self.terminal:- return []- chunks = self._ensure_started()- chunks.extend(self._complete_response())- return chunks-- def response_payload(self, *, status: str) -> dict[str, Any]:- return {- "id": self._response_id,- "object": "response",- "created_at": self._created_at,- "status": status,- "model": str(self._request.get("model", "")),- "output": list(self._output),- "parallel_tool_calls": bool(self._request.get("parallel_tool_calls", True)),- "tool_choice": self._request.get("tool_choice", "auto"),- "temperature": self._request.get("temperature"),- "top_p": self._request.get("top_p"),- "max_output_tokens": self._request.get("max_output_tokens"),- "usage": self._usage,- "error": None if status == "completed" else {},- }-- def _ensure_started(self) -> list[str]:- if self._started:- return []- self._started = True- return [- format_response_sse_event(- "response.created",- {- "type": "response.created",- "response": self.response_payload(status="in_progress"),- },- )- ]-- def _handle_content_block_start(self, data: Mapping[str, Any]) -> list[str]:- block = data.get("content_block")- if not isinstance(block, dict):- return []- block_type = block.get("type")- if block_type == "text":- index = _event_index(data)- if index is not None:- self._text_block_indexes.add(index)- chunks = self._ensure_text_started()- if text := str(block.get("text", "")):- chunks.extend(self._emit_text_delta(text))- return chunks- if block_type == "thinking":- index = _event_index(data)- if index is None:- return []- state = self._start_reasoning_block(index)- chunks = self._reasoning_item_added(state)- if text := str(block.get("thinking", "")):- chunks.extend(self._emit_reasoning_delta(state, text))- return chunks- if block_type == "redacted_thinking":- index = _event_index(data)- if index is None:- return []- state = self._start_reasoning_block(- index, encrypted_content=str(block.get("data", ""))- )- return self._reasoning_item_added(state)- if block_type == "tool_use":- index = _event_index(data)- if index is None:- return []- call_id = str(block.get("id", "") or _new_call_id())- item_id = f"fc_{uuid.uuid4().hex[:24]}"- output_index = len(self._output)- namespace, name = responses_tool_identity_from_anthropic_name(- self._request, str(block.get("name", ""))- )- state = _ToolState(- output_index=output_index,- item_id=item_id,- call_id=call_id,- name=name,- namespace=namespace,- )- initial_input = block.get("input")- if isinstance(initial_input, dict) and initial_input:- state.argument_parts.append(json.dumps(initial_input))- self._tools_by_block_index[index] = state- return []-- def _handle_content_block_delta(self, data: Mapping[str, Any]) -> list[str]:- delta = data.get("delta")- if not isinstance(delta, dict):- return []- delta_type = delta.get("type")- if delta_type == "text_delta":- return self._emit_text_delta(str(delta.get("text", "")))- if delta_type == "thinking_delta":- index = _event_index(data)- if index is None:- return []- state = self._reasoning_by_block_index.get(index)- if state is None:- state = self._start_reasoning_block(index)- return [- *self._reasoning_item_added(state),- *self._emit_reasoning_delta(state, str(delta.get("thinking", ""))),- ]- return self._emit_reasoning_delta(state, str(delta.get("thinking", "")))- if delta_type == "input_json_delta":- index = _event_index(data)- if index is not None and index in self._tools_by_block_index:- self._tools_by_block_index[index].argument_parts.append(- str(delta.get("partial_json", ""))- )- return []-- def _handle_content_block_stop(self, data: Mapping[str, Any]) -> list[str]:- index = _event_index(data)- if index is None:- return []- if index in self._text_block_indexes:- self._text_block_indexes.remove(index)- return self._complete_text_if_needed()- reasoning_state = self._reasoning_by_block_index.pop(index, None)- if reasoning_state is not None:- return self._complete_reasoning(reasoning_state)- return self._handle_tool_block_stop(index)-- def _start_reasoning_block(- self, index: int, *, encrypted_content: str | None = None- ) -> _ReasoningState:- state = _ReasoningState(- output_index=len(self._output),- item_id=_new_reasoning_item_id(),- encrypted_content=encrypted_content,- )- self._reasoning_by_block_index[index] = state- return state-- def _reasoning_item_added(self, state: _ReasoningState) -> list[str]:- item = _reasoning_output_item(state, status="in_progress")- return [- format_response_sse_event(- "response.output_item.added",- {- "type": "response.output_item.added",- "output_index": state.output_index,- "item": item,- },- )- ]-- def _emit_reasoning_delta(self, state: _ReasoningState, text: str) -> list[str]:- if not text:- return []- state.text_parts.append(text)- return [- format_response_sse_event(- "response.reasoning_text.delta",- {- "type": "response.reasoning_text.delta",- "item_id": state.item_id,- "output_index": state.output_index,- "content_index": 0,- "delta": text,- },- )- ]-- def _complete_reasoning(self, state: _ReasoningState) -> list[str]:- item = _reasoning_output_item(state, status="completed")- self._output.append(item)- chunks: list[str] = []- text = "".join(state.text_parts)- if text:- chunks.append(- format_response_sse_event(- "response.reasoning_text.done",- {- "type": "response.reasoning_text.done",- "item_id": state.item_id,- "output_index": state.output_index,- "content_index": 0,- "text": text,- },- )- )- chunks.append(- format_response_sse_event(- "response.output_item.done",- {- "type": "response.output_item.done",- "output_index": state.output_index,- "item": item,- },- )- )- return chunks-- def _handle_tool_block_stop(self, index: int) -> list[str]:- state = self._tools_by_block_index.pop(index, None)- if state is None:- return []- return self._complete_tool_call(state)-- def _handle_message_delta(self, data: Mapping[str, Any]) -> None:- delta = data.get("delta")- if isinstance(delta, dict):- self._stop_reason = str(delta.get("stop_reason") or "")- usage = data.get("usage")- if isinstance(usage, dict):- input_tokens = usage.get("input_tokens")- output_tokens = usage.get("output_tokens")- safe_in = input_tokens if isinstance(input_tokens, int) else 0- safe_out = output_tokens if isinstance(output_tokens, int) else 0- self._usage = {- "input_tokens": safe_in,- "output_tokens": safe_out,- "total_tokens": safe_in + safe_out,- }-- def _ensure_text_started(self) -> list[str]:- if self._text_started:- return []- if self._text_done:- self._text_item_id = _new_message_item_id()- self._text_done = False- self._text_parts = []- self._text_started = True- output_index = len(self._output)- self._text_output_index = output_index- item = {- "id": self._text_item_id,- "type": "message",- "status": "in_progress",- "role": "assistant",- "content": [],- }- return [- format_response_sse_event(- "response.output_item.added",- {- "type": "response.output_item.added",- "output_index": output_index,- "item": item,- },- ),- format_response_sse_event(- "response.content_part.added",- {- "type": "response.content_part.added",- "item_id": self._text_item_id,- "output_index": output_index,- "content_index": 0,- "part": {"type": "output_text", "text": "", "annotations": []},- },- ),- ]-- def _emit_text_delta(self, text: str) -> list[str]:- if not text:- return []- chunks = self._ensure_text_started()- self._text_parts.append(text)- output_index = self._current_text_output_index()- chunks.append(- format_response_sse_event(- "response.output_text.delta",- {- "type": "response.output_text.delta",- "item_id": self._text_item_id,- "output_index": output_index,- "content_index": 0,- "delta": text,- },- )- )- return chunks-- def _complete_text_if_needed(self) -> list[str]:- if not self._text_started or self._text_done:- return []- self._text_done = True- self._text_started = False- text = "".join(self._text_parts)- output_index = self._current_text_output_index()- item = _message_item(self._text_item_id, text, "completed")- if output_index >= len(self._output):- self._output.append(item)- else:- self._output.insert(output_index, item)- return [- format_response_sse_event(- "response.output_text.done",- {- "type": "response.output_text.done",- "item_id": self._text_item_id,- "output_index": output_index,- "content_index": 0,- "text": text,- },- ),- format_response_sse_event(- "response.content_part.done",- {- "type": "response.content_part.done",- "item_id": self._text_item_id,- "output_index": output_index,- "content_index": 0,- "part": {"type": "output_text", "text": text, "annotations": []},- },- ),- format_response_sse_event(- "response.output_item.done",- {- "type": "response.output_item.done",- "output_index": output_index,- "item": item,- },- ),- ]-- def _complete_tool_call(self, state: _ToolState) -> list[str]:- arguments = "".join(state.argument_parts) or "{}"- item = {- "id": state.item_id,- "type": "function_call",- "status": "completed",- "call_id": state.call_id,- "name": state.name,- "arguments": arguments,- }- if state.namespace:- item["namespace"] = state.namespace- self._output.append(item)- chunks = [- format_response_sse_event(- "response.output_item.added",- {- "type": "response.output_item.added",- "output_index": state.output_index,- "item": {**item, "status": "in_progress", "arguments": ""},- },- )- ]- if arguments:- chunks.append(- format_response_sse_event(- "response.function_call_arguments.delta",- {- "type": "response.function_call_arguments.delta",- "item_id": state.item_id,- "output_index": state.output_index,- "delta": arguments,- },- )- )- chunks.extend(- [- format_response_sse_event(- "response.function_call_arguments.done",- {- "type": "response.function_call_arguments.done",- "item_id": state.item_id,- "output_index": state.output_index,- "arguments": arguments,- },- ),- format_response_sse_event(- "response.output_item.done",- {- "type": "response.output_item.done",- "output_index": state.output_index,- "item": item,- },- ),- ]- )- return chunks-- def _complete_response(self) -> list[str]:- chunks = self._complete_text_if_needed()- for index in list(self._reasoning_by_block_index):- state = self._reasoning_by_block_index.pop(index)- chunks.extend(self._complete_reasoning(state))- self.final_response = self.response_payload(status="completed")- if self._stop_reason:- self.final_response["stop_reason"] = self._stop_reason- chunks.append(- format_response_sse_event(- "response.completed",- {"type": "response.completed", "response": self.final_response},- )- )- self.terminal = True- return chunks-- def _error_event(self, data: Mapping[str, Any]) -> list[str]:- error = data.get("error")- if not isinstance(error, dict):- error = {"type": "api_error", "message": str(data)}- self.final_response = self.response_payload(status="failed")- self.final_response["error"] = {- "message": str(error.get("message", "")),- "type": str(error.get("type", "api_error")),- "param": None,- "code": None,- }- self.terminal = True- return [- format_response_sse_event(- "error",- {"type": "error", "error": self.final_response["error"]},- )- ]-- def _current_text_output_index(self) -> int:- if self._text_output_index is None:- self._text_output_index = len(self._output)- return self._text_output_index---async def _iter_sse_events(- chunks: AsyncIterable[Any],-) -> AsyncIterator[_AnthropicSseEvent]:- buffer = ""- async for chunk in chunks:- if isinstance(chunk, bytes):- buffer += chunk.decode("utf-8", errors="replace")- else:- buffer += str(chunk)-- while "\n\n" in buffer:- raw, buffer = buffer.split("\n\n", 1)- event = _parse_sse_event(raw)- if event is not None:- yield event-- if buffer.strip():- event = _parse_sse_event(buffer)- if event is not None:- yield event---def _parse_sse_event(raw: str) -> _AnthropicSseEvent | None:- event_type = ""- data_parts: list[str] = []- for line in raw.splitlines():- stripped = line.rstrip("\r")- if stripped.startswith("event:"):- event_type = stripped.split(":", 1)[1].strip()- elif stripped.startswith("data:"):- data_parts.append(stripped.split(":", 1)[1].strip())- if not event_type and not data_parts:- return None- data_text = "\n".join(data_parts)- if data_text == "[DONE]":- return None- try:- parsed = json.loads(data_text) if data_text else {}- except json.JSONDecodeError:- parsed = {"raw": data_text}- if not isinstance(parsed, dict):- parsed = {"value": parsed}- return _AnthropicSseEvent(event=event_type, data=parsed)---def _event_index(data: Mapping[str, Any]) -> int | None:- value = data.get("index")- return value if isinstance(value, int) else None---def _base_response(- request: Mapping[str, Any], *, response_id: str, status: str-) -> dict[str, Any]:- return {- "id": response_id,- "object": "response",- "created_at": int(time.time()),- "status": status,- "model": str(request.get("model", "")),- "output": [],- "parallel_tool_calls": bool(request.get("parallel_tool_calls", True)),- "tool_choice": request.get("tool_choice", "auto"),- "temperature": request.get("temperature"),- "top_p": request.get("top_p"),- "max_output_tokens": request.get("max_output_tokens"),- "usage": None,- "error": None,- }---def _message_text_events(- item: Mapping[str, Any], output_index: int, text: str-) -> list[str]:- item_id = str(item.get("id", ""))- return [- format_response_sse_event(- "response.content_part.added",- {- "type": "response.content_part.added",- "item_id": item_id,- "output_index": output_index,- "content_index": 0,- "part": {"type": "output_text", "text": "", "annotations": []},- },- ),- format_response_sse_event(- "response.output_text.delta",- {- "type": "response.output_text.delta",- "item_id": item_id,- "output_index": output_index,- "content_index": 0,- "delta": text,- },- ),- format_response_sse_event(- "response.output_text.done",- {- "type": "response.output_text.done",- "item_id": item_id,- "output_index": output_index,- "content_index": 0,- "text": text,- },- ),- format_response_sse_event(- "response.content_part.done",- {- "type": "response.content_part.done",- "item_id": item_id,- "output_index": output_index,- "content_index": 0,- "part": {"type": "output_text", "text": text, "annotations": []},- },- ),- ]---def _reasoning_text_events(- item: Mapping[str, Any], output_index: int, text: str-) -> list[str]:- item_id = str(item.get("id", ""))- return [- format_response_sse_event(- "response.reasoning_text.delta",- {- "type": "response.reasoning_text.delta",- "item_id": item_id,- "output_index": output_index,- "content_index": 0,- "delta": text,- },- ),- format_response_sse_event(- "response.reasoning_text.done",- {- "type": "response.reasoning_text.done",- "item_id": item_id,- "output_index": output_index,- "content_index": 0,- "text": text,- },- ),- ]---def _message_item(item_id: str, text: str, status: str) -> dict[str, Any]:- return {- "id": item_id,- "type": "message",- "status": status,- "role": "assistant",- "content": [{"type": "output_text", "text": text, "annotations": []}],- }---def _reasoning_output_item(state: _ReasoningState, *, status: str) -> dict[str, Any]:- item: dict[str, Any] = {- "id": state.item_id,- "type": "reasoning",- "status": status,- "summary": [],- }- if state.encrypted_content is not None:- item["encrypted_content"] = state.encrypted_content- return item- item["content"] = [{"type": "reasoning_text", "text": "".join(state.text_parts)}]- return item---def _message_item_text(item: Mapping[str, Any]) -> str:- content = item.get("content")- if not isinstance(content, list):- return ""- parts = [- str(part.get("text", ""))- for part in content- if isinstance(part, dict) and part.get("type") == "output_text"- ]- return "".join(parts)---def _reasoning_item_text(item: Mapping[str, Any]) -> str:- content = item.get("content")- if not isinstance(content, list):- return ""- parts = [- str(part.get("text", ""))- for part in content- if isinstance(part, dict) and part.get("type") == "reasoning_text"- ]- return "".join(parts)---def _in_progress_item(item: Mapping[str, Any]) -> dict[str, Any]:- clone = dict(item)- clone["status"] = "in_progress"- if clone.get("type") == "message":- clone["content"] = []- if clone.get("type") == "reasoning" and "content" in clone:- clone["content"] = []- if clone.get("type") == "function_call":- clone["arguments"] = ""- return clone---def _new_response_id() -> str:- return f"resp_{uuid.uuid4().hex}"---def _new_message_item_id() -> str:- return f"msg_{uuid.uuid4().hex}"---def _new_reasoning_item_id() -> str:- return f"rs_{uuid.uuid4().hex}"---def _new_call_id() -> str:- return f"call_{uuid.uuid4().hex[:24]}"diff --git a/core/openai_responses/stream.py b/core/openai_responses/stream.pynew file mode 100644index 00000000..78cdb66a--- /dev/null+++ b/core/openai_responses/stream.py@@ -0,0 +1,25 @@+"""Translate Anthropic SSE streams into OpenAI Responses SSE streams."""++from __future__ import annotations++from collections.abc import AsyncIterable, AsyncIterator, Mapping+from typing import Any++from .anthropic_sse import iter_sse_events+from .stream_state import ResponsesStreamAssembler+++async def iter_responses_sse_from_anthropic(+ chunks: AsyncIterable[Any],+ request: Mapping[str, Any],+) -> AsyncIterator[str]:+ """Yield Responses SSE events translated from an Anthropic SSE stream."""++ assembler = ResponsesStreamAssembler(request)+ async for event in iter_sse_events(chunks):+ for chunk in assembler.process_anthropic_event(event):+ yield chunk+ if assembler.terminal:+ return+ for chunk in assembler.finish_if_needed():+ yield chunkdiff --git a/core/openai_responses/stream_state.py b/core/openai_responses/stream_state.pynew file mode 100644index 00000000..10c1c9c0--- /dev/null+++ b/core/openai_responses/stream_state.py@@ -0,0 +1,645 @@+"""Block-indexed OpenAI Responses stream assembly."""++from __future__ import annotations++import json+import time+import uuid+from collections.abc import Mapping+from dataclasses import dataclass, field+from typing import Any, Literal++from .anthropic_sse import AnthropicSseEvent+from .events import format_response_sse_event+from .ids import (+ new_call_id,+ new_message_item_id,+ new_reasoning_item_id,+ new_response_id,+)+from .items import (+ encrypted_reasoning_item,+ message_item,+ reasoning_item,+)+from .tools import (+ custom_tool_input_text_from_arguments,+ responses_tool_identity_from_anthropic_name,+)+++@dataclass(slots=True)+class _TextBlockState:+ index: int+ output_index: int+ item_id: str+ text_parts: list[str] = field(default_factory=list)+++@dataclass(slots=True)+class _ReasoningBlockState:+ index: int+ output_index: int+ item_id: str+ text_parts: list[str] = field(default_factory=list)+ encrypted_content: str | None = None+++@dataclass(slots=True)+class _ToolBlockState:+ index: int+ output_index: int+ item_id: str+ call_id: str+ kind: Literal["function", "custom"]+ name: str+ namespace: str | None = None+ argument_parts: list[str] = field(default_factory=list)+++_BlockState = _TextBlockState | _ReasoningBlockState | _ToolBlockState+++class ResponsesStreamAssembler:+ """Assemble Responses SSE events from indexed Anthropic content blocks."""++ def __init__(self, request: Mapping[str, Any]) -> None:+ self._request = request+ self._response_id = new_response_id()+ self._created_at = int(time.time())+ self._output_slots: list[dict[str, Any] | None] = []+ self._active_blocks: dict[int, _BlockState] = {}+ self._fallback_text_index = -1+ self._input_tokens: int | None = None+ self._output_tokens: int | None = None+ self._started = False+ self.terminal = False+ self.final_response: dict[str, Any] | None = None++ def process_anthropic_event(self, event: AnthropicSseEvent) -> list[str]:+ if self.terminal:+ return []++ chunks = self._ensure_started()+ if event.event == "content_block_start":+ chunks.extend(self._handle_content_block_start(event.data))+ elif event.event == "content_block_delta":+ chunks.extend(self._handle_content_block_delta(event.data))+ elif event.event == "content_block_stop":+ chunks.extend(self._handle_content_block_stop(event.data))+ elif event.event == "message_delta":+ self._handle_message_delta(event.data)+ elif event.event == "message_stop":+ chunks.extend(self.complete_response())+ elif event.event == "error":+ chunks.extend(self.fail_response(event.data))+ return chunks++ def finish_if_needed(self) -> list[str]:+ if self.terminal:+ return []+ chunks = self._ensure_started()+ chunks.extend(self.complete_response())+ return chunks++ def response_payload(+ self, *, status: str, error: dict[str, Any] | None = None+ ) -> dict[str, Any]:+ return {+ "id": self._response_id,+ "object": "response",+ "created_at": self._created_at,+ "status": status,+ "model": str(self._request.get("model", "")),+ "output": self._output(),+ "parallel_tool_calls": bool(self._request.get("parallel_tool_calls", True)),+ "tool_choice": self._request.get("tool_choice", "auto"),+ "temperature": self._request.get("temperature"),+ "top_p": self._request.get("top_p"),+ "max_output_tokens": self._request.get("max_output_tokens"),+ "usage": self._usage(),+ "error": error,+ }++ def complete_response(self) -> list[str]:+ chunks = self._flush_active_blocks()+ self.final_response = self.response_payload(status="completed")+ chunks.append(+ format_response_sse_event(+ "response.completed",+ {"type": "response.completed", "response": self.final_response},+ )+ )+ self.terminal = True+ return chunks++ def fail_response(self, data: Mapping[str, Any]) -> list[str]:+ chunks = self._flush_active_blocks()+ error = _openai_error_from_anthropic_error(data)+ self.final_response = self.response_payload(status="failed", error=error)+ chunks.append(+ format_response_sse_event(+ "response.failed",+ {"type": "response.failed", "response": self.final_response},+ )+ )+ self.terminal = True+ return chunks++ def _ensure_started(self) -> list[str]:+ if self._started:+ return []+ self._started = True+ return [+ format_response_sse_event(+ "response.created",+ {+ "type": "response.created",+ "response": self.response_payload(status="in_progress"),+ },+ )+ ]++ def _handle_content_block_start(self, data: Mapping[str, Any]) -> list[str]:+ block = data.get("content_block")+ if not isinstance(block, dict):+ return []+ block_type = block.get("type")+ index = _event_index(data)+ if block_type == "text":+ index = self._safe_index(index)+ chunks, state = self._start_text_block(index)+ if text := _string_value(block.get("text")):+ chunks.extend(self._emit_text_delta(state, text))+ return chunks+ if block_type == "thinking":+ if index is None:+ return []+ chunks, state = self._start_reasoning_block(index)+ if text := _string_value(block.get("thinking")):+ chunks.extend(self._emit_reasoning_delta(state, text))+ return chunks+ if block_type == "redacted_thinking":+ if index is None:+ return []+ chunks, _state = self._start_reasoning_block(+ index, encrypted_content=_string_value(block.get("data"))+ )+ return chunks+ if block_type == "tool_use":+ if index is None:+ return []+ return self._start_tool_block(index, block)+ return []++ def _handle_content_block_delta(self, data: Mapping[str, Any]) -> list[str]:+ delta = data.get("delta")+ if not isinstance(delta, dict):+ return []+ delta_type = delta.get("type")+ index = _event_index(data)+ if delta_type == "text_delta":+ index = self._safe_index(index)+ state = self._active_blocks.get(index)+ chunks: list[str] = []+ if not isinstance(state, _TextBlockState):+ chunks, state = self._start_text_block(index)+ chunks.extend(+ self._emit_text_delta(state, _string_value(delta.get("text")))+ )+ return chunks+ if delta_type == "thinking_delta":+ if index is None:+ return []+ state = self._active_blocks.get(index)+ chunks = []+ if not isinstance(state, _ReasoningBlockState):+ chunks, state = self._start_reasoning_block(index)+ chunks.extend(+ self._emit_reasoning_delta(state, _string_value(delta.get("thinking")))+ )+ return chunks+ if delta_type == "input_json_delta":+ state = self._active_blocks.get(index) if index is not None else None+ if isinstance(state, _ToolBlockState):+ state.argument_parts.append(_string_value(delta.get("partial_json")))+ return []++ def _handle_content_block_stop(self, data: Mapping[str, Any]) -> list[str]:+ index = _event_index(data)+ if index is None:+ return []+ state = self._active_blocks.pop(index, None)+ if state is None:+ return []+ return self._complete_block(state)++ def _handle_message_delta(self, data: Mapping[str, Any]) -> None:+ usage = data.get("usage")+ if not isinstance(usage, dict):+ return+ if isinstance(usage.get("input_tokens"), int):+ self._input_tokens = usage["input_tokens"]+ if isinstance(usage.get("output_tokens"), int):+ self._output_tokens = usage["output_tokens"]++ def _start_text_block(self, index: int) -> tuple[list[str], _TextBlockState]:+ chunks = self._complete_existing_block(index)+ output_index = self._reserve_output_slot()+ state = _TextBlockState(+ index=index,+ output_index=output_index,+ item_id=new_message_item_id(),+ )+ self._active_blocks[index] = state+ item = {+ "id": state.item_id,+ "type": "message",+ "status": "in_progress",+ "role": "assistant",+ "content": [],+ }+ chunks.extend(+ [+ format_response_sse_event(+ "response.output_item.added",+ {+ "type": "response.output_item.added",+ "output_index": output_index,+ "item": item,+ },+ ),+ format_response_sse_event(+ "response.content_part.added",+ {+ "type": "response.content_part.added",+ "item_id": state.item_id,+ "output_index": output_index,+ "content_index": 0,+ "part": {+ "type": "output_text",+ "text": "",+ "annotations": [],+ },+ },+ ),+ ]+ )+ return chunks, state++ def _start_reasoning_block(+ self, index: int, *, encrypted_content: str | None = None+ ) -> tuple[list[str], _ReasoningBlockState]:+ chunks = self._complete_existing_block(index)+ output_index = self._reserve_output_slot()+ state = _ReasoningBlockState(+ index=index,+ output_index=output_index,+ item_id=new_reasoning_item_id(),+ encrypted_content=encrypted_content,+ )+ self._active_blocks[index] = state+ chunks.append(+ format_response_sse_event(+ "response.output_item.added",+ {+ "type": "response.output_item.added",+ "output_index": output_index,+ "item": _reasoning_output_item(state, status="in_progress"),+ },+ )+ )+ return chunks, state++ def _start_tool_block(self, index: int, block: Mapping[str, Any]) -> list[str]:+ chunks = self._complete_existing_block(index)+ identity = responses_tool_identity_from_anthropic_name(+ self._request, _string_value(block.get("name"))+ )+ state = _ToolBlockState(+ index=index,+ output_index=self._reserve_output_slot(),+ item_id=f"{'ctc' if identity.kind == 'custom' else 'fc'}_"+ f"{uuid.uuid4().hex[:24]}",+ call_id=_string_value(block.get("id")) or new_call_id(),+ kind=identity.kind,+ name=identity.name,+ namespace=identity.namespace,+ )+ initial_input = block.get("input")+ if (identity.kind == "custom" and initial_input not in (None, {}, "")) or (+ isinstance(initial_input, dict) and initial_input+ ):+ state.argument_parts.append(json.dumps(initial_input))+ self._active_blocks[index] = state+ chunks.append(+ format_response_sse_event(+ "response.output_item.added",+ {+ "type": "response.output_item.added",+ "output_index": state.output_index,+ "item": self._tool_item(state, status="in_progress"),+ },+ )+ )+ return chunks++ def _emit_text_delta(self, state: _TextBlockState, text: str) -> list[str]:+ if not text:+ return []+ state.text_parts.append(text)+ return [+ format_response_sse_event(+ "response.output_text.delta",+ {+ "type": "response.output_text.delta",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "content_index": 0,+ "delta": text,+ },+ )+ ]++ def _emit_reasoning_delta(+ self, state: _ReasoningBlockState, text: str+ ) -> list[str]:+ if not text:+ return []+ state.text_parts.append(text)+ return [+ format_response_sse_event(+ "response.reasoning_text.delta",+ {+ "type": "response.reasoning_text.delta",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "content_index": 0,+ "delta": text,+ },+ )+ ]++ def _complete_existing_block(self, index: int) -> list[str]:+ existing = self._active_blocks.pop(index, None)+ if existing is None:+ return []+ return self._complete_block(existing)++ def _complete_block(self, state: _BlockState) -> list[str]:+ if isinstance(state, _TextBlockState):+ return self._complete_text_block(state)+ if isinstance(state, _ReasoningBlockState):+ return self._complete_reasoning_block(state)+ return self._complete_tool_block(state)++ def _complete_text_block(self, state: _TextBlockState) -> list[str]:+ text = "".join(state.text_parts)+ item = message_item(state.item_id, text, "completed")+ self._commit_output(state.output_index, item)+ return [+ format_response_sse_event(+ "response.output_text.done",+ {+ "type": "response.output_text.done",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "content_index": 0,+ "text": text,+ },+ ),+ format_response_sse_event(+ "response.content_part.done",+ {+ "type": "response.content_part.done",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "content_index": 0,+ "part": {"type": "output_text", "text": text, "annotations": []},+ },+ ),+ format_response_sse_event(+ "response.output_item.done",+ {+ "type": "response.output_item.done",+ "output_index": state.output_index,+ "item": item,+ },+ ),+ ]++ def _complete_reasoning_block(self, state: _ReasoningBlockState) -> list[str]:+ item = _reasoning_output_item(state, status="completed")+ self._commit_output(state.output_index, item)+ chunks: list[str] = []+ text = "".join(state.text_parts)+ if text:+ chunks.append(+ format_response_sse_event(+ "response.reasoning_text.done",+ {+ "type": "response.reasoning_text.done",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "content_index": 0,+ "text": text,+ },+ )+ )+ chunks.append(+ format_response_sse_event(+ "response.output_item.done",+ {+ "type": "response.output_item.done",+ "output_index": state.output_index,+ "item": item,+ },+ )+ )+ return chunks++ def _complete_tool_block(self, state: _ToolBlockState) -> list[str]:+ if state.kind == "custom":+ return self._complete_custom_tool_block(state)+ arguments = "".join(state.argument_parts) or "{}"+ item = self._tool_item(state, status="completed", arguments=arguments)+ self._commit_output(state.output_index, item)+ chunks: list[str] = []+ if arguments:+ chunks.append(+ format_response_sse_event(+ "response.function_call_arguments.delta",+ {+ "type": "response.function_call_arguments.delta",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "delta": arguments,+ },+ )+ )+ chunks.extend(+ [+ format_response_sse_event(+ "response.function_call_arguments.done",+ {+ "type": "response.function_call_arguments.done",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "arguments": arguments,+ },+ ),+ format_response_sse_event(+ "response.output_item.done",+ {+ "type": "response.output_item.done",+ "output_index": state.output_index,+ "item": item,+ },+ ),+ ]+ )+ return chunks++ def _complete_custom_tool_block(self, state: _ToolBlockState) -> list[str]:+ input_text = custom_tool_input_text_from_arguments(+ "".join(state.argument_parts)+ )+ item = self._tool_item(state, status="completed", input_text=input_text)+ self._commit_output(state.output_index, item)+ chunks: list[str] = []+ if input_text:+ chunks.append(+ format_response_sse_event(+ "response.custom_tool_call_input.delta",+ {+ "type": "response.custom_tool_call_input.delta",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "delta": input_text,+ },+ )+ )+ chunks.extend(+ [+ format_response_sse_event(+ "response.custom_tool_call_input.done",+ {+ "type": "response.custom_tool_call_input.done",+ "item_id": state.item_id,+ "output_index": state.output_index,+ "input": input_text,+ },+ ),+ format_response_sse_event(+ "response.output_item.done",+ {+ "type": "response.output_item.done",+ "output_index": state.output_index,+ "item": item,+ },+ ),+ ]+ )+ return chunks++ def _tool_item(+ self,+ state: _ToolBlockState,+ *,+ status: str,+ arguments: str = "",+ input_text: str = "",+ ) -> dict[str, Any]:+ if state.kind == "custom":+ item = {+ "id": state.item_id,+ "type": "custom_tool_call",+ "status": status,+ "call_id": state.call_id,+ "name": state.name,+ "input": input_text,+ }+ else:+ item = {+ "id": state.item_id,+ "type": "function_call",+ "status": status,+ "call_id": state.call_id,+ "name": state.name,+ "arguments": arguments,+ }+ if state.namespace:+ item["namespace"] = state.namespace+ return item++ def _flush_active_blocks(self) -> list[str]:+ chunks: list[str] = []+ states = sorted(+ self._active_blocks.values(), key=lambda state: state.output_index+ )+ self._active_blocks.clear()+ for state in states:+ chunks.extend(self._complete_block(state))+ return chunks++ def _reserve_output_slot(self) -> int:+ output_index = len(self._output_slots)+ self._output_slots.append(None)+ return output_index++ def _commit_output(self, output_index: int, item: dict[str, Any]) -> None:+ while output_index >= len(self._output_slots):+ self._output_slots.append(None)+ self._output_slots[output_index] = item++ def _output(self) -> list[dict[str, Any]]:+ return [item for item in self._output_slots if item is not None]++ def _usage(self) -> dict[str, int] | None:+ if self._input_tokens is None and self._output_tokens is None:+ return None+ input_tokens = self._input_tokens or 0+ output_tokens = self._output_tokens or 0+ return {+ "input_tokens": input_tokens,+ "output_tokens": output_tokens,+ "total_tokens": input_tokens + output_tokens,+ }++ def _safe_index(self, index: int | None) -> int:+ if index is not None:+ return index+ value = self._fallback_text_index+ self._fallback_text_index -= 1+ return value+++def _event_index(data: Mapping[str, Any]) -> int | None:+ value = data.get("index")+ return value if isinstance(value, int) else None+++def _reasoning_output_item(+ state: _ReasoningBlockState, *, status: str+) -> dict[str, Any]:+ if state.encrypted_content is not None:+ return encrypted_reasoning_item(state.item_id, state.encrypted_content, status)+ return reasoning_item(state.item_id, "".join(state.text_parts), status)+++def _openai_error_from_anthropic_error(data: Mapping[str, Any]) -> dict[str, Any]:+ error = data.get("error")+ if not isinstance(error, dict):+ error = {"type": "api_error", "message": str(data)}+ return {+ "message": str(error.get("message", "")),+ "type": str(error.get("type", "api_error")),+ "param": None,+ "code": None,+ }+++def _string_value(value: Any) -> str:+ if value is None:+ return ""+ return value if isinstance(value, str) else str(value)diff --git a/core/openai_responses/tools.py b/core/openai_responses/tools.pynew file mode 100644index 00000000..08623c37--- /dev/null+++ b/core/openai_responses/tools.py@@ -0,0 +1,372 @@+"""Tool conversion helpers for the OpenAI Responses adapter."""++from __future__ import annotations++import hashlib+import json+import re+from collections.abc import Mapping+from dataclasses import dataclass+from typing import Any, Literal++from .errors import ResponsesConversionError+from .ids import new_call_id++_MAX_ANTHROPIC_TOOL_NAME_LEN = 64+_NAMESPACE_TOOL_SEPARATOR = "__"+_UNSUPPORTED_PASSIVE_TOOL_TYPES = frozenset({"web_search", "image_generation"})+_INVALID_TOOL_NAME_CHARS = re.compile(r"[^A-Za-z0-9_-]+")+++@dataclass(frozen=True, slots=True)+class ResponsesToolIdentity:+ kind: Literal["function", "custom"]+ name: str+ namespace: str | None = None+++def convert_tools(value: Any) -> list[dict[str, Any]] | None:+ if value is None:+ return None+ if not isinstance(value, list):+ raise ResponsesConversionError("Responses tools must be a list")++ tools: list[dict[str, Any]] = []+ for tool in value:+ if not isinstance(tool, dict):+ raise ResponsesConversionError(+ f"Unsupported Responses tool: {type(tool).__name__}"+ )+ tool_type = tool.get("type")+ if tool_type == "function":+ tools.append(_convert_function_tool(tool, namespace=None))+ continue+ if tool_type == "custom":+ tools.append(_convert_custom_tool(tool, namespace=None))+ continue+ if tool_type == "namespace":+ tools.extend(_convert_namespace_tool(tool))+ continue+ if tool_type in _UNSUPPORTED_PASSIVE_TOOL_TYPES:+ continue+ if tool_type != "function":+ raise ResponsesConversionError(+ f"Unsupported Responses tool type: {tool_type!r}"+ )+ return tools+++def convert_tool_choice(value: Any) -> dict[str, Any] | None:+ if value is None or value == "auto":+ return None+ if value == "none":+ return None+ if value == "required":+ return {"type": "any"}+ if isinstance(value, dict):+ choice_type = value.get("type")+ if choice_type == "function":+ namespace = optional_str(value.get("namespace"))+ name = required_str(value.get("name"), "tool_choice.name")+ return {+ "type": "tool",+ "name": responses_tool_name_to_anthropic_name(+ name, namespace=namespace+ ),+ }+ if choice_type == "custom":+ source = _custom_source(value)+ namespace = optional_str(source.get("namespace")) or optional_str(+ value.get("namespace")+ )+ name = required_str(source.get("name"), "tool_choice.name")+ return {+ "type": "tool",+ "name": responses_tool_name_to_anthropic_name(+ name, namespace=namespace+ ),+ }+ if choice_type == "tool":+ namespace = optional_str(value.get("namespace"))+ name = optional_str(value.get("name"))+ if name:+ return {+ "type": "tool",+ "name": responses_tool_name_to_anthropic_name(+ name, namespace=namespace+ ),+ }+ return dict(value)+ if choice_type in {"auto", "any"}:+ return dict(value)+ raise ResponsesConversionError(f"Unsupported Responses tool_choice: {value!r}")+++def responses_tool_name_to_anthropic_name(+ name: str, *, namespace: str | None = None+) -> str:+ """Return a deterministic Anthropic tool name for a Responses tool identity."""++ if not namespace:+ return name+ combined = (+ f"{_tool_name_part(namespace)}"+ f"{_NAMESPACE_TOOL_SEPARATOR}"+ f"{_tool_name_part(name)}"+ )+ if len(combined) <= _MAX_ANTHROPIC_TOOL_NAME_LEN:+ return combined+ digest = hashlib.sha1(combined.encode("utf-8")).hexdigest()[:8]+ prefix_len = _MAX_ANTHROPIC_TOOL_NAME_LEN - len(digest) - 1+ return f"{combined[:prefix_len]}_{digest}"+++def responses_tool_identity_from_anthropic_name(+ request: Mapping[str, Any], anthropic_name: str+) -> ResponsesToolIdentity:+ """Return the Responses namespace/name represented by an Anthropic tool name."""++ tools = request.get("tools")+ if not isinstance(tools, list):+ return ResponsesToolIdentity(kind="function", name=anthropic_name)+ for tool in tools:+ if not isinstance(tool, dict):+ continue+ tool_type = tool.get("type")+ if tool_type == "function":+ source = tool.get("function")+ function = source if isinstance(source, dict) else tool+ if (name := optional_str(function.get("name"))) and (+ responses_tool_name_to_anthropic_name(name) == anthropic_name+ ):+ return ResponsesToolIdentity(kind="function", name=name)+ continue+ if tool_type == "custom":+ source = _custom_source(tool)+ if (name := optional_str(source.get("name"))) and (+ responses_tool_name_to_anthropic_name(name) == anthropic_name+ ):+ return ResponsesToolIdentity(kind="custom", name=name)+ continue+ if tool_type != "namespace":+ continue+ namespace = optional_str(tool.get("name"))+ nested_tools = tool.get("tools")+ if not namespace or not isinstance(nested_tools, list):+ continue+ for nested_tool in nested_tools:+ if not isinstance(nested_tool, dict):+ continue+ nested_tool_type = nested_tool.get("type")+ if nested_tool_type == "function":+ source = nested_tool.get("function")+ function = source if isinstance(source, dict) else nested_tool+ if (name := optional_str(function.get("name"))) and (+ responses_tool_name_to_anthropic_name(name, namespace=namespace)+ == anthropic_name+ ):+ return ResponsesToolIdentity(+ kind="function", name=name, namespace=namespace+ )+ continue+ if nested_tool_type == "custom":+ source = _custom_source(nested_tool)+ if (name := optional_str(source.get("name"))) and (+ responses_tool_name_to_anthropic_name(name, namespace=namespace)+ == anthropic_name+ ):+ return ResponsesToolIdentity(+ kind="custom", name=name, namespace=namespace+ )+ return ResponsesToolIdentity(kind="function", name=anthropic_name)+++def parse_arguments(value: Any) -> dict[str, Any]:+ if value is None or value == "":+ return {}+ if isinstance(value, dict):+ return value+ if not isinstance(value, str):+ raise ResponsesConversionError("Responses function_call arguments must be JSON")+ try:+ parsed = json.loads(value)+ except json.JSONDecodeError as exc:+ raise ResponsesConversionError(+ f"Responses function_call arguments are invalid JSON: {exc.msg}"+ ) from exc+ if not isinstance(parsed, dict):+ raise ResponsesConversionError(+ "Responses function_call arguments must decode to an object"+ )+ return parsed+++def custom_tool_input_to_anthropic(value: Any) -> dict[str, str]:+ return {"input": custom_tool_input_text(value)}+++def custom_tool_input_text(value: Any) -> str:+ if value is None:+ return ""+ if isinstance(value, str):+ return value+ return _json_dumps(value)+++def custom_tool_input_text_from_anthropic(value: Any) -> str:+ if isinstance(value, Mapping):+ raw_input = value.get("input")+ if isinstance(raw_input, str):+ return raw_input+ if raw_input is not None:+ return custom_tool_input_text(raw_input)+ if not value:+ return ""+ return _json_dumps(value)+ return custom_tool_input_text(value)+++def custom_tool_input_text_from_arguments(arguments: str) -> str:+ if not arguments:+ return ""+ try:+ parsed = json.loads(arguments)+ except json.JSONDecodeError:+ return arguments+ return custom_tool_input_text_from_anthropic(parsed)+++def call_id_from_item(item: Mapping[str, Any]) -> str:+ for key in ("call_id", "id"):+ if value := optional_str(item.get(key)):+ return value+ return new_call_id()+++def required_str(value: Any, field_name: str) -> str:+ if isinstance(value, str) and value:+ return value+ raise ResponsesConversionError(+ f"Responses field {field_name} must be a non-empty string"+ )+++def optional_str(value: Any) -> str | None:+ return value if isinstance(value, str) else None+++def _convert_namespace_tool(tool: Mapping[str, Any]) -> list[dict[str, Any]]:+ namespace = required_str(tool.get("name"), "tool.namespace.name")+ nested_tools = tool.get("tools")+ if not isinstance(nested_tools, list):+ raise ResponsesConversionError(+ f"Responses namespace tool {namespace!r} tools must be a list"+ )++ converted_tools: list[dict[str, Any]] = []+ for nested_tool in nested_tools:+ if not isinstance(nested_tool, dict):+ raise ResponsesConversionError(+ f"Unsupported Responses namespace tool: {type(nested_tool).__name__}"+ )+ nested_tool_type = nested_tool.get("type")+ if nested_tool_type == "function":+ converted_tools.append(+ _convert_function_tool(nested_tool, namespace=namespace)+ )+ continue+ if nested_tool_type == "custom":+ converted_tools.append(+ _convert_custom_tool(nested_tool, namespace=namespace)+ )+ continue+ raise ResponsesConversionError(+ f"Unsupported Responses namespace tool type: {nested_tool_type!r}"+ )+ return converted_tools+++def _convert_function_tool(+ tool: Mapping[str, Any], *, namespace: str | None+) -> dict[str, Any]:+ function = tool.get("function")+ source = function if isinstance(function, dict) else tool+ name = required_str(source.get("name"), "tool.name")+ schema = source.get("parameters")+ if schema is None:+ schema = {"type": "object", "properties": {}}+ if not isinstance(schema, dict):+ raise ResponsesConversionError(+ f"Responses tool {name!r} parameters must be an object"+ )+ converted: dict[str, Any] = {+ "name": responses_tool_name_to_anthropic_name(name, namespace=namespace),+ "input_schema": schema,+ }+ if description := optional_str(source.get("description")):+ converted["description"] = description+ return converted+++def _convert_custom_tool(+ tool: Mapping[str, Any], *, namespace: str | None+) -> dict[str, Any]:+ source = _custom_source(tool)+ name = required_str(source.get("name"), "tool.name")+ converted: dict[str, Any] = {+ "name": responses_tool_name_to_anthropic_name(name, namespace=namespace),+ "input_schema": {+ "type": "object",+ "properties": {+ "input": {+ "type": "string",+ "description": "Free-form input for the custom tool.",+ }+ },+ "required": ["input"],+ },+ }+ if description := _custom_tool_description(source):+ converted["description"] = description+ return converted+++def _custom_source(tool: Mapping[str, Any]) -> Mapping[str, Any]:+ custom = tool.get("custom")+ return custom if isinstance(custom, Mapping) else tool+++def _custom_tool_description(source: Mapping[str, Any]) -> str | None:+ parts: list[str] = []+ if description := optional_str(source.get("description")):+ parts.append(description)+ format_value = source.get("format")+ if isinstance(format_value, Mapping):+ format_type = optional_str(format_value.get("type"))+ if format_type == "text":+ parts.append("Custom tool input format: unconstrained text.")+ elif format_type == "grammar":+ syntax = optional_str(format_value.get("syntax"))+ definition = optional_str(format_value.get("definition"))+ guidance = "Custom tool input format: grammar"+ if syntax:+ guidance = f"{guidance} ({syntax})"+ guidance = f"{guidance}: {definition}" if definition else f"{guidance}."+ parts.append(guidance)+ elif format_type:+ parts.append(f"Custom tool input format: {format_type}.")+ else:+ parts.append(f"Custom tool input format: {_json_dumps(format_value)}")+ return "\n\n".join(parts) if parts else None+++def _tool_name_part(value: str) -> str:+ normalized = _INVALID_TOOL_NAME_CHARS.sub("_", value).strip("_")+ return normalized or "tool"+++def _json_dumps(value: Any) -> str:+ try:+ return json.dumps(value)+ except TypeError:+ return str(value)diff --git a/pyproject.toml b/pyproject.tomlindex 05b520a5..f25f2a17 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build"[project]name = "free-claude-code"-version = "2.3.2"+version = "2.3.3"description = "Middleware between Claude Code CLI (Anthropic API) and NVIDIA NIM"readme = "README.md"requires-python = ">=3.14.0"diff --git a/smoke/capabilities.py b/smoke/capabilities.pyindex 38bf02be..ef6f1d06 100644--- a/smoke/capabilities.py+++ b/smoke/capabilities.py@@ -66,7 +66,11 @@ CAPABILITY_CONTRACTS: tuple[CapabilityContract, ...] = ("OpenAI Responses requests from Codex","Responses SSE or JSON response","OpenAI-shaped error or conversion error",- ("tests/api/test_openai_responses.py",),+ (+ "tests/api/test_openai_responses.py",+ "tests/core/openai_responses/test_sse.py",+ "tests/cli/test_adapters.py",+ ),("test_probe_and_models_routes","test_provider_codex_responses_text_e2e",diff --git a/smoke/features.py b/smoke/features.pyindex 8485d49c..b11e4f32 100644--- a/smoke/features.py+++ b/smoke/features.py@@ -91,7 +91,11 @@ FEATURE_INVENTORY: tuple[FeatureCoverage, ...] = ("drop_in_codex_replacement","OpenAI Responses API and Codex CLI adapter route through the proxy","readme",- ("tests/api/test_openai_responses.py", "tests/cli/test_adapters.py"),+ (+ "tests/api/test_openai_responses.py",+ "tests/cli/test_adapters.py",+ "tests/core/openai_responses/test_sse.py",+ ),("test_probe_and_models_routes",),("test_provider_codex_responses_text_e2e",),("api", "providers"),diff --git a/tests/api/test_openai_responses.py b/tests/api/test_openai_responses.pyindex 9d1efd5c..fb8bed47 100644--- a/tests/api/test_openai_responses.py+++ b/tests/api/test_openai_responses.py@@ -55,7 +55,6 @@ def test_create_response_stream_routes_through_provider(json={"model": "nvidia_nim/test-model","input": "Hello",- "stream": True,"max_output_tokens": 32,},)@@ -76,10 +75,35 @@ def test_create_response_stream_routes_through_provider(assert routed.max_tokens == 32-def test_create_response_non_stream_collects_response(+def test_create_response_stream_bypasses_local_message_optimizations() -> None:+ provider = FakeProvider(_anthropic_text_stream("Provider response"))+ app = create_app(lifespan_enabled=False)+ with (+ patch("api.dependencies.resolve_provider", return_value=provider),+ patch(+ "api.services.try_optimizations",+ side_effect=AssertionError("Responses must not use message optimizations"),+ ),+ TestClient(app) as client,+ ):+ response = client.post(+ "/v1/responses",+ json={+ "model": "nvidia_nim/test-model",+ "input": "quota check",+ },+ )++ assert response.status_code == 200+ completed = parse_sse_text(response.text)[-1].data["response"]+ assert completed["output"][0]["content"][0]["text"] == "Provider response"+ assert provider.requests[0].messages[0].content == "quota check"+++def test_create_response_stream_false_returns_openai_error(responses_client: tuple[TestClient, FakeProvider],) -> None:- client, _provider = responses_client+ client, provider = responses_clientresponse = client.post("/v1/responses",@@ -90,11 +114,11 @@ def test_create_response_non_stream_collects_response(},)- assert response.status_code == 200+ assert response.status_code == 400payload = response.json()- assert payload["object"] == "response"- assert payload["status"] == "completed"- assert payload["output"][0]["content"][0]["text"] == "Hello from provider"+ assert payload["error"]["type"] == "invalid_request_error"+ assert "streaming only" in payload["error"]["message"]+ assert provider.requests == []def test_create_response_stream_preserves_interleaved_reasoning_order() -> None:@@ -138,31 +162,6 @@ def test_create_response_stream_preserves_interleaved_reasoning_order() -> None:assert completed["output"][4]["content"][0]["text"] == "final answer"-def test_create_response_non_stream_preserves_reasoning_items() -> None:- provider = FakeProvider(_anthropic_reasoning_text_stream())- app = create_app(lifespan_enabled=False)- with (- patch("api.dependencies.resolve_provider", return_value=provider),- TestClient(app) as client,- ):- response = client.post(- "/v1/responses",- json={- "model": "nvidia_nim/test-model",- "input": "Think then answer",- "stream": False,- },- )-- assert response.status_code == 200- payload = response.json()- assert [item["type"] for item in payload["output"]] == ["reasoning", "message"]- assert payload["output"][0]["content"] == [- {"type": "reasoning_text", "text": "provider reasoning"}- ]- assert payload["output"][1]["content"][0]["text"] == "provider answer"--def test_create_response_tool_stream_emits_function_call() -> None:provider = FakeProvider(_anthropic_tool_stream())app = create_app(lifespan_enabled=False)@@ -238,6 +237,92 @@ def test_create_response_accepts_codex_namespace_tool_request() -> None:assert call["name"] == "js"+def test_create_response_accepts_codex_custom_tool_request() -> None:+ provider = FakeProvider(+ _anthropic_tool_stream(+ tool_name="apply_patch",+ partial_json='{"input":"*** Begin Patch"}',+ )+ )+ app = create_app(lifespan_enabled=False)+ with (+ patch("api.dependencies.resolve_provider", return_value=provider),+ TestClient(app) as client,+ ):+ response = client.post(+ "/v1/responses",+ json={+ "model": "nvidia_nim/test-model",+ "input": "Use apply_patch",+ "stream": True,+ "tools": [+ {+ "type": "custom",+ "name": "apply_patch",+ "description": "Apply repo patches",+ "format": {"type": "text"},+ }+ ],+ "tool_choice": {"type": "custom", "name": "apply_patch"},+ },+ )++ assert response.status_code == 200+ routed = provider.requests[0]+ assert [tool.name for tool in routed.tools] == ["apply_patch"]+ assert routed.tool_choice == {"type": "tool", "name": "apply_patch"}+ events = parse_sse_text(response.text)+ assert "response.custom_tool_call_input.delta" in [event.event for event in events]+ completed = events[-1].data["response"]+ call = completed["output"][0]+ assert call["type"] == "custom_tool_call"+ assert call["name"] == "apply_patch"+ assert call["input"] == "*** Begin Patch"+++def test_create_response_stream_provider_error_returns_response_failed() -> None:+ provider = FakeProvider(+ [+ format_sse_event(+ "error",+ {+ "type": "error",+ "error": {+ "type": "api_error",+ "message": "provider failed",+ },+ },+ )+ ]+ )+ app = create_app(lifespan_enabled=False)+ with (+ patch("api.dependencies.resolve_provider", return_value=provider),+ TestClient(app) as client,+ ):+ response = client.post(+ "/v1/responses",+ json={+ "model": "nvidia_nim/test-model",+ "input": "Hello",+ "stream": True,+ },+ )++ assert response.status_code == 200+ events = parse_sse_text(response.text)+ assert [event.event for event in events] == ["response.created", "response.failed"]+ failed = events[-1].data["response"]+ assert failed["id"] == events[0].data["response"]["id"]+ assert failed["status"] == "failed"+ assert failed["error"] == {+ "message": "provider failed",+ "type": "api_error",+ "param": None,+ "code": None,+ }++def test_create_response_replays_prior_reasoning_as_reasoning_content() -> None:provider = FakeProvider(_anthropic_text_stream("done"))app = create_app(lifespan_enabled=False)@@ -415,7 +500,9 @@ def _anthropic_text_stream(text: str) -> list[str]:]-def _anthropic_tool_stream(tool_name: str = "echo") -> list[str]:+def _anthropic_tool_stream(+ tool_name: str = "echo", partial_json: str = '{"value":"FCC"}'+) -> list[str]:return [format_sse_event("message_start", {"type": "message_start", "message": {}}),format_sse_event(@@ -438,7 +525,7 @@ def _anthropic_tool_stream(tool_name: str = "echo") -> list[str]:"index": 0,"delta": {"type": "input_json_delta",- "partial_json": '{"value":"FCC"}',+ "partial_json": partial_json,},},),diff --git a/tests/cli/test_adapters.py b/tests/cli/test_adapters.pyindex 31094442..a5f8b316 100644--- a/tests/cli/test_adapters.py+++ b/tests/cli/test_adapters.py@@ -346,6 +346,306 @@ def test_codex_adapter_parses_response_text_delta() -> None:]+def test_codex_adapter_marks_streamed_message_items_seen() -> None:+ state = CliParseState()+ item = {+ "type": "message",+ "id": "msg1",+ "content": [{"type": "output_text", "text": "hi"}],+ }++ delta_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.output_text.delta",+ "item_id": "msg1",+ "output_index": 0,+ "delta": "hi",+ }+ ),+ state,+ )+ )+ item_done_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps({"type": "response.output_item.done", "item": item}),+ state,+ )+ )+ completed_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [item]},+ }+ ),+ state,+ )+ )++ assert delta_events == [+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "hi"},+ }+ ]+ assert item_done_events == []+ assert completed_events == []+++def test_codex_adapter_dedupes_output_index_only_message_done() -> None:+ state = CliParseState()+ item = {+ "type": "message",+ "content": [{"type": "output_text", "text": "hi"}],+ }++ delta_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.output_text.delta",+ "output_index": 0,+ "delta": "hi",+ }+ ),+ state,+ )+ )+ item_done_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.output_item.done",+ "output_index": 0,+ "item": item,+ }+ ),+ state,+ )+ )++ assert delta_events == [+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "hi"},+ }+ ]+ assert item_done_events == []+++def test_codex_adapter_prefers_streamed_output_index_for_final_message_id() -> None:+ state = CliParseState()+ item = {+ "type": "message",+ "id": "msg_1",+ "content": [{"type": "output_text", "text": "hi"}],+ }++ delta_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.output_text.delta",+ "output_index": 0,+ "delta": "hi",+ }+ ),+ state,+ )+ )+ item_done_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.output_item.done",+ "output_index": 0,+ "item": item,+ }+ ),+ state,+ )+ )+ completed_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [item]},+ }+ ),+ state,+ )+ )++ assert delta_events == [+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "hi"},+ }+ ]+ assert item_done_events == []+ assert completed_events == []+++def test_codex_adapter_dedupes_output_index_only_completed_message() -> None:+ state = CliParseState()+ item = {+ "type": "message",+ "content": [{"type": "output_text", "text": "hi"}],+ }++ delta_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.output_text.delta",+ "output_index": 0,+ "delta": "hi",+ }+ ),+ state,+ )+ )+ completed_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [item]},+ }+ ),+ state,+ )+ )++ assert delta_events == [+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "hi"},+ }+ ]+ assert completed_events == []+++def test_codex_adapter_output_index_dedupe_scope_advances_after_terminal() -> None:+ state = CliParseState()+ streamed_item = {+ "type": "message",+ "content": [{"type": "output_text", "text": "first"}],+ }+ fallback_item = {+ "type": "message",+ "content": [{"type": "output_text", "text": "second"}],+ }++ list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.output_text.delta",+ "output_index": 0,+ "delta": "first",+ }+ ),+ state,+ )+ )+ first_completed_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [streamed_item]},+ }+ ),+ state,+ )+ )+ second_completed_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [fallback_item]},+ }+ ),+ state,+ )+ )++ assert first_completed_events == []+ assert second_completed_events == [+ {+ "type": "assistant",+ "message": {"content": [{"type": "text", "text": "second"}]},+ }+ ]+++def test_codex_adapter_output_index_dedupe_scope_advances_after_failed_response() -> (+ None+):+ state = CliParseState()+ fallback_item = {+ "type": "message",+ "content": [{"type": "output_text", "text": "second"}],+ }++ list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.output_text.delta",+ "output_index": 0,+ "delta": "first",+ }+ ),+ state,+ )+ )+ failed_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.failed",+ "response": {+ "error": {+ "message": "upstream failed",+ },+ },+ }+ ),+ state,+ )+ )+ completed_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [fallback_item]},+ }+ ),+ state,+ )+ )++ assert failed_events == [{"type": "error", "error": {"message": "upstream failed"}}]+ assert completed_events == [+ {+ "type": "assistant",+ "message": {"content": [{"type": "text", "text": "second"}]},+ }+ ]++def test_codex_adapter_parses_response_reasoning_text_delta() -> None:events = list(CODEX_CLI_ADAPTER.parse_stdout_line(@@ -399,6 +699,149 @@ def test_codex_adapter_parses_completed_function_call_item() -> None:]+def test_codex_adapter_parses_completed_custom_tool_call_item() -> None:+ line = json.dumps(+ {+ "type": "response.output_item.done",+ "item": {+ "type": "custom_tool_call",+ "call_id": "call_1",+ "name": "apply_patch",+ "input": "*** Begin Patch",+ },+ }+ )++ events = list(CODEX_CLI_ADAPTER.parse_stdout_line(line, CliParseState()))++ assert events == [+ {+ "type": "assistant",+ "message": {+ "content": [+ {+ "type": "tool_use",+ "id": "call_1",+ "name": "apply_patch",+ "input": {"input": "*** Begin Patch"},+ }+ ]+ },+ }+ ]+++def test_codex_adapter_dedupes_output_item_done_against_completed_output() -> None:+ state = CliParseState()+ item = {+ "type": "custom_tool_call",+ "id": "ctc_1",+ "call_id": "call_1",+ "name": "apply_patch",+ "input": "*** Begin Patch",+ }++ item_done_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps({"type": "response.output_item.done", "item": item}),+ state,+ )+ )+ completed_events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [item]},+ }+ ),+ state,+ )+ )++ assert item_done_events == [+ {+ "type": "assistant",+ "message": {+ "content": [+ {+ "type": "tool_use",+ "id": "call_1",+ "name": "apply_patch",+ "input": {"input": "*** Begin Patch"},+ }+ ]+ },+ }+ ]+ assert completed_events == []+++def test_codex_adapter_completed_output_is_fallback_for_unseen_message_item() -> None:+ item = {+ "type": "message",+ "id": "msg1",+ "content": [{"type": "output_text", "text": "hi"}],+ }++ events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [item]},+ }+ ),+ CliParseState(),+ )+ )++ assert events == [+ {+ "type": "assistant",+ "message": {"content": [{"type": "text", "text": "hi"}]},+ }+ ]+++def test_codex_adapter_completed_output_is_fallback_for_unseen_tool_item() -> None:+ item = {+ "type": "function_call",+ "id": "fc_1",+ "call_id": "call_1",+ "name": "echo",+ "arguments": '{"value":"FCC"}',+ }++ events = list(+ CODEX_CLI_ADAPTER.parse_stdout_line(+ json.dumps(+ {+ "type": "response.completed",+ "response": {"output": [item]},+ }+ ),+ CliParseState(),+ )+ )++ assert events == [+ {+ "type": "assistant",+ "message": {+ "content": [+ {+ "type": "tool_use",+ "id": "call_1",+ "name": "echo",+ "input": {"value": "FCC"},+ }+ ]+ },+ }+ ]++def test_codex_adapter_reasoning_summary_delta_remains_raw() -> None:events = list(CODEX_CLI_ADAPTER.parse_stdout_line(diff --git a/tests/contracts/test_import_boundaries.py b/tests/contracts/test_import_boundaries.pyindex 37357d4c..9ee9c073 100644--- a/tests/contracts/test_import_boundaries.py+++ b/tests/contracts/test_import_boundaries.py@@ -155,6 +155,56 @@ def test_provider_transports_live_under_transport_family_packages() -> None:assert offenders == []+def test_openai_responses_uses_adapter_boundary() -> None:+ repo_root = Path(__file__).resolve().parents[2]+ responses_root = repo_root / "core" / "openai_responses"++ assert not (responses_root / "conversion.py").exists()+ assert not (responses_root / "sse.py").exists()+ assert not (responses_root / "output.py").exists()+ for filename in {+ "adapter.py",+ "anthropic_sse.py",+ "errors.py",+ "events.py",+ "ids.py",+ "input.py",+ "items.py",+ "reasoning.py",+ "stream.py",+ "stream_state.py",+ "tools.py",+ }:+ assert (responses_root / filename).exists()++ services_text = (repo_root / "api" / "services.py").read_text(encoding="utf-8")+ assert "from core.openai_responses import OpenAIResponsesAdapter" in services_text+ for old_helper in {+ "responses_request_to_anthropic_payload",+ "anthropic_message_response_to_openai_response",+ "iter_anthropic_sse_as_openai_responses",+ "collect_openai_response_from_anthropic_sse",+ "iter_message_response_as_openai_responses",+ }:+ assert old_helper not in services_text++ offenders: list[str] = []+ for path in (repo_root / "api").rglob("*.py"):+ for imported in _imports_from(path, repo_root):+ if imported is not None and imported.startswith("core.openai_responses."):+ rel = path.relative_to(repo_root)+ offenders.append(f"{rel}: {imported}")+ assert sorted(offenders) == []++ adapter_text = (responses_root / "adapter.py").read_text(encoding="utf-8")+ for deleted_api in {+ "from_anthropic_message",+ "collect_from_anthropic_sse",+ "iter_sse_from_anthropic_message",+ }:+ assert deleted_api not in adapter_text++def _imports_matching(roots: list[Path], *, forbidden_prefixes: tuple[str, ...]) -> list[str]:diff --git a/tests/core/openai_responses/test_conversion.py b/tests/core/openai_responses/test_conversion.pyindex 10425caa..f4055446 100644--- a/tests/core/openai_responses/test_conversion.py+++ b/tests/core/openai_responses/test_conversion.py@@ -2,15 +2,14 @@ from __future__ import annotationsimport pytest-from core.openai_responses import (- ResponsesConversionError,- anthropic_message_response_to_openai_response,- responses_request_to_anthropic_payload,-)+from core.openai_responses import OpenAIResponsesAdapter++_ADAPTER = OpenAIResponsesAdapter()+_CONVERSION_ERROR = OpenAIResponsesAdapter.ConversionErrordef test_responses_string_input_converts_to_anthropic_message() -> None:- payload = responses_request_to_anthropic_payload(+ payload = _ADAPTER.to_anthropic_payload({"model": "nvidia_nim/test-model","instructions": "System instructions",@@ -32,7 +31,7 @@ def test_responses_string_input_converts_to_anthropic_message() -> None:def test_responses_messages_tools_and_tool_results_convert() -> None:- payload = responses_request_to_anthropic_payload(+ payload = _ADAPTER.to_anthropic_payload({"model": "deepseek/deepseek-chat","input": [@@ -112,7 +111,7 @@ def test_responses_messages_tools_and_tool_results_convert() -> None:def test_responses_tool_choice_none_disables_forwarded_tools() -> None:- payload = responses_request_to_anthropic_payload(+ payload = _ADAPTER.to_anthropic_payload({"model": "deepseek/deepseek-chat","input": "Reply without tools",@@ -135,7 +134,7 @@ def test_responses_tool_choice_none_disables_forwarded_tools() -> None:def test_responses_namespace_tools_flatten_for_anthropic() -> None:- payload = responses_request_to_anthropic_payload(+ payload = _ADAPTER.to_anthropic_payload({"model": "nvidia_nim/test-model","input": "Use JS",@@ -180,8 +179,114 @@ def test_responses_namespace_tools_flatten_for_anthropic() -> None:assert payload["tool_choice"] == {"type": "tool", "name": "mcp__node_repl__js"}+def test_responses_namespaced_tool_choice_type_tool_flattens_for_anthropic() -> None:+ payload = _ADAPTER.to_anthropic_payload(+ {+ "model": "nvidia_nim/test-model",+ "input": "Use JS",+ "tools": [+ {+ "type": "namespace",+ "name": "mcp__node_repl",+ "tools": [+ {+ "type": "function",+ "name": "js",+ "parameters": {"type": "object", "properties": {}},+ }+ ],+ }+ ],+ "tool_choice": {+ "type": "tool",+ "namespace": "mcp__node_repl",+ "name": "js",+ },+ }+ )++ assert payload["tool_choice"] == {"type": "tool", "name": "mcp__node_repl__js"}+++def test_responses_custom_tool_converts_to_anthropic_string_tool() -> None:+ payload = _ADAPTER.to_anthropic_payload(+ {+ "model": "nvidia_nim/test-model",+ "input": "Use apply_patch",+ "tools": [+ {+ "type": "custom",+ "name": "apply_patch",+ "description": "Apply a repo patch",+ "format": {+ "type": "grammar",+ "syntax": "lark",+ "definition": "start: /.+/",+ },+ }+ ],+ "tool_choice": {"type": "custom", "name": "apply_patch"},+ }+ )++ assert payload["tools"] == [+ {+ "name": "apply_patch",+ "description": (+ "Apply a repo patch\n\n"+ "Custom tool input format: grammar (lark): start: /.+/"+ ),+ "input_schema": {+ "type": "object",+ "properties": {+ "input": {+ "type": "string",+ "description": "Free-form input for the custom tool.",+ }+ },+ "required": ["input"],+ },+ }+ ]+ assert payload["tool_choice"] == {"type": "tool", "name": "apply_patch"}+++def test_responses_namespaced_custom_tool_flattens_for_anthropic() -> None:+ payload = _ADAPTER.to_anthropic_payload(+ {+ "model": "nvidia_nim/test-model",+ "input": "Use shell",+ "tools": [+ {+ "type": "namespace",+ "name": "mcp__shell",+ "tools": [+ {+ "type": "custom",+ "name": "exec",+ "description": "Run shell text",+ "format": {"type": "text"},+ }+ ],+ }+ ],+ "tool_choice": {+ "type": "custom",+ "namespace": "mcp__shell",+ "custom": {"name": "exec"},+ },+ }+ )++ assert payload["tools"][0]["name"] == "mcp__shell__exec"+ assert payload["tools"][0]["description"] == (+ "Run shell text\n\nCustom tool input format: unconstrained text."+ )+ assert payload["tool_choice"] == {"type": "tool", "name": "mcp__shell__exec"}++def test_responses_passive_codex_built_in_tools_are_ignored() -> None:- payload = responses_request_to_anthropic_payload(+ payload = _ADAPTER.to_anthropic_payload({"model": "nvidia_nim/test-model","input": "Hello",@@ -203,7 +308,7 @@ def test_responses_passive_codex_built_in_tools_are_ignored() -> None:def test_responses_namespaced_prior_function_call_flattens_tool_use_name() -> None:- payload = responses_request_to_anthropic_payload(+ payload = _ADAPTER.to_anthropic_payload({"model": "nvidia_nim/test-model","input": [@@ -226,46 +331,55 @@ def test_responses_namespaced_prior_function_call_flattens_tool_use_name() -> Noassert payload["messages"][0]["content"][0]["name"] == "mcp__node_repl__js"-def test_anthropic_namespaced_tool_response_restores_responses_namespace() -> None:- response = anthropic_message_response_to_openai_response(+def test_responses_prior_custom_tool_call_flattens_tool_use_name() -> None:+ payload = _ADAPTER.to_anthropic_payload(+ {+ "model": "nvidia_nim/test-model",+ "input": [+ {+ "type": "custom_tool_call",+ "call_id": "call_1",+ "namespace": "mcp__shell",+ "name": "exec",+ "input": "printf FCC",+ },+ {+ "type": "custom_tool_call_output",+ "call_id": "call_1",+ "output": "FCC",+ },+ ],+ }+ )++ assert payload["messages"] == [{+ "role": "assistant","content": [{"type": "tool_use",- "id": "toolu_1",- "name": "mcp__node_repl__js",- "input": {"code": "1+1"},+ "id": "call_1",+ "name": "mcp__shell__exec",+ "input": {"input": "printf FCC"},}- ]+ ],},{- "model": "nvidia_nim/test-model",- "tools": [+ "role": "user",+ "content": [{- "type": "namespace",- "name": "mcp__node_repl",- "tools": [- {- "type": "function",- "name": "js",- "parameters": {"type": "object", "properties": {}},- }- ],+ "type": "tool_result",+ "tool_use_id": "call_1",+ "content": "FCC",}],},- )-- call = response["output"][0]- assert call["namespace"] == "mcp__node_repl"- assert call["name"] == "js"+ ]def test_responses_unsupported_tool_type_is_clear() -> None:- with pytest.raises(- ResponsesConversionError, match="Unsupported Responses tool type"- ):- responses_request_to_anthropic_payload(+ with pytest.raises(_CONVERSION_ERROR, match="Unsupported Responses tool type"):+ _ADAPTER.to_anthropic_payload({"model": "nvidia_nim/test-model","input": "Hello",@@ -275,8 +389,8 @@ def test_responses_unsupported_tool_type_is_clear() -> None:def test_responses_invalid_function_arguments_are_rejected() -> None:- with pytest.raises(ResponsesConversionError, match="invalid JSON"):- responses_request_to_anthropic_payload(+ with pytest.raises(_CONVERSION_ERROR, match="invalid JSON"):+ _ADAPTER.to_anthropic_payload({"model": "nvidia_nim/test-model","input": [diff --git a/tests/core/openai_responses/test_sse.py b/tests/core/openai_responses/test_sse.pyindex 59f259d1..dc3f0957 100644--- a/tests/core/openai_responses/test_sse.py+++ b/tests/core/openai_responses/test_sse.py@@ -1,21 +1,21 @@from __future__ import annotationsfrom collections.abc import AsyncIterator+from typing import Anyimport pytestfrom core.anthropic.sse import format_sse_eventfrom core.anthropic.stream_contracts import parse_sse_text-from core.openai_responses import (- collect_openai_response_from_anthropic_sse,- iter_anthropic_sse_as_openai_responses,-)+from core.openai_responses import OpenAIResponsesAdapter++_ADAPTER = OpenAIResponsesAdapter()@pytest.mark.asyncioasync def test_anthropic_text_stream_converts_to_responses_sse() -> None:text = await _collect_sse(- iter_anthropic_sse_as_openai_responses(+ _ADAPTER.iter_sse_from_anthropic(_aiter(_anthropic_text_stream("Hello Codex")),{"model": "nvidia_nim/test-model", "stream": True},)@@ -38,7 +38,7 @@ async def test_anthropic_text_stream_converts_to_responses_sse() -> None:@pytest.mark.asyncioasync def test_anthropic_tool_stream_converts_to_function_call_item() -> None:text = await _collect_sse(- iter_anthropic_sse_as_openai_responses(+ _ADAPTER.iter_sse_from_anthropic(_aiter(_anthropic_tool_stream()),{"model": "nvidia_nim/test-model", "stream": True},)@@ -59,7 +59,7 @@ async def test_anthropic_tool_stream_converts_to_function_call_item() -> None:@pytest.mark.asyncioasync def test_namespaced_anthropic_tool_stream_restores_responses_namespace() -> None:text = await _collect_sse(- iter_anthropic_sse_as_openai_responses(+ _ADAPTER.iter_sse_from_anthropic(_aiter(_anthropic_tool_stream(tool_name="mcp__node_repl__js")),{"model": "nvidia_nim/test-model",@@ -90,9 +90,46 @@ async def test_namespaced_anthropic_tool_stream_restores_responses_namespace() -@pytest.mark.asyncio-async def test_anthropic_error_stream_converts_to_responses_error_event() -> None:+async def test_anthropic_custom_tool_stream_converts_to_custom_tool_call() -> None:+ text = await _collect_sse(+ _ADAPTER.iter_sse_from_anthropic(+ _aiter(+ _anthropic_tool_stream(+ tool_name="apply_patch",+ partial_json='{"input":"*** Begin Patch"}',+ )+ ),+ {+ "model": "nvidia_nim/test-model",+ "stream": True,+ "tools": [+ {+ "type": "custom",+ "name": "apply_patch",+ "format": {"type": "text"},+ }+ ],+ },+ )+ )++ events = parse_sse_text(text)+ names = [event.event for event in events]+ assert "response.custom_tool_call_input.delta" in names+ assert "response.custom_tool_call_input.done" in names+ assert "response.function_call_arguments.delta" not in names+ completed = events[-1].data["response"]+ custom_call = completed["output"][0]+ assert custom_call["type"] == "custom_tool_call"+ assert custom_call["call_id"] == "toolu_1"+ assert custom_call["name"] == "apply_patch"+ assert custom_call["input"] == "*** Begin Patch"+++async def test_anthropic_error_stream_converts_to_response_failed_event() -> None:text = await _collect_sse(- iter_anthropic_sse_as_openai_responses(+ _ADAPTER.iter_sse_from_anthropic(_aiter([format_sse_event(@@ -113,20 +150,136 @@ async def test_anthropic_error_stream_converts_to_responses_error_event() -> Nonevents = parse_sse_text(text)assert events[0].event == "response.created"- assert events[1].event == "error"- assert events[1].data["error"]["message"] == "upstream failed"+ assert events[1].event == "response.failed"+ failed = events[1].data["response"]+ assert failed["status"] == "failed"+ assert failed["error"]["message"] == "upstream failed"@pytest.mark.asyncio-async def test_collect_non_stream_response_from_anthropic_sse() -> None:- response = await collect_openai_response_from_anthropic_sse(- _aiter(_anthropic_text_stream("Non-stream")),- {"model": "deepseek/deepseek-chat", "stream": False},+async def test_split_usage_deltas_are_accumulated() -> None:+ response = await _completed_response_from_sse(+ _aiter(+ [+ *_anthropic_text_stream("usage")[:-2],+ format_sse_event(+ "message_delta",+ {+ "type": "message_delta",+ "delta": {"stop_reason": "end_turn"},+ "usage": {"input_tokens": 11},+ },+ ),+ format_sse_event(+ "message_delta",+ {+ "type": "message_delta",+ "delta": {},+ "usage": {"output_tokens": 7},+ },+ ),+ format_sse_event("message_stop", {"type": "message_stop"}),+ ]+ ),+ {"model": "nvidia_nim/test-model", "stream": True},)- assert response["status"] == "completed"- assert response["model"] == "deepseek/deepseek-chat"- assert response["output"][0]["content"][0]["text"] == "Non-stream"+ assert response["usage"] == {+ "input_tokens": 11,+ "output_tokens": 7,+ "total_tokens": 18,+ }+ assert "stop_reason" not in response+++ ("request_payload", "tool_name", "partial_json", "expected_type", "expected_field"),+ [+ (+ {"model": "nvidia_nim/test-model", "stream": True},+ "echo",+ '{"value":"FCC"}',+ "function_call",+ ("arguments", '{"value":"FCC"}'),+ ),+ (+ {+ "model": "nvidia_nim/test-model",+ "stream": True,+ "tools": [{"type": "custom", "name": "apply_patch"}],+ },+ "apply_patch",+ '{"input":"*** Begin Patch"}',+ "custom_tool_call",+ ("input", "*** Begin Patch"),+ ),+ ],+)+async def test_pending_tool_blocks_flush_on_message_stop_and_eof(+ request_payload: dict[str, object],+ tool_name: str,+ partial_json: str,+ expected_type: str,+ expected_field: tuple[str, str],+) -> None:+ stream = _anthropic_tool_stream(+ tool_name=tool_name, partial_json=partial_json, include_block_stop=False+ )+ message_stop_response = await _completed_response_from_sse(+ _aiter(stream), request_payload+ )+ eof_response = await _completed_response_from_sse(+ _aiter(stream[:-1]), request_payload+ )++ for response in (message_stop_response, eof_response):+ call = response["output"][0]+ assert call["type"] == expected_type+ assert call[expected_field[0]] == expected_field[1]+++async def test_overlapping_text_and_tool_blocks_keep_reserved_output_indexes() -> None:+ text = await _collect_sse(+ _ADAPTER.iter_sse_from_anthropic(+ _aiter(_overlapping_text_tool_stream()),+ {"model": "nvidia_nim/test-model", "stream": True},+ )+ )++ events = parse_sse_text(text)+ item_events = [+ (event.event, event.data["output_index"])+ for event in events+ if event.event in {"response.output_item.added", "response.output_item.done"}+ ]+ assert item_events == [+ ("response.output_item.added", 0),+ ("response.output_item.added", 1),+ ("response.output_item.done", 1),+ ("response.output_item.done", 0),+ ]+ completed = events[-1].data["response"]+ assert [item["type"] for item in completed["output"]] == [+ "message",+ "function_call",+ ]+ assert completed["output"][0]["content"][0]["text"] == "text"+ assert completed["output"][1]["arguments"] == '{"value":"FCC"}'+++async def test_overlapping_text_blocks_do_not_merge_content_by_index() -> None:+ response = await _completed_response_from_sse(+ _aiter(_overlapping_text_stream()),+ {"model": "nvidia_nim/test-model", "stream": True},+ )++ assert [item["content"][0]["text"] for item in response["output"]] == [+ "A1-A2",+ "B1-B2",+ ]async def _collect_sse(chunks: AsyncIterator[str]) -> str:@@ -134,6 +287,16 @@ async def _collect_sse(chunks: AsyncIterator[str]) -> str:return "".join(parts)+async def _completed_response_from_sse(+ chunks: AsyncIterator[str],+ request: dict[str, object],+) -> dict[str, Any]:+ text = await _collect_sse(_ADAPTER.iter_sse_from_anthropic(chunks, request))+ events = parse_sse_text(text)+ assert events[-1].event == "response.completed"+ return events[-1].data["response"]++async def _aiter(chunks: list[str]) -> AsyncIterator[str]:for chunk in chunks:yield chunk@@ -174,8 +337,13 @@ def _anthropic_text_stream(text: str) -> list[str]:]-def _anthropic_tool_stream(tool_name: str = "echo") -> list[str]:- return [+def _anthropic_tool_stream(+ tool_name: str = "echo",+ partial_json: str = '{"value":"FCC"}',+ *,+ include_block_stop: bool = True,+) -> list[str]:+ chunks = [format_sse_event("message_start", {"type": "message_start", "message": {}}),format_sse_event("content_block_start",@@ -195,12 +363,83 @@ def _anthropic_tool_stream(tool_name: str = "echo") -> list[str]:{"type": "content_block_delta","index": 0,+ "delta": {+ "type": "input_json_delta",+ "partial_json": partial_json,+ },+ },+ ),+ ]+ if include_block_stop:+ chunks.append(+ format_sse_event(+ "content_block_stop",+ {"type": "content_block_stop", "index": 0},+ )+ )+ chunks.extend(+ [+ format_sse_event(+ "message_delta",+ {+ "type": "message_delta",+ "delta": {"stop_reason": "tool_use", "stop_sequence": None},+ "usage": {"input_tokens": 3, "output_tokens": 4},+ },+ ),+ format_sse_event("message_stop", {"type": "message_stop"}),+ ]+ )+ return chunks+++def _overlapping_text_tool_stream() -> list[str]:+ return [+ format_sse_event("message_start", {"type": "message_start", "message": {}}),+ format_sse_event(+ "content_block_start",+ {+ "type": "content_block_start",+ "index": 0,+ "content_block": {"type": "text", "text": ""},+ },+ ),+ format_sse_event(+ "content_block_delta",+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "text"},+ },+ ),+ format_sse_event(+ "content_block_start",+ {+ "type": "content_block_start",+ "index": 1,+ "content_block": {+ "type": "tool_use",+ "id": "toolu_1",+ "name": "echo",+ "input": {},+ },+ },+ ),+ format_sse_event(+ "content_block_delta",+ {+ "type": "content_block_delta",+ "index": 1,"delta": {"type": "input_json_delta","partial_json": '{"value":"FCC"}',},},),+ format_sse_event(+ "content_block_stop",+ {"type": "content_block_stop", "index": 1},+ ),format_sse_event("content_block_stop",{"type": "content_block_stop", "index": 0},@@ -215,3 +454,66 @@ def _anthropic_tool_stream(tool_name: str = "echo") -> list[str]:),format_sse_event("message_stop", {"type": "message_stop"}),]+++def _overlapping_text_stream() -> list[str]:+ return [+ format_sse_event("message_start", {"type": "message_start", "message": {}}),+ format_sse_event(+ "content_block_start",+ {+ "type": "content_block_start",+ "index": 0,+ "content_block": {"type": "text", "text": ""},+ },+ ),+ format_sse_event(+ "content_block_delta",+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "A1-"},+ },+ ),+ format_sse_event(+ "content_block_start",+ {+ "type": "content_block_start",+ "index": 1,+ "content_block": {"type": "text", "text": ""},+ },+ ),+ format_sse_event(+ "content_block_delta",+ {+ "type": "content_block_delta",+ "index": 1,+ "delta": {"type": "text_delta", "text": "B1-"},+ },+ ),+ format_sse_event(+ "content_block_delta",+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "A2"},+ },+ ),+ format_sse_event(+ "content_block_stop",+ {"type": "content_block_stop", "index": 0},+ ),+ format_sse_event(+ "content_block_delta",+ {+ "type": "content_block_delta",+ "index": 1,+ "delta": {"type": "text_delta", "text": "B2"},+ },+ ),+ format_sse_event(+ "content_block_stop",+ {"type": "content_block_stop", "index": 1},+ ),+ format_sse_event("message_stop", {"type": "message_stop"}),+ ]diff --git a/uv.lock b/uv.lockindex 1a28163f..c9ae7552 100644--- a/uv.lock+++ b/uv.lock@@ -561,7 +561,7 @@ wheels = [[[package]]name = "free-claude-code"-version = "2.3.2"+version = "2.3.3"source = { editable = "." }dependencies = [{ name = "aiohttp" },