@runappsai/agent 0.1.10 → 0.1.11

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
@@ -1,5 +1,6 @@
1
1
  import type { ChatMessage, ContentPart, Unsubscribe } from "./llm.js";
2
2
  import { type InputKind, type ModelInfo, type ModelProvider, type RunAppsClientLike } from "./provider.js";
3
+ import { type CompactionChoice, type CompactionSettings } from "./compaction.js";
3
4
  import type { AgentEventMap, AgentEventName, AgentOptions, AgentUI, AgentUsage, Permissions, RunEnd, RunResult, SessionData, Tool } from "./types.js";
4
5
  import { type Workspace } from "./workspace.js";
5
6
  export type PromptContent = string | ContentPart[];
@@ -15,6 +16,10 @@ export declare class Agent {
15
16
  private readonly opts;
16
17
  /** `webTools` as configured (a function is read every turn). */
17
18
  private web;
19
+ /** Whether the loop summarises older turns (else it only trims them): `compaction !== false`, or `configure({ compaction })`. */
20
+ private compactSummaries;
21
+ /** The compaction thresholds set (an unset one is the default): the `compaction` object, or `configure({ compaction })`. */
22
+ private compactThresholds;
18
23
  /** The platform's web tools, loaded the first time they are offered. */
19
24
  private webCache;
20
25
  private history;
@@ -63,6 +68,7 @@ export declare class Agent {
63
68
  maxTokens: number | "auto";
64
69
  permissions: "ask" | "auto" | "custom";
65
70
  reasoningEffort?: string;
71
+ compaction: CompactionChoice;
66
72
  };
67
73
  /** Changes settings for the runs that follow (a run in progress keeps its own). `undefined` leaves a field alone; `reasoningEffort: null` takes the model's default. */
68
74
  configure(patch: {
@@ -72,7 +78,17 @@ export declare class Agent {
72
78
  permissions?: Permissions;
73
79
  reasoningEffort?: string | null;
74
80
  webTools?: boolean;
81
+ compaction?: CompactionChoice;
75
82
  }): void;
83
+ /**
84
+ * Compaction as it applies to a window of `window` tokens: whether older
85
+ * turns are summarised (else only trimmed), at how many tokens left it
86
+ * starts (`reserveTokens`) and how much of the newest it keeps verbatim
87
+ * (`keepRecentTokens`), each capped by the window (20 % / 40 %).
88
+ */
89
+ compactionFor(window: number): CompactionSettings & {
90
+ summarize: boolean;
91
+ };
76
92
  /** A snapshot to save (`restore` continues it). Taken at the last complete turn. */
77
93
  export(): SessionData;
78
94
  /** Forgets the conversation (the workspace stays). */
package/dist/agent.js CHANGED
@@ -146,6 +146,10 @@ export class Agent {
146
146
  opts;
147
147
  /** `webTools` as configured (a function is read every turn). */
148
148
  web;
149
+ /** Whether the loop summarises older turns (else it only trims them): `compaction !== false`, or `configure({ compaction })`. */
150
+ compactSummaries;
151
+ /** The compaction thresholds set (an unset one is the default): the `compaction` object, or `configure({ compaction })`. */
152
+ compactThresholds;
149
153
  /** The platform's web tools, loaded the first time they are offered. */
150
154
  webCache = null;
151
155
  history = [];
@@ -174,6 +178,8 @@ export class Agent {
174
178
  constructor(provider, opts = {}) {
175
179
  this.provider = toProvider(provider);
176
180
  this.opts = opts;
181
+ this.compactSummaries = opts.compaction !== false;
182
+ this.compactThresholds = typeof opts.compaction === "object" ? { ...opts.compaction } : {};
177
183
  this.id = opts.id || `agent_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
178
184
  this.ui = opts.ui ?? domUI();
179
185
  this.workspace = opts.workspace ?? null;
@@ -312,10 +318,25 @@ export class Agent {
312
318
  get settings() {
313
319
  const p = this.opts.permissions ?? "ask";
314
320
  const re = this.opts.extra?.reasoning_effort;
315
- return { model: this.opts.model, maxTurns: this.opts.maxTurns ?? DEFAULT_MAX_TURNS, maxTokens: typeof this.opts.maxTokens === "number" ? this.opts.maxTokens : "auto", permissions: typeof p === "function" ? "custom" : p, reasoningEffort: typeof re === "string" ? re : undefined };
321
+ return { model: this.opts.model, maxTurns: this.opts.maxTurns ?? DEFAULT_MAX_TURNS, maxTokens: typeof this.opts.maxTokens === "number" ? this.opts.maxTokens : "auto", permissions: typeof p === "function" ? "custom" : p, reasoningEffort: typeof re === "string" ? re : undefined, compaction: { summarize: this.compactSummaries, ...this.compactThresholds } };
316
322
  }
317
323
  /** Changes settings for the runs that follow (a run in progress keeps its own). `undefined` leaves a field alone; `reasoningEffort: null` takes the model's default. */
318
324
  configure(patch) {
325
+ if (patch.compaction) {
326
+ const c = patch.compaction;
327
+ if (typeof c.summarize === "boolean")
328
+ this.compactSummaries = c.summarize;
329
+ // A threshold named with no number goes back to its default.
330
+ for (const k of ["reserveTokens", "keepRecentTokens"]) {
331
+ if (!(k in c))
332
+ continue;
333
+ const v = c[k];
334
+ if (typeof v === "number" && Number.isFinite(v) && v > 0)
335
+ this.compactThresholds[k] = Math.floor(v);
336
+ else
337
+ delete this.compactThresholds[k];
338
+ }
339
+ }
319
340
  if ("model" in patch)
320
341
  this.model = patch.model || undefined;
321
342
  if (typeof patch.webTools === "boolean")
@@ -335,6 +356,15 @@ export class Agent {
335
356
  if (patch.permissions !== undefined)
336
357
  this.opts.permissions = patch.permissions;
337
358
  }
359
+ /**
360
+ * Compaction as it applies to a window of `window` tokens: whether older
361
+ * turns are summarised (else only trimmed), at how many tokens left it
362
+ * starts (`reserveTokens`) and how much of the newest it keeps verbatim
363
+ * (`keepRecentTokens`), each capped by the window (20 % / 40 %).
364
+ */
365
+ compactionFor(window) {
366
+ return { summarize: this.compactSummaries, ...resolveSettings(window, this.compactThresholds) };
367
+ }
338
368
  /** A snapshot to save (`restore` continues it). Taken at the last complete turn. */
339
369
  export() {
340
370
  const messages = this.history.slice(0, this.committed);
@@ -566,7 +596,7 @@ export class Agent {
566
596
  const { output: maxTokens, input: catalogueWindow } = await this.limits();
567
597
  // A refusal that states the model's real limit corrects the window for the rest of the run.
568
598
  let window = catalogueWindow;
569
- let compaction = resolveSettings(window, typeof this.opts.compaction === "object" ? this.opts.compaction : {});
599
+ let compaction = resolveSettings(window, this.compactThresholds);
570
600
  let overflowRetries = 0;
571
601
  try {
572
602
  if (prefaceError)
@@ -591,7 +621,7 @@ export class Agent {
591
621
  // window is nearly full; elide/drop as the fallback.
592
622
  const tokensBefore = estimateTokens(this.history, fixed);
593
623
  if (shouldCompact(tokensBefore, window, compaction)) {
594
- const summarized = this.opts.compaction === false ? false : await this.compactWith(compaction, runId, signal);
624
+ const summarized = !this.compactSummaries ? false : await this.compactWith(compaction, runId, signal);
595
625
  const after = estimateTokens(this.history, fixed);
596
626
  if (!summarized || shouldCompact(after, window, compaction)) {
597
627
  // Trim, keeping a compaction summary at the front pinned: it
@@ -644,7 +674,7 @@ export class Agent {
644
674
  const limit = overflowLimit(err);
645
675
  if (limit !== undefined && limit < window) {
646
676
  window = limit;
647
- compaction = resolveSettings(window, typeof this.opts.compaction === "object" ? this.opts.compaction : {});
677
+ compaction = resolveSettings(window, this.compactThresholds);
648
678
  }
649
679
  // Asked twice at most: first with the older turns summarised, then with nothing left but what the latest needs.
650
680
  let smaller = false;
@@ -856,7 +886,7 @@ export class Agent {
856
886
  compact(focus) {
857
887
  return this.exclusive("compact", async (signal) => {
858
888
  const window = await this.contextTokens();
859
- const settings = this.squeezed(resolveSettings(window, typeof this.opts.compaction === "object" ? this.opts.compaction : {}));
889
+ const settings = this.squeezed(resolveSettings(window, this.compactThresholds));
860
890
  const done = await this.compactWith(settings, undefined, signal, focus);
861
891
  if (done)
862
892
  await this.saveSession();
@@ -876,7 +906,7 @@ export class Agent {
876
906
  if (!toSummarize.length)
877
907
  return first;
878
908
  const window = await this.contextTokens();
879
- const settings = resolveSettings(window, typeof this.opts.compaction === "object" ? this.opts.compaction : {});
909
+ const settings = resolveSettings(window, this.compactThresholds);
880
910
  this.emit("compaction_start", {});
881
911
  try {
882
912
  return await this.writeSummary({ toSummarize, previousSummary: first ?? undefined, kept: [] }, settings, undefined, signal);
@@ -937,7 +967,7 @@ export class Agent {
937
967
  */
938
968
  async squeeze(window, round, settings, fixed, runId, signal) {
939
969
  const before = estimateTokens(this.history, fixed);
940
- if (round === 0 && this.opts.compaction !== false)
970
+ if (round === 0 && this.compactSummaries)
941
971
  await this.compactWith(this.squeezed(settings), runId, signal);
942
972
  const pinned = this.history.length && summaryOf(this.history[0]) !== null ? [this.history[0]] : [];
943
973
  const rest = this.history.slice(pinned.length);