zeitlich 0.2.7 → 0.2.8
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 +68 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +68 -22
- package/dist/index.js.map +1 -1
- package/dist/{workflow-CyYHDbrr.d.cts → workflow-BdAuMMjY.d.cts} +44 -12
- package/dist/{workflow-CyYHDbrr.d.ts → workflow-BdAuMMjY.d.ts} +44 -12
- package/dist/workflow.cjs +63 -19
- 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 +63 -19
- package/dist/workflow.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/model-invoker.ts +7 -3
- package/src/lib/session.ts +44 -10
- package/src/lib/state-manager.ts +56 -0
- package/src/lib/tool-router.ts +12 -10
- package/src/lib/types.ts +18 -8
- package/src/tools/subagent/handler.ts +2 -1
package/dist/index.cjs
CHANGED
|
@@ -72,11 +72,12 @@ function createSubagentHandler(subagents) {
|
|
|
72
72
|
args: [input],
|
|
73
73
|
taskQueue: config.taskQueue ?? parentTaskQueue
|
|
74
74
|
};
|
|
75
|
-
const { toolResponse, data } = typeof config.workflow === "string" ? await workflow.executeChild(config.workflow, childOpts) : await workflow.executeChild(config.workflow, childOpts);
|
|
75
|
+
const { toolResponse, data, usage } = typeof config.workflow === "string" ? await workflow.executeChild(config.workflow, childOpts) : await workflow.executeChild(config.workflow, childOpts);
|
|
76
76
|
const validated = config.resultSchema ? config.resultSchema.parse(data) : null;
|
|
77
77
|
return {
|
|
78
78
|
toolResponse,
|
|
79
|
-
data: validated
|
|
79
|
+
data: validated,
|
|
80
|
+
...usage && { usage }
|
|
80
81
|
};
|
|
81
82
|
};
|
|
82
83
|
}
|
|
@@ -88,20 +89,17 @@ function createToolRouter(options) {
|
|
|
88
89
|
for (const [_key, tool] of Object.entries(options.tools)) {
|
|
89
90
|
toolMap.set(tool.name, tool);
|
|
90
91
|
}
|
|
91
|
-
const isEnabled = (tool) => tool.enabled
|
|
92
|
+
const isEnabled = (tool) => tool.enabled?.() ?? true;
|
|
92
93
|
if (options.subagents) {
|
|
93
|
-
|
|
94
|
-
(s) => s.enabled !== false
|
|
95
|
-
);
|
|
96
|
-
if (enabledSubagents.length > 0) {
|
|
94
|
+
if (options.subagents.length > 0) {
|
|
97
95
|
const subagentHooksMap = /* @__PURE__ */ new Map();
|
|
98
|
-
for (const s of
|
|
96
|
+
for (const s of options.subagents) {
|
|
99
97
|
if (s.hooks) subagentHooksMap.set(s.agentName, s.hooks);
|
|
100
98
|
}
|
|
101
99
|
const resolveSubagentName = (args) => args.subagent;
|
|
102
100
|
toolMap.set("Subagent", {
|
|
103
|
-
...createSubagentTool(
|
|
104
|
-
handler: createSubagentHandler(
|
|
101
|
+
...createSubagentTool(options.subagents),
|
|
102
|
+
handler: createSubagentHandler(options.subagents),
|
|
105
103
|
...subagentHooksMap.size > 0 && {
|
|
106
104
|
hooks: {
|
|
107
105
|
onPreToolUse: async (ctx) => {
|
|
@@ -411,6 +409,7 @@ function hasNoOtherToolCalls(toolCalls, excludeName) {
|
|
|
411
409
|
var createSession = async ({
|
|
412
410
|
threadId,
|
|
413
411
|
agentName,
|
|
412
|
+
description,
|
|
414
413
|
maxTurns = 50,
|
|
415
414
|
metadata = {},
|
|
416
415
|
runAgent,
|
|
@@ -450,7 +449,9 @@ var createSession = async ({
|
|
|
450
449
|
}
|
|
451
450
|
};
|
|
452
451
|
return {
|
|
453
|
-
runSession: async ({
|
|
452
|
+
runSession: async ({
|
|
453
|
+
stateManager
|
|
454
|
+
}) => {
|
|
454
455
|
workflow.setHandler(
|
|
455
456
|
workflow.defineUpdate(`add${agentName}Message`),
|
|
456
457
|
async (message) => {
|
|
@@ -477,7 +478,6 @@ var createSession = async ({
|
|
|
477
478
|
metadata
|
|
478
479
|
});
|
|
479
480
|
}
|
|
480
|
-
stateManager.setTools(toolRouter.getToolDefinitions());
|
|
481
481
|
await initializeThread(threadId);
|
|
482
482
|
if (appendSystemPrompt && systemPrompt && systemPrompt.trim() !== "") {
|
|
483
483
|
await appendSystemMessage(threadId, systemPrompt);
|
|
@@ -488,15 +488,25 @@ var createSession = async ({
|
|
|
488
488
|
while (stateManager.isRunning() && !stateManager.isTerminal() && stateManager.getTurns() < maxTurns) {
|
|
489
489
|
stateManager.incrementTurns();
|
|
490
490
|
const currentTurn = stateManager.getTurns();
|
|
491
|
-
|
|
491
|
+
stateManager.setTools(toolRouter.getToolDefinitions());
|
|
492
|
+
const { message, rawToolCalls, usage } = await runAgent({
|
|
492
493
|
threadId,
|
|
493
494
|
agentName,
|
|
494
|
-
metadata
|
|
495
|
+
metadata,
|
|
496
|
+
systemPrompt,
|
|
497
|
+
description
|
|
495
498
|
});
|
|
499
|
+
if (usage) {
|
|
500
|
+
stateManager.updateUsage(usage);
|
|
501
|
+
}
|
|
496
502
|
if (!toolRouter.hasTools() || rawToolCalls.length === 0) {
|
|
497
503
|
stateManager.complete();
|
|
498
504
|
exitReason = "completed";
|
|
499
|
-
return
|
|
505
|
+
return {
|
|
506
|
+
finalMessage: message,
|
|
507
|
+
exitReason,
|
|
508
|
+
usage: stateManager.getTotalUsage()
|
|
509
|
+
};
|
|
500
510
|
}
|
|
501
511
|
const parsedToolCalls = [];
|
|
502
512
|
for (const tc of rawToolCalls) {
|
|
@@ -513,9 +523,17 @@ var createSession = async ({
|
|
|
513
523
|
});
|
|
514
524
|
}
|
|
515
525
|
}
|
|
516
|
-
await toolRouter.processToolCalls(
|
|
517
|
-
|
|
518
|
-
|
|
526
|
+
const toolCallResults = await toolRouter.processToolCalls(
|
|
527
|
+
parsedToolCalls,
|
|
528
|
+
{
|
|
529
|
+
turn: currentTurn
|
|
530
|
+
}
|
|
531
|
+
);
|
|
532
|
+
for (const result of toolCallResults) {
|
|
533
|
+
if (result.usage) {
|
|
534
|
+
stateManager.updateUsage(result.usage);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
519
537
|
if (stateManager.getStatus() === "WAITING_FOR_INPUT") {
|
|
520
538
|
const conditionMet = await workflow.condition(
|
|
521
539
|
() => stateManager.getStatus() === "RUNNING",
|
|
@@ -537,7 +555,11 @@ var createSession = async ({
|
|
|
537
555
|
} finally {
|
|
538
556
|
await callSessionEnd(exitReason, stateManager.getTurns());
|
|
539
557
|
}
|
|
540
|
-
return
|
|
558
|
+
return {
|
|
559
|
+
finalMessage: null,
|
|
560
|
+
exitReason,
|
|
561
|
+
usage: stateManager.getTotalUsage()
|
|
562
|
+
};
|
|
541
563
|
}
|
|
542
564
|
};
|
|
543
565
|
};
|
|
@@ -574,6 +596,11 @@ function createAgentStateManager({
|
|
|
574
596
|
let version = initialState?.version ?? 0;
|
|
575
597
|
let turns = initialState?.turns ?? 0;
|
|
576
598
|
let tools = initialState?.tools ?? [];
|
|
599
|
+
let totalInputTokens = 0;
|
|
600
|
+
let totalOutputTokens = 0;
|
|
601
|
+
let totalCachedWriteTokens = 0;
|
|
602
|
+
let totalCachedReadTokens = 0;
|
|
603
|
+
let totalReasonTokens = 0;
|
|
577
604
|
const tasks = new Map(initialState?.tasks);
|
|
578
605
|
const {
|
|
579
606
|
status: _,
|
|
@@ -688,6 +715,23 @@ function createAgentStateManager({
|
|
|
688
715
|
version++;
|
|
689
716
|
}
|
|
690
717
|
return deleted;
|
|
718
|
+
},
|
|
719
|
+
updateUsage(usage) {
|
|
720
|
+
totalInputTokens += usage.inputTokens ?? 0;
|
|
721
|
+
totalOutputTokens += usage.outputTokens ?? 0;
|
|
722
|
+
totalCachedWriteTokens += usage.cachedWriteTokens ?? 0;
|
|
723
|
+
totalCachedReadTokens += usage.cachedReadTokens ?? 0;
|
|
724
|
+
totalReasonTokens += usage.reasonTokens ?? 0;
|
|
725
|
+
},
|
|
726
|
+
getTotalUsage() {
|
|
727
|
+
return {
|
|
728
|
+
totalInputTokens,
|
|
729
|
+
totalOutputTokens,
|
|
730
|
+
totalCachedWriteTokens,
|
|
731
|
+
totalCachedReadTokens,
|
|
732
|
+
totalReasonTokens,
|
|
733
|
+
turns
|
|
734
|
+
};
|
|
691
735
|
}
|
|
692
736
|
};
|
|
693
737
|
}
|
|
@@ -1239,9 +1283,11 @@ async function invokeModel({
|
|
|
1239
1283
|
args: tc.args
|
|
1240
1284
|
})),
|
|
1241
1285
|
usage: {
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1286
|
+
inputTokens: response.usage_metadata?.input_tokens,
|
|
1287
|
+
outputTokens: response.usage_metadata?.output_tokens,
|
|
1288
|
+
reasonTokens: response.usage_metadata?.output_token_details?.reasoning,
|
|
1289
|
+
cachedWriteTokens: response.usage_metadata?.input_token_details?.cache_creation,
|
|
1290
|
+
cachedReadTokens: response.usage_metadata?.input_token_details?.cache_read
|
|
1245
1291
|
}
|
|
1246
1292
|
};
|
|
1247
1293
|
}
|