@theokit/sdk 2.2.0 → 2.4.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.
Files changed (85) hide show
  1. package/CHANGELOG.md +122 -0
  2. package/dist/a2a/index.cjs +191 -48
  3. package/dist/a2a/index.cjs.map +1 -1
  4. package/dist/a2a/index.js +192 -49
  5. package/dist/a2a/index.js.map +1 -1
  6. package/dist/compaction.cjs +78 -0
  7. package/dist/compaction.cjs.map +1 -0
  8. package/dist/compaction.d.cts +76 -0
  9. package/dist/compaction.d.ts +76 -0
  10. package/dist/compaction.js +70 -0
  11. package/dist/compaction.js.map +1 -0
  12. package/dist/{cron-JSPSFczQ.d.cts → cron-B656C3iq.d.cts} +28 -2
  13. package/dist/{cron-Aksw2Hy4.d.ts → cron-CM2M9mhB.d.ts} +28 -2
  14. package/dist/cron.cjs +192 -57
  15. package/dist/cron.cjs.map +1 -1
  16. package/dist/cron.d.cts +2 -2
  17. package/dist/cron.d.ts +2 -2
  18. package/dist/cron.js +192 -57
  19. package/dist/cron.js.map +1 -1
  20. package/dist/{errors-Bcw_Pakm.d.ts → errors-DG_7CAUg.d.ts} +1 -1
  21. package/dist/{errors-Vhg6ZV4o.d.cts → errors-QDYUPABr.d.cts} +1 -1
  22. package/dist/errors.d.cts +2 -2
  23. package/dist/eval.cjs +192 -57
  24. package/dist/eval.cjs.map +1 -1
  25. package/dist/eval.js +192 -57
  26. package/dist/eval.js.map +1 -1
  27. package/dist/index.cjs +275 -68
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.cts +50 -7
  30. package/dist/index.d.ts +50 -7
  31. package/dist/index.js +275 -69
  32. package/dist/index.js.map +1 -1
  33. package/dist/internal/agent-loop/loop.d.ts +5 -0
  34. package/dist/internal/llm/model-capabilities.d.ts +40 -0
  35. package/dist/internal/llm/model-identifier.d.ts +9 -1
  36. package/dist/internal/llm/model-option.d.ts +38 -0
  37. package/dist/internal/runtime/compression/compression-attempt.d.ts +24 -0
  38. package/dist/internal/runtime/compression/compression-config.d.ts +33 -0
  39. package/dist/internal/runtime/compression/compression-decision.d.ts +10 -0
  40. package/dist/internal/runtime/compression/compression-helpers.d.ts +18 -0
  41. package/dist/internal/runtime/compression/compression-model-registry.d.ts +41 -0
  42. package/dist/internal/runtime/compression/compression-summarizer.d.ts +29 -0
  43. package/dist/internal/runtime/context/project-instructions.d.ts +66 -0
  44. package/dist/internal/runtime/context/replay-history.d.ts +43 -0
  45. package/dist/internal/runtime/hooks/hooks-frontmatter.d.ts +1 -1
  46. package/dist/internal/runtime/lifecycle/run-to-completion.d.ts +22 -0
  47. package/dist/internal/runtime/skills/discover-skills.d.ts +68 -0
  48. package/dist/internal/runtime/skills/skills-block.d.ts +18 -0
  49. package/dist/internal/runtime/skills/subagent-tool-scope.d.ts +25 -0
  50. package/dist/messages.cjs +24 -0
  51. package/dist/messages.cjs.map +1 -0
  52. package/dist/messages.d.cts +33 -0
  53. package/dist/messages.d.ts +33 -0
  54. package/dist/messages.js +20 -0
  55. package/dist/messages.js.map +1 -0
  56. package/dist/models.cjs +233 -0
  57. package/dist/models.cjs.map +1 -0
  58. package/dist/models.d.cts +16 -0
  59. package/dist/models.d.ts +16 -0
  60. package/dist/models.js +228 -0
  61. package/dist/models.js.map +1 -0
  62. package/dist/project.cjs +149 -0
  63. package/dist/project.cjs.map +1 -0
  64. package/dist/project.d.cts +14 -0
  65. package/dist/project.d.ts +14 -0
  66. package/dist/project.js +146 -0
  67. package/dist/project.js.map +1 -0
  68. package/dist/{run-ekGKZlmg.d.cts → run-BPRYG1Id.d.cts} +55 -2
  69. package/dist/{run-ekGKZlmg.d.ts → run-BPRYG1Id.d.ts} +55 -2
  70. package/dist/skills.cjs +282 -0
  71. package/dist/skills.cjs.map +1 -0
  72. package/dist/skills.d.cts +19 -0
  73. package/dist/skills.d.ts +19 -0
  74. package/dist/skills.js +279 -0
  75. package/dist/skills.js.map +1 -0
  76. package/dist/subagents.cjs +24 -0
  77. package/dist/subagents.cjs.map +1 -0
  78. package/dist/subagents.d.cts +14 -0
  79. package/dist/subagents.d.ts +14 -0
  80. package/dist/subagents.js +21 -0
  81. package/dist/subagents.js.map +1 -0
  82. package/dist/types/agent.d.ts +22 -0
  83. package/dist/types/conversation-storage.d.ts +5 -1
  84. package/dist/types/run.d.ts +54 -1
  85. package/package.json +62 -2
