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