@langgraph-js/sdk 3.0.0 → 3.0.3

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.
@@ -208,7 +208,7 @@ export class LangGraphClient extends EventEmitter {
208
208
  throw new Error("Thread or Assistant not initialized");
209
209
  }
210
210
  if (!this.currentThread) {
211
- await this.createThread({ graphId: this.currentAssistant.graph_id, threadId: this.currentThread.thread_id });
211
+ await this.createThread({ graphId: this.currentAssistant.graph_id });
212
212
  this.emit("thread", {
213
213
  event: "thread/create",
214
214
  data: {
@@ -1,4 +1,4 @@
1
- import { createElement, createContext, useContext, useMemo, useEffect } from "react";
1
+ import { createElement, createContext, useContext, useMemo, useEffect, useRef } from "react";
2
2
  import { createChatStore, useUnionStore } from "../ui-store/index.js";
3
3
  import { useStore } from "@nanostores/react";
4
4
  const ChatContext = createContext(undefined);
@@ -9,7 +9,14 @@ export const useChat = () => {
9
9
  }
10
10
  return context;
11
11
  };
12
- export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://localhost:8123", defaultHeaders = {}, withCredentials = false, showHistory = false, showGraph = false, onInitError, }) => {
12
+ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://localhost:8123", defaultHeaders, withCredentials = false, showHistory = false, showGraph = false, onInitError, }) => {
13
+ // 使用 useMemo 稳定 defaultHeaders 的引用
14
+ const stableHeaders = useMemo(() => defaultHeaders || {}, [defaultHeaders]);
15
+ // 使用 useRef 保存 onInitError 的最新引用
16
+ const onInitErrorRef = useRef(onInitError);
17
+ useEffect(() => {
18
+ onInitErrorRef.current = onInitError;
19
+ }, [onInitError]);
13
20
  const store = useMemo(() => {
14
21
  const F = withCredentials
15
22
  ? (url, options) => {
@@ -19,7 +26,7 @@ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://loc
19
26
  : fetch;
20
27
  return createChatStore(defaultAgent, {
21
28
  apiUrl,
22
- defaultHeaders,
29
+ defaultHeaders: stableHeaders,
23
30
  callerOptions: {
24
31
  fetch: F,
25
32
  maxRetries: 1,
@@ -28,9 +35,15 @@ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://loc
28
35
  showHistory,
29
36
  showGraph,
30
37
  });
31
- }, [defaultAgent, apiUrl, defaultHeaders, withCredentials, showHistory, showGraph]);
38
+ }, [defaultAgent, apiUrl, stableHeaders, withCredentials, showHistory, showGraph]);
32
39
  const unionStore = useUnionStore(store, useStore);
40
+ // 使用 ref 标记是否已初始化
41
+ const initializedRef = useRef(false);
33
42
  useEffect(() => {
43
+ if (initializedRef.current) {
44
+ return;
45
+ }
46
+ initializedRef.current = true;
34
47
  unionStore
35
48
  .initClient()
36
49
  .then((res) => {
@@ -40,10 +53,10 @@ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://loc
40
53
  })
41
54
  .catch((err) => {
42
55
  console.error(err);
43
- if (onInitError) {
44
- onInitError(err, unionStore.currentAgent);
56
+ if (onInitErrorRef.current) {
57
+ onInitErrorRef.current(err, unionStore.currentAgent);
45
58
  }
46
59
  });
47
- }, [unionStore, onInitError]);
60
+ }, [unionStore]);
48
61
  return createElement(ChatContext.Provider, { value: unionStore }, children);
49
62
  };
@@ -1,4 +1,4 @@
1
- import { defineComponent, inject, provide, onMounted } from "vue";
1
+ import { defineComponent, inject, provide, onMounted, defineExpose } from "vue";
2
2
  import { createChatStore } from "../ui-store/index.js";
3
3
  import { useStore } from "@nanostores/vue";
4
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langgraph-js/sdk",
3
- "version": "3.0.0",
3
+ "version": "3.0.3",
4
4
  "description": "The UI SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -302,7 +302,7 @@ export class LangGraphClient<TStateType = unknown> extends EventEmitter<LangGrap
302
302
  throw new Error("Thread or Assistant not initialized");
303
303
  }
