@runappsai/agent 0.1.10 → 0.1.15

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;
@@ -36,7 +41,11 @@ export declare class Agent {
36
41
  private readonly loaded;
37
42
  private createdAt;
38
43
  /** The session's title, fixed once known (a later compaction must not turn it into the summary's first words). */
39
- private title;
44
+ private fixedTitle;
45
+ /** The conversation's title as the session list shows it (the first thing the user wrote, unless `setTitle` fixed another); undefined before there is one. */
46
+ get title(): string | undefined;
47
+ /** Fixes the conversation's title (a scheduled task's name, say); saved with the session. */
48
+ setTitle(title: string): void;
40
49
  private _ended;
41
50
  private persist;
42
51
  /** `provider`: a ModelProvider, or a RunApps SDK client (wrapped in runappsProvider). */
@@ -63,6 +72,7 @@ export declare class Agent {
63
72
  maxTokens: number | "auto";
64
73
  permissions: "ask" | "auto" | "custom";
65
74
  reasoningEffort?: string;
75
+ compaction: CompactionChoice;
66
76
  };
67
77
  /** 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
78
  configure(patch: {
@@ -72,7 +82,17 @@ export declare class Agent {
72
82
  permissions?: Permissions;
73
83
  reasoningEffort?: string | null;
74
84
  webTools?: boolean;
85
+ compaction?: CompactionChoice;
75
86
  }): void;
87
+ /**
88
+ * Compaction as it applies to a window of `window` tokens: whether older
89
+ * turns are summarised (else only trimmed), at how many tokens left it
90
+ * starts (`reserveTokens`) and how much of the newest it keeps verbatim
91
+ * (`keepRecentTokens`), each capped by the window (20 % / 40 %).
92
+ */
93
+ compactionFor(window: number): CompactionSettings & {
94
+ summarize: boolean;
95
+ };
76
96
  /** A snapshot to save (`restore` continues it). Taken at the last complete turn. */
77
97
  export(): SessionData;
78
98
  /** 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 = [];
@@ -167,13 +171,19 @@ export class Agent {
167
171
  loaded;
168
172
  createdAt = Date.now();
169
173
  /** The session's title, fixed once known (a later compaction must not turn it into the summary's first words). */
170
- title;
174
+ fixedTitle;
175
+ /** The conversation's title as the session list shows it (the first thing the user wrote, unless `setTitle` fixed another); undefined before there is one. */
176
+ get title() { return this.fixedTitle; }
177
+ /** Fixes the conversation's title (a scheduled task's name, say); saved with the session. */
178
+ setTitle(title) { this.fixedTitle = title.trim() || undefined; }
171
179
  _ended = null;
172
180
  persist;
173
181
  /** `provider`: a ModelProvider, or a RunApps SDK client (wrapped in runappsProvider). */
174
182
  constructor(provider, opts = {}) {
175
183
  this.provider = toProvider(provider);
176
184
  this.opts = opts;
185
+ this.compactSummaries = opts.compaction !== false;
186
+ this.compactThresholds = typeof opts.compaction === "object" ? { ...opts.compaction } : {};
177
187
  this.id = opts.id || `agent_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
178
188
  this.ui = opts.ui ?? domUI();
179
189
  this.workspace = opts.workspace ?? null;
@@ -234,7 +244,7 @@ export class Agent {
234
244
  this.createdAt = data.createdAt ?? data.savedAt ?? Date.now();
235
245
  this._ended = data.ended ?? null;
236
246
  // Titles saved before titleOf skipped summaries and expanded commands are worked out again.
237
- this.title = data.title && !isMachineTitle(data.title) ? data.title : undefined;
247
+ this.fixedTitle = data.title && !isMachineTitle(data.title) ? data.title : undefined;
238
248
  if (data.model && !this.opts.model) {
239
249
  this.opts.model = data.model;
240
250
  this.catalogue = null;
@@ -312,10 +322,25 @@ export class Agent {
312
322
  get settings() {
313
323
  const p = this.opts.permissions ?? "ask";
314
324
  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 };
325
+ 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
326
  }
317
327
  /** 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
328
  configure(patch) {
329
+ if (patch.compaction) {
330
+ const c = patch.compaction;
331
+ if (typeof c.summarize === "boolean")
332
+ this.compactSummaries = c.summarize;
333
+ // A threshold named with no number goes back to its default.
334
+ for (const k of ["reserveTokens", "keepRecentTokens"]) {
335
+ if (!(k in c))
336
+ continue;
337
+ const v = c[k];
338
+ if (typeof v === "number" && Number.isFinite(v) && v > 0)
339
+ this.compactThresholds[k] = Math.floor(v);
340
+ else
341
+ delete this.compactThresholds[k];
342
+ }
343
+ }
319
344
  if ("model" in patch)
320
345
  this.model = patch.model || undefined;
321
346
  if (typeof patch.webTools === "boolean")
@@ -335,6 +360,15 @@ export class Agent {
335
360
  if (patch.permissions !== undefined)
336
361
  this.opts.permissions = patch.permissions;
337
362
  }
363
+ /**
364
+ * Compaction as it applies to a window of `window` tokens: whether older
365
+ * turns are summarised (else only trimmed), at how many tokens left it
366
+ * starts (`reserveTokens`) and how much of the newest it keeps verbatim
367
+ * (`keepRecentTokens`), each capped by the window (20 % / 40 %).
368
+ */
369
+ compactionFor(window) {
370
+ return { summarize: this.compactSummaries, ...resolveSettings(window, this.compactThresholds) };
371
+ }
338
372
  /** A snapshot to save (`restore` continues it). Taken at the last complete turn. */
339
373
  export() {
340
374
  const messages = this.history.slice(0, this.committed);
@@ -349,12 +383,12 @@ export class Agent {
349
383
  savedAt: Date.now(),
350
384
  ...(this._ended ? { ended: this._ended } : {}),
351
385
  };
352
- if (!this.title) {
386
+ if (!this.fixedTitle) {
353
387
  const tt = titleOf(data);
354
388
  if (tt !== UNTITLED)
355
- this.title = tt;
389
+ this.fixedTitle = tt;
356
390
  }
357
- data.title = this.title ?? UNTITLED;
391
+ data.title = this.fixedTitle ?? UNTITLED;
358
392
  return data;
359
393
  }
360
394
  /** Forgets the conversation (the workspace stays). */
@@ -362,7 +396,7 @@ export class Agent {
362
396
  if (this.current)
363
397
  throw new Error("cannot reset while a run is active; abort() first");
364
398
  this.history = [];
365
- this.title = undefined;
399
+ this.fixedTitle = undefined;
366
400
  this._ended = null;
367
401
  this.committed = 0;
368
402
  this.droppedTurns = 0;
@@ -566,7 +600,7 @@ export class Agent {
566
600
  const { output: maxTokens, input: catalogueWindow } = await this.limits();
567
601
  // A refusal that states the model's real limit corrects the window for the rest of the run.
568
602
  let window = catalogueWindow;
569
- let compaction = resolveSettings(window, typeof this.opts.compaction === "object" ? this.opts.compaction : {});
603
+ let compaction = resolveSettings(window, this.compactThresholds);
570
604
  let overflowRetries = 0;
571
605
  try {
572
606
  if (prefaceError)
@@ -591,7 +625,7 @@ export class Agent {
591
625
  // window is nearly full; elide/drop as the fallback.
592
626
  const tokensBefore = estimateTokens(this.history, fixed);
593
627
  if (shouldCompact(tokensBefore, window, compaction)) {
594
- const summarized = this.opts.compaction === false ? false : await this.compactWith(compaction, runId, signal);
628
+ const summarized = !this.compactSummaries ? false : await this.compactWith(compaction, runId, signal);
595
629
  const after = estimateTokens(this.history, fixed);
596
630
  if (!summarized || shouldCompact(after, window, compaction)) {
597
631
  // Trim, keeping a compaction summary at the front pinned: it
@@ -644,7 +678,7 @@ export class Agent {
644
678
  const limit = overflowLimit(err);
645
679
  if (limit !== undefined && limit < window) {
646
680
  window = limit;
647
- compaction = resolveSettings(window, typeof this.opts.compaction === "object" ? this.opts.compaction : {});
681
+ compaction = resolveSettings(window, this.compactThresholds);
648
682
  }
649
683
  // Asked twice at most: first with the older turns summarised, then with nothing left but what the latest needs.
650
684
  let smaller = false;
@@ -856,7 +890,7 @@ export class Agent {
856
890
  compact(focus) {
857
891
  return this.exclusive("compact", async (signal) => {
858
892
  const window = await this.contextTokens();
859
- const settings = this.squeezed(resolveSettings(window, typeof this.opts.compaction === "object" ? this.opts.compaction : {}));
893
+ const settings = this.squeezed(resolveSettings(window, this.compactThresholds));
860
894
  const done = await this.compactWith(settings, undefined, signal, focus);
861
895
  if (done)
862
896
  await this.saveSession();
@@ -876,7 +910,7 @@ export class Agent {
876
910
  if (!toSummarize.length)
877
911
  return first;
878
912
  const window = await this.contextTokens();
879
- const settings = resolveSettings(window, typeof this.opts.compaction === "object" ? this.opts.compaction : {});
913
+ const settings = resolveSettings(window, this.compactThresholds);
880
914
  this.emit("compaction_start", {});
881
915
  try {
882
916
  return await this.writeSummary({ toSummarize, previousSummary: first ?? undefined, kept: [] }, settings, undefined, signal);
@@ -937,7 +971,7 @@ export class Agent {
937
971
  */
938
972
  async squeeze(window, round, settings, fixed, runId, signal) {
939
973
  const before = estimateTokens(this.history, fixed);
940
- if (round === 0 && this.opts.compaction !== false)
974
+ if (round === 0 && this.compactSummaries)
941
975
  await this.compactWith(this.squeezed(settings), runId, signal);
942
976
  const pinned = this.history.length && summaryOf(this.history[0]) !== null ? [this.history[0]] : [];
943
977
  const rest = this.history.slice(pinned.length);
@@ -1251,7 +1285,7 @@ export class Agent {
1251
1285
  if (question === false)
1252
1286
  return true;
1253
1287
  const { title, message, always, ok, body } = typeof question === "string" ? { title: question, message: undefined, always: undefined, ok: undefined, body: undefined } : question;
1254
- return this.ui.confirm(title, message, { danger: !!tool.destructive, signal, always, ...(ok ? { ok } : {}), ...(body ? { body } : {}) });
1288
+ return this.ui.confirm(title, message, { danger: !!tool.destructive, signal, always, permission: true, ...(ok ? { ok } : {}), ...(body ? { body } : {}) });
1255
1289
  }
1256
1290
  /** After an abort, every open tool call gets a result so the transcript stays valid. */
1257
1291
  closeOpenToolCalls() {