@copilotkit/sdk-js 2.0.0-next.1 → 2.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -1
  3. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  4. package/dist/header-propagation.cjs +37 -0
  5. package/dist/header-propagation.cjs.map +1 -0
  6. package/dist/header-propagation.d.cts +16 -0
  7. package/dist/header-propagation.d.cts.map +1 -0
  8. package/dist/header-propagation.d.mts +16 -0
  9. package/dist/header-propagation.d.mts.map +1 -0
  10. package/dist/header-propagation.mjs +35 -0
  11. package/dist/header-propagation.mjs.map +1 -0
  12. package/dist/index.cjs +5 -0
  13. package/dist/index.d.cts +18 -0
  14. package/dist/index.d.mts +18 -0
  15. package/dist/index.mjs +3 -1
  16. package/dist/langchain.cjs +21 -0
  17. package/dist/langchain.cjs.map +1 -0
  18. package/dist/langchain.d.cts +139 -0
  19. package/dist/langchain.d.mts +140 -0
  20. package/dist/langchain.mjs +7 -23
  21. package/dist/langchain.mjs.map +1 -1
  22. package/dist/langgraph/middleware.cjs +433 -0
  23. package/dist/langgraph/middleware.cjs.map +1 -0
  24. package/dist/langgraph/middleware.d.cts +86 -0
  25. package/dist/langgraph/middleware.d.cts.map +1 -0
  26. package/dist/langgraph/middleware.d.mts +86 -0
  27. package/dist/langgraph/middleware.d.mts.map +1 -0
  28. package/dist/langgraph/middleware.mjs +429 -0
  29. package/dist/langgraph/middleware.mjs.map +1 -0
  30. package/dist/langgraph/state-schema.cjs +37 -0
  31. package/dist/langgraph/state-schema.cjs.map +1 -0
  32. package/dist/langgraph/state-schema.d.cts +118 -0
  33. package/dist/langgraph/state-schema.d.cts.map +1 -0
  34. package/dist/langgraph/state-schema.d.mts +118 -0
  35. package/dist/langgraph/state-schema.d.mts.map +1 -0
  36. package/dist/langgraph/state-schema.mjs +36 -0
  37. package/dist/langgraph/state-schema.mjs.map +1 -0
  38. package/dist/langgraph/types.cjs +60 -0
  39. package/dist/langgraph/types.cjs.map +1 -0
  40. package/dist/langgraph/types.d.cts +335 -0
  41. package/dist/langgraph/types.d.cts.map +1 -0
  42. package/dist/langgraph/types.d.mts +335 -0
  43. package/dist/langgraph/types.d.mts.map +1 -0
  44. package/dist/langgraph/types.mjs +57 -0
  45. package/dist/langgraph/types.mjs.map +1 -0
  46. package/dist/langgraph/utils.cjs +297 -0
  47. package/dist/langgraph/utils.cjs.map +1 -0
  48. package/dist/langgraph/utils.d.cts +209 -0
  49. package/dist/langgraph/utils.d.cts.map +1 -0
  50. package/dist/langgraph/utils.d.mts +209 -0
  51. package/dist/langgraph/utils.d.mts.map +1 -0
  52. package/dist/langgraph/utils.mjs +289 -0
  53. package/dist/langgraph/utils.mjs.map +1 -0
  54. package/dist/langgraph-middlewares.cjs +22 -0
  55. package/dist/langgraph-middlewares.d.cts +30 -0
  56. package/dist/langgraph-middlewares.d.mts +30 -0
  57. package/dist/langgraph-middlewares.mjs +5 -0
  58. package/dist/langgraph.cjs +21 -0
  59. package/dist/langgraph.d.cts +5 -0
  60. package/dist/langgraph.d.mts +5 -0
  61. package/dist/langgraph.mjs +6 -27
  62. package/package.json +50 -47
  63. package/src/__tests__/error-handling.test.ts +54 -20
  64. package/src/__tests__/header-propagation.test.ts +131 -0
  65. package/src/header-propagation.ts +61 -0
  66. package/src/index.ts +41 -0
  67. package/src/langchain.ts +168 -8
  68. package/src/langgraph/__tests__/middleware.a2ui-params.test.ts +139 -0
  69. package/src/langgraph/__tests__/middleware.native-a2ui.test.ts +118 -0
  70. package/src/langgraph/__tests__/middleware.properties.test.ts +357 -0
  71. package/src/langgraph/__tests__/middleware.test.ts +1141 -0
  72. package/src/langgraph/__tests__/state-schema.test.ts +85 -0
  73. package/src/langgraph/__tests__/utils.test.ts +363 -0
  74. package/src/langgraph/index.ts +1 -0
  75. package/src/langgraph/middleware.ts +698 -95
  76. package/src/langgraph/state-schema.ts +34 -0
  77. package/src/langgraph/types.ts +57 -0
  78. package/src/langgraph/utils.ts +76 -19
  79. package/src/langgraph-middlewares.ts +55 -0
  80. package/tsconfig.json +11 -2
  81. package/tsdown.config.ts +18 -0
  82. package/vitest.config.mjs +11 -0
  83. package/CHANGELOG.md +0 -2169
  84. package/dist/chunk-JLGOQLGS.mjs +0 -433
  85. package/dist/chunk-JLGOQLGS.mjs.map +0 -1
  86. package/dist/index.d.ts +0 -2
  87. package/dist/index.js +0 -1
  88. package/dist/index.js.map +0 -1
  89. package/dist/index.mjs.map +0 -1
  90. package/dist/langchain-2645763d.d.ts +0 -427
  91. package/dist/langchain.d.ts +0 -6
  92. package/dist/langchain.js +0 -284
  93. package/dist/langchain.js.map +0 -1
  94. package/dist/langgraph/index.d.ts +0 -11
  95. package/dist/langgraph.js +0 -476
  96. package/dist/langgraph.js.map +0 -1
  97. package/dist/langgraph.mjs.map +0 -1
  98. package/jest.config.js +0 -5
  99. package/tsup.config.ts +0 -23
