( tools: ToolDefinition[], getContext: () => ServerToolContext, options?: AdaptToolsOptions )
| 72 | } |
| 73 | |
| 74 | export function adaptToolsForAgent( |
| 75 | tools: ToolDefinition[], |
| 76 | getContext: () => ServerToolContext, |
| 77 | options?: AdaptToolsOptions |
| 78 | ): AgentTool<any, any>[] { |
| 79 | const tokenBudget = options?.maxToolResultTokens ?? DEFAULT_MAX_TOOL_RESULT_TOKENS |
| 80 | const chartSchemaGateState = createChartSchemaGateState() |
| 81 | |
| 82 | return tools.map((tool) => |
| 83 | wrapWithChartSchemaGate( |
| 84 | { |
| 85 | name: tool.name, |
| 86 | label: tool.name, |
| 87 | description: tool.description, |
| 88 | parameters: convertJsonSchemaToParameters(tool.inputSchema) as any, |
| 89 | async execute(_toolCallId: string, params: unknown): Promise<AgentToolResult<unknown>> { |
| 90 | const toolParams = (params && typeof params === 'object' ? params : {}) as Record<string, unknown> |
| 91 | const ctx = getContext() |
| 92 | const execCtx: ToolExecutionContext = { |
| 93 | db: ctx.db, |
| 94 | dataProvider: new CoreDataProvider(ctx.db), |
| 95 | sessionId: ctx.sessionId, |
| 96 | locale: ctx.locale, |
| 97 | semanticIndexService: ctx.semanticIndexService, |
| 98 | preprocessConfig: ctx.preprocessConfig, |
| 99 | ownerPlatformId: ctx.ownerPlatformId, |
| 100 | timeFilter: ctx.timeFilter, |
| 101 | maxMessagesLimit: ctx.maxMessagesLimit, |
| 102 | maxToolResultTokens: tokenBudget, |
| 103 | segmentText: (texts, locale, options) => batchSegmentWithFrequency(texts, locale as any, options as any), |
| 104 | desensitizeMessages: (messages: RawMessage[]): RawMessage[] => |
| 105 | preprocessMessages( |
| 106 | messages as PreprocessableMessage[], |
| 107 | ctx.preprocessConfig as PreprocessConfig | undefined |
| 108 | ) as RawMessage[], |
| 109 | } |
| 110 | try { |
| 111 | const result = await tool.handler(toolParams, execCtx) |
| 112 | const chartDetails = |
| 113 | result.chart || result.charts |
| 114 | ? { |
| 115 | ...(result.chart ? { chart: result.chart } : {}), |
| 116 | ...(result.charts ? { charts: result.charts } : {}), |
| 117 | } |
| 118 | : {} |
| 119 | |
| 120 | if (result.rawMessages && result.rawMessages.length > 0) { |
| 121 | // tools may mirror rawMessages inside data; keep it out of extraDetails |
| 122 | // so the pipeline only renders scalar metadata (same as the desktop adapter) |
| 123 | const { rawMessages: _rawInData, ...extraDetails } = (result.data ?? {}) as Record<string, unknown> |
| 124 | const preprocessCfg = ctx.preprocessConfig as PreprocessConfig | undefined |
| 125 | const pipelineResult = applyPreprocessingPipeline({ |
| 126 | rawMessages: result.rawMessages as PreprocessableMessage[], |
| 127 | preprocessConfig: preprocessCfg, |
| 128 | anonymizeNames: preprocessCfg?.anonymizeNames ?? false, |
| 129 | ownerPlatformId: ctx.ownerPlatformId, |
| 130 | locale: ctx.locale, |
| 131 | maxToolResultTokens: tokenBudget, |
no test coverage detected