@langchain/langgraph-sdk 0.0.43 → 0.0.44

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.
@@ -26,6 +26,13 @@ class MessageTupleManager {
26
26
  this.chunks = {};
27
27
  }
28
28
  add(serialized) {
29
+ // TODO: this is sometimes sent from the API
30
+ // figure out how to prevent this or move this to LC.js
31
+ if (serialized.type.endsWith("MessageChunk")) {
32
+ serialized.type = serialized.type
33
+ .slice(0, -"MessageChunk".length)
34
+ .toLowerCase();
35
+ }
29
36
  const chunk = (0, messages_1.convertToChunk)((0, messages_1.coerceMessageLikeToMessage)(serialized));
30
37
  const id = chunk.id;
31
38
  if (!id)
@@ -195,7 +202,7 @@ function useStream(options) {
195
202
  const messageManagerRef = (0, react_1.useRef)(new MessageTupleManager());
196
203
  const submittingRef = (0, react_1.useRef)(false);
197
204
  const abortRef = (0, react_1.useRef)(null);
198
- const trackStreamModeRef = (0, react_1.useRef)(["values", "messages-tuple"]);
205
+ const trackStreamModeRef = (0, react_1.useRef)(["values"]);
199
206
  const trackStreamMode = (0, react_1.useCallback)((mode) => {
200
207
  if (!trackStreamModeRef.current.includes(mode))
201
208
  trackStreamModeRef.current.push(mode);
@@ -376,6 +383,7 @@ function useStream(options) {
376
383
  catch (error) {
377
384
  if (!(error instanceof Error &&
378
385
  (error.name === "AbortError" || error.name === "TimeoutError"))) {
386
+ console.error(error);
379
387
  setStreamError(error);
380
388
  onError?.(error);
381
389
  }
@@ -388,7 +396,7 @@ function useStream(options) {
388
396
  abortRef.current = null;
389
397
  }
390
398
  };
391
- const error = isLoading ? streamError : historyError;
399
+ const error = streamError ?? historyError;
392
400
  const values = streamValues ?? historyValues;
393
401
  return {
394
402
  get values() {
@@ -403,6 +411,16 @@ function useStream(options) {
403
411
  setBranch,
404
412
  history: flatHistory,
405
413
  experimental_branchTree: rootSequence,
414
+ get interrupt() {
415
+ // Don't show the interrupt if the stream is loading
416
+ if (isLoading)
417
+ return undefined;
418
+ const interrupts = threadHead?.tasks?.at(-1)?.interrupts;
419
+ if (interrupts == null || interrupts.length === 0)
420
+ return undefined;
421
+ // Return only the current interrupt
422
+ return interrupts.at(-1);
423
+ },
406
424
  get messages() {
407
425
  trackStreamMode("messages-tuple");
408
426
  return getMessages(values);
@@ -1,7 +1,7 @@
1
1
  import { type ClientConfig } from "../client.js";
2
2
  import type { Command, DisconnectMode, MultitaskStrategy, OnCompletionBehavior } from "../types.js";
3
3
  import type { Message } from "../types.messages.js";
4
- import type { Checkpoint, Config, Metadata, ThreadState } from "../schema.js";
4
+ import type { Checkpoint, Config, Interrupt, Metadata, ThreadState } from "../schema.js";
5
5
  import type { CustomStreamEvent, MetadataStreamEvent, StreamMode, UpdatesStreamEvent } from "../types.stream.js";
6
6
  interface Node<StateType = any> {
7
7
  type: "node";
@@ -35,7 +35,25 @@ export type MessageMetadata<StateType extends Record<string, unknown>> = {
35
35
  */
36
36
  branchOptions: string[] | undefined;
37
37
  };
38
- interface UseStreamOptions<StateType extends Record<string, unknown> = Record<string, unknown>, UpdateType extends Record<string, unknown> = Partial<StateType>, CustomType = unknown> {
38
+ type BagTemplate = {
39
+ ConfigurableType?: Record<string, unknown>;
40
+ InterruptType?: unknown;
41
+ CustomEventType?: unknown;
42
+ UpdateType?: unknown;
43
+ };
44
+ type GetUpdateType<Bag extends BagTemplate, StateType extends Record<string, unknown>> = Bag extends {
45
+ UpdateType: unknown;
46
+ } ? Bag["UpdateType"] : Partial<StateType>;
47
+ type GetConfigurableType<Bag extends BagTemplate> = Bag extends {
48
+ ConfigurableType: Record<string, unknown>;
49
+ } ? Bag["ConfigurableType"] : Record<string, unknown>;
50
+ type GetInterruptType<Bag extends BagTemplate> = Bag extends {
51
+ InterruptType: unknown;
52
+ } ? Bag["InterruptType"] : unknown;
53
+ type GetCustomEventType<Bag extends BagTemplate> = Bag extends {
54
+ CustomEventType: unknown;
55
+ } ? Bag["CustomEventType"] : unknown;
56
+ interface UseStreamOptions<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> {
39
57
  /**
40
58
  * The ID of the assistant to use.
41
59
  */
@@ -66,11 +84,11 @@ interface UseStreamOptions<StateType extends Record<string, unknown> = Record<st
66
84
  /**
67
85
  * Callback that is called when an update event is received.
68
86
  */
69
- onUpdateEvent?: (data: UpdatesStreamEvent<UpdateType>["data"]) => void;
87
+ onUpdateEvent?: (data: UpdatesStreamEvent<GetUpdateType<Bag, StateType>>["data"]) => void;
70
88
  /**
71
89
  * Callback that is called when a custom event is received.
72
90
  */
73
- onCustomEvent?: (data: CustomStreamEvent<CustomType>["data"]) => void;
91
+ onCustomEvent?: (data: CustomStreamEvent<GetCustomEventType<Bag>>["data"]) => void;
74
92
  /**
75
93
  * Callback that is called when a metadata event is received.
76
94
  */
@@ -84,7 +102,7 @@ interface UseStreamOptions<StateType extends Record<string, unknown> = Record<st
84
102
  */
85
103
  onThreadId?: (threadId: string) => void;
86
104
  }
87
- interface UseStream<StateType extends Record<string, unknown> = Record<string, unknown>, UpdateType extends Record<string, unknown> = Partial<StateType>> {
105
+ interface UseStream<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> {
88
106
  /**
89
107
  * The current values of the thread.
90
108
  */
@@ -104,7 +122,7 @@ interface UseStream<StateType extends Record<string, unknown> = Record<string, u
104
122
  /**
105
123
  * Create and stream a run to the thread.
106
124
  */
107
- submit: (values: UpdateType, options?: SubmitOptions<StateType>) => void;
125
+ submit: (values: GetUpdateType<Bag, StateType> | null | undefined, options?: SubmitOptions<StateType, GetConfigurableType<Bag>>) => void;
108
126
  /**
109
127
  * The current branch of the thread.
110
128
  */
@@ -122,6 +140,10 @@ interface UseStream<StateType extends Record<string, unknown> = Record<string, u
122
140
  * @experimental
123
141
  */
124
142
  experimental_branchTree: Sequence<StateType>;
143
+ /**
144
+ * Get the interrupt value for the stream if interrupted.
145
+ */
146
+ interrupt: Interrupt<GetInterruptType<Bag>> | undefined;
125
147
  /**
126
148
  * Messages inferred from the thread.
127
149
  * Will automatically update with incoming message chunks.
@@ -137,8 +159,11 @@ interface UseStream<StateType extends Record<string, unknown> = Record<string, u
137
159
  */
138
160
  getMessagesMetadata: (message: Message, index?: number) => MessageMetadata<StateType> | undefined;
139
161
  }
140
- interface SubmitOptions<StateType extends Record<string, unknown> = Record<string, unknown>> {
141
- config?: Config;
162
+ type ConfigWithConfigurable<ConfigurableType extends Record<string, unknown>> = Config & {
163
+ configurable?: ConfigurableType;
164
+ };
165
+ interface SubmitOptions<StateType extends Record<string, unknown> = Record<string, unknown>, ConfigurableType extends Record<string, unknown> = Record<string, unknown>> {
166
+ config?: ConfigWithConfigurable<ConfigurableType>;
142
167
  checkpoint?: Omit<Checkpoint, "thread_id"> | null;
143
168
  command?: Command;
144
169
  interruptBefore?: "*" | string[];
@@ -151,5 +176,10 @@ interface SubmitOptions<StateType extends Record<string, unknown> = Record<strin
151
176
  streamMode?: Array<StreamMode>;
152
177
  optimisticValues?: Partial<StateType> | ((prev: StateType) => Partial<StateType>);
153
178
  }
154
- export declare function useStream<StateType extends Record<string, unknown> = Record<string, unknown>, UpdateType extends Record<string, unknown> = Partial<StateType>, CustomType = unknown>(options: UseStreamOptions<StateType, UpdateType, CustomType>): UseStream<StateType, UpdateType>;
179
+ export declare function useStream<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends {
180
+ ConfigurableType?: Record<string, unknown>;
181
+ InterruptType?: unknown;
182
+ CustomEventType?: unknown;
183
+ UpdateType?: unknown;
184
+ } = BagTemplate>(options: UseStreamOptions<StateType, Bag>): UseStream<StateType, Bag>;
155
185
  export {};
@@ -23,6 +23,13 @@ class MessageTupleManager {
23
23
  this.chunks = {};
24
24
  }
25
25
  add(serialized) {
26
+ // TODO: this is sometimes sent from the API
27
+ // figure out how to prevent this or move this to LC.js
28
+ if (serialized.type.endsWith("MessageChunk")) {
29
+ serialized.type = serialized.type
30
+ .slice(0, -"MessageChunk".length)
31
+ .toLowerCase();
32
+ }
26
33
  const chunk = convertToChunk(coerceMessageLikeToMessage(serialized));
27
34
  const id = chunk.id;
28
35
  if (!id)
@@ -192,7 +199,7 @@ export function useStream(options) {
192
199
  const messageManagerRef = useRef(new MessageTupleManager());
193
200
  const submittingRef = useRef(false);
194
201
  const abortRef = useRef(null);
195
- const trackStreamModeRef = useRef(["values", "messages-tuple"]);
202
+ const trackStreamModeRef = useRef(["values"]);
196
203
  const trackStreamMode = useCallback((mode) => {
197
204
  if (!trackStreamModeRef.current.includes(mode))
198
205
  trackStreamModeRef.current.push(mode);
@@ -373,6 +380,7 @@ export function useStream(options) {
373
380
  catch (error) {
374
381
  if (!(error instanceof Error &&
375
382
  (error.name === "AbortError" || error.name === "TimeoutError"))) {
383
+ console.error(error);
376
384
  setStreamError(error);
377
385
  onError?.(error);
378
386
  }
@@ -385,7 +393,7 @@ export function useStream(options) {
385
393
  abortRef.current = null;
386
394
  }
387
395
  };
388
- const error = isLoading ? streamError : historyError;
396
+ const error = streamError ?? historyError;
389
397
  const values = streamValues ?? historyValues;
390
398
  return {
391
399
  get values() {
@@ -400,6 +408,16 @@ export function useStream(options) {
400
408
  setBranch,
401
409
  history: flatHistory,
402
410
  experimental_branchTree: rootSequence,
411
+ get interrupt() {
412
+ // Don't show the interrupt if the stream is loading
413
+ if (isLoading)
414
+ return undefined;
415
+ const interrupts = threadHead?.tasks?.at(-1)?.interrupts;
416
+ if (interrupts == null || interrupts.length === 0)
417
+ return undefined;
418
+ // Return only the current interrupt
419
+ return interrupts.at(-1);
420
+ },
403
421
  get messages() {
404
422
  trackStreamMode("messages-tuple");
405
423
  return getMessages(values);
package/dist/schema.d.ts CHANGED
@@ -103,8 +103,8 @@ export interface AssistantGraph {
103
103
  /**
104
104
  * An interrupt thrown inside a thread.
105
105
  */
106
- export interface Interrupt {
107
- value: unknown;
106
+ export interface Interrupt<TValue = unknown> {
107
+ value: TValue;
108
108
  when: "during";
109
109
  resumable: boolean;
110
110
  ns?: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-sdk",
3
- "version": "0.0.43",
3
+ "version": "0.0.44",
4
4
  "description": "Client library for interacting with the LangGraph API",
5
5
  "type": "module",
6
6
  "packageManager": "yarn@1.22.19",