@langchain/langgraph-sdk 0.0.51 → 0.0.53

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/client.cjs CHANGED
@@ -907,6 +907,35 @@ class StoreClient extends BaseClient {
907
907
  }
908
908
  }
909
909
  exports.StoreClient = StoreClient;
910
+ class UiClient extends BaseClient {
911
+ static getOrCached(key, fn) {
912
+ if (UiClient.promiseCache[key] != null) {
913
+ return UiClient.promiseCache[key];
914
+ }
915
+ const promise = fn();
916
+ UiClient.promiseCache[key] = promise;
917
+ return promise;
918
+ }
919
+ async getComponent(assistantId, agentName) {
920
+ return UiClient["getOrCached"](`${this.apiUrl}-${assistantId}-${agentName}`, async () => {
921
+ const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(`/ui/${assistantId}`, {
922
+ headers: {
923
+ Accept: "text/html",
924
+ "Content-Type": "application/json",
925
+ },
926
+ method: "POST",
927
+ json: { name: agentName },
928
+ }));
929
+ return response.text();
930
+ });
931
+ }
932
+ }
933
+ Object.defineProperty(UiClient, "promiseCache", {
934
+ enumerable: true,
935
+ configurable: true,
936
+ writable: true,
937
+ value: {}
938
+ });
910
939
  class Client {
911
940
  constructor(config) {
912
941
  /**
@@ -954,11 +983,22 @@ class Client {
954
983
  writable: true,
955
984
  value: void 0
956
985
  });
986
+ /**
987
+ * The client for interacting with the UI.
988
+ * @internal Used by LoadExternalComponent and the API might change in the future.
989
+ */
990
+ Object.defineProperty(this, "~ui", {
991
+ enumerable: true,
992
+ configurable: true,
993
+ writable: true,
994
+ value: void 0
995
+ });
957
996
  this.assistants = new AssistantsClient(config);
958
997
  this.threads = new ThreadsClient(config);
959
998
  this.runs = new RunsClient(config);
960
999
  this.crons = new CronsClient(config);
961
1000
  this.store = new StoreClient(config);
1001
+ this["~ui"] = new UiClient(config);
962
1002
  }
963
1003
  }
964
1004
  exports.Client = Client;
package/dist/client.d.ts CHANGED
@@ -504,6 +504,11 @@ export declare class StoreClient extends BaseClient {
504
504
  offset?: number;
505
505
  }): Promise<ListNamespaceResponse>;
506
506
  }
507
+ declare class UiClient extends BaseClient {
508
+ private static promiseCache;
509
+ private static getOrCached;
510
+ getComponent(assistantId: string, agentName: string): Promise<string>;
511
+ }
507
512
  export declare class Client<TStateType = DefaultValues, TUpdateType = TStateType, TCustomEventType = unknown> {
508
513
  /**
509
514
  * The client for interacting with assistants.
@@ -525,6 +530,11 @@ export declare class Client<TStateType = DefaultValues, TUpdateType = TStateType
525
530
  * The client for interacting with the KV store.
526
531
  */
527
532
  store: StoreClient;
533
+ /**
534
+ * The client for interacting with the UI.
535
+ * @internal Used by LoadExternalComponent and the API might change in the future.
536
+ */
537
+ "~ui": UiClient;
528
538
  constructor(config?: ClientConfig);
529
539
  }
530
540
  export {};
package/dist/client.js CHANGED
@@ -898,6 +898,35 @@ export class StoreClient extends BaseClient {
898
898
  });
899
899
  }
900
900
  }
