@langchain/svelte 0.2.0 → 0.3.1
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 +144 -438
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -30
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +54 -30
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +144 -438
- package/dist/index.js.map +1 -1
- package/dist/stream.custom.cjs +34 -160
- package/dist/stream.custom.cjs.map +1 -1
- package/dist/stream.custom.js +36 -162
- package/dist/stream.custom.js.map +1 -1
- package/package.json +3 -3
- package/dist/context.cjs +0 -86
- package/dist/context.cjs.map +0 -1
- package/dist/context.d.cts +0 -77
- package/dist/context.d.cts.map +0 -1
- package/dist/context.d.ts +0 -77
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -85
- package/dist/context.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_stream_custom = require("./stream.custom.cjs");
|
|
3
|
-
const require_context = require("./context.cjs");
|
|
4
3
|
let svelte_store = require("svelte/store");
|
|
5
4
|
let svelte = require("svelte");
|
|
6
5
|
let _langchain_langgraph_sdk_ui = require("@langchain/langgraph-sdk/ui");
|
|
7
6
|
let _langchain_langgraph_sdk = require("@langchain/langgraph-sdk");
|
|
8
|
-
let _langchain_langgraph_sdk_utils = require("@langchain/langgraph-sdk/utils");
|
|
9
7
|
//#region src/index.ts
|
|
10
8
|
const STREAM_CONTEXT_KEY = Symbol.for("langchain:stream-context");
|
|
11
9
|
/**
|
|
@@ -31,8 +29,8 @@ function setStreamContext(stream) {
|
|
|
31
29
|
}
|
|
32
30
|
/**
|
|
33
31
|
* Retrieves the `useStream` instance previously provided by a parent
|
|
34
|
-
* component via {@link setStreamContext}
|
|
35
|
-
* initialisation.
|
|
32
|
+
* component via {@link setStreamContext} or {@link provideStream}.
|
|
33
|
+
* Must be called during component initialisation.
|
|
36
34
|
*
|
|
37
35
|
* @throws If no stream context has been set by an ancestor component.
|
|
38
36
|
*
|
|
@@ -50,436 +48,154 @@ function getStreamContext() {
|
|
|
50
48
|
if (!ctx) throw new Error("getStreamContext must be used within a component that has called setStreamContext");
|
|
51
49
|
return ctx;
|
|
52
50
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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;
|
|
60
109
|
}
|
|
61
110
|
function useStream(options) {
|
|
62
111
|
if ("transport" in options) return require_stream_custom.useStreamCustom(options);
|
|
63
112
|
return useStreamLGP(options);
|
|
64
113
|
}
|
|
65
114
|
function useStreamLGP(options) {
|
|
66
|
-
const runMetadataStorage = (() => {
|
|
67
|
-
if (typeof window === "undefined") return null;
|
|
68
|
-
const storage = options.reconnectOnMount;
|
|
69
|
-
if (storage === true) return window.sessionStorage;
|
|
70
|
-
if (typeof storage === "function") return storage();
|
|
71
|
-
return null;
|
|
72
|
-
})();
|
|
73
|
-
const getMessages = (value) => {
|
|
74
|
-
const messagesKey = options.messagesKey ?? "messages";
|
|
75
|
-
return Array.isArray(value[messagesKey]) ? value[messagesKey] : [];
|
|
76
|
-
};
|
|
77
|
-
const setMessages = (current, messages) => {
|
|
78
|
-
const messagesKey = options.messagesKey ?? "messages";
|
|
79
|
-
return {
|
|
80
|
-
...current,
|
|
81
|
-
[messagesKey]: messages
|
|
82
|
-
};
|
|
83
|
-
};
|
|
84
|
-
const historyLimit = typeof options.fetchStateHistory === "object" && options.fetchStateHistory != null ? options.fetchStateHistory.limit ?? false : options.fetchStateHistory ?? false;
|
|
85
|
-
const threadId = (0, svelte_store.writable)(void 0);
|
|
86
|
-
let threadIdPromise = null;
|
|
87
115
|
const client = options.client ?? new _langchain_langgraph_sdk.Client({ apiUrl: options.apiUrl });
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
mutate: async () => void 0
|
|
93
|
-
});
|
|
94
|
-
async function mutate(mutateId) {
|
|
95
|
-
const tid = mutateId ?? (0, svelte_store.get)(threadId);
|
|
96
|
-
if (!tid) return void 0;
|
|
97
|
-
try {
|
|
98
|
-
const data = await fetchHistory(client, tid, { limit: historyLimit });
|
|
99
|
-
history.set({
|
|
100
|
-
data,
|
|
101
|
-
error: void 0,
|
|
102
|
-
isLoading: false,
|
|
103
|
-
mutate
|
|
104
|
-
});
|
|
105
|
-
return data;
|
|
106
|
-
} catch (err) {
|
|
107
|
-
history.update((prev) => ({
|
|
108
|
-
...prev,
|
|
109
|
-
error: err,
|
|
110
|
-
isLoading: false
|
|
111
|
-
}));
|
|
112
|
-
options.onError?.(err, void 0);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
history.update((prev) => ({
|
|
117
|
-
...prev,
|
|
118
|
-
mutate
|
|
119
|
-
}));
|
|
120
|
-
const branch = (0, svelte_store.writable)("");
|
|
121
|
-
const branchContext = (0, svelte_store.derived)([branch, history], ([$branch, $history]) => (0, _langchain_langgraph_sdk_ui.getBranchContext)($branch, $history.data ?? void 0));
|
|
122
|
-
const messageManager = new _langchain_langgraph_sdk_ui.MessageTupleManager();
|
|
123
|
-
const stream = new _langchain_langgraph_sdk_ui.StreamManager(messageManager, {
|
|
124
|
-
throttle: options.throttle ?? false,
|
|
125
|
-
subagentToolNames: options.subagentToolNames,
|
|
126
|
-
filterSubagentMessages: options.filterSubagentMessages,
|
|
127
|
-
toMessage: _langchain_langgraph_sdk_ui.toMessageClass
|
|
128
|
-
});
|
|
129
|
-
const pendingRuns = new _langchain_langgraph_sdk_ui.PendingRunsTracker();
|
|
130
|
-
const historyValues = (0, svelte_store.derived)([branchContext], ([$branchContext]) => $branchContext.threadHead?.values ?? options.initialValues ?? {});
|
|
131
|
-
const historyError = (0, svelte_store.derived)([branchContext], ([$branchContext]) => {
|
|
132
|
-
const error = $branchContext.threadHead?.tasks?.at(-1)?.error;
|
|
133
|
-
if (error == null) return void 0;
|
|
134
|
-
try {
|
|
135
|
-
const parsed = JSON.parse(error);
|
|
136
|
-
if (_langchain_langgraph_sdk_ui.StreamError.isStructuredError(parsed)) return new _langchain_langgraph_sdk_ui.StreamError(parsed);
|
|
137
|
-
return parsed;
|
|
138
|
-
} catch {}
|
|
139
|
-
return error;
|
|
140
|
-
});
|
|
141
|
-
const streamValues = (0, svelte_store.writable)(stream.values);
|
|
142
|
-
const streamError = (0, svelte_store.writable)(stream.error);
|
|
143
|
-
const isLoading = (0, svelte_store.writable)(stream.isLoading);
|
|
144
|
-
const queueEntries = (0, svelte_store.writable)(pendingRuns.entries);
|
|
145
|
-
const queueSize = (0, svelte_store.writable)(pendingRuns.size);
|
|
146
|
-
const values = (0, svelte_store.derived)([streamValues, historyValues], ([$streamValues, $historyValues]) => $streamValues ?? $historyValues);
|
|
147
|
-
const error = (0, svelte_store.derived)([
|
|
148
|
-
streamError,
|
|
149
|
-
historyError,
|
|
150
|
-
history
|
|
151
|
-
], ([$streamError, $historyError, $history]) => $streamError ?? $historyError ?? $history.error);
|
|
152
|
-
const messageMetadata = (0, svelte_store.derived)([history, branchContext], ([$history, $branchContext]) => (0, _langchain_langgraph_sdk_ui.getMessagesMetadataMap)({
|
|
153
|
-
initialValues: options.initialValues,
|
|
154
|
-
history: $history.data,
|
|
155
|
-
getMessages,
|
|
156
|
-
branchContext: $branchContext
|
|
157
|
-
}));
|
|
158
|
-
const subagentVersion = (0, svelte_store.writable)(0);
|
|
159
|
-
const unsubscribe = stream.subscribe(() => {
|
|
160
|
-
streamValues.set(stream.values);
|
|
161
|
-
streamError.set(stream.error);
|
|
162
|
-
isLoading.set(stream.isLoading);
|
|
163
|
-
subagentVersion.update((v) => v + 1);
|
|
164
|
-
});
|
|
165
|
-
const unsubQueue = pendingRuns.subscribe(() => {
|
|
166
|
-
queueEntries.set(pendingRuns.entries);
|
|
167
|
-
queueSize.set(pendingRuns.size);
|
|
116
|
+
const orchestrator = new _langchain_langgraph_sdk_ui.StreamOrchestrator(options, {
|
|
117
|
+
getClient: () => client,
|
|
118
|
+
getAssistantId: () => options.assistantId,
|
|
119
|
+
getMessagesKey: () => options.messagesKey ?? "messages"
|
|
168
120
|
});
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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);
|
|
173
125
|
});
|
|
174
126
|
let fetchController = null;
|
|
175
|
-
const unsubReconstruct =
|
|
127
|
+
const unsubReconstruct = (0, svelte_store.derived)(version, () => {
|
|
128
|
+
const hvMessages = orchestrator.messages;
|
|
129
|
+
if (!options.filterSubagentMessages) return false;
|
|
130
|
+
if (orchestrator.isLoading || orchestrator.historyData.isLoading) return false;
|
|
131
|
+
return hvMessages.length > 0;
|
|
132
|
+
}).subscribe(($should) => {
|
|
176
133
|
if ($should) {
|
|
177
|
-
const hvMessages = getMessages((0, svelte_store.get)(historyValues));
|
|
178
|
-
stream.reconstructSubagents(hvMessages, { skipIfPopulated: true });
|
|
179
134
|
fetchController?.abort();
|
|
180
|
-
fetchController =
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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;
|
|
186
145
|
}
|
|
187
146
|
});
|
|
188
147
|
(0, svelte.onDestroy)(() => {
|
|
189
148
|
fetchController?.abort();
|
|
190
149
|
unsubscribe();
|
|
191
150
|
unsubReconstruct();
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
function stop() {
|
|
195
|
-
return stream.stop((0, svelte_store.get)(historyValues), { onStop: (args) => {
|
|
196
|
-
const tid = (0, svelte_store.get)(threadId);
|
|
197
|
-
if (runMetadataStorage && tid) {
|
|
198
|
-
const runId = runMetadataStorage.getItem(`lg:stream:${tid}`);
|
|
199
|
-
if (runId) client.runs.cancel(tid, runId);
|
|
200
|
-
runMetadataStorage.removeItem(`lg:stream:${tid}`);
|
|
201
|
-
}
|
|
202
|
-
options.onStop?.(args);
|
|
203
|
-
} });
|
|
204
|
-
}
|
|
205
|
-
function setBranch(value) {
|
|
206
|
-
branch.set(value);
|
|
207
|
-
}
|
|
208
|
-
function submitDirect(values, submitOptions) {
|
|
209
|
-
const currentBranchContext = (0, svelte_store.get)(branchContext);
|
|
210
|
-
const checkpointId = submitOptions?.checkpoint?.checkpoint_id;
|
|
211
|
-
branch.set(checkpointId != null ? currentBranchContext.branchByCheckpoint[checkpointId]?.branch ?? "" : "");
|
|
212
|
-
const includeImplicitBranch = historyLimit === true || typeof historyLimit === "number";
|
|
213
|
-
const shouldRefetch = options.onFinish != null || includeImplicitBranch;
|
|
214
|
-
let checkpoint = submitOptions?.checkpoint ?? (includeImplicitBranch ? currentBranchContext.threadHead?.checkpoint : void 0) ?? void 0;
|
|
215
|
-
if (submitOptions?.checkpoint === null) checkpoint = void 0;
|
|
216
|
-
if (checkpoint != null) delete checkpoint.thread_id;
|
|
217
|
-
let callbackMeta;
|
|
218
|
-
let rejoinKey;
|
|
219
|
-
let usableThreadId;
|
|
220
|
-
return stream.start(async (signal) => {
|
|
221
|
-
usableThreadId = (0, svelte_store.get)(threadId);
|
|
222
|
-
if (!usableThreadId) {
|
|
223
|
-
const threadPromise = client.threads.create({
|
|
224
|
-
threadId: submitOptions?.threadId,
|
|
225
|
-
metadata: submitOptions?.metadata
|
|
226
|
-
});
|
|
227
|
-
threadIdPromise = threadPromise.then((t) => t.thread_id);
|
|
228
|
-
usableThreadId = (await threadPromise).thread_id;
|
|
229
|
-
threadId.set(usableThreadId);
|
|
230
|
-
options.onThreadId?.(usableThreadId);
|
|
231
|
-
}
|
|
232
|
-
const streamMode = [
|
|
233
|
-
"values",
|
|
234
|
-
"messages-tuple",
|
|
235
|
-
"updates",
|
|
236
|
-
...submitOptions?.streamMode ?? []
|
|
237
|
-
];
|
|
238
|
-
if (options.onUpdateEvent && !streamMode.includes("updates")) streamMode.push("updates");
|
|
239
|
-
if (options.onCustomEvent && !streamMode.includes("custom")) streamMode.push("custom");
|
|
240
|
-
if (options.onCheckpointEvent && !streamMode.includes("checkpoints")) streamMode.push("checkpoints");
|
|
241
|
-
if (options.onTaskEvent && !streamMode.includes("tasks")) streamMode.push("tasks");
|
|
242
|
-
if ("onDebugEvent" in options && options.onDebugEvent && !streamMode.includes("debug")) streamMode.push("debug");
|
|
243
|
-
if ("onLangChainEvent" in options && options.onLangChainEvent && !streamMode.includes("events")) streamMode.push("events");
|
|
244
|
-
stream.setStreamValues(() => {
|
|
245
|
-
const prev = {
|
|
246
|
-
...(0, svelte_store.get)(historyValues),
|
|
247
|
-
...stream.values
|
|
248
|
-
};
|
|
249
|
-
if (submitOptions?.optimisticValues != null) return {
|
|
250
|
-
...prev,
|
|
251
|
-
...typeof submitOptions.optimisticValues === "function" ? submitOptions.optimisticValues(prev) : submitOptions.optimisticValues
|
|
252
|
-
};
|
|
253
|
-
return { ...prev };
|
|
254
|
-
});
|
|
255
|
-
const streamResumable = submitOptions?.streamResumable ?? !!runMetadataStorage;
|
|
256
|
-
return client.runs.stream(usableThreadId, options.assistantId, {
|
|
257
|
-
input: values,
|
|
258
|
-
config: submitOptions?.config,
|
|
259
|
-
context: submitOptions?.context,
|
|
260
|
-
command: submitOptions?.command,
|
|
261
|
-
interruptBefore: submitOptions?.interruptBefore,
|
|
262
|
-
interruptAfter: submitOptions?.interruptAfter,
|
|
263
|
-
metadata: submitOptions?.metadata,
|
|
264
|
-
multitaskStrategy: submitOptions?.multitaskStrategy,
|
|
265
|
-
onCompletion: submitOptions?.onCompletion,
|
|
266
|
-
onDisconnect: submitOptions?.onDisconnect ?? (streamResumable ? "continue" : "cancel"),
|
|
267
|
-
signal,
|
|
268
|
-
checkpoint,
|
|
269
|
-
streamMode,
|
|
270
|
-
streamSubgraphs: submitOptions?.streamSubgraphs,
|
|
271
|
-
streamResumable,
|
|
272
|
-
durability: submitOptions?.durability,
|
|
273
|
-
onRunCreated(params) {
|
|
274
|
-
callbackMeta = {
|
|
275
|
-
run_id: params.run_id,
|
|
276
|
-
thread_id: params.thread_id ?? usableThreadId
|
|
277
|
-
};
|
|
278
|
-
if (runMetadataStorage) {
|
|
279
|
-
rejoinKey = `lg:stream:${usableThreadId}`;
|
|
280
|
-
runMetadataStorage.setItem(rejoinKey, callbackMeta.run_id);
|
|
281
|
-
}
|
|
282
|
-
options.onCreated?.(callbackMeta);
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
}, {
|
|
286
|
-
getMessages,
|
|
287
|
-
setMessages,
|
|
288
|
-
initialValues: (0, svelte_store.get)(historyValues),
|
|
289
|
-
callbacks: options,
|
|
290
|
-
async onSuccess() {
|
|
291
|
-
if (rejoinKey) runMetadataStorage?.removeItem(rejoinKey);
|
|
292
|
-
if (shouldRefetch && usableThreadId) {
|
|
293
|
-
const lastHead = (await mutate(usableThreadId))?.at(0);
|
|
294
|
-
if (lastHead) {
|
|
295
|
-
options.onFinish?.(lastHead, callbackMeta);
|
|
296
|
-
return null;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
},
|
|
300
|
-
onError(error) {
|
|
301
|
-
options.onError?.(error, callbackMeta);
|
|
302
|
-
submitOptions?.onError?.(error, callbackMeta);
|
|
303
|
-
},
|
|
304
|
-
onFinish: () => {}
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
let submitting = false;
|
|
308
|
-
function drainQueue() {
|
|
309
|
-
if (!(0, svelte_store.get)(isLoading) && !submitting && pendingRuns.size > 0) {
|
|
310
|
-
const next = pendingRuns.shift();
|
|
311
|
-
if (next) {
|
|
312
|
-
submitting = true;
|
|
313
|
-
joinStream(next.id).finally(() => {
|
|
314
|
-
submitting = false;
|
|
315
|
-
drainQueue();
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
isLoading.subscribe(() => {
|
|
321
|
-
drainQueue();
|
|
151
|
+
unsubDrain();
|
|
152
|
+
orchestrator.dispose();
|
|
322
153
|
});
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
submitting = true;
|
|
327
|
-
try {
|
|
328
|
-
await submitDirect(values, submitOptions);
|
|
329
|
-
} finally {
|
|
330
|
-
submitting = false;
|
|
331
|
-
}
|
|
332
|
-
return;
|
|
333
|
-
}
|
|
334
|
-
let usableThreadId = (0, svelte_store.get)(threadId);
|
|
335
|
-
if (!usableThreadId && threadIdPromise) usableThreadId = await threadIdPromise;
|
|
336
|
-
if (usableThreadId) {
|
|
337
|
-
try {
|
|
338
|
-
const run = await client.runs.create(usableThreadId, options.assistantId, {
|
|
339
|
-
input: values,
|
|
340
|
-
config: submitOptions?.config,
|
|
341
|
-
context: submitOptions?.context,
|
|
342
|
-
command: submitOptions?.command,
|
|
343
|
-
interruptBefore: submitOptions?.interruptBefore,
|
|
344
|
-
interruptAfter: submitOptions?.interruptAfter,
|
|
345
|
-
metadata: submitOptions?.metadata,
|
|
346
|
-
multitaskStrategy: "enqueue",
|
|
347
|
-
streamResumable: true,
|
|
348
|
-
streamSubgraphs: submitOptions?.streamSubgraphs,
|
|
349
|
-
durability: submitOptions?.durability
|
|
350
|
-
});
|
|
351
|
-
pendingRuns.add({
|
|
352
|
-
id: run.run_id,
|
|
353
|
-
values,
|
|
354
|
-
options: submitOptions,
|
|
355
|
-
createdAt: new Date(run.created_at)
|
|
356
|
-
});
|
|
357
|
-
} catch (error) {
|
|
358
|
-
options.onError?.(error, void 0);
|
|
359
|
-
submitOptions?.onError?.(error, void 0);
|
|
360
|
-
}
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
submitting = true;
|
|
365
|
-
const result = submitDirect(values, submitOptions);
|
|
366
|
-
Promise.resolve(result).finally(() => {
|
|
367
|
-
submitting = false;
|
|
368
|
-
drainQueue();
|
|
369
|
-
});
|
|
370
|
-
return result;
|
|
371
|
-
}
|
|
372
|
-
async function joinStream(runId, lastEventId, joinOptions) {
|
|
373
|
-
lastEventId ??= "-1";
|
|
374
|
-
const tid = (0, svelte_store.get)(threadId);
|
|
375
|
-
if (!tid) return;
|
|
376
|
-
const callbackMeta = {
|
|
377
|
-
thread_id: tid,
|
|
378
|
-
run_id: runId
|
|
379
|
-
};
|
|
380
|
-
await stream.start(async (signal) => {
|
|
381
|
-
const rawStream = client.runs.joinStream(tid, runId, {
|
|
382
|
-
signal,
|
|
383
|
-
lastEventId,
|
|
384
|
-
streamMode: joinOptions?.streamMode
|
|
385
|
-
});
|
|
386
|
-
return joinOptions?.filter != null ? (0, _langchain_langgraph_sdk_ui.filterStream)(rawStream, joinOptions.filter) : rawStream;
|
|
387
|
-
}, {
|
|
388
|
-
getMessages,
|
|
389
|
-
setMessages,
|
|
390
|
-
initialValues: (0, svelte_store.get)(historyValues),
|
|
391
|
-
callbacks: options,
|
|
392
|
-
async onSuccess() {
|
|
393
|
-
runMetadataStorage?.removeItem(`lg:stream:${tid}`);
|
|
394
|
-
const lastHead = (await mutate(tid))?.at(0);
|
|
395
|
-
if (lastHead) options.onFinish?.(lastHead, callbackMeta);
|
|
396
|
-
},
|
|
397
|
-
onError(error) {
|
|
398
|
-
options.onError?.(error, callbackMeta);
|
|
399
|
-
},
|
|
400
|
-
onFinish: () => {}
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
let shouldReconnect = !!runMetadataStorage;
|
|
404
|
-
(0, svelte.onMount)(() => {
|
|
405
|
-
const tid = (0, svelte_store.get)(threadId);
|
|
406
|
-
if (shouldReconnect && runMetadataStorage && tid) {
|
|
407
|
-
const runId = runMetadataStorage.getItem(`lg:stream:${tid}`);
|
|
408
|
-
if (runId) {
|
|
409
|
-
shouldReconnect = false;
|
|
410
|
-
joinStream(runId);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
154
|
+
const valuesStore = (0, svelte_store.derived)(version, () => {
|
|
155
|
+
orchestrator.trackStreamMode("values");
|
|
156
|
+
return orchestrator.values;
|
|
413
157
|
});
|
|
414
|
-
const
|
|
415
|
-
const
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
], ([$streamValues, $streamError, $branchContext, $isLoading]) => {
|
|
421
|
-
return (0, _langchain_langgraph_sdk_ui.extractInterrupts)($streamValues, {
|
|
422
|
-
isLoading: $isLoading,
|
|
423
|
-
threadState: $branchContext.threadHead,
|
|
424
|
-
error: $streamError
|
|
425
|
-
});
|
|
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);
|
|
426
164
|
});
|
|
427
|
-
const
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
branchContext,
|
|
431
|
-
isLoading
|
|
432
|
-
], ([$streamValues, $streamError, $branchContext, $isLoading]) => {
|
|
433
|
-
const vals = $streamValues ?? (0, svelte_store.get)(historyValues);
|
|
434
|
-
if (vals != null && "__interrupt__" in vals && Array.isArray(vals.__interrupt__)) {
|
|
435
|
-
const valueInterrupts = vals.__interrupt__;
|
|
436
|
-
if (valueInterrupts.length === 0) return [{ when: "breakpoint" }];
|
|
437
|
-
return valueInterrupts;
|
|
438
|
-
}
|
|
439
|
-
if ($isLoading) return [];
|
|
440
|
-
const allInterrupts = ($branchContext.threadHead?.tasks ?? []).flatMap((t) => t.interrupts ?? []);
|
|
441
|
-
if (allInterrupts.length > 0) return allInterrupts;
|
|
442
|
-
if (!($branchContext.threadHead?.next ?? []).length || $streamError != null) return [];
|
|
443
|
-
return [{ when: "breakpoint" }];
|
|
165
|
+
const toolCallsStore = (0, svelte_store.derived)(version, () => {
|
|
166
|
+
orchestrator.trackStreamMode("messages-tuple", "values");
|
|
167
|
+
return orchestrator.toolCalls;
|
|
444
168
|
});
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
const
|
|
450
|
-
|
|
451
|
-
|
|
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;
|
|
452
177
|
});
|
|
453
|
-
const
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
return $branchContext.branchTree;
|
|
178
|
+
const activeSubagentsStore = (0, svelte_store.derived)(version, () => {
|
|
179
|
+
orchestrator.trackStreamMode("updates", "messages-tuple");
|
|
180
|
+
return orchestrator.activeSubagents;
|
|
457
181
|
});
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
const
|
|
467
|
-
const
|
|
468
|
-
const
|
|
469
|
-
const
|
|
470
|
-
const
|
|
471
|
-
const isThreadLoadingRef = (0, svelte_store.fromStore)(isThreadLoading);
|
|
472
|
-
const branchRef = (0, svelte_store.fromStore)(branch);
|
|
473
|
-
const messagesRef = (0, svelte_store.fromStore)(messages);
|
|
474
|
-
const toolCallsRef = (0, svelte_store.fromStore)(toolCalls);
|
|
475
|
-
const interruptRef = (0, svelte_store.fromStore)(interrupt);
|
|
476
|
-
const interruptsRef = (0, svelte_store.fromStore)(interrupts);
|
|
477
|
-
const historyListRef = (0, svelte_store.fromStore)(historyList);
|
|
478
|
-
const experimentalBranchTreeRef = (0, svelte_store.fromStore)(experimentalBranchTree);
|
|
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);
|
|
479
195
|
const subagentsRef = (0, svelte_store.fromStore)(subagentsStore);
|
|
480
196
|
const activeSubagentsRef = (0, svelte_store.fromStore)(activeSubagentsStore);
|
|
481
|
-
const queueEntriesRef = (0, svelte_store.fromStore)(
|
|
482
|
-
const queueSizeRef = (0, svelte_store.fromStore)(
|
|
197
|
+
const queueEntriesRef = (0, svelte_store.fromStore)(queueEntriesStore);
|
|
198
|
+
const queueSizeRef = (0, svelte_store.fromStore)(queueSizeStore);
|
|
483
199
|
return {
|
|
484
200
|
assistantId: options.assistantId,
|
|
485
201
|
client,
|
|
@@ -498,14 +214,18 @@ function useStreamLGP(options) {
|
|
|
498
214
|
get branch() {
|
|
499
215
|
return branchRef.current;
|
|
500
216
|
},
|
|
501
|
-
setBranch
|
|
217
|
+
setBranch(value) {
|
|
218
|
+
orchestrator.setBranch(value);
|
|
219
|
+
},
|
|
502
220
|
get messages() {
|
|
503
221
|
return messagesRef.current;
|
|
504
222
|
},
|
|
505
223
|
get toolCalls() {
|
|
506
224
|
return toolCallsRef.current;
|
|
507
225
|
},
|
|
508
|
-
getToolCalls
|
|
226
|
+
getToolCalls(message) {
|
|
227
|
+
return orchestrator.getToolCalls(message);
|
|
228
|
+
},
|
|
509
229
|
get interrupt() {
|
|
510
230
|
return interruptRef.current;
|
|
511
231
|
},
|
|
@@ -518,10 +238,12 @@ function useStreamLGP(options) {
|
|
|
518
238
|
get experimental_branchTree() {
|
|
519
239
|
return experimentalBranchTreeRef.current;
|
|
520
240
|
},
|
|
521
|
-
getMessagesMetadata,
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
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),
|
|
525
247
|
queue: {
|
|
526
248
|
get entries() {
|
|
527
249
|
return queueEntriesRef.current;
|
|
@@ -529,27 +251,11 @@ function useStreamLGP(options) {
|
|
|
529
251
|
get size() {
|
|
530
252
|
return queueSizeRef.current;
|
|
531
253
|
},
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
const removed = pendingRuns.remove(id);
|
|
535
|
-
if (removed && tid) await client.runs.cancel(tid, id);
|
|
536
|
-
return removed;
|
|
537
|
-
},
|
|
538
|
-
async clear() {
|
|
539
|
-
const tid = (0, svelte_store.get)(threadId);
|
|
540
|
-
const removed = pendingRuns.removeAll();
|
|
541
|
-
if (tid && removed.length > 0) await Promise.all(removed.map((e) => client.runs.cancel(tid, e.id)));
|
|
542
|
-
}
|
|
254
|
+
cancel: (id) => orchestrator.cancelQueueItem(id),
|
|
255
|
+
clear: () => orchestrator.clearQueue()
|
|
543
256
|
},
|
|
544
257
|
switchThread(newThreadId) {
|
|
545
|
-
|
|
546
|
-
const prevThreadId = (0, svelte_store.get)(threadId);
|
|
547
|
-
threadId.set(newThreadId ?? void 0);
|
|
548
|
-
stream.clear();
|
|
549
|
-
const removed = pendingRuns.removeAll();
|
|
550
|
-
if (prevThreadId && removed.length > 0) Promise.all(removed.map((e) => client.runs.cancel(prevThreadId, e.id)));
|
|
551
|
-
if (newThreadId != null) options.onThreadId?.(newThreadId);
|
|
552
|
-
}
|
|
258
|
+
orchestrator.switchThread(newThreadId);
|
|
553
259
|
},
|
|
554
260
|
get subagents() {
|
|
555
261
|
return subagentsRef.current;
|
|
@@ -558,13 +264,13 @@ function useStreamLGP(options) {
|
|
|
558
264
|
return activeSubagentsRef.current;
|
|
559
265
|
},
|
|
560
266
|
getSubagent(toolCallId) {
|
|
561
|
-
return
|
|
267
|
+
return orchestrator.getSubagent(toolCallId);
|
|
562
268
|
},
|
|
563
269
|
getSubagentsByType(type) {
|
|
564
|
-
return
|
|
270
|
+
return orchestrator.getSubagentsByType(type);
|
|
565
271
|
},
|
|
566
272
|
getSubagentsByMessage(messageId) {
|
|
567
|
-
return
|
|
273
|
+
return orchestrator.getSubagentsByMessage(messageId);
|
|
568
274
|
}
|
|
569
275
|
};
|
|
570
276
|
}
|
|
@@ -594,7 +300,7 @@ Object.defineProperty(exports, "extractToolCallIdFromNamespace", {
|
|
|
594
300
|
return _langchain_langgraph_sdk_ui.extractToolCallIdFromNamespace;
|
|
595
301
|
}
|
|
596
302
|
});
|
|
597
|
-
exports.getStream =
|
|
303
|
+
exports.getStream = getStream;
|
|
598
304
|
exports.getStreamContext = getStreamContext;
|
|
599
305
|
Object.defineProperty(exports, "isSubagentNamespace", {
|
|
600
306
|
enumerable: true,
|
|
@@ -602,7 +308,7 @@ Object.defineProperty(exports, "isSubagentNamespace", {
|
|
|
602
308
|
return _langchain_langgraph_sdk_ui.isSubagentNamespace;
|
|
603
309
|
}
|
|
604
310
|
});
|
|
605
|
-
exports.provideStream =
|
|
311
|
+
exports.provideStream = provideStream;
|
|
606
312
|
exports.setStreamContext = setStreamContext;
|
|
607
313
|
exports.useStream = useStream;
|
|
608
314
|
|