@@ -0,0 +1,57 @@
1
+ import { Annotation, MessagesAnnotation } from "@langchain/langgraph";
2
+
3
+ //#region src/langgraph/types.ts
4
+ const CopilotKitPropertiesAnnotation = Annotation.Root({
5
+ actions: Annotation,
6
+ context: Annotation,
7
+ interceptedToolCalls: Annotation,
8
+ originalAIMessageId: Annotation
9
+ });
10
+ const CopilotKitStateAnnotation = Annotation.Root({
11
+ copilotkit: Annotation,
12
+ ...MessagesAnnotation.spec
13
+ });
14
+ const COPILOTKIT_PROPERTIES_JSON_SCHEMA = {
15
+ type: "object",
16
+ properties: {
17
+ actions: {
18
+ type: "array",
19
+ items: {}
20
+ },
21
+ context: {
22
+ type: "array",
23
+ items: {
24
+ type: "object",
25
+ properties: {
26
+ description: { type: "string" },
27
+ value: { type: "string" }
28
+ },
29
+ required: ["description", "value"]
30
+ }
31
+ },
32
+ interceptedToolCalls: {
33
+ type: "array",
34
+ items: {}
35
+ },
36
+ originalAIMessageId: { type: "string" }
37
+ }
38
+ };
39
+ /**
40
+ * Standard Schema describing the `copilotkit` field on agent state.
41
+ *
42
+ * CopilotKit populates these fields at runtime, so the schema accepts any
43
+ * input shape. Use it with `new StateSchema({ ...CopilotKitStateSchema.fields })`.
44
+ */
45
+ const CopilotKitPropertiesSchema = { "~standard": {
46
+ version: 1,
47
+ vendor: "@copilotkit/sdk-js",
48
+ validate: (value) => ({ value }),
49
+ jsonSchema: {
50
+ input: () => COPILOTKIT_PROPERTIES_JSON_SCHEMA,
51
+ output: () => COPILOTKIT_PROPERTIES_JSON_SCHEMA
52
+ }
53
+ } };
54
+
55
+ //#endregion
56
+ export { CopilotKitPropertiesAnnotation, CopilotKitPropertiesSchema, CopilotKitStateAnnotation };
57
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","names":[],"sources":["../../src/langgraph/types.ts"],"sourcesContent":["import { Annotation, MessagesAnnotation } from \"@langchain/langgraph\";\n\nexport interface StandardSerializableSchema<Input, Output = Input> {\n readonly \"~standard\": {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (\n value: unknown,\n ) => { value: Output } | { issues: ReadonlyArray<{ message: string }> };\n readonly types?: { readonly input: Input; readonly output: Output };\n readonly jsonSchema: {\n readonly input: (options: { target: string }) => Record<string, unknown>;\n readonly output: (options: { target: string }) => Record<string, unknown>;\n };\n };\n}\n\nexport const CopilotKitPropertiesAnnotation = Annotation.Root({\n actions: Annotation<any[]>,\n context: Annotation<{ description: string; value: string }[]>,\n interceptedToolCalls: Annotation<any[]>,\n originalAIMessageId: Annotation<string>,\n});\n\nexport const CopilotKitStateAnnotation = Annotation.Root({\n copilotkit: Annotation<typeof CopilotKitPropertiesAnnotation.State>,\n ...MessagesAnnotation.spec,\n});\n\nconst COPILOTKIT_PROPERTIES_JSON_SCHEMA = {\n type: \"object\",\n properties: {\n actions: { type: \"array\", items: {} },\n context: {\n type: \"array\",\n items: {\n type: \"object\",\n properties: {\n description: { type: \"string\" },\n value: { type: \"string\" },\n },\n required: [\"description\", \"value\"],\n },\n },\n interceptedToolCalls: { type: \"array\", items: {} },\n originalAIMessageId: { type: \"string\" },\n },\n};\n\n/**\n * Standard Schema describing the `copilotkit` field on agent state.\n *\n * CopilotKit populates these fields at runtime, so the schema accepts any\n * input shape. Use it with `new StateSchema({ ...CopilotKitStateSchema.fields })`.\n */\nexport const CopilotKitPropertiesSchema: StandardSerializableSchema<\n typeof CopilotKitPropertiesAnnotation.State\n> = {\n \"~standard\": {\n version: 1,\n vendor: \"@copilotkit/sdk-js\",\n validate: (value) => ({\n value: value as typeof CopilotKitPropertiesAnnotation.State,\n }),\n jsonSchema: {\n input: () => COPILOTKIT_PROPERTIES_JSON_SCHEMA,\n output: () => COPILOTKIT_PROPERTIES_JSON_SCHEMA,\n },\n },\n};\n\nexport interface IntermediateStateConfig {\n stateKey: string;\n tool: string;\n toolArgument?: string;\n}\n\nexport interface OptionsConfig {\n emitToolCalls?: boolean | string | string[];\n emitMessages?: boolean;\n emitAll?: boolean;\n emitIntermediateState?: IntermediateStateConfig[];\n}\n\nexport type CopilotKitState = typeof CopilotKitStateAnnotation.State;\nexport type CopilotKitProperties = typeof CopilotKitPropertiesAnnotation.State;\n"],"mappings":";;;AAiBA,MAAa,iCAAiC,WAAW,KAAK;CAC5D,SAAS;CACT,SAAS;CACT,sBAAsB;CACtB,qBAAqB;CACtB,CAAC;AAEF,MAAa,4BAA4B,WAAW,KAAK;CACvD,YAAY;CACZ,GAAG,mBAAmB;CACvB,CAAC;AAEF,MAAM,oCAAoC;CACxC,MAAM;CACN,YAAY;EACV,SAAS;GAAE,MAAM;GAAS,OAAO,EAAE;GAAE;EACrC,SAAS;GACP,MAAM;GACN,OAAO;IACL,MAAM;IACN,YAAY;KACV,aAAa,EAAE,MAAM,UAAU;KAC/B,OAAO,EAAE,MAAM,UAAU;KAC1B;IACD,UAAU,CAAC,eAAe,QAAQ;IACnC;GACF;EACD,sBAAsB;GAAE,MAAM;GAAS,OAAO,EAAE;GAAE;EAClD,qBAAqB,EAAE,MAAM,UAAU;EACxC;CACF;;;;;;;AAQD,MAAa,6BAET,EACF,aAAa;CACX,SAAS;CACT,QAAQ;CACR,WAAW,WAAW,EACb,OACR;CACD,YAAY;EACV,aAAa;EACb,cAAc;EACf;CACF,EACF"}
@@ -0,0 +1,297 @@
1
+ const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
2
+ let _langchain_langgraph = require("@langchain/langgraph");
3
+ let _langchain_core_callbacks_dispatch = require("@langchain/core/callbacks/dispatch");
4
+ let _copilotkit_shared = require("@copilotkit/shared");
5
+ let _langchain_core_tools = require("@langchain/core/tools");
6
+ let _langchain_core_messages = require("@langchain/core/messages");
7
+
8
+ //#region src/langgraph/utils.ts
9
+ /**
10
+ * Customize the LangGraph configuration for use in CopilotKit.
11
+ *
12
+ * To the CopilotKit SDK, run:
13
+ *
14
+ * ```bash
15
+ * npm install @copilotkit/sdk-js
16
+ * ```
17
+ *
18
+ * ### Examples
19
+ *
20
+ * Disable emitting messages and tool calls:
21
+ *
22
+ * ```typescript
23
+ * import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js";
24
+ *
25
+ * config = copilotkitCustomizeConfig(
26
+ * config,
27
+ * emitMessages=false,
28
+ * emitToolCalls=false
29
+ * )
30
+ * ```
31
+ *
32
+ * To emit a tool call as streaming LangGraph state, pass the destination key in state,
33
+ * the tool name and optionally the tool argument. (If you don't pass the argument name,
34
+ * all arguments are emitted under the state key.)
35
+ *
36
+ * ```typescript
37
+ * import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js";
38
+ *
39
+ * config = copilotkitCustomizeConfig(
40
+ * config,
41
+ * emitIntermediateState=[
42
+ * {
43
+ * "stateKey": "steps",
44
+ * "tool": "SearchTool",
45
+ * "toolArgument": "steps",
46
+ * },
47
+ * ],
48
+ * )
49
+ * ```
50
+ */
51
+ function copilotkitCustomizeConfig(baseConfig, options) {
52
+ if (baseConfig && typeof baseConfig !== "object") throw new _copilotkit_shared.CopilotKitMisuseError({ message: "baseConfig must be an object or null/undefined" });
53
+ if (options && typeof options !== "object") throw new _copilotkit_shared.CopilotKitMisuseError({ message: "options must be an object when provided" });
54
+ if (options?.emitIntermediateState) {
55
+ if (!Array.isArray(options.emitIntermediateState)) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "emitIntermediateState must be an array when provided" });
56
+ options.emitIntermediateState.forEach((state, index) => {
57
+ if (!state || typeof state !== "object") throw new _copilotkit_shared.CopilotKitMisuseError({ message: `emitIntermediateState[${index}] must be an object` });
58
+ if (!state.stateKey || typeof state.stateKey !== "string") throw new _copilotkit_shared.CopilotKitMisuseError({ message: `emitIntermediateState[${index}] must have a valid 'stateKey' string property` });
59
+ if (!state.tool || typeof state.tool !== "string") throw new _copilotkit_shared.CopilotKitMisuseError({ message: `emitIntermediateState[${index}] must have a valid 'tool' string property` });
60
+ if (state.toolArgument && typeof state.toolArgument !== "string") throw new _copilotkit_shared.CopilotKitMisuseError({ message: `emitIntermediateState[${index}].toolArgument must be a string when provided` });
61
+ });
62
+ }
63
+ try {
64
+ const metadata = baseConfig?.metadata || {};
65
+ if (options?.emitAll) {
66
+ metadata["copilotkit:emit-tool-calls"] = true;
67
+ metadata["copilotkit:emit-messages"] = true;
68
+ } else {
69
+ if (options?.emitToolCalls !== void 0) metadata["copilotkit:emit-tool-calls"] = options.emitToolCalls;
70
+ if (options?.emitMessages !== void 0) metadata["copilotkit:emit-messages"] = options.emitMessages;
71
+ }
72
+ if (options?.emitIntermediateState) metadata["copilotkit:emit-intermediate-state"] = options.emitIntermediateState.map((state) => ({
73
+ tool: state.tool,
74
+ tool_argument: state.toolArgument,
75
+ state_key: state.stateKey
76
+ }));
77
+ baseConfig = baseConfig || {};
78
+ return {
79
+ ...baseConfig,
80
+ metadata
81
+ };
82
+ } catch (error) {
83
+ throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Failed to customize config: ${error instanceof Error ? error.message : String(error)}` });
84
+ }
85
+ }
86
+ /**
87
+ * Exits the current agent after the run completes. Calling copilotkit_exit() will
88
+ * not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
89
+ * the run completes.
90
+ *
91
+ * ### Examples
92
+ *
93
+ * ```typescript
94
+ * import { copilotkitExit } from "@copilotkit/sdk-js";
95
+ *
96
+ * async function myNode(state: Any):
97
+ * await copilotkitExit(config)
98
+ * return state
99
+ * ```
100
+ */
101
+ async function copilotkitExit(config) {
102
+ if (!config) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "LangGraph configuration is required for copilotkitExit" });
103
+ try {
104
+ await (0, _langchain_core_callbacks_dispatch.dispatchCustomEvent)("copilotkit_exit", {}, config);
105
+ } catch (error) {
106
+ throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Failed to dispatch exit event: ${error instanceof Error ? error.message : String(error)}` });
107
+ }
108
+ }
109
+ /**
110
+ * Emits intermediate state to CopilotKit. Useful if you have a longer running node and you want to
111
+ * update the user with the current state of the node.
112
+ *
113
+ * ### Examples
114
+ *
115
+ * ```typescript
116
+ * import { copilotkitEmitState } from "@copilotkit/sdk-js";
117
+ *
118
+ * for (let i = 0; i < 10; i++) {
119
+ * await someLongRunningOperation(i);
120
+ * await copilotkitEmitState(config, { progress: i });
121
+ * }
122
+ * ```
123
+ */
124
+ async function copilotkitEmitState(config, state) {
125
+ if (!config) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "LangGraph configuration is required for copilotkitEmitState" });
126
+ if (state === void 0) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "State is required for copilotkitEmitState" });
127
+ try {
128
+ await (0, _langchain_core_callbacks_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_intermediate_state", state, config);
129
+ } catch (error) {
130
+ throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Failed to emit state: ${error instanceof Error ? error.message : String(error)}` });
131
+ }
132
+ }
133
+ /**
134
+ * Manually emits a message to CopilotKit. Useful in longer running nodes to update the user.
135
+ * Important: You still need to return the messages from the node.
136
+ *
137
+ * ### Examples
138
+ *
139
+ * ```typescript
140
+ * import { copilotkitEmitMessage } from "@copilotkit/sdk-js";
141
+ *
142
+ * const message = "Step 1 of 10 complete";
143
+ * await copilotkitEmitMessage(config, message);
144
+ *
145
+ * // Return the message from the node
146
+ * return {
147
+ * "messages": [AIMessage(content=message)]
148
+ * }
149
+ * ```
150
+ */
151
+ async function copilotkitEmitMessage(config, message) {
152
+ if (!config) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "LangGraph configuration is required for copilotkitEmitMessage" });
153
+ if (!message || typeof message !== "string") throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Message must be a non-empty string for copilotkitEmitMessage" });
154
+ try {
155
+ await (0, _langchain_core_callbacks_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_message", {
156
+ message,
157
+ message_id: (0, _copilotkit_shared.randomId)(),
158
+ role: "assistant"
159
+ }, config);
160
+ } catch (error) {
161
+ throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Failed to emit message: ${error instanceof Error ? error.message : String(error)}` });
162
+ }
163
+ }
164
+ /**
165
+ * Manually emits a tool call to CopilotKit.
166
+ *
167
+ * ### Examples
168
+ *
169
+ * ```typescript
170
+ * import { copilotkitEmitToolCall } from "@copilotkit/sdk-js";
171
+ *
172
+ * const autoId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 });
173
+ *
174
+ * // With a custom ID for correlation/idempotency:
175
+ * const customId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 }, { toolCallId: "my-custom-id" });
176
+ * ```
177
+ *
178
+ * @returns The tool call ID used for the emitted call — equals `options.toolCallId`
179
+ * when provided, otherwise a randomly generated ID.
180
+ */
181
+ async function copilotkitEmitToolCall(config, name, args, options) {
182
+ if (!config) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "LangGraph configuration is required for copilotkitEmitToolCall" });
183
+ if (typeof name !== "string" || name.trim().length === 0) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Tool name must be a non-empty string for copilotkitEmitToolCall" });
184
+ if (options?.toolCallId !== void 0 && (typeof options.toolCallId !== "string" || options.toolCallId.trim().length === 0)) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Tool call id must be a non-empty string when provided for copilotkitEmitToolCall" });
185
+ if (args === void 0) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Tool arguments are required for copilotkitEmitToolCall" });
186
+ try {
187
+ JSON.stringify(args);
188
+ } catch (error) {
189
+ throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Tool arguments for '${name}' are not JSON-serializable: ${error instanceof Error ? error.message : String(error)}` });
190
+ }
191
+ const toolCallId = options?.toolCallId ?? (0, _copilotkit_shared.randomUUID)();
192
+ try {
193
+ await (0, _langchain_core_callbacks_dispatch.dispatchCustomEvent)("copilotkit_manually_emit_tool_call", {
194
+ name,
195
+ args,
196
+ id: toolCallId
197
+ }, config);
198
+ } catch (error) {
199
+ const wrapped = /* @__PURE__ */ new Error(`copilotkitEmitToolCall dispatch failed for tool="${name}" id="${toolCallId}": ${error instanceof Error ? error.message : String(error)}`);
200
+ wrapped.cause = error;
201
+ throw wrapped;
202
+ }
203
+ return toolCallId;
204
+ }
205
+ function convertActionToDynamicStructuredTool(actionInput) {
206
+ if (!actionInput) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Action input is required but was not provided" });
207
+ if (!actionInput.name || typeof actionInput.name !== "string") throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Action must have a valid 'name' property of type string" });
208
+ if (actionInput.description == void 0 || actionInput.description == null || typeof actionInput.description !== "string") throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Action '${actionInput.name}' must have a valid 'description' property of type string` });
209
+ if (!actionInput.parameters) throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Action '${actionInput.name}' must have a 'parameters' property` });
210
+ try {
211
+ return new _langchain_core_tools.DynamicStructuredTool({
212
+ name: actionInput.name,
213
+ description: actionInput.description,
214
+ schema: (0, _copilotkit_shared.convertJsonSchemaToZodSchema)(actionInput.parameters, true),
215
+ func: async () => {
216
+ return "";
217
+ }
218
+ });
219
+ } catch (error) {
220
+ throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Failed to convert action '${actionInput.name}' to DynamicStructuredTool: ${error instanceof Error ? error.message : String(error)}` });
221
+ }
222
+ }
223
+ /**
224
+ * Use this function to convert a list of actions you get from state
225
+ * to a list of dynamic structured tools.
226
+ *
227
+ * ### Examples
228
+ *
229
+ * ```typescript
230
+ * import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js";
231
+ *
232
+ * const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);
233
+ * ```
234
+ */
235
+ function convertActionsToDynamicStructuredTools(actions) {
236
+ if (!Array.isArray(actions)) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Actions must be an array" });
237
+ return actions.map((action, index) => {
238
+ try {
239
+ return convertActionToDynamicStructuredTool(action.type === "function" ? action.function : action);
240
+ } catch (error) {
241
+ throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Failed to convert action at index ${index}: ${error instanceof Error ? error.message : String(error)}` });
242
+ }
243
+ });
244
+ }
245
+ function copilotKitInterrupt({ message, action, args }) {
246
+ if (!message && !action) throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Either message or action (and optional arguments) must be provided for copilotKitInterrupt" });
247
+ if (action && typeof action !== "string") throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Action must be a string when provided to copilotKitInterrupt" });
248
+ if (message && typeof message !== "string") throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Message must be a string when provided to copilotKitInterrupt" });
249
+ if (args && typeof args !== "object") throw new _copilotkit_shared.CopilotKitMisuseError({ message: "Args must be an object when provided to copilotKitInterrupt" });
250
+ let interruptValues = null;
251
+ let interruptMessage = null;
252
+ let answer = null;
253
+ try {
254
+ if (message) {
255
+ interruptValues = message;
256
+ interruptMessage = new _langchain_core_messages.AIMessage({
257
+ content: message,
258
+ id: (0, _copilotkit_shared.randomId)()
259
+ });
260
+ } else {
261
+ interruptMessage = new _langchain_core_messages.AIMessage({
262
+ content: "",
263
+ tool_calls: [{
264
+ id: (0, _copilotkit_shared.randomId)(),
265
+ name: action,
266
+ args: args ?? {}
267
+ }]
268
+ });
269
+ interruptValues = {
270
+ action,
271
+ args: args ?? {}
272
+ };
273
+ }
274
+ const response = (0, _langchain_langgraph.interrupt)({
275
+ __copilotkit_interrupt_value__: interruptValues,
276
+ __copilotkit_messages__: [interruptMessage]
277
+ });
278
+ answer = response[response.length - 1].content;
279
+ return {
280
+ answer,
281
+ messages: response
282
+ };
283
+ } catch (error) {
284
+ throw new _copilotkit_shared.CopilotKitMisuseError({ message: `Failed to create interrupt: ${error instanceof Error ? error.message : String(error)}` });
285
+ }
286
+ }
287
+
288
+ //#endregion
289
+ exports.convertActionToDynamicStructuredTool = convertActionToDynamicStructuredTool;
290
+ exports.convertActionsToDynamicStructuredTools = convertActionsToDynamicStructuredTools;
291
+ exports.copilotKitInterrupt = copilotKitInterrupt;
292
+ exports.copilotkitCustomizeConfig = copilotkitCustomizeConfig;
293
+ exports.copilotkitEmitMessage = copilotkitEmitMessage;
294
+ exports.copilotkitEmitState = copilotkitEmitState;
295
+ exports.copilotkitEmitToolCall = copilotkitEmitToolCall;
296
+ exports.copilotkitExit = copilotkitExit;
297
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.cjs","names":["CopilotKitMisuseError","DynamicStructuredTool","AIMessage"],"sources":["../../src/langgraph/utils.ts"],"sourcesContent":["import type { RunnableConfig } from \"@langchain/core/runnables\";\nimport { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\nimport {\n convertJsonSchemaToZodSchema,\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\";\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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AmB,IAAIE,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,IAAIF,yCAAsB,EAC9B,SAAS,+BAA+B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,IAC/F,CAAC"}
@@ -0,0 +1,209 @@
1
+ import { OptionsConfig } from "./types.cjs";
2
+ import { RunnableConfig } from "@langchain/core/runnables";
3
+ import { DynamicStructuredTool } from "@langchain/core/tools";
4
+
5
+ //#region src/langgraph/utils.d.ts
6
+ /**
7
+ * Customize the LangGraph configuration for use in CopilotKit.
8
+ *
9
+ * To the CopilotKit SDK, run:
10
+ *
11
+ * ```bash
12
+ * npm install @copilotkit/sdk-js
13
+ * ```
14
+ *
15
+ * ### Examples
16
+ *
17
+ * Disable emitting messages and tool calls:
18
+ *
19
+ * ```typescript
20
+ * import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js";
21
+ *
22
+ * config = copilotkitCustomizeConfig(
23
+ * config,
24
+ * emitMessages=false,
25
+ * emitToolCalls=false
26
+ * )
27
+ * ```
28
+ *
29
+ * To emit a tool call as streaming LangGraph state, pass the destination key in state,
30
+ * the tool name and optionally the tool argument. (If you don't pass the argument name,
31
+ * all arguments are emitted under the state key.)
32
+ *
33
+ * ```typescript
34
+ * import { copilotkitCustomizeConfig } from "@copilotkit/sdk-js";
35
+ *
36
+ * config = copilotkitCustomizeConfig(
37
+ * config,
38
+ * emitIntermediateState=[
39
+ * {
40
+ * "stateKey": "steps",
41
+ * "tool": "SearchTool",
42
+ * "toolArgument": "steps",
43
+ * },
44
+ * ],
45
+ * )
46
+ * ```
47
+ */
48
+ declare function copilotkitCustomizeConfig(
49
+ /**
50
+ * The LangChain/LangGraph configuration to customize.
51
+ */
52
+ baseConfig: RunnableConfig,
53
+ /**
54
+ * Configuration options:
55
+ * - `emitMessages: boolean?`
56
+ * Configure how messages are emitted. By default, all messages are emitted. Pass false to
57
+ * disable emitting messages.
58
+ * - `emitToolCalls: boolean | string | string[]?`
59
+ * Configure how tool calls are emitted. By default, all tool calls are emitted. Pass false to
60
+ * disable emitting tool calls. Pass a string or list of strings to emit only specific tool calls.
61
+ * - `emitIntermediateState: IntermediateStateConfig[]?`
62
+ * Lets you emit tool calls as streaming LangGraph state.
63
+ */
64
+ options?: OptionsConfig): RunnableConfig;
65
+ /**
66
+ * Exits the current agent after the run completes. Calling copilotkit_exit() will
67
+ * not immediately stop the agent. Instead, it signals to CopilotKit to stop the agent after
68
+ * the run completes.
69
+ *
70
+ * ### Examples
71
+ *
72
+ * ```typescript
73
+ * import { copilotkitExit } from "@copilotkit/sdk-js";
74
+ *
75
+ * async function myNode(state: Any):
76
+ * await copilotkitExit(config)
77
+ * return state
78
+ * ```
79
+ */
80
+ declare function copilotkitExit(
81
+ /**
82
+ * The LangChain/LangGraph configuration.
83
+ */
84
+ config: RunnableConfig): Promise<void>;
85
+ /**
86
+ * Emits intermediate state to CopilotKit. Useful if you have a longer running node and you want to
87
+ * update the user with the current state of the node.
88
+ *
89
+ * ### Examples
90
+ *
91
+ * ```typescript
92
+ * import { copilotkitEmitState } from "@copilotkit/sdk-js";
93
+ *
94
+ * for (let i = 0; i < 10; i++) {
95
+ * await someLongRunningOperation(i);
96
+ * await copilotkitEmitState(config, { progress: i });
97
+ * }
98
+ * ```
99
+ */
100
+ declare function copilotkitEmitState(
101
+ /**
102
+ * The LangChain/LangGraph configuration.
103
+ */
104
+ config: RunnableConfig,
105
+ /**
106
+ * The state to emit.
107
+ */
108
+ state: any): Promise<void>;
109
+ /**
110
+ * Manually emits a message to CopilotKit. Useful in longer running nodes to update the user.
111
+ * Important: You still need to return the messages from the node.
112
+ *
113
+ * ### Examples
114
+ *
115
+ * ```typescript
116
+ * import { copilotkitEmitMessage } from "@copilotkit/sdk-js";
117
+ *
118
+ * const message = "Step 1 of 10 complete";
119
+ * await copilotkitEmitMessage(config, message);
120
+ *
121
+ * // Return the message from the node
122
+ * return {
123
+ * "messages": [AIMessage(content=message)]
124
+ * }
125
+ * ```
126
+ */
127
+ declare function copilotkitEmitMessage(
128
+ /**
129
+ * The LangChain/LangGraph configuration.
130
+ */
131
+ config: RunnableConfig,
132
+ /**
133
+ * The message to emit.
134
+ */
135
+ message: string): Promise<void>;
136
+ /**
137
+ * Manually emits a tool call to CopilotKit.
138
+ *
139
+ * ### Examples
140
+ *
141
+ * ```typescript
142
+ * import { copilotkitEmitToolCall } from "@copilotkit/sdk-js";
143
+ *
144
+ * const autoId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 });
145
+ *
146
+ * // With a custom ID for correlation/idempotency:
147
+ * const customId = await copilotkitEmitToolCall(config, "SearchTool", { steps: 10 }, { toolCallId: "my-custom-id" });
148
+ * ```
149
+ *
150
+ * @returns The tool call ID used for the emitted call — equals `options.toolCallId`
151
+ * when provided, otherwise a randomly generated ID.
152
+ */
153
+ declare function copilotkitEmitToolCall(
154
+ /**
155
+ * The LangChain/LangGraph configuration.
156
+ */
157
+ config: RunnableConfig,
158
+ /**
159
+ * The name of the tool to emit.
160
+ */
161
+ name: string,
162
+ /**
163
+ * The arguments to emit.
164
+ */
165
+ args: any,
166
+ /**
167
+ * Options for the tool call emission.
168
+ */
169
+ options?: {
170
+ /**
171
+ * Optional tool call ID. If not provided, a random ID is generated.
172
+ * When provided, this ID is used as the toolCallId and parentMessageId
173
+ * in AG-UI protocol events. The caller is responsible for ensuring uniqueness.
174
+ */
175
+ toolCallId?: string;
176
+ }): Promise<string>;
177
+ declare function convertActionToDynamicStructuredTool(actionInput: any): DynamicStructuredTool<any>;
178
+ /**
179
+ * Use this function to convert a list of actions you get from state
180
+ * to a list of dynamic structured tools.
181
+ *
182
+ * ### Examples
183
+ *
184
+ * ```typescript
185
+ * import { convertActionsToDynamicStructuredTools } from "@copilotkit/sdk-js";
186
+ *
187
+ * const tools = convertActionsToDynamicStructuredTools(state.copilotkit.actions);
188
+ * ```
189
+ */
190
+ declare function convertActionsToDynamicStructuredTools(
191
+ /**
192
+ * The list of actions to convert.
193
+ */
194
+ actions: any[]): DynamicStructuredTool<any>[];
195
+ declare function copilotKitInterrupt({
196
+ message,
197
+ action,
198
+ args
199
+ }: {
200
+ message?: string;
201
+ action?: string;
202
+ args?: Record<string, any>;
203
+ }): {
204
+ answer: any;
205
+ messages: any;
206
+ };
207
+ //#endregion
208
+ export { convertActionToDynamicStructuredTool, convertActionsToDynamicStructuredTools, copilotKitInterrupt, copilotkitCustomizeConfig, copilotkitEmitMessage, copilotkitEmitState, copilotkitEmitToolCall, copilotkitExit };
209
+ //# sourceMappingURL=utils.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.cts","names":[],"sources":["../../src/langgraph/utils.ts"],"mappings":";;;;;;;AAuDA;;;;;;;;;;;;;;;AAwHA;;;;;;;;;AAmCA;;;;;;;;;;AAoDA;;;;;;iBA/MgB,yBAAA;;;;AAId,UAAA,EAAY,cAAA;;;;;;;;;;;;AAYZ,OAAA,GAAU,aAAA,GACT,cAAA;AAmUH;;;;;AA0DA;;;;;AAyBA;;;;;AAnFA,iBA5NsB,cAAA;;;;AAIpB,MAAA,EAAQ,cAAA,GAAc,OAAA;;;;;;;;;;;;;;;;iBA+BF,mBAAA;;;;AAIpB,MAAA,EAAQ,cAAA;;;;AAIR,KAAA,QAAU,OAAA;;;;;;;;;;;;;;;;;;;iBA4CU,qBAAA;;;;AAIpB,MAAA,EAAQ,cAAA;;;;AAIR,OAAA,WAAe,OAAA;;;;;;;;;;;;;;;;;;iBA2CK,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"}