@langgraph-js/sdk 3.2.0 → 3.3.0
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/LangGraphClient.d.ts +3 -1
- package/dist/LangGraphClient.js +7 -2
- package/dist/react/ChatContext.d.ts +1 -0
- package/dist/react/ChatContext.js +3 -2
- package/dist/tool/createTool.d.ts +92 -0
- package/dist/ui-store/createChatStore.d.ts +1 -0
- package/dist/ui-store/createChatStore.js +3 -3
- package/dist/vue/ChatContext.d.ts +1 -0
- package/dist/vue/ChatContext.js +1 -0
- package/package.json +1 -1
- package/src/LangGraphClient.ts +12 -2
- package/src/react/ChatContext.ts +4 -1
- package/src/ui-store/createChatStore.ts +2 -1
- package/src/vue/ChatContext.ts +2 -0
|
@@ -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
|
|
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.
|
package/dist/LangGraphClient.js
CHANGED
|
@@ -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 {
|
|
@@ -240,7 +245,7 @@ export class LangGraphClient extends EventEmitter {
|
|
|
240
245
|
messages: messagesToSend,
|
|
241
246
|
fe_tools: await this.tools.toJSON(this.currentAssistant.graph_id),
|
|
242
247
|
},
|
|
243
|
-
streamMode: ["messages", "values"],
|
|
248
|
+
streamMode: ["messages", "values", "updates"],
|
|
244
249
|
streamSubgraphs: true,
|
|
245
250
|
command,
|
|
246
251
|
});
|
|
@@ -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);
|
|
@@ -46,6 +46,98 @@ export declare const createJSONDefineTool: <Args extends ZodRawShape>(tool: Unio
|
|
|
46
46
|
name: string;
|
|
47
47
|
description: string;
|
|
48
48
|
parameters: Args | ({
|
|
49
|
+
anyOf: import("zod-to-json-schema").JsonSchema7DateType[];
|
|
50
|
+
} & {
|
|
51
|
+
title?: string;
|
|
52
|
+
default?: any;
|
|
53
|
+
description?: string;
|
|
54
|
+
markdownDescription?: string;
|
|
55
|
+
} & {
|
|
56
|
+
$schema?: string | undefined;
|
|
57
|
+
definitions?: {
|
|
58
|
+
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
59
|
+
} | undefined;
|
|
60
|
+
}) | ({
|
|
61
|
+
type: "object" | "array";
|
|
62
|
+
} & {
|
|
63
|
+
title?: string;
|
|
64
|
+
default?: any;
|
|
65
|
+
description?: string;
|
|
66
|
+
markdownDescription?: string;
|
|
67
|
+
} & {
|
|
68
|
+
$schema?: string | undefined;
|
|
69
|
+
definitions?: {
|
|
70
|
+
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
71
|
+
} | undefined;
|
|
72
|
+
}) | ({
|
|
73
|
+
type: ("string" | "number" | "boolean" | "integer" | "null") | ("string" | "number" | "boolean" | "integer" | "null")[];
|
|
74
|
+
} & {
|
|
75
|
+
title?: string;
|
|
76
|
+
default?: any;
|
|
77
|
+
description?: string;
|
|
78
|
+
markdownDescription?: string;
|
|
79
|
+
} & {
|
|
80
|
+
$schema?: string | undefined;
|
|
81
|
+
definitions?: {
|
|
82
|
+
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
83
|
+
} | undefined;
|
|
84
|
+
}) | ({
|
|
85
|
+
anyOf: import("zod-to-json-schema").JsonSchema7Type[];
|
|
86
|
+
} & {
|
|
87
|
+
title?: string;
|
|
88
|
+
default?: any;
|
|
89
|
+
description?: string;
|
|
90
|
+
markdownDescription?: string;
|
|
91
|
+
} & {
|
|
92
|
+
$schema?: string | undefined;
|
|
93
|
+
definitions?: {
|
|
94
|
+
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
95
|
+
} | undefined;
|
|
96
|
+
}) | (import("zod-to-json-schema").JsonSchema7UndefinedType & {
|
|
97
|
+
title?: string;
|
|
98
|
+
default?: any;
|
|
99
|
+
description?: string;
|
|
100
|
+
markdownDescription?: string;
|
|
101
|
+
} & {
|
|
102
|
+
$schema?: string | undefined;
|
|
103
|
+
definitions?: {
|
|
104
|
+
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
105
|
+
} | undefined;
|
|
106
|
+
}) | (import("zod-to-json-schema").JsonSchema7AnyType & {
|
|
107
|
+
title?: string;
|
|
108
|
+
default?: any;
|
|
109
|
+
description?: string;
|
|
110
|
+
markdownDescription?: string;
|
|
111
|
+
} & {
|
|
112
|
+
$schema?: string | undefined;
|
|
113
|
+
definitions?: {
|
|
114
|
+
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
115
|
+
} | undefined;
|
|
116
|
+
}) | ({
|
|
117
|
+
anyOf: [import("zod-to-json-schema").JsonSchema7Type, import("zod-to-json-schema").JsonSchema7NullType];
|
|
118
|
+
} & {
|
|
119
|
+
title?: string;
|
|
120
|
+
default?: any;
|
|
121
|
+
description?: string;
|
|
122
|
+
markdownDescription?: string;
|
|
123
|
+
} & {
|
|
124
|
+
$schema?: string | undefined;
|
|
125
|
+
definitions?: {
|
|
126
|
+
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
127
|
+
} | undefined;
|
|
128
|
+
}) | ({
|
|
129
|
+
type: [string, "null"];
|
|
130
|
+
} & {
|
|
131
|
+
title?: string;
|
|
132
|
+
default?: any;
|
|
133
|
+
description?: string;
|
|
134
|
+
markdownDescription?: string;
|
|
135
|
+
} & {
|
|
136
|
+
$schema?: string | undefined;
|
|
137
|
+
definitions?: {
|
|
138
|
+
[key: string]: import("zod-to-json-schema").JsonSchema7Type;
|
|
139
|
+
} | undefined;
|
|
140
|
+
}) | (import("zod-to-json-schema").JsonSchema7AllOfType & {
|
|
49
141
|
title?: string;
|
|
50
142
|
default?: any;
|
|
51
143
|
description?: string;
|
|
@@ -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
|
-
(
|
|
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())
|
package/dist/vue/ChatContext.js
CHANGED
|
@@ -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
package/src/LangGraphClient.ts
CHANGED
|
@@ -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(
|
|
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");
|
|
@@ -336,7 +346,7 @@ export class LangGraphClient<TStateType = unknown> extends EventEmitter<LangGrap
|
|
|
336
346
|
messages: messagesToSend,
|
|
337
347
|
fe_tools: await this.tools.toJSON(this.currentAssistant!.graph_id),
|
|
338
348
|
},
|
|
339
|
-
streamMode: ["messages", "values"],
|
|
349
|
+
streamMode: ["messages", "values", "updates"],
|
|
340
350
|
streamSubgraphs: true,
|
|
341
351
|
command,
|
|
342
352
|
});
|
package/src/react/ChatContext.ts
CHANGED
|
@@ -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();
|
package/src/vue/ChatContext.ts
CHANGED
|
@@ -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
|
|