304
304
  if (!this.currentThread) {
305
- await this.createThread({ graphId: this.currentAssistant!.graph_id!, threadId: this.currentThread!.thread_id! });
305
+ await this.createThread({ graphId: this.currentAssistant!.graph_id! });
306
306
  this.emit("thread", {
307
307
  event: "thread/create",
308
308
  data: {
@@ -1,4 +1,4 @@
1
- import { createElement, createContext, useContext, useMemo, ReactNode, useEffect } from "react";
1
+ import { createElement, createContext, useContext, useMemo, ReactNode, useEffect, useRef } from "react";
2
2
 
3
3
  import { createChatStore, UnionStore, useUnionStore } from "../ui-store/index.js";
4
4
  import { useStore } from "@nanostores/react";
@@ -28,12 +28,21 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
28
28
  children,
29
29
  defaultAgent = "",
30
30
  apiUrl = "http://localhost:8123",
31
- defaultHeaders = {},
31
+ defaultHeaders,
32
32
  withCredentials = false,
33
33
  showHistory = false,
34
34
  showGraph = false,
35
35
  onInitError,
36
36
  }) => {
37
+ // 使用 useMemo 稳定 defaultHeaders 的引用
38
+ const stableHeaders = useMemo(() => defaultHeaders || {}, [defaultHeaders]);
39
+
40
+ // 使用 useRef 保存 onInitError 的最新引用
41
+ const onInitErrorRef = useRef(onInitError);
42
+ useEffect(() => {
43
+ onInitErrorRef.current = onInitError;
44
+ }, [onInitError]);
45
+
37
46
  const store = useMemo(() => {
38
47
  const F = withCredentials
39
48
  ? (url: string, options: RequestInit) => {
@@ -46,7 +55,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
46
55
  defaultAgent,
47
56
  {
48
57
  apiUrl,
49
- defaultHeaders,
58
+ defaultHeaders: stableHeaders,
50
59
  callerOptions: {
51
60
  fetch: F,
52
61
  maxRetries: 1,
@@ -57,11 +66,19 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
57
66
  showGraph,
58
67
  }
59
68
  );
60
- }, [defaultAgent, apiUrl, defaultHeaders, withCredentials, showHistory, showGraph]);
69
+ }, [defaultAgent, apiUrl, stableHeaders, withCredentials, showHistory, showGraph]);
61
70
 
62
71
  const unionStore = useUnionStore(store, useStore);
63
72
 
73
+ // 使用 ref 标记是否已初始化
74
+ const initializedRef = useRef(false);
75
+
64
76
  useEffect(() => {
77
+ if (initializedRef.current) {
78
+ return;
79
+ }
80
+ initializedRef.current = true;
81
+
65
82
  unionStore
66
83
  .initClient()
67
84
  .then((res) => {
@@ -71,11 +88,11 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
71
88
  })
72
89
  .catch((err) => {
73
90
  console.error(err);
74
- if (onInitError) {
75
- onInitError(err, unionStore.currentAgent);
91
+ if (onInitErrorRef.current) {
92
+ onInitErrorRef.current(err, unionStore.currentAgent);
76
93
  }
77
94
  });
78
- }, [unionStore, onInitError]);
95
+ }, [unionStore]);
79
96
 
80
97
  return createElement(ChatContext.Provider, { value: unionStore }, children);
81
98
  };
@@ -1,4 +1,4 @@
1
- import { defineComponent, inject, provide, onMounted, h, type InjectionKey, type PropType, Ref } from "vue";
1
+ import { defineComponent, inject, provide, onMounted, defineExpose, type InjectionKey, type PropType, Ref } from "vue";
2
2
  import { createChatStore } from "../ui-store/index.js";
3
3
  import { useStore } from "@nanostores/vue";
4
4
  import { PreinitializedWritableAtom, StoreValue } from "nanostores";