901
+ class UiClient extends BaseClient {
902
+ static getOrCached(key, fn) {
903
+ if (UiClient.promiseCache[key] != null) {
904
+ return UiClient.promiseCache[key];
905
+ }
906
+ const promise = fn();
907
+ UiClient.promiseCache[key] = promise;
908
+ return promise;
909
+ }
910
+ async getComponent(assistantId, agentName) {
911
+ return UiClient["getOrCached"](`${this.apiUrl}-${assistantId}-${agentName}`, async () => {
912
+ const response = await this.asyncCaller.fetch(...this.prepareFetchOptions(`/ui/${assistantId}`, {
913
+ headers: {
914
+ Accept: "text/html",
915
+ "Content-Type": "application/json",
916
+ },
917
+ method: "POST",
918
+ json: { name: agentName },
919
+ }));
920
+ return response.text();
921
+ });
922
+ }
923
+ }
924
+ Object.defineProperty(UiClient, "promiseCache", {
925
+ enumerable: true,
926
+ configurable: true,
927
+ writable: true,
928
+ value: {}
929
+ });
901
930
  export class Client {
902
931
  constructor(config) {
903
932
  /**
@@ -945,10 +974,21 @@ export class Client {
945
974
  writable: true,
946
975
  value: void 0
947
976
  });
977
+ /**
978
+ * The client for interacting with the UI.
979
+ * @internal Used by LoadExternalComponent and the API might change in the future.
980
+ */
981
+ Object.defineProperty(this, "~ui", {
982
+ enumerable: true,
983
+ configurable: true,
984
+ writable: true,
985
+ value: void 0
986
+ });
948
987
  this.assistants = new AssistantsClient(config);
949
988
  this.threads = new ThreadsClient(config);
950
989
  this.runs = new RunsClient(config);
951
990
  this.crons = new CronsClient(config);
952
991
  this.store = new StoreClient(config);
992
+ this["~ui"] = new UiClient(config);
953
993
  }
954
994
  }
@@ -444,6 +444,8 @@ function useStream(options) {
444
444
  trackStreamMode("values");
445
445
  return values;
446
446
  },
447
+ client,
448
+ assistantId,
447
449
  error,
448
450
  isLoading,
449
451
  stop,
@@ -1,4 +1,4 @@
1
- import { type ClientConfig } from "../client.js";
1
+ import { Client, 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
4
  import type { Checkpoint, Config, Interrupt, Metadata, ThreadState } from "../schema.js";
@@ -168,6 +168,14 @@ export interface UseStream<StateType extends Record<string, unknown> = Record<st
168
168
  * @returns The metadata for the message.
169
169
  */
170
170
  getMessagesMetadata: (message: Message, index?: number) => MessageMetadata<StateType> | undefined;
171
+ /**
172
+ * LangGraph SDK client used to send request and receive responses.
173
+ */
174
+ client: Client;
175
+ /**
176
+ * The ID of the assistant to use.
177
+ */
178
+ assistantId: string;
171
179
  }
172
180
  type ConfigWithConfigurable<ConfigurableType extends Record<string, unknown>> = Config & {
173
181
  configurable?: ConfigurableType;
@@ -441,6 +441,8 @@ export function useStream(options) {
441
441
  trackStreamMode("values");
442
442
  return values;
443
443
  },
444
+ client,
445
+ assistantId,
444
446
  error,
445
447
  isLoading,
446
448
  stop,
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ "use client";
2
3
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
4
  if (k2 === undefined) k2 = k;
4
5
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -84,23 +85,9 @@ class ComponentStore {
84
85
  }
85
86
  }
86
87
  const COMPONENT_STORE = new ComponentStore();
87
- const COMPONENT_PROMISE_CACHE = {};
88
88
  const EXT_STORE_SYMBOL = Symbol.for("LGUI_EXT_STORE");
89
89
  const REQUIRE_SYMBOL = Symbol.for("LGUI_REQUIRE");
90
- function fetchComponent(apiUrl, assistantId, agentName) {
91
- const cacheKey = `${apiUrl}-${assistantId}-${agentName}`;
92
- if (COMPONENT_PROMISE_CACHE[cacheKey] != null) {
93
- return COMPONENT_PROMISE_CACHE[cacheKey];
94
- }
95
- const request = fetch(`${apiUrl}/ui/${assistantId}`, {
96
- headers: { Accept: "text/html", "Content-Type": "application/json" },
97
- method: "POST",
98
- body: JSON.stringify({ name: agentName }),
99
- }).then((a) => a.text());
100
- COMPONENT_PROMISE_CACHE[cacheKey] = request;
101
- return request;
102
- }
103
- function LoadExternalComponent({ apiUrl = "http://localhost:2024", assistantId, stream, message, meta, fallback, components, ...props }) {
90
+ function LoadExternalComponent({ stream, message, meta, fallback, components, ...props }) {
104
91
  const ref = React.useRef(null);
105
92
  const id = React.useId();
106
93
  const shadowRootId = `child-shadow-${id}`;
@@ -108,10 +95,11 @@ function LoadExternalComponent({ apiUrl = "http://localhost:2024", assistantId,
108
95
  const state = React.useSyncExternalStore(store.subscribe, store.getSnapshot);
109
96
  const clientComponent = components?.[message.name];
110
97
  const hasClientComponent = clientComponent != null;
98
+ const uiClient = stream.client["~ui"];
111
99
  React.useEffect(() => {
112
100
  if (hasClientComponent)
113
101
  return;
114
- fetchComponent(apiUrl, assistantId, message.name).then((html) => {
102
+ uiClient.getComponent(stream.assistantId, message.name).then((html) => {
115
103
  const dom = ref.current;
116
104
  if (!dom)
117
105
  return;
@@ -121,12 +109,18 @@ function LoadExternalComponent({ apiUrl = "http://localhost:2024", assistantId,
121
109
  .createContextualFragment(html.replace("{{shadowRootId}}", shadowRootId));
122
110
  root.appendChild(fragment);
123
111
  });
124
- }, [apiUrl, assistantId, message.name, shadowRootId, hasClientComponent]);
112
+ }, [
113
+ uiClient,
114
+ stream.assistantId,
115
+ message.name,
116
+ shadowRootId,
117
+ hasClientComponent,
118
+ ]);
125
119
  if (hasClientComponent) {
126
- return React.createElement(clientComponent, message.content);
120
+ return React.createElement(clientComponent, message.props);
127
121
  }
128
122
  return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { id: shadowRootId, ref: ref, ...props }), (0, jsx_runtime_1.jsx)(UseStreamContext.Provider, { value: { stream, meta }, children: state?.target != null
129
- ? ReactDOM.createPortal(React.createElement(state.comp, message.content), state.target)
123
+ ? ReactDOM.createPortal(React.createElement(state.comp, message.props), state.target)
130
124
  : fallback })] }));
131
125
  }
