@theokit/agents 0.45.0 → 0.47.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/LICENSE +201 -0
- package/dist/{bridge-entry-DQJMvdRy.d.ts → bridge-entry-BQ6UUSLZ.d.ts} +69 -9
- package/dist/bridge.d.ts +1 -1
- package/dist/bridge.js +3 -3
- package/dist/{chunk-XRPFOFYF.js → chunk-FEH7JFH7.js} +864 -354
- package/dist/chunk-FEH7JFH7.js.map +1 -0
- package/dist/index.d.ts +108 -4
- package/dist/index.js +234 -3
- package/dist/index.js.map +1 -1
- package/package.json +11 -9
- package/dist/chunk-XRPFOFYF.js.map +0 -1
|
@@ -27,6 +27,142 @@ import {
|
|
|
27
27
|
__name
|
|
28
28
|
} from "./chunk-7QVYU63E.js";
|
|
29
29
|
|
|
30
|
+
// src/bridge/define-agent.ts
|
|
31
|
+
var AGENT_BRAND = /* @__PURE__ */ Symbol.for("theokit.agent.definition");
|
|
32
|
+
function defineAgent(config) {
|
|
33
|
+
return {
|
|
34
|
+
...config,
|
|
35
|
+
[AGENT_BRAND]: true
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
__name(defineAgent, "defineAgent");
|
|
39
|
+
function isAgentDefinition(value) {
|
|
40
|
+
return typeof value === "object" && value !== null && value[AGENT_BRAND] === true;
|
|
41
|
+
}
|
|
42
|
+
__name(isAgentDefinition, "isAgentDefinition");
|
|
43
|
+
function toCompiledTool(tool) {
|
|
44
|
+
const handler = tool.handler;
|
|
45
|
+
const compiled = {
|
|
46
|
+
name: tool.name,
|
|
47
|
+
description: tool.description,
|
|
48
|
+
inputSchema: tool.inputSchema,
|
|
49
|
+
handler: /* @__PURE__ */ __name((input, ctx) => handler(input, ctx), "handler")
|
|
50
|
+
};
|
|
51
|
+
for (const sym of Object.getOwnPropertySymbols(tool)) {
|
|
52
|
+
;
|
|
53
|
+
compiled[sym] = tool[sym];
|
|
54
|
+
}
|
|
55
|
+
return compiled;
|
|
56
|
+
}
|
|
57
|
+
__name(toCompiledTool, "toCompiledTool");
|
|
58
|
+
function compileAgentDefinition(def) {
|
|
59
|
+
return {
|
|
60
|
+
model: def.model,
|
|
61
|
+
reasoningEffort: def.reasoningEffort,
|
|
62
|
+
systemPrompt: def.system,
|
|
63
|
+
tools: (def.tools ?? []).map(toCompiledTool),
|
|
64
|
+
agents: {},
|
|
65
|
+
stream: true,
|
|
66
|
+
// M7 — run-context flows to CompiledAgentOptions.runContext (distinct from the
|
|
67
|
+
// context-window `context` field the decorator path uses); absent ⇒ no key.
|
|
68
|
+
...def.context !== void 0 ? {
|
|
69
|
+
runContext: def.context
|
|
70
|
+
} : {},
|
|
71
|
+
// M9 — guardrails flow through unchanged; the runner applies them at the input boundary.
|
|
72
|
+
...def.guardrails !== void 0 ? {
|
|
73
|
+
guardrails: def.guardrails
|
|
74
|
+
} : {},
|
|
75
|
+
// M14 — HITL approvals compile into the same `hitl` map the decorator path produces.
|
|
76
|
+
...def.approvals !== void 0 ? {
|
|
77
|
+
hitl: compileApprovals(def)
|
|
78
|
+
} : {},
|
|
79
|
+
// M13 — skills: a static list → SDK skills.enabled; a resolver → carried for the request path.
|
|
80
|
+
...compileSkillsSelection(def.skills),
|
|
81
|
+
// theokit-file-based-config — the declared `.theokit/` sources flow to the run path, which
|
|
82
|
+
// projects them into `Agent.create({ local.settingSources })`; absent ⇒ inline config only.
|
|
83
|
+
...def.settingSources !== void 0 ? {
|
|
84
|
+
settingSources: def.settingSources
|
|
85
|
+
} : {},
|
|
86
|
+
// M49 — memory flows to the projection layer; `assembleM8CreateOptions` forwards it to Agent.create.
|
|
87
|
+
...def.memory !== void 0 ? {
|
|
88
|
+
memory: def.memory
|
|
89
|
+
} : {},
|
|
90
|
+
// Hooks are converted here — the layer EVERY path converges on — rather than on the builder, so
|
|
91
|
+
// `defineAgent({ hooks })` cannot type-check and silently no-op. A lifecycle hook that is
|
|
92
|
+
// declared but never registered is a security gate that does not gate.
|
|
93
|
+
...compileHooksAndPlugins(def),
|
|
94
|
+
// MCP — builder/`defineAgent` servers converge on the same `mcpServers` field the `@MCP`
|
|
95
|
+
// decorator path populates; the SDK adapter forwards it to `Agent.create({ mcpServers })`.
|
|
96
|
+
...def.mcpServers !== void 0 ? {
|
|
97
|
+
mcpServers: def.mcpServers
|
|
98
|
+
} : {}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
__name(compileAgentDefinition, "compileAgentDefinition");
|
|
102
|
+
function compileHooksAndPlugins(def) {
|
|
103
|
+
const map = def.hooks;
|
|
104
|
+
const entries = Object.entries(map ?? {}).filter(([, h]) => typeof h === "function");
|
|
105
|
+
const explicit = def.plugins ?? [];
|
|
106
|
+
if (entries.length === 0) {
|
|
107
|
+
return def.plugins !== void 0 ? {
|
|
108
|
+
plugins: def.plugins
|
|
109
|
+
} : {};
|
|
110
|
+
}
|
|
111
|
+
const plugin = {
|
|
112
|
+
name: "theokit-builder-hooks",
|
|
113
|
+
version: "1.0.0",
|
|
114
|
+
kind: "general",
|
|
115
|
+
register(ctx) {
|
|
116
|
+
for (const [hookName, handler] of entries) {
|
|
117
|
+
ctx.on(hookName, handler);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
plugins: [
|
|
123
|
+
...explicit,
|
|
124
|
+
plugin
|
|
125
|
+
]
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
__name(compileHooksAndPlugins, "compileHooksAndPlugins");
|
|
129
|
+
function compileSkillsSelection(skills) {
|
|
130
|
+
if (skills === void 0) return {};
|
|
131
|
+
if (typeof skills === "function") return {
|
|
132
|
+
skillsResolver: skills
|
|
133
|
+
};
|
|
134
|
+
const enabled = [];
|
|
135
|
+
const inline = [];
|
|
136
|
+
for (const entry of skills) {
|
|
137
|
+
if (typeof entry === "string") enabled.push(entry);
|
|
138
|
+
else inline.push(entry);
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
skills: {
|
|
142
|
+
enabled,
|
|
143
|
+
autoInject: true,
|
|
144
|
+
...inline.length > 0 ? {
|
|
145
|
+
inline
|
|
146
|
+
} : {}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
__name(compileSkillsSelection, "compileSkillsSelection");
|
|
151
|
+
function compileApprovals(def) {
|
|
152
|
+
const toolNames = new Set((def.tools ?? []).map((t) => t.name));
|
|
153
|
+
const gates = /* @__PURE__ */ new Map();
|
|
154
|
+
for (const [toolName, options] of Object.entries(def.approvals ?? {})) {
|
|
155
|
+
if (!toolNames.has(toolName)) {
|
|
156
|
+
throw new Error(`[@theokit/agents] defineAgent approval references unknown tool "${toolName}". Declared tools: ${[
|
|
157
|
+
...toolNames
|
|
158
|
+
].join(", ") || "(none)"}.`);
|
|
159
|
+
}
|
|
160
|
+
gates.set(toolName, options);
|
|
161
|
+
}
|
|
162
|
+
return gates;
|
|
163
|
+
}
|
|
164
|
+
__name(compileApprovals, "compileApprovals");
|
|
165
|
+
|
|
30
166
|
// src/bridge/agent-execution-context.ts
|
|
31
167
|
function createAgentExecutionContext(base, agent2, run, toolCall) {
|
|
32
168
|
return {
|
|
@@ -506,110 +642,6 @@ function generateAgentRoutes(ctx) {
|
|
|
506
642
|
}
|
|
507
643
|
__name(generateAgentRoutes, "generateAgentRoutes");
|
|
508
644
|
|
|
509
|
-
// src/bridge/define-agent.ts
|
|
510
|
-
var AGENT_BRAND = /* @__PURE__ */ Symbol.for("theokit.agent.definition");
|
|
511
|
-
function defineAgent(config) {
|
|
512
|
-
return {
|
|
513
|
-
...config,
|
|
514
|
-
[AGENT_BRAND]: true
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
__name(defineAgent, "defineAgent");
|
|
518
|
-
function isAgentDefinition(value) {
|
|
519
|
-
return typeof value === "object" && value !== null && value[AGENT_BRAND] === true;
|
|
520
|
-
}
|
|
521
|
-
__name(isAgentDefinition, "isAgentDefinition");
|
|
522
|
-
function toCompiledTool(tool) {
|
|
523
|
-
const handler = tool.handler;
|
|
524
|
-
const compiled = {
|
|
525
|
-
name: tool.name,
|
|
526
|
-
description: tool.description,
|
|
527
|
-
inputSchema: tool.inputSchema,
|
|
528
|
-
handler: /* @__PURE__ */ __name((input, ctx) => handler(input, ctx), "handler")
|
|
529
|
-
};
|
|
530
|
-
for (const sym of Object.getOwnPropertySymbols(tool)) {
|
|
531
|
-
;
|
|
532
|
-
compiled[sym] = tool[sym];
|
|
533
|
-
}
|
|
534
|
-
return compiled;
|
|
535
|
-
}
|
|
536
|
-
__name(toCompiledTool, "toCompiledTool");
|
|
537
|
-
function compileAgentDefinition(def) {
|
|
538
|
-
return {
|
|
539
|
-
model: def.model,
|
|
540
|
-
reasoningEffort: def.reasoningEffort,
|
|
541
|
-
systemPrompt: def.system,
|
|
542
|
-
tools: (def.tools ?? []).map(toCompiledTool),
|
|
543
|
-
agents: {},
|
|
544
|
-
stream: true,
|
|
545
|
-
// M7 — run-context flows to CompiledAgentOptions.runContext (distinct from the
|
|
546
|
-
// context-window `context` field the decorator path uses); absent ⇒ no key.
|
|
547
|
-
...def.context !== void 0 ? {
|
|
548
|
-
runContext: def.context
|
|
549
|
-
} : {},
|
|
550
|
-
// M9 — guardrails flow through unchanged; the runner applies them at the input boundary.
|
|
551
|
-
...def.guardrails !== void 0 ? {
|
|
552
|
-
guardrails: def.guardrails
|
|
553
|
-
} : {},
|
|
554
|
-
// M14 — HITL approvals compile into the same `hitl` map the decorator path produces.
|
|
555
|
-
...def.approvals !== void 0 ? {
|
|
556
|
-
hitl: compileApprovals(def)
|
|
557
|
-
} : {},
|
|
558
|
-
// M13 — skills: a static list → SDK skills.enabled; a resolver → carried for the request path.
|
|
559
|
-
...compileSkillsSelection(def.skills),
|
|
560
|
-
// theokit-file-based-config — the declared `.theokit/` sources flow to the run path, which
|
|
561
|
-
// projects them into `Agent.create({ local.settingSources })`; absent ⇒ inline config only.
|
|
562
|
-
...def.settingSources !== void 0 ? {
|
|
563
|
-
settingSources: def.settingSources
|
|
564
|
-
} : {},
|
|
565
|
-
...def.plugins !== void 0 ? {
|
|
566
|
-
plugins: def.plugins
|
|
567
|
-
} : {},
|
|
568
|
-
// MCP — builder/`defineAgent` servers converge on the same `mcpServers` field the `@MCP`
|
|
569
|
-
// decorator path populates; the SDK adapter forwards it to `Agent.create({ mcpServers })`.
|
|
570
|
-
...def.mcpServers !== void 0 ? {
|
|
571
|
-
mcpServers: def.mcpServers
|
|
572
|
-
} : {}
|
|
573
|
-
};
|
|
574
|
-
}
|
|
575
|
-
__name(compileAgentDefinition, "compileAgentDefinition");
|
|
576
|
-
function compileSkillsSelection(skills) {
|
|
577
|
-
if (skills === void 0) return {};
|
|
578
|
-
if (typeof skills === "function") return {
|
|
579
|
-
skillsResolver: skills
|
|
580
|
-
};
|
|
581
|
-
const enabled = [];
|
|
582
|
-
const inline = [];
|
|
583
|
-
for (const entry of skills) {
|
|
584
|
-
if (typeof entry === "string") enabled.push(entry);
|
|
585
|
-
else inline.push(entry);
|
|
586
|
-
}
|
|
587
|
-
return {
|
|
588
|
-
skills: {
|
|
589
|
-
enabled,
|
|
590
|
-
autoInject: true,
|
|
591
|
-
...inline.length > 0 ? {
|
|
592
|
-
inline
|
|
593
|
-
} : {}
|
|
594
|
-
}
|
|
595
|
-
};
|
|
596
|
-
}
|
|
597
|
-
__name(compileSkillsSelection, "compileSkillsSelection");
|
|
598
|
-
function compileApprovals(def) {
|
|
599
|
-
const toolNames = new Set((def.tools ?? []).map((t) => t.name));
|
|
600
|
-
const gates = /* @__PURE__ */ new Map();
|
|
601
|
-
for (const [toolName, options] of Object.entries(def.approvals ?? {})) {
|
|
602
|
-
if (!toolNames.has(toolName)) {
|
|
603
|
-
throw new Error(`[@theokit/agents] defineAgent approval references unknown tool "${toolName}". Declared tools: ${[
|
|
604
|
-
...toolNames
|
|
605
|
-
].join(", ") || "(none)"}.`);
|
|
606
|
-
}
|
|
607
|
-
gates.set(toolName, options);
|
|
608
|
-
}
|
|
609
|
-
return gates;
|
|
610
|
-
}
|
|
611
|
-
__name(compileApprovals, "compileApprovals");
|
|
612
|
-
|
|
613
645
|
// src/bridge/event-translator.ts
|
|
614
646
|
function asString(value, fallback) {
|
|
615
647
|
if (typeof value === "string") return value;
|
|
@@ -952,6 +984,21 @@ function assembleM8CreateOptions(compiled) {
|
|
|
952
984
|
options.mcpServers = compiled.mcpServers;
|
|
953
985
|
applied.push("mcpServers");
|
|
954
986
|
}
|
|
987
|
+
if (compiled.memory !== void 0) {
|
|
988
|
+
if ("enabled" in compiled.memory) {
|
|
989
|
+
options.memory = compiled.memory;
|
|
990
|
+
} else {
|
|
991
|
+
const dropped = Object.keys(compiled.memory);
|
|
992
|
+
if (dropped.length > 0) {
|
|
993
|
+
process.stderr.write(`[theokit-agents] @Memory decorator options not yet mapped to the SDK (${dropped.join(", ")}) \u2014 memory enabled with defaults
|
|
994
|
+
`);
|
|
995
|
+
}
|
|
996
|
+
options.memory = {
|
|
997
|
+
enabled: true
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
applied.push("memory");
|
|
1001
|
+
}
|
|
955
1002
|
return {
|
|
956
1003
|
options,
|
|
957
1004
|
applied
|
|
@@ -1360,252 +1407,722 @@ function createSdkAgentStream(compiled, compiledTools, apiKey, overrides = {}) {
|
|
|
1360
1407
|
};
|
|
1361
1408
|
}
|
|
1362
1409
|
}
|
|
1363
|
-
});
|
|
1364
|
-
}
|
|
1365
|
-
__name(createSdkAgentStream, "createSdkAgentStream");
|
|
1366
|
-
async function* streamSdkAgent(rt, compiled, sdkTools, opts) {
|
|
1367
|
-
const { Agent } = rt;
|
|
1368
|
-
const { apiKey, model, reasoningEffort, overrides, parseThinkTags, stripToolDialect, sessionId, message, factoryOpts, runId, t0 } = opts;
|
|
1369
|
-
const { options: m8, applied } = assembleM8CreateOptions(compiled);
|
|
1370
|
-
if (overrides.cwd !== void 0) m8.local = {
|
|
1371
|
-
...m8.local,
|
|
1372
|
-
cwd: overrides.cwd
|
|
1373
|
-
};
|
|
1374
|
-
if (overrides.baseDir !== void 0) m8.local = {
|
|
1375
|
-
...m8.local,
|
|
1376
|
-
baseDir: overrides.baseDir
|
|
1377
|
-
};
|
|
1378
|
-
const extra = buildExtraCreateOptions(overrides, compiled);
|
|
1379
|
-
if (applied.length > 0) {
|
|
1380
|
-
debugLog("[THEO_AGENT_M8_RUNTIME_APPLIED]", {
|
|
1381
|
-
skills: applied.includes("skills"),
|
|
1382
|
-
contextWindow: applied.includes("context"),
|
|
1383
|
-
projectContext: applied.includes("projectContext")
|
|
1410
|
+
});
|
|
1411
|
+
}
|
|
1412
|
+
__name(createSdkAgentStream, "createSdkAgentStream");
|
|
1413
|
+
async function* streamSdkAgent(rt, compiled, sdkTools, opts) {
|
|
1414
|
+
const { Agent } = rt;
|
|
1415
|
+
const { apiKey, model, reasoningEffort, overrides, parseThinkTags, stripToolDialect, sessionId, message, factoryOpts, runId, t0 } = opts;
|
|
1416
|
+
const { options: m8, applied } = assembleM8CreateOptions(compiled);
|
|
1417
|
+
if (overrides.cwd !== void 0) m8.local = {
|
|
1418
|
+
...m8.local,
|
|
1419
|
+
cwd: overrides.cwd
|
|
1420
|
+
};
|
|
1421
|
+
if (overrides.baseDir !== void 0) m8.local = {
|
|
1422
|
+
...m8.local,
|
|
1423
|
+
baseDir: overrides.baseDir
|
|
1424
|
+
};
|
|
1425
|
+
const extra = buildExtraCreateOptions(overrides, compiled);
|
|
1426
|
+
if (applied.length > 0) {
|
|
1427
|
+
debugLog("[THEO_AGENT_M8_RUNTIME_APPLIED]", {
|
|
1428
|
+
skills: applied.includes("skills"),
|
|
1429
|
+
contextWindow: applied.includes("context"),
|
|
1430
|
+
projectContext: applied.includes("projectContext")
|
|
1431
|
+
});
|
|
1432
|
+
}
|
|
1433
|
+
const agent2 = await Agent.getOrCreate(sessionId, {
|
|
1434
|
+
apiKey,
|
|
1435
|
+
model: buildModelSelection(model, reasoningEffort),
|
|
1436
|
+
tools: sdkTools,
|
|
1437
|
+
...m8,
|
|
1438
|
+
...extra
|
|
1439
|
+
});
|
|
1440
|
+
try {
|
|
1441
|
+
const queue = createAsyncQueue();
|
|
1442
|
+
const { state, onDelta } = createDeltaSink(queue);
|
|
1443
|
+
const sendOptions = {
|
|
1444
|
+
onDelta
|
|
1445
|
+
};
|
|
1446
|
+
if (factoryOpts?.disableTools === true) sendOptions.toolChoice = "none";
|
|
1447
|
+
const sendInput = overrides.images && overrides.images.length > 0 ? {
|
|
1448
|
+
text: message,
|
|
1449
|
+
images: overrides.images
|
|
1450
|
+
} : message;
|
|
1451
|
+
const sendPromise = agent2.send(sendInput, sendOptions);
|
|
1452
|
+
const openStream = /* @__PURE__ */ __name(async () => (await sendPromise).stream(), "openStream");
|
|
1453
|
+
const merged = mergeDeltaStream(queue, openStream, runId, state);
|
|
1454
|
+
for await (const event of applyTextTransforms(merged, {
|
|
1455
|
+
parseThinkTags,
|
|
1456
|
+
stripToolDialect
|
|
1457
|
+
})) {
|
|
1458
|
+
yield event;
|
|
1459
|
+
}
|
|
1460
|
+
if (!state.sawError) {
|
|
1461
|
+
yield realUsageDone(await (await sendPromise).wait(), t0);
|
|
1462
|
+
}
|
|
1463
|
+
} finally {
|
|
1464
|
+
await agent2.dispose();
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
__name(streamSdkAgent, "streamSdkAgent");
|
|
1468
|
+
function toAgentFactory(def, opts) {
|
|
1469
|
+
const compiled = compileAgentDefinition(def);
|
|
1470
|
+
const overrides = opts.overrides ?? {};
|
|
1471
|
+
const model = overrides.model ?? compiled.model ?? "openai/gpt-4o-mini";
|
|
1472
|
+
const reasoningEffort = overrides.reasoningEffort ?? compiled.reasoningEffort;
|
|
1473
|
+
const runContext = overrides.runContext ?? compiled.runContext;
|
|
1474
|
+
return async (sessionId) => {
|
|
1475
|
+
const rt = await loadSdkRuntime();
|
|
1476
|
+
if (!rt) {
|
|
1477
|
+
throw new Error("[@theokit/agents] @theokit/sdk is not installed \u2014 run: pnpm add @theokit/sdk");
|
|
1478
|
+
}
|
|
1479
|
+
const sdkTools = buildSdkTools(compiled.tools, rt.defineTool, overrides.sdkTools, runContext);
|
|
1480
|
+
const inlineSkills = compiled.skills?.inline;
|
|
1481
|
+
if (inlineSkills !== void 0 && inlineSkills.length > 0 && rt.defineSkillReadTool !== void 0 && !compiled.tools.some((t) => t.name === "skill_read")) {
|
|
1482
|
+
sdkTools.push(rt.defineSkillReadTool(inlineSkills));
|
|
1483
|
+
}
|
|
1484
|
+
const { options: m8 } = assembleM8CreateOptions(compiled);
|
|
1485
|
+
if (overrides.cwd !== void 0) m8.local = {
|
|
1486
|
+
...m8.local,
|
|
1487
|
+
cwd: overrides.cwd
|
|
1488
|
+
};
|
|
1489
|
+
if (overrides.baseDir !== void 0) m8.local = {
|
|
1490
|
+
...m8.local,
|
|
1491
|
+
baseDir: overrides.baseDir
|
|
1492
|
+
};
|
|
1493
|
+
const extra = buildExtraCreateOptions(overrides, compiled);
|
|
1494
|
+
const agent2 = await rt.Agent.getOrCreate(sessionId, {
|
|
1495
|
+
apiKey: opts.apiKey,
|
|
1496
|
+
model: buildModelSelection(model, reasoningEffort),
|
|
1497
|
+
tools: sdkTools,
|
|
1498
|
+
...m8,
|
|
1499
|
+
...extra
|
|
1500
|
+
});
|
|
1501
|
+
return agent2;
|
|
1502
|
+
};
|
|
1503
|
+
}
|
|
1504
|
+
__name(toAgentFactory, "toAgentFactory");
|
|
1505
|
+
|
|
1506
|
+
// ../presenter/dist/index.js
|
|
1507
|
+
var __defProp = Object.defineProperty;
|
|
1508
|
+
var __name2 = /* @__PURE__ */ __name((target, value) => __defProp(target, "name", {
|
|
1509
|
+
value,
|
|
1510
|
+
configurable: true
|
|
1511
|
+
}), "__name");
|
|
1512
|
+
var UnknownPresenterError = class extends Error {
|
|
1513
|
+
static {
|
|
1514
|
+
__name(this, "UnknownPresenterError");
|
|
1515
|
+
}
|
|
1516
|
+
static {
|
|
1517
|
+
__name2(this, "UnknownPresenterError");
|
|
1518
|
+
}
|
|
1519
|
+
name = "UnknownPresenterError";
|
|
1520
|
+
constructor(surface, known) {
|
|
1521
|
+
super(`No presenter registered for surface "${surface}". Known: ${known.join(", ") || "(none)"}.`);
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
var PresenterRegistry = class {
|
|
1525
|
+
static {
|
|
1526
|
+
__name(this, "PresenterRegistry");
|
|
1527
|
+
}
|
|
1528
|
+
static {
|
|
1529
|
+
__name2(this, "PresenterRegistry");
|
|
1530
|
+
}
|
|
1531
|
+
#presenters = /* @__PURE__ */ new Map();
|
|
1532
|
+
/** Register (or replace) the presenter for its surface. Returns `this` for fluent wiring. */
|
|
1533
|
+
register(presenter) {
|
|
1534
|
+
this.#presenters.set(presenter.surface, presenter);
|
|
1535
|
+
return this;
|
|
1536
|
+
}
|
|
1537
|
+
/** Whether a presenter is registered for `surface`. */
|
|
1538
|
+
has(surface) {
|
|
1539
|
+
return this.#presenters.has(surface);
|
|
1540
|
+
}
|
|
1541
|
+
/** The registered surface keys. */
|
|
1542
|
+
surfaces() {
|
|
1543
|
+
return [
|
|
1544
|
+
...this.#presenters.keys()
|
|
1545
|
+
];
|
|
1546
|
+
}
|
|
1547
|
+
/** Resolve the presenter for `surface`, or throw {@link UnknownPresenterError} (never returns undefined). */
|
|
1548
|
+
resolve(surface) {
|
|
1549
|
+
const p = this.#presenters.get(surface);
|
|
1550
|
+
if (p === void 0) throw new UnknownPresenterError(surface, this.surfaces());
|
|
1551
|
+
return p;
|
|
1552
|
+
}
|
|
1553
|
+
};
|
|
1554
|
+
function asString2(value, fallback) {
|
|
1555
|
+
return typeof value === "string" ? value : fallback;
|
|
1556
|
+
}
|
|
1557
|
+
__name(asString2, "asString");
|
|
1558
|
+
__name2(asString2, "asString");
|
|
1559
|
+
function serializeToolResult(value, fallback) {
|
|
1560
|
+
if (typeof value === "string") return value;
|
|
1561
|
+
if (value === void 0 || value === null) return fallback;
|
|
1562
|
+
try {
|
|
1563
|
+
return JSON.stringify(value);
|
|
1564
|
+
} catch {
|
|
1565
|
+
return typeof value === "bigint" ? value.toString() : fallback;
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
__name(serializeToolResult, "serializeToolResult");
|
|
1569
|
+
__name2(serializeToolResult, "serializeToolResult");
|
|
1570
|
+
function fromAssistant(msg) {
|
|
1571
|
+
const events = [];
|
|
1572
|
+
const content = msg.message?.content;
|
|
1573
|
+
if (!Array.isArray(content)) return events;
|
|
1574
|
+
for (const block of content) {
|
|
1575
|
+
const b = block;
|
|
1576
|
+
if (b.type === "text" && b.text) events.push({
|
|
1577
|
+
type: "text",
|
|
1578
|
+
text: b.text
|
|
1579
|
+
});
|
|
1580
|
+
if (b.type === "tool_use") {
|
|
1581
|
+
events.push({
|
|
1582
|
+
type: "tool-call",
|
|
1583
|
+
callId: b.id ?? `tc-${Date.now()}`,
|
|
1584
|
+
name: b.name ?? "unknown",
|
|
1585
|
+
input: b.input ?? {}
|
|
1586
|
+
});
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
return events;
|
|
1590
|
+
}
|
|
1591
|
+
__name(fromAssistant, "fromAssistant");
|
|
1592
|
+
__name2(fromAssistant, "fromAssistant");
|
|
1593
|
+
function fromToolUse(msg) {
|
|
1594
|
+
const status = msg.status;
|
|
1595
|
+
const callId = asString2(msg.call_id, `tc-${Date.now()}`);
|
|
1596
|
+
const name = asString2(msg.name, "unknown");
|
|
1597
|
+
if (status === "completed") {
|
|
1598
|
+
return [
|
|
1599
|
+
{
|
|
1600
|
+
type: "tool-result",
|
|
1601
|
+
callId,
|
|
1602
|
+
name,
|
|
1603
|
+
result: serializeToolResult(msg.result, ""),
|
|
1604
|
+
isError: false
|
|
1605
|
+
}
|
|
1606
|
+
];
|
|
1607
|
+
}
|
|
1608
|
+
if (status === "error") {
|
|
1609
|
+
return [
|
|
1610
|
+
{
|
|
1611
|
+
type: "tool-result",
|
|
1612
|
+
callId,
|
|
1613
|
+
name,
|
|
1614
|
+
result: serializeToolResult(msg.result, "Tool failed"),
|
|
1615
|
+
isError: true
|
|
1616
|
+
}
|
|
1617
|
+
];
|
|
1618
|
+
}
|
|
1619
|
+
if (status === "running") {
|
|
1620
|
+
return [
|
|
1621
|
+
{
|
|
1622
|
+
type: "tool-call",
|
|
1623
|
+
callId,
|
|
1624
|
+
name,
|
|
1625
|
+
input: msg.args ?? msg.input ?? msg.arguments ?? {}
|
|
1626
|
+
}
|
|
1627
|
+
];
|
|
1628
|
+
}
|
|
1629
|
+
return [];
|
|
1630
|
+
}
|
|
1631
|
+
__name(fromToolUse, "fromToolUse");
|
|
1632
|
+
__name2(fromToolUse, "fromToolUse");
|
|
1633
|
+
function fromStatus(msg) {
|
|
1634
|
+
const s = msg.status;
|
|
1635
|
+
if (s === "FINISHED" || s === "CANCELLED") return [
|
|
1636
|
+
{
|
|
1637
|
+
type: "finish",
|
|
1638
|
+
reason: s.toLowerCase()
|
|
1639
|
+
}
|
|
1640
|
+
];
|
|
1641
|
+
if (s === "ERROR" || s === "EXPIRED") {
|
|
1642
|
+
return [
|
|
1643
|
+
{
|
|
1644
|
+
type: "error",
|
|
1645
|
+
message: asString2(msg.message, "Agent error"),
|
|
1646
|
+
code: "AGENT_ERROR"
|
|
1647
|
+
}
|
|
1648
|
+
];
|
|
1649
|
+
}
|
|
1650
|
+
return [];
|
|
1651
|
+
}
|
|
1652
|
+
__name(fromStatus, "fromStatus");
|
|
1653
|
+
__name2(fromStatus, "fromStatus");
|
|
1654
|
+
function fromSdkMessage(msg) {
|
|
1655
|
+
switch (msg.type) {
|
|
1656
|
+
case "assistant":
|
|
1657
|
+
return fromAssistant(msg);
|
|
1658
|
+
case "tool_call":
|
|
1659
|
+
return fromToolUse(msg);
|
|
1660
|
+
case "thinking":
|
|
1661
|
+
return [
|
|
1662
|
+
{
|
|
1663
|
+
type: "reasoning",
|
|
1664
|
+
text: asString2(msg.text, "")
|
|
1665
|
+
}
|
|
1666
|
+
];
|
|
1667
|
+
case "status":
|
|
1668
|
+
return fromStatus(msg);
|
|
1669
|
+
// `system` / run-lifecycle is framework framing, not pure output — intentionally skipped (ADR-4).
|
|
1670
|
+
default:
|
|
1671
|
+
return [];
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
__name(fromSdkMessage, "fromSdkMessage");
|
|
1675
|
+
__name2(fromSdkMessage, "fromSdkMessage");
|
|
1676
|
+
function fromInteractionUpdate(update) {
|
|
1677
|
+
switch (update.type) {
|
|
1678
|
+
case "text-delta":
|
|
1679
|
+
return update.text ? [
|
|
1680
|
+
{
|
|
1681
|
+
type: "text",
|
|
1682
|
+
text: update.text
|
|
1683
|
+
}
|
|
1684
|
+
] : [];
|
|
1685
|
+
case "thinking-delta":
|
|
1686
|
+
return update.text ? [
|
|
1687
|
+
{
|
|
1688
|
+
type: "reasoning",
|
|
1689
|
+
text: update.text
|
|
1690
|
+
}
|
|
1691
|
+
] : [];
|
|
1692
|
+
case "tool-call-started":
|
|
1693
|
+
return [
|
|
1694
|
+
{
|
|
1695
|
+
type: "tool-call",
|
|
1696
|
+
callId: update.callId,
|
|
1697
|
+
name: update.toolCall.name,
|
|
1698
|
+
input: update.toolCall.args ?? {}
|
|
1699
|
+
}
|
|
1700
|
+
];
|
|
1701
|
+
case "partial-tool-call":
|
|
1702
|
+
return [
|
|
1703
|
+
{
|
|
1704
|
+
type: "partial-tool-call",
|
|
1705
|
+
callId: update.callId,
|
|
1706
|
+
name: update.toolCall.name,
|
|
1707
|
+
input: update.toolCall.args ?? {}
|
|
1708
|
+
}
|
|
1709
|
+
];
|
|
1710
|
+
case "tool-call-completed":
|
|
1711
|
+
return [
|
|
1712
|
+
{
|
|
1713
|
+
type: "tool-result",
|
|
1714
|
+
callId: update.callId,
|
|
1715
|
+
name: update.toolCall.name,
|
|
1716
|
+
result: serializeToolResult(update.toolCall.result, ""),
|
|
1717
|
+
isError: false
|
|
1718
|
+
}
|
|
1719
|
+
];
|
|
1720
|
+
default:
|
|
1721
|
+
return [];
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
__name(fromInteractionUpdate, "fromInteractionUpdate");
|
|
1725
|
+
__name2(fromInteractionUpdate, "fromInteractionUpdate");
|
|
1726
|
+
var UIMessageStreamPresenter = class {
|
|
1727
|
+
static {
|
|
1728
|
+
__name(this, "UIMessageStreamPresenter");
|
|
1729
|
+
}
|
|
1730
|
+
static {
|
|
1731
|
+
__name2(this, "UIMessageStreamPresenter");
|
|
1732
|
+
}
|
|
1733
|
+
surface = "ui-message-stream";
|
|
1734
|
+
#textId;
|
|
1735
|
+
#openBlock = null;
|
|
1736
|
+
#reasoningId = null;
|
|
1737
|
+
#seen = /* @__PURE__ */ new Set();
|
|
1738
|
+
constructor(options) {
|
|
1739
|
+
this.#textId = options.textId;
|
|
1740
|
+
}
|
|
1741
|
+
/** Emit the opening `start` chunk (exactly once, before any event). */
|
|
1742
|
+
start() {
|
|
1743
|
+
return [
|
|
1744
|
+
{
|
|
1745
|
+
type: "start"
|
|
1746
|
+
}
|
|
1747
|
+
];
|
|
1748
|
+
}
|
|
1749
|
+
present(event) {
|
|
1750
|
+
switch (event.type) {
|
|
1751
|
+
case "text":
|
|
1752
|
+
return this.#emitTextDelta(event.text);
|
|
1753
|
+
case "reasoning":
|
|
1754
|
+
return this.#emitReasoningDelta(event.text);
|
|
1755
|
+
case "tool-call":
|
|
1756
|
+
return [
|
|
1757
|
+
...this.#closeOpenBlock(),
|
|
1758
|
+
...this.#emitToolCall(event.callId, event.name, event.input)
|
|
1759
|
+
];
|
|
1760
|
+
case "tool-result":
|
|
1761
|
+
return [
|
|
1762
|
+
...this.#closeOpenBlock(),
|
|
1763
|
+
...this.#emitToolResult(event.callId, event.name, event.result, event.isError ?? false)
|
|
1764
|
+
];
|
|
1765
|
+
case "error":
|
|
1766
|
+
return [
|
|
1767
|
+
{
|
|
1768
|
+
type: "error",
|
|
1769
|
+
errorText: event.message
|
|
1770
|
+
}
|
|
1771
|
+
];
|
|
1772
|
+
// `partial-tool-call` streams incremental args — the current web path emits no chunk for it
|
|
1773
|
+
// (args are shown on the committed `tool-call`); `finish` / `status` are handled by finish()/host.
|
|
1774
|
+
default:
|
|
1775
|
+
return [];
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
/**
|
|
1779
|
+
* Close any open text/reasoning block and emit the terminal `finish` chunk. When `metadata` is given
|
|
1780
|
+
* (a clean run's turn totals from the framework `done`), it rides `messageMetadata`; otherwise the
|
|
1781
|
+
* finish is bare (error/abort turns), byte-identical to the original translator.
|
|
1782
|
+
*/
|
|
1783
|
+
finish(metadata) {
|
|
1784
|
+
const out = this.#closeOpenBlock();
|
|
1785
|
+
out.push(metadata ? {
|
|
1786
|
+
type: "finish",
|
|
1787
|
+
messageMetadata: metadata
|
|
1788
|
+
} : {
|
|
1789
|
+
type: "finish"
|
|
1384
1790
|
});
|
|
1791
|
+
return out;
|
|
1385
1792
|
}
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1793
|
+
/**
|
|
1794
|
+
* Close any open text/reasoning block WITHOUT finishing the stream. The host calls this before
|
|
1795
|
+
* interleaving a framework chunk (HITL approval / checkpoint) that must not appear inside an open text
|
|
1796
|
+
* block — mirroring the original translator's `closeOpenBlock` before those chunks. Idempotent.
|
|
1797
|
+
*/
|
|
1798
|
+
closeBlock() {
|
|
1799
|
+
return this.#closeOpenBlock();
|
|
1800
|
+
}
|
|
1801
|
+
/**
|
|
1802
|
+
* Whether a `callId` has already been introduced (a `tool-call` or a synthesized `tool-input`). The host
|
|
1803
|
+
* uses this so a framework `approval_required` reuses the EC-1 synthesize-input-first rule exactly once.
|
|
1804
|
+
*/
|
|
1805
|
+
hasSeen(callId) {
|
|
1806
|
+
return this.#seen.has(callId);
|
|
1807
|
+
}
|
|
1808
|
+
/** Mark a `callId` as introduced (the host synthesized its `tool-input` for a framework chunk). */
|
|
1809
|
+
markSeen(callId) {
|
|
1810
|
+
this.#seen.add(callId);
|
|
1811
|
+
}
|
|
1812
|
+
// --- internal open-block state machine (verbatim from the original translator) ---
|
|
1813
|
+
#closeOpenBlock() {
|
|
1814
|
+
const out = [];
|
|
1815
|
+
if (this.#openBlock === "text") {
|
|
1816
|
+
out.push({
|
|
1817
|
+
type: "text-end",
|
|
1818
|
+
id: this.#textId
|
|
1819
|
+
});
|
|
1820
|
+
} else if (this.#openBlock === "reasoning" && this.#reasoningId) {
|
|
1821
|
+
out.push({
|
|
1822
|
+
type: "reasoning-end",
|
|
1823
|
+
id: this.#reasoningId
|
|
1824
|
+
});
|
|
1411
1825
|
}
|
|
1412
|
-
|
|
1413
|
-
|
|
1826
|
+
this.#openBlock = null;
|
|
1827
|
+
this.#reasoningId = null;
|
|
1828
|
+
return out;
|
|
1414
1829
|
}
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
return async (sessionId) => {
|
|
1424
|
-
const rt = await loadSdkRuntime();
|
|
1425
|
-
if (!rt) {
|
|
1426
|
-
throw new Error("[@theokit/agents] @theokit/sdk is not installed \u2014 run: pnpm add @theokit/sdk");
|
|
1830
|
+
#emitTextDelta(content) {
|
|
1831
|
+
const out = [];
|
|
1832
|
+
if (this.#openBlock !== "text") {
|
|
1833
|
+
out.push(...this.#closeOpenBlock(), {
|
|
1834
|
+
type: "text-start",
|
|
1835
|
+
id: this.#textId
|
|
1836
|
+
});
|
|
1837
|
+
this.#openBlock = "text";
|
|
1427
1838
|
}
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1839
|
+
out.push({
|
|
1840
|
+
type: "text-delta",
|
|
1841
|
+
id: this.#textId,
|
|
1842
|
+
delta: content
|
|
1843
|
+
});
|
|
1844
|
+
return out;
|
|
1845
|
+
}
|
|
1846
|
+
#emitReasoningDelta(content) {
|
|
1847
|
+
const out = [];
|
|
1848
|
+
let reasoningId = this.#openBlock === "reasoning" ? this.#reasoningId : null;
|
|
1849
|
+
if (reasoningId === null) {
|
|
1850
|
+
out.push(...this.#closeOpenBlock());
|
|
1851
|
+
reasoningId = crypto.randomUUID();
|
|
1852
|
+
this.#reasoningId = reasoningId;
|
|
1853
|
+
out.push({
|
|
1854
|
+
type: "reasoning-start",
|
|
1855
|
+
id: reasoningId
|
|
1856
|
+
});
|
|
1857
|
+
this.#openBlock = "reasoning";
|
|
1432
1858
|
}
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
};
|
|
1438
|
-
if (overrides.baseDir !== void 0) m8.local = {
|
|
1439
|
-
...m8.local,
|
|
1440
|
-
baseDir: overrides.baseDir
|
|
1441
|
-
};
|
|
1442
|
-
const extra = buildExtraCreateOptions(overrides, compiled);
|
|
1443
|
-
const agent2 = await rt.Agent.getOrCreate(sessionId, {
|
|
1444
|
-
apiKey: opts.apiKey,
|
|
1445
|
-
model: buildModelSelection(model, reasoningEffort),
|
|
1446
|
-
tools: sdkTools,
|
|
1447
|
-
...m8,
|
|
1448
|
-
...extra
|
|
1859
|
+
out.push({
|
|
1860
|
+
type: "reasoning-delta",
|
|
1861
|
+
id: reasoningId,
|
|
1862
|
+
delta: content
|
|
1449
1863
|
});
|
|
1450
|
-
return
|
|
1451
|
-
};
|
|
1452
|
-
}
|
|
1453
|
-
__name(toAgentFactory, "toAgentFactory");
|
|
1454
|
-
|
|
1455
|
-
// src/bridge/ui-message-stream-translator.ts
|
|
1456
|
-
function* closeOpenBlock(state, textId) {
|
|
1457
|
-
if (state.openBlock === "text") {
|
|
1458
|
-
yield {
|
|
1459
|
-
type: "text-end",
|
|
1460
|
-
id: textId
|
|
1461
|
-
};
|
|
1462
|
-
} else if (state.openBlock === "reasoning" && state.reasoningId) {
|
|
1463
|
-
yield {
|
|
1464
|
-
type: "reasoning-end",
|
|
1465
|
-
id: state.reasoningId
|
|
1466
|
-
};
|
|
1864
|
+
return out;
|
|
1467
1865
|
}
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
state.openBlock = "text";
|
|
1866
|
+
#emitToolCall(callId, name, input) {
|
|
1867
|
+
this.#seen.add(callId);
|
|
1868
|
+
return [
|
|
1869
|
+
{
|
|
1870
|
+
type: "tool-input-available",
|
|
1871
|
+
toolCallId: callId,
|
|
1872
|
+
toolName: name,
|
|
1873
|
+
input,
|
|
1874
|
+
dynamic: true
|
|
1875
|
+
}
|
|
1876
|
+
];
|
|
1480
1877
|
}
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1878
|
+
#emitToolResult(callId, name, result, isError2) {
|
|
1879
|
+
const out = [];
|
|
1880
|
+
if (!this.#seen.has(callId)) {
|
|
1881
|
+
this.#seen.add(callId);
|
|
1882
|
+
out.push({
|
|
1883
|
+
type: "tool-input-available",
|
|
1884
|
+
toolCallId: callId,
|
|
1885
|
+
toolName: name,
|
|
1886
|
+
input: {},
|
|
1887
|
+
dynamic: true
|
|
1888
|
+
});
|
|
1889
|
+
}
|
|
1890
|
+
const output = typeof result === "string" ? result : "";
|
|
1891
|
+
if (isError2) {
|
|
1892
|
+
out.push({
|
|
1893
|
+
type: "tool-output-error",
|
|
1894
|
+
toolCallId: callId,
|
|
1895
|
+
errorText: output
|
|
1896
|
+
});
|
|
1897
|
+
} else {
|
|
1898
|
+
out.push({
|
|
1899
|
+
type: "tool-output-available",
|
|
1900
|
+
toolCallId: callId,
|
|
1901
|
+
output
|
|
1902
|
+
});
|
|
1903
|
+
}
|
|
1904
|
+
return out;
|
|
1905
|
+
}
|
|
1906
|
+
};
|
|
1907
|
+
var ANSI = {
|
|
1908
|
+
text: "",
|
|
1909
|
+
reasoning: "\x1B[2m",
|
|
1910
|
+
tool: "\x1B[36m",
|
|
1911
|
+
"tool-result": "\x1B[2m",
|
|
1912
|
+
"tool-error": "\x1B[31m",
|
|
1913
|
+
error: "\x1B[31m",
|
|
1914
|
+
status: "\x1B[35m",
|
|
1915
|
+
finish: "\x1B[2m"
|
|
1916
|
+
};
|
|
1917
|
+
var RESET = "\x1B[0m";
|
|
1918
|
+
function preview(value, max) {
|
|
1919
|
+
const raw = typeof value === "string" ? value : safeJson(value);
|
|
1920
|
+
const flat = raw.replace(/\s+/g, " ").trim();
|
|
1921
|
+
return flat.length > max ? `${flat.slice(0, max - 1)}\u2026` : flat;
|
|
1922
|
+
}
|
|
1923
|
+
__name(preview, "preview");
|
|
1924
|
+
__name2(preview, "preview");
|
|
1925
|
+
function safeJson(value) {
|
|
1926
|
+
if (value === void 0 || value === null) return "";
|
|
1927
|
+
try {
|
|
1928
|
+
return JSON.stringify(value);
|
|
1929
|
+
} catch {
|
|
1930
|
+
return typeof value === "bigint" ? value.toString() : "";
|
|
1499
1931
|
}
|
|
1500
|
-
yield {
|
|
1501
|
-
type: "reasoning-delta",
|
|
1502
|
-
id: reasoningId,
|
|
1503
|
-
delta: content
|
|
1504
|
-
};
|
|
1505
1932
|
}
|
|
1506
|
-
__name(
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
type: "tool-input-available",
|
|
1511
|
-
toolCallId: event.callId,
|
|
1512
|
-
toolName: event.toolName,
|
|
1513
|
-
input: event.input,
|
|
1514
|
-
dynamic: true
|
|
1515
|
-
};
|
|
1933
|
+
__name(safeJson, "safeJson");
|
|
1934
|
+
__name2(safeJson, "safeJson");
|
|
1935
|
+
function suffix(open, value, close) {
|
|
1936
|
+
return value === void 0 ? "" : open + value + close;
|
|
1516
1937
|
}
|
|
1517
|
-
__name(
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
type: "tool-input-available",
|
|
1523
|
-
toolCallId: event.callId,
|
|
1524
|
-
toolName: event.toolName,
|
|
1525
|
-
input: {},
|
|
1526
|
-
dynamic: true
|
|
1527
|
-
};
|
|
1938
|
+
__name(suffix, "suffix");
|
|
1939
|
+
__name2(suffix, "suffix");
|
|
1940
|
+
var TerminalPresenter = class {
|
|
1941
|
+
static {
|
|
1942
|
+
__name(this, "TerminalPresenter");
|
|
1528
1943
|
}
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1944
|
+
static {
|
|
1945
|
+
__name2(this, "TerminalPresenter");
|
|
1946
|
+
}
|
|
1947
|
+
surface = "terminal";
|
|
1948
|
+
#ansi;
|
|
1949
|
+
#max;
|
|
1950
|
+
constructor(options = {}) {
|
|
1951
|
+
this.#ansi = options.ansi ?? false;
|
|
1952
|
+
this.#max = options.maxPreview ?? 88;
|
|
1953
|
+
}
|
|
1954
|
+
present(event) {
|
|
1955
|
+
switch (event.type) {
|
|
1956
|
+
case "text":
|
|
1957
|
+
return event.text.length > 0 ? [
|
|
1958
|
+
this.#row("text", event.text)
|
|
1959
|
+
] : [];
|
|
1960
|
+
case "reasoning":
|
|
1961
|
+
return event.text.length > 0 ? [
|
|
1962
|
+
this.#row("reasoning", `\xB7 ${event.text}`)
|
|
1963
|
+
] : [];
|
|
1964
|
+
case "tool-call":
|
|
1965
|
+
return [
|
|
1966
|
+
this.#row("tool", `\u23FA ${event.name}(${preview(event.input, this.#max)})`)
|
|
1967
|
+
];
|
|
1968
|
+
case "tool-result":
|
|
1969
|
+
return [
|
|
1970
|
+
this.#toolResult(event.isError === true, preview(event.result, this.#max))
|
|
1971
|
+
];
|
|
1972
|
+
case "error":
|
|
1973
|
+
return [
|
|
1974
|
+
this.#row("error", `\u2716 ${event.message}${suffix(" (", event.code, ")")}`)
|
|
1975
|
+
];
|
|
1976
|
+
case "status":
|
|
1977
|
+
return [
|
|
1978
|
+
this.#row("status", `\u25CF ${event.status}${suffix(" \u2014 ", event.detail, "")}`)
|
|
1979
|
+
];
|
|
1980
|
+
case "finish":
|
|
1981
|
+
return [
|
|
1982
|
+
this.#row("finish", this.#finishText(event.reason, event.usage?.totalTokens))
|
|
1983
|
+
];
|
|
1984
|
+
// `partial-tool-call` streams incremental args — the committed `tool-call` renders them.
|
|
1985
|
+
default:
|
|
1986
|
+
return [];
|
|
1987
|
+
}
|
|
1541
1988
|
}
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1989
|
+
#toolResult(isError2, body) {
|
|
1990
|
+
return this.#row(isError2 ? "tool-error" : "tool-result", ` \u23BF ${body}`);
|
|
1991
|
+
}
|
|
1992
|
+
#finishText(reason, tokens) {
|
|
1993
|
+
const r = suffix(" ", reason, "");
|
|
1994
|
+
const t = tokens === void 0 ? "" : ` \xB7 ${tokens} tokens`;
|
|
1995
|
+
return `<<${r}${t} >>`;
|
|
1996
|
+
}
|
|
1997
|
+
#row(kind, text) {
|
|
1998
|
+
return {
|
|
1999
|
+
kind,
|
|
2000
|
+
text: this.#ansi && ANSI[kind] !== "" ? `${ANSI[kind]}${text}${RESET}` : text
|
|
1553
2001
|
};
|
|
1554
2002
|
}
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
}
|
|
2003
|
+
};
|
|
2004
|
+
var JsonPresenter = class {
|
|
2005
|
+
static {
|
|
2006
|
+
__name(this, "JsonPresenter");
|
|
2007
|
+
}
|
|
2008
|
+
static {
|
|
2009
|
+
__name2(this, "JsonPresenter");
|
|
2010
|
+
}
|
|
2011
|
+
surface = "json";
|
|
2012
|
+
#ns;
|
|
2013
|
+
constructor(options = {}) {
|
|
2014
|
+
this.#ns = options.namespace ?? "agent.";
|
|
2015
|
+
}
|
|
2016
|
+
present(event) {
|
|
2017
|
+
const { type, ...payload } = event;
|
|
2018
|
+
return [
|
|
2019
|
+
{
|
|
2020
|
+
type: `${this.#ns}${type}`,
|
|
2021
|
+
...payload
|
|
2022
|
+
}
|
|
2023
|
+
];
|
|
2024
|
+
}
|
|
2025
|
+
};
|
|
2026
|
+
|
|
2027
|
+
// src/bridge/present-ui-message-stream.ts
|
|
2028
|
+
function toAgentOutputEvent(e) {
|
|
2029
|
+
switch (e.type) {
|
|
2030
|
+
case "text_delta":
|
|
2031
|
+
return {
|
|
2032
|
+
type: "text",
|
|
2033
|
+
text: e.content
|
|
2034
|
+
};
|
|
2035
|
+
case "thinking":
|
|
2036
|
+
return {
|
|
2037
|
+
type: "reasoning",
|
|
2038
|
+
text: e.content
|
|
2039
|
+
};
|
|
2040
|
+
case "tool_call":
|
|
2041
|
+
return {
|
|
2042
|
+
type: "tool-call",
|
|
2043
|
+
callId: e.callId,
|
|
2044
|
+
name: e.toolName,
|
|
2045
|
+
input: e.input
|
|
2046
|
+
};
|
|
2047
|
+
case "tool_result":
|
|
2048
|
+
return {
|
|
2049
|
+
type: "tool-result",
|
|
2050
|
+
callId: e.callId,
|
|
2051
|
+
name: e.toolName,
|
|
2052
|
+
result: e.output,
|
|
2053
|
+
isError: e.isError
|
|
2054
|
+
};
|
|
2055
|
+
default:
|
|
2056
|
+
return null;
|
|
2057
|
+
}
|
|
1560
2058
|
}
|
|
1561
|
-
__name(
|
|
1562
|
-
function
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
transient: true
|
|
2059
|
+
__name(toAgentOutputEvent, "toAgentOutputEvent");
|
|
2060
|
+
function doneToMetadata(event) {
|
|
2061
|
+
return event.cost === void 0 ? {
|
|
2062
|
+
usage: event.usage,
|
|
2063
|
+
durationMs: event.durationMs
|
|
2064
|
+
} : {
|
|
2065
|
+
usage: event.usage,
|
|
2066
|
+
durationMs: event.durationMs,
|
|
2067
|
+
cost: event.cost
|
|
1571
2068
|
};
|
|
1572
2069
|
}
|
|
1573
|
-
__name(
|
|
1574
|
-
async function*
|
|
2070
|
+
__name(doneToMetadata, "doneToMetadata");
|
|
2071
|
+
async function* presentUIMessageStream(events, opts) {
|
|
2072
|
+
const presenter = new UIMessageStreamPresenter({
|
|
2073
|
+
textId: opts.textId
|
|
2074
|
+
});
|
|
1575
2075
|
yield {
|
|
1576
2076
|
type: "start"
|
|
1577
2077
|
};
|
|
1578
|
-
const state = {
|
|
1579
|
-
openBlock: null,
|
|
1580
|
-
reasoningId: null
|
|
1581
|
-
};
|
|
1582
|
-
const seenToolCallIds = /* @__PURE__ */ new Set();
|
|
1583
2078
|
let turnMetadata;
|
|
1584
2079
|
try {
|
|
1585
2080
|
for await (const event of events) {
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
}
|
|
1591
|
-
|
|
1592
|
-
yield*
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
2081
|
+
const output = toAgentOutputEvent(event);
|
|
2082
|
+
if (output !== null) {
|
|
2083
|
+
yield* presenter.present(output);
|
|
2084
|
+
continue;
|
|
2085
|
+
}
|
|
2086
|
+
if (event.type === "approval_required") {
|
|
2087
|
+
yield* presenter.closeBlock();
|
|
2088
|
+
if (!presenter.hasSeen(event.callId)) {
|
|
2089
|
+
presenter.markSeen(event.callId);
|
|
2090
|
+
yield {
|
|
2091
|
+
type: "tool-input-available",
|
|
2092
|
+
toolCallId: event.callId,
|
|
2093
|
+
toolName: event.toolName,
|
|
2094
|
+
input: event.input ?? {},
|
|
2095
|
+
dynamic: true
|
|
2096
|
+
};
|
|
2097
|
+
}
|
|
2098
|
+
yield {
|
|
2099
|
+
type: "tool-approval-request",
|
|
2100
|
+
approvalId: event.callId,
|
|
2101
|
+
toolCallId: event.callId
|
|
2102
|
+
};
|
|
2103
|
+
continue;
|
|
2104
|
+
}
|
|
2105
|
+
if (event.type === "checkpoint_saved") {
|
|
2106
|
+
yield* presenter.closeBlock();
|
|
2107
|
+
yield {
|
|
2108
|
+
type: "data-checkpoint",
|
|
2109
|
+
data: {
|
|
2110
|
+
checkpointId: event.checkpointId,
|
|
2111
|
+
resumeToken: event.resumeToken,
|
|
2112
|
+
step: event.step
|
|
2113
|
+
},
|
|
2114
|
+
transient: true
|
|
2115
|
+
};
|
|
2116
|
+
continue;
|
|
2117
|
+
}
|
|
2118
|
+
if (event.type === "error") {
|
|
1603
2119
|
yield {
|
|
1604
2120
|
type: "error",
|
|
1605
2121
|
errorText: event.message
|
|
1606
2122
|
};
|
|
1607
2123
|
break;
|
|
1608
|
-
}
|
|
2124
|
+
}
|
|
2125
|
+
if (event.type === "done") {
|
|
1609
2126
|
turnMetadata = doneToMetadata(event);
|
|
1610
2127
|
break;
|
|
1611
2128
|
}
|
|
@@ -1616,26 +2133,9 @@ async function* translateToUIMessageStream(events, opts) {
|
|
|
1616
2133
|
errorText: String(err)
|
|
1617
2134
|
};
|
|
1618
2135
|
}
|
|
1619
|
-
yield*
|
|
1620
|
-
yield turnMetadata ? {
|
|
1621
|
-
type: "finish",
|
|
1622
|
-
messageMetadata: turnMetadata
|
|
1623
|
-
} : {
|
|
1624
|
-
type: "finish"
|
|
1625
|
-
};
|
|
1626
|
-
}
|
|
1627
|
-
__name(translateToUIMessageStream, "translateToUIMessageStream");
|
|
1628
|
-
function doneToMetadata(event) {
|
|
1629
|
-
return event.cost === void 0 ? {
|
|
1630
|
-
usage: event.usage,
|
|
1631
|
-
durationMs: event.durationMs
|
|
1632
|
-
} : {
|
|
1633
|
-
usage: event.usage,
|
|
1634
|
-
durationMs: event.durationMs,
|
|
1635
|
-
cost: event.cost
|
|
1636
|
-
};
|
|
2136
|
+
yield* presenter.finish(turnMetadata);
|
|
1637
2137
|
}
|
|
1638
|
-
__name(
|
|
2138
|
+
__name(presentUIMessageStream, "presentUIMessageStream");
|
|
1639
2139
|
|
|
1640
2140
|
// src/bridge/agent-builder.ts
|
|
1641
2141
|
function contextualTool(tool, _requiredContext) {
|
|
@@ -1701,6 +2201,14 @@ function makeBuilder(config) {
|
|
|
1701
2201
|
...config,
|
|
1702
2202
|
settingSources: sources
|
|
1703
2203
|
}), "settingSources"),
|
|
2204
|
+
memory: /* @__PURE__ */ __name((settings) => makeBuilder({
|
|
2205
|
+
...config,
|
|
2206
|
+
memory: settings
|
|
2207
|
+
}), "memory"),
|
|
2208
|
+
hooks: /* @__PURE__ */ __name((map) => makeBuilder({
|
|
2209
|
+
...config,
|
|
2210
|
+
hooks: map
|
|
2211
|
+
}), "hooks"),
|
|
1704
2212
|
plugins: /* @__PURE__ */ __name((list) => makeBuilder({
|
|
1705
2213
|
...config,
|
|
1706
2214
|
plugins: list
|
|
@@ -1862,6 +2370,7 @@ function streamAgentUIMessages(compiled, apiKey, input) {
|
|
|
1862
2370
|
const overrides = {};
|
|
1863
2371
|
if (input.cwd !== void 0) overrides.cwd = input.cwd;
|
|
1864
2372
|
if (input.baseDir !== void 0) overrides.baseDir = input.baseDir;
|
|
2373
|
+
if (input.images !== void 0) overrides.images = input.images;
|
|
1865
2374
|
let source;
|
|
1866
2375
|
if (!input.hitl || input.hitl.gated.size === 0) {
|
|
1867
2376
|
const events2 = createSdkAgentStream(compiled, compiled.tools, apiKey, overrides)(input.message, input.sessionId);
|
|
@@ -1902,7 +2411,7 @@ function streamAgentUIMessages(compiled, apiKey, input) {
|
|
|
1902
2411
|
}
|
|
1903
2412
|
const durableCheckpoint = compiled.checkpoint?.storage === "filesystem";
|
|
1904
2413
|
const events = durableCheckpoint ? appendCheckpointSaved(source, input.sessionId) : source;
|
|
1905
|
-
return
|
|
2414
|
+
return presentUIMessageStream(events, {
|
|
1906
2415
|
textId
|
|
1907
2416
|
});
|
|
1908
2417
|
}
|
|
@@ -2223,10 +2732,10 @@ var DelegationError = class extends Error {
|
|
|
2223
2732
|
};
|
|
2224
2733
|
|
|
2225
2734
|
// src/loop/run-reflective-loop.ts
|
|
2226
|
-
function
|
|
2735
|
+
function asString3(value, fallback) {
|
|
2227
2736
|
return typeof value === "string" ? value : fallback;
|
|
2228
2737
|
}
|
|
2229
|
-
__name(
|
|
2738
|
+
__name(asString3, "asString");
|
|
2230
2739
|
function asNumber(value, fallback) {
|
|
2231
2740
|
return typeof value === "number" ? value : fallback;
|
|
2232
2741
|
}
|
|
@@ -2302,15 +2811,15 @@ function deriveFinishReason(signals) {
|
|
|
2302
2811
|
}
|
|
2303
2812
|
__name(deriveFinishReason, "deriveFinishReason");
|
|
2304
2813
|
function pushToolResult(event, r, callInputs) {
|
|
2305
|
-
const id =
|
|
2814
|
+
const id = asString3(event.callId, "");
|
|
2306
2815
|
const call = callInputs.get(id);
|
|
2307
2816
|
r.toolCalls.push({
|
|
2308
2817
|
id,
|
|
2309
|
-
name: call?.name ??
|
|
2818
|
+
name: call?.name ?? asString3(event.toolName, "unknown"),
|
|
2310
2819
|
// Prefer the correlated tool_call input (real SDK shape); fall back to an input on the
|
|
2311
2820
|
// result event itself (some streams/tests carry it there), else {}.
|
|
2312
2821
|
input: call?.input ?? event.input ?? {},
|
|
2313
|
-
output:
|
|
2822
|
+
output: asString3(event.output, "")
|
|
2314
2823
|
});
|
|
2315
2824
|
}
|
|
2316
2825
|
__name(pushToolResult, "pushToolResult");
|
|
@@ -2329,8 +2838,8 @@ function accumulateEvent(event, r, signals, callInputs) {
|
|
|
2329
2838
|
if (event.type === "text_delta" && typeof event.content === "string") {
|
|
2330
2839
|
r.responseText += event.content;
|
|
2331
2840
|
} else if (event.type === "tool_call") {
|
|
2332
|
-
callInputs.set(
|
|
2333
|
-
name:
|
|
2841
|
+
callInputs.set(asString3(event.callId, ""), {
|
|
2842
|
+
name: asString3(event.toolName, "unknown"),
|
|
2334
2843
|
input: event.input ?? {}
|
|
2335
2844
|
});
|
|
2336
2845
|
} else if (event.type === "tool_result") {
|
|
@@ -2338,11 +2847,11 @@ function accumulateEvent(event, r, signals, callInputs) {
|
|
|
2338
2847
|
pushToolResult(event, r, callInputs);
|
|
2339
2848
|
} else if (event.type === "done") {
|
|
2340
2849
|
signals.sawDone = true;
|
|
2341
|
-
signals.doneFinishReason =
|
|
2850
|
+
signals.doneFinishReason = asString3(event.finishReason, "");
|
|
2342
2851
|
applyDone(event, r);
|
|
2343
2852
|
} else if (event.type === "error") {
|
|
2344
2853
|
signals.sawError = true;
|
|
2345
|
-
r.errorMessage =
|
|
2854
|
+
r.errorMessage = asString3(event.message, "Unknown agent error");
|
|
2346
2855
|
}
|
|
2347
2856
|
}
|
|
2348
2857
|
__name(accumulateEvent, "accumulateEvent");
|
|
@@ -3013,6 +3522,10 @@ function matchRoute(routes, method, pathname) {
|
|
|
3013
3522
|
__name(matchRoute, "matchRoute");
|
|
3014
3523
|
|
|
3015
3524
|
export {
|
|
3525
|
+
AGENT_BRAND,
|
|
3526
|
+
isAgentDefinition,
|
|
3527
|
+
compileAgentDefinition,
|
|
3528
|
+
compileSkillsSelection,
|
|
3016
3529
|
createAgentExecutionContext,
|
|
3017
3530
|
isAgentContext,
|
|
3018
3531
|
compileContextWindow,
|
|
@@ -3033,16 +3546,13 @@ export {
|
|
|
3033
3546
|
isError,
|
|
3034
3547
|
isApprovalRequired,
|
|
3035
3548
|
generateAgentRoutes,
|
|
3036
|
-
AGENT_BRAND,
|
|
3037
|
-
isAgentDefinition,
|
|
3038
|
-
compileAgentDefinition,
|
|
3039
3549
|
translateSdkEvent,
|
|
3040
3550
|
buildModelSelection,
|
|
3041
3551
|
createThinkTagExtractor,
|
|
3042
3552
|
extractThinkTagStream,
|
|
3043
3553
|
createSdkAgentStream,
|
|
3044
3554
|
toAgentFactory,
|
|
3045
|
-
|
|
3555
|
+
presentUIMessageStream,
|
|
3046
3556
|
contextualTool,
|
|
3047
3557
|
agent,
|
|
3048
3558
|
AgentDefinitionError,
|
|
@@ -3085,4 +3595,4 @@ export {
|
|
|
3085
3595
|
generateAgentManifest,
|
|
3086
3596
|
agentsPlugin
|
|
3087
3597
|
};
|
|
3088
|
-
//# sourceMappingURL=chunk-
|
|
3598
|
+
//# sourceMappingURL=chunk-FEH7JFH7.js.map
|