wspx 0.1.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. wspx/__init__.py +24 -0
  2. wspx/_version.py +1 -0
  3. wspx/addressing.py +415 -0
  4. wspx/auth.py +77 -0
  5. wspx/cli/__init__.py +1 -0
  6. wspx/cli/client.py +124 -0
  7. wspx/cli/config.py +89 -0
  8. wspx/cli/connect.py +88 -0
  9. wspx/cli/main.py +848 -0
  10. wspx/cloud/__init__.py +20 -0
  11. wspx/cloud/app.py +191 -0
  12. wspx/cloud/backend.py +141 -0
  13. wspx/cloud/compose.py +85 -0
  14. wspx/cloud/deploy.py +102 -0
  15. wspx/config.py +189 -0
  16. wspx/control/__init__.py +5 -0
  17. wspx/control/app.py +488 -0
  18. wspx/control/backend.py +244 -0
  19. wspx/control/gateway_client.py +79 -0
  20. wspx/control/mcp_server.py +71 -0
  21. wspx/control/sessions.py +424 -0
  22. wspx/control/tracedb.py +183 -0
  23. wspx/control/ui/assets/index-BJFweZAn.css +1 -0
  24. wspx/control/ui/assets/index-DhUVhWIK.js +10 -0
  25. wspx/control/ui/index.html +13 -0
  26. wspx/control/views.py +180 -0
  27. wspx/diagnostics.py +146 -0
  28. wspx/driver/__init__.py +7 -0
  29. wspx/driver/loop.py +255 -0
  30. wspx/driver/messages.py +62 -0
  31. wspx/driver/providers/__init__.py +84 -0
  32. wspx/driver/providers/anthropic.py +149 -0
  33. wspx/driver/providers/compat.py +115 -0
  34. wspx/driver/providers/google.py +138 -0
  35. wspx/driver/providers/openai.py +125 -0
  36. wspx/driver/run.py +134 -0
  37. wspx/errors.py +69 -0
  38. wspx/gateway/__init__.py +8 -0
  39. wspx/gateway/app.py +626 -0
  40. wspx/gateway/ledger.py +536 -0
  41. wspx/gateway/prices.py +75 -0
  42. wspx/gateway/usage.py +284 -0
  43. wspx/handles.py +117 -0
  44. wspx/integrations/AGENTS.snippet.md +8 -0
  45. wspx/integrations/SKILL.md +52 -0
  46. wspx/integrations/__init__.py +17 -0
  47. wspx/integrations/claude-plugin/.claude-plugin/plugin.json +30 -0
  48. wspx/integrations/claude-plugin/skills/wspx/SKILL.md +52 -0
  49. wspx/integrations/codex-hooks.toml +9 -0
  50. wspx/lab/__init__.py +5 -0
  51. wspx/lab/analysis.py +485 -0
  52. wspx/lab/check.py +489 -0
  53. wspx/lab/evaluate.py +108 -0
  54. wspx/lab/export.py +275 -0
  55. wspx/lab/grep.py +754 -0
  56. wspx/lab/index.py +239 -0
  57. wspx/lab/ir/__init__.py +1 -0
  58. wspx/lab/ir/capture.py +299 -0
  59. wspx/lab/ir/graph.py +148 -0
  60. wspx/lab/ir/model_ir.py +522 -0
  61. wspx/lab/ir/semantics.py +1255 -0
  62. wspx/lab/ir/tape.py +853 -0
  63. wspx/lab/loader.py +204 -0
  64. wspx/lab/merge.py +200 -0
  65. wspx/lab/ops/__init__.py +76 -0
  66. wspx/lab/ops/builtin.py +379 -0
  67. wspx/lab/patch.py +320 -0
  68. wspx/lab/probe.py +206 -0
  69. wspx/lab/results.py +114 -0
  70. wspx/lab/sdk.py +120 -0
  71. wspx/lab/server.py +236 -0
  72. wspx/lab/session.py +1574 -0
  73. wspx/lab/tensors.py +119 -0
  74. wspx/lab/text.py +69 -0
  75. wspx/lab/tree.py +302 -0
  76. wspx/loops.py +127 -0
  77. wspx/project.py +143 -0
  78. wspx/py.typed +0 -0
  79. wspx/safety/__init__.py +15 -0
  80. wspx/safety/kill.py +47 -0
  81. wspx/safety/sweeper.py +68 -0
  82. wspx/store/__init__.py +64 -0
  83. wspx/store/objects.py +130 -0
  84. wspx/store/overlay.py +209 -0
  85. wspx/store/refs.py +150 -0
  86. wspx/testing/__init__.py +21 -0
  87. wspx/testing/api.py +155 -0
  88. wspx/testing/builtin.py +139 -0
  89. wspx/testing/runner.py +257 -0
  90. wspx/tools/__init__.py +17 -0
  91. wspx/tools/dispatch.py +129 -0
  92. wspx/tools/schemas.py +313 -0
  93. wspx/vcs/__init__.py +17 -0
  94. wspx/vcs/repo.py +431 -0
  95. wspx-0.1.0.dev0.dist-info/METADATA +278 -0
  96. wspx-0.1.0.dev0.dist-info/RECORD +99 -0
  97. wspx-0.1.0.dev0.dist-info/WHEEL +4 -0
  98. wspx-0.1.0.dev0.dist-info/entry_points.txt +10 -0
  99. wspx-0.1.0.dev0.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,84 @@
