@langchain/svelte 0.1.3 → 0.3.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/index.js CHANGED
@@ -1,472 +1,279 @@
1
1
  import { useStreamCustom } from "./stream.custom.js";
2
- import { derived, get, writable } from "svelte/store";
3
- import { onDestroy, onMount } from "svelte";
4
- import { FetchStreamTransport, MessageTupleManager, PendingRunsTracker, StreamError, StreamManager, SubagentManager, calculateDepthFromNamespace, ensureHistoryMessageInstances, ensureMessageInstances, extractInterrupts, extractParentIdFromNamespace, extractToolCallIdFromNamespace, filterStream, getBranchContext, getMessagesMetadataMap, isSubagentNamespace, toMessageClass } from "@langchain/langgraph-sdk/ui";
2
+ import { derived, fromStore, writable } from "svelte/store";
3
+ import { getContext, onDestroy, onMount, setContext } from "svelte";
4
+ import { FetchStreamTransport, StreamOrchestrator, SubagentManager, calculateDepthFromNamespace, ensureMessageInstances, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace } from "@langchain/langgraph-sdk/ui";
5
5
  import { Client } from "@langchain/langgraph-sdk";
6
- import { getToolCallsWithResults } from "@langchain/langgraph-sdk/utils";
7
6
  //#region src/index.ts
