@langgraph-js/sdk 3.1.0 → 3.2.1

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.
@@ -169,7 +169,9 @@ export declare class LangGraphClient<TStateType = unknown> extends EventEmitter<
169
169
  * @zh 初始化 Assistant。
170
170
  * @en Initializes the Assistant.
171
171
  */
172
- initAssistant(agentName?: string): Promise<void>;
172
+ initAssistant(agentName?: string, config?: {
173
+ fallbackToAvailableAssistants?: boolean;
174
+ }): Promise<Assistant | undefined>;
173
175
  /**
174
176
  * @zh 创建一个新的 Thread。
175
177
  * @en Creates a new Thread.
@@ -46,7 +46,7 @@ export class LangGraphClient extends EventEmitter {
46
46
  * @zh 初始化 Assistant。
47
47
  * @en Initializes the Assistant.
48
48
  */
49
- async initAssistant(agentName) {
49
+ async initAssistant(agentName, config = {}) {
50
50
  try {
51
51
  const assistants = await this.listAssistants();
52
52
  this.availableAssistants = assistants;
@@ -54,11 +54,16 @@ export class LangGraphClient extends EventEmitter {
54
54
  if (agentName) {
55
55
  this.currentAssistant = assistants.find((assistant) => assistant.graph_id === agentName) || null;
56
56
  if (!this.currentAssistant) {
57
+ if (config.fallbackToAvailableAssistants) {
58
+ this.currentAssistant = this.availableAssistants[0];
59
+ return this.currentAssistant;
60
+ }
57
61
  throw new Error("Agent not found: " + agentName);
58
62
  }
59
63
  }
60
64
  else {
61
65
  this.currentAssistant = assistants[0];
66
+ return this.currentAssistant;
62
67
  }
63
68
  }
64
69
  else {
@@ -61,6 +61,7 @@ interface ChatProviderProps {
61
61
  withCredentials?: boolean;
62
62
  showHistory?: boolean;
63
63
  showGraph?: boolean;
64
+ fallbackToAvailableAssistants?: boolean;
64
65
  onInitError?: (error: any, currentAgent: string) => void;
65
66
  }
66
67
  export declare const ChatProvider: React.FC<ChatProviderProps>;
@@ -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, onInitError, }) => {
12
+ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://localhost:8123", defaultHeaders, withCredentials = false, showHistory = false, showGraph = false, fallbackToAvailableAssistants = false, onInitError, }) => {
13
13
  // 使用 useMemo 稳定 defaultHeaders 的引用
14
14
  const stableHeaders = useMemo(() => defaultHeaders || {}, [defaultHeaders]);
15
15
  // 使用 useRef 保存 onInitError 的最新引用
@@ -34,8 +34,9 @@ export const ChatProvider = ({ children, defaultAgent = "", apiUrl = "http://loc
34
34
  }, {
35
35
  showHistory,
36
36
  showGraph,
37
+ fallbackToAvailableAssistants,
37
38
  });
38
- }, [defaultAgent, apiUrl, stableHeaders, withCredentials, showHistory, showGraph]);
39
+ }, [defaultAgent, apiUrl, stableHeaders, withCredentials, showHistory, showGraph, fallbackToAvailableAssistants]);
39
40
  const unionStore = useUnionStore(store, useStore);
40
41
  // 使用 ref 标记是否已初始化
41
42
  const initializedRef = useRef(false);
