@theokit/sdk 4.17.0 → 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
@@ -6121,9 +6121,7 @@ async function hydrateSession(agentId, loc) {
6121
6121
  hydratedKeys.add(key2);
6122
6122
  const persisted = await readSessionMessages(loc.store, agentId);
6123
6123
  if (persisted.length === 0) return;
6124
- if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
6125
- sessions.set(agentId, persisted);
6126
- }
6124
+ sessions.set(agentId, persisted);
6127
6125
  }
6128
6126
  async function flushSessionWrites() {
6129
6127
  while (pendingWrites.size > 0) {
@@ -7515,10 +7513,12 @@ __export(run_until_exports, {
7515
7513
  async function* runUntilImpl(agent, goal, options, deps) {
7516
7514
  const maxTurns = options?.maxTurns ?? 20;
7517
7515
  const maxFails = options?.maxConsecutiveJudgeFailures ?? 3;
7516
+ const tokenBudget = options?.tokenBudget;
7518
7517
  const signal = options?.signal;
7519
7518
  const isAborted = () => signal !== void 0 && signal.aborted;
7520
7519
  let turn = 0;
7521
7520
  let consecutiveFailures = 0;
7521
+ let tokensUsed = 0;
7522
7522
  let lastResponse = "";
7523
7523
  if (isAborted()) {
7524
7524
  yield {
@@ -7526,13 +7526,13 @@ async function* runUntilImpl(agent, goal, options, deps) {
7526
7526
  status: "paused",
7527
7527
  reason: "aborted via AbortSignal before first turn"
7528
7528
  };
7529
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
7529
+ return { status: "paused", turnsUsed: 0, tokensUsed, finalResponse: void 0 };
7530
7530
  }
7531
7531
  yield { type: "status_change", status: "active", reason: "Goal started" };
7532
7532
  while (turn < maxTurns) {
7533
7533
  if (isAborted()) {
7534
7534
  yield { type: "status_change", status: "paused", reason: "aborted via AbortSignal" };
7535
- return { status: "paused", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7535
+ return { status: "paused", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
7536
7536
  }
7537
7537
  turn += 1;
7538
7538
  yield { type: "turn_start", turn, goal };
@@ -7540,6 +7540,8 @@ async function* runUntilImpl(agent, goal, options, deps) {
7540
7540
  const run = await agent.send(continuationPrompt);
7541
7541
  const result = await run.wait();
7542
7542
  lastResponse = result.result ?? "";
7543
+ const turnTokens = result.usage?.totalTokens;
7544
+ if (typeof turnTokens === "number") tokensUsed += turnTokens;
7543
7545
  yield { type: "agent_response", turn, content: lastResponse };
7544
7546
  const judgeOpts = {};
7545
7547
  if (options?.judgeModel !== void 0) judgeOpts.judgeModel = options.judgeModel;
@@ -7565,6 +7567,7 @@ async function* runUntilImpl(agent, goal, options, deps) {
7565
7567
  return {
7566
7568
  status: "failed",
7567
7569
  turnsUsed: turn,
7570
+ tokensUsed,
7568
7571
  finalResponse: lastResponse || void 0
7569
7572
  };
7570
7573
  }
@@ -7573,7 +7576,7 @@ async function* runUntilImpl(agent, goal, options, deps) {
7573
7576
  }
7574
7577
  if (judgment.verdict === "done") {
7575
7578
  yield { type: "status_change", status: "completed", reason: judgment.reason };
7576
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7579
+ return { status: "completed", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
7577
7580
  }
7578
7581
  if (judgment.verdict === "skipped") {
7579
7582
  yield {
@@ -7581,7 +7584,15 @@ async function* runUntilImpl(agent, goal, options, deps) {
7581
7584
  status: "completed",
7582
7585
  reason: `skipped: ${judgment.reason}`
7583
7586
  };
7584
- 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 };
7585
7596
  }
7586
7597
  yield { type: "continuation", turn, prompt: continuationPrompt };
7587
7598
  }
@@ -7590,13 +7601,32 @@ async function* runUntilImpl(agent, goal, options, deps) {
7590
7601
  status: "failed",
7591
7602
  reason: `max turns (${maxTurns}) exhausted`
7592
7603
  };
7593
- return { status: "failed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
7604
+ return { status: "failed", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
7594
7605
  }
7595
7606
  function composeContinuation(goal, lastResponse) {
7596
- return `Continue working toward the goal: ${goal}
7597
-
7598
- Your last response was:
7599
- ${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");
7600
7630
  }
7601
7631
  var init_run_until = __esm({
7602
7632
  "src/internal/runtime/lifecycle/run-until.ts"() {
@@ -20210,7 +20240,7 @@ function localAgentRunUntil(agent, goal, options) {
20210
20240
  status: "paused",
20211
20241
  reason: "runUntil() requires an explicit goal (durable objectives removed in v4.0)"
20212
20242
  };
20213
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
20243
+ return { status: "paused", turnsUsed: 0, tokensUsed: 0, finalResponse: void 0 };
20214
20244
  }
20215
20245
  return yield* runUntilImpl2(agent, goal, options, await buildDeps());
20216
20246
  }