@langchain/langgraph-sdk 0.0.78 → 0.0.79

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.
@@ -346,12 +346,14 @@ function useStream(options) {
346
346
  }
347
347
  };
348
348
  async function consumeStream(action) {
349
+ let getCallbackMeta;
349
350
  try {
350
351
  setIsLoading(true);
351
352
  setStreamError(undefined);
352
353
  submittingRef.current = true;
353
354
  abortRef.current = new AbortController();
354
355
  const run = await action(abortRef.current.signal);
356
+ getCallbackMeta = run.getCallbackMeta;
355
357
  let streamError;
356
358
  for await (const { event, data } of run.stream) {
357
359
  if (event === "error") {
@@ -406,14 +408,14 @@ function useStream(options) {
406
408
  throw streamError;
407
409
  const lastHead = result.at(0);
408
410
  if (lastHead)
409
- onFinish?.(lastHead);
411
+ onFinish?.(lastHead, getCallbackMeta?.());
410
412
  }
411
413
  catch (error) {
412
414
  if (!(error instanceof Error &&
413
415
  (error.name === "AbortError" || error.name === "TimeoutError"))) {
414
416
  console.error(error);
415
417
  setStreamError(error);
416
- onError?.(error);
418
+ onError?.(error, getCallbackMeta?.());
417
419
  }
418
420
  }
419
421
  finally {
@@ -439,6 +441,7 @@ function useStream(options) {
439
441
  return history.mutate(threadId);
440
442
  },
441
443
  stream,
444
+ getCallbackMeta: () => ({ thread_id: threadId, run_id: runId }),
442
445
  };
443
446
  });
444
447
  };
@@ -480,6 +483,8 @@ function useStream(options) {
480
483
  if (checkpoint != null)
481
484
  delete checkpoint.thread_id;
482
485
  let rejoinKey;
486
+ let callbackMeta;
487
+ const streamResumable = submitOptions?.streamResumable ?? !!runMetadataStorage;
483
488
  const stream = client.runs.stream(usableThreadId, assistantId, {
484
489
  input: values,
485
490
  config: submitOptions?.config,
@@ -490,26 +495,27 @@ function useStream(options) {
490
495
  multitaskStrategy: submitOptions?.multitaskStrategy,
491
496
  onCompletion: submitOptions?.onCompletion,
492
497
  onDisconnect: submitOptions?.onDisconnect ??
493
- (runMetadataStorage ? "continue" : "cancel"),
498
+ (streamResumable ? "continue" : "cancel"),
494
499
  signal,
495
500
  checkpoint,
496
501
  streamMode,
497
502
  streamSubgraphs: submitOptions?.streamSubgraphs,
498
- streamResumable: submitOptions?.streamResumable ?? !!runMetadataStorage,
503
+ streamResumable,
499
504
  onRunCreated(params) {
500
- const runParams = {
505
+ callbackMeta = {
501
506
  run_id: params.run_id,
502
507
  thread_id: params.thread_id ?? usableThreadId,
503
508
  };
504
509
  if (runMetadataStorage) {
505
- rejoinKey = `lg:stream:${runParams.thread_id}`;
506
- runMetadataStorage.setItem(rejoinKey, runParams.run_id);
510
+ rejoinKey = `lg:stream:${callbackMeta.thread_id}`;
511
+ runMetadataStorage.setItem(rejoinKey, callbackMeta.run_id);
507
512
  }
508
- onCreated?.(runParams);
513
+ onCreated?.(callbackMeta);
509
514
  },
510
515
  });
511
516
  return {
512
517
  stream,
518
+ getCallbackMeta: () => callbackMeta,
513
519
  onSuccess: () => {
514
520
  if (rejoinKey)
515
521
  runMetadataStorage?.removeItem(rejoinKey);
@@ -53,6 +53,10 @@ type GetInterruptType<Bag extends BagTemplate> = Bag extends {
53
53
  type GetCustomEventType<Bag extends BagTemplate> = Bag extends {
54
54
  CustomEventType: unknown;
55
55
  } ? Bag["CustomEventType"] : unknown;
56
+ interface RunCallbackMeta {
57
+ run_id: string;
58
+ thread_id: string;
59
+ }
56
60
  export interface UseStreamOptions<StateType extends Record<string, unknown> = Record<string, unknown>, Bag extends BagTemplate = BagTemplate> {
57
61
  /**
58
62
  * The ID of the assistant to use.
@@ -88,18 +92,15 @@ export interface UseStreamOptions<StateType extends Record<string, unknown> = Re
88
92
  /**
89
93
  * Callback that is called when an error occurs.
90
94
  */
91
- onError?: (error: unknown) => void;
95
+ onError?: (error: unknown, run: RunCallbackMeta | undefined) => void;
92
96
  /**
93
97
  * Callback that is called when the stream is finished.
94
98
  */
95
- onFinish?: (state: ThreadState<StateType>) => void;
99
+ onFinish?: (state: ThreadState<StateType>, run: RunCallbackMeta | undefined) => void;
96
100
  /**
97
101
  * Callback that is called when a new stream is created.
98
102
  */
99
- onCreated?: (run: {
100
- run_id: string;
101
- thread_id: string;
102
- }) => void;
103
+ onCreated?: (run: RunCallbackMeta) => void;
103
104
  /**
104
105
  * Callback that is called when an update event is received.
105
106
  */
@@ -343,12 +343,14 @@ export function useStream(options) {
343
343
  }
344
344
  };
345
345
  async function consumeStream(action) {
346
+ let getCallbackMeta;
346
347
  try {
347
348
  setIsLoading(true);
348
349
  setStreamError(undefined);
349
350
  submittingRef.current = true;
350
351
  abortRef.current = new AbortController();
351
352
  const run = await action(abortRef.current.signal);
353
+ getCallbackMeta = run.getCallbackMeta;
352
354
  let streamError;
353
355
  for await (const { event, data } of run.stream) {
354
356
  if (event === "error") {
@@ -403,14 +405,14 @@ export function useStream(options) {
403
405
  throw streamError;
404
406
  const lastHead = result.at(0);
405
407
  if (lastHead)
406
- onFinish?.(lastHead);
408
+ onFinish?.(lastHead, getCallbackMeta?.());
407
409
  }
408
410
  catch (error) {
409
411
  if (!(error instanceof Error &&
410
412
  (error.name === "AbortError" || error.name === "TimeoutError"))) {
411
413
  console.error(error);
412
414
  setStreamError(error);
413
- onError?.(error);
415
+ onError?.(error, getCallbackMeta?.());
414
416
  }
415
417
  }
416
418
  finally {
@@ -436,6 +438,7 @@ export function useStream(options) {
436
438
  return history.mutate(threadId);
437
439
  },
438
440
  stream,
441
+ getCallbackMeta: () => ({ thread_id: threadId, run_id: runId }),
439
442
  };
440
443
  });
441
444
  };
@@ -477,6 +480,8 @@ export function useStream(options) {
477
480
  if (checkpoint != null)
478
481
  delete checkpoint.thread_id;
479
482
  let rejoinKey;
483
+ let callbackMeta;
484
+ const streamResumable = submitOptions?.streamResumable ?? !!runMetadataStorage;
480
485
  const stream = client.runs.stream(usableThreadId, assistantId, {
481
486
  input: values,
482
487
  config: submitOptions?.config,
@@ -487,26 +492,27 @@ export function useStream(options) {
487
492
  multitaskStrategy: submitOptions?.multitaskStrategy,
488
493
  onCompletion: submitOptions?.onCompletion,
489
494
  onDisconnect: submitOptions?.onDisconnect ??
490
- (runMetadataStorage ? "continue" : "cancel"),
495
+ (streamResumable ? "continue" : "cancel"),
491
496
  signal,
492
497
  checkpoint,
493
498
  streamMode,
494
499
  streamSubgraphs: submitOptions?.streamSubgraphs,
495
- streamResumable: submitOptions?.streamResumable ?? !!runMetadataStorage,
500
+ streamResumable,
496
501
  onRunCreated(params) {
497
- const runParams = {
502
+ callbackMeta = {
498
503
  run_id: params.run_id,
499
504
  thread_id: params.thread_id ?? usableThreadId,
500
505
  };
501
506
  if (runMetadataStorage) {
502
- rejoinKey = `lg:stream:${runParams.thread_id}`;
503
- runMetadataStorage.setItem(rejoinKey, runParams.run_id);
507
+ rejoinKey = `lg:stream:${callbackMeta.thread_id}`;
508
+ runMetadataStorage.setItem(rejoinKey, callbackMeta.run_id);
504
509
  }
505
- onCreated?.(runParams);
510
+ onCreated?.(callbackMeta);
506
511
  },
507
512
  });
508
513
  return {
509
514
  stream,
515
+ getCallbackMeta: () => callbackMeta,
510
516
  onSuccess: () => {
511
517
  if (rejoinKey)
512
518
  runMetadataStorage?.removeItem(rejoinKey);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-sdk",
3
- "version": "0.0.78",
3
+ "version": "0.0.79",
4
4
  "description": "Client library for interacting with the LangGraph API",
5
5
  "type": "module",
6
6
  "packageManager": "yarn@1.22.19",