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

Files changed (92) hide show
  1. lm_deluge/__init__.py +25 -2
  2. lm_deluge/api_requests/anthropic.py +92 -17
  3. lm_deluge/api_requests/base.py +47 -11
  4. lm_deluge/api_requests/bedrock.py +7 -4
  5. lm_deluge/api_requests/chat_reasoning.py +4 -0
  6. lm_deluge/api_requests/gemini.py +138 -18
  7. lm_deluge/api_requests/openai.py +114 -21
  8. lm_deluge/client.py +282 -49
  9. lm_deluge/config.py +15 -3
  10. lm_deluge/mock_openai.py +643 -0
  11. lm_deluge/models/__init__.py +12 -1
  12. lm_deluge/models/anthropic.py +17 -2
  13. lm_deluge/models/arcee.py +16 -0
  14. lm_deluge/models/deepseek.py +36 -4
  15. lm_deluge/models/google.py +29 -0
  16. lm_deluge/models/grok.py +24 -0
  17. lm_deluge/models/kimi.py +36 -0
  18. lm_deluge/models/minimax.py +10 -0
  19. lm_deluge/models/openai.py +100 -0
  20. lm_deluge/models/openrouter.py +86 -8
  21. lm_deluge/models/together.py +11 -0
  22. lm_deluge/models/zai.py +1 -0
  23. lm_deluge/pipelines/gepa/__init__.py +95 -0
  24. lm_deluge/pipelines/gepa/core.py +354 -0
  25. lm_deluge/pipelines/gepa/docs/samples.py +696 -0
  26. lm_deluge/pipelines/gepa/examples/01_synthetic_keywords.py +140 -0
  27. lm_deluge/pipelines/gepa/examples/02_gsm8k_math.py +261 -0
  28. lm_deluge/pipelines/gepa/examples/03_hotpotqa_multihop.py +300 -0
  29. lm_deluge/pipelines/gepa/examples/04_batch_classification.py +271 -0
  30. lm_deluge/pipelines/gepa/examples/simple_qa.py +129 -0
  31. lm_deluge/pipelines/gepa/optimizer.py +435 -0
  32. lm_deluge/pipelines/gepa/proposer.py +235 -0
  33. lm_deluge/pipelines/gepa/util.py +165 -0
  34. lm_deluge/{llm_tools → pipelines}/score.py +2 -2
  35. lm_deluge/{llm_tools → pipelines}/translate.py +5 -3
  36. lm_deluge/prompt.py +224 -40
  37. lm_deluge/request_context.py +7 -2
  38. lm_deluge/tool/__init__.py +1118 -0
  39. lm_deluge/tool/builtin/anthropic/__init__.py +300 -0
  40. lm_deluge/tool/builtin/gemini.py +59 -0
  41. lm_deluge/tool/builtin/openai.py +74 -0
  42. lm_deluge/tool/cua/__init__.py +173 -0
  43. lm_deluge/tool/cua/actions.py +148 -0
  44. lm_deluge/tool/cua/base.py +27 -0
  45. lm_deluge/tool/cua/batch.py +215 -0
  46. lm_deluge/tool/cua/converters.py +466 -0
  47. lm_deluge/tool/cua/kernel.py +702 -0
  48. lm_deluge/tool/cua/trycua.py +989 -0
  49. lm_deluge/tool/prefab/__init__.py +45 -0
  50. lm_deluge/tool/prefab/batch_tool.py +156 -0
  51. lm_deluge/tool/prefab/docs.py +1119 -0
  52. lm_deluge/tool/prefab/email.py +294 -0
  53. lm_deluge/tool/prefab/filesystem.py +1711 -0
  54. lm_deluge/tool/prefab/full_text_search/__init__.py +285 -0
  55. lm_deluge/tool/prefab/full_text_search/tantivy_index.py +396 -0
  56. lm_deluge/tool/prefab/memory.py +458 -0
  57. lm_deluge/tool/prefab/otc/__init__.py +165 -0
  58. lm_deluge/tool/prefab/otc/executor.py +281 -0
  59. lm_deluge/tool/prefab/otc/parse.py +188 -0
  60. lm_deluge/tool/prefab/random.py +212 -0
  61. lm_deluge/tool/prefab/rlm/__init__.py +296 -0
  62. lm_deluge/tool/prefab/rlm/executor.py +349 -0
  63. lm_deluge/tool/prefab/rlm/parse.py +144 -0
  64. lm_deluge/tool/prefab/sandbox.py +1621 -0
  65. lm_deluge/tool/prefab/sheets.py +385 -0
  66. lm_deluge/tool/prefab/subagents.py +233 -0
  67. lm_deluge/tool/prefab/todos.py +342 -0
  68. lm_deluge/tool/prefab/tool_search.py +169 -0
  69. lm_deluge/tool/prefab/web_search.py +199 -0
  70. lm_deluge/tracker.py +16 -13
  71. lm_deluge/util/schema.py +412 -0
  72. lm_deluge/warnings.py +8 -0
  73. {lm_deluge-0.0.67.dist-info → lm_deluge-0.0.88.dist-info}/METADATA +22 -9
  74. lm_deluge-0.0.88.dist-info/RECORD +117 -0
  75. lm_deluge/built_in_tools/anthropic/__init__.py +0 -128
  76. lm_deluge/built_in_tools/openai.py +0 -28
  77. lm_deluge/presets/cerebras.py +0 -17
  78. lm_deluge/presets/meta.py +0 -13
  79. lm_deluge/tool.py +0 -849
  80. lm_deluge-0.0.67.dist-info/RECORD +0 -72
  81. lm_deluge/{llm_tools → pipelines}/__init__.py +1 -1
  82. /lm_deluge/{llm_tools → pipelines}/classify.py +0 -0
  83. /lm_deluge/{llm_tools → pipelines}/extract.py +0 -0
  84. /lm_deluge/{llm_tools → pipelines}/locate.py +0 -0
  85. /lm_deluge/{llm_tools → pipelines}/ocr.py +0 -0
  86. /lm_deluge/{built_in_tools → tool/builtin}/anthropic/bash.py +0 -0
  87. /lm_deluge/{built_in_tools → tool/builtin}/anthropic/computer_use.py +0 -0
  88. /lm_deluge/{built_in_tools → tool/builtin}/anthropic/editor.py +0 -0
  89. /lm_deluge/{built_in_tools → tool/builtin}/base.py +0 -0
  90. {lm_deluge-0.0.67.dist-info → lm_deluge-0.0.88.dist-info}/WHEEL +0 -0
  91. {lm_deluge-0.0.67.dist-info → lm_deluge-0.0.88.dist-info}/licenses/LICENSE +0 -0
  92. {lm_deluge-0.0.67.dist-info → lm_deluge-0.0.88.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,342 @@
1
+ # Adapted from https://github.com/sst/opencode - MIT License
2
+ # MIT License
3
+ # Copyright (c) 2025 opencode
4
+
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+ from __future__ import annotations
23
+
24
+ import json
25
+ import uuid
26
+ from typing import Any, Literal, Sequence
27
+
28
+ from pydantic import BaseModel, Field, field_validator
29
+
30
+ from .. import Tool
31
+
32
+ TODO_WRITE_DESCRIPTION = """Use this tool to create and manage a structured task list for your current coding session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
33
+ It also helps the user understand the progress of the task and overall progress of their requests.
34
+
35
+ ## When to Use This Tool
36
+ Use this tool proactively in these scenarios:
37
+
38
+ 1. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
39
+ 2. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
40
+ 3. User explicitly requests todo list - When the user directly asks you to use the todo list
41
+ 4. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
42
+ 5. After receiving new instructions - Immediately capture user requirements as todos. Feel free to edit the todo list based on new information.
43
+ 6. After completing a task - Mark it complete and add any new follow-up tasks
44
+ 7. When you start working on a new task, mark the todo as in_progress. Ideally you should only have one todo as in_progress at a time. Complete existing tasks before starting new ones.
45
+
46
+ ## When NOT to Use This Tool
47
+
48
+ Skip using this tool when:
49
+ 1. There is only a single, straightforward task
50
+ 2. The task is trivial and tracking it provides no organizational benefit
51
+ 3. The task can be completed in less than 3 trivial steps
52
+ 4. The task is purely conversational or informational
53
+
54
+ NOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.
55
+
56
+ ## Examples of When to Use the Todo List
57
+
58
+ <example>
59
+ User: I want to add a dark mode toggle to the application settings. Make sure you run the tests and build when you're done!
60
+ Assistant: I'll help add a dark mode toggle to your application settings. Let me create a todo list to track this implementation.
61
+ *Creates todo list with the following items:*
62
+ 1. Create dark mode toggle component in Settings page
63
+ 2. Add dark mode state management (context/store)
64
+ 3. Implement CSS-in-JS styles for dark theme
65
+ 4. Update existing components to support theme switching
66
+ 5. Run tests and build process, addressing any failures or errors that occur
67
+ *Begins working on the first task*
68
+
69
+ <reasoning>
70
+ The assistant used the todo list because:
71
+ 1. Adding dark mode is a multi-step feature requiring UI, state management, and styling changes
72
+ 2. The user explicitly requested tests and build be run afterward
73
+ 3. The assistant inferred that tests and build need to pass by adding "Ensure tests and build succeed" as the final task
74
+ </reasoning>
75
+ </example>
76
+
77
+ <example>
78
+ User: Help me rename the function getCwd to getCurrentWorkingDirectory across my project
79
+ Assistant: Let me first search through your codebase to find all occurrences of 'getCwd'.
80
+ *Uses grep or search tools to locate all instances of getCwd in the codebase*
81
+ Assistant: I've found 15 instances of 'getCwd' across 8 different files. Let me create a todo list to track these changes.
82
+ *Creates todo list with specific items for each file that needs updating*
83
+
84
+ <reasoning>
85
+ The assistant used the todo list because:
86
+ 1. First, the assistant searched to understand the scope of the task
87
+ 2. Upon finding multiple occurrences across different files, it determined this was a complex task with multiple steps
88
+ 3. The todo list helps ensure every instance is tracked and updated systematically
89
+ 4. This approach prevents missing any occurrences and maintains code consistency
90
+ </reasoning>
91
+ </example>
92
+
93
+
94
+ <example>
95
+ User: I need to implement these features for my e-commerce site: user registration, product catalog, shopping cart, and checkout flow.
96
+ Assistant: I'll help implement these features. First, let's add all the features to the todo list.
97
+ *Creates a todo list breaking down each feature into specific tasks based on the project architecture*
98
+ Assistant: Let's start with user registration. This will involve creating database models, API endpoints, and frontend forms that integrate with your existing authentication system.
99
+
100
+ <reasoning>
101
+ The assistant used the todo list because:
102
+ 1. The user provided multiple complex features to implement in a comma separated list
103
+ 2. The todo list helps organize these large features into manageable tasks
104
+ 3. This approach allows for tracking progress across the entire implementation
105
+ </reasoning>
106
+ </example>
107
+
108
+ <example>
109
+ User: Can you help optimize my React application? It's rendering slowly and has performance issues.</user>
110
+ Assistant: I'll help optimize your React application. First, let me examine your codebase to identify potential performance bottlenecks.
111
+ *Reviews component structure, render patterns, state management, and data fetching*
112
+ Assistant: After analyzing your codebase, I've identified several performance issues. Let me create a todo list to track our optimization efforts.
113
+ *Creates todo list with items like: 1) Implement memoization for expensive calculations in ProductList, 2) Add virtualization for long lists in Dashboard, 3) Optimize image loading in Gallery component, 4) Fix state update loops in ShoppingCart, 5) Review bundle size and implement code splitting*
114
+ Let's start by implementing memoization for the expensive calculations in your ProductList component.</assistant>
115
+
116
+ <reasoning>
117
+ The assistant used the todo list because:
118
+ 1. First, the assistant examined the codebase to identify specific performance issues
119
+ 2. Based on this analysis, it identified multiple optimization opportunities
120
+ 3. Performance optimization is a non-trivial task requiring multiple steps
121
+ 4. The todo list helps methodically track improvements across different components
122
+ 5. This systematic approach ensures all performance bottlenecks are addressed
123
+ </reasoning>
124
+ </example>
125
+
126
+ ## Examples of When NOT to Use the Todo List
127
+
128
+ <example>
129
+ User: How do I print 'Hello World' in Python?
130
+ Assistant: In Python, you can print "Hello World" with this simple code:
131
+
132
+ python
133
+ print("Hello World")
134
+
135
+ This will output the text "Hello World" to the console when executed.</assistant>
136
+
137
+ <reasoning>
138
+ The assistant did not use the todo list because this is a single, trivial task that can be completed in one step. There's no need to track multiple tasks or steps for such a straightforward request.
139
+ </reasoning>
140
+ </example>
141
+
142
+ <example>
143
+ User: What does the git status command do?
144
+ Assistant: The git status command shows the current state of your working directory and staging area. It displays which changes have been staged, which haven't, and which files aren't being tracked by Git. This is a fundamental command for understanding the current state of your repository before making commits.
145
+
146
+ <reasoning>
147
+ The assistant did not use the todo list because this is an informational request with no actual coding task to complete. The user is simply asking for an explanation, not for the assistant to perform multiple steps or tasks.
148
+ </reasoning>
149
+ </example>
150
+
151
+ <example>
152
+ User: Can you add a comment to the calculateTotal function to explain what it does?
153
+ Assistant: Sure, let me add a comment to the calculateTotal function to explain what it does.
154
+ * Uses the Edit tool to add a comment to the calculateTotal function *
155
+
156
+ <reasoning>
157
+ The assistant did not use the todo list because this is a single, straightforward task confined to one location in the code. Adding a comment doesn't require tracking multiple steps or systematic organization.
158
+ </reasoning>
159
+ </example>
160
+
161
+ <example>
162
+ User: Run npm install for me and tell me what happens.
163
+ Assistant: I'll run the npm install command for you.
164
+
165
+ *Executes: npm install*
166
+
167
+ The command completed successfully. Here's the output:
168
+ [Output of npm install command]
169
+
170
+ All dependencies have been installed according to your package.json file.
171
+
172
+ <reasoning>
173
+ The assistant did not use the todo list because this is a single command execution with immediate results. There are no multiple steps to track or organize, making the todo list unnecessary for this straightforward task.
174
+ </reasoning>
175
+ </example>
176
+
177
+ ## Task States and Management
178
+
179
+ 1. **Task States**: Use these states to track progress:
180
+ - pending: Task not yet started
181
+ - in_progress: Currently working on (limit to ONE task at a time)
182
+ - completed: Task finished successfully
183
+ - cancelled: Task no longer needed
184
+
185
+ 2. **Task Management**:
186
+ - Update task status in real-time as you work
187
+ - Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
188
+ - Only have ONE task in_progress at any time
189
+ - Complete current tasks before starting new ones
190
+ - Cancel tasks that become irrelevant
191
+
192
+ 3. **Task Breakdown**:
193
+ - Create specific, actionable items
194
+ - Break complex tasks into smaller, manageable steps
195
+ - Use clear, descriptive task names
196
+
197
+ When in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully.
198
+ """
199
+
200
+ TODO_READ_DESCRIPTION = "Use this tool to read your todo list"
201
+
202
+ TodoStatus = Literal["pending", "in_progress", "completed", "cancelled"]
203
+ TodoPriority = Literal["high", "medium", "low"]
204
+
205
+
206
+ class TodoItem(BaseModel):
207
+ """Structured representation of a single todo entry."""
208
+
209
+ content: str = Field(description="Brief description of the task")
210
+ status: TodoStatus = Field(
211
+ default="pending",
212
+ description="Current status of the task: pending, in_progress, completed, cancelled",
213
+ )
214
+ priority: TodoPriority = Field(
215
+ default="medium",
216
+ description="Priority level of the task: high, medium, low",
217
+ )
218
+ id: str = Field(
219
+ default_factory=lambda: uuid.uuid4().hex,
220
+ description="Unique identifier for the todo item",
221
+ )
222
+
223
+ @field_validator("status", "priority", mode="before")
224
+ @classmethod
225
+ def _normalize_lower(cls, value: Any) -> Any:
226
+ if isinstance(value, str):
227
+ return value.strip().lower()
228
+ return value
229
+
230
+ def is_active(self) -> bool:
231
+ return self.status not in {"completed", "cancelled"}
232
+
233
+
234
+ TodoLike = TodoItem | dict[str, Any]
235
+
236
+
237
+ class TodoManager:
238
+ """Stateful todo scratchpad that exposes read/write tools."""
239
+
240
+ def __init__(
241
+ self,
242
+ todos: Sequence[TodoLike] | None = None,
243
+ *,
244
+ write_tool_name: str = "todowrite",
245
+ read_tool_name: str = "todoread",
246
+ ):
247
+ self.write_tool_name = write_tool_name
248
+ self.read_tool_name = read_tool_name
249
+ self._todos: list[TodoItem] = []
250
+ self._tools: list[Tool] | None = None
251
+
252
+ if todos:
253
+ self._todos = [self._coerce(todo) for todo in todos]
254
+
255
+ def _coerce(self, todo: TodoLike) -> TodoItem:
256
+ if isinstance(todo, TodoItem):
257
+ return todo
258
+ if isinstance(todo, dict):
259
+ return TodoItem(**todo)
260
+ raise TypeError("Todos must be TodoItem instances or dictionaries")
261
+
262
+ def _serialize(self) -> list[dict[str, Any]]:
263
+ return [todo.model_dump() for todo in self._todos]
264
+
265
+ def _pending_count(self) -> int:
266
+ return sum(1 for todo in self._todos if todo.is_active())
267
+
268
+ def _format_output(self) -> str:
269
+ payload = {
270
+ "title": f"{self._pending_count()} todos",
271
+ "todos": self._serialize(),
272
+ }
273
+ return json.dumps(payload, indent=2)
274
+
275
+ def _write_tool(self, todos: list[dict[str, Any]]) -> str:
276
+ self._todos = [self._coerce(todo) for todo in todos]
277
+ return self._format_output()
278
+
279
+ def _read_tool(self) -> str:
280
+ return self._format_output()
281
+
282
+ def get_todos(self) -> list[TodoItem]:
283
+ """Return a copy of the current todo list."""
284
+ return list(self._todos)
285
+
286
+ def get_tools(self) -> list[Tool]:
287
+ """Return Tool instances bound to this manager's state."""
288
+ if self._tools is not None:
289
+ return self._tools
290
+
291
+ todo_definition = {
292
+ "type": "object",
293
+ "properties": {
294
+ "content": {
295
+ "type": "string",
296
+ "description": "Brief description of the task",
297
+ },
298
+ "status": {
299
+ "type": "string",
300
+ "description": "Current status of the task: pending, in_progress, completed, cancelled",
301
+ "enum": ["pending", "in_progress", "completed", "cancelled"],
302
+ },
303
+ "priority": {
304
+ "type": "string",
305
+ "description": "Priority level of the task: high, medium, low",
306
+ "enum": ["high", "medium", "low"],
307
+ },
308
+ "id": {
309
+ "type": "string",
310
+ "description": "Unique identifier for the todo item",
311
+ },
312
+ },
313
+ "required": ["content", "status", "priority", "id"],
314
+ }
315
+
316
+ write_tool = Tool(
317
+ name=self.write_tool_name,
318
+ description=TODO_WRITE_DESCRIPTION,
319
+ parameters={
320
+ "todos": {
321
+ "type": "array",
322
+ "description": "The updated todo list",
323
+ "items": {"$ref": "#/$defs/Todo"},
324
+ }
325
+ },
326
+ required=["todos"],
327
+ definitions={"Todo": todo_definition},
328
+ run=self._write_tool,
329
+ )
330
+
331
+ read_tool = Tool(
332
+ name=self.read_tool_name,
333
+ description=TODO_READ_DESCRIPTION,
334
+ parameters={},
335
+ run=self._read_tool,
336
+ )
337
+
338
+ self._tools = [write_tool, read_tool]
339
+ return self._tools
340
+
341
+
342
+ __all__ = ["TodoManager", "TodoItem", "TodoStatus", "TodoPriority"]
@@ -0,0 +1,169 @@
1
+ """Tool search utility that exposes search + call helpers to the model."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import re
7
+ from typing import Any
8
+
9
+ from .. import Tool
10
+
11
+
12
+ class ToolSearchTool:
13
+ """Allow a model to discover and invoke tools by searching name/description."""
14
+
15
+ def __init__(
16
+ self,
17
+ tools: list[Tool],
18
+ *,
19
+ base_name: str = "tool_search_tool",
20
+ search_tool_name: str | None = None,
21
+ call_tool_name: str | None = None,
22
+ max_results_default: int = 10,
23
+ ):
24
+ self.tools = tools
25
+ self.base_name = base_name
26
+ self.search_tool_name = search_tool_name or f"{base_name}_search"
27
+ self.call_tool_name = call_tool_name or f"{base_name}_call"
28
+ self.max_results_default = max_results_default
29
+ self._registry = self._build_registry(tools)
30
+
31
+ def _build_registry(self, tools: list[Tool]) -> dict[str, dict[str, Any]]:
32
+ """Assign stable IDs to tools and store searchable metadata."""
33
+ registry: dict[str, dict[str, Any]] = {}
34
+ seen_counts: dict[str, int] = {}
35
+
36
+ for index, tool in enumerate(tools):
37
+ suffix = seen_counts.get(tool.name, 0)
38
+ seen_counts[tool.name] = suffix + 1
39
+ tool_id = tool.name if suffix == 0 else f"{tool.name}_{suffix}"
40
+
41
+ registry[tool_id] = {
42
+ "id": tool_id,
43
+ "tool": tool,
44
+ "name": tool.name,
45
+ "description": tool.description or "",
46
+ "parameters": tool.parameters or {},
47
+ "required": tool.required or [],
48
+ }
49
+
50
+ return registry
51
+
52
+ def _tool_signature(self, entry: dict[str, Any]) -> str:
53
+ params = []
54
+ for name, schema in entry["parameters"].items():
55
+ json_type = schema.get("type", "any")
56
+ params.append(f"{name}: {json_type}")
57
+ signature = (
58
+ f"{entry['name']}({', '.join(params)})" if params else f"{entry['name']}()"
59
+ )
60
+ return f"{signature} [{entry['id']}]"
61
+
62
+ def _search_description(self) -> str:
63
+ lines = [
64
+ "Find tools by regex against their name or description.",
65
+ "Returns matched tool ids plus argument schemas.",
66
+ ]
67
+ return " ".join(lines)
68
+
69
+ def _call_description(self) -> str:
70
+ lines = [
71
+ "Call any tool returned by the search helper.",
72
+ "Supply a tool id and its arguments inside the `arguments` (or `args`) object; do not place tool params at the top level.",
73
+ ]
74
+ return " ".join(lines)
75
+
76
+ async def _search(self, pattern: str, max_results: int | None = None) -> str:
77
+ """Search tools by regex and return their metadata."""
78
+ try:
79
+ compiled = re.compile(pattern, re.IGNORECASE)
80
+ except re.error as exc:
81
+ return json.dumps({"error": f"Invalid regex: {exc}"})
82
+
83
+ limit = max_results or self.max_results_default
84
+ matches: list[dict[str, Any]] = []
85
+ for entry in self._registry.values():
86
+ if compiled.search(entry["name"]) or compiled.search(entry["description"]):
87
+ matches.append(
88
+ {
89
+ "id": entry["id"],
90
+ "name": entry["name"],
91
+ "description": entry["description"],
92
+ "parameters": entry["parameters"],
93
+ "required": entry["required"],
94
+ "signature": self._tool_signature(entry),
95
+ }
96
+ )
97
+ if len(matches) >= limit:
98
+ break
99
+
100
+ return json.dumps(matches)
101
+
102
+ async def _call(
103
+ self,
104
+ tool_id: str,
105
+ arguments: dict[str, Any] | None = None,
106
+ args: dict[str, Any] | None = None,
107
+ ) -> str:
108
+ """Invoke a matched tool by id."""
109
+ entry = self._registry.get(tool_id)
110
+ if entry is None:
111
+ return json.dumps({"error": f"Unknown tool id '{tool_id}'"})
112
+
113
+ tool = entry["tool"]
114
+ merged_args = arguments if arguments is not None else args
115
+ if merged_args is None:
116
+ merged_args = {}
117
+ try:
118
+ output = await tool.acall(**merged_args)
119
+ return json.dumps({"tool": tool.name, "tool_id": tool_id, "result": output})
120
+ except Exception as exc: # pragma: no cover - defensive
121
+ return json.dumps(
122
+ {
123
+ "tool": tool.name,
124
+ "tool_id": tool_id,
125
+ "error": f"{type(exc).__name__}: {exc}",
126
+ }
127
+ )
128
+
129
+ def get_tools(self) -> list[Tool]:
130
+ """Return search + call tools for injection into an agent loop."""
131
+ search_tool = Tool(
132
+ name=self.search_tool_name,
133
+ description=self._search_description(),
134
+ run=self._search,
135
+ parameters={
136
+ "pattern": {
137
+ "type": "string",
138
+ "description": "Regex to match against tool names and descriptions",
139
+ },
140
+ "max_results": {
141
+ "type": "integer",
142
+ "description": "Optional limit on number of matches to return",
143
+ },
144
+ },
145
+ required=["pattern"],
146
+ )
147
+
148
+ call_tool = Tool(
149
+ name=self.call_tool_name,
150
+ description=self._call_description(),
151
+ run=self._call,
152
+ parameters={
153
+ "tool_id": {
154
+ "type": "string",
155
+ "description": "Tool id returned by the search helper",
156
+ },
157
+ "arguments": {
158
+ "type": "object",
159
+ "description": "Arguments to pass to the matched tool. Put all parameters inside this object (preferred).",
160
+ },
161
+ "args": {
162
+ "type": "object",
163
+ "description": "Alias for 'arguments' if you prefer a shorter key. Do not pass tool args at the top level.",
164
+ },
165
+ },
166
+ required=["tool_id"],
167
+ )
168
+
169
+ return [search_tool, call_tool]