zeitlich 0.2.3 → 0.2.4

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/dist/index.cjs CHANGED
@@ -14,38 +14,38 @@ var z3__default = /*#__PURE__*/_interopDefault(z3);
14
14
  var crypto__default = /*#__PURE__*/_interopDefault(crypto);
15
15
 
16
16
  // src/lib/session.ts
17
- var TASK_TOOL = "Task";
18
- function buildTaskDescription(subagents) {
17
+ var SUBAGENT_TOOL = "Subagent";
18
+ function buildSubagentDescription(subagents) {
19
19
  const subagentList = subagents.map((s) => `- **${s.name}**: ${s.description}`).join("\n");
20
- return `Launch a new agent to handle complex, multi-step tasks autonomously.
20
+ return `Launch a new agent to handle complex tasks autonomously.
21
21
 
22
- The ${TASK_TOOL} tool launches specialized agents (subprocesses) that autonomously handle complex tasks. Each agent type has specific capabilities and tools available to it.
22
+ The ${SUBAGENT_TOOL} tool launches specialized agents (subprocesses) that autonomously handle complex tasks. Each agent type has specific capabilities and tools available to it.
23
23
 
24
24
  Available agent types:
25
25
 
26
26
  ${subagentList}
27
27
 
28
- When using the ${TASK_TOOL} tool, you must specify a subagent parameter to select which agent type to use.
28
+ When using the ${SUBAGENT_TOOL} tool, you must specify a subagent parameter to select which agent type to use.
29
29
 
30
30
  Usage notes:
31
31
 
32
32
  - Always include a short description (3-5 words) summarizing what the agent will do
33
33
  - Launch multiple agents concurrently whenever possible, to maximize performance; to do that, use a single message with multiple tool uses
34
- - 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.
34
+ - When the agent is done, it will return a single message back to you.
35
35
  - Each invocation starts fresh - provide a detailed task description with all necessary context.
36
36
  - Provide clear, detailed prompts so the agent can work autonomously and return exactly the information you need.
37
37
  - The agent's outputs should generally be trusted
38
38
  - Clearly tell the agent what type of work you expect since it is not aware of the user's intent
39
39
  - 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.`;
40
40
  }
41
- function createTaskTool(subagents) {
41
+ function createSubagentTool(subagents) {
42
42
  if (subagents.length === 0) {
43
43
  throw new Error("createTaskTool requires at least one subagent");
44
44
  }
45
45
  const names = subagents.map((s) => s.name);
46
46
  return {
47
- name: TASK_TOOL,
48
- description: buildTaskDescription(subagents),
47
+ name: SUBAGENT_TOOL,
48
+ description: buildSubagentDescription(subagents),
49
49
  schema: z3__default.default.object({
50
50
  subagent: z3__default.default.enum(names).describe("The type of subagent to launch"),
51
51
  description: z3__default.default.string().describe("A short (3-5 word) description of the task"),
@@ -53,7 +53,7 @@ function createTaskTool(subagents) {
53
53
  })
54
54
  };
55
55
  }
56
- function createTaskHandler(subagents) {
56
+ function createSubagentHandler(subagents) {
57
57
  const { workflowId: parentWorkflowId, taskQueue: parentTaskQueue } = workflow.workflowInfo();
58
58
  return async (args) => {
59
59
  const config = subagents.find((s) => s.name === args.subagent);
@@ -72,15 +72,11 @@ function createTaskHandler(subagents) {
72
72
  args: [input],
73
73
  taskQueue: config.taskQueue ?? parentTaskQueue
74
74
  };
75
- const childResult = typeof config.workflow === "string" ? await workflow.executeChild(config.workflow, childOpts) : await workflow.executeChild(config.workflow, childOpts);
76
- const validated = config.resultSchema ? config.resultSchema.parse(childResult) : childResult;
77
- const toolResponse = typeof validated === "string" ? validated : JSON.stringify(validated, null, 2);
75
+ const { toolResponse, data } = typeof config.workflow === "string" ? await workflow.executeChild(config.workflow, childOpts) : await workflow.executeChild(config.workflow, childOpts);
76
+ const validated = config.resultSchema ? config.resultSchema.parse(data) : null;
78
77
  return {
79
78
  toolResponse,
80
- data: {
81
- result: validated,
82
- childWorkflowId
83
- }
79
+ data: validated
84
80
  };
85
81
  };
86
82
  }
@@ -99,9 +95,9 @@ function createToolRouter(options) {
99
95
  if (s.hooks) subagentHooksMap.set(s.name, s.hooks);
100
96
  }
101
97
  const resolveSubagentName = (args) => args.subagent;
102
- toolMap.set("Task", {
103
- ...createTaskTool(options.subagents),
104
- handler: createTaskHandler(options.subagents),
98
+ toolMap.set("Subagent", {
99
+ ...createSubagentTool(options.subagents),
100
+ handler: createSubagentHandler(options.subagents),
105
101
  ...subagentHooksMap.size > 0 && {
106
102
  hooks: {
107
103
  onPreToolUse: async (ctx) => {
@@ -452,22 +448,16 @@ var createSession = async ({
452
448
  while (stateManager.isRunning() && !stateManager.isTerminal() && stateManager.getTurns() < maxTurns) {
453
449
  stateManager.incrementTurns();
454
450
  const currentTurn = stateManager.getTurns();
455
- const { message, stopReason } = await runAgent({
451
+ const { message, rawToolCalls } = await runAgent({
456
452
  threadId,
457
453
  agentName,
458
454
  metadata
459
455
  });
460
- if (stopReason === "end_turn") {
461
- stateManager.complete();
462
- exitReason = "completed";
463
- return message;
464
- }
465
- if (!toolRouter.hasTools()) {
456
+ if (!toolRouter.hasTools() || rawToolCalls.length === 0) {
466
457
  stateManager.complete();
467
458
  exitReason = "completed";
468
459
  return message;
469
460
  }
470
- const rawToolCalls = await threadOps.parseToolCalls(message);
471
461
  const parsedToolCalls = [];
472
462
  for (const tc of rawToolCalls) {
473
463
  try {
@@ -520,8 +510,7 @@ function proxyDefaultThreadOps(options) {
520
510
  return {
521
511
  initializeThread: activities.initializeThread,
522
512
  appendHumanMessage: activities.appendHumanMessage,
523
- appendToolResult: activities.appendToolResult,
524
- parseToolCalls: activities.parseToolCalls
513
+ appendToolResult: activities.appendToolResult
525
514
  };
526
515
  }
527
516
 
@@ -1100,6 +1089,8 @@ function createThreadManager(config) {
1100
1089
  };
1101
1090
  return Object.assign(base, helpers);
1102
1091
  }
1092
+
1093
+ // src/activities.ts
1103
1094
  function createSharedActivities(redis) {
1104
1095
  return {
1105
1096
  async appendToolResult(config) {
@@ -1118,15 +1109,6 @@ function createSharedActivities(redis) {
1118
1109
  async appendHumanMessage(threadId, content) {
1119
1110
  const thread = createThreadManager({ redis, threadId });
1120
1111
  await thread.appendHumanMessage(content);
1121
- },
1122
- async parseToolCalls(storedMessage) {
1123
- const message = messages.mapStoredMessageToChatMessage(storedMessage);
1124
- const toolCalls = message.tool_calls ?? [];
1125
- return toolCalls.map((toolCall) => ({
1126
- id: toolCall.id,
1127
- name: toolCall.name,
1128
- args: toolCall.args
1129
- }));
1130
1112
  }
1131
1113
  };
1132
1114
  }
@@ -1164,9 +1146,14 @@ async function invokeModel({
1164
1146
  }
1165
1147
  );
1166
1148
  await thread.append([response.toDict()]);
1149
+ const toolCalls = response.tool_calls ?? [];
1167
1150
  return {
1168
1151
  message: response.toDict(),
1169
- stopReason: response.response_metadata?.stop_reason ?? null,
1152
+ rawToolCalls: toolCalls.map((tc) => ({
1153
+ id: tc.id,
1154
+ name: tc.name,
1155
+ args: tc.args
1156
+ })),
1170
1157
  usage: {
1171
1158
  input_tokens: response.usage_metadata?.input_tokens,
1172
1159
  output_tokens: response.usage_metadata?.output_tokens,
@@ -1368,10 +1355,10 @@ exports.createEditHandler = createEditHandler;
1368
1355
  exports.createGlobHandler = createGlobHandler;
1369
1356
  exports.createSession = createSession;
1370
1357
  exports.createSharedActivities = createSharedActivities;
1358
+ exports.createSubagentTool = createSubagentTool;
1371
1359
  exports.createTaskCreateHandler = createTaskCreateHandler;
1372
1360
  exports.createTaskGetHandler = createTaskGetHandler;
1373
1361
  exports.createTaskListHandler = createTaskListHandler;
1374
- exports.createTaskTool = createTaskTool;
1375
1362
  exports.createTaskUpdateHandler = createTaskUpdateHandler;
1376
1363
  exports.createThreadManager = createThreadManager;
1377
1364
  exports.createToolRouter = createToolRouter;