lm-deluge 0.0.16__py3-none-any.whl → 0.0.18__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.
@@ -0,0 +1,559 @@
1
+ # from collections import defaultdict
2
+ # from pathlib import Path
3
+ # from typing import Any, Literal, get_args
4
+
5
+ # Command_20250124 = Literal[
6
+ # "view",
7
+ # "create",
8
+ # "str_replace",
9
+ # "insert",
10
+ # "undo_edit",
11
+ # ]
12
+
13
+ # Command_20250429 = Literal[
14
+ # "view",
15
+ # "create",
16
+ # "str_replace",
17
+ # "insert",
18
+ # ]
19
+ # SNIPPET_LINES: int = 4
20
+
21
+
22
+ # class EditTool:
23
+ # """
24
+ # An filesystem editor tool that allows the agent to view,
25
+ # create, and edit files. The tool parameters are defined by
26
+ # Anthropic and are not editable.
27
+ # """
28
+
29
+ # api_type: Literal["text_editor_20250124"] = "text_editor_20250124"
30
+ # name: Literal["str_replace_editor"] = "str_replace_editor"
31
+
32
+ # _file_history: dict[Path, list[str]]
33
+
34
+ # def __init__(self):
35
+ # self._file_history = defaultdict(list)
36
+ # super().__init__()
37
+
38
+ # def to_params(self) -> Any:
39
+ # return {
40
+ # "name": self.name,
41
+ # "type": self.api_type,
42
+ # }
43
+
44
+ # async def __call__(
45
+ # self,
46
+ # *,
47
+ # command: Command_20250124,
48
+ # path: str,
49
+ # file_text: str | None = None,
50
+ # view_range: list[int] | None = None,
51
+ # old_str: str | None = None,
52
+ # new_str: str | None = None,
53
+ # insert_line: int | None = None,
54
+ # **kwargs,
55
+ # ):
56
+ # _path = Path(path)
57
+ # self.validate_path(command, _path)
58
+ # if command == "view":
59
+ # return await self.view(_path, view_range)
60
+ # elif command == "create":
61
+ # if file_text is None:
62
+ # raise ToolError("Parameter `file_text` is required for command: create")
63
+ # self.write_file(_path, file_text)
64
+ # self._file_history[_path].append(file_text)
65
+ # return ToolResult(output=f"File created successfully at: {_path}")
66
+ # elif command == "str_replace":
67
+ # if old_str is None:
68
+ # raise ToolError(
69
+ # "Parameter `old_str` is required for command: str_replace"
70
+ # )
71
+ # return self.str_replace(_path, old_str, new_str)
72
+ # elif command == "insert":
73
+ # if insert_line is None:
74
+ # raise ToolError(
75
+ # "Parameter `insert_line` is required for command: insert"
76
+ # )
77
+ # if new_str is None:
78
+ # raise ToolError("Parameter `new_str` is required for command: insert")
79
+ # return self.insert(_path, insert_line, new_str)
80
+ # elif command == "undo_edit":
81
+ # return self.undo_edit(_path)
82
+ # raise ToolError(
83
+ # f"Unrecognized command {command}. The allowed commands for the {self.name} tool are: {', '.join(get_args(Command_20250124))}"
84
+ # )
85
+
86
+ # def validate_path(self, command: str, path: Path):
87
+ # """
88
+ # Check that the path/command combination is valid.
89
+ # """
90
+ # # Check if its an absolute path
91
+ # if not path.is_absolute():
92
+ # suggested_path = Path("") / path
93
+ # raise ToolError(
94
+ # f"The path {path} is not an absolute path, it should start with `/`. Maybe you meant {suggested_path}?"
95
+ # )
96
+ # # Check if path exists
97
+ # if not path.exists() and command != "create":
98
+ # raise ToolError(
99
+ # f"The path {path} does not exist. Please provide a valid path."
100
+ # )
101
+ # if path.exists() and command == "create":
102
+ # raise ToolError(
103
+ # f"File already exists at: {path}. Cannot overwrite files using command `create`."
104
+ # )
105
+ # # Check if the path points to a directory
106
+ # if path.is_dir():
107
+ # if command != "view":
108
+ # raise ToolError(
109
+ # f"The path {path} is a directory and only the `view` command can be used on directories"
110
+ # )
111
+
112
+ # async def view(self, path: Path, view_range: list[int] | None = None):
113
+ # """Implement the view command"""
114
+ # if path.is_dir():
115
+ # if view_range:
116
+ # raise ToolError(
117
+ # "The `view_range` parameter is not allowed when `path` points to a directory."
118
+ # )
119
+
120
+ # _, stdout, stderr = await run(
121
+ # rf"find {path} -maxdepth 2 -not -path '*/\.*'"
122
+ # )
123
+ # if not stderr:
124
+ # stdout = f"Here's the files and directories up to 2 levels deep in {path}, excluding hidden items:\n{stdout}\n"
125
+ # return CLIResult(output=stdout, error=stderr)
126
+
127
+ # file_content = self.read_file(path)
128
+ # init_line = 1
129
+ # if view_range:
130
+ # if len(view_range) != 2 or not all(isinstance(i, int) for i in view_range):
131
+ # raise ToolError(
132
+ # "Invalid `view_range`. It should be a list of two integers."
133
+ # )
134
+ # file_lines = file_content.split("\n")
135
+ # n_lines_file = len(file_lines)
136
+ # init_line, final_line = view_range
137
+ # if init_line < 1 or init_line > n_lines_file:
138
+ # raise ToolError(
139
+ # f"Invalid `view_range`: {view_range}. Its first element `{init_line}` should be within the range of lines of the file: {[1, n_lines_file]}"
140
+ # )
141
+ # if final_line > n_lines_file:
142
+ # raise ToolError(
143
+ # f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be smaller than the number of lines in the file: `{n_lines_file}`"
144
+ # )
145
+ # if final_line != -1 and final_line < init_line:
146
+ # raise ToolError(
147
+ # f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be larger or equal than its first `{init_line}`"
148
+ # )
149
+
150
+ # if final_line == -1:
151
+ # file_content = "\n".join(file_lines[init_line - 1 :])
152
+ # else:
153
+ # file_content = "\n".join(file_lines[init_line - 1 : final_line])
154
+
155
+ # return CLIResult(
156
+ # output=self._make_output(file_content, str(path), init_line=init_line)
157
+ # )
158
+
159
+ # def str_replace(self, path: Path, old_str: str, new_str: str | None):
160
+ # """Implement the str_replace command, which replaces old_str with new_str in the file content"""
161
+ # # Read the file content
162
+ # file_content = self.read_file(path).expandtabs()
163
+ # old_str = old_str.expandtabs()
164
+ # new_str = new_str.expandtabs() if new_str is not None else ""
165
+
166
+ # # Check if old_str is unique in the file
167
+ # occurrences = file_content.count(old_str)
168
+ # if occurrences == 0:
169
+ # raise ToolError(
170
+ # f"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}."
171
+ # )
172
+ # elif occurrences > 1:
173
+ # file_content_lines = file_content.split("\n")
174
+ # lines = [
175
+ # idx + 1
176
+ # for idx, line in enumerate(file_content_lines)
177
+ # if old_str in line
178
+ # ]
179
+ # raise ToolError(
180
+ # f"No replacement was performed. Multiple occurrences of old_str `{old_str}` in lines {lines}. Please ensure it is unique"
181
+ # )
182
+
183
+ # # Replace old_str with new_str
184
+ # new_file_content = file_content.replace(old_str, new_str)
185
+
186
+ # # Write the new content to the file
187
+ # self.write_file(path, new_file_content)
188
+
189
+ # # Save the content to history
190
+ # self._file_history[path].append(file_content)
191
+
192
+ # # Create a snippet of the edited section
193
+ # replacement_line = file_content.split(old_str)[0].count("\n")
194
+ # start_line = max(0, replacement_line - SNIPPET_LINES)
195
+ # end_line = replacement_line + SNIPPET_LINES + new_str.count("\n")
196
+ # snippet = "\n".join(new_file_content.split("\n")[start_line : end_line + 1])
197
+
198
+ # # Prepare the success message
199
+ # success_msg = f"The file {path} has been edited. "
200
+ # success_msg += self._make_output(
201
+ # snippet, f"a snippet of {path}", start_line + 1
202
+ # )
203
+ # success_msg += "Review the changes and make sure they are as expected. Edit the file again if necessary."
204
+
205
+ # return CLIResult(output=success_msg)
206
+
207
+ # def insert(self, path: Path, insert_line: int, new_str: str):
208
+ # """Implement the insert command, which inserts new_str at the specified line in the file content."""
209
+ # file_text = self.read_file(path).expandtabs()
210
+ # new_str = new_str.expandtabs()
211
+ # file_text_lines = file_text.split("\n")
212
+ # n_lines_file = len(file_text_lines)
213
+
214
+ # if insert_line < 0 or insert_line > n_lines_file:
215
+ # raise ToolError(
216
+ # f"Invalid `insert_line` parameter: {insert_line}. It should be within the range of lines of the file: {[0, n_lines_file]}"
217
+ # )
218
+
219
+ # new_str_lines = new_str.split("\n")
220
+ # new_file_text_lines = (
221
+ # file_text_lines[:insert_line]
222
+ # + new_str_lines
223
+ # + file_text_lines[insert_line:]
224
+ # )
225
+ # snippet_lines = (
226
+ # file_text_lines[max(0, insert_line - SNIPPET_LINES) : insert_line]
227
+ # + new_str_lines
228
+ # + file_text_lines[insert_line : insert_line + SNIPPET_LINES]
229
+ # )
230
+
231
+ # new_file_text = "\n".join(new_file_text_lines)
232
+ # snippet = "\n".join(snippet_lines)
233
+
234
+ # self.write_file(path, new_file_text)
235
+ # self._file_history[path].append(file_text)
236
+
237
+ # success_msg = f"The file {path} has been edited. "
238
+ # success_msg += self._make_output(
239
+ # snippet,
240
+ # "a snippet of the edited file",
241
+ # max(1, insert_line - SNIPPET_LINES + 1),
242
+ # )
243
+ # success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary."
244
+ # return CLIResult(output=success_msg)
245
+
246
+ # def undo_edit(self, path: Path):
247
+ # """Implement the undo_edit command."""
248
+ # if not self._file_history[path]:
249
+ # raise ToolError(f"No edit history found for {path}.")
250
+
251
+ # old_text = self._file_history[path].pop()
252
+ # self.write_file(path, old_text)
253
+
254
+ # return CLIResult(
255
+ # output=f"Last edit to {path} undone successfully. {self._make_output(old_text, str(path))}"
256
+ # )
257
+
258
+ # def read_file(self, path: Path):
259
+ # """Read the content of a file from a given path; raise a ToolError if an error occurs."""
260
+ # try:
261
+ # return path.read_text()
262
+ # except Exception as e:
263
+ # raise ToolError(f"Ran into {e} while trying to read {path}") from None
264
+
265
+ # def write_file(self, path: Path, file: str):
266
+ # """Write the content of a file to a given path; raise a ToolError if an error occurs."""
267
+ # try:
268
+ # path.write_text(file)
269
+ # except Exception as e:
270
+ # raise ToolError(f"Ran into {e} while trying to write to {path}") from None
271
+
272
+ # def _make_output(
273
+ # self,
274
+ # file_content: str,
275
+ # file_descriptor: str,
276
+ # init_line: int = 1,
277
+ # expand_tabs: bool = True,
278
+ # ):
279
+ # """Generate output for the CLI based on the content of a file."""
280
+ # file_content = maybe_truncate(file_content)
281
+ # if expand_tabs:
282
+ # file_content = file_content.expandtabs()
283
+ # file_content = "\n".join(
284
+ # [
285
+ # f"{i + init_line:6}\t{line}"
286
+ # for i, line in enumerate(file_content.split("\n"))
287
+ # ]
288
+ # )
289
+ # return (
290
+ # f"Here's the result of running `cat -n` on {file_descriptor}:\n"
291
+ # + file_content
292
+ # + "\n"
293
+ # )
294
+
295
+
296
+ # class EditTool20250429(BaseAnthropicTool):
297
+ # """
298
+ # An filesystem editor tool that allows the agent to view, create, and edit files.
299
+ # The tool parameters are defined by Anthropic and are not editable.
300
+ # """
301
+
302
+ # api_type: Literal["str_replace_based_edit_tool"] = "str_replace_based_edit_tool"
303
+ # name: Literal["str_replace_based_edit_tool"] = "str_replace_based_edit_tool"
304
+
305
+ # _file_history: dict[Path, list[str]]
306
+
307
+ # def __init__(self):
308
+ # self._file_history = defaultdict(list)
309
+ # super().__init__()
310
+
311
+ # def to_params(self) -> Any:
312
+ # return {
313
+ # "name": self.name,
314
+ # "type": self.api_type,
315
+ # }
316
+
317
+ # async def __call__(
318
+ # self,
319
+ # *,
320
+ # command: Command_20250429,
321
+ # path: str,
322
+ # file_text: str | None = None,
323
+ # view_range: list[int] | None = None,
324
+ # old_str: str | None = None,
325
+ # new_str: str | None = None,
326
+ # insert_line: int | None = None,
327
+ # **kwargs,
328
+ # ):
329
+ # _path = Path(path)
330
+ # self.validate_path(command, _path)
331
+ # if command == "view":
332
+ # return await self.view(_path, view_range)
333
+ # elif command == "create":
334
+ # if file_text is None:
335
+ # raise ToolError("Parameter `file_text` is required for command: create")
336
+ # self.write_file(_path, file_text)
337
+ # self._file_history[_path].append(file_text)
338
+ # return ToolResult(output=f"File created successfully at: {_path}")
339
+ # elif command == "str_replace":
340
+ # if old_str is None:
341
+ # raise ToolError(
342
+ # "Parameter `old_str` is required for command: str_replace"
343
+ # )
344
+ # return self.str_replace(_path, old_str, new_str)
345
+ # elif command == "insert":
346
+ # if insert_line is None:
347
+ # raise ToolError(
348
+ # "Parameter `insert_line` is required for command: insert"
349
+ # )
350
+ # if new_str is None:
351
+ # raise ToolError("Parameter `new_str` is required for command: insert")
352
+ # return self.insert(_path, insert_line, new_str)
353
+ # # Note: undo_edit command was removed in this version
354
+ # raise ToolError(
355
+ # f"Unrecognized command {command}. The allowed commands for the {self.name} tool are: {', '.join(get_args(Command_20250429))}"
356
+ # )
357
+
358
+ # def validate_path(self, command: str, path: Path):
359
+ # """
360
+ # Check that the path/command combination is valid.
361
+ # """
362
+ # # Check if its an absolute path
363
+ # if not path.is_absolute():
364
+ # suggested_path = Path("") / path
365
+ # raise ToolError(
366
+ # f"The path {path} is not an absolute path, it should start with `/`. Maybe you meant {suggested_path}?"
367
+ # )
368
+ # # Check if path exists
369
+ # if not path.exists() and command != "create":
370
+ # raise ToolError(
371
+ # f"The path {path} does not exist. Please provide a valid path."
372
+ # )
373
+ # if path.exists() and command == "create":
374
+ # raise ToolError(
375
+ # f"File already exists at: {path}. Cannot overwrite files using command `create`."
376
+ # )
377
+ # # Check if the path points to a directory
378
+ # if path.is_dir():
379
+ # if command != "view":
380
+ # raise ToolError(
381
+ # f"The path {path} is a directory and only the `view` command can be used on directories"
382
+ # )
383
+
384
+ # async def view(self, path: Path, view_range: list[int] | None = None):
385
+ # """Implement the view command"""
386
+ # if path.is_dir():
387
+ # if view_range:
388
+ # raise ToolError(
389
+ # "The `view_range` parameter is not allowed when `path` points to a directory."
390
+ # )
391
+
392
+ # _, stdout, stderr = await run(
393
+ # rf"find {path} -maxdepth 2 -not -path '*/\.*'"
394
+ # )
395
+ # if not stderr:
396
+ # stdout = f"Here's the files and directories up to 2 levels deep in {path}, excluding hidden items:\n{stdout}\n"
397
+ # return CLIResult(output=stdout, error=stderr)
398
+
399
+ # file_content = self.read_file(path)
400
+ # init_line = 1
401
+ # if view_range:
402
+ # if len(view_range) != 2 or not all(isinstance(i, int) for i in view_range):
403
+ # raise ToolError(
404
+ # "Invalid `view_range`. It should be a list of two integers."
405
+ # )
406
+ # file_lines = file_content.split("\n")
407
+ # n_lines_file = len(file_lines)
408
+ # init_line, final_line = view_range
409
+ # if init_line < 1 or init_line > n_lines_file:
410
+ # raise ToolError(
411
+ # f"Invalid `view_range`: {view_range}. Its first element `{init_line}` should be within the range of lines of the file: {[1, n_lines_file]}"
412
+ # )
413
+ # if final_line > n_lines_file:
414
+ # raise ToolError(
415
+ # f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be smaller than the number of lines in the file: `{n_lines_file}`"
416
+ # )
417
+ # if final_line != -1 and final_line < init_line:
418
+ # raise ToolError(
419
+ # f"Invalid `view_range`: {view_range}. Its second element `{final_line}` should be larger or equal than its first `{init_line}`"
420
+ # )
421
+
422
+ # if final_line == -1:
423
+ # file_content = "\n".join(file_lines[init_line - 1 :])
424
+ # else:
425
+ # file_content = "\n".join(file_lines[init_line - 1 : final_line])
426
+
427
+ # return CLIResult(
428
+ # output=self._make_output(file_content, str(path), init_line=init_line)
429
+ # )
430
+
431
+ # def str_replace(self, path: Path, old_str: str, new_str: str | None):
432
+ # """Implement the str_replace command, which replaces old_str with new_str in the file content"""
433
+ # # Read the file content
434
+ # file_content = self.read_file(path).expandtabs()
435
+ # old_str = old_str.expandtabs()
436
+ # new_str = new_str.expandtabs() if new_str is not None else ""
437
+
438
+ # # Check if old_str is unique in the file
439
+ # occurrences = file_content.count(old_str)
440
+ # if occurrences == 0:
441
+ # raise ToolError(
442
+ # f"No replacement was performed, old_str `{old_str}` did not appear verbatim in {path}."
443
+ # )
444
+ # elif occurrences > 1:
445
+ # file_content_lines = file_content.split("\n")
446
+ # lines = [
447
+ # idx + 1
448
+ # for idx, line in enumerate(file_content_lines)
449
+ # if old_str in line
450
+ # ]
451
+ # raise ToolError(
452
+ # f"No replacement was performed. Multiple occurrences of old_str `{old_str}` in lines {lines}. Please ensure it is unique"
453
+ # )
454
+
455
+ # # Replace old_str with new_str
456
+ # new_file_content = file_content.replace(old_str, new_str)
457
+
458
+ # # Write the new content to the file
459
+ # self.write_file(path, new_file_content)
460
+
461
+ # # Save the content to history
462
+ # self._file_history[path].append(file_content)
463
+
464
+ # # Create a snippet of the edited section
465
+ # replacement_line = file_content.split(old_str)[0].count("\n")
466
+ # start_line = max(0, replacement_line - SNIPPET_LINES)
467
+ # end_line = replacement_line + SNIPPET_LINES + new_str.count("\n")
468
+ # snippet = "\n".join(new_file_content.split("\n")[start_line : end_line + 1])
469
+
470
+ # # Prepare the success message
471
+ # success_msg = f"The file {path} has been edited. "
472
+ # success_msg += self._make_output(
473
+ # snippet, f"a snippet of {path}", start_line + 1
474
+ # )
475
+ # success_msg += "Review the changes and make sure they are as expected. Edit the file again if necessary."
476
+
477
+ # return CLIResult(output=success_msg)
478
+
479
+ # def insert(self, path: Path, insert_line: int, new_str: str):
480
+ # """Implement the insert command, which inserts new_str at the specified line in the file content."""
481
+ # file_text = self.read_file(path).expandtabs()
482
+ # new_str = new_str.expandtabs()
483
+ # file_text_lines = file_text.split("\n")
484
+ # n_lines_file = len(file_text_lines)
485
+
486
+ # if insert_line < 0 or insert_line > n_lines_file:
487
+ # raise ToolError(
488
+ # f"Invalid `insert_line` parameter: {insert_line}. It should be within the range of lines of the file: {[0, n_lines_file]}"
489
+ # )
490
+
491
+ # new_str_lines = new_str.split("\n")
492
+ # new_file_text_lines = (
493
+ # file_text_lines[:insert_line]
494
+ # + new_str_lines
495
+ # + file_text_lines[insert_line:]
496
+ # )
497
+ # snippet_lines = (
498
+ # file_text_lines[max(0, insert_line - SNIPPET_LINES) : insert_line]
499
+ # + new_str_lines
500
+ # + file_text_lines[insert_line : insert_line + SNIPPET_LINES]
501
+ # )
502
+
503
+ # new_file_text = "\n".join(new_file_text_lines)
504
+ # snippet = "\n".join(snippet_lines)
505
+
506
+ # self.write_file(path, new_file_text)
507
+ # self._file_history[path].append(file_text)
508
+
509
+ # success_msg = f"The file {path} has been edited. "
510
+ # success_msg += self._make_output(
511
+ # snippet,
512
+ # "a snippet of the edited file",
513
+ # max(1, insert_line - SNIPPET_LINES + 1),
514
+ # )
515
+ # success_msg += "Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary."
516
+ # return CLIResult(output=success_msg)
517
+
518
+ # # Note: undo_edit method is not implemented in this version as it was removed
519
+
520
+ # def read_file(self, path: Path):
521
+ # """Read the content of a file from a given path; raise a ToolError if an error occurs."""
522
+ # try:
523
+ # return path.read_text()
524
+ # except Exception as e:
525
+ # raise ToolError(f"Ran into {e} while trying to read {path}") from None
526
+
527
+ # def write_file(self, path: Path, file: str):
528
+ # """Write the content of a file to a given path; raise a ToolError if an error occurs."""
529
+ # try:
530
+ # path.write_text(file)
531
+ # except Exception as e:
532
+ # raise ToolError(f"Ran into {e} while trying to write to {path}") from None
533
+
534
+ # def _make_output(
535
+ # self,
536
+ # file_content: str,
537
+ # file_descriptor: str,
538
+ # init_line: int = 1,
539
+ # expand_tabs: bool = True,
540
+ # ):
541
+ # """Generate output for the CLI based on the content of a file."""
542
+ # file_content = maybe_truncate(file_content)
543
+ # if expand_tabs:
544
+ # file_content = file_content.expandtabs()
545
+ # file_content = "\n".join(
546
+ # [
547
+ # f"{i + init_line:6}\t{line}"
548
+ # for i, line in enumerate(file_content.split("\n"))
549
+ # ]
550
+ # )
551
+ # return (
552
+ # f"Here's the result of running `cat -n` on {file_descriptor}:\n"
553
+ # + file_content
554
+ # + "\n"
555
+ # )
556
+
557
+
558
+ # class EditTool20241022(EditTool20250124):
559
+ # api_type: Literal["text_editor_20250429"] = "text_editor_20250429" # pyright: ignore[reportIncompatibleVariableOverride]
@@ -0,0 +1,9 @@
1
+ class BuiltInTool:
2
+ def for_anthropic(self):
3
+ raise NotImplementedError("Not implemented")
4
+
5
+ def for_openai_chat(self):
6
+ raise NotImplementedError("Not implemented")
7
+
8
+ def for_openai_responses(self):
9
+ raise NotImplementedError("Not implemented")
lm_deluge/client.py CHANGED
@@ -50,6 +50,7 @@ class LLMClient(BaseModel):
50
50
  max_attempts: int = 5
