zeitlich 0.1.0
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/LICENSE +21 -0
- package/README.md +494 -0
- package/dist/index.d.mts +152 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.js +1623 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1582 -0
- package/dist/index.mjs.map +1 -0
- package/dist/workflow-DeVGEXSc.d.mts +1201 -0
- package/dist/workflow-DeVGEXSc.d.ts +1201 -0
- package/dist/workflow.d.mts +4 -0
- package/dist/workflow.d.ts +4 -0
- package/dist/workflow.js +762 -0
- package/dist/workflow.js.map +1 -0
- package/dist/workflow.mjs +734 -0
- package/dist/workflow.mjs.map +1 -0
- package/package.json +92 -0
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
import { workflowInfo, uuid4, executeChild, proxyActivities } from '@temporalio/workflow';
|
|
2
|
+
import z2, { z } from 'zod';
|
|
3
|
+
import { minimatch } from 'minimatch';
|
|
4
|
+
|
|
5
|
+
// src/lib/session.ts
|
|
6
|
+
var TASK_TOOL = "Task";
|
|
7
|
+
function buildTaskDescription(subagents) {
|
|
8
|
+
const subagentList = subagents.map((s) => `- **${s.name}**: ${s.description}`).join("\n");
|
|
9
|
+
return `Launch a new agent to handle complex, multi-step tasks autonomously.
|
|
10
|
+
|
|
11
|
+
The ${TASK_TOOL} tool launches specialized agents (subprocesses) that autonomously handle complex tasks. Each agent type has specific capabilities and tools available to it.
|
|
12
|
+
|
|
13
|
+
Available agent types:
|
|
14
|
+
|
|
15
|
+
${subagentList}
|
|
16
|
+
|
|
17
|
+
When using the ${TASK_TOOL} tool, you must specify a subagent parameter to select which agent type to use.
|
|
18
|
+
|
|
19
|
+
Usage notes:
|
|
20
|
+
|
|
21
|
+
- Always include a short description (3-5 words) summarizing what the agent will do
|
|
22
|
+
- Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses
|
|
23
|
+
- When the agent is done, it will return a single message back to you. The result returned by the agent is not visible to the user. To show the user the result, you should send a text message back to the user with a concise summary of the result.
|
|
24
|
+
- Each invocation starts fresh - provide a detailed task description with all necessary context.
|
|
25
|
+
- Provide clear, detailed prompts so the agent can work autonomously and return exactly the information you need.
|
|
26
|
+
- The agent's outputs should generally be trusted
|
|
27
|
+
- Clearly tell the agent what type of work you expect since it is not aware of the user's intent
|
|
28
|
+
- If the agent description mentions that it should be used proactively, then you should try your best to use it without the user having to ask for it first. Use your judgement.`;
|
|
29
|
+
}
|
|
30
|
+
function createTaskTool(subagents) {
|
|
31
|
+
if (subagents.length === 0) {
|
|
32
|
+
throw new Error("createTaskTool requires at least one subagent");
|
|
33
|
+
}
|
|
34
|
+
const names = subagents.map((s) => s.name);
|
|
35
|
+
return {
|
|
36
|
+
name: TASK_TOOL,
|
|
37
|
+
description: buildTaskDescription(subagents),
|
|
38
|
+
schema: z2.object({
|
|
39
|
+
subagent: z2.enum(names).describe("The type of subagent to launch"),
|
|
40
|
+
description: z2.string().describe("A short (3-5 word) description of the task"),
|
|
41
|
+
prompt: z2.string().describe("The task for the agent to perform")
|
|
42
|
+
})
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function createTaskHandler(subagents) {
|
|
46
|
+
const { workflowId: parentWorkflowId, taskQueue: parentTaskQueue } = workflowInfo();
|
|
47
|
+
return async (args, _toolCallId) => {
|
|
48
|
+
const config = subagents.find((s) => s.name === args.subagent);
|
|
49
|
+
if (!config) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`Unknown subagent: ${args.subagent}. Available: ${subagents.map((s) => s.name).join(", ")}`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const childWorkflowId = `${parentWorkflowId}-${args.subagent}-${uuid4()}`;
|
|
55
|
+
const childResult = await executeChild(config.workflowType, {
|
|
56
|
+
workflowId: childWorkflowId,
|
|
57
|
+
args: [{ prompt: args.prompt }],
|
|
58
|
+
taskQueue: config.taskQueue ?? parentTaskQueue
|
|
59
|
+
});
|
|
60
|
+
const validated = config.resultSchema ? config.resultSchema.parse(childResult) : childResult;
|
|
61
|
+
const content = typeof validated === "string" ? validated : JSON.stringify(validated, null, 2);
|
|
62
|
+
return {
|
|
63
|
+
content,
|
|
64
|
+
result: {
|
|
65
|
+
result: validated,
|
|
66
|
+
childWorkflowId
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/lib/subagent-support.ts
|
|
73
|
+
function withSubagentSupport(userTools, config) {
|
|
74
|
+
if (config.subagents.length === 0) {
|
|
75
|
+
throw new Error("withSubagentSupport requires at least one subagent");
|
|
76
|
+
}
|
|
77
|
+
const taskTool = createTaskTool(config.subagents);
|
|
78
|
+
const taskHandler = createTaskHandler(config.subagents);
|
|
79
|
+
return {
|
|
80
|
+
tools: {
|
|
81
|
+
...userTools,
|
|
82
|
+
Task: taskTool
|
|
83
|
+
},
|
|
84
|
+
taskHandler
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function hasTaskTool(tools) {
|
|
88
|
+
return "Task" in tools;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/lib/session.ts
|
|
92
|
+
var createSession = async ({ threadId, agentName, maxTurns = 50, metadata = {} }, {
|
|
93
|
+
runAgent,
|
|
94
|
+
promptManager,
|
|
95
|
+
toolRouter,
|
|
96
|
+
toolRegistry,
|
|
97
|
+
hooks = {}
|
|
98
|
+
}) => {
|
|
99
|
+
const { initializeThread, appendHumanMessage, parseToolCalls } = proxyActivities({
|
|
100
|
+
startToCloseTimeout: "30m",
|
|
101
|
+
retry: {
|
|
102
|
+
maximumAttempts: 6,
|
|
103
|
+
initialInterval: "5s",
|
|
104
|
+
maximumInterval: "15m",
|
|
105
|
+
backoffCoefficient: 4
|
|
106
|
+
},
|
|
107
|
+
heartbeatTimeout: "5m"
|
|
108
|
+
});
|
|
109
|
+
const callSessionEnd = async (exitReason, turns) => {
|
|
110
|
+
if (hooks.onSessionEnd) {
|
|
111
|
+
await hooks.onSessionEnd({
|
|
112
|
+
threadId,
|
|
113
|
+
agentName,
|
|
114
|
+
exitReason,
|
|
115
|
+
turns,
|
|
116
|
+
metadata
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
runSession: async (prompt, stateManager) => {
|
|
122
|
+
if (hooks.onSessionStart) {
|
|
123
|
+
await hooks.onSessionStart({
|
|
124
|
+
threadId,
|
|
125
|
+
agentName,
|
|
126
|
+
metadata
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
await initializeThread(threadId);
|
|
130
|
+
await appendHumanMessage(
|
|
131
|
+
threadId,
|
|
132
|
+
await promptManager.buildContextMessage(prompt)
|
|
133
|
+
);
|
|
134
|
+
let exitReason = "completed";
|
|
135
|
+
try {
|
|
136
|
+
while (stateManager.isRunning() && !stateManager.isTerminal() && stateManager.getTurns() < maxTurns) {
|
|
137
|
+
stateManager.incrementTurns();
|
|
138
|
+
const currentTurn = stateManager.getTurns();
|
|
139
|
+
const { message, stopReason } = await runAgent(
|
|
140
|
+
{
|
|
141
|
+
threadId,
|
|
142
|
+
agentName,
|
|
143
|
+
metadata
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
systemPrompt: await promptManager.getSystemPrompt()
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
if (stopReason === "end_turn") {
|
|
150
|
+
stateManager.complete();
|
|
151
|
+
exitReason = "completed";
|
|
152
|
+
return message;
|
|
153
|
+
}
|
|
154
|
+
const rawToolCalls = await parseToolCalls(message);
|
|
155
|
+
const parsedToolCalls = rawToolCalls.map(
|
|
156
|
+
(tc) => toolRegistry.parseToolCall(tc)
|
|
157
|
+
);
|
|
158
|
+
await toolRouter.processToolCalls(parsedToolCalls, {
|
|
159
|
+
turn: currentTurn
|
|
160
|
+
});
|
|
161
|
+
if (stateManager.getStatus() === "WAITING_FOR_INPUT") {
|
|
162
|
+
exitReason = "waiting_for_input";
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (stateManager.getTurns() >= maxTurns && stateManager.isRunning()) {
|
|
167
|
+
exitReason = "max_turns";
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {
|
|
170
|
+
exitReason = "failed";
|
|
171
|
+
throw error;
|
|
172
|
+
} finally {
|
|
173
|
+
await callSessionEnd(exitReason, stateManager.getTurns());
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// src/lib/types.ts
|
|
181
|
+
function isTerminalStatus(status) {
|
|
182
|
+
return status === "COMPLETED" || status === "FAILED" || status === "CANCELLED";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// src/lib/state-manager.ts
|
|
186
|
+
function createAgentStateManager(config) {
|
|
187
|
+
let status = "RUNNING";
|
|
188
|
+
let version = 0;
|
|
189
|
+
let turns = 0;
|
|
190
|
+
const customState = { ...config?.initialState ?? {} };
|
|
191
|
+
function buildState() {
|
|
192
|
+
return {
|
|
193
|
+
status,
|
|
194
|
+
version,
|
|
195
|
+
turns,
|
|
196
|
+
...customState
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
getStatus() {
|
|
201
|
+
return status;
|
|
202
|
+
},
|
|
203
|
+
isRunning() {
|
|
204
|
+
return status === "RUNNING";
|
|
205
|
+
},
|
|
206
|
+
isTerminal() {
|
|
207
|
+
return isTerminalStatus(status);
|
|
208
|
+
},
|
|
209
|
+
getTurns() {
|
|
210
|
+
return turns;
|
|
211
|
+
},
|
|
212
|
+
getVersion() {
|
|
213
|
+
return version;
|
|
214
|
+
},
|
|
215
|
+
run() {
|
|
216
|
+
status = "RUNNING";
|
|
217
|
+
version++;
|
|
218
|
+
},
|
|
219
|
+
waitForInput() {
|
|
220
|
+
status = "WAITING_FOR_INPUT";
|
|
221
|
+
version++;
|
|
222
|
+
},
|
|
223
|
+
complete() {
|
|
224
|
+
status = "COMPLETED";
|
|
225
|
+
version++;
|
|
226
|
+
},
|
|
227
|
+
fail() {
|
|
228
|
+
status = "FAILED";
|
|
229
|
+
version++;
|
|
230
|
+
},
|
|
231
|
+
cancel() {
|
|
232
|
+
status = "CANCELLED";
|
|
233
|
+
version++;
|
|
234
|
+
},
|
|
235
|
+
incrementVersion() {
|
|
236
|
+
version++;
|
|
237
|
+
},
|
|
238
|
+
incrementTurns() {
|
|
239
|
+
turns++;
|
|
240
|
+
},
|
|
241
|
+
get(key) {
|
|
242
|
+
return customState[key];
|
|
243
|
+
},
|
|
244
|
+
set(key, value) {
|
|
245
|
+
customState[key] = value;
|
|
246
|
+
version++;
|
|
247
|
+
},
|
|
248
|
+
getCurrentState() {
|
|
249
|
+
return buildState();
|
|
250
|
+
},
|
|
251
|
+
shouldReturnFromWait(lastKnownVersion) {
|
|
252
|
+
return version > lastKnownVersion || isTerminalStatus(status);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
var AGENT_HANDLER_NAMES = {
|
|
257
|
+
getAgentState: "getAgentState",
|
|
258
|
+
waitForStateChange: "waitForStateChange",
|
|
259
|
+
addMessage: "addMessage"
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
// src/lib/prompt-manager.ts
|
|
263
|
+
function createPromptManager(config) {
|
|
264
|
+
const { baseSystemPrompt, instructionsPrompt, buildContextMessage } = config;
|
|
265
|
+
async function resolvePrompt(prompt) {
|
|
266
|
+
if (typeof prompt === "function") {
|
|
267
|
+
return prompt();
|
|
268
|
+
}
|
|
269
|
+
return prompt;
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
async getSystemPrompt() {
|
|
273
|
+
const base = await resolvePrompt(baseSystemPrompt);
|
|
274
|
+
const instructions = await resolvePrompt(instructionsPrompt);
|
|
275
|
+
return [base, instructions].join("\n");
|
|
276
|
+
},
|
|
277
|
+
async buildContextMessage(context) {
|
|
278
|
+
return buildContextMessage(context);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// src/lib/tool-registry.ts
|
|
284
|
+
function createToolRegistry(tools) {
|
|
285
|
+
const toolMap = /* @__PURE__ */ new Map();
|
|
286
|
+
for (const [_key, tool] of Object.entries(tools)) {
|
|
287
|
+
toolMap.set(tool.name, tool);
|
|
288
|
+
}
|
|
289
|
+
return {
|
|
290
|
+
parseToolCall(toolCall) {
|
|
291
|
+
const tool = toolMap.get(toolCall.name);
|
|
292
|
+
if (!tool) {
|
|
293
|
+
throw new Error(`Tool ${toolCall.name} not found`);
|
|
294
|
+
}
|
|
295
|
+
const parsedArgs = tool.schema.parse(toolCall.args);
|
|
296
|
+
return {
|
|
297
|
+
id: toolCall.id ?? "",
|
|
298
|
+
name: toolCall.name,
|
|
299
|
+
args: parsedArgs
|
|
300
|
+
};
|
|
301
|
+
},
|
|
302
|
+
getToolList() {
|
|
303
|
+
return Object.values(tools);
|
|
304
|
+
},
|
|
305
|
+
getTool(name) {
|
|
306
|
+
return tools[name];
|
|
307
|
+
},
|
|
308
|
+
hasTool(name) {
|
|
309
|
+
return toolMap.has(name);
|
|
310
|
+
},
|
|
311
|
+
getToolNames() {
|
|
312
|
+
return Array.from(toolMap.keys());
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// src/lib/tool-router.ts
|
|
318
|
+
function createToolRouter(options, handlers) {
|
|
319
|
+
const { parallel = true, threadId, appendToolResult, hooks } = options;
|
|
320
|
+
async function processToolCall(toolCall, turn) {
|
|
321
|
+
const startTime = Date.now();
|
|
322
|
+
let effectiveArgs = toolCall.args;
|
|
323
|
+
if (hooks?.onPreToolUse) {
|
|
324
|
+
const preResult = await hooks.onPreToolUse({
|
|
325
|
+
toolCall,
|
|
326
|
+
threadId,
|
|
327
|
+
turn
|
|
328
|
+
});
|
|
329
|
+
if (preResult?.skip) {
|
|
330
|
+
await appendToolResult({
|
|
331
|
+
threadId,
|
|
332
|
+
toolCallId: toolCall.id,
|
|
333
|
+
content: JSON.stringify({
|
|
334
|
+
skipped: true,
|
|
335
|
+
reason: "Skipped by PreToolUse hook"
|
|
336
|
+
})
|
|
337
|
+
});
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
if (preResult?.modifiedArgs !== void 0) {
|
|
341
|
+
effectiveArgs = preResult.modifiedArgs;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const handler = handlers[toolCall.name];
|
|
345
|
+
let result;
|
|
346
|
+
let content;
|
|
347
|
+
try {
|
|
348
|
+
if (handler) {
|
|
349
|
+
const response = await handler(
|
|
350
|
+
effectiveArgs,
|
|
351
|
+
toolCall.id
|
|
352
|
+
);
|
|
353
|
+
result = response.result;
|
|
354
|
+
content = response.content;
|
|
355
|
+
} else {
|
|
356
|
+
result = { error: `Unknown tool: ${toolCall.name}` };
|
|
357
|
+
content = JSON.stringify(result, null, 2);
|
|
358
|
+
}
|
|
359
|
+
} catch (error) {
|
|
360
|
+
if (hooks?.onPostToolUseFailure) {
|
|
361
|
+
const failureResult = await hooks.onPostToolUseFailure({
|
|
362
|
+
toolCall,
|
|
363
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
364
|
+
threadId,
|
|
365
|
+
turn
|
|
366
|
+
});
|
|
367
|
+
if (failureResult?.fallbackContent !== void 0) {
|
|
368
|
+
content = failureResult.fallbackContent;
|
|
369
|
+
result = { error: String(error), recovered: true };
|
|
370
|
+
} else if (failureResult?.suppress) {
|
|
371
|
+
content = JSON.stringify({ error: String(error), suppressed: true });
|
|
372
|
+
result = { error: String(error), suppressed: true };
|
|
373
|
+
} else {
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
} else {
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
await appendToolResult({ threadId, toolCallId: toolCall.id, content });
|
|
381
|
+
const toolResult = {
|
|
382
|
+
toolCallId: toolCall.id,
|
|
383
|
+
name: toolCall.name,
|
|
384
|
+
result
|
|
385
|
+
};
|
|
386
|
+
if (hooks?.onPostToolUse) {
|
|
387
|
+
const durationMs = Date.now() - startTime;
|
|
388
|
+
await hooks.onPostToolUse({
|
|
389
|
+
toolCall,
|
|
390
|
+
result: toolResult,
|
|
391
|
+
threadId,
|
|
392
|
+
turn,
|
|
393
|
+
durationMs
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
return toolResult;
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
async processToolCalls(toolCalls, context) {
|
|
400
|
+
if (toolCalls.length === 0) {
|
|
401
|
+
return [];
|
|
402
|
+
}
|
|
403
|
+
const turn = context?.turn ?? 0;
|
|
404
|
+
if (parallel) {
|
|
405
|
+
const results2 = await Promise.all(
|
|
406
|
+
toolCalls.map((tc) => processToolCall(tc, turn))
|
|
407
|
+
);
|
|
408
|
+
return results2.filter(
|
|
409
|
+
(r) => r !== null
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
const results = [];
|
|
413
|
+
for (const toolCall of toolCalls) {
|
|
414
|
+
const result = await processToolCall(toolCall, turn);
|
|
415
|
+
if (result !== null) {
|
|
416
|
+
results.push(result);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return results;
|
|
420
|
+
},
|
|
421
|
+
async processToolCallsByName(toolCalls, toolName, handler) {
|
|
422
|
+
const matchingCalls = toolCalls.filter((tc) => tc.name === toolName);
|
|
423
|
+
if (matchingCalls.length === 0) {
|
|
424
|
+
return [];
|
|
425
|
+
}
|
|
426
|
+
const processOne = async (toolCall) => {
|
|
427
|
+
const response = await handler(
|
|
428
|
+
toolCall.args,
|
|
429
|
+
toolCall.id
|
|
430
|
+
);
|
|
431
|
+
await appendToolResult({
|
|
432
|
+
threadId,
|
|
433
|
+
toolCallId: toolCall.id,
|
|
434
|
+
content: response.content
|
|
435
|
+
});
|
|
436
|
+
return {
|
|
437
|
+
toolCallId: toolCall.id,
|
|
438
|
+
name: toolCall.name,
|
|
439
|
+
result: response.result
|
|
440
|
+
};
|
|
441
|
+
};
|
|
442
|
+
if (parallel) {
|
|
443
|
+
return Promise.all(matchingCalls.map(processOne));
|
|
444
|
+
}
|
|
445
|
+
const results = [];
|
|
446
|
+
for (const toolCall of matchingCalls) {
|
|
447
|
+
results.push(await processOne(toolCall));
|
|
448
|
+
}
|
|
449
|
+
return results;
|
|
450
|
+
},
|
|
451
|
+
filterByName(toolCalls, name) {
|
|
452
|
+
return toolCalls.filter(
|
|
453
|
+
(tc) => tc.name === name
|
|
454
|
+
);
|
|
455
|
+
},
|
|
456
|
+
hasToolCall(toolCalls, name) {
|
|
457
|
+
return toolCalls.some((tc) => tc.name === name);
|
|
458
|
+
},
|
|
459
|
+
getResultsByName(results, name) {
|
|
460
|
+
return results.filter(
|
|
461
|
+
(r) => r.name === name
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
function hasNoOtherToolCalls(toolCalls, excludeName) {
|
|
467
|
+
return toolCalls.filter((tc) => tc.name !== excludeName).length === 0;
|
|
468
|
+
}
|
|
469
|
+
var askUserQuestionTool = {
|
|
470
|
+
name: "AskUserQuestion",
|
|
471
|
+
description: `Use this tool when you need to ask the user questions during execution. This allows you to:
|
|
472
|
+
|
|
473
|
+
1. Gather user preferences or requirements
|
|
474
|
+
2. Clarify ambiguous instructions
|
|
475
|
+
3. Get decisions on implementation choices as you work
|
|
476
|
+
4. Offer choices to the user about what direction to take.
|
|
477
|
+
|
|
478
|
+
Usage notes:
|
|
479
|
+
|
|
480
|
+
* Users will always be able to select "Other" to provide custom text input
|
|
481
|
+
* Use multiSelect: true to allow multiple answers to be selected for a question
|
|
482
|
+
* If you recommend a specific option, make that the first option in the list and add "(Recommended)" at the end of the label
|
|
483
|
+
`,
|
|
484
|
+
schema: z2.object({
|
|
485
|
+
questions: z2.array(
|
|
486
|
+
z2.object({
|
|
487
|
+
question: z2.string().describe("The full question text to display"),
|
|
488
|
+
header: z2.string().describe("Short label for the question (max 12 characters)"),
|
|
489
|
+
options: z2.array(
|
|
490
|
+
z2.object({
|
|
491
|
+
label: z2.string(),
|
|
492
|
+
description: z2.string()
|
|
493
|
+
})
|
|
494
|
+
).min(0).max(4).describe("Array of 0-4 choices, each with label and description"),
|
|
495
|
+
multiSelect: z2.boolean().describe("If true, users can select multiple options")
|
|
496
|
+
})
|
|
497
|
+
)
|
|
498
|
+
}),
|
|
499
|
+
strict: true
|
|
500
|
+
};
|
|
501
|
+
var globTool = {
|
|
502
|
+
name: "Glob",
|
|
503
|
+
description: `Search for files matching a glob pattern within the available file system.
|
|
504
|
+
|
|
505
|
+
Usage:
|
|
506
|
+
- Use glob patterns like "**/*.ts" to find all TypeScript files
|
|
507
|
+
- Use "docs/**" to find all files in the docs directory
|
|
508
|
+
- Patterns are matched against virtual paths in the file system
|
|
509
|
+
|
|
510
|
+
Examples:
|
|
511
|
+
- "*.md" - Find all markdown files in the root
|
|
512
|
+
- "**/*.test.ts" - Find all test files recursively
|
|
513
|
+
- "src/**/*.ts" - Find all TypeScript files in src directory
|
|
514
|
+
`,
|
|
515
|
+
schema: z.object({
|
|
516
|
+
pattern: z.string().describe("Glob pattern to match files against"),
|
|
517
|
+
root: z.string().optional().describe("Optional root directory to search from")
|
|
518
|
+
}),
|
|
519
|
+
strict: true
|
|
520
|
+
};
|
|
521
|
+
var grepTool = {
|
|
522
|
+
name: "Grep",
|
|
523
|
+
description: `Search file contents for a pattern within the available file system.
|
|
524
|
+
|
|
525
|
+
Usage:
|
|
526
|
+
- Searches for a regex pattern across file contents
|
|
527
|
+
- Returns matching lines with file paths and line numbers
|
|
528
|
+
- Can filter by file patterns and limit results
|
|
529
|
+
|
|
530
|
+
Examples:
|
|
531
|
+
- Search for "TODO" in all files
|
|
532
|
+
- Search for function definitions with "function.*handleClick"
|
|
533
|
+
- Search case-insensitively with ignoreCase: true
|
|
534
|
+
`,
|
|
535
|
+
schema: z.object({
|
|
536
|
+
pattern: z.string().describe("Regex pattern to search for in file contents"),
|
|
537
|
+
ignoreCase: z.boolean().optional().describe("Case-insensitive search (default: false)"),
|
|
538
|
+
maxMatches: z.number().optional().describe("Maximum number of matches to return (default: 50)"),
|
|
539
|
+
includePatterns: z.array(z.string()).optional().describe("Glob patterns to include (e.g., ['*.ts', '*.js'])"),
|
|
540
|
+
excludePatterns: z.array(z.string()).optional().describe("Glob patterns to exclude (e.g., ['*.test.ts'])"),
|
|
541
|
+
contextLines: z.number().optional().describe("Number of context lines to show around matches")
|
|
542
|
+
}),
|
|
543
|
+
strict: true
|
|
544
|
+
};
|
|
545
|
+
var readTool = {
|
|
546
|
+
name: "FileRead",
|
|
547
|
+
description: `Read file contents with optional pagination.
|
|
548
|
+
|
|
549
|
+
Usage:
|
|
550
|
+
- Provide the virtual path to the file you want to read
|
|
551
|
+
- Supports text files, images, and PDFs
|
|
552
|
+
- For large files, use offset and limit to read specific portions
|
|
553
|
+
|
|
554
|
+
The tool returns the file content in an appropriate format:
|
|
555
|
+
- Text files: Plain text content
|
|
556
|
+
- Images: Base64-encoded image data
|
|
557
|
+
- PDFs: Extracted text content
|
|
558
|
+
`,
|
|
559
|
+
schema: z.object({
|
|
560
|
+
path: z.string().describe("Virtual path to the file to read"),
|
|
561
|
+
offset: z.number().optional().describe(
|
|
562
|
+
"Line number to start reading from (1-indexed, for text files)"
|
|
563
|
+
),
|
|
564
|
+
limit: z.number().optional().describe("Maximum number of lines to read (for text files)")
|
|
565
|
+
}),
|
|
566
|
+
strict: true
|
|
567
|
+
};
|
|
568
|
+
var writeTool = {
|
|
569
|
+
name: "FileWrite",
|
|
570
|
+
description: `Create or overwrite a file with new content.
|
|
571
|
+
|
|
572
|
+
Usage:
|
|
573
|
+
- Provide the absolute virtual path to the file
|
|
574
|
+
- The file will be created if it doesn't exist
|
|
575
|
+
- If the file exists, it will be completely overwritten
|
|
576
|
+
|
|
577
|
+
IMPORTANT:
|
|
578
|
+
- You must read the file first (in this session) before writing to it
|
|
579
|
+
- This is an atomic write operation - the entire file is replaced
|
|
580
|
+
- Path must be absolute (e.g., "/docs/readme.md", not "docs/readme.md")
|
|
581
|
+
`,
|
|
582
|
+
schema: z.object({
|
|
583
|
+
file_path: z.string().describe("The absolute virtual path to the file to write"),
|
|
584
|
+
content: z.string().describe("The content to write to the file")
|
|
585
|
+
}),
|
|
586
|
+
strict: true
|
|
587
|
+
};
|
|
588
|
+
var editTool = {
|
|
589
|
+
name: "FileEdit",
|
|
590
|
+
description: `Edit specific sections of a file by replacing text.
|
|
591
|
+
|
|
592
|
+
Usage:
|
|
593
|
+
- Provide the exact text to find and replace
|
|
594
|
+
- The old_string must match exactly (whitespace-sensitive)
|
|
595
|
+
- By default, only replaces the first occurrence
|
|
596
|
+
- Use replace_all: true to replace all occurrences
|
|
597
|
+
|
|
598
|
+
IMPORTANT:
|
|
599
|
+
- You must read the file first (in this session) before editing it
|
|
600
|
+
- old_string must be unique in the file (unless using replace_all)
|
|
601
|
+
- The operation fails if old_string is not found
|
|
602
|
+
- old_string and new_string must be different
|
|
603
|
+
`,
|
|
604
|
+
schema: z.object({
|
|
605
|
+
file_path: z.string().describe("The absolute virtual path to the file to modify"),
|
|
606
|
+
old_string: z.string().describe("The exact text to replace"),
|
|
607
|
+
new_string: z.string().describe(
|
|
608
|
+
"The text to replace it with (must be different from old_string)"
|
|
609
|
+
),
|
|
610
|
+
replace_all: z.boolean().optional().describe(
|
|
611
|
+
"If true, replace all occurrences of old_string (default: false)"
|
|
612
|
+
)
|
|
613
|
+
}),
|
|
614
|
+
strict: true
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
// src/lib/filesystem/types.ts
|
|
618
|
+
function fileContentToMessageContent(content) {
|
|
619
|
+
switch (content.type) {
|
|
620
|
+
case "text":
|
|
621
|
+
return [{ type: "text", text: content.content }];
|
|
622
|
+
case "image":
|
|
623
|
+
return [
|
|
624
|
+
{
|
|
625
|
+
type: "image_url",
|
|
626
|
+
image_url: {
|
|
627
|
+
url: `data:${content.mimeType};base64,${content.data}`
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
];
|
|
631
|
+
case "pdf":
|
|
632
|
+
return [{ type: "text", text: content.content }];
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
var DEFAULT_HEADER = "Available files and directories:";
|
|
636
|
+
var DEFAULT_DESCRIPTION = "You have access to the following files. Use the Read, Glob, and Grep tools to explore them.";
|
|
637
|
+
function isExcluded(path, excludePatterns) {
|
|
638
|
+
if (!excludePatterns || excludePatterns.length === 0) {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
return excludePatterns.some((pattern) => minimatch(path, pattern));
|
|
642
|
+
}
|
|
643
|
+
function renderNode(node, options, depth, indent) {
|
|
644
|
+
const lines = [];
|
|
645
|
+
if (options.maxDepth !== void 0 && depth > options.maxDepth) {
|
|
646
|
+
return lines;
|
|
647
|
+
}
|
|
648
|
+
if (isExcluded(node.path, options.excludePatterns)) {
|
|
649
|
+
return lines;
|
|
650
|
+
}
|
|
651
|
+
const parts = node.path.split("/");
|
|
652
|
+
const name = parts[parts.length - 1] || node.path;
|
|
653
|
+
let line = indent;
|
|
654
|
+
if (node.type === "directory") {
|
|
655
|
+
line += `${name}/`;
|
|
656
|
+
} else {
|
|
657
|
+
line += name;
|
|
658
|
+
}
|
|
659
|
+
if (options.showMimeTypes && node.mimeType) {
|
|
660
|
+
line += ` [${node.mimeType}]`;
|
|
661
|
+
}
|
|
662
|
+
if (options.showDescriptions !== false && node.description) {
|
|
663
|
+
line += ` - ${node.description}`;
|
|
664
|
+
}
|
|
665
|
+
lines.push(line);
|
|
666
|
+
if (node.type === "directory" && node.children) {
|
|
667
|
+
const childIndent = indent + " ";
|
|
668
|
+
for (const child of node.children) {
|
|
669
|
+
lines.push(...renderNode(child, options, depth + 1, childIndent));
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return lines;
|
|
673
|
+
}
|
|
674
|
+
function buildFileTreePrompt(nodes, options = {}) {
|
|
675
|
+
const header = options.headerText ?? DEFAULT_HEADER;
|
|
676
|
+
const description = options.descriptionText ?? DEFAULT_DESCRIPTION;
|
|
677
|
+
const lines = [];
|
|
678
|
+
for (const node of nodes) {
|
|
679
|
+
lines.push(...renderNode(node, options, 0, ""));
|
|
680
|
+
}
|
|
681
|
+
if (lines.length === 0) {
|
|
682
|
+
return `<file_system>
|
|
683
|
+
${header}
|
|
684
|
+
|
|
685
|
+
${description}
|
|
686
|
+
|
|
687
|
+
(no files available)
|
|
688
|
+
</file_system>`;
|
|
689
|
+
}
|
|
690
|
+
return `<file_system>
|
|
691
|
+
${header}
|
|
692
|
+
|
|
693
|
+
${lines.join("\n")}
|
|
694
|
+
</file_system>`;
|
|
695
|
+
}
|
|
696
|
+
function flattenFileTree(nodes) {
|
|
697
|
+
const paths = [];
|
|
698
|
+
function traverse(node) {
|
|
699
|
+
if (node.type === "file") {
|
|
700
|
+
paths.push(node.path);
|
|
701
|
+
}
|
|
702
|
+
if (node.children) {
|
|
703
|
+
for (const child of node.children) {
|
|
704
|
+
traverse(child);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
for (const node of nodes) {
|
|
709
|
+
traverse(node);
|
|
710
|
+
}
|
|
711
|
+
return paths;
|
|
712
|
+
}
|
|
713
|
+
function isPathInScope(path, scopedNodes) {
|
|
714
|
+
const allowedPaths = flattenFileTree(scopedNodes);
|
|
715
|
+
return allowedPaths.includes(path);
|
|
716
|
+
}
|
|
717
|
+
function findNodeByPath(path, nodes) {
|
|
718
|
+
for (const node of nodes) {
|
|
719
|
+
if (node.path === path) {
|
|
720
|
+
return node;
|
|
721
|
+
}
|
|
722
|
+
if (node.children) {
|
|
723
|
+
const found = findNodeByPath(path, node.children);
|
|
724
|
+
if (found) {
|
|
725
|
+
return found;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return void 0;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
export { AGENT_HANDLER_NAMES, askUserQuestionTool, buildFileTreePrompt, createAgentStateManager, createPromptManager, createSession, createTaskHandler, createTaskTool, createToolRegistry, createToolRouter, editTool, fileContentToMessageContent, findNodeByPath, flattenFileTree, globTool, grepTool, hasNoOtherToolCalls, hasTaskTool, isPathInScope, isTerminalStatus, readTool, withSubagentSupport, writeTool };
|
|
733
|
+
//# sourceMappingURL=workflow.mjs.map
|
|
734
|
+
//# sourceMappingURL=workflow.mjs.map
|