@n8n/instance-ai 1.10.2 → 1.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +128 -0
- package/dist/agent/instance-agent.js +8 -0
- package/dist/agent/instance-agent.js.map +1 -1
- package/dist/agent/shared-prompts.d.ts +1 -0
- package/dist/agent/shared-prompts.js +25 -0
- package/dist/agent/shared-prompts.js.map +1 -1
- package/dist/agent/sub-agent-debriefing.d.ts +8 -8
- package/dist/agent/system-prompt.d.ts +2 -0
- package/dist/agent/system-prompt.js +52 -93
- package/dist/agent/system-prompt.js.map +1 -1
- package/dist/build.tsbuildinfo +1 -1
- package/dist/constants/max-steps.d.ts +0 -1
- package/dist/constants/max-steps.js +0 -1
- package/dist/constants/max-steps.js.map +1 -1
- package/dist/index.d.ts +123 -33
- package/dist/index.js +103 -96
- package/dist/index.js.map +1 -1
- package/dist/knowledge-base/build-templates-index.d.ts +12 -0
- package/dist/knowledge-base/build-templates-index.js +113 -0
- package/dist/knowledge-base/build-templates-index.js.map +1 -0
- package/dist/knowledge-base/extract-builder-templates-archive.d.ts +2 -0
- package/dist/knowledge-base/extract-builder-templates-archive.js +89 -0
- package/dist/knowledge-base/extract-builder-templates-archive.js.map +1 -0
- package/dist/knowledge-base/materialize-knowledge-base.d.ts +67 -0
- package/dist/knowledge-base/materialize-knowledge-base.js +186 -0
- package/dist/knowledge-base/materialize-knowledge-base.js.map +1 -0
- package/dist/mcp/mcp-client-manager.js +6 -1
- package/dist/mcp/mcp-client-manager.js.map +1 -1
- package/dist/parsers/pdf-parser.js +4 -0
- package/dist/parsers/pdf-parser.js.map +1 -1
- package/dist/planned-tasks/planned-task-service.d.ts +5 -1
- package/dist/planned-tasks/planned-task-service.js +29 -1
- package/dist/planned-tasks/planned-task-service.js.map +1 -1
- package/dist/runtime/run-state-registry.d.ts +8 -0
- package/dist/runtime/run-state-registry.js.map +1 -1
- package/dist/runtime/terminal-response-guard.d.ts +2 -1
- package/dist/runtime/terminal-response-guard.js +20 -0
- package/dist/runtime/terminal-response-guard.js.map +1 -1
- package/dist/skills/materialize-runtime-skills.d.ts +1 -7
- package/dist/skills/materialize-runtime-skills.js +96 -176
- package/dist/skills/materialize-runtime-skills.js.map +1 -1
- package/dist/storage/index.d.ts +1 -1
- package/dist/storage/planned-task-storage.js +1 -0
- package/dist/storage/planned-task-storage.js.map +1 -1
- package/dist/storage/terminal-outcome-storage.d.ts +2 -2
- package/dist/storage/workflow-loop-storage.d.ts +199 -70
- package/dist/storage/workflow-loop-storage.js +107 -0
- package/dist/storage/workflow-loop-storage.js.map +1 -1
- package/dist/stream/work-summary-accumulator.d.ts +6 -6
- package/dist/tools/attachments/parse-file.tool.d.ts +18 -18
- package/dist/tools/credentials.tool.d.ts +2 -5
- package/dist/tools/credentials.tool.js +2 -6
- package/dist/tools/credentials.tool.js.map +1 -1
- package/dist/tools/executions.tool.js +53 -7
- package/dist/tools/executions.tool.js.map +1 -1
- package/dist/tools/index.js +3 -9
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/nodes/node-search-engine.d.ts +2 -0
- package/dist/tools/nodes/node-search-engine.js +66 -7
- package/dist/tools/nodes/node-search-engine.js.map +1 -1
- package/dist/tools/nodes.tool.js +10 -3
- package/dist/tools/nodes.tool.js.map +1 -1
- package/dist/tools/orchestration/complete-checkpoint.tool.js +69 -0
- package/dist/tools/orchestration/complete-checkpoint.tool.js.map +1 -1
- package/dist/tools/orchestration/delegate.schemas.d.ts +5 -5
- package/dist/tools/orchestration/delegate.tool.js +1 -1
- package/dist/tools/orchestration/delegate.tool.js.map +1 -1
- package/dist/tools/orchestration/plan.tool.js +96 -60
- package/dist/tools/orchestration/plan.tool.js.map +1 -1
- package/dist/tools/orchestration/report-verification-verdict.tool.d.ts +9 -6
- package/dist/tools/orchestration/report-verification-verdict.tool.js +6 -1
- package/dist/tools/orchestration/report-verification-verdict.tool.js.map +1 -1
- package/dist/tools/orchestration/verify-built-workflow.tool.js +3 -2
- package/dist/tools/orchestration/verify-built-workflow.tool.js.map +1 -1
- package/dist/tools/tool-ids.d.ts +0 -5
- package/dist/tools/tool-ids.js +3 -7
- package/dist/tools/tool-ids.js.map +1 -1
- package/dist/tools/workflows/build-workflow.tool.d.ts +8 -2
- package/dist/tools/workflows/build-workflow.tool.js +371 -17
- package/dist/tools/workflows/build-workflow.tool.js.map +1 -1
- package/dist/tools/workflows/materialize-node-type.tool.d.ts +4 -4
- package/dist/tools/workflows/materialize-node-type.tool.js +2 -2
- package/dist/tools/workflows/materialize-node-type.tool.js.map +1 -1
- package/dist/tools/workflows/setup-workflow.schema.d.ts +32 -32
- package/dist/tools/workflows/submit-workflow.tool.d.ts +16 -12
- package/dist/tools/workflows/submit-workflow.tool.js +9 -2
- package/dist/tools/workflows/submit-workflow.tool.js.map +1 -1
- package/dist/tools/workflows/write-sandbox-file.tool.js +2 -2
- package/dist/tools/workflows/write-sandbox-file.tool.js.map +1 -1
- package/dist/tools/workflows.tool.js +5 -0
- package/dist/tools/workflows.tool.js.map +1 -1
- package/dist/tracing/langsmith-tracing.js +6 -1
- package/dist/tracing/langsmith-tracing.js.map +1 -1
- package/dist/tracing/trace-payloads.js +2 -6
- package/dist/tracing/trace-payloads.js.map +1 -1
- package/dist/types.d.ts +48 -9
- package/dist/types.js.map +1 -1
- package/dist/workflow-builder/build-failure-tracker.d.ts +10 -0
- package/dist/workflow-builder/build-failure-tracker.js +36 -0
- package/dist/workflow-builder/build-failure-tracker.js.map +1 -0
- package/dist/workflow-builder/index.d.ts +1 -0
- package/dist/workflow-builder/index.js +4 -1
- package/dist/workflow-builder/index.js.map +1 -1
- package/dist/workflow-loop/guidance.js +10 -6
- package/dist/workflow-loop/guidance.js.map +1 -1
- package/dist/workflow-loop/index.d.ts +3 -2
- package/dist/workflow-loop/index.js +14 -1
- package/dist/workflow-loop/index.js.map +1 -1
- package/dist/workflow-loop/runtime.js +3 -1
- package/dist/workflow-loop/runtime.js.map +1 -1
- package/dist/workflow-loop/verification-obligation.d.ts +15 -0
- package/dist/workflow-loop/verification-obligation.js +140 -0
- package/dist/workflow-loop/verification-obligation.js.map +1 -0
- package/dist/workflow-loop/workflow-loop-controller.js +26 -1
- package/dist/workflow-loop/workflow-loop-controller.js.map +1 -1
- package/dist/workflow-loop/workflow-loop-state.d.ts +416 -62
- package/dist/workflow-loop/workflow-loop-state.js +66 -1
- package/dist/workflow-loop/workflow-loop-state.js.map +1 -1
- package/dist/workspace/builder-templates-service.js.map +1 -1
- package/dist/workspace/compute-workspace-content-hash.d.ts +1 -0
- package/dist/workspace/compute-workspace-content-hash.js +15 -0
- package/dist/workspace/compute-workspace-content-hash.js.map +1 -0
- package/dist/workspace/create-workspace.d.ts +9 -43
- package/dist/workspace/create-workspace.js +45 -55
- package/dist/workspace/create-workspace.js.map +1 -1
- package/dist/workspace/lazy-runtime-workspace.js +1 -1
- package/dist/workspace/lazy-runtime-workspace.js.map +1 -1
- package/dist/workspace/prebaked-workspace-bundle.d.ts +39 -0
- package/dist/workspace/prebaked-workspace-bundle.js +75 -0
- package/dist/workspace/prebaked-workspace-bundle.js.map +1 -0
- package/dist/workspace/sandbox-fs.d.ts +9 -24
- package/dist/workspace/sandbox-fs.js +2 -16
- package/dist/workspace/sandbox-fs.js.map +1 -1
- package/dist/workspace/sandbox-setup.d.ts +1 -7
- package/dist/workspace/sandbox-setup.js +28 -170
- package/dist/workspace/sandbox-setup.js.map +1 -1
- package/dist/workspace/snapshot-image-context.d.ts +5 -0
- package/dist/workspace/snapshot-image-context.js +36 -0
- package/dist/workspace/snapshot-image-context.js.map +1 -0
- package/dist/workspace/snapshot-manager.d.ts +9 -9
- package/dist/workspace/snapshot-manager.js +57 -67
- package/dist/workspace/snapshot-manager.js.map +1 -1
- package/dist/workspace/template-telemetry.js +4 -4
- package/dist/workspace/template-telemetry.js.map +1 -1
- package/dist/workspace/workspace-file-content.d.ts +2 -0
- package/dist/workspace/workspace-file-content.js +11 -0
- package/dist/workspace/workspace-file-content.js.map +1 -0
- package/dist/workspace/workspace-files.d.ts +20 -0
- package/dist/workspace/workspace-files.js +74 -0
- package/dist/workspace/workspace-files.js.map +1 -0
- package/dist/workspace/workspace-manifest.d.ts +12 -0
- package/dist/workspace/workspace-manifest.js +28 -0
- package/dist/workspace/workspace-manifest.js.map +1 -0
- package/package.json +21 -15
- package/skills/data-table-manager/SKILL.md +6 -6
- package/skills/debugging-executions/SKILL.md +44 -0
- package/skills/planned-task-runtime/SKILL.md +163 -0
- package/skills/planning/SKILL.md +137 -0
- package/skills/post-build-flow/SKILL.md +119 -0
- package/skills/post-build-flow/references/trigger-input-data-shapes.md +38 -0
- package/skills/workflow-builder/SKILL.md +608 -0
- package/dist/tools/best-practices/index.d.ts +0 -2
- package/dist/tools/best-practices/index.js +0 -10
- package/dist/tools/best-practices/index.js.map +0 -1
- package/dist/tools/best-practices/techniques.d.ts +0 -1
- package/dist/tools/best-practices/techniques.js +0 -7
- package/dist/tools/best-practices/techniques.js.map +0 -1
- package/dist/tools/orchestration/add-plan-item.tool.d.ts +0 -5
- package/dist/tools/orchestration/add-plan-item.tool.js +0 -89
- package/dist/tools/orchestration/add-plan-item.tool.js.map +0 -1
- package/dist/tools/orchestration/blueprint-accumulator.d.ts +0 -49
- package/dist/tools/orchestration/blueprint-accumulator.js +0 -176
- package/dist/tools/orchestration/blueprint-accumulator.js.map +0 -1
- package/dist/tools/orchestration/blueprint.schema.d.ts +0 -182
- package/dist/tools/orchestration/blueprint.schema.js +0 -56
- package/dist/tools/orchestration/blueprint.schema.js.map +0 -1
- package/dist/tools/orchestration/build-workflow-agent.prompt.d.ts +0 -8
- package/dist/tools/orchestration/build-workflow-agent.prompt.js +0 -466
- package/dist/tools/orchestration/build-workflow-agent.prompt.js.map +0 -1
- package/dist/tools/orchestration/build-workflow-agent.tool.d.ts +0 -148
- package/dist/tools/orchestration/build-workflow-agent.tool.js +0 -1498
- package/dist/tools/orchestration/build-workflow-agent.tool.js.map +0 -1
- package/dist/tools/orchestration/builder-memory-compaction.d.ts +0 -42
- package/dist/tools/orchestration/builder-memory-compaction.js +0 -132
- package/dist/tools/orchestration/builder-memory-compaction.js.map +0 -1
- package/dist/tools/orchestration/plan-agent-prompt.d.ts +0 -1
- package/dist/tools/orchestration/plan-agent-prompt.js +0 -83
- package/dist/tools/orchestration/plan-agent-prompt.js.map +0 -1
- package/dist/tools/orchestration/plan-with-agent.tool.d.ts +0 -27
- package/dist/tools/orchestration/plan-with-agent.tool.js +0 -710
- package/dist/tools/orchestration/plan-with-agent.tool.js.map +0 -1
- package/dist/tools/orchestration/submit-plan.tool.d.ts +0 -3
- package/dist/tools/orchestration/submit-plan.tool.js +0 -98
- package/dist/tools/orchestration/submit-plan.tool.js.map +0 -1
- package/dist/tools/templates.tool.d.ts +0 -1
- package/dist/tools/templates.tool.js +0 -58
- package/dist/tools/templates.tool.js.map +0 -1
- package/dist/workspace/daytona-auth-manager.d.ts +0 -26
- package/dist/workspace/daytona-auth-manager.js +0 -90
- package/dist/workspace/daytona-auth-manager.js.map +0 -1
- package/dist/workspace/daytona-filesystem.d.ts +0 -25
- package/dist/workspace/daytona-filesystem.js +0 -132
- package/dist/workspace/daytona-filesystem.js.map +0 -1
- package/dist/workspace/daytona-sandbox.d.ts +0 -71
- package/dist/workspace/daytona-sandbox.js +0 -368
- package/dist/workspace/daytona-sandbox.js.map +0 -1
- package/dist/workspace/lazy-daytona.d.ts +0 -2
- package/dist/workspace/lazy-daytona.js +0 -12
- package/dist/workspace/lazy-daytona.js.map +0 -1
- package/dist/workspace/local-filesystem.d.ts +0 -48
- package/dist/workspace/local-filesystem.js +0 -250
- package/dist/workspace/local-filesystem.js.map +0 -1
- package/dist/workspace/local-sandbox.d.ts +0 -28
- package/dist/workspace/local-sandbox.js +0 -121
- package/dist/workspace/local-sandbox.js.map +0 -1
- package/dist/workspace/n8n-sandbox-filesystem.d.ts +0 -25
- package/dist/workspace/n8n-sandbox-filesystem.js +0 -128
- package/dist/workspace/n8n-sandbox-filesystem.js.map +0 -1
- package/dist/workspace/n8n-sandbox-sandbox.d.ts +0 -35
- package/dist/workspace/n8n-sandbox-sandbox.js +0 -143
- package/dist/workspace/n8n-sandbox-sandbox.js.map +0 -1
|
@@ -1,710 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.__testBuildPlannerBriefingContext = exports.__testGetPriorToolObservations = exports.__testGetRecentMessages = exports.__testFormatMessagesForBriefing = void 0;
|
|
4
|
-
exports.__testClearPlannedTaskGraph = __testClearPlannedTaskGraph;
|
|
5
|
-
exports.__testRehydrateAccumulatorFromGraph = __testRehydrateAccumulatorFromGraph;
|
|
6
|
-
exports.createPlanWithAgentTool = createPlanWithAgentTool;
|
|
7
|
-
const agents_1 = require("@n8n/agents");
|
|
8
|
-
const luxon_1 = require("luxon");
|
|
9
|
-
const zod_1 = require("zod");
|
|
10
|
-
const add_plan_item_tool_1 = require("./add-plan-item.tool");
|
|
11
|
-
const agent_persistence_1 = require("./agent-persistence");
|
|
12
|
-
const blueprint_accumulator_1 = require("./blueprint-accumulator");
|
|
13
|
-
const display_utils_1 = require("./display-utils");
|
|
14
|
-
const plan_agent_prompt_1 = require("./plan-agent-prompt");
|
|
15
|
-
const submit_plan_tool_1 = require("./submit-plan.tool");
|
|
16
|
-
const tracing_utils_1 = require("./tracing-utils");
|
|
17
|
-
const runtime_workspace_1 = require("../../agent/runtime-workspace");
|
|
18
|
-
const max_steps_1 = require("../../constants/max-steps");
|
|
19
|
-
const consume_with_hitl_1 = require("../../stream/consume-with-hitl");
|
|
20
|
-
const tool_registry_1 = require("../../tool-registry");
|
|
21
|
-
const langsmith_tracing_1 = require("../../tracing/langsmith-tracing");
|
|
22
|
-
const stream_helpers_1 = require("../../utils/stream-helpers");
|
|
23
|
-
const credentials_tool_1 = require("../credentials.tool");
|
|
24
|
-
const data_tables_tool_1 = require("../data-tables.tool");
|
|
25
|
-
const ask_user_tool_1 = require("../shared/ask-user.tool");
|
|
26
|
-
const templates_tool_1 = require("../templates.tool");
|
|
27
|
-
const MESSAGE_HISTORY_COUNT = 5;
|
|
28
|
-
const PLANNER_DOMAIN_TOOL_NAMES = [
|
|
29
|
-
'nodes',
|
|
30
|
-
credentials_tool_1.CREDENTIALS_TOOL_ID,
|
|
31
|
-
data_tables_tool_1.DATA_TABLES_TOOL_ID,
|
|
32
|
-
'workflows',
|
|
33
|
-
ask_user_tool_1.ASK_USER_TOOL_ID,
|
|
34
|
-
];
|
|
35
|
-
const PLANNER_RESEARCH_TOOL_NAMES = ['research'];
|
|
36
|
-
const RELEVANT_PRIOR_TOOL_NAMES = new Set([
|
|
37
|
-
ask_user_tool_1.ASK_USER_TOOL_ID,
|
|
38
|
-
credentials_tool_1.CREDENTIALS_TOOL_ID,
|
|
39
|
-
data_tables_tool_1.DATA_TABLES_TOOL_ID,
|
|
40
|
-
]);
|
|
41
|
-
function extractTextFromMemoryContent(content) {
|
|
42
|
-
if (typeof content === 'string')
|
|
43
|
-
return content;
|
|
44
|
-
if (Array.isArray(content))
|
|
45
|
-
return extractTextParts(content);
|
|
46
|
-
return '';
|
|
47
|
-
}
|
|
48
|
-
function extractTextParts(parts) {
|
|
49
|
-
return parts
|
|
50
|
-
.filter((c) => typeof c === 'object' &&
|
|
51
|
-
c !== null &&
|
|
52
|
-
'type' in c &&
|
|
53
|
-
c.type === 'text' &&
|
|
54
|
-
'text' in c &&
|
|
55
|
-
typeof c.text === 'string')
|
|
56
|
-
.map((c) => c.text)
|
|
57
|
-
.join('\n');
|
|
58
|
-
}
|
|
59
|
-
function isRecord(value) {
|
|
60
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
61
|
-
}
|
|
62
|
-
function readString(value) {
|
|
63
|
-
return typeof value === 'string' && value.trim().length > 0 ? value : undefined;
|
|
64
|
-
}
|
|
65
|
-
function readRecord(value) {
|
|
66
|
-
return isRecord(value) ? value : undefined;
|
|
67
|
-
}
|
|
68
|
-
function readArray(value) {
|
|
69
|
-
return Array.isArray(value) ? value : [];
|
|
70
|
-
}
|
|
71
|
-
function readStringArray(value) {
|
|
72
|
-
return readArray(value).filter((item) => typeof item === 'string');
|
|
73
|
-
}
|
|
74
|
-
function addUnique(target, seen, value) {
|
|
75
|
-
if (!value || seen.has(value))
|
|
76
|
-
return;
|
|
77
|
-
seen.add(value);
|
|
78
|
-
target.push(value);
|
|
79
|
-
}
|
|
80
|
-
function summarizeList(values, limit = 10) {
|
|
81
|
-
const visible = values.slice(0, limit).join(', ');
|
|
82
|
-
const remaining = values.length - limit;
|
|
83
|
-
return remaining > 0 ? `${visible}, and ${remaining} more` : visible;
|
|
84
|
-
}
|
|
85
|
-
async function getRecentMessages(context, count) {
|
|
86
|
-
const messages = [];
|
|
87
|
-
if (context.memory) {
|
|
88
|
-
try {
|
|
89
|
-
const history = await context.memory.getMessages(context.threadId, {
|
|
90
|
-
limit: count,
|
|
91
|
-
});
|
|
92
|
-
for (const m of history) {
|
|
93
|
-
if (!('role' in m))
|
|
94
|
-
continue;
|
|
95
|
-
const role = m.role;
|
|
96
|
-
const content = extractTextFromMemoryContent(m.content);
|
|
97
|
-
if ((role === 'user' || role === 'assistant') && content.length > 0) {
|
|
98
|
-
messages.push({ role, content });
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
catch {
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (shouldAppendCurrentUserMessage(messages, context.currentUserMessage)) {
|
|
106
|
-
messages.push({ role: 'user', content: context.currentUserMessage });
|
|
107
|
-
}
|
|
108
|
-
return messages;
|
|
109
|
-
}
|
|
110
|
-
function shouldAppendCurrentUserMessage(messages, currentUserMessage) {
|
|
111
|
-
const current = currentUserMessage?.trim();
|
|
112
|
-
if (!current)
|
|
113
|
-
return false;
|
|
114
|
-
const lastUserMessage = [...messages].reverse().find((message) => message.role === 'user');
|
|
115
|
-
return lastUserMessage?.content.trim() !== current;
|
|
116
|
-
}
|
|
117
|
-
function getPriorToolObservations(context) {
|
|
118
|
-
const toolCalls = new Map();
|
|
119
|
-
const pendingResults = new Map();
|
|
120
|
-
for (const event of getPriorToolEvents(context)) {
|
|
121
|
-
if (event.type === 'tool-call') {
|
|
122
|
-
const { toolCallId, toolName, args } = event.payload;
|
|
123
|
-
if (!RELEVANT_PRIOR_TOOL_NAMES.has(toolName))
|
|
124
|
-
continue;
|
|
125
|
-
const pendingResult = pendingResults.get(toolCallId);
|
|
126
|
-
toolCalls.set(toolCallId, {
|
|
127
|
-
toolName,
|
|
128
|
-
args,
|
|
129
|
-
result: pendingResult,
|
|
130
|
-
hasResult: pendingResults.has(toolCallId),
|
|
131
|
-
});
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
if (event.type === 'tool-result') {
|
|
135
|
-
const { toolCallId, result } = event.payload;
|
|
136
|
-
const existing = toolCalls.get(toolCallId);
|
|
137
|
-
if (existing) {
|
|
138
|
-
existing.result = result;
|
|
139
|
-
existing.hasResult = true;
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
pendingResults.set(toolCallId, result);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return [...toolCalls.values()]
|
|
147
|
-
.filter((observation) => observation.hasResult)
|
|
148
|
-
.map(({ toolName, args, result }) => ({ toolName, args, result }));
|
|
149
|
-
}
|
|
150
|
-
function getPriorToolEvents(context) {
|
|
151
|
-
if (context.messageGroupId) {
|
|
152
|
-
const runIds = getMessageGroupRunIds(context);
|
|
153
|
-
if (runIds.length > 0) {
|
|
154
|
-
try {
|
|
155
|
-
return context.eventBus.getEventsForRuns(context.threadId, runIds);
|
|
156
|
-
}
|
|
157
|
-
catch {
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
try {
|
|
162
|
-
return context.eventBus.getEventsForRun(context.threadId, context.runId);
|
|
163
|
-
}
|
|
164
|
-
catch {
|
|
165
|
-
return [];
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
function getMessageGroupRunIds(context) {
|
|
169
|
-
const messageGroupId = context.messageGroupId;
|
|
170
|
-
if (!messageGroupId)
|
|
171
|
-
return [];
|
|
172
|
-
const runIds = new Set();
|
|
173
|
-
try {
|
|
174
|
-
for (const { event } of context.eventBus.getEventsAfter(context.threadId, 0)) {
|
|
175
|
-
if (event.type === 'run-start' && event.payload.messageGroupId === messageGroupId) {
|
|
176
|
-
runIds.add(event.runId);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
catch {
|
|
181
|
-
return [context.runId];
|
|
182
|
-
}
|
|
183
|
-
runIds.add(context.runId);
|
|
184
|
-
return [...runIds];
|
|
185
|
-
}
|
|
186
|
-
function buildPlannerBriefingContext(observations) {
|
|
187
|
-
const collectedAnswers = [];
|
|
188
|
-
const discoveredResources = [];
|
|
189
|
-
const seenAnswers = new Set();
|
|
190
|
-
const seenResources = new Set();
|
|
191
|
-
const credentialsById = buildCredentialLookup(observations);
|
|
192
|
-
for (const observation of observations) {
|
|
193
|
-
if (observation.toolName === ask_user_tool_1.ASK_USER_TOOL_ID) {
|
|
194
|
-
for (const answer of extractAskUserAnswerLines(observation)) {
|
|
195
|
-
addUnique(collectedAnswers, seenAnswers, answer);
|
|
196
|
-
}
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
if (observation.toolName === credentials_tool_1.CREDENTIALS_TOOL_ID) {
|
|
200
|
-
const action = readString(observation.args.action);
|
|
201
|
-
if (action === 'list') {
|
|
202
|
-
addUnique(discoveredResources, seenResources, summarizeCredentials(observation.result));
|
|
203
|
-
}
|
|
204
|
-
if (action === 'setup') {
|
|
205
|
-
for (const selection of extractCredentialSelectionLines(observation, credentialsById)) {
|
|
206
|
-
addUnique(collectedAnswers, seenAnswers, selection);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
if (observation.toolName === data_tables_tool_1.DATA_TABLES_TOOL_ID &&
|
|
212
|
-
readString(observation.args.action) === 'list') {
|
|
213
|
-
addUnique(discoveredResources, seenResources, summarizeDataTables(observation.result));
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return { collectedAnswers, discoveredResources };
|
|
217
|
-
}
|
|
218
|
-
function buildCredentialLookup(observations) {
|
|
219
|
-
const credentialsById = new Map();
|
|
220
|
-
for (const observation of observations) {
|
|
221
|
-
if (observation.toolName !== credentials_tool_1.CREDENTIALS_TOOL_ID)
|
|
222
|
-
continue;
|
|
223
|
-
for (const credential of extractCredentials(observation.result)) {
|
|
224
|
-
if (credential.id)
|
|
225
|
-
credentialsById.set(credential.id, credential);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return credentialsById;
|
|
229
|
-
}
|
|
230
|
-
function extractAskUserAnswerLines(observation) {
|
|
231
|
-
const result = readRecord(observation.result);
|
|
232
|
-
if (!result || result.answered === false)
|
|
233
|
-
return [];
|
|
234
|
-
const questionsById = extractQuestionTextById(observation.args);
|
|
235
|
-
const answers = readArray(result.answers);
|
|
236
|
-
const lines = [];
|
|
237
|
-
for (const answerValue of answers) {
|
|
238
|
-
const answer = readRecord(answerValue);
|
|
239
|
-
if (!answer || answer.skipped === true)
|
|
240
|
-
continue;
|
|
241
|
-
const questionId = readString(answer.questionId);
|
|
242
|
-
const question = readString(answer.question) ?? (questionId ? questionsById.get(questionId) : undefined);
|
|
243
|
-
const selectedOptions = readStringArray(answer.selectedOptions);
|
|
244
|
-
const customText = readString(answer.customText);
|
|
245
|
-
const values = [...selectedOptions, ...(customText ? [customText] : [])];
|
|
246
|
-
if (!question || values.length === 0)
|
|
247
|
-
continue;
|
|
248
|
-
lines.push(`${question}: ${values.join(', ')}`);
|
|
249
|
-
}
|
|
250
|
-
return lines;
|
|
251
|
-
}
|
|
252
|
-
function extractQuestionTextById(args) {
|
|
253
|
-
const questionsById = new Map();
|
|
254
|
-
for (const questionValue of readArray(args.questions)) {
|
|
255
|
-
const question = readRecord(questionValue);
|
|
256
|
-
const id = readString(question?.id);
|
|
257
|
-
const text = readString(question?.question);
|
|
258
|
-
if (id && text)
|
|
259
|
-
questionsById.set(id, text);
|
|
260
|
-
}
|
|
261
|
-
return questionsById;
|
|
262
|
-
}
|
|
263
|
-
function extractCredentialSelectionLines(observation, credentialsById) {
|
|
264
|
-
const result = readRecord(observation.result);
|
|
265
|
-
const credentials = readRecord(result?.credentials);
|
|
266
|
-
if (!credentials)
|
|
267
|
-
return [];
|
|
268
|
-
const lines = [];
|
|
269
|
-
for (const [credentialType, credentialIdValue] of Object.entries(credentials)) {
|
|
270
|
-
const credentialId = readString(credentialIdValue);
|
|
271
|
-
if (!credentialId)
|
|
272
|
-
continue;
|
|
273
|
-
const credential = credentialsById.get(credentialId);
|
|
274
|
-
const label = credential
|
|
275
|
-
? `${credential.name} (${credential.type})`
|
|
276
|
-
: `credential ID ${credentialId}`;
|
|
277
|
-
lines.push(`Credential selected for ${credentialType}: ${label}`);
|
|
278
|
-
}
|
|
279
|
-
return lines;
|
|
280
|
-
}
|
|
281
|
-
function summarizeCredentials(result) {
|
|
282
|
-
const credentials = extractCredentials(result);
|
|
283
|
-
if (credentials.length === 0)
|
|
284
|
-
return undefined;
|
|
285
|
-
return `Credentials available: ${summarizeList(credentials.map((credential) => `${credential.name} (${credential.type})`))}`;
|
|
286
|
-
}
|
|
287
|
-
function extractCredentials(result) {
|
|
288
|
-
const record = readRecord(result);
|
|
289
|
-
return readArray(record?.credentials)
|
|
290
|
-
.map(readCredentialBrief)
|
|
291
|
-
.filter((credential) => credential !== undefined);
|
|
292
|
-
}
|
|
293
|
-
function readCredentialBrief(value) {
|
|
294
|
-
const record = readRecord(value);
|
|
295
|
-
const name = readString(record?.name);
|
|
296
|
-
const type = readString(record?.type);
|
|
297
|
-
if (!name || !type)
|
|
298
|
-
return undefined;
|
|
299
|
-
const id = readString(record?.id);
|
|
300
|
-
return {
|
|
301
|
-
name,
|
|
302
|
-
type,
|
|
303
|
-
...(id ? { id } : {}),
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
function summarizeDataTables(result) {
|
|
307
|
-
const tables = extractDataTables(result);
|
|
308
|
-
if (tables.length === 0)
|
|
309
|
-
return undefined;
|
|
310
|
-
return `Data tables available: ${summarizeList(tables.map((table) => table.name))}`;
|
|
311
|
-
}
|
|
312
|
-
function extractDataTables(result) {
|
|
313
|
-
const record = readRecord(result);
|
|
314
|
-
return readArray(record?.tables)
|
|
315
|
-
.map(readDataTableBrief)
|
|
316
|
-
.filter((table) => table !== undefined);
|
|
317
|
-
}
|
|
318
|
-
function readDataTableBrief(value) {
|
|
319
|
-
const record = readRecord(value);
|
|
320
|
-
const name = readString(record?.name);
|
|
321
|
-
if (!name)
|
|
322
|
-
return undefined;
|
|
323
|
-
const id = readString(record?.id);
|
|
324
|
-
return {
|
|
325
|
-
name,
|
|
326
|
-
...(id ? { id } : {}),
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
function formatMessagesForBriefing(messages, guidance, timeZone, briefingContext) {
|
|
330
|
-
const parts = [];
|
|
331
|
-
const now = timeZone ? luxon_1.DateTime.now().setZone(timeZone) : luxon_1.DateTime.now();
|
|
332
|
-
const isoNow = now.toISO({ includeOffset: true }) ?? new Date().toISOString();
|
|
333
|
-
parts.push(`<current-datetime>${isoNow}</current-datetime>`);
|
|
334
|
-
if (timeZone) {
|
|
335
|
-
parts.push(`<user-timezone>${timeZone}</user-timezone>`);
|
|
336
|
-
}
|
|
337
|
-
if (messages.length > 0) {
|
|
338
|
-
parts.push('## Recent conversation');
|
|
339
|
-
for (const m of messages) {
|
|
340
|
-
const label = m.role === 'user' ? 'User' : 'Assistant';
|
|
341
|
-
const content = m.content.length > 2000 ? m.content.slice(0, 2000) + '...' : m.content;
|
|
342
|
-
parts.push(`**${label}:** ${content}`);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
if (briefingContext?.collectedAnswers.length) {
|
|
346
|
-
parts.push('## Already-collected answers');
|
|
347
|
-
for (const answer of briefingContext.collectedAnswers) {
|
|
348
|
-
parts.push(`- ${answer}`);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
if (briefingContext?.discoveredResources.length) {
|
|
352
|
-
parts.push('## Already-discovered resources');
|
|
353
|
-
for (const resource of briefingContext.discoveredResources) {
|
|
354
|
-
parts.push(`- ${resource}`);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
if (guidance) {
|
|
358
|
-
parts.push(`\n## Orchestrator guidance\n${guidance}`);
|
|
359
|
-
}
|
|
360
|
-
parts.push('\nDesign the solution blueprint based on the conversation above.');
|
|
361
|
-
return parts.join('\n\n');
|
|
362
|
-
}
|
|
363
|
-
exports.__testFormatMessagesForBriefing = formatMessagesForBriefing;
|
|
364
|
-
exports.__testGetRecentMessages = getRecentMessages;
|
|
365
|
-
exports.__testGetPriorToolObservations = getPriorToolObservations;
|
|
366
|
-
exports.__testBuildPlannerBriefingContext = buildPlannerBriefingContext;
|
|
367
|
-
function publishClearingEvent(context) {
|
|
368
|
-
context.eventBus.publish(context.threadId, {
|
|
369
|
-
type: 'tasks-update',
|
|
370
|
-
runId: context.runId,
|
|
371
|
-
agentId: context.orchestratorAgentId,
|
|
372
|
-
payload: { tasks: { tasks: [] }, planItems: [] },
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
async function clearDraftChecklist(context) {
|
|
376
|
-
try {
|
|
377
|
-
await context.taskStorage.save(context.threadId, { tasks: [] });
|
|
378
|
-
}
|
|
379
|
-
catch {
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
async function __testClearPlannedTaskGraph(context) {
|
|
383
|
-
return await clearPlannedTaskGraph(context);
|
|
384
|
-
}
|
|
385
|
-
async function clearPlannedTaskGraph(context) {
|
|
386
|
-
if (!context.plannedTaskService)
|
|
387
|
-
return;
|
|
388
|
-
try {
|
|
389
|
-
const graph = await context.plannedTaskService.getGraph(context.threadId);
|
|
390
|
-
if (!graph)
|
|
391
|
-
return;
|
|
392
|
-
if (graph.planRunId !== context.runId)
|
|
393
|
-
return;
|
|
394
|
-
if (graph.status !== 'awaiting_approval')
|
|
395
|
-
return;
|
|
396
|
-
await context.plannedTaskService.clear(context.threadId);
|
|
397
|
-
}
|
|
398
|
-
catch {
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
async function __testRehydrateAccumulatorFromGraph(context, accumulator) {
|
|
402
|
-
return await rehydrateAccumulatorFromGraph(context, accumulator);
|
|
403
|
-
}
|
|
404
|
-
async function rehydrateAccumulatorFromGraph(context, accumulator) {
|
|
405
|
-
if (!context.plannedTaskService)
|
|
406
|
-
return;
|
|
407
|
-
try {
|
|
408
|
-
const graph = await context.plannedTaskService.getGraph(context.threadId);
|
|
409
|
-
if (graph?.status === 'awaiting_approval' && graph.tasks.length > 0) {
|
|
410
|
-
accumulator.loadFromTasks(graph.tasks);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
catch {
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
const planToolSuspendSchema = zod_1.z
|
|
417
|
-
.object({
|
|
418
|
-
requestId: zod_1.z.string(),
|
|
419
|
-
message: zod_1.z.string(),
|
|
420
|
-
severity: zod_1.z.string(),
|
|
421
|
-
inputType: zod_1.z.string().optional(),
|
|
422
|
-
})
|
|
423
|
-
.passthrough();
|
|
424
|
-
const planToolResumeSchema = zod_1.z.record(zod_1.z.unknown());
|
|
425
|
-
function createPlanWithAgentTool(context) {
|
|
426
|
-
return new agents_1.Tool('plan')
|
|
427
|
-
.description('Design and execute a multi-step plan. Spawns a planner agent that reads ' +
|
|
428
|
-
'the conversation history, discovers available credentials, data tables, ' +
|
|
429
|
-
'and best practices, designs the architecture, and shows it to the user ' +
|
|
430
|
-
'for approval. Use when the request requires 2 or more tasks with ' +
|
|
431
|
-
'dependencies. When this tool returns, the plan is already approved ' +
|
|
432
|
-
'and tasks are dispatched — just acknowledge briefly and end your turn.')
|
|
433
|
-
.input(zod_1.z.object({
|
|
434
|
-
guidance: zod_1.z
|
|
435
|
-
.string()
|
|
436
|
-
.optional()
|
|
437
|
-
.describe('Optional steering note for the planner — use ONLY when the conversation ' +
|
|
438
|
-
'history alone is ambiguous about what to build. The planner reads the ' +
|
|
439
|
-
'last 5 messages directly, so do NOT rewrite the user request here.'),
|
|
440
|
-
}))
|
|
441
|
-
.output(zod_1.z.object({
|
|
442
|
-
result: zod_1.z.string(),
|
|
443
|
-
}))
|
|
444
|
-
.suspend(planToolSuspendSchema)
|
|
445
|
-
.resume(planToolResumeSchema)
|
|
446
|
-
.handler(async (input, ctx) => {
|
|
447
|
-
const resumeData = ctx.resumeData;
|
|
448
|
-
const isResume = resumeData !== undefined && resumeData !== null;
|
|
449
|
-
if (!isResume && context.plannedTaskService && context.messageGroupId) {
|
|
450
|
-
const existing = await context.plannedTaskService.getGraph(context.threadId);
|
|
451
|
-
if (existing?.status === 'cancelled' &&
|
|
452
|
-
existing.messageGroupId === context.messageGroupId) {
|
|
453
|
-
context.logger.info('plan tool blocked: user denied a plan earlier in this turn', {
|
|
454
|
-
threadId: context.threadId,
|
|
455
|
-
messageGroupId: context.messageGroupId,
|
|
456
|
-
});
|
|
457
|
-
return {
|
|
458
|
-
result: 'The user denied a plan earlier in this turn. Do not invoke the plan tool again — acknowledge briefly and wait for the next user message.',
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
const plannerTools = (0, tool_registry_1.createToolRegistry)();
|
|
463
|
-
for (const name of PLANNER_DOMAIN_TOOL_NAMES) {
|
|
464
|
-
const tool = context.domainTools.get(name);
|
|
465
|
-
if (tool)
|
|
466
|
-
plannerTools.set(name, tool);
|
|
467
|
-
}
|
|
468
|
-
for (const name of PLANNER_RESEARCH_TOOL_NAMES) {
|
|
469
|
-
const tool = context.domainTools.get(name);
|
|
470
|
-
if (tool)
|
|
471
|
-
plannerTools.set(name, tool);
|
|
472
|
-
}
|
|
473
|
-
plannerTools.set('templates', (0, templates_tool_1.createTemplatesTool)());
|
|
474
|
-
const accumulator = new blueprint_accumulator_1.BlueprintAccumulator();
|
|
475
|
-
plannerTools.set('add-plan-item', (0, add_plan_item_tool_1.createAddPlanItemTool)(accumulator, context));
|
|
476
|
-
plannerTools.set('remove-plan-item', (0, add_plan_item_tool_1.createRemovePlanItemTool)(accumulator, context));
|
|
477
|
-
plannerTools.set('submit-plan', (0, submit_plan_tool_1.createSubmitPlanTool)(accumulator, context));
|
|
478
|
-
const subAgentId = `agent-planner-${context.runId}`;
|
|
479
|
-
const tracedPlannerTools = (0, tracing_utils_1.traceSubAgentTools)(context, plannerTools, 'planner');
|
|
480
|
-
const subAgent = new agents_1.Agent('Workflow Planner Agent')
|
|
481
|
-
.model(context.modelId)
|
|
482
|
-
.instructions(plan_agent_prompt_1.PLANNER_AGENT_PROMPT, {
|
|
483
|
-
providerOptions: {
|
|
484
|
-
anthropic: { cacheControl: { type: 'ephemeral' } },
|
|
485
|
-
},
|
|
486
|
-
})
|
|
487
|
-
.tool((0, tool_registry_1.toolRegistryValues)(tracedPlannerTools))
|
|
488
|
-
.checkpoint(context.checkpointStore ?? 'memory');
|
|
489
|
-
(0, runtime_workspace_1.attachRuntimeWorkspaceCapabilities)(subAgent, {
|
|
490
|
-
runtimeSkills: context.runtimeSkills,
|
|
491
|
-
});
|
|
492
|
-
const telemetry = context.tracing?.getTelemetry?.({
|
|
493
|
-
agentRole: 'planner',
|
|
494
|
-
functionId: 'instance-ai.subagent.planner',
|
|
495
|
-
executionMode: 'background',
|
|
496
|
-
metadata: { agent_id: subAgentId },
|
|
497
|
-
});
|
|
498
|
-
if (telemetry) {
|
|
499
|
-
subAgent.telemetry(telemetry);
|
|
500
|
-
}
|
|
501
|
-
let traceRun;
|
|
502
|
-
try {
|
|
503
|
-
let consumeResult;
|
|
504
|
-
if (isResume) {
|
|
505
|
-
const resumeInfo = await context.findSubAgentResumeInfo?.('planner');
|
|
506
|
-
if (!resumeInfo) {
|
|
507
|
-
return {
|
|
508
|
-
result: 'The planning step could not be resumed because its state was lost. Please send a new message to continue.',
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
await rehydrateAccumulatorFromGraph(context, accumulator);
|
|
512
|
-
traceRun = await (0, tracing_utils_1.startSubAgentTrace)(context, {
|
|
513
|
-
agentId: subAgentId,
|
|
514
|
-
role: 'planner',
|
|
515
|
-
kind: 'planner',
|
|
516
|
-
inputs: { resumed: true },
|
|
517
|
-
});
|
|
518
|
-
(0, langsmith_tracing_1.mergeTraceRunInputs)(traceRun, (0, langsmith_tracing_1.buildAgentTraceInputs)({
|
|
519
|
-
systemPrompt: plan_agent_prompt_1.PLANNER_AGENT_PROMPT,
|
|
520
|
-
tools: tracedPlannerTools,
|
|
521
|
-
modelId: context.modelId,
|
|
522
|
-
}));
|
|
523
|
-
consumeResult = await (0, tracing_utils_1.withTraceRun)(context, traceRun, async () => {
|
|
524
|
-
const resumed = await (0, stream_helpers_1.resumeAgentStream)(subAgent, resumeData, {
|
|
525
|
-
runId: resumeInfo.runId,
|
|
526
|
-
toolCallId: resumeInfo.toolCallId,
|
|
527
|
-
persistence: resumeInfo.persistence,
|
|
528
|
-
maxIterations: max_steps_1.MAX_STEPS.PLANNER,
|
|
529
|
-
});
|
|
530
|
-
return await (0, consume_with_hitl_1.consumeStreamCascading)({
|
|
531
|
-
agent: subAgent,
|
|
532
|
-
stream: resumed,
|
|
533
|
-
runId: context.runId,
|
|
534
|
-
agentId: subAgentId,
|
|
535
|
-
eventBus: context.eventBus,
|
|
536
|
-
logger: context.logger,
|
|
537
|
-
threadId: context.threadId,
|
|
538
|
-
abortSignal: context.abortSignal,
|
|
539
|
-
});
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
else {
|
|
543
|
-
if (context.persistInFlightUserMessage) {
|
|
544
|
-
await context.persistInFlightUserMessage();
|
|
545
|
-
}
|
|
546
|
-
const messages = await getRecentMessages(context, MESSAGE_HISTORY_COUNT);
|
|
547
|
-
const briefingContext = buildPlannerBriefingContext(getPriorToolObservations(context));
|
|
548
|
-
const briefing = formatMessagesForBriefing(messages, input.guidance, context.timeZone, briefingContext);
|
|
549
|
-
const subtitle = input.guidance ?? messages.find((m) => m.role === 'user')?.content ?? 'Planning...';
|
|
550
|
-
context.eventBus.publish(context.threadId, {
|
|
551
|
-
type: 'agent-spawned',
|
|
552
|
-
runId: context.runId,
|
|
553
|
-
agentId: subAgentId,
|
|
554
|
-
payload: {
|
|
555
|
-
parentId: context.orchestratorAgentId,
|
|
556
|
-
role: 'planner',
|
|
557
|
-
tools: (0, tool_registry_1.toolRegistryKeys)(plannerTools),
|
|
558
|
-
kind: 'planner',
|
|
559
|
-
title: 'Planning',
|
|
560
|
-
subtitle: (0, display_utils_1.truncateLabel)(subtitle),
|
|
561
|
-
goal: briefing,
|
|
562
|
-
},
|
|
563
|
-
});
|
|
564
|
-
traceRun = await (0, tracing_utils_1.startSubAgentTrace)(context, {
|
|
565
|
-
agentId: subAgentId,
|
|
566
|
-
role: 'planner',
|
|
567
|
-
kind: 'planner',
|
|
568
|
-
inputs: {
|
|
569
|
-
guidance: input.guidance,
|
|
570
|
-
messageCount: messages.length,
|
|
571
|
-
},
|
|
572
|
-
});
|
|
573
|
-
(0, langsmith_tracing_1.mergeTraceRunInputs)(traceRun, (0, langsmith_tracing_1.buildAgentTraceInputs)({
|
|
574
|
-
systemPrompt: plan_agent_prompt_1.PLANNER_AGENT_PROMPT,
|
|
575
|
-
tools: tracedPlannerTools,
|
|
576
|
-
modelId: context.modelId,
|
|
577
|
-
}));
|
|
578
|
-
consumeResult = await (0, tracing_utils_1.withTraceRun)(context, traceRun, async () => {
|
|
579
|
-
const persistence = await (0, agent_persistence_1.createSubAgentPersistence)(context, {
|
|
580
|
-
agentKind: 'planner',
|
|
581
|
-
});
|
|
582
|
-
const stream = await subAgent.stream(briefing, {
|
|
583
|
-
maxIterations: max_steps_1.MAX_STEPS.PLANNER,
|
|
584
|
-
abortSignal: context.abortSignal,
|
|
585
|
-
persistence,
|
|
586
|
-
providerOptions: {
|
|
587
|
-
anthropic: { cacheControl: { type: 'ephemeral' } },
|
|
588
|
-
},
|
|
589
|
-
});
|
|
590
|
-
return await (0, consume_with_hitl_1.consumeStreamCascading)({
|
|
591
|
-
agent: subAgent,
|
|
592
|
-
stream,
|
|
593
|
-
runId: context.runId,
|
|
594
|
-
agentId: subAgentId,
|
|
595
|
-
eventBus: context.eventBus,
|
|
596
|
-
logger: context.logger,
|
|
597
|
-
threadId: context.threadId,
|
|
598
|
-
abortSignal: context.abortSignal,
|
|
599
|
-
});
|
|
600
|
-
});
|
|
601
|
-
}
|
|
602
|
-
if (consumeResult.status === 'suspended') {
|
|
603
|
-
const parsed = planToolSuspendSchema.safeParse(consumeResult.suspension.suspendPayload);
|
|
604
|
-
if (!parsed.success) {
|
|
605
|
-
context.logger.warn('Planner emitted a suspension payload missing required fields', {
|
|
606
|
-
threadId: context.threadId,
|
|
607
|
-
runId: context.runId,
|
|
608
|
-
toolName: consumeResult.suspension.toolName,
|
|
609
|
-
zodIssues: parsed.error.issues,
|
|
610
|
-
});
|
|
611
|
-
publishClearingEvent(context);
|
|
612
|
-
await clearDraftChecklist(context);
|
|
613
|
-
await clearPlannedTaskGraph(context);
|
|
614
|
-
return {
|
|
615
|
-
result: 'Planner requested user input but the payload was malformed. Please try again.',
|
|
616
|
-
};
|
|
617
|
-
}
|
|
618
|
-
return await ctx.suspend(parsed.data);
|
|
619
|
-
}
|
|
620
|
-
const resultText = consumeResult.status === 'completed' ? await consumeResult.text : '';
|
|
621
|
-
if (traceRun) {
|
|
622
|
-
await (0, tracing_utils_1.finishTraceRun)(context, traceRun, {
|
|
623
|
-
outputs: {
|
|
624
|
-
result: resultText,
|
|
625
|
-
agentId: subAgentId,
|
|
626
|
-
role: 'planner',
|
|
627
|
-
hasItems: !accumulator.isEmpty(),
|
|
628
|
-
itemCount: accumulator.getTaskItemsForEvent().length,
|
|
629
|
-
},
|
|
630
|
-
});
|
|
631
|
-
}
|
|
632
|
-
context.eventBus.publish(context.threadId, {
|
|
633
|
-
type: 'agent-completed',
|
|
634
|
-
runId: context.runId,
|
|
635
|
-
agentId: subAgentId,
|
|
636
|
-
payload: {
|
|
637
|
-
role: 'planner',
|
|
638
|
-
result: resultText,
|
|
639
|
-
},
|
|
640
|
-
});
|
|
641
|
-
if (accumulator.isApproved()) {
|
|
642
|
-
if (context.plannedTaskService) {
|
|
643
|
-
await context.plannedTaskService.approvePlan(context.threadId);
|
|
644
|
-
}
|
|
645
|
-
if (context.schedulePlannedTasks) {
|
|
646
|
-
await context.schedulePlannedTasks();
|
|
647
|
-
}
|
|
648
|
-
const persistedCount = await getPersistedTaskCount(context);
|
|
649
|
-
const taskCount = persistedCount ?? accumulator.getTaskList().length;
|
|
650
|
-
return {
|
|
651
|
-
result: `Plan approved and ${taskCount} task${taskCount === 1 ? '' : 's'} dispatched.`,
|
|
652
|
-
};
|
|
653
|
-
}
|
|
654
|
-
if (accumulator.isDenied()) {
|
|
655
|
-
publishClearingEvent(context);
|
|
656
|
-
await clearDraftChecklist(context);
|
|
657
|
-
return { result: 'Plan denied by user. No tasks were dispatched.' };
|
|
658
|
-
}
|
|
659
|
-
publishClearingEvent(context);
|
|
660
|
-
await clearDraftChecklist(context);
|
|
661
|
-
await clearPlannedTaskGraph(context);
|
|
662
|
-
if (!accumulator.isEmpty()) {
|
|
663
|
-
return {
|
|
664
|
-
result: `Planner added ${accumulator.getTaskList().length} items but did not submit the plan for approval. The plan was not executed.`,
|
|
665
|
-
};
|
|
666
|
-
}
|
|
667
|
-
return {
|
|
668
|
-
result: `Planner finished without producing a plan. Agent output: ${resultText}`,
|
|
669
|
-
};
|
|
670
|
-
}
|
|
671
|
-
catch (error) {
|
|
672
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
673
|
-
if (traceRun) {
|
|
674
|
-
await (0, tracing_utils_1.failTraceRun)(context, traceRun, error, {
|
|
675
|
-
agent_id: subAgentId,
|
|
676
|
-
agent_role: 'planner',
|
|
677
|
-
});
|
|
678
|
-
}
|
|
679
|
-
context.eventBus.publish(context.threadId, {
|
|
680
|
-
type: 'agent-completed',
|
|
681
|
-
runId: context.runId,
|
|
682
|
-
agentId: subAgentId,
|
|
683
|
-
payload: {
|
|
684
|
-
role: 'planner',
|
|
685
|
-
result: '',
|
|
686
|
-
error: errorMessage,
|
|
687
|
-
},
|
|
688
|
-
});
|
|
689
|
-
if (!accumulator.isApproved()) {
|
|
690
|
-
publishClearingEvent(context);
|
|
691
|
-
await clearDraftChecklist(context);
|
|
692
|
-
await clearPlannedTaskGraph(context);
|
|
693
|
-
}
|
|
694
|
-
return { result: `Planner error: ${errorMessage}` };
|
|
695
|
-
}
|
|
696
|
-
})
|
|
697
|
-
.build();
|
|
698
|
-
}
|
|
699
|
-
async function getPersistedTaskCount(context) {
|
|
700
|
-
if (!context.plannedTaskService)
|
|
701
|
-
return undefined;
|
|
702
|
-
try {
|
|
703
|
-
const graph = await context.plannedTaskService.getGraph(context.threadId);
|
|
704
|
-
return graph?.tasks?.length;
|
|
705
|
-
}
|
|
706
|
-
catch {
|
|
707
|
-
return undefined;
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
//# sourceMappingURL=plan-with-agent.tool.js.map
|