@copilotkit/runtime 1.82.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.
- package/dist/agent/index.cjs +8 -3
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts +41 -1
- package/dist/agent/index.d.cts.map +1 -1
- package/dist/agent/index.d.mts +42 -2
- package/dist/agent/index.d.mts.map +1 -1
- package/dist/agent/index.mjs +8 -3
- package/dist/agent/index.mjs.map +1 -1
- package/dist/agent/url-download.cjs +504 -0
- package/dist/agent/url-download.cjs.map +1 -0
- package/dist/agent/url-download.d.cts +80 -0
- package/dist/agent/url-download.d.cts.map +1 -0
- package/dist/agent/url-download.d.mts +80 -0
- package/dist/agent/url-download.d.mts.map +1 -0
- package/dist/agent/url-download.mjs +501 -0
- package/dist/agent/url-download.mjs.map +1 -0
- package/dist/lib/runtime/copilot-runtime.cjs +2 -1
- package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts +5 -0
- package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.mts +5 -0
- package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.mjs +2 -1
- package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.cjs +3 -0
- package/dist/lib/runtime/telemetry-agent-runner.cjs.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.cts +7 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.cts.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.mts +7 -1
- package/dist/lib/runtime/telemetry-agent-runner.d.mts.map +1 -1
- package/dist/lib/runtime/telemetry-agent-runner.mjs +3 -0
- package/dist/lib/runtime/telemetry-agent-runner.mjs.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/v2/index.cjs +12 -3
- package/dist/v2/index.d.cts +5 -3
- package/dist/v2/index.d.mts +5 -3
- package/dist/v2/index.mjs +6 -5
- package/dist/v2/runtime/core/channel-manager.cjs +1 -1
- package/dist/v2/runtime/core/channel-manager.mjs +1 -1
- package/dist/v2/runtime/core/fetch-handler.cjs +2 -2
- package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.mjs +2 -2
- package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -1
- package/dist/v2/runtime/core/package-info.cjs +1 -1
- package/dist/v2/runtime/core/package-info.mjs +1 -1
- package/dist/v2/runtime/core/runtime.cjs +12 -2
- package/dist/v2/runtime/core/runtime.cjs.map +1 -1
- package/dist/v2/runtime/core/runtime.d.cts +40 -1
- package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
- package/dist/v2/runtime/core/runtime.d.mts +40 -1
- package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
- package/dist/v2/runtime/core/runtime.mjs +12 -2
- package/dist/v2/runtime/core/runtime.mjs.map +1 -1
- package/dist/v2/runtime/handlers/get-runtime-info.cjs +6 -6
- package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -1
- package/dist/v2/runtime/handlers/get-runtime-info.mjs +6 -6
- package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.cjs +9 -1
- package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.mjs +9 -1
- package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-inspector-learning.cjs +1 -1
- package/dist/v2/runtime/handlers/handle-inspector-learning.mjs +1 -1
- package/dist/v2/runtime/handlers/handle-run.cjs +16 -2
- package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.mjs +16 -2
- package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-stop.cjs +14 -3
- package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-stop.mjs +14 -3
- package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-trajectory-connect.cjs +1 -1
- package/dist/v2/runtime/handlers/handle-trajectory-connect.mjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/annotate.cjs +2 -2
- package/dist/v2/runtime/handlers/intelligence/annotate.mjs +2 -2
- package/dist/v2/runtime/handlers/intelligence/memories.cjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/memories.mjs +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.cjs +4 -4
- package/dist/v2/runtime/handlers/intelligence/run.mjs +4 -4
- package/dist/v2/runtime/handlers/intelligence/threads.cjs +137 -31
- package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/threads.mjs +138 -32
- package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/agent-utils.cjs +3 -3
- package/dist/v2/runtime/handlers/shared/agent-utils.mjs +3 -3
- package/dist/v2/runtime/handlers/shared/platform-error.cjs +1 -1
- package/dist/v2/runtime/handlers/shared/platform-error.mjs +1 -1
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs +22 -3
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs +22 -4
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs +19 -2
- package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.mjs +19 -2
- package/dist/v2/runtime/handlers/shared/sse-response.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/thread-access.cjs +138 -0
- package/dist/v2/runtime/handlers/shared/thread-access.cjs.map +1 -0
- package/dist/v2/runtime/handlers/shared/thread-access.d.cts +29 -0
- package/dist/v2/runtime/handlers/shared/thread-access.d.cts.map +1 -0
- package/dist/v2/runtime/handlers/shared/thread-access.d.mts +29 -0
- package/dist/v2/runtime/handlers/shared/thread-access.d.mts.map +1 -0
- package/dist/v2/runtime/handlers/shared/thread-access.mjs +133 -0
- package/dist/v2/runtime/handlers/shared/thread-access.mjs.map +1 -0
- package/dist/v2/runtime/handlers/sse/connect.cjs +2 -1
- package/dist/v2/runtime/handlers/sse/connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/connect.mjs +2 -1
- package/dist/v2/runtime/handlers/sse/connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/run.cjs +23 -6
- package/dist/v2/runtime/handlers/sse/run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/run.mjs +23 -6
- package/dist/v2/runtime/handlers/sse/run.mjs.map +1 -1
- package/dist/v2/runtime/index.d.cts +3 -2
- package/dist/v2/runtime/index.d.cts.map +1 -1
- package/dist/v2/runtime/index.d.mts +3 -2
- package/dist/v2/runtime/index.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.cjs +16 -4
- package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.cts +4 -37
- package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.mts +4 -37
- package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs +16 -4
- package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
- package/dist/v2/runtime/runner/agent-runner.cjs +55 -0
- package/dist/v2/runtime/runner/agent-runner.cjs.map +1 -1
- package/dist/v2/runtime/runner/agent-runner.d.cts +90 -6
- package/dist/v2/runtime/runner/agent-runner.d.cts.map +1 -1
- package/dist/v2/runtime/runner/agent-runner.d.mts +90 -6
- package/dist/v2/runtime/runner/agent-runner.d.mts.map +1 -1
- package/dist/v2/runtime/runner/agent-runner.mjs +50 -1
- package/dist/v2/runtime/runner/agent-runner.mjs.map +1 -1
- package/dist/v2/runtime/runner/in-memory.cjs +205 -25
- package/dist/v2/runtime/runner/in-memory.cjs.map +1 -1
- package/dist/v2/runtime/runner/in-memory.d.cts +112 -15
- package/dist/v2/runtime/runner/in-memory.d.cts.map +1 -1
- package/dist/v2/runtime/runner/in-memory.d.mts +112 -15
- package/dist/v2/runtime/runner/in-memory.d.mts.map +1 -1
- package/dist/v2/runtime/runner/in-memory.mjs +206 -27
- package/dist/v2/runtime/runner/in-memory.mjs.map +1 -1
- package/dist/v2/runtime/runner/index.d.cts +2 -2
- package/dist/v2/runtime/runner/index.d.mts +2 -2
- package/dist/v2/runtime/runner/index.mjs +2 -2
- package/package.json +6 -6
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest } from "./agent-runner.mjs";
|
|
2
|
+
import { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest, LocalThreadEndpointScope } from "./agent-runner.mjs";
|
|
3
3
|
import { Observable, ReplaySubject } from "rxjs";
|
|
4
4
|
import { AbstractAgent, BaseEvent, Message } from "@ag-ui/client";
|
|
5
5
|
|
|
@@ -114,14 +114,21 @@ interface InMemoryThread {
|
|
|
114
114
|
name: string | null;
|
|
115
115
|
agentId: string;
|
|
116
116
|
organizationId: "";
|
|
117
|
-
|
|
117
|
+
/** Identity that owns the thread; `""` for a thread created without identity. */
|
|
118
|
+
createdById: string;
|
|
118
119
|
archived: false;
|
|
119
120
|
createdAt: string;
|
|
120
121
|
updatedAt: string;
|
|
121
122
|
}
|
|
122
123
|
declare class InMemoryEventStore {
|
|
123
124
|
threadId: string;
|
|
124
|
-
|
|
125
|
+
readonly ownerId: string;
|
|
126
|
+
/**
|
|
127
|
+
* @param ownerId Identity that owns this thread, recorded when the thread is
|
|
128
|
+
* created by its first run. `""` is the anonymous owner;
|
|
129
|
+
* {@link ɵSERVER_THREAD_OWNER} is the owner of a thread created server-side.
|
|
130
|
+
*/
|
|
131
|
+
constructor(threadId: string, ownerId: string);
|
|
125
132
|
/** The subject that current consumers subscribe to. */
|
|
126
133
|
subject: ReplaySubject<BaseEvent> | null;
|
|
127
134
|
/** True while a run is actively producing events. */
|
|
@@ -172,8 +179,44 @@ declare class InMemoryEventStore {
|
|
|
172
179
|
*/
|
|
173
180
|
createdAt: number | null;
|
|
174
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* How long, and how many, owner records the store keeps for threads that left
|
|
184
|
+
* it through LRU eviction or a scoped clear. See
|
|
185
|
+
* {@link ɵBoundedThreadStore.ownerOf}.
|
|
186
|
+
*
|
|
187
|
+
* When a record is added over a limit, expired records go first; then, when
|
|
188
|
+
* the record's owner holds more than `maxEntriesPerOwner`, that owner's
|
|
189
|
+
* oldest record; then, over `maxEntries`, the oldest record of any owner. So
|
|
190
|
+
* one identity that churns threads (anonymous callers share one identity)
|
|
191
|
+
* only displaces its own records. Residual risk: many distinct identities
|
|
192
|
+
* together can still exceed `maxEntries` and push out another identity's
|
|
193
|
+
* live record. That frees the id for a new owner but leaks nothing, since
|
|
194
|
+
* the removed thread's history is already gone, and it needs the victim's
|
|
195
|
+
* thread id.
|
|
196
|
+
*/
|
|
197
|
+
interface ɵOwnerRetention {
|
|
198
|
+
/** Milliseconds an owner record is kept after its thread was removed. */
|
|
199
|
+
ttlMs: number;
|
|
200
|
+
/** Most owner records kept in all. */
|
|
201
|
+
maxEntries: number;
|
|
202
|
+
/** Most owner records kept for one owner. */
|
|
203
|
+
maxEntriesPerOwner: number;
|
|
204
|
+
/** Clock, replaceable in tests. */
|
|
205
|
+
now: () => number;
|
|
206
|
+
}
|
|
207
|
+
declare const ɵOWNER_RETENTION_DEFAULTS: ɵOwnerRetention;
|
|
175
208
|
declare class ɵBoundedThreadStore {
|
|
176
209
|
private readonly map;
|
|
210
|
+
/**
|
|
211
|
+
* Owners of threads that were removed from `map`, oldest first. A removed
|
|
212
|
+
* thread's history is gone, but its id stays reserved for its owner until
|
|
213
|
+
* the record expires, so another identity cannot claim the id and receive
|
|
214
|
+
* the owner's next runs or later replays.
|
|
215
|
+
*/
|
|
216
|
+
private readonly removedOwners;
|
|
217
|
+
/** The ids in `removedOwners`, per owner, oldest first. */
|
|
218
|
+
private readonly removedOwnersByOwner;
|
|
219
|
+
private ownerRetention;
|
|
177
220
|
private totalBytes;
|
|
178
221
|
private warned;
|
|
179
222
|
/** True once limits have been EXPLICITLY set (via setLimits), not just the constructor default. */
|
|
@@ -206,7 +249,11 @@ declare class ɵBoundedThreadStore {
|
|
|
206
249
|
get size(): number;
|
|
207
250
|
/** Re-insert at the tail so Map iteration order stays LRU-first. */
|
|
208
251
|
private touchOrder;
|
|
209
|
-
|
|
252
|
+
/**
|
|
253
|
+
* The thread's store, created and owned by `ownerId` when it does not exist
|
|
254
|
+
* yet. An existing store keeps its owner; callers check access first.
|
|
255
|
+
*/
|
|
256
|
+
getOrCreate(threadId: string, ownerId?: string): InMemoryEventStore;
|
|
210
257
|
get(threadId: string, opts: {
|
|
211
258
|
touch: boolean;
|
|
212
259
|
}): InMemoryEventStore | undefined;
|
|
@@ -227,7 +274,13 @@ declare class ɵBoundedThreadStore {
|
|
|
227
274
|
* finalization completes.
|
|
228
275
|
*/
|
|
229
276
|
private evictOneLru;
|
|
230
|
-
|
|
277
|
+
/**
|
|
278
|
+
* Append a finished run to `writer`, the store its run started on. Nothing is
|
|
279
|
+
* written when `writer` is no longer the live store for `threadId` (the thread
|
|
280
|
+
* was removed, or removed and claimed again): a run must never write into a
|
|
281
|
+
* thread it did not start on, which may now belong to another owner.
|
|
282
|
+
*/
|
|
283
|
+
appendRun(threadId: string, writer: InMemoryEventStore | undefined, run: HistoricRun): void;
|
|
231
284
|
private enforceRunCap;
|
|
232
285
|
/**
|
|
233
286
|
* Trim the store back under the byte ceiling by evicting LRU non-running
|
|
@@ -236,10 +289,43 @@ declare class ɵBoundedThreadStore {
|
|
|
236
289
|
*/
|
|
237
290
|
private evictByBytesIfNeeded;
|
|
238
291
|
private removeThread;
|
|
292
|
+
/**
|
|
293
|
+
* Keep the owner of a removed thread, within the retention bounds. See
|
|
294
|
+
* {@link ɵOwnerRetention} for which records go first over a limit.
|
|
295
|
+
*/
|
|
296
|
+
private rememberOwner;
|
|
297
|
+
/** Remove the owner record of `threadId`, if any. */
|
|
298
|
+
private forgetOwner;
|
|
299
|
+
/** Drop expired owner records. They are oldest first, so stop at the first live one. */
|
|
300
|
+
private pruneRemovedOwners;
|
|
301
|
+
/**
|
|
302
|
+
* Owner of `threadId`: the live thread's owner, or the retained owner of a
|
|
303
|
+
* thread that was evicted or cleared. `undefined` when nobody owns the id, so
|
|
304
|
+
* the next run may claim it.
|
|
305
|
+
*/
|
|
306
|
+
ownerOf(threadId: string): string | undefined;
|
|
307
|
+
/** True when a caller acting as `owner` may run on, or claim, `threadId`. */
|
|
308
|
+
canClaim(threadId: string, owner: string): boolean;
|
|
309
|
+
/**
|
|
310
|
+
* Replace the owner-record retention. Omitted fields fall back to
|
|
311
|
+
* {@link ɵOWNER_RETENTION_DEFAULTS}. Internal: for tests.
|
|
312
|
+
*/
|
|
313
|
+
ɵsetOwnerRetention(retention: Partial<ɵOwnerRetention>): void;
|
|
239
314
|
private evictThreadsIfNeeded;
|
|
240
315
|
private noteEviction;
|
|
241
|
-
listThreads(): InMemoryThread[];
|
|
242
|
-
|
|
316
|
+
listThreads(scope?: LocalThreadEndpointScope): InMemoryThread[];
|
|
317
|
+
/**
|
|
318
|
+
* Removes every thread visible to `scope`, except threads that are running
|
|
319
|
+
* or still finalizing: removing one would drop its owner record while its
|
|
320
|
+
* run still writes, so another identity could claim the id. Those threads
|
|
321
|
+
* stay, as in `evictOneLru`. A scoped clear leaves other owners' threads
|
|
322
|
+
* untouched.
|
|
323
|
+
*
|
|
324
|
+
* Without a scope the whole store is reset, running threads included. That
|
|
325
|
+
* is an internal reset for tests; `appendRun` keeps an orphaned run from
|
|
326
|
+
* writing into a thread created after the reset.
|
|
327
|
+
*/
|
|
328
|
+
clear(scope?: LocalThreadEndpointScope): void;
|
|
243
329
|
}
|
|
244
330
|
/**
|
|
245
331
|
* Process-wide singleton backing every {@link InMemoryAgentRunner}. Exported
|
|
@@ -278,13 +364,15 @@ declare class InMemoryAgentRunner extends AgentRunner {
|
|
|
278
364
|
isRunning(request: AgentRunnerIsRunningRequest): Promise<boolean>;
|
|
279
365
|
stop(request: AgentRunnerStopRequest): Promise<boolean | undefined>;
|
|
280
366
|
/**
|
|
281
|
-
* Returns a summary of every thread that
|
|
367
|
+
* Returns a summary of every thread that `scope`'s owner holds in the
|
|
368
|
+
* process-wide store, which every InMemoryAgentRunner shares. Without a
|
|
369
|
+
* scope, the server owner's threads ({@link ɵSERVER_THREAD_OWNER}).
|
|
282
370
|
*
|
|
283
371
|
* This powers the local-dev fallback for `GET /threads` when the Intelligence
|
|
284
372
|
* platform is not configured. Each entry mirrors the shape of a platform
|
|
285
373
|
* `ThreadRecord` so the HTTP handler can use the same response envelope.
|
|
286
374
|
*/
|
|
287
|
-
listThreads(): InMemoryThread[];
|
|
375
|
+
listThreads(scope?: LocalThreadEndpointScope): InMemoryThread[];
|
|
288
376
|
/**
|
|
289
377
|
* Returns all messages for a thread, using the snapshot captured at the end
|
|
290
378
|
* of the most recent run.
|
|
@@ -294,7 +382,7 @@ declare class InMemoryAgentRunner extends AgentRunner {
|
|
|
294
382
|
* objects come directly from the ag-ui agent, so their shape is compatible
|
|
295
383
|
* with CopilotKit Intelligence's `ThreadMessage` type.
|
|
296
384
|
*/
|
|
297
|
-
getThreadMessages(threadId: string): Message[];
|
|
385
|
+
getThreadMessages(threadId: string, scope?: LocalThreadEndpointScope): Message[];
|
|
298
386
|
/**
|
|
299
387
|
* Returns all AG-UI events for a thread, compacted across historic runs.
|
|
300
388
|
*
|
|
@@ -303,7 +391,7 @@ declare class InMemoryAgentRunner extends AgentRunner {
|
|
|
303
391
|
* the connection-replay path in {@link connect}, so the stream a
|
|
304
392
|
* late-joining inspector sees matches what this method returns.
|
|
305
393
|
*/
|
|
306
|
-
getThreadEvents(threadId: string): BaseEvent[];
|
|
394
|
+
getThreadEvents(threadId: string, scope?: LocalThreadEndpointScope): BaseEvent[];
|
|
307
395
|
/**
|
|
308
396
|
* Returns the agent state snapshot for a thread.
|
|
309
397
|
*
|
|
@@ -314,9 +402,11 @@ declare class InMemoryAgentRunner extends AgentRunner {
|
|
|
314
402
|
*
|
|
315
403
|
* Returns `null` when the thread has never emitted a STATE_SNAPSHOT.
|
|
316
404
|
*/
|
|
317
|
-
getThreadState(threadId: string): Record<string, unknown> | null;
|
|
405
|
+
getThreadState(threadId: string, scope?: LocalThreadEndpointScope): Record<string, unknown> | null;
|
|
318
406
|
/**
|
|
319
|
-
* Clears
|
|
407
|
+
* Clears the in-memory history of `scope`'s threads (without a scope, the
|
|
408
|
+
* server owner's). A thread with a run still active or finalizing is kept;
|
|
409
|
+
* see {@link ɵBoundedThreadStore.clear}.
|
|
320
410
|
*
|
|
321
411
|
* Powers the local-dev fallback for `POST /threads/clear`, letting consumers
|
|
322
412
|
* (e.g. the demo's Clear button) reset to an empty thread list without
|
|
@@ -324,8 +414,15 @@ declare class InMemoryAgentRunner extends AgentRunner {
|
|
|
324
414
|
* platform path: there, thread history lives in a real database and must
|
|
325
415
|
* not be wiped this way.
|
|
326
416
|
*/
|
|
327
|
-
clearThreads(): void;
|
|
417
|
+
clearThreads(scope?: LocalThreadEndpointScope): void;
|
|
418
|
+
/**
|
|
419
|
+
* False when the thread id is held by an owner other than `scope`'s: a
|
|
420
|
+
* live thread, or the retained owner of an evicted or cleared one.
|
|
421
|
+
*/
|
|
422
|
+
isThreadAccessible(threadId: string, scope: LocalThreadEndpointScope): boolean;
|
|
423
|
+
/** The thread's store, or `undefined` when unknown or not visible to `scope`. */
|
|
424
|
+
private peekVisible;
|
|
328
425
|
}
|
|
329
426
|
//#endregion
|
|
330
|
-
export { InMemoryAgentRunner, InMemoryAgentRunnerOptions, InMemoryLimits, InMemoryThread, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵestimateBytes, ɵnormalizeLimits };
|
|
427
|
+
export { InMemoryAgentRunner, InMemoryAgentRunnerOptions, InMemoryLimits, InMemoryThread, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵOWNER_RETENTION_DEFAULTS, ɵOwnerRetention, ɵestimateBytes, ɵnormalizeLimits };
|
|
331
428
|
//# sourceMappingURL=in-memory.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory.d.mts","names":[],"sources":["../../../../src/v2/runtime/runner/in-memory.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"in-memory.d.mts","names":[],"sources":["../../../../src/v2/runtime/runner/in-memory.ts"],"mappings":"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"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
import { AgentRunner, ThreadAlreadyRunningError } from "./agent-runner.mjs";
|
|
2
|
+
import { AgentRunner, ThreadAccessDeniedError, ThreadAlreadyRunningError, ɵSERVER_THREAD_OWNER, ɵresolveThreadOwner } from "./agent-runner.mjs";
|
|
3
3
|
import { finalizeRunEvents } from "@copilotkit/shared";
|
|
4
4
|
import { ReplaySubject } from "rxjs";
|
|
5
5
|
import { EventType, compactEvents } from "@ag-ui/client";
|
|
@@ -71,8 +71,14 @@ function ɵestimateBytes(value) {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
var InMemoryEventStore = class {
|
|
74
|
-
|
|
74
|
+
/**
|
|
75
|
+
* @param ownerId Identity that owns this thread, recorded when the thread is
|
|
76
|
+
* created by its first run. `""` is the anonymous owner;
|
|
77
|
+
* {@link ɵSERVER_THREAD_OWNER} is the owner of a thread created server-side.
|
|
78
|
+
*/
|
|
79
|
+
constructor(threadId, ownerId) {
|
|
75
80
|
this.threadId = threadId;
|
|
81
|
+
this.ownerId = ownerId;
|
|
76
82
|
this.subject = null;
|
|
77
83
|
this.isRunning = false;
|
|
78
84
|
this.currentRunId = null;
|
|
@@ -87,9 +93,31 @@ var InMemoryEventStore = class {
|
|
|
87
93
|
this.createdAt = null;
|
|
88
94
|
}
|
|
89
95
|
};
|
|
96
|
+
/**
|
|
97
|
+
* True when a caller acting as `owner` may reach `store`: only the thread's
|
|
98
|
+
* own owner may. Resolve an optional owner or scope with
|
|
99
|
+
* {@link ɵresolveThreadOwner} first; an omitted one is the server owner, never
|
|
100
|
+
* "every thread".
|
|
101
|
+
*/
|
|
102
|
+
function isVisibleTo(store, owner) {
|
|
103
|
+
return store.ownerId === owner;
|
|
104
|
+
}
|
|
105
|
+
/** The owner a Thread endpoint call acts as. */
|
|
106
|
+
function scopeOwner(scope) {
|
|
107
|
+
return ɵresolveThreadOwner(scope?.ownerId);
|
|
108
|
+
}
|
|
109
|
+
const ɵOWNER_RETENTION_DEFAULTS = {
|
|
110
|
+
ttlMs: 1440 * 60 * 1e3,
|
|
111
|
+
maxEntries: 1e5,
|
|
112
|
+
maxEntriesPerOwner: 1e4,
|
|
113
|
+
now: () => Date.now()
|
|
114
|
+
};
|
|
90
115
|
var ɵBoundedThreadStore = class {
|
|
91
116
|
constructor(limits) {
|
|
92
117
|
this.map = /* @__PURE__ */ new Map();
|
|
118
|
+
this.removedOwners = /* @__PURE__ */ new Map();
|
|
119
|
+
this.removedOwnersByOwner = /* @__PURE__ */ new Map();
|
|
120
|
+
this.ownerRetention = ɵOWNER_RETENTION_DEFAULTS;
|
|
93
121
|
this.totalBytes = 0;
|
|
94
122
|
this.warned = false;
|
|
95
123
|
this.limitsExplicitlySet = false;
|
|
@@ -139,13 +167,18 @@ var ɵBoundedThreadStore = class {
|
|
|
139
167
|
this.map.delete(threadId);
|
|
140
168
|
this.map.set(threadId, store);
|
|
141
169
|
}
|
|
142
|
-
|
|
170
|
+
/**
|
|
171
|
+
* The thread's store, created and owned by `ownerId` when it does not exist
|
|
172
|
+
* yet. An existing store keeps its owner; callers check access first.
|
|
173
|
+
*/
|
|
174
|
+
getOrCreate(threadId, ownerId = ɵSERVER_THREAD_OWNER) {
|
|
143
175
|
const existing = this.map.get(threadId);
|
|
144
176
|
if (existing) {
|
|
145
177
|
this.touchOrder(threadId, existing);
|
|
146
178
|
return existing;
|
|
147
179
|
}
|
|
148
|
-
const store = new InMemoryEventStore(threadId);
|
|
180
|
+
const store = new InMemoryEventStore(threadId, ownerId);
|
|
181
|
+
this.forgetOwner(threadId);
|
|
149
182
|
this.map.set(threadId, store);
|
|
150
183
|
this.evictThreadsIfNeeded(threadId);
|
|
151
184
|
return store;
|
|
@@ -183,9 +216,15 @@ var ɵBoundedThreadStore = class {
|
|
|
183
216
|
}
|
|
184
217
|
return false;
|
|
185
218
|
}
|
|
186
|
-
|
|
219
|
+
/**
|
|
220
|
+
* Append a finished run to `writer`, the store its run started on. Nothing is
|
|
221
|
+
* written when `writer` is no longer the live store for `threadId` (the thread
|
|
222
|
+
* was removed, or removed and claimed again): a run must never write into a
|
|
223
|
+
* thread it did not start on, which may now belong to another owner.
|
|
224
|
+
*/
|
|
225
|
+
appendRun(threadId, writer, run) {
|
|
187
226
|
const store = this.map.get(threadId);
|
|
188
|
-
if (!store) return;
|
|
227
|
+
if (!store || store !== writer) return;
|
|
189
228
|
if (store.createdAt === null) store.createdAt = run.createdAt;
|
|
190
229
|
if (run.messages.length > 0) {
|
|
191
230
|
this.totalBytes -= store.approxMessagesSnapshotBytes;
|
|
@@ -223,6 +262,87 @@ var ɵBoundedThreadStore = class {
|
|
|
223
262
|
for (const run of store.historicRuns) this.totalBytes -= run.approxEventBytes ?? 0;
|
|
224
263
|
this.totalBytes -= store.approxMessagesSnapshotBytes;
|
|
225
264
|
this.map.delete(threadId);
|
|
265
|
+
this.rememberOwner(threadId, store.ownerId);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Keep the owner of a removed thread, within the retention bounds. See
|
|
269
|
+
* {@link ɵOwnerRetention} for which records go first over a limit.
|
|
270
|
+
*/
|
|
271
|
+
rememberOwner(threadId, ownerId) {
|
|
272
|
+
const { maxEntries, maxEntriesPerOwner, now } = this.ownerRetention;
|
|
273
|
+
this.forgetOwner(threadId);
|
|
274
|
+
this.removedOwners.set(threadId, {
|
|
275
|
+
ownerId,
|
|
276
|
+
removedAt: now()
|
|
277
|
+
});
|
|
278
|
+
let owned = this.removedOwnersByOwner.get(ownerId);
|
|
279
|
+
if (!owned) {
|
|
280
|
+
owned = /* @__PURE__ */ new Set();
|
|
281
|
+
this.removedOwnersByOwner.set(ownerId, owned);
|
|
282
|
+
}
|
|
283
|
+
owned.add(threadId);
|
|
284
|
+
this.pruneRemovedOwners();
|
|
285
|
+
while (owned.size > maxEntriesPerOwner) {
|
|
286
|
+
const oldest = owned.values().next().value;
|
|
287
|
+
if (oldest === void 0) break;
|
|
288
|
+
this.forgetOwner(oldest);
|
|
289
|
+
}
|
|
290
|
+
while (this.removedOwners.size > maxEntries) {
|
|
291
|
+
const oldest = this.removedOwners.keys().next().value;
|
|
292
|
+
if (oldest === void 0) break;
|
|
293
|
+
this.forgetOwner(oldest);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/** Remove the owner record of `threadId`, if any. */
|
|
297
|
+
forgetOwner(threadId) {
|
|
298
|
+
const record = this.removedOwners.get(threadId);
|
|
299
|
+
if (!record) return;
|
|
300
|
+
this.removedOwners.delete(threadId);
|
|
301
|
+
const owned = this.removedOwnersByOwner.get(record.ownerId);
|
|
302
|
+
owned?.delete(threadId);
|
|
303
|
+
if (owned?.size === 0) this.removedOwnersByOwner.delete(record.ownerId);
|
|
304
|
+
}
|
|
305
|
+
/** Drop expired owner records. They are oldest first, so stop at the first live one. */
|
|
306
|
+
pruneRemovedOwners() {
|
|
307
|
+
const { ttlMs, now } = this.ownerRetention;
|
|
308
|
+
const cutoff = now() - ttlMs;
|
|
309
|
+
for (const [threadId, record] of this.removedOwners) {
|
|
310
|
+
if (record.removedAt >= cutoff) break;
|
|
311
|
+
this.forgetOwner(threadId);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Owner of `threadId`: the live thread's owner, or the retained owner of a
|
|
316
|
+
* thread that was evicted or cleared. `undefined` when nobody owns the id, so
|
|
317
|
+
* the next run may claim it.
|
|
318
|
+
*/
|
|
319
|
+
ownerOf(threadId) {
|
|
320
|
+
const store = this.map.get(threadId);
|
|
321
|
+
if (store) return store.ownerId;
|
|
322
|
+
this.pruneRemovedOwners();
|
|
323
|
+
const record = this.removedOwners.get(threadId);
|
|
324
|
+
if (!record) return void 0;
|
|
325
|
+
const { ttlMs, now } = this.ownerRetention;
|
|
326
|
+
if (record.removedAt < now() - ttlMs) {
|
|
327
|
+
this.forgetOwner(threadId);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
return record.ownerId;
|
|
331
|
+
}
|
|
332
|
+
/** True when a caller acting as `owner` may run on, or claim, `threadId`. */
|
|
333
|
+
canClaim(threadId, owner) {
|
|
334
|
+
const current = this.ownerOf(threadId);
|
|
335
|
+
return current === void 0 || current === owner;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Replace the owner-record retention. Omitted fields fall back to
|
|
339
|
+
* {@link ɵOWNER_RETENTION_DEFAULTS}. Internal: for tests.
|
|
340
|
+
*/
|
|
341
|
+
ɵsetOwnerRetention(retention) {
|
|
342
|
+
this.ownerRetention = {
|
|
343
|
+
...ɵOWNER_RETENTION_DEFAULTS,
|
|
344
|
+
...retention
|
|
345
|
+
};
|
|
226
346
|
}
|
|
227
347
|
evictThreadsIfNeeded(protect) {
|
|
228
348
|
while (this.map.size > this.limits.maxThreads) if (!this.evictOneLru(protect)) break;
|
|
@@ -234,17 +354,18 @@ var ɵBoundedThreadStore = class {
|
|
|
234
354
|
console.warn(EVICTION_GUIDANCE);
|
|
235
355
|
} catch {}
|
|
236
356
|
}
|
|
237
|
-
listThreads() {
|
|
357
|
+
listThreads(scope) {
|
|
238
358
|
const threads = [];
|
|
239
359
|
for (const [threadId, store] of this.map) {
|
|
240
360
|
if (store.historicRuns.length === 0) continue;
|
|
361
|
+
if (!isVisibleTo(store, scopeOwner(scope))) continue;
|
|
241
362
|
const lastRun = store.historicRuns[store.historicRuns.length - 1];
|
|
242
363
|
threads.push({
|
|
243
364
|
id: threadId,
|
|
244
365
|
name: null,
|
|
245
366
|
agentId: lastRun.agentId,
|
|
246
367
|
organizationId: "",
|
|
247
|
-
createdById:
|
|
368
|
+
createdById: store.ownerId,
|
|
248
369
|
archived: false,
|
|
249
370
|
createdAt: new Date(store.createdAt ?? lastRun.createdAt).toISOString(),
|
|
250
371
|
updatedAt: new Date(lastRun.createdAt).toISOString()
|
|
@@ -252,8 +373,28 @@ var ɵBoundedThreadStore = class {
|
|
|
252
373
|
}
|
|
253
374
|
return threads.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());
|
|
254
375
|
}
|
|
255
|
-
|
|
376
|
+
/**
|
|
377
|
+
* Removes every thread visible to `scope`, except threads that are running
|
|
378
|
+
* or still finalizing: removing one would drop its owner record while its
|
|
379
|
+
* run still writes, so another identity could claim the id. Those threads
|
|
380
|
+
* stay, as in `evictOneLru`. A scoped clear leaves other owners' threads
|
|
381
|
+
* untouched.
|
|
382
|
+
*
|
|
383
|
+
* Without a scope the whole store is reset, running threads included. That
|
|
384
|
+
* is an internal reset for tests; `appendRun` keeps an orphaned run from
|
|
385
|
+
* writing into a thread created after the reset.
|
|
386
|
+
*/
|
|
387
|
+
clear(scope) {
|
|
388
|
+
if (scope !== void 0) {
|
|
389
|
+
for (const [threadId, store] of this.map) {
|
|
390
|
+
if (store.isRunning || store.stopRequested) continue;
|
|
391
|
+
if (isVisibleTo(store, scopeOwner(scope))) this.removeThread(threadId, store);
|
|
392
|
+
}
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
256
395
|
this.map.clear();
|
|
396
|
+
this.removedOwners.clear();
|
|
397
|
+
this.removedOwnersByOwner.clear();
|
|
257
398
|
this.totalBytes = 0;
|
|
258
399
|
this.warned = false;
|
|
259
400
|
}
|
|
@@ -295,7 +436,9 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
295
436
|
}
|
|
296
437
|
}
|
|
297
438
|
run(request) {
|
|
298
|
-
const
|
|
439
|
+
const owner = ɵresolveThreadOwner(request.ownerId);
|
|
440
|
+
if (!sharedStore.canClaim(request.threadId, owner)) throw new ThreadAccessDeniedError();
|
|
441
|
+
const store = sharedStore.getOrCreate(request.threadId, owner);
|
|
299
442
|
if (store.isRunning || store.stopRequested) {
|
|
300
443
|
if (this.onConcurrentRun !== "supersede") throw new ThreadAlreadyRunningError();
|
|
301
444
|
const priorAgent = store.agent;
|
|
@@ -360,7 +503,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
360
503
|
const ownsThread = store.currentRunId === request.input.runId;
|
|
361
504
|
if (ownsThread && (!isError || preFinalizeEventCount > 0 || finalizeControl.stopRequested)) {
|
|
362
505
|
const compactedEvents = compactEvents(currentRunEvents);
|
|
363
|
-
sharedStore.appendRun(request.threadId, {
|
|
506
|
+
sharedStore.appendRun(request.threadId, store, {
|
|
364
507
|
threadId: request.threadId,
|
|
365
508
|
runId: request.input.runId,
|
|
366
509
|
agentId: request.agent.agentId ?? "default",
|
|
@@ -416,11 +559,29 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
416
559
|
finalizeRun({ interruptionMessage: error instanceof Error ? error.message : String(error) });
|
|
417
560
|
}
|
|
418
561
|
};
|
|
419
|
-
runAgent()
|
|
562
|
+
runAgent().catch((error) => {
|
|
563
|
+
console.error("Failed to finish agent run", {
|
|
564
|
+
threadId: request.threadId,
|
|
565
|
+
runId: request.input.runId
|
|
566
|
+
}, error);
|
|
567
|
+
if (store.currentRunId === request.input.runId) {
|
|
568
|
+
store.currentEvents = null;
|
|
569
|
+
store.currentRunId = null;
|
|
570
|
+
store.agent = null;
|
|
571
|
+
store.runSubject = null;
|
|
572
|
+
store.stopRequested = false;
|
|
573
|
+
store.isRunning = false;
|
|
574
|
+
store.activeFinalize = null;
|
|
575
|
+
}
|
|
576
|
+
runSubject.complete();
|
|
577
|
+
nextSubject.complete();
|
|
578
|
+
if (store.subject === nextSubject) store.subject = null;
|
|
579
|
+
});
|
|
420
580
|
return runSubject.asObservable();
|
|
421
581
|
}
|
|
422
582
|
connect(request) {
|
|
423
|
-
const
|
|
583
|
+
const found = sharedStore.peek(request.threadId);
|
|
584
|
+
const store = found && isVisibleTo(found, ɵresolveThreadOwner(request.ownerId)) ? sharedStore.get(request.threadId, { touch: true }) : void 0;
|
|
424
585
|
const connectionSubject = new ReplaySubject(Infinity);
|
|
425
586
|
if (!store) {
|
|
426
587
|
connectionSubject.complete();
|
|
@@ -447,11 +608,13 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
447
608
|
}
|
|
448
609
|
isRunning(request) {
|
|
449
610
|
const store = sharedStore.peek(request.threadId);
|
|
450
|
-
return Promise.resolve(
|
|
611
|
+
if (!store || !isVisibleTo(store, ɵresolveThreadOwner(request.ownerId))) return Promise.resolve(false);
|
|
612
|
+
return Promise.resolve(store.isRunning);
|
|
451
613
|
}
|
|
452
614
|
stop(request) {
|
|
453
615
|
const store = sharedStore.peek(request.threadId);
|
|
454
616
|
if (!store || !store.isRunning) return Promise.resolve(false);
|
|
617
|
+
if (!isVisibleTo(store, ɵresolveThreadOwner(request.ownerId))) return Promise.resolve(false);
|
|
455
618
|
if (request.runId !== void 0 && store.currentRunId !== request.runId) return Promise.resolve(false);
|
|
456
619
|
if (store.stopRequested) return Promise.resolve(false);
|
|
457
620
|
store.stopRequested = true;
|
|
@@ -477,14 +640,16 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
477
640
|
}
|
|
478
641
|
}
|
|
479
642
|
/**
|
|
480
|
-
* Returns a summary of every thread that
|
|
643
|
+
* Returns a summary of every thread that `scope`'s owner holds in the
|
|
644
|
+
* process-wide store, which every InMemoryAgentRunner shares. Without a
|
|
645
|
+
* scope, the server owner's threads ({@link ɵSERVER_THREAD_OWNER}).
|
|
481
646
|
*
|
|
482
647
|
* This powers the local-dev fallback for `GET /threads` when the Intelligence
|
|
483
648
|
* platform is not configured. Each entry mirrors the shape of a platform
|
|
484
649
|
* `ThreadRecord` so the HTTP handler can use the same response envelope.
|
|
485
650
|
*/
|
|
486
|
-
listThreads() {
|
|
487
|
-
return sharedStore.listThreads();
|
|
651
|
+
listThreads(scope) {
|
|
652
|
+
return sharedStore.listThreads(scope);
|
|
488
653
|
}
|
|
489
654
|
/**
|
|
490
655
|
* Returns all messages for a thread, using the snapshot captured at the end
|
|
@@ -495,8 +660,8 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
495
660
|
* objects come directly from the ag-ui agent, so their shape is compatible
|
|
496
661
|
* with CopilotKit Intelligence's `ThreadMessage` type.
|
|
497
662
|
*/
|
|
498
|
-
getThreadMessages(threadId) {
|
|
499
|
-
const store =
|
|
663
|
+
getThreadMessages(threadId, scope) {
|
|
664
|
+
const store = this.peekVisible(threadId, scope);
|
|
500
665
|
if (!store) return [];
|
|
501
666
|
return [...store.messagesSnapshot];
|
|
502
667
|
}
|
|
@@ -508,8 +673,8 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
508
673
|
* the connection-replay path in {@link connect}, so the stream a
|
|
509
674
|
* late-joining inspector sees matches what this method returns.
|
|
510
675
|
*/
|
|
511
|
-
getThreadEvents(threadId) {
|
|
512
|
-
const store =
|
|
676
|
+
getThreadEvents(threadId, scope) {
|
|
677
|
+
const store = this.peekVisible(threadId, scope);
|
|
513
678
|
if (!store || store.historicRuns.length === 0) return [];
|
|
514
679
|
const all = [];
|
|
515
680
|
for (const run of store.historicRuns) all.push(...run.events);
|
|
@@ -525,8 +690,8 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
525
690
|
*
|
|
526
691
|
* Returns `null` when the thread has never emitted a STATE_SNAPSHOT.
|
|
527
692
|
*/
|
|
528
|
-
getThreadState(threadId) {
|
|
529
|
-
const events = this.getThreadEvents(threadId);
|
|
693
|
+
getThreadState(threadId, scope) {
|
|
694
|
+
const events = this.getThreadEvents(threadId, scope);
|
|
530
695
|
for (let i = events.length - 1; i >= 0; i--) {
|
|
531
696
|
const event = events[i];
|
|
532
697
|
if (event.type === EventType.STATE_SNAPSHOT) {
|
|
@@ -538,7 +703,9 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
538
703
|
return null;
|
|
539
704
|
}
|
|
540
705
|
/**
|
|
541
|
-
* Clears
|
|
706
|
+
* Clears the in-memory history of `scope`'s threads (without a scope, the
|
|
707
|
+
* server owner's). A thread with a run still active or finalizing is kept;
|
|
708
|
+
* see {@link ɵBoundedThreadStore.clear}.
|
|
542
709
|
*
|
|
543
710
|
* Powers the local-dev fallback for `POST /threads/clear`, letting consumers
|
|
544
711
|
* (e.g. the demo's Clear button) reset to an empty thread list without
|
|
@@ -546,11 +713,23 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
546
713
|
* platform path: there, thread history lives in a real database and must
|
|
547
714
|
* not be wiped this way.
|
|
548
715
|
*/
|
|
549
|
-
clearThreads() {
|
|
550
|
-
sharedStore.clear();
|
|
716
|
+
clearThreads(scope) {
|
|
717
|
+
sharedStore.clear({ ownerId: scopeOwner(scope) });
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* False when the thread id is held by an owner other than `scope`'s: a
|
|
721
|
+
* live thread, or the retained owner of an evicted or cleared one.
|
|
722
|
+
*/
|
|
723
|
+
isThreadAccessible(threadId, scope) {
|
|
724
|
+
return sharedStore.canClaim(threadId, scopeOwner(scope));
|
|
725
|
+
}
|
|
726
|
+
/** The thread's store, or `undefined` when unknown or not visible to `scope`. */
|
|
727
|
+
peekVisible(threadId, scope) {
|
|
728
|
+
const store = sharedStore.peek(threadId);
|
|
729
|
+
return store && isVisibleTo(store, scopeOwner(scope)) ? store : void 0;
|
|
551
730
|
}
|
|
552
731
|
};
|
|
553
732
|
|
|
554
733
|
//#endregion
|
|
555
|
-
export { InMemoryAgentRunner, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵestimateBytes, ɵnormalizeLimits };
|
|
734
|
+
export { InMemoryAgentRunner, ɵBoundedThreadStore, ɵGLOBAL_STORE, ɵINMEMORY_DEFAULTS, ɵOWNER_RETENTION_DEFAULTS, ɵestimateBytes, ɵnormalizeLimits };
|
|
556
735
|
//# sourceMappingURL=in-memory.mjs.map
|