@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.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import './run-ekGKZlmg.cjs';
2
- export { I as Cron } from './cron-JSPSFczQ.cjs';
1
+ import './run-BPRYG1Id.cjs';
2
+ export { I as Cron } from './cron-B656C3iq.cjs';
package/dist/cron.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import './run-ekGKZlmg.js';
2
- export { I as Cron } from './cron-Aksw2Hy4.js';
1
+ import './run-BPRYG1Id.js';
2
+ export { I as Cron } from './cron-CM2M9mhB.js';
package/dist/cron.js CHANGED
@@ -1437,6 +1437,71 @@ var init_agent_factory_registry = __esm({
1437
1437
  }
1438
1438
  });
1439
1439
 
1440
+ // src/internal/runtime/lifecycle/run-to-completion.ts
1441
+ var run_to_completion_exports = {};
1442
+ __export(run_to_completion_exports, {
1443
+ classifyRound: () => classifyRound,
1444
+ runToCompletionImpl: () => runToCompletionImpl
1445
+ });
1446
+ function isEmptyRound(result) {
1447
+ return (result.result ?? "").trim() === "";
1448
+ }
1449
+ function classifyRound(result, round, maxRounds, emptyStreak) {
1450
+ if (result.stoppedAtIterationLimit !== true) return "done";
1451
+ if (isEmptyRound(result) && emptyStreak >= 1) return "no_progress";
1452
+ if (round >= maxRounds) return "step_limit";
1453
+ return "continue";
1454
+ }
1455
+ function addUsage(acc, u) {
1456
+ if (u === void 0) return acc;
1457
+ const inputTokens = (acc?.inputTokens ?? 0) + u.inputTokens;
1458
+ const outputTokens = (acc?.outputTokens ?? 0) + u.outputTokens;
1459
+ const sumOpt = (a, b) => a === void 0 && b === void 0 ? void 0 : (a ?? 0) + (b ?? 0);
1460
+ return {
1461
+ inputTokens,
1462
+ outputTokens,
1463
+ totalTokens: inputTokens + outputTokens,
1464
+ cacheReadTokens: sumOpt(acc?.cacheReadTokens, u.cacheReadTokens),
1465
+ cacheWriteTokens: sumOpt(acc?.cacheWriteTokens, u.cacheWriteTokens),
1466
+ reasoningTokens: sumOpt(acc?.reasoningTokens, u.reasoningTokens)
1467
+ };
1468
+ }
1469
+ function buildResult(terminal, rounds, lastResult, usage) {
1470
+ return { terminal, rounds, lastResult, ...usage !== void 0 ? { usage } : {} };
1471
+ }
1472
+ async function stepRound(agent, prompt, sendOptions, round, maxRounds, state3) {
1473
+ const run = await agent.send(prompt, sendOptions);
1474
+ const result = await run.wait();
1475
+ const usage = addUsage(state3.usage, result.usage);
1476
+ const decision = classifyRound(result, round, maxRounds, state3.emptyStreak);
1477
+ if (decision !== "continue") return { terminal: buildResult(decision, round, result, usage) };
1478
+ const emptyStreak = isEmptyRound(result) ? state3.emptyStreak + 1 : 0;
1479
+ return { next: { usage, emptyStreak }, lastResult: result };
1480
+ }
1481
+ async function runToCompletionImpl(agent, message, options) {
1482
+ const maxRounds = options?.maxRounds ?? DEFAULT_MAX_ROUNDS;
1483
+ const continuationPrompt = options?.continuationPrompt ?? DEFAULT_CONTINUATION_PROMPT;
1484
+ const { onTruncated, signal, sendOptions } = options ?? {};
1485
+ let state3 = { usage: void 0, emptyStreak: 0 };
1486
+ for (let round = 0; ; round += 1) {
1487
+ const prompt = round === 0 ? message : continuationPrompt;
1488
+ const outcome = await stepRound(agent, prompt, sendOptions, round, maxRounds, state3);
1489
+ if ("terminal" in outcome) return outcome.terminal;
1490
+ state3 = outcome.next;
1491
+ await onTruncated?.({ round });
1492
+ if (signal?.aborted === true) {
1493
+ return buildResult("step_limit", round, outcome.lastResult, state3.usage);
1494
+ }
1495
+ }
1496
+ }
1497
+ var DEFAULT_MAX_ROUNDS, DEFAULT_CONTINUATION_PROMPT;
1498
+ var init_run_to_completion = __esm({
1499
+ "src/internal/runtime/lifecycle/run-to-completion.ts"() {
1500
+ DEFAULT_MAX_ROUNDS = 5;
1501
+ DEFAULT_CONTINUATION_PROMPT = "Continue from where you left off and finish the task. If it is already complete, give the final answer.";
1502
+ }
1503
+ });
1504
+
1440
1505
  // src/internal/runtime/lifecycle/fork-agent.ts
