@copilotkit/sdk-js 1.9.2-next.2 → 1.9.2-next.21
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 +119 -0
- package/dist/chunk-Q5S2AURJ.mjs +311 -0
- package/dist/chunk-Q5S2AURJ.mjs.map +1 -0
- package/dist/langchain.js +191 -43
- package/dist/langchain.js.map +1 -1
- package/dist/langchain.mjs +1 -1
- package/dist/langgraph.js +245 -74
- package/dist/langgraph.js.map +1 -1
- package/dist/langgraph.mjs +1 -1
- package/package.json +3 -3
- package/src/__tests__/error-handling.test.ts +207 -0
- package/src/langgraph.ts +265 -68
- package/dist/chunk-PMVIZ7R2.mjs +0 -140
- package/dist/chunk-PMVIZ7R2.mjs.map +0 -1
package/dist/chunk-PMVIZ7R2.mjs
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
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 } 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
|
-
const metadata = (baseConfig == null ? void 0 : baseConfig.metadata) || {};
|
|
19
|
-
if (options == null ? void 0 : options.emitAll) {
|
|
20
|
-
metadata["copilotkit:emit-tool-calls"] = true;
|
|
21
|
-
metadata["copilotkit:emit-messages"] = true;
|
|
22
|
-
} else {
|
|
23
|
-
if ((options == null ? void 0 : options.emitToolCalls) !== void 0) {
|
|
24
|
-
metadata["copilotkit:emit-tool-calls"] = options.emitToolCalls;
|
|
25
|
-
}
|
|
26
|
-
if ((options == null ? void 0 : options.emitMessages) !== void 0) {
|
|
27
|
-
metadata["copilotkit:emit-messages"] = options.emitMessages;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (options == null ? void 0 : options.emitIntermediateState) {
|
|
31
|
-
const snakeCaseIntermediateState = options.emitIntermediateState.map((state) => ({
|
|
32
|
-
tool: state.tool,
|
|
33
|
-
tool_argument: state.toolArgument,
|
|
34
|
-
state_key: state.stateKey
|
|
35
|
-
}));
|
|
36
|
-
metadata["copilotkit:emit-intermediate-state"] = snakeCaseIntermediateState;
|
|
37
|
-
}
|
|
38
|
-
baseConfig = baseConfig || {};
|
|
39
|
-
return {
|
|
40
|
-
...baseConfig,
|
|
41
|
-
metadata
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
__name(copilotkitCustomizeConfig, "copilotkitCustomizeConfig");
|
|
45
|
-
async function copilotkitExit(config) {
|
|
46
|
-
await dispatchCustomEvent("copilotkit_exit", {}, config);
|
|
47
|
-
}
|
|
48
|
-
__name(copilotkitExit, "copilotkitExit");
|
|
49
|
-
async function copilotkitEmitState(config, state) {
|
|
50
|
-
await dispatchCustomEvent("copilotkit_manually_emit_intermediate_state", state, config);
|
|
51
|
-
}
|
|
52
|
-
__name(copilotkitEmitState, "copilotkitEmitState");
|
|
53
|
-
async function copilotkitEmitMessage(config, message) {
|
|
54
|
-
await dispatchCustomEvent("copilotkit_manually_emit_message", {
|
|
55
|
-
message,
|
|
56
|
-
message_id: randomId(),
|
|
57
|
-
role: "assistant"
|
|
58
|
-
}, config);
|
|
59
|
-
}
|
|
60
|
-
__name(copilotkitEmitMessage, "copilotkitEmitMessage");
|
|
61
|
-
async function copilotkitEmitToolCall(config, name, args) {
|
|
62
|
-
await dispatchCustomEvent("copilotkit_manually_emit_tool_call", {
|
|
63
|
-
name,
|
|
64
|
-
args,
|
|
65
|
-
id: randomId()
|
|
66
|
-
}, config);
|
|
67
|
-
}
|
|
68
|
-
__name(copilotkitEmitToolCall, "copilotkitEmitToolCall");
|
|
69
|
-
function convertActionToDynamicStructuredTool(actionInput) {
|
|
70
|
-
return new DynamicStructuredTool({
|
|
71
|
-
name: actionInput.name,
|
|
72
|
-
description: actionInput.description,
|
|
73
|
-
schema: convertJsonSchemaToZodSchema(actionInput.parameters, true),
|
|
74
|
-
func: async () => {
|
|
75
|
-
return "";
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
__name(convertActionToDynamicStructuredTool, "convertActionToDynamicStructuredTool");
|
|
80
|
-
function convertActionsToDynamicStructuredTools(actions) {
|
|
81
|
-
return actions.map((action) => convertActionToDynamicStructuredTool(action));
|
|
82
|
-
}
|
|
83
|
-
__name(convertActionsToDynamicStructuredTools, "convertActionsToDynamicStructuredTools");
|
|
84
|
-
function copilotKitInterrupt({ message, action, args }) {
|
|
85
|
-
if (!message && !action) {
|
|
86
|
-
throw new Error("Either message or action (and optional arguments) must be provided");
|
|
87
|
-
}
|
|
88
|
-
let interruptValues = null;
|
|
89
|
-
let interruptMessage = null;
|
|
90
|
-
let answer = null;
|
|
91
|
-
if (message) {
|
|
92
|
-
interruptValues = message;
|
|
93
|
-
interruptMessage = new AIMessage({
|
|
94
|
-
content: message,
|
|
95
|
-
id: randomId()
|
|
96
|
-
});
|
|
97
|
-
} else {
|
|
98
|
-
const toolId = randomId();
|
|
99
|
-
interruptMessage = new AIMessage({
|
|
100
|
-
content: "",
|
|
101
|
-
tool_calls: [
|
|
102
|
-
{
|
|
103
|
-
id: toolId,
|
|
104
|
-
name: action,
|
|
105
|
-
args: args ?? {}
|
|
106
|
-
}
|
|
107
|
-
]
|
|
108
|
-
});
|
|
109
|
-
interruptValues = {
|
|
110
|
-
action,
|
|
111
|
-
args: args ?? {}
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
const response = interrupt({
|
|
115
|
-
__copilotkit_interrupt_value__: interruptValues,
|
|
116
|
-
__copilotkit_messages__: [
|
|
117
|
-
interruptMessage
|
|
118
|
-
]
|
|
119
|
-
});
|
|
120
|
-
answer = response[response.length - 1].content;
|
|
121
|
-
return {
|
|
122
|
-
answer,
|
|
123
|
-
messages: response
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
__name(copilotKitInterrupt, "copilotKitInterrupt");
|
|
127
|
-
|
|
128
|
-
export {
|
|
129
|
-
CopilotKitPropertiesAnnotation,
|
|
130
|
-
CopilotKitStateAnnotation,
|
|
131
|
-
copilotkitCustomizeConfig,
|
|
132
|
-
copilotkitExit,
|
|
133
|
-
copilotkitEmitState,
|
|
134
|
-
copilotkitEmitMessage,
|
|
135
|
-
copilotkitEmitToolCall,
|
|
136
|
-
convertActionToDynamicStructuredTool,
|
|
137
|
-
convertActionsToDynamicStructuredTools,
|
|
138
|
-
copilotKitInterrupt
|
|
139
|
-
};
|
|
140
|
-
//# sourceMappingURL=chunk-PMVIZ7R2.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/langgraph.ts"],"sourcesContent":["import { RunnableConfig } from \"@langchain/core/runnables\";\nimport { dispatchCustomEvent } from \"@langchain/core/callbacks/dispatch\";\nimport { convertJsonSchemaToZodSchema, randomId } 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 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}\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 await dispatchCustomEvent(\"copilotkit_exit\", {}, config);\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 await dispatchCustomEvent(\"copilotkit_manually_emit_intermediate_state\", state, config);\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 await dispatchCustomEvent(\n \"copilotkit_manually_emit_message\",\n { message, message_id: randomId(), role: \"assistant\" },\n config,\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 await dispatchCustomEvent(\n \"copilotkit_manually_emit_tool_call\",\n { name, args, id: randomId() },\n config,\n );\n}\n\nexport function convertActionToDynamicStructuredTool(actionInput: any): DynamicStructuredTool<any> {\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}\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 return actions.map((action) => convertActionToDynamicStructuredTool(action));\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 Error(\"Either message or action (and optional arguments) must be provided\");\n }\n\n let interruptValues = null;\n let interruptMessage = null;\n let answer = null;\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}\n"],"mappings":";;;;AACA,SAASA,2BAA2B;AACpC,SAASC,8BAA8BC,gBAAgB;AACvD,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,QAAMC,YAAWF,yCAAYE,aAAY,CAAC;AAE1C,MAAID,mCAASE,SAAS;AACpBD,aAAS,4BAAA,IAAgC;AACzCA,aAAS,0BAAA,IAA8B;EACzC,OAAO;AACL,SAAID,mCAASG,mBAAkBC,QAAW;AACxCH,eAAS,4BAAA,IAAgCD,QAAQG;IACnD;AACA,SAAIH,mCAASK,kBAAiBD,QAAW;AACvCH,eAAS,0BAAA,IAA8BD,QAAQK;IACjD;EACF;AAEA,MAAIL,mCAASM,uBAAuB;AAClC,UAAMC,6BAA6BP,QAAQM,sBAAsBE,IAAI,CAACC,WAAW;MAC/EC,MAAMD,MAAMC;MACZC,eAAeF,MAAMG;MACrBC,WAAWJ,MAAMK;IACnB,EAAA;AAEAb,aAAS,oCAAA,IAAwCM;EACnD;AAEAR,eAAaA,cAAc,CAAC;AAE5B,SAAO;IAC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MACTE,YAAY;QAAC;UAAEnB,IAAIkB;UAAQpB,MAAMY;UAAQX,MAAMA,QAAQ,CAAC;QAAE;;IAC5D,CAAA;AACAc,sBAAkB;MAChBH;MACAX,MAAMA,QAAQ,CAAC;IACjB;EACF;AAEA,QAAMqB,WAAWC,UAAU;IACzBC,gCAAgCT;IAChCU,yBAAyB;MAACT;;EAC5B,CAAA;AACAC,WAASK,SAASA,SAASI,SAAS,CAAA,EAAGP;AAEvC,SAAO;IACLF;IACAU,UAAUL;EACZ;AACF;AAzCgBT;","names":["dispatchCustomEvent","convertJsonSchemaToZodSchema","randomId","Annotation","MessagesAnnotation","interrupt","DynamicStructuredTool","AIMessage","CopilotKitPropertiesAnnotation","Annotation","Root","actions","CopilotKitStateAnnotation","copilotkit","MessagesAnnotation","spec","copilotkitCustomizeConfig","baseConfig","options","metadata","emitAll","emitToolCalls","undefined","emitMessages","emitIntermediateState","snakeCaseIntermediateState","map","state","tool","tool_argument","toolArgument","state_key","stateKey","copilotkitExit","config","dispatchCustomEvent","copilotkitEmitState","copilotkitEmitMessage","message","message_id","randomId","role","copilotkitEmitToolCall","name","args","id","convertActionToDynamicStructuredTool","actionInput","DynamicStructuredTool","description","schema","convertJsonSchemaToZodSchema","parameters","func","convertActionsToDynamicStructuredTools","action","copilotKitInterrupt","Error","interruptValues","interruptMessage","answer","AIMessage","content","toolId","tool_calls","response","interrupt","__copilotkit_interrupt_value__","__copilotkit_messages__","length","messages"]}
|