package/dist/cron.cjs CHANGED
@@ -1440,6 +1440,71 @@ var init_agent_factory_registry = __esm({
1440
1440
  }
1441
1441
  });
1442
1442
 
1443
+ // src/internal/runtime/lifecycle/run-to-completion.ts
1444
+ var run_to_completion_exports = {};
1445
+ __export(run_to_completion_exports, {
1446
+ classifyRound: () => classifyRound,
1447
+ runToCompletionImpl: () => runToCompletionImpl
1448
+ });
1449
+ function isEmptyRound(result) {
1450
+ return (result.result ?? "").trim() === "";
1451
+ }
1452
+ function classifyRound(result, round, maxRounds, emptyStreak) {
1453
+ if (result.stoppedAtIterationLimit !== true) return "done";
1454
+ if (isEmptyRound(result) && emptyStreak >= 1) return "no_progress";
1455
+ if (round >= maxRounds) return "step_limit";
1456
+ return "continue";
1457
+ }
1458
+ function addUsage(acc, u) {
1459
+ if (u === void 0) return acc;
1460
+ const inputTokens = (acc?.inputTokens ?? 0) + u.inputTokens;
1461
+ const outputTokens = (acc?.outputTokens ?? 0) + u.outputTokens;
1462
+ const sumOpt = (a, b) => a === void 0 && b === void 0 ? void 0 : (a ?? 0) + (b ?? 0);
1463
+ return {
1464
+ inputTokens,
1465
+ outputTokens,
1466
+ totalTokens: inputTokens + outputTokens,
1467
+ cacheReadTokens: sumOpt(acc?.cacheReadTokens, u.cacheReadTokens),
1468
+ cacheWriteTokens: sumOpt(acc?.cacheWriteTokens, u.cacheWriteTokens),
1469
+ reasoningTokens: sumOpt(acc?.reasoningTokens, u.reasoningTokens)
1470
+ };
1471
+ }
1472
+ function buildResult(terminal, rounds, lastResult, usage) {
1473
+ return { terminal, rounds, lastResult, ...usage !== void 0 ? { usage } : {} };
1474
+ }
1475
+ async function stepRound(agent, prompt, sendOptions, round, maxRounds, state3) {
1476
+ const run = await agent.send(prompt, sendOptions);
1477
+ const result = await run.wait();
1478
+ const usage = addUsage(state3.usage, result.usage);
1479
+ const decision = classifyRound(result, round, maxRounds, state3.emptyStreak);
1480
+ if (decision !== "continue") return { terminal: buildResult(decision, round, result, usage) };
1481
+ const emptyStreak = isEmptyRound(result) ? state3.emptyStreak + 1 : 0;
1482
+ return { next: { usage, emptyStreak }, lastResult: result };
1483
+ }
1484
+ async function runToCompletionImpl(agent, message, options) {
1485
+ const maxRounds = options?.maxRounds ?? DEFAULT_MAX_ROUNDS;
1486
+ const continuationPrompt = options?.continuationPrompt ?? DEFAULT_CONTINUATION_PROMPT;
1487
+ const { onTruncated, signal, sendOptions } = options ?? {};
1488
+ let state3 = { usage: void 0, emptyStreak: 0 };
1489
+ for (let round = 0; ; round += 1) {
1490
+ const prompt = round === 0 ? message : continuationPrompt;
1491
+ const outcome = await stepRound(agent, prompt, sendOptions, round, maxRounds, state3);
1492
+ if ("terminal" in outcome) return outcome.terminal;
1493
+ state3 = outcome.next;
1494
+ await onTruncated?.({ round });
1495
+ if (signal?.aborted === true) {
1496
+ return buildResult("step_limit", round, outcome.lastResult, state3.usage);
1497
+ }
1498
+ }
1499
+ }
1500
+ var DEFAULT_MAX_ROUNDS, DEFAULT_CONTINUATION_PROMPT;
1501
+ var init_run_to_completion = __esm({
1502
+ "src/internal/runtime/lifecycle/run-to-completion.ts"() {
1503
+ DEFAULT_MAX_ROUNDS = 5;
1504
+ DEFAULT_CONTINUATION_PROMPT = "Continue from where you left off and finish the task. If it is already complete, give the final answer.";
1505
+ }
1506
+ });
1507
+
1443
1508
  // src/internal/runtime/lifecycle/fork-agent.ts
