58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431 | class Agent:
"""Drives a provider through a tool-use loop until it stops."""
def __init__(
self,
provider: Provider,
fallback_providers: list[Provider] | None = None,
tools: list[Tool] | None = None,
system_prompt: str = DEFAULT_SYSTEM_PROMPT,
max_turns: int = 25,
max_history_turns: int | None = None,
on_tool_call: Callable[[str, dict], None] | None = None,
on_tool_result: Callable[[str, str, bool], None] | None = None,
on_text_delta: Callable[[str], None] | None = None,
confirm: Callable[[str, str], bool | str] | Confirm | None = None,
on_usage: Callable[[Usage, Usage, float], None] | None = None,
on_request: Callable[[int, int], None] | None = None,
auto_compact: bool = True,
compact_threshold: float = 0.8,
on_context: Callable[[int, int], None] | None = None,
on_compact_start: Callable[[], None] | None = None,
on_compact_end: Callable[[int, int], None] | None = None,
on_message: Callable[[], None] | None = None,
on_retry: Callable[[int, float, Exception], None] | None = None,
on_turn_end: Callable[[], None] | None = None,
on_trace_event: Callable[[dict], None] | None = None,
on_provider_fallback: (
Callable[[Provider, Provider, Exception], None] | None
) = None,
) -> None:
self.provider = provider
self.fallback_providers = fallback_providers or []
self._provider_chain = [provider, *self.fallback_providers]
self._provider_index = 0
self.on_provider_fallback = on_provider_fallback
self.tools = {tool.name: tool for tool in (tools or DEFAULT_TOOLS)}
self.system_prompt = system_prompt
self.max_turns = max_turns
self.max_history_turns = max_history_turns
self.on_tool_call = on_tool_call
self.on_tool_result = on_tool_result
self.on_text_delta = on_text_delta
self.confirm = confirm
self.on_usage = on_usage
self.on_request = on_request
self.auto_compact = auto_compact
self.compact_threshold = compact_threshold
self.on_context = on_context
self.on_compact_start = on_compact_start
self.on_compact_end = on_compact_end
self.on_message = on_message
self.on_retry = on_retry
self.on_turn_end = on_turn_end
self.on_trace_event = on_trace_event
self.usage = Usage()
self._last_context_tokens = 0
def _trace(self, event_type: str, **fields) -> None:
if self.on_trace_event:
self.on_trace_event({"type": event_type, **fields})
def _complete(self, **kwargs) -> ProviderResponse:
"""`provider.complete()` with retry + exponential backoff on
transient failures (rate limits, 5xx, network blips) within a
provider. If `fallback_providers` were given and the active
provider still fails after its own retry budget (retryable or
not), advances to the next provider in `_provider_chain` — a
fixed list captured once at construction, walked by index so a
provider already tried this run is never retried, and one
earlier in the chain that recovers isn't skipped forever. A
real error on the last provider in the chain still raises."""
starting_index = self._provider_index
last_exc: Exception | None = None
for index in range(starting_index, len(self._provider_chain)):
provider = self._provider_chain[index]
delay = _RETRY_BASE_DELAY
for attempt in range(_MAX_RETRIES + 1):
try:
response = provider.complete(**kwargs)
if index != starting_index and self.on_provider_fallback:
self.on_provider_fallback(
self.provider, provider, last_exc
)
self._provider_index = index
self.provider = provider
return response
except Exception as exc:
last_exc = exc
if attempt >= _MAX_RETRIES or not _is_retryable(exc):
self._trace("provider_error", error=str(exc))
break
self._trace(
"retry",
attempt=attempt + 1,
delay=delay,
error=str(exc),
)
if self.on_retry:
self.on_retry(attempt + 1, delay, exc)
time.sleep(delay)
delay *= 2
if last_exc is None:
raise AssertionError(
"_complete exited the provider loop without an exception "
"— this is a bug"
)
raise last_exc
def _notify_message(self) -> None:
"""Fired after every message is appended to the session — lets a
caller persist incrementally instead of only after the whole
(possibly long, multi-tool-call) turn finishes, so a crash
mid-turn doesn't lose everything already done in it."""
if self.on_message:
self.on_message()
def _tool_schemas(self) -> list[dict]:
return [tool.schema() for tool in self.tools.values()]
def _execute(self, name: str, arguments: dict) -> tuple[str, bool]:
tool = self.tools.get(name)
if tool is None:
return f"Unknown tool: {name}", True
if tool.dangerous:
if self.confirm is None:
return (
f"Refused to run dangerous tool '{name}' without a confirm "
"callback. Pass confirm=... or use --yes only from a trusted CLI.",
True,
)
preview = tool.preview(**arguments)
ask = (
self.confirm.ask
if isinstance(self.confirm, Confirm)
else self.confirm
)
answer = ask(name, preview)
if answer is not True:
if isinstance(answer, str) and answer:
return f"User declined and said: {answer}", True
return "User declined to run this tool.", True
try:
result = tool.run(**arguments)
except Exception as exc:
return (
f"Tool '{name}' raised {exc.__class__.__name__}: {exc}",
True,
)
return result.output, result.is_error
def _can_parallelize(self, calls: list[ToolCall]) -> bool:
if len(calls) <= 1:
return False
names = [call.name for call in calls]
for name in set(names):
if names.count(name) <= 1:
continue
tool = self.tools.get(name)
if tool is None or not tool.concurrent_safe:
return False
return not any(
(tool := self.tools.get(call.name)) is not None and tool.dangerous
for call in calls
)
def _run_tool_calls(
self,
calls: list[ToolCall],
session: Session,
cancel_event: threading.Event | None,
) -> None:
for call in calls:
self._trace("tool_call", name=call.name, arguments=call.arguments)
if self.on_tool_call:
self.on_tool_call(call.name, call.arguments)
if cancel_event and cancel_event.is_set():
results = {call.id: ("Cancelled by user.", True) for call in calls}
elif self._can_parallelize(calls):
with ThreadPoolExecutor(max_workers=len(calls)) as executor:
futures = {
call.id: executor.submit(
self._execute, call.name, call.arguments
)
for call in calls
}
results = {
call_id: future.result() for call_id, future in futures.items()
}
else:
results = {}
for call in calls:
if cancel_event and cancel_event.is_set():
results[call.id] = ("Cancelled by user.", True)
else:
results[call.id] = self._execute(call.name, call.arguments)
for call in calls:
result_text, is_error = results[call.id]
if (
not is_error
and len(result_text) > _TOOL_RESULT_SUMMARIZE_THRESHOLD
):
result_text = self._summarize_tool_result(
call.name, result_text
)
self._trace(
"tool_result",
name=call.name,
is_error=is_error,
result_len=len(result_text),
)
if self.on_tool_result:
self.on_tool_result(call.name, result_text, is_error)
session.add_tool_result(call.id, result_text)
self._notify_message()
def _summarize_tool_result(self, tool_name: str, text: str) -> str:
summary = self._complete(
system_prompt="Summarize tool output concisely for context compaction.",
messages=[
Message(
role="user",
content=f"Tool '{tool_name}' returned:\n\n{text}\n\n{_TOOL_SUMMARY_PROMPT}",
)
],
tools=[],
)
self.usage = self.usage + summary.usage
return f"[Summarized tool output — {len(text)} chars condensed]\n{summary.text}"
def _compact(self, session: Session) -> None:
"""Summarize everything but the most recent turns via the
provider itself, replacing the older history with one condensed
message — keeps the conversation going instead of hitting the
model's context limit."""
turn_starts = [
i for i, m in enumerate(session.messages) if m.role == "user"
]
if len(turn_starts) <= _COMPACT_KEEP_RECENT_TURNS:
return
if self.on_compact_start:
self.on_compact_start()
before_count = len(session.messages)
cutoff = turn_starts[-_COMPACT_KEEP_RECENT_TURNS]
older, recent = session.messages[:cutoff], session.messages[cutoff:]
summary = self._complete(
system_prompt="Summarize conversations concisely for context compaction.",
messages=[
*older,
Message(role="user", content=_COMPACT_SUMMARY_PROMPT),
],
tools=[],
)
self.usage = self.usage + summary.usage
recent[0] = Message(
role=recent[0].role,
content=(
f"[Earlier conversation summary]\n{summary.text}\n\n---\n\n"
f"{recent[0].content}"
),
tool_call_id=recent[0].tool_call_id,
tool_calls=recent[0].tool_calls,
images=recent[0].images,
)
session.messages = recent
self._notify_message()
self._trace(
"compact", before=before_count, after=len(session.messages)
)
if self.on_compact_end:
self.on_compact_end(before_count, len(session.messages))
def run(
self,
prompt: str,
session: Session | None = None,
images: list[str] | None = None,
cancel_event: threading.Event | None = None,
) -> str:
"""Runs until the model stops calling tools, `max_turns` is hit,
or `cancel_event` is set — checked at each turn boundary and
before every tool call, never mid-request, so an in-flight
provider call always finishes first."""
session = session or Session(system_prompt=self.system_prompt)
session.add_user(prompt, images=images)
self._notify_message()
self._trace("run_start", prompt_len=len(prompt))
if self.max_history_turns is not None:
session.trim(self.max_history_turns)
for _ in range(self.max_turns):
if cancel_event and cancel_event.is_set():
self._trace("run_end", reason="cancelled")
return "Cancelled by user."
context_window = context_window_for(self.provider.model)
if self.auto_compact and self._last_context_tokens >= (
context_window * self.compact_threshold
):
self._compact(session)
tools = self._tool_schemas()
if self.on_request:
self.on_request(len(session.history()), len(tools))
started_at = time.perf_counter()
response = self._complete(
system_prompt=self.system_prompt,
messages=session.history(),
tools=tools,
on_delta=self.on_text_delta,
)
elapsed = time.perf_counter() - started_at
self.usage = self.usage + response.usage
if self.on_usage:
self.on_usage(response.usage, self.usage, elapsed)
self._trace(
"turn",
model=self.provider.model,
tool_calls=[call.name for call in response.tool_calls],
stop_reason=response.stop_reason,
elapsed=elapsed,
input_tokens=response.usage.input_tokens,
output_tokens=response.usage.output_tokens,
)
self._last_context_tokens = response.usage.input_tokens
if self.on_context:
self.on_context(self._last_context_tokens, context_window)
tool_calls = [
{
"id": call.id,
"name": call.name,
"arguments": call.arguments,
**({"extra": call.extra} if call.extra else {}),
}
for call in response.tool_calls
]
session.add_assistant(response.text, tool_calls=tool_calls or None)
self._notify_message()
if self.on_turn_end:
self.on_turn_end()
if not response.tool_calls:
self._trace("run_end", reason="done")
return response.text
self._run_tool_calls(response.tool_calls, session, cancel_event)
if cancel_event and cancel_event.is_set():
self._trace("run_end", reason="cancelled")
return "Cancelled by user."
self._trace("run_end", reason="max_turns")
return "Reached max_turns without finishing."
|