@runappsai/agent 0.3.0 → 0.3.2

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
@@ -23,6 +23,7 @@ export declare class Agent {
23
23
  /** The platform's web tools, loaded the first time they are offered. */
24
24
  private webCache;
25
25
  private history;
26
+ private recall;
26
27
  private committed;
27
28
  private droppedTurns;
28
29
  private _usage;
@@ -158,6 +159,7 @@ export declare class Agent {
158
159
  private run;
159
160
  /** Moves queued steering into the history and says so (`steered`), so a UI draws each message once, when it lands. */
160
161
  private applySteering;
162
+ /** `budget`: tokens a request should stay under (tool results are left out to get there, see results.ts); 0 for none. */
161
163
  private toWireHistory;
162
164
  /** What the model in use takes besides text (undefined: not known — then it is sent). */
163
165
  private mediaSupport;
package/dist/agent.js CHANGED
@@ -13,6 +13,7 @@ import { validateSchema } from "./json-schema.js";
13
13
  import { sandboxTool } from "./sandbox.js";
14
14
  import { webTools as loadWebTools } from "./web-tools.js";
15
15
  import { DEFAULT_MAX_RESULT_CHARS, normalizeResult, textOf, toChatTool, truncate } from "./tool.js";
16
+ import { needsRecall, RECALL_TOOL, recallTool, shapeResults } from "./results.js";
16
17
  import { askUserTool, domUI } from "./dialogs.js";
17
18
  import { fileGuard } from "./file-guard.js";
18
19
  import { isMachineTitle, normalizePath, titleOf, UNTITLED } from "./workspace.js";
@@ -153,6 +154,7 @@ export class Agent {
153
154
  /** The platform's web tools, loaded the first time they are offered. */
154
155
  webCache = null;
155
156
  history = [];
157
+ recall = null;
156
158
  committed = 0;
157
159
  droppedTurns = 0;
158
160
  _usage = emptyUsage();
@@ -645,7 +647,7 @@ export class Agent {
645
647
  const params = {
646
648
  ...(this.opts.extra ?? {}),
647
649
  model: this.opts.model ?? "",
648
- messages: [{ role: "system", content: system }, ...this.toWireHistory(await this.mediaSupport())],
650
+ messages: [{ role: "system", content: system }, ...this.toWireHistory(await this.mediaSupport(), Math.floor(window * 0.6), fixed)],
649
651
  max_tokens: maxTokens,
650
652
  };
651
653
  if (!params.model)
@@ -798,14 +800,16 @@ export class Agent {
798
800
  this.loop.warned = false;
799
801
  this.emit("steered", { runId, messages: steer });
800
802
  }
801
- toWireHistory(media = {}) {
803
+ /** `budget`: tokens a request should stay under (tool results are left out to get there, see results.ts); 0 for none. */
804
+ toWireHistory(media = {}, budget = 0, system) {
802
805
  let lastUser = -1;
803
806
  for (let i = this.history.length - 1; i >= 0; i--)
804
807
  if (this.history[i].role === "user") {
805
808
  lastUser = i;
806
809
  break;
807
810
  }
808
- return withoutMedia(moveToolImages(this.history.map((m, i) => toWire(m, i < lastUser))), media, this.opts.model);
811
+ const wire = shapeResults(this.history.map((m, i) => toWire(m, i < lastUser)), budget, system);
812
+ return withoutMedia(moveToolImages(wire), media, this.opts.model);
809
813
  }
810
814
  /** What the model in use takes besides text (undefined: not known — then it is sent). */
811
815
  async mediaSupport() {
@@ -823,6 +827,9 @@ export class Agent {
823
827
  /** The tools offered this turn: `vision` tools only while the model is known to take pictures, the web tools while they are on. */
824
828
  async activeTools() {
825
829
  let tools = this.tools;
830
+ // recall_result once a result could have been shortened in what is sent (results.ts)
831
+ if (needsRecall(this.history) && !tools.some((t) => t.name === RECALL_TOOL))
832
+ tools = [...tools, (this.recall ??= recallTool(() => this.history))];
826
833
  if (tools.some((t) => t.vision)) {
827
834
  const sees = await this.accepts("image");
828
835
  tools = tools.filter((t) => !t.vision || sees === true);
@@ -1368,6 +1375,7 @@ export class Agent {
1368
1375
  prompt_tokens: u.prompt_tokens - before.prompt_tokens,
1369
1376
  completion_tokens: u.completion_tokens - before.completion_tokens,
1370
1377
  calls: u.calls - before.calls,
1378
+ ...((u.cached_tokens ?? 0) > (before.cached_tokens ?? 0) ? { cached_tokens: (u.cached_tokens ?? 0) - (before.cached_tokens ?? 0) } : {}),
1371
1379
  };
1372
1380
  return;
1373
1381
  }
@@ -1380,6 +1388,9 @@ export class Agent {
1380
1388
  this._usage.total_cost = round(this._usage.total_cost + (u.total_cost ?? 0));
1381
1389
  this._usage.prompt_tokens += u.prompt_tokens ?? 0;
1382
1390
  this._usage.completion_tokens += u.completion_tokens ?? 0;
1391
+ const cached = u.prompt_tokens_details?.cached_tokens ?? 0;
1392
+ if (cached > 0)
1393
+ this._usage.cached_tokens = (this._usage.cached_tokens ?? 0) + cached;
1383
1394
  this._usage.calls++;
1384
1395
  }
1385
1396
  /** How full the model's context is: tokens the next call would send (estimated) against the window. */