@theokit/sdk 3.5.0 → 3.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/CHANGELOG.md +6 -0
- package/dist/a2a/index.cjs +306 -192
- package/dist/a2a/index.cjs.map +1 -1
- package/dist/a2a/index.js +306 -192
- package/dist/a2a/index.js.map +1 -1
- package/dist/{cron-D_BnMYIf.d.cts → cron-CZmTuHol.d.cts} +10 -0
- package/dist/{cron-I-6f-0As.d.ts → cron-DdETATFN.d.ts} +10 -0
- package/dist/cron.cjs +471 -357
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.d.cts +1 -1
- package/dist/cron.d.ts +1 -1
- package/dist/cron.js +471 -357
- package/dist/cron.js.map +1 -1
- package/dist/eval.cjs +471 -357
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +471 -357
- package/dist/eval.js.map +1 -1
- package/dist/index.cjs +536 -465
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +536 -465
- package/dist/index.js.map +1 -1
- package/dist/internal/runtime/local-agent/real-local-run-tools.d.ts +19 -0
- package/dist/internal/runtime/reasoning/native-reasoning.d.ts +25 -0
- package/dist/internal/runtime/system-prompt/sources/reasoning-provider.d.ts +1 -0
- package/dist/types/agent.d.ts +10 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -973,10 +973,10 @@ function buildToolPrompt(prompt) {
|
|
|
973
973
|
Respond by calling the \`output\` tool with the structured answer that matches the schema.`;
|
|
974
974
|
}
|
|
975
975
|
function setupStructuredOutput(schema, maxRetries) {
|
|
976
|
-
const
|
|
976
|
+
const z11 = requireZod();
|
|
977
977
|
const jsonSchema = toJsonSchema(schema, { unrepresentable: "any" });
|
|
978
978
|
return {
|
|
979
|
-
z:
|
|
979
|
+
z: z11,
|
|
980
980
|
jsonSchema,
|
|
981
981
|
maxRetries: maxRetries ?? 1,
|
|
982
982
|
initialUsage: { inputTokens: 0, outputTokens: 0 }
|
|
@@ -5766,6 +5766,213 @@ var init_subagents_loader = __esm({
|
|
|
5766
5766
|
init_yaml_frontmatter();
|
|
5767
5767
|
}
|
|
5768
5768
|
});
|
|
5769
|
+
function loadJsonrepair() {
|
|
5770
|
+
if (cachedJsonrepair === void 0) {
|
|
5771
|
+
const req = createRequire(import.meta.url);
|
|
5772
|
+
cachedJsonrepair = req("jsonrepair").jsonrepair;
|
|
5773
|
+
}
|
|
5774
|
+
return cachedJsonrepair;
|
|
5775
|
+
}
|
|
5776
|
+
function isPlainObject(v) {
|
|
5777
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
5778
|
+
}
|
|
5779
|
+
function toFiniteNumber(raw) {
|
|
5780
|
+
if (raw === "") return void 0;
|
|
5781
|
+
const n = Number(raw);
|
|
5782
|
+
return Number.isFinite(n) && String(n) === raw ? n : void 0;
|
|
5783
|
+
}
|
|
5784
|
+
function tryJson(raw, repair) {
|
|
5785
|
+
const t = raw.trimStart();
|
|
5786
|
+
if (!(t.startsWith("{") || t.startsWith("["))) return void 0;
|
|
5787
|
+
try {
|
|
5788
|
+
return JSON.parse(repair ? loadJsonrepair()(t) : t);
|
|
5789
|
+
} catch {
|
|
5790
|
+
return void 0;
|
|
5791
|
+
}
|
|
5792
|
+
}
|
|
5793
|
+
function heuristicCoerce(raw, repairJson) {
|
|
5794
|
+
if (raw === "true") return true;
|
|
5795
|
+
if (raw === "false") return false;
|
|
5796
|
+
if (raw === "null") return null;
|
|
5797
|
+
const n = toFiniteNumber(raw);
|
|
5798
|
+
if (n !== void 0) return n;
|
|
5799
|
+
const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
|
|
5800
|
+
return json === void 0 ? raw : json;
|
|
5801
|
+
}
|
|
5802
|
+
function coerceCandidates(raw, repairJson) {
|
|
5803
|
+
const out = [];
|
|
5804
|
+
if (raw === "true") out.push(true);
|
|
5805
|
+
else if (raw === "false") out.push(false);
|
|
5806
|
+
else if (raw === "null") out.push(null);
|
|
5807
|
+
const n = toFiniteNumber(raw);
|
|
5808
|
+
if (n !== void 0) out.push(n);
|
|
5809
|
+
const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
|
|
5810
|
+
if (json !== void 0) out.push(json);
|
|
5811
|
+
out.push(raw);
|
|
5812
|
+
return out;
|
|
5813
|
+
}
|
|
5814
|
+
function objectShape(schema) {
|
|
5815
|
+
const shape = schema?.shape;
|
|
5816
|
+
return shape !== null && typeof shape === "object" ? shape : void 0;
|
|
5817
|
+
}
|
|
5818
|
+
var cachedJsonrepair;
|
|
5819
|
+
var init_coerce = __esm({
|
|
5820
|
+
"src/sanitize/coerce.ts"() {
|
|
5821
|
+
}
|
|
5822
|
+
});
|
|
5823
|
+
|
|
5824
|
+
// src/sanitize/sanitize-tool-input.ts
|
|
5825
|
+
function applyTrim(key2, value, ctx) {
|
|
5826
|
+
const trimmed = value.trim();
|
|
5827
|
+
if (trimmed !== value) ctx.notes.push(`trimmed "${key2}"`);
|
|
5828
|
+
return trimmed;
|
|
5829
|
+
}
|
|
5830
|
+
function applyCoerce(key2, raw, ctx) {
|
|
5831
|
+
const field = ctx.shape?.[key2];
|
|
5832
|
+
let coerced = raw;
|
|
5833
|
+
if (field) {
|
|
5834
|
+
for (const candidate of coerceCandidates(raw, ctx.repairJson)) {
|
|
5835
|
+
if (field.safeParse(candidate).success) {
|
|
5836
|
+
coerced = candidate;
|
|
5837
|
+
break;
|
|
5838
|
+
}
|
|
5839
|
+
}
|
|
5840
|
+
} else {
|
|
5841
|
+
coerced = heuristicCoerce(raw, ctx.repairJson);
|
|
5842
|
+
}
|
|
5843
|
+
if (coerced !== raw) ctx.notes.push(`coerced "${key2}"`);
|
|
5844
|
+
return coerced;
|
|
5845
|
+
}
|
|
5846
|
+
function applyRepair(key2, value, ctx) {
|
|
5847
|
+
const repaired = tryJson(value, true);
|
|
5848
|
+
if (repaired === void 0) return value;
|
|
5849
|
+
ctx.notes.push(`repaired json "${key2}"`);
|
|
5850
|
+
return repaired;
|
|
5851
|
+
}
|
|
5852
|
+
function sanitizeString(key2, value, ctx) {
|
|
5853
|
+
let out = ctx.trim ? applyTrim(key2, value, ctx) : value;
|
|
5854
|
+
if (ctx.coerce && typeof out === "string") out = applyCoerce(key2, out, ctx);
|
|
5855
|
+
if (ctx.repairJson && !ctx.coerce && typeof out === "string") out = applyRepair(key2, out, ctx);
|
|
5856
|
+
return out;
|
|
5857
|
+
}
|
|
5858
|
+
function walk(input, ctx, depth) {
|
|
5859
|
+
const out = {};
|
|
5860
|
+
for (const [key2, value] of Object.entries(input)) {
|
|
5861
|
+
if (typeof value === "string") out[key2] = sanitizeString(key2, value, ctx);
|
|
5862
|
+
else if (ctx.deep && depth < ctx.maxDepth && isPlainObject(value))
|
|
5863
|
+
out[key2] = walk(value, ctx, depth + 1);
|
|
5864
|
+
else out[key2] = value;
|
|
5865
|
+
}
|
|
5866
|
+
return out;
|
|
5867
|
+
}
|
|
5868
|
+
function sanitizeToolInput(input, options) {
|
|
5869
|
+
if (!isPlainObject(input)) return { value: input, changed: false, notes: [] };
|
|
5870
|
+
const ctx = {
|
|
5871
|
+
trim: options?.trim ?? true,
|
|
5872
|
+
coerce: options?.coerce ?? false,
|
|
5873
|
+
repairJson: options?.repairJson ?? false,
|
|
5874
|
+
deep: options?.deep ?? false,
|
|
5875
|
+
maxDepth: options?.maxDepth ?? 8,
|
|
5876
|
+
shape: objectShape(options?.schema),
|
|
5877
|
+
notes: []
|
|
5878
|
+
};
|
|
5879
|
+
const value = walk(input, ctx, 0);
|
|
5880
|
+
return { value, changed: ctx.notes.length > 0, notes: ctx.notes };
|
|
5881
|
+
}
|
|
5882
|
+
var init_sanitize_tool_input = __esm({
|
|
5883
|
+
"src/sanitize/sanitize-tool-input.ts"() {
|
|
5884
|
+
init_coerce();
|
|
5885
|
+
}
|
|
5886
|
+
});
|
|
5887
|
+
|
|
5888
|
+
// src/define-tool.ts
|
|
5889
|
+
async function runValidated(spec, input, ctx) {
|
|
5890
|
+
const raw = spec.sanitize ? sanitizeToolInput(input, {
|
|
5891
|
+
...spec.sanitize === true ? {} : spec.sanitize,
|
|
5892
|
+
schema: spec.inputSchema
|
|
5893
|
+
}).value : input;
|
|
5894
|
+
const parsed = spec.inputSchema.parse(raw);
|
|
5895
|
+
const out = await spec.handler(parsed, ctx);
|
|
5896
|
+
return spec.outputSchema === void 0 ? out : spec.outputSchema.parse(out);
|
|
5897
|
+
}
|
|
5898
|
+
function serializeOutput(validated) {
|
|
5899
|
+
return typeof validated === "string" ? validated : JSON.stringify(validated);
|
|
5900
|
+
}
|
|
5901
|
+
function shapeModelOutput(spec, validated) {
|
|
5902
|
+
if (spec.toModelOutput !== void 0) return spec.toModelOutput(validated);
|
|
5903
|
+
return serializeOutput(validated);
|
|
5904
|
+
}
|
|
5905
|
+
function defineTool(spec) {
|
|
5906
|
+
const inputSchema = toJsonSchema(spec.inputSchema, {
|
|
5907
|
+
unrepresentable: "any"
|
|
5908
|
+
});
|
|
5909
|
+
const handler = async (input, ctx) => {
|
|
5910
|
+
const validated = await runValidated(spec, input, ctx);
|
|
5911
|
+
return shapeModelOutput(spec, validated);
|
|
5912
|
+
};
|
|
5913
|
+
const tool = { name: spec.name, description: spec.description, inputSchema, handler };
|
|
5914
|
+
if (spec.toModelOutput !== void 0) {
|
|
5915
|
+
const resolver = async (input, ctx) => {
|
|
5916
|
+
const validated = await runValidated(spec, input, ctx);
|
|
5917
|
+
return { model: shapeModelOutput(spec, validated), app: serializeOutput(validated) };
|
|
5918
|
+
};
|
|
5919
|
+
handler[TOOL_SPLIT_RESOLVER] = resolver;
|
|
5920
|
+
}
|
|
5921
|
+
return tool;
|
|
5922
|
+
}
|
|
5923
|
+
var TOOL_SPLIT_RESOLVER, Tool;
|
|
5924
|
+
var init_define_tool = __esm({
|
|
5925
|
+
"src/define-tool.ts"() {
|
|
5926
|
+
init_to_json_schema();
|
|
5927
|
+
init_sanitize_tool_input();
|
|
5928
|
+
TOOL_SPLIT_RESOLVER = /* @__PURE__ */ Symbol("theokit.toolSplitResolver");
|
|
5929
|
+
Tool = class {
|
|
5930
|
+
constructor() {
|
|
5931
|
+
}
|
|
5932
|
+
static create(spec) {
|
|
5933
|
+
return defineTool(spec);
|
|
5934
|
+
}
|
|
5935
|
+
};
|
|
5936
|
+
}
|
|
5937
|
+
});
|
|
5938
|
+
function createThinkTool() {
|
|
5939
|
+
return Tool.create({
|
|
5940
|
+
name: "think",
|
|
5941
|
+
description: "Scratchpad: reason through ONE step before answering. No side effects \u2014 your private reasoning space. Call it repeatedly before the final answer.",
|
|
5942
|
+
inputSchema: z.object({ thought: z.string().min(1, "think: `thought` must be non-empty.") }),
|
|
5943
|
+
handler: ({ thought }) => thought
|
|
5944
|
+
});
|
|
5945
|
+
}
|
|
5946
|
+
function isNativeReasoning(model) {
|
|
5947
|
+
if (model === void 0 || typeof model === "string") return false;
|
|
5948
|
+
const params = model.params;
|
|
5949
|
+
if (params === void 0) return false;
|
|
5950
|
+
return params.some((p) => NATIVE_REASONING_PARAM_IDS.has(p.id));
|
|
5951
|
+
}
|
|
5952
|
+
function warnDoubleReasoningOnce() {
|
|
5953
|
+
if (warned2.has("double-reasoning")) return;
|
|
5954
|
+
warned2.add("double-reasoning");
|
|
5955
|
+
process.stderr.write(
|
|
5956
|
+
"[theokit-sdk] `reasoning: true` skipped \u2014 a native reasoning model is configured (model.params thinking/reasoning); native reasoning wins. Remove one to silence.\n"
|
|
5957
|
+
);
|
|
5958
|
+
}
|
|
5959
|
+
function reasoningActive(reasoning, model) {
|
|
5960
|
+
if (reasoning !== true) return false;
|
|
5961
|
+
if (isNativeReasoning(model)) {
|
|
5962
|
+
warnDoubleReasoningOnce();
|
|
5963
|
+
return false;
|
|
5964
|
+
}
|
|
5965
|
+
return true;
|
|
5966
|
+
}
|
|
5967
|
+
var NATIVE_REASONING_PARAM_IDS, REASONING_PREAMBLE, warned2;
|
|
5968
|
+
var init_native_reasoning = __esm({
|
|
5969
|
+
"src/internal/runtime/reasoning/native-reasoning.ts"() {
|
|
5970
|
+
init_define_tool();
|
|
5971
|
+
NATIVE_REASONING_PARAM_IDS = /* @__PURE__ */ new Set(["thinking", "reasoning", "reasoning_effort"]);
|
|
5972
|
+
REASONING_PREAMBLE = "Think step by step before answering: use the `think` tool to reason through each step, validate your work, then answer. Reason about magnitude before comparing numbers.";
|
|
5973
|
+
warned2 = /* @__PURE__ */ new Set();
|
|
5974
|
+
}
|
|
5975
|
+
});
|
|
5769
5976
|
|
|
5770
5977
|
// src/internal/runtime/skills/skill-frontmatter.ts
|
|
5771
5978
|
function asString(v) {
|
|
@@ -6017,6 +6224,9 @@ async function buildAssemblyContext(inputs, userText, baseSystemPrompt, memoryFa
|
|
|
6017
6224
|
if (activeMemorySummary !== void 0 && activeMemorySummary.length > 0) {
|
|
6018
6225
|
assemblyCtx.activeMemorySummary = activeMemorySummary;
|
|
6019
6226
|
}
|
|
6227
|
+
if (reasoningActive(inputs.options.reasoning, inputs.model)) {
|
|
6228
|
+
assemblyCtx.reasoning = true;
|
|
6229
|
+
}
|
|
6020
6230
|
if (inputs.context !== void 0) {
|
|
6021
6231
|
await inputs.context.applyScope(contextPaths);
|
|
6022
6232
|
const internal = inputs.context.internalAssemblySnapshot();
|
|
@@ -6038,6 +6248,7 @@ async function assembleSystemPromptForSend(inputs, userText, baseSystemPrompt, m
|
|
|
6038
6248
|
}
|
|
6039
6249
|
var init_local_assembly = __esm({
|
|
6040
6250
|
"src/internal/runtime/system-prompt/local-assembly.ts"() {
|
|
6251
|
+
init_native_reasoning();
|
|
6041
6252
|
init_skills_manager();
|
|
6042
6253
|
}
|
|
6043
6254
|
});
|
|
@@ -6173,6 +6384,21 @@ ${lines.join("\n")}
|
|
|
6173
6384
|
}
|
|
6174
6385
|
});
|
|
6175
6386
|
|
|
6387
|
+
// src/internal/runtime/system-prompt/sources/reasoning-provider.ts
|
|
6388
|
+
var ReasoningPromptProvider;
|
|
6389
|
+
var init_reasoning_provider = __esm({
|
|
6390
|
+
"src/internal/runtime/system-prompt/sources/reasoning-provider.ts"() {
|
|
6391
|
+
init_native_reasoning();
|
|
6392
|
+
ReasoningPromptProvider = class {
|
|
6393
|
+
id = "reasoning";
|
|
6394
|
+
priority = 1;
|
|
6395
|
+
contribute(ctx) {
|
|
6396
|
+
return Promise.resolve(ctx.reasoning === true ? REASONING_PREAMBLE : void 0);
|
|
6397
|
+
}
|
|
6398
|
+
};
|
|
6399
|
+
}
|
|
6400
|
+
});
|
|
6401
|
+
|
|
6176
6402
|
// src/internal/runtime/skills/skills-block.ts
|
|
6177
6403
|
function buildSkillsBlock(skills) {
|
|
6178
6404
|
if (skills.length === 0) return void 0;
|
|
@@ -6215,6 +6441,7 @@ var init_pipeline = __esm({
|
|
|
6215
6441
|
init_base_provider();
|
|
6216
6442
|
init_context_provider();
|
|
6217
6443
|
init_memory_provider();
|
|
6444
|
+
init_reasoning_provider();
|
|
6218
6445
|
init_skills_provider();
|
|
6219
6446
|
SystemPromptPipeline = class _SystemPromptPipeline {
|
|
6220
6447
|
providers;
|
|
@@ -6254,6 +6481,7 @@ var init_pipeline = __esm({
|
|
|
6254
6481
|
*/
|
|
6255
6482
|
static default() {
|
|
6256
6483
|
return new _SystemPromptPipeline([
|
|
6484
|
+
new ReasoningPromptProvider(),
|
|
6257
6485
|
new ActiveMemoryPromptProvider(),
|
|
6258
6486
|
new ContextPromptProvider(),
|
|
6259
6487
|
new SkillsPromptProvider(),
|
|
@@ -7750,171 +7978,12 @@ var init_local_run = __esm({
|
|
|
7750
7978
|
};
|
|
7751
7979
|
}
|
|
7752
7980
|
});
|
|
7753
|
-
function inheritSubAgentCredentials(tool, creds) {
|
|
7754
|
-
const sink = tool[INHERIT_CREDENTIALS];
|
|
7755
|
-
if (typeof sink === "function") sink(creds);
|
|
7756
|
-
}
|
|
7757
|
-
async function applyDelegationStart(spec, input, iteration) {
|
|
7758
|
-
if (spec.onDelegationStart === void 0) return { input };
|
|
7759
|
-
const decision = await spec.onDelegationStart({ input, name: spec.name, iteration });
|
|
7760
|
-
if (decision === void 0) return { input };
|
|
7761
|
-
if (decision.proceed === false)
|
|
7762
|
-
return { reject: decision.rejectionReason ?? "(delegation rejected)" };
|
|
7763
|
-
return {
|
|
7764
|
-
input: decision.modifiedInput ?? input,
|
|
7765
|
-
...decision.modifiedMaxSteps !== void 0 ? { maxSteps: decision.modifiedMaxSteps } : {}
|
|
7766
|
-
};
|
|
7767
|
-
}
|
|
7768
|
-
async function collectChildToolResults(run) {
|
|
7769
|
-
const lines = [];
|
|
7770
|
-
for await (const event of run.stream()) {
|
|
7771
|
-
if (event.type === "tool_call" && event.status === "completed") {
|
|
7772
|
-
const rendered = typeof event.result === "string" ? event.result : JSON.stringify(event.result ?? null);
|
|
7773
|
-
lines.push(`${event.name}: ${rendered}`);
|
|
7774
|
-
}
|
|
7775
|
-
}
|
|
7776
|
-
if (lines.length === 0) return "";
|
|
7777
|
-
return `
|
|
7778
7981
|
|
|
7779
|
-
|
|
7780
|
-
|
|
7781
|
-
|
|
7782
|
-
}
|
|
7783
|
-
function buildChildCreateOptions(spec, inherited) {
|
|
7784
|
-
const model = spec.model ? { id: spec.model } : inherited?.model;
|
|
7785
|
-
return {
|
|
7786
|
-
...inherited?.apiKey !== void 0 ? { apiKey: inherited.apiKey } : {},
|
|
7787
|
-
...model !== void 0 ? { model } : {},
|
|
7788
|
-
...inherited?.plugins !== void 0 ? { plugins: inherited.plugins } : {},
|
|
7789
|
-
systemPrompt: spec.instructions,
|
|
7790
|
-
tools: spec.tools ?? []
|
|
7791
|
-
};
|
|
7792
|
-
}
|
|
7793
|
-
async function runChildAgent(spec, input, signal, maxSteps, inherited) {
|
|
7794
|
-
const { Agent: Agent2 } = await Promise.resolve().then(() => (init_agent(), agent_exports));
|
|
7795
|
-
const agent = await Agent2.create(buildChildCreateOptions(spec, inherited));
|
|
7796
|
-
try {
|
|
7797
|
-
const sendOptions = {
|
|
7798
|
-
...signal !== void 0 ? { signal } : {},
|
|
7799
|
-
...maxSteps !== void 0 ? { maxIterations: maxSteps } : {},
|
|
7800
|
-
// SE3 — a delegated child's turn is initiated by the coordinating parent.
|
|
7801
|
-
origin: { kind: "coordinator" }
|
|
7802
|
-
};
|
|
7803
|
-
const run = await agent.send(input, sendOptions);
|
|
7804
|
-
const result = await run.wait();
|
|
7805
|
-
const text = result.result ?? "(no response)";
|
|
7806
|
-
return spec.includeToolResults === true ? text + await collectChildToolResults(run) : text;
|
|
7807
|
-
} finally {
|
|
7808
|
-
agent.dispose();
|
|
7809
|
-
}
|
|
7810
|
-
}
|
|
7811
|
-
async function notifyDelegationError(spec, input, error, iteration) {
|
|
7812
|
-
if (spec.onDelegationComplete === void 0) return;
|
|
7982
|
+
// src/types/run-events.ts
|
|
7983
|
+
function emitRunEvent(sink, event) {
|
|
7984
|
+
if (sink === void 0) return;
|
|
7813
7985
|
try {
|
|
7814
|
-
|
|
7815
|
-
} catch {
|
|
7816
|
-
}
|
|
7817
|
-
}
|
|
7818
|
-
function applyMessageFilter(spec, input, messages) {
|
|
7819
|
-
if (spec.messageFilter === void 0 || messages === void 0) return input;
|
|
7820
|
-
const filtered = spec.messageFilter({ messages, input, name: spec.name });
|
|
7821
|
-
if (filtered.length === 0) return input;
|
|
7822
|
-
const preamble = filtered.map((m) => `${m.role}: ${m.content}`).join("\n");
|
|
7823
|
-
return `Prior conversation:
|
|
7824
|
-
${preamble}
|
|
7825
|
-
|
|
7826
|
-
Task:
|
|
7827
|
-
${input}`;
|
|
7828
|
-
}
|
|
7829
|
-
async function applyDelegationComplete(spec, input, result, iteration) {
|
|
7830
|
-
if (spec.onDelegationComplete === void 0) return result;
|
|
7831
|
-
const completion = await spec.onDelegationComplete({ input, name: spec.name, result, iteration });
|
|
7832
|
-
return completion?.feedback !== void 0 ? result + completion.feedback : result;
|
|
7833
|
-
}
|
|
7834
|
-
function defineSubAgent(spec, _parentDepth = 0) {
|
|
7835
|
-
const currentDepth = _parentDepth + 1;
|
|
7836
|
-
const maxDepth = spec.maxDelegationDepth ?? 3;
|
|
7837
|
-
if (currentDepth > maxDepth) {
|
|
7838
|
-
throw new MaxDelegationDepthError(currentDepth, maxDepth);
|
|
7839
|
-
}
|
|
7840
|
-
const inputZod = z.object({
|
|
7841
|
-
input: z.string().describe("Task for the subagent")
|
|
7842
|
-
});
|
|
7843
|
-
const inputSchema = {
|
|
7844
|
-
type: "object",
|
|
7845
|
-
properties: {
|
|
7846
|
-
input: { type: "string", description: "Task for the subagent" }
|
|
7847
|
-
},
|
|
7848
|
-
required: ["input"],
|
|
7849
|
-
additionalProperties: false
|
|
7850
|
-
};
|
|
7851
|
-
let iteration = 0;
|
|
7852
|
-
let inherited;
|
|
7853
|
-
const tool = {
|
|
7854
|
-
name: spec.name,
|
|
7855
|
-
description: spec.description,
|
|
7856
|
-
inputSchema,
|
|
7857
|
-
handler: async (rawInput, ctx) => {
|
|
7858
|
-
const { input: parsed } = inputZod.parse(rawInput);
|
|
7859
|
-
iteration += 1;
|
|
7860
|
-
const capturedIteration = iteration;
|
|
7861
|
-
const start = await applyDelegationStart(spec, parsed, capturedIteration);
|
|
7862
|
-
if ("reject" in start) return start.reject;
|
|
7863
|
-
const input = applyMessageFilter(spec, start.input, ctx?.messages);
|
|
7864
|
-
let result;
|
|
7865
|
-
try {
|
|
7866
|
-
result = await runChildAgent(spec, input, ctx?.signal, start.maxSteps, inherited);
|
|
7867
|
-
} catch (error) {
|
|
7868
|
-
await notifyDelegationError(spec, input, error, capturedIteration);
|
|
7869
|
-
throw error;
|
|
7870
|
-
}
|
|
7871
|
-
return applyDelegationComplete(spec, input, result, capturedIteration);
|
|
7872
|
-
}
|
|
7873
|
-
};
|
|
7874
|
-
Object.defineProperty(tool, INHERIT_CREDENTIALS, {
|
|
7875
|
-
value: ((creds) => {
|
|
7876
|
-
inherited = creds;
|
|
7877
|
-
}),
|
|
7878
|
-
enumerable: false
|
|
7879
|
-
});
|
|
7880
|
-
return tool;
|
|
7881
|
-
}
|
|
7882
|
-
function subAgentToolsFromDefinitions(agents2, parentTools) {
|
|
7883
|
-
return Object.entries(agents2).map(([name, def]) => {
|
|
7884
|
-
const whitelist = Array.isArray(def.tools) && def.tools.length > 0 ? new Set(def.tools) : void 0;
|
|
7885
|
-
const childTools = whitelist ? parentTools.filter((t) => whitelist.has(t.name)) : parentTools;
|
|
7886
|
-
return defineSubAgent({
|
|
7887
|
-
name,
|
|
7888
|
-
description: def.description,
|
|
7889
|
-
instructions: def.prompt,
|
|
7890
|
-
...def.model !== void 0 && def.model !== "inherit" ? { model: def.model.id } : {},
|
|
7891
|
-
tools: [...childTools]
|
|
7892
|
-
});
|
|
7893
|
-
});
|
|
7894
|
-
}
|
|
7895
|
-
var INHERIT_CREDENTIALS, MaxDelegationDepthError;
|
|
7896
|
-
var init_subagent = __esm({
|
|
7897
|
-
"src/a2a/subagent.ts"() {
|
|
7898
|
-
INHERIT_CREDENTIALS = /* @__PURE__ */ Symbol("theokit.subagent.inheritCredentials");
|
|
7899
|
-
MaxDelegationDepthError = class extends Error {
|
|
7900
|
-
constructor(currentDepth, maxDepth) {
|
|
7901
|
-
super(`Max delegation depth ${maxDepth} exceeded (current: ${currentDepth})`);
|
|
7902
|
-
this.currentDepth = currentDepth;
|
|
7903
|
-
this.maxDepth = maxDepth;
|
|
7904
|
-
this.name = "MaxDelegationDepthError";
|
|
7905
|
-
}
|
|
7906
|
-
currentDepth;
|
|
7907
|
-
maxDepth;
|
|
7908
|
-
code = "max_delegation_depth";
|
|
7909
|
-
};
|
|
7910
|
-
}
|
|
7911
|
-
});
|
|
7912
|
-
|
|
7913
|
-
// src/types/run-events.ts
|
|
7914
|
-
function emitRunEvent(sink, event) {
|
|
7915
|
-
if (sink === void 0) return;
|
|
7916
|
-
try {
|
|
7917
|
-
sink(event);
|
|
7986
|
+
sink(event);
|
|
7918
7987
|
} catch {
|
|
7919
7988
|
}
|
|
7920
7989
|
}
|
|
@@ -8739,253 +8808,84 @@ var init_repair_middleware = __esm({
|
|
|
8739
8808
|
DECIMAL_RE = /^-?\d+(\.\d+)?$/;
|
|
8740
8809
|
}
|
|
8741
8810
|
});
|
|
8742
|
-
|
|
8743
|
-
|
|
8744
|
-
|
|
8745
|
-
|
|
8746
|
-
}
|
|
8747
|
-
return cachedJsonrepair;
|
|
8748
|
-
}
|
|
8749
|
-
function isPlainObject(v) {
|
|
8750
|
-
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
8751
|
-
}
|
|
8752
|
-
function toFiniteNumber(raw) {
|
|
8753
|
-
if (raw === "") return void 0;
|
|
8754
|
-
const n = Number(raw);
|
|
8755
|
-
return Number.isFinite(n) && String(n) === raw ? n : void 0;
|
|
8811
|
+
|
|
8812
|
+
// src/tool-error.ts
|
|
8813
|
+
function renderToolErrorMessage(content) {
|
|
8814
|
+
if (typeof content === "string") return content;
|
|
8815
|
+
return content.map((block) => block.type === "text" ? block.text : `[${block.source.media_type} image]`).join("\n");
|
|
8756
8816
|
}
|
|
8757
|
-
|
|
8758
|
-
|
|
8759
|
-
|
|
8760
|
-
|
|
8761
|
-
|
|
8762
|
-
|
|
8763
|
-
|
|
8817
|
+
var ToolError;
|
|
8818
|
+
var init_tool_error = __esm({
|
|
8819
|
+
"src/tool-error.ts"() {
|
|
8820
|
+
init_errors();
|
|
8821
|
+
ToolError = class extends TheokitAgentError {
|
|
8822
|
+
name = "ToolError";
|
|
8823
|
+
/** The error content surfaced to the model: a string, or text/image blocks. */
|
|
8824
|
+
content;
|
|
8825
|
+
constructor(content, options = {}) {
|
|
8826
|
+
super(renderToolErrorMessage(content), { ...options, isRetryable: false });
|
|
8827
|
+
this.content = content;
|
|
8828
|
+
}
|
|
8829
|
+
};
|
|
8764
8830
|
}
|
|
8831
|
+
});
|
|
8832
|
+
|
|
8833
|
+
// src/internal/runtime/tools/shell-tool.ts
|
|
8834
|
+
async function runShell(options) {
|
|
8835
|
+
if (options.sandbox === true && isObviouslyUnsafe(options.command)) {
|
|
8836
|
+
return {
|
|
8837
|
+
stdout: "",
|
|
8838
|
+
stderr: `Sandbox refused command: ${options.command}`,
|
|
8839
|
+
exitCode: 126,
|
|
8840
|
+
timedOut: false
|
|
8841
|
+
};
|
|
8842
|
+
}
|
|
8843
|
+
const result = await spawnAndCollect({
|
|
8844
|
+
command: "sh",
|
|
8845
|
+
args: ["-c", options.command],
|
|
8846
|
+
cwd: options.cwd,
|
|
8847
|
+
...options.env !== void 0 ? { env: options.env } : {},
|
|
8848
|
+
...options.timeoutMs !== void 0 ? { timeoutMs: options.timeoutMs } : {}
|
|
8849
|
+
});
|
|
8850
|
+
const stderr = result.spawnError !== void 0 ? result.stderr + result.spawnError.message : result.stderr;
|
|
8851
|
+
return {
|
|
8852
|
+
stdout: result.stdout,
|
|
8853
|
+
stderr,
|
|
8854
|
+
exitCode: result.exitCode,
|
|
8855
|
+
timedOut: result.timedOut
|
|
8856
|
+
};
|
|
8765
8857
|
}
|
|
8766
|
-
function
|
|
8767
|
-
if (
|
|
8768
|
-
if (
|
|
8769
|
-
|
|
8770
|
-
const n = toFiniteNumber(raw);
|
|
8771
|
-
if (n !== void 0) return n;
|
|
8772
|
-
const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
|
|
8773
|
-
return json === void 0 ? raw : json;
|
|
8774
|
-
}
|
|
8775
|
-
function coerceCandidates(raw, repairJson) {
|
|
8776
|
-
const out = [];
|
|
8777
|
-
if (raw === "true") out.push(true);
|
|
8778
|
-
else if (raw === "false") out.push(false);
|
|
8779
|
-
else if (raw === "null") out.push(null);
|
|
8780
|
-
const n = toFiniteNumber(raw);
|
|
8781
|
-
if (n !== void 0) out.push(n);
|
|
8782
|
-
const json = tryJson(raw, false) ?? (repairJson ? tryJson(raw, true) : void 0);
|
|
8783
|
-
if (json !== void 0) out.push(json);
|
|
8784
|
-
out.push(raw);
|
|
8785
|
-
return out;
|
|
8786
|
-
}
|
|
8787
|
-
function objectShape(schema) {
|
|
8788
|
-
const shape = schema?.shape;
|
|
8789
|
-
return shape !== null && typeof shape === "object" ? shape : void 0;
|
|
8858
|
+
function isObviouslyUnsafe(command) {
|
|
8859
|
+
if (/(^|\s)(rm|mv|cp)\s+[^|;]*\s+\/(etc|var|root)\b/.test(command)) return true;
|
|
8860
|
+
if (/sudo\s/.test(command)) return true;
|
|
8861
|
+
return false;
|
|
8790
8862
|
}
|
|
8791
|
-
var
|
|
8792
|
-
|
|
8793
|
-
|
|
8863
|
+
var init_shell_tool = __esm({
|
|
8864
|
+
"src/internal/runtime/tools/shell-tool.ts"() {
|
|
8865
|
+
init_spawn_collect();
|
|
8794
8866
|
}
|
|
8795
8867
|
});
|
|
8796
8868
|
|
|
8797
|
-
// src/
|
|
8798
|
-
function
|
|
8799
|
-
|
|
8800
|
-
|
|
8801
|
-
return trimmed;
|
|
8802
|
-
}
|
|
8803
|
-
function applyCoerce(key2, raw, ctx) {
|
|
8804
|
-
const field = ctx.shape?.[key2];
|
|
8805
|
-
let coerced = raw;
|
|
8806
|
-
if (field) {
|
|
8807
|
-
for (const candidate of coerceCandidates(raw, ctx.repairJson)) {
|
|
8808
|
-
if (field.safeParse(candidate).success) {
|
|
8809
|
-
coerced = candidate;
|
|
8810
|
-
break;
|
|
8811
|
-
}
|
|
8812
|
-
}
|
|
8813
|
-
} else {
|
|
8814
|
-
coerced = heuristicCoerce(raw, ctx.repairJson);
|
|
8869
|
+
// src/internal/agent-loop/tool-executors.ts
|
|
8870
|
+
async function executeTool(inputs, resolved, call) {
|
|
8871
|
+
if (resolved === void 0) {
|
|
8872
|
+
return { stdout: "", stderr: `Unknown tool ${call.name}`, exitCode: 127 };
|
|
8815
8873
|
}
|
|
8816
|
-
if (
|
|
8817
|
-
return
|
|
8874
|
+
if (resolved.origin === "shell") return runShellTool(inputs, call);
|
|
8875
|
+
if (resolved.origin === "memory") return runMemoryTool(resolved, call, inputs.context);
|
|
8876
|
+
if (resolved.origin === "custom")
|
|
8877
|
+
return runCustomTool(resolved, call, inputs.signal, inputs.context, inputs.messages);
|
|
8878
|
+
return runMcpTool(inputs, resolved, call);
|
|
8818
8879
|
}
|
|
8819
|
-
function
|
|
8820
|
-
|
|
8821
|
-
if (repaired === void 0) return value;
|
|
8822
|
-
ctx.notes.push(`repaired json "${key2}"`);
|
|
8823
|
-
return repaired;
|
|
8880
|
+
async function runMemoryTool(resolved, call, context) {
|
|
8881
|
+
return runHandlerTool("memory", resolved.memoryHandler, call, void 0, context);
|
|
8824
8882
|
}
|
|
8825
|
-
function
|
|
8826
|
-
|
|
8827
|
-
if (ctx.coerce && typeof out === "string") out = applyCoerce(key2, out, ctx);
|
|
8828
|
-
if (ctx.repairJson && !ctx.coerce && typeof out === "string") out = applyRepair(key2, out, ctx);
|
|
8829
|
-
return out;
|
|
8883
|
+
async function runCustomTool(resolved, call, signal, context, messages) {
|
|
8884
|
+
return runHandlerTool("custom", resolved.customHandler, call, signal, context, messages);
|
|
8830
8885
|
}
|
|
8831
|
-
function
|
|
8832
|
-
|
|
8833
|
-
|
|
8834
|
-
if (typeof value === "string") out[key2] = sanitizeString(key2, value, ctx);
|
|
8835
|
-
else if (ctx.deep && depth < ctx.maxDepth && isPlainObject(value))
|
|
8836
|
-
out[key2] = walk(value, ctx, depth + 1);
|
|
8837
|
-
else out[key2] = value;
|
|
8838
|
-
}
|
|
8839
|
-
return out;
|
|
8840
|
-
}
|
|
8841
|
-
function sanitizeToolInput(input, options) {
|
|
8842
|
-
if (!isPlainObject(input)) return { value: input, changed: false, notes: [] };
|
|
8843
|
-
const ctx = {
|
|
8844
|
-
trim: options?.trim ?? true,
|
|
8845
|
-
coerce: options?.coerce ?? false,
|
|
8846
|
-
repairJson: options?.repairJson ?? false,
|
|
8847
|
-
deep: options?.deep ?? false,
|
|
8848
|
-
maxDepth: options?.maxDepth ?? 8,
|
|
8849
|
-
shape: objectShape(options?.schema),
|
|
8850
|
-
notes: []
|
|
8851
|
-
};
|
|
8852
|
-
const value = walk(input, ctx, 0);
|
|
8853
|
-
return { value, changed: ctx.notes.length > 0, notes: ctx.notes };
|
|
8854
|
-
}
|
|
8855
|
-
var init_sanitize_tool_input = __esm({
|
|
8856
|
-
"src/sanitize/sanitize-tool-input.ts"() {
|
|
8857
|
-
init_coerce();
|
|
8858
|
-
}
|
|
8859
|
-
});
|
|
8860
|
-
|
|
8861
|
-
// src/define-tool.ts
|
|
8862
|
-
async function runValidated(spec, input, ctx) {
|
|
8863
|
-
const raw = spec.sanitize ? sanitizeToolInput(input, {
|
|
8864
|
-
...spec.sanitize === true ? {} : spec.sanitize,
|
|
8865
|
-
schema: spec.inputSchema
|
|
8866
|
-
}).value : input;
|
|
8867
|
-
const parsed = spec.inputSchema.parse(raw);
|
|
8868
|
-
const out = await spec.handler(parsed, ctx);
|
|
8869
|
-
return spec.outputSchema === void 0 ? out : spec.outputSchema.parse(out);
|
|
8870
|
-
}
|
|
8871
|
-
function serializeOutput(validated) {
|
|
8872
|
-
return typeof validated === "string" ? validated : JSON.stringify(validated);
|
|
8873
|
-
}
|
|
8874
|
-
function shapeModelOutput(spec, validated) {
|
|
8875
|
-
if (spec.toModelOutput !== void 0) return spec.toModelOutput(validated);
|
|
8876
|
-
return serializeOutput(validated);
|
|
8877
|
-
}
|
|
8878
|
-
function defineTool(spec) {
|
|
8879
|
-
const inputSchema = toJsonSchema(spec.inputSchema, {
|
|
8880
|
-
unrepresentable: "any"
|
|
8881
|
-
});
|
|
8882
|
-
const handler = async (input, ctx) => {
|
|
8883
|
-
const validated = await runValidated(spec, input, ctx);
|
|
8884
|
-
return shapeModelOutput(spec, validated);
|
|
8885
|
-
};
|
|
8886
|
-
const tool = { name: spec.name, description: spec.description, inputSchema, handler };
|
|
8887
|
-
if (spec.toModelOutput !== void 0) {
|
|
8888
|
-
const resolver = async (input, ctx) => {
|
|
8889
|
-
const validated = await runValidated(spec, input, ctx);
|
|
8890
|
-
return { model: shapeModelOutput(spec, validated), app: serializeOutput(validated) };
|
|
8891
|
-
};
|
|
8892
|
-
handler[TOOL_SPLIT_RESOLVER] = resolver;
|
|
8893
|
-
}
|
|
8894
|
-
return tool;
|
|
8895
|
-
}
|
|
8896
|
-
var TOOL_SPLIT_RESOLVER, Tool;
|
|
8897
|
-
var init_define_tool = __esm({
|
|
8898
|
-
"src/define-tool.ts"() {
|
|
8899
|
-
init_to_json_schema();
|
|
8900
|
-
init_sanitize_tool_input();
|
|
8901
|
-
TOOL_SPLIT_RESOLVER = /* @__PURE__ */ Symbol("theokit.toolSplitResolver");
|
|
8902
|
-
Tool = class {
|
|
8903
|
-
constructor() {
|
|
8904
|
-
}
|
|
8905
|
-
static create(spec) {
|
|
8906
|
-
return defineTool(spec);
|
|
8907
|
-
}
|
|
8908
|
-
};
|
|
8909
|
-
}
|
|
8910
|
-
});
|
|
8911
|
-
|
|
8912
|
-
// src/tool-error.ts
|
|
8913
|
-
function renderToolErrorMessage(content) {
|
|
8914
|
-
if (typeof content === "string") return content;
|
|
8915
|
-
return content.map((block) => block.type === "text" ? block.text : `[${block.source.media_type} image]`).join("\n");
|
|
8916
|
-
}
|
|
8917
|
-
var ToolError;
|
|
8918
|
-
var init_tool_error = __esm({
|
|
8919
|
-
"src/tool-error.ts"() {
|
|
8920
|
-
init_errors();
|
|
8921
|
-
ToolError = class extends TheokitAgentError {
|
|
8922
|
-
name = "ToolError";
|
|
8923
|
-
/** The error content surfaced to the model: a string, or text/image blocks. */
|
|
8924
|
-
content;
|
|
8925
|
-
constructor(content, options = {}) {
|
|
8926
|
-
super(renderToolErrorMessage(content), { ...options, isRetryable: false });
|
|
8927
|
-
this.content = content;
|
|
8928
|
-
}
|
|
8929
|
-
};
|
|
8930
|
-
}
|
|
8931
|
-
});
|
|
8932
|
-
|
|
8933
|
-
// src/internal/runtime/tools/shell-tool.ts
|
|
8934
|
-
async function runShell(options) {
|
|
8935
|
-
if (options.sandbox === true && isObviouslyUnsafe(options.command)) {
|
|
8936
|
-
return {
|
|
8937
|
-
stdout: "",
|
|
8938
|
-
stderr: `Sandbox refused command: ${options.command}`,
|
|
8939
|
-
exitCode: 126,
|
|
8940
|
-
timedOut: false
|
|
8941
|
-
};
|
|
8942
|
-
}
|
|
8943
|
-
const result = await spawnAndCollect({
|
|
8944
|
-
command: "sh",
|
|
8945
|
-
args: ["-c", options.command],
|
|
8946
|
-
cwd: options.cwd,
|
|
8947
|
-
...options.env !== void 0 ? { env: options.env } : {},
|
|
8948
|
-
...options.timeoutMs !== void 0 ? { timeoutMs: options.timeoutMs } : {}
|
|
8949
|
-
});
|
|
8950
|
-
const stderr = result.spawnError !== void 0 ? result.stderr + result.spawnError.message : result.stderr;
|
|
8951
|
-
return {
|
|
8952
|
-
stdout: result.stdout,
|
|
8953
|
-
stderr,
|
|
8954
|
-
exitCode: result.exitCode,
|
|
8955
|
-
timedOut: result.timedOut
|
|
8956
|
-
};
|
|
8957
|
-
}
|
|
8958
|
-
function isObviouslyUnsafe(command) {
|
|
8959
|
-
if (/(^|\s)(rm|mv|cp)\s+[^|;]*\s+\/(etc|var|root)\b/.test(command)) return true;
|
|
8960
|
-
if (/sudo\s/.test(command)) return true;
|
|
8961
|
-
return false;
|
|
8962
|
-
}
|
|
8963
|
-
var init_shell_tool = __esm({
|
|
8964
|
-
"src/internal/runtime/tools/shell-tool.ts"() {
|
|
8965
|
-
init_spawn_collect();
|
|
8966
|
-
}
|
|
8967
|
-
});
|
|
8968
|
-
|
|
8969
|
-
// src/internal/agent-loop/tool-executors.ts
|
|
8970
|
-
async function executeTool(inputs, resolved, call) {
|
|
8971
|
-
if (resolved === void 0) {
|
|
8972
|
-
return { stdout: "", stderr: `Unknown tool ${call.name}`, exitCode: 127 };
|
|
8973
|
-
}
|
|
8974
|
-
if (resolved.origin === "shell") return runShellTool(inputs, call);
|
|
8975
|
-
if (resolved.origin === "memory") return runMemoryTool(resolved, call, inputs.context);
|
|
8976
|
-
if (resolved.origin === "custom")
|
|
8977
|
-
return runCustomTool(resolved, call, inputs.signal, inputs.context, inputs.messages);
|
|
8978
|
-
return runMcpTool(inputs, resolved, call);
|
|
8979
|
-
}
|
|
8980
|
-
async function runMemoryTool(resolved, call, context) {
|
|
8981
|
-
return runHandlerTool("memory", resolved.memoryHandler, call, void 0, context);
|
|
8982
|
-
}
|
|
8983
|
-
async function runCustomTool(resolved, call, signal, context, messages) {
|
|
8984
|
-
return runHandlerTool("custom", resolved.customHandler, call, signal, context, messages);
|
|
8985
|
-
}
|
|
8986
|
-
async function runHandlerTool(kind, handler, call, signal, context, messages) {
|
|
8987
|
-
if (handler === void 0) {
|
|
8988
|
-
return { stdout: "", stderr: `${kind} tool ${call.name} has no handler`, exitCode: 127 };
|
|
8886
|
+
async function runHandlerTool(kind, handler, call, signal, context, messages) {
|
|
8887
|
+
if (handler === void 0) {
|
|
8888
|
+
return { stdout: "", stderr: `${kind} tool ${call.name} has no handler`, exitCode: 127 };
|
|
8989
8889
|
}
|
|
8990
8890
|
try {
|
|
8991
8891
|
const split = handler[TOOL_SPLIT_RESOLVER];
|
|
@@ -13359,6 +13259,198 @@ var init_register_plugin_providers = __esm({
|
|
|
13359
13259
|
}
|
|
13360
13260
|
});
|
|
13361
13261
|
|
|
13262
|
+
// src/internal/runtime/local-agent/real-local-run-provider.ts
|
|
13263
|
+
function inferProviderFromApiKey(apiKey) {
|
|
13264
|
+
if (apiKey === void 0 || apiKey.length === 0) return void 0;
|
|
13265
|
+
const byPrefix = [
|
|
13266
|
+
{ provider: "openrouter", prefix: "sk-or-" },
|
|
13267
|
+
{ provider: "anthropic", prefix: "sk-ant-" },
|
|
13268
|
+
{ provider: "openai", prefix: "sk-" }
|
|
13269
|
+
];
|
|
13270
|
+
for (const { provider, prefix } of byPrefix) {
|
|
13271
|
+
if (apiKey.startsWith(prefix) && getProviderProfile(provider) !== void 0) {
|
|
13272
|
+
return provider;
|
|
13273
|
+
}
|
|
13274
|
+
}
|
|
13275
|
+
return void 0;
|
|
13276
|
+
}
|
|
13277
|
+
function detectPrimaryProvider() {
|
|
13278
|
+
if (process.env.ANTHROPIC_API_KEY !== void 0 && process.env.ANTHROPIC_API_KEY.length > 0) {
|
|
13279
|
+
return "anthropic";
|
|
13280
|
+
}
|
|
13281
|
+
if (process.env.OPENAI_API_KEY !== void 0 && process.env.OPENAI_API_KEY.length > 0) {
|
|
13282
|
+
return "openai";
|
|
13283
|
+
}
|
|
13284
|
+
if (process.env.OPENROUTER_API_KEY !== void 0 && process.env.OPENROUTER_API_KEY.length > 0) {
|
|
13285
|
+
return "openrouter";
|
|
13286
|
+
}
|
|
13287
|
+
return "openai";
|
|
13288
|
+
}
|
|
13289
|
+
var init_real_local_run_provider = __esm({
|
|
13290
|
+
"src/internal/runtime/local-agent/real-local-run-provider.ts"() {
|
|
13291
|
+
init_providers();
|
|
13292
|
+
}
|
|
13293
|
+
});
|
|
13294
|
+
function inheritSubAgentCredentials(tool, creds) {
|
|
13295
|
+
const sink = tool[INHERIT_CREDENTIALS];
|
|
13296
|
+
if (typeof sink === "function") sink(creds);
|
|
13297
|
+
}
|
|
13298
|
+
async function applyDelegationStart(spec, input, iteration) {
|
|
13299
|
+
if (spec.onDelegationStart === void 0) return { input };
|
|
13300
|
+
const decision = await spec.onDelegationStart({ input, name: spec.name, iteration });
|
|
13301
|
+
if (decision === void 0) return { input };
|
|
13302
|
+
if (decision.proceed === false)
|
|
13303
|
+
return { reject: decision.rejectionReason ?? "(delegation rejected)" };
|
|
13304
|
+
return {
|
|
13305
|
+
input: decision.modifiedInput ?? input,
|
|
13306
|
+
...decision.modifiedMaxSteps !== void 0 ? { maxSteps: decision.modifiedMaxSteps } : {}
|
|
13307
|
+
};
|
|
13308
|
+
}
|
|
13309
|
+
async function collectChildToolResults(run) {
|
|
13310
|
+
const lines = [];
|
|
13311
|
+
for await (const event of run.stream()) {
|
|
13312
|
+
if (event.type === "tool_call" && event.status === "completed") {
|
|
13313
|
+
const rendered = typeof event.result === "string" ? event.result : JSON.stringify(event.result ?? null);
|
|
13314
|
+
lines.push(`${event.name}: ${rendered}`);
|
|
13315
|
+
}
|
|
13316
|
+
}
|
|
13317
|
+
if (lines.length === 0) return "";
|
|
13318
|
+
return `
|
|
13319
|
+
|
|
13320
|
+
<subagent-tool-results>
|
|
13321
|
+
${lines.join("\n")}
|
|
13322
|
+
</subagent-tool-results>`;
|
|
13323
|
+
}
|
|
13324
|
+
function buildChildCreateOptions(spec, inherited) {
|
|
13325
|
+
const model = spec.model ? { id: spec.model } : inherited?.model;
|
|
13326
|
+
return {
|
|
13327
|
+
...inherited?.apiKey !== void 0 ? { apiKey: inherited.apiKey } : {},
|
|
13328
|
+
...model !== void 0 ? { model } : {},
|
|
13329
|
+
...inherited?.plugins !== void 0 ? { plugins: inherited.plugins } : {},
|
|
13330
|
+
systemPrompt: spec.instructions,
|
|
13331
|
+
tools: spec.tools ?? []
|
|
13332
|
+
};
|
|
13333
|
+
}
|
|
13334
|
+
async function runChildAgent(spec, input, signal, maxSteps, inherited) {
|
|
13335
|
+
const { Agent: Agent2 } = await Promise.resolve().then(() => (init_agent(), agent_exports));
|
|
13336
|
+
const agent = await Agent2.create(buildChildCreateOptions(spec, inherited));
|
|
13337
|
+
try {
|
|
13338
|
+
const sendOptions = {
|
|
13339
|
+
...signal !== void 0 ? { signal } : {},
|
|
13340
|
+
...maxSteps !== void 0 ? { maxIterations: maxSteps } : {},
|
|
13341
|
+
// SE3 — a delegated child's turn is initiated by the coordinating parent.
|
|
13342
|
+
origin: { kind: "coordinator" }
|
|
13343
|
+
};
|
|
13344
|
+
const run = await agent.send(input, sendOptions);
|
|
13345
|
+
const result = await run.wait();
|
|
13346
|
+
const text = result.result ?? "(no response)";
|
|
13347
|
+
return spec.includeToolResults === true ? text + await collectChildToolResults(run) : text;
|
|
13348
|
+
} finally {
|
|
13349
|
+
agent.dispose();
|
|
13350
|
+
}
|
|
13351
|
+
}
|
|
13352
|
+
async function notifyDelegationError(spec, input, error, iteration) {
|
|
13353
|
+
if (spec.onDelegationComplete === void 0) return;
|
|
13354
|
+
try {
|
|
13355
|
+
await spec.onDelegationComplete({ input, name: spec.name, error, iteration });
|
|
13356
|
+
} catch {
|
|
13357
|
+
}
|
|
13358
|
+
}
|
|
13359
|
+
function applyMessageFilter(spec, input, messages) {
|
|
13360
|
+
if (spec.messageFilter === void 0 || messages === void 0) return input;
|
|
13361
|
+
const filtered = spec.messageFilter({ messages, input, name: spec.name });
|
|
13362
|
+
if (filtered.length === 0) return input;
|
|
13363
|
+
const preamble = filtered.map((m) => `${m.role}: ${m.content}`).join("\n");
|
|
13364
|
+
return `Prior conversation:
|
|
13365
|
+
${preamble}
|
|
13366
|
+
|
|
13367
|
+
Task:
|
|
13368
|
+
${input}`;
|
|
13369
|
+
}
|
|
13370
|
+
async function applyDelegationComplete(spec, input, result, iteration) {
|
|
13371
|
+
if (spec.onDelegationComplete === void 0) return result;
|
|
13372
|
+
const completion = await spec.onDelegationComplete({ input, name: spec.name, result, iteration });
|
|
13373
|
+
return completion?.feedback !== void 0 ? result + completion.feedback : result;
|
|
13374
|
+
}
|
|
13375
|
+
function defineSubAgent(spec, _parentDepth = 0) {
|
|
13376
|
+
const currentDepth = _parentDepth + 1;
|
|
13377
|
+
const maxDepth = spec.maxDelegationDepth ?? 3;
|
|
13378
|
+
if (currentDepth > maxDepth) {
|
|
13379
|
+
throw new MaxDelegationDepthError(currentDepth, maxDepth);
|
|
13380
|
+
}
|
|
13381
|
+
const inputZod = z.object({
|
|
13382
|
+
input: z.string().describe("Task for the subagent")
|
|
13383
|
+
});
|
|
13384
|
+
const inputSchema = {
|
|
13385
|
+
type: "object",
|
|
13386
|
+
properties: {
|
|
13387
|
+
input: { type: "string", description: "Task for the subagent" }
|
|
13388
|
+
},
|
|
13389
|
+
required: ["input"],
|
|
13390
|
+
additionalProperties: false
|
|
13391
|
+
};
|
|
13392
|
+
let iteration = 0;
|
|
13393
|
+
let inherited;
|
|
13394
|
+
const tool = {
|
|
13395
|
+
name: spec.name,
|
|
13396
|
+
description: spec.description,
|
|
13397
|
+
inputSchema,
|
|
13398
|
+
handler: async (rawInput, ctx) => {
|
|
13399
|
+
const { input: parsed } = inputZod.parse(rawInput);
|
|
13400
|
+
iteration += 1;
|
|
13401
|
+
const capturedIteration = iteration;
|
|
13402
|
+
const start = await applyDelegationStart(spec, parsed, capturedIteration);
|
|
13403
|
+
if ("reject" in start) return start.reject;
|
|
13404
|
+
const input = applyMessageFilter(spec, start.input, ctx?.messages);
|
|
13405
|
+
let result;
|
|
13406
|
+
try {
|
|
13407
|
+
result = await runChildAgent(spec, input, ctx?.signal, start.maxSteps, inherited);
|
|
13408
|
+
} catch (error) {
|
|
13409
|
+
await notifyDelegationError(spec, input, error, capturedIteration);
|
|
13410
|
+
throw error;
|
|
13411
|
+
}
|
|
13412
|
+
return applyDelegationComplete(spec, input, result, capturedIteration);
|
|
13413
|
+
}
|
|
13414
|
+
};
|
|
13415
|
+
Object.defineProperty(tool, INHERIT_CREDENTIALS, {
|
|
13416
|
+
value: ((creds) => {
|
|
13417
|
+
inherited = creds;
|
|
13418
|
+
}),
|
|
13419
|
+
enumerable: false
|
|
13420
|
+
});
|
|
13421
|
+
return tool;
|
|
13422
|
+
}
|
|
13423
|
+
function subAgentToolsFromDefinitions(agents2, parentTools) {
|
|
13424
|
+
return Object.entries(agents2).map(([name, def]) => {
|
|
13425
|
+
const whitelist = Array.isArray(def.tools) && def.tools.length > 0 ? new Set(def.tools) : void 0;
|
|
13426
|
+
const childTools = whitelist ? parentTools.filter((t) => whitelist.has(t.name)) : parentTools;
|
|
13427
|
+
return defineSubAgent({
|
|
13428
|
+
name,
|
|
13429
|
+
description: def.description,
|
|
13430
|
+
instructions: def.prompt,
|
|
13431
|
+
...def.model !== void 0 && def.model !== "inherit" ? { model: def.model.id } : {},
|
|
13432
|
+
tools: [...childTools]
|
|
13433
|
+
});
|
|
13434
|
+
});
|
|
13435
|
+
}
|
|
13436
|
+
var INHERIT_CREDENTIALS, MaxDelegationDepthError;
|
|
13437
|
+
var init_subagent = __esm({
|
|
13438
|
+
"src/a2a/subagent.ts"() {
|
|
13439
|
+
INHERIT_CREDENTIALS = /* @__PURE__ */ Symbol("theokit.subagent.inheritCredentials");
|
|
13440
|
+
MaxDelegationDepthError = class extends Error {
|
|
13441
|
+
constructor(currentDepth, maxDepth) {
|
|
13442
|
+
super(`Max delegation depth ${maxDepth} exceeded (current: ${currentDepth})`);
|
|
13443
|
+
this.currentDepth = currentDepth;
|
|
13444
|
+
this.maxDepth = maxDepth;
|
|
13445
|
+
this.name = "MaxDelegationDepthError";
|
|
13446
|
+
}
|
|
13447
|
+
currentDepth;
|
|
13448
|
+
maxDepth;
|
|
13449
|
+
code = "max_delegation_depth";
|
|
13450
|
+
};
|
|
13451
|
+
}
|
|
13452
|
+
});
|
|
13453
|
+
|
|
13362
13454
|
// src/internal/tool-registry/personality-filter.ts
|
|
13363
13455
|
function applyPersonalityFilter(exposedTools, whitelist, opts) {
|
|
13364
13456
|
if (whitelist === void 0) return exposedTools;
|
|
@@ -13414,36 +13506,49 @@ var init_personality_filter = __esm({
|
|
|
13414
13506
|
}
|
|
13415
13507
|
});
|
|
13416
13508
|
|
|
13417
|
-
// src/internal/runtime/local-agent/real-local-run-
|
|
13418
|
-
function
|
|
13419
|
-
if (
|
|
13420
|
-
|
|
13421
|
-
{ provider: "openrouter", prefix: "sk-or-" },
|
|
13422
|
-
{ provider: "anthropic", prefix: "sk-ant-" },
|
|
13423
|
-
{ provider: "openai", prefix: "sk-" }
|
|
13424
|
-
];
|
|
13425
|
-
for (const { provider, prefix } of byPrefix) {
|
|
13426
|
-
if (apiKey.startsWith(prefix) && getProviderProfile(provider) !== void 0) {
|
|
13427
|
-
return provider;
|
|
13428
|
-
}
|
|
13429
|
-
}
|
|
13430
|
-
return void 0;
|
|
13509
|
+
// src/internal/runtime/local-agent/real-local-run-tools.ts
|
|
13510
|
+
function declarativeSubagentTools(agents2, parentTools) {
|
|
13511
|
+
if (agents2 === void 0 || Object.keys(agents2).length === 0) return [];
|
|
13512
|
+
return subAgentToolsFromDefinitions(agents2, parentTools);
|
|
13431
13513
|
}
|
|
13432
|
-
function
|
|
13433
|
-
|
|
13434
|
-
|
|
13435
|
-
|
|
13436
|
-
|
|
13437
|
-
|
|
13438
|
-
}
|
|
13439
|
-
|
|
13440
|
-
|
|
13514
|
+
function bindParentCredentials(tools, agentOptions) {
|
|
13515
|
+
const parentPlugins = Array.isArray(agentOptions.plugins) ? agentOptions.plugins : void 0;
|
|
13516
|
+
const credentials = {
|
|
13517
|
+
...agentOptions.apiKey !== void 0 ? { apiKey: agentOptions.apiKey } : {},
|
|
13518
|
+
...typeof agentOptions.model === "object" ? { model: agentOptions.model } : {},
|
|
13519
|
+
...parentPlugins !== void 0 ? { plugins: parentPlugins } : {}
|
|
13520
|
+
};
|
|
13521
|
+
for (const tool of tools) inheritSubAgentCredentials(tool, credentials);
|
|
13522
|
+
}
|
|
13523
|
+
function buildCustomToolsInput(agentOptions, sendOptions, pluginManager, personalityToolWhitelist, agentId, personalityName, subagents, effectiveModel) {
|
|
13524
|
+
const baseTools = sendOptions?.tools ?? agentOptions.tools ?? [];
|
|
13525
|
+
const agentsForTools = subagents !== void 0 && Object.keys(subagents).length > 0 ? subagents : agentOptions.agents;
|
|
13526
|
+
const subagentTools = declarativeSubagentTools(agentsForTools, baseTools);
|
|
13527
|
+
const pluginTools = pluginManager?.aggregated.tools ?? [];
|
|
13528
|
+
const reasoningTools = reasoningActive(agentOptions.reasoning, effectiveModel) ? [createThinkTool()] : [];
|
|
13529
|
+
if (baseTools.length === 0 && subagentTools.length === 0 && pluginTools.length === 0 && reasoningTools.length === 0) {
|
|
13530
|
+
return {};
|
|
13441
13531
|
}
|
|
13442
|
-
|
|
13532
|
+
const allTools = [...baseTools, ...subagentTools, ...pluginTools, ...reasoningTools];
|
|
13533
|
+
bindParentCredentials(allTools, agentOptions);
|
|
13534
|
+
const merged = allTools.map((tool) => ({
|
|
13535
|
+
name: tool.name,
|
|
13536
|
+
description: tool.description,
|
|
13537
|
+
inputSchema: tool.inputSchema,
|
|
13538
|
+
handler: tool.handler
|
|
13539
|
+
}));
|
|
13540
|
+
const customTools = applyPersonalityFilter(merged, personalityToolWhitelist, {
|
|
13541
|
+
agentId,
|
|
13542
|
+
personalityName
|
|
13543
|
+
});
|
|
13544
|
+
if (customTools.length === 0 && personalityToolWhitelist === void 0) return {};
|
|
13545
|
+
return { customTools };
|
|
13443
13546
|
}
|
|
13444
|
-
var
|
|
13445
|
-
"src/internal/runtime/local-agent/real-local-run-
|
|
13446
|
-
|
|
13547
|
+
var init_real_local_run_tools = __esm({
|
|
13548
|
+
"src/internal/runtime/local-agent/real-local-run-tools.ts"() {
|
|
13549
|
+
init_subagent();
|
|
13550
|
+
init_personality_filter();
|
|
13551
|
+
init_native_reasoning();
|
|
13447
13552
|
}
|
|
13448
13553
|
});
|
|
13449
13554
|
|
|
@@ -13562,7 +13667,8 @@ function buildLoopInputs(options, runId, userText) {
|
|
|
13562
13667
|
options.personalityToolWhitelist,
|
|
13563
13668
|
options.agentId,
|
|
13564
13669
|
options.personalityName,
|
|
13565
|
-
options.subagents
|
|
13670
|
+
options.subagents,
|
|
13671
|
+
options.model
|
|
13566
13672
|
),
|
|
13567
13673
|
...options.pluginManager !== void 0 ? { pluginManager: options.pluginManager } : {},
|
|
13568
13674
|
// D318 — forward SendOptions.signal to the agent loop so streamLlmTurn
|
|
@@ -13600,40 +13706,6 @@ function buildLoopInputs(options, runId, userText) {
|
|
|
13600
13706
|
...options.agentOptions.memoryProvider !== void 0 ? { memoryProvider: options.agentOptions.memoryProvider } : {}
|
|
13601
13707
|
};
|
|
13602
13708
|
}
|
|
13603
|
-
function declarativeSubagentTools(agents2, parentTools) {
|
|
13604
|
-
if (agents2 === void 0 || Object.keys(agents2).length === 0) return [];
|
|
13605
|
-
return subAgentToolsFromDefinitions(agents2, parentTools);
|
|
13606
|
-
}
|
|
13607
|
-
function bindParentCredentials(tools, agentOptions) {
|
|
13608
|
-
const parentPlugins = Array.isArray(agentOptions.plugins) ? agentOptions.plugins : void 0;
|
|
13609
|
-
const credentials = {
|
|
13610
|
-
...agentOptions.apiKey !== void 0 ? { apiKey: agentOptions.apiKey } : {},
|
|
13611
|
-
...typeof agentOptions.model === "object" ? { model: agentOptions.model } : {},
|
|
13612
|
-
...parentPlugins !== void 0 ? { plugins: parentPlugins } : {}
|
|
13613
|
-
};
|
|
13614
|
-
for (const tool of tools) inheritSubAgentCredentials(tool, credentials);
|
|
13615
|
-
}
|
|
13616
|
-
function buildCustomToolsInput(agentOptions, sendOptions, pluginManager, personalityToolWhitelist, agentId, personalityName, subagents) {
|
|
13617
|
-
const baseTools = sendOptions?.tools ?? agentOptions.tools ?? [];
|
|
13618
|
-
const agentsForTools = subagents !== void 0 && Object.keys(subagents).length > 0 ? subagents : agentOptions.agents;
|
|
13619
|
-
const subagentTools = declarativeSubagentTools(agentsForTools, baseTools);
|
|
13620
|
-
const pluginTools = pluginManager?.aggregated.tools ?? [];
|
|
13621
|
-
if (baseTools.length === 0 && subagentTools.length === 0 && pluginTools.length === 0) return {};
|
|
13622
|
-
const allTools = [...baseTools, ...subagentTools, ...pluginTools];
|
|
13623
|
-
bindParentCredentials(allTools, agentOptions);
|
|
13624
|
-
const merged = allTools.map((tool) => ({
|
|
13625
|
-
name: tool.name,
|
|
13626
|
-
description: tool.description,
|
|
13627
|
-
inputSchema: tool.inputSchema,
|
|
13628
|
-
handler: tool.handler
|
|
13629
|
-
}));
|
|
13630
|
-
const customTools = applyPersonalityFilter(merged, personalityToolWhitelist, {
|
|
13631
|
-
agentId,
|
|
13632
|
-
personalityName
|
|
13633
|
-
});
|
|
13634
|
-
if (customTools.length === 0 && personalityToolWhitelist === void 0) return {};
|
|
13635
|
-
return { customTools };
|
|
13636
|
-
}
|
|
13637
13709
|
function buildMcpMap(options) {
|
|
13638
13710
|
const map = /* @__PURE__ */ new Map();
|
|
13639
13711
|
const inline = options.sendOptions.mcpServers ?? options.agentOptions.mcpServers;
|
|
@@ -13646,7 +13718,6 @@ function buildMcpMap(options) {
|
|
|
13646
13718
|
var pluginProvidersAnnounced, RealLocalRun;
|
|
13647
13719
|
var init_real_local_run = __esm({
|
|
13648
13720
|
"src/internal/runtime/local-agent/real-local-run.ts"() {
|
|
13649
|
-
init_subagent();
|
|
13650
13721
|
init_errors();
|
|
13651
13722
|
init_run_events();
|
|
13652
13723
|
init_loop();
|
|
@@ -13659,11 +13730,11 @@ var init_real_local_run = __esm({
|
|
|
13659
13730
|
init_providers();
|
|
13660
13731
|
init_register_plugin_providers();
|
|
13661
13732
|
init_tracer();
|
|
13662
|
-
init_personality_filter();
|
|
13663
13733
|
init_async_local_storage();
|
|
13664
13734
|
init_fixture_run_base();
|
|
13665
13735
|
init_run_registry();
|
|
13666
13736
|
init_real_local_run_provider();
|
|
13737
|
+
init_real_local_run_tools();
|
|
13667
13738
|
pluginProvidersAnnounced = false;
|
|
13668
13739
|
RealLocalRun = class extends FixtureRunBase {
|
|
13669
13740
|
buildInputs;
|