1444
1509
  var fork_agent_exports = {};
1445
1510
  __export(fork_agent_exports, {
@@ -4867,6 +4932,18 @@ var CloudAgent = class {
4867
4932
  "fork"
4868
4933
  );
4869
4934
  }
4935
+ /**
4936
+ * The continuation driver re-sends against a stateful local session; the
4937
+ * cloud runtime manages its own continuation policy server-side (M1 Phase 3).
4938
+ *
4939
+ * @public
4940
+ */
4941
+ runToCompletion() {
4942
+ throw new UnsupportedRunOperationError(
4943
+ "Agent.runToCompletion() is not supported on cloud agents. Cloud runtime manages continuation server-side. Use a local agent.",
4944
+ "runToCompletion"
4945
+ );
4946
+ }
4870
4947
  /**
4871
4948
  * Personality presets require consistent server-side enforcement that
4872
4949
  * the cloud runtime (pre-release) does not yet provide. Reject explicitly
@@ -6427,6 +6504,8 @@ function parseSubagentMarkdown(raw, filename) {
6427
6504
  if (fields.model !== void 0) {
6428
6505
  definition.model = fields.model === "inherit" ? "inherit" : { id: fields.model };
6429
6506
  }
6507
+ const tools = fields.tools?.split(/[\s,]+/).map((t) => t.trim()).filter((t) => t.length > 0);
6508
+ if (tools !== void 0 && tools.length > 0) definition.tools = tools;
6430
6509
  return { name, definition };
6431
6510
  }
6432
6511
  function splitFrontmatter2(raw, filename) {
@@ -6590,21 +6669,24 @@ ${lines.join("\n")}
6590
6669
  }
6591
6670
  };
6592
6671
 
6672
+ // src/internal/runtime/skills/skills-block.ts
6673
+ function buildSkillsBlock(skills) {
6674
+ if (skills.length === 0) return void 0;
6675
+ const lines = skills.map(
6676
+ (skill) => ` - ${escapeBlockBody(skill.name)}: ${escapeBlockBody(skill.description)}`
6677
+ );
6678
+ return `<skills>
6679
+ ${lines.join("\n")}
6680
+ </skills>`;
6681
+ }
6682
+
6593
6683
  // src/internal/runtime/system-prompt/sources/skills-provider.ts
6594
6684
  var SkillsPromptProvider = class {
6595
6685
  id = "skills";
6596
6686
  priority = 20;
6597
6687
  contribute(ctx) {
6598
6688
  if (ctx.skillsAutoInject === false) return Promise.resolve(void 0);
6599
- if (ctx.skills.length === 0) return Promise.resolve(void 0);
6600
- const lines = ctx.skills.map((skill) => {
6601
- const name = escapeBlockBody(skill.name);
6602
- const description = escapeBlockBody(skill.description);
6603
- return ` - ${name}: ${description}`;
6604
- });
6605
- return Promise.resolve(`<skills>
6606
- ${lines.join("\n")}
6607
- </skills>`);
6689
+ return Promise.resolve(buildSkillsBlock(ctx.skills));
6608
6690
  }
6609
6691
  };
6610
6692
 
@@ -7712,7 +7794,7 @@ async function loadPluginManifestFromMarkdown(pluginsRoot, folderName) {
7712
7794
  return metadata;
7713
7795
  }
7714
7796
 
7715
- // src/internal/runtime/skills/skills-manager.ts
7797
+ // src/internal/runtime/skills/discover-skills.ts
7716
7798
  init_errors();
7717
7799
 
7718
7800
  // src/internal/runtime/skills/skill-frontmatter.ts
@@ -7784,6 +7866,61 @@ function hasContent(value) {
7784
7866
  return value !== void 0 && value.trim().length > 0;
7785
7867
  }
7786
7868
 
7869
+ // src/internal/runtime/skills/discover-skills.ts
7870
+ async function discoverSkills(dir, options) {
7871
+ let entries;
7872
+ try {
7873
+ entries = await readWorkspaceDir(dir, "skills_read_error", "skills directory");
7874
+ } catch {
7875
+ return [];
7876
+ }
7877
+ const skills = [];
7878
+ for (const entry of entries) {
7879
+ if (!entry.isDirectory()) continue;
7880
+ let skillDir;
7881
+ try {
7882
+ skillDir = safePathJoin(dir, entry.name);
7883
+ assertNoSymlinkEscape(skillDir, dir);
7884
+ } catch {
7885
+ continue;
7886
+ }
7887
+ const skillPath = path.join(skillDir, "SKILL.md");
7888
+ let raw;
7889
+ try {
7890
+ raw = await promises.readFile(skillPath, "utf8");
7891
+ } catch {
7892
+ continue;
7893
+ }
7894
+ const skill = tryParseSkill(raw, entry.name, skillPath, options);
7895
+ if (skill !== void 0) skills.push(skill);
7896
+ }
7897
+ return skills;
7898
+ }
7899
+ function tryParseSkill(raw, fallbackName, source, options) {
7900
+ try {
7901
+ const frontmatter = parseSkillFrontmatter(raw, fallbackName);
7902
+ const skill = {
7903
+ name: frontmatter.name,
7904
+ description: frontmatter.description,
7905
+ source
7906
+ };
7907
+ if (frontmatter.category !== void 0) skill.category = frontmatter.category;
7908
+ if (frontmatter.dependencies !== void 0) skill.dependencies = frontmatter.dependencies;
7909
+ return skill;
7910
+ } catch (cause) {
7911
+ if (cause instanceof ConfigurationError) {
7912
+ options?.onInvalidSkill?.({
7913
+ name: fallbackName,
7914
+ source,
7915
+ code: cause.code ?? "unknown",
7916
+ message: cause.message
7917
+ });
7918
+ return void 0;
7919
+ }
7920
+ throw cause;
7921
+ }
7922
+ }
7923
+
7787
7924
  // src/internal/runtime/skills/skills-manager.ts
7788
7925
  var SkillsManager = class {
7789
7926
  constructor(cwd, _enabled, settingSourcesIncludeProject) {
@@ -7801,56 +7938,20 @@ var SkillsManager = class {
7801
7938
  await this.refresh();
7802
7939
  }
7803
7940
  async refresh() {
7804
- this.skills = [];
7805
7941
  const skillsRoot = path.join(this.cwd, ".theokit", "skills");
7806
- const entries = await readWorkspaceDir(skillsRoot, "skills_read_error", "skills directory");
7807
- for (const entry of entries) {
7808
- if (!entry.isDirectory()) continue;
7809
- let skillDir;
7810
- try {
7811
- skillDir = safePathJoin(skillsRoot, entry.name);
7812
- assertNoSymlinkEscape(skillDir, skillsRoot);
7813
- } catch {
7814
- continue;
7815
- }
7816
- const skillPath = path.join(skillDir, "SKILL.md");
7817
- let raw;
7818
- try {
7819
- raw = await promises.readFile(skillPath, "utf8");
7820
- } catch {
7821
- continue;
7942
+ this.skills = await discoverSkills(skillsRoot, {
7943
+ onInvalidSkill: (info) => {
7944
+ process.stderr.write(
7945
+ `[theokit-sdk] skill ${info.name} skipped (${info.code}): ${info.message}
7946
+ `
7947
+ );
7822
7948
  }
7823
- const metadata = tryParseSkill(raw, entry.name, skillPath);
7824
- if (metadata !== void 0) this.skills.push(metadata);
7825
- }
7949
+ });
7826
7950
  }
7827
7951
  list() {
7828
7952
  return Promise.resolve(this.skills);
7829
7953
  }
7830
7954
  };
7831
- function tryParseSkill(raw, fallbackName, source) {
7832
- try {
7833
- const frontmatter = parseSkillFrontmatter(raw, fallbackName);
7834
- const metadata = {
7835
- name: frontmatter.name,
7836
- description: frontmatter.description,
7837
- source
7838
- };
7839
- if (frontmatter.category !== void 0) metadata.category = frontmatter.category;
7840
- if (frontmatter.dependencies !== void 0) metadata.dependencies = frontmatter.dependencies;
7841
- return metadata;
7842
- } catch (cause) {
7843
- if (cause instanceof ConfigurationError) {
7844
- const code = cause.code ?? "unknown";
7845
- process.stderr.write(
7846
- `[theokit-sdk] skill ${fallbackName} skipped (${code}): ${cause.message}
7847
- `
7848
- );
7849
- return void 0;
7850
- }
7851
- throw cause;
7852
- }
7853
- }
7854
7955
 
7855
7956
  // src/internal/runtime/local-agent/local-agent-bootstrap.ts
7856
7957
  function registerLocalAgent(args) {
@@ -8263,6 +8364,7 @@ async function initLoopContext(inputs) {
8263
8364
  finalStatus: "finished",
8264
8365
  usage: new UsageAccumulator(),
8265
8366
  nudgeAttempts: 0,
8367
+ stopFeedbackAttempts: 0,
8266
8368
  ...memoryProviderHandle !== void 0 ? { memoryProviderHandle } : {},
8267
8369
  ...memorySystemPromptAdditions !== void 0 ? { memorySystemPromptAdditions } : {}
8268
8370
  };
@@ -8407,8 +8509,9 @@ function registerLoopError(ctx, cause) {
8407
8509
  if (ctx.error !== void 0) return;
8408
8510
  const rawMessage = cause?.message;
8409
8511
  const message = typeof rawMessage === "string" ? rawMessage : cause instanceof Error ? cause.message : String(cause);
8512
+ const metaCode = cause?.metadata?.code;
8410
8513
  const rawCode = cause?.code;
8411
- const code = typeof rawCode === "string" ? rawCode : void 0;
8514
+ const code = typeof metaCode === "string" ? metaCode : typeof rawCode === "string" ? rawCode : void 0;
8412
8515
  ctx.error = code !== void 0 ? { message, code, cause } : { message, cause };
8413
8516
  }
8414
8517
  async function runCollectorLoop(generator, inputs, ctx) {
@@ -9204,6 +9307,7 @@ function computeUsageCost(inputs, usage) {
9204
9307
 
9205
9308
  // src/internal/agent-loop/loop.ts
9206
9309
  var MAX_NUDGE_ATTEMPTS = 2;
9310
+ var MAX_STOP_FEEDBACK_ATTEMPTS = 2;
9207
9311
  async function runAgentLoop(inputs) {
9208
9312
  const sendSpan = inputs.telemetry?.startSpan("agent.send", {
9209
9313
  agentId: inputs.agentId,
@@ -9361,6 +9465,28 @@ function shouldNudgeAndContinue(ctx, llmOutput) {
9361
9465
  });
9362
9466
  return true;
9363
9467
  }
9468
+ async function reflectAfterStop(inputs, ctx) {
9469
+ const result = await inputs.hooks.run({
9470
+ event: "stop",
9471
+ agentId: inputs.agentId,
9472
+ runId: inputs.runId
9473
+ });
9474
+ if (result.blocked) return false;
9475
+ if (ctx.stopFeedbackAttempts >= MAX_STOP_FEEDBACK_ATTEMPTS) return false;
9476
+ const feedback = result.decisions.find(
9477
+ (d) => d.decision === "feedback" && (d.feedback ?? "").length > 0
9478
+ )?.feedback;
9479
+ if (feedback === void 0) return false;
9480
+ ctx.stopFeedbackAttempts += 1;
9481
+ ctx.messages.push({ role: "user", content: [{ type: "text", text: feedback }] });
9482
+ return true;
9483
+ }
9484
+ async function finishOrReflect(inputs, ctx, llmOutput) {
9485
+ if (shouldNudgeAndContinue(ctx, llmOutput)) return "continue";
9486
+ if (await reflectAfterStop(inputs, ctx)) return "continue";
9487
+ ctx.finalStatus = "finished";
9488
+ return "done";
9489
+ }
9364
9490
  async function runIteration(inputs, ctx) {
9365
9491
  const llmOutput = await streamLlmTurn(inputs, ctx);
9366
9492
  accumulateUsage(ctx.usage, llmOutput);
@@ -9394,9 +9520,7 @@ async function continueOrTerminate(inputs, ctx, llmOutput) {
9394
9520
  await emitAssistantTextStep(inputs, ctx, llmOutput.text);
9395
9521
  }
9396
9522
  if (llmOutput.stopReason !== "tool_use" || llmOutput.toolCalls.length === 0) {
9397
- if (shouldNudgeAndContinue(ctx, llmOutput)) return "continue";
9398
- ctx.finalStatus = "finished";
9399
- return "done";
9523
+ return finishOrReflect(inputs, ctx, llmOutput);
9400
9524
  }
9401
9525
  ctx.messages.push(buildAssistantTurn(llmOutput.text, llmOutput.toolCalls));
9402
9526
  const toolResults = await dispatchTools(inputs, ctx.tools, llmOutput.toolCalls, ctx.events);
@@ -14347,6 +14471,13 @@ function localAgentRunUntil(agent, goal, options) {
14347
14471
  }
14348
14472
  return wrap();
14349
14473
  }
14474
+ function localAgentRunToCompletion(agent, message, options) {
14475
+ async function run() {
14476
+ const { runToCompletionImpl: runToCompletionImpl2 } = await Promise.resolve().then(() => (init_run_to_completion(), run_to_completion_exports));
14477
+ return runToCompletionImpl2({ send: (m, o) => agent.send(m, o) }, message, options);
14478
+ }
14479
+ return run();
14480
+ }
14350
14481
  async function localAgentFork(parent, options) {
14351
14482
  const { forkAgentImpl: forkAgentImpl2 } = await Promise.resolve().then(() => (init_fork_agent(), fork_agent_exports));
14352
14483
  const { getAgentFacade: getAgentFacade2 } = await Promise.resolve().then(() => (init_agent_factory_registry(), agent_factory_registry_exports));
@@ -14887,6 +15018,10 @@ var LocalAgent = class {
14887
15018
  fork(options) {
14888
15019
  return localAgentFork({ agentId: this.agentId, options: this.options, personalitySlugSnapshot: this.personalityStore.active(this.agentId) }, options);
14889
15020
  }
15021
+ // biome-ignore format: G8 budget — see runUntil comment above.
15022
+ runToCompletion(message, options) {
15023
+ return localAgentRunToCompletion(this, message, options);
15024
+ }
14890
15025
  };
14891
15026
  function resolveCwd(cwd) {
14892
15027
  return (Array.isArray(cwd) ? cwd[0] : cwd) ?? process.cwd();