8
- function fetchHistory(client, threadId, options) {
9
- if (options?.limit === false) return client.threads.getState(threadId).then((state) => {
10
- if (state.checkpoint == null) return [];
11
- return [state];
12
- });
13
- const limit = typeof options?.limit === "number" ? options.limit : 10;
14
- return client.threads.getHistory(threadId, { limit });
7
+ const STREAM_CONTEXT_KEY = Symbol.for("langchain:stream-context");
8
+ /**
9
+ * Provides a `useStream` return value to all descendant components via
10
+ * Svelte's context API. Must be called during component initialisation
11
+ * (i.e. at the top level of a `<script>` block).
12
+ *
13
+ * @example
14
+ * ```svelte
15
+ * <script lang="ts">
16
+ * import { useStream, setStreamContext } from "@langchain/svelte";
17
+ *
18
+ * const stream = useStream({ assistantId: "agent", apiUrl: "..." });
19
+ * setStreamContext(stream);
20
+ * <\/script>
21
+ *
22
+ * <ChildComponent />
23
+ * ```
24
+ */
25
+ function setStreamContext(stream) {
26
+ setContext(STREAM_CONTEXT_KEY, stream);
27
+ return stream;
28
+ }
29
+ /**
30
+ * Retrieves the `useStream` instance previously provided by a parent
31
+ * component via {@link setStreamContext} or {@link provideStream}.
32
+ * Must be called during component initialisation.
33
+ *
34
+ * @throws If no stream context has been set by an ancestor component.
35
+ *
36
+ * @example
37
+ * ```svelte
38
+ * <script lang="ts">
39
+ * import { getStreamContext } from "@langchain/svelte";
40
+ *
41
+ * const stream = getStreamContext();
42
+ * <\/script>
43
+ * ```
44
+ */
45
+ function getStreamContext() {
46
+ const ctx = getContext(STREAM_CONTEXT_KEY);
47
+ if (!ctx) throw new Error("getStreamContext must be used within a component that has called setStreamContext");
48
+ return ctx;
49
+ }
50
+ /**
51
+ * Creates a shared `useStream` instance and makes it available to all
52
+ * descendant components via Svelte's `setContext`/`getContext`.
53
+ *
54
+ * Call this in a parent component's `<script>` block. Children access
55
+ * the shared stream via {@link getStream}.
56
+ *
57
+ * Uses the same context key as {@link setStreamContext}/{@link getStreamContext},
58
+ * so both retrieval functions work interchangeably.
59
+ *
60
+ * @example
61
+ * ```svelte
62
+ * <!-- ChatContainer.svelte -->
63
+ * <script lang="ts">
64
+ * import { provideStream } from "@langchain/svelte";
65
+ *
66
+ * provideStream({
67
+ * assistantId: "agent",
68
+ * apiUrl: "http://localhost:2024",
69
+ * });
70
+ * <\/script>
71
+ *
72
+ * <ChatHeader />
73
+ * <MessageList />
74
+ * <MessageInput />
75
+ * ```
76
+ *
77
+ * @returns The stream instance (same as calling `useStream` directly).
78
+ */
79
+ function provideStream(options) {
80
+ const stream = useStream(options);
81
+ setContext(STREAM_CONTEXT_KEY, stream);
82
+ return stream;
83
+ }
84
+ /**
85
+ * Retrieves the shared stream instance from the nearest ancestor that
86
+ * called {@link provideStream} or {@link setStreamContext}.
87
+ *
88
+ * Throws if no ancestor has provided a stream.
89
+ *
90
+ * @example
91
+ * ```svelte
92
+ * <!-- MessageList.svelte -->
93
+ * <script lang="ts">
94
+ * import { getStream } from "@langchain/svelte";
95
+ *
96
+ * const stream = getStream();
97
+ * <\/script>
98
+ *
99
+ * {#each stream.messages as msg (msg.id)}
100
+ * <div>{msg.content}</div>
101
+ * {/each}
102
+ * ```
103
+ */
104
+ function getStream() {
105
+ const context = getContext(STREAM_CONTEXT_KEY);
106
+ if (context == null) throw new Error("getStream() requires a parent component to call provideStream(). Add provideStream({ assistantId: '...' }) in an ancestor component.");
107
+ return context;
15
108
  }
16
109
  function useStream(options) {
17
110
  if ("transport" in options) return useStreamCustom(options);
18
111
  return useStreamLGP(options);
19
112
  }
20
113
  function useStreamLGP(options) {
21
- const runMetadataStorage = (() => {
22
- if (typeof window === "undefined") return null;
23
- const storage = options.reconnectOnMount;
24
- if (storage === true) return window.sessionStorage;
25
- if (typeof storage === "function") return storage();
26
- return null;
27
- })();
28
- const getMessages = (value) => {
29
- const messagesKey = options.messagesKey ?? "messages";
30
- return Array.isArray(value[messagesKey]) ? value[messagesKey] : [];
31
- };
32
- const setMessages = (current, messages) => {
33
- const messagesKey = options.messagesKey ?? "messages";
34
- return {
35
- ...current,
36
- [messagesKey]: messages
37
- };
38
- };
39
- const historyLimit = typeof options.fetchStateHistory === "object" && options.fetchStateHistory != null ? options.fetchStateHistory.limit ?? false : options.fetchStateHistory ?? false;
40
- const threadId = writable(void 0);
41
- let threadIdPromise = null;
42
114
  const client = options.client ?? new Client({ apiUrl: options.apiUrl });
43
- const history = writable({
44
- data: void 0,
45
- error: void 0,
46
- isLoading: false,
47
- mutate: async () => void 0
115
+ const orchestrator = new StreamOrchestrator(options, {
116
+ getClient: () => client,
117
+ getAssistantId: () => options.assistantId,
118
+ getMessagesKey: () => options.messagesKey ?? "messages"
48
119
  });
49
- async function mutate(mutateId) {
50
- const tid = mutateId ?? get(threadId);
51
- if (!tid) return void 0;
52
- try {
53
- const data = await fetchHistory(client, tid, { limit: historyLimit });
54
- history.set({
55
- data,
56
- error: void 0,
57
- isLoading: false,
58
- mutate
59
- });
60
- return data;
61
- } catch (err) {
62
- history.update((prev) => ({
63
- ...prev,
64
- error: err,
65
- isLoading: false
66
- }));
67
- options.onError?.(err, void 0);
68
- return;
69
- }
70
- }
71
- history.update((prev) => ({
72
- ...prev,
73
- mutate
74
- }));
75
- const branch = writable("");
76
- const branchContext = derived([branch, history], ([$branch, $history]) => getBranchContext($branch, $history.data ?? void 0));
77
- const messageManager = new MessageTupleManager();
78
- const stream = new StreamManager(messageManager, {
79
- throttle: options.throttle ?? false,
80
- subagentToolNames: options.subagentToolNames,
81
- filterSubagentMessages: options.filterSubagentMessages,
82
- toMessage: toMessageClass
83
- });
84
- const pendingRuns = new PendingRunsTracker();
85
- const historyValues = derived([branchContext], ([$branchContext]) => $branchContext.threadHead?.values ?? options.initialValues ?? {});
86
- const historyError = derived([branchContext], ([$branchContext]) => {
87
- const error = $branchContext.threadHead?.tasks?.at(-1)?.error;
88
- if (error == null) return void 0;
89
- try {
90
- const parsed = JSON.parse(error);
91
- if (StreamError.isStructuredError(parsed)) return new StreamError(parsed);
92
- return parsed;
93
- } catch {}
94
- return error;
95
- });
96
- const streamValues = writable(stream.values);
97
- const streamError = writable(stream.error);
98
- const isLoading = writable(stream.isLoading);
99
- const queueEntries = writable(pendingRuns.entries);
100
- const queueSize = writable(pendingRuns.size);
101
- const values = derived([streamValues, historyValues], ([$streamValues, $historyValues]) => $streamValues ?? $historyValues);
102
- const error = derived([
103
- streamError,
104
- historyError,
105
- history
106
- ], ([$streamError, $historyError, $history]) => $streamError ?? $historyError ?? $history.error);
107
- const messageMetadata = derived([history, branchContext], ([$history, $branchContext]) => getMessagesMetadataMap({
108
- initialValues: options.initialValues,
109
- history: $history.data,
110
- getMessages,
111
- branchContext: $branchContext
112
- }));
113
- const subagentVersion = writable(0);
114
- const unsubscribe = stream.subscribe(() => {
115
- streamValues.set(stream.values);
116
- streamError.set(stream.error);
117
- isLoading.set(stream.isLoading);
118
- subagentVersion.update((v) => v + 1);
120
+ orchestrator.initThreadId(options.threadId ?? void 0);
121
+ const version = writable(0);
122
+ const unsubscribe = orchestrator.subscribe(() => {
123
+ version.update((v) => v + 1);
119
124
  });
120
- const unsubQueue = pendingRuns.subscribe(() => {
121
- queueEntries.set(pendingRuns.entries);
122
- queueSize.set(pendingRuns.size);
123
- });
124
- const unsubReconstruct = derived([isLoading, history], ([$isLoading, $history]) => {
125
+ let fetchController = null;
126
+ const unsubReconstruct = derived(version, () => {
127
+ const hvMessages = orchestrator.messages;
125
128
  if (!options.filterSubagentMessages) return false;
126
- if ($isLoading || $history.isLoading) return false;
127
- return getMessages(get(historyValues)).length > 0;
129
+ if (orchestrator.isLoading || orchestrator.historyData.isLoading) return false;
130
+ return hvMessages.length > 0;
128
131
  }).subscribe(($should) => {
129
132
  if ($should) {
130
- const hvMessages = getMessages(get(historyValues));
131
- stream.reconstructSubagents(hvMessages, { skipIfPopulated: true });
133
+ fetchController?.abort();
134
+ fetchController = orchestrator.reconstructSubagentsIfNeeded();
135
+ }
136
+ });
137
+ const unsubDrain = derived(version, () => orchestrator.isLoading).subscribe(() => {
138
+ orchestrator.drainQueue();
139
+ });
140
+ let { shouldReconnect } = orchestrator;
141
+ onMount(() => {
142
+ if (shouldReconnect) {
143
+ if (orchestrator.tryReconnect()) shouldReconnect = false;
132
144
  }
133
145
  });
134
146
  onDestroy(() => {
147
+ fetchController?.abort();
135
148
  unsubscribe();
136
149
  unsubReconstruct();
137
- unsubQueue();
150
+ unsubDrain();
151
+ orchestrator.dispose();
138
152
  });
139
- function stop() {
140
- return stream.stop(get(historyValues), { onStop: (args) => {
141
- const tid = get(threadId);
142
- if (runMetadataStorage && tid) {
143
- const runId = runMetadataStorage.getItem(`lg:stream:${tid}`);
144
- if (runId) client.runs.cancel(tid, runId);
145
- runMetadataStorage.removeItem(`lg:stream:${tid}`);
146
- }
147
- options.onStop?.(args);
148
- } });
149
- }
150
- function setBranch(value) {
151
- branch.set(value);
152
- }
153
- function submitDirect(values, submitOptions) {
154
- const currentBranchContext = get(branchContext);
155
- const checkpointId = submitOptions?.checkpoint?.checkpoint_id;
156
- branch.set(checkpointId != null ? currentBranchContext.branchByCheckpoint[checkpointId]?.branch ?? "" : "");
157
- const includeImplicitBranch = historyLimit === true || typeof historyLimit === "number";
158
- const shouldRefetch = options.onFinish != null || includeImplicitBranch;
159
- let checkpoint = submitOptions?.checkpoint ?? (includeImplicitBranch ? currentBranchContext.threadHead?.checkpoint : void 0) ?? void 0;
160
- if (submitOptions?.checkpoint === null) checkpoint = void 0;
161
- if (checkpoint != null) delete checkpoint.thread_id;
162
- let callbackMeta;
163
- let rejoinKey;
164
- let usableThreadId;
165
- return stream.start(async (signal) => {
166
- usableThreadId = get(threadId);
167
- if (!usableThreadId) {
168
- const threadPromise = client.threads.create({
169
- threadId: submitOptions?.threadId,
170
- metadata: submitOptions?.metadata
171
- });
172
- threadIdPromise = threadPromise.then((t) => t.thread_id);
173
- usableThreadId = (await threadPromise).thread_id;
174
- threadId.set(usableThreadId);
175
- options.onThreadId?.(usableThreadId);
176
- }
177
- const streamMode = [
178
- "values",
179
- "messages-tuple",
180
- "updates",
181
- ...submitOptions?.streamMode ?? []
182
- ];
183
- if (options.onUpdateEvent && !streamMode.includes("updates")) streamMode.push("updates");
184
- if (options.onCustomEvent && !streamMode.includes("custom")) streamMode.push("custom");
185
- if (options.onCheckpointEvent && !streamMode.includes("checkpoints")) streamMode.push("checkpoints");
186
- if (options.onTaskEvent && !streamMode.includes("tasks")) streamMode.push("tasks");
187
- if ("onDebugEvent" in options && options.onDebugEvent && !streamMode.includes("debug")) streamMode.push("debug");
188
- if ("onLangChainEvent" in options && options.onLangChainEvent && !streamMode.includes("events")) streamMode.push("events");
189
- stream.setStreamValues(() => {
190
- const prev = {
191
- ...get(historyValues),
192
- ...stream.values
193
- };
194
- if (submitOptions?.optimisticValues != null) return {
195
- ...prev,
196
- ...typeof submitOptions.optimisticValues === "function" ? submitOptions.optimisticValues(prev) : submitOptions.optimisticValues
197
- };
198
- return { ...prev };
199
- });
200
- const streamResumable = submitOptions?.streamResumable ?? !!runMetadataStorage;
201
- return client.runs.stream(usableThreadId, options.assistantId, {
202
- input: values,
203
- config: submitOptions?.config,
204
- context: submitOptions?.context,
205
- command: submitOptions?.command,
206
- interruptBefore: submitOptions?.interruptBefore,
207
- interruptAfter: submitOptions?.interruptAfter,
208
- metadata: submitOptions?.metadata,
209
- multitaskStrategy: submitOptions?.multitaskStrategy,
210
- onCompletion: submitOptions?.onCompletion,
211
- onDisconnect: submitOptions?.onDisconnect ?? (streamResumable ? "continue" : "cancel"),
212
- signal,
213
- checkpoint,
214
- streamMode,
215
- streamSubgraphs: submitOptions?.streamSubgraphs,
216
- streamResumable,
217
- durability: submitOptions?.durability,
218
- onRunCreated(params) {
219
- callbackMeta = {
220
- run_id: params.run_id,
221
- thread_id: params.thread_id ?? usableThreadId
222
- };
223
- if (runMetadataStorage) {
224
- rejoinKey = `lg:stream:${usableThreadId}`;
225
- runMetadataStorage.setItem(rejoinKey, callbackMeta.run_id);
226
- }
227
- options.onCreated?.(callbackMeta);
228
- }
229
- });
230
- }, {
231
- getMessages,
232
- setMessages,
233
- initialValues: get(historyValues),
234
- callbacks: options,
235
- async onSuccess() {
236
- if (rejoinKey) runMetadataStorage?.removeItem(rejoinKey);
237
- if (shouldRefetch && usableThreadId) {
238
- const lastHead = (await mutate(usableThreadId))?.at(0);
239
- if (lastHead) {
240
- options.onFinish?.(lastHead, callbackMeta);
241
- return null;
242
- }
243
- }
244
- },
245
- onError(error) {
246
- options.onError?.(error, callbackMeta);
247
- submitOptions?.onError?.(error, callbackMeta);
248
- },
249
- onFinish: () => {}
250
- });
251
- }
252
- let submitting = false;
253
- function drainQueue() {
254
- if (!get(isLoading) && !submitting && pendingRuns.size > 0) {
255
- const next = pendingRuns.shift();
256
- if (next) {
257
- submitting = true;
258
- joinStream(next.id).finally(() => {
259
- submitting = false;
260
- drainQueue();
261
- });
262
- }
263
- }
264
- }
265
- isLoading.subscribe(() => {
266
- drainQueue();
153
+ const valuesStore = derived(version, () => {
154
+ orchestrator.trackStreamMode("values");
155
+ return orchestrator.values;
267
156
  });
268
- async function submit(values, submitOptions) {
269
- if (stream.isLoading || submitting) {
270
- if (submitOptions?.multitaskStrategy === "interrupt" || submitOptions?.multitaskStrategy === "rollback") {
271
- submitting = true;
272
- try {
273
- await submitDirect(values, submitOptions);
274
- } finally {
275
- submitting = false;
276
- }
277
- return;
278
- }
279
- let usableThreadId = get(threadId);
280
- if (!usableThreadId && threadIdPromise) usableThreadId = await threadIdPromise;
281
- if (usableThreadId) {
282
- try {
283
- const run = await client.runs.create(usableThreadId, options.assistantId, {
284
- input: values,
285
- config: submitOptions?.config,
286
- context: submitOptions?.context,
287
- command: submitOptions?.command,
288
- interruptBefore: submitOptions?.interruptBefore,
289
- interruptAfter: submitOptions?.interruptAfter,
290
- metadata: submitOptions?.metadata,
291
- multitaskStrategy: "enqueue",
292
- streamResumable: true,
293
- streamSubgraphs: submitOptions?.streamSubgraphs,
294
- durability: submitOptions?.durability
295
- });
296
- pendingRuns.add({
297
- id: run.run_id,
298
- values,
299
- options: submitOptions,
300
- createdAt: new Date(run.created_at)
301
- });
302
- } catch (error) {
303
- options.onError?.(error, void 0);
304
- submitOptions?.onError?.(error, void 0);
305
- }
306
- return;
307
- }
308
- }
309
- submitting = true;
310
- const result = submitDirect(values, submitOptions);
311
- Promise.resolve(result).finally(() => {
312
- submitting = false;
313
- drainQueue();
314
- });
315
- return result;
316
- }
317
- async function joinStream(runId, lastEventId, joinOptions) {
318
- lastEventId ??= "-1";
319
- const tid = get(threadId);
320
- if (!tid) return;
321
- const callbackMeta = {
322
- thread_id: tid,
323
- run_id: runId
324
- };
325
- await stream.start(async (signal) => {
326
- const rawStream = client.runs.joinStream(tid, runId, {
327
- signal,
328
- lastEventId,
329
- streamMode: joinOptions?.streamMode
330
- });
331
- return joinOptions?.filter != null ? filterStream(rawStream, joinOptions.filter) : rawStream;
332
- }, {
333
- getMessages,
334
- setMessages,
335
- initialValues: get(historyValues),
336
- callbacks: options,
337
- async onSuccess() {
338
- runMetadataStorage?.removeItem(`lg:stream:${tid}`);
339
- const lastHead = (await mutate(tid))?.at(0);
340
- if (lastHead) options.onFinish?.(lastHead, callbackMeta);
341
- },
342
- onError(error) {
343
- options.onError?.(error, callbackMeta);
344
- },
345
- onFinish: () => {}
346
- });
347
- }
348
- let shouldReconnect = !!runMetadataStorage;
349
- onMount(() => {
350
- const tid = get(threadId);
351
- if (shouldReconnect && runMetadataStorage && tid) {
352
- const runId = runMetadataStorage.getItem(`lg:stream:${tid}`);
353
- if (runId) {
354
- shouldReconnect = false;
355
- joinStream(runId);
356
- }
357
- }
358
- });
359
- const messages = derived([streamValues, historyValues], ([$streamValues, $historyValues]) => ensureMessageInstances(getMessages($streamValues ?? $historyValues)));
360
- const interrupt = derived([
361
- streamValues,
362
- streamError,
363
- branchContext,
364
- isLoading
365
- ], ([$streamValues, $streamError, $branchContext, $isLoading]) => {
366
- return extractInterrupts($streamValues, {
367
- isLoading: $isLoading,
368
- threadState: $branchContext.threadHead,
369
- error: $streamError
370
- });
157
+ const errorStore = derived(version, () => orchestrator.error);
158
+ const isLoadingStore = derived(version, () => orchestrator.isLoading);
159
+ const branchStore = derived(version, () => orchestrator.branch);
160
+ const messagesStore = derived(version, () => {
161
+ orchestrator.trackStreamMode("messages-tuple", "values");
162
+ return ensureMessageInstances(orchestrator.messages);
371
163
  });
372
- const interrupts = derived([
373
- streamValues,
374
- streamError,
375
- branchContext,
376
- isLoading
377
- ], ([$streamValues, $streamError, $branchContext, $isLoading]) => {
378
- const vals = $streamValues ?? get(historyValues);
379
- if (vals != null && "__interrupt__" in vals && Array.isArray(vals.__interrupt__)) {
380
- const valueInterrupts = vals.__interrupt__;
381
- if (valueInterrupts.length === 0) return [{ when: "breakpoint" }];
382
- return valueInterrupts;
383
- }
384
- if ($isLoading) return [];
385
- const allInterrupts = ($branchContext.threadHead?.tasks ?? []).flatMap((t) => t.interrupts ?? []);
386
- if (allInterrupts.length > 0) return allInterrupts;
387
- if (!($branchContext.threadHead?.next ?? []).length || $streamError != null) return [];
388
- return [{ when: "breakpoint" }];
164
+ const toolCallsStore = derived(version, () => {
165
+ orchestrator.trackStreamMode("messages-tuple", "values");
166
+ return orchestrator.toolCalls;
389
167
  });
390
- const toolCalls = derived([streamValues, historyValues], ([$streamValues, $historyValues]) => getToolCallsWithResults(getMessages($streamValues ?? $historyValues)));
391
- function getToolCalls(message) {
392
- return getToolCallsWithResults(getMessages(get(streamValues) ?? get(historyValues))).filter((tc) => tc.aiMessage.id === message.id);
393
- }
394
- const historyList = derived([branchContext], ([$branchContext]) => {
395
- if (historyLimit === false) throw new Error("`fetchStateHistory` must be set to `true` to use `history`");
396
- return ensureHistoryMessageInstances($branchContext.flatHistory, options.messagesKey ?? "messages");
168
+ const interruptStore = derived(version, () => orchestrator.interrupt);
169
+ const interruptsStore = derived(version, () => orchestrator.interrupts);
170
+ const historyListStore = derived(version, () => orchestrator.flatHistory);
171
+ const isThreadLoadingStore = derived(version, () => orchestrator.isThreadLoading);
172
+ const experimentalBranchTreeStore = derived(version, () => orchestrator.experimental_branchTree);
173
+ const subagentsStore = derived(version, () => {
174
+ orchestrator.trackStreamMode("updates", "messages-tuple");
175
+ return orchestrator.subagents;
397
176
  });
398
- const isThreadLoading = derived([history], ([$history]) => $history.isLoading && $history.data == null);
399
- const experimentalBranchTree = derived([branchContext], ([$branchContext]) => {
400
- if (historyLimit === false) throw new Error("`fetchStateHistory` must be set to `true` to use `experimental_branchTree`");
401
- return $branchContext.branchTree;
177
+ const activeSubagentsStore = derived(version, () => {
178
+ orchestrator.trackStreamMode("updates", "messages-tuple");
179
+ return orchestrator.activeSubagents;
402
180
  });
403
- function getMessagesMetadata(message, index) {
404
- const streamMetadata = messageManager.get(message.id)?.metadata;
405
- const historyMetadata = get(messageMetadata)?.find((m) => m.messageId === (message.id ?? index));
406
- if (streamMetadata != null || historyMetadata != null) return {
407
- ...historyMetadata,
408
- streamMetadata
409
- };
410
- }
181
+ const queueEntriesStore = derived(version, () => orchestrator.queueEntries);
182
+ const queueSizeStore = derived(version, () => orchestrator.queueSize);
183
+ const valuesRef = fromStore(valuesStore);
184
+ const errorRef = fromStore(errorStore);
185
+ const isLoadingRef = fromStore(isLoadingStore);
186
+ const branchRef = fromStore(branchStore);
187
+ const messagesRef = fromStore(messagesStore);
188
+ const toolCallsRef = fromStore(toolCallsStore);
189
+ const interruptRef = fromStore(interruptStore);
190
+ const interruptsRef = fromStore(interruptsStore);
191
+ const historyListRef = fromStore(historyListStore);
192
+ const isThreadLoadingRef = fromStore(isThreadLoadingStore);
193
+ const experimentalBranchTreeRef = fromStore(experimentalBranchTreeStore);
194
+ const subagentsRef = fromStore(subagentsStore);
195
+ const activeSubagentsRef = fromStore(activeSubagentsStore);
196
+ const queueEntriesRef = fromStore(queueEntriesStore);
197
+ const queueSizeRef = fromStore(queueSizeStore);
411
198
  return {
412
199
  assistantId: options.assistantId,
413
200
  client,
414
- values,
415
- error,
416
- isLoading,
417
- isThreadLoading,
418
- branch,
419
- setBranch,
420
- messages,
421
- toolCalls,
422
- getToolCalls,
423
- interrupt,
424
- interrupts,
425
- history: historyList,
426
- experimental_branchTree: experimentalBranchTree,
427
- getMessagesMetadata,
428
- submit,
429
- stop,
430
- joinStream,
201
+ get values() {
202
+ return valuesRef.current;
203
+ },
204
+ get error() {
205
+ return errorRef.current;
206
+ },
207
+ get isLoading() {
208
+ return isLoadingRef.current;
209
+ },
210
+ get isThreadLoading() {
211
+ return isThreadLoadingRef.current;
212
+ },
213
+ get branch() {
214
+ return branchRef.current;
215
+ },
216
+ setBranch(value) {
217
+ orchestrator.setBranch(value);
218
+ },
219
+ get messages() {
220
+ return messagesRef.current;
221
+ },
222
+ get toolCalls() {
223
+ return toolCallsRef.current;
224
+ },
225
+ getToolCalls(message) {
226
+ return orchestrator.getToolCalls(message);
227
+ },
228
+ get interrupt() {
229
+ return interruptRef.current;
230
+ },
231
+ get interrupts() {
232
+ return interruptsRef.current;
233
+ },
234
+ get history() {
235
+ return historyListRef.current;
236
+ },
237
+ get experimental_branchTree() {
238
+ return experimentalBranchTreeRef.current;
239
+ },
240
+ getMessagesMetadata(message, index) {
241
+ return orchestrator.getMessagesMetadata(message, index);
242
+ },
243
+ submit: (...args) => orchestrator.submit(...args),
244
+ stop: () => orchestrator.stop(),
245
+ joinStream: (...args) => orchestrator.joinStream(...args),
431
246
  queue: {
432
- entries: queueEntries,
433
- size: queueSize,
434
- async cancel(id) {
435
- const tid = get(threadId);
436
- const removed = pendingRuns.remove(id);
437
- if (removed && tid) await client.runs.cancel(tid, id);
438
- return removed;
247
+ get entries() {
248
+ return queueEntriesRef.current;
249
+ },
250
+ get size() {
251
+ return queueSizeRef.current;
439
252
  },
440
- async clear() {
441
- const tid = get(threadId);
442
- const removed = pendingRuns.removeAll();
443
- if (tid && removed.length > 0) await Promise.all(removed.map((e) => client.runs.cancel(tid, e.id)));
444
- }
253
+ cancel: (id) => orchestrator.cancelQueueItem(id),
254
+ clear: () => orchestrator.clearQueue()
445
255
  },
446
256
  switchThread(newThreadId) {
447
- if (newThreadId !== (get(threadId) ?? null)) {
448
- const prevThreadId = get(threadId);
449
- threadId.set(newThreadId ?? void 0);
450
- stream.clear();
451
- const removed = pendingRuns.removeAll();
452
- if (prevThreadId && removed.length > 0) Promise.all(removed.map((e) => client.runs.cancel(prevThreadId, e.id)));
453
- if (newThreadId != null) options.onThreadId?.(newThreadId);
454
- }
257
+ orchestrator.switchThread(newThreadId);
258
+ },
259
+ get subagents() {
260
+ return subagentsRef.current;
261
+ },
262
+ get activeSubagents() {
263
+ return activeSubagentsRef.current;
455
264
  },
456
- subagents: derived(subagentVersion, () => stream.getSubagents()),
457
- activeSubagents: derived(subagentVersion, () => stream.getActiveSubagents()),
458
265
  getSubagent(toolCallId) {
459
- return stream.getSubagent(toolCallId);
266
+ return orchestrator.getSubagent(toolCallId);
460
267
  },
461
268
  getSubagentsByType(type) {
462
- return stream.getSubagentsByType(type);
269
+ return orchestrator.getSubagentsByType(type);
463
270
  },
464
271
  getSubagentsByMessage(messageId) {
465
- return stream.getSubagentsByMessage(messageId);
272
+ return orchestrator.getSubagentsByMessage(messageId);
466
273
  }
467
274
  };
468
275
  }
469
276
  //#endregion
470
- export { FetchStreamTransport, SubagentManager, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, isSubagentNamespace, useStream };
277
+ export { FetchStreamTransport, SubagentManager, calculateDepthFromNamespace, extractParentIdFromNamespace, extractToolCallIdFromNamespace, getStream, getStreamContext, isSubagentNamespace, provideStream, setStreamContext, useStream };
471
278
 
472
279
  //# sourceMappingURL=index.js.map