@@ -28,6 +28,7 @@ export declare const getHistoryContent: (thread: Thread) => string | any[];
28
28
  export declare const createChatStore: (initClientName: string, config: Partial<LangGraphClientConfig>, context?: {
29
29
  showHistory?: boolean;
30
30
  showGraph?: boolean;
31
+ fallbackToAvailableAssistants?: boolean;
31
32
  onInit?: (client: LangGraphClient) => void;
32
33
  }) => {
33
34
  data: {
@@ -104,12 +104,12 @@ export const createChatStore = (initClientName, config, context = {}) => {
104
104
  * @en Initializes the LangGraph client.
105
105
  */
106
106
  async function initClient() {
107
- var _a, _b;
107
+ var _a, _b, _c;
108
108
  const newClient = new LangGraphClient({
109
109
  ...config,
110
110
  client: (_a = config.client) !== null && _a !== void 0 ? _a : (await createLangGraphServerClient(config)),
111
111
  });
112
- await newClient.initAssistant(currentAgent.get());
112
+ await newClient.initAssistant(currentAgent.get(), { fallbackToAvailableAssistants: (_b = context.fallbackToAvailableAssistants) !== null && _b !== void 0 ? _b : false });
113
113
  currentAgent.set(newClient.getCurrentAssistant().graph_id);
114
114
  // 不再需要创建,sendMessage 会自动创建
115
115
  // await newClient.createThread();
@@ -147,7 +147,7 @@ export const createChatStore = (initClientName, config, context = {}) => {
147
147
  currentChatId.set(((_a = newClient.getCurrentThread()) === null || _a === void 0 ? void 0 : _a.thread_id) || null);
148
148
  updateUI(newClient);
149
149
  });
150
- (_b = context.onInit) === null || _b === void 0 ? void 0 : _b.call(context, newClient);
150
+ (_c = context.onInit) === null || _c === void 0 ? void 0 : _c.call(context, newClient);
151
151
  newClient.graphState = {};
152
152
  client.set(newClient);
153
153
  if (showGraph.get())
@@ -31,6 +31,7 @@ export interface ChatProviderProps {
31
31
  withCredentials?: boolean;
32
32
  showHistory?: boolean;
33
33
  showGraph?: boolean;
34
+ fallbackToAvailableAssistants?: boolean;
34
35
  onInitError?: (error: any, currentAgent: string) => void;
35
36
  }
36
37
  /**
@@ -48,6 +48,7 @@ export const useChatProvider = (props) => {
48
48
  }, {
49
49
  showHistory: props.showHistory,
50
50
  showGraph: props.showGraph,
51
+ fallbackToAvailableAssistants: props.fallbackToAvailableAssistants,
51
52
  });
52
53
  const unionStore = useUnionStoreVue(store, useStore);
53
54
  // 提供 store 给子组件
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langgraph-js/sdk",
3
- "version": "3.1.0",
3
+ "version": "3.2.1",
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",
@@ -44,8 +44,7 @@
44
44
  "url": "https://github.com/KonghaYao/YaoAgent/issues"
45
45
  },
46
46
  "dependencies": {
47
- "@langchain/langgraph-sdk": "^0.1.10",
48
- "@langgraph-js/pure-graph": "^1.5.2",
47
+ "@langchain/langgraph-sdk": "^1.0.0",
49
48
  "eventemitter3": "^5.0.1",
50
49
  "jsonrepair": "^3.12.0",
51
50
  "nanostores": "^1.0.1",
@@ -74,6 +73,7 @@
74
73
  }
75
74
  },
76
75
  "devDependencies": {
76
+ "@langgraph-js/pure-graph": "^2.0.0",
77
77
  "@types/react": "^19.1.6",
78
78
  "vue": "^3.5.14"
79
79
  },
@@ -137,7 +137,12 @@ export class LangGraphClient<TStateType = unknown> extends EventEmitter<LangGrap
137
137
  * @zh 初始化 Assistant。
138
138
  * @en Initializes the Assistant.
139
139
  */
140
- async initAssistant(agentName?: string) {
140
+ async initAssistant(
141
+ agentName?: string,
142
+ config: {
143
+ fallbackToAvailableAssistants?: boolean;
144
+ } = {}
145
+ ) {
141
146
  try {
142
147
  const assistants = await this.listAssistants();
143
148
  this.availableAssistants = assistants;
@@ -145,10 +150,15 @@ export class LangGraphClient<TStateType = unknown> extends EventEmitter<LangGrap
145
150
  if (agentName) {
146
151
  this.currentAssistant = assistants.find((assistant: any) => assistant.graph_id === agentName) || null;
147
152
  if (!this.currentAssistant) {
153
+ if (config.fallbackToAvailableAssistants) {
154
+ this.currentAssistant = this.availableAssistants[0];
155
+ return this.currentAssistant;
156
+ }
148
157
  throw new Error("Agent not found: " + agentName);
149
158
  }
150
159
  } else {
151
160
  this.currentAssistant = assistants[0];
161
+ return this.currentAssistant;
152
162
  }
153
163
  } else {
154
164
  throw new Error("No assistants found");
@@ -21,6 +21,7 @@ interface ChatProviderProps {
21
21
  withCredentials?: boolean;
22
22
  showHistory?: boolean;
23
23
  showGraph?: boolean;
24
+ fallbackToAvailableAssistants?: boolean;
24
25
  onInitError?: (error: any, currentAgent: string) => void;
25
26
  }
26
27
 
@@ -32,6 +33,7 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
32
33
  withCredentials = false,
33
34
  showHistory = false,
34
35
  showGraph = false,
36
+ fallbackToAvailableAssistants = false,
35
37
  onInitError,
36
38
  }) => {
37
39
  // 使用 useMemo 稳定 defaultHeaders 的引用
@@ -64,9 +66,10 @@ export const ChatProvider: React.FC<ChatProviderProps> = ({
64
66
  {
65
67
  showHistory,
66
68
  showGraph,
69
+ fallbackToAvailableAssistants,
67
70
  }
68
71
  );
69
- }, [defaultAgent, apiUrl, stableHeaders, withCredentials, showHistory, showGraph]);
72
+ }, [defaultAgent, apiUrl, stableHeaders, withCredentials, showHistory, showGraph, fallbackToAvailableAssistants]);
70
73
 
71
74
  const unionStore = useUnionStore(store, useStore);
72
75
 
@@ -71,6 +71,7 @@ export const createChatStore = (
71
71
  context: {
72
72
  showHistory?: boolean;
73
73
  showGraph?: boolean;
74
+ fallbackToAvailableAssistants?: boolean;
74
75
  onInit?: (client: LangGraphClient) => void;
75
76
  } = {}
76
77
  ) => {
@@ -116,7 +117,7 @@ export const createChatStore = (
116
117
  ...config,
117
118
  client: config.client ?? (await createLangGraphServerClient(config as LangGraphClientConfig)),
118
119
  });
119
- await newClient.initAssistant(currentAgent.get());
120
+ await newClient.initAssistant(currentAgent.get(), { fallbackToAvailableAssistants: context.fallbackToAvailableAssistants ?? false });
120
121
  currentAgent.set(newClient.getCurrentAssistant()!.graph_id);
121
122
  // 不再需要创建,sendMessage 会自动创建
122
123
  // await newClient.createThread();
@@ -53,6 +53,7 @@ export interface ChatProviderProps {
53
53
  withCredentials?: boolean;
54
54
  showHistory?: boolean;
55
55
  showGraph?: boolean;
56
+ fallbackToAvailableAssistants?: boolean;
56
57
  onInitError?: (error: any, currentAgent: string) => void;
57
58
  }
58
59
 
@@ -81,6 +82,7 @@ export const useChatProvider = (props: ChatProviderProps) => {
81
82
  {
82
83
  showHistory: props.showHistory,
83
84
  showGraph: props.showGraph,
85
+ fallbackToAvailableAssistants: props.fallbackToAvailableAssistants,
84
86
  }
85
87
  );
86
88
 
package/vitest.confit.ts DELETED
@@ -1,3 +0,0 @@
1
- export default {
2
- test: {},
3
- };