51
51
  request_timeout: int = 30
52
52
  cache: Any = None
53
+ extra_headers: dict[str, str] | None = None
53
54
  # sampling params - if provided, and sampling_params is not,
54
55
  # these override the defaults
55
56
  temperature: float = 0.75
@@ -364,6 +365,7 @@ class LLMClient(BaseModel):
364
365
  tools=tools,
365
366
  cache=cache,
366
367
  use_responses_api=use_responses_api,
368
+ extra_headers=self.extra_headers,
367
369
  )
368
370
  except StopIteration:
369
371
  prompts_not_finished = False
@@ -451,13 +453,98 @@ class LLMClient(BaseModel):
451
453
  model, sampling_params = self._select_model()
452
454
  if isinstance(prompt, str):
453
455
  prompt = Conversation.user(prompt)
454
- async for item in stream_chat(model, prompt, sampling_params, tools, None):
456
+ async for item in stream_chat(
457
+ model, prompt, sampling_params, tools, None, self.extra_headers
458
+ ):
455
459
  if isinstance(item, str):
456
460
  print(item, end="", flush=True)
457
461
  else:
458
462
  # final item
459
463
  return item
460
464
 
465
+ async def run_agent_loop(
466
+ self,
467
+ conversation: str | Conversation,
468
+ *,
469
+ tools: list[Tool | dict] | None = None,
470
+ max_rounds: int = 5,
471
+ show_progress: bool = False,
472
+ ) -> tuple[Conversation, APIResponse]:
473
+ """Run a simple agent loop until no more tool calls are returned.
474
+
475
+ The provided ``conversation`` will be mutated and returned alongside the
476
+ final ``APIResponse`` from the model. ``tools`` may include ``Tool``
477
+ instances or built‑in tool dictionaries.
478
+ """
479
+
480
+ if isinstance(conversation, str):
481
+ conversation = Conversation.user(conversation)
482
+
483
+ last_response: APIResponse | None = None
484
+
485
+ for _ in range(max_rounds):
486
+ responses = await self.process_prompts_async(
487
+ [conversation],
488
+ tools=tools, # type: ignore
489
+ return_completions_only=False,
490
+ show_progress=show_progress,
491
+ )
492
+
493
+ last_response = responses[0]
494
+ if last_response is None or last_response.content is None:
495
+ break
496
+
497
+ conversation.add(last_response.content)
498
+
499
+ tool_calls = last_response.content.tool_calls
500
+ if not tool_calls:
501
+ break
502
+
503
+ for call in tool_calls:
504
+ tool_obj = None
505
+ if tools:
506
+ for t in tools:
507
+ if isinstance(t, Tool) and t.name == call.name:
508
+ tool_obj = t
509
+ break
510
+
511
+ if isinstance(tool_obj, Tool) and tool_obj.run is not None:
512
+ try:
513
+ result = await tool_obj.acall(**call.arguments)
514
+ except Exception as e: # pragma: no cover - best effort
515
+ result = f"Error: {e}"
516
+ else:
517
+ result = f"Tool {call.name} not found"
518
+
519
+ if not isinstance(result, (str, dict, list)):
520
+ result = str(result)
521
+
522
+ conversation.add_tool_result(call.id, result) # type: ignore
523
+
524
+ if last_response is None:
525
+ raise RuntimeError("model did not return a response")
526
+
527
+ return conversation, last_response
528
+
529
+ def run_agent_loop_sync(
530
+ self,
531
+ conversation: str | Conversation,
532
+ *,
533
+ tools: list[Tool | dict | MCPServer] | None = None,
534
+ max_rounds: int = 5,
535
+ show_progress: bool = False,
536
+ ) -> tuple[Conversation, APIResponse]:
537
+ """Synchronous wrapper for :meth:`run_agent_loop`."""
538
+
539
+ return asyncio.run(
540
+ self.run_agent_loop(
541
+ conversation,
542
+ tools=tools, # type: ignore
543
+ max_rounds=max_rounds,
544
+ show_progress=show_progress,
545
+ )
546
+ )
547
+
461
548
  async def submit_batch_job(
462
549
  self,
463
550
  prompts: Sequence[str | list[dict] | Conversation],