lm-deluge 0.0.16__py3-none-any.whl → 0.0.17__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.

Potentially problematic release.


This version of lm-deluge might be problematic. Click here for more details.

@@ -1,5 +1,7 @@
1
1
  from typing import Literal
2
2
 
3
+ # from lm_deluge.prompt import ToolCall
4
+
3
5
  ToolVersion = Literal["2024-10-22", "2025-01-24", "2025-04-29"]
4
6
  ToolType = Literal["bash", "computer", "editor"]
5
7
 
@@ -102,7 +104,7 @@ def web_search_tool(max_uses: int = 5):
102
104
  "type": "web_search_20250305",
103
105
  "name": "web_search",
104
106
  # Optional: Limit the number of searches per request
105
- "max_uses": 5,
107
+ "max_uses": max_uses,
106
108
  # You can use either allowed_domains or blocked_domains, but not both in the same request.
107
109
  # Optional: Only include results from these domains
108
110
  # "allowed_domains": ["example.com", "trusteddomain.org"],
File without changes
File without changes
@@ -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
@@ -458,6 +458,89 @@ class LLMClient(BaseModel):
458
458
  # final item
459
459
  return item
460
460
 
461
+ async def run_agent_loop(
462
+ self,
463
+ conversation: str | Conversation,
464
+ *,
465
+ tools: list[Tool | dict] | None = None,
466
+ max_rounds: int = 5,
467
+ show_progress: bool = False,
468
+ ) -> tuple[Conversation, APIResponse]:
469
+ """Run a simple agent loop until no more tool calls are returned.
470
+
471
+ The provided ``conversation`` will be mutated and returned alongside the
472
+ final ``APIResponse`` from the model. ``tools`` may include ``Tool``
473
+ instances or built‑in tool dictionaries.
474
+ """
475
+
476
+ if isinstance(conversation, str):
477
+ conversation = Conversation.user(conversation)
478
+
479
+ last_response: APIResponse | None = None
480
+
481
+ for _ in range(max_rounds):
482
+ responses = await self.process_prompts_async(
483
+ [conversation],
484
+ tools=tools, # type: ignore
485
+ return_completions_only=False,
486
+ show_progress=show_progress,
487
+ )
488
+
489
+ last_response = responses[0]
490
+ if last_response is None or last_response.content is None:
491
+ break
492
+
493
+ conversation.add(last_response.content)
494
+
495
+ tool_calls = last_response.content.tool_calls
496
+ if not tool_calls:
497
+ break
498
+
499
+ for call in tool_calls:
500
+ tool_obj = None
501
+ if tools:
502
+ for t in tools:
503
+ if isinstance(t, Tool) and t.name == call.name:
504
+ tool_obj = t
505
+ break
506
+
507
+ if isinstance(tool_obj, Tool) and tool_obj.run is not None:
508
+ try:
509
+ result = await tool_obj.acall(**call.arguments)
510
+ except Exception as e: # pragma: no cover - best effort
511
+ result = f"Error: {e}"
512
+ else:
513
+ result = f"Tool {call.name} not found"
514
+
515
+ if not isinstance(result, (str, dict, list)):
516
+ result = str(result)
517
+
518
+ conversation.add_tool_result(call.id, result) # type: ignore
519
+
520
+ if last_response is None:
521
+ raise RuntimeError("model did not return a response")
522
+
523
+ return conversation, last_response
524
+
525
+ def run_agent_loop_sync(
526
+ self,
527
+ conversation: str | Conversation,
528
+ *,
529
+ tools: list[Tool | dict | MCPServer] | None = None,
530
+ max_rounds: int = 5,
531
+ show_progress: bool = False,
532
+ ) -> tuple[Conversation, APIResponse]:
533
+ """Synchronous wrapper for :meth:`run_agent_loop`."""
534
+
535
+ return asyncio.run(
536
+ self.run_agent_loop(
537
+ conversation,
538
+ tools=tools, # type: ignore
539
+ max_rounds=max_rounds,
540
+ show_progress=show_progress,
541
+ )
542
+ )
543
+
461
544
  async def submit_batch_job(
462
545
  self,
463
546
  prompts: Sequence[str | list[dict] | Conversation],
lm_deluge/tool.py CHANGED
@@ -255,6 +255,10 @@ class Tool(BaseModel):
255
255
  },
256
256
  }
257
257
 
