@copilotkit/sdk-js 1.9.2-next.7 → 1.9.2-next.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @copilotkit/sdk-js
2
2
 
3
+ ## 1.9.2-next.9
4
+
5
+ ### Patch Changes
6
+
7
+ - 1d1c51d: - feat: surface all errors in structured format
8
+ - Updated dependencies [1d1c51d]
9
+ - @copilotkit/shared@1.9.2-next.9
10
+
11
+ ## 1.9.2-next.8
12
+
13
+ ### Patch Changes
14
+
15
+ - @copilotkit/shared@1.9.2-next.8
16
+
3
17
  ## 1.9.2-next.7
4
18
 
5
19
  ### Patch Changes
@@ -0,0 +1,311 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/langgraph.ts
5
+ import { dispatchCustomEvent } from "@langchain/core/callbacks/dispatch";
6
+ import { convertJsonSchemaToZodSchema, randomId, CopilotKitMisuseError } from "@copilotkit/shared";
7
+ import { Annotation, MessagesAnnotation, interrupt } from "@langchain/langgraph";
8
+ import { DynamicStructuredTool } from "@langchain/core/tools";
9
+ import { AIMessage } from "@langchain/core/messages";
10
+ var CopilotKitPropertiesAnnotation = Annotation.Root({
11
+ actions: Annotation
12
+ });
13
+ var CopilotKitStateAnnotation = Annotation.Root({
14
+ copilotkit: Annotation,
15
+ ...MessagesAnnotation.spec
16
+ });
17
+ function copilotkitCustomizeConfig(baseConfig, options) {
18
+ if (baseConfig && typeof baseConfig !== "object") {
19
+ throw new CopilotKitMisuseError({
20
+ message: "baseConfig must be an object or null/undefined"
21
+ });
22
+ }
23
+ if (options && typeof options !== "object") {
24
+ throw new CopilotKitMisuseError({
25
+ message: "options must be an object when provided"
26
+ });
27
+ }
28
+ if (options == null ? void 0 : options.emitIntermediateState) {
29
+ if (!Array.isArray(options.emitIntermediateState)) {
30
+ throw new CopilotKitMisuseError({
31
+ message: "emitIntermediateState must be an array when provided"
32
+ });
33
+ }
34
+ options.emitIntermediateState.forEach((state, index) => {
35
+ if (!state || typeof state !== "object") {
36
+ throw new CopilotKitMisuseError({
37
+ message: `emitIntermediateState[${index}] must be an object`
38
+ });
39
+ }
40
+ if (!state.stateKey || typeof state.stateKey !== "string") {
41
+ throw new CopilotKitMisuseError({
42
+ message: `emitIntermediateState[${index}] must have a valid 'stateKey' string property`
43
+ });
44
+ }
45
+ if (!state.tool || typeof state.tool !== "string") {
46
+ throw new CopilotKitMisuseError({
47
+ message: `emitIntermediateState[${index}] must have a valid 'tool' string property`
48
+ });
49
+ }
50
+ if (state.toolArgument && typeof state.toolArgument !== "string") {
51
+ throw new CopilotKitMisuseError({
52
+ message: `emitIntermediateState[${index}].toolArgument must be a string when provided`
53
+ });
54
+ }
55
+ });
56
+ }
57
+ try {
58
+ const metadata = (baseConfig == null ? void 0 : baseConfig.metadata) || {};
59
+ if (options == null ? void 0 : options.emitAll) {
60
+ metadata["copilotkit:emit-tool-calls"] = true;
61
+ metadata["copilotkit:emit-messages"] = true;
62
+ } else {
63
+ if ((options == null ? void 0 : options.emitToolCalls) !== void 0) {
64
+ metadata["copilotkit:emit-tool-calls"] = options.emitToolCalls;
65
+ }
66
+ if ((options == null ? void 0 : options.emitMessages) !== void 0) {
67
+ metadata["copilotkit:emit-messages"] = options.emitMessages;
68
+ }
69
+ }
70
+ if (options == null ? void 0 : options.emitIntermediateState) {
71
+ const snakeCaseIntermediateState = options.emitIntermediateState.map((state) => ({
72
+ tool: state.tool,
73
+ tool_argument: state.toolArgument,
74
+ state_key: state.stateKey
75
+ }));
76
+ metadata["copilotkit:emit-intermediate-state"] = snakeCaseIntermediateState;
77
+ }
78
+ baseConfig = baseConfig || {};
79
+ return {
80
+ ...baseConfig,
81
+ metadata
82
+ };
83
+ } catch (error) {
84
+ throw new CopilotKitMisuseError({
85
+ message: `Failed to customize config: ${error instanceof Error ? error.message : String(error)}`
86
+ });
87
+ }
88
+ }
89
+ __name(copilotkitCustomizeConfig, "copilotkitCustomizeConfig");
90
+ async function copilotkitExit(config) {
91
+ if (!config) {
92
+ throw new CopilotKitMisuseError({
93
+ message: "LangGraph configuration is required for copilotkitExit"
94
+ });
95
+ }
96
+ try {
97
+ await dispatchCustomEvent("copilotkit_exit", {}, config);
98
+ } catch (error) {
99
+ throw new CopilotKitMisuseError({
100
+ message: `Failed to dispatch exit event: ${error instanceof Error ? error.message : String(error)}`
101
+ });
102
+ }
103
+ }
104
+ __name(copilotkitExit, "copilotkitExit");
105
+ async function copilotkitEmitState(config, state) {
106
+ if (!config) {
107
+ throw new CopilotKitMisuseError({
108
+ message: "LangGraph configuration is required for copilotkitEmitState"
109
+ });
110
+ }
111
+ if (state === void 0) {
112
+ throw new CopilotKitMisuseError({
113
+ message: "State is required for copilotkitEmitState"
114
+ });
115
+ }
116
+ try {
117
+ await dispatchCustomEvent("copilotkit_manually_emit_intermediate_state", state, config);
118
+ } catch (error) {
119
+ throw new CopilotKitMisuseError({
120
+ message: `Failed to emit state: ${error instanceof Error ? error.message : String(error)}`
121
+ });
122
+ }
123
+ }
124
+ __name(copilotkitEmitState, "copilotkitEmitState");
125
+ async function copilotkitEmitMessage(config, message) {
126
+ if (!config) {
127
+ throw new CopilotKitMisuseError({
128
+ message: "LangGraph configuration is required for copilotkitEmitMessage"
129
+ });
130
+ }
131
+ if (!message || typeof message !== "string") {
132
+ throw new CopilotKitMisuseError({
133
+ message: "Message must be a non-empty string for copilotkitEmitMessage"
134
+ });
135
+ }
136
+ try {
137
+ await dispatchCustomEvent("copilotkit_manually_emit_message", {
138
+ message,
139
+ message_id: randomId(),
140
+ role: "assistant"
141
+ }, config);
142
+ } catch (error) {
143
+ throw new CopilotKitMisuseError({
144
+ message: `Failed to emit message: ${error instanceof Error ? error.message : String(error)}`
145
+ });
146
+ }
147
+ }
148
+ __name(copilotkitEmitMessage, "copilotkitEmitMessage");
149
+ async function copilotkitEmitToolCall(config, name, args) {
150
+ if (!config) {
151
+ throw new CopilotKitMisuseError({
152
+ message: "LangGraph configuration is required for copilotkitEmitToolCall"
153
+ });
154
+ }
155
+ if (!name || typeof name !== "string") {
156
+ throw new CopilotKitMisuseError({
157
+ message: "Tool name must be a non-empty string for copilotkitEmitToolCall"
158
+ });
159
+ }
160
+ if (args === void 0) {
161
+ throw new CopilotKitMisuseError({
162
+ message: "Tool arguments are required for copilotkitEmitToolCall"
163
+ });
164
+ }
165
+ try {
166
+ await dispatchCustomEvent("copilotkit_manually_emit_tool_call", {
167
+ name,
168
+ args,
169
+ id: randomId()
170
+ }, config);
171
+ } catch (error) {
172
+ throw new CopilotKitMisuseError({
173
+ message: `Failed to emit tool call '${name}': ${error instanceof Error ? error.message : String(error)}`
174
+ });
175
+ }
176
+ }
177
+ __name(copilotkitEmitToolCall, "copilotkitEmitToolCall");
178
+ function convertActionToDynamicStructuredTool(actionInput) {
179
+ if (!actionInput) {
180
+ throw new CopilotKitMisuseError({
181
+ message: "Action input is required but was not provided"
182
+ });
183
+ }
184
+ if (!actionInput.name || typeof actionInput.name !== "string") {
185
+ throw new CopilotKitMisuseError({
186
+ message: "Action must have a valid 'name' property of type string"
187
+ });
188
+ }
189
+ if (!actionInput.description || typeof actionInput.description !== "string") {
190
+ throw new CopilotKitMisuseError({
191
+ message: `Action '${actionInput.name}' must have a valid 'description' property of type string`
192
+ });
193
+ }
194
+ if (!actionInput.parameters) {
195
+ throw new CopilotKitMisuseError({
196
+ message: `Action '${actionInput.name}' must have a 'parameters' property`
197
+ });
198
+ }
199
+ try {
200
+ return new DynamicStructuredTool({
201
+ name: actionInput.name,
202
+ description: actionInput.description,
203
+ schema: convertJsonSchemaToZodSchema(actionInput.parameters, true),
204
+ func: async () => {
205
+ return "";
206
+ }
207
+ });
208
+ } catch (error) {
209
+ throw new CopilotKitMisuseError({
210
+ message: `Failed to convert action '${actionInput.name}' to DynamicStructuredTool: ${error instanceof Error ? error.message : String(error)}`
211
+ });
212
+ }
213
+ }
214
+ __name(convertActionToDynamicStructuredTool, "convertActionToDynamicStructuredTool");
215
+ function convertActionsToDynamicStructuredTools(actions) {
216
+ if (!Array.isArray(actions)) {
217
+ throw new CopilotKitMisuseError({
218
+ message: "Actions must be an array"
219
+ });
220
+ }
221
+ return actions.map((action, index) => {
222
+ try {
223
+ return convertActionToDynamicStructuredTool(action);
224
+ } catch (error) {
225
+ throw new CopilotKitMisuseError({
226
+ message: `Failed to convert action at index ${index}: ${error instanceof Error ? error.message : String(error)}`
227
+ });
228
+ }
229
+ });
230
+ }
231
+ __name(convertActionsToDynamicStructuredTools, "convertActionsToDynamicStructuredTools");
232
+ function copilotKitInterrupt({ message, action, args }) {
233
+ if (!message && !action) {
234
+ throw new CopilotKitMisuseError({
235
+ message: "Either message or action (and optional arguments) must be provided for copilotKitInterrupt"
236
+ });
237
+ }
238
+ if (action && typeof action !== "string") {
239
+ throw new CopilotKitMisuseError({
240
+ message: "Action must be a string when provided to copilotKitInterrupt"
241
+ });
242
+ }
243
+ if (message && typeof message !== "string") {
244
+ throw new CopilotKitMisuseError({
245
+ message: "Message must be a string when provided to copilotKitInterrupt"
246
+ });
247
+ }
248
+ if (args && typeof args !== "object") {
249
+ throw new CopilotKitMisuseError({
250
+ message: "Args must be an object when provided to copilotKitInterrupt"
251
+ });
252
+ }
253
+ let interruptValues = null;
254
+ let interruptMessage = null;
255
+ let answer = null;
256
+ try {
257
+ if (message) {
258
+ interruptValues = message;
259
+ interruptMessage = new AIMessage({
260
+ content: message,
261
+ id: randomId()
262
+ });
263
+ } else {
264
+ const toolId = randomId();
265
+ interruptMessage = new AIMessage({
266
+ content: "",
267
+ tool_calls: [
268
+ {
269
+ id: toolId,
270
+ name: action,
271
+ args: args ?? {}
272
+ }
273
+ ]
274
+ });
275
+ interruptValues = {
276
+ action,
277
+ args: args ?? {}
278
+ };
279
+ }
280
+ const response = interrupt({
281
+ __copilotkit_interrupt_value__: interruptValues,
282
+ __copilotkit_messages__: [
283
+ interruptMessage
284
+ ]
285
+ });
286
+ answer = response[response.length - 1].content;
287
+ return {
288
+ answer,
289
+ messages: response
290
+ };
291
+ } catch (error) {
292
+ throw new CopilotKitMisuseError({
293
+ message: `Failed to create interrupt: ${error instanceof Error ? error.message : String(error)}`
294
+ });
295
+ }
296
+ }
297
+ __name(copilotKitInterrupt, "copilotKitInterrupt");
298
+
299
+ export {
300
+ CopilotKitPropertiesAnnotation,
301
+ CopilotKitStateAnnotation,
302
+ copilotkitCustomizeConfig,
303
+ copilotkitExit,
304
+ copilotkitEmitState,
305
+ copilotkitEmitMessage,
306
+ copilotkitEmitToolCall,
307
+ convertActionToDynamicStructuredTool,
308
+ convertActionsToDynamicStructuredTools,
309
+ copilotKitInterrupt
310
+ };
311
+ //# sourceMappingURL=chunk-Q5S2AURJ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/langgraph.ts"],"sourcesContent":["import { RunnableConfig } from \"@langchain/core/runnables\";\nimport { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\nimport { convertJsonSchemaToZodSchema, randomId, CopilotKitMisuseError } from \"@copilotkit/shared\";\nimport { Annotation, MessagesAnnotation, interrupt } from \"@langchain/langgraph\";\nimport { DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { AIMessage } from \"@langchain/core/messages\";\n\ninterface IntermediateStateConfig {\n stateKey: string;\n tool: string;\n toolArgument?: string;\n}\n\ninterface OptionsConfig {\n emitToolCalls?: boolean | string | string[];\n emitMessages?: boolean;\n emitAll?: boolean;\n emitIntermediateState?: IntermediateStateConfig[];\n}\n\nexport const CopilotKitPropertiesAnnotation = Annotation.Root({\n actions: Annotation<any[]>,\n});\n\nexport const CopilotKitStateAnnotation = Annotation.Root({\n copilotkit: Annotation<typeof CopilotKitPropertiesAnnotation.State>,\n ...MessagesAnnotation.spec,\n});\n\nexport type CopilotKitState = typeof CopilotKitStateAnnotation.State;\nexport type CopilotKitProperties = typeof CopilotKitPropertiesAnnotation.State;\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((state) => ({\n tool: state.tool,\n tool_argument: state.toolArgument,\n state_key: state.stateKey,\n }));\n\n metadata[\"copilotkit:emit-intermediate-state\"] = 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(\"copilotkit_manually_emit_intermediate_state\", state, config);\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 * await copilotkitEmitToolCall(config, name=\"SearchTool\", args={\"steps\": 10})\n * ```\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 if (!config) {\n throw new CopilotKitMisuseError({\n message: \"LangGraph configuration is required for copilotkitEmitToolCall\",\n });\n }\n\n if (!name || typeof name !== \"string\") {\n throw new CopilotKitMisuseError({\n message: \"Tool name must be a non-empty string 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 await dispatchCustomEvent(\n \"copilotkit_manually_emit_tool_call\",\n { name, args, id: randomId() },\n config,\n );\n } catch (error) {\n throw new CopilotKitMisuseError({\n message: `Failed to emit tool call '${name}': ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n}\n\nexport function convertActionToDynamicStructuredTool(actionInput: any): 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 (!actionInput.description || typeof actionInput.description !== \"string\") {\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(action);\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":";;;;AACA,SAASA,2BAA2B;AACpC,SAASC,8BAA8BC,UAAUC,6BAA6B;AAC9E,SAASC,YAAYC,oBAAoBC,iBAAiB;AAC1D,SAASC,6BAA6B;AACtC,SAASC,iBAAiB;AAenB,IAAMC,iCAAiCC,WAAWC,KAAK;EAC5DC,SAASF;AACX,CAAA;AAEO,IAAMG,4BAA4BH,WAAWC,KAAK;EACvDG,YAAYJ;EACZ,GAAGK,mBAAmBC;AACxB,CAAA;AA+CO,SAASC,0BAIdC,YAYAC,SAAuB;AAEvB,MAAID,cAAc,OAAOA,eAAe,UAAU;AAChD,UAAM,IAAIE,sBAAsB;MAC9BC,SAAS;IACX,CAAA;EACF;AAEA,MAAIF,WAAW,OAAOA,YAAY,UAAU;AAC1C,UAAM,IAAIC,sBAAsB;MAC9BC,SAAS;IACX,CAAA;EACF;AAGA,MAAIF,mCAASG,uBAAuB;AAClC,QAAI,CAACC,MAAMC,QAAQL,QAAQG,qBAAqB,GAAG;AACjD,YAAM,IAAIF,sBAAsB;QAC9BC,SAAS;MACX,CAAA;IACF;AAEAF,YAAQG,sBAAsBG,QAAQ,CAACC,OAAOC,UAAAA;AAC5C,UAAI,CAACD,SAAS,OAAOA,UAAU,UAAU;AACvC,cAAM,IAAIN,sBAAsB;UAC9BC,SAAS,yBAAyBM;QAC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AElB,IAAIiB;YAAQnB,MAAMY;YAAQX,MAAMA,QAAQ,CAAC;UAAE;;MAC5D,CAAA;AACAa,wBAAkB;QAChBF;QACAX,MAAMA,QAAQ,CAAC;MACjB;IACF;AAEA,UAAMoB,WAAWC,UAAU;MACzBC,gCAAgCT;MAChCU,yBAAyB;QAACT;;IAC5B,CAAA;AACAC,aAASK,SAASA,SAASI,SAAS,CAAA,EAAGP;AAEvC,WAAO;MACLF;MACAU,UAAUL;IACZ;EACF,SAASjC,OAAP;AACA,UAAM,IAAIpB,sBAAsB;MAC9BC,SAAS,+BAA+BmB,iBAAiBC,QAAQD,MAAMnB,UAAUqB,OAAOF,KAAAA;IAC1F,CAAA;EACF;AACF;AArEgByB;","names":["dispatchCustomEvent","convertJsonSchemaToZodSchema","randomId","CopilotKitMisuseError","Annotation","MessagesAnnotation","interrupt","DynamicStructuredTool","AIMessage","CopilotKitPropertiesAnnotation","Annotation","Root","actions","CopilotKitStateAnnotation","copilotkit","MessagesAnnotation","spec","copilotkitCustomizeConfig","baseConfig","options","CopilotKitMisuseError","message","emitIntermediateState","Array","isArray","forEach","state","index","stateKey","tool","toolArgument","metadata","emitAll","emitToolCalls","undefined","emitMessages","snakeCaseIntermediateState","map","tool_argument","state_key","error","Error","String","copilotkitExit","config","dispatchCustomEvent","copilotkitEmitState","copilotkitEmitMessage","message_id","randomId","role","copilotkitEmitToolCall","name","args","id","convertActionToDynamicStructuredTool","actionInput","description","parameters","DynamicStructuredTool","schema","convertJsonSchemaToZodSchema","func","convertActionsToDynamicStructuredTools","action","copilotKitInterrupt","interruptValues","interruptMessage","answer","AIMessage","content","toolId","tool_calls","response","interrupt","__copilotkit_interrupt_value__","__copilotkit_messages__","length","messages"]}
package/dist/langchain.js CHANGED
@@ -46,70 +46,218 @@ var CopilotKitStateAnnotation = import_langgraph.Annotation.Root({
46
46
  ...import_langgraph.MessagesAnnotation.spec
47
47
  });