1
+ """Provider adapters: one ``Provider.step`` interface over each SDK.
2
+
3
+ Driver names:
4
+
5
+ ==================================== ============================================
6
+ ``anthropic:<model>`` Anthropic Messages API (adaptive thinking)
7
+ ``openai:<model>`` OpenAI Responses API (encrypted reasoning)
8
+ ``google:<model>`` Gemini via google-genai (thought signatures)
9
+ ``hf:<repo>`` an open model on the driver-model server
10
+ ``hf-router:<repo>`` Hugging Face Inference Providers
11
+ ``openai-compatible:<name>#<model>`` a configured vLLM, SGLang or Ollama endpoint
12
+ ==================================== ============================================
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ from typing import Any, Protocol
18
+
19
+ from wspx.driver.messages import Message, Turn
20
+ from wspx.errors import WspxError
21
+
22
+
23
+ class Provider(Protocol):
24
+ name: str
25
+ model: str
26
+
27
+ def step(
28
+ self,
29
+ system: str,
30
+ history: list[Message],
31
+ tools: list[dict[str, Any]],
32
+ *,
33
+ effort: str = "high",
34
+ max_tokens: int = 64_000,
35
+ ) -> Turn: ...
36
+
37
+ def tools(self) -> list[dict[str, Any]]: ...
38
+
39
+
40
+ def parse_driver(spec: str) -> tuple[str, str]:
41
+ kind, sep, model = spec.partition(":")
42
+ if not sep or not model:
43
+ raise WspxError(
44
+ f"driver {spec!r}: expected '<provider>:<model>', e.g. anthropic:claude-opus-5-5"
45
+ )
46
+ if kind not in ("anthropic", "openai", "google", "hf", "hf-router", "openai-compatible"):
47
+ raise WspxError(f"unknown driver provider {kind!r}")
48
+ return kind, model
49
+
50
+
51
+ def make_provider(spec: str, *, gateway: str, token: str, client: Any = None) -> Provider:
52
+ """Build a provider whose SDK talks to the gateway with a session token."""
53
+ kind, model = parse_driver(spec)
54
+ gateway = gateway.rstrip("/")
55
+ if kind == "anthropic":
56
+ from wspx.driver.providers.anthropic import AnthropicProvider
57
+
58
+ return AnthropicProvider(
59
+ model, client=client, base_url=f"{gateway}/v1/anthropic", token=token
60
+ )
61
+ if kind == "openai":
62
+ from wspx.driver.providers.openai import OpenAIProvider
63
+
64
+ return OpenAIProvider(model, client=client, base_url=f"{gateway}/v1/openai/v1", token=token)
65
+ if kind == "google":
66
+ from wspx.driver.providers.google import GoogleProvider
67
+
68
+ return GoogleProvider(model, client=client, base_url=f"{gateway}/v1/google", token=token)
69
+ from wspx.driver.providers.compat import ChatProvider
70
+
71
+ if kind == "hf":
72
+ return ChatProvider(
73
+ model, name=spec, client=client, base_url=f"{gateway}/v1/compat/hf/v1", token=token
74
+ )
75
+ if kind == "hf-router":
76
+ return ChatProvider(
77
+ model, name=spec, client=client, base_url=f"{gateway}/v1/hf-router/v1", token=token
78
+ )
79
+ endpoint, _, served = model.partition("#")
80
+ if not served:
81
+ raise WspxError(f"{spec!r}: expected openai-compatible:<endpoint>#<model>")
82
+ return ChatProvider(
83
+ served, name=spec, client=client, base_url=f"{gateway}/v1/compat/{endpoint}/v1", token=token
84
+ )
@@ -0,0 +1,149 @@
1
+ """Anthropic Messages API adapter.
2
+
3
+ Adaptive thinking with summarized display; thinking blocks are replayed
4
+ unchanged on every request. On models that support it, server-side refusal
5
+ fallbacks are enabled (``fallbacks: "default"``), and a final ``refusal`` stop
6
+ reason is surfaced rather than read as content.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import time
12
+ from typing import Any
13
+
14
+ from wspx.driver.messages import Message, ToolCall, Turn, Usage
15
+ from wspx.tools import anthropic_tools
16
+
17
+ FALLBACK_BETA = "server-side-fallback-2026-07-01"
18
+ FALLBACK_MODELS = (
19
+ "claude-fable-5-2",
20
+ "claude-fable-5-1",
21
+ "claude-opus-5-5",
22
+ "claude-opus-5",
23
+ "claude-sonnet-5-5",
24
+ )
25
+ ADAPTIVE_PREFIXES = (
26
+ "claude-fable",
27
+ "claude-mythos",
28
+ "claude-opus-5",
29
+ "claude-opus-4-8",
30
+ "claude-opus-4-7",
31
+ "claude-opus-4-6",
32
+ "claude-sonnet-5",
33
+ "claude-sonnet-4-6",
34
+ "claude-haiku-5",
35
+ )
36
+
37
+
38
+ class AnthropicProvider:
39
+ name = "anthropic"
40
+
41
+ def __init__(
42
+ self,
43
+ model: str,
44
+ *,
45
+ client: Any = None,
46
+ base_url: str | None = None,
47
+ token: str | None = None,
48
+ fallbacks: bool = True,
49
+ ) -> None:
50
+ if client is None:
51
+ import anthropic
52
+
53
+ client = anthropic.Anthropic(api_key=token, base_url=base_url, max_retries=4)
54
+ self.client = client
55
+ self.model = model
56
+ self.fallbacks = fallbacks and model in FALLBACK_MODELS
57
+
58
+ def tools(self) -> list[dict[str, Any]]:
59
+ return anthropic_tools()
60
+
61
+ def messages(self, history: list[Message]) -> list[dict[str, Any]]:
62
+ out: list[dict[str, Any]] = []
63
+ for msg in history:
64
+ if msg.role == "assistant":
65
+ content = msg.native if msg.provider == self.name and msg.native else _plain(msg)
66
+ out.append({"role": "assistant", "content": content})
67
+ continue
68
+ blocks: list[dict[str, Any]] = [
69
+ {
70
+ "type": "tool_result",
71
+ "tool_use_id": o.call_id,
72
+ "content": o.content,
73
+ "is_error": o.is_error,
74
+ }
75
+ for o in msg.outputs
76
+ ]
77
+ if msg.text:
78
+ blocks.append({"type": "text", "text": msg.text})
79
+ blocks.extend({"type": "text", "text": f"[operator] {note}"} for note in msg.notes)
80
+ out.append({"role": "user", "content": blocks})
81
+ return out
82
+
83
+ def step(
84
+ self,
85
+ system: str,
86
+ history: list[Message],
87
+ tools: list[dict[str, Any]],
88
+ *,
89
+ effort: str = "high",
90
+ max_tokens: int = 64_000,
91
+ ) -> Turn:
92
+ params: dict[str, Any] = {
93
+ "model": self.model,
94
+ "max_tokens": max_tokens,
95
+ "system": [{"type": "text", "text": system, "cache_control": {"type": "ephemeral"}}],
96
+ "messages": self.messages(history),
97
+ "tools": tools,
98
+ }
99
+ if self.model.startswith(ADAPTIVE_PREFIXES):
100
+ params["thinking"] = {"type": "adaptive", "display": "summarized"}
101
+ params["output_config"] = {"effort": effort}
102
+ started = time.perf_counter()
103
+ if self.fallbacks:
104
+ with self.client.beta.messages.stream(
105
+ **params, betas=[FALLBACK_BETA], fallbacks="default"
106
+ ) as stream:
107
+ message = stream.get_final_message()
108
+ else:
109
+ with self.client.messages.stream(**params) as stream:
110
+ message = stream.get_final_message()
111
+ return self._turn(message, time.perf_counter() - started)
112
+
113
+ def _turn(self, message: Any, latency: float) -> Turn:
114
+ text, reasoning, calls, native = "", [], [], []
115
+ for block in message.content:
116
+ native.append(block.model_dump(exclude_none=True, by_alias=True))
117
+ if block.type == "text":
118
+ text += block.text
119
+ elif block.type == "thinking":
120
+ reasoning.append(block.thinking)
121
+ elif block.type == "tool_use":
122
+ calls.append(ToolCall(block.id, block.name, dict(block.input or {})))
123
+ u = message.usage
124
+ usage = Usage(
125
+ input=u.input_tokens or 0,
126
+ output=u.output_tokens or 0,
127
+ cache_read=getattr(u, "cache_read_input_tokens", 0) or 0,
128
+ cache_write=getattr(u, "cache_creation_input_tokens", 0) or 0,
129
+ )
130
+ thought = "\n".join(r for r in reasoning if r) or None
131
+ return Turn(
132
+ text,
133
+ thought,
134
+ calls,
135
+ usage,
136
+ message.stop_reason or "end_turn",
137
+ native,
138
+ getattr(message, "model", self.model),
139
+ latency,
140
+ )
141
+
142
+
143
+ def _plain(msg: Message) -> list[dict[str, Any]]:
144
+ """An assistant turn from another provider, re-expressed (its reasoning cannot carry over)."""
145
+ blocks: list[dict[str, Any]] = [{"type": "text", "text": msg.text}] if msg.text else []
146
+ blocks.extend(
147
+ {"type": "tool_use", "id": c.id, "name": c.name, "input": c.args} for c in msg.tool_calls
148
+ )
149
+ return blocks or [{"type": "text", "text": "(no content)"}]
@@ -0,0 +1,115 @@
1
+ """OpenAI-compatible Chat Completions adapter (vLLM, SGLang, Ollama, HF router).
2
+
3
+ Open models expose no portable reasoning state, so turns replay as plain
4
+ messages; any ``reasoning_content`` the server returns is shown, not resent.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import time
11
+ from typing import Any
12
+
13
+ from wspx.driver.messages import Message, ToolCall, Turn, Usage
14
+ from wspx.tools import TOOLS
15
+
16
+
17
+ class ChatProvider:
18
+ def __init__(
19
+ self,
20
+ model: str,
21
+ *,
22
+ name: str,
23
+ client: Any = None,
24
+ base_url: str | None = None,
25
+ token: str | None = None,
26
+ ) -> None:
27
+ if client is None:
28
+ import openai
29
+
30
+ client = openai.OpenAI(api_key=token or "none", base_url=base_url, max_retries=4)
31
+ self.client = client
32
+ self.model = model
33
+ self.name = name
34
+
35
+ def tools(self) -> list[dict[str, Any]]:
36
+ return [
37
+ {
38
+ "type": "function",
39
+ "function": {
40
+ "name": t.name,
41
+ "description": t.description,
42
+ "parameters": t.schema(),
43
+ },
44
+ }
45
+ for t in TOOLS.values()
46
+ ]
47
+
48
+ def messages(self, system: str, history: list[Message]) -> list[dict[str, Any]]:
49
+ out: list[dict[str, Any]] = [{"role": "system", "content": system}]
50
+ for msg in history:
51
+ if msg.role == "assistant":
52
+ entry: dict[str, Any] = {"role": "assistant", "content": msg.text or None}
53
+ if msg.tool_calls:
54
+ entry["tool_calls"] = [
55
+ {
56
+ "id": c.id,
57
+ "type": "function",
58
+ "function": {"name": c.name, "arguments": json.dumps(c.args)},
59
+ }
60
+ for c in msg.tool_calls
61
+ ]
62
+ out.append(entry)
63
+ continue
64
+ out.extend(
65
+ {"role": "tool", "tool_call_id": o.call_id, "content": o.content}
66
+ for o in msg.outputs
67
+ )
68
+ text = "\n".join([msg.text, *(f"[operator] {n}" for n in msg.notes)]).strip()
69
+ if text:
70
+ out.append({"role": "user", "content": text})
71
+ return out
72
+
73
+ def step(
74
+ self,
75
+ system: str,
76
+ history: list[Message],
77
+ tools: list[dict[str, Any]],
78
+ *,
79
+ effort: str = "high",
80
+ max_tokens: int = 16_000,
81
+ ) -> Turn:
82
+ del effort
83
+ started = time.perf_counter()
84
+ response = self.client.chat.completions.create(
85
+ model=self.model,
86
+ messages=self.messages(system, history),
87
+ tools=tools,
88
+ max_tokens=max_tokens,
89
+ )
90
+ choice = response.choices[0]
91
+ msg = choice.message
92
+ calls = []
93
+ for tc in msg.tool_calls or []:
94
+ try:
95
+ args = json.loads(tc.function.arguments or "{}")
96
+ except json.JSONDecodeError:
97
+ args = {"__invalid_json__": tc.function.arguments}
98
+ calls.append(ToolCall(tc.id, tc.function.name, args))
99
+ u = response.usage
100
+ usage = Usage(
101
+ input=getattr(u, "prompt_tokens", 0) or 0,
102
+ output=getattr(u, "completion_tokens", 0) or 0,
103
+ )
104
+ reasoning = getattr(msg, "reasoning_content", None)
105
+ stop = "tool_use" if calls else (choice.finish_reason or "stop")
106
+ return Turn(
107
+ msg.content or "",
108
+ reasoning,
109
+ calls,
110
+ usage,
111
+ stop,
112
+ None,
113
+ self.model,
114
+ time.perf_counter() - started,
115
+ )
@@ -0,0 +1,138 @@
1
+ """Gemini adapter (google-genai).
2
+
3
+ Thought summaries are requested with ``include_thoughts``. Model turns are
4
+ replayed as the SDK returned them, thought signatures included, which Gemini
5
+ needs to continue its reasoning across tool calls.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import time
11
+ import uuid
12
+ from typing import Any
13
+
14
+ from wspx.driver.messages import Message, ToolCall, Turn, Usage
15
+ from wspx.tools import gemini_tools
16
+
17
+
18
+ class GoogleProvider:
19
+ name = "google"
20
+
21
+ def __init__(
22
+ self,
23
+ model: str,
24
+ *,
25
+ client: Any = None,
26
+ base_url: str | None = None,
27
+ token: str | None = None,
28
+ ) -> None:
29
+ if client is None:
30
+ from google import genai
31
+ from google.genai import types
32
+
33
+ client = genai.Client(api_key=token, http_options=types.HttpOptions(base_url=base_url))
34
+ self.client = client
35
+ self.model = model
36
+
37
+ def tools(self) -> list[dict[str, Any]]:
38
+ return gemini_tools()
39
+
40
+ def contents(self, history: list[Message]) -> list[Any]:
41
+ from google.genai import types
42
+
43
+ out: list[Any] = []
44
+ for msg in history:
45
+ if msg.role == "assistant":
46
+ if msg.provider == self.name and msg.native is not None:
47
+ out.append(msg.native)
48
+ else:
49
+ parts = [types.Part(text=msg.text)] if msg.text else []
50
+ parts += [
51
+ types.Part(
52
+ function_call=types.FunctionCall(name=c.name, args=c.args, id=c.id)
53
+ )
54
+ for c in msg.tool_calls
55
+ ]
56
+ out.append(
57
+ types.Content(
58
+ role="model", parts=parts or [types.Part(text="(no content)")]
59
+ )
60
+ )
61
+ continue
62
+ parts = [
63
+ types.Part(
64
+ function_response=types.FunctionResponse(
65
+ name=o.name,
66
+ id=o.call_id,
67
+ response={"error" if o.is_error else "result": o.content},
68
+ )
69
+ )
70
+ for o in msg.outputs
71
+ ]
72
+ if msg.text:
73
+ parts.append(types.Part(text=msg.text))
74
+ parts.extend(types.Part(text=f"[operator] {note}") for note in msg.notes)
75
+ out.append(types.Content(role="user", parts=parts))
76
+ return out
77
+
78
+ def step(
79
+ self,
80
+ system: str,
81
+ history: list[Message],
82
+ tools: list[dict[str, Any]],
83
+ *,
84
+ effort: str = "high",
85
+ max_tokens: int = 64_000,
86
+ ) -> Turn:
87
+ from google.genai import types
88
+
89
+ config = types.GenerateContentConfig(
90
+ system_instruction=system,
91
+ tools=[
92
+ types.Tool(function_declarations=[types.FunctionDeclaration(**t) for t in tools])
93
+ ],
94
+ thinking_config=types.ThinkingConfig(include_thoughts=True),
95
+ automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True),
96
+ max_output_tokens=max_tokens,
97
+ )
98
+ del effort
99
+ started = time.perf_counter()
100
+ response = self.client.models.generate_content(
101
+ model=self.model, contents=self.contents(history), config=config
102
+ )
103
+ candidate = response.candidates[0] if response.candidates else None
104
+ content = candidate.content if candidate is not None else None
105
+ text, reasoning, calls = "", [], []
106
+ for part in content.parts if content is not None and content.parts else []:
107
+ if part.thought and part.text:
108
+ reasoning.append(part.text)
109
+ elif part.text:
110
+ text += part.text
111
+ if part.function_call is not None:
112
+ fc = part.function_call
113
+ calls.append(
114
+ ToolCall(
115
+ fc.id or f"call_{uuid.uuid4().hex[:8]}", fc.name or "", dict(fc.args or {})
116
+ )
117
+ )
118
+ meta = response.usage_metadata
119
+ cached = (getattr(meta, "cached_content_token_count", 0) or 0) if meta else 0
120
+ usage = Usage(
121
+ input=((meta.prompt_token_count or 0) - cached) if meta else 0,
122
+ output=((meta.candidates_token_count or 0) + (meta.thoughts_token_count or 0))
123
+ if meta
124
+ else 0,
125
+ cache_read=cached,
126
+ reasoning=(meta.thoughts_token_count or 0) if meta else 0,
127
+ )
128
+ stop = "tool_use" if calls else str(getattr(candidate, "finish_reason", "STOP") or "STOP")
129
+ return Turn(
130
+ text,
131
+ "\n".join(reasoning) or None,
132
+ calls,
133
+ usage,
134
+ stop,
135
+ content,
136
+ self.model,
137
+ time.perf_counter() - started,
138
+ )
@@ -0,0 +1,125 @@
1
+ """OpenAI Responses API adapter.
2
+
3
+ Requests run with ``store=False`` and ask for ``reasoning.encrypted_content``,
4
+ so the reasoning items in each response can be replayed verbatim on the next
5
+ request without server-side state.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import time
12
+ from typing import Any
13
+
14
+ from wspx.driver.messages import Message, ToolCall, Turn, Usage
15
+ from wspx.tools import openai_tools
16
+
17
+
18
+ class OpenAIProvider:
19
+ name = "openai"
20
+
21
+ def __init__(
22
+ self,
23
+ model: str,
24
+ *,
25
+ client: Any = None,
26
+ base_url: str | None = None,
27
+ token: str | None = None,
28
+ ) -> None:
29
+ if client is None:
30
+ import openai
31
+
32
+ client = openai.OpenAI(api_key=token, base_url=base_url, max_retries=4)
33
+ self.client = client
34
+ self.model = model
35
+
36
+ def tools(self) -> list[dict[str, Any]]:
37
+ return openai_tools()
38
+
39
+ def items(self, history: list[Message]) -> list[dict[str, Any]]:
40
+ out: list[dict[str, Any]] = []
41
+ for msg in history:
42
+ if msg.role == "assistant":
43
+ if msg.provider == self.name and msg.native:
44
+ out.extend(msg.native)
45
+ else:
46
+ if msg.text:
47
+ out.append({"role": "assistant", "content": msg.text})
48
+ out.extend(
49
+ {
50
+ "type": "function_call",
51
+ "call_id": c.id,
52
+ "name": c.name,
53
+ "arguments": json.dumps(c.args),
54
+ }
55
+ for c in msg.tool_calls
56
+ )
57
+ continue
58
+ out.extend(
59
+ {"type": "function_call_output", "call_id": o.call_id, "output": o.content}
60
+ for o in msg.outputs
61
+ )
62
+ if msg.text:
63
+ out.append({"role": "user", "content": msg.text})
64
+ out.extend({"role": "developer", "content": note} for note in msg.notes)
65
+ return out
66
+
67
+ def step(
68
+ self,
69
+ system: str,
70
+ history: list[Message],
71
+ tools: list[dict[str, Any]],
72
+ *,
73
+ effort: str = "high",
74
+ max_tokens: int = 64_000,
75
+ ) -> Turn:
76
+ started = time.perf_counter()
77
+ response = self.client.responses.create(
78
+ model=self.model,
79
+ instructions=system,
80
+ input=self.items(history),
81
+ tools=tools,
82
+ reasoning={"effort": effort, "summary": "auto"},
83
+ include=["reasoning.encrypted_content"],
84
+ store=False,
85
+ max_output_tokens=max_tokens,
86
+ )
87
+ text = ""
88
+ reasoning: list[str] = []
89
+ calls: list[ToolCall] = []
90
+ native: list[dict[str, Any]] = []
91
+ for item in response.output:
92
+ native.append(item.model_dump(exclude_none=True))
93
+ if item.type == "message":
94
+ text += "".join(
95
+ p.text for p in item.content if getattr(p, "type", "") == "output_text"
96
+ )
97
+ elif item.type == "reasoning":
98
+ reasoning.extend(s.text for s in (item.summary or []))
99
+ elif item.type == "function_call":
100
+ try:
101
+ args = json.loads(item.arguments or "{}")
102
+ except json.JSONDecodeError:
103
+ args = {"__invalid_json__": item.arguments}
104
+ calls.append(ToolCall(item.call_id, item.name, args))
105
+ u = response.usage
106
+ details = getattr(u, "input_tokens_details", None)
107
+ usage = Usage(
108
+ input=u.input_tokens,
109
+ output=u.output_tokens,
110
+ cache_read=getattr(details, "cached_tokens", 0) or 0,
111
+ cache_write=getattr(details, "cache_write_tokens", 0) or 0,
112
+ reasoning=getattr(getattr(u, "output_tokens_details", None), "reasoning_tokens", 0)
113
+ or 0,
114
+ )
115
+ stop = "tool_use" if calls else (response.status or "completed")
116
+ return Turn(
117
+ text,
118
+ "\n".join(reasoning) or None,
119
+ calls,
120
+ usage,
121
+ stop,
122
+ native,
123
+ response.model,
124
+ time.perf_counter() - started,
125
+ )