@copilotkit/runtime 1.81.0 → 1.83.0

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 (156) hide show
  1. package/dist/agent/index.cjs +18 -10
  2. package/dist/agent/index.cjs.map +1 -1
  3. package/dist/agent/index.d.cts +41 -1
  4. package/dist/agent/index.d.cts.map +1 -1
  5. package/dist/agent/index.d.mts +42 -2
  6. package/dist/agent/index.d.mts.map +1 -1
  7. package/dist/agent/index.mjs +18 -10
  8. package/dist/agent/index.mjs.map +1 -1
  9. package/dist/agent/url-download.cjs +504 -0
  10. package/dist/agent/url-download.cjs.map +1 -0
  11. package/dist/agent/url-download.d.cts +80 -0
  12. package/dist/agent/url-download.d.cts.map +1 -0
  13. package/dist/agent/url-download.d.mts +80 -0
  14. package/dist/agent/url-download.d.mts.map +1 -0
  15. package/dist/agent/url-download.mjs +501 -0
  16. package/dist/agent/url-download.mjs.map +1 -0
  17. package/dist/lib/runtime/copilot-runtime.cjs +2 -1
  18. package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
  19. package/dist/lib/runtime/copilot-runtime.d.cts +5 -0
  20. package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
  21. package/dist/lib/runtime/copilot-runtime.d.mts +5 -0
  22. package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
  23. package/dist/lib/runtime/copilot-runtime.mjs +2 -1
  24. package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
  25. package/dist/lib/runtime/telemetry-agent-runner.cjs +4 -1
  26. package/dist/lib/runtime/telemetry-agent-runner.cjs.map +1 -1
  27. package/dist/lib/runtime/telemetry-agent-runner.d.cts +7 -1
  28. package/dist/lib/runtime/telemetry-agent-runner.d.cts.map +1 -1
  29. package/dist/lib/runtime/telemetry-agent-runner.d.mts +7 -1
  30. package/dist/lib/runtime/telemetry-agent-runner.d.mts.map +1 -1
  31. package/dist/lib/runtime/telemetry-agent-runner.mjs +4 -1
  32. package/dist/lib/runtime/telemetry-agent-runner.mjs.map +1 -1
  33. package/dist/package.cjs +1 -1
  34. package/dist/package.mjs +1 -1
  35. package/dist/v2/index.cjs +12 -3
  36. package/dist/v2/index.d.cts +5 -3
  37. package/dist/v2/index.d.mts +5 -3
  38. package/dist/v2/index.mjs +6 -5
  39. package/dist/v2/runtime/core/channel-manager.cjs +2 -1
  40. package/dist/v2/runtime/core/channel-manager.cjs.map +1 -1
  41. package/dist/v2/runtime/core/channel-manager.mjs +2 -1
  42. package/dist/v2/runtime/core/channel-manager.mjs.map +1 -1
  43. package/dist/v2/runtime/core/fetch-handler.cjs +3 -3
  44. package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -1
  45. package/dist/v2/runtime/core/fetch-handler.mjs +3 -3
  46. package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -1
  47. package/dist/v2/runtime/core/package-info.cjs +1 -1
  48. package/dist/v2/runtime/core/package-info.mjs +1 -1
  49. package/dist/v2/runtime/core/runtime.cjs +12 -2
  50. package/dist/v2/runtime/core/runtime.cjs.map +1 -1
  51. package/dist/v2/runtime/core/runtime.d.cts +40 -1
  52. package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
  53. package/dist/v2/runtime/core/runtime.d.mts +40 -1
  54. package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
  55. package/dist/v2/runtime/core/runtime.mjs +12 -2
  56. package/dist/v2/runtime/core/runtime.mjs.map +1 -1
  57. package/dist/v2/runtime/handlers/get-runtime-info.cjs +6 -6
  58. package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -1
  59. package/dist/v2/runtime/handlers/get-runtime-info.mjs +6 -6
  60. package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -1
  61. package/dist/v2/runtime/handlers/handle-connect.cjs +9 -1
  62. package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -1
  63. package/dist/v2/runtime/handlers/handle-connect.mjs +9 -1
  64. package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -1
  65. package/dist/v2/runtime/handlers/handle-inspector-learning.cjs +1 -1
  66. package/dist/v2/runtime/handlers/handle-inspector-learning.mjs +1 -1
  67. package/dist/v2/runtime/handlers/handle-run.cjs +16 -2
  68. package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
  69. package/dist/v2/runtime/handlers/handle-run.mjs +16 -2
  70. package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
  71. package/dist/v2/runtime/handlers/handle-stop.cjs +14 -3
  72. package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -1
  73. package/dist/v2/runtime/handlers/handle-stop.mjs +14 -3
  74. package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -1
  75. package/dist/v2/runtime/handlers/handle-trajectory-connect.cjs +1 -1
  76. package/dist/v2/runtime/handlers/handle-trajectory-connect.mjs +1 -1
  77. package/dist/v2/runtime/handlers/intelligence/annotate.cjs +2 -2
  78. package/dist/v2/runtime/handlers/intelligence/annotate.mjs +2 -2
  79. package/dist/v2/runtime/handlers/intelligence/memories.cjs +1 -1
  80. package/dist/v2/runtime/handlers/intelligence/memories.mjs +1 -1
  81. package/dist/v2/runtime/handlers/intelligence/run.cjs +7 -4
  82. package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -1
  83. package/dist/v2/runtime/handlers/intelligence/run.mjs +7 -4
  84. package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -1
  85. package/dist/v2/runtime/handlers/intelligence/threads.cjs +137 -31
  86. package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +1 -1
  87. package/dist/v2/runtime/handlers/intelligence/threads.mjs +138 -32
  88. package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +1 -1
  89. package/dist/v2/runtime/handlers/shared/agent-utils.cjs +3 -3
  90. package/dist/v2/runtime/handlers/shared/agent-utils.mjs +3 -3
  91. package/dist/v2/runtime/handlers/shared/mcp-apps-servers.cjs +2 -1
  92. package/dist/v2/runtime/handlers/shared/mcp-apps-servers.cjs.map +1 -1
  93. package/dist/v2/runtime/handlers/shared/mcp-apps-servers.mjs +3 -1
  94. package/dist/v2/runtime/handlers/shared/mcp-apps-servers.mjs.map +1 -1
  95. package/dist/v2/runtime/handlers/shared/platform-error.cjs +1 -1
  96. package/dist/v2/runtime/handlers/shared/platform-error.mjs +1 -1
  97. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs +22 -3
  98. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs.map +1 -1
  99. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs +22 -4
  100. package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs.map +1 -1
  101. package/dist/v2/runtime/handlers/shared/sse-response.cjs +20 -3
  102. package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -1
  103. package/dist/v2/runtime/handlers/shared/sse-response.mjs +20 -3
  104. package/dist/v2/runtime/handlers/shared/sse-response.mjs.map +1 -1
  105. package/dist/v2/runtime/handlers/shared/thread-access.cjs +138 -0
  106. package/dist/v2/runtime/handlers/shared/thread-access.cjs.map +1 -0
  107. package/dist/v2/runtime/handlers/shared/thread-access.d.cts +29 -0
  108. package/dist/v2/runtime/handlers/shared/thread-access.d.cts.map +1 -0
  109. package/dist/v2/runtime/handlers/shared/thread-access.d.mts +29 -0
  110. package/dist/v2/runtime/handlers/shared/thread-access.d.mts.map +1 -0
  111. package/dist/v2/runtime/handlers/shared/thread-access.mjs +133 -0
  112. package/dist/v2/runtime/handlers/shared/thread-access.mjs.map +1 -0
  113. package/dist/v2/runtime/handlers/sse/connect.cjs +2 -1
  114. package/dist/v2/runtime/handlers/sse/connect.cjs.map +1 -1
  115. package/dist/v2/runtime/handlers/sse/connect.mjs +2 -1
  116. package/dist/v2/runtime/handlers/sse/connect.mjs.map +1 -1
  117. package/dist/v2/runtime/handlers/sse/run.cjs +23 -6
  118. package/dist/v2/runtime/handlers/sse/run.cjs.map +1 -1
  119. package/dist/v2/runtime/handlers/sse/run.mjs +23 -6
  120. package/dist/v2/runtime/handlers/sse/run.mjs.map +1 -1
  121. package/dist/v2/runtime/index.d.cts +3 -2
  122. package/dist/v2/runtime/index.d.cts.map +1 -1
  123. package/dist/v2/runtime/index.d.mts +3 -2
  124. package/dist/v2/runtime/index.d.mts.map +1 -1
  125. package/dist/v2/runtime/intelligence-platform/client.cjs +17 -4
  126. package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
  127. package/dist/v2/runtime/intelligence-platform/client.d.cts +9 -37
  128. package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
  129. package/dist/v2/runtime/intelligence-platform/client.d.mts +9 -37
  130. package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
  131. package/dist/v2/runtime/intelligence-platform/client.mjs +17 -4
  132. package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
  133. package/dist/v2/runtime/runner/agent-runner.cjs +55 -0
  134. package/dist/v2/runtime/runner/agent-runner.cjs.map +1 -1
  135. package/dist/v2/runtime/runner/agent-runner.d.cts +90 -6
  136. package/dist/v2/runtime/runner/agent-runner.d.cts.map +1 -1
  137. package/dist/v2/runtime/runner/agent-runner.d.mts +90 -6
  138. package/dist/v2/runtime/runner/agent-runner.d.mts.map +1 -1
  139. package/dist/v2/runtime/runner/agent-runner.mjs +50 -1
  140. package/dist/v2/runtime/runner/agent-runner.mjs.map +1 -1
  141. package/dist/v2/runtime/runner/in-memory.cjs +205 -25
  142. package/dist/v2/runtime/runner/in-memory.cjs.map +1 -1
  143. package/dist/v2/runtime/runner/in-memory.d.cts +112 -15
  144. package/dist/v2/runtime/runner/in-memory.d.cts.map +1 -1
  145. package/dist/v2/runtime/runner/in-memory.d.mts +112 -15
  146. package/dist/v2/runtime/runner/in-memory.d.mts.map +1 -1
  147. package/dist/v2/runtime/runner/in-memory.mjs +206 -27
  148. package/dist/v2/runtime/runner/in-memory.mjs.map +1 -1
  149. package/dist/v2/runtime/runner/index.d.cts +2 -2
  150. package/dist/v2/runtime/runner/index.d.mts +2 -2
  151. package/dist/v2/runtime/runner/index.mjs +2 -2
  152. package/dist/v2/runtime/telemetry/telemetry-client.cjs +1 -1
  153. package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -1
  154. package/dist/v2/runtime/telemetry/telemetry-client.mjs +1 -1
  155. package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -1
  156. package/package.json +6 -6