1441
1506
  var fork_agent_exports = {};
1442
1507
  __export(fork_agent_exports, {
@@ -4864,6 +4929,18 @@ var CloudAgent = class {
4864
4929
  "fork"
4865
4930
  );
4866
4931
  }
4932
+ /**
4933
+ * The continuation driver re-sends against a stateful local session; the
4934
+ * cloud runtime manages its own continuation policy server-side (M1 Phase 3).
4935
+ *
4936
+ * @public
4937
+ */
4938
+ runToCompletion() {
4939
+ throw new UnsupportedRunOperationError(
4940
+ "Agent.runToCompletion() is not supported on cloud agents. Cloud runtime manages continuation server-side. Use a local agent.",
4941
+ "runToCompletion"
4942
+ );
4943
+ }
4867
4944
  /**
4868
4945
  * Personality presets require consistent server-side enforcement that
4869
4946
  * the cloud runtime (pre-release) does not yet provide. Reject explicitly
@@ -6424,6 +6501,8 @@ function parseSubagentMarkdown(raw, filename) {
6424
6501
  if (fields.model !== void 0) {
6425
6502
  definition.model = fields.model === "inherit" ? "inherit" : { id: fields.model };
6426
6503
  }
6504
+ const tools = fields.tools?.split(/[\s,]+/).map((t) => t.trim()).filter((t) => t.length > 0);
6505
+ if (tools !== void 0 && tools.length > 0) definition.tools = tools;
6427
6506
  return { name, definition };
6428
6507
  }
6429
6508
  function splitFrontmatter2(raw, filename) {
@@ -6587,21 +6666,24 @@ ${lines.join("\n")}
6587
6666
  }
6588
6667
  };
6589
6668
 
6669
+ // src/internal/runtime/skills/skills-block.ts
6670
+ function buildSkillsBlock(skills) {
6671
+ if (skills.length === 0) return void 0;
6672
+ const lines = skills.map(
6673
+ (skill) => ` - ${escapeBlockBody(skill.name)}: ${escapeBlockBody(skill.description)}`
6674
+ );
6675
+ return `<skills>
6676
+ ${lines.join("\n")}
6677
+ </skills>`;
6678
+ }
6679
+
6590
6680
  // src/internal/runtime/system-prompt/sources/skills-provider.ts
6591
6681
  var SkillsPromptProvider = class {
6592
6682
  id = "skills";
6593
6683
  priority = 20;
6594
6684
  contribute(ctx) {
6595
6685
  if (ctx.skillsAutoInject === false) return Promise.resolve(void 0);
6596
- if (ctx.skills.length === 0) return Promise.resolve(void 0);
6597
- const lines = ctx.skills.map((skill) => {
6598
- const name = escapeBlockBody(skill.name);
6599
- const description = escapeBlockBody(skill.description);
6600
- return ` - ${name}: ${description}`;
6601
- });
6602
- return Promise.resolve(`<skills>
6603
- ${lines.join("\n")}
6604
- </skills>`);
6686
+ return Promise.resolve(buildSkillsBlock(ctx.skills));
6605
6687
  }
6606
6688
  };
6607
6689
 
@@ -7709,7 +7791,7 @@ async function loadPluginManifestFromMarkdown(pluginsRoot, folderName) {
7709
7791
  return metadata;
7710
7792
  }
7711
7793
 
7712
- // src/internal/runtime/skills/skills-manager.ts
7794
+ // src/internal/runtime/skills/discover-skills.ts
7713
7795
  init_errors();
7714
7796
 
7715
7797
  // src/internal/runtime/skills/skill-frontmatter.ts
@@ -7781,6 +7863,61 @@ function hasContent(value) {
7781
7863
  return value !== void 0 && value.trim().length > 0;
7782
7864
  }
7783
7865
 
7866
+ // src/internal/runtime/skills/discover-skills.ts
7867
+ async function discoverSkills(dir, options) {
7868
+ let entries;
7869
+ try {
7870
+ entries = await readWorkspaceDir(dir, "skills_read_error", "skills directory");
7871
+ } catch {
7872
+ return [];
7873
+ }
7874
+ const skills = [];
7875
+ for (const entry of entries) {
7876
+ if (!entry.isDirectory()) continue;
7877
+ let skillDir;
7878
+ try {
7879
+ skillDir = safePathJoin(dir, entry.name);
7880
+ assertNoSymlinkEscape(skillDir, dir);
7881
+ } catch {
7882
+ continue;
7883
+ }
7884
+ const skillPath = join(skillDir, "SKILL.md");
7885
+ let raw;
7886
+ try {
7887
+ raw = await readFile(skillPath, "utf8");
7888
+ } catch {
7889
+ continue;
7890
+ }
7891
+ const skill = tryParseSkill(raw, entry.name, skillPath, options);
7892
+ if (skill !== void 0) skills.push(skill);
7893
+ }
7894
+ return skills;
7895
+ }
7896
+ function tryParseSkill(raw, fallbackName, source, options) {
7897
+ try {
7898
+ const frontmatter = parseSkillFrontmatter(raw, fallbackName);
7899
+ const skill = {
7900
+ name: frontmatter.name,
7901
+ description: frontmatter.description,
7902
+ source
7903
+ };
7904
+ if (frontmatter.category !== void 0) skill.category = frontmatter.category;
7905
+ if (frontmatter.dependencies !== void 0) skill.dependencies = frontmatter.dependencies;
7906
+ return skill;
7907
+ } catch (cause) {
7908
+ if (cause instanceof ConfigurationError) {
7909
+ options?.onInvalidSkill?.({
7910
+ name: fallbackName,
7911
+ source,
7912
+ code: cause.code ?? "unknown",
7913
+ message: cause.message
7914
+ });
7915
+ return void 0;
7916
+ }
7917
+ throw cause;
7918
+ }
7919
+ }
7920
+
7784
7921
  // src/internal/runtime/skills/skills-manager.ts
7785
7922
  var SkillsManager = class {
7786
7923
  constructor(cwd, _enabled, settingSourcesIncludeProject) {
@@ -7798,56 +7935,20 @@ var SkillsManager = class {
7798
7935
  await this.refresh();
7799
7936
  }
7800
7937
  async refresh() {
7801
- this.skills = [];
7802
7938
  const skillsRoot = join(this.cwd, ".theokit", "skills");
7803
- const entries = await readWorkspaceDir(skillsRoot, "skills_read_error", "skills directory");
7804
- for (const entry of entries) {
7805
- if (!entry.isDirectory()) continue;
7806
- let skillDir;
7807
- try {
7808
- skillDir = safePathJoin(skillsRoot, entry.name);
7809
- assertNoSymlinkEscape(skillDir, skillsRoot);
7810
- } catch {
7811
- continue;
7812
- }
7813
- const skillPath = join(skillDir, "SKILL.md");
7814
- let raw;
7815
- try {
7816
- raw = await readFile(skillPath, "utf8");
7817
- } catch {
7818
- continue;
7939
+ this.skills = await discoverSkills(skillsRoot, {
7940
+ onInvalidSkill: (info) => {
7941
+ process.stderr.write(
7942
+ `[theokit-sdk] skill ${info.name} skipped (${info.code}): ${info.message}
7943
+ `
7944
+ );
7819
7945
  }
7820
- const metadata = tryParseSkill(raw, entry.name, skillPath);
7821
- if (metadata !== void 0) this.skills.push(metadata);
7822
- }
7946
+ });
7823
7947
  }
7824
7948
  list() {
7825
7949
  return Promise.resolve(this.skills);
7826
7950
  }
7827
7951
  };
7828
- function tryParseSkill(raw, fallbackName, source) {
7829
- try {
7830
- const frontmatter = parseSkillFrontmatter(raw, fallbackName);
7831
- const metadata = {
7832
- name: frontmatter.name,
7833
- description: frontmatter.description,
7834
- source
7835
- };
7836
- if (frontmatter.category !== void 0) metadata.category = frontmatter.category;
7837
- if (frontmatter.dependencies !== void 0) metadata.dependencies = frontmatter.dependencies;
7838
- return metadata;
7839
- } catch (cause) {
7840
- if (cause instanceof ConfigurationError) {
7841
- const code = cause.code ?? "unknown";
7842
- process.stderr.write(
7843
- `[theokit-sdk] skill ${fallbackName} skipped (${code}): ${cause.message}
7844
- `
7845
- );
7846
- return void 0;
7847
- }
7848
- throw cause;
7849
- }
7850
- }
7851
7952
 
7852
7953
  // src/internal/runtime/local-agent/local-agent-bootstrap.ts
7853
7954
  function registerLocalAgent(args) {
@@ -8260,6 +8361,7 @@ async function initLoopContext(inputs) {
8260
8361
  finalStatus: "finished",
8261
8362
  usage: new UsageAccumulator(),
8262
8363
  nudgeAttempts: 0,
8364
+ stopFeedbackAttempts: 0,
8263
8365
  ...memoryProviderHandle !== void 0 ? { memoryProviderHandle } : {},
8264
8366
  ...memorySystemPromptAdditions !== void 0 ? { memorySystemPromptAdditions } : {}
8265
8367
  };
@@ -8404,8 +8506,9 @@ function registerLoopError(ctx, cause) {
8404
8506
  if (ctx.error !== void 0) return;
8405
8507
  const rawMessage = cause?.message;
8406
8508
  const message = typeof rawMessage === "string" ? rawMessage : cause instanceof Error ? cause.message : String(cause);
8509
+ const metaCode = cause?.metadata?.code;
8407
8510
  const rawCode = cause?.code;
8408
- const code = typeof rawCode === "string" ? rawCode : void 0;
8511
+ const code = typeof metaCode === "string" ? metaCode : typeof rawCode === "string" ? rawCode : void 0;
8409
8512
  ctx.error = code !== void 0 ? { message, code, cause } : { message, cause };
8410
8513
  }
8411
8514
  async function runCollectorLoop(generator, inputs, ctx) {
@@ -9201,6 +9304,7 @@ function computeUsageCost(inputs, usage) {
9201
9304
 
9202
9305
  // src/internal/agent-loop/loop.ts
9203
9306
  var MAX_NUDGE_ATTEMPTS = 2;
9307
+ var MAX_STOP_FEEDBACK_ATTEMPTS = 2;
9204
9308
  async function runAgentLoop(inputs) {
9205
9309
  const sendSpan = inputs.telemetry?.startSpan("agent.send", {
9206
9310
  agentId: inputs.agentId,
@@ -9358,6 +9462,28 @@ function shouldNudgeAndContinue(ctx, llmOutput) {
9358
9462
  });
9359
9463
  return true;
9360
9464
  }
9465
+ async function reflectAfterStop(inputs, ctx) {
9466
+ const result = await inputs.hooks.run({
9467
+ event: "stop",
9468
+ agentId: inputs.agentId,
9469
+ runId: inputs.runId
9470
+ });
9471
+ if (result.blocked) return false;
9472
+ if (ctx.stopFeedbackAttempts >= MAX_STOP_FEEDBACK_ATTEMPTS) return false;
9473
+ const feedback = result.decisions.find(
9474
+ (d) => d.decision === "feedback" && (d.feedback ?? "").length > 0
9475
+ )?.feedback;
9476
+ if (feedback === void 0) return false;
9477
+ ctx.stopFeedbackAttempts += 1;
9478
+ ctx.messages.push({ role: "user", content: [{ type: "text", text: feedback }] });
9479
+ return true;
9480
+ }
9481
+ async function finishOrReflect(inputs, ctx, llmOutput) {
9482
+ if (shouldNudgeAndContinue(ctx, llmOutput)) return "continue";
9483
+ if (await reflectAfterStop(inputs, ctx)) return "continue";
9484
+ ctx.finalStatus = "finished";
9485
+ return "done";
9486
+ }
9361
9487
  async function runIteration(inputs, ctx) {
9362
9488
  const llmOutput = await streamLlmTurn(inputs, ctx);
9363
9489
  accumulateUsage(ctx.usage, llmOutput);
@@ -9391,9 +9517,7 @@ async function continueOrTerminate(inputs, ctx, llmOutput) {
9391
9517
  await emitAssistantTextStep(inputs, ctx, llmOutput.text);
9392
9518
  }
9393
9519
  if (llmOutput.stopReason !== "tool_use" || llmOutput.toolCalls.length === 0) {
9394
- if (shouldNudgeAndContinue(ctx, llmOutput)) return "continue";
9395
- ctx.finalStatus = "finished";
9396
- return "done";
9520
+ return finishOrReflect(inputs, ctx, llmOutput);
9397
9521
  }
9398
9522
  ctx.messages.push(buildAssistantTurn(llmOutput.text, llmOutput.toolCalls));
9399
9523
  const toolResults = await dispatchTools(inputs, ctx.tools, llmOutput.toolCalls, ctx.events);
@@ -14344,6 +14468,13 @@ function localAgentRunUntil(agent, goal, options) {
14344
14468
  }
14345
14469
  return wrap();
14346
14470
  }
14471
+ function localAgentRunToCompletion(agent, message, options) {
14472
+ async function run() {
14473
+ const { runToCompletionImpl: runToCompletionImpl2 } = await Promise.resolve().then(() => (init_run_to_completion(), run_to_completion_exports));
14474
+ return runToCompletionImpl2({ send: (m, o) => agent.send(m, o) }, message, options);
14475
+ }
14476
+ return run();
14477
+ }
14347
14478
  async function localAgentFork(parent, options) {
14348
14479
  const { forkAgentImpl: forkAgentImpl2 } = await Promise.resolve().then(() => (init_fork_agent(), fork_agent_exports));
14349
14480
  const { getAgentFacade: getAgentFacade2 } = await Promise.resolve().then(() => (init_agent_factory_registry(), agent_factory_registry_exports));
@@ -14884,6 +15015,10 @@ var LocalAgent = class {
14884
15015
  fork(options) {
14885
15016
  return localAgentFork({ agentId: this.agentId, options: this.options, personalitySlugSnapshot: this.personalityStore.active(this.agentId) }, options);
14886
15017
  }
15018
+ // biome-ignore format: G8 budget — see runUntil comment above.
15019
+ runToCompletion(message, options) {
15020
+ return localAgentRunToCompletion(this, message, options);
15021
+ }
14887
15022
  };
14888
15023
  function resolveCwd(cwd) {
14889
15024
  return (Array.isArray(cwd) ? cwd[0] : cwd) ?? process.cwd();