@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/eval.js CHANGED
@@ -6252,10 +6252,12 @@ __export(run_until_exports, {
6252
6252
  async function* runUntilImpl(agent, goal, options, deps) {
6253
6253
  const maxTurns = options?.maxTurns ?? 20;
6254
6254
  const maxFails = options?.maxConsecutiveJudgeFailures ?? 3;
6255
+ const tokenBudget = options?.tokenBudget;
6255
6256
  const signal = options?.signal;
6256
6257
  const isAborted = () => signal !== void 0 && signal.aborted;
6257
6258
  let turn = 0;
6258
6259
  let consecutiveFailures = 0;
6260
+ let tokensUsed = 0;
6259
6261
  let lastResponse = "";
6260
6262
  if (isAborted()) {
6261
6263
  yield {
@@ -6263,20 +6265,44 @@ async function* runUntilImpl(agent, goal, options, deps) {
6263
6265
  status: "paused",
6264
6266
  reason: "aborted via AbortSignal before first turn"
6265
6267
  };
6266
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
6268
+ return { status: "paused", turnsUsed: 0, tokensUsed, finalResponse: void 0 };
6267
6269
  }
6268
6270
  yield { type: "status_change", status: "active", reason: "Goal started" };
6269
6271
  while (turn < maxTurns) {
6270
6272
  if (isAborted()) {
6271
6273
  yield { type: "status_change", status: "paused", reason: "aborted via AbortSignal" };
6272
- return { status: "paused", turnsUsed: turn, finalResponse: lastResponse || void 0 };
6274
+ return {
6275
+ status: "paused",
6276
+ turnsUsed: turn,
6277
+ tokensUsed,
6278
+ finalResponse: lastResponse || void 0
6279
+ };
6273
6280
  }
6274
6281
  turn += 1;
6275
6282
  yield { type: "turn_start", turn, goal };
6276
6283
  const continuationPrompt = turn === 1 ? goal : composeContinuation(goal, lastResponse);
6277
6284
  const run = await agent.send(continuationPrompt);
6285
+ const cancelOnAbort = () => {
6286
+ void run.cancel?.();
6287
+ };
6288
+ if (signal !== void 0) {
6289
+ if (signal.aborted) cancelOnAbort();
6290
+ else signal.addEventListener("abort", cancelOnAbort, { once: true });
6291
+ }
6278
6292
  const result = await run.wait();
6293
+ if (signal !== void 0) signal.removeEventListener("abort", cancelOnAbort);
6279
6294
  lastResponse = result.result ?? "";
6295
+ const turnTokens = result.usage?.totalTokens;
6296
+ if (typeof turnTokens === "number") tokensUsed += turnTokens;
6297
+ if (isAborted()) {
6298
+ yield { type: "status_change", status: "paused", reason: "aborted via AbortSignal" };
6299
+ return {
6300
+ status: "paused",
6301
+ turnsUsed: turn,
6302
+ tokensUsed,
6303
+ finalResponse: lastResponse || void 0
6304
+ };
6305
+ }
6280
6306
  yield { type: "agent_response", turn, content: lastResponse };
6281
6307
  const judgeOpts = {};
6282
6308
  if (options?.judgeModel !== void 0) judgeOpts.judgeModel = options.judgeModel;
@@ -6302,6 +6328,7 @@ async function* runUntilImpl(agent, goal, options, deps) {
6302
6328
  return {
6303
6329
  status: "failed",
6304
6330
  turnsUsed: turn,
6331
+ tokensUsed,
6305
6332
  finalResponse: lastResponse || void 0
6306
6333
  };
6307
6334
  }
@@ -6310,7 +6337,12 @@ async function* runUntilImpl(agent, goal, options, deps) {
6310
6337
  }
6311
6338
  if (judgment.verdict === "done") {
6312
6339
  yield { type: "status_change", status: "completed", reason: judgment.reason };
6313
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
6340
+ return {
6341
+ status: "completed",
6342
+ turnsUsed: turn,
6343
+ tokensUsed,
6344
+ finalResponse: lastResponse || void 0
6345
+ };
6314
6346
  }
6315
6347
  if (judgment.verdict === "skipped") {
6316
6348
  yield {
@@ -6318,7 +6350,25 @@ async function* runUntilImpl(agent, goal, options, deps) {
6318
6350
  status: "completed",
6319
6351
  reason: `skipped: ${judgment.reason}`
6320
6352
  };
6321
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
6353
+ return {
6354
+ status: "completed",
6355
+ turnsUsed: turn,
6356
+ tokensUsed,
6357
+ finalResponse: lastResponse || void 0
6358
+ };
6359
+ }
6360
+ if (tokenBudget !== void 0 && tokensUsed >= tokenBudget) {
6361
+ yield {
6362
+ type: "status_change",
6363
+ status: "budget_limited",
6364
+ reason: `token budget (${tokenBudget}) reached: ${tokensUsed} used`
6365
+ };
6366
+ return {
6367
+ status: "budget_limited",
6368
+ turnsUsed: turn,
6369
+ tokensUsed,
6370
+ finalResponse: lastResponse || void 0
6371
+ };
6322
6372
  }
6323
6373
  yield { type: "continuation", turn, prompt: continuationPrompt };
6324
6374
  }
@@ -6327,13 +6377,37 @@ async function* runUntilImpl(agent, goal, options, deps) {
6327
6377
  status: "failed",
6328
6378
  reason: `max turns (${maxTurns}) exhausted`
6329
6379
  };
6330
- return { status: "failed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
6380
+ return {
6381
+ status: "failed",
6382
+ turnsUsed: turn,
6383
+ tokensUsed,
6384
+ finalResponse: lastResponse || void 0
6385
+ };
6331
6386
  }
6332
6387
  function composeContinuation(goal, lastResponse) {
6333
- return `Continue working toward the goal: ${goal}
6334
-
6335
- Your last response was:
6336
- ${lastResponse.slice(0, 1e3)}`;
6388
+ return [
6389
+ "Continue working toward the active goal.",
6390
+ "",
6391
+ `<objective>
6392
+ ${goal}
6393
+ </objective>`,
6394
+ "",
6395
+ "Continuation behavior:",
6396
+ "- This goal persists across turns. Keep the full objective intact; if it cannot be finished now,",
6397
+ " make concrete progress toward the real requested end state and do not redefine success around a",
6398
+ " smaller or easier task.",
6399
+ "",
6400
+ "Work from evidence:",
6401
+ "- Use the current worktree and external state as authoritative. Inspect the current state before",
6402
+ " relying on it. Improve, replace, or remove existing work as needed to satisfy the actual objective.",
6403
+ "",
6404
+ "Completion audit:",
6405
+ "- Before deciding the goal is achieved, treat completion as unproven and verify it against the actual",
6406
+ " current state, requirement by requirement. Treat uncertain or indirect evidence as not achieved.",
6407
+ "",
6408
+ `Your last response was:
6409
+ ${lastResponse.slice(0, 1e3)}`
6410
+ ].join("\n");
6337
6411
  }
6338
6412
  var init_run_until = __esm({
6339
6413
  "src/internal/runtime/lifecycle/run-until.ts"() {
@@ -18573,7 +18647,7 @@ function localAgentRunUntil(agent, goal, options) {
18573
18647
  status: "paused",
18574
18648
  reason: "runUntil() requires an explicit goal (durable objectives removed in v4.0)"
18575
18649
  };
18576
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
18650
+ return { status: "paused", turnsUsed: 0, tokensUsed: 0, finalResponse: void 0 };
18577
18651
  }
18578
18652
  return yield* runUntilImpl2(agent, goal, options, await buildDeps());
18579
18653
  }