@copilotkit/sdk-js 2.0.0-rc.41 → 2.0.0-rc.43
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/langgraph/utils.cjs +60 -39
- package/dist/langgraph/utils.cjs.map +1 -1
- package/dist/langgraph/utils.d.cts +64 -43
- package/dist/langgraph/utils.d.cts.map +1 -1
- package/dist/langgraph/utils.d.mts +64 -43
- package/dist/langgraph/utils.d.mts.map +1 -1
- package/dist/langgraph/utils.mjs +60 -39
- package/dist/langgraph/utils.mjs.map +1 -1
- package/package.json +2 -2
- package/src/langgraph/utils.ts +60 -39
package/dist/langgraph/utils.cjs
CHANGED
|
@@ -10,7 +10,7 @@ let _langchain_core_messages = require("@langchain/core/messages");
|
|
|
10
10
|
/**
|
|
11
11
|
* Customize the LangGraph configuration for use in CopilotKit.
|
|
12
12
|
*
|
|
13
|
-
* To the CopilotKit SDK, run:
|
|
13
|
+
* To install the CopilotKit SDK, run:
|
|
14
14
|
*
|
|
15
15
|
* ```bash
|
|
16
16
|
* npm install @copilotkit/sdk-js
|
|
@@ -21,13 +21,15 @@ let _langchain_core_messages = require("@langchain/core/messages");
|
|
|
21
21
|
* Disable emitting messages and tool calls:
|
|
22
22
|
*
|
|
23
23
|
* ```typescript
|
|
24
|
-
* import {
|
|
24
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
25
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
25
26
|
*
|
|
26
|
-
* config
|
|
27
|
-
* config,
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
27
|
+
* function customize(config: RunnableConfig) {
|
|
28
|
+
* return copilotkitCustomizeConfig(config, {
|
|
29
|
+
* emitMessages: false,
|
|
30
|
+
* emitToolCalls: false,
|
|
31
|
+
* });
|
|
32
|
+
* }
|
|
31
33
|
* ```
|
|
32
34
|
*
|
|
33
35
|
* To emit a tool call as streaming LangGraph state, pass the destination key in state,
|
|
@@ -35,18 +37,20 @@ let _langchain_core_messages = require("@langchain/core/messages");
|
|
|
35
37
|
* all arguments are emitted under the state key.)
|
|
36
38
|
*
|
|
37
39
|
* ```typescript
|
|
38
|
-
* import {
|
|
40
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
41
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
39
42
|
*
|
|
40
|
-
* config
|
|
41
|
-
* config,
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
43
|
+
* function customize(config: RunnableConfig) {
|
|
44
|
+
* return copilotkitCustomizeConfig(config, {
|
|
45
|
+
* emitIntermediateState: [
|
|
46
|
+
* {
|
|
47
|
+
* stateKey: "steps",
|
|
48
|
+
* tool: "SearchTool",
|
|
49
|
+
* toolArgument: "steps",
|
|
50
|
+
* },
|
|
51
|
+
* ],
|
|
52
|
+
* });
|
|
53
|
+
* }
|
|
50
54
|
* ```
|
|
51
55
|
*/
|
|
52
56
|
function copilotkitCustomizeConfig(baseConfig, options) {
|
|
@@ -85,18 +89,23 @@ function copilotkitCustomizeConfig(baseConfig, options) {
|
|
|
85
89
|
}
|
|
86
90
|
}
|
|
87
91
|
/**
|
|
88
|
-
* Exits the current agent after the run completes. Calling
|
|
92
|
+
* Exits the current agent after the run completes. Calling copilotkitExit() will
|
|
89
93
|
* not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
|
|
90
94
|
* the run completes.
|
|
91
95
|
*
|
|
92
96
|
* ### Examples
|
|
93
97
|
*
|
|
94
98
|
* ```typescript
|
|
95
|
-
* import {
|
|
99
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
100
|
+
* import type { MessagesAnnotation } from "@langchain/langgraph";
|
|
101
|
+
* import { copilotkitExit } from "@copilotkit/sdk-js/langgraph";
|
|
102
|
+
*
|
|
103
|
+
* type AgentState = typeof MessagesAnnotation.State;
|
|
96
104
|
*
|
|
97
|
-
* async function myNode(state:
|
|
98
|
-
* await copilotkitExit(config)
|
|
99
|
-
* return state
|
|
105
|
+
* async function myNode(state: AgentState, config: RunnableConfig) {
|
|
106
|
+
* await copilotkitExit(config);
|
|
107
|
+
* return state;
|
|
108
|
+
* }
|
|
100
109
|
* ```
|
|
101
110
|
*/
|
|
102
111
|
async function copilotkitExit(config) {
|
|
@@ -114,11 +123,16 @@ async function copilotkitExit(config) {
|
|
|
114
123
|
* ### Examples
|
|
115
124
|
*
|
|
116
125
|
* ```typescript
|
|
117
|
-
* import {
|
|
126
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
127
|
+
* import { copilotkitEmitState } from "@copilotkit/sdk-js/langgraph";
|
|
128
|
+
*
|
|
129
|
+
* declare function someLongRunningOperation(step: number): Promise<void>;
|
|
118
130
|
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
131
|
+
* async function myNode(config: RunnableConfig) {
|
|
132
|
+
* for (let i = 0; i < 10; i++) {
|
|
133
|
+
* await someLongRunningOperation(i);
|
|
134
|
+
* await copilotkitEmitState(config, { progress: i });
|
|
135
|
+
* }
|
|
122
136
|
* }
|
|
123
137
|
* ```
|
|
124
138
|
*/
|
|
@@ -138,14 +152,18 @@ async function copilotkitEmitState(config, state) {
|
|
|
138
152
|
* ### Examples
|
|
139
153
|
*
|
|
140
154
|
* ```typescript
|
|
141
|
-
* import {
|
|
155
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
156
|
+
* import { AIMessage } from "@langchain/core/messages";
|
|
157
|
+
* import { copilotkitEmitMessage } from "@copilotkit/sdk-js/langgraph";
|
|
142
158
|
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
159
|
+
* async function myNode(config: RunnableConfig) {
|
|
160
|
+
* const message = "Step 1 of 10 complete";
|
|
161
|
+
* await copilotkitEmitMessage(config, message);
|
|
145
162
|
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
163
|
+
* // Return the message from the node
|
|
164
|
+
* return {
|
|
165
|
+
* messages: [new AIMessage({ content: message })],
|
|
166
|
+
* };
|
|
149
167
|
* }
|
|
150
168
|
* ```
|
|
151
169
|
*/
|
|
@@ -168,15 +186,18 @@ async function copilotkitEmitMessage(config, message) {
|
|
|
168
186
|
* ### Examples
|
|
169
187
|
*
|
|
170
188
|
* ```typescript
|
|
171
|
-
* import {
|
|
189
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
190
|
+
* import { copilotkitEmitToolCall } from "@copilotkit/sdk-js/langgraph";
|
|
172
191
|
*
|
|
173
|
-
*
|
|
192
|
+
* async function myNode(config: RunnableConfig) {
|
|
193
|
+
* const autoId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 });
|
|
174
194
|
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
195
|
+
* // With a custom ID for correlation/idempotency:
|
|
196
|
+
* const customId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 }, { toolCallId: "my-custom-id" });
|
|
197
|
+
* }
|
|
177
198
|
* ```
|
|
178
199
|
*
|
|
179
|
-
* @returns The tool call ID used for the emitted call
|
|
200
|
+
* @returns The tool call ID used for the emitted call. It equals `options.toolCallId`
|
|
180
201
|
* when provided, otherwise a randomly generated ID.
|
|
181
202
|
*/
|
|
182
203
|
async function copilotkitEmitToolCall(config, name, args, options) {
|
|
@@ -228,7 +249,7 @@ function convertActionToDynamicStructuredTool(actionInput) {
|
|
|
228
249
|
* ### Examples
|
|
229
250
|
*
|
|
230
251
|
* ```typescript
|
|
231
|
-
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js";
|
|
252
|
+
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js/langgraph";
|
|
232
253
|
*
|
|
233
254
|
* const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);
|
|
234
255
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","names":["CopilotKitMisuseError","DynamicStructuredTool","convertJsonSchemaToZodSchema","AIMessage"],"sources":["../../src/langgraph/utils.ts"],"sourcesContent":["import type { RunnableConfig } from \"@langchain/core/runnables\";\nimport { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\nimport {\n randomId,\n randomUUID,\n CopilotKitMisuseError,\n} from \"@copilotkit/shared\";\nimport { interrupt } from \"@langchain/langgraph\";\nimport { DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { AIMessage } from \"@langchain/core/messages\";\nimport type { OptionsConfig } from \"./types\";\nimport { convertJsonSchemaToZodSchema } from \"./json-schema-to-zod\";\n\n/**\n * Customize the LangGraph configuration for use in CopilotKit.\n *\n * To the CopilotKit SDK, run:\n *\n * ```bash\n * npm install @copilotkit/sdk-js\n * ```\n *\n * ### Examples\n *\n * Disable emitting messages and tool calls:\n *\n * ```typescript\n * import { copilotkitCustomizeConfig } from \"@copilotkit/sdk-js\";\n *\n * config = copilotkitCustomizeConfig(\n * config,\n * emitMessages=false,\n * emitToolCalls=false\n * )\n * ```\n *\n * To emit a tool call as streaming LangGraph state, pass the destination key in state,\n * the tool name and optionally the tool argument. (If you don't pass the argument name,\n * all arguments are emitted under the state key.)\n *\n * ```typescript\n * import { copilotkitCustomizeConfig } from \"@copilotkit/sdk-js\";\n *\n * config = copilotkitCustomizeConfig(\n * config,\n * emitIntermediateState=[\n * {\n * \"stateKey\": \"steps\",\n * \"tool\": \"SearchTool\",\n * \"toolArgument\": \"steps\",\n * },\n * ],\n * )\n * ```\n */\nexport function copilotkitCustomizeConfig(\n /**\n * The LangChain/LangGraph configuration to customize.\n */\n baseConfig: RunnableConfig,\n /**\n * Configuration options:\n * - `emitMessages: boolean?`\n * Configure how messages are emitted. By default, all messages are emitted. Pass false to\n * disable emitting messages.\n * - `emitToolCalls: boolean | string | string[]?`\n * Configure how tool calls are emitted. By default, all tool calls are emitted. Pass false to\n * disable emitting tool calls. Pass a string or list of strings to emit only specific tool calls.\n * - `emitIntermediateState: IntermediateStateConfig[]?`\n * Lets you emit tool calls as streaming LangGraph state.\n */\n options?: OptionsConfig,\n): RunnableConfig {\n if (baseConfig && typeof baseConfig !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"baseConfig must be an object or null/undefined\",\n });\n }\n\n if (options && typeof options !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"options must be an object when provided\",\n });\n }\n\n // Validate emitIntermediateState structure\n if (options?.emitIntermediateState) {\n if (!Array.isArray(options.emitIntermediateState)) {\n throw new CopilotKitMisuseError({\n message: \"emitIntermediateState must be an array when provided\",\n });\n }\n\n options.emitIntermediateState.forEach((state, index) => {\n if (!state || typeof state !== \"object\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must be an object`,\n });\n }\n\n if (!state.stateKey || typeof state.stateKey !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must have a valid 'stateKey' string property`,\n });\n }\n\n if (!state.tool || typeof state.tool !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must have a valid 'tool' string property`,\n });\n }\n\n if (state.toolArgument && typeof state.toolArgument !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}].toolArgument must be a string when provided`,\n });\n }\n });\n }\n\n try {\n const metadata = baseConfig?.metadata || {};\n\n if (options?.emitAll) {\n metadata[\"copilotkit:emit-tool-calls\"] = true;\n metadata[\"copilotkit:emit-messages\"] = true;\n } else {\n if (options?.emitToolCalls !== undefined) {\n metadata[\"copilotkit:emit-tool-calls\"] = options.emitToolCalls;\n }\n if (options?.emitMessages !== undefined) {\n metadata[\"copilotkit:emit-messages\"] = options.emitMessages;\n }\n }\n\n if (options?.emitIntermediateState) {\n const snakeCaseIntermediateState = options.emitIntermediateState.map(\n (state) => ({\n tool: state.tool,\n tool_argument: state.toolArgument,\n state_key: state.stateKey,\n }),\n );\n\n metadata[\"copilotkit:emit-intermediate-state\"] =\n snakeCaseIntermediateState;\n }\n\n baseConfig = baseConfig || {};\n\n return {\n ...baseConfig,\n metadata: metadata,\n };\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to customize config: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Exits the current agent after the run completes. Calling copilotkit_exit() will\n * not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after\n * the run completes.\n *\n * ### Examples\n *\n * ```typescript\n * import { copilotkitExit } from \"@copilotkit/sdk-js\";\n *\n * async function myNode(state: Any):\n * await copilotkitExit(config)\n * return state\n * ```\n */\nexport async function copilotkitExit(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitExit\",\n });\n }\n\n try {\n await dispatchCustomEvent(\"copilotkit_exit\", {}, config);\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to dispatch exit event: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Emits intermediate state to CopilotKit. Useful if you have a longer running node and you want to\n * update the user with the current state of the node.\n *\n * ### Examples\n *\n * ```typescript\n * import { copilotkitEmitState } from \"@copilotkit/sdk-js\";\n *\n * for (let i = 0; i < 10; i++) {\n * await someLongRunningOperation(i);\n * await copilotkitEmitState(config, { progress: i });\n * }\n * ```\n */\nexport async function copilotkitEmitState(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The state to emit.\n */\n state: any,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitState\",\n });\n }\n\n if (state === undefined) {\n throw new CopilotKitMisuseError({\n message: \"State is required for copilotkitEmitState\",\n });\n }\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_intermediate_state\",\n state,\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit state: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Manually emits a message to CopilotKit. Useful in longer running nodes to update the user.\n * Important: You still need to return the messages from the node.\n *\n * ### Examples\n *\n * ```typescript\n * import { copilotkitEmitMessage } from \"@copilotkit/sdk-js\";\n *\n * const message = \"Step 1 of 10 complete\";\n * await copilotkitEmitMessage(config, message);\n *\n * // Return the message from the node\n * return {\n * \"messages\": [AIMessage(content=message)]\n * }\n * ```\n */\nexport async function copilotkitEmitMessage(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The message to emit.\n */\n message: string,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitMessage\",\n });\n }\n\n if (!message || typeof message !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Message must be a non-empty string for copilotkitEmitMessage\",\n });\n }\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_message\",\n { message, message_id: randomId(), role: \"assistant\" },\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit message: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Manually emits a tool call to CopilotKit.\n *\n * ### Examples\n *\n * ```typescript\n * import { copilotkitEmitToolCall } from \"@copilotkit/sdk-js\";\n *\n * const autoId = await copilotkitEmitToolCall(config, \"SearchTool\", { steps: 10 });\n *\n * // With a custom ID for correlation/idempotency:\n * const customId = await copilotkitEmitToolCall(config, \"SearchTool\", { steps: 10 }, { toolCallId: \"my-custom-id\" });\n * ```\n *\n * @returns The tool call ID used for the emitted call — equals `options.toolCallId`\n * when provided, otherwise a randomly generated ID.\n */\nexport async function copilotkitEmitToolCall(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The name of the tool to emit.\n */\n name: string,\n /**\n * The arguments to emit.\n */\n args: any,\n /**\n * Options for the tool call emission.\n */\n options?: {\n /**\n * Optional tool call ID. If not provided, a random ID is generated.\n * When provided, this ID is used as the toolCallId and parentMessageId\n * in AG-UI protocol events. The caller is responsible for ensuring uniqueness.\n */\n toolCallId?: string;\n },\n): Promise<string> {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitToolCall\",\n });\n }\n\n if (typeof name !== \"string\" || name.trim().length === 0) {\n throw new CopilotKitMisuseError({\n message:\n \"Tool name must be a non-empty string for copilotkitEmitToolCall\",\n });\n }\n\n if (\n options?.toolCallId !== undefined &&\n (typeof options.toolCallId !== \"string\" ||\n options.toolCallId.trim().length === 0)\n ) {\n throw new CopilotKitMisuseError({\n message:\n \"Tool call id must be a non-empty string when provided for copilotkitEmitToolCall\",\n });\n }\n\n if (args === undefined) {\n throw new CopilotKitMisuseError({\n message: \"Tool arguments are required for copilotkitEmitToolCall\",\n });\n }\n\n try {\n JSON.stringify(args);\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Tool arguments for '${name}' are not JSON-serializable: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n\n const toolCallId = options?.toolCallId ?? randomUUID();\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_tool_call\",\n { name, args, id: toolCallId },\n config,\n );\n } catch (error) {\n const wrapped = new Error(\n `copilotkitEmitToolCall dispatch failed for tool=\"${name}\" id=\"${toolCallId}\": ${error instanceof Error ? error.message : String(error)}`,\n );\n (wrapped as any).cause = error;\n throw wrapped;\n }\n\n return toolCallId;\n}\n\nexport function convertActionToDynamicStructuredTool(\n actionInput: any,\n): DynamicStructuredTool<any> {\n if (!actionInput) {\n throw new CopilotKitMisuseError({\n message: \"Action input is required but was not provided\",\n });\n }\n\n if (!actionInput.name || typeof actionInput.name !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Action must have a valid 'name' property of type string\",\n });\n }\n\n if (\n actionInput.description == undefined ||\n actionInput.description == null ||\n typeof actionInput.description !== \"string\"\n ) {\n throw new CopilotKitMisuseError({\n message: `Action '${actionInput.name}' must have a valid 'description' property of type string`,\n });\n }\n\n if (!actionInput.parameters) {\n throw new CopilotKitMisuseError({\n message: `Action '${actionInput.name}' must have a 'parameters' property`,\n });\n }\n\n try {\n return new DynamicStructuredTool({\n name: actionInput.name,\n description: actionInput.description,\n schema: convertJsonSchemaToZodSchema(actionInput.parameters, true),\n func: async () => {\n return \"\";\n },\n });\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to convert action '${actionInput.name}' to DynamicStructuredTool: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Use this function to convert a list of actions you get from state\n * to a list of dynamic structured tools.\n *\n * ### Examples\n *\n * ```typescript\n * import { convertActionsToDynamicStructuredTools } from \"@copilotkit/sdk-js\";\n *\n * const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);\n * ```\n */\nexport function convertActionsToDynamicStructuredTools(\n /**\n * The list of actions to convert.\n */\n actions: any[],\n): DynamicStructuredTool<any>[] {\n if (!Array.isArray(actions)) {\n throw new CopilotKitMisuseError({\n message: \"Actions must be an array\",\n });\n }\n\n return actions.map((action, index) => {\n try {\n return convertActionToDynamicStructuredTool(\n action.type === \"function\" ? action.function : action,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to convert action at index ${index}: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n });\n}\n\nexport function copilotKitInterrupt({\n message,\n action,\n args,\n}: {\n message?: string;\n action?: string;\n args?: Record<string, any>;\n}) {\n if (!message && !action) {\n throw new CopilotKitMisuseError({\n message:\n \"Either message or action (and optional arguments) must be provided for copilotKitInterrupt\",\n });\n }\n\n if (action && typeof action !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Action must be a string when provided to copilotKitInterrupt\",\n });\n }\n\n if (message && typeof message !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Message must be a string when provided to copilotKitInterrupt\",\n });\n }\n\n if (args && typeof args !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"Args must be an object when provided to copilotKitInterrupt\",\n });\n }\n\n let interruptValues = null;\n let interruptMessage = null;\n let answer = null;\n\n try {\n if (message) {\n interruptValues = message;\n interruptMessage = new AIMessage({ content: message, id: randomId() });\n } else {\n const toolId = randomId();\n interruptMessage = new AIMessage({\n content: \"\",\n tool_calls: [{ id: toolId, name: action, args: args ?? {} }],\n });\n interruptValues = {\n action,\n args: args ?? {},\n };\n }\n\n const response = interrupt({\n __copilotkit_interrupt_value__: interruptValues,\n __copilotkit_messages__: [interruptMessage],\n });\n answer = response[response.length - 1].content;\n\n return {\n answer,\n messages: response,\n };\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to create interrupt: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuDA,SAAgB,0BAId,YAYA,SACgB;AAChB,KAAI,cAAc,OAAO,eAAe,SACtC,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,kDACV,CAAC;AAGJ,KAAI,WAAW,OAAO,YAAY,SAChC,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,2CACV,CAAC;AAIJ,KAAI,SAAS,uBAAuB;AAClC,MAAI,CAAC,MAAM,QAAQ,QAAQ,sBAAsB,CAC/C,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,wDACV,CAAC;AAGJ,UAAQ,sBAAsB,SAAS,OAAO,UAAU;AACtD,OAAI,CAAC,SAAS,OAAO,UAAU,SAC7B,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,yBAAyB,MAAM,sBACzC,CAAC;AAGJ,OAAI,CAAC,MAAM,YAAY,OAAO,MAAM,aAAa,SAC/C,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,yBAAyB,MAAM,iDACzC,CAAC;AAGJ,OAAI,CAAC,MAAM,QAAQ,OAAO,MAAM,SAAS,SACvC,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,yBAAyB,MAAM,6CACzC,CAAC;AAGJ,OAAI,MAA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B,oBAAoB,EAClC,SACA,QACA,QAKC;AACD,KAAI,CAAC,WAAW,CAAC,OACf,OAAM,IAAIA,yCAAsB,EAC9B,SACE,8FACH,CAAC;AAGJ,KAAI,UAAU,OAAO,WAAW,SAC9B,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,gEACV,CAAC;AAGJ,KAAI,WAAW,OAAO,YAAY,SAChC,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,iEACV,CAAC;AAGJ,KAAI,QAAQ,OAAO,SAAS,SAC1B,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,+DACV,CAAC;CAGJ,IAAI,kBAAkB;CACtB,IAAI,mBAAmB;CACvB,IAAI,SAAS;AAEb,KAAI;AACF,MAAI,SAAS;AACX,qBAAkB;AAClB,sBAAmB,IAAIG,mCAAU;IAAE,SAAS;IAAS,sCAAc;IAAE,CAAC;SACjE;AAEL,sBAAmB,IAAIA,mCAAU;IAC/B,SAAS;IACT,YAAY,CAAC;KAAE,sCAHQ;KAGI,MAAM;KAAQ,MAAM,QAAQ,EAAE;KAAE,CAAC;IAC7D,CAAC;AACF,qBAAkB;IAChB;IACA,MAAM,QAAQ,EAAE;IACjB;;EAGH,MAAM,+CAAqB;GACzB,gCAAgC;GAChC,yBAAyB,CAAC,iBAAiB;GAC5C,CAAC;AACF,WAAS,SAAS,SAAS,SAAS,GAAG;AAEvC,SAAO;GACL;GACA,UAAU;GACX;UACM,OAAO;AACd,QAAM,IAAIH,yCAAsB,EAC9B,SAAS,+BAA+B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,IAC/F,CAAC"}
|
|
1
|
+
{"version":3,"file":"utils.cjs","names":["CopilotKitMisuseError","DynamicStructuredTool","convertJsonSchemaToZodSchema","AIMessage"],"sources":["../../src/langgraph/utils.ts"],"sourcesContent":["import type { RunnableConfig } from \"@langchain/core/runnables\";\nimport { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\nimport {\n randomId,\n randomUUID,\n CopilotKitMisuseError,\n} from \"@copilotkit/shared\";\nimport { interrupt } from \"@langchain/langgraph\";\nimport { DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { AIMessage } from \"@langchain/core/messages\";\nimport type { OptionsConfig } from \"./types\";\nimport { convertJsonSchemaToZodSchema } from \"./json-schema-to-zod\";\n\n/**\n * Customize the LangGraph configuration for use in CopilotKit.\n *\n * To install the CopilotKit SDK, run:\n *\n * ```bash\n * npm install @copilotkit/sdk-js\n * ```\n *\n * ### Examples\n *\n * Disable emitting messages and tool calls:\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { copilotkitCustomizeConfig } from \"@copilotkit/sdk-js/langgraph\";\n *\n * function customize(config: RunnableConfig) {\n * return copilotkitCustomizeConfig(config, {\n * emitMessages: false,\n * emitToolCalls: false,\n * });\n * }\n * ```\n *\n * To emit a tool call as streaming LangGraph state, pass the destination key in state,\n * the tool name and optionally the tool argument. (If you don't pass the argument name,\n * all arguments are emitted under the state key.)\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { copilotkitCustomizeConfig } from \"@copilotkit/sdk-js/langgraph\";\n *\n * function customize(config: RunnableConfig) {\n * return copilotkitCustomizeConfig(config, {\n * emitIntermediateState: [\n * {\n * stateKey: \"steps\",\n * tool: \"SearchTool\",\n * toolArgument: \"steps\",\n * },\n * ],\n * });\n * }\n * ```\n */\nexport function copilotkitCustomizeConfig(\n /**\n * The LangChain/LangGraph configuration to customize.\n */\n baseConfig: RunnableConfig,\n /**\n * Configuration options:\n * - `emitMessages: boolean?`\n * Configure how messages are emitted. By default, all messages are emitted. Pass false to\n * disable emitting messages.\n * - `emitToolCalls: boolean | string | string[]?`\n * Configure how tool calls are emitted. By default, all tool calls are emitted. Pass false to\n * disable emitting tool calls. Pass a string or list of strings to emit only specific tool calls.\n * - `emitIntermediateState: IntermediateStateConfig[]?`\n * Lets you emit tool calls as streaming LangGraph state.\n */\n options?: OptionsConfig,\n): RunnableConfig {\n if (baseConfig && typeof baseConfig !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"baseConfig must be an object or null/undefined\",\n });\n }\n\n if (options && typeof options !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"options must be an object when provided\",\n });\n }\n\n // Validate emitIntermediateState structure\n if (options?.emitIntermediateState) {\n if (!Array.isArray(options.emitIntermediateState)) {\n throw new CopilotKitMisuseError({\n message: \"emitIntermediateState must be an array when provided\",\n });\n }\n\n options.emitIntermediateState.forEach((state, index) => {\n if (!state || typeof state !== \"object\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must be an object`,\n });\n }\n\n if (!state.stateKey || typeof state.stateKey !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must have a valid 'stateKey' string property`,\n });\n }\n\n if (!state.tool || typeof state.tool !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must have a valid 'tool' string property`,\n });\n }\n\n if (state.toolArgument && typeof state.toolArgument !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}].toolArgument must be a string when provided`,\n });\n }\n });\n }\n\n try {\n const metadata = baseConfig?.metadata || {};\n\n if (options?.emitAll) {\n metadata[\"copilotkit:emit-tool-calls\"] = true;\n metadata[\"copilotkit:emit-messages\"] = true;\n } else {\n if (options?.emitToolCalls !== undefined) {\n metadata[\"copilotkit:emit-tool-calls\"] = options.emitToolCalls;\n }\n if (options?.emitMessages !== undefined) {\n metadata[\"copilotkit:emit-messages\"] = options.emitMessages;\n }\n }\n\n if (options?.emitIntermediateState) {\n const snakeCaseIntermediateState = options.emitIntermediateState.map(\n (state) => ({\n tool: state.tool,\n tool_argument: state.toolArgument,\n state_key: state.stateKey,\n }),\n );\n\n metadata[\"copilotkit:emit-intermediate-state\"] =\n snakeCaseIntermediateState;\n }\n\n baseConfig = baseConfig || {};\n\n return {\n ...baseConfig,\n metadata: metadata,\n };\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to customize config: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Exits the current agent after the run completes. Calling copilotkitExit() will\n * not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after\n * the run completes.\n *\n * ### Examples\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import type { MessagesAnnotation } from \"@langchain/langgraph\";\n * import { copilotkitExit } from \"@copilotkit/sdk-js/langgraph\";\n *\n * type AgentState = typeof MessagesAnnotation.State;\n *\n * async function myNode(state: AgentState, config: RunnableConfig) {\n * await copilotkitExit(config);\n * return state;\n * }\n * ```\n */\nexport async function copilotkitExit(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitExit\",\n });\n }\n\n try {\n await dispatchCustomEvent(\"copilotkit_exit\", {}, config);\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to dispatch exit event: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Emits intermediate state to CopilotKit. Useful if you have a longer running node and you want to\n * update the user with the current state of the node.\n *\n * ### Examples\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { copilotkitEmitState } from \"@copilotkit/sdk-js/langgraph\";\n *\n * declare function someLongRunningOperation(step: number): Promise<void>;\n *\n * async function myNode(config: RunnableConfig) {\n * for (let i = 0; i < 10; i++) {\n * await someLongRunningOperation(i);\n * await copilotkitEmitState(config, { progress: i });\n * }\n * }\n * ```\n */\nexport async function copilotkitEmitState(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The state to emit.\n */\n state: any,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitState\",\n });\n }\n\n if (state === undefined) {\n throw new CopilotKitMisuseError({\n message: \"State is required for copilotkitEmitState\",\n });\n }\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_intermediate_state\",\n state,\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit state: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Manually emits a message to CopilotKit. Useful in longer running nodes to update the user.\n * Important: You still need to return the messages from the node.\n *\n * ### Examples\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { AIMessage } from \"@langchain/core/messages\";\n * import { copilotkitEmitMessage } from \"@copilotkit/sdk-js/langgraph\";\n *\n * async function myNode(config: RunnableConfig) {\n * const message = \"Step 1 of 10 complete\";\n * await copilotkitEmitMessage(config, message);\n *\n * // Return the message from the node\n * return {\n * messages: [new AIMessage({ content: message })],\n * };\n * }\n * ```\n */\nexport async function copilotkitEmitMessage(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The message to emit.\n */\n message: string,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitMessage\",\n });\n }\n\n if (!message || typeof message !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Message must be a non-empty string for copilotkitEmitMessage\",\n });\n }\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_message\",\n { message, message_id: randomId(), role: \"assistant\" },\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit message: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Manually emits a tool call to CopilotKit.\n *\n * ### Examples\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { copilotkitEmitToolCall } from \"@copilotkit/sdk-js/langgraph\";\n *\n * async function myNode(config: RunnableConfig) {\n * const autoId = await copilotkitEmitToolCall(config, \"SearchTool\", { steps: 10 });\n *\n * // With a custom ID for correlation/idempotency:\n * const customId = await copilotkitEmitToolCall(config, \"SearchTool\", { steps: 10 }, { toolCallId: \"my-custom-id\" });\n * }\n * ```\n *\n * @returns The tool call ID used for the emitted call. It equals `options.toolCallId`\n * when provided, otherwise a randomly generated ID.\n */\nexport async function copilotkitEmitToolCall(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The name of the tool to emit.\n */\n name: string,\n /**\n * The arguments to emit.\n */\n args: any,\n /**\n * Options for the tool call emission.\n */\n options?: {\n /**\n * Optional tool call ID. If not provided, a random ID is generated.\n * When provided, this ID is used as the toolCallId and parentMessageId\n * in AG-UI protocol events. The caller is responsible for ensuring uniqueness.\n */\n toolCallId?: string;\n },\n): Promise<string> {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitToolCall\",\n });\n }\n\n if (typeof name !== \"string\" || name.trim().length === 0) {\n throw new CopilotKitMisuseError({\n message:\n \"Tool name must be a non-empty string for copilotkitEmitToolCall\",\n });\n }\n\n if (\n options?.toolCallId !== undefined &&\n (typeof options.toolCallId !== \"string\" ||\n options.toolCallId.trim().length === 0)\n ) {\n throw new CopilotKitMisuseError({\n message:\n \"Tool call id must be a non-empty string when provided for copilotkitEmitToolCall\",\n });\n }\n\n if (args === undefined) {\n throw new CopilotKitMisuseError({\n message: \"Tool arguments are required for copilotkitEmitToolCall\",\n });\n }\n\n try {\n JSON.stringify(args);\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Tool arguments for '${name}' are not JSON-serializable: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n\n const toolCallId = options?.toolCallId ?? randomUUID();\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_tool_call\",\n { name, args, id: toolCallId },\n config,\n );\n } catch (error) {\n const wrapped = new Error(\n `copilotkitEmitToolCall dispatch failed for tool=\"${name}\" id=\"${toolCallId}\": ${error instanceof Error ? error.message : String(error)}`,\n );\n (wrapped as any).cause = error;\n throw wrapped;\n }\n\n return toolCallId;\n}\n\nexport function convertActionToDynamicStructuredTool(\n actionInput: any,\n): DynamicStructuredTool<any> {\n if (!actionInput) {\n throw new CopilotKitMisuseError({\n message: \"Action input is required but was not provided\",\n });\n }\n\n if (!actionInput.name || typeof actionInput.name !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Action must have a valid 'name' property of type string\",\n });\n }\n\n if (\n actionInput.description == undefined ||\n actionInput.description == null ||\n typeof actionInput.description !== \"string\"\n ) {\n throw new CopilotKitMisuseError({\n message: `Action '${actionInput.name}' must have a valid 'description' property of type string`,\n });\n }\n\n if (!actionInput.parameters) {\n throw new CopilotKitMisuseError({\n message: `Action '${actionInput.name}' must have a 'parameters' property`,\n });\n }\n\n try {\n return new DynamicStructuredTool({\n name: actionInput.name,\n description: actionInput.description,\n schema: convertJsonSchemaToZodSchema(actionInput.parameters, true),\n func: async () => {\n return \"\";\n },\n });\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to convert action '${actionInput.name}' to DynamicStructuredTool: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Use this function to convert a list of actions you get from state\n * to a list of dynamic structured tools.\n *\n * ### Examples\n *\n * ```typescript\n * import { convertActionsToDynamicStructuredTools } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);\n * ```\n */\nexport function convertActionsToDynamicStructuredTools(\n /**\n * The list of actions to convert.\n */\n actions: any[],\n): DynamicStructuredTool<any>[] {\n if (!Array.isArray(actions)) {\n throw new CopilotKitMisuseError({\n message: \"Actions must be an array\",\n });\n }\n\n return actions.map((action, index) => {\n try {\n return convertActionToDynamicStructuredTool(\n action.type === \"function\" ? action.function : action,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to convert action at index ${index}: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n });\n}\n\nexport function copilotKitInterrupt({\n message,\n action,\n args,\n}: {\n message?: string;\n action?: string;\n args?: Record<string, any>;\n}) {\n if (!message && !action) {\n throw new CopilotKitMisuseError({\n message:\n \"Either message or action (and optional arguments) must be provided for copilotKitInterrupt\",\n });\n }\n\n if (action && typeof action !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Action must be a string when provided to copilotKitInterrupt\",\n });\n }\n\n if (message && typeof message !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Message must be a string when provided to copilotKitInterrupt\",\n });\n }\n\n if (args && typeof args !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"Args must be an object when provided to copilotKitInterrupt\",\n });\n }\n\n let interruptValues = null;\n let interruptMessage = null;\n let answer = null;\n\n try {\n if (message) {\n interruptValues = message;\n interruptMessage = new AIMessage({ content: message, id: randomId() });\n } else {\n const toolId = randomId();\n interruptMessage = new AIMessage({\n content: \"\",\n tool_calls: [{ id: toolId, name: action, args: args ?? {} }],\n });\n interruptValues = {\n action,\n args: args ?? {},\n };\n }\n\n const response = interrupt({\n __copilotkit_interrupt_value__: interruptValues,\n __copilotkit_messages__: [interruptMessage],\n });\n answer = response[response.length - 1].content;\n\n return {\n answer,\n messages: response,\n };\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to create interrupt: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DA,SAAgB,0BAId,YAYA,SACgB;AAChB,KAAI,cAAc,OAAO,eAAe,SACtC,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,kDACV,CAAC;AAGJ,KAAI,WAAW,OAAO,YAAY,SAChC,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,2CACV,CAAC;AAIJ,KAAI,SAAS,uBAAuB;AAClC,MAAI,CAAC,MAAM,QAAQ,QAAQ,sBAAsB,CAC/C,OAAM,IAAIA,yCAAsB,EAC9B,SAAS,wDACV,CAAC;AAGJ,UAAQ,sBAAsB,SAAS,OAAO,UAAU;AACtD,OAAI,CAAC,SAAS,OAAO,UAAU,SAC7B,OAAM,IAAIA,yCAAsB,EAC9B,SAA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+BAA+B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,IAC/F,CAAC"}
|
|
@@ -6,7 +6,7 @@ import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
|
6
6
|
/**
|
|
7
7
|
* Customize the LangGraph configuration for use in CopilotKit.
|
|
8
8
|
*
|
|
9
|
-
* To the CopilotKit SDK, run:
|
|
9
|
+
* To install the CopilotKit SDK, run:
|
|
10
10
|
*
|
|
11
11
|
* ```bash
|
|
12
12
|
* npm install @copilotkit/sdk-js
|
|
@@ -17,13 +17,15 @@ import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
|
17
17
|
* Disable emitting messages and tool calls:
|
|
18
18
|
*
|
|
19
19
|
* ```typescript
|
|
20
|
-
* import {
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
20
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
21
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
22
|
+
*
|
|
23
|
+
* function customize(config: RunnableConfig) {
|
|
24
|
+
* return copilotkitCustomizeConfig(config, {
|
|
25
|
+
* emitMessages: false,
|
|
26
|
+
* emitToolCalls: false,
|
|
27
|
+
* });
|
|
28
|
+
* }
|
|
27
29
|
* ```
|
|
28
30
|
*
|
|
29
31
|
* To emit a tool call as streaming LangGraph state, pass the destination key in state,
|
|
@@ -31,18 +33,20 @@ import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
|
31
33
|
* all arguments are emitted under the state key.)
|
|
32
34
|
*
|
|
33
35
|
* ```typescript
|
|
34
|
-
* import {
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
36
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
37
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
38
|
+
*
|
|
39
|
+
* function customize(config: RunnableConfig) {
|
|
40
|
+
* return copilotkitCustomizeConfig(config, {
|
|
41
|
+
* emitIntermediateState: [
|
|
42
|
+
* {
|
|
43
|
+
* stateKey: "steps",
|
|
44
|
+
* tool: "SearchTool",
|
|
45
|
+
* toolArgument: "steps",
|
|
46
|
+
* },
|
|
47
|
+
* ],
|
|
48
|
+
* });
|
|
49
|
+
* }
|
|
46
50
|
* ```
|
|
47
51
|
*/
|
|
48
52
|
declare function copilotkitCustomizeConfig(
|
|
@@ -63,18 +67,23 @@ baseConfig: RunnableConfig,
|
|
|
63
67
|
*/
|
|
64
68
|
options?: OptionsConfig): RunnableConfig;
|
|
65
69
|
/**
|
|
66
|
-
* Exits the current agent after the run completes. Calling
|
|
70
|
+
* Exits the current agent after the run completes. Calling copilotkitExit() will
|
|
67
71
|
* not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
|
|
68
72
|
* the run completes.
|
|
69
73
|
*
|
|
70
74
|
* ### Examples
|
|
71
75
|
*
|
|
72
76
|
* ```typescript
|
|
73
|
-
* import {
|
|
77
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
78
|
+
* import type { MessagesAnnotation } from "@langchain/langgraph";
|
|
79
|
+
* import { copilotkitExit } from "@copilotkit/sdk-js/langgraph";
|
|
74
80
|
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
81
|
+
* type AgentState = typeof MessagesAnnotation.State;
|
|
82
|
+
*
|
|
83
|
+
* async function myNode(state: AgentState, config: RunnableConfig) {
|
|
84
|
+
* await copilotkitExit(config);
|
|
85
|
+
* return state;
|
|
86
|
+
* }
|
|
78
87
|
* ```
|
|
79
88
|
*/
|
|
80
89
|
declare function copilotkitExit(
|
|
@@ -89,11 +98,16 @@ config: RunnableConfig): Promise<void>;
|
|
|
89
98
|
* ### Examples
|
|
90
99
|
*
|
|
91
100
|
* ```typescript
|
|
92
|
-
* import {
|
|
101
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
102
|
+
* import { copilotkitEmitState } from "@copilotkit/sdk-js/langgraph";
|
|
93
103
|
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
104
|
+
* declare function someLongRunningOperation(step: number): Promise<void>;
|
|
105
|
+
*
|
|
106
|
+
* async function myNode(config: RunnableConfig) {
|
|
107
|
+
* for (let i = 0; i < 10; i++) {
|
|
108
|
+
* await someLongRunningOperation(i);
|
|
109
|
+
* await copilotkitEmitState(config, { progress: i });
|
|
110
|
+
* }
|
|
97
111
|
* }
|
|
98
112
|
* ```
|
|
99
113
|
*/
|
|
@@ -113,14 +127,18 @@ state: any): Promise<void>;
|
|
|
113
127
|
* ### Examples
|
|
114
128
|
*
|
|
115
129
|
* ```typescript
|
|
116
|
-
* import {
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
130
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
131
|
+
* import { AIMessage } from "@langchain/core/messages";
|
|
132
|
+
* import { copilotkitEmitMessage } from "@copilotkit/sdk-js/langgraph";
|
|
133
|
+
*
|
|
134
|
+
* async function myNode(config: RunnableConfig) {
|
|
135
|
+
* const message = "Step 1 of 10 complete";
|
|
136
|
+
* await copilotkitEmitMessage(config, message);
|
|
137
|
+
*
|
|
138
|
+
* // Return the message from the node
|
|
139
|
+
* return {
|
|
140
|
+
* messages: [new AIMessage({ content: message })],
|
|
141
|
+
* };
|
|
124
142
|
* }
|
|
125
143
|
* ```
|
|
126
144
|
*/
|
|
@@ -139,15 +157,18 @@ message: string): Promise<void>;
|
|
|
139
157
|
* ### Examples
|
|
140
158
|
*
|
|
141
159
|
* ```typescript
|
|
142
|
-
* import {
|
|
160
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
161
|
+
* import { copilotkitEmitToolCall } from "@copilotkit/sdk-js/langgraph";
|
|
143
162
|
*
|
|
144
|
-
*
|
|
163
|
+
* async function myNode(config: RunnableConfig) {
|
|
164
|
+
* const autoId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 });
|
|
145
165
|
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
166
|
+
* // With a custom ID for correlation/idempotency:
|
|
167
|
+
* const customId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 }, { toolCallId: "my-custom-id" });
|
|
168
|
+
* }
|
|
148
169
|
* ```
|
|
149
170
|
*
|
|
150
|
-
* @returns The tool call ID used for the emitted call
|
|
171
|
+
* @returns The tool call ID used for the emitted call. It equals `options.toolCallId`
|
|
151
172
|
* when provided, otherwise a randomly generated ID.
|
|
152
173
|
*/
|
|
153
174
|
declare function copilotkitEmitToolCall(
|
|
@@ -182,7 +203,7 @@ declare function convertActionToDynamicStructuredTool(actionInput: any): Dynamic
|
|
|
182
203
|
* ### Examples
|
|
183
204
|
*
|
|
184
205
|
* ```typescript
|
|
185
|
-
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js";
|
|
206
|
+
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js/langgraph";
|
|
186
207
|
*
|
|
187
208
|
* const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);
|
|
188
209
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.cts","names":[],"sources":["../../src/langgraph/utils.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"utils.d.cts","names":[],"sources":["../../src/langgraph/utils.ts"],"mappings":";;;;;;;AA2DA;;;;;;;;;;;;;;;AA6HA;;;;;;;;;AAwCA;;;;;;;;;;AAwDA;;;;;;;;;;iBA7NgB,yBAAA;;;;AAId,UAAA,EAAY,cAAA;;;;;;;;;AAiWd;;;AArVE,OAAA,GAAU,aAAA,GACT,cAAA;;AA8YH;;;;;AAyBA;;;;;;;;;;;;;;iBA3TsB,cAAA;;;;AAIpB,MAAA,EAAQ,cAAA,GAAc,OAAA;;;;;;;;;;;;;;;;;;;;;iBAoCF,mBAAA;;;;AAIpB,MAAA,EAAQ,cAAA;;;;AAIR,KAAA,QAAU,OAAA;;;;;;;;;;;;;;;;;;;;;;;iBAgDU,qBAAA;;;;AAIpB,MAAA,EAAQ,cAAA;;;;AAIR,OAAA,WAAe,OAAA;;;;;;;;;;;;;;;;;;;;;iBA8CK,sBAAA;;;;AAIpB,MAAA,EAAQ,cAAA;;;;AAIR,IAAA;;;;AAIA,IAAA;;;;AAIA,OAAA;;;;;;EAME,UAAA;AAAA,IAED,OAAA;AAAA,iBA0Da,oCAAA,CACd,WAAA,QACC,qBAAA;;;;;;;;;;;;;iBAwDa,sCAAA;;;;AAId,OAAA,UACC,qBAAA;AAAA,iBAoBa,mBAAA,CAAA;EACd,OAAA;EACA,MAAA;EACA;AAAA;EAEA,OAAA;EACA,MAAA;EACA,IAAA,GAAO,MAAA;AAAA"}
|
|
@@ -6,7 +6,7 @@ import { RunnableConfig } from "@langchain/core/runnables";
|
|
|
6
6
|
/**
|
|
7
7
|
* Customize the LangGraph configuration for use in CopilotKit.
|
|
8
8
|
*
|
|
9
|
-
* To the CopilotKit SDK, run:
|
|
9
|
+
* To install the CopilotKit SDK, run:
|
|
10
10
|
*
|
|
11
11
|
* ```bash
|
|
12
12
|
* npm install @copilotkit/sdk-js
|
|
@@ -17,13 +17,15 @@ import { RunnableConfig } from "@langchain/core/runnables";
|
|
|
17
17
|
* Disable emitting messages and tool calls:
|
|
18
18
|
*
|
|
19
19
|
* ```typescript
|
|
20
|
-
* import {
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
20
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
21
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
22
|
+
*
|
|
23
|
+
* function customize(config: RunnableConfig) {
|
|
24
|
+
* return copilotkitCustomizeConfig(config, {
|
|
25
|
+
* emitMessages: false,
|
|
26
|
+
* emitToolCalls: false,
|
|
27
|
+
* });
|
|
28
|
+
* }
|
|
27
29
|
* ```
|
|
28
30
|
*
|
|
29
31
|
* To emit a tool call as streaming LangGraph state, pass the destination key in state,
|
|
@@ -31,18 +33,20 @@ import { RunnableConfig } from "@langchain/core/runnables";
|
|
|
31
33
|
* all arguments are emitted under the state key.)
|
|
32
34
|
*
|
|
33
35
|
* ```typescript
|
|
34
|
-
* import {
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
36
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
37
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
38
|
+
*
|
|
39
|
+
* function customize(config: RunnableConfig) {
|
|
40
|
+
* return copilotkitCustomizeConfig(config, {
|
|
41
|
+
* emitIntermediateState: [
|
|
42
|
+
* {
|
|
43
|
+
* stateKey: "steps",
|
|
44
|
+
* tool: "SearchTool",
|
|
45
|
+
* toolArgument: "steps",
|
|
46
|
+
* },
|
|
47
|
+
* ],
|
|
48
|
+
* });
|
|
49
|
+
* }
|
|
46
50
|
* ```
|
|
47
51
|
*/
|
|
48
52
|
declare function copilotkitCustomizeConfig(
|
|
@@ -63,18 +67,23 @@ baseConfig: RunnableConfig,
|
|
|
63
67
|
*/
|
|
64
68
|
options?: OptionsConfig): RunnableConfig;
|
|
65
69
|
/**
|
|
66
|
-
* Exits the current agent after the run completes. Calling
|
|
70
|
+
* Exits the current agent after the run completes. Calling copilotkitExit() will
|
|
67
71
|
* not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
|
|
68
72
|
* the run completes.
|
|
69
73
|
*
|
|
70
74
|
* ### Examples
|
|
71
75
|
*
|
|
72
76
|
* ```typescript
|
|
73
|
-
* import {
|
|
77
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
78
|
+
* import type { MessagesAnnotation } from "@langchain/langgraph";
|
|
79
|
+
* import { copilotkitExit } from "@copilotkit/sdk-js/langgraph";
|
|
74
80
|
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
81
|
+
* type AgentState = typeof MessagesAnnotation.State;
|
|
82
|
+
*
|
|
83
|
+
* async function myNode(state: AgentState, config: RunnableConfig) {
|
|
84
|
+
* await copilotkitExit(config);
|
|
85
|
+
* return state;
|
|
86
|
+
* }
|
|
78
87
|
* ```
|
|
79
88
|
*/
|
|
80
89
|
declare function copilotkitExit(
|
|
@@ -89,11 +98,16 @@ config: RunnableConfig): Promise<void>;
|
|
|
89
98
|
* ### Examples
|
|
90
99
|
*
|
|
91
100
|
* ```typescript
|
|
92
|
-
* import {
|
|
101
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
102
|
+
* import { copilotkitEmitState } from "@copilotkit/sdk-js/langgraph";
|
|
93
103
|
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
104
|
+
* declare function someLongRunningOperation(step: number): Promise<void>;
|
|
105
|
+
*
|
|
106
|
+
* async function myNode(config: RunnableConfig) {
|
|
107
|
+
* for (let i = 0; i < 10; i++) {
|
|
108
|
+
* await someLongRunningOperation(i);
|
|
109
|
+
* await copilotkitEmitState(config, { progress: i });
|
|
110
|
+
* }
|
|
97
111
|
* }
|
|
98
112
|
* ```
|
|
99
113
|
*/
|
|
@@ -113,14 +127,18 @@ state: any): Promise<void>;
|
|
|
113
127
|
* ### Examples
|
|
114
128
|
*
|
|
115
129
|
* ```typescript
|
|
116
|
-
* import {
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
130
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
131
|
+
* import { AIMessage } from "@langchain/core/messages";
|
|
132
|
+
* import { copilotkitEmitMessage } from "@copilotkit/sdk-js/langgraph";
|
|
133
|
+
*
|
|
134
|
+
* async function myNode(config: RunnableConfig) {
|
|
135
|
+
* const message = "Step 1 of 10 complete";
|
|
136
|
+
* await copilotkitEmitMessage(config, message);
|
|
137
|
+
*
|
|
138
|
+
* // Return the message from the node
|
|
139
|
+
* return {
|
|
140
|
+
* messages: [new AIMessage({ content: message })],
|
|
141
|
+
* };
|
|
124
142
|
* }
|
|
125
143
|
* ```
|
|
126
144
|
*/
|
|
@@ -139,15 +157,18 @@ message: string): Promise<void>;
|
|
|
139
157
|
* ### Examples
|
|
140
158
|
*
|
|
141
159
|
* ```typescript
|
|
142
|
-
* import {
|
|
160
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
161
|
+
* import { copilotkitEmitToolCall } from "@copilotkit/sdk-js/langgraph";
|
|
143
162
|
*
|
|
144
|
-
*
|
|
163
|
+
* async function myNode(config: RunnableConfig) {
|
|
164
|
+
* const autoId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 });
|
|
145
165
|
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
166
|
+
* // With a custom ID for correlation/idempotency:
|
|
167
|
+
* const customId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 }, { toolCallId: "my-custom-id" });
|
|
168
|
+
* }
|
|
148
169
|
* ```
|
|
149
170
|
*
|
|
150
|
-
* @returns The tool call ID used for the emitted call
|
|
171
|
+
* @returns The tool call ID used for the emitted call. It equals `options.toolCallId`
|
|
151
172
|
* when provided, otherwise a randomly generated ID.
|
|
152
173
|
*/
|
|
153
174
|
declare function copilotkitEmitToolCall(
|
|
@@ -182,7 +203,7 @@ declare function convertActionToDynamicStructuredTool(actionInput: any): Dynamic
|
|
|
182
203
|
* ### Examples
|
|
183
204
|
*
|
|
184
205
|
* ```typescript
|
|
185
|
-
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js";
|
|
206
|
+
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js/langgraph";
|
|
186
207
|
*
|
|
187
208
|
* const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);
|
|
188
209
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.mts","names":[],"sources":["../../src/langgraph/utils.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"utils.d.mts","names":[],"sources":["../../src/langgraph/utils.ts"],"mappings":";;;;;;;AA2DA;;;;;;;;;;;;;;;AA6HA;;;;;;;;;AAwCA;;;;;;;;;;AAwDA;;;;;;;;;;iBA7NgB,yBAAA;;;;AAId,UAAA,EAAY,cAAA;;;;;;;;;AAiWd;;;AArVE,OAAA,GAAU,aAAA,GACT,cAAA;;AA8YH;;;;;AAyBA;;;;;;;;;;;;;;iBA3TsB,cAAA;;;;AAIpB,MAAA,EAAQ,cAAA,GAAc,OAAA;;;;;;;;;;;;;;;;;;;;;iBAoCF,mBAAA;;;;AAIpB,MAAA,EAAQ,cAAA;;;;AAIR,KAAA,QAAU,OAAA;;;;;;;;;;;;;;;;;;;;;;;iBAgDU,qBAAA;;;;AAIpB,MAAA,EAAQ,cAAA;;;;AAIR,OAAA,WAAe,OAAA;;;;;;;;;;;;;;;;;;;;;iBA8CK,sBAAA;;;;AAIpB,MAAA,EAAQ,cAAA;;;;AAIR,IAAA;;;;AAIA,IAAA;;;;AAIA,OAAA;;;;;;EAME,UAAA;AAAA,IAED,OAAA;AAAA,iBA0Da,oCAAA,CACd,WAAA,QACC,qBAAA;;;;;;;;;;;;;iBAwDa,sCAAA;;;;AAId,OAAA,UACC,qBAAA;AAAA,iBAoBa,mBAAA,CAAA;EACd,OAAA;EACA,MAAA;EACA;AAAA;EAEA,OAAA;EACA,MAAA;EACA,IAAA,GAAO,MAAA;AAAA"}
|
package/dist/langgraph/utils.mjs
CHANGED
|
@@ -9,7 +9,7 @@ import { AIMessage } from "@langchain/core/messages";
|
|
|
9
9
|
/**
|
|
10
10
|
* Customize the LangGraph configuration for use in CopilotKit.
|
|
11
11
|
*
|
|
12
|
-
* To the CopilotKit SDK, run:
|
|
12
|
+
* To install the CopilotKit SDK, run:
|
|
13
13
|
*
|
|
14
14
|
* ```bash
|
|
15
15
|
* npm install @copilotkit/sdk-js
|
|
@@ -20,13 +20,15 @@ import { AIMessage } from "@langchain/core/messages";
|
|
|
20
20
|
* Disable emitting messages and tool calls:
|
|
21
21
|
*
|
|
22
22
|
* ```typescript
|
|
23
|
-
* import {
|
|
23
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
24
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
24
25
|
*
|
|
25
|
-
* config
|
|
26
|
-
* config,
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
26
|
+
* function customize(config: RunnableConfig) {
|
|
27
|
+
* return copilotkitCustomizeConfig(config, {
|
|
28
|
+
* emitMessages: false,
|
|
29
|
+
* emitToolCalls: false,
|
|
30
|
+
* });
|
|
31
|
+
* }
|
|
30
32
|
* ```
|
|
31
33
|
*
|
|
32
34
|
* To emit a tool call as streaming LangGraph state, pass the destination key in state,
|
|
@@ -34,18 +36,20 @@ import { AIMessage } from "@langchain/core/messages";
|
|
|
34
36
|
* all arguments are emitted under the state key.)
|
|
35
37
|
*
|
|
36
38
|
* ```typescript
|
|
37
|
-
* import {
|
|
39
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
40
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
38
41
|
*
|
|
39
|
-
* config
|
|
40
|
-
* config,
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
42
|
+
* function customize(config: RunnableConfig) {
|
|
43
|
+
* return copilotkitCustomizeConfig(config, {
|
|
44
|
+
* emitIntermediateState: [
|
|
45
|
+
* {
|
|
46
|
+
* stateKey: "steps",
|
|
47
|
+
* tool: "SearchTool",
|
|
48
|
+
* toolArgument: "steps",
|
|
49
|
+
* },
|
|
50
|
+
* ],
|
|
51
|
+
* });
|
|
52
|
+
* }
|
|
49
53
|
* ```
|
|
50
54
|
*/
|
|
51
55
|
function copilotkitCustomizeConfig(baseConfig, options) {
|
|
@@ -84,18 +88,23 @@ function copilotkitCustomizeConfig(baseConfig, options) {
|
|
|
84
88
|
}
|
|
85
89
|
}
|
|
86
90
|
/**
|
|
87
|
-
* Exits the current agent after the run completes. Calling
|
|
91
|
+
* Exits the current agent after the run completes. Calling copilotkitExit() will
|
|
88
92
|
* not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
|
|
89
93
|
* the run completes.
|
|
90
94
|
*
|
|
91
95
|
* ### Examples
|
|
92
96
|
*
|
|
93
97
|
* ```typescript
|
|
94
|
-
* import {
|
|
98
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
99
|
+
* import type { MessagesAnnotation } from "@langchain/langgraph";
|
|
100
|
+
* import { copilotkitExit } from "@copilotkit/sdk-js/langgraph";
|
|
101
|
+
*
|
|
102
|
+
* type AgentState = typeof MessagesAnnotation.State;
|
|
95
103
|
*
|
|
96
|
-
* async function myNode(state:
|
|
97
|
-
* await copilotkitExit(config)
|
|
98
|
-
* return state
|
|
104
|
+
* async function myNode(state: AgentState, config: RunnableConfig) {
|
|
105
|
+
* await copilotkitExit(config);
|
|
106
|
+
* return state;
|
|
107
|
+
* }
|
|
99
108
|
* ```
|
|
100
109
|
*/
|
|
101
110
|
async function copilotkitExit(config) {
|
|
@@ -113,11 +122,16 @@ async function copilotkitExit(config) {
|
|
|
113
122
|
* ### Examples
|
|
114
123
|
*
|
|
115
124
|
* ```typescript
|
|
116
|
-
* import {
|
|
125
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
126
|
+
* import { copilotkitEmitState } from "@copilotkit/sdk-js/langgraph";
|
|
127
|
+
*
|
|
128
|
+
* declare function someLongRunningOperation(step: number): Promise<void>;
|
|
117
129
|
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
130
|
+
* async function myNode(config: RunnableConfig) {
|
|
131
|
+
* for (let i = 0; i < 10; i++) {
|
|
132
|
+
* await someLongRunningOperation(i);
|
|
133
|
+
* await copilotkitEmitState(config, { progress: i });
|
|
134
|
+
* }
|
|
121
135
|
* }
|
|
122
136
|
* ```
|
|
123
137
|
*/
|
|
@@ -137,14 +151,18 @@ async function copilotkitEmitState(config, state) {
|
|
|
137
151
|
* ### Examples
|
|
138
152
|
*
|
|
139
153
|
* ```typescript
|
|
140
|
-
* import {
|
|
154
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
155
|
+
* import { AIMessage } from "@langchain/core/messages";
|
|
156
|
+
* import { copilotkitEmitMessage } from "@copilotkit/sdk-js/langgraph";
|
|
141
157
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
158
|
+
* async function myNode(config: RunnableConfig) {
|
|
159
|
+
* const message = "Step 1 of 10 complete";
|
|
160
|
+
* await copilotkitEmitMessage(config, message);
|
|
144
161
|
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
162
|
+
* // Return the message from the node
|
|
163
|
+
* return {
|
|
164
|
+
* messages: [new AIMessage({ content: message })],
|
|
165
|
+
* };
|
|
148
166
|
* }
|
|
149
167
|
* ```
|
|
150
168
|
*/
|
|
@@ -167,15 +185,18 @@ async function copilotkitEmitMessage(config, message) {
|
|
|
167
185
|
* ### Examples
|
|
168
186
|
*
|
|
169
187
|
* ```typescript
|
|
170
|
-
* import {
|
|
188
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
189
|
+
* import { copilotkitEmitToolCall } from "@copilotkit/sdk-js/langgraph";
|
|
171
190
|
*
|
|
172
|
-
*
|
|
191
|
+
* async function myNode(config: RunnableConfig) {
|
|
192
|
+
* const autoId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 });
|
|
173
193
|
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
194
|
+
* // With a custom ID for correlation/idempotency:
|
|
195
|
+
* const customId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 }, { toolCallId: "my-custom-id" });
|
|
196
|
+
* }
|
|
176
197
|
* ```
|
|
177
198
|
*
|
|
178
|
-
* @returns The tool call ID used for the emitted call
|
|
199
|
+
* @returns The tool call ID used for the emitted call. It equals `options.toolCallId`
|
|
179
200
|
* when provided, otherwise a randomly generated ID.
|
|
180
201
|
*/
|
|
181
202
|
async function copilotkitEmitToolCall(config, name, args, options) {
|
|
@@ -227,7 +248,7 @@ function convertActionToDynamicStructuredTool(actionInput) {
|
|
|
227
248
|
* ### Examples
|
|
228
249
|
*
|
|
229
250
|
* ```typescript
|
|
230
|
-
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js";
|
|
251
|
+
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js/langgraph";
|
|
231
252
|
*
|
|
232
253
|
* const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);
|
|
233
254
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/langgraph/utils.ts"],"sourcesContent":["import type { RunnableConfig } from \"@langchain/core/runnables\";\nimport { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\nimport {\n randomId,\n randomUUID,\n CopilotKitMisuseError,\n} from \"@copilotkit/shared\";\nimport { interrupt } from \"@langchain/langgraph\";\nimport { DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { AIMessage } from \"@langchain/core/messages\";\nimport type { OptionsConfig } from \"./types\";\nimport { convertJsonSchemaToZodSchema } from \"./json-schema-to-zod\";\n\n/**\n * Customize the LangGraph configuration for use in CopilotKit.\n *\n * To the CopilotKit SDK, run:\n *\n * ```bash\n * npm install @copilotkit/sdk-js\n * ```\n *\n * ### Examples\n *\n * Disable emitting messages and tool calls:\n *\n * ```typescript\n * import { copilotkitCustomizeConfig } from \"@copilotkit/sdk-js\";\n *\n * config = copilotkitCustomizeConfig(\n * config,\n * emitMessages=false,\n * emitToolCalls=false\n * )\n * ```\n *\n * To emit a tool call as streaming LangGraph state, pass the destination key in state,\n * the tool name and optionally the tool argument. (If you don't pass the argument name,\n * all arguments are emitted under the state key.)\n *\n * ```typescript\n * import { copilotkitCustomizeConfig } from \"@copilotkit/sdk-js\";\n *\n * config = copilotkitCustomizeConfig(\n * config,\n * emitIntermediateState=[\n * {\n * \"stateKey\": \"steps\",\n * \"tool\": \"SearchTool\",\n * \"toolArgument\": \"steps\",\n * },\n * ],\n * )\n * ```\n */\nexport function copilotkitCustomizeConfig(\n /**\n * The LangChain/LangGraph configuration to customize.\n */\n baseConfig: RunnableConfig,\n /**\n * Configuration options:\n * - `emitMessages: boolean?`\n * Configure how messages are emitted. By default, all messages are emitted. Pass false to\n * disable emitting messages.\n * - `emitToolCalls: boolean | string | string[]?`\n * Configure how tool calls are emitted. By default, all tool calls are emitted. Pass false to\n * disable emitting tool calls. Pass a string or list of strings to emit only specific tool calls.\n * - `emitIntermediateState: IntermediateStateConfig[]?`\n * Lets you emit tool calls as streaming LangGraph state.\n */\n options?: OptionsConfig,\n): RunnableConfig {\n if (baseConfig && typeof baseConfig !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"baseConfig must be an object or null/undefined\",\n });\n }\n\n if (options && typeof options !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"options must be an object when provided\",\n });\n }\n\n // Validate emitIntermediateState structure\n if (options?.emitIntermediateState) {\n if (!Array.isArray(options.emitIntermediateState)) {\n throw new CopilotKitMisuseError({\n message: \"emitIntermediateState must be an array when provided\",\n });\n }\n\n options.emitIntermediateState.forEach((state, index) => {\n if (!state || typeof state !== \"object\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must be an object`,\n });\n }\n\n if (!state.stateKey || typeof state.stateKey !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must have a valid 'stateKey' string property`,\n });\n }\n\n if (!state.tool || typeof state.tool !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must have a valid 'tool' string property`,\n });\n }\n\n if (state.toolArgument && typeof state.toolArgument !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}].toolArgument must be a string when provided`,\n });\n }\n });\n }\n\n try {\n const metadata = baseConfig?.metadata || {};\n\n if (options?.emitAll) {\n metadata[\"copilotkit:emit-tool-calls\"] = true;\n metadata[\"copilotkit:emit-messages\"] = true;\n } else {\n if (options?.emitToolCalls !== undefined) {\n metadata[\"copilotkit:emit-tool-calls\"] = options.emitToolCalls;\n }\n if (options?.emitMessages !== undefined) {\n metadata[\"copilotkit:emit-messages\"] = options.emitMessages;\n }\n }\n\n if (options?.emitIntermediateState) {\n const snakeCaseIntermediateState = options.emitIntermediateState.map(\n (state) => ({\n tool: state.tool,\n tool_argument: state.toolArgument,\n state_key: state.stateKey,\n }),\n );\n\n metadata[\"copilotkit:emit-intermediate-state\"] =\n snakeCaseIntermediateState;\n }\n\n baseConfig = baseConfig || {};\n\n return {\n ...baseConfig,\n metadata: metadata,\n };\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to customize config: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Exits the current agent after the run completes. Calling copilotkit_exit() will\n * not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after\n * the run completes.\n *\n * ### Examples\n *\n * ```typescript\n * import { copilotkitExit } from \"@copilotkit/sdk-js\";\n *\n * async function myNode(state: Any):\n * await copilotkitExit(config)\n * return state\n * ```\n */\nexport async function copilotkitExit(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitExit\",\n });\n }\n\n try {\n await dispatchCustomEvent(\"copilotkit_exit\", {}, config);\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to dispatch exit event: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Emits intermediate state to CopilotKit. Useful if you have a longer running node and you want to\n * update the user with the current state of the node.\n *\n * ### Examples\n *\n * ```typescript\n * import { copilotkitEmitState } from \"@copilotkit/sdk-js\";\n *\n * for (let i = 0; i < 10; i++) {\n * await someLongRunningOperation(i);\n * await copilotkitEmitState(config, { progress: i });\n * }\n * ```\n */\nexport async function copilotkitEmitState(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The state to emit.\n */\n state: any,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitState\",\n });\n }\n\n if (state === undefined) {\n throw new CopilotKitMisuseError({\n message: \"State is required for copilotkitEmitState\",\n });\n }\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_intermediate_state\",\n state,\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit state: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Manually emits a message to CopilotKit. Useful in longer running nodes to update the user.\n * Important: You still need to return the messages from the node.\n *\n * ### Examples\n *\n * ```typescript\n * import { copilotkitEmitMessage } from \"@copilotkit/sdk-js\";\n *\n * const message = \"Step 1 of 10 complete\";\n * await copilotkitEmitMessage(config, message);\n *\n * // Return the message from the node\n * return {\n * \"messages\": [AIMessage(content=message)]\n * }\n * ```\n */\nexport async function copilotkitEmitMessage(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The message to emit.\n */\n message: string,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitMessage\",\n });\n }\n\n if (!message || typeof message !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Message must be a non-empty string for copilotkitEmitMessage\",\n });\n }\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_message\",\n { message, message_id: randomId(), role: \"assistant\" },\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit message: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Manually emits a tool call to CopilotKit.\n *\n * ### Examples\n *\n * ```typescript\n * import { copilotkitEmitToolCall } from \"@copilotkit/sdk-js\";\n *\n * const autoId = await copilotkitEmitToolCall(config, \"SearchTool\", { steps: 10 });\n *\n * // With a custom ID for correlation/idempotency:\n * const customId = await copilotkitEmitToolCall(config, \"SearchTool\", { steps: 10 }, { toolCallId: \"my-custom-id\" });\n * ```\n *\n * @returns The tool call ID used for the emitted call — equals `options.toolCallId`\n * when provided, otherwise a randomly generated ID.\n */\nexport async function copilotkitEmitToolCall(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The name of the tool to emit.\n */\n name: string,\n /**\n * The arguments to emit.\n */\n args: any,\n /**\n * Options for the tool call emission.\n */\n options?: {\n /**\n * Optional tool call ID. If not provided, a random ID is generated.\n * When provided, this ID is used as the toolCallId and parentMessageId\n * in AG-UI protocol events. The caller is responsible for ensuring uniqueness.\n */\n toolCallId?: string;\n },\n): Promise<string> {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitToolCall\",\n });\n }\n\n if (typeof name !== \"string\" || name.trim().length === 0) {\n throw new CopilotKitMisuseError({\n message:\n \"Tool name must be a non-empty string for copilotkitEmitToolCall\",\n });\n }\n\n if (\n options?.toolCallId !== undefined &&\n (typeof options.toolCallId !== \"string\" ||\n options.toolCallId.trim().length === 0)\n ) {\n throw new CopilotKitMisuseError({\n message:\n \"Tool call id must be a non-empty string when provided for copilotkitEmitToolCall\",\n });\n }\n\n if (args === undefined) {\n throw new CopilotKitMisuseError({\n message: \"Tool arguments are required for copilotkitEmitToolCall\",\n });\n }\n\n try {\n JSON.stringify(args);\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Tool arguments for '${name}' are not JSON-serializable: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n\n const toolCallId = options?.toolCallId ?? randomUUID();\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_tool_call\",\n { name, args, id: toolCallId },\n config,\n );\n } catch (error) {\n const wrapped = new Error(\n `copilotkitEmitToolCall dispatch failed for tool=\"${name}\" id=\"${toolCallId}\": ${error instanceof Error ? error.message : String(error)}`,\n );\n (wrapped as any).cause = error;\n throw wrapped;\n }\n\n return toolCallId;\n}\n\nexport function convertActionToDynamicStructuredTool(\n actionInput: any,\n): DynamicStructuredTool<any> {\n if (!actionInput) {\n throw new CopilotKitMisuseError({\n message: \"Action input is required but was not provided\",\n });\n }\n\n if (!actionInput.name || typeof actionInput.name !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Action must have a valid 'name' property of type string\",\n });\n }\n\n if (\n actionInput.description == undefined ||\n actionInput.description == null ||\n typeof actionInput.description !== \"string\"\n ) {\n throw new CopilotKitMisuseError({\n message: `Action '${actionInput.name}' must have a valid 'description' property of type string`,\n });\n }\n\n if (!actionInput.parameters) {\n throw new CopilotKitMisuseError({\n message: `Action '${actionInput.name}' must have a 'parameters' property`,\n });\n }\n\n try {\n return new DynamicStructuredTool({\n name: actionInput.name,\n description: actionInput.description,\n schema: convertJsonSchemaToZodSchema(actionInput.parameters, true),\n func: async () => {\n return \"\";\n },\n });\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to convert action '${actionInput.name}' to DynamicStructuredTool: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Use this function to convert a list of actions you get from state\n * to a list of dynamic structured tools.\n *\n * ### Examples\n *\n * ```typescript\n * import { convertActionsToDynamicStructuredTools } from \"@copilotkit/sdk-js\";\n *\n * const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);\n * ```\n */\nexport function convertActionsToDynamicStructuredTools(\n /**\n * The list of actions to convert.\n */\n actions: any[],\n): DynamicStructuredTool<any>[] {\n if (!Array.isArray(actions)) {\n throw new CopilotKitMisuseError({\n message: \"Actions must be an array\",\n });\n }\n\n return actions.map((action, index) => {\n try {\n return convertActionToDynamicStructuredTool(\n action.type === \"function\" ? action.function : action,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to convert action at index ${index}: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n });\n}\n\nexport function copilotKitInterrupt({\n message,\n action,\n args,\n}: {\n message?: string;\n action?: string;\n args?: Record<string, any>;\n}) {\n if (!message && !action) {\n throw new CopilotKitMisuseError({\n message:\n \"Either message or action (and optional arguments) must be provided for copilotKitInterrupt\",\n });\n }\n\n if (action && typeof action !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Action must be a string when provided to copilotKitInterrupt\",\n });\n }\n\n if (message && typeof message !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Message must be a string when provided to copilotKitInterrupt\",\n });\n }\n\n if (args && typeof args !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"Args must be an object when provided to copilotKitInterrupt\",\n });\n }\n\n let interruptValues = null;\n let interruptMessage = null;\n let answer = null;\n\n try {\n if (message) {\n interruptValues = message;\n interruptMessage = new AIMessage({ content: message, id: randomId() });\n } else {\n const toolId = randomId();\n interruptMessage = new AIMessage({\n content: \"\",\n tool_calls: [{ id: toolId, name: action, args: args ?? {} }],\n });\n interruptValues = {\n action,\n args: args ?? {},\n };\n }\n\n const response = interrupt({\n __copilotkit_interrupt_value__: interruptValues,\n __copilotkit_messages__: [interruptMessage],\n });\n answer = response[response.length - 1].content;\n\n return {\n answer,\n messages: response,\n };\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to create interrupt: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;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B;AAClB,sBAAmB,IAAI,UAAU;IAAE,SAAS;IAAS,IAAI,UAAU;IAAE,CAAC;SACjE;AAEL,sBAAmB,IAAI,UAAU;IAC/B,SAAS;IACT,YAAY,CAAC;KAAE,IAHF,UAAU;KAGI,MAAM;KAAQ,MAAM,QAAQ,EAAE;KAAE,CAAC;IAC7D,CAAC;AACF,qBAAkB;IAChB;IACA,MAAM,QAAQ,EAAE;IACjB;;EAGH,MAAM,WAAW,UAAU;GACzB,gCAAgC;GAChC,yBAAyB,CAAC,iBAAiB;GAC5C,CAAC;AACF,WAAS,SAAS,SAAS,SAAS,GAAG;AAEvC,SAAO;GACL;GACA,UAAU;GACX;UACM,OAAO;AACd,QAAM,IAAI,sBAAsB,EAC9B,SAAS,+BAA+B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,IAC/F,CAAC"}
|
|
1
|
+
{"version":3,"file":"utils.mjs","names":[],"sources":["../../src/langgraph/utils.ts"],"sourcesContent":["import type { RunnableConfig } from \"@langchain/core/runnables\";\nimport { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\nimport {\n randomId,\n randomUUID,\n CopilotKitMisuseError,\n} from \"@copilotkit/shared\";\nimport { interrupt } from \"@langchain/langgraph\";\nimport { DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { AIMessage } from \"@langchain/core/messages\";\nimport type { OptionsConfig } from \"./types\";\nimport { convertJsonSchemaToZodSchema } from \"./json-schema-to-zod\";\n\n/**\n * Customize the LangGraph configuration for use in CopilotKit.\n *\n * To install the CopilotKit SDK, run:\n *\n * ```bash\n * npm install @copilotkit/sdk-js\n * ```\n *\n * ### Examples\n *\n * Disable emitting messages and tool calls:\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { copilotkitCustomizeConfig } from \"@copilotkit/sdk-js/langgraph\";\n *\n * function customize(config: RunnableConfig) {\n * return copilotkitCustomizeConfig(config, {\n * emitMessages: false,\n * emitToolCalls: false,\n * });\n * }\n * ```\n *\n * To emit a tool call as streaming LangGraph state, pass the destination key in state,\n * the tool name and optionally the tool argument. (If you don't pass the argument name,\n * all arguments are emitted under the state key.)\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { copilotkitCustomizeConfig } from \"@copilotkit/sdk-js/langgraph\";\n *\n * function customize(config: RunnableConfig) {\n * return copilotkitCustomizeConfig(config, {\n * emitIntermediateState: [\n * {\n * stateKey: \"steps\",\n * tool: \"SearchTool\",\n * toolArgument: \"steps\",\n * },\n * ],\n * });\n * }\n * ```\n */\nexport function copilotkitCustomizeConfig(\n /**\n * The LangChain/LangGraph configuration to customize.\n */\n baseConfig: RunnableConfig,\n /**\n * Configuration options:\n * - `emitMessages: boolean?`\n * Configure how messages are emitted. By default, all messages are emitted. Pass false to\n * disable emitting messages.\n * - `emitToolCalls: boolean | string | string[]?`\n * Configure how tool calls are emitted. By default, all tool calls are emitted. Pass false to\n * disable emitting tool calls. Pass a string or list of strings to emit only specific tool calls.\n * - `emitIntermediateState: IntermediateStateConfig[]?`\n * Lets you emit tool calls as streaming LangGraph state.\n */\n options?: OptionsConfig,\n): RunnableConfig {\n if (baseConfig && typeof baseConfig !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"baseConfig must be an object or null/undefined\",\n });\n }\n\n if (options && typeof options !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"options must be an object when provided\",\n });\n }\n\n // Validate emitIntermediateState structure\n if (options?.emitIntermediateState) {\n if (!Array.isArray(options.emitIntermediateState)) {\n throw new CopilotKitMisuseError({\n message: \"emitIntermediateState must be an array when provided\",\n });\n }\n\n options.emitIntermediateState.forEach((state, index) => {\n if (!state || typeof state !== \"object\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must be an object`,\n });\n }\n\n if (!state.stateKey || typeof state.stateKey !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must have a valid 'stateKey' string property`,\n });\n }\n\n if (!state.tool || typeof state.tool !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}] must have a valid 'tool' string property`,\n });\n }\n\n if (state.toolArgument && typeof state.toolArgument !== \"string\") {\n throw new CopilotKitMisuseError({\n message: `emitIntermediateState[${index}].toolArgument must be a string when provided`,\n });\n }\n });\n }\n\n try {\n const metadata = baseConfig?.metadata || {};\n\n if (options?.emitAll) {\n metadata[\"copilotkit:emit-tool-calls\"] = true;\n metadata[\"copilotkit:emit-messages\"] = true;\n } else {\n if (options?.emitToolCalls !== undefined) {\n metadata[\"copilotkit:emit-tool-calls\"] = options.emitToolCalls;\n }\n if (options?.emitMessages !== undefined) {\n metadata[\"copilotkit:emit-messages\"] = options.emitMessages;\n }\n }\n\n if (options?.emitIntermediateState) {\n const snakeCaseIntermediateState = options.emitIntermediateState.map(\n (state) => ({\n tool: state.tool,\n tool_argument: state.toolArgument,\n state_key: state.stateKey,\n }),\n );\n\n metadata[\"copilotkit:emit-intermediate-state\"] =\n snakeCaseIntermediateState;\n }\n\n baseConfig = baseConfig || {};\n\n return {\n ...baseConfig,\n metadata: metadata,\n };\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to customize config: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Exits the current agent after the run completes. Calling copilotkitExit() will\n * not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after\n * the run completes.\n *\n * ### Examples\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import type { MessagesAnnotation } from \"@langchain/langgraph\";\n * import { copilotkitExit } from \"@copilotkit/sdk-js/langgraph\";\n *\n * type AgentState = typeof MessagesAnnotation.State;\n *\n * async function myNode(state: AgentState, config: RunnableConfig) {\n * await copilotkitExit(config);\n * return state;\n * }\n * ```\n */\nexport async function copilotkitExit(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitExit\",\n });\n }\n\n try {\n await dispatchCustomEvent(\"copilotkit_exit\", {}, config);\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to dispatch exit event: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Emits intermediate state to CopilotKit. Useful if you have a longer running node and you want to\n * update the user with the current state of the node.\n *\n * ### Examples\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { copilotkitEmitState } from \"@copilotkit/sdk-js/langgraph\";\n *\n * declare function someLongRunningOperation(step: number): Promise<void>;\n *\n * async function myNode(config: RunnableConfig) {\n * for (let i = 0; i < 10; i++) {\n * await someLongRunningOperation(i);\n * await copilotkitEmitState(config, { progress: i });\n * }\n * }\n * ```\n */\nexport async function copilotkitEmitState(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The state to emit.\n */\n state: any,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitState\",\n });\n }\n\n if (state === undefined) {\n throw new CopilotKitMisuseError({\n message: \"State is required for copilotkitEmitState\",\n });\n }\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_intermediate_state\",\n state,\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit state: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Manually emits a message to CopilotKit. Useful in longer running nodes to update the user.\n * Important: You still need to return the messages from the node.\n *\n * ### Examples\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { AIMessage } from \"@langchain/core/messages\";\n * import { copilotkitEmitMessage } from \"@copilotkit/sdk-js/langgraph\";\n *\n * async function myNode(config: RunnableConfig) {\n * const message = \"Step 1 of 10 complete\";\n * await copilotkitEmitMessage(config, message);\n *\n * // Return the message from the node\n * return {\n * messages: [new AIMessage({ content: message })],\n * };\n * }\n * ```\n */\nexport async function copilotkitEmitMessage(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The message to emit.\n */\n message: string,\n) {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitMessage\",\n });\n }\n\n if (!message || typeof message !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Message must be a non-empty string for copilotkitEmitMessage\",\n });\n }\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_message\",\n { message, message_id: randomId(), role: \"assistant\" },\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit message: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Manually emits a tool call to CopilotKit.\n *\n * ### Examples\n *\n * ```typescript\n * import type { RunnableConfig } from \"@langchain/core/runnables\";\n * import { copilotkitEmitToolCall } from \"@copilotkit/sdk-js/langgraph\";\n *\n * async function myNode(config: RunnableConfig) {\n * const autoId = await copilotkitEmitToolCall(config, \"SearchTool\", { steps: 10 });\n *\n * // With a custom ID for correlation/idempotency:\n * const customId = await copilotkitEmitToolCall(config, \"SearchTool\", { steps: 10 }, { toolCallId: \"my-custom-id\" });\n * }\n * ```\n *\n * @returns The tool call ID used for the emitted call. It equals `options.toolCallId`\n * when provided, otherwise a randomly generated ID.\n */\nexport async function copilotkitEmitToolCall(\n /**\n * The LangChain/LangGraph configuration.\n */\n config: RunnableConfig,\n /**\n * The name of the tool to emit.\n */\n name: string,\n /**\n * The arguments to emit.\n */\n args: any,\n /**\n * Options for the tool call emission.\n */\n options?: {\n /**\n * Optional tool call ID. If not provided, a random ID is generated.\n * When provided, this ID is used as the toolCallId and parentMessageId\n * in AG-UI protocol events. The caller is responsible for ensuring uniqueness.\n */\n toolCallId?: string;\n },\n): Promise<string> {\n if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitToolCall\",\n });\n }\n\n if (typeof name !== \"string\" || name.trim().length === 0) {\n throw new CopilotKitMisuseError({\n message:\n \"Tool name must be a non-empty string for copilotkitEmitToolCall\",\n });\n }\n\n if (\n options?.toolCallId !== undefined &&\n (typeof options.toolCallId !== \"string\" ||\n options.toolCallId.trim().length === 0)\n ) {\n throw new CopilotKitMisuseError({\n message:\n \"Tool call id must be a non-empty string when provided for copilotkitEmitToolCall\",\n });\n }\n\n if (args === undefined) {\n throw new CopilotKitMisuseError({\n message: \"Tool arguments are required for copilotkitEmitToolCall\",\n });\n }\n\n try {\n JSON.stringify(args);\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Tool arguments for '${name}' are not JSON-serializable: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n\n const toolCallId = options?.toolCallId ?? randomUUID();\n\n try {\n await dispatchCustomEvent(\n \"copilotkit_manually_emit_tool_call\",\n { name, args, id: toolCallId },\n config,\n );\n } catch (error) {\n const wrapped = new Error(\n `copilotkitEmitToolCall dispatch failed for tool=\"${name}\" id=\"${toolCallId}\": ${error instanceof Error ? error.message : String(error)}`,\n );\n (wrapped as any).cause = error;\n throw wrapped;\n }\n\n return toolCallId;\n}\n\nexport function convertActionToDynamicStructuredTool(\n actionInput: any,\n): DynamicStructuredTool<any> {\n if (!actionInput) {\n throw new CopilotKitMisuseError({\n message: \"Action input is required but was not provided\",\n });\n }\n\n if (!actionInput.name || typeof actionInput.name !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Action must have a valid 'name' property of type string\",\n });\n }\n\n if (\n actionInput.description == undefined ||\n actionInput.description == null ||\n typeof actionInput.description !== \"string\"\n ) {\n throw new CopilotKitMisuseError({\n message: `Action '${actionInput.name}' must have a valid 'description' property of type string`,\n });\n }\n\n if (!actionInput.parameters) {\n throw new CopilotKitMisuseError({\n message: `Action '${actionInput.name}' must have a 'parameters' property`,\n });\n }\n\n try {\n return new DynamicStructuredTool({\n name: actionInput.name,\n description: actionInput.description,\n schema: convertJsonSchemaToZodSchema(actionInput.parameters, true),\n func: async () => {\n return \"\";\n },\n });\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to convert action '${actionInput.name}' to DynamicStructuredTool: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n/**\n * Use this function to convert a list of actions you get from state\n * to a list of dynamic structured tools.\n *\n * ### Examples\n *\n * ```typescript\n * import { convertActionsToDynamicStructuredTools } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);\n * ```\n */\nexport function convertActionsToDynamicStructuredTools(\n /**\n * The list of actions to convert.\n */\n actions: any[],\n): DynamicStructuredTool<any>[] {\n if (!Array.isArray(actions)) {\n throw new CopilotKitMisuseError({\n message: \"Actions must be an array\",\n });\n }\n\n return actions.map((action, index) => {\n try {\n return convertActionToDynamicStructuredTool(\n action.type === \"function\" ? action.function : action,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to convert action at index ${index}: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n });\n}\n\nexport function copilotKitInterrupt({\n message,\n action,\n args,\n}: {\n message?: string;\n action?: string;\n args?: Record<string, any>;\n}) {\n if (!message && !action) {\n throw new CopilotKitMisuseError({\n message:\n \"Either message or action (and optional arguments) must be provided for copilotKitInterrupt\",\n });\n }\n\n if (action && typeof action !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Action must be a string when provided to copilotKitInterrupt\",\n });\n }\n\n if (message && typeof message !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Message must be a string when provided to copilotKitInterrupt\",\n });\n }\n\n if (args && typeof args !== \"object\") {\n throw new CopilotKitMisuseError({\n message: \"Args must be an object when provided to copilotKitInterrupt\",\n });\n }\n\n let interruptValues = null;\n let interruptMessage = null;\n let answer = null;\n\n try {\n if (message) {\n interruptValues = message;\n interruptMessage = new AIMessage({ content: message, id: randomId() });\n } else {\n const toolId = randomId();\n interruptMessage = new AIMessage({\n content: \"\",\n tool_calls: [{ id: toolId, name: action, args: args ?? {} }],\n });\n interruptValues = {\n action,\n args: args ?? {},\n };\n }\n\n const response = interrupt({\n __copilotkit_interrupt_value__: interruptValues,\n __copilotkit_messages__: [interruptMessage],\n });\n answer = response[response.length - 1].content;\n\n return {\n answer,\n messages: response,\n };\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to create interrupt: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DA,SAAgB,0BAId,YAYA,SACgB;AAChB,KAAI,cAAc,OAAO,eAAe,SACtC,OAAM,IAAI,sBAAsB,EAC9B,SAAS,kDACV,CAAC;AAGJ,KAAI,WAAW,OAAO,YAAY,SAChC,OAAM,IAAI,sBAAsB,EAC9B,SAAS,2CACV,CAAC;AAIJ,KAAI,SAAS,uBAAuB;AAClC,MAAI,CAAC,MAAM,QAAQ,QAAQ,sBAAsB,CAC/C,OAAM,IAAI,sBAAsB,EAC9B,SAAS,wDACV,CAAC;AAGJ,UAAQ,sBAAsB,SAAS,OAAO,UAAU;AACtD,OAAI,CAAC,SAAS,OAAO,UAAU,SAC7B,OAAM,IAAI,sBAAsB,EAC9B,SAAS,yBAAyB,MAAM,sBACzC,CAAC;AAGJ,OAAI,CAAC,MAAM,YAAY,OAAO,MAAM,aAAa,SAC/C,OAAM,IAAI,sBAAsB,EAC9B,SAAS,yBAAyB,MAAM,iDACzC,CAAC;AAGJ,OAAI,CAAC,MAAM,QAAQ,OAAO,MAAM,SAAS,SACvC,OAAM,IAAI,sBAAsB,EAC9B,SAAS,yBAAyB,MAAM,6CACzC,CAAC;AAGJ,OAAI,MAAM,gBAAgB,OAAO,MAAM,iBAAiB,SACtD,OAAM,IAAI,sBAAsB,EAC9B,SAAS,yBAAyB,MAAM,gDACzC,CAAC;IAEJ;;AAGJ,KAAI;EACF,MAAM,WAAW,YAAY,YAAY,EAAE;AAE3C,MAAI,SAAS,SAAS;AACpB,YAAS,gCAAgC;AACzC,YAAS,8BAA8B;SAClC;AACL,OAAI,S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W,OAAO,YAAY,SAChC,OAAM,IAAI,sBAAsB,EAC9B,SAAS,iEACV,CAAC;AAGJ,KAAI,QAAQ,OAAO,SAAS,SAC1B,OAAM,IAAI,sBAAsB,EAC9B,SAAS,+DACV,CAAC;CAGJ,IAAI,kBAAkB;CACtB,IAAI,mBAAmB;CACvB,IAAI,SAAS;AAEb,KAAI;AACF,MAAI,SAAS;AACX,qBAAkB;AAClB,sBAAmB,IAAI,UAAU;IAAE,SAAS;IAAS,IAAI,UAAU;IAAE,CAAC;SACjE;AAEL,sBAAmB,IAAI,UAAU;IAC/B,SAAS;IACT,YAAY,CAAC;KAAE,IAHF,UAAU;KAGI,MAAM;KAAQ,MAAM,QAAQ,EAAE;KAAE,CAAC;IAC7D,CAAC;AACF,qBAAkB;IAChB;IACA,MAAM,QAAQ,EAAE;IACjB;;EAGH,MAAM,WAAW,UAAU;GACzB,gCAAgC;GAChC,yBAAyB,CAAC,iBAAiB;GAC5C,CAAC;AACF,WAAS,SAAS,SAAS,SAAS,GAAG;AAEvC,SAAO;GACL;GACA,UAAU;GACX;UACM,OAAO;AACd,QAAM,IAAI,sBAAsB,EAC9B,SAAS,+BAA+B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,IAC/F,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@copilotkit/sdk-js",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.43",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
67
|
"@ag-ui/langgraph": "0.0.44",
|
|
68
|
-
"@copilotkit/shared": "2.0.0-rc.
|
|
68
|
+
"@copilotkit/shared": "2.0.0-rc.43",
|
|
69
69
|
"@standard-schema/spec": "^1.0.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
package/src/langgraph/utils.ts
CHANGED
|
@@ -14,7 +14,7 @@ import { convertJsonSchemaToZodSchema } from "./json-schema-to-zod";
|
|
|
14
14
|
/**
|
|
15
15
|
* Customize the LangGraph configuration for use in CopilotKit.
|
|
16
16
|
*
|
|
17
|
-
* To the CopilotKit SDK, run:
|
|
17
|
+
* To install the CopilotKit SDK, run:
|
|
18
18
|
*
|
|
19
19
|
* ```bash
|
|
20
20
|
* npm install @copilotkit/sdk-js
|
|
@@ -25,13 +25,15 @@ import { convertJsonSchemaToZodSchema } from "./json-schema-to-zod";
|
|
|
25
25
|
* Disable emitting messages and tool calls:
|
|
26
26
|
*
|
|
27
27
|
* ```typescript
|
|
28
|
-
* import {
|
|
28
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
29
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
29
30
|
*
|
|
30
|
-
* config
|
|
31
|
-
* config,
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
31
|
+
* function customize(config: RunnableConfig) {
|
|
32
|
+
* return copilotkitCustomizeConfig(config, {
|
|
33
|
+
* emitMessages: false,
|
|
34
|
+
* emitToolCalls: false,
|
|
35
|
+
* });
|
|
36
|
+
* }
|
|
35
37
|
* ```
|
|
36
38
|
*
|
|
37
39
|
* To emit a tool call as streaming LangGraph state, pass the destination key in state,
|
|
@@ -39,18 +41,20 @@ import { convertJsonSchemaToZodSchema } from "./json-schema-to-zod";
|
|
|
39
41
|
* all arguments are emitted under the state key.)
|
|
40
42
|
*
|
|
41
43
|
* ```typescript
|
|
42
|
-
* import {
|
|
44
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
45
|
+
* import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js/langgraph";
|
|
43
46
|
*
|
|
44
|
-
* config
|
|
45
|
-
* config,
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
47
|
+
* function customize(config: RunnableConfig) {
|
|
48
|
+
* return copilotkitCustomizeConfig(config, {
|
|
49
|
+
* emitIntermediateState: [
|
|
50
|
+
* {
|
|
51
|
+
* stateKey: "steps",
|
|
52
|
+
* tool: "SearchTool",
|
|
53
|
+
* toolArgument: "steps",
|
|
54
|
+
* },
|
|
55
|
+
* ],
|
|
56
|
+
* });
|
|
57
|
+
* }
|
|
54
58
|
* ```
|
|
55
59
|
*/
|
|
56
60
|
export function copilotkitCustomizeConfig(
|
|
@@ -159,18 +163,23 @@ export function copilotkitCustomizeConfig(
|
|
|
159
163
|
}
|
|
160
164
|
}
|
|
161
165
|
/**
|
|
162
|
-
* Exits the current agent after the run completes. Calling
|
|
166
|
+
* Exits the current agent after the run completes. Calling copilotkitExit() will
|
|
163
167
|
* not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
|
|
164
168
|
* the run completes.
|
|
165
169
|
*
|
|
166
170
|
* ### Examples
|
|
167
171
|
*
|
|
168
172
|
* ```typescript
|
|
169
|
-
* import {
|
|
173
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
174
|
+
* import type { MessagesAnnotation } from "@langchain/langgraph";
|
|
175
|
+
* import { copilotkitExit } from "@copilotkit/sdk-js/langgraph";
|
|
176
|
+
*
|
|
177
|
+
* type AgentState = typeof MessagesAnnotation.State;
|
|
170
178
|
*
|
|
171
|
-
* async function myNode(state:
|
|
172
|
-
* await copilotkitExit(config)
|
|
173
|
-
* return state
|
|
179
|
+
* async function myNode(state: AgentState, config: RunnableConfig) {
|
|
180
|
+
* await copilotkitExit(config);
|
|
181
|
+
* return state;
|
|
182
|
+
* }
|
|
174
183
|
* ```
|
|
175
184
|
*/
|
|
176
185
|
export async function copilotkitExit(
|
|
@@ -200,11 +209,16 @@ export async function copilotkitExit(
|
|
|
200
209
|
* ### Examples
|
|
201
210
|
*
|
|
202
211
|
* ```typescript
|
|
203
|
-
* import {
|
|
212
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
213
|
+
* import { copilotkitEmitState } from "@copilotkit/sdk-js/langgraph";
|
|
214
|
+
*
|
|
215
|
+
* declare function someLongRunningOperation(step: number): Promise<void>;
|
|
204
216
|
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
217
|
+
* async function myNode(config: RunnableConfig) {
|
|
218
|
+
* for (let i = 0; i < 10; i++) {
|
|
219
|
+
* await someLongRunningOperation(i);
|
|
220
|
+
* await copilotkitEmitState(config, { progress: i });
|
|
221
|
+
* }
|
|
208
222
|
* }
|
|
209
223
|
* ```
|
|
210
224
|
*/
|
|
@@ -249,14 +263,18 @@ export async function copilotkitEmitState(
|
|
|
249
263
|
* ### Examples
|
|
250
264
|
*
|
|
251
265
|
* ```typescript
|
|
252
|
-
* import {
|
|
266
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
267
|
+
* import { AIMessage } from "@langchain/core/messages";
|
|
268
|
+
* import { copilotkitEmitMessage } from "@copilotkit/sdk-js/langgraph";
|
|
253
269
|
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
270
|
+
* async function myNode(config: RunnableConfig) {
|
|
271
|
+
* const message = "Step 1 of 10 complete";
|
|
272
|
+
* await copilotkitEmitMessage(config, message);
|
|
256
273
|
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
274
|
+
* // Return the message from the node
|
|
275
|
+
* return {
|
|
276
|
+
* messages: [new AIMessage({ content: message })],
|
|
277
|
+
* };
|
|
260
278
|
* }
|
|
261
279
|
* ```
|
|
262
280
|
*/
|
|
@@ -300,15 +318,18 @@ export async function copilotkitEmitMessage(
|
|
|
300
318
|
* ### Examples
|
|
301
319
|
*
|
|
302
320
|
* ```typescript
|
|
303
|
-
* import {
|
|
321
|
+
* import type { RunnableConfig } from "@langchain/core/runnables";
|
|
322
|
+
* import { copilotkitEmitToolCall } from "@copilotkit/sdk-js/langgraph";
|
|
304
323
|
*
|
|
305
|
-
*
|
|
324
|
+
* async function myNode(config: RunnableConfig) {
|
|
325
|
+
* const autoId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 });
|
|
306
326
|
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
327
|
+
* // With a custom ID for correlation/idempotency:
|
|
328
|
+
* const customId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 }, { toolCallId: "my-custom-id" });
|
|
329
|
+
* }
|
|
309
330
|
* ```
|
|
310
331
|
*
|
|
311
|
-
* @returns The tool call ID used for the emitted call
|
|
332
|
+
* @returns The tool call ID used for the emitted call. It equals `options.toolCallId`
|
|
312
333
|
* when provided, otherwise a randomly generated ID.
|
|
313
334
|
*/
|
|
314
335
|
export async function copilotkitEmitToolCall(
|
|
@@ -446,7 +467,7 @@ export function convertActionToDynamicStructuredTool(
|
|
|
446
467
|
* ### Examples
|
|
447
468
|
*
|
|
448
469
|
* ```typescript
|
|
449
|
-
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js";
|
|
470
|
+
* import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js/langgraph";
|
|
450
471
|
*
|
|
451
472
|
* const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);
|
|
452
473
|
* ```
|