132
126
  exports.LoadExternalComponent = LoadExternalComponent;
@@ -49,10 +49,6 @@ declare class ComponentStore {
49
49
  declare const EXT_STORE_SYMBOL: unique symbol;
50
50
  declare const REQUIRE_SYMBOL: unique symbol;
51
51
  interface LoadExternalComponentProps extends Pick<React.HTMLAttributes<HTMLDivElement>, "style" | "className"> {
52
- /** API URL of the LangGraph Platform */
53
- apiUrl?: string;
54
- /** ID of the assistant */
55
- assistantId: string;
56
52
  /** Stream of the assistant */
57
53
  stream: ReturnType<typeof useStream>;
58
54
  /** UI message to be rendered */
@@ -67,7 +63,7 @@ interface LoadExternalComponentProps extends Pick<React.HTMLAttributes<HTMLDivEl
67
63
  */
68
64
  components?: Record<string, React.FunctionComponent | React.ComponentClass>;
69
65
  }
70
- export declare function LoadExternalComponent({ apiUrl, assistantId, stream, message, meta, fallback, components, ...props }: LoadExternalComponentProps): JsxRuntime.JSX.Element;
66
+ export declare function LoadExternalComponent({ stream, message, meta, fallback, components, ...props }: LoadExternalComponentProps): JsxRuntime.JSX.Element;
71
67
  declare global {
72
68
  interface Window {
73
69
  [EXT_STORE_SYMBOL]: ComponentStore;
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
3
  import { useStream } from "../react/index.js";
3
4
  import * as React from "react";
@@ -57,23 +58,9 @@ class ComponentStore {
57
58
  }
58
59
  }
59
60
  const COMPONENT_STORE = new ComponentStore();
60
- const COMPONENT_PROMISE_CACHE = {};
61
61
  const EXT_STORE_SYMBOL = Symbol.for("LGUI_EXT_STORE");
62
62
  const REQUIRE_SYMBOL = Symbol.for("LGUI_REQUIRE");
63
- function fetchComponent(apiUrl, assistantId, agentName) {
64
- const cacheKey = `${apiUrl}-${assistantId}-${agentName}`;
65
- if (COMPONENT_PROMISE_CACHE[cacheKey] != null) {
66
- return COMPONENT_PROMISE_CACHE[cacheKey];
67
- }
68
- const request = fetch(`${apiUrl}/ui/${assistantId}`, {
69
- headers: { Accept: "text/html", "Content-Type": "application/json" },
70
- method: "POST",
71
- body: JSON.stringify({ name: agentName }),
72
- }).then((a) => a.text());
73
- COMPONENT_PROMISE_CACHE[cacheKey] = request;
74
- return request;
75
- }
76
- export function LoadExternalComponent({ apiUrl = "http://localhost:2024", assistantId, stream, message, meta, fallback, components, ...props }) {
63
+ export function LoadExternalComponent({ stream, message, meta, fallback, components, ...props }) {
77
64
  const ref = React.useRef(null);
78
65
  const id = React.useId();
79
66
  const shadowRootId = `child-shadow-${id}`;
@@ -81,10 +68,11 @@ export function LoadExternalComponent({ apiUrl = "http://localhost:2024", assist
81
68
  const state = React.useSyncExternalStore(store.subscribe, store.getSnapshot);
82
69
  const clientComponent = components?.[message.name];
83
70
  const hasClientComponent = clientComponent != null;
71
+ const uiClient = stream.client["~ui"];
84
72
  React.useEffect(() => {
85
73
  if (hasClientComponent)
86
74
  return;
87
- fetchComponent(apiUrl, assistantId, message.name).then((html) => {
75
+ uiClient.getComponent(stream.assistantId, message.name).then((html) => {
88
76
  const dom = ref.current;
89
77
  if (!dom)
90
78
  return;
@@ -94,12 +82,18 @@ export function LoadExternalComponent({ apiUrl = "http://localhost:2024", assist
94
82
  .createContextualFragment(html.replace("{{shadowRootId}}", shadowRootId));
95
83
  root.appendChild(fragment);
96
84
  });
97
- }, [apiUrl, assistantId, message.name, shadowRootId, hasClientComponent]);
85
+ }, [
86
+ uiClient,
87
+ stream.assistantId,
88
+ message.name,
89
+ shadowRootId,
90
+ hasClientComponent,
91
+ ]);
98
92
  if (hasClientComponent) {
99
- return React.createElement(clientComponent, message.content);
93
+ return React.createElement(clientComponent, message.props);
100
94
  }
101
95
  return (_jsxs(_Fragment, { children: [_jsx("div", { id: shadowRootId, ref: ref, ...props }), _jsx(UseStreamContext.Provider, { value: { stream, meta }, children: state?.target != null
102
- ? ReactDOM.createPortal(React.createElement(state.comp, message.content), state.target)
96
+ ? ReactDOM.createPortal(React.createElement(state.comp, message.props), state.target)
103
97
  : fallback })] }));
104
98
  }
105
99
  export function bootstrapUiContext() {
@@ -18,10 +18,10 @@ const typedUi = (config) => {
18
18
  type: "ui",
19
19
  id: message?.id ?? (0, uuid_1.v4)(),
20
20
  name: message?.name,
21
- content: message?.content,
22
- additional_kwargs: {
21
+ props: message?.props,
22
+ metadata: {
23
23
  ...metadata,
24
- ...message?.additional_kwargs,
24
+ ...message?.metadata,
25
25
  ...(options?.message ? { message_id: options.message.id } : null),
26
26
  },
27
27
  };
@@ -13,8 +13,8 @@ export declare const typedUi: <Decl extends Record<string, ElementType>>(config:
13
13
  push: <K extends keyof Decl & string>(message: {
14
14
  id?: string | undefined;
15
15
  name: K;
16
- content: { [K_1 in keyof Decl]: ComponentPropsWithoutRef<Decl[K_1]>; }[K];
17
- additional_kwargs?: Record<string, unknown> | undefined;
16
+ props: { [K_1 in keyof Decl]: ComponentPropsWithoutRef<Decl[K_1]>; }[K];
17
+ metadata?: Record<string, unknown> | undefined;
18
18
  }, options?: {
19
19
  message?: MessageLike;
20
20
  }) => UIMessage;
@@ -15,10 +15,10 @@ export const typedUi = (config) => {
15
15
  type: "ui",
16
16
  id: message?.id ?? uuidv4(),
17
17
  name: message?.name,
18
- content: message?.content,
19
- additional_kwargs: {
18
+ props: message?.props,
19
+ metadata: {
20
20
  ...metadata,
21
- ...message?.additional_kwargs,
21
+ ...message?.metadata,
22
22
  ...(options?.message ? { message_id: options.message.id } : null),
23
23
  },
24
24
  };
@@ -2,8 +2,8 @@ export interface UIMessage {
2
2
  type: "ui";
3
3
  id: string;
4
4
  name: string;
5
- content: Record<string, unknown>;
6
- additional_kwargs: {
5
+ props: Record<string, unknown>;
6
+ metadata: {
7
7
  run_id: string;
8
8
  message_id?: string;
9
9
  [key: string]: unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-sdk",
3
- "version": "0.0.51",
3
+ "version": "0.0.53",
4
4
  "description": "Client library for interacting with the LangGraph API",
5
5
  "type": "module",
6
6
  "packageManager": "yarn@1.22.19",