@runappsai/agent 0.3.1 → 0.3.3

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/agent.d.ts CHANGED
@@ -126,6 +126,17 @@ export declare class Agent {
126
126
  /** Stops the current run (pending tool calls get a "skipped" result) and drops the queue. */
127
127
  abort(): Promise<void>;
128
128
  private enqueue;
129
+ /**
130
+ * A user message joining the conversation, with the host's state (`messageContext`) as it is now
131
+ * as its last part. Done when it joins, not when it is typed: a queued message carries the state
132
+ * it is answered in. State the conversation already ends on is not said again.
133
+ */
134
+ private withState;
135
+ /**
136
+ * The newest state still in the conversation. Read from the history itself, not remembered:
137
+ * compaction, a rewind or a reload can take the message that carried it away.
138
+ */
139
+ private lastState;
129
140
  private userMessage;
130
141
  /**
131
142
  * Goes back to before message `index` (as in `messages`): later messages
package/dist/agent.js CHANGED
@@ -6,7 +6,7 @@
6
6
  import { ContextOverflowError, errorStatus, isContextOverflow, modelAccepts, overflowLimit, ProviderError, toProvider } from "./provider.js";
7
7
  import { planCut, resolveSettings, serializeConversation, shouldCompact, splitForSummary, summaryMessage, summaryOf, summarizationMessages } from "./compaction.js";
8
8
  import { collectCompletion } from "./completion.js";
9
- import { buildSystemPrompt, estimateTokens, fitBudget } from "./context.js";
9
+ import { buildSystemPrompt, estimateTokens, fitBudget, STATE_MARK } from "./context.js";
10
10
  import { expandSlash } from "./templates.js";
11
11
  import { fileTools } from "./file-tools.js";
12
12
  import { validateSchema } from "./json-schema.js";
@@ -474,6 +474,42 @@ export class Agent {
474
474
  this.emit("queued", { kind, message });
475
475
  });
476
476
  }
477
+ /**
478
+ * A user message joining the conversation, with the host's state (`messageContext`) as it is now
479
+ * as its last part. Done when it joins, not when it is typed: a queued message carries the state
480
+ * it is answered in. State the conversation already ends on is not said again.
481
+ */
482
+ withState(m, last = this.lastState()) {
483
+ let state;
484
+ try {
485
+ state = this.opts.messageContext?.();
486
+ }
487
+ catch {
488
+ state = undefined;
489
+ }
490
+ if (!state || state === last)
491
+ return m;
492
+ const parts = typeof m.content === "string" ? [{ type: "text", text: m.content }] : [...(m.content ?? [])];
493
+ parts.push({ type: "text", text: STATE_MARK + state });
494
+ return { ...m, content: parts };
495
+ }
496
+ /**
497
+ * The newest state still in the conversation. Read from the history itself, not remembered:
498
+ * compaction, a rewind or a reload can take the message that carried it away.
499
+ */
500
+ lastState(messages = this.history) {
501
+ for (let i = messages.length - 1; i >= 0; i--) {
502
+ const m = messages[i];
503
+ if (m.role !== "user" || !Array.isArray(m.content))
504
+ continue;
505
+ for (let j = m.content.length - 1; j >= 0; j--) {
506
+ const part = m.content[j];
507
+ if (part.type === "text" && part.text.startsWith(STATE_MARK))
508
+ return part.text.slice(STATE_MARK.length);
509
+ }
510
+ }
511
+ return undefined;
512
+ }
477
513
  userMessage(content) {
478
514
  const parts = typeof content === "string" ? [{ type: "text", text: content }] : content.slice();
479
515
  // `ts` (when it was sent) rides along in the saved session and is stripped before the model sees it.
@@ -594,6 +630,7 @@ export class Agent {
594
630
  prefaced = message;
595
631
  prefaceError = err instanceof Error ? err : new Error(String(err));
596
632
  }
633
+ prefaced = this.withState(prefaced);
597
634
  this.history.push(prefaced);
598
635
  }
599
636
  this._ended = null;
@@ -790,7 +827,10 @@ export class Agent {
790
827
  }
791
828
  /** Moves queued steering into the history and says so (`steered`), so a UI draws each message once, when it lands. */
792
829
  applySteering(runId) {
793
- const steer = this.queue.filter((q) => q.kind === "steer").map((q) => q.message);
830
+ const steer = [];
831
+ for (const q of this.queue)
832
+ if (q.kind === "steer")
833
+ steer.push(this.withState(q.message, this.lastState(steer) ?? this.lastState()));
794
834
  if (!steer.length)
795
835
  return;
796
836
  this.queue = this.queue.filter((q) => q.kind !== "steer");
@@ -1375,6 +1415,7 @@ export class Agent {
1375
1415
  prompt_tokens: u.prompt_tokens - before.prompt_tokens,
1376
1416
  completion_tokens: u.completion_tokens - before.completion_tokens,
1377
1417
  calls: u.calls - before.calls,
1418
+ ...((u.cached_tokens ?? 0) > (before.cached_tokens ?? 0) ? { cached_tokens: (u.cached_tokens ?? 0) - (before.cached_tokens ?? 0) } : {}),
1378
1419
  };
1379
1420
  return;
1380
1421
  }
@@ -1387,6 +1428,9 @@ export class Agent {
1387
1428
  this._usage.total_cost = round(this._usage.total_cost + (u.total_cost ?? 0));
1388
1429
  this._usage.prompt_tokens += u.prompt_tokens ?? 0;
1389
1430
  this._usage.completion_tokens += u.completion_tokens ?? 0;
1431
+ const cached = u.prompt_tokens_details?.cached_tokens ?? 0;
1432
+ if (cached > 0)
1433
+ this._usage.cached_tokens = (this._usage.cached_tokens ?? 0) + cached;
1390
1434
  this._usage.calls++;
1391
1435
  }
1392
1436
  /** How full the model's context is: tokens the next call would send (estimated) against the window. */