@langgraph-js/sdk 1.0.0 → 1.1.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.
@@ -0,0 +1,461 @@
1
+ import { Client, Thread, Message, Assistant, HumanMessage, AIMessage, ToolMessage, Command } from "@langchain/langgraph-sdk";
2
+ import { ToolManager } from "./ToolManager";
3
+ import { CallToolResult } from "./tool";
4
+ import { AsyncCallerParams } from "@langchain/langgraph-sdk/dist/utils/async_caller";
5
+
6
+ export type RenderMessage = Message & {
7
+ /** 工具入参 ,聚合而来*/
8
+ tool_input?: string;
9
+ additional_kwargs?: {
10
+ done?: boolean;
11
+ tool_calls?: {
12
+ function: {
13
+ arguments: string;
14
+ };
15
+ }[];
16
+ };
17
+ usage_metadata?: {
18
+ total_tokens: number;
19
+ input_tokens: number;
20
+ output_tokens: number;
21
+ };
22
+ response_metadata?: {
23
+ create_time: string;
24
+ };
25
+ /** 耗时 */
26
+ spend_time?: number;
27
+ /** 渲染时的唯一 id,聚合而来*/
28
+ unique_id?: string;
29
+ };
30
+
31
+ export interface LangGraphClientConfig {
32
+ apiUrl?: string;
33
+ apiKey?: string;
34
+ callerOptions?: AsyncCallerParams;
35
+ timeoutMs?: number;
36
+ defaultHeaders?: Record<string, string | null | undefined>;
37
+ }
38
+
39
+ export class StreamingMessageType {
40
+ static isUser(m: Message) {
41
+ return m.type === "human";
42
+ }
43
+ static isTool(m: Message) {
44
+ return m.type === "tool";
45
+ }
46
+ static isAssistant(m: Message) {
47
+ return m.type === "ai" && !this.isToolAssistant(m);
48
+ }
49
+ static isToolAssistant(m: Message) {
50
+ /** @ts-ignore */
51
+ return m.type === "ai" && (m.tool_calls?.length || m.tool_call_chunks?.length);
52
+ }
53
+ }
54
+
55
+ type StreamingUpdateEvent = {
56
+ type: "message" | "value" | "update" | "error" | "thread" | "done";
57
+ data: any;
58
+ };
59
+
60
+ type StreamingUpdateCallback = (event: StreamingUpdateEvent) => void;
61
+
62
+ export class LangGraphClient extends Client {
63
+ private currentAssistant: Assistant | null = null;
64
+ private currentThread: Thread | null = null;
65
+ private streamingCallbacks: Set<StreamingUpdateCallback> = new Set();
66
+ tools: ToolManager = new ToolManager();
67
+ stopController: AbortController | null = null;
68
+
69
+ constructor(config: LangGraphClientConfig) {
70
+ super(config);
71
+ }
72
+ availableAssistants: Assistant[] = [];
73
+ private listAssistants() {
74
+ return this.assistants.search({
75
+ metadata: null,
76
+ offset: 0,
77
+ limit: 100,
78
+ });
79
+ }
80
+ async initAssistant(agentName: string) {
81
+ try {
82
+ const assistants = await this.listAssistants();
83
+ this.availableAssistants = assistants;
84
+ if (assistants.length > 0) {
85
+ this.currentAssistant = assistants.find((assistant) => assistant.name === agentName) || null;
86
+ if (!this.currentAssistant) {
87
+ throw new Error("Agent not found");
88
+ }
89
+ } else {
90
+ throw new Error("No assistants found");
91
+ }
92
+ } catch (error) {
93
+ console.error("Failed to initialize LangGraphClient:", error);
94
+ throw error;
95
+ }
96
+ }
97
+
98
+ async createThread({
99
+ threadId,
100
+ }: {
101
+ threadId?: string;
102
+ } = {}) {
103
+ try {
104
+ this.currentThread = await this.threads.create({
105
+ threadId,
106
+ });
107
+ return this.currentThread;
108
+ } catch (error) {
109
+ console.error("Failed to create new thread:", error);
110
+ throw error;
111
+ }
112
+ }
113
+ async listThreads<T>() {
114
+ return this.threads.search<T>({
115
+ sortOrder: "desc",
116
+ });
117
+ }
118
+ /** 从历史中恢复数据 */
119
+ async resetThread(agent: string, threadId: string) {
120
+ await this.initAssistant(agent);
121
+ this.currentThread = await this.threads.get(threadId);
122
+ this.graphState = this.currentThread.values;
123
+ this.graphMessages = this.graphState.messages;
124
+ this.emitStreamingUpdate({
125
+ type: "value",
126
+ data: {
127
+ event: "messages/partial",
128
+ data: {
129
+ messages: this.graphMessages,
130
+ },
131
+ },
132
+ });
133
+ }
134
+
135
+ streamingMessage: RenderMessage[] = [];
136
+ /** 图发过来的更新信息 */
137
+ graphMessages: RenderMessage[] = [];
138
+ cloneMessage(message: Message): Message {
139
+ return JSON.parse(JSON.stringify(message));
140
+ }
141
+ private replaceMessageWithValuesMessage(message: AIMessage | ToolMessage, isTool = false): Message {
142
+ const key = (isTool ? "tool_call_id" : "id") as any as "id";
143
+ const valuesMessage = this.graphMessages.find((i) => i[key] === message[key]);
144
+ if (valuesMessage) {
145
+ return {
146
+ ...valuesMessage,
147
+ /** @ts-ignore */
148
+ tool_input: message.tool_input,
149
+ };
150
+ }
151
+ return message;
152
+ }
153
+
154
+ /** 用于 UI 中的流式渲染中的消息 */
155
+ get renderMessage() {
156
+ const previousMessage = new Map<string, Message>();
157
+ const result: Message[] = [];
158
+ const inputMessages = [...this.graphMessages, ...this.streamingMessage];
159
+
160
+ // 从后往前遍历,这样可以保证最新的消息在前面
161
+ for (let i = inputMessages.length - 1; i >= 0; i--) {
162
+ const message = this.cloneMessage(inputMessages[i]);
163
+
164
+ if (!message.id) {
165
+ result.unshift(message);
166
+ continue;
167
+ }
168
+
169
+ // 如果已经处理过这个 id 的消息,跳过
170
+ if (previousMessage.has(message.id)) {
171
+ continue;
172
+ }
173
+
174
+ if (StreamingMessageType.isToolAssistant(message)) {
175
+ const m = this.replaceMessageWithValuesMessage(message as AIMessage);
176
+ // 记录这个 id 的消息,并添加到结果中
177
+ previousMessage.set(message.id, m);
178
+
179
+ /** @ts-ignore */
180
+ const tool_calls: NonNullable<AIMessage["tool_calls"]> = (m as AIMessage).tool_calls?.length ? (m as AIMessage).tool_calls : (m as RenderMessage).tool_call_chunks;
181
+ const new_tool_calls = tool_calls!.map((tool, index) => {
182
+ return this.replaceMessageWithValuesMessage(
183
+ {
184
+ type: "tool",
185
+ additional_kwargs: {},
186
+ /** @ts-ignore */
187
+ tool_input: m.additional_kwargs?.tool_calls[index].function.arguments,
188
+ id: tool.id,
189
+ name: tool.name,
190
+ response_metadata: {},
191
+ tool_call_id: tool.id!,
192
+ },
193
+ true
194
+ );
195
+ });
196
+ for (const tool of new_tool_calls) {
197
+ if (!previousMessage.has(tool.id!)) {
198
+ result.unshift(tool);
199
+ previousMessage.set(tool.id!, tool);
200
+ }
201
+ }
202
+ result.unshift(m);
203
+ } else {
204
+ // 记录这个 id 的消息,并添加到结果中
205
+ const m = this.replaceMessageWithValuesMessage(message as AIMessage);
206
+ previousMessage.set(message.id, m);
207
+ result.unshift(m);
208
+ }
209
+ }
210
+
211
+ return this.attachInfoForMessage(this.composeToolMessages(result as RenderMessage[]));
212
+ }
213
+ attachInfoForMessage(result: RenderMessage[]) {
214
+ let lastMessage: RenderMessage | null = null;
215
+ for (const message of result) {
216
+ const createTime = message.response_metadata?.create_time || "";
217
+ // 用长度作为渲染 id,长度变了就要重新渲染
218
+ message.unique_id = message.id! + JSON.stringify(message.content).length;
219
+ message.spend_time = new Date(createTime).getTime() - new Date(lastMessage?.response_metadata?.create_time || createTime).getTime();
220
+ if (!message.usage_metadata && (message as AIMessage).response_metadata?.usage) {
221
+ const usage = (message as AIMessage).response_metadata!.usage as {
222
+ prompt_tokens: number;
223
+ completion_tokens: number;
224
+ total_tokens: number;
225
+ };
226
+ message.usage_metadata = {
227
+ input_tokens: usage.prompt_tokens,
228
+ output_tokens: usage.completion_tokens,
229
+ total_tokens: usage.total_tokens,
230
+ };
231
+ }
232
+ lastMessage = message;
233
+ }
234
+ return result;
235
+ }
236
+ composeToolMessages(messages: RenderMessage[]): RenderMessage[] {
237
+ const result: RenderMessage[] = [];
238
+ const assistantToolMessages = new Map<string, { args: string }>();
239
+ const toolParentMessage = new Map<string, RenderMessage>();
240
+ for (const message of messages) {
241
+ if (StreamingMessageType.isToolAssistant(message)) {
242
+ /** @ts-ignore 只有 tool_call_chunks 的 args 才是文本 */
243
+ message.tool_call_chunks?.forEach((element) => {
244
+ assistantToolMessages.set(element.id!, element);
245
+ toolParentMessage.set(element.id!, message);
246
+ });
247
+ if (!message.content) continue;
248
+ }
249
+ if (StreamingMessageType.isTool(message) && !message.tool_input) {
250
+ const assistantToolMessage = assistantToolMessages.get(message.tool_call_id!);
251
+ const parentMessage = toolParentMessage.get(message.tool_call_id!);
252
+ if (assistantToolMessage) {
253
+ message.tool_input = assistantToolMessage.args;
254
+ if (message.additional_kwargs) {
255
+ message.additional_kwargs.done = true;
256
+ } else {
257
+ message.additional_kwargs = {
258
+ done: true,
259
+ };
260
+ }
261
+ }
262
+ if (parentMessage) {
263
+ message.usage_metadata = parentMessage.usage_metadata;
264
+ }
265
+ }
266
+ result.push(message);
267
+ }
268
+ return result;
269
+ }
270
+ get tokenCounter() {
271
+ return this.graphMessages.reduce(
272
+ (acc, message) => {
273
+ if (message.usage_metadata) {
274
+ acc.total_tokens += message.usage_metadata?.total_tokens || 0;
275
+ acc.input_tokens += message.usage_metadata?.input_tokens || 0;
276
+ acc.output_tokens += message.usage_metadata?.output_tokens || 0;
277
+ } else if ((message as AIMessage).response_metadata?.usage) {
278
+ const usage = (message as AIMessage).response_metadata?.usage as {
279
+ prompt_tokens: number;
280
+ completion_tokens: number;
281
+ total_tokens: number;
282
+ };
283
+ acc.total_tokens += usage.total_tokens || 0;
284
+ acc.input_tokens += usage.prompt_tokens || 0;
285
+ acc.output_tokens += usage.completion_tokens || 0;
286
+ }
287
+
288
+ return acc;
289
+ },
290
+ {
291
+ total_tokens: 0,
292
+ input_tokens: 0,
293
+ output_tokens: 0,
294
+ }
295
+ );
296
+ }
297
+ onStreamingUpdate(callback: StreamingUpdateCallback) {
298
+ this.streamingCallbacks.add(callback);
299
+ return () => {
300
+ this.streamingCallbacks.delete(callback);
301
+ };
302
+ }
303
+
304
+ private emitStreamingUpdate(event: StreamingUpdateEvent) {
305
+ this.streamingCallbacks.forEach((callback) => callback(event));
306
+ }
307
+ graphState: any = {};
308
+ currentRun?: { run_id: string };
309
+ cancelRun() {
310
+ if (this.currentThread?.thread_id && this.currentRun?.run_id) {
311
+ this.runs.cancel(this.currentThread!.thread_id, this.currentRun.run_id);
312
+ }
313
+ }
314
+ async sendMessage(input: string | Message[], { extraParams, _debug, command }: { extraParams?: Record<string, any>; _debug?: { streamResponse?: any }; command?: Command } = {}) {
315
+ if (!this.currentAssistant) {
316
+ throw new Error("Thread or Assistant not initialized");
317
+ }
318
+ if (!this.currentThread) {
319
+ await this.createThread();
320
+ this.emitStreamingUpdate({
321
+ type: "thread",
322
+ data: {
323
+ event: "thread/create",
324
+ data: {
325
+ thread: this.currentThread,
326
+ },
327
+ },
328
+ });
329
+ }
330
+
331
+ const messagesToSend = Array.isArray(input)
332
+ ? input
333
+ : [
334
+ {
335
+ type: "human",
336
+ content: input,
337
+ } as HumanMessage,
338
+ ];
339
+ const streamResponse =
340
+ _debug?.streamResponse ||
341
+ this.runs.stream(this.currentThread!.thread_id, this.currentAssistant.assistant_id, {
342
+ input: { ...this.graphState, ...(extraParams || {}), messages: messagesToSend, fe_tools: this.tools.toJSON() },
343
+ streamMode: ["messages", "values"],
344
+ streamSubgraphs: true,
345
+ command,
346
+ });
347
+ const streamRecord: any[] = [];
348
+ for await (const chunk of streamResponse) {
349
+ streamRecord.push(chunk);
350
+ if (chunk.event === "metadata") {
351
+ this.currentRun = chunk.data;
352
+ } else if (chunk.event === "error") {
353
+ this.emitStreamingUpdate({
354
+ type: "error",
355
+ data: chunk,
356
+ });
357
+ } else if (chunk.event === "messages/partial") {
358
+ for (const message of chunk.data) {
359
+ this.streamingMessage.push(message);
360
+ }
361
+ this.emitStreamingUpdate({
362
+ type: "message",
363
+ data: chunk,
364
+ });
365
+ continue;
366
+ } else if (chunk.event.startsWith("values")) {
367
+ const data = chunk.data as { messages: Message[] };
368
+
369
+ if (data.messages) {
370
+ const isResume = !!command?.resume;
371
+ const isLongerThanLocal = data.messages.length >= this.graphMessages.length;
372
+ // resume 情况下,长度低于前端 message 的统统不接受
373
+ if (!isResume || (isResume && isLongerThanLocal)) {
374
+ this.graphMessages = data.messages as RenderMessage[];
375
+ this.emitStreamingUpdate({
376
+ type: "value",
377
+ data: chunk,
378
+ });
379
+ }
380
+ }
381
+ this.graphState = chunk.data;
382
+ this.streamingMessage = [];
383
+ continue;
384
+ }
385
+ }
386
+ this.streamingMessage = [];
387
+ const data = await this.runFETool();
388
+ if (data) streamRecord.push(...data);
389
+ this.emitStreamingUpdate({
390
+ type: "done",
391
+ data: {
392
+ event: "done",
393
+ },
394
+ });
395
+ return streamRecord;
396
+ }
397
+ private runFETool() {
398
+ const data = this.graphMessages;
399
+ const lastMessage = data[data.length - 1];
400
+ // 如果最后一条消息是前端工具消息,则调用工具
401
+ if (lastMessage.type === "ai" && lastMessage.tool_calls?.length) {
402
+ const result = lastMessage.tool_calls.map((tool) => {
403
+ if (this.tools.getTool(tool.name!)) {
404
+ const toolMessage: ToolMessage = {
405
+ ...tool,
406
+ tool_call_id: tool.id!,
407
+ /** @ts-ignore */
408
+ tool_input: JSON.stringify(tool.args),
409
+ additional_kwargs: {},
410
+ };
411
+ // json 校验
412
+ return this.callFETool(toolMessage, tool.args);
413
+ }
414
+ });
415
+ return Promise.all(result);
416
+ }
417
+ }
418
+ private async callFETool(message: ToolMessage, args: any) {
419
+ const that = this; // 防止 this 被错误解析
420
+ const result = await this.tools.callTool(message.name!, args, { client: that, message });
421
+ return this.resume(result);
422
+ }
423
+ /** 恢复消息,当中断流时使用 */
424
+ resume(result: CallToolResult) {
425
+ return this.sendMessage([], {
426
+ command: {
427
+ resume: result,
428
+ },
429
+ });
430
+ }
431
+ /** 完成工具等待 */
432
+ doneFEToolWaiting(id: string, result: CallToolResult) {
433
+ const done = this.tools.doneWaiting(id, result);
434
+ if (!done && this.currentThread?.status === "interrupted") {
435
+ this.resume(result);
436
+ }
437
+ }
438
+
439
+ getCurrentThread() {
440
+ return this.currentThread;
441
+ }
442
+
443
+ getCurrentAssistant() {
444
+ return this.currentAssistant;
445
+ }
446
+
447
+ async reset() {
448
+ await this.initAssistant(this.currentAssistant?.name!);
449
+ this.currentThread = null;
450
+ this.graphState = {};
451
+ this.graphMessages = [];
452
+ this.streamingMessage = [];
453
+ this.currentRun = undefined;
454
+ this.emitStreamingUpdate({
455
+ type: "value",
456
+ data: {
457
+ event: "messages/partial",
458
+ },
459
+ });
460
+ }
461
+ }
@@ -0,0 +1,29 @@
1
+ export class SpendTime {
2
+ private timeCounter = new Map<string, [Date, Date] | [Date]>();
3
+
4
+ start(key: string) {
5
+ this.timeCounter.set(key, [new Date()]);
6
+ }
7
+
8
+ end(key: string) {
9
+ this.timeCounter.set(key, [this.timeCounter.get(key)?.[0] || new Date(), new Date()]);
10
+ }
11
+ setSpendTime(key: string) {
12
+ if (this.timeCounter.has(key)) {
13
+ this.end(key);
14
+ } else {
15
+ this.start(key);
16
+ }
17
+ }
18
+ getStartTime(key: string) {
19
+ return this.timeCounter.get(key)?.[0] || new Date();
20
+ }
21
+ getEndTime(key: string) {
22
+ return this.timeCounter.get(key)?.[1] || new Date();
23
+ }
24
+
25
+ getSpendTime(key: string) {
26
+ const [start, end = new Date()] = this.timeCounter.get(key) || [new Date(), new Date()];
27
+ return end.getTime() - start.getTime();
28
+ }
29
+ }
@@ -0,0 +1,100 @@
1
+ import { ToolMessage } from "@langchain/langgraph-sdk";
2
+ import { LangGraphClient } from "./LangGraphClient";
3
+ import { CallToolResult, createJSONDefineTool, UnionTool } from "./tool/createTool";
4
+
5
+ export class ToolManager {
6
+ private tools: Map<string, UnionTool<any>> = new Map();
7
+
8
+ /**
9
+ * 注册一个工具
10
+ * @param tool 要注册的工具
11
+ */
12
+ bindTool(tool: UnionTool<any>) {
13
+ if (this.tools.has(tool.name)) {
14
+ throw new Error(`Tool with name ${tool.name} already exists`);
15
+ }
16
+ this.tools.set(tool.name, tool);
17
+ }
18
+
19
+ /**
20
+ * 注册多个工具
21
+ * @param tools 要注册的工具数组
22
+ */
23
+ bindTools(tools: UnionTool<any>[]) {
24
+ tools.forEach((tool) => this.bindTool(tool));
25
+ }
26
+
27
+ /**
28
+ * 获取所有已注册的工具
29
+ * @returns 工具数组
30
+ */
31
+ getAllTools(): UnionTool<any>[] {
32
+ return Array.from(this.tools.values());
33
+ }
34
+
35
+ /**
36
+ * 获取指定名称的工具
37
+ * @param name 工具名称
38
+ * @returns 工具实例或 undefined
39
+ */
40
+ getTool(name: string): UnionTool<any> | undefined {
41
+ return this.tools.get(name);
42
+ }
43
+
44
+ /**
45
+ * 移除指定名称的工具
46
+ * @param name 工具名称
47
+ * @returns 是否成功移除
48
+ */
49
+ removeTool(name: string): boolean {
50
+ return this.tools.delete(name);
51
+ }
52
+
53
+ /**
54
+ * 清空所有工具
55
+ */
56
+ clearTools() {
57
+ this.tools.clear();
58
+ }
59
+ async callTool(name: string, args: any, context: { client: LangGraphClient; message: ToolMessage }) {
60
+ const tool = this.getTool(name);
61
+ if (!tool) {
62
+ throw new Error(`Tool with name ${name} not found`);
63
+ }
64
+ return await tool.execute(args, context);
65
+ }
66
+ toJSON() {
67
+ return Array.from(this.tools.values()).map((i) => createJSONDefineTool(i));
68
+ }
69
+
70
+ // === 专门为前端设计的异步触发结构
71
+ private waitingMap: Map<string, (value: CallToolResult) => void> = new Map();
72
+ doneWaiting(id: string, value: CallToolResult) {
73
+ if (this.waitingMap.has(id)) {
74
+ this.waitingMap.get(id)!(value);
75
+ this.waitingMap.delete(id);
76
+ return true;
77
+ } else {
78
+ console.warn(`Waiting for tool ${id} not found`);
79
+ return false;
80
+ }
81
+ }
82
+ waitForDone(id: string) {
83
+ if (this.waitingMap.has(id)) {
84
+ return this.waitingMap.get(id);
85
+ }
86
+ const promise = new Promise((resolve, reject) => {
87
+ this.waitingMap.set(id, resolve);
88
+ });
89
+ return promise;
90
+ }
91
+ /** 等待用户输入
92
+ * @example
93
+ * // 继续 chat 流
94
+ * client.tools.doneWaiting(message.id!, (e.target as any).value);
95
+ */
96
+ static waitForUIDone<T>(_: T, context: { client: LangGraphClient; message: ToolMessage }) {
97
+ // console.log(context.message);
98
+ return context.client.tools.waitForDone(context.message.id!);
99
+ }
100
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./LangGraphClient";
2
+ export * from "./tool";
3
+ export * from "@langchain/langgraph-sdk";
4
+ export * from "./ui-store";
5
+ export * from "./ToolManager";
@@ -0,0 +1,72 @@
1
+ import { Message } from "@langchain/langgraph-sdk";
2
+ /**
3
+ * copy and modify from copilotkit
4
+ * https://github.com/copilotkit/copilotkit
5
+ *
6
+ * MIT License
7
+ */
8
+ type TypeMap = {
9
+ string: string;
10
+ number: number;
11
+ boolean: boolean;
12
+ object: object;
13
+ "string[]": string[];
14
+ "number[]": number[];
15
+ "boolean[]": boolean[];
16
+ "object[]": object[];
17
+ };
18
+
19
+ type AbstractParameter = {
20
+ name: string;
21
+ type?: keyof TypeMap;
22
+ description?: string;
23
+ required?: boolean;
24
+ };
25
+
26
+ interface StringParameter extends AbstractParameter {
27
+ type: "string";
28
+ enum?: string[];
29
+ }
30
+
31
+ interface ObjectParameter extends AbstractParameter {
32
+ type: "object";
33
+ attributes?: Parameter[];
34
+ }
35
+
36
+ interface ObjectArrayParameter extends AbstractParameter {
37
+ type: "object[]";
38
+ attributes?: Parameter[];
39
+ }
40
+
41
+ type SpecialParameters = StringParameter | ObjectParameter | ObjectArrayParameter;
42
+ interface BaseParameter extends AbstractParameter {
43
+ type?: Exclude<AbstractParameter["type"], SpecialParameters["type"]>;
44
+ }
45
+
46
+ export type Parameter = BaseParameter | SpecialParameters;
47
+
48
+ type OptionalParameterType<P extends AbstractParameter> = P["required"] extends false ? undefined : never;
49
+
50
+ type StringParameterType<P> = P extends StringParameter ? (P extends { enum?: Array<infer E> } ? E : string) : never;
51
+
52
+ type ObjectParameterType<P> = P extends ObjectParameter ? (P extends { attributes?: infer Attributes extends Parameter[] } ? MappedParameterTypes<Attributes> : object) : never;
53
+
54
+ type ObjectArrayParameterType<P> = P extends ObjectArrayParameter ? (P extends { attributes?: infer Attributes extends Parameter[] } ? MappedParameterTypes<Attributes>[] : any[]) : never;
55
+
56
+ type MappedTypeOrString<T> = T extends keyof TypeMap ? TypeMap[T] : string;
57
+ type BaseParameterType<P extends AbstractParameter> = P extends { type: infer T } ? (T extends BaseParameter["type"] ? MappedTypeOrString<T> : never) : string;
58
+
59
+ export type MappedParameterTypes<T extends Parameter[] | [] = []> = T extends []
60
+ ? Record<string, any>
61
+ : {
62
+ [P in T[number] as P["name"]]: OptionalParameterType<P> | StringParameterType<P> | ObjectParameterType<P> | ObjectArrayParameterType<P> | BaseParameterType<P>;
63
+ };
64
+
65
+ export type Action<T extends Parameter[] | [] = []> = {
66
+ name: string;
67
+ description?: string;
68
+ parameters?: T;
69
+ handler?: T extends [] ? () => any | Promise<any> : (args: MappedParameterTypes<T>, context?: any) => any | Promise<any>;
70
+ returnDirect?: boolean;
71
+ callbackMessage?: () => Message[];
72
+ };