@theokit/sdk 4.17.1 → 4.18.1

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.cjs CHANGED
@@ -7513,10 +7513,12 @@ __export(run_until_exports, {
7513
7513
  async function* runUntilImpl(agent, goal, options, deps) {
7514
7514
  const maxTurns = options?.maxTurns ?? 20;
7515
7515
  const maxFails = options?.maxConsecutiveJudgeFailures ?? 3;
7516
+ const tokenBudget = options?.tokenBudget;
7516
7517
  const signal = options?.signal;
7517
7518
  const isAborted = () => signal !== void 0 && signal.aborted;
7518
7519
  let turn = 0;
7519
7520
  let consecutiveFailures = 0;
7521
+ let tokensUsed = 0;
7520
7522
  let lastResponse = "";
7521
7523
  if (isAborted()) {
7522
7524
  yield {
@@ -7524,20 +7526,44 @@ async function* runUntilImpl(agent, goal, options, deps) {
7524
7526
  status: "paused",
7525
7527
  reason: "aborted via AbortSignal before first turn"
7526
7528
  };
7527
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
7529
+ return { status: "paused", turnsUsed: 0, tokensUsed, finalResponse: void 0 };
7528
7530
  }
7529
7531
  yield { type: "status_change", status: "active", reason: "Goal started" };
7530
7532
  while (turn < maxTurns) {
7531
7533
  if (isAborted()) {
7532
7534
  yield { type: "status_change", status: "paused", reason: "aborted via AbortSignal" };
7533
- return { status: "paused", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7535
+ return {
7536
+ status: "paused",
7537
+ turnsUsed: turn,
7538
+ tokensUsed,
7539
+ finalResponse: lastResponse || void 0
7540
+ };
7534
7541
  }
7535
7542
  turn += 1;
7536
7543
  yield { type: "turn_start", turn, goal };
7537
7544
  const continuationPrompt = turn === 1 ? goal : composeContinuation(goal, lastResponse);
7538
7545
  const run = await agent.send(continuationPrompt);
7546
+ const cancelOnAbort = () => {
7547
+ void run.cancel?.();
7548
+ };
7549
+ if (signal !== void 0) {
7550
+ if (signal.aborted) cancelOnAbort();
7551
+ else signal.addEventListener("abort", cancelOnAbort, { once: true });
7552
+ }
7539
7553
  const result = await run.wait();
7554
+ if (signal !== void 0) signal.removeEventListener("abort", cancelOnAbort);
7540
7555
  lastResponse = result.result ?? "";
7556
+ const turnTokens = result.usage?.totalTokens;
7557
+ if (typeof turnTokens === "number") tokensUsed += turnTokens;
7558
+ if (isAborted()) {
7559
+ yield { type: "status_change", status: "paused", reason: "aborted via AbortSignal" };
7560
+ return {
7561
+ status: "paused",
7562
+ turnsUsed: turn,
7563
+ tokensUsed,
7564
+ finalResponse: lastResponse || void 0
7565
+ };
7566
+ }
7541
7567
  yield { type: "agent_response", turn, content: lastResponse };
7542
7568
  const judgeOpts = {};
7543
7569
  if (options?.judgeModel !== void 0) judgeOpts.judgeModel = options.judgeModel;
@@ -7563,6 +7589,7 @@ async function* runUntilImpl(agent, goal, options, deps) {
7563
7589
  return {
7564
7590
  status: "failed",
7565
7591
  turnsUsed: turn,
7592
+ tokensUsed,
7566
7593
  finalResponse: lastResponse || void 0
7567
7594
  };
7568
7595
  }
@@ -7571,7 +7598,12 @@ async function* runUntilImpl(agent, goal, options, deps) {
7571
7598
  }
7572
7599
  if (judgment.verdict === "done") {
7573
7600
  yield { type: "status_change", status: "completed", reason: judgment.reason };
7574
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7601
+ return {
7602
+ status: "completed",
7603
+ turnsUsed: turn,
7604
+ tokensUsed,
7605
+ finalResponse: lastResponse || void 0
7606
+ };
7575
7607
  }
7576
7608
  if (judgment.verdict === "skipped") {
7577
7609
  yield {
@@ -7579,7 +7611,25 @@ async function* runUntilImpl(agent, goal, options, deps) {
7579
7611
  status: "completed",
7580
7612
  reason: `skipped: ${judgment.reason}`
7581
7613
  };
7582
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7614
+ return {
7615
+ status: "completed",
7616
+ turnsUsed: turn,
7617
+ tokensUsed,
7618
+ finalResponse: lastResponse || void 0
7619
+ };
7620
+ }
7621
+ if (tokenBudget !== void 0 && tokensUsed >= tokenBudget) {
7622
+ yield {
7623
+ type: "status_change",
7624
+ status: "budget_limited",
7625
+ reason: `token budget (${tokenBudget}) reached: ${tokensUsed} used`
7626
+ };
7627
+ return {
7628
+ status: "budget_limited",
7629
+ turnsUsed: turn,
7630
+ tokensUsed,
7631
+ finalResponse: lastResponse || void 0
7632
+ };
7583
7633
  }
7584
7634
  yield { type: "continuation", turn, prompt: continuationPrompt };
7585
7635
  }
@@ -7588,13 +7638,37 @@ async function* runUntilImpl(agent, goal, options, deps) {
7588
7638
  status: "failed",
7589
7639
  reason: `max turns (${maxTurns}) exhausted`
7590
7640
  };
7591
- return { status: "failed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7641
+ return {
7642
+ status: "failed",
7643
+ turnsUsed: turn,
7644
+ tokensUsed,
7645
+ finalResponse: lastResponse || void 0
7646
+ };
7592
7647
  }
7593
7648
  function composeContinuation(goal, lastResponse) {
7594
- return `Continue working toward the goal: ${goal}
7595
-
7596
- Your last response was:
7597
- ${lastResponse.slice(0, 1e3)}`;
7649
+ return [
7650
+ "Continue working toward the active goal.",
7651
+ "",
7652
+ `<objective>
7653
+ ${goal}
7654
+ </objective>`,
7655
+ "",
7656
+ "Continuation behavior:",
7657
+ "- This goal persists across turns. Keep the full objective intact; if it cannot be finished now,",
7658
+ " make concrete progress toward the real requested end state and do not redefine success around a",
7659
+ " smaller or easier task.",
7660
+ "",
7661
+ "Work from evidence:",
7662
+ "- Use the current worktree and external state as authoritative. Inspect the current state before",
7663
+ " relying on it. Improve, replace, or remove existing work as needed to satisfy the actual objective.",
7664
+ "",
7665
+ "Completion audit:",
7666
+ "- Before deciding the goal is achieved, treat completion as unproven and verify it against the actual",
7667
+ " current state, requirement by requirement. Treat uncertain or indirect evidence as not achieved.",
7668
+ "",
7669
+ `Your last response was:
7670
+ ${lastResponse.slice(0, 1e3)}`
7671
+ ].join("\n");
7598
7672
  }
7599
7673
  var init_run_until = __esm({
7600
7674
  "src/internal/runtime/lifecycle/run-until.ts"() {
@@ -20208,7 +20282,7 @@ function localAgentRunUntil(agent, goal, options) {
20208
20282
  status: "paused",
20209
20283
  reason: "runUntil() requires an explicit goal (durable objectives removed in v4.0)"
20210
20284
  };
20211
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
20285
+ return { status: "paused", turnsUsed: 0, tokensUsed: 0, finalResponse: void 0 };
20212
20286
  }
20213
20287
  return yield* runUntilImpl2(agent, goal, options, await buildDeps());
20214
20288
  }