@theokit/sdk 4.17.1 → 4.18.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/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,13 +7526,13 @@ 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 { status: "paused", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
7534
7536
  }
7535
7537
  turn += 1;
7536
7538
  yield { type: "turn_start", turn, goal };
@@ -7538,6 +7540,8 @@ async function* runUntilImpl(agent, goal, options, deps) {
7538
7540
  const run = await agent.send(continuationPrompt);
7539
7541
  const result = await run.wait();
7540
7542
  lastResponse = result.result ?? "";
7543
+ const turnTokens = result.usage?.totalTokens;
7544
+ if (typeof turnTokens === "number") tokensUsed += turnTokens;
7541
7545
  yield { type: "agent_response", turn, content: lastResponse };
7542
7546
  const judgeOpts = {};
7543
7547
  if (options?.judgeModel !== void 0) judgeOpts.judgeModel = options.judgeModel;
@@ -7563,6 +7567,7 @@ async function* runUntilImpl(agent, goal, options, deps) {
7563
7567
  return {
7564
7568
  status: "failed",
7565
7569
  turnsUsed: turn,
7570
+ tokensUsed,
7566
7571
  finalResponse: lastResponse || void 0
7567
7572
  };
7568
7573
  }
@@ -7571,7 +7576,7 @@ async function* runUntilImpl(agent, goal, options, deps) {
7571
7576
  }
7572
7577
  if (judgment.verdict === "done") {
7573
7578
  yield { type: "status_change", status: "completed", reason: judgment.reason };
7574
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7579
+ return { status: "completed", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
7575
7580
  }
7576
7581
  if (judgment.verdict === "skipped") {
7577
7582
  yield {
@@ -7579,7 +7584,15 @@ async function* runUntilImpl(agent, goal, options, deps) {
7579
7584
  status: "completed",
7580
7585
  reason: `skipped: ${judgment.reason}`
7581
7586
  };
7582
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7587
+ return { status: "completed", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
7588
+ }
7589
+ if (tokenBudget !== void 0 && tokensUsed >= tokenBudget) {
7590
+ yield {
7591
+ type: "status_change",
7592
+ status: "budget_limited",
7593
+ reason: `token budget (${tokenBudget}) reached: ${tokensUsed} used`
7594
+ };
7595
+ return { status: "budget_limited", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
7583
7596
  }
7584
7597
  yield { type: "continuation", turn, prompt: continuationPrompt };
7585
7598
  }
@@ -7588,13 +7601,32 @@ async function* runUntilImpl(agent, goal, options, deps) {
7588
7601
  status: "failed",
7589
7602
  reason: `max turns (${maxTurns}) exhausted`
7590
7603
  };
7591
- return { status: "failed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7604
+ return { status: "failed", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
7592
7605
  }
7593
7606
  function composeContinuation(goal, lastResponse) {
7594
- return `Continue working toward the goal: ${goal}
7595
-
7596
- Your last response was:
7597
- ${lastResponse.slice(0, 1e3)}`;
7607
+ return [
7608
+ "Continue working toward the active goal.",
7609
+ "",
7610
+ `<objective>
7611
+ ${goal}
7612
+ </objective>`,
7613
+ "",
7614
+ "Continuation behavior:",
7615
+ "- This goal persists across turns. Keep the full objective intact; if it cannot be finished now,",
7616
+ " make concrete progress toward the real requested end state and do not redefine success around a",
7617
+ " smaller or easier task.",
7618
+ "",
7619
+ "Work from evidence:",
7620
+ "- Use the current worktree and external state as authoritative. Inspect the current state before",
7621
+ " relying on it. Improve, replace, or remove existing work as needed to satisfy the actual objective.",
7622
+ "",
7623
+ "Completion audit:",
7624
+ "- Before deciding the goal is achieved, treat completion as unproven and verify it against the actual",
7625
+ " current state, requirement by requirement. Treat uncertain or indirect evidence as not achieved.",
7626
+ "",
7627
+ `Your last response was:
7628
+ ${lastResponse.slice(0, 1e3)}`
7629
+ ].join("\n");
7598
7630
  }
7599
7631
  var init_run_until = __esm({
7600
7632
  "src/internal/runtime/lifecycle/run-until.ts"() {
@@ -20208,7 +20240,7 @@ function localAgentRunUntil(agent, goal, options) {
20208
20240
  status: "paused",
20209
20241
  reason: "runUntil() requires an explicit goal (durable objectives removed in v4.0)"
20210
20242
  };
20211
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
20243
+ return { status: "paused", turnsUsed: 0, tokensUsed: 0, finalResponse: void 0 };
20212
20244
  }
20213
20245
  return yield* runUntilImpl2(agent, goal, options, await buildDeps());
20214
20246
  }