|
21 | 21 |
|
22 | 22 | from .improved_compaction import ImprovedSessionCompaction, CompactionConfig |
23 | 23 | from .llm_utils import call_llm, LLMCaller |
| 24 | +from .tools_v2 import ToolRegistry, ToolResult |
24 | 25 |
|
25 | 26 | logger = logging.getLogger(__name__) |
26 | 27 |
|
@@ -154,88 +155,6 @@ def _match_pattern(self, tool_name: str, pattern: str) -> bool: |
154 | 155 | return fnmatch.fnmatch(tool_name, pattern) |
155 | 156 |
|
156 | 157 |
|
157 | | -class ToolRegistry: |
158 | | - """工具注册表""" |
159 | | - |
160 | | - def __init__(self): |
161 | | - self._tools: Dict[str, Any] = {} |
162 | | - |
163 | | - def register(self, tool: Any) -> "ToolRegistry": |
164 | | - # 优先使用 metadata.name,其次使用 name 属性 |
165 | | - if hasattr(tool, "metadata") and hasattr(tool.metadata, "name"): |
166 | | - name = tool.metadata.name |
167 | | - elif hasattr(tool, "name"): |
168 | | - name = tool.name |
169 | | - else: |
170 | | - name = str(tool) |
171 | | - self._tools[name] = tool |
172 | | - logger.debug(f"[ToolRegistry] 注册工具: {name}") |
173 | | - return self |
174 | | - |
175 | | - def get(self, name: str) -> Optional[Any]: |
176 | | - return self._tools.get(name) |
177 | | - |
178 | | - async def execute( |
179 | | - self, name: str, args: Dict[str, Any], context: Optional[Dict[str, Any]] = None |
180 | | - ) -> "ToolResult": |
181 | | - """执行工具""" |
182 | | - from .tools_v2 import ToolResult |
183 | | - |
184 | | - tool = self._tools.get(name) |
185 | | - if not tool: |
186 | | - return ToolResult(success=False, output="", error=f"工具不存在: {name}") |
187 | | - |
188 | | - try: |
189 | | - if hasattr(tool, "execute"): |
190 | | - result = await tool.execute(args, context) |
191 | | - return result |
192 | | - elif callable(tool): |
193 | | - result = tool(**args) |
194 | | - if hasattr(result, "__await__"): |
195 | | - result = await result |
196 | | - if isinstance(result, ToolResult): |
197 | | - return result |
198 | | - return ToolResult( |
199 | | - success=True, |
200 | | - output=str(result) if result else "", |
201 | | - ) |
202 | | - else: |
203 | | - return ToolResult( |
204 | | - success=False, output="", error=f"工具不可执行: {name}" |
205 | | - ) |
206 | | - except Exception as e: |
207 | | - logger.exception(f"[ToolRegistry] 工具执行异常: {name}") |
208 | | - return ToolResult(success=False, output="", error=str(e)) |
209 | | - |
210 | | - def list_tools(self) -> List[str]: |
211 | | - return list(self._tools.keys()) |
212 | | - |
213 | | - def list_all(self) -> List[Any]: |
214 | | - """列出所有工具对象""" |
215 | | - return list(self._tools.values()) |
216 | | - |
217 | | - def list_names(self) -> List[str]: |
218 | | - """列出所有工具名称""" |
219 | | - return list(self._tools.keys()) |
220 | | - |
221 | | - def get_openai_tools(self) -> List[Dict[str, Any]]: |
222 | | - result = [] |
223 | | - for name, tool in self._tools.items(): |
224 | | - if hasattr(tool, "get_openai_spec"): |
225 | | - result.append(tool.get_openai_spec()) |
226 | | - else: |
227 | | - result.append( |
228 | | - { |
229 | | - "type": "function", |
230 | | - "function": { |
231 | | - "name": name, |
232 | | - "description": getattr(tool, "description", ""), |
233 | | - }, |
234 | | - } |
235 | | - ) |
236 | | - return result |
237 | | - |
238 | | - |
239 | 158 | @dataclass |
240 | 159 | class SubagentSession: |
241 | 160 | """子代理会话""" |
@@ -1119,8 +1038,9 @@ def _build_llm_messages(self) -> List: |
1119 | 1038 | else: |
1120 | 1039 | messages.append(SystemMessage(content=msg.content)) |
1121 | 1040 |
|
1122 | | - if self.tools.list_tools(): |
1123 | | - tools_desc = "Available tools: " + ", ".join(self.tools.list_tools()) |
| 1041 | + if self.tools.list_all(): |
| 1042 | + tool_names = [t.metadata.name for t in self.tools.list_all()] |
| 1043 | + tools_desc = "Available tools: " + ", ".join(tool_names) |
1124 | 1044 | messages.append(SystemMessage(content=tools_desc)) |
1125 | 1045 |
|
1126 | 1046 | return messages |
|
0 commit comments