48
48
  function copilotkitCustomizeConfig(baseConfig, options) {
49
- const metadata = (baseConfig == null ? void 0 : baseConfig.metadata) || {};
50
- if (options == null ? void 0 : options.emitAll) {
51
- metadata["copilotkit:emit-tool-calls"] = true;
52
- metadata["copilotkit:emit-messages"] = true;
53
- } else {
54
- if ((options == null ? void 0 : options.emitToolCalls) !== void 0) {
55
- metadata["copilotkit:emit-tool-calls"] = options.emitToolCalls;
49
+ if (baseConfig && typeof baseConfig !== "object") {
50
+ throw new import_shared.CopilotKitMisuseError({
51
+ message: "baseConfig must be an object or null/undefined"
52
+ });
53
+ }
54
+ if (options && typeof options !== "object") {
55
+ throw new import_shared.CopilotKitMisuseError({
56
+ message: "options must be an object when provided"
57
+ });
58
+ }
59
+ if (options == null ? void 0 : options.emitIntermediateState) {
60
+ if (!Array.isArray(options.emitIntermediateState)) {
61
+ throw new import_shared.CopilotKitMisuseError({
62
+ message: "emitIntermediateState must be an array when provided"
63
+ });
56
64
  }
57
- if ((options == null ? void 0 : options.emitMessages) !== void 0) {
58
- metadata["copilotkit:emit-messages"] = options.emitMessages;
65
+ options.emitIntermediateState.forEach((state, index) => {
66
+ if (!state || typeof state !== "object") {
67
+ throw new import_shared.CopilotKitMisuseError({
68
+ message: `emitIntermediateState[${index}] must be an object`
69
+ });
70
+ }
71
+ if (!state.stateKey || typeof state.stateKey !== "string") {
72
+ throw new import_shared.CopilotKitMisuseError({
73
+ message: `emitIntermediateState[${index}] must have a valid 'stateKey' string property`
74
+ });
75
+ }
76
+ if (!state.tool || typeof state.tool !== "string") {
77
+ throw new import_shared.CopilotKitMisuseError({
78
+ message: `emitIntermediateState[${index}] must have a valid 'tool' string property`
79
+ });
80
+ }
81
+ if (state.toolArgument && typeof state.toolArgument !== "string") {
82
+ throw new import_shared.CopilotKitMisuseError({
83
+ message: `emitIntermediateState[${index}].toolArgument must be a string when provided`
84
+ });
85
+ }
86
+ });
87
+ }
88
+ try {
89
+ const metadata = (baseConfig == null ? void 0 : baseConfig.metadata) || {};
90
+ if (options == null ? void 0 : options.emitAll) {
91
+ metadata["copilotkit:emit-tool-calls"] = true;
92
+ metadata["copilotkit:emit-messages"] = true;
93
+ } else {
94
+ if ((options == null ? void 0 : options.emitToolCalls) !== void 0) {
95
+ metadata["copilotkit:emit-tool-calls"] = options.emitToolCalls;
96
+ }
97
+ if ((options == null ? void 0 : options.emitMessages) !== void 0) {
98
+ metadata["copilotkit:emit-messages"] = options.emitMessages;
99
+ }
59
100
  }
101
+ if (options == null ? void 0 : options.emitIntermediateState) {
102
+ const snakeCaseIntermediateState = options.emitIntermediateState.map((state) => ({
103
+ tool: state.tool,
104
+ tool_argument: state.toolArgument,
105
+ state_key: state.stateKey
106
+ }));
107
+ metadata["copilotkit:emit-intermediate-state"] = snakeCaseIntermediateState;
108
+ }
109
+ baseConfig = baseConfig || {};
110
+ return {
111
+ ...baseConfig,
112
+ metadata
113
+ };
114
+ } catch (error) {
115
+ throw new import_shared.CopilotKitMisuseError({
116
+ message: `Failed to customize config: ${error instanceof Error ? error.message : String(error)}`
117
+ });
60
118
  }
61
- if (options == null ? void 0 : options.emitIntermediateState) {
62
- const snakeCaseIntermediateState = options.emitIntermediateState.map((state) => ({
63
- tool: state.tool,
64
- tool_argument: state.toolArgument,
65
- state_key: state.stateKey
66
- }));
67
- metadata["copilotkit:emit-intermediate-state"] = snakeCaseIntermediateState;
68
- }
69
- baseConfig = baseConfig || {};
70
- return {
71
- ...baseConfig,
72
- metadata
73
- };
74
119
  }
75
120
  __name(copilotkitCustomizeConfig, "copilotkitCustomizeConfig");
76
121
  async function copilotkitExit(config) {
77
- await (0, import_dispatch.dispatchCustomEvent)("copilotkit_exit", {}, config);
122
+ if (!config) {
123
+ throw new import_shared.CopilotKitMisuseError({
124
+ message: "LangGraph configuration is required for copilotkitExit"
125
+ });
126
+ }
127
+ try {
128
+ await (0, import_dispatch.dispatchCustomEvent)("copilotkit_exit", {}, config);
129
+ } catch (error) {
130
+ throw new import_shared.CopilotKitMisuseError({
131
+ message: `Failed to dispatch exit event: ${error instanceof Error ? error.message : String(error)}`
132
+ });
133
+ }
78
134
  }
79
135
  __name(copilotkitExit, "copilotkitExit");
80
136
  async function copilotkitEmitState(config, state) {
81
- await (0, import_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_intermediate_state", state, config);
137
+ if (!config) {
138
+ throw new import_shared.CopilotKitMisuseError({
139
+ message: "LangGraph configuration is required for copilotkitEmitState"
140
+ });
141
+ }
142
+ if (state === void 0) {
143
+ throw new import_shared.CopilotKitMisuseError({
144
+ message: "State is required for copilotkitEmitState"
145
+ });
146
+ }
147
+ try {
148
+ await (0, import_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_intermediate_state", state, config);
149
+ } catch (error) {
150
+ throw new import_shared.CopilotKitMisuseError({
151
+ message: `Failed to emit state: ${error instanceof Error ? error.message : String(error)}`
152
+ });
153
+ }
82
154
  }
83
155
  __name(copilotkitEmitState, "copilotkitEmitState");
84
156
  async function copilotkitEmitMessage(config, message) {
85
- await (0, import_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_message", {
86
- message,
87
- message_id: (0, import_shared.randomId)(),
88
- role: "assistant"
89
- }, config);
157
+ if (!config) {
158
+ throw new import_shared.CopilotKitMisuseError({
159
+ message: "LangGraph configuration is required for copilotkitEmitMessage"
160
+ });
161
+ }
162
+ if (!message || typeof message !== "string") {
163
+ throw new import_shared.CopilotKitMisuseError({
164
+ message: "Message must be a non-empty string for copilotkitEmitMessage"
165
+ });
166
+ }
167
+ try {
168
+ await (0, import_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_message", {
169
+ message,
170
+ message_id: (0, import_shared.randomId)(),
171
+ role: "assistant"
172
+ }, config);
173
+ } catch (error) {
174
+ throw new import_shared.CopilotKitMisuseError({
175
+ message: `Failed to emit message: ${error instanceof Error ? error.message : String(error)}`
176
+ });
177
+ }
90
178
  }
91
179
  __name(copilotkitEmitMessage, "copilotkitEmitMessage");
92
180
  async function copilotkitEmitToolCall(config, name, args) {
93
- await (0, import_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_tool_call", {
94
- name,
95
- args,
96
- id: (0, import_shared.randomId)()
97
- }, config);
181
+ if (!config) {
182
+ throw new import_shared.CopilotKitMisuseError({
183
+ message: "LangGraph configuration is required for copilotkitEmitToolCall"
184
+ });
185
+ }
186
+ if (!name || typeof name !== "string") {
187
+ throw new import_shared.CopilotKitMisuseError({
188
+ message: "Tool name must be a non-empty string for copilotkitEmitToolCall"
189
+ });
190
+ }
191
+ if (args === void 0) {
192
+ throw new import_shared.CopilotKitMisuseError({
193
+ message: "Tool arguments are required for copilotkitEmitToolCall"
194
+ });
195
+ }
196
+ try {
197
+ await (0, import_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_tool_call", {
198
+ name,
199
+ args,
200
+ id: (0, import_shared.randomId)()
201
+ }, config);
202
+ } catch (error) {
203
+ throw new import_shared.CopilotKitMisuseError({
204
+ message: `Failed to emit tool call '${name}': ${error instanceof Error ? error.message : String(error)}`
205
+ });
206
+ }
98
207
  }
99
208
  __name(copilotkitEmitToolCall, "copilotkitEmitToolCall");
100
209
  function convertActionToDynamicStructuredTool(actionInput) {
101
- return new import_tools.DynamicStructuredTool({
102
- name: actionInput.name,
103
- description: actionInput.description,
104
- schema: (0, import_shared.convertJsonSchemaToZodSchema)(actionInput.parameters, true),
105
- func: async () => {
106
- return "";
107
- }
108
- });
210
+ if (!actionInput) {
211
+ throw new import_shared.CopilotKitMisuseError({
212
+ message: "Action input is required but was not provided"
213
+ });
214
+ }
215
+ if (!actionInput.name || typeof actionInput.name !== "string") {
216
+ throw new import_shared.CopilotKitMisuseError({
217
+ message: "Action must have a valid 'name' property of type string"
218
+ });
219
+ }
220
+ if (!actionInput.description || typeof actionInput.description !== "string") {
221
+ throw new import_shared.CopilotKitMisuseError({
222
+ message: `Action '${actionInput.name}' must have a valid 'description' property of type string`
223
+ });
224
+ }
225
+ if (!actionInput.parameters) {
226
+ throw new import_shared.CopilotKitMisuseError({
227
+ message: `Action '${actionInput.name}' must have a 'parameters' property`
228
+ });
229
+ }
230
+ try {
231
+ return new import_tools.DynamicStructuredTool({
232
+ name: actionInput.name,
233
+ description: actionInput.description,
234
+ schema: (0, import_shared.convertJsonSchemaToZodSchema)(actionInput.parameters, true),
235
+ func: async () => {
236
+ return "";
237
+ }
238
+ });
239
+ } catch (error) {
240
+ throw new import_shared.CopilotKitMisuseError({
241
+ message: `Failed to convert action '${actionInput.name}' to DynamicStructuredTool: ${error instanceof Error ? error.message : String(error)}`
242
+ });
243
+ }
109
244
  }
110
245
  __name(convertActionToDynamicStructuredTool, "convertActionToDynamicStructuredTool");
111
246
  function convertActionsToDynamicStructuredTools(actions) {
112
- return actions.map((action) => convertActionToDynamicStructuredTool(action));
247
+ if (!Array.isArray(actions)) {
248
+ throw new import_shared.CopilotKitMisuseError({
249
+ message: "Actions must be an array"
250
+ });
251
+ }
252
+ return actions.map((action, index) => {
253
+ try {
254
+ return convertActionToDynamicStructuredTool(action);
255
+ } catch (error) {
256
+ throw new import_shared.CopilotKitMisuseError({
257
+ message: `Failed to convert action at index ${index}: ${error instanceof Error ? error.message : String(error)}`
258
+ });
259
+ }
260
+ });
113
261
  }
114
262
  __name(convertActionsToDynamicStructuredTools, "convertActionsToDynamicStructuredTools");
115
263