UNPKG

n8n

Version:

n8n Workflow Automation Tool

126 lines 6.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createN8nDelegateSubAgentTool = createN8nDelegateSubAgentTool; exports.formatSubAgentToolOutput = formatSubAgentToolOutput; const agents_1 = require("@n8n/agents"); const n8n_workflow_1 = require("n8n-workflow"); const response_error_1 = require("../../../errors/response-errors/abstract/response.error"); function createN8nDelegateSubAgentTool(options) { const { runner, sourcesById, availableSubAgents, policy, inlineSubAgentModelsByDifficulty, resolveInlineSubAgentProviderTools, ...runContext } = options; return (0, agents_1.createDelegateSubAgentTool)({ ...(availableSubAgents !== undefined ? { availableSubAgents } : {}), ...(policy !== undefined ? { policy } : {}), ...(inlineSubAgentModelsByDifficulty !== undefined ? { inlineSubAgentModelsByDifficulty } : {}), ...(resolveInlineSubAgentProviderTools !== undefined ? { resolveInlineSubAgentProviderTools } : {}), shouldRetrySubAgentResumeError, runSubAgent: async (request, helpers) => { if (request.subAgentId === agents_1.INLINE_SUB_AGENT_ID) { return await helpers.runInlineSubAgent(request); } const selectedSource = selectSubAgentSource({ sourcesById, subAgentId: request.subAgentId, }); if (!selectedSource) { return { status: 'failed', taskPath: request.taskPath, answer: '', error: `No configured subagent matched "${request.subAgentId}". Use "inline" for an inline sub-agent, or pass one of the configured subagent IDs.`, }; } const result = await runner.runForeground({ goal: request.goal, source: selectedSource, executionMode: 'foreground', ...(request.context !== undefined ? { context: request.context } : {}), ...(request.expectedOutput !== undefined ? { expectedOutput: request.expectedOutput } : {}), ...(policy !== undefined ? { policy } : {}), ...(request.parentThreadId !== undefined ? { parentThreadId: request.parentThreadId } : {}), ...(request.parentResourceId !== undefined ? { parentResourceId: request.parentResourceId } : {}), taskPath: request.taskPath, }, { ...runContext, ...(request.parentExecutionCounter !== undefined ? { executionCounter: request.parentExecutionCounter } : {}), ...(request.parentAbortSignal !== undefined ? { abortSignal: request.parentAbortSignal } : {}), ...(request.parentTelemetry !== undefined ? { telemetry: request.parentTelemetry } : {}), onChunk: helpers.emitChunk, }); return formatSubAgentToolOutput(result); }, resumeSubAgent: async (request, helpers) => { if (request.subAgentId === agents_1.INLINE_SUB_AGENT_ID) { return { status: 'failed', taskPath: request.taskPath, answer: '', error: 'Inline sub-agent resumes must be handled by the parent Agent runtime.', }; } if (request.childThreadId === undefined || request.resumeContext === undefined) { return { status: 'failed', taskPath: request.taskPath, answer: '', error: 'Configured sub-agent checkpoint metadata is missing or invalid.', }; } const result = await runner.resumeForeground(request, { ...runContext, ...(request.parentExecutionCounter !== undefined ? { executionCounter: request.parentExecutionCounter } : {}), ...(request.parentAbortSignal !== undefined ? { abortSignal: request.parentAbortSignal } : {}), ...(request.parentTelemetry !== undefined ? { telemetry: request.parentTelemetry } : {}), onChunk: helpers.emitChunk, }); return formatSubAgentToolOutput(result); }, cancelSubAgent: async (request) => { if (request.subAgentId === agents_1.INLINE_SUB_AGENT_ID) { throw new Error('Inline sub-agent cancellation must be handled by the parent Agent runtime.'); } if (request.resumeContext === undefined) { throw new Error('Configured sub-agent checkpoint metadata is missing or invalid.'); } await runner.cancelForeground(request); }, }); } function shouldRetrySubAgentResumeError(error) { if (error instanceof n8n_workflow_1.OperationalError) return true; if (!(error instanceof response_error_1.ResponseError)) return false; return [408, 425, 429, 502, 503, 504].includes(error.httpStatusCode); } function selectSubAgentSource(options) { const { sourcesById, subAgentId } = options; if (subAgentId === agents_1.INLINE_SUB_AGENT_ID) return undefined; return sourcesById?.[subAgentId]; } function formatSubAgentToolOutput(result) { const output = (0, agents_1.generateResultToDelegateSubAgentOutput)(result.taskPath, result.result, result.threadId); return { ...output, ...(output.status === 'suspended' && result.resumeContext !== undefined ? { resumeContext: result.resumeContext } : {}), }; } //# sourceMappingURL=delegate-sub-agent-tool.js.map