@copilotkit/runtime 1.62.2-canary.1783457132 → 1.62.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.
Files changed (52) hide show
  1. package/dist/package.cjs +1 -1
  2. package/dist/package.mjs +1 -1
  3. package/dist/v2/index.cjs +1 -5
  4. package/dist/v2/index.d.cts +2 -2
  5. package/dist/v2/index.d.mts +2 -2
  6. package/dist/v2/index.mjs +2 -2
  7. package/dist/v2/runtime/core/fetch-handler.cjs +24 -1
  8. package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -1
  9. package/dist/v2/runtime/core/fetch-handler.d.cts.map +1 -1
  10. package/dist/v2/runtime/core/fetch-handler.d.mts.map +1 -1
  11. package/dist/v2/runtime/core/fetch-handler.mjs +24 -1
  12. package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -1
  13. package/dist/v2/runtime/core/fetch-router.cjs +8 -0
  14. package/dist/v2/runtime/core/fetch-router.cjs.map +1 -1
  15. package/dist/v2/runtime/core/fetch-router.mjs +8 -0
  16. package/dist/v2/runtime/core/fetch-router.mjs.map +1 -1
  17. package/dist/v2/runtime/core/hooks.cjs.map +1 -1
  18. package/dist/v2/runtime/core/hooks.d.cts +3 -0
  19. package/dist/v2/runtime/core/hooks.d.cts.map +1 -1
  20. package/dist/v2/runtime/core/hooks.d.mts +3 -0
  21. package/dist/v2/runtime/core/hooks.d.mts.map +1 -1
  22. package/dist/v2/runtime/core/hooks.mjs.map +1 -1
  23. package/dist/v2/runtime/endpoints/single-route-helpers.cjs +1 -0
  24. package/dist/v2/runtime/endpoints/single-route-helpers.cjs.map +1 -1
  25. package/dist/v2/runtime/endpoints/single-route-helpers.mjs +1 -0
  26. package/dist/v2/runtime/endpoints/single-route-helpers.mjs.map +1 -1
  27. package/dist/v2/runtime/handlers/get-runtime-info.cjs +1 -0
  28. package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -1
  29. package/dist/v2/runtime/handlers/get-runtime-info.mjs +1 -0
  30. package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -1
  31. package/dist/v2/runtime/handlers/handle-suggest.cjs +83 -0
  32. package/dist/v2/runtime/handlers/handle-suggest.cjs.map +1 -0
  33. package/dist/v2/runtime/handlers/handle-suggest.mjs +82 -0
  34. package/dist/v2/runtime/handlers/handle-suggest.mjs.map +1 -0
  35. package/dist/v2/runtime/handlers/shared/sse-response.cjs +4 -4
  36. package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -1
  37. package/dist/v2/runtime/handlers/shared/sse-response.mjs +4 -4
  38. package/dist/v2/runtime/handlers/shared/sse-response.mjs.map +1 -1
  39. package/dist/v2/runtime/index.d.cts +1 -1
  40. package/dist/v2/runtime/index.d.mts +1 -1
  41. package/dist/v2/runtime/runner/in-memory.cjs +37 -221
  42. package/dist/v2/runtime/runner/in-memory.cjs.map +1 -1
  43. package/dist/v2/runtime/runner/in-memory.d.cts +3 -164
  44. package/dist/v2/runtime/runner/in-memory.d.cts.map +1 -1
  45. package/dist/v2/runtime/runner/in-memory.d.mts +3 -164
  46. package/dist/v2/runtime/runner/in-memory.d.mts.map +1 -1
  47. package/dist/v2/runtime/runner/in-memory.mjs +38 -218
  48. package/dist/v2/runtime/runner/in-memory.mjs.map +1 -1
  49. package/dist/v2/runtime/runner/index.d.cts +1 -1
  50. package/dist/v2/runtime/runner/index.d.mts +1 -1
  51. package/dist/v2/runtime/runner/index.mjs +1 -1
  52. package/package.json +2 -2
@@ -5,26 +5,6 @@ import { ReplaySubject } from "rxjs";
5
5
  import { EventType, compactEvents } from "@ag-ui/client";
6
6
 
7
7
  //#region src/v2/runtime/runner/in-memory.ts