@@ -72,8 +72,14 @@ function ɵestimateBytes(value) {
72
72
  }
73
73
  }
74
74
  var InMemoryEventStore = class {
75
- constructor(threadId) {
75
+ /**
76
+ * @param ownerId Identity that owns this thread, recorded when the thread is
77
+ * created by its first run. `""` is the anonymous owner;
78
+ * {@link ɵSERVER_THREAD_OWNER} is the owner of a thread created server-side.
79
+ */
80
+ constructor(threadId, ownerId) {
76
81
  this.threadId = threadId;
82
+ this.ownerId = ownerId;
77
83
  this.subject = null;
78
84
  this.isRunning = false;
79
85
  this.currentRunId = null;
@@ -88,9 +94,31 @@ var InMemoryEventStore = class {
88
94
  this.createdAt = null;
89
95
  }
90
96
  };
97
+ /**
98
+ * True when a caller acting as `owner` may reach `store`: only the thread's
99
+ * own owner may. Resolve an optional owner or scope with
100
+ * {@link ɵresolveThreadOwner} first; an omitted one is the server owner, never
101
+ * "every thread".
102
+ */
103
+ function isVisibleTo(store, owner) {
104
+ return store.ownerId === owner;
105
+ }
106
+ /** The owner a Thread endpoint call acts as. */
107
+ function scopeOwner(scope) {
108
+ return require_agent_runner.ɵresolveThreadOwner(scope?.ownerId);
109
+ }
110
+ const ɵOWNER_RETENTION_DEFAULTS = {
111
+ ttlMs: 1440 * 60 * 1e3,
112
+ maxEntries: 1e5,
113
+ maxEntriesPerOwner: 1e4,
114
+ now: () => Date.now()
115
+ };
91
116
  var ɵBoundedThreadStore = class {
92
117
  constructor(limits) {
93
118
  this.map = /* @__PURE__ */ new Map();
119
+ this.removedOwners = /* @__PURE__ */ new Map();
120
+ this.removedOwnersByOwner = /* @__PURE__ */ new Map();
121
+ this.ownerRetention = ɵOWNER_RETENTION_DEFAULTS;
94
122
  this.totalBytes = 0;
95
123
  this.warned = false;
96
124
  this.limitsExplicitlySet = false;
@@ -140,13 +168,18 @@ var ɵBoundedThreadStore = class {
140
168
  this.map.delete(threadId);
141
169
  this.map.set(threadId, store);
142
170
  }
143
- getOrCreate(threadId) {
171
+ /**
172
+ * The thread's store, created and owned by `ownerId` when it does not exist
173
+ * yet. An existing store keeps its owner; callers check access first.
174
+ */
175
+ getOrCreate(threadId, ownerId = require_agent_runner.ɵSERVER_THREAD_OWNER) {
144
176
  const existing = this.map.get(threadId);
145
177
  if (existing) {
146
178
  this.touchOrder(threadId, existing);
147
179
  return existing;
148
180
  }
149
- const store = new InMemoryEventStore(threadId);
181
+ const store = new InMemoryEventStore(threadId, ownerId);
182
+ this.forgetOwner(threadId);
150
183
  this.map.set(threadId, store);
151
184
  this.evictThreadsIfNeeded(threadId);
152
185
  return store;
@@ -184,9 +217,15 @@ var ɵBoundedThreadStore = class {
184
217
  }
185
218
  return false;
186
219
  }
187
- appendRun(threadId, run) {
220
+ /**
221
+ * Append a finished run to `writer`, the store its run started on. Nothing is
222
+ * written when `writer` is no longer the live store for `threadId` (the thread
223
+ * was removed, or removed and claimed again): a run must never write into a
224
+ * thread it did not start on, which may now belong to another owner.
225
+ */
226
+ appendRun(threadId, writer, run) {
188
227
  const store = this.map.get(threadId);
189
- if (!store) return;
228
+ if (!store || store !== writer) return;
190
229
  if (store.createdAt === null) store.createdAt = run.createdAt;
191
230
  if (run.messages.length > 0) {
192
231
  this.totalBytes -= store.approxMessagesSnapshotBytes;
@@ -224,6 +263,87 @@ var ɵBoundedThreadStore = class {
224
263
  for (const run of store.historicRuns) this.totalBytes -= run.approxEventBytes ?? 0;
225
264
  this.totalBytes -= store.approxMessagesSnapshotBytes;
226
265
  this.map.delete(threadId);
266
+ this.rememberOwner(threadId, store.ownerId);
267
+ }
268
+ /**
269
+ * Keep the owner of a removed thread, within the retention bounds. See
270
+ * {@link ɵOwnerRetention} for which records go first over a limit.
271
+ */
272
+ rememberOwner(threadId, ownerId) {
273
+ const { maxEntries, maxEntriesPerOwner, now } = this.ownerRetention;
274
+ this.forgetOwner(threadId);
275
+ this.removedOwners.set(threadId, {
276
+ ownerId,
277
+ removedAt: now()
278
+ });
279
+ let owned = this.removedOwnersByOwner.get(ownerId);
280
+ if (!owned) {
281
+ owned = /* @__PURE__ */ new Set();
282
+ this.removedOwnersByOwner.set(ownerId, owned);
283
+ }
284
+ owned.add(threadId);
285
+ this.pruneRemovedOwners();
286
+ while (owned.size > maxEntriesPerOwner) {
287
+ const oldest = owned.values().next().value;
288
+ if (oldest === void 0) break;
289
+ this.forgetOwner(oldest);
290
+ }
291
+ while (this.removedOwners.size > maxEntries) {
292
+ const oldest = this.removedOwners.keys().next().value;
293
+ if (oldest === void 0) break;
294
+ this.forgetOwner(oldest);
295
+ }
296
+ }
297
+ /** Remove the owner record of `threadId`, if any. */
298
+ forgetOwner(threadId) {
299
+ const record = this.removedOwners.get(threadId);
300
+ if (!record) return;
301
+ this.removedOwners.delete(threadId);
302
+ const owned = this.removedOwnersByOwner.get(record.ownerId);
303
+ owned?.delete(threadId);
304
+ if (owned?.size === 0) this.removedOwnersByOwner.delete(record.ownerId);
305
+ }
306
+ /** Drop expired owner records. They are oldest first, so stop at the first live one. */
307
+ pruneRemovedOwners() {
308
+ const { ttlMs, now } = this.ownerRetention;
309
+ const cutoff = now() - ttlMs;
310
+ for (const [threadId, record] of this.removedOwners) {
311
+ if (record.removedAt >= cutoff) break;
312
+ this.forgetOwner(threadId);
313
+ }
314
+ }
315
+ /**
316
+ * Owner of `threadId`: the live thread's owner, or the retained owner of a
317
+ * thread that was evicted or cleared. `undefined` when nobody owns the id, so
318
+ * the next run may claim it.
319
+ */
320
+ ownerOf(threadId) {
321
+ const store = this.map.get(threadId);
322
+ if (store) return store.ownerId;
323
+ this.pruneRemovedOwners();
324
+ const record = this.removedOwners.get(threadId);
325
+ if (!record) return void 0;
326
+ const { ttlMs, now } = this.ownerRetention;
327
+ if (record.removedAt < now() - ttlMs) {
328
+ this.forgetOwner(threadId);
329
+ return;
330
+ }
331
+ return record.ownerId;
332
+ }
333
+ /** True when a caller acting as `owner` may run on, or claim, `threadId`. */
334
+ canClaim(threadId, owner) {
335
+ const current = this.ownerOf(threadId);
336
+ return current === void 0 || current === owner;
337
+ }
338
+ /**
339
+ * Replace the owner-record retention. Omitted fields fall back to
340
+ * {@link ɵOWNER_RETENTION_DEFAULTS}. Internal: for tests.
341
+ */
342
+ ɵsetOwnerRetention(retention) {
343
+ this.ownerRetention = {
344
+ ...ɵOWNER_RETENTION_DEFAULTS,
345
+ ...retention
346
+ };
227
347
  }
228
348
  evictThreadsIfNeeded(protect) {
229
349
  while (this.map.size > this.limits.maxThreads) if (!this.evictOneLru(protect)) break;
@@ -235,17 +355,18 @@ var ɵBoundedThreadStore = class {
235
355
  console.warn(EVICTION_GUIDANCE);
236
356
  } catch {}
237
357
  }
238
- listThreads() {
358
+ listThreads(scope) {
239
359
  const threads = [];
240
360
  for (const [threadId, store] of this.map) {
241
361
  if (store.historicRuns.length === 0) continue;
362
+ if (!isVisibleTo(store, scopeOwner(scope))) continue;
242
363
  const lastRun = store.historicRuns[store.historicRuns.length - 1];
243
364
  threads.push({
244
365
  id: threadId,
245
366
  name: null,
246
367
  agentId: lastRun.agentId,
247
368
  organizationId: "",
248
- createdById: "",
369
+ createdById: store.ownerId,
249
370
  archived: false,
250
371
  createdAt: new Date(store.createdAt ?? lastRun.createdAt).toISOString(),
251
372
  updatedAt: new Date(lastRun.createdAt).toISOString()
@@ -253,8 +374,28 @@ var ɵBoundedThreadStore = class {
253
374
  }
254
375
  return threads.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
255
376
  }
256
- clear() {
377
+ /**
378
+ * Removes every thread visible to `scope`, except threads that are running
379
+ * or still finalizing: removing one would drop its owner record while its
380
+ * run still writes, so another identity could claim the id. Those threads
381
+ * stay, as in `evictOneLru`. A scoped clear leaves other owners' threads
382
+ * untouched.
383
+ *
384
+ * Without a scope the whole store is reset, running threads included. That
385
+ * is an internal reset for tests; `appendRun` keeps an orphaned run from
386
+ * writing into a thread created after the reset.
387
+ */
388
+ clear(scope) {
389
+ if (scope !== void 0) {
390
+ for (const [threadId, store] of this.map) {
391
+ if (store.isRunning || store.stopRequested) continue;
392
+ if (isVisibleTo(store, scopeOwner(scope))) this.removeThread(threadId, store);
393
+ }
394
+ return;
395
+ }
257
396
  this.map.clear();
397
+ this.removedOwners.clear();
398
+ this.removedOwnersByOwner.clear();
258
399
  this.totalBytes = 0;
259
400
  this.warned = false;
260
401
  }
@@ -296,7 +437,9 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
296
437
  }
297
438
  }
298
439
  run(request) {
299
- const store = sharedStore.getOrCreate(request.threadId);
440
+ const owner = require_agent_runner.ɵresolveThreadOwner(request.ownerId);
441
+ if (!sharedStore.canClaim(request.threadId, owner)) throw new require_agent_runner.ThreadAccessDeniedError();
442
+ const store = sharedStore.getOrCreate(request.threadId, owner);
300
443
  if (store.isRunning || store.stopRequested) {
301
444
  if (this.onConcurrentRun !== "supersede") throw new require_agent_runner.ThreadAlreadyRunningError();
302
445
  const priorAgent = store.agent;
@@ -361,7 +504,7 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
361
504
  const ownsThread = store.currentRunId === request.input.runId;
362
505
  if (ownsThread && (!isError || preFinalizeEventCount > 0 || finalizeControl.stopRequested)) {
363
506
  const compactedEvents = (0, _ag_ui_client.compactEvents)(currentRunEvents);
364
- sharedStore.appendRun(request.threadId, {
507
+ sharedStore.appendRun(request.threadId, store, {
365
508
  threadId: request.threadId,
366
509
  runId: request.input.runId,
367
510
  agentId: request.agent.agentId ?? "default",
@@ -417,11 +560,29 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
417
560
  finalizeRun({ interruptionMessage: error instanceof Error ? error.message : String(error) });
418
561
  }
419
562
  };
420
- runAgent();
563
+ runAgent().catch((error) => {
564
+ console.error("Failed to finish agent run", {
565
+ threadId: request.threadId,
566
+ runId: request.input.runId
567
+ }, error);
568
+ if (store.currentRunId === request.input.runId) {
569
+ store.currentEvents = null;
570
+ store.currentRunId = null;
571
+ store.agent = null;
572
+ store.runSubject = null;
573
+ store.stopRequested = false;
574
+ store.isRunning = false;
575
+ store.activeFinalize = null;
576
+ }
577
+ runSubject.complete();
578
+ nextSubject.complete();
579
+ if (store.subject === nextSubject) store.subject = null;
580
+ });
421
581
  return runSubject.asObservable();
422
582
  }
423
583
  connect(request) {
424
- const store = sharedStore.get(request.threadId, { touch: true });
584
+ const found = sharedStore.peek(request.threadId);
585
+ const store = found && isVisibleTo(found, require_agent_runner.ɵresolveThreadOwner(request.ownerId)) ? sharedStore.get(request.threadId, { touch: true }) : void 0;
425
586
  const connectionSubject = new rxjs.ReplaySubject(Infinity);
426
587
  if (!store) {
427
588
  connectionSubject.complete();
@@ -448,11 +609,13 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
448
609
  }
449
610
  isRunning(request) {
450
611
  const store = sharedStore.peek(request.threadId);
451
- return Promise.resolve(store?.isRunning ?? false);
612
+ if (!store || !isVisibleTo(store, require_agent_runner.ɵresolveThreadOwner(request.ownerId))) return Promise.resolve(false);
613
+ return Promise.resolve(store.isRunning);
452
614
  }
453
615
  stop(request) {
454
616
  const store = sharedStore.peek(request.threadId);
455
617
  if (!store || !store.isRunning) return Promise.resolve(false);
618
+ if (!isVisibleTo(store, require_agent_runner.ɵresolveThreadOwner(request.ownerId))) return Promise.resolve(false);
456
619
  if (request.runId !== void 0 && store.currentRunId !== request.runId) return Promise.resolve(false);
457
620
  if (store.stopRequested) return Promise.resolve(false);
458
621
  store.stopRequested = true;
@@ -478,14 +641,16 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
478
641
  }
479
642
  }
480
643
  /**
481
- * Returns a summary of every thread that has been run through this runner.
644
+ * Returns a summary of every thread that `scope`'s owner holds in the
645
+ * process-wide store, which every InMemoryAgentRunner shares. Without a
646
+ * scope, the server owner's threads ({@link ɵSERVER_THREAD_OWNER}).
482
647
  *
483
648
  * This powers the local-dev fallback for `GET /threads` when the Intelligence
484
649
  * platform is not configured. Each entry mirrors the shape of a platform
485
650
  * `ThreadRecord` so the HTTP handler can use the same response envelope.
486
651
  */
487
- listThreads() {
488
- return sharedStore.listThreads();
652
+ listThreads(scope) {
653
+ return sharedStore.listThreads(scope);
489
654
  }
490
655
  /**
491
656
  * Returns all messages for a thread, using the snapshot captured at the end
@@ -496,8 +661,8 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
496
661
  * objects come directly from the ag-ui agent, so their shape is compatible
497
662
  * with CopilotKit Intelligence's `ThreadMessage` type.
498
663
  */
499
- getThreadMessages(threadId) {
500
- const store = sharedStore.peek(threadId);
664
+ getThreadMessages(threadId, scope) {
665
+ const store = this.peekVisible(threadId, scope);
501
666
  if (!store) return [];
502
667
  return [...store.messagesSnapshot];
503
668
  }
@@ -509,8 +674,8 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
509
674
  * the connection-replay path in {@link connect}, so the stream a
510
675
  * late-joining inspector sees matches what this method returns.
511
676
  */
512
- getThreadEvents(threadId) {
513
- const store = sharedStore.peek(threadId);
677
+ getThreadEvents(threadId, scope) {
678
+ const store = this.peekVisible(threadId, scope);
514
679
  if (!store || store.historicRuns.length === 0) return [];
515
680
  const all = [];
516
681
  for (const run of store.historicRuns) all.push(...run.events);
@@ -526,8 +691,8 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
526
691
  *
527
692
  * Returns `null` when the thread has never emitted a STATE_SNAPSHOT.
528
693
  */
529
- getThreadState(threadId) {
530
- const events = this.getThreadEvents(threadId);
694
+ getThreadState(threadId, scope) {
695
+ const events = this.getThreadEvents(threadId, scope);
531
696
  for (let i = events.length - 1; i >= 0; i--) {
532
697
  const event = events[i];
533
698
  if (event.type === _ag_ui_client.EventType.STATE_SNAPSHOT) {
@@ -539,7 +704,9 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
539
704
  return null;
540
705
  }
541
706
  /**
542
- * Clears all in-memory thread history.
707
+ * Clears the in-memory history of `scope`'s threads (without a scope, the
708
+ * server owner's). A thread with a run still active or finalizing is kept;
709
+ * see {@link ɵBoundedThreadStore.clear}.
543
710
  *
544
711
  * Powers the local-dev fallback for `POST /threads/clear`, letting consumers
545
712
  * (e.g. the demo's Clear button) reset to an empty thread list without
@@ -547,8 +714,20 @@ var InMemoryAgentRunner = class extends require_agent_runner.AgentRunner {
547
714
  * platform path: there, thread history lives in a real database and must
548
715
  * not be wiped this way.
549
716
  */
550
- clearThreads() {
551
- sharedStore.clear();
717
+ clearThreads(scope) {
718
+ sharedStore.clear({ ownerId: scopeOwner(scope) });
719
+ }
720
+ /**
721
+ * False when the thread id is held by an owner other than `scope`'s: a
722
+ * live thread, or the retained owner of an evicted or cleared one.
723
+ */
724
+ isThreadAccessible(threadId, scope) {
725
+ return sharedStore.canClaim(threadId, scopeOwner(scope));
726
+ }
727
+ /** The thread's store, or `undefined` when unknown or not visible to `scope`. */
728
+ peekVisible(threadId, scope) {
729
+ const store = sharedStore.peek(threadId);
730
+ return store && isVisibleTo(store, scopeOwner(scope)) ? store : void 0;
552
731
  }
553
732
  };
554
733
 
@@ -557,6 +736,7 @@ exports.InMemoryAgentRunner = InMemoryAgentRunner;
557
736
  exports.ɵBoundedThreadStore = ɵBoundedThreadStore;
558
737
  exports.ɵGLOBAL_STORE = ɵGLOBAL_STORE;
559
738
  exports.ɵINMEMORY_DEFAULTS = ɵINMEMORY_DEFAULTS;
739
+ exports.ɵOWNER_RETENTION_DEFAULTS = ɵOWNER_RETENTION_DEFAULTS;
560
740
  exports.ɵestimateBytes = ɵestimateBytes;
561
741
  exports.ɵnormalizeLimits = ɵnormalizeLimits;
562
742
  //# sourceMappingURL=in-memory.cjs.map