( params: AiToolExecuteRequest, deps: AiToolExecutionDeps )
| 43 | |
| 44 | /** 在注入的上下文上执行注册表工具,处理取消、超大结果截断与错误映射。 */ |
| 45 | export async function executeRegistryTool( |
| 46 | params: AiToolExecuteRequest, |
| 47 | deps: AiToolExecutionDeps |
| 48 | ): Promise<AiToolExecuteResult> { |
| 49 | const { toolName, params: toolParams, abortSignal } = params |
| 50 | const entry = AGENT_TOOL_REGISTRY.find((tool) => tool.name === toolName) |
| 51 | if (!entry) { |
| 52 | return { success: false, error: `Tool not found: ${toolName}` } |
| 53 | } |
| 54 | |
| 55 | try { |
| 56 | assertNotAborted(abortSignal) |
| 57 | const execCtx = buildToolExecutionContext(params, deps) |
| 58 | |
| 59 | const startTime = Date.now() |
| 60 | const result = await entry.handler(toolParams, execCtx) |
| 61 | const elapsed = Date.now() - startTime |
| 62 | assertNotAborted(abortSignal) |
| 63 | |
| 64 | let details = (result.data as Record<string, unknown> | undefined) ?? undefined |
| 65 | let truncated = false |
| 66 | |
| 67 | if (details) { |
| 68 | stripAvatarFields(details) |
| 69 | const raw = JSON.stringify(details) |
| 70 | if (raw.length > MAX_RESULT_CHARS) { |
| 71 | truncated = true |
| 72 | details = { _truncated: true, _originalSize: raw.length, _preview: raw.slice(0, MAX_RESULT_CHARS) } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return { |
| 77 | success: true, |
| 78 | elapsed, |
| 79 | content: [{ type: 'text', text: result.content }], |
| 80 | details, |
| 81 | truncated, |
| 82 | } |
| 83 | } catch (error) { |
| 84 | if (abortSignal.aborted) { |
| 85 | return { success: false, error: 'cancelled' } |
| 86 | } |
| 87 | return { success: false, error: error instanceof Error ? error.message : String(error) } |
| 88 | } |
| 89 | } |
no test coverage detected