@langchain/langgraph-sdk 0.0.49 → 0.0.51

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.
@@ -378,7 +378,17 @@ function useStream(options) {
378
378
  if (event === "updates")
379
379
  options.onUpdateEvent?.(data);
380
380
  if (event === "custom")
381
- options.onCustomEvent?.(data);
381
+ options.onCustomEvent?.(data, {
382
+ mutate: (update) => setStreamValues((prev) => {
383
+ // should not happen
384
+ if (prev == null)
385
+ return prev;
386
+ return {
387
+ ...prev,
388
+ ...(typeof update === "function" ? update(prev) : update),
389
+ };
390
+ }),
391
+ });
382
392
  if (event === "metadata")
383
393
  options.onMetadataEvent?.(data);
384
394
  if (event === "values")
@@ -96,7 +96,9 @@ interface UseStreamOptions<StateType extends Record<string, unknown> = Record<st
96
96
  /**
97
97
  * Callback that is called when a custom event is received.
98
98
  */
99
- onCustomEvent?: (data: CustomStreamEvent<GetCustomEventType<Bag>>["data"]) => void;
99
+ onCustomEvent?: (data: CustomStreamEvent<GetCustomEventType<Bag>>["data"], options: {
100
+ mutate: (update: Partial<StateType> | ((prev: StateType) => Partial<StateType>)) => void;
101
+ }) => void;
100
102
  /**
101
103
  * Callback that is called when a metadata event is received.
102
104
  */
@@ -375,7 +375,17 @@ export function useStream(options) {
375
375
  if (event === "updates")
376
376
  options.onUpdateEvent?.(data);
377
377
  if (event === "custom")
378
- options.onCustomEvent?.(data);
378
+ options.onCustomEvent?.(data, {
379
+ mutate: (update) => setStreamValues((prev) => {
380
+ // should not happen
381
+ if (prev == null)
382
+ return prev;
383
+ return {
384
+ ...prev,
385
+ ...(typeof update === "function" ? update(prev) : update),
386
+ };
387
+ }),
388
+ });
379
389
  if (event === "metadata")
380
390
  options.onMetadataEvent?.(data);
381
391
  if (event === "values")
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LoadExternalComponent = exports.useStreamContext = void 0;
3
+ exports.uiMessageReducer = exports.LoadExternalComponent = exports.useStreamContext = void 0;
4
4
  const client_js_1 = require("./client.cjs");
5
5
  (0, client_js_1.bootstrapUiContext)();
6
6
  var client_js_2 = require("./client.cjs");
7
7
  Object.defineProperty(exports, "useStreamContext", { enumerable: true, get: function () { return client_js_2.useStreamContext; } });
8
8
  Object.defineProperty(exports, "LoadExternalComponent", { enumerable: true, get: function () { return client_js_2.LoadExternalComponent; } });
9
+ var types_js_1 = require("./types.cjs");
10
+ Object.defineProperty(exports, "uiMessageReducer", { enumerable: true, get: function () { return types_js_1.uiMessageReducer; } });
@@ -1,2 +1,2 @@
1
1
  export { useStreamContext, LoadExternalComponent } from "./client.js";
2
- export type { UIMessage, RemoveUIMessage } from "./types.js";
2
+ export { uiMessageReducer, type UIMessage, type RemoveUIMessage, } from "./types.js";
@@ -1,3 +1,4 @@
1
1
  import { bootstrapUiContext } from "./client.js";
2
2
  bootstrapUiContext();
3
3
  export { useStreamContext, LoadExternalComponent } from "./client.js";
4
+ export { uiMessageReducer, } from "./types.js";
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.typedUi = void 0;
4
4
  const uuid_1 = require("uuid");
5
5
  const typedUi = (config) => {
6
- let collect = [];
6
+ let items = [];
7
7
  const runId = config.metadata?.run_id ?? config.runId;
8
8
  if (!runId)
9
9
  throw new Error("run_id is required");
@@ -13,23 +13,28 @@ const typedUi = (config) => {
13
13
  name: config.runName,
14
14
  run_id: runId,
15
15
  };
16
- const create = (name, props) => ({
17
- type: "ui",
18
- id: (0, uuid_1.v4)(),
19
- name,
20
- content: props,
21
- additional_kwargs: metadata,
22
- });
23
- const remove = (id) => ({ type: "remove-ui", id });
24
- return {
25
- create,
26
- remove,
27
- collect,
28
- write: (name, props) => {
29
- const evt = create(name, props);
30
- collect.push(evt);
31
- config.writer?.(evt);
32
- },
16
+ const handlePush = (message, options) => {
17
+ const evt = {
18
+ type: "ui",
19
+ id: message?.id ?? (0, uuid_1.v4)(),
20
+ name: message?.name,
21
+ content: message?.content,
22
+ additional_kwargs: {
23
+ ...metadata,
24
+ ...message?.additional_kwargs,
25
+ ...(options?.message ? { message_id: options.message.id } : null),
26
+ },
27
+ };
28
+ items.push(evt);
29
+ config.writer?.(evt);
30
+ return evt;
33
31
  };
32
+ const handleDelete = (id) => {
33
+ const evt = { type: "remove-ui", id };
34
+ items.push(evt);
35
+ config.writer?.(evt);
36
+ return evt;
37
+ };
38
+ return { push: handlePush, delete: handleDelete, items };
34
39
  };
35
40
  exports.typedUi = typedUi;
@@ -1,5 +1,8 @@
1
1
  import type { ComponentPropsWithoutRef, ElementType } from "react";
2
2
  import type { RemoveUIMessage, UIMessage } from "../types.js";
3
+ interface MessageLike {
4
+ id?: string;
5
+ }
3
6
  export declare const typedUi: <Decl extends Record<string, ElementType>>(config: {
4
7
  writer?: (chunk: unknown) => void;
5
8
  runId?: string;
@@ -7,8 +10,15 @@ export declare const typedUi: <Decl extends Record<string, ElementType>>(config:
7
10
  tags?: string[];
8
11
  runName?: string;
9
12
  }) => {
10
- create: <K extends keyof Decl & string>(name: K, props: { [K_1 in keyof Decl]: ComponentPropsWithoutRef<Decl[K_1]>; }[K]) => UIMessage;
11
- remove: (id: string) => RemoveUIMessage;
12
- collect: (UIMessage | RemoveUIMessage)[];
13
- write: <K_2 extends keyof Decl & string>(name: K_2, props: { [K_1 in keyof Decl]: ComponentPropsWithoutRef<Decl[K_1]>; }[K_2]) => void;
13
+ push: <K extends keyof Decl & string>(message: {
14
+ id?: string | undefined;
15
+ name: K;
16
+ content: { [K_1 in keyof Decl]: ComponentPropsWithoutRef<Decl[K_1]>; }[K];
17
+ additional_kwargs?: Record<string, unknown> | undefined;
18
+ }, options?: {
19
+ message?: MessageLike;
20
+ }) => UIMessage;
21
+ delete: (id: string) => RemoveUIMessage;
22
+ items: (UIMessage | RemoveUIMessage)[];
14
23
  };
24
+ export {};
@@ -1,6 +1,6 @@
1
1
  import { v4 as uuidv4 } from "uuid";
2
2
  export const typedUi = (config) => {
3
- let collect = [];
3
+ let items = [];
4
4
  const runId = config.metadata?.run_id ?? config.runId;
5
5
  if (!runId)
6
6
  throw new Error("run_id is required");
@@ -10,22 +10,27 @@ export const typedUi = (config) => {
10
10
  name: config.runName,
11
11
  run_id: runId,
12
12
  };
13
- const create = (name, props) => ({
14
- type: "ui",
15
- id: uuidv4(),
16
- name,
17
- content: props,
18
- additional_kwargs: metadata,
19
- });
20
- const remove = (id) => ({ type: "remove-ui", id });
21
- return {
22
- create,
23
- remove,
24
- collect,
25
- write: (name, props) => {
26
- const evt = create(name, props);
27
- collect.push(evt);
28
- config.writer?.(evt);
29
- },
13
+ const handlePush = (message, options) => {
14
+ const evt = {
15
+ type: "ui",
16
+ id: message?.id ?? uuidv4(),
17
+ name: message?.name,
18
+ content: message?.content,
19
+ additional_kwargs: {
20
+ ...metadata,
21
+ ...message?.additional_kwargs,
22
+ ...(options?.message ? { message_id: options.message.id } : null),
23
+ },
24
+ };
25
+ items.push(evt);
26
+ config.writer?.(evt);
27
+ return evt;
30
28
  };
29
+ const handleDelete = (id) => {
30
+ const evt = { type: "remove-ui", id };
31
+ items.push(evt);
32
+ config.writer?.(evt);
33
+ return evt;
34
+ };
35
+ return { push: handlePush, delete: handleDelete, items };
31
36
  };
@@ -5,6 +5,7 @@ export interface UIMessage {
5
5
  content: Record<string, unknown>;
6
6
  additional_kwargs: {
7
7
  run_id: string;
8
+ message_id?: string;
8
9
  [key: string]: unknown;
9
10
  };
10
11
  }
@@ -28,9 +28,7 @@ export type AIMessage = {
28
28
  tool_calls?: {
29
29
  name: string;
30
30
  args: {
31
- [x: string]: {
32
- [x: string]: any;
33
- };
31
+ [x: string]: any;
34
32
  };
35
33
  id?: string | undefined;
36
34
  type?: "tool_call" | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-sdk",
3
- "version": "0.0.49",
3
+ "version": "0.0.51",
4
4
  "description": "Client library for interacting with the LangGraph API",
5
5
  "type": "module",
6
6
  "packageManager": "yarn@1.22.19",