@langchain/langgraph-sdk 0.0.112 → 0.1.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.
@@ -1,329 +1,7 @@
1
- import { Client, type ClientConfig } from "../client.js";
2
- import type { Command, DisconnectMode, Durability, MultitaskStrategy, OnCompletionBehavior } from "../types.js";
3
- import type { Message } from "../types.messages.js";
4
- import type { Checkpoint, Config, Interrupt, Metadata, ThreadState } from "../schema.js";
5
- import type { CheckpointsStreamEvent, CustomStreamEvent, DebugStreamEvent, EventsStreamEvent, MetadataStreamEvent, StreamMode, TasksStreamEvent, UpdatesStreamEvent } from "../types.stream.js";
6
- interface Node<StateType = any> {
7
- type: "node";
8
- value: ThreadState<StateType>;
9
- path: string[];
10
- }
11
- interface Fork<StateType = any> {
12
- type: "fork";
13
- items: Array<Sequence<StateType>>;
14
- }
15
- interface Sequence<StateType = any> {
16
- type: "sequence";
17
- items: Array<Node<StateType> | Fork<StateType>>;
18
- }
19
- export type MessageMetadata<StateType extends Record<string, unknown>> = {
20
- /**
21
- * The ID of the message used.
22
- */
23
- messageId: string;
24
- /**
25
- * The first thread state the message was seen in.
26
- */
27
- firstSeenState: ThreadState<StateType> | undefined;
28
- /**
29
- * The branch of the message.
30
- */
31
- branch: string | undefined;
32
- /**
33
- * The list of branches this message is part of.
34
- * This is useful for displaying branching controls.
35
- */
36
- branchOptions: string[] | undefined;
37
- /**
38
- * Metadata sent alongside the message during run streaming.
39
- * @remarks This metadata only exists temporarily in browser memory during streaming and is not persisted after completion.
40
- */
41
- streamMetadata: Record<string, unknown> | undefined;
42
- };
43
- type BagTemplate = {
44
- ConfigurableType?: Record<string, unknown>;
45
- InterruptType?: unknown;
46
- CustomEventType?: unknown;
47
- UpdateType?: unknown;
48
- };
49
- type GetUpdateType<Bag extends BagTemplate, StateType extends Record<string, unknown>> = Bag extends {
50
- UpdateType: unknown;
51
- } ? Bag["UpdateType"] : Partial<StateType>;
52
- type GetConfigurableType<Bag extends BagTemplate> = Bag extends {
53
- ConfigurableType: Record<string, unknown>;
54
- } ? Bag["ConfigurableType"] : Record<string, unknown>;
55
- type GetInterruptType<Bag extends BagTemplate> = Bag extends {
56
- InterruptType: unknown;
57
- } ? Bag["InterruptType"] : unknown;
58
- type GetCustomEventType<Bag extends BagTemplate> = Bag extends {
59
- CustomEventType: unknown;
60
- } ? Bag["CustomEventType"] : unknown;
61
- interface RunCallbackMeta {
62
- run_id: string;
63
- thread_id: string;
64
- }
65
- export interface UseStreamOptions<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> {
66
- /**
67
- * The ID of the assistant to use.
68
- */
69
- assistantId: string;
70
- /**
71
- * Client used to send requests.
72
- */
73
- client?: Client;
74
- /**
75
- * The URL of the API to use.
76
- */
77
- apiUrl?: ClientConfig["apiUrl"];
78
- /**
79
- * The API key to use.
80
- */
81
- apiKey?: ClientConfig["apiKey"];
82
- /**
83
- * Custom call options, such as custom fetch implementation.
84
- */
85
- callerOptions?: ClientConfig["callerOptions"];
86
- /**
87
- * Default headers to send with requests.
88
- */
89
- defaultHeaders?: ClientConfig["defaultHeaders"];
90
- /**
91
- * Specify the key within the state that contains messages.
92
- * Defaults to "messages".
93
- *
94
- * @default "messages"
95
- */
96
- messagesKey?: string;
97
- /**
98
- * Callback that is called when an error occurs.
99
- */
100
- onError?: (error: unknown, run: RunCallbackMeta | undefined) => void;
101
- /**
102
- * Callback that is called when the stream is finished.
103
- */
104
- onFinish?: (state: ThreadState<StateType>, run: RunCallbackMeta | undefined) => void;
105
- /**
106
- * Callback that is called when a new stream is created.
107
- */
108
- onCreated?: (run: RunCallbackMeta) => void;
109
- /**
110
- * Callback that is called when an update event is received.
111
- */
112
- onUpdateEvent?: (data: UpdatesStreamEvent<GetUpdateType<Bag, StateType>>["data"], options: {
113
- namespace: string[] | undefined;
114
- mutate: (update: Partial<StateType> | ((prev: StateType) => Partial<StateType>)) => void;
115
- }) => void;
116
- /**
117
- * Callback that is called when a custom event is received.
118
- */
119
- onCustomEvent?: (data: CustomStreamEvent<GetCustomEventType<Bag>>["data"], options: {
120
- namespace: string[] | undefined;
121
- mutate: (update: Partial<StateType> | ((prev: StateType) => Partial<StateType>)) => void;
122
- }) => void;
123
- /**
124
- * Callback that is called when a metadata event is received.
125
- */
126
- onMetadataEvent?: (data: MetadataStreamEvent["data"]) => void;
127
- /**
128
- * Callback that is called when a LangChain event is received.
129
- * @see https://langchain-ai.github.io/langgraph/cloud/how-tos/stream_events/#stream-graph-in-events-mode for more details.
130
- */
131
- onLangChainEvent?: (data: EventsStreamEvent["data"]) => void;
132
- /**
133
- * Callback that is called when a debug event is received.
134
- * @internal This API is experimental and subject to change.
135
- */
136
- onDebugEvent?: (data: DebugStreamEvent["data"], options: {
137
- namespace: string[] | undefined;
138
- }) => void;
139
- /**
140
- * Callback that is called when a checkpoints event is received.
141
- */
142
- onCheckpointEvent?: (data: CheckpointsStreamEvent<StateType>["data"], options: {
143
- namespace: string[] | undefined;
144
- }) => void;
145
- /**
146
- * Callback that is called when a tasks event is received.
147
- */
148
- onTaskEvent?: (data: TasksStreamEvent<StateType, GetUpdateType<Bag, StateType>>["data"], options: {
149
- namespace: string[] | undefined;
150
- }) => void;
151
- /**
152
- * Callback that is called when the stream is stopped by the user.
153
- * Provides a mutate function to update the stream state immediately
154
- * without requiring a server roundtrip.
155
- *
156
- * @example
157
- * ```typescript
158
- * onStop: ({ mutate }) => {
159
- * mutate((prev) => ({
160
- * ...prev,
161
- * ui: prev.ui?.map(component =>
162
- * component.props.isLoading
163
- * ? { ...component, props: { ...component.props, stopped: true, isLoading: false }}
164
- * : component
165
- * )
166
- * }));
167
- * }
168
- * ```
169
- */
170
- onStop?: (options: {
171
- mutate: (update: Partial<StateType> | ((prev: StateType) => Partial<StateType>)) => void;
172
- }) => void;
173
- /**
174
- * The ID of the thread to fetch history and current values from.
175
- */
176
- threadId?: string | null;
177
- /**
178
- * Callback that is called when the thread ID is updated (ie when a new thread is created).
179
- */
180
- onThreadId?: (threadId: string) => void;
181
- /** Will reconnect the stream on mount */
182
- reconnectOnMount?: boolean | (() => RunMetadataStorage);
183
- /**
184
- * Initial values to display immediately when loading a thread.
185
- * Useful for displaying cached thread data while official history loads.
186
- * These values will be replaced when official thread data is fetched.
187
- *
188
- * Note: UI components from initialValues will render immediately if they're
189
- * predefined in LoadExternalComponent's components prop, providing instant
190
- * cached UI display without server fetches.
191
- */
192
- initialValues?: StateType | null;
193
- /**
194
- * Whether to fetch the history of the thread.
195
- * If true, the history will be fetched from the server. Defaults to 1000 entries.
196
- * If false, only the last state will be fetched from the server.
197
- * @default true
198
- */
199
- fetchStateHistory?: boolean | {
200
- limit: number;
201
- };
202
- }
203
- interface RunMetadataStorage {
204
- getItem(key: `lg:stream:${string}`): string | null;
205
- setItem(key: `lg:stream:${string}`, value: string): void;
206
- removeItem(key: `lg:stream:${string}`): void;
207
- }
208
- export interface UseStream<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> {
209
- /**
210
- * The current values of the thread.
211
- */
212
- values: StateType;
213
- /**
214
- * Last seen error from the thread or during streaming.
215
- */
216
- error: unknown;
217
- /**
218
- * Whether the stream is currently running.
219
- */
220
- isLoading: boolean;
221
- /**
222
- * Whether the thread is currently being loaded.
223
- */
224
- isThreadLoading: boolean;
225
- /**
226
- * Stops the stream.
227
- */
228
- stop: () => void;
229
- /**
230
- * Create and stream a run to the thread.
231
- */
232
- submit: (values: GetUpdateType<Bag, StateType> | null | undefined, options?: SubmitOptions<StateType, GetConfigurableType<Bag>>) => void;
233
- /**
234
- * The current branch of the thread.
235
- */
236
- branch: string;
237
- /**
238
- * Set the branch of the thread.
239
- */
240
- setBranch: (branch: string) => void;
241
- /**
242
- * Flattened history of thread states of a thread.
243
- */
244
- history: ThreadState<StateType>[];
245
- /**
246
- * Tree of all branches for the thread.
247
- * @experimental
248
- */
249
- experimental_branchTree: Sequence<StateType>;
250
- /**
251
- * Get the interrupt value for the stream if interrupted.
252
- */
253
- interrupt: Interrupt<GetInterruptType<Bag>> | undefined;
254
- /**
255
- * Messages inferred from the thread.
256
- * Will automatically update with incoming message chunks.
257
- */
258
- messages: Message[];
259
- /**
260
- * Get the metadata for a message, such as first thread state the message
261
- * was seen in and branch information.
262
-
263
- * @param message - The message to get the metadata for.
264
- * @param index - The index of the message in the thread.
265
- * @returns The metadata for the message.
266
- */
267
- getMessagesMetadata: (message: Message, index?: number) => MessageMetadata<StateType> | undefined;
268
- /**
269
- * LangGraph SDK client used to send request and receive responses.
270
- */
271
- client: Client;
272
- /**
273
- * The ID of the assistant to use.
274
- */
275
- assistantId: string;
276
- /**
277
- * Join an active stream.
278
- */
279
- joinStream: (runId: string, lastEventId?: string, options?: {
280
- streamMode?: StreamMode | StreamMode[];
281
- }) => Promise<void>;
282
- }
283
- type ConfigWithConfigurable<ConfigurableType extends Record<string, unknown>> = Config & {
284
- configurable?: ConfigurableType;
285
- };
286
- interface SubmitOptions<StateType extends Record<string, unknown> = Record<string, unknown>, ContextType extends Record<string, unknown> = Record<string, unknown>> {
287
- config?: ConfigWithConfigurable<ContextType>;
288
- context?: ContextType;
289
- checkpoint?: Omit<Checkpoint, "thread_id"> | null;
290
- command?: Command;
291
- interruptBefore?: "*" | string[];
292
- interruptAfter?: "*" | string[];
293
- metadata?: Metadata;
294
- multitaskStrategy?: MultitaskStrategy;
295
- onCompletion?: OnCompletionBehavior;
296
- onDisconnect?: DisconnectMode;
297
- feedbackKeys?: string[];
298
- streamMode?: Array<StreamMode>;
299
- optimisticValues?: Partial<StateType> | ((prev: StateType) => Partial<StateType>);
300
- /**
301
- * Whether or not to stream the nodes of any subgraphs called
302
- * by the assistant.
303
- * @default false
304
- */
305
- streamSubgraphs?: boolean;
306
- streamResumable?: boolean;
307
- /**
308
- * Whether to checkpoint during the run (or only at the end/interruption).
309
- * - `"async"`: Save checkpoint asynchronously while the next step executes (default).
310
- * - `"sync"`: Save checkpoint synchronously before the next step starts.
311
- * - `"exit"`: Save checkpoint only when the graph exits.
312
- * @default "async"
313
- */
314
- durability?: Durability;
315
- /**
316
- * The ID to use when creating a new thread. When provided, this ID will be used
317
- * for thread creation when threadId is `null` or `undefined`.
318
- * This enables optimistic UI updates where you know the thread ID
319
- * before the thread is actually created.
320
- */
321
- threadId?: string;
322
- }
1
+ import type { BagTemplate, UseStreamOptions, UseStream } from "./types.js";
323
2
  export declare function useStream<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends {
324
3
  ConfigurableType?: Record<string, unknown>;
325
4
  InterruptType?: unknown;
326
5
  CustomEventType?: unknown;
327
6
  UpdateType?: unknown;
328
7
  } = BagTemplate>(options: UseStreamOptions<StateType, Bag>): UseStream<StateType, Bag>;
329
- export {};