@runtypelabs/sdk 4.8.1 → 4.9.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
@@ -6301,6 +6301,8 @@ var _AgentsEndpoint = class _AgentsEndpoint {
6301
6301
  }
6302
6302
  let currentBody = response.body;
6303
6303
  let accumulatedOutput = "";
6304
+ let lastKnownCost = 0;
6305
+ let lastKnownTokens;
6304
6306
  let pauseCount = 0;
6305
6307
  let discoveryPauseCount = 0;
6306
6308
  let consecutiveDiscoveryPauseCount = 0;
@@ -6318,6 +6320,15 @@ var _AgentsEndpoint = class _AgentsEndpoint {
6318
6320
  }
6319
6321
  callbacks?.onTurnDelta?.(event);
6320
6322
  },
6323
+ onTurnComplete: (event) => {
6324
+ if (typeof event.cost === "number") {
6325
+ lastKnownCost = event.cost;
6326
+ }
6327
+ if (event.tokens) {
6328
+ lastKnownTokens = event.tokens;
6329
+ }
6330
+ callbacks?.onTurnComplete?.(event);
6331
+ },
6321
6332
  onAgentPaused: (event) => {
6322
6333
  pausedEvent = event;
6323
6334
  callbacks?.onAgentPaused?.(event);
@@ -6447,6 +6458,39 @@ var _AgentsEndpoint = class _AgentsEndpoint {
6447
6458
  callbacks?.onAgentComplete?.(forcedCompleteEvent);
6448
6459
  return { completeEvent: forcedCompleteEvent, toolMessages };
6449
6460
  }
6461
+ if (options?.shouldInterrupt?.()) {
6462
+ callbacks?.onLocalToolExecutionComplete?.({
6463
+ executionId,
6464
+ toolCallId: toolId,
6465
+ toolName,
6466
+ parameters: parsedParams,
6467
+ result: toolResult,
6468
+ success: true,
6469
+ completedAt: (/* @__PURE__ */ new Date()).toISOString(),
6470
+ durationMs: Date.now() - localExecutionStartedAtMs
6471
+ });
6472
+ const interruptCompleteEvent = {
6473
+ type: "agent_complete",
6474
+ executionId,
6475
+ seq: 0,
6476
+ agentId: id,
6477
+ success: true,
6478
+ iterations: 1,
6479
+ stopReason: "end_turn",
6480
+ completedAt: (/* @__PURE__ */ new Date()).toISOString(),
6481
+ // Carry the spend observed so far so the interrupted session's
6482
+ // cost still lands in marathon totals and budget accounting
6483
+ totalCost: lastKnownCost,
6484
+ ...lastKnownTokens ? { totalTokens: lastKnownTokens } : {},
6485
+ finalOutput: [
6486
+ accumulatedOutput.trim(),
6487
+ "Session ended early: user steering was queued and will be delivered at the start of the next session."
6488
+ ].filter(Boolean).join("\n\n"),
6489
+ duration: 0
6490
+ };
6491
+ callbacks?.onAgentComplete?.(interruptCompleteEvent);
6492
+ return { completeEvent: interruptCompleteEvent, toolMessages };
6493
+ }
6450
6494
  const resumeResponse = await this.client.requestStream(`/agents/${id}/resume`, {
6451
6495
  method: "POST",
6452
6496
  body: JSON.stringify({
@@ -7512,6 +7556,7 @@ var _AgentsEndpoint = class _AgentsEndpoint {
7512
7556
  if (session === 0 && !options.previousMessages) {
7513
7557
  state.originalMessage = options.message;
7514
7558
  }
7559
+ const queuedSteeringMessages = options.getQueuedUserMessages?.() ?? [];
7515
7560
  const preparedSession = await this.prepareSessionContext(
7516
7561
  options.message,
7517
7562
  state,
@@ -7531,7 +7576,8 @@ var _AgentsEndpoint = class _AgentsEndpoint {
7531
7576
  builtinToolSchemas,
7532
7577
  onContextCompaction: options.onContextCompaction,
7533
7578
  onContextNotice: options.onContextNotice
7534
- }
7579
+ },
7580
+ queuedSteeringMessages
7535
7581
  );
7536
7582
  const { messages, requestContextManagement, pendingNativeCompactionEvent } = preparedSession;
7537
7583
  let sessionResult;
@@ -7558,7 +7604,8 @@ var _AgentsEndpoint = class _AgentsEndpoint {
7558
7604
  sessionLocalTools || options.localTools,
7559
7605
  sessionCallbacks,
7560
7606
  {
7561
- onLocalToolResult: this.createLocalToolLoopGuard(state, sessionTrace, workflow)
7607
+ onLocalToolResult: this.createLocalToolLoopGuard(state, sessionTrace, workflow),
7608
+ shouldInterrupt: options.hasQueuedUserMessages
7562
7609
  },
7563
7610
  state.taskName
7564
7611
  );
@@ -8234,7 +8281,7 @@ Do NOT redo any of the above work.`
8234
8281
  );
8235
8282
  return prepared.messages;
8236
8283
  }
8237
- async prepareSessionContext(originalMessage, state, sessionIndex, maxSessions, localToolNames, continuationContext, workflow, builtinToolIds, compactionOptions) {
8284
+ async prepareSessionContext(originalMessage, state, sessionIndex, maxSessions, localToolNames, continuationContext, workflow, builtinToolIds, compactionOptions, steeringMessages) {
8238
8285
  const wf = workflow ?? defaultWorkflow;
8239
8286
  const compactInstructions = compactionOptions?.compactInstructions;
8240
8287
  const resolvedStrategy = continuationContext?.compact ? "summary_fallback" : this.resolveCompactStrategy(compactionOptions?.compactStrategy, compactionOptions?.model);
@@ -8319,6 +8366,11 @@ Do NOT redo any of the above work.`
8319
8366
  const phaseBlock = ["", this.buildPhaseInstructions(state, wf)].join("\n");
8320
8367
  const candidateBlock = wf.buildCandidateBlock?.(state) ?? "";
8321
8368
  const multiSessionInstruction = `This is a multi-session task (session ${sessionIndex + 1}/${maxSessions}). When you have fully completed the task, end your response with TASK_COMPLETE on its own line.`;
8369
+ const steeringLines = steeringMessages && steeringMessages.length > 0 ? [
8370
+ "--- User steering (queued during the previous session) ---",
8371
+ ...steeringMessages,
8372
+ ""
8373
+ ] : [];
8322
8374
  if (continuationContext && sessionIndex === 0) {
8323
8375
  const replayHistoryMessages = this.sanitizeReplayHistoryMessages(
8324
8376
  continuationContext.previousMessages
@@ -8329,6 +8381,7 @@ Do NOT redo any of the above work.`
8329
8381
  const userContent = [
8330
8382
  continuationGuardrail,
8331
8383
  "",
8384
+ ...steeringLines,
8332
8385
  userMessage,
8333
8386
  phaseBlock,
8334
8387
  toolsBlock,
@@ -8431,6 +8484,7 @@ Do NOT redo any of the above work.`
8431
8484
  const recoveryMessage2 = this.buildStuckTurnRecoveryMessage(state, wf);
8432
8485
  const rejectionNotice = state.lastCompletionRejectionReason ? `TASK_COMPLETE was rejected because: ${state.lastCompletionRejectionReason}. Address this before signaling completion again.` : void 0;
8433
8486
  const continuationContent = [
8487
+ ...steeringLines,
8434
8488
  "Continue the task.",
8435
8489
  phaseBlock,
8436
8490
  toolsBlock,
@@ -8504,6 +8558,7 @@ Do NOT redo any of the above work.`
8504
8558
  const recoveryMessage = this.buildStuckTurnRecoveryMessage(state, wf);
8505
8559
  const fallbackRejectionNotice = state.lastCompletionRejectionReason ? `TASK_COMPLETE was rejected because: ${state.lastCompletionRejectionReason}. Address this before signaling completion again.` : void 0;
8506
8560
  const content = [
8561
+ ...steeringLines,
8507
8562
  originalMessage,
8508
8563
  phaseBlock,
8509
8564
  toolsBlock,