@langgraph-js/sdk 4.3.4 → 4.3.6

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/History.d.ts CHANGED
@@ -43,7 +43,7 @@ export declare class History {
43
43
  * @zh 激活指定会话(懒加载创建 Client)
44
44
  * @en Activates the specified session (lazy load client)
45
45
  */
46
- activateSession(sessionId: string): Promise<SessionInfo>;
46
+ activateSession(sessionId: string, mustResetStream?: boolean): Promise<SessionInfo>;
47
47
  /**
48
48
  * @zh 获取当前活跃的会话
49
49
  * @en Gets the current active session
package/dist/History.js CHANGED
@@ -45,18 +45,18 @@ export class History {
45
45
  * @zh 激活指定会话(懒加载创建 Client)
46
46
  * @en Activates the specified session (lazy load client)
47
47
  */
48
- async activateSession(sessionId) {
49
- const session = this.sessions.get(sessionId);
50
- if (!session) {
51
- throw new Error(`Session ${sessionId} not found`);
52
- }
48
+ async activateSession(sessionId, mustResetStream = false) {
49
+ const session = this.sessions.get(sessionId) || {
50
+ sessionId,
51
+ agentName: this.virtualClient.getCurrentAssistant()?.graph_id,
52
+ };
53
53
  // 懒加载:只在激活时创建 Client
54
54
  if (!session.client) {
55
55
  const client = new LangGraphClient(this.clientConfig);
56
56
  await client.initAssistant(session.agentName);
57
57
  // 只有在有 thread 的情况下才重置(恢复已有会话)
58
58
  // 新会话的 thread 会在发送第一条消息时自动创建
59
- if (session.thread) {
59
+ if (session.thread || mustResetStream) {
60
60
  await client.resetThread(session.agentName, sessionId);
61
61
  }
62
62
  session.client = client;
@@ -390,7 +390,7 @@ export class LangGraphClient extends EventEmitter {
390
390
  additional_kwargs: {},
391
391
  };
392
392
  // json 校验
393
- return this.callFETool(toolMessage, tool.args);
393
+ return this.callFETool(toolMessage, tool.args).catch((e) => console.warn(e));
394
394
  });
395
395
  console.log("batch call tools", result.length);
396
396
  // 只有当卡住流程时,才改变状态为 interrupted
@@ -34,7 +34,7 @@ export declare const useChat: () => UnionStore<{
34
34
  initClient: () => Promise<import("../History.js").History>;
35
35
  getClient: () => import("../LangGraphClient.js").LangGraphClient<unknown> | null;
36
36
  getHistory: () => import("../History.js").History | null;
37
- activateSession: (sessionId: string) => Promise<void>;
37
+ activateSession: (sessionId: string, mustResetStream?: boolean) => Promise<void>;
38
38
  createNewSession: () => Promise<void>;
39
39
  refreshSessionList: () => Promise<void>;
40
40
  refreshHistoryList: () => Promise<void>;
@@ -70,6 +70,7 @@ interface ChatProviderProps {
70
70
  apiUrl?: string;
71
71
  defaultHeaders?: Record<string, string>;
72
72
  withCredentials?: boolean;
73
+ fetch?: typeof fetch;
73
74
  showHistory?: boolean;
74
75
  showGraph?: boolean;
75
76
  fallbackToAvailableAssistants?: boolean;
@@ -9,7 +9,7 @@ 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, fallbackToAvailableAssistants = false, autoRestoreLastSession = false, onInitError, client, legacyMode = false, }) => {
12
+ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://localhost:8123", defaultHeaders, withCredentials = false, fetch, showHistory = false, showGraph = false, fallbackToAvailableAssistants = false, autoRestoreLastSession = false, onInitError, client, legacyMode = false, }) => {
13
13
  // 使用 useMemo 稳定 defaultHeaders 的引用
14
14
  const stableHeaders = useMemo(() => defaultHeaders || {}, [defaultHeaders]);
15
15
  // 使用 useRef 保存 onInitError 的最新引用
@@ -18,12 +18,13 @@ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://loc
18
18
  onInitErrorRef.current = onInitError;
19
19
  }, [onInitError]);
20
20
  const store = useMemo(() => {
21
+ const baseFetch = fetch || globalThis.fetch;
21
22
  const F = withCredentials
22
23
  ? (url, options) => {
23
24
  options.credentials = "include";
24
- return fetch(url, options);
25
+ return baseFetch(url, options);
25
26
  }
26
- : fetch;
27
+ : baseFetch;
27
28
  const config = {
28
29
  apiUrl,
29
30
  defaultHeaders: stableHeaders,
@@ -42,7 +43,7 @@ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://loc
42
43
  fallbackToAvailableAssistants,
43
44
  autoRestoreLastSession,
44
45
  });
45
- }, [defaultAgent, apiUrl, stableHeaders, withCredentials, showHistory, showGraph, fallbackToAvailableAssistants, autoRestoreLastSession]);
46
+ }, [defaultAgent, apiUrl, stableHeaders, withCredentials, fetch, showHistory, showGraph, fallbackToAvailableAssistants, autoRestoreLastSession]);
46
47
  const unionStore = useUnionStore(store, useStore);
