@perstack/runtime 0.0.23 → 0.0.24
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.d.ts +62 -23
- package/dist/index.js +210 -283
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -17,118 +17,107 @@ import { z as z2 } from "zod";
|
|
|
17
17
|
import { anthropic } from "@ai-sdk/anthropic";
|
|
18
18
|
import { google } from "@ai-sdk/google";
|
|
19
19
|
import { openai } from "@ai-sdk/openai";
|
|
20
|
-
var
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
20
|
+
var anthropicModelConfigs = [
|
|
21
|
+
{
|
|
22
|
+
modelId: "claude-opus-4-20250514",
|
|
23
|
+
default: true,
|
|
24
|
+
contextWindow: 2e5
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
modelId: "claude-sonnet-4-20250514",
|
|
28
|
+
default: false,
|
|
29
|
+
contextWindow: 2e5
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
modelId: "claude-3-7-sonnet-20250219",
|
|
33
|
+
default: false,
|
|
34
|
+
contextWindow: 2e5
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
modelId: "claude-3-5-sonnet-latest",
|
|
38
|
+
default: false,
|
|
39
|
+
contextWindow: 2e5
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
modelId: "claude-3-5-haiku-latest",
|
|
43
|
+
default: false,
|
|
44
|
+
contextWindow: 2e5
|
|
45
|
+
}
|
|
34
46
|
];
|
|
35
|
-
var
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"gemini-2.0-pro-exp-02-05",
|
|
52
|
-
"gemini-2.0-flash-thinking-exp-01-21",
|
|
53
|
-
"gemini-2.0-flash-exp",
|
|
54
|
-
"gemini-2.5-pro",
|
|
55
|
-
"gemini-2.5-flash",
|
|
56
|
-
"gemini-2.5-flash-lite",
|
|
57
|
-
"gemini-2.5-pro-exp-03-25",
|
|
58
|
-
"gemini-2.5-flash-preview-04-17"
|
|
47
|
+
var googleModelConfigs = [
|
|
48
|
+
{
|
|
49
|
+
modelId: "gemini-2.5-pro",
|
|
50
|
+
default: false,
|
|
51
|
+
contextWindow: 1e6
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
modelId: "gemini-2.5-flash",
|
|
55
|
+
default: false,
|
|
56
|
+
contextWindow: 1e6
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
modelId: "gemini-2.5-flash-lite",
|
|
60
|
+
default: false,
|
|
61
|
+
contextWindow: 1e6
|
|
62
|
+
}
|
|
59
63
|
];
|
|
60
|
-
var
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
"gpt-4o",
|
|
82
|
-
"gpt-4o-2024-05-13",
|
|
83
|
-
"gpt-4o-2024-08-06",
|
|
84
|
-
"gpt-4o-2024-11-20",
|
|
85
|
-
"gpt-4o-audio-preview",
|
|
86
|
-
"gpt-4o-audio-preview-2024-10-01",
|
|
87
|
-
"gpt-4o-audio-preview-2024-12-17",
|
|
88
|
-
"gpt-4o-search-preview",
|
|
89
|
-
"gpt-4o-search-preview-2025-03-11",
|
|
90
|
-
"gpt-4o-mini-search-preview",
|
|
91
|
-
"gpt-4o-mini-search-preview-2025-03-11",
|
|
92
|
-
"gpt-4o-mini",
|
|
93
|
-
"gpt-4o-mini-2024-07-18"
|
|
94
|
-
// "gpt-4-turbo", - Legacy model
|
|
95
|
-
// "gpt-4-turbo-2024-04-09", - Legacy model
|
|
96
|
-
// "gpt-4-turbo-preview", - Legacy model
|
|
97
|
-
// "gpt-4-0125-preview", - No image input support
|
|
98
|
-
// "gpt-4-1106-preview", - No image input support
|
|
99
|
-
// "gpt-4", - No image input support
|
|
100
|
-
// "gpt-4-0613", - No image input support
|
|
101
|
-
// "gpt-3.5-turbo-0125", - Legacy model
|
|
102
|
-
// "gpt-3.5-turbo", - Legacy model
|
|
103
|
-
// "gpt-3.5-turbo-1106", - Legacy model
|
|
104
|
-
// "chatgpt-4o-latest", - No tool support
|
|
64
|
+
var openAIModelConfigs = [
|
|
65
|
+
{
|
|
66
|
+
modelId: "o4-mini",
|
|
67
|
+
default: false,
|
|
68
|
+
contextWindow: 2e5
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
modelId: "o3",
|
|
72
|
+
default: false,
|
|
73
|
+
contextWindow: 2e5
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
modelId: "o3-mini",
|
|
77
|
+
default: false,
|
|
78
|
+
contextWindow: 2e5
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
modelId: "gpt-4.1",
|
|
82
|
+
default: false,
|
|
83
|
+
contextWindow: 1e6
|
|
84
|
+
}
|
|
105
85
|
];
|
|
106
|
-
var
|
|
107
|
-
[...
|
|
108
|
-
model,
|
|
109
|
-
|
|
110
|
-
])
|
|
86
|
+
var modelConfigs = Object.fromEntries(
|
|
87
|
+
Array.from(/* @__PURE__ */ new Set([...anthropicModelConfigs, ...googleModelConfigs, ...openAIModelConfigs])).map(
|
|
88
|
+
(model) => [model.modelId, model]
|
|
89
|
+
)
|
|
111
90
|
);
|
|
112
91
|
function getDefaultModelName() {
|
|
113
|
-
const model = Object.values(
|
|
92
|
+
const model = Object.values(modelConfigs).find((model2) => model2.default);
|
|
114
93
|
if (!model) {
|
|
115
94
|
throw new Error("No default model found");
|
|
116
95
|
}
|
|
117
|
-
return model.
|
|
96
|
+
return model.modelId;
|
|
118
97
|
}
|
|
119
98
|
function getModel(modelId) {
|
|
120
99
|
const unwrappedModelId = modelId ?? getDefaultModelName();
|
|
121
|
-
if (
|
|
100
|
+
if (anthropicModelConfigs.some((model) => model.modelId === unwrappedModelId)) {
|
|
122
101
|
return anthropic(unwrappedModelId);
|
|
123
102
|
}
|
|
124
|
-
if (
|
|
103
|
+
if (googleModelConfigs.some((model) => model.modelId === unwrappedModelId)) {
|
|
125
104
|
return google(unwrappedModelId);
|
|
126
105
|
}
|
|
127
|
-
if (
|
|
106
|
+
if (openAIModelConfigs.some((model) => model.modelId === unwrappedModelId)) {
|
|
128
107
|
return openai(unwrappedModelId);
|
|
129
108
|
}
|
|
130
109
|
throw new Error(`Unsupported model: ${unwrappedModelId}`);
|
|
131
110
|
}
|
|
111
|
+
function getModelConfig(modelId) {
|
|
112
|
+
const modelConfig = modelConfigs[modelId];
|
|
113
|
+
if (!modelConfig) {
|
|
114
|
+
throw new Error(`Unsupported model: ${modelId}`);
|
|
115
|
+
}
|
|
116
|
+
return modelConfig;
|
|
117
|
+
}
|
|
118
|
+
function calculateWindowUsage(usage, contextWindow) {
|
|
119
|
+
return (usage.inputTokens + usage.cachedInputTokens + usage.outputTokens) / contextWindow;
|
|
120
|
+
}
|
|
132
121
|
|
|
133
122
|
// src/schemas/messages.ts
|
|
134
123
|
import { z } from "zod";
|
|
@@ -223,6 +212,10 @@ var MessageSchema = z.union([
|
|
|
223
212
|
]);
|
|
224
213
|
|
|
225
214
|
// src/schemas/runtime.ts
|
|
215
|
+
var defaultTemperature = 0;
|
|
216
|
+
var defaultMaxSteps = void 0;
|
|
217
|
+
var defaultMaxRetries = 5;
|
|
218
|
+
var defaultTimeout = 5 * 1e3 * 60;
|
|
226
219
|
var expertNameRegex = /^(@[a-z0-9][a-z0-9_-]*\/)?[a-z0-9][a-z0-9_-]*$/;
|
|
227
220
|
var versionRegex = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\w.-]+)?(?:\+[\w.-]+)?$/;
|
|
228
221
|
var tagNameRegex = /^[a-z0-9][a-z0-9_-]*$/;
|
|
@@ -321,11 +314,11 @@ var ExpertSchema = z2.object({
|
|
|
321
314
|
tags: z2.array(z2.string().regex(tagNameRegex).min(1)).optional().default([])
|
|
322
315
|
});
|
|
323
316
|
var UsageSchema = z2.object({
|
|
324
|
-
|
|
325
|
-
|
|
317
|
+
inputTokens: z2.number(),
|
|
318
|
+
outputTokens: z2.number(),
|
|
319
|
+
reasoningTokens: z2.number(),
|
|
326
320
|
totalTokens: z2.number(),
|
|
327
|
-
|
|
328
|
-
cacheReadInputTokens: z2.number()
|
|
321
|
+
cachedInputTokens: z2.number()
|
|
329
322
|
});
|
|
330
323
|
var CheckpointStatusSchema = z2.enum([
|
|
331
324
|
"init",
|
|
@@ -367,7 +360,9 @@ var CheckpointSchema = z2.object({
|
|
|
367
360
|
toolName: z2.string(),
|
|
368
361
|
checkpointId: z2.string()
|
|
369
362
|
}).optional(),
|
|
370
|
-
usage: UsageSchema
|
|
363
|
+
usage: UsageSchema,
|
|
364
|
+
contextWindow: z2.number(),
|
|
365
|
+
contextWindowUsage: z2.number()
|
|
371
366
|
});
|
|
372
367
|
var RunParamsSchema = z2.object({
|
|
373
368
|
setting: z2.object({
|
|
@@ -393,10 +388,10 @@ var RunParamsSchema = z2.object({
|
|
|
393
388
|
)
|
|
394
389
|
),
|
|
395
390
|
model: z2.string().optional().default(getDefaultModelName()),
|
|
396
|
-
temperature: z2.number().min(0).max(1).optional().default(
|
|
397
|
-
maxSteps: z2.number().min(1).optional(),
|
|
398
|
-
maxRetries: z2.number().min(0).optional().default(
|
|
399
|
-
timeout: z2.number().min(0).optional().default(
|
|
391
|
+
temperature: z2.number().min(0).max(1).optional().default(defaultTemperature),
|
|
392
|
+
maxSteps: z2.number().min(1).optional().default(defaultMaxSteps),
|
|
393
|
+
maxRetries: z2.number().min(0).optional().default(defaultMaxRetries),
|
|
394
|
+
timeout: z2.number().min(0).optional().default(defaultTimeout),
|
|
400
395
|
workspace: z2.string().optional(),
|
|
401
396
|
startedAt: z2.number().optional().default(Date.now()),
|
|
402
397
|
updatedAt: z2.number().optional().default(Date.now())
|
|
@@ -466,7 +461,7 @@ async function defaultStoreEvent(event) {
|
|
|
466
461
|
// package.json
|
|
467
462
|
var package_default = {
|
|
468
463
|
name: "@perstack/runtime",
|
|
469
|
-
version: "0.0.
|
|
464
|
+
version: "0.0.24",
|
|
470
465
|
description: "PerStack Runtime",
|
|
471
466
|
author: "Wintermute Technologies, Inc.",
|
|
472
467
|
license: "Apache-2.0",
|
|
@@ -527,14 +522,6 @@ async function defaultEventListener(e) {
|
|
|
527
522
|
switch (e.type) {
|
|
528
523
|
case "startRun": {
|
|
529
524
|
log(`${header(e)} Perstack@${package_default.version} started`);
|
|
530
|
-
const { inputMessages } = e;
|
|
531
|
-
for (const message of inputMessages) {
|
|
532
|
-
if (message.type === "userMessage") {
|
|
533
|
-
logUserMessage(message);
|
|
534
|
-
} else if (message.type === "toolMessage") {
|
|
535
|
-
logToolMessage(message);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
525
|
break;
|
|
539
526
|
}
|
|
540
527
|
case "startGeneration": {
|
|
@@ -542,12 +529,11 @@ async function defaultEventListener(e) {
|
|
|
542
529
|
break;
|
|
543
530
|
}
|
|
544
531
|
case "retry": {
|
|
545
|
-
logUsage(e);
|
|
546
532
|
log(`${header(e)} Retrying tool call generation`);
|
|
533
|
+
debug(e.reason);
|
|
547
534
|
break;
|
|
548
535
|
}
|
|
549
536
|
case "callTool": {
|
|
550
|
-
logUsage(e);
|
|
551
537
|
log(`${header(e)} Calling tool`);
|
|
552
538
|
if (e.toolCall.skillName === "@perstack/base") {
|
|
553
539
|
switch (e.toolCall.toolName) {
|
|
@@ -580,14 +566,12 @@ async function defaultEventListener(e) {
|
|
|
580
566
|
break;
|
|
581
567
|
}
|
|
582
568
|
case "callInteractiveTool": {
|
|
583
|
-
logUsage(e);
|
|
584
569
|
log(`${header(e)} Calling interactive tool`);
|
|
585
570
|
log(`${header(e)} Tool: ${e.toolCall.skillName}/${e.toolCall.toolName}`);
|
|
586
571
|
debug(`${header(e)} Args: ${JSON.stringify(e.toolCall.args, null, 2)}`);
|
|
587
572
|
break;
|
|
588
573
|
}
|
|
589
574
|
case "callDelegate": {
|
|
590
|
-
logUsage(e);
|
|
591
575
|
log(`${header(e)} Calling delegate`);
|
|
592
576
|
log(`${header(e)} Tool: ${e.toolCall.toolName}`);
|
|
593
577
|
debug(`${header(e)} Args: ${JSON.stringify(e.toolCall.args, null, 2)}`);
|
|
@@ -641,77 +625,46 @@ async function defaultEventListener(e) {
|
|
|
641
625
|
break;
|
|
642
626
|
}
|
|
643
627
|
case "stopRunByInteractiveTool": {
|
|
628
|
+
logUsage(e);
|
|
644
629
|
log(`${header(e)} Stopping run by interactive tool`);
|
|
645
630
|
break;
|
|
646
631
|
}
|
|
647
632
|
case "stopRunByDelegate": {
|
|
633
|
+
logUsage(e);
|
|
648
634
|
log(`${header(e)} Stopping run by delegate`);
|
|
649
635
|
break;
|
|
650
636
|
}
|
|
651
637
|
case "stopRunByExceededMaxSteps": {
|
|
638
|
+
logUsage(e);
|
|
652
639
|
log(`${header(e)} Stopping run by exceeded max steps`);
|
|
653
640
|
break;
|
|
654
641
|
}
|
|
655
642
|
case "continueToNextStep": {
|
|
643
|
+
const contextWindowUsage = e.checkpoint.contextWindowUsage * 100;
|
|
644
|
+
logUsage(e);
|
|
656
645
|
log(`${header(e)} Continuing to next step`);
|
|
646
|
+
log(`${header(e)} Context window usage: ${contextWindowUsage.toFixed(2)}%`);
|
|
657
647
|
break;
|
|
658
648
|
}
|
|
659
649
|
}
|
|
660
650
|
}
|
|
661
|
-
function logUserMessage(message) {
|
|
662
|
-
const t = (/* @__PURE__ */ new Date()).toISOString();
|
|
663
|
-
const contents = message.contents;
|
|
664
|
-
for (const content of contents) {
|
|
665
|
-
if (content.type === "textPart") {
|
|
666
|
-
log(`${t} User: ${content.text}`);
|
|
667
|
-
} else if (content.type === "imageUrlPart") {
|
|
668
|
-
log(`${t} User: ${content.url}`);
|
|
669
|
-
} else if (content.type === "imageInlinePart") {
|
|
670
|
-
log(`${t} User: Inline image`);
|
|
671
|
-
} else if (content.type === "imageBinaryPart") {
|
|
672
|
-
log(`${t} User: Binary image`);
|
|
673
|
-
} else if (content.type === "fileUrlPart") {
|
|
674
|
-
log(`${t} User: ${content.url}`);
|
|
675
|
-
} else if (content.type === "fileInlinePart") {
|
|
676
|
-
log(`${t} User: Inline file`);
|
|
677
|
-
} else if (content.type === "fileBinaryPart") {
|
|
678
|
-
log(`${t} User: Binary file`);
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
function logToolMessage(message) {
|
|
683
|
-
const t = (/* @__PURE__ */ new Date()).toISOString();
|
|
684
|
-
const contents = message.contents;
|
|
685
|
-
for (const content of contents) {
|
|
686
|
-
if (content.type === "toolResultPart") {
|
|
687
|
-
const { contents: contents2 } = content;
|
|
688
|
-
for (const content2 of contents2) {
|
|
689
|
-
if (content2.type === "textPart") {
|
|
690
|
-
log(`${t} Tool Result: ${content2.text}`);
|
|
691
|
-
} else if (content2.type === "imageInlinePart") {
|
|
692
|
-
log(`${t} Tool Result: Inline image`);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
651
|
function logUsage(e) {
|
|
699
|
-
const
|
|
700
|
-
`In: ${e.usage.
|
|
701
|
-
`
|
|
702
|
-
`
|
|
703
|
-
`
|
|
704
|
-
`Cache-
|
|
652
|
+
const usageByStep = [
|
|
653
|
+
`In: ${e.step.usage.inputTokens.toLocaleString()}`,
|
|
654
|
+
`Reasoning: ${e.step.usage.reasoningTokens.toLocaleString()}`,
|
|
655
|
+
`Out: ${e.step.usage.outputTokens.toLocaleString()}`,
|
|
656
|
+
`Total: ${e.step.usage.totalTokens.toLocaleString()}`,
|
|
657
|
+
`Cache-read: ${e.step.usage.cachedInputTokens.toLocaleString()}`
|
|
705
658
|
].join(", ");
|
|
706
|
-
log(`${header(e)} Tokens usage: ${usageByGeneration}`);
|
|
707
659
|
const usageByRun = [
|
|
708
|
-
`In: ${e.usage.
|
|
709
|
-
`
|
|
710
|
-
`
|
|
711
|
-
`
|
|
712
|
-
`Cache-
|
|
660
|
+
`In: ${e.checkpoint.usage.inputTokens.toLocaleString()}`,
|
|
661
|
+
`Reasoning: ${e.checkpoint.usage.reasoningTokens.toLocaleString()}`,
|
|
662
|
+
`Out: ${e.checkpoint.usage.outputTokens.toLocaleString()}`,
|
|
663
|
+
`Total: ${e.checkpoint.usage.totalTokens.toLocaleString()}`,
|
|
664
|
+
`Cache-read: ${e.checkpoint.usage.cachedInputTokens.toLocaleString()}`
|
|
713
665
|
].join(", ");
|
|
714
|
-
log(`${header(e)}
|
|
666
|
+
log(`${header(e)} Tokens usage by step: ${usageByStep}`);
|
|
667
|
+
log(`${header(e)} Tokens usage by run: ${usageByRun}`);
|
|
715
668
|
}
|
|
716
669
|
|
|
717
670
|
// src/events/event-emitter.ts
|
|
@@ -1152,7 +1105,7 @@ async function callingDelegateLogic({
|
|
|
1152
1105
|
step,
|
|
1153
1106
|
skillManagers
|
|
1154
1107
|
}) {
|
|
1155
|
-
if (!step
|
|
1108
|
+
if (!step.toolCall) {
|
|
1156
1109
|
throw new Error("No tool call found");
|
|
1157
1110
|
}
|
|
1158
1111
|
const { id, toolName, args } = step.toolCall;
|
|
@@ -1210,7 +1163,7 @@ async function callingToolLogic({
|
|
|
1210
1163
|
step,
|
|
1211
1164
|
skillManagers
|
|
1212
1165
|
}) {
|
|
1213
|
-
if (!step
|
|
1166
|
+
if (!step.toolCall) {
|
|
1214
1167
|
throw new Error("No tool call found");
|
|
1215
1168
|
}
|
|
1216
1169
|
const { id, skillName, toolName, args } = step.toolCall;
|
|
@@ -1476,36 +1429,29 @@ function toolResultPartToCoreToolResultPart(part) {
|
|
|
1476
1429
|
// src/usage.ts
|
|
1477
1430
|
function createEmptyUsage() {
|
|
1478
1431
|
return {
|
|
1479
|
-
|
|
1480
|
-
|
|
1432
|
+
inputTokens: 0,
|
|
1433
|
+
outputTokens: 0,
|
|
1434
|
+
reasoningTokens: 0,
|
|
1481
1435
|
totalTokens: 0,
|
|
1482
|
-
|
|
1483
|
-
cacheReadInputTokens: 0
|
|
1436
|
+
cachedInputTokens: 0
|
|
1484
1437
|
};
|
|
1485
1438
|
}
|
|
1486
1439
|
function usageFromGenerateTextResult(result) {
|
|
1487
|
-
let cacheCreationInputTokens = 0;
|
|
1488
|
-
let cacheReadInputTokens = 0;
|
|
1489
|
-
if (result.providerMetadata?.anthropic) {
|
|
1490
|
-
const anthropicMetadata = result.providerMetadata.anthropic;
|
|
1491
|
-
cacheCreationInputTokens = anthropicMetadata.cacheCreationInputTokens || 0;
|
|
1492
|
-
cacheReadInputTokens = anthropicMetadata.cacheReadInputTokens || 0;
|
|
1493
|
-
}
|
|
1494
1440
|
return {
|
|
1495
|
-
|
|
1496
|
-
|
|
1441
|
+
inputTokens: result.usage.inputTokens || 0,
|
|
1442
|
+
outputTokens: result.usage.outputTokens || 0,
|
|
1443
|
+
reasoningTokens: result.usage.reasoningTokens || 0,
|
|
1497
1444
|
totalTokens: result.usage.totalTokens || 0,
|
|
1498
|
-
|
|
1499
|
-
cacheReadInputTokens
|
|
1445
|
+
cachedInputTokens: result.usage.cachedInputTokens || 0
|
|
1500
1446
|
};
|
|
1501
1447
|
}
|
|
1502
1448
|
function sumUsage(a, b) {
|
|
1503
1449
|
return {
|
|
1504
|
-
|
|
1505
|
-
|
|
1450
|
+
inputTokens: a.inputTokens + b.inputTokens,
|
|
1451
|
+
outputTokens: a.outputTokens + b.outputTokens,
|
|
1452
|
+
reasoningTokens: a.reasoningTokens + b.reasoningTokens,
|
|
1506
1453
|
totalTokens: a.totalTokens + b.totalTokens,
|
|
1507
|
-
|
|
1508
|
-
cacheReadInputTokens: a.cacheReadInputTokens + b.cacheReadInputTokens
|
|
1454
|
+
cachedInputTokens: a.cachedInputTokens + b.cachedInputTokens
|
|
1509
1455
|
};
|
|
1510
1456
|
}
|
|
1511
1457
|
|
|
@@ -1515,7 +1461,7 @@ async function generatingRunResultLogic({
|
|
|
1515
1461
|
checkpoint,
|
|
1516
1462
|
step
|
|
1517
1463
|
}) {
|
|
1518
|
-
if (!step
|
|
1464
|
+
if (!step.toolCall || !step.toolResult) {
|
|
1519
1465
|
throw new Error("No tool call or tool result found");
|
|
1520
1466
|
}
|
|
1521
1467
|
const { id, toolName } = step.toolCall;
|
|
@@ -1543,17 +1489,11 @@ async function generatingRunResultLogic({
|
|
|
1543
1489
|
});
|
|
1544
1490
|
} catch (error) {
|
|
1545
1491
|
if (error instanceof Error) {
|
|
1492
|
+
const reason = JSON.stringify({ error: error.name, message: error.message });
|
|
1546
1493
|
return retry(setting, checkpoint, {
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
{
|
|
1551
|
-
type: "textPart",
|
|
1552
|
-
text: JSON.stringify({ error: error.name, message: error.message })
|
|
1553
|
-
}
|
|
1554
|
-
])
|
|
1555
|
-
],
|
|
1556
|
-
usage: checkpoint.usage
|
|
1494
|
+
reason,
|
|
1495
|
+
newMessages: [toolMessage, createUserMessage([{ type: "textPart", text: reason }])],
|
|
1496
|
+
usage: createEmptyUsage()
|
|
1557
1497
|
});
|
|
1558
1498
|
}
|
|
1559
1499
|
throw error;
|
|
@@ -1566,6 +1506,7 @@ async function generatingRunResultLogic({
|
|
|
1566
1506
|
...checkpoint,
|
|
1567
1507
|
messages: [...messages, ...newMessages],
|
|
1568
1508
|
usage: sumUsage(checkpoint.usage, usage),
|
|
1509
|
+
contextWindowUsage: calculateWindowUsage(usage, checkpoint.contextWindow),
|
|
1569
1510
|
status: "completed"
|
|
1570
1511
|
},
|
|
1571
1512
|
step: {
|
|
@@ -1602,16 +1543,12 @@ async function generatingToolCallLogic({
|
|
|
1602
1543
|
});
|
|
1603
1544
|
} catch (error) {
|
|
1604
1545
|
if (error instanceof Error) {
|
|
1546
|
+
console.error(error);
|
|
1547
|
+
const reason = JSON.stringify({ error: error.name, message: error.message });
|
|
1605
1548
|
return retry(setting, checkpoint, {
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
type: "textPart",
|
|
1610
|
-
text: JSON.stringify({ error: error.name, message: error.message })
|
|
1611
|
-
}
|
|
1612
|
-
])
|
|
1613
|
-
],
|
|
1614
|
-
usage: checkpoint.usage
|
|
1549
|
+
reason,
|
|
1550
|
+
newMessages: [createUserMessage([{ type: "textPart", text: reason }])],
|
|
1551
|
+
usage: createEmptyUsage()
|
|
1615
1552
|
});
|
|
1616
1553
|
}
|
|
1617
1554
|
throw error;
|
|
@@ -1620,18 +1557,13 @@ async function generatingToolCallLogic({
|
|
|
1620
1557
|
const { text, toolCalls, finishReason } = result;
|
|
1621
1558
|
const toolCall = toolCalls[0];
|
|
1622
1559
|
if (!toolCall) {
|
|
1560
|
+
const reason = JSON.stringify({
|
|
1561
|
+
error: "Error: No tool call generated",
|
|
1562
|
+
message: "You must generate a tool call. Try again."
|
|
1563
|
+
});
|
|
1623
1564
|
return retry(setting, checkpoint, {
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
{
|
|
1627
|
-
type: "textPart",
|
|
1628
|
-
text: JSON.stringify({
|
|
1629
|
-
error: "Error: No tool call generated",
|
|
1630
|
-
message: "You must generate a tool call. Try again."
|
|
1631
|
-
})
|
|
1632
|
-
}
|
|
1633
|
-
])
|
|
1634
|
-
],
|
|
1565
|
+
reason,
|
|
1566
|
+
newMessages: [createUserMessage([{ type: "textPart", text: reason }])],
|
|
1635
1567
|
usage
|
|
1636
1568
|
});
|
|
1637
1569
|
}
|
|
@@ -1671,7 +1603,12 @@ async function generatingToolCallLogic({
|
|
|
1671
1603
|
}
|
|
1672
1604
|
}
|
|
1673
1605
|
if (finishReason === "length") {
|
|
1606
|
+
const reason = JSON.stringify({
|
|
1607
|
+
error: "Error: Tool call generation failed",
|
|
1608
|
+
message: "Generation length exceeded. Try again."
|
|
1609
|
+
});
|
|
1674
1610
|
return retry(setting, checkpoint, {
|
|
1611
|
+
reason,
|
|
1675
1612
|
newMessages: [
|
|
1676
1613
|
createExpertMessage([
|
|
1677
1614
|
{
|
|
@@ -1686,15 +1623,7 @@ async function generatingToolCallLogic({
|
|
|
1686
1623
|
type: "toolResultPart",
|
|
1687
1624
|
toolCallId: toolCall.toolCallId,
|
|
1688
1625
|
toolName: toolCall.toolName,
|
|
1689
|
-
contents: [
|
|
1690
|
-
{
|
|
1691
|
-
type: "textPart",
|
|
1692
|
-
text: JSON.stringify({
|
|
1693
|
-
error: "Error: Tool call generation failed",
|
|
1694
|
-
message: "Generation length exceeded. Try again."
|
|
1695
|
-
})
|
|
1696
|
-
}
|
|
1697
|
-
]
|
|
1626
|
+
contents: [{ type: "textPart", text: reason }]
|
|
1698
1627
|
}
|
|
1699
1628
|
])
|
|
1700
1629
|
],
|
|
@@ -1703,16 +1632,7 @@ async function generatingToolCallLogic({
|
|
|
1703
1632
|
id: toolCall.toolCallId,
|
|
1704
1633
|
skillName: skillManager.name,
|
|
1705
1634
|
toolName: toolCall.toolName,
|
|
1706
|
-
result: [
|
|
1707
|
-
{
|
|
1708
|
-
type: "textPart",
|
|
1709
|
-
id: createId6(),
|
|
1710
|
-
text: JSON.stringify({
|
|
1711
|
-
error: "Error: Tool call generation failed",
|
|
1712
|
-
message: "Generation length exceeded. Try again."
|
|
1713
|
-
})
|
|
1714
|
-
}
|
|
1715
|
-
]
|
|
1635
|
+
result: [{ type: "textPart", id: createId6(), text: reason }]
|
|
1716
1636
|
},
|
|
1717
1637
|
usage
|
|
1718
1638
|
});
|
|
@@ -1852,28 +1772,7 @@ async function initLogic({
|
|
|
1852
1772
|
]
|
|
1853
1773
|
});
|
|
1854
1774
|
}
|
|
1855
|
-
case "
|
|
1856
|
-
if (!setting.input.text) {
|
|
1857
|
-
throw new Error("Input message is undefined");
|
|
1858
|
-
}
|
|
1859
|
-
return startRun(setting, checkpoint, {
|
|
1860
|
-
initialCheckpoint: checkpoint,
|
|
1861
|
-
inputMessages: [createUserMessage([{ type: "textPart", text: setting.input.text }])]
|
|
1862
|
-
});
|
|
1863
|
-
}
|
|
1864
|
-
case "stoppedByDelegate": {
|
|
1865
|
-
if (!setting.input.interactiveToolCallResult) {
|
|
1866
|
-
throw new Error("Interactive tool call result is undefined");
|
|
1867
|
-
}
|
|
1868
|
-
return startRun(setting, checkpoint, {
|
|
1869
|
-
initialCheckpoint: checkpoint,
|
|
1870
|
-
inputMessages: [
|
|
1871
|
-
createUserMessage([
|
|
1872
|
-
{ type: "textPart", text: setting.input.interactiveToolCallResult.text }
|
|
1873
|
-
])
|
|
1874
|
-
]
|
|
1875
|
-
});
|
|
1876
|
-
}
|
|
1775
|
+
case "stoppedByDelegate":
|
|
1877
1776
|
case "stoppedByInteractiveTool": {
|
|
1878
1777
|
if (!setting.input.interactiveToolCallResult) {
|
|
1879
1778
|
throw new Error("Interactive tool call result is undefined");
|
|
@@ -1893,7 +1792,13 @@ async function initLogic({
|
|
|
1893
1792
|
});
|
|
1894
1793
|
}
|
|
1895
1794
|
default:
|
|
1896
|
-
|
|
1795
|
+
if (!setting.input.text) {
|
|
1796
|
+
throw new Error("Input message is undefined");
|
|
1797
|
+
}
|
|
1798
|
+
return startRun(setting, checkpoint, {
|
|
1799
|
+
initialCheckpoint: checkpoint,
|
|
1800
|
+
inputMessages: [createUserMessage([{ type: "textPart", text: setting.input.text }])]
|
|
1801
|
+
});
|
|
1897
1802
|
}
|
|
1898
1803
|
}
|
|
1899
1804
|
|
|
@@ -1914,7 +1819,7 @@ async function resolvingImageFileLogic({
|
|
|
1914
1819
|
checkpoint,
|
|
1915
1820
|
step
|
|
1916
1821
|
}) {
|
|
1917
|
-
if (!step
|
|
1822
|
+
if (!step.toolCall || !step.toolResult) {
|
|
1918
1823
|
throw new Error("No tool call or tool result found");
|
|
1919
1824
|
}
|
|
1920
1825
|
const { id, toolName } = step.toolCall;
|
|
@@ -1965,7 +1870,7 @@ async function resolvingPdfFileLogic({
|
|
|
1965
1870
|
checkpoint,
|
|
1966
1871
|
step
|
|
1967
1872
|
}) {
|
|
1968
|
-
if (!step
|
|
1873
|
+
if (!step.toolCall || !step.toolResult) {
|
|
1969
1874
|
throw new Error("No tool call or tool result found");
|
|
1970
1875
|
}
|
|
1971
1876
|
const { id, toolName } = step.toolCall;
|
|
@@ -2021,7 +1926,7 @@ async function resolvingToolResultLogic({
|
|
|
2021
1926
|
checkpoint,
|
|
2022
1927
|
step
|
|
2023
1928
|
}) {
|
|
2024
|
-
if (!step
|
|
1929
|
+
if (!step.toolCall || !step.toolResult) {
|
|
2025
1930
|
throw new Error("No tool call or tool result found");
|
|
2026
1931
|
}
|
|
2027
1932
|
const { id, toolName } = step.toolCall;
|
|
@@ -2077,17 +1982,14 @@ var runtimeStateMachine = setup({
|
|
|
2077
1982
|
startRun: {
|
|
2078
1983
|
target: "PreparingForStep",
|
|
2079
1984
|
actions: assign({
|
|
2080
|
-
checkpoint: ({ event }) => ({
|
|
2081
|
-
...
|
|
2082
|
-
|
|
2083
|
-
|
|
1985
|
+
checkpoint: ({ context, event }) => ({
|
|
1986
|
+
...context.checkpoint,
|
|
1987
|
+
status: "proceeding",
|
|
1988
|
+
messages: [...context.checkpoint.messages, ...event.inputMessages]
|
|
2084
1989
|
}),
|
|
2085
|
-
step: ({ event }) => ({
|
|
2086
|
-
|
|
2087
|
-
inputMessages: event.inputMessages
|
|
2088
|
-
newMessages: [],
|
|
2089
|
-
usage: createEmptyUsage(),
|
|
2090
|
-
startedAt: event.timestamp
|
|
1990
|
+
step: ({ context, event }) => ({
|
|
1991
|
+
...context.step,
|
|
1992
|
+
inputMessages: event.inputMessages
|
|
2091
1993
|
})
|
|
2092
1994
|
})
|
|
2093
1995
|
}
|
|
@@ -2100,7 +2002,7 @@ var runtimeStateMachine = setup({
|
|
|
2100
2002
|
actions: assign({
|
|
2101
2003
|
step: ({ context, event }) => ({
|
|
2102
2004
|
stepNumber: context.checkpoint.stepNumber,
|
|
2103
|
-
inputMessages: context.step.inputMessages,
|
|
2005
|
+
inputMessages: context.step.inputMessages ?? [],
|
|
2104
2006
|
newMessages: [],
|
|
2105
2007
|
usage: createEmptyUsage(),
|
|
2106
2008
|
startedAt: event.timestamp
|
|
@@ -2134,7 +2036,11 @@ var runtimeStateMachine = setup({
|
|
|
2134
2036
|
checkpoint: ({ context, event }) => ({
|
|
2135
2037
|
...context.checkpoint,
|
|
2136
2038
|
messages: [...context.checkpoint.messages, event.newMessage],
|
|
2137
|
-
usage: sumUsage(context.checkpoint.usage, event.usage)
|
|
2039
|
+
usage: sumUsage(context.checkpoint.usage, event.usage),
|
|
2040
|
+
contextWindowUsage: calculateWindowUsage(
|
|
2041
|
+
event.usage,
|
|
2042
|
+
context.checkpoint.contextWindow
|
|
2043
|
+
)
|
|
2138
2044
|
}),
|
|
2139
2045
|
step: ({ context, event }) => ({
|
|
2140
2046
|
...context.step,
|
|
@@ -2150,7 +2056,11 @@ var runtimeStateMachine = setup({
|
|
|
2150
2056
|
checkpoint: ({ context, event }) => ({
|
|
2151
2057
|
...context.checkpoint,
|
|
2152
2058
|
messages: [...context.checkpoint.messages, event.newMessage],
|
|
2153
|
-
usage: sumUsage(context.checkpoint.usage, event.usage)
|
|
2059
|
+
usage: sumUsage(context.checkpoint.usage, event.usage),
|
|
2060
|
+
contextWindowUsage: calculateWindowUsage(
|
|
2061
|
+
event.usage,
|
|
2062
|
+
context.checkpoint.contextWindow
|
|
2063
|
+
)
|
|
2154
2064
|
}),
|
|
2155
2065
|
step: ({ context, event }) => ({
|
|
2156
2066
|
...context.step,
|
|
@@ -2166,7 +2076,11 @@ var runtimeStateMachine = setup({
|
|
|
2166
2076
|
checkpoint: ({ context, event }) => ({
|
|
2167
2077
|
...context.checkpoint,
|
|
2168
2078
|
messages: [...context.checkpoint.messages, event.newMessage],
|
|
2169
|
-
usage: sumUsage(context.checkpoint.usage, event.usage)
|
|
2079
|
+
usage: sumUsage(context.checkpoint.usage, event.usage),
|
|
2080
|
+
contextWindowUsage: calculateWindowUsage(
|
|
2081
|
+
event.usage,
|
|
2082
|
+
context.checkpoint.contextWindow
|
|
2083
|
+
)
|
|
2170
2084
|
}),
|
|
2171
2085
|
step: ({ context, event }) => ({
|
|
2172
2086
|
...context.step,
|
|
@@ -2318,7 +2232,10 @@ var runtimeStateMachine = setup({
|
|
|
2318
2232
|
target: "Stopped",
|
|
2319
2233
|
actions: assign({
|
|
2320
2234
|
checkpoint: ({ event }) => event.checkpoint,
|
|
2321
|
-
step: ({ event }) =>
|
|
2235
|
+
step: ({ event }) => ({
|
|
2236
|
+
...event.step,
|
|
2237
|
+
inputMessages: void 0
|
|
2238
|
+
})
|
|
2322
2239
|
})
|
|
2323
2240
|
}
|
|
2324
2241
|
}
|
|
@@ -2329,7 +2246,10 @@ var runtimeStateMachine = setup({
|
|
|
2329
2246
|
target: "Stopped",
|
|
2330
2247
|
actions: assign({
|
|
2331
2248
|
checkpoint: ({ event }) => event.checkpoint,
|
|
2332
|
-
step: ({ event }) =>
|
|
2249
|
+
step: ({ event }) => ({
|
|
2250
|
+
...event.step,
|
|
2251
|
+
inputMessages: void 0
|
|
2252
|
+
})
|
|
2333
2253
|
})
|
|
2334
2254
|
}
|
|
2335
2255
|
}
|
|
@@ -2340,7 +2260,10 @@ var runtimeStateMachine = setup({
|
|
|
2340
2260
|
target: "Stopped",
|
|
2341
2261
|
actions: assign({
|
|
2342
2262
|
checkpoint: ({ event }) => event.checkpoint,
|
|
2343
|
-
step: ({ event }) =>
|
|
2263
|
+
step: ({ event }) => ({
|
|
2264
|
+
...event.step,
|
|
2265
|
+
inputMessages: void 0
|
|
2266
|
+
})
|
|
2344
2267
|
})
|
|
2345
2268
|
}
|
|
2346
2269
|
}
|
|
@@ -2455,9 +2378,7 @@ async function run(runInput, options) {
|
|
|
2455
2378
|
const eventEmitter = new RunEventEmitter();
|
|
2456
2379
|
eventEmitter.subscribe(eventListener);
|
|
2457
2380
|
let { setting, checkpoint } = runParams;
|
|
2458
|
-
|
|
2459
|
-
checkpoint.status = "proceeding";
|
|
2460
|
-
}
|
|
2381
|
+
const modelConfig = getModelConfig(setting.model);
|
|
2461
2382
|
if (setting.workspace) {
|
|
2462
2383
|
if (!path.isAbsolute(setting.workspace)) {
|
|
2463
2384
|
throw new Error(`Workspace path must be absolute: ${setting.workspace}`);
|
|
@@ -2490,7 +2411,9 @@ async function run(runInput, options) {
|
|
|
2490
2411
|
stepNumber: 1,
|
|
2491
2412
|
status: "init",
|
|
2492
2413
|
messages: [],
|
|
2493
|
-
usage: createEmptyUsage()
|
|
2414
|
+
usage: createEmptyUsage(),
|
|
2415
|
+
contextWindow: modelConfig.contextWindow,
|
|
2416
|
+
contextWindowUsage: 0
|
|
2494
2417
|
},
|
|
2495
2418
|
eventListener,
|
|
2496
2419
|
skillManagers
|
|
@@ -2757,6 +2680,10 @@ export {
|
|
|
2757
2680
|
continueToNextStep,
|
|
2758
2681
|
createEvent,
|
|
2759
2682
|
defaultEventListener,
|
|
2683
|
+
defaultMaxRetries,
|
|
2684
|
+
defaultMaxSteps,
|
|
2685
|
+
defaultTemperature,
|
|
2686
|
+
defaultTimeout,
|
|
2760
2687
|
expertKeyRegex,
|
|
2761
2688
|
expertNameRegex,
|
|
2762
2689
|
finishToolCall,
|