258
+ def for_openai(self, strict: bool = True, **kwargs):
259
+ """just an alias for the above"""
260
+ return self.for_openai_completions(strict=strict, **kwargs)
261
+
258
262
  def for_openai_responses(self, **kwargs) -> dict[str, Any]:
259
263
  if self.built_in:
260
264
  return {"type": self.type, **self.built_in_args, **kwargs}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lm_deluge
3
- Version: 0.0.16
3
+ Version: 0.0.17
4
4
  Summary: Python utility for using LLM API models.
5
5
  Author-email: Benjamin Anderson <ben@trytaylor.ai>
6
6
  Requires-Python: >=3.10
@@ -211,6 +211,16 @@ for tool_call in resps[0].tool_calls:
211
211
  # this is dumb sorry will make it better
212
212
  tool_to_call = [x for x in tools if x.name == tool_call.name][0]
213
213
  tool_to_call.call(**tool_call.arguments) # in async code, use .acall()
214
+
215
+ # or use the built-in agent loop to handle this automatically
216
+ import asyncio
217
+
218
+ async def main():
219
+ conv = Conversation.user("List the files in the current directory")
220
+ conv, resp = await client.run_agent_loop(conv, tools=tools)
221
+ print(resp.content.completion)
222
+
223
+ asyncio.run(main())
214
224
  ```
215
225
 
216
226
  ### Prompt Caching (Anthropic)
@@ -2,7 +2,7 @@ lm_deluge/__init__.py,sha256=mAztMuxINmh7dGbYnT8tsmw1eryQAvd0jpY8yHzd0EE,315
2
2
  lm_deluge/agent.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  lm_deluge/batches.py,sha256=05t8UL1xCKjLRKtZLkfbexLqro6T_ufFVsaNIMk05Fw,17725
4
4
  lm_deluge/cache.py,sha256=VB1kv8rM2t5XWPR60uhszFcxLDnVKOe1oA5hYjVDjIo,4375
5
- lm_deluge/client.py,sha256=Qn44_3x73PI0AxlmbGwO1MNz7fzjrkjB-RkhGf7k0Jo,22691
5
+ lm_deluge/client.py,sha256=yE82ssvJnAzaYPQchd-cAsbdVFefLGMp-D29aFkZKlE,25530
6
6
  lm_deluge/config.py,sha256=H1tQyJDNHGFuwxqQNL5Z-CjWAC0luHSBA3iY_pxmACM,932
7
7
  lm_deluge/embed.py,sha256=CO-TOlC5kOTAM8lcnicoG4u4K664vCBwHF1vHa-nAGg,13382
8
8
  lm_deluge/errors.py,sha256=oHjt7YnxWbh-eXMScIzov4NvpJMo0-2r5J6Wh5DQ1tk,209
@@ -13,7 +13,7 @@ lm_deluge/models.py,sha256=6ZCirxOpdcg_M24cKUABYbRpLK-r9dlkXxUS9aeh0UY,49657
13
13
  lm_deluge/prompt.py,sha256=SaLcUjfzgeIZRzb6fxLp6PTFLxpvcSlaazJq3__2Sqs,35248
14
14
  lm_deluge/request_context.py,sha256=SfPu9pl5NgDVLaWGQkSXdQZ7Mm-Vw4GSTlOu-PAOE3k,2290
15
15
  lm_deluge/rerank.py,sha256=-NBAJdHz9OB-SWWJnHzkFmeVO4wR6lFV7Vw-SxG7aVo,11457
16
- lm_deluge/tool.py,sha256=-jeP6lYbJxwLhuiS7m84LAfgbwCjgyH-yuUCt031L58,12239
16
+ lm_deluge/tool.py,sha256=X6NDabz53BVe1pokaKCeTLCF1-AlMAxOn1_KWiCSb7c,12407
17
17
  lm_deluge/tracker.py,sha256=-EkFDAklh5mclIFR-5SthAwNL4p1yKS8LUN7rhpOVPQ,9266
18
18
  lm_deluge/usage.py,sha256=VMEKghePFIID5JFBObqYxFpgYxnbYm_dnHy7V1-_T6M,4866
19
19
  lm_deluge/api_requests/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
@@ -30,8 +30,12 @@ lm_deluge/api_requests/deprecated/cohere.py,sha256=KgDScD6_bWhAzOY5BHZQKSA3kurt4
30
30
  lm_deluge/api_requests/deprecated/deepseek.py,sha256=FEApI93VAWDwuaqTooIyKMgONYqRhdUmiAPBRme-IYs,4582
31
31
  lm_deluge/api_requests/deprecated/mistral.py,sha256=pOfOZUM4U35I3Plch84SnAFpDAzouHcSNNMtgxRvjy4,4709
32
32
  lm_deluge/api_requests/deprecated/vertex.py,sha256=ygXz2RjdXErPCSBbiHLEWbf5_sSTIi31WoX0UaoYzRI,15275
33
- lm_deluge/built_in_tools/anthropic.py,sha256=ZvO-8hBSQdD_RzWYF0APytr8grBnBovICA76yHYTFNA,4478
33
+ lm_deluge/built_in_tools/base.py,sha256=FLYdKVAqlffA6WOu4j8wQVRd0iHMsyBW_T3vfl--aXo,276
34
34
  lm_deluge/built_in_tools/openai.py,sha256=aLuJdXbANvXVIU38Vo2zsir7zlwWgX0d8oDPT7Ql64A,721
35
+ lm_deluge/built_in_tools/anthropic/__init__.py,sha256=Dxm8MJTUwMhyXT_78uIdRe8dJsLHZcHThJ9UEurTw18,4526
36
+ lm_deluge/built_in_tools/anthropic/bash.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
37
+ lm_deluge/built_in_tools/anthropic/computer_use.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
38
+ lm_deluge/built_in_tools/anthropic/editor.py,sha256=DyC_DrHVTm1khU9QDL39vBuhu4tO5mS5H7xMRIT0Ng4,23327
35
39
  lm_deluge/llm_tools/__init__.py,sha256=TbZTETq9i_9yYskFWQKOG4pGh5ZiyE_D-h3RArfhGp4,231
36
40
  lm_deluge/llm_tools/extract.py,sha256=C3drVAMaoFx5jNE38Xi5cXxrqboyoZ9cE7nX5ylWbXw,4482
37
41
  lm_deluge/llm_tools/ocr.py,sha256=7fDlvs6uUOvbxMasvGGNJx5Fj6biM6z3lijKZaGN26k,23
@@ -41,8 +45,8 @@ lm_deluge/util/json.py,sha256=_4Oar2Cmz2L1DK3EtPLPDxD6rsYHxjROmV8ZpmMjQ-4,5822
41
45
  lm_deluge/util/logprobs.py,sha256=UkBZakOxWluaLqHrjARu7xnJ0uCHVfLGHJdnYlEcutk,11768
42
46
  lm_deluge/util/validation.py,sha256=hz5dDb3ebvZrZhnaWxOxbNSVMI6nmaOODBkk0htAUhs,1575
43
47
  lm_deluge/util/xml.py,sha256=Ft4zajoYBJR3HHCt2oHwGfymGLdvp_gegVmJ-Wqk4Ck,10547
44
- lm_deluge-0.0.16.dist-info/licenses/LICENSE,sha256=uNNXGXPCw2TC7CUs7SEBkA-Mz6QBQFWUUEWDMgEs1dU,1058
45
- lm_deluge-0.0.16.dist-info/METADATA,sha256=hfp55fuKt7dlheGj1uOcOoSMzSLmddXA85Gqe1U4KAM,12689
46
- lm_deluge-0.0.16.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
47
- lm_deluge-0.0.16.dist-info/top_level.txt,sha256=hqU-TJX93yBwpgkDtYcXyLr3t7TLSCCZ_reytJjwBaE,10
48
- lm_deluge-0.0.16.dist-info/RECORD,,
48
+ lm_deluge-0.0.17.dist-info/licenses/LICENSE,sha256=uNNXGXPCw2TC7CUs7SEBkA-Mz6QBQFWUUEWDMgEs1dU,1058
49
+ lm_deluge-0.0.17.dist-info/METADATA,sha256=vjbrAWwFloi0nwrDXWcUT31DmhomLoielYzsCf_2y7E,12978
50
+ lm_deluge-0.0.17.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
51
+ lm_deluge-0.0.17.dist-info/top_level.txt,sha256=hqU-TJX93yBwpgkDtYcXyLr3t7TLSCCZ_reytJjwBaE,10
52
+ lm_deluge-0.0.17.dist-info/RECORD,,