agent0-runtime 0.1.0__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.
- agent0_runtime/__init__.py +172 -0
- agent0_runtime/backend.py +78 -0
- agent0_runtime/builtin_tools.py +200 -0
- agent0_runtime/capabilities.py +50 -0
- agent0_runtime/client.py +69 -0
- agent0_runtime/credentials.py +85 -0
- agent0_runtime/events.py +151 -0
- agent0_runtime/filesystem.py +243 -0
- agent0_runtime/history.py +155 -0
- agent0_runtime/images.py +167 -0
- agent0_runtime/lifecycle.py +194 -0
- agent0_runtime/loop.py +588 -0
- agent0_runtime/managed.py +78 -0
- agent0_runtime/openai_backend.py +433 -0
- agent0_runtime/plugins.py +141 -0
- agent0_runtime/providers.py +95 -0
- agent0_runtime/py.typed +0 -0
- agent0_runtime/responses.py +267 -0
- agent0_runtime/routing.py +335 -0
- agent0_runtime/routing_backend.py +161 -0
- agent0_runtime/search.py +22 -0
- agent0_runtime/session.py +75 -0
- agent0_runtime/shell.py +190 -0
- agent0_runtime/skills/websearch/SKILL.md +82 -0
- agent0_runtime/tools.py +335 -0
- agent0_runtime/types.py +153 -0
- agent0_runtime/web_fetch.py +285 -0
- agent0_runtime/web_search.py +295 -0
- agent0_runtime-0.1.0.dist-info/METADATA +102 -0
- agent0_runtime-0.1.0.dist-info/RECORD +32 -0
- agent0_runtime-0.1.0.dist-info/WHEEL +4 -0
- agent0_runtime-0.1.0.dist-info/licenses/LICENSE +21 -0
agent0_runtime/loop.py
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
"""Headless model/tool agent loop with structured events and typed outcomes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import inspect
|
|
7
|
+
import os
|
|
8
|
+
from collections.abc import Callable, Iterable, Mapping
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Protocol
|
|
11
|
+
|
|
12
|
+
from .backend import BackendRegistry, ModelBackend, ModelRequest
|
|
13
|
+
from .events import EventBus, EventEmitter, EventListener
|
|
14
|
+
from .lifecycle import LifecycleHooks, run_with_lifecycle
|
|
15
|
+
from .plugins import PluginContext, PluginManager
|
|
16
|
+
from .session import RunContext, RuntimeSession
|
|
17
|
+
from .tools import (
|
|
18
|
+
_TOOL_ATTRIBUTE,
|
|
19
|
+
Tool,
|
|
20
|
+
ToolContext,
|
|
21
|
+
ToolExecutor,
|
|
22
|
+
ToolRegistry,
|
|
23
|
+
_as_tool,
|
|
24
|
+
)
|
|
25
|
+
from .types import (
|
|
26
|
+
TOOL_ATTACHMENTS_KEY,
|
|
27
|
+
AgentInterrupted,
|
|
28
|
+
CancellationToken,
|
|
29
|
+
Message,
|
|
30
|
+
Recovery,
|
|
31
|
+
RunResult,
|
|
32
|
+
RunStatus,
|
|
33
|
+
ToolCall,
|
|
34
|
+
ToolResult,
|
|
35
|
+
new_run_id,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
DEFAULT_MAX_TURNS = 200
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class HistoryPolicy(Protocol):
|
|
42
|
+
"""Provider/history policy used by the loop without knowing its wire format."""
|
|
43
|
+
|
|
44
|
+
def estimate(self, messages: list[Message]) -> int: ...
|
|
45
|
+
|
|
46
|
+
def should_compact(self, prompt_tokens: int | None, context_limit: int | None) -> bool: ...
|
|
47
|
+
|
|
48
|
+
def compact(
|
|
49
|
+
self, messages: list[Message], *, aggressive: bool = False
|
|
50
|
+
) -> tuple[list[Message], int]: ...
|
|
51
|
+
|
|
52
|
+
def recover(
|
|
53
|
+
self, messages: list[Message], error: Exception, *, attempt: int
|
|
54
|
+
) -> Recovery | None: ...
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _estimate(messages: list[Message]) -> int:
|
|
58
|
+
chars = sum(len(str(message.get("content") or "")) for message in messages)
|
|
59
|
+
chars += sum(
|
|
60
|
+
len(str((call.get("function") or {}).get("arguments") or ""))
|
|
61
|
+
for message in messages
|
|
62
|
+
for call in message.get("tool_calls") or []
|
|
63
|
+
)
|
|
64
|
+
return chars // 4 + 1
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _last_user_question(messages: list[Message]) -> str | None:
|
|
68
|
+
for message in reversed(messages):
|
|
69
|
+
content = message.get("content")
|
|
70
|
+
if message.get("role") == "user" and isinstance(content, str) and content.strip():
|
|
71
|
+
return content
|
|
72
|
+
return None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _answer_pending(messages: list[Message], calls: list[ToolCall]) -> None:
|
|
76
|
+
"""Keep every interrupted assistant tool batch replayable."""
|
|
77
|
+
|
|
78
|
+
answered = {
|
|
79
|
+
message.get("tool_call_id") for message in messages if message.get("role") == "tool"
|
|
80
|
+
}
|
|
81
|
+
for call in calls:
|
|
82
|
+
if call.id and call.id not in answered:
|
|
83
|
+
messages.append(
|
|
84
|
+
{"role": "tool", "tool_call_id": call.id, "content": "error: interrupted"}
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _private_state(message: Message, state: dict[str, Any]) -> None:
|
|
89
|
+
for key, value in state.items():
|
|
90
|
+
if key.startswith("_"):
|
|
91
|
+
message[key] = value
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _merge_attachments(message: Message, result: ToolResult) -> None:
|
|
95
|
+
if result.attachments:
|
|
96
|
+
message[TOOL_ATTACHMENTS_KEY] = [block.to_dict() for block in result.attachments]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _visible_tool_candidates(frame: Any | None) -> list[Tool]:
|
|
100
|
+
"""Collect uniquely identified decorated tools visible from the caller stack."""
|
|
101
|
+
|
|
102
|
+
candidates: list[Tool] = []
|
|
103
|
+
seen: set[int] = set()
|
|
104
|
+
while frame is not None:
|
|
105
|
+
for scope in (frame.f_locals, frame.f_globals):
|
|
106
|
+
for value in scope.values():
|
|
107
|
+
if id(value) in seen:
|
|
108
|
+
continue
|
|
109
|
+
definition = getattr(value, _TOOL_ATTRIBUTE, None)
|
|
110
|
+
tool = definition if isinstance(definition, Tool) else None
|
|
111
|
+
if tool is None and isinstance(value, Tool):
|
|
112
|
+
tool = value
|
|
113
|
+
if tool is not None:
|
|
114
|
+
seen.add(id(value))
|
|
115
|
+
candidates.append(tool)
|
|
116
|
+
frame = frame.f_back
|
|
117
|
+
return candidates
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _resolve_tools(
|
|
121
|
+
values: Iterable[Tool | str | Callable[..., Any]],
|
|
122
|
+
candidates: list[Tool],
|
|
123
|
+
) -> list[Tool]:
|
|
124
|
+
"""Resolve Tool objects, decorated callables, and decorated names."""
|
|
125
|
+
|
|
126
|
+
resolved: list[Tool] = []
|
|
127
|
+
for value in values:
|
|
128
|
+
if not isinstance(value, str):
|
|
129
|
+
resolved.append(_as_tool(value))
|
|
130
|
+
continue
|
|
131
|
+
matches: list[Tool] = []
|
|
132
|
+
for candidate in candidates:
|
|
133
|
+
if candidate.name == value and all(candidate is not seen for seen in matches):
|
|
134
|
+
matches.append(candidate)
|
|
135
|
+
if len(matches) != 1:
|
|
136
|
+
qualifier = "not found" if not matches else "ambiguous"
|
|
137
|
+
raise ValueError(f"tool name {value!r} is {qualifier}; pass the function instead")
|
|
138
|
+
resolved.append(matches[0])
|
|
139
|
+
return resolved
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class Agent:
|
|
143
|
+
"""Reusable headless runtime; one instance may serve multiple sequential runs.
|
|
144
|
+
|
|
145
|
+
Backend/session implementations decide whether they support concurrent runs.
|
|
146
|
+
Event subscription and tool/plugin registration are instance-scoped, while
|
|
147
|
+
run IDs, event sequences, cancellation tokens, and outcomes are per run.
|
|
148
|
+
"""
|
|
149
|
+
|
|
150
|
+
@classmethod
|
|
151
|
+
def from_env(
|
|
152
|
+
cls,
|
|
153
|
+
model: str | None = None,
|
|
154
|
+
*,
|
|
155
|
+
tools: ToolRegistry | Iterable[Tool | str | Callable[..., Any]] = (),
|
|
156
|
+
context_limit: int | None = None,
|
|
157
|
+
**runtime_options: Any,
|
|
158
|
+
) -> Agent:
|
|
159
|
+
"""Build the recommended default Agent from provider environment variables.
|
|
160
|
+
|
|
161
|
+
With several configured providers, use an explicit ``provider/model``
|
|
162
|
+
reference. The returned Agent owns and closes its OpenAI network client.
|
|
163
|
+
Advanced runtime options are forwarded to the regular constructor.
|
|
164
|
+
"""
|
|
165
|
+
|
|
166
|
+
from openai import AsyncOpenAI
|
|
167
|
+
|
|
168
|
+
from .providers import get_provider, resolve_all_env
|
|
169
|
+
|
|
170
|
+
registry = tools if isinstance(tools, ToolRegistry) else None
|
|
171
|
+
if registry is None:
|
|
172
|
+
values = list(tools)
|
|
173
|
+
if any(isinstance(value, str) for value in values):
|
|
174
|
+
caller = inspect.currentframe().f_back
|
|
175
|
+
try:
|
|
176
|
+
candidates = _visible_tool_candidates(caller)
|
|
177
|
+
registry = ToolRegistry(_resolve_tools(values, candidates))
|
|
178
|
+
finally:
|
|
179
|
+
# Never retain caller frame references on this Agent.
|
|
180
|
+
del caller
|
|
181
|
+
else:
|
|
182
|
+
registry = ToolRegistry(values)
|
|
183
|
+
requested = (model or os.environ.get("AGENT0_MODEL", "")).strip()
|
|
184
|
+
credentials = resolve_all_env()
|
|
185
|
+
if not credentials:
|
|
186
|
+
raise ValueError("no model provider configured; set DEEPSEEK_API_KEY or KIMI_API_KEY")
|
|
187
|
+
available = [provider for key in credentials if (provider := get_provider(key))]
|
|
188
|
+
if requested == "auto":
|
|
189
|
+
raise ValueError("Agent.from_env() needs a concrete provider/model, not 'auto'")
|
|
190
|
+
if "/" in requested:
|
|
191
|
+
provider_name, model_name = requested.split("/", 1)
|
|
192
|
+
provider = get_provider(provider_name)
|
|
193
|
+
if provider is None or not model_name:
|
|
194
|
+
raise ValueError(f"invalid model reference: {requested!r}")
|
|
195
|
+
elif requested:
|
|
196
|
+
if len(available) != 1:
|
|
197
|
+
raise ValueError("use provider/model when more than one provider is configured")
|
|
198
|
+
provider, model_name = available[0], requested
|
|
199
|
+
else:
|
|
200
|
+
provider = available[0]
|
|
201
|
+
model_name = provider.default_model
|
|
202
|
+
credential = credentials.get(provider.id)
|
|
203
|
+
if credential is None:
|
|
204
|
+
raise ValueError(f"{requested!r} requires {provider.env_keys[0]}")
|
|
205
|
+
client = AsyncOpenAI(
|
|
206
|
+
api_key=credential.api_key,
|
|
207
|
+
base_url=os.environ.get("AGENT0_BASE_URL", "").strip() or credential.base_url,
|
|
208
|
+
)
|
|
209
|
+
return cls.from_openai(
|
|
210
|
+
client,
|
|
211
|
+
model_name,
|
|
212
|
+
provider_id=provider.id,
|
|
213
|
+
context_limit=context_limit,
|
|
214
|
+
tools=registry,
|
|
215
|
+
close_backend=runtime_options.pop("close_backend", True),
|
|
216
|
+
**runtime_options,
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
@classmethod
|
|
220
|
+
def from_openai(
|
|
221
|
+
cls,
|
|
222
|
+
client: Any,
|
|
223
|
+
model: str,
|
|
224
|
+
*,
|
|
225
|
+
provider_id: str | None = None,
|
|
226
|
+
context_limit: int | None = None,
|
|
227
|
+
tools: ToolRegistry | Iterable[Tool | Callable[..., Any]] = (),
|
|
228
|
+
close_backend: bool = False,
|
|
229
|
+
supports_vision: Callable[[str | None], bool] | None = None,
|
|
230
|
+
retry_sleeps: tuple[float, ...] | None = None,
|
|
231
|
+
stream_close_timeout: float | None = None,
|
|
232
|
+
**runtime_options: Any,
|
|
233
|
+
) -> Agent:
|
|
234
|
+
"""Assemble the default Responses backend for an existing host client.
|
|
235
|
+
|
|
236
|
+
The host owns an injected client by default. ``close_backend=True``
|
|
237
|
+
transfers backend/client cleanup to this Agent.
|
|
238
|
+
"""
|
|
239
|
+
|
|
240
|
+
from .history import ResponsesHistoryPolicy
|
|
241
|
+
from .openai_backend import OpenAIResponsesBackend
|
|
242
|
+
|
|
243
|
+
registry = tools if isinstance(tools, ToolRegistry) else ToolRegistry(tools)
|
|
244
|
+
backend_options: dict[str, Any] = {}
|
|
245
|
+
if supports_vision is not None:
|
|
246
|
+
backend_options["supports_vision"] = supports_vision
|
|
247
|
+
if retry_sleeps is not None:
|
|
248
|
+
backend_options["retry_sleeps"] = retry_sleeps
|
|
249
|
+
if stream_close_timeout is not None:
|
|
250
|
+
backend_options["stream_close_timeout"] = stream_close_timeout
|
|
251
|
+
backend = OpenAIResponsesBackend(
|
|
252
|
+
client,
|
|
253
|
+
model,
|
|
254
|
+
provider_id=provider_id,
|
|
255
|
+
context_limit=context_limit,
|
|
256
|
+
**backend_options,
|
|
257
|
+
)
|
|
258
|
+
runtime_options.setdefault("history", ResponsesHistoryPolicy())
|
|
259
|
+
return cls(
|
|
260
|
+
backend,
|
|
261
|
+
tools=registry,
|
|
262
|
+
close_backend=close_backend,
|
|
263
|
+
**runtime_options,
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
def __init__(
|
|
267
|
+
self,
|
|
268
|
+
backend: ModelBackend,
|
|
269
|
+
*,
|
|
270
|
+
tools: ToolRegistry | None = None,
|
|
271
|
+
history: HistoryPolicy | None = None,
|
|
272
|
+
events: EventBus | None = None,
|
|
273
|
+
lifecycle: LifecycleHooks | None = None,
|
|
274
|
+
backends: BackendRegistry | None = None,
|
|
275
|
+
services: Mapping[str, Any] | None = None,
|
|
276
|
+
close_backend: bool = True,
|
|
277
|
+
plugins: Iterable[Any] = (),
|
|
278
|
+
plugin_names: Iterable[str] = (),
|
|
279
|
+
) -> None:
|
|
280
|
+
self.backend = backend
|
|
281
|
+
self.tools = tools if tools is not None else ToolRegistry()
|
|
282
|
+
self.history = history
|
|
283
|
+
self.events = events if events is not None else EventBus()
|
|
284
|
+
self.lifecycle = lifecycle if lifecycle is not None else LifecycleHooks()
|
|
285
|
+
self.backends = backends if backends is not None else BackendRegistry()
|
|
286
|
+
self.services = dict(services or {})
|
|
287
|
+
self.close_backend = close_backend
|
|
288
|
+
self._closed = False
|
|
289
|
+
self.plugins = PluginManager(
|
|
290
|
+
PluginContext(
|
|
291
|
+
tools=self.tools,
|
|
292
|
+
backends=self.backends,
|
|
293
|
+
events=self.events,
|
|
294
|
+
lifecycle=self.lifecycle,
|
|
295
|
+
services=self.services,
|
|
296
|
+
)
|
|
297
|
+
)
|
|
298
|
+
for plugin in plugins:
|
|
299
|
+
self.plugins.load(plugin)
|
|
300
|
+
names = tuple(plugin_names)
|
|
301
|
+
if names:
|
|
302
|
+
self.plugins.load_discovered(names)
|
|
303
|
+
|
|
304
|
+
async def __aenter__(self) -> Agent:
|
|
305
|
+
if self._closed:
|
|
306
|
+
raise RuntimeError("agent is closed")
|
|
307
|
+
return self
|
|
308
|
+
|
|
309
|
+
async def __aexit__(self, _exc_type: Any, _exc: Any, _tb: Any) -> None:
|
|
310
|
+
await self.close()
|
|
311
|
+
|
|
312
|
+
async def close(self) -> None:
|
|
313
|
+
"""Close plugins and, when owned, the backend and its network resources."""
|
|
314
|
+
|
|
315
|
+
if self._closed:
|
|
316
|
+
return
|
|
317
|
+
self._closed = True
|
|
318
|
+
try:
|
|
319
|
+
await self.plugins.close()
|
|
320
|
+
finally:
|
|
321
|
+
if self.close_backend:
|
|
322
|
+
close = getattr(self.backend, "aclose", None) or getattr(
|
|
323
|
+
self.backend, "close", None
|
|
324
|
+
)
|
|
325
|
+
if close is not None:
|
|
326
|
+
result = close()
|
|
327
|
+
if inspect.isawaitable(result):
|
|
328
|
+
await result
|
|
329
|
+
|
|
330
|
+
async def run(
|
|
331
|
+
self,
|
|
332
|
+
task: str,
|
|
333
|
+
*,
|
|
334
|
+
cwd: Path | None = None,
|
|
335
|
+
messages: list[Message] | None = None,
|
|
336
|
+
workspace: Any | None = None,
|
|
337
|
+
max_turns: int = DEFAULT_MAX_TURNS,
|
|
338
|
+
context_limit: int | None = None,
|
|
339
|
+
prompt_tokens: int | None = None,
|
|
340
|
+
cancellation: CancellationToken | None = None,
|
|
341
|
+
on_event: EventListener | None = None,
|
|
342
|
+
session: Any | None = None,
|
|
343
|
+
run_id: str = "",
|
|
344
|
+
) -> RunResult:
|
|
345
|
+
"""Run one task in a session, optionally wrapping it in lifecycle hooks.
|
|
346
|
+
|
|
347
|
+
``cwd`` is the legacy filesystem spelling of ``workspace``. Non-filesystem
|
|
348
|
+
agents may omit both; filesystem/shell tools then report a clear error if
|
|
349
|
+
a model nevertheless selects them.
|
|
350
|
+
"""
|
|
351
|
+
|
|
352
|
+
runtime_session = session if isinstance(session, RuntimeSession) else None
|
|
353
|
+
if runtime_session is not None and messages is None:
|
|
354
|
+
messages = runtime_session.messages
|
|
355
|
+
elif messages is None:
|
|
356
|
+
# A one-shot run needs no explicit session or mutable message list.
|
|
357
|
+
messages = []
|
|
358
|
+
selected_workspace = workspace if workspace is not None else cwd
|
|
359
|
+
if selected_workspace is None and runtime_session is not None:
|
|
360
|
+
metadata_workspace = runtime_session.metadata.get("workspace")
|
|
361
|
+
selected_workspace = metadata_workspace
|
|
362
|
+
if runtime_session is None:
|
|
363
|
+
session_id = (
|
|
364
|
+
getattr(session, "id", "") or getattr(session, "session_id", "") or "session"
|
|
365
|
+
)
|
|
366
|
+
runtime_session = RuntimeSession(
|
|
367
|
+
id=str(session_id),
|
|
368
|
+
messages=messages,
|
|
369
|
+
metadata={},
|
|
370
|
+
)
|
|
371
|
+
context = RunContext(
|
|
372
|
+
session=runtime_session,
|
|
373
|
+
run_id=run_id or new_run_id(),
|
|
374
|
+
workspace=selected_workspace,
|
|
375
|
+
cancellation=cancellation,
|
|
376
|
+
)
|
|
377
|
+
work = self.run_in_context(
|
|
378
|
+
context,
|
|
379
|
+
task,
|
|
380
|
+
max_turns=max_turns,
|
|
381
|
+
context_limit=context_limit,
|
|
382
|
+
prompt_tokens=prompt_tokens,
|
|
383
|
+
on_event=on_event,
|
|
384
|
+
)
|
|
385
|
+
if session is not None:
|
|
386
|
+
return await run_with_lifecycle(
|
|
387
|
+
session, work, run_id=context.run_id, hooks=self.lifecycle
|
|
388
|
+
)
|
|
389
|
+
return await work
|
|
390
|
+
|
|
391
|
+
async def run_in_context(
|
|
392
|
+
self,
|
|
393
|
+
context: RunContext,
|
|
394
|
+
task: str,
|
|
395
|
+
*,
|
|
396
|
+
max_turns: int = DEFAULT_MAX_TURNS,
|
|
397
|
+
context_limit: int | None = None,
|
|
398
|
+
prompt_tokens: int | None = None,
|
|
399
|
+
on_event: EventListener | None = None,
|
|
400
|
+
) -> RunResult:
|
|
401
|
+
"""Run one task using an explicit per-run context."""
|
|
402
|
+
|
|
403
|
+
if self._closed:
|
|
404
|
+
raise RuntimeError("agent is closed")
|
|
405
|
+
listeners = (on_event,) if on_event is not None else ()
|
|
406
|
+
events = self.events.for_run(context.run_id, listeners=listeners)
|
|
407
|
+
services = {**self.services, **dict(context.services)}
|
|
408
|
+
return await self._run(
|
|
409
|
+
task,
|
|
410
|
+
workspace=context.workspace,
|
|
411
|
+
messages=context.session.messages,
|
|
412
|
+
session_id=context.session.id,
|
|
413
|
+
max_turns=max_turns,
|
|
414
|
+
context_limit=context_limit,
|
|
415
|
+
prompt_tokens=prompt_tokens,
|
|
416
|
+
cancellation=context.cancellation,
|
|
417
|
+
services=services,
|
|
418
|
+
events=events,
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
async def _run(
|
|
422
|
+
self,
|
|
423
|
+
task: str,
|
|
424
|
+
*,
|
|
425
|
+
workspace: Any | None,
|
|
426
|
+
messages: list[Message],
|
|
427
|
+
session_id: str,
|
|
428
|
+
max_turns: int,
|
|
429
|
+
context_limit: int | None,
|
|
430
|
+
prompt_tokens: int | None,
|
|
431
|
+
cancellation: CancellationToken | None,
|
|
432
|
+
services: Mapping[str, Any],
|
|
433
|
+
events: EventEmitter,
|
|
434
|
+
) -> RunResult:
|
|
435
|
+
if max_turns < 1:
|
|
436
|
+
raise ValueError("max_turns must be at least 1")
|
|
437
|
+
messages.append({"role": "user", "content": task})
|
|
438
|
+
last_prompt = prompt_tokens
|
|
439
|
+
last_usage: dict[str, int] | None = None
|
|
440
|
+
finish_reason: str | None = None
|
|
441
|
+
turns = recovery_attempts = 0
|
|
442
|
+
pending: list[ToolCall] = []
|
|
443
|
+
await events.emit("turn_started", task=task)
|
|
444
|
+
try:
|
|
445
|
+
if cancellation is not None:
|
|
446
|
+
cancellation.throw_if_cancelled(run_id=events.run_id)
|
|
447
|
+
prepare = getattr(self.backend, "prepare", None)
|
|
448
|
+
if prepare is not None:
|
|
449
|
+
estimate = self.history.estimate(messages) if self.history else _estimate(messages)
|
|
450
|
+
await prepare(task, messages, estimate, events)
|
|
451
|
+
while turns < max_turns:
|
|
452
|
+
prompt = (
|
|
453
|
+
last_prompt
|
|
454
|
+
if last_prompt is not None
|
|
455
|
+
else (self.history.estimate(messages) if self.history else _estimate(messages))
|
|
456
|
+
)
|
|
457
|
+
backend_limit = getattr(self.backend, "context_limit", None)
|
|
458
|
+
active_limit = backend_limit if backend_limit is not None else context_limit
|
|
459
|
+
if self.history and self.history.should_compact(prompt, active_limit):
|
|
460
|
+
new_messages, changed = self.history.compact(messages)
|
|
461
|
+
if changed:
|
|
462
|
+
messages[:] = new_messages
|
|
463
|
+
last_prompt = None
|
|
464
|
+
await events.emit("compacted", changed=changed, aggressive=False)
|
|
465
|
+
await events.emit("model_started", turn=turns + 1)
|
|
466
|
+
try:
|
|
467
|
+
turn = await self.backend.complete(
|
|
468
|
+
ModelRequest(
|
|
469
|
+
messages=messages,
|
|
470
|
+
tools=self.tools.schemas(),
|
|
471
|
+
run_id=events.run_id,
|
|
472
|
+
turn=turns + 1,
|
|
473
|
+
cancellation=cancellation,
|
|
474
|
+
),
|
|
475
|
+
events,
|
|
476
|
+
)
|
|
477
|
+
except AgentInterrupted:
|
|
478
|
+
raise
|
|
479
|
+
except Exception as error:
|
|
480
|
+
recovery_attempts += 1
|
|
481
|
+
recovery = (
|
|
482
|
+
self.history.recover(messages, error, attempt=recovery_attempts)
|
|
483
|
+
if self.history
|
|
484
|
+
else None
|
|
485
|
+
)
|
|
486
|
+
if recovery is None:
|
|
487
|
+
raise
|
|
488
|
+
messages[:] = recovery.messages
|
|
489
|
+
last_prompt = None
|
|
490
|
+
if recovery.compacted:
|
|
491
|
+
await events.emit("compacted", changed=recovery.compacted, aggressive=True)
|
|
492
|
+
continue
|
|
493
|
+
|
|
494
|
+
recovery_attempts = 0
|
|
495
|
+
turns += 1
|
|
496
|
+
if turn.usage:
|
|
497
|
+
last_usage = turn.usage
|
|
498
|
+
last_prompt = turn.usage.get("prompt_tokens", last_prompt)
|
|
499
|
+
await events.emit("usage", turn=turns, usage=turn.usage)
|
|
500
|
+
finish_reason = turn.finish_reason
|
|
501
|
+
assistant: Message = {"role": "assistant", "content": turn.text}
|
|
502
|
+
_private_state(assistant, turn.state)
|
|
503
|
+
if turn.calls:
|
|
504
|
+
assistant["tool_calls"] = [call.as_message() for call in turn.calls]
|
|
505
|
+
messages.append(assistant)
|
|
506
|
+
if not turn.calls:
|
|
507
|
+
result = RunResult(
|
|
508
|
+
text=turn.text,
|
|
509
|
+
status=RunStatus.SUCCEEDED,
|
|
510
|
+
turns=turns,
|
|
511
|
+
run_id=events.run_id,
|
|
512
|
+
session_id=session_id,
|
|
513
|
+
finish_reason=finish_reason,
|
|
514
|
+
usage=last_usage,
|
|
515
|
+
)
|
|
516
|
+
await events.emit(
|
|
517
|
+
"turn_succeeded",
|
|
518
|
+
turn=turns,
|
|
519
|
+
text=turn.text,
|
|
520
|
+
finish_reason=finish_reason,
|
|
521
|
+
)
|
|
522
|
+
return result
|
|
523
|
+
|
|
524
|
+
pending = list(turn.calls)
|
|
525
|
+
question = _last_user_question(messages)
|
|
526
|
+
for call in pending:
|
|
527
|
+
if cancellation is not None:
|
|
528
|
+
cancellation.throw_if_cancelled(run_id=events.run_id)
|
|
529
|
+
execution = await self.executor.execute(
|
|
530
|
+
call,
|
|
531
|
+
ToolContext(
|
|
532
|
+
cwd=workspace,
|
|
533
|
+
call=call,
|
|
534
|
+
model=turn.model or getattr(self.backend, "model", None),
|
|
535
|
+
user_question=question,
|
|
536
|
+
cancellation=cancellation,
|
|
537
|
+
services=services,
|
|
538
|
+
),
|
|
539
|
+
events,
|
|
540
|
+
turn=turns,
|
|
541
|
+
)
|
|
542
|
+
tool_message: Message = {
|
|
543
|
+
"role": "tool",
|
|
544
|
+
"tool_call_id": call.id,
|
|
545
|
+
"content": execution.result.text,
|
|
546
|
+
}
|
|
547
|
+
_private_state(tool_message, execution.result.state)
|
|
548
|
+
_merge_attachments(tool_message, execution.result)
|
|
549
|
+
messages.append(tool_message)
|
|
550
|
+
pending = []
|
|
551
|
+
|
|
552
|
+
result = RunResult(
|
|
553
|
+
text="",
|
|
554
|
+
status=RunStatus.BLOCKED,
|
|
555
|
+
turns=turns,
|
|
556
|
+
run_id=events.run_id,
|
|
557
|
+
session_id=session_id,
|
|
558
|
+
finish_reason=finish_reason,
|
|
559
|
+
stop_reason="max_turns",
|
|
560
|
+
usage=last_usage,
|
|
561
|
+
)
|
|
562
|
+
await events.emit("turn_blocked", turn=turns, reason="max_turns", text=result.text)
|
|
563
|
+
return result
|
|
564
|
+
except AgentInterrupted as error:
|
|
565
|
+
_answer_pending(messages, pending)
|
|
566
|
+
if error.partial:
|
|
567
|
+
messages.append({"role": "assistant", "content": error.partial})
|
|
568
|
+
error.run_id = error.run_id or events.run_id
|
|
569
|
+
await events.emit(
|
|
570
|
+
"turn_interrupted", turn=turns, reason=error.reason, partial=error.partial
|
|
571
|
+
)
|
|
572
|
+
raise
|
|
573
|
+
except (asyncio.CancelledError, KeyboardInterrupt) as error:
|
|
574
|
+
_answer_pending(messages, pending)
|
|
575
|
+
interrupted = AgentInterrupted(run_id=events.run_id)
|
|
576
|
+
await events.emit("turn_interrupted", turn=turns, reason="interrupted")
|
|
577
|
+
raise interrupted from error
|
|
578
|
+
except Exception as error:
|
|
579
|
+
await events.emit(
|
|
580
|
+
"turn_failed", turn=turns, error_type=type(error).__name__, reason=str(error)
|
|
581
|
+
)
|
|
582
|
+
raise
|
|
583
|
+
|
|
584
|
+
@property
|
|
585
|
+
def executor(self) -> ToolExecutor:
|
|
586
|
+
# Constructing is cheap and keeps the public registry replaceable by an
|
|
587
|
+
# embedder before a run, without a second stale registry reference.
|
|
588
|
+
return ToolExecutor(self.tools)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Provider behavior built from runtime metadata, credential storage, and clients."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from .client import make_openai_client, make_openai_sync_client
|
|
10
|
+
from .credentials import FileCredentialStore
|
|
11
|
+
from .providers import Credential, ProviderSpec
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class ManagedProvider(ProviderSpec):
|
|
16
|
+
"""A ProviderSpec with optional host-selected storage and client factories."""
|
|
17
|
+
|
|
18
|
+
credential_store: FileCredentialStore | None = None
|
|
19
|
+
base_url_override: str | Callable[[], str] = ""
|
|
20
|
+
client_factory: Callable[..., Any] = make_openai_client
|
|
21
|
+
sync_client_factory: Callable[..., Any] = make_openai_sync_client
|
|
22
|
+
|
|
23
|
+
def _override(self) -> str:
|
|
24
|
+
value = (
|
|
25
|
+
self.base_url_override() if callable(self.base_url_override) else self.base_url_override
|
|
26
|
+
)
|
|
27
|
+
return value.strip()
|
|
28
|
+
|
|
29
|
+
def resolve(self) -> Credential | None:
|
|
30
|
+
if credential := self.resolve_env():
|
|
31
|
+
return Credential(
|
|
32
|
+
credential.api_key,
|
|
33
|
+
self._override() or credential.base_url,
|
|
34
|
+
credential.source,
|
|
35
|
+
)
|
|
36
|
+
if self.credential_store is not None:
|
|
37
|
+
return self.credential_store.resolve(self.id, self.base_url)
|
|
38
|
+
return None
|
|
39
|
+
|
|
40
|
+
async def login(
|
|
41
|
+
self,
|
|
42
|
+
*,
|
|
43
|
+
_args: list[str],
|
|
44
|
+
prompt_secret: Any,
|
|
45
|
+
_on_status: Any | None = None,
|
|
46
|
+
) -> Credential:
|
|
47
|
+
if self.credential_store is None:
|
|
48
|
+
raise ValueError("no credential store is configured")
|
|
49
|
+
key = (prompt_secret(self.prompt) or "").strip()
|
|
50
|
+
if not key:
|
|
51
|
+
raise ValueError("cancelled — empty key")
|
|
52
|
+
return self.credential_store.login(self.id, key, self.base_url)
|
|
53
|
+
|
|
54
|
+
def logout(self) -> bool:
|
|
55
|
+
return self.credential_store.logout(self.id) if self.credential_store else False
|
|
56
|
+
|
|
57
|
+
def make_client(self, credential: Credential) -> Any:
|
|
58
|
+
return self.client_factory(credential.api_key, credential.base_url)
|
|
59
|
+
|
|
60
|
+
async def list_models(self, credential: Credential) -> list[str]:
|
|
61
|
+
client = self.make_client(credential)
|
|
62
|
+
try:
|
|
63
|
+
page = await client.models.list()
|
|
64
|
+
return sorted({m.id for m in page.data if getattr(m, "id", None)})
|
|
65
|
+
finally:
|
|
66
|
+
await client.close()
|
|
67
|
+
|
|
68
|
+
def list_models_sync(self, credential: Credential, *, timeout: float) -> list[str]:
|
|
69
|
+
client = self.sync_client_factory(
|
|
70
|
+
credential.api_key,
|
|
71
|
+
credential.base_url,
|
|
72
|
+
timeout=timeout,
|
|
73
|
+
)
|
|
74
|
+
try:
|
|
75
|
+
page = client.models.list()
|
|
76
|
+
return sorted({m.id for m in page.data if getattr(m, "id", None)})
|
|
77
|
+
finally:
|
|
78
|
+
client.close()
|