zeitlich 0.2.4 → 0.2.5
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 +70 -40
- package/dist/index.cjs +61 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -6
- package/dist/index.d.ts +7 -6
- package/dist/index.js +61 -36
- package/dist/index.js.map +1 -1
- package/dist/{workflow-PjeURKw4.d.cts → workflow-Dg5JMeOC.d.cts} +18 -4
- package/dist/{workflow-PjeURKw4.d.ts → workflow-Dg5JMeOC.d.ts} +18 -4
- package/dist/workflow.cjs +52 -35
- package/dist/workflow.cjs.map +1 -1
- package/dist/workflow.d.cts +1 -1
- package/dist/workflow.d.ts +1 -1
- package/dist/workflow.js +52 -35
- package/dist/workflow.js.map +1 -1
- package/package.json +1 -1
- package/src/activities.ts +11 -0
- package/src/index.ts +4 -4
- package/src/lib/model-invoker.ts +5 -4
- package/src/lib/session.ts +17 -4
- package/src/lib/thread-manager.ts +2 -3
- package/src/lib/tool-router.ts +40 -31
- package/src/lib/types.ts +10 -2
- package/src/tools/subagent/handler.ts +4 -5
- package/src/tools/subagent/tool.ts +3 -3
- package/src/workflow.ts +2 -2
|
@@ -44,6 +44,8 @@ interface ThreadManager extends BaseThreadManager<StoredMessage> {
|
|
|
44
44
|
createToolMessage(content: ToolMessageContent, toolCallId: string): StoredMessage;
|
|
45
45
|
/** Create and append a HumanMessage */
|
|
46
46
|
appendHumanMessage(content: string | MessageContent): Promise<void>;
|
|
47
|
+
/** Create and append a SystemMessage */
|
|
48
|
+
appendSystemMessage(content: string): Promise<void>;
|
|
47
49
|
/** Create and append a ToolMessage */
|
|
48
50
|
appendToolMessage(content: ToolMessageContent, toolCallId: string): Promise<void>;
|
|
49
51
|
/** Create and append an AIMessage */
|
|
@@ -66,7 +68,7 @@ declare function createThreadManager<T>(config: ThreadManagerConfig<T>): BaseThr
|
|
|
66
68
|
* @example
|
|
67
69
|
* const subagentTool = createSubagentTool([
|
|
68
70
|
* {
|
|
69
|
-
*
|
|
71
|
+
* agentName: "researcher",
|
|
70
72
|
* description: "Researches topics and gathers information",
|
|
71
73
|
* workflow: "researcherWorkflow",
|
|
72
74
|
* resultSchema: z.object({ findings: z.string() }),
|
|
@@ -503,6 +505,8 @@ interface ThreadOps {
|
|
|
503
505
|
appendHumanMessage(threadId: string, content: string | MessageContent): Promise<void>;
|
|
504
506
|
/** Append a tool result to the thread */
|
|
505
507
|
appendToolResult(config: ToolResultConfig): Promise<void>;
|
|
508
|
+
/** Append a system message to the thread */
|
|
509
|
+
appendSystemMessage(threadId: string, content: string): Promise<void>;
|
|
506
510
|
}
|
|
507
511
|
/**
|
|
508
512
|
* Configuration for a Zeitlich agent session
|
|
@@ -510,12 +514,16 @@ interface ThreadOps {
|
|
|
510
514
|
interface ZeitlichAgentConfig<T extends ToolMap, M = StoredMessage> {
|
|
511
515
|
threadId: string;
|
|
512
516
|
agentName: string;
|
|
517
|
+
/** Description, used for sub agents */
|
|
518
|
+
description?: string;
|
|
519
|
+
systemPrompt?: string;
|
|
513
520
|
metadata?: Record<string, unknown>;
|
|
521
|
+
appendSystemPrompt?: boolean;
|
|
514
522
|
maxTurns?: number;
|
|
515
523
|
/** Workflow-specific runAgent activity (with tools pre-bound) */
|
|
516
524
|
runAgent: RunAgentActivity<M>;
|
|
517
525
|
/** Thread operations (initialize, append messages, parse tool calls) */
|
|
518
|
-
threadOps
|
|
526
|
+
threadOps?: ThreadOps;
|
|
519
527
|
/** Tool router for processing tool calls (optional if agent has no tools) */
|
|
520
528
|
tools?: T;
|
|
521
529
|
/** Subagent configurations */
|
|
@@ -572,9 +580,11 @@ interface ToolResultConfig {
|
|
|
572
580
|
*/
|
|
573
581
|
interface SubagentConfig<TResult extends z$1.ZodType = z$1.ZodType> {
|
|
574
582
|
/** Identifier used in Task tool's subagent parameter */
|
|
575
|
-
|
|
583
|
+
agentName: string;
|
|
576
584
|
/** Description shown to the parent agent explaining what this subagent does */
|
|
577
585
|
description: string;
|
|
586
|
+
/** Whether this subagent is available (default: true). Disabled subagents are excluded from the Subagent tool. */
|
|
587
|
+
enabled?: boolean;
|
|
578
588
|
/** Temporal workflow function or type name (used with executeChild) */
|
|
579
589
|
workflow: string | Workflow;
|
|
580
590
|
/** Optional task queue - defaults to parent's queue if not specified */
|
|
@@ -910,7 +920,7 @@ interface SessionLifecycleHooks {
|
|
|
910
920
|
/** Called when session ends */
|
|
911
921
|
onSessionEnd?: SessionEndHook;
|
|
912
922
|
}
|
|
913
|
-
declare const createSession: <T extends ToolMap, M = unknown>({ threadId, agentName, maxTurns, metadata, runAgent, threadOps, buildContextMessage, subagents, tools, processToolsInParallel, hooks, }: ZeitlichAgentConfig<T, M>) => Promise<ZeitlichSession<M>>;
|
|
923
|
+
declare const createSession: <T extends ToolMap, M = unknown>({ threadId, agentName, maxTurns, metadata, runAgent, threadOps, buildContextMessage, subagents, tools, processToolsInParallel, hooks, appendSystemPrompt, systemPrompt, }: ZeitlichAgentConfig<T, M>) => Promise<ZeitlichSession<M>>;
|
|
914
924
|
/**
|
|
915
925
|
* Proxy the default ZeitlichSharedActivities as ThreadOps<StoredMessage>.
|
|
916
926
|
* Call this in workflow code for the standard LangChain/StoredMessage setup.
|
|
@@ -947,6 +957,10 @@ interface ZeitlichSharedActivities {
|
|
|
947
957
|
* Append a human message to a thread.
|
|
948
958
|
*/
|
|
949
959
|
appendHumanMessage(threadId: string, content: string | MessageContent): Promise<void>;
|
|
960
|
+
/**
|
|
961
|
+
* Append a system message to a thread.
|
|
962
|
+
*/
|
|
963
|
+
appendSystemMessage(threadId: string, content: string): Promise<void>;
|
|
950
964
|
}
|
|
951
965
|
/**
|
|
952
966
|
* Creates shared Temporal activities for thread management
|
|
@@ -44,6 +44,8 @@ interface ThreadManager extends BaseThreadManager<StoredMessage> {
|
|
|
44
44
|
createToolMessage(content: ToolMessageContent, toolCallId: string): StoredMessage;
|
|
45
45
|
/** Create and append a HumanMessage */
|
|
46
46
|
appendHumanMessage(content: string | MessageContent): Promise<void>;
|
|
47
|
+
/** Create and append a SystemMessage */
|
|
48
|
+
appendSystemMessage(content: string): Promise<void>;
|
|
47
49
|
/** Create and append a ToolMessage */
|
|
48
50
|
appendToolMessage(content: ToolMessageContent, toolCallId: string): Promise<void>;
|
|
49
51
|
/** Create and append an AIMessage */
|
|
@@ -66,7 +68,7 @@ declare function createThreadManager<T>(config: ThreadManagerConfig<T>): BaseThr
|
|
|
66
68
|
* @example
|
|
67
69
|
* const subagentTool = createSubagentTool([
|
|
68
70
|
* {
|
|
69
|
-
*
|
|
71
|
+
* agentName: "researcher",
|
|
70
72
|
* description: "Researches topics and gathers information",
|
|
71
73
|
* workflow: "researcherWorkflow",
|
|
72
74
|
* resultSchema: z.object({ findings: z.string() }),
|
|
@@ -503,6 +505,8 @@ interface ThreadOps {
|
|
|
503
505
|
appendHumanMessage(threadId: string, content: string | MessageContent): Promise<void>;
|
|
504
506
|
/** Append a tool result to the thread */
|
|
505
507
|
appendToolResult(config: ToolResultConfig): Promise<void>;
|
|
508
|
+
/** Append a system message to the thread */
|
|
509
|
+
appendSystemMessage(threadId: string, content: string): Promise<void>;
|
|
506
510
|
}
|
|
507
511
|
/**
|
|
508
512
|
* Configuration for a Zeitlich agent session
|
|
@@ -510,12 +514,16 @@ interface ThreadOps {
|
|
|
510
514
|
interface ZeitlichAgentConfig<T extends ToolMap, M = StoredMessage> {
|
|
511
515
|
threadId: string;
|
|
512
516
|
agentName: string;
|
|
517
|
+
/** Description, used for sub agents */
|
|
518
|
+
description?: string;
|
|
519
|
+
systemPrompt?: string;
|
|
513
520
|
metadata?: Record<string, unknown>;
|
|
521
|
+
appendSystemPrompt?: boolean;
|
|
514
522
|
maxTurns?: number;
|
|
515
523
|
/** Workflow-specific runAgent activity (with tools pre-bound) */
|
|
516
524
|
runAgent: RunAgentActivity<M>;
|
|
517
525
|
/** Thread operations (initialize, append messages, parse tool calls) */
|
|
518
|
-
threadOps
|
|
526
|
+
threadOps?: ThreadOps;
|
|
519
527
|
/** Tool router for processing tool calls (optional if agent has no tools) */
|
|
520
528
|
tools?: T;
|
|
521
529
|
/** Subagent configurations */
|
|
@@ -572,9 +580,11 @@ interface ToolResultConfig {
|
|
|
572
580
|
*/
|
|
573
581
|
interface SubagentConfig<TResult extends z$1.ZodType = z$1.ZodType> {
|
|
574
582
|
/** Identifier used in Task tool's subagent parameter */
|
|
575
|
-
|
|
583
|
+
agentName: string;
|
|
576
584
|
/** Description shown to the parent agent explaining what this subagent does */
|
|
577
585
|
description: string;
|
|
586
|
+
/** Whether this subagent is available (default: true). Disabled subagents are excluded from the Subagent tool. */
|
|
587
|
+
enabled?: boolean;
|
|
578
588
|
/** Temporal workflow function or type name (used with executeChild) */
|
|
579
589
|
workflow: string | Workflow;
|
|
580
590
|
/** Optional task queue - defaults to parent's queue if not specified */
|
|
@@ -910,7 +920,7 @@ interface SessionLifecycleHooks {
|
|
|
910
920
|
/** Called when session ends */
|
|
911
921
|
onSessionEnd?: SessionEndHook;
|
|
912
922
|
}
|
|
913
|
-
declare const createSession: <T extends ToolMap, M = unknown>({ threadId, agentName, maxTurns, metadata, runAgent, threadOps, buildContextMessage, subagents, tools, processToolsInParallel, hooks, }: ZeitlichAgentConfig<T, M>) => Promise<ZeitlichSession<M>>;
|
|
923
|
+
declare const createSession: <T extends ToolMap, M = unknown>({ threadId, agentName, maxTurns, metadata, runAgent, threadOps, buildContextMessage, subagents, tools, processToolsInParallel, hooks, appendSystemPrompt, systemPrompt, }: ZeitlichAgentConfig<T, M>) => Promise<ZeitlichSession<M>>;
|
|
914
924
|
/**
|
|
915
925
|
* Proxy the default ZeitlichSharedActivities as ThreadOps<StoredMessage>.
|
|
916
926
|
* Call this in workflow code for the standard LangChain/StoredMessage setup.
|
|
@@ -947,6 +957,10 @@ interface ZeitlichSharedActivities {
|
|
|
947
957
|
* Append a human message to a thread.
|
|
948
958
|
*/
|
|
949
959
|
appendHumanMessage(threadId: string, content: string | MessageContent): Promise<void>;
|
|
960
|
+
/**
|
|
961
|
+
* Append a system message to a thread.
|
|
962
|
+
*/
|
|
963
|
+
appendSystemMessage(threadId: string, content: string): Promise<void>;
|
|
950
964
|
}
|
|
951
965
|
/**
|
|
952
966
|
* Creates shared Temporal activities for thread management
|
package/dist/workflow.cjs
CHANGED
|
@@ -10,7 +10,7 @@ var z3__default = /*#__PURE__*/_interopDefault(z3);
|
|
|
10
10
|
// src/lib/session.ts
|
|
11
11
|
var SUBAGENT_TOOL = "Subagent";
|
|
12
12
|
function buildSubagentDescription(subagents) {
|
|
13
|
-
const subagentList = subagents.map((s) => `- **${s.
|
|
13
|
+
const subagentList = subagents.map((s) => `- **${s.agentName}**: ${s.description}`).join("\n");
|
|
14
14
|
return `Launch a new agent to handle complex tasks autonomously.
|
|
15
15
|
|
|
16
16
|
The ${SUBAGENT_TOOL} tool launches specialized agents (subprocesses) that autonomously handle complex tasks. Each agent type has specific capabilities and tools available to it.
|
|
@@ -36,7 +36,7 @@ function createSubagentTool(subagents) {
|
|
|
36
36
|
if (subagents.length === 0) {
|
|
37
37
|
throw new Error("createTaskTool requires at least one subagent");
|
|
38
38
|
}
|
|
39
|
-
const names = subagents.map((s) => s.
|
|
39
|
+
const names = subagents.map((s) => s.agentName);
|
|
40
40
|
return {
|
|
41
41
|
name: SUBAGENT_TOOL,
|
|
42
42
|
description: buildSubagentDescription(subagents),
|
|
@@ -48,15 +48,15 @@ function createSubagentTool(subagents) {
|
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
function createSubagentHandler(subagents) {
|
|
51
|
-
const {
|
|
51
|
+
const { taskQueue: parentTaskQueue } = workflow.workflowInfo();
|
|
52
52
|
return async (args) => {
|
|
53
|
-
const config = subagents.find((s) => s.
|
|
53
|
+
const config = subagents.find((s) => s.agentName === args.subagent);
|
|
54
54
|
if (!config) {
|
|
55
55
|
throw new Error(
|
|
56
|
-
`Unknown subagent: ${args.subagent}. Available: ${subagents.map((s) => s.
|
|
56
|
+
`Unknown subagent: ${args.subagent}. Available: ${subagents.map((s) => s.agentName).join(", ")}`
|
|
57
57
|
);
|
|
58
58
|
}
|
|
59
|
-
const childWorkflowId = `${
|
|
59
|
+
const childWorkflowId = `${args.subagent}-${workflow.uuid4()}`;
|
|
60
60
|
const input = {
|
|
61
61
|
prompt: args.prompt,
|
|
62
62
|
...config.context && { context: config.context }
|
|
@@ -84,31 +84,36 @@ function createToolRouter(options) {
|
|
|
84
84
|
}
|
|
85
85
|
const isEnabled = (tool) => tool.enabled !== false;
|
|
86
86
|
if (options.subagents) {
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
87
|
+
const enabledSubagents = options.subagents.filter(
|
|
88
|
+
(s) => s.enabled !== false
|
|
89
|
+
);
|
|
90
|
+
if (enabledSubagents.length > 0) {
|
|
91
|
+
const subagentHooksMap = /* @__PURE__ */ new Map();
|
|
92
|
+
for (const s of enabledSubagents) {
|
|
93
|
+
if (s.hooks) subagentHooksMap.set(s.agentName, s.hooks);
|
|
94
|
+
}
|
|
95
|
+
const resolveSubagentName = (args) => args.subagent;
|
|
96
|
+
toolMap.set("Subagent", {
|
|
97
|
+
...createSubagentTool(enabledSubagents),
|
|
98
|
+
handler: createSubagentHandler(enabledSubagents),
|
|
99
|
+
...subagentHooksMap.size > 0 && {
|
|
100
|
+
hooks: {
|
|
101
|
+
onPreToolUse: async (ctx) => {
|
|
102
|
+
const hooks = subagentHooksMap.get(resolveSubagentName(ctx.args));
|
|
103
|
+
return hooks?.onPreExecution?.(ctx) ?? {};
|
|
104
|
+
},
|
|
105
|
+
onPostToolUse: async (ctx) => {
|
|
106
|
+
const hooks = subagentHooksMap.get(resolveSubagentName(ctx.args));
|
|
107
|
+
await hooks?.onPostExecution?.(ctx);
|
|
108
|
+
},
|
|
109
|
+
onPostToolUseFailure: async (ctx) => {
|
|
110
|
+
const hooks = subagentHooksMap.get(resolveSubagentName(ctx.args));
|
|
111
|
+
return hooks?.onExecutionFailure?.(ctx) ?? {};
|
|
112
|
+
}
|
|
108
113
|
}
|
|
109
114
|
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
112
117
|
}
|
|
113
118
|
async function processToolCall(toolCall, turn, handlerContext) {
|
|
114
119
|
const startTime = Date.now();
|
|
@@ -389,11 +394,19 @@ var createSession = async ({
|
|
|
389
394
|
subagents,
|
|
390
395
|
tools = {},
|
|
391
396
|
processToolsInParallel = true,
|
|
392
|
-
hooks = {}
|
|
397
|
+
hooks = {},
|
|
398
|
+
appendSystemPrompt = true,
|
|
399
|
+
systemPrompt
|
|
393
400
|
}) => {
|
|
401
|
+
const {
|
|
402
|
+
appendToolResult,
|
|
403
|
+
appendHumanMessage,
|
|
404
|
+
initializeThread,
|
|
405
|
+
appendSystemMessage
|
|
406
|
+
} = threadOps ?? proxyDefaultThreadOps();
|
|
394
407
|
const toolRouter = createToolRouter({
|
|
395
408
|
tools,
|
|
396
|
-
appendToolResult
|
|
409
|
+
appendToolResult,
|
|
397
410
|
threadId,
|
|
398
411
|
hooks,
|
|
399
412
|
subagents,
|
|
@@ -420,8 +433,11 @@ var createSession = async ({
|
|
|
420
433
|
});
|
|
421
434
|
}
|
|
422
435
|
stateManager.setTools(toolRouter.getToolDefinitions());
|
|
423
|
-
await
|
|
424
|
-
|
|
436
|
+
await initializeThread(threadId);
|
|
437
|
+
if (appendSystemPrompt && systemPrompt && systemPrompt.trim() !== "") {
|
|
438
|
+
await appendSystemMessage(threadId, systemPrompt);
|
|
439
|
+
}
|
|
440
|
+
await appendHumanMessage(threadId, await buildContextMessage());
|
|
425
441
|
let exitReason = "completed";
|
|
426
442
|
try {
|
|
427
443
|
while (stateManager.isRunning() && !stateManager.isTerminal() && stateManager.getTurns() < maxTurns) {
|
|
@@ -442,7 +458,7 @@ var createSession = async ({
|
|
|
442
458
|
try {
|
|
443
459
|
parsedToolCalls.push(toolRouter.parseToolCall(tc));
|
|
444
460
|
} catch (error) {
|
|
445
|
-
await
|
|
461
|
+
await appendToolResult({
|
|
446
462
|
threadId,
|
|
447
463
|
toolCallId: tc.id ?? "",
|
|
448
464
|
toolName: tc.name,
|
|
@@ -489,7 +505,8 @@ function proxyDefaultThreadOps(options) {
|
|
|
489
505
|
return {
|
|
490
506
|
initializeThread: activities.initializeThread,
|
|
491
507
|
appendHumanMessage: activities.appendHumanMessage,
|
|
492
|
-
appendToolResult: activities.appendToolResult
|
|
508
|
+
appendToolResult: activities.appendToolResult,
|
|
509
|
+
appendSystemMessage: activities.appendSystemMessage
|
|
493
510
|
};
|
|
494
511
|
}
|
|
495
512
|
|