@theokit/agents 0.4.0 → 0.6.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/dist/bridge.d.ts +111 -30
- package/dist/bridge.js +10 -2
- package/dist/{chunk-NC5EE7HN.js → chunk-KTYBQ7HY.js} +429 -79
- package/dist/chunk-KTYBQ7HY.js.map +1 -0
- package/dist/{chunk-O4UG4RXE.js → chunk-MVEY7HEY.js} +58 -1
- package/dist/chunk-MVEY7HEY.js.map +1 -0
- package/dist/{chunk-VKHDADMG.js → chunk-XWVZS2PQ.js} +2 -59
- package/dist/chunk-XWVZS2PQ.js.map +1 -0
- package/dist/decorators.d.ts +3 -38
- package/dist/decorators.js +6 -6
- package/dist/index.d.ts +199 -4
- package/dist/index.js +31 -7
- package/dist/{mcp-yyfC5u7t.d.ts → skills-DmN1HGUb.d.ts} +50 -15
- package/package.json +18 -7
- package/dist/chunk-NC5EE7HN.js.map +0 -1
- package/dist/chunk-O4UG4RXE.js.map +0 -1
- package/dist/chunk-VKHDADMG.js.map +0 -1
|
@@ -9,14 +9,16 @@ import {
|
|
|
9
9
|
TOOL_METHODS,
|
|
10
10
|
Trace,
|
|
11
11
|
getAgentConfig,
|
|
12
|
+
getContextWindowConfig,
|
|
12
13
|
getGatewayConfig,
|
|
13
14
|
getMcpConfig,
|
|
14
15
|
getMemoryConfig,
|
|
15
16
|
getMeta,
|
|
16
17
|
getMixins,
|
|
18
|
+
getProjectContextConfig,
|
|
17
19
|
getSkillsConfig,
|
|
18
20
|
getSubAgents
|
|
19
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-MVEY7HEY.js";
|
|
20
22
|
import {
|
|
21
23
|
__name
|
|
22
24
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -40,6 +42,68 @@ function isAgentContext(ctx) {
|
|
|
40
42
|
}
|
|
41
43
|
__name(isAgentContext, "isAgentContext");
|
|
42
44
|
|
|
45
|
+
// src/bridge/compile-context-window.ts
|
|
46
|
+
var STRATEGY_KNOBS = [
|
|
47
|
+
"compactionStrategy",
|
|
48
|
+
"preserveLastN",
|
|
49
|
+
"preserveToolResults",
|
|
50
|
+
"preserveSystemPrompt"
|
|
51
|
+
];
|
|
52
|
+
function compileContextWindow(options) {
|
|
53
|
+
const context = {};
|
|
54
|
+
if (typeof options.maxTokens === "number") {
|
|
55
|
+
context.maxTokens = options.maxTokens;
|
|
56
|
+
}
|
|
57
|
+
const opts = options;
|
|
58
|
+
const metadataOnlyKnobs = STRATEGY_KNOBS.filter((knob) => opts[knob] !== void 0);
|
|
59
|
+
return {
|
|
60
|
+
context,
|
|
61
|
+
metadataOnlyKnobs
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
__name(compileContextWindow, "compileContextWindow");
|
|
65
|
+
|
|
66
|
+
// src/bridge/compile-project-context.ts
|
|
67
|
+
var UNMAPPED_KNOBS = [
|
|
68
|
+
"indexStrategy",
|
|
69
|
+
"relevanceStrategy",
|
|
70
|
+
"maxFilesInContext",
|
|
71
|
+
"includeExtensions",
|
|
72
|
+
"rootMarkers"
|
|
73
|
+
];
|
|
74
|
+
function projectContextMetadataOnlyKnobs(options) {
|
|
75
|
+
const opts = options;
|
|
76
|
+
return UNMAPPED_KNOBS.filter((knob) => opts[knob] !== void 0);
|
|
77
|
+
}
|
|
78
|
+
__name(projectContextMetadataOnlyKnobs, "projectContextMetadataOnlyKnobs");
|
|
79
|
+
function compileProjectContext(options, base) {
|
|
80
|
+
return async (promptCtx) => {
|
|
81
|
+
const cwd = promptCtx.cwd;
|
|
82
|
+
if (!cwd) {
|
|
83
|
+
return base ?? "";
|
|
84
|
+
}
|
|
85
|
+
const { buildEnvContext, buildRepoMap } = await import("@theokit/sdk-tools");
|
|
86
|
+
const { readProjectInstructions } = await import("@theokit/sdk/project");
|
|
87
|
+
const env = buildEnvContext(cwd);
|
|
88
|
+
const repoMap = buildRepoMap(cwd, {
|
|
89
|
+
ignore: options.ignorePatterns
|
|
90
|
+
});
|
|
91
|
+
let instructions = "";
|
|
92
|
+
try {
|
|
93
|
+
instructions = (await readProjectInstructions(cwd)).content ?? "";
|
|
94
|
+
} catch {
|
|
95
|
+
instructions = "";
|
|
96
|
+
}
|
|
97
|
+
return [
|
|
98
|
+
env,
|
|
99
|
+
repoMap,
|
|
100
|
+
instructions,
|
|
101
|
+
base
|
|
102
|
+
].filter(Boolean).join("\n\n");
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
__name(compileProjectContext, "compileProjectContext");
|
|
106
|
+
|
|
43
107
|
// src/bridge/walk-agent-metadata.ts
|
|
44
108
|
import "reflect-metadata";
|
|
45
109
|
import { Reflector } from "@theokit/http";
|
|
@@ -49,7 +113,9 @@ var USE_FILTERS = /* @__PURE__ */ Symbol.for("theokit:http-decorators:use-filter
|
|
|
49
113
|
var AgentWarningCode = {
|
|
50
114
|
INTERCEPTOR_METADATA_ONLY: "THEO_AGENT_INTERCEPTOR_METADATA_ONLY",
|
|
51
115
|
FILTER_METADATA_ONLY: "THEO_AGENT_FILTER_METADATA_ONLY",
|
|
52
|
-
BUDGET_TOP_LEVEL_METADATA_ONLY: "THEO_AGENT_BUDGET_TOP_LEVEL_METADATA_ONLY"
|
|
116
|
+
BUDGET_TOP_LEVEL_METADATA_ONLY: "THEO_AGENT_BUDGET_TOP_LEVEL_METADATA_ONLY",
|
|
117
|
+
CONTEXT_STRATEGY_METADATA_ONLY: "THEO_AGENT_CONTEXT_STRATEGY_METADATA_ONLY",
|
|
118
|
+
PROJECT_CONTEXT_KNOB_METADATA_ONLY: "THEO_AGENT_PROJECT_CONTEXT_KNOB_METADATA_ONLY"
|
|
53
119
|
};
|
|
54
120
|
var reflectorInstance = new Reflector();
|
|
55
121
|
function walkToolbox(ToolboxClass) {
|
|
@@ -88,6 +154,21 @@ function walkToolbox(ToolboxClass) {
|
|
|
88
154
|
};
|
|
89
155
|
}
|
|
90
156
|
__name(walkToolbox, "walkToolbox");
|
|
157
|
+
function warnUnmappedDecoratorKnobs(agentName, contextWindow, projectContext) {
|
|
158
|
+
if (contextWindow) {
|
|
159
|
+
const { metadataOnlyKnobs } = compileContextWindow(contextWindow);
|
|
160
|
+
if (metadataOnlyKnobs.length > 0) {
|
|
161
|
+
console.warn(`[${AgentWarningCode.CONTEXT_STRATEGY_METADATA_ONLY}] Agent ${agentName}: @ContextWindow knob(s) ${metadataOnlyKnobs.join(", ")} are metadata-only \u2014 the SDK manages transcript compaction internally. Only maxTokens is forwarded to Agent.create({ context }).`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (projectContext) {
|
|
165
|
+
const unmapped = projectContextMetadataOnlyKnobs(projectContext);
|
|
166
|
+
if (unmapped.length > 0) {
|
|
167
|
+
console.warn(`[${AgentWarningCode.PROJECT_CONTEXT_KNOB_METADATA_ONLY}] Agent ${agentName}: @ProjectContext knob(s) ${unmapped.join(", ")} are metadata-only \u2014 the repo map is composed via buildRepoMap/buildEnvContext/readProjectInstructions; only ignorePatterns is forwarded.`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
__name(warnUnmappedDecoratorKnobs, "warnUnmappedDecoratorKnobs");
|
|
91
172
|
var agentWalkCache = /* @__PURE__ */ new WeakMap();
|
|
92
173
|
function walkAgentMetadata(AgentClass, toolboxClasses = []) {
|
|
93
174
|
if (toolboxClasses.length === 0) {
|
|
@@ -121,6 +202,9 @@ function walkAgentMetadata(AgentClass, toolboxClasses = []) {
|
|
|
121
202
|
const memory = getMemoryConfig(AgentClass);
|
|
122
203
|
const skills = getSkillsConfig(AgentClass);
|
|
123
204
|
const mcpServers = getMcpConfig(AgentClass);
|
|
205
|
+
const contextWindow = getContextWindowConfig(AgentClass);
|
|
206
|
+
const projectContext = getProjectContextConfig(AgentClass);
|
|
207
|
+
warnUnmappedDecoratorKnobs(AgentClass.name, contextWindow, projectContext);
|
|
124
208
|
const result = {
|
|
125
209
|
agentConfig,
|
|
126
210
|
mainLoop,
|
|
@@ -133,6 +217,8 @@ function walkAgentMetadata(AgentClass, toolboxClasses = []) {
|
|
|
133
217
|
subAgentClasses,
|
|
134
218
|
memory,
|
|
135
219
|
skills,
|
|
220
|
+
contextWindow,
|
|
221
|
+
projectContext,
|
|
136
222
|
mcpServers
|
|
137
223
|
};
|
|
138
224
|
if (toolboxClasses.length === 0) {
|
|
@@ -153,6 +239,20 @@ function validateUniqueRoutes(results) {
|
|
|
153
239
|
}
|
|
154
240
|
__name(validateUniqueRoutes, "validateUniqueRoutes");
|
|
155
241
|
|
|
242
|
+
// src/bridge/compile-skills.ts
|
|
243
|
+
function compileSkills(options) {
|
|
244
|
+
if (options.autoDiscover) {
|
|
245
|
+
return {
|
|
246
|
+
autoInject: true
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
enabled: options.include,
|
|
251
|
+
autoInject: true
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
__name(compileSkills, "compileSkills");
|
|
255
|
+
|
|
156
256
|
// src/bridge/agent-compiler.ts
|
|
157
257
|
function compileTools(toolboxes, toolboxInstances) {
|
|
158
258
|
const tools = [];
|
|
@@ -200,7 +300,9 @@ function compileAgent(walkResult, toolboxInstances = /* @__PURE__ */ new Map())
|
|
|
200
300
|
tools,
|
|
201
301
|
agents,
|
|
202
302
|
memory: walkResult.memory,
|
|
203
|
-
skills: walkResult.skills,
|
|
303
|
+
skills: walkResult.skills ? compileSkills(walkResult.skills) : void 0,
|
|
304
|
+
context: walkResult.contextWindow ? compileContextWindow(walkResult.contextWindow).context : void 0,
|
|
305
|
+
projectContext: walkResult.projectContext,
|
|
204
306
|
mcpServers: walkResult.mcpServers,
|
|
205
307
|
maxIterations: walkResult.mainLoop.maxIterations ?? walkResult.agentConfig.maxIterations,
|
|
206
308
|
timeoutMs: walkResult.mainLoop.timeoutMs ?? walkResult.agentConfig.timeoutMs,
|
|
@@ -375,7 +477,8 @@ function translateSystemEvent(msg, runId) {
|
|
|
375
477
|
__name(translateSystemEvent, "translateSystemEvent");
|
|
376
478
|
function translateAssistantEvent(msg) {
|
|
377
479
|
const events = [];
|
|
378
|
-
const
|
|
480
|
+
const message = msg.message;
|
|
481
|
+
const content = message?.content;
|
|
379
482
|
if (!Array.isArray(content)) return events;
|
|
380
483
|
for (const block of content) {
|
|
381
484
|
const b = block;
|
|
@@ -399,12 +502,14 @@ function translateAssistantEvent(msg) {
|
|
|
399
502
|
__name(translateAssistantEvent, "translateAssistantEvent");
|
|
400
503
|
function translateToolCallEvent(msg) {
|
|
401
504
|
const status = msg.status;
|
|
505
|
+
const callId = asString(msg.call_id, `tc-${Date.now()}`);
|
|
506
|
+
const toolName = asString(msg.name, "unknown");
|
|
402
507
|
if (status === "completed") {
|
|
403
508
|
return [
|
|
404
509
|
{
|
|
405
510
|
type: "tool_result",
|
|
406
|
-
callId
|
|
407
|
-
toolName
|
|
511
|
+
callId,
|
|
512
|
+
toolName,
|
|
408
513
|
output: asString(msg.result, ""),
|
|
409
514
|
durationMs: 0,
|
|
410
515
|
isError: false
|
|
@@ -415,9 +520,9 @@ function translateToolCallEvent(msg) {
|
|
|
415
520
|
return [
|
|
416
521
|
{
|
|
417
522
|
type: "tool_result",
|
|
418
|
-
callId
|
|
419
|
-
toolName
|
|
420
|
-
output: asString(msg.
|
|
523
|
+
callId,
|
|
524
|
+
toolName,
|
|
525
|
+
output: asString(msg.result, "Tool failed"),
|
|
421
526
|
durationMs: 0,
|
|
422
527
|
isError: true
|
|
423
528
|
}
|
|
@@ -428,7 +533,7 @@ function translateToolCallEvent(msg) {
|
|
|
428
533
|
__name(translateToolCallEvent, "translateToolCallEvent");
|
|
429
534
|
function translateStatusEvent(msg) {
|
|
430
535
|
const s = msg.status;
|
|
431
|
-
if (s === "
|
|
536
|
+
if (s === "FINISHED" || s === "CANCELLED") {
|
|
432
537
|
return [
|
|
433
538
|
{
|
|
434
539
|
type: "done",
|
|
@@ -443,12 +548,12 @@ function translateStatusEvent(msg) {
|
|
|
443
548
|
}
|
|
444
549
|
];
|
|
445
550
|
}
|
|
446
|
-
if (s === "
|
|
551
|
+
if (s === "ERROR" || s === "EXPIRED") {
|
|
447
552
|
return [
|
|
448
553
|
{
|
|
449
554
|
type: "error",
|
|
450
555
|
code: "AGENT_ERROR",
|
|
451
|
-
message: asString(msg.
|
|
556
|
+
message: asString(msg.message, "Agent error"),
|
|
452
557
|
retryable: false
|
|
453
558
|
}
|
|
454
559
|
];
|
|
@@ -468,7 +573,7 @@ function translateSdkEvent(msg, runId) {
|
|
|
468
573
|
return [
|
|
469
574
|
{
|
|
470
575
|
type: "thinking",
|
|
471
|
-
content: asString(msg.
|
|
576
|
+
content: asString(msg.text, "")
|
|
472
577
|
}
|
|
473
578
|
];
|
|
474
579
|
case "status":
|
|
@@ -480,8 +585,37 @@ function translateSdkEvent(msg, runId) {
|
|
|
480
585
|
__name(translateSdkEvent, "translateSdkEvent");
|
|
481
586
|
|
|
482
587
|
// src/bridge/sdk-adapter.ts
|
|
483
|
-
function
|
|
484
|
-
const
|
|
588
|
+
function assembleM8CreateOptions(compiled) {
|
|
589
|
+
const options = {};
|
|
590
|
+
const applied = [];
|
|
591
|
+
const base = compiled.systemPrompt;
|
|
592
|
+
if (compiled.skills) {
|
|
593
|
+
options.skills = compiled.skills;
|
|
594
|
+
options.local = {
|
|
595
|
+
settingSources: [
|
|
596
|
+
"project"
|
|
597
|
+
]
|
|
598
|
+
};
|
|
599
|
+
applied.push("skills");
|
|
600
|
+
}
|
|
601
|
+
if (compiled.context) {
|
|
602
|
+
options.context = compiled.context;
|
|
603
|
+
applied.push("context");
|
|
604
|
+
}
|
|
605
|
+
if (compiled.projectContext) {
|
|
606
|
+
options.systemPrompt = compileProjectContext(compiled.projectContext, base);
|
|
607
|
+
applied.push("projectContext");
|
|
608
|
+
} else if (base !== void 0) {
|
|
609
|
+
options.systemPrompt = base;
|
|
610
|
+
}
|
|
611
|
+
return {
|
|
612
|
+
options,
|
|
613
|
+
applied
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
__name(assembleM8CreateOptions, "assembleM8CreateOptions");
|
|
617
|
+
function createSdkAgentStream(compiled, compiledTools, apiKey, envModel) {
|
|
618
|
+
const model = envModel ?? compiled.model ?? "openai/gpt-4o-mini";
|
|
485
619
|
return (message, _sessionId) => ({
|
|
486
620
|
async *[Symbol.asyncIterator]() {
|
|
487
621
|
const runId = `run-${Date.now()}`;
|
|
@@ -508,32 +642,44 @@ function createSdkAgentStream(agentWalk, compiledTools, apiKey, envModel) {
|
|
|
508
642
|
handler: t.handler
|
|
509
643
|
}));
|
|
510
644
|
try {
|
|
645
|
+
const { options: m8, applied } = assembleM8CreateOptions(compiled);
|
|
646
|
+
if (applied.length > 0) {
|
|
647
|
+
console.debug("[THEO_AGENT_M8_RUNTIME_APPLIED]", {
|
|
648
|
+
skills: applied.includes("skills"),
|
|
649
|
+
context: applied.includes("context"),
|
|
650
|
+
projectContext: applied.includes("projectContext")
|
|
651
|
+
});
|
|
652
|
+
}
|
|
511
653
|
const agent = await Agent.create({
|
|
512
654
|
apiKey,
|
|
513
655
|
model: {
|
|
514
656
|
id: model
|
|
515
657
|
},
|
|
516
658
|
tools: sdkTools,
|
|
517
|
-
|
|
659
|
+
...m8
|
|
518
660
|
});
|
|
519
661
|
const run = await agent.send(message);
|
|
662
|
+
let sawTerminal = false;
|
|
520
663
|
for await (const sdkEvent of run.stream()) {
|
|
521
664
|
const translated = translateSdkEvent(sdkEvent, runId);
|
|
522
665
|
for (const event of translated) {
|
|
666
|
+
if (event.type === "done" || event.type === "error") sawTerminal = true;
|
|
523
667
|
yield event;
|
|
524
668
|
}
|
|
525
669
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
670
|
+
if (!sawTerminal) {
|
|
671
|
+
yield {
|
|
672
|
+
type: "done",
|
|
673
|
+
result: "",
|
|
674
|
+
usage: {
|
|
675
|
+
inputTokens: 0,
|
|
676
|
+
outputTokens: 0,
|
|
677
|
+
totalTokens: 0
|
|
678
|
+
},
|
|
679
|
+
durationMs: Date.now() - t0,
|
|
680
|
+
cost: 0
|
|
681
|
+
};
|
|
682
|
+
}
|
|
537
683
|
await agent.dispose();
|
|
538
684
|
} catch (err) {
|
|
539
685
|
yield {
|
|
@@ -548,7 +694,66 @@ function createSdkAgentStream(agentWalk, compiledTools, apiKey, envModel) {
|
|
|
548
694
|
}
|
|
549
695
|
__name(createSdkAgentStream, "createSdkAgentStream");
|
|
550
696
|
|
|
551
|
-
// src/
|
|
697
|
+
// src/loop/loop-strategy.ts
|
|
698
|
+
import { z } from "zod";
|
|
699
|
+
var DEFAULT_MAX_ITERATIONS = 8;
|
|
700
|
+
var loopStrategyConfigSchema = z.object({
|
|
701
|
+
name: z.enum([
|
|
702
|
+
"simple-chat",
|
|
703
|
+
"plan-act-reflect",
|
|
704
|
+
"react"
|
|
705
|
+
]),
|
|
706
|
+
maxIterations: z.number().int().min(1)
|
|
707
|
+
});
|
|
708
|
+
function resolveLoopStrategy(strategy, maxIterations = DEFAULT_MAX_ITERATIONS) {
|
|
709
|
+
const cfg = loopStrategyConfigSchema.parse({
|
|
710
|
+
name: strategy,
|
|
711
|
+
maxIterations
|
|
712
|
+
});
|
|
713
|
+
if (cfg.name === "simple-chat") {
|
|
714
|
+
return {
|
|
715
|
+
name: cfg.name,
|
|
716
|
+
maxIterations: cfg.maxIterations,
|
|
717
|
+
shouldContinue: /* @__PURE__ */ __name(() => false, "shouldContinue")
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
return {
|
|
721
|
+
name: cfg.name,
|
|
722
|
+
maxIterations: cfg.maxIterations,
|
|
723
|
+
shouldContinue: /* @__PURE__ */ __name((outcome) => outcome.finishReason === "tool-calls" && outcome.round < cfg.maxIterations, "shouldContinue")
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
__name(resolveLoopStrategy, "resolveLoopStrategy");
|
|
727
|
+
|
|
728
|
+
// src/loop/reflection-strategy.ts
|
|
729
|
+
import { z as z2 } from "zod";
|
|
730
|
+
var reflectionStrategyConfigSchema = z2.object({
|
|
731
|
+
name: z2.string().min(1)
|
|
732
|
+
});
|
|
733
|
+
var ladderReflectionStrategy = {
|
|
734
|
+
name: "ladder",
|
|
735
|
+
reflect(outcome) {
|
|
736
|
+
if (outcome.finishReason === "tool-calls") {
|
|
737
|
+
return {
|
|
738
|
+
feedback: `Tool results received (round ${outcome.round}). Reflect on whether the goal is met; if not, refine the approach and continue.`,
|
|
739
|
+
continue: true
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
return {
|
|
743
|
+
continue: false
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
var noopReflectionStrategy = {
|
|
748
|
+
name: "noop",
|
|
749
|
+
reflect() {
|
|
750
|
+
return {
|
|
751
|
+
continue: true
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
// src/bridge/delegation-types.ts
|
|
552
757
|
var BudgetExceededError = class extends Error {
|
|
553
758
|
static {
|
|
554
759
|
__name(this, "BudgetExceededError");
|
|
@@ -572,16 +777,185 @@ var DelegationError = class extends Error {
|
|
|
572
777
|
this.name = "DelegationError";
|
|
573
778
|
}
|
|
574
779
|
};
|
|
780
|
+
|
|
781
|
+
// src/loop/run-reflective-loop.ts
|
|
575
782
|
function asString2(value, fallback) {
|
|
576
|
-
|
|
577
|
-
return fallback;
|
|
783
|
+
return typeof value === "string" ? value : fallback;
|
|
578
784
|
}
|
|
579
785
|
__name(asString2, "asString");
|
|
580
786
|
function asNumber(value, fallback) {
|
|
581
|
-
|
|
582
|
-
return fallback;
|
|
787
|
+
return typeof value === "number" ? value : fallback;
|
|
583
788
|
}
|
|
584
789
|
__name(asNumber, "asNumber");
|
|
790
|
+
function deriveFinishReason(signals) {
|
|
791
|
+
if (signals.sawError) return "error";
|
|
792
|
+
if (signals.sawDone && signals.doneFinishReason === "tool-calls") return "tool-calls";
|
|
793
|
+
if (signals.sawToolResult) return "tool-calls";
|
|
794
|
+
return "stop";
|
|
795
|
+
}
|
|
796
|
+
__name(deriveFinishReason, "deriveFinishReason");
|
|
797
|
+
async function consumeOneRound(factory, prompt, sessionId, signal) {
|
|
798
|
+
const r = {
|
|
799
|
+
responseText: "",
|
|
800
|
+
toolCalls: [],
|
|
801
|
+
cost: 0,
|
|
802
|
+
tokens: 0,
|
|
803
|
+
finishReason: "stop",
|
|
804
|
+
errorMessage: ""
|
|
805
|
+
};
|
|
806
|
+
const signals = {
|
|
807
|
+
sawError: false,
|
|
808
|
+
sawDone: false,
|
|
809
|
+
doneFinishReason: "",
|
|
810
|
+
sawToolResult: false
|
|
811
|
+
};
|
|
812
|
+
for await (const event of factory(prompt, sessionId)) {
|
|
813
|
+
if (signal?.aborted) break;
|
|
814
|
+
if (event.type === "text_delta" && typeof event.content === "string") {
|
|
815
|
+
r.responseText += event.content;
|
|
816
|
+
} else if (event.type === "tool_result") {
|
|
817
|
+
signals.sawToolResult = true;
|
|
818
|
+
r.toolCalls.push({
|
|
819
|
+
name: asString2(event.toolName, "unknown"),
|
|
820
|
+
input: event.input ?? {},
|
|
821
|
+
output: asString2(event.output, "")
|
|
822
|
+
});
|
|
823
|
+
} else if (event.type === "done") {
|
|
824
|
+
signals.sawDone = true;
|
|
825
|
+
signals.doneFinishReason = asString2(event.finishReason, "");
|
|
826
|
+
r.cost = asNumber(event.cost, 0);
|
|
827
|
+
r.tokens = event.usage?.totalTokens ?? 0;
|
|
828
|
+
} else if (event.type === "error") {
|
|
829
|
+
signals.sawError = true;
|
|
830
|
+
r.errorMessage = asString2(event.message, "Unknown agent error");
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
r.finishReason = deriveFinishReason(signals);
|
|
834
|
+
return r;
|
|
835
|
+
}
|
|
836
|
+
__name(consumeOneRound, "consumeOneRound");
|
|
837
|
+
async function runReflectiveLoop(factory, message, sessionId, config) {
|
|
838
|
+
const { loop, reflection, budget = Number.POSITIVE_INFINITY, signal, agentName = loop.name } = config;
|
|
839
|
+
const acc = {
|
|
840
|
+
response: "",
|
|
841
|
+
toolCalls: [],
|
|
842
|
+
cost: 0,
|
|
843
|
+
tokens: 0,
|
|
844
|
+
rounds: 0
|
|
845
|
+
};
|
|
846
|
+
let round = 1;
|
|
847
|
+
let feedback;
|
|
848
|
+
while (!signal?.aborted) {
|
|
849
|
+
const prompt = round === 1 || !feedback ? message : `${message}
|
|
850
|
+
|
|
851
|
+
[reflection] ${feedback}`;
|
|
852
|
+
let r;
|
|
853
|
+
try {
|
|
854
|
+
r = await consumeOneRound(factory, prompt, sessionId, signal);
|
|
855
|
+
} catch (err) {
|
|
856
|
+
if (err instanceof BudgetExceededError || err instanceof DelegationError) throw err;
|
|
857
|
+
throw new DelegationError(agentName, err);
|
|
858
|
+
}
|
|
859
|
+
acc.response += r.responseText;
|
|
860
|
+
acc.toolCalls.push(...r.toolCalls);
|
|
861
|
+
acc.cost += r.cost;
|
|
862
|
+
acc.tokens += r.tokens;
|
|
863
|
+
if (r.finishReason === "error") throw new DelegationError(agentName, r.errorMessage);
|
|
864
|
+
if (Number.isFinite(budget) && acc.cost > budget) {
|
|
865
|
+
throw new BudgetExceededError(agentName, acc.cost, budget);
|
|
866
|
+
}
|
|
867
|
+
const outcome = {
|
|
868
|
+
finishReason: r.finishReason,
|
|
869
|
+
round,
|
|
870
|
+
toolCalls: r.toolCalls,
|
|
871
|
+
responseText: r.responseText
|
|
872
|
+
};
|
|
873
|
+
const reflectionResult = reflection.reflect(outcome);
|
|
874
|
+
if (!(reflectionResult.continue && loop.shouldContinue(outcome))) {
|
|
875
|
+
acc.rounds = round;
|
|
876
|
+
console.debug("[THEO_AGENT_MAINLOOP_RUNTIME_APPLIED]", {
|
|
877
|
+
strategy: loop.name,
|
|
878
|
+
rounds: round
|
|
879
|
+
});
|
|
880
|
+
return acc;
|
|
881
|
+
}
|
|
882
|
+
feedback = reflectionResult.feedback;
|
|
883
|
+
round += 1;
|
|
884
|
+
}
|
|
885
|
+
acc.rounds = round - 1;
|
|
886
|
+
return acc;
|
|
887
|
+
}
|
|
888
|
+
__name(runReflectiveLoop, "runReflectiveLoop");
|
|
889
|
+
|
|
890
|
+
// src/loop/agent-runner.ts
|
|
891
|
+
var AgentRunner = class {
|
|
892
|
+
static {
|
|
893
|
+
__name(this, "AgentRunner");
|
|
894
|
+
}
|
|
895
|
+
compiled;
|
|
896
|
+
agentName;
|
|
897
|
+
loopStrategy;
|
|
898
|
+
reflectionStrategy;
|
|
899
|
+
streamEnabled;
|
|
900
|
+
constructor(compiled, agentName, loopStrategy, reflectionStrategy, streamEnabled) {
|
|
901
|
+
this.compiled = compiled;
|
|
902
|
+
this.agentName = agentName;
|
|
903
|
+
this.loopStrategy = loopStrategy;
|
|
904
|
+
this.reflectionStrategy = reflectionStrategy;
|
|
905
|
+
this.streamEnabled = streamEnabled;
|
|
906
|
+
}
|
|
907
|
+
/** Start a fluent builder for `AgentClass`. */
|
|
908
|
+
static builder(AgentClass) {
|
|
909
|
+
return new AgentRunnerBuilder(AgentClass);
|
|
910
|
+
}
|
|
911
|
+
/** Run the agent to a terminal result via the shared reflective loop. */
|
|
912
|
+
run(message, opts) {
|
|
913
|
+
const streamFactory = createSdkAgentStream(this.compiled, this.compiled.tools, opts.apiKey, this.compiled.model);
|
|
914
|
+
const sessionId = opts.sessionId ?? `runner-${crypto.randomUUID()}`;
|
|
915
|
+
return runReflectiveLoop(streamFactory, message, sessionId, {
|
|
916
|
+
loop: this.loopStrategy,
|
|
917
|
+
reflection: this.reflectionStrategy,
|
|
918
|
+
budget: opts.budget,
|
|
919
|
+
agentName: this.agentName,
|
|
920
|
+
signal: opts.signal
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
var AgentRunnerBuilder = class {
|
|
925
|
+
static {
|
|
926
|
+
__name(this, "AgentRunnerBuilder");
|
|
927
|
+
}
|
|
928
|
+
AgentClass;
|
|
929
|
+
reflectionOverride;
|
|
930
|
+
streamEnabled = true;
|
|
931
|
+
constructor(AgentClass) {
|
|
932
|
+
this.AgentClass = AgentClass;
|
|
933
|
+
}
|
|
934
|
+
/** Override the default reflection strategy (OCP — plan Drawback #2). No arg ⇒ keep default. */
|
|
935
|
+
reflection(strategy) {
|
|
936
|
+
if (strategy) this.reflectionOverride = strategy;
|
|
937
|
+
return this;
|
|
938
|
+
}
|
|
939
|
+
/** Record the streaming preference (see {@link AgentRunner.streamEnabled}). */
|
|
940
|
+
stream(enabled = true) {
|
|
941
|
+
this.streamEnabled = enabled;
|
|
942
|
+
return this;
|
|
943
|
+
}
|
|
944
|
+
/** Walk + compile + resolve strategies — the compile→execute boundary (no I/O). */
|
|
945
|
+
build() {
|
|
946
|
+
const walk = walkAgentMetadata(this.AgentClass, []);
|
|
947
|
+
const toolboxInstances = new Map(walk.toolboxes.map((tb) => [
|
|
948
|
+
tb.class,
|
|
949
|
+
new tb.class()
|
|
950
|
+
]));
|
|
951
|
+
const compiled = compileAgent(walk, toolboxInstances);
|
|
952
|
+
const loopStrategy = resolveLoopStrategy(walk.mainLoop.strategy, walk.mainLoop.maxIterations);
|
|
953
|
+
const reflectionStrategy = this.reflectionOverride ?? (walk.mainLoop.strategy === "plan-act-reflect" ? ladderReflectionStrategy : noopReflectionStrategy);
|
|
954
|
+
return new AgentRunner(compiled, walk.agentConfig.name, loopStrategy, reflectionStrategy, this.streamEnabled);
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
|
|
958
|
+
// src/bridge/agent-orchestrator.ts
|
|
585
959
|
function requireApiKey(opts, agentName) {
|
|
586
960
|
const apiKey = opts.apiKey ?? "";
|
|
587
961
|
if (!apiKey) {
|
|
@@ -599,33 +973,6 @@ function mergeTools(parentTools, subTools) {
|
|
|
599
973
|
];
|
|
600
974
|
}
|
|
601
975
|
__name(mergeTools, "mergeTools");
|
|
602
|
-
function processStreamEvent(event, acc, budget, agentName) {
|
|
603
|
-
if (event.type === "text_delta" && typeof event.content === "string") {
|
|
604
|
-
acc.response += event.content;
|
|
605
|
-
return;
|
|
606
|
-
}
|
|
607
|
-
if (event.type === "tool_result") {
|
|
608
|
-
acc.toolCalls.push({
|
|
609
|
-
name: asString2(event.toolName, "unknown"),
|
|
610
|
-
input: event.input ?? {},
|
|
611
|
-
output: asString2(event.output, "")
|
|
612
|
-
});
|
|
613
|
-
return;
|
|
614
|
-
}
|
|
615
|
-
if (event.type === "done") {
|
|
616
|
-
acc.cost = asNumber(event.cost, 0);
|
|
617
|
-
const usage = event.usage;
|
|
618
|
-
acc.tokens = usage?.totalTokens ?? 0;
|
|
619
|
-
if (Number.isFinite(budget) && acc.cost > budget) {
|
|
620
|
-
throw new BudgetExceededError(agentName, acc.cost, budget);
|
|
621
|
-
}
|
|
622
|
-
return;
|
|
623
|
-
}
|
|
624
|
-
if (event.type === "error") {
|
|
625
|
-
throw new DelegationError(agentName, asString2(event.message, "Unknown agent error"));
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
__name(processStreamEvent, "processStreamEvent");
|
|
629
976
|
async function delegate(SubAgentClass, message, opts = {}) {
|
|
630
977
|
const apiKey = requireApiKey(opts, SubAgentClass.name);
|
|
631
978
|
const walk = walkAgentMetadata(SubAgentClass, []);
|
|
@@ -636,26 +983,17 @@ async function delegate(SubAgentClass, message, opts = {}) {
|
|
|
636
983
|
const compiled = compileAgent(walk, toolboxInstances);
|
|
637
984
|
const allTools = mergeTools(opts.parentTools ?? [], compiled.tools);
|
|
638
985
|
const budget = Math.min(opts.budget ?? Infinity, opts.parentBudgetRemaining ?? Infinity);
|
|
639
|
-
const streamFactory = createSdkAgentStream(
|
|
986
|
+
const streamFactory = createSdkAgentStream(compiled, allTools, apiKey, walk.agentConfig.model);
|
|
640
987
|
const sessionId = opts.sessionId ?? `sub-${crypto.randomUUID()}`;
|
|
641
|
-
const
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
}
|
|
651
|
-
} catch (err) {
|
|
652
|
-
if (err instanceof BudgetExceededError || err instanceof DelegationError) throw err;
|
|
653
|
-
throw new DelegationError(SubAgentClass.name, err);
|
|
654
|
-
}
|
|
655
|
-
if (Number.isFinite(budget) && acc.cost > budget) {
|
|
656
|
-
throw new BudgetExceededError(SubAgentClass.name, acc.cost, budget);
|
|
657
|
-
}
|
|
658
|
-
return acc;
|
|
988
|
+
const loopStrategy = resolveLoopStrategy(walk.mainLoop.strategy, walk.mainLoop.maxIterations);
|
|
989
|
+
const reflection = loopStrategy.name === "plan-act-reflect" ? ladderReflectionStrategy : noopReflectionStrategy;
|
|
990
|
+
return runReflectiveLoop(streamFactory, message, sessionId, {
|
|
991
|
+
loop: loopStrategy,
|
|
992
|
+
reflection,
|
|
993
|
+
budget,
|
|
994
|
+
agentName: SubAgentClass.name,
|
|
995
|
+
signal: opts.signal
|
|
996
|
+
});
|
|
659
997
|
}
|
|
660
998
|
__name(delegate, "delegate");
|
|
661
999
|
|
|
@@ -786,9 +1124,13 @@ __name(matchRoute, "matchRoute");
|
|
|
786
1124
|
export {
|
|
787
1125
|
createAgentExecutionContext,
|
|
788
1126
|
isAgentContext,
|
|
1127
|
+
compileContextWindow,
|
|
1128
|
+
projectContextMetadataOnlyKnobs,
|
|
1129
|
+
compileProjectContext,
|
|
789
1130
|
AgentWarningCode,
|
|
790
1131
|
walkAgentMetadata,
|
|
791
1132
|
validateUniqueRoutes,
|
|
1133
|
+
compileSkills,
|
|
792
1134
|
compileTools,
|
|
793
1135
|
compileAgent,
|
|
794
1136
|
streamAgentResponse,
|
|
@@ -801,10 +1143,18 @@ export {
|
|
|
801
1143
|
generateAgentRoutes,
|
|
802
1144
|
translateSdkEvent,
|
|
803
1145
|
createSdkAgentStream,
|
|
1146
|
+
DEFAULT_MAX_ITERATIONS,
|
|
1147
|
+
loopStrategyConfigSchema,
|
|
1148
|
+
resolveLoopStrategy,
|
|
1149
|
+
reflectionStrategyConfigSchema,
|
|
1150
|
+
ladderReflectionStrategy,
|
|
1151
|
+
noopReflectionStrategy,
|
|
804
1152
|
BudgetExceededError,
|
|
805
1153
|
DelegationError,
|
|
1154
|
+
AgentRunner,
|
|
1155
|
+
AgentRunnerBuilder,
|
|
806
1156
|
delegate,
|
|
807
1157
|
generateAgentManifest,
|
|
808
1158
|
agentsPlugin
|
|
809
1159
|
};
|
|
810
|
-
//# sourceMappingURL=chunk-
|
|
1160
|
+
//# sourceMappingURL=chunk-KTYBQ7HY.js.map
|