47
48
  // 使用 ref 标记是否已初始化
48
49
  const initializedRef = useRef(false);
@@ -34,7 +34,7 @@ export declare const useChat: () => UnionStoreSolid<{
34
34
  initClient: () => Promise<import("../History.js").History>;
35
35
  getClient: () => import("../LangGraphClient.js").LangGraphClient<unknown> | null;
36
36
  getHistory: () => import("../History.js").History | null;
37
- activateSession: (sessionId: string) => Promise<void>;
37
+ activateSession: (sessionId: string, mustResetStream?: boolean) => Promise<void>;
38
38
  createNewSession: () => Promise<void>;
39
39
  refreshSessionList: () => Promise<void>;
40
40
  refreshHistoryList: () => Promise<void>;
@@ -70,6 +70,7 @@ interface ChatProviderProps {
70
70
  apiUrl?: string;
71
71
  defaultHeaders?: Record<string, string>;
72
72
  withCredentials?: boolean;
73
+ fetch?: typeof fetch;
73
74
  showHistory?: boolean;
74
75
  showGraph?: boolean;
75
76
  fallbackToAvailableAssistants?: boolean;
@@ -26,12 +26,15 @@ export const ChatProvider = (props) => {
26
26
  // 使用 createMemo 稳定 defaultHeaders 的引用
27
27
  const stableHeaders = createMemo(() => props.defaultHeaders || {});
28
28
  // 使用 createMemo 创建 fetch 函数
29
- const F = createMemo(() => props.withCredentials
30
- ? (url, options) => {
31
- options.credentials = "include";
32
- return fetch(url, options);
33
- }
34
- : fetch);
29
+ const F = createMemo(() => {
30
+ const baseFetch = props.fetch || globalThis.fetch;
31
+ return props.withCredentials
32
+ ? (url, options) => {
33
+ options.credentials = "include";
34
+ return baseFetch(url, options);
35
+ }
36
+ : baseFetch;
37
+ });
35
38
  const store = createMemo(() => {
36
39
  const config = {
37
40
  apiUrl: props.apiUrl || "http://localhost:8123",
@@ -50,7 +50,7 @@ export declare const createChatStore: (initClientName: string, config: Partial<L
50
50
  initClient: () => Promise<History>;
51
51
  getClient: () => LangGraphClient<unknown> | null;
52
52
  getHistory: () => History | null;
53
- activateSession: (sessionId: string) => Promise<void>;
53
+ activateSession: (sessionId: string, mustResetStream?: boolean) => Promise<void>;
54
54
  createNewSession: () => Promise<void>;
55
55
  refreshSessionList: () => Promise<void>;
56
56
  refreshHistoryList: () => Promise<void>;
@@ -234,7 +234,7 @@ export const createChatStore = (initClientName, config, context = {}) => {
234
234
  };
235
235
  }
236
236
  // ============ 会话激活逻辑 ============
237
- async function activateSession(sessionId) {
237
+ async function activateSession(sessionId, mustResetStream = false) {
238
238
  const historyManager = history.get();
239
239
  if (!historyManager)
240
240
  return;
@@ -246,7 +246,7 @@ export const createChatStore = (initClientName, config, context = {}) => {
246
246
  inChatError.set(null);
247
247
  interruptData.set(null);
248
248
  isInterrupted.set(false);
249
- const session = await historyManager.activateSession(sessionId);
249
+ const session = await historyManager.activateSession(sessionId, mustResetStream);
250
250
  const activeClient = session.client;
251
251
  if (activeClient) {
252
252
  cleanupCurrentClient = setupClientListeners(activeClient);
@@ -416,7 +416,7 @@ export const createChatStore = (initClientName, config, context = {}) => {
416
416
  // 历史记录(兼容旧 API)
417
417
  addToHistory,
418
418
  createNewChat: createNewSession,
419
- toHistoryChat: (thread) => activateSession(thread.thread_id),
419
+ toHistoryChat: (thread) => activateSession(thread.thread_id, true),
420
420
  async deleteHistoryChat(thread) {
421
421
  const historyManager = history.get();
422
422
  if (historyManager) {
@@ -30,6 +30,7 @@ export interface ChatProviderProps {
30
30
  apiUrl?: string;
31
31
  defaultHeaders?: Record<string, string>;
32
32
  withCredentials?: boolean;
33
+ fetch?: typeof fetch;
33
34
  showHistory?: boolean;
34
35
  showGraph?: boolean;
35
36
  fallbackToAvailableAssistants?: boolean;
@@ -77,7 +78,7 @@ export declare const useChatProvider: (props: ChatProviderProps) => {
77
78
  initClient: () => Promise<import("../History.js").History>;
78
79
  getClient: () => import("../LangGraphClient.js").LangGraphClient<unknown> | null;
79
80
  getHistory: () => import("../History.js").History | null;
80
- activateSession: (sessionId: string) => Promise<void>;
81
+ activateSession: (sessionId: string, mustResetStream?: boolean) => Promise<void>;
81
82
  createNewSession: () => Promise<void>;
82
83
  refreshSessionList: () => Promise<void>;
83
84
  refreshHistoryList: () => Promise<void>;
@@ -129,6 +130,10 @@ export declare const ChatProvider: import("vue").DefineComponent<import("vue").E
129
130
  type: PropType<boolean>;
130
131
  default: boolean;
131
132
  };
133
+ fetch: {
134
+ type: PropType<typeof fetch>;
135
+ default: undefined;
136
+ };
132
137
  showHistory: {
133
138
  type: PropType<boolean>;
134
139
  default: boolean;
@@ -164,6 +169,10 @@ export declare const ChatProvider: import("vue").DefineComponent<import("vue").E
164
169
  type: PropType<boolean>;
165
170
  default: boolean;
166
171
  };
172
+ fetch: {
173
+ type: PropType<typeof fetch>;
174
+ default: undefined;
175
+ };
167
176
  showHistory: {
168
177
  type: PropType<boolean>;
169
178
  default: boolean;
@@ -187,6 +196,7 @@ export declare const ChatProvider: import("vue").DefineComponent<import("vue").E
187
196
  showHistory: boolean;
188
197
  defaultAgent: string;
189
198
  withCredentials: boolean;
199
+ fetch: typeof fetch;
190
200
  autoRestoreLastSession: boolean;
191
201
  onInitError: (error: any, currentAgent: string) => void;
192
202
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
@@ -32,12 +32,13 @@ export const useChat = () => {
32
32
  * @en Chat Provider Hook, used directly in setup
33
33
  */
34
34
  export const useChatProvider = (props) => {
35
+ const baseFetch = props.fetch || globalThis.fetch;
35
36
  const F = props.withCredentials
36
37
  ? (url, options) => {
37
38
  options.credentials = "include";
38
- return fetch(url, options);
39
+ return baseFetch(url, options);
39
40
  }
40
- : fetch;
41
+ : baseFetch;
41
42
  const store = createChatStore(props.defaultAgent || "", {
42
43
  apiUrl: props.apiUrl,
43
44
  defaultHeaders: props.defaultHeaders,
@@ -92,6 +93,10 @@ export const ChatProvider = defineComponent({
92
93
  type: Boolean,
93
94
  default: false,
94
95
  },
96
+ fetch: {
97
+ type: Function,
98
+ default: undefined,
99
+ },
95
100
  showHistory: {
96
101
  type: Boolean,
97
102
  default: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langgraph-js/sdk",
3
- "version": "4.3.4",
3
+ "version": "4.3.6",
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",
package/src/History.ts CHANGED
@@ -78,11 +78,11 @@ export class History {
78
78
  * @zh 激活指定会话(懒加载创建 Client)
79
79
  * @en Activates the specified session (lazy load client)
80
80
  */
81
- async activateSession(sessionId: string): Promise<SessionInfo> {
82
- const session = this.sessions.get(sessionId);
83
- if (!session) {
84
- throw new Error(`Session ${sessionId} not found`);
85
- }
81
+ async activateSession(sessionId: string, mustResetStream = false): Promise<SessionInfo> {
82
+ const session: SessionInfo = this.sessions.get(sessionId) || {
83
+ sessionId,
84
+ agentName: this.virtualClient.getCurrentAssistant()?.graph_id!,
85
+ };
86
86
 
87
87
  // 懒加载:只在激活时创建 Client
88
88
  if (!session.client) {
@@ -91,7 +91,7 @@ export class History {
91
91
 
92
92
  // 只有在有 thread 的情况下才重置(恢复已有会话)
93
93
  // 新会话的 thread 会在发送第一条消息时自动创建
94
- if (session.thread) {
94
+ if (session.thread || mustResetStream) {
95
95
  await client.resetThread(session.agentName, sessionId);
96
96
  }
97
97
 
@@ -510,7 +510,7 @@ export class LangGraphClient<TStateType = unknown> extends EventEmitter<LangGrap
510
510
  additional_kwargs: {},
511
511
  };
512
512
  // json 校验
513
- return this.callFETool(toolMessage, tool.args);
513
+ return this.callFETool(toolMessage, tool.args).catch((e) => console.warn(e));
514
514
  });
515
515
  console.log("batch call tools", result.length);
516
516
  // 只有当卡住流程时,才改变状态为 interrupted
@@ -20,6 +20,7 @@ interface ChatProviderProps {
20
20
  apiUrl?: string;
21
21
  defaultHeaders?: Record<string, string>;
22
22
  withCredentials?: boolean;
23
+ fetch?: typeof fetch;
23
24
  showHistory?: boolean;
24
25
  showGraph?: boolean;
25
26
  fallbackToAvailableAssistants?: boolean;
@@ -36,6 +37,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
36
37
  apiUrl = "http://localhost:8123",
37
38
  defaultHeaders,
38
39
  withCredentials = false,
40
+ fetch,
39
41
  showHistory = false,
40
42
  showGraph = false,
41
43
  fallbackToAvailableAssistants = false,
@@ -54,12 +56,13 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
54
56
  }, [onInitError]);
55
57
 
56
58
  const store = useMemo(() => {
59
+ const baseFetch = fetch || globalThis.fetch;
57
60
  const F = withCredentials
58
61
  ? (url: string, options: RequestInit) => {
59
62
  options.credentials = "include";
60
- return fetch(url, options);
63
+ return baseFetch(url, options);
61
64
  }
62
- : fetch;
65
+ : baseFetch;
63
66
 
64
67
  const config = {
65
68
  apiUrl,
@@ -78,7 +81,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
78
81
  fallbackToAvailableAssistants,
79
82
  autoRestoreLastSession,
80
83
  });
81
- }, [defaultAgent, apiUrl, stableHeaders, withCredentials, showHistory, showGraph, fallbackToAvailableAssistants, autoRestoreLastSession]);
84
+ }, [defaultAgent, apiUrl, stableHeaders, withCredentials, fetch, showHistory, showGraph, fallbackToAvailableAssistants, autoRestoreLastSession]);
82
85
 
83
86
  const unionStore = useUnionStore(store, useStore);
84
87
 
@@ -20,6 +20,7 @@ interface ChatProviderProps {
20
20
  apiUrl?: string;
21
21
  defaultHeaders?: Record<string, string>;
22
22
  withCredentials?: boolean;
23
+ fetch?: typeof fetch;
23
24
  showHistory?: boolean;
24
25
  showGraph?: boolean;
25
26
  fallbackToAvailableAssistants?: boolean;
@@ -62,14 +63,15 @@ export const ChatProvider = (props: ChatProviderProps) => {
62
63
  const stableHeaders = createMemo(() => props.defaultHeaders || {});
63
64
 
64
65
  // 使用 createMemo 创建 fetch 函数
65
- const F = createMemo(() =>
66
- props.withCredentials
66
+ const F = createMemo(() => {
67
+ const baseFetch = props.fetch || globalThis.fetch;
68
+ return props.withCredentials
67
69
  ? (url: string, options: RequestInit) => {
68
70
  options.credentials = "include";
69
- return fetch(url, options);
71
+ return baseFetch(url, options);
70
72
  }
71
- : fetch
72
- );
73
+ : baseFetch;
74
+ });
73
75
 
74
76
  const store = createMemo(() => {
75
77
  const config = {
@@ -281,7 +281,7 @@ export const createChatStore = (initClientName: string, config: Partial<LangGrap
281
281
 
282
282
  // ============ 会话激活逻辑 ============
283
283
 
284
- async function activateSession(sessionId: string) {
284
+ async function activateSession(sessionId: string, mustResetStream = false) {
285
285
  const historyManager = history.get();
286
286
  if (!historyManager) return;
287
287
 
@@ -295,7 +295,7 @@ export const createChatStore = (initClientName: string, config: Partial<LangGrap
295
295
  interruptData.set(null);
296
296
  isInterrupted.set(false);
297
297
 
298
- const session = await historyManager.activateSession(sessionId);
298
+ const session = await historyManager.activateSession(sessionId, mustResetStream);
299
299
  const activeClient = session.client;
300
300
 
301
301
  if (activeClient) {
@@ -488,7 +488,7 @@ export const createChatStore = (initClientName: string, config: Partial<LangGrap
488
488
  // 历史记录(兼容旧 API)
489
489
  addToHistory,
490
490
  createNewChat: createNewSession,
491
- toHistoryChat: (thread: Thread<{ messages: Message[] }>) => activateSession(thread.thread_id),
491
+ toHistoryChat: (thread: Thread<{ messages: Message[] }>) => activateSession(thread.thread_id, true),
492
492
  async deleteHistoryChat(thread: Thread<{ messages: Message[] }>) {
493
493
  const historyManager = history.get();
494
494
  if (historyManager) {
@@ -52,6 +52,7 @@ export interface ChatProviderProps {
52
52
  apiUrl?: string;
53
53
  defaultHeaders?: Record<string, string>;
54
54
  withCredentials?: boolean;
55
+ fetch?: typeof fetch;
55
56
  showHistory?: boolean;
56
57
  showGraph?: boolean;
57
58
  fallbackToAvailableAssistants?: boolean;
@@ -67,12 +68,13 @@ export interface ChatProviderProps {
67
68
  * @en Chat Provider Hook, used directly in setup
68
69
  */
69
70
  export const useChatProvider = (props: ChatProviderProps) => {
71
+ const baseFetch = props.fetch || globalThis.fetch;
70
72
  const F = props.withCredentials
71
73
  ? (url: string, options: RequestInit) => {
72
74
  options.credentials = "include";
73
- return fetch(url, options);
75
+ return baseFetch(url, options);
74
76
  }
75
- : fetch;
77
+ : baseFetch;
76
78
 
77
79
  const store = createChatStore(
78
80
  props.defaultAgent || "",
@@ -137,6 +139,10 @@ export const ChatProvider = defineComponent({
137
139
  type: Boolean as PropType<boolean>,
138
140
  default: false,
139
141
  },
142
+ fetch: {
143
+ type: Function as PropType<typeof fetch>,
144
+ default: undefined,
145
+ },
140
146
  showHistory: {
141
147
  type: Boolean as PropType<boolean>,
142
148
  default: false,