8
- const ɵINMEMORY_DEFAULTS = {
9
- maxThreads: 1e3,
10
- maxRunsPerThread: 100,
11
- maxBytes: 512 * 1024 ** 2
12
- };
13
- const EVICTION_GUIDANCE = "[CopilotKit] InMemoryAgentRunner evicted in-memory thread history to stay under memory limits. This runner is bounded and non-durable by design. For durable or production threads, configure an Intelligence backend.";
14
- const LIMITS_CLOBBER_GUIDANCE = "[CopilotKit] InMemoryAgentRunner was constructed with in-memory limits that differ from the already-configured process-global store; the last-constructed runner's limits apply to ALL in-memory threads (the store is shared per-process). Configure a single consistent set of limits, or use an Intelligence backend for isolated bounds.";
15
- /**
16
- * Best-effort approximate byte size of a value, via serialized length.
17
- * Never throws — returns 0 when the value cannot be serialized. This is an
18
- * approximation (UTF-16 length, not exact heap bytes), used only for relative
19
- * accounting against `maxBytes`.
20
- */
21
- function ɵestimateBytes(value) {
22
- try {
23
- return JSON.stringify(value)?.length ?? 0;
24
- } catch {
25
- return 0;
26
- }
27
- }
28
8
  var InMemoryEventStore = class {
29
9
  constructor(threadId) {
30
10
  this.threadId = threadId;
@@ -36,195 +16,21 @@ var InMemoryEventStore = class {
36
16
  this.runSubject = null;
37
17
  this.stopRequested = false;
38
18
  this.currentEvents = null;
39
- this.messagesSnapshot = [];
40
- this.approxMessagesSnapshotBytes = 0;
41
- }
42
- };
43
- var ɵBoundedThreadStore = class {
44
- constructor(limits) {
45
- this.limits = limits;
46
- this.map = /* @__PURE__ */ new Map();
47
- this.totalBytes = 0;
48
- this.warned = false;
49
- this.limitsExplicitlySet = false;
50
- this.clobberWarned = false;
51
- }
52
- get byteTotal() {
53
- return this.totalBytes;
54
- }
55
- /**
56
- * Reconfigure the process-global store's bounds. Called by the
57
- * {@link InMemoryAgentRunner} constructor when limits are passed. Because the
58
- * store is a per-process singleton, this replaces the bounds for ALL in-memory
59
- * threads. Emits {@link LIMITS_CLOBBER_GUIDANCE} at most ONCE per store when a
60
- * SECOND (or later) explicit set arrives whose resolved values differ from the
61
- * prior explicit set — i.e. a genuine clobber of an already-customized config.
62
- * The first explicit customization (defaults → custom) is the intended
63
- * override and never warns; identical re-sets never warn.
64
- */
65
- setLimits(limits) {
66
- if (this.limitsExplicitlySet && !this.clobberWarned && (limits.maxThreads !== this.limits.maxThreads || limits.maxRunsPerThread !== this.limits.maxRunsPerThread || limits.maxBytes !== this.limits.maxBytes)) {
67
- this.clobberWarned = true;
68
- try {
69
- console.warn(LIMITS_CLOBBER_GUIDANCE);
70
- } catch {}
71
- }
72
- this.limitsExplicitlySet = true;
73
- this.limits = limits;
74
- }
75
- get size() {
76
- return this.map.size;
77
- }
78
- /** Re-insert at the tail so Map iteration order stays LRU-first. */
79
- touchOrder(threadId, store) {
80
- this.map.delete(threadId);
81
- this.map.set(threadId, store);
82
- }
83
- getOrCreate(threadId) {
84
- const existing = this.map.get(threadId);
85
- if (existing) {
86
- this.touchOrder(threadId, existing);
87
- return existing;
88
- }
89
- const store = new InMemoryEventStore(threadId);
90
- this.map.set(threadId, store);
91
- this.evictThreadsIfNeeded(threadId);
92
- return store;
93
- }
94
- get(threadId, opts) {
95
- const store = this.map.get(threadId);
96
- if (store && opts.touch) this.touchOrder(threadId, store);
97
- return store;
98
- }
99
- peek(threadId) {
100
- return this.map.get(threadId);
101
- }
102
- /**
103
- * Evict the least-recently-used thread that is neither running NOR
104
- * mid-finalization. Returns false if none evictable. The `protect` thread
105
- * (typically the one just created) is never evicted, so a fresh thread is not
106
- * immediately dropped when it is the only non-running candidate.
107
- *
108
- * A thread is skipped while `isRunning` OR `stopRequested` is set.
109
- * `stop()` flips `isRunning` to false the moment it aborts the agent, but the
110
- * run keeps finalizing asynchronously (the abort trips the `catch` in
111
- * `runAgent`, which later calls `appendRun`). During that window
112
- * `stopRequested` stays true; evicting the thread then would make the pending
113
- * `appendRun` hit `if (!store) return` and silently drop the aborted run's
114
- * history. Guarding on `stopRequested` keeps the thread alive until
115
- * finalization completes.
116
- */
117
- evictOneLru(protect) {
118
- for (const [threadId, store] of this.map) {
119
- if (threadId === protect) continue;
120
- if (store.isRunning || store.stopRequested) continue;
121
- this.removeThread(threadId, store);
122
- this.noteEviction();
123
- return true;
124
- }
125
- return false;
126
- }
127
- appendRun(threadId, run) {
128
- const store = this.map.get(threadId);
129
- if (!store) return;
130
- if (run.messages.length > 0) {
131
- this.totalBytes -= store.approxMessagesSnapshotBytes;
132
- store.messagesSnapshot = run.messages;
133
- store.approxMessagesSnapshotBytes = ɵestimateBytes(run.messages);
134
- this.totalBytes += store.approxMessagesSnapshotBytes;
135
- }
136
- run.messages = [];
137
- run.approxMessageBytes = 0;
138
- run.approxEventBytes = ɵestimateBytes(run.events);
139
- store.historicRuns.push(run);
140
- this.totalBytes += run.approxEventBytes;
141
- this.touchOrder(threadId, store);
142
- this.enforceRunCap(store);
143
- this.evictByBytesIfNeeded(threadId);
144
- }
145
- enforceRunCap(store) {
146
- const cap = this.limits.maxRunsPerThread;
147
- if (!cap || cap === Infinity) return;
148
- while (store.historicRuns.length > cap) {
149
- const dropped = store.historicRuns.shift();
150
- this.totalBytes -= dropped.approxEventBytes ?? 0;
151
- }
152
- }
153
- /**
154
- * Trim the store back under the byte ceiling by evicting LRU non-running
155
- * threads. `protect` (the just-appended thread) is never self-evicted, so a
156
- * fresh run pushes OTHER threads out rather than dropping itself.
157
- */
158
- evictByBytesIfNeeded(protect) {
159
- while (this.totalBytes > this.limits.maxBytes) if (!this.evictOneLru(protect)) break;
160
- }
161
- removeThread(threadId, store) {
162
- for (const run of store.historicRuns) this.totalBytes -= run.approxEventBytes ?? 0;
163
- this.totalBytes -= store.approxMessagesSnapshotBytes;
164
- this.map.delete(threadId);
165
- }
166
- evictThreadsIfNeeded(protect) {
167
- while (this.map.size > this.limits.maxThreads) if (!this.evictOneLru(protect)) break;
168
- }
169
- noteEviction() {
170
- if (this.warned) return;
171
- this.warned = true;
172
- try {
173
- console.warn(EVICTION_GUIDANCE);
174
- } catch {}
175
- }
176
- listThreads() {
177
- const threads = [];
178
- for (const [threadId, store] of this.map) {
179
- if (store.historicRuns.length === 0) continue;
180
- const firstRun = store.historicRuns[0];
181
- const lastRun = store.historicRuns[store.historicRuns.length - 1];
182
- threads.push({
183
- id: threadId,
184
- name: null,
185
- agentId: lastRun.agentId,
186
- organizationId: "",
187
- createdById: "",
188
- archived: false,
189
- createdAt: new Date(firstRun.createdAt).toISOString(),
190
- updatedAt: new Date(lastRun.createdAt).toISOString()
191
- });
192
- }
193
- return threads.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
194
- }
195
- clear() {
196
- this.map.clear();
197
- this.totalBytes = 0;
198
- this.warned = false;
199
19
  }
200
20
  };
201
- /**
202
- * Process-wide singleton backing every {@link InMemoryAgentRunner}. Exported
203
- * (with the `ɵ` internal-API prefix) so tests can inspect the exact store the
204
- * runner writes to; not part of the public API.
205
- */
206
- const ɵGLOBAL_STORE = new ɵBoundedThreadStore(ɵINMEMORY_DEFAULTS);
207
- const sharedStore = ɵGLOBAL_STORE;
21
+ const GLOBAL_STORE = /* @__PURE__ */ new Map();
208
22
  var InMemoryAgentRunner = class extends AgentRunner {
209
- /**
210
- * @param limits Optional bounds for the process-global in-memory store. Omit
211
- * for safe defaults ({@link ɵINMEMORY_DEFAULTS}). When multiple runners are
212
- * constructed with differing limits, the last-constructed wins — in practice
213
- * the OSS/SSE default construction passes nothing. If a second (or later)
214
- * runner is constructed with limits that DIFFER from an already-customized
215
- * store, a one-time `console.warn` is emitted to signal that the shared store's
216
- * bounds are being clobbered for ALL in-memory threads.
217
- */
218
- constructor(limits) {
219
- super();
23
+ constructor(..._args) {
24
+ super(..._args);
220
25
  this.ɵsupportsLocalThreadEndpoints = true;
221
- if (limits) sharedStore.setLimits({
222
- ...ɵINMEMORY_DEFAULTS,
223
- ...limits
224
- });
225
26
  }
226
27
  run(request) {
227
- const store = sharedStore.getOrCreate(request.threadId);
28
+ let existingStore = GLOBAL_STORE.get(request.threadId);
29
+ if (!existingStore) {
30
+ existingStore = new InMemoryEventStore(request.threadId);
31
+ GLOBAL_STORE.set(request.threadId, existingStore);
32
+ }
33
+ const store = existingStore;
228
34
  if (store.isRunning) throw new Error("Thread already running");
229
35
  store.isRunning = true;
230
36
  store.currentRunId = request.input.runId;
@@ -286,7 +92,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
286
92
  }
287
93
  if (store.currentRunId) {
288
94
  const compactedEvents = compactEvents(currentRunEvents);
289
- sharedStore.appendRun(request.threadId, {
95
+ store.historicRuns.push({
290
96
  threadId: request.threadId,
291
97
  runId: store.currentRunId,
292
98
  agentId: request.agent.agentId ?? "default",
@@ -304,7 +110,6 @@ var InMemoryAgentRunner = class extends AgentRunner {
304
110
  store.isRunning = false;
305
111
  runSubject.complete();
306
112
  nextSubject.complete();
307
- store.subject = null;
308
113
  } catch (error) {
309
114
  const interruptionMessage = error instanceof Error ? error.message : String(error);
310
115
  const appendedEvents = finalizeRunEvents(currentRunEvents, {
@@ -317,7 +122,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
317
122
  }
318
123
  if (store.currentRunId && currentRunEvents.length > 0) {
319
124
  const compactedEvents = compactEvents(currentRunEvents);
320
- sharedStore.appendRun(request.threadId, {
125
+ store.historicRuns.push({
321
126
  threadId: request.threadId,
322
127
  runId: store.currentRunId,
323
128
  agentId: request.agent.agentId ?? "default",
@@ -335,7 +140,6 @@ var InMemoryAgentRunner = class extends AgentRunner {
335
140
  store.isRunning = false;
336
141
  runSubject.complete();
337
142
  nextSubject.complete();
338
- store.subject = null;
339
143
  }
340
144
  };
341
145
  if (prevSubject) prevSubject.subscribe({
@@ -347,7 +151,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
347
151
  return runSubject.asObservable();
348
152
  }
349
153
  connect(request) {
350
- const store = sharedStore.get(request.threadId, { touch: true });
154
+ const store = GLOBAL_STORE.get(request.threadId);
351
155
  const connectionSubject = new ReplaySubject(Infinity);
352
156
  if (!store) {
353
157
  connectionSubject.complete();
@@ -373,11 +177,11 @@ var InMemoryAgentRunner = class extends AgentRunner {
373
177
  return connectionSubject.asObservable();
374
178
  }
375
179
  isRunning(request) {
376
- const store = sharedStore.peek(request.threadId);
180
+ const store = GLOBAL_STORE.get(request.threadId);
377
181
  return Promise.resolve(store?.isRunning ?? false);
378
182
  }
379
183
  stop(request) {
380
- const store = sharedStore.peek(request.threadId);
184
+ const store = GLOBAL_STORE.get(request.threadId);
381
185
  if (!store || !store.isRunning) return Promise.resolve(false);
382
186
  if (store.stopRequested) return Promise.resolve(false);
383
187
  store.stopRequested = true;
@@ -406,7 +210,23 @@ var InMemoryAgentRunner = class extends AgentRunner {
406
210
  * `ThreadRecord` so the HTTP handler can use the same response envelope.
407
211
  */
408
212
  listThreads() {
409
- return sharedStore.listThreads();
213
+ const threads = [];
214
+ for (const [threadId, store] of GLOBAL_STORE) {
215
+ if (store.historicRuns.length === 0) continue;
216
+ const firstRun = store.historicRuns[0];
217
+ const lastRun = store.historicRuns[store.historicRuns.length - 1];
218
+ threads.push({
219
+ id: threadId,
220
+ name: null,
221
+ agentId: lastRun.agentId,
222
+ organizationId: "",
223
+ createdById: "",
224
+ archived: false,
225
+ createdAt: new Date(firstRun.createdAt).toISOString(),
226
+ updatedAt: new Date(lastRun.createdAt).toISOString()
227
+ });
228
+ }
229
+ return threads.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
410
230
  }
411
231
  /**
412
232
  * Returns all messages for a thread, using the snapshot captured at the end
@@ -418,9 +238,9 @@ var InMemoryAgentRunner = class extends AgentRunner {
418
238
  * with the Intelligence platform's `ThreadMessage` type.
419
239
  */
420
240
  getThreadMessages(threadId) {
421
- const store = sharedStore.peek(threadId);
422
- if (!store) return [];
423
- return [...store.messagesSnapshot];
241
+ const store = GLOBAL_STORE.get(threadId);
242
+ if (!store || store.historicRuns.length === 0) return [];
243
+ return store.historicRuns[store.historicRuns.length - 1].messages;
424
244
  }
425
245
  /**
426
246
  * Returns all AG-UI events for a thread, compacted across historic runs.
@@ -431,7 +251,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
431
251
  * late-joining inspector sees matches what this method returns.
432
252
  */
433
253
  getThreadEvents(threadId) {
434
- const store = sharedStore.peek(threadId);
254
+ const store = GLOBAL_STORE.get(threadId);
435
255
  if (!store || store.historicRuns.length === 0) return [];
436
256
  const all = [];
437
257
  for (const run of store.historicRuns) all.push(...run.events);
@@ -453,7 +273,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
453
273
  const event = events[i];
454
274
  if (event.type === EventType.STATE_SNAPSHOT) {
455
275
  const snapshot = event.snapshot;
456
- if (snapshot && typeof snapshot === "object" && !Array.isArray(snapshot)) return { ...snapshot };
276
+ if (snapshot && typeof snapshot === "object") return snapshot;
457
277
  return null;
458
278
  }
459
279
  }
@@ -469,10 +289,10 @@ var InMemoryAgentRunner = class extends AgentRunner {
469
289
  * not be wiped this way.
470
290
  */
471
291
  clearThreads() {
472
- sharedStore.clear();
292
+ GLOBAL_STORE.clear();
473
293
  }
474
294
  };
475
295
 
476
296
  //#endregion
477
- export { InMemoryAgentRunner, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵestimateBytes };
297
+ export { InMemoryAgentRunner };
478
298
  //# sourceMappingURL=in-memory.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"in-memory.mjs","names":[],"sources":["../../../../src/v2/runtime/runner/in-memory.ts"],"sourcesContent":["import type {\n AgentRunnerConnectRequest,\n AgentRunnerIsRunningRequest,\n AgentRunnerRunRequest,\n} from \"./agent-runner\";\nimport { AgentRunner } from \"./agent-runner\";\nimport type { AgentRunnerStopRequest } from \"./agent-runner\";\nimport type { Observable } from \"rxjs\";\nimport { ReplaySubject } from \"rxjs\";\nimport type {\n AbstractAgent,\n BaseEvent,\n Message,\n RunStartedEvent,\n StateSnapshotEvent,\n} from \"@ag-ui/client\";\nimport { EventType, compactEvents } from \"@ag-ui/client\";\nimport { finalizeRunEvents } from \"@copilotkit/shared\";\n\nexport interface InMemoryLimits {\n /** LRU cap on distinct threads. */\n maxThreads?: number;\n /** FIFO cap on runs kept per thread. `Infinity` or `0` disables the cap. */\n maxRunsPerThread?: number;\n /**\n * Approximate byte ceiling on RETAINED thread/run history. Enforced at run\n * completion (in `appendRun`), where LRU non-running threads are evicted to\n * keep the total under this limit.\n *\n * Limitation: this bounds only history that has already been committed. A\n * single in-flight run's buffered events (`currentRunEvents` and the two\n * `ReplaySubject<BaseEvent>(Infinity)` buffers in `run()`) are NOT counted\n * until that run completes, so `maxBytes` does not bound a single runaway\n * run mid-stream.\n *\n * Limitation: byte eviction drops only other LRU non-running threads and\n * never self-evicts the active/just-appended thread, so a single dominant\n * thread's own retained history is not byte-trimmed (bounded only by\n * `maxRunsPerThread`). `maxBytes` is thus a cross-thread ceiling enforced by\n * evicting OTHER threads, not a per-thread cap.\n */\n maxBytes?: number;\n}\n\nexport const ɵINMEMORY_DEFAULTS: Required<InMemoryLimits> = {\n maxThreads: 1000,\n maxRunsPerThread: 100,\n maxBytes: 512 * 1024 ** 2,\n};\n\nconst EVICTION_GUIDANCE =\n \"[CopilotKit] InMemoryAgentRunner evicted in-memory thread history to stay \" +\n \"under memory limits. This runner is bounded and non-durable by design. For \" +\n \"durable or production threads, configure an Intelligence backend.\";\n\nconst LIMITS_CLOBBER_GUIDANCE =\n \"[CopilotKit] InMemoryAgentRunner was constructed with in-memory limits that \" +\n \"differ from the already-configured process-global store; the last-constructed \" +\n \"runner's limits apply to ALL in-memory threads (the store is shared per-process). \" +\n \"Configure a single consistent set of limits, or use an Intelligence backend for \" +\n \"isolated bounds.\";\n\n/**\n * Best-effort approximate byte size of a value, via serialized length.\n * Never throws — returns 0 when the value cannot be serialized. This is an\n * approximation (UTF-16 length, not exact heap bytes), used only for relative\n * accounting against `maxBytes`.\n */\nexport function ɵestimateBytes(value: unknown): number {\n try {\n return JSON.stringify(value)?.length ?? 0;\n } catch {\n return 0;\n }\n}\n\ninterface HistoricRun {\n threadId: string;\n runId: string;\n /** ID of the agent that executed this run. */\n agentId: string;\n parentRunId: string | null;\n events: BaseEvent[];\n /**\n * Snapshot of all messages (input + generated) at the end of this run, as\n * passed in by the caller. NOTE: `BoundedThreadStore.appendRun` moves this\n * snapshot to the THREAD level (`InMemoryEventStore.messagesSnapshot`) and\n * clears this field to `[]`, so a stored HistoricRun never carries messages.\n * The thread-messages fallback reads the thread-level snapshot, not this.\n */\n messages: Message[];\n createdAt: number;\n /** Approximate retained byte size of `events`; set by BoundedThreadStore at append. */\n approxEventBytes?: number;\n /**\n * Legacy field retained for shape compatibility. `appendRun` always zeroes it\n * because message bytes are accounted at the thread level, not per run.\n */\n approxMessageBytes?: number;\n}\n\n/**\n * Lightweight thread summary returned by {@link InMemoryAgentRunner.listThreads}.\n * Shape matches the Intelligence platform's ThreadRecord so the same HTTP\n * response envelope can be used for both backends.\n */\nexport interface InMemoryThread {\n id: string;\n name: string | null;\n agentId: string;\n organizationId: \"\"; // always empty in in-memory mode\n createdById: \"\"; // always empty in in-memory mode\n archived: false; // always false in in-memory mode\n createdAt: string;\n updatedAt: string;\n}\n\nclass InMemoryEventStore {\n constructor(public threadId: string) {}\n\n /** The subject that current consumers subscribe to. */\n subject: ReplaySubject<BaseEvent> | null = null;\n\n /** True while a run is actively producing events. */\n isRunning = false;\n\n /** Current run ID */\n currentRunId: string | null = null;\n\n /** Historic completed runs */\n historicRuns: HistoricRun[] = [];\n\n /** Currently running agent instance (if any). */\n agent: AbstractAgent | null = null;\n\n /** Subject returned from run() while the run is active. */\n runSubject: ReplaySubject<BaseEvent> | null = null;\n\n /** True once stop() has been requested but the run has not yet finalized. */\n stopRequested = false;\n\n /** Reference to the events emitted in the current run. */\n currentEvents: BaseEvent[] | null = null;\n\n /**\n * The thread's single latest NON-EMPTY message snapshot, held at the THREAD\n * level (independent of `historicRuns` lifecycle). Decoupling the snapshot\n * from per-run storage means run-cap FIFO eviction and interleaved\n * empty-snapshot runs can never drop or pin the thread's message history.\n */\n messagesSnapshot: Message[] = [];\n\n /** Approximate retained byte size of `messagesSnapshot`. */\n approxMessagesSnapshotBytes = 0;\n}\n\nexport class ɵBoundedThreadStore {\n private readonly map = new Map<string, InMemoryEventStore>();\n private totalBytes = 0;\n private warned = false;\n /** True once limits have been EXPLICITLY set (via setLimits), not just the constructor default. */\n private limitsExplicitlySet = false;\n /** Warn-once latch for the clobber warning, kept distinct from the eviction `warned` latch. */\n private clobberWarned = false;\n\n constructor(private limits: Required<InMemoryLimits>) {}\n\n get byteTotal(): number {\n return this.totalBytes;\n }\n\n /**\n * Reconfigure the process-global store's bounds. Called by the\n * {@link InMemoryAgentRunner} constructor when limits are passed. Because the\n * store is a per-process singleton, this replaces the bounds for ALL in-memory\n * threads. Emits {@link LIMITS_CLOBBER_GUIDANCE} at most ONCE per store when a\n * SECOND (or later) explicit set arrives whose resolved values differ from the\n * prior explicit set — i.e. a genuine clobber of an already-customized config.\n * The first explicit customization (defaults → custom) is the intended\n * override and never warns; identical re-sets never warn.\n */\n setLimits(limits: Required<InMemoryLimits>): void {\n if (\n this.limitsExplicitlySet &&\n !this.clobberWarned &&\n (limits.maxThreads !== this.limits.maxThreads ||\n limits.maxRunsPerThread !== this.limits.maxRunsPerThread ||\n limits.maxBytes !== this.limits.maxBytes)\n ) {\n this.clobberWarned = true;\n try {\n console.warn(LIMITS_CLOBBER_GUIDANCE);\n } catch {\n // best-effort: logging must never break construction\n }\n }\n this.limitsExplicitlySet = true;\n this.limits = limits;\n }\n\n get size(): number {\n return this.map.size;\n }\n\n /** Re-insert at the tail so Map iteration order stays LRU-first. */\n private touchOrder(threadId: string, store: InMemoryEventStore): void {\n this.map.delete(threadId);\n this.map.set(threadId, store);\n }\n\n getOrCreate(threadId: string): InMemoryEventStore {\n const existing = this.map.get(threadId);\n if (existing) {\n this.touchOrder(threadId, existing);\n return existing;\n }\n const store = new InMemoryEventStore(threadId);\n this.map.set(threadId, store);\n this.evictThreadsIfNeeded(threadId);\n return store;\n }\n\n get(\n threadId: string,\n opts: { touch: boolean },\n ): InMemoryEventStore | undefined {\n const store = this.map.get(threadId);\n if (store && opts.touch) this.touchOrder(threadId, store);\n return store;\n }\n\n peek(threadId: string): InMemoryEventStore | undefined {\n return this.map.get(threadId);\n }\n\n /**\n * Evict the least-recently-used thread that is neither running NOR\n * mid-finalization. Returns false if none evictable. The `protect` thread\n * (typically the one just created) is never evicted, so a fresh thread is not\n * immediately dropped when it is the only non-running candidate.\n *\n * A thread is skipped while `isRunning` OR `stopRequested` is set.\n * `stop()` flips `isRunning` to false the moment it aborts the agent, but the\n * run keeps finalizing asynchronously (the abort trips the `catch` in\n * `runAgent`, which later calls `appendRun`). During that window\n * `stopRequested` stays true; evicting the thread then would make the pending\n * `appendRun` hit `if (!store) return` and silently drop the aborted run's\n * history. Guarding on `stopRequested` keeps the thread alive until\n * finalization completes.\n */\n private evictOneLru(protect?: string): boolean {\n for (const [threadId, store] of this.map) {\n if (threadId === protect) continue; // never evict the just-created thread\n // never evict a running or still-finalizing (stop-requested) thread\n if (store.isRunning || store.stopRequested) continue;\n this.removeThread(threadId, store);\n this.noteEviction();\n return true;\n }\n return false;\n }\n\n appendRun(threadId: string, run: HistoricRun): void {\n const store = this.map.get(threadId);\n if (!store) return; // best-effort: nothing to append to\n\n // Thread-level message snapshot: keep the single latest NON-EMPTY snapshot\n // on the store, decoupled from `historicRuns`. When the incoming run\n // carries a non-empty snapshot, replace the thread's snapshot (adjusting\n // byte accounting). When it's empty (non-array `agent.messages` or an\n // error-path run), leave the existing thread snapshot untouched so history\n // is never lost. The snapshot never lives on a HistoricRun, so run-cap FIFO\n // eviction can never drop it and an interleaved empty run can never pin it.\n if (run.messages.length > 0) {\n this.totalBytes -= store.approxMessagesSnapshotBytes;\n // Store the incoming array directly (SHALLOW, array-level copy). `run.messages`\n // is already a fresh `[...agent.messages]` array created in run(), so we own the\n // array and it is decoupled from `agent.messages` at the array level (push/splice\n // on the agent's array cannot mutate our snapshot). We deliberately do NOT deep-copy\n // here: `structuredClone` throws DataCloneError on a non-cloneable message field,\n // which would wedge the thread and hang SSE — inconsistent with `ɵestimateBytes`,\n // which tolerates the same bad-payload class. The tradeoff is that the inner\n // `Message` objects remain shared by reference with `agent.messages`, so an agent\n // that mutates its own message objects IN PLACE after the run can still be observed\n // through this snapshot. That inner-object isolation is a known limitation tracked as\n // follow-up; callers must treat returned messages as read-only. Estimate bytes on the\n // same value so accounting matches exactly what is retained.\n store.messagesSnapshot = run.messages;\n store.approxMessagesSnapshotBytes = ɵestimateBytes(run.messages);\n this.totalBytes += store.approxMessagesSnapshotBytes;\n }\n\n // Do not carry message bytes on the HistoricRun: the snapshot is now tracked\n // at the thread level, so historicRuns must never account message bytes.\n run.messages = [];\n run.approxMessageBytes = 0;\n\n // Compute this run's approximate event size once, at append time.\n run.approxEventBytes = ɵestimateBytes(run.events);\n store.historicRuns.push(run);\n this.totalBytes += run.approxEventBytes;\n this.touchOrder(threadId, store);\n\n this.enforceRunCap(store);\n this.evictByBytesIfNeeded(threadId);\n }\n\n private enforceRunCap(store: InMemoryEventStore): void {\n const cap = this.limits.maxRunsPerThread;\n if (!cap || cap === Infinity) return; // 0 or Infinity → disabled\n while (store.historicRuns.length > cap) {\n const dropped = store.historicRuns.shift()!;\n // Only event bytes live on a HistoricRun; the message snapshot is tracked\n // at the thread level and survives run-cap eviction.\n this.totalBytes -= dropped.approxEventBytes ?? 0;\n }\n }\n\n /**\n * Trim the store back under the byte ceiling by evicting LRU non-running\n * threads. `protect` (the just-appended thread) is never self-evicted, so a\n * fresh run pushes OTHER threads out rather than dropping itself.\n */\n private evictByBytesIfNeeded(protect?: string): void {\n while (this.totalBytes > this.limits.maxBytes) {\n if (!this.evictOneLru(protect)) break; // only protected/running threads left → accept overage\n }\n }\n\n private removeThread(threadId: string, store: InMemoryEventStore): void {\n for (const run of store.historicRuns) {\n this.totalBytes -= run.approxEventBytes ?? 0;\n }\n // The thread's message snapshot is tracked at the store level, so it must\n // be reclaimed here in addition to the per-run event bytes.\n this.totalBytes -= store.approxMessagesSnapshotBytes;\n this.map.delete(threadId);\n }\n\n private evictThreadsIfNeeded(protect?: string): void {\n while (this.map.size > this.limits.maxThreads) {\n if (!this.evictOneLru(protect)) break; // everything evictable is running → accept overage\n }\n }\n\n private noteEviction(): void {\n if (this.warned) return;\n this.warned = true;\n try {\n console.warn(EVICTION_GUIDANCE);\n } catch {\n // best-effort: logging must never break a run\n }\n }\n\n listThreads(): InMemoryThread[] {\n const threads: InMemoryThread[] = [];\n for (const [threadId, store] of this.map) {\n if (store.historicRuns.length === 0) continue;\n const firstRun = store.historicRuns[0]!;\n const lastRun = store.historicRuns[store.historicRuns.length - 1]!;\n threads.push({\n id: threadId,\n name: null,\n agentId: lastRun.agentId,\n organizationId: \"\",\n createdById: \"\",\n archived: false,\n createdAt: new Date(firstRun.createdAt).toISOString(),\n updatedAt: new Date(lastRun.createdAt).toISOString(),\n });\n }\n return threads.sort(\n (a, b) =>\n new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(),\n );\n }\n\n clear(): void {\n this.map.clear();\n this.totalBytes = 0;\n this.warned = false;\n }\n}\n\n/**\n * Process-wide singleton backing every {@link InMemoryAgentRunner}. Exported\n * (with the `ɵ` internal-API prefix) so tests can inspect the exact store the\n * runner writes to; not part of the public API.\n */\nexport const ɵGLOBAL_STORE = new ɵBoundedThreadStore(ɵINMEMORY_DEFAULTS);\nconst sharedStore = ɵGLOBAL_STORE;\n\nexport class InMemoryAgentRunner extends AgentRunner {\n readonly ɵsupportsLocalThreadEndpoints = true;\n\n /**\n * @param limits Optional bounds for the process-global in-memory store. Omit\n * for safe defaults ({@link ɵINMEMORY_DEFAULTS}). When multiple runners are\n * constructed with differing limits, the last-constructed wins — in practice\n * the OSS/SSE default construction passes nothing. If a second (or later)\n * runner is constructed with limits that DIFFER from an already-customized\n * store, a one-time `console.warn` is emitted to signal that the shared store's\n * bounds are being clobbered for ALL in-memory threads.\n */\n constructor(limits?: InMemoryLimits) {\n super();\n if (limits) {\n sharedStore.setLimits({ ...ɵINMEMORY_DEFAULTS, ...limits });\n }\n }\n\n run(request: AgentRunnerRunRequest): Observable<BaseEvent> {\n const store = sharedStore.getOrCreate(request.threadId);\n\n if (store.isRunning) {\n throw new Error(\"Thread already running\");\n }\n store.isRunning = true;\n store.currentRunId = request.input.runId;\n store.agent = request.agent;\n store.stopRequested = false;\n\n // Track seen message IDs and current run events for this run\n const seenMessageIds = new Set<string>();\n const currentRunEvents: BaseEvent[] = [];\n store.currentEvents = currentRunEvents;\n\n // Get all previously seen message IDs from historic runs\n const historicMessageIds = new Set<string>();\n for (const run of store.historicRuns) {\n for (const event of run.events) {\n if (\"messageId\" in event && typeof event.messageId === \"string\") {\n historicMessageIds.add(event.messageId);\n }\n if (event.type === EventType.RUN_STARTED) {\n const runStarted = event as RunStartedEvent;\n const messages = runStarted.input?.messages ?? [];\n for (const message of messages) {\n historicMessageIds.add(message.id);\n }\n }\n }\n }\n\n const nextSubject = new ReplaySubject<BaseEvent>(Infinity);\n const prevSubject = store.subject;\n\n // Update the store's subject immediately\n store.subject = nextSubject;\n\n // Create a subject for run() return value\n const runSubject = new ReplaySubject<BaseEvent>(Infinity);\n store.runSubject = runSubject;\n\n // Helper function to run the agent and handle errors\n const runAgent = async () => {\n // Get parent run ID for chaining\n const lastRun = store.historicRuns[store.historicRuns.length - 1];\n const parentRunId = lastRun?.runId ?? null;\n\n try {\n await request.agent.runAgent(request.input, {\n onEvent: ({ event }) => {\n let processedEvent: BaseEvent = event;\n if (event.type === EventType.RUN_STARTED) {\n const runStartedEvent = event as RunStartedEvent;\n if (!runStartedEvent.input) {\n const sanitizedMessages = request.input.messages\n ? request.input.messages.filter(\n (message) => !historicMessageIds.has(message.id),\n )\n : undefined;\n const updatedInput = {\n ...request.input,\n ...(sanitizedMessages !== undefined\n ? { messages: sanitizedMessages }\n : {}),\n };\n processedEvent = {\n ...runStartedEvent,\n input: updatedInput,\n } as RunStartedEvent;\n }\n }\n\n runSubject.next(processedEvent); // For run() return - only agent events\n nextSubject.next(processedEvent); // For connect() / store - all events\n currentRunEvents.push(processedEvent); // Accumulate for storage\n },\n onNewMessage: ({ message }) => {\n // Called for each new message\n if (!seenMessageIds.has(message.id)) {\n seenMessageIds.add(message.id);\n }\n },\n onRunStartedEvent: () => {\n // Mark any messages from the input as seen so they aren't emitted twice\n if (request.input.messages) {\n for (const message of request.input.messages) {\n if (!seenMessageIds.has(message.id)) {\n seenMessageIds.add(message.id);\n }\n }\n }\n },\n });\n\n const appendedEvents = finalizeRunEvents(currentRunEvents, {\n stopRequested: store.stopRequested,\n });\n for (const event of appendedEvents) {\n runSubject.next(event);\n nextSubject.next(event);\n }\n\n // Store the completed run in memory with ONLY its events\n if (store.currentRunId) {\n // Compact the events before storing (like SQLite does)\n const compactedEvents = compactEvents(currentRunEvents);\n\n sharedStore.appendRun(request.threadId, {\n threadId: request.threadId,\n runId: store.currentRunId,\n agentId: request.agent.agentId ?? \"default\",\n parentRunId,\n events: compactedEvents,\n // Snapshot all messages (input + generated) for the thread-messages endpoint\n messages: Array.isArray(request.agent.messages)\n ? [...request.agent.messages]\n : [],\n createdAt: Date.now(),\n });\n }\n\n // Complete the run\n store.currentEvents = null;\n store.currentRunId = null;\n store.agent = null;\n store.runSubject = null;\n store.stopRequested = false;\n store.isRunning = false;\n runSubject.complete();\n nextSubject.complete();\n // Time-scoped release: events are now in historicRuns, so the infinite\n // ReplaySubject buffers are pure duplication — drop them so they become\n // collectable. connect() only subscribes to store.subject while\n // isRunning || stopRequested (both false here), and rebuilds history\n // from historicRuns, so this is safe.\n store.subject = null;\n } catch (error) {\n const interruptionMessage =\n error instanceof Error ? error.message : String(error);\n const appendedEvents = finalizeRunEvents(currentRunEvents, {\n stopRequested: store.stopRequested,\n interruptionMessage,\n });\n for (const event of appendedEvents) {\n runSubject.next(event);\n nextSubject.next(event);\n }\n\n // Store the run even if it failed (partial events)\n if (store.currentRunId && currentRunEvents.length > 0) {\n // Compact the events before storing (like SQLite does)\n const compactedEvents = compactEvents(currentRunEvents);\n sharedStore.appendRun(request.threadId, {\n threadId: request.threadId,\n runId: store.currentRunId,\n agentId: request.agent.agentId ?? \"default\",\n parentRunId,\n events: compactedEvents,\n messages: Array.isArray(request.agent.messages)\n ? [...request.agent.messages]\n : [],\n createdAt: Date.now(),\n });\n }\n\n // Complete the run\n store.currentEvents = null;\n store.currentRunId = null;\n store.agent = null;\n store.runSubject = null;\n store.stopRequested = false;\n store.isRunning = false;\n runSubject.complete();\n nextSubject.complete();\n // Time-scoped release: events are now in historicRuns, so the infinite\n // ReplaySubject buffers are pure duplication — drop them so they become\n // collectable. connect() only subscribes to store.subject while\n // isRunning || stopRequested (both false here), and rebuilds history\n // from historicRuns, so this is safe.\n store.subject = null;\n }\n };\n\n // Bridge previous events if they exist\n if (prevSubject) {\n prevSubject.subscribe({\n next: (e) => nextSubject.next(e),\n error: (err) => nextSubject.error(err),\n complete: () => {\n // Don't complete nextSubject here - it needs to stay open for new events\n },\n });\n }\n\n // Start the agent execution immediately (not lazily)\n runAgent();\n\n // Return the run subject (only agent events, no injected messages)\n return runSubject.asObservable();\n }\n\n connect(request: AgentRunnerConnectRequest): Observable<BaseEvent> {\n const store = sharedStore.get(request.threadId, { touch: true });\n const connectionSubject = new ReplaySubject<BaseEvent>(Infinity);\n\n if (!store) {\n // No store means no events\n connectionSubject.complete();\n return connectionSubject.asObservable();\n }\n\n // Collect all historic events from memory\n const allHistoricEvents: BaseEvent[] = [];\n for (const run of store.historicRuns) {\n allHistoricEvents.push(...run.events);\n }\n\n // Apply compaction to all historic events together (like SQLite)\n const compactedEvents = compactEvents(allHistoricEvents);\n\n // Emit compacted events and track message IDs\n const emittedMessageIds = new Set<string>();\n for (const event of compactedEvents) {\n connectionSubject.next(event);\n if (\"messageId\" in event && typeof event.messageId === \"string\") {\n emittedMessageIds.add(event.messageId);\n }\n }\n\n // Bridge active run to connection if exists\n if (store.subject && (store.isRunning || store.stopRequested)) {\n store.subject.subscribe({\n next: (event) => {\n // Skip message events that we've already emitted from historic\n if (\n \"messageId\" in event &&\n typeof event.messageId === \"string\" &&\n emittedMessageIds.has(event.messageId)\n ) {\n return;\n }\n connectionSubject.next(event);\n },\n complete: () => connectionSubject.complete(),\n error: (err) => connectionSubject.error(err),\n });\n } else {\n // No active run, complete after historic events\n connectionSubject.complete();\n }\n\n return connectionSubject.asObservable();\n }\n\n isRunning(request: AgentRunnerIsRunningRequest): Promise<boolean> {\n const store = sharedStore.peek(request.threadId);\n return Promise.resolve(store?.isRunning ?? false);\n }\n\n stop(request: AgentRunnerStopRequest): Promise<boolean | undefined> {\n const store = sharedStore.peek(request.threadId);\n if (!store || !store.isRunning) {\n return Promise.resolve(false);\n }\n if (store.stopRequested) {\n return Promise.resolve(false);\n }\n\n store.stopRequested = true;\n store.isRunning = false;\n\n const agent = store.agent;\n if (!agent) {\n store.stopRequested = false;\n store.isRunning = false;\n return Promise.resolve(false);\n }\n\n try {\n agent.abortRun();\n return Promise.resolve(true);\n } catch (error) {\n console.error(\"Failed to abort agent run\", error);\n store.stopRequested = false;\n store.isRunning = true;\n return Promise.resolve(false);\n }\n }\n\n /**\n * Returns a summary of every thread that has been run through this runner.\n *\n * This powers the local-dev fallback for `GET /threads` when the Intelligence\n * platform is not configured. Each entry mirrors the shape of a platform\n * `ThreadRecord` so the HTTP handler can use the same response envelope.\n */\n listThreads(): InMemoryThread[] {\n return sharedStore.listThreads();\n }\n\n /**\n * Returns all messages for a thread, using the snapshot captured at the end\n * of the most recent run.\n *\n * This powers the local-dev fallback for `GET /threads/:threadId/messages`\n * when the Intelligence platform is not configured. The returned `Message[]`\n * objects come directly from the ag-ui agent, so their shape is compatible\n * with the Intelligence platform's `ThreadMessage` type.\n */\n getThreadMessages(threadId: string): Message[] {\n const store = sharedStore.peek(threadId);\n if (!store) return [];\n // The thread's latest non-empty snapshot is held at the store level,\n // independent of `historicRuns` lifecycle, so run-cap eviction and\n // interleaved empty-snapshot runs can never lose it. Return a SHALLOW\n // (array-level) copy: a fresh array so a caller mutating array STRUCTURE\n // (push/splice/reassign elements) cannot affect the stored snapshot. We\n // deliberately do NOT deep-copy: `structuredClone` throws DataCloneError on a\n // non-cloneable message field, which would wedge the thread and hang SSE —\n // inconsistent with `ɵestimateBytes`, which tolerates the same bad-payload class.\n // The tradeoff is that the inner `Message` objects remain shared by reference with\n // the stored snapshot, so mutating a returned message's FIELD\n // (e.g. `getThreadMessages(t)[0].content = \"x\"`) is NOT isolated and would corrupt\n // the stored snapshot. That inner-object isolation is a known limitation tracked as\n // follow-up; callers must treat returned messages as read-only.\n return [...store.messagesSnapshot];\n }\n\n /**\n * Returns all AG-UI events for a thread, compacted across historic runs.\n *\n * Powers the local-dev fallback for `GET /threads/:threadId/events` when the\n * Intelligence platform is not configured. The compaction logic matches\n * the connection-replay path in {@link connect}, so the stream a\n * late-joining inspector sees matches what this method returns.\n */\n getThreadEvents(threadId: string): BaseEvent[] {\n const store = sharedStore.peek(threadId);\n if (!store || store.historicRuns.length === 0) return [];\n const all: BaseEvent[] = [];\n for (const run of store.historicRuns) all.push(...run.events);\n return compactEvents(all);\n }\n\n /**\n * Returns the agent state snapshot for a thread.\n *\n * Derived from the last `STATE_SNAPSHOT` in the compacted event stream. The\n * AG-UI `compactEvents` helper consolidates STATE_DELTA events and produces\n * a single trailing STATE_SNAPSHOT when state changes exist, so this is a\n * faithful view of state at the end of the most recent run.\n *\n * Returns `null` when the thread has never emitted a STATE_SNAPSHOT.\n */\n getThreadState(threadId: string): Record<string, unknown> | null {\n const events = this.getThreadEvents(threadId);\n // Walk backwards — the last snapshot wins.\n for (let i = events.length - 1; i >= 0; i--) {\n const event = events[i]!;\n if (event.type === EventType.STATE_SNAPSHOT) {\n const snapshot = (event as StateSnapshotEvent).snapshot;\n // Only plain objects satisfy the Record<string, unknown> contract.\n // `typeof [] === \"object\"` is true, so arrays must be rejected\n // explicitly to avoid returning an array typed as a Record.\n if (\n snapshot &&\n typeof snapshot === \"object\" &&\n !Array.isArray(snapshot)\n ) {\n // Return a defensive shallow copy so callers can't mutate the\n // snapshot object held inside the stored event (matches the\n // getThreadMessages defensive-copy approach).\n return { ...(snapshot as Record<string, unknown>) };\n }\n return null;\n }\n }\n return null;\n }\n\n /**\n * Clears all in-memory thread history.\n *\n * Powers the local-dev fallback for `POST /threads/clear`, letting consumers\n * (e.g. the demo's Clear button) reset to an empty thread list without\n * restarting the runtime. Intentionally not exposed on the Intelligence\n * platform path: there, thread history lives in a real database and must\n * not be wiped this way.\n */\n clearThreads(): void {\n sharedStore.clear();\n }\n}\n"],"mappings":";;;;;;;AA4CA,MAAa,qBAA+C;CAC1D,YAAY;CACZ,kBAAkB;CAClB,UAAU,MAAM,QAAQ;CACzB;AAED,MAAM,oBACJ;AAIF,MAAM,0BACJ;;;;;;;AAYF,SAAgB,eAAe,OAAwB;AACrD,KAAI;AACF,SAAO,KAAK,UAAU,MAAM,EAAE,UAAU;SAClC;AACN,SAAO;;;AA6CX,IAAM,qBAAN,MAAyB;CACvB,YAAY,AAAO,UAAkB;EAAlB;iBAGwB;mBAG/B;sBAGkB;sBAGA,EAAE;eAGF;oBAGgB;uBAG9B;uBAGoB;0BAQN,EAAE;qCAGF;;;AAGhC,IAAa,sBAAb,MAAiC;CAS/B,YAAY,AAAQ,QAAkC;EAAlC;6BARG,IAAI,KAAiC;oBACvC;gBACJ;6BAEa;uBAEN;;CAIxB,IAAI,YAAoB;AACtB,SAAO,KAAK;;;;;;;;;;;;CAad,UAAU,QAAwC;AAChD,MACE,KAAK,uBACL,CAAC,KAAK,kBACL,OAAO,eAAe,KAAK,OAAO,cACjC,OAAO,qBAAqB,KAAK,OAAO,oBACxC,OAAO,aAAa,KAAK,OAAO,WAClC;AACA,QAAK,gBAAgB;AACrB,OAAI;AACF,YAAQ,KAAK,wBAAwB;WAC/B;;AAIV,OAAK,sBAAsB;AAC3B,OAAK,SAAS;;CAGhB,IAAI,OAAe;AACjB,SAAO,KAAK,IAAI;;;CAIlB,AAAQ,WAAW,UAAkB,OAAiC;AACpE,OAAK,IAAI,OAAO,SAAS;AACzB,OAAK,IAAI,IAAI,UAAU,MAAM;;CAG/B,YAAY,UAAsC;EAChD,MAAM,WAAW,KAAK,IAAI,IAAI,SAAS;AACvC,MAAI,UAAU;AACZ,QAAK,WAAW,UAAU,SAAS;AACnC,UAAO;;EAET,MAAM,QAAQ,IAAI,mBAAm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,MAAM,QAAQ,YAAY,KAAK,SAAS;AACxC,MAAI,CAAC,SAAS,MAAM,aAAa,WAAW,EAAG,QAAO,EAAE;EACxD,MAAM,MAAmB,EAAE;AAC3B,OAAK,MAAM,OAAO,MAAM,aAAc,KAAI,KAAK,GAAG,IAAI,OAAO;AAC7D,SAAO,cAAc,IAAI;;;;;;;;;;;;CAa3B,eAAe,UAAkD;EAC/D,MAAM,SAAS,KAAK,gBAAgB,SAAS;AAE7C,OAAK,IAAI,IAAI,OAAO,SAAS,GAAG,KAAK,GAAG,KAAK;GAC3C,MAAM,QAAQ,OAAO;AACrB,OAAI,MAAM,SAAS,UAAU,gBAAgB;IAC3C,MAAM,WAAY,MAA6B;AAI/C,QACE,YACA,OAAO,aAAa,YACpB,CAAC,MAAM,QAAQ,SAAS,CAKxB,QAAO,EAAE,GAAI,UAAsC;AAErD,WAAO;;;AAGX,SAAO;;;;;;;;;;;CAYT,eAAqB;AACnB,cAAY,OAAO"}
1
+ {"version":3,"file":"in-memory.mjs","names":[],"sources":["../../../../src/v2/runtime/runner/in-memory.ts"],"sourcesContent":["import type {\n AgentRunnerConnectRequest,\n AgentRunnerIsRunningRequest,\n AgentRunnerRunRequest,\n} from \"./agent-runner\";\nimport { AgentRunner } from \"./agent-runner\";\nimport type { AgentRunnerStopRequest } from \"./agent-runner\";\nimport type { Observable } from \"rxjs\";\nimport { ReplaySubject } from \"rxjs\";\nimport type {\n AbstractAgent,\n BaseEvent,\n Message,\n RunStartedEvent,\n StateSnapshotEvent,\n} from \"@ag-ui/client\";\nimport { EventType, compactEvents } from \"@ag-ui/client\";\nimport { finalizeRunEvents } from \"@copilotkit/shared\";\n\ninterface HistoricRun {\n threadId: string;\n runId: string;\n /** ID of the agent that executed this run. */\n agentId: string;\n parentRunId: string | null;\n events: BaseEvent[];\n /**\n * Snapshot of all messages (input + generated) at the end of this run.\n * Used by the local thread-messages fallback endpoint.\n */\n messages: Message[];\n createdAt: number;\n}\n\n/**\n * Lightweight thread summary returned by {@link InMemoryAgentRunner.listThreads}.\n * Shape matches the Intelligence platform's ThreadRecord so the same HTTP\n * response envelope can be used for both backends.\n */\nexport interface InMemoryThread {\n id: string;\n name: string | null;\n agentId: string;\n organizationId: \"\"; // always empty in in-memory mode\n createdById: \"\"; // always empty in in-memory mode\n archived: false; // always false in in-memory mode\n createdAt: string;\n updatedAt: string;\n}\n\nclass InMemoryEventStore {\n constructor(public threadId: string) {}\n\n /** The subject that current consumers subscribe to. */\n subject: ReplaySubject<BaseEvent> | null = null;\n\n /** True while a run is actively producing events. */\n isRunning = false;\n\n /** Current run ID */\n currentRunId: string | null = null;\n\n /** Historic completed runs */\n historicRuns: HistoricRun[] = [];\n\n /** Currently running agent instance (if any). */\n agent: AbstractAgent | null = null;\n\n /** Subject returned from run() while the run is active. */\n runSubject: ReplaySubject<BaseEvent> | null = null;\n\n /** True once stop() has been requested but the run has not yet finalized. */\n stopRequested = false;\n\n /** Reference to the events emitted in the current run. */\n currentEvents: BaseEvent[] | null = null;\n}\n\nconst GLOBAL_STORE = new Map<string, InMemoryEventStore>();\n\nexport class InMemoryAgentRunner extends AgentRunner {\n readonly ɵsupportsLocalThreadEndpoints = true;\n\n run(request: AgentRunnerRunRequest): Observable<BaseEvent> {\n let existingStore = GLOBAL_STORE.get(request.threadId);\n if (!existingStore) {\n existingStore = new InMemoryEventStore(request.threadId);\n GLOBAL_STORE.set(request.threadId, existingStore);\n }\n const store = existingStore; // Now store is const and non-null\n\n if (store.isRunning) {\n throw new Error(\"Thread already running\");\n }\n store.isRunning = true;\n store.currentRunId = request.input.runId;\n store.agent = request.agent;\n store.stopRequested = false;\n\n // Track seen message IDs and current run events for this run\n const seenMessageIds = new Set<string>();\n const currentRunEvents: BaseEvent[] = [];\n store.currentEvents = currentRunEvents;\n\n // Get all previously seen message IDs from historic runs\n const historicMessageIds = new Set<string>();\n for (const run of store.historicRuns) {\n for (const event of run.events) {\n if (\"messageId\" in event && typeof event.messageId === \"string\") {\n historicMessageIds.add(event.messageId);\n }\n if (event.type === EventType.RUN_STARTED) {\n const runStarted = event as RunStartedEvent;\n const messages = runStarted.input?.messages ?? [];\n for (const message of messages) {\n historicMessageIds.add(message.id);\n }\n }\n }\n }\n\n const nextSubject = new ReplaySubject<BaseEvent>(Infinity);\n const prevSubject = store.subject;\n\n // Update the store's subject immediately\n store.subject = nextSubject;\n\n // Create a subject for run() return value\n const runSubject = new ReplaySubject<BaseEvent>(Infinity);\n store.runSubject = runSubject;\n\n // Helper function to run the agent and handle errors\n const runAgent = async () => {\n // Get parent run ID for chaining\n const lastRun = store.historicRuns[store.historicRuns.length - 1];\n const parentRunId = lastRun?.runId ?? null;\n\n try {\n await request.agent.runAgent(request.input, {\n onEvent: ({ event }) => {\n let processedEvent: BaseEvent = event;\n if (event.type === EventType.RUN_STARTED) {\n const runStartedEvent = event as RunStartedEvent;\n if (!runStartedEvent.input) {\n const sanitizedMessages = request.input.messages\n ? request.input.messages.filter(\n (message) => !historicMessageIds.has(message.id),\n )\n : undefined;\n const updatedInput = {\n ...request.input,\n ...(sanitizedMessages !== undefined\n ? { messages: sanitizedMessages }\n : {}),\n };\n processedEvent = {\n ...runStartedEvent,\n input: updatedInput,\n } as RunStartedEvent;\n }\n }\n\n runSubject.next(processedEvent); // For run() return - only agent events\n nextSubject.next(processedEvent); // For connect() / store - all events\n currentRunEvents.push(processedEvent); // Accumulate for storage\n },\n onNewMessage: ({ message }) => {\n // Called for each new message\n if (!seenMessageIds.has(message.id)) {\n seenMessageIds.add(message.id);\n }\n },\n onRunStartedEvent: () => {\n // Mark any messages from the input as seen so they aren't emitted twice\n if (request.input.messages) {\n for (const message of request.input.messages) {\n if (!seenMessageIds.has(message.id)) {\n seenMessageIds.add(message.id);\n }\n }\n }\n },\n });\n\n const appendedEvents = finalizeRunEvents(currentRunEvents, {\n stopRequested: store.stopRequested,\n });\n for (const event of appendedEvents) {\n runSubject.next(event);\n nextSubject.next(event);\n }\n\n // Store the completed run in memory with ONLY its events\n if (store.currentRunId) {\n // Compact the events before storing (like SQLite does)\n const compactedEvents = compactEvents(currentRunEvents);\n\n store.historicRuns.push({\n threadId: request.threadId,\n runId: store.currentRunId,\n agentId: request.agent.agentId ?? \"default\",\n parentRunId,\n events: compactedEvents,\n // Snapshot all messages (input + generated) for the thread-messages endpoint\n messages: Array.isArray(request.agent.messages)\n ? [...request.agent.messages]\n : [],\n createdAt: Date.now(),\n });\n }\n\n // Complete the run\n store.currentEvents = null;\n store.currentRunId = null;\n store.agent = null;\n store.runSubject = null;\n store.stopRequested = false;\n store.isRunning = false;\n runSubject.complete();\n nextSubject.complete();\n } catch (error) {\n const interruptionMessage =\n error instanceof Error ? error.message : String(error);\n const appendedEvents = finalizeRunEvents(currentRunEvents, {\n stopRequested: store.stopRequested,\n interruptionMessage,\n });\n for (const event of appendedEvents) {\n runSubject.next(event);\n nextSubject.next(event);\n }\n\n // Store the run even if it failed (partial events)\n if (store.currentRunId && currentRunEvents.length > 0) {\n // Compact the events before storing (like SQLite does)\n const compactedEvents = compactEvents(currentRunEvents);\n store.historicRuns.push({\n threadId: request.threadId,\n runId: store.currentRunId,\n agentId: request.agent.agentId ?? \"default\",\n parentRunId,\n events: compactedEvents,\n messages: Array.isArray(request.agent.messages)\n ? [...request.agent.messages]\n : [],\n createdAt: Date.now(),\n });\n }\n\n // Complete the run\n store.currentEvents = null;\n store.currentRunId = null;\n store.agent = null;\n store.runSubject = null;\n store.stopRequested = false;\n store.isRunning = false;\n runSubject.complete();\n nextSubject.complete();\n }\n };\n\n // Bridge previous events if they exist\n if (prevSubject) {\n prevSubject.subscribe({\n next: (e) => nextSubject.next(e),\n error: (err) => nextSubject.error(err),\n complete: () => {\n // Don't complete nextSubject here - it needs to stay open for new events\n },\n });\n }\n\n // Start the agent execution immediately (not lazily)\n runAgent();\n\n // Return the run subject (only agent events, no injected messages)\n return runSubject.asObservable();\n }\n\n connect(request: AgentRunnerConnectRequest): Observable<BaseEvent> {\n const store = GLOBAL_STORE.get(request.threadId);\n const connectionSubject = new ReplaySubject<BaseEvent>(Infinity);\n\n if (!store) {\n // No store means no events\n connectionSubject.complete();\n return connectionSubject.asObservable();\n }\n\n // Collect all historic events from memory\n const allHistoricEvents: BaseEvent[] = [];\n for (const run of store.historicRuns) {\n allHistoricEvents.push(...run.events);\n }\n\n // Apply compaction to all historic events together (like SQLite)\n const compactedEvents = compactEvents(allHistoricEvents);\n\n // Emit compacted events and track message IDs\n const emittedMessageIds = new Set<string>();\n for (const event of compactedEvents) {\n connectionSubject.next(event);\n if (\"messageId\" in event && typeof event.messageId === \"string\") {\n emittedMessageIds.add(event.messageId);\n }\n }\n\n // Bridge active run to connection if exists\n if (store.subject && (store.isRunning || store.stopRequested)) {\n store.subject.subscribe({\n next: (event) => {\n // Skip message events that we've already emitted from historic\n if (\n \"messageId\" in event &&\n typeof event.messageId === \"string\" &&\n emittedMessageIds.has(event.messageId)\n ) {\n return;\n }\n connectionSubject.next(event);\n },\n complete: () => connectionSubject.complete(),\n error: (err) => connectionSubject.error(err),\n });\n } else {\n // No active run, complete after historic events\n connectionSubject.complete();\n }\n\n return connectionSubject.asObservable();\n }\n\n isRunning(request: AgentRunnerIsRunningRequest): Promise<boolean> {\n const store = GLOBAL_STORE.get(request.threadId);\n return Promise.resolve(store?.isRunning ?? false);\n }\n\n stop(request: AgentRunnerStopRequest): Promise<boolean | undefined> {\n const store = GLOBAL_STORE.get(request.threadId);\n if (!store || !store.isRunning) {\n return Promise.resolve(false);\n }\n if (store.stopRequested) {\n return Promise.resolve(false);\n }\n\n store.stopRequested = true;\n store.isRunning = false;\n\n const agent = store.agent;\n if (!agent) {\n store.stopRequested = false;\n store.isRunning = false;\n return Promise.resolve(false);\n }\n\n try {\n agent.abortRun();\n return Promise.resolve(true);\n } catch (error) {\n console.error(\"Failed to abort agent run\", error);\n store.stopRequested = false;\n store.isRunning = true;\n return Promise.resolve(false);\n }\n }\n\n /**\n * Returns a summary of every thread that has been run through this runner.\n *\n * This powers the local-dev fallback for `GET /threads` when the Intelligence\n * platform is not configured. Each entry mirrors the shape of a platform\n * `ThreadRecord` so the HTTP handler can use the same response envelope.\n */\n listThreads(): InMemoryThread[] {\n const threads: InMemoryThread[] = [];\n for (const [threadId, store] of GLOBAL_STORE) {\n if (store.historicRuns.length === 0) continue;\n const firstRun = store.historicRuns[0]!;\n const lastRun = store.historicRuns[store.historicRuns.length - 1]!;\n threads.push({\n id: threadId,\n name: null,\n agentId: lastRun.agentId,\n organizationId: \"\",\n createdById: \"\",\n archived: false,\n createdAt: new Date(firstRun.createdAt).toISOString(),\n updatedAt: new Date(lastRun.createdAt).toISOString(),\n });\n }\n // Most recently updated first\n return threads.sort(\n (a, b) =>\n new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(),\n );\n }\n\n /**\n * Returns all messages for a thread, using the snapshot captured at the end\n * of the most recent run.\n *\n * This powers the local-dev fallback for `GET /threads/:threadId/messages`\n * when the Intelligence platform is not configured. The returned `Message[]`\n * objects come directly from the ag-ui agent, so their shape is compatible\n * with the Intelligence platform's `ThreadMessage` type.\n */\n getThreadMessages(threadId: string): Message[] {\n const store = GLOBAL_STORE.get(threadId);\n if (!store || store.historicRuns.length === 0) return [];\n // The last run's snapshot has the complete conversation history\n return store.historicRuns[store.historicRuns.length - 1]!.messages;\n }\n\n /**\n * Returns all AG-UI events for a thread, compacted across historic runs.\n *\n * Powers the local-dev fallback for `GET /threads/:threadId/events` when the\n * Intelligence platform is not configured. The compaction logic matches\n * the connection-replay path in {@link connect}, so the stream a\n * late-joining inspector sees matches what this method returns.\n */\n getThreadEvents(threadId: string): BaseEvent[] {\n const store = GLOBAL_STORE.get(threadId);\n if (!store || store.historicRuns.length === 0) return [];\n const all: BaseEvent[] = [];\n for (const run of store.historicRuns) all.push(...run.events);\n return compactEvents(all);\n }\n\n /**\n * Returns the agent state snapshot for a thread.\n *\n * Derived from the last `STATE_SNAPSHOT` in the compacted event stream. The\n * AG-UI `compactEvents` helper consolidates STATE_DELTA events and produces\n * a single trailing STATE_SNAPSHOT when state changes exist, so this is a\n * faithful view of state at the end of the most recent run.\n *\n * Returns `null` when the thread has never emitted a STATE_SNAPSHOT.\n */\n getThreadState(threadId: string): Record<string, unknown> | null {\n const events = this.getThreadEvents(threadId);\n // Walk backwards — the last snapshot wins.\n for (let i = events.length - 1; i >= 0; i--) {\n const event = events[i]!;\n if (event.type === EventType.STATE_SNAPSHOT) {\n const snapshot = (event as StateSnapshotEvent).snapshot;\n if (snapshot && typeof snapshot === \"object\") {\n return snapshot as Record<string, unknown>;\n }\n return null;\n }\n }\n return null;\n }\n\n /**\n * Clears all in-memory thread history.\n *\n * Powers the local-dev fallback for `POST /threads/clear`, letting consumers\n * (e.g. the demo's Clear button) reset to an empty thread list without\n * restarting the runtime. Intentionally not exposed on the Intelligence\n * platform path: there, thread history lives in a real database and must\n * not be wiped this way.\n */\n clearThreads(): void {\n GLOBAL_STORE.clear();\n }\n}\n"],"mappings":";;;;;;;AAkDA,IAAM,qBAAN,MAAyB;CACvB,YAAY,AAAO,UAAkB;EAAlB;iBAGwB;mBAG/B;sBAGkB;sBAGA,EAAE;eAGF;oBAGgB;uBAG9B;uBAGoB;;;AAGtC,MAAM,+BAAe,IAAI,KAAiC;AAE1D,IAAa,sBAAb,cAAyC,YAAY;;;uCACV;;CAEzC,IAAI,SAAuD;EACzD,IAAI,gBAAgB,aAAa,IAAI,QAAQ,SAAS;AACtD,MAAI,CAAC,eAAe;AAClB,mBAAgB,IAAI,mBAAmB,QAAQ,SAAS;AACxD,gBAAa,IAAI,QAAQ,UAAU,cAAc;;EAEnD,MAAM,QAAQ;AAEd,MAAI,MAAM,UACR,OAAM,IAAI,MAAM,yBAAyB;AAE3C,QAAM,YAAY;AAClB,QAAM,eAAe,QAAQ,MAAM;AACnC,QAAM,QAAQ,QAAQ;AACtB,QAAM,gBAAgB;EAGtB,MAAM,iCAAiB,IAAI,KAAa;EACxC,MAAM,mBAAgC,EAAE;AACxC,QAAM,gBAAgB;EAGtB,MAAM,qCAA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,SAAO,MAAM,aAAa,MAAM,aAAa,SAAS,GAAI;;;;;;;;;;CAW5D,gBAAgB,UAA+B;EAC7C,MAAM,QAAQ,aAAa,IAAI,SAAS;AACxC,MAAI,CAAC,SAAS,MAAM,aAAa,WAAW,EAAG,QAAO,EAAE;EACxD,MAAM,MAAmB,EAAE;AAC3B,OAAK,MAAM,OAAO,MAAM,aAAc,KAAI,KAAK,GAAG,IAAI,OAAO;AAC7D,SAAO,cAAc,IAAI;;;;;;;;;;;;CAa3B,eAAe,UAAkD;EAC/D,MAAM,SAAS,KAAK,gBAAgB,SAAS;AAE7C,OAAK,IAAI,IAAI,OAAO,SAAS,GAAG,KAAK,GAAG,KAAK;GAC3C,MAAM,QAAQ,OAAO;AACrB,OAAI,MAAM,SAAS,UAAU,gBAAgB;IAC3C,MAAM,WAAY,MAA6B;AAC/C,QAAI,YAAY,OAAO,aAAa,SAClC,QAAO;AAET,WAAO;;;AAGX,SAAO;;;;;;;;;;;CAYT,eAAqB;AACnB,eAAa,OAAO"}
@@ -1,6 +1,6 @@
1
1
 
2
2
  import { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest, LocalThreadEndpointRecord, LocalThreadEndpointRunner, supportsLocalThreadEndpoints } from "./agent-runner.cjs";
3
- import { InMemoryAgentRunner, InMemoryLimits, InMemoryThread, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵestimateBytes } from "./in-memory.cjs";
3
+ import { InMemoryAgentRunner, InMemoryThread } from "./in-memory.cjs";
4
4
  import { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerStartupBoundary } from "./intelligence.cjs";
5
5
  import { finalizeRunEvents } from "@copilotkit/shared";
6
6
  export { finalizeRunEvents };
@@ -1,6 +1,6 @@
1
1
  import "reflect-metadata";
2
2
  import { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest, LocalThreadEndpointRecord, LocalThreadEndpointRunner, supportsLocalThreadEndpoints } from "./agent-runner.mjs";
3
- import { InMemoryAgentRunner, InMemoryLimits, InMemoryThread, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵestimateBytes } from "./in-memory.mjs";
3
+ import { InMemoryAgentRunner, InMemoryThread } from "./in-memory.mjs";
4
4
  import { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions, RunnerStartupBoundary } from "./intelligence.mjs";
5
5
  import { finalizeRunEvents as finalizeRunEvents$1 } from "@copilotkit/shared";
6
6
  export { finalizeRunEvents$1 as finalizeRunEvents };
@@ -1,6 +1,6 @@
1
1
  import "reflect-metadata";
2
2
  import { AgentRunner, supportsLocalThreadEndpoints } from "./agent-runner.mjs";
3
- import { InMemoryAgentRunner, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵestimateBytes } from "./in-memory.mjs";
3
+ import { InMemoryAgentRunner } from "./in-memory.mjs";
4
4
  import { IntelligenceAgentRunner } from "./intelligence.mjs";
5
5
  import { finalizeRunEvents as finalizeRunEvents$1 } from "@copilotkit/shared";
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@copilotkit/runtime",
3
- "version": "1.62.2-canary.1783457132",
3
+ "version": "1.62.3",
4
4
  "private": false,
5
5
  "keywords": [
6
6
  "ai",
@@ -105,7 +105,7 @@
105
105
  "uuid": "^10.0.0",
106
106
  "ws": "^8.18.0",
107
107
  "zod": "^3.23.3",
108
- "@copilotkit/shared": "1.62.2-canary.1783457132"
108
+ "@copilotkit/shared": "1.62.3"
109
109
  },
110
110
  "devDependencies": {
111
111
  "@copilotkit/aimock": "latest",