@theokit/sdk 4.19.1 → 4.19.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -6176,7 +6176,9 @@ function isCompactSummary(content) {
6176
6176
  function plainText(content) {
6177
6177
  if (typeof content === "string") return content;
6178
6178
  if (!Array.isArray(content)) return void 0;
6179
- const texts = content.filter((p) => p !== null && typeof p === "object" && p.type === "text" && typeof p.text === "string").map((p) => p.text);
6179
+ const texts = content.filter(
6180
+ (p) => p !== null && typeof p === "object" && p.type === "text" && typeof p.text === "string"
6181
+ ).map((p) => p.text);
6180
6182
  return texts.length > 0 ? texts.join("\n") : void 0;
6181
6183
  }
6182
6184
  async function compactSessionTranscript(opts) {
@@ -6297,8 +6299,10 @@ async function autoCompactIfNeeded(opts) {
6297
6299
  return true;
6298
6300
  } catch (cause) {
6299
6301
  const msg = cause instanceof Error ? cause.message : String(cause);
6300
- process.stderr.write(`[theokit-sdk] auto-compaction failed (left transcript untouched): ${msg}
6301
- `);
6302
+ process.stderr.write(
6303
+ `[theokit-sdk] auto-compaction failed (left transcript untouched): ${msg}
6304
+ `
6305
+ );
6302
6306
  return false;
6303
6307
  }
6304
6308
  }
@@ -6308,11 +6312,11 @@ var init_compact_session = __esm({
6308
6312
  init_compaction();
6309
6313
  init_router();
6310
6314
  init_real_local_run_provider();
6315
+ init_session_transcript();
6311
6316
  init_providers();
6312
6317
  init_compression_model_registry();
6313
6318
  init_compression_summarizer();
6314
6319
  init_agent_session();
6315
- init_session_transcript();
6316
6320
  COMPACT_SUMMARY_MARKER = "[[theokit:compact-summary]]";
6317
6321
  COMPACT_USER_MESSAGE_MAX_TOKENS = 2e4;
6318
6322
  autoCompactAttempts = (() => {
@@ -6534,7 +6538,9 @@ async function loadDriver(filePath) {
6534
6538
  }
6535
6539
  try {
6536
6540
  const mod = await (driverLoaderOverrides?.nodeSqlite?.() ?? Promise.resolve(
6537
- process.getBuiltinModule?.("node:sqlite") ?? (() => {
6541
+ process.getBuiltinModule?.(
6542
+ "node:sqlite"
6543
+ ) ?? (() => {
6538
6544
  throw new Error("node:sqlite built-in unavailable (Node < 22.3)");
6539
6545
  })()
6540
6546
  ));
@@ -7501,10 +7507,136 @@ var init_context = __esm({
7501
7507
  }
7502
7508
  });
7503
7509
 
7510
+ // src/internal/judge/parse-verdict.ts
7511
+ function parseVerdict(text) {
7512
+ const trimmed = text.trim();
7513
+ if (trimmed.startsWith(DONE_PREFIX)) {
7514
+ return {
7515
+ verdict: "done",
7516
+ reason: trimmed.slice(DONE_PREFIX.length).trim(),
7517
+ parseFailed: false
7518
+ };
7519
+ }
7520
+ if (trimmed.startsWith(CONTINUE_PREFIX)) {
7521
+ return {
7522
+ verdict: "continue",
7523
+ reason: trimmed.slice(CONTINUE_PREFIX.length).trim(),
7524
+ parseFailed: false
7525
+ };
7526
+ }
7527
+ if (trimmed.startsWith(SKIPPED_PREFIX)) {
7528
+ return {
7529
+ verdict: "skipped",
7530
+ reason: trimmed.slice(SKIPPED_PREFIX.length).trim(),
7531
+ parseFailed: false
7532
+ };
7533
+ }
7534
+ return {
7535
+ verdict: "continue",
7536
+ reason: `judge response malformed: "${trimmed.slice(0, 100)}"`,
7537
+ parseFailed: true
7538
+ };
7539
+ }
7540
+ var DONE_PREFIX, CONTINUE_PREFIX, SKIPPED_PREFIX;
7541
+ var init_parse_verdict = __esm({
7542
+ "src/internal/judge/parse-verdict.ts"() {
7543
+ DONE_PREFIX = "DONE:";
7544
+ CONTINUE_PREFIX = "CONTINUE:";
7545
+ SKIPPED_PREFIX = "SKIPPED:";
7546
+ }
7547
+ });
7548
+
7549
+ // src/internal/judge/judge-call.ts
7550
+ var judge_call_exports = {};
7551
+ __export(judge_call_exports, {
7552
+ composeJudgePrompt: () => composeJudgePrompt,
7553
+ judgeCallImpl: () => judgeCallImpl
7554
+ });
7555
+ async function judgeCallImpl(ctx, options, deps) {
7556
+ const prompt = composeJudgePrompt(ctx);
7557
+ const apiKey = options?.apiKey ?? process.env.OPENROUTER_API_KEY;
7558
+ if (apiKey === void 0) {
7559
+ return {
7560
+ verdict: "continue",
7561
+ reason: "judge unavailable: OPENROUTER_API_KEY missing and no override passed via options.apiKey",
7562
+ parseFailed: true
7563
+ };
7564
+ }
7565
+ const judgeModel = options?.judgeModel ?? "openai/gpt-4o-mini";
7566
+ let auxAgent;
7567
+ try {
7568
+ auxAgent = await deps.create({
7569
+ apiKey,
7570
+ model: { id: judgeModel },
7571
+ tools: [],
7572
+ local: {},
7573
+ metadata: { forkOrigin: "judge" }
7574
+ });
7575
+ const run = await auxAgent.send(prompt);
7576
+ const result = await run.wait();
7577
+ return parseVerdict(result.result ?? "");
7578
+ } catch (err) {
7579
+ return {
7580
+ verdict: "continue",
7581
+ reason: `judge call failed: ${err instanceof Error ? err.message : String(err)}`,
7582
+ parseFailed: true
7583
+ };
7584
+ } finally {
7585
+ if (auxAgent !== void 0) {
7586
+ try {
7587
+ await auxAgent.dispose();
7588
+ } catch {
7589
+ }
7590
+ }
7591
+ }
7592
+ }
7593
+ function composeJudgePrompt(ctx) {
7594
+ const subgoals = ctx.subgoals !== void 0 && ctx.subgoals.length > 0 ? ctx.subgoals.join(", ") : "(none)";
7595
+ return `You are a goal judge. Determine if this goal is satisfied.
7596
+
7597
+ Goal: ${ctx.goal}
7598
+ Subgoals: ${subgoals}
7599
+ Last agent response: ${ctx.lastResponse}
7600
+
7601
+ Respond with EXACTLY one of:
7602
+ - DONE: <reason>
7603
+ - CONTINUE: <what's left>
7604
+ - SKIPPED: <why not applicable>
7605
+
7606
+ Be strict. If unclear, prefer CONTINUE.`;
7607
+ }
7608
+ var init_judge_call = __esm({
7609
+ "src/internal/judge/judge-call.ts"() {
7610
+ init_parse_verdict();
7611
+ }
7612
+ });
7613
+
7614
+ // src/goal-loop.ts
7615
+ function runGoalLoop(agent, goal, options, depsOverride) {
7616
+ async function* wrap() {
7617
+ const { runUntilImpl: runUntilImpl2 } = await Promise.resolve().then(() => (init_run_until(), run_until_exports));
7618
+ const deps = depsOverride ?? await (async () => {
7619
+ const { judgeCallImpl: judgeCallImpl2 } = await Promise.resolve().then(() => (init_judge_call(), judge_call_exports));
7620
+ const { getAgentFacade: getAgentFacade2 } = await Promise.resolve().then(() => (init_agent_factory_registry(), agent_factory_registry_exports));
7621
+ const create = getAgentFacade2().create;
7622
+ return {
7623
+ judge: (ctx, opts) => judgeCallImpl2(ctx, opts, { create })
7624
+ };
7625
+ })();
7626
+ return yield* runUntilImpl2(agent, goal, options, deps);
7627
+ }
7628
+ return wrap();
7629
+ }
7630
+ var GOAL_CONTINUATION_MARKER;
7631
+ var init_goal_loop = __esm({
7632
+ "src/goal-loop.ts"() {
7633
+ GOAL_CONTINUATION_MARKER = "[[theokit:goal-continuation]]";
7634
+ }
7635
+ });
7636
+
7504
7637
  // src/internal/runtime/lifecycle/run-until.ts
7505
7638
  var run_until_exports = {};
7506
7639
  __export(run_until_exports, {
7507
- GOAL_CONTINUATION_MARKER: () => GOAL_CONTINUATION_MARKER,
7508
7640
  composeContinuation: () => composeContinuation,
7509
7641
  runUntilImpl: () => runUntilImpl
7510
7642
  });
@@ -7669,114 +7801,9 @@ ${goal}
7669
7801
  ${lastResponse.slice(-1e3)}`
7670
7802
  ].join("\n");
7671
7803
  }
7672
- var GOAL_CONTINUATION_MARKER;
7673
7804
  var init_run_until = __esm({
7674
7805
  "src/internal/runtime/lifecycle/run-until.ts"() {
7675
- GOAL_CONTINUATION_MARKER = "[[theokit:goal-continuation]]";
7676
- }
7677
- });
7678
-
7679
- // src/internal/judge/parse-verdict.ts
7680
- function parseVerdict(text) {
7681
- const trimmed = text.trim();
7682
- if (trimmed.startsWith(DONE_PREFIX)) {
7683
- return {
7684
- verdict: "done",
7685
- reason: trimmed.slice(DONE_PREFIX.length).trim(),
7686
- parseFailed: false
7687
- };
7688
- }
7689
- if (trimmed.startsWith(CONTINUE_PREFIX)) {
7690
- return {
7691
- verdict: "continue",
7692
- reason: trimmed.slice(CONTINUE_PREFIX.length).trim(),
7693
- parseFailed: false
7694
- };
7695
- }
7696
- if (trimmed.startsWith(SKIPPED_PREFIX)) {
7697
- return {
7698
- verdict: "skipped",
7699
- reason: trimmed.slice(SKIPPED_PREFIX.length).trim(),
7700
- parseFailed: false
7701
- };
7702
- }
7703
- return {
7704
- verdict: "continue",
7705
- reason: `judge response malformed: "${trimmed.slice(0, 100)}"`,
7706
- parseFailed: true
7707
- };
7708
- }
7709
- var DONE_PREFIX, CONTINUE_PREFIX, SKIPPED_PREFIX;
7710
- var init_parse_verdict = __esm({
7711
- "src/internal/judge/parse-verdict.ts"() {
7712
- DONE_PREFIX = "DONE:";
7713
- CONTINUE_PREFIX = "CONTINUE:";
7714
- SKIPPED_PREFIX = "SKIPPED:";
7715
- }
7716
- });
7717
-
7718
- // src/internal/judge/judge-call.ts
7719
- var judge_call_exports = {};
7720
- __export(judge_call_exports, {
7721
- composeJudgePrompt: () => composeJudgePrompt,
7722
- judgeCallImpl: () => judgeCallImpl
7723
- });
7724
- async function judgeCallImpl(ctx, options, deps) {
7725
- const prompt = composeJudgePrompt(ctx);
7726
- const apiKey = options?.apiKey ?? process.env.OPENROUTER_API_KEY;
7727
- if (apiKey === void 0) {
7728
- return {
7729
- verdict: "continue",
7730
- reason: "judge unavailable: OPENROUTER_API_KEY missing and no override passed via options.apiKey",
7731
- parseFailed: true
7732
- };
7733
- }
7734
- const judgeModel = options?.judgeModel ?? "openai/gpt-4o-mini";
7735
- let auxAgent;
7736
- try {
7737
- auxAgent = await deps.create({
7738
- apiKey,
7739
- model: { id: judgeModel },
7740
- tools: [],
7741
- local: {},
7742
- metadata: { forkOrigin: "judge" }
7743
- });
7744
- const run = await auxAgent.send(prompt);
7745
- const result = await run.wait();
7746
- return parseVerdict(result.result ?? "");
7747
- } catch (err) {
7748
- return {
7749
- verdict: "continue",
7750
- reason: `judge call failed: ${err instanceof Error ? err.message : String(err)}`,
7751
- parseFailed: true
7752
- };
7753
- } finally {
7754
- if (auxAgent !== void 0) {
7755
- try {
7756
- await auxAgent.dispose();
7757
- } catch {
7758
- }
7759
- }
7760
- }
7761
- }
7762
- function composeJudgePrompt(ctx) {
7763
- const subgoals = ctx.subgoals !== void 0 && ctx.subgoals.length > 0 ? ctx.subgoals.join(", ") : "(none)";
7764
- return `You are a goal judge. Determine if this goal is satisfied.
7765
-
7766
- Goal: ${ctx.goal}
7767
- Subgoals: ${subgoals}
7768
- Last agent response: ${ctx.lastResponse}
7769
-
7770
- Respond with EXACTLY one of:
7771
- - DONE: <reason>
7772
- - CONTINUE: <what's left>
7773
- - SKIPPED: <why not applicable>
7774
-
7775
- Be strict. If unclear, prefer CONTINUE.`;
7776
- }
7777
- var init_judge_call = __esm({
7778
- "src/internal/judge/judge-call.ts"() {
7779
- init_parse_verdict();
7806
+ init_goal_loop();
7780
7807
  }
7781
7808
  });
7782
7809
 
@@ -21316,8 +21343,8 @@ async function getRegisteredAgentOrThrow(agentId) {
21316
21343
  // src/agent.ts
21317
21344
  init_errors();
21318
21345
  init_discovery();
21319
- init_agent_session();
21320
21346
  init_agent_factory_registry();
21347
+ init_agent_session();
21321
21348
  var streamObjectImport;
21322
21349
  var Agent = class _Agent {
21323
21350
  constructor() {
@@ -21609,7 +21636,9 @@ var Agent = class _Agent {
21609
21636
  reg = getRegisteredAgent(agentId);
21610
21637
  }
21611
21638
  if (reg === void 0 || reg.runtime !== "local") {
21612
- throw new UnknownAgentError(`No local agent "${agentId}" registered \u2014 compact targets local sessions.`);
21639
+ throw new UnknownAgentError(
21640
+ `No local agent "${agentId}" registered \u2014 compact targets local sessions.`
21641
+ );
21613
21642
  }
21614
21643
  const cwd = reg.cwd ?? process.cwd();
21615
21644
  const optModel = reg.options.model;
@@ -21619,16 +21648,20 @@ var Agent = class _Agent {
21619
21648
  const { defaultBaseDir: defaultBaseDir2, expandTilde: expandTilde2 } = await Promise.resolve().then(() => (init_session_transcript(), session_transcript_exports));
21620
21649
  const baseDir = reg.options.local?.baseDir !== void 0 ? expandTilde2(reg.options.local.baseDir) : defaultBaseDir2();
21621
21650
  const store = new FsSessionStore2({ baseDir, cwd });
21622
- return enqueueSessionWrite(cwd, agentId, () => compactSessionTranscript2({
21623
- store,
21624
- loc: { cwd, agentId, model },
21625
- sessionId: agentId,
21626
- trigger: options.trigger ?? "manual",
21627
- summarize: options.summarize ?? buildDefaultSummarizer2({
21628
- agentModel: model,
21629
- ...reg.options.apiKey !== void 0 ? { apiKey: reg.options.apiKey } : {}
21651
+ return enqueueSessionWrite(
21652
+ cwd,
21653
+ agentId,
21654
+ () => compactSessionTranscript2({
21655
+ store,
21656
+ loc: { cwd, agentId, model },
21657
+ sessionId: agentId,
21658
+ trigger: options.trigger ?? "manual",
21659
+ summarize: options.summarize ?? buildDefaultSummarizer2({
21660
+ agentModel: model,
21661
+ ...reg.options.apiKey !== void 0 ? { apiKey: reg.options.apiKey } : {}
21662
+ })
21630
21663
  })
21631
- }));
21664
+ );
21632
21665
  }
21633
21666
  /**
21634
21667
  * M51 — inject a SYNTHETIC user+assistant pair into a LOCAL session's persisted transcript WITHOUT
@@ -21645,7 +21678,9 @@ var Agent = class _Agent {
21645
21678
  reg = getRegisteredAgent(agentId);
21646
21679
  }
21647
21680
  if (reg === void 0 || reg.runtime !== "local") {
21648
- throw new UnknownAgentError(`No local agent "${agentId}" registered \u2014 injectSessionTurn targets local sessions.`);
21681
+ throw new UnknownAgentError(
21682
+ `No local agent "${agentId}" registered \u2014 injectSessionTurn targets local sessions.`
21683
+ );
21649
21684
  }
21650
21685
  const cwd = reg.cwd ?? process.cwd();
21651
21686
  const optModel = reg.options.model;
@@ -22810,24 +22845,7 @@ var EventBus = class {
22810
22845
 
22811
22846
  // src/index.ts
22812
22847
  init_generate_object();
22813
-
22814
- // src/goal-loop.ts
22815
- init_run_until();
22816
- function runGoalLoop(agent, goal, options, depsOverride) {
22817
- async function* wrap() {
22818
- const { runUntilImpl: runUntilImpl2 } = await Promise.resolve().then(() => (init_run_until(), run_until_exports));
22819
- const deps = depsOverride ?? await (async () => {
22820
- const { judgeCallImpl: judgeCallImpl2 } = await Promise.resolve().then(() => (init_judge_call(), judge_call_exports));
22821
- const { getAgentFacade: getAgentFacade2 } = await Promise.resolve().then(() => (init_agent_factory_registry(), agent_factory_registry_exports));
22822
- const create = getAgentFacade2().create;
22823
- return {
22824
- judge: (ctx, opts) => judgeCallImpl2(ctx, opts, { create })
22825
- };
22826
- })();
22827
- return yield* runUntilImpl2(agent, goal, options, deps);
22828
- }
22829
- return wrap();
22830
- }
22848
+ init_goal_loop();
22831
22849
 
22832
22850
  // src/internal/budget/tracker/budget-tracker-counter.ts
22833
22851
  function createCounterBudgetTracker(options = {}) {