import type { AgentTool } from '@zhin.js/ai'; import type { ZhinAgentPrivate } from '../internal/agent-host.js'; import type { ToolFilter, ToolSource, ToolSystemConfig } from './contracts.js'; import { BuiltinToolSource, DedupeToolFilter, ExternalToolSource, McpToolSource, RegisteredToolSource, SkillToolSource, type CollectToolsContext, } from './sources.js'; import { resolveAgentToolsForTurn, type ResolvedToolsForTurn, } from './deferred-resolution.js'; export type { CollectToolsContext } from './sources.js'; export type { ResolvedToolsForTurn } from './deferred-resolution.js'; export type CollectToolsForTurnInput = CollectToolsContext; /** 每 turn 独立 Source 列表,不 mutate 共享 ToolSystem 实例。 */ export function createDefaultToolSources(context: CollectToolsContext): ToolSource[] { return [ new ExternalToolSource(context.externalTools), new SkillToolSource(context.skillRegistry), new RegisteredToolSource(context.externalRegistered), new BuiltinToolSource(), new McpToolSource(), ]; } export function createToolSystem(config: ToolSystemConfig = {}): ToolSystem { return new ToolSystem(config); } export class ToolSystem { private readonly customSources: ToolSource[] = []; private readonly filters: ToolFilter[] = []; constructor(_config: ToolSystemConfig = {}) { this.addFilter(new DedupeToolFilter()); } addSource(source: ToolSource): void { this.customSources.push(source); this.customSources.sort((a, b) => b.priority - a.priority); } addFilter(filter: ToolFilter): void { this.filters.push(filter); } async collectTools(context: CollectToolsContext, sources?: ToolSource[]): Promise { const activeSources = sources ?? [ ...createDefaultToolSources(context), ...this.customSources, ]; let tools: AgentTool[] = []; for (const source of activeSources) { tools.push(...(await source.collectTools(context))); } for (const filter of this.filters) { tools = filter.filter(tools, { message: context.message }); } return tools; } async collectForTurn(input: CollectToolsForTurnInput): Promise { return this.collectTools(input, createDefaultToolSources(input)); } async resolveForTurn( host: ZhinAgentPrivate, allTools: AgentTool[], sessionId: string, platform?: string, ): Promise { return resolveAgentToolsForTurn(host, allTools, sessionId, platform); } } /** * @deprecated Tool pre-execution was removed (#1250 第 56 批); kept for one release with no effect and * removed in the next minor. `preExecution` is always empty and nothing runs before the model. */ export interface ToolRunPlan { preExecTools: AgentTool[]; preExecution: { tools: AgentTool[]; data: string }; hasNonPreExecTools: boolean; mode: 'chat' | 'pre-exec-fast-path' | 'agent'; } /** * @deprecated No effect: tool pre-execution was removed (#1250 第 56 批); removed in the next minor. * Executes nothing and returns the plan of a turn without pre-executable tools. */ export async function planToolRun(tools: AgentTool[], _timeoutMs?: number): Promise { return { preExecTools: [], preExecution: { tools: [], data: '' }, hasNonPreExecTools: tools.length > 0, mode: tools.length === 0 ? 'chat' : 'agent', }; } /** Stamps the owning generation so ToolRuntime can reject retired-generation tools. */ export function stampToolGeneration( tools: T[], generation: number, ): T[] { for (const tool of tools) tool.generation = generation; return tools; } /** 无 per-turn 可变 state;并发 collectForTurn 安全。 */ export const defaultToolSystem = createToolSystem();