@copilotkit/sdk-js 1.56.4 → 1.56.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/langgraph/middleware.cjs +144 -11
- package/dist/langgraph/middleware.cjs.map +1 -1
- package/dist/langgraph/middleware.d.cts +68 -1
- package/dist/langgraph/middleware.d.cts.map +1 -1
- package/dist/langgraph/middleware.d.mts +68 -1
- package/dist/langgraph/middleware.d.mts.map +1 -1
- package/dist/langgraph/middleware.mjs +143 -12
- package/dist/langgraph/middleware.mjs.map +1 -1
- package/dist/langgraph/state-schema.d.cts +22 -16
- package/dist/langgraph/state-schema.d.cts.map +1 -1
- package/dist/langgraph/state-schema.d.mts +22 -16
- package/dist/langgraph/state-schema.d.mts.map +1 -1
- package/dist/langgraph/types.d.cts +107 -50
- package/dist/langgraph/types.d.cts.map +1 -1
- package/dist/langgraph/types.d.mts +107 -50
- package/dist/langgraph/types.d.mts.map +1 -1
- package/dist/langgraph.cjs +3 -1
- package/dist/langgraph.d.cts +2 -2
- package/dist/langgraph.d.mts +2 -2
- package/dist/langgraph.mjs +2 -2
- package/package.json +6 -6
- package/src/langgraph/__tests__/middleware.test.ts +611 -0
- package/src/langgraph/middleware.ts +210 -15
|
@@ -4,6 +4,94 @@ let zod = require("zod");
|
|
|
4
4
|
zod = require_runtime.__toESM(zod);
|
|
5
5
|
|
|
6
6
|
//#region src/langgraph/middleware.ts
|
|
7
|
+
/**
|
|
8
|
+
* Augment a Standard-Schema–compatible schema (e.g. Zod) with a
|
|
9
|
+
* `~standard.jsonSchema.input` hook so LangGraph's
|
|
10
|
+
* `getJsonSchemaFromSchema` (called from `StateSchema.getJsonSchema`)
|
|
11
|
+
* can serialize the field.
|
|
12
|
+
*
|
|
13
|
+
* Without this, Zod v4 fields carry `~standard.validate` + `vendor` only,
|
|
14
|
+
* and `isStandardJSONSchema()` returns false, so the field is silently
|
|
15
|
+
* dropped from the graph's `output_schema`. That makes AG-UI
|
|
16
|
+
* `STATE_SNAPSHOT` events filter the field out of the payload sent to
|
|
17
|
+
* the frontend even though the underlying thread state has the value.
|
|
18
|
+
*
|
|
19
|
+
* Use this on any custom state field you want visible to the frontend
|
|
20
|
+
* via `useAgent().state.*`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { zodState } from "@copilotkit/sdk-js/langgraph";
|
|
25
|
+
*
|
|
26
|
+
* const stateSchema = z.object({
|
|
27
|
+
* todos: zodState(z.array(TodoSchema).default(() => [])),
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function zodState(schema) {
|
|
32
|
+
const std = schema["~standard"];
|
|
33
|
+
if (std && typeof std === "object" && !("jsonSchema" in std)) {
|
|
34
|
+
let cached;
|
|
35
|
+
std.jsonSchema = { input: () => {
|
|
36
|
+
if (cached) return cached;
|
|
37
|
+
try {
|
|
38
|
+
const maybeV4ToJsonSchema = zod.toJSONSchema;
|
|
39
|
+
cached = typeof maybeV4ToJsonSchema === "function" ? maybeV4ToJsonSchema(schema) : {};
|
|
40
|
+
} catch {
|
|
41
|
+
cached = {};
|
|
42
|
+
}
|
|
43
|
+
return cached;
|
|
44
|
+
} };
|
|
45
|
+
}
|
|
46
|
+
return schema;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Internal/framework state keys that should never be auto-surfaced to the
|
|
50
|
+
* LLM as user-facing state. These are reducer-managed message buckets,
|
|
51
|
+
* CopilotKit/AG-UI plumbing, or graph-internal scaffolding.
|
|
52
|
+
*/
|
|
53
|
+
const RESERVED_STATE_KEYS = new Set([
|
|
54
|
+
"messages",
|
|
55
|
+
"copilotkit",
|
|
56
|
+
"ag-ui",
|
|
57
|
+
"tools",
|
|
58
|
+
"structured_response",
|
|
59
|
+
"thread_id",
|
|
60
|
+
"remaining_steps"
|
|
61
|
+
]);
|
|
62
|
+
const buildStateNote = (state, expose) => {
|
|
63
|
+
if (expose === false) return null;
|
|
64
|
+
const allow = Array.isArray(expose) ? new Set(expose) : null;
|
|
65
|
+
const snapshot = {};
|
|
66
|
+
for (const key of Object.keys(state)) {
|
|
67
|
+
if (allow ? !allow.has(key) : RESERVED_STATE_KEYS.has(key) || key.startsWith("_")) continue;
|
|
68
|
+
const value = state[key];
|
|
69
|
+
if (value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0 || typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) continue;
|
|
70
|
+
snapshot[key] = value;
|
|
71
|
+
}
|
|
72
|
+
if (Object.keys(snapshot).length === 0) return null;
|
|
73
|
+
let body;
|
|
74
|
+
try {
|
|
75
|
+
body = JSON.stringify(snapshot, null, 2);
|
|
76
|
+
} catch {
|
|
77
|
+
body = String(snapshot);
|
|
78
|
+
}
|
|
79
|
+
return `Current agent state:\n${body}`;
|
|
80
|
+
};
|
|
81
|
+
const applyStateNote = (request, expose) => {
|
|
82
|
+
const note = buildStateNote(request.state ?? {}, expose);
|
|
83
|
+
if (!note) return request;
|
|
84
|
+
const existing = request.systemPrompt;
|
|
85
|
+
if (existing == null) return {
|
|
86
|
+
...request,
|
|
87
|
+
systemPrompt: new langchain.SystemMessage({ content: note })
|
|
88
|
+
};
|
|
89
|
+
const baseText = typeof existing === "string" ? existing : typeof existing.content === "string" ? existing.content : String(existing.content);
|
|
90
|
+
return {
|
|
91
|
+
...request,
|
|
92
|
+
systemPrompt: new langchain.SystemMessage({ content: `${baseText}\n\n${note}` })
|
|
93
|
+
};
|
|
94
|
+
};
|
|
7
95
|
const createAppContextBeforeAgent = (state, runtime) => {
|
|
8
96
|
const messages = state.messages;
|
|
9
97
|
if (!messages || messages.length === 0) return;
|
|
@@ -55,15 +143,38 @@ const createAppContextBeforeAgent = (state, runtime) => {
|
|
|
55
143
|
messages: updatedMessages
|
|
56
144
|
};
|
|
57
145
|
};
|
|
58
|
-
|
|
146
|
+
/**
|
|
147
|
+
* CopilotKit Middleware for LangGraph agents.
|
|
148
|
+
*
|
|
149
|
+
* Enables:
|
|
150
|
+
* - Dynamic frontend tools from state.tools
|
|
151
|
+
* - Context provided from CopilotKit useCopilotReadable
|
|
152
|
+
*
|
|
153
|
+
* Works with any agent (prebuilt or custom).
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* import { createAgent } from "langchain";
|
|
158
|
+
* import { copilotkitMiddleware } from "@copilotkit/sdk-js/langgraph";
|
|
159
|
+
*
|
|
160
|
+
* const agent = createAgent({
|
|
161
|
+
* model: "gpt-4o",
|
|
162
|
+
* tools: [backendTool],
|
|
163
|
+
* middleware: [copilotkitMiddleware],
|
|
164
|
+
* });
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
const copilotKitStateSchema = zod.object({ copilotkit: zodState(zod.object({
|
|
168
|
+
actions: zod.array(zod.any()),
|
|
169
|
+
context: zod.any().optional(),
|
|
170
|
+
interceptedToolCalls: zod.array(zod.any()).optional(),
|
|
171
|
+
originalAIMessageId: zod.string().optional()
|
|
172
|
+
}).optional()) });
|
|
173
|
+
const buildMiddlewareInput = (exposeState) => ({
|
|
59
174
|
name: "CopilotKitMiddleware",
|
|
60
|
-
stateSchema:
|
|
61
|
-
actions: zod.array(zod.any()),
|
|
62
|
-
context: zod.any().optional(),
|
|
63
|
-
interceptedToolCalls: zod.array(zod.any()).optional(),
|
|
64
|
-
originalAIMessageId: zod.string().optional()
|
|
65
|
-
}).optional() }),
|
|
175
|
+
stateSchema: copilotKitStateSchema,
|
|
66
176
|
wrapModelCall: async (request, handler) => {
|
|
177
|
+
request = applyStateNote(request, exposeState);
|
|
67
178
|
const frontendTools = request.state["copilotkit"]?.actions ?? [];
|
|
68
179
|
if (frontendTools.length === 0) return handler(request);
|
|
69
180
|
const mergedTools = [...request.tools || [], ...frontendTools];
|
|
@@ -128,12 +239,34 @@ const middlewareInput = {
|
|
|
128
239
|
}
|
|
129
240
|
};
|
|
130
241
|
}
|
|
242
|
+
});
|
|
243
|
+
/**
|
|
244
|
+
* Build a CopilotKit middleware instance with custom options.
|
|
245
|
+
*
|
|
246
|
+
* Use this when you want to override the default state-exposure behavior
|
|
247
|
+
* (for example to hide a sensitive key, or to use an explicit allowlist).
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* import { createCopilotkitMiddleware } from "@copilotkit/sdk-js/langgraph";
|
|
252
|
+
*
|
|
253
|
+
* const middleware = createCopilotkitMiddleware({
|
|
254
|
+
* exposeState: ["liked", "todos"],
|
|
255
|
+
* });
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
const createCopilotkitMiddleware = (options = {}) => {
|
|
259
|
+
return (0, langchain.createMiddleware)(buildMiddlewareInput(options.exposeState ?? false));
|
|
131
260
|
};
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
261
|
+
/**
|
|
262
|
+
* Default CopilotKit middleware singleton — does NOT surface user state
|
|
263
|
+
* to the LLM. Pass `exposeState: true` (or an allowlist) to
|
|
264
|
+
* {@link createCopilotkitMiddleware} to opt in.
|
|
265
|
+
*/
|
|
266
|
+
const copilotkitMiddleware = createCopilotkitMiddleware();
|
|
136
267
|
|
|
137
268
|
//#endregion
|
|
138
269
|
exports.copilotkitMiddleware = copilotkitMiddleware;
|
|
270
|
+
exports.createCopilotkitMiddleware = createCopilotkitMiddleware;
|
|
271
|
+
exports.zodState = zodState;
|
|
139
272
|
//# sourceMappingURL=middleware.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.cjs","names":["SystemMessage","z","AIMessage"],"sources":["../../src/langgraph/middleware.ts"],"sourcesContent":["import { createMiddleware, AIMessage, SystemMessage } from \"langchain\";\nimport type { InteropZodObject } from \"@langchain/core/utils/types\";\nimport * as z from \"zod\";\n\nconst createAppContextBeforeAgent = (state, runtime) => {\n const messages = state.messages;\n\n if (!messages || messages.length === 0) {\n return;\n }\n\n // Get app context from runtime\n const appContext = state[\"copilotkit\"]?.context ?? runtime?.context;\n\n // Check if appContext is missing or empty\n const isEmptyContext =\n !appContext ||\n (typeof appContext === \"string\" && appContext.trim() === \"\") ||\n (typeof appContext === \"object\" && Object.keys(appContext).length === 0);\n\n if (isEmptyContext) {\n return;\n }\n\n // Create the context content\n const contextContent =\n typeof appContext === \"string\"\n ? appContext\n : JSON.stringify(appContext, null, 2);\n const contextMessageContent = `App Context:\\n${contextContent}`;\n const contextMessagePrefix = \"App Context:\\n\";\n\n // Helper to get message content as string\n const getContentString = (msg: any): string | null => {\n if (typeof msg.content === \"string\") return msg.content;\n if (Array.isArray(msg.content) && msg.content[0]?.text)\n return msg.content[0].text;\n return null;\n };\n\n // Find the first system/developer message (not our context message) to determine\n // where to insert our context message (right after it)\n let firstSystemIndex = -1;\n\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg._getType?.();\n if (type === \"system\" || type === \"developer\") {\n const content = getContentString(msg);\n // Skip if this is our own context message\n if (content?.startsWith(contextMessagePrefix)) {\n continue;\n }\n firstSystemIndex = i;\n break;\n }\n }\n\n // Check if our context message already exists\n let existingContextIndex = -1;\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg._getType?.();\n if (type === \"system\" || type === \"developer\") {\n const content = getContentString(msg);\n if (content?.startsWith(contextMessagePrefix)) {\n existingContextIndex = i;\n break;\n }\n }\n }\n\n // Create the context message\n const contextMessage = new SystemMessage({ content: contextMessageContent });\n\n let updatedMessages;\n\n if (existingContextIndex !== -1) {\n // Replace existing context message\n updatedMessages = [...messages];\n updatedMessages[existingContextIndex] = contextMessage;\n } else {\n // Insert after the first system message, or at position 0 if no system message\n const insertIndex = firstSystemIndex !== -1 ? firstSystemIndex + 1 : 0;\n updatedMessages = [\n ...messages.slice(0, insertIndex),\n contextMessage,\n ...messages.slice(insertIndex),\n ];\n }\n\n return {\n ...state,\n messages: updatedMessages,\n };\n};\n\n/**\n * CopilotKit Middleware for LangGraph agents.\n *\n * Enables:\n * - Dynamic frontend tools from state.tools\n * - Context provided from CopilotKit useCopilotReadable\n *\n * Works with any agent (prebuilt or custom).\n *\n * @example\n * ```typescript\n * import { createAgent } from \"langchain\";\n * import { copilotkitMiddleware } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const agent = createAgent({\n * model: \"gpt-4o\",\n * tools: [backendTool],\n * middleware: [copilotkitMiddleware],\n * });\n * ```\n */\nconst copilotKitStateSchema = z.object({\n copilotkit: z\n .object({\n actions: z.array(z.any()),\n context: z.any().optional(),\n interceptedToolCalls: z.array(z.any()).optional(),\n originalAIMessageId: z.string().optional(),\n })\n .optional(),\n});\n\nconst middlewareInput = {\n name: \"CopilotKitMiddleware\",\n\n stateSchema: copilotKitStateSchema as unknown as InteropZodObject,\n\n // Inject frontend tools before model call\n wrapModelCall: async (request, handler) => {\n const frontendTools = request.state[\"copilotkit\"]?.actions ?? [];\n\n if (frontendTools.length === 0) {\n return handler(request);\n }\n\n const existingTools = request.tools || [];\n const mergedTools = [...existingTools, ...frontendTools];\n\n return handler({\n ...request,\n tools: mergedTools,\n });\n },\n\n beforeAgent: createAppContextBeforeAgent,\n\n // Restore frontend tool calls to AIMessage before agent exits\n afterAgent: (state) => {\n const interceptedToolCalls = state[\"copilotkit\"]?.interceptedToolCalls;\n const originalMessageId = state[\"copilotkit\"]?.originalAIMessageId;\n\n if (!interceptedToolCalls?.length || !originalMessageId) {\n return;\n }\n\n let messageFound = false;\n const updatedMessages = state.messages.map((msg: any) => {\n if (AIMessage.isInstance(msg) && msg.id === originalMessageId) {\n messageFound = true;\n const existingToolCalls = msg.tool_calls || [];\n return new AIMessage({\n content: msg.content,\n tool_calls: [...existingToolCalls, ...interceptedToolCalls],\n id: msg.id,\n });\n }\n return msg;\n });\n\n // Only clear intercepted state if we successfully restored the tool calls\n if (!messageFound) {\n console.warn(\n `CopilotKit: Could not find message with id ${originalMessageId} to restore tool calls`,\n );\n return;\n }\n\n return {\n messages: updatedMessages,\n copilotkit: {\n ...state[\"copilotkit\"],\n interceptedToolCalls: undefined,\n originalAIMessageId: undefined,\n },\n };\n },\n\n // Intercept frontend tool calls after model returns, before ToolNode executes\n afterModel: (state) => {\n const frontendTools = state[\"copilotkit\"]?.actions ?? [];\n if (frontendTools.length === 0) return;\n\n const frontendToolNames = new Set(\n frontendTools.map((t: any) => t.function?.name || t.name),\n );\n\n const lastMessage = state.messages[state.messages.length - 1];\n if (!AIMessage.isInstance(lastMessage) || !lastMessage.tool_calls?.length) {\n return;\n }\n\n const backendToolCalls: any[] = [];\n const frontendToolCalls: any[] = [];\n\n for (const call of lastMessage.tool_calls) {\n if (frontendToolNames.has(call.name)) {\n frontendToolCalls.push(call);\n } else {\n backendToolCalls.push(call);\n }\n }\n\n if (frontendToolCalls.length === 0) return;\n\n const updatedAIMessage = new AIMessage({\n content: lastMessage.content,\n tool_calls: backendToolCalls,\n id: lastMessage.id,\n });\n\n return {\n messages: [...state.messages.slice(0, -1), updatedAIMessage],\n copilotkit: {\n ...state[\"copilotkit\"],\n interceptedToolCalls: frontendToolCalls,\n originalAIMessageId: lastMessage.id,\n },\n };\n },\n} as any;\nconst createCopilotKitMiddleware = () => {\n return createMiddleware(middlewareInput);\n};\n\nexport const copilotkitMiddleware = createCopilotKitMiddleware();\n"],"mappings":";;;;;;AAIA,MAAM,+BAA+B,OAAO,YAAY;CACtD,MAAM,WAAW,MAAM;AAEvB,KAAI,CAAC,YAAY,SAAS,WAAW,EACnC;CAIF,MAAM,aAAa,MAAM,eAAe,WAAW,SAAS;AAQ5D,KAJE,CAAC,cACA,OAAO,eAAe,YAAY,WAAW,MAAM,KAAK,MACxD,OAAO,eAAe,YAAY,OAAO,KAAK,WAAW,CAAC,WAAW,EAGtE;CAQF,MAAM,wBAAwB,iBAH5B,OAAO,eAAe,WAClB,aACA,KAAK,UAAU,YAAY,MAAM,EAAE;CAEzC,MAAM,uBAAuB;CAG7B,MAAM,oBAAoB,QAA4B;AACpD,MAAI,OAAO,IAAI,YAA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EAClC,MAAM,oBAA2B,EAAE;AAEnC,OAAK,MAAM,QAAQ,YAAY,WAC7B,KAAI,kBAAkB,IAAI,KAAK,KAAK,CAClC,mBAAkB,KAAK,KAAK;MAE5B,kBAAiB,KAAK,KAAK;AAI/B,MAAI,kBAAkB,WAAW,EAAG;EAEpC,MAAM,mBAAmB,IAAIA,oBAAU;GACrC,SAAS,YAAY;GACrB,YAAY;GACZ,IAAI,YAAY;GACjB,CAAC;AAEF,SAAO;GACL,UAAU,CAAC,GAAG,MAAM,SAAS,MAAM,GAAG,GAAG,EAAE,iBAAiB;GAC5D,YAAY;IACV,GAAG,MAAM;IACT,sBAAsB;IACtB,qBAAqB,YAAY;IAClC;GACF;;CAEJ;AACD,MAAM,mCAAmC;AACvC,wCAAwB,gBAAgB;;AAG1C,MAAa,uBAAuB,4BAA4B"}
|
|
1
|
+
{"version":3,"file":"middleware.cjs","names":["z","SystemMessage","AIMessage"],"sources":["../../src/langgraph/middleware.ts"],"sourcesContent":["import { createMiddleware, AIMessage, SystemMessage } from \"langchain\";\nimport type { InteropZodObject } from \"@langchain/core/utils/types\";\nimport type {\n StandardJSONSchemaV1,\n StandardSchemaV1,\n} from \"@standard-schema/spec\";\nimport * as z from \"zod\";\n\ntype WithJsonSchema<T> = T extends { \"~standard\": infer S }\n ? Omit<T, \"~standard\"> & {\n \"~standard\": S &\n StandardJSONSchemaV1.Props<\n S extends StandardSchemaV1.Props<infer I, any> ? I : unknown,\n S extends StandardSchemaV1.Props<any, infer O> ? O : unknown\n >;\n }\n : T;\n\n/**\n * Augment a Standard-Schema–compatible schema (e.g. Zod) with a\n * `~standard.jsonSchema.input` hook so LangGraph's\n * `getJsonSchemaFromSchema` (called from `StateSchema.getJsonSchema`)\n * can serialize the field.\n *\n * Without this, Zod v4 fields carry `~standard.validate` + `vendor` only,\n * and `isStandardJSONSchema()` returns false, so the field is silently\n * dropped from the graph's `output_schema`. That makes AG-UI\n * `STATE_SNAPSHOT` events filter the field out of the payload sent to\n * the frontend even though the underlying thread state has the value.\n *\n * Use this on any custom state field you want visible to the frontend\n * via `useAgent().state.*`.\n *\n * @example\n * ```ts\n * import { zodState } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const stateSchema = z.object({\n * todos: zodState(z.array(TodoSchema).default(() => [])),\n * });\n * ```\n */\nexport function zodState<T extends object>(schema: T): WithJsonSchema<T> {\n const std = (schema as { \"~standard\"?: { jsonSchema?: unknown } })[\n \"~standard\"\n ];\n if (std && typeof std === \"object\" && !(\"jsonSchema\" in std)) {\n let cached: Record<string, unknown> | undefined;\n std.jsonSchema = {\n input: () => {\n if (cached) return cached;\n // Prefer zod-v4's native `toJSONSchema` when available. Falls back to\n // an empty object, which is sufficient for the field to appear in the\n // graph's output_schema (langgraph-api treats it as an opaque field).\n try {\n const maybeV4ToJsonSchema = (\n z as unknown as {\n toJSONSchema?: (s: unknown) => Record<string, unknown>;\n }\n ).toJSONSchema;\n cached =\n typeof maybeV4ToJsonSchema === \"function\"\n ? maybeV4ToJsonSchema(schema)\n : {};\n } catch {\n cached = {};\n }\n return cached;\n },\n };\n }\n return schema as WithJsonSchema<T>;\n}\n\n/**\n * Internal/framework state keys that should never be auto-surfaced to the\n * LLM as user-facing state. These are reducer-managed message buckets,\n * CopilotKit/AG-UI plumbing, or graph-internal scaffolding.\n */\nconst RESERVED_STATE_KEYS: ReadonlySet<string> = new Set([\n \"messages\",\n \"copilotkit\",\n \"ag-ui\",\n \"tools\",\n \"structured_response\",\n \"thread_id\",\n \"remaining_steps\",\n]);\n\n/**\n * Controls how user-defined state keys are surfaced into the LLM prompt\n * on every model call. Off by default to avoid leaking arbitrary state\n * into prompts; opt in explicitly.\n *\n * - `false` (default) — never surface state.\n * - `true` — every state key not in the reserved internal set and not\n * prefixed with `_` is JSON-serialized into a \"Current agent state:\"\n * note appended to the system prompt.\n * - `string[]` — only surface the named keys (use this when you want\n * explicit control over what the LLM sees, e.g. `[\"liked\", \"todos\"]`).\n */\nexport type ExposeStateOption = boolean | readonly string[];\n\nconst buildStateNote = (\n state: Record<string, unknown>,\n expose: ExposeStateOption,\n): string | null => {\n if (expose === false) return null;\n\n const allow: ReadonlySet<string> | null = Array.isArray(expose)\n ? new Set(expose)\n : null;\n\n const snapshot: Record<string, unknown> = {};\n for (const key of Object.keys(state)) {\n if (\n allow\n ? !allow.has(key)\n : RESERVED_STATE_KEYS.has(key) || key.startsWith(\"_\")\n ) {\n continue;\n }\n const value = state[key];\n if (\n value === undefined ||\n value === null ||\n value === \"\" ||\n (Array.isArray(value) && value.length === 0) ||\n (typeof value === \"object\" &&\n !Array.isArray(value) &&\n Object.keys(value as Record<string, unknown>).length === 0)\n ) {\n continue;\n }\n snapshot[key] = value;\n }\n\n if (Object.keys(snapshot).length === 0) return null;\n\n let body: string;\n try {\n body = JSON.stringify(snapshot, null, 2);\n } catch {\n body = String(snapshot);\n }\n return `Current agent state:\\n${body}`;\n};\n\nconst applyStateNote = (request: any, expose: ExposeStateOption): any => {\n const note = buildStateNote(\n (request.state ?? {}) as Record<string, unknown>,\n expose,\n );\n if (!note) return request;\n\n const existing = request.systemPrompt;\n if (existing == null) {\n return { ...request, systemPrompt: new SystemMessage({ content: note }) };\n }\n // existing may be a string OR a SystemMessage\n const baseText =\n typeof existing === \"string\"\n ? existing\n : typeof existing.content === \"string\"\n ? existing.content\n : String(existing.content);\n return {\n ...request,\n systemPrompt: new SystemMessage({ content: `${baseText}\\n\\n${note}` }),\n };\n};\n\nconst createAppContextBeforeAgent = (state, runtime) => {\n const messages = state.messages;\n\n if (!messages || messages.length === 0) {\n return;\n }\n\n // Get app context from runtime\n const appContext = state[\"copilotkit\"]?.context ?? runtime?.context;\n\n // Check if appContext is missing or empty\n const isEmptyContext =\n !appContext ||\n (typeof appContext === \"string\" && appContext.trim() === \"\") ||\n (typeof appContext === \"object\" && Object.keys(appContext).length === 0);\n\n if (isEmptyContext) {\n return;\n }\n\n // Create the context content\n const contextContent =\n typeof appContext === \"string\"\n ? appContext\n : JSON.stringify(appContext, null, 2);\n const contextMessageContent = `App Context:\\n${contextContent}`;\n const contextMessagePrefix = \"App Context:\\n\";\n\n // Helper to get message content as string\n const getContentString = (msg: any): string | null => {\n if (typeof msg.content === \"string\") return msg.content;\n if (Array.isArray(msg.content) && msg.content[0]?.text)\n return msg.content[0].text;\n return null;\n };\n\n // Find the first system/developer message (not our context message) to determine\n // where to insert our context message (right after it)\n let firstSystemIndex = -1;\n\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg._getType?.();\n if (type === \"system\" || type === \"developer\") {\n const content = getContentString(msg);\n // Skip if this is our own context message\n if (content?.startsWith(contextMessagePrefix)) {\n continue;\n }\n firstSystemIndex = i;\n break;\n }\n }\n\n // Check if our context message already exists\n let existingContextIndex = -1;\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg._getType?.();\n if (type === \"system\" || type === \"developer\") {\n const content = getContentString(msg);\n if (content?.startsWith(contextMessagePrefix)) {\n existingContextIndex = i;\n break;\n }\n }\n }\n\n // Create the context message\n const contextMessage = new SystemMessage({ content: contextMessageContent });\n\n let updatedMessages;\n\n if (existingContextIndex !== -1) {\n // Replace existing context message\n updatedMessages = [...messages];\n updatedMessages[existingContextIndex] = contextMessage;\n } else {\n // Insert after the first system message, or at position 0 if no system message\n const insertIndex = firstSystemIndex !== -1 ? firstSystemIndex + 1 : 0;\n updatedMessages = [\n ...messages.slice(0, insertIndex),\n contextMessage,\n ...messages.slice(insertIndex),\n ];\n }\n\n return {\n ...state,\n messages: updatedMessages,\n };\n};\n\n/**\n * CopilotKit Middleware for LangGraph agents.\n *\n * Enables:\n * - Dynamic frontend tools from state.tools\n * - Context provided from CopilotKit useCopilotReadable\n *\n * Works with any agent (prebuilt or custom).\n *\n * @example\n * ```typescript\n * import { createAgent } from \"langchain\";\n * import { copilotkitMiddleware } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const agent = createAgent({\n * model: \"gpt-4o\",\n * tools: [backendTool],\n * middleware: [copilotkitMiddleware],\n * });\n * ```\n */\nconst copilotKitStateSchema = z.object({\n copilotkit: zodState(\n z\n .object({\n actions: z.array(z.any()),\n context: z.any().optional(),\n interceptedToolCalls: z.array(z.any()).optional(),\n originalAIMessageId: z.string().optional(),\n })\n .optional(),\n ),\n});\n\nconst buildMiddlewareInput = (exposeState: ExposeStateOption) => ({\n name: \"CopilotKitMiddleware\",\n\n stateSchema: copilotKitStateSchema as unknown as InteropZodObject,\n\n // Inject frontend tools and surface user state before model call\n wrapModelCall: async (request: any, handler: (req: any) => Promise<any>) => {\n request = applyStateNote(request, exposeState);\n const frontendTools = request.state[\"copilotkit\"]?.actions ?? [];\n\n if (frontendTools.length === 0) {\n return handler(request);\n }\n\n const existingTools = request.tools || [];\n const mergedTools = [...existingTools, ...frontendTools];\n\n return handler({\n ...request,\n tools: mergedTools,\n });\n },\n\n beforeAgent: createAppContextBeforeAgent,\n\n // Restore frontend tool calls to AIMessage before agent exits\n afterAgent: (state) => {\n const interceptedToolCalls = state[\"copilotkit\"]?.interceptedToolCalls;\n const originalMessageId = state[\"copilotkit\"]?.originalAIMessageId;\n\n if (!interceptedToolCalls?.length || !originalMessageId) {\n return;\n }\n\n let messageFound = false;\n const updatedMessages = state.messages.map((msg: any) => {\n if (AIMessage.isInstance(msg) && msg.id === originalMessageId) {\n messageFound = true;\n const existingToolCalls = msg.tool_calls || [];\n return new AIMessage({\n content: msg.content,\n tool_calls: [...existingToolCalls, ...interceptedToolCalls],\n id: msg.id,\n });\n }\n return msg;\n });\n\n // Only clear intercepted state if we successfully restored the tool calls\n if (!messageFound) {\n console.warn(\n `CopilotKit: Could not find message with id ${originalMessageId} to restore tool calls`,\n );\n return;\n }\n\n return {\n messages: updatedMessages,\n copilotkit: {\n ...state[\"copilotkit\"],\n interceptedToolCalls: undefined,\n originalAIMessageId: undefined,\n },\n };\n },\n\n // Intercept frontend tool calls after model returns, before ToolNode executes\n afterModel: (state) => {\n const frontendTools = state[\"copilotkit\"]?.actions ?? [];\n if (frontendTools.length === 0) return;\n\n const frontendToolNames = new Set(\n frontendTools.map((t: any) => t.function?.name || t.name),\n );\n\n const lastMessage = state.messages[state.messages.length - 1];\n if (!AIMessage.isInstance(lastMessage) || !lastMessage.tool_calls?.length) {\n return;\n }\n\n const backendToolCalls: any[] = [];\n const frontendToolCalls: any[] = [];\n\n for (const call of lastMessage.tool_calls) {\n if (frontendToolNames.has(call.name)) {\n frontendToolCalls.push(call);\n } else {\n backendToolCalls.push(call);\n }\n }\n\n if (frontendToolCalls.length === 0) return;\n\n const updatedAIMessage = new AIMessage({\n content: lastMessage.content,\n tool_calls: backendToolCalls,\n id: lastMessage.id,\n });\n\n return {\n messages: [...state.messages.slice(0, -1), updatedAIMessage],\n copilotkit: {\n ...state[\"copilotkit\"],\n interceptedToolCalls: frontendToolCalls,\n originalAIMessageId: lastMessage.id,\n },\n };\n },\n});\n\n/**\n * Build a CopilotKit middleware instance with custom options.\n *\n * Use this when you want to override the default state-exposure behavior\n * (for example to hide a sensitive key, or to use an explicit allowlist).\n *\n * @example\n * ```typescript\n * import { createCopilotkitMiddleware } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const middleware = createCopilotkitMiddleware({\n * exposeState: [\"liked\", \"todos\"],\n * });\n * ```\n */\nexport const createCopilotkitMiddleware = (\n options: { exposeState?: ExposeStateOption } = {},\n) => {\n const exposeState = options.exposeState ?? false;\n return createMiddleware(buildMiddlewareInput(exposeState) as any);\n};\n\n/**\n * Default CopilotKit middleware singleton — does NOT surface user state\n * to the LLM. Pass `exposeState: true` (or an allowlist) to\n * {@link createCopilotkitMiddleware} to opt in.\n */\nexport const copilotkitMiddleware = createCopilotkitMiddleware();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,SAAgB,SAA2B,QAA8B;CACvE,MAAM,MAAO,OACX;AAEF,KAAI,OAAO,OAAO,QAAQ,YAAY,EAAE,gBAAgB,MAAM;EAC5D,IAAI;AACJ,MAAI,aAAa,EACf,aAAa;A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;;;AAQnE,MAAa,uBAAuB,4BAA4B"}
|
|
@@ -1,8 +1,75 @@
|
|
|
1
1
|
import * as langchain from "langchain";
|
|
2
2
|
import * as _langchain_core_tools0 from "@langchain/core/tools";
|
|
3
|
+
import { StandardJSONSchemaV1, StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
4
|
|
|
4
5
|
//#region src/langgraph/middleware.d.ts
|
|
6
|
+
type WithJsonSchema<T> = T extends {
|
|
7
|
+
"~standard": infer S;
|
|
8
|
+
} ? Omit<T, "~standard"> & {
|
|
9
|
+
"~standard": S & StandardJSONSchemaV1.Props<S extends StandardSchemaV1.Props<infer I, any> ? I : unknown, S extends StandardSchemaV1.Props<any, infer O> ? O : unknown>;
|
|
10
|
+
} : T;
|
|
11
|
+
/**
|
|
12
|
+
* Augment a Standard-Schema–compatible schema (e.g. Zod) with a
|
|
13
|
+
* `~standard.jsonSchema.input` hook so LangGraph's
|
|
14
|
+
* `getJsonSchemaFromSchema` (called from `StateSchema.getJsonSchema`)
|
|
15
|
+
* can serialize the field.
|
|
16
|
+
*
|
|
17
|
+
* Without this, Zod v4 fields carry `~standard.validate` + `vendor` only,
|
|
18
|
+
* and `isStandardJSONSchema()` returns false, so the field is silently
|
|
19
|
+
* dropped from the graph's `output_schema`. That makes AG-UI
|
|
20
|
+
* `STATE_SNAPSHOT` events filter the field out of the payload sent to
|
|
21
|
+
* the frontend even though the underlying thread state has the value.
|
|
22
|
+
*
|
|
23
|
+
* Use this on any custom state field you want visible to the frontend
|
|
24
|
+
* via `useAgent().state.*`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { zodState } from "@copilotkit/sdk-js/langgraph";
|
|
29
|
+
*
|
|
30
|
+
* const stateSchema = z.object({
|
|
31
|
+
* todos: zodState(z.array(TodoSchema).default(() => [])),
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare function zodState<T extends object>(schema: T): WithJsonSchema<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Controls how user-defined state keys are surfaced into the LLM prompt
|
|
38
|
+
* on every model call. Off by default to avoid leaking arbitrary state
|
|
39
|
+
* into prompts; opt in explicitly.
|
|
40
|
+
*
|
|
41
|
+
* - `false` (default) — never surface state.
|
|
42
|
+
* - `true` — every state key not in the reserved internal set and not
|
|
43
|
+
* prefixed with `_` is JSON-serialized into a "Current agent state:"
|
|
44
|
+
* note appended to the system prompt.
|
|
45
|
+
* - `string[]` — only surface the named keys (use this when you want
|
|
46
|
+
* explicit control over what the LLM sees, e.g. `["liked", "todos"]`).
|
|
47
|
+
*/
|
|
48
|
+
type ExposeStateOption = boolean | readonly string[];
|
|
49
|
+
/**
|
|
50
|
+
* Build a CopilotKit middleware instance with custom options.
|
|
51
|
+
*
|
|
52
|
+
* Use this when you want to override the default state-exposure behavior
|
|
53
|
+
* (for example to hide a sensitive key, or to use an explicit allowlist).
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* import { createCopilotkitMiddleware } from "@copilotkit/sdk-js/langgraph";
|
|
58
|
+
*
|
|
59
|
+
* const middleware = createCopilotkitMiddleware({
|
|
60
|
+
* exposeState: ["liked", "todos"],
|
|
61
|
+
* });
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare const createCopilotkitMiddleware: (options?: {
|
|
65
|
+
exposeState?: ExposeStateOption;
|
|
66
|
+
}) => langchain.AgentMiddleware<undefined, undefined, unknown, readonly (_langchain_core_tools0.ClientTool | _langchain_core_tools0.ServerTool)[]>;
|
|
67
|
+
/**
|
|
68
|
+
* Default CopilotKit middleware singleton — does NOT surface user state
|
|
69
|
+
* to the LLM. Pass `exposeState: true` (or an allowlist) to
|
|
70
|
+
* {@link createCopilotkitMiddleware} to opt in.
|
|
71
|
+
*/
|
|
5
72
|
declare const copilotkitMiddleware: langchain.AgentMiddleware<undefined, undefined, unknown, readonly (_langchain_core_tools0.ClientTool | _langchain_core_tools0.ServerTool)[]>;
|
|
6
73
|
//#endregion
|
|
7
|
-
export { copilotkitMiddleware };
|
|
74
|
+
export { ExposeStateOption, copilotkitMiddleware, createCopilotkitMiddleware, zodState };
|
|
8
75
|
//# sourceMappingURL=middleware.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.cts","names":[],"sources":["../../src/langgraph/middleware.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"middleware.d.cts","names":[],"sources":["../../src/langgraph/middleware.ts"],"mappings":";;;;;KAQK,cAAA,MAAoB,CAAA;EAAY,WAAA;AAAA,IACjC,IAAA,CAAK,CAAA;EACH,WAAA,EAAa,CAAA,GACX,oBAAA,CAAqB,KAAA,CACnB,CAAA,SAAU,gBAAA,CAAiB,KAAA,iBAAsB,CAAA,YACjD,CAAA,SAAU,gBAAA,CAAiB,KAAA,iBAAsB,CAAA;AAAA,IAGvD,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;iBA0BY,QAAA,kBAAA,CAA2B,MAAA,EAAQ,CAAA,GAAI,cAAA,CAAe,CAAA;;;;;;;;;;;AAAtE;;KA2DY,iBAAA;;;;;;;;;;;;;AAAZ;;;cAmUa,0BAAA,GACX,OAAA;EAAW,WAAA,GAAc,iBAAA;AAAA,MAAwB,SAAA,CAAA,eAAA,0CAAP,sBAAA,CAAO,UAAA,GAAA,sBAAA,CAAA,UAAA;;;;;;cAWtC,oBAAA,EAAoB,SAAA,CAAA,eAAA,0CAA+B,sBAAA,CAA/B,UAAA,GAAA,sBAAA,CAAA,UAAA"}
|
|
@@ -1,8 +1,75 @@
|
|
|
1
1
|
import * as _langchain_core_tools0 from "@langchain/core/tools";
|
|
2
2
|
import * as langchain from "langchain";
|
|
3
|
+
import { StandardJSONSchemaV1, StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
4
|
|
|
4
5
|
//#region src/langgraph/middleware.d.ts
|
|
6
|
+
type WithJsonSchema<T> = T extends {
|
|
7
|
+
"~standard": infer S;
|
|
8
|
+
} ? Omit<T, "~standard"> & {
|
|
9
|
+
"~standard": S & StandardJSONSchemaV1.Props<S extends StandardSchemaV1.Props<infer I, any> ? I : unknown, S extends StandardSchemaV1.Props<any, infer O> ? O : unknown>;
|
|
10
|
+
} : T;
|
|
11
|
+
/**
|
|
12
|
+
* Augment a Standard-Schema–compatible schema (e.g. Zod) with a
|
|
13
|
+
* `~standard.jsonSchema.input` hook so LangGraph's
|
|
14
|
+
* `getJsonSchemaFromSchema` (called from `StateSchema.getJsonSchema`)
|
|
15
|
+
* can serialize the field.
|
|
16
|
+
*
|
|
17
|
+
* Without this, Zod v4 fields carry `~standard.validate` + `vendor` only,
|
|
18
|
+
* and `isStandardJSONSchema()` returns false, so the field is silently
|
|
19
|
+
* dropped from the graph's `output_schema`. That makes AG-UI
|
|
20
|
+
* `STATE_SNAPSHOT` events filter the field out of the payload sent to
|
|
21
|
+
* the frontend even though the underlying thread state has the value.
|
|
22
|
+
*
|
|
23
|
+
* Use this on any custom state field you want visible to the frontend
|
|
24
|
+
* via `useAgent().state.*`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { zodState } from "@copilotkit/sdk-js/langgraph";
|
|
29
|
+
*
|
|
30
|
+
* const stateSchema = z.object({
|
|
31
|
+
* todos: zodState(z.array(TodoSchema).default(() => [])),
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare function zodState<T extends object>(schema: T): WithJsonSchema<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Controls how user-defined state keys are surfaced into the LLM prompt
|
|
38
|
+
* on every model call. Off by default to avoid leaking arbitrary state
|
|
39
|
+
* into prompts; opt in explicitly.
|
|
40
|
+
*
|
|
41
|
+
* - `false` (default) — never surface state.
|
|
42
|
+
* - `true` — every state key not in the reserved internal set and not
|
|
43
|
+
* prefixed with `_` is JSON-serialized into a "Current agent state:"
|
|
44
|
+
* note appended to the system prompt.
|
|
45
|
+
* - `string[]` — only surface the named keys (use this when you want
|
|
46
|
+
* explicit control over what the LLM sees, e.g. `["liked", "todos"]`).
|
|
47
|
+
*/
|
|
48
|
+
type ExposeStateOption = boolean | readonly string[];
|
|
49
|
+
/**
|
|
50
|
+
* Build a CopilotKit middleware instance with custom options.
|
|
51
|
+
*
|
|
52
|
+
* Use this when you want to override the default state-exposure behavior
|
|
53
|
+
* (for example to hide a sensitive key, or to use an explicit allowlist).
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* import { createCopilotkitMiddleware } from "@copilotkit/sdk-js/langgraph";
|
|
58
|
+
*
|
|
59
|
+
* const middleware = createCopilotkitMiddleware({
|
|
60
|
+
* exposeState: ["liked", "todos"],
|
|
61
|
+
* });
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
declare const createCopilotkitMiddleware: (options?: {
|
|
65
|
+
exposeState?: ExposeStateOption;
|
|
66
|
+
}) => langchain.AgentMiddleware<undefined, undefined, unknown, readonly (_langchain_core_tools0.ClientTool | _langchain_core_tools0.ServerTool)[]>;
|
|
67
|
+
/**
|
|
68
|
+
* Default CopilotKit middleware singleton — does NOT surface user state
|
|
69
|
+
* to the LLM. Pass `exposeState: true` (or an allowlist) to
|
|
70
|
+
* {@link createCopilotkitMiddleware} to opt in.
|
|
71
|
+
*/
|
|
5
72
|
declare const copilotkitMiddleware: langchain.AgentMiddleware<undefined, undefined, unknown, readonly (_langchain_core_tools0.ClientTool | _langchain_core_tools0.ServerTool)[]>;
|
|
6
73
|
//#endregion
|
|
7
|
-
export { copilotkitMiddleware };
|
|
74
|
+
export { ExposeStateOption, copilotkitMiddleware, createCopilotkitMiddleware, zodState };
|
|
8
75
|
//# sourceMappingURL=middleware.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.mts","names":[],"sources":["../../src/langgraph/middleware.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"middleware.d.mts","names":[],"sources":["../../src/langgraph/middleware.ts"],"mappings":";;;;;KAQK,cAAA,MAAoB,CAAA;EAAY,WAAA;AAAA,IACjC,IAAA,CAAK,CAAA;EACH,WAAA,EAAa,CAAA,GACX,oBAAA,CAAqB,KAAA,CACnB,CAAA,SAAU,gBAAA,CAAiB,KAAA,iBAAsB,CAAA,YACjD,CAAA,SAAU,gBAAA,CAAiB,KAAA,iBAAsB,CAAA;AAAA,IAGvD,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;iBA0BY,QAAA,kBAAA,CAA2B,MAAA,EAAQ,CAAA,GAAI,cAAA,CAAe,CAAA;;;;;;;;;;;AAAtE;;KA2DY,iBAAA;;;;;;;;;;;;;AAAZ;;;cAmUa,0BAAA,GACX,OAAA;EAAW,WAAA,GAAc,iBAAA;AAAA,MAAwB,SAAA,CAAA,eAAA,0CAAP,sBAAA,CAAO,UAAA,GAAA,sBAAA,CAAA,UAAA;;;;;;cAWtC,oBAAA,EAAoB,SAAA,CAAA,eAAA,0CAA+B,sBAAA,CAA/B,UAAA,GAAA,sBAAA,CAAA,UAAA"}
|
|
@@ -2,6 +2,94 @@ import { AIMessage, SystemMessage, createMiddleware } from "langchain";
|
|
|
2
2
|
import * as z from "zod";
|
|
3
3
|
|
|
4
4
|
//#region src/langgraph/middleware.ts
|
|
5
|
+
/**
|
|
6
|
+
* Augment a Standard-Schema–compatible schema (e.g. Zod) with a
|
|
7
|
+
* `~standard.jsonSchema.input` hook so LangGraph's
|
|
8
|
+
* `getJsonSchemaFromSchema` (called from `StateSchema.getJsonSchema`)
|
|
9
|
+
* can serialize the field.
|
|
10
|
+
*
|
|
11
|
+
* Without this, Zod v4 fields carry `~standard.validate` + `vendor` only,
|
|
12
|
+
* and `isStandardJSONSchema()` returns false, so the field is silently
|
|
13
|
+
* dropped from the graph's `output_schema`. That makes AG-UI
|
|
14
|
+
* `STATE_SNAPSHOT` events filter the field out of the payload sent to
|
|
15
|
+
* the frontend even though the underlying thread state has the value.
|
|
16
|
+
*
|
|
17
|
+
* Use this on any custom state field you want visible to the frontend
|
|
18
|
+
* via `useAgent().state.*`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { zodState } from "@copilotkit/sdk-js/langgraph";
|
|
23
|
+
*
|
|
24
|
+
* const stateSchema = z.object({
|
|
25
|
+
* todos: zodState(z.array(TodoSchema).default(() => [])),
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function zodState(schema) {
|
|
30
|
+
const std = schema["~standard"];
|
|
31
|
+
if (std && typeof std === "object" && !("jsonSchema" in std)) {
|
|
32
|
+
let cached;
|
|
33
|
+
std.jsonSchema = { input: () => {
|
|
34
|
+
if (cached) return cached;
|
|
35
|
+
try {
|
|
36
|
+
const maybeV4ToJsonSchema = z.toJSONSchema;
|
|
37
|
+
cached = typeof maybeV4ToJsonSchema === "function" ? maybeV4ToJsonSchema(schema) : {};
|
|
38
|
+
} catch {
|
|
39
|
+
cached = {};
|
|
40
|
+
}
|
|
41
|
+
return cached;
|
|
42
|
+
} };
|
|
43
|
+
}
|
|
44
|
+
return schema;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Internal/framework state keys that should never be auto-surfaced to the
|
|
48
|
+
* LLM as user-facing state. These are reducer-managed message buckets,
|
|
49
|
+
* CopilotKit/AG-UI plumbing, or graph-internal scaffolding.
|
|
50
|
+
*/
|
|
51
|
+
const RESERVED_STATE_KEYS = new Set([
|
|
52
|
+
"messages",
|
|
53
|
+
"copilotkit",
|
|
54
|
+
"ag-ui",
|
|
55
|
+
"tools",
|
|
56
|
+
"structured_response",
|
|
57
|
+
"thread_id",
|
|
58
|
+
"remaining_steps"
|
|
59
|
+
]);
|
|
60
|
+
const buildStateNote = (state, expose) => {
|
|
61
|
+
if (expose === false) return null;
|
|
62
|
+
const allow = Array.isArray(expose) ? new Set(expose) : null;
|
|
63
|
+
const snapshot = {};
|
|
64
|
+
for (const key of Object.keys(state)) {
|
|
65
|
+
if (allow ? !allow.has(key) : RESERVED_STATE_KEYS.has(key) || key.startsWith("_")) continue;
|
|
66
|
+
const value = state[key];
|
|
67
|
+
if (value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0 || typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) continue;
|
|
68
|
+
snapshot[key] = value;
|
|
69
|
+
}
|
|
70
|
+
if (Object.keys(snapshot).length === 0) return null;
|
|
71
|
+
let body;
|
|
72
|
+
try {
|
|
73
|
+
body = JSON.stringify(snapshot, null, 2);
|
|
74
|
+
} catch {
|
|
75
|
+
body = String(snapshot);
|
|
76
|
+
}
|
|
77
|
+
return `Current agent state:\n${body}`;
|
|
78
|
+
};
|
|
79
|
+
const applyStateNote = (request, expose) => {
|
|
80
|
+
const note = buildStateNote(request.state ?? {}, expose);
|
|
81
|
+
if (!note) return request;
|
|
82
|
+
const existing = request.systemPrompt;
|
|
83
|
+
if (existing == null) return {
|
|
84
|
+
...request,
|
|
85
|
+
systemPrompt: new SystemMessage({ content: note })
|
|
86
|
+
};
|
|
87
|
+
const baseText = typeof existing === "string" ? existing : typeof existing.content === "string" ? existing.content : String(existing.content);
|
|
88
|
+
return {
|
|
89
|
+
...request,
|
|
90
|
+
systemPrompt: new SystemMessage({ content: `${baseText}\n\n${note}` })
|
|
91
|
+
};
|
|
92
|
+
};
|
|
5
93
|
const createAppContextBeforeAgent = (state, runtime) => {
|
|
6
94
|
const messages = state.messages;
|
|
7
95
|
if (!messages || messages.length === 0) return;
|
|
@@ -53,15 +141,38 @@ const createAppContextBeforeAgent = (state, runtime) => {
|
|
|
53
141
|
messages: updatedMessages
|
|
54
142
|
};
|
|
55
143
|
};
|
|
56
|
-
|
|
144
|
+
/**
|
|
145
|
+
* CopilotKit Middleware for LangGraph agents.
|
|
146
|
+
*
|
|
147
|
+
* Enables:
|
|
148
|
+
* - Dynamic frontend tools from state.tools
|
|
149
|
+
* - Context provided from CopilotKit useCopilotReadable
|
|
150
|
+
*
|
|
151
|
+
* Works with any agent (prebuilt or custom).
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```typescript
|
|
155
|
+
* import { createAgent } from "langchain";
|
|
156
|
+
* import { copilotkitMiddleware } from "@copilotkit/sdk-js/langgraph";
|
|
157
|
+
*
|
|
158
|
+
* const agent = createAgent({
|
|
159
|
+
* model: "gpt-4o",
|
|
160
|
+
* tools: [backendTool],
|
|
161
|
+
* middleware: [copilotkitMiddleware],
|
|
162
|
+
* });
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
const copilotKitStateSchema = z.object({ copilotkit: zodState(z.object({
|
|
166
|
+
actions: z.array(z.any()),
|
|
167
|
+
context: z.any().optional(),
|
|
168
|
+
interceptedToolCalls: z.array(z.any()).optional(),
|
|
169
|
+
originalAIMessageId: z.string().optional()
|
|
170
|
+
}).optional()) });
|
|
171
|
+
const buildMiddlewareInput = (exposeState) => ({
|
|
57
172
|
name: "CopilotKitMiddleware",
|
|
58
|
-
stateSchema:
|
|
59
|
-
actions: z.array(z.any()),
|
|
60
|
-
context: z.any().optional(),
|
|
61
|
-
interceptedToolCalls: z.array(z.any()).optional(),
|
|
62
|
-
originalAIMessageId: z.string().optional()
|
|
63
|
-
}).optional() }),
|
|
173
|
+
stateSchema: copilotKitStateSchema,
|
|
64
174
|
wrapModelCall: async (request, handler) => {
|
|
175
|
+
request = applyStateNote(request, exposeState);
|
|
65
176
|
const frontendTools = request.state["copilotkit"]?.actions ?? [];
|
|
66
177
|
if (frontendTools.length === 0) return handler(request);
|
|
67
178
|
const mergedTools = [...request.tools || [], ...frontendTools];
|
|
@@ -126,12 +237,32 @@ const middlewareInput = {
|
|
|
126
237
|
}
|
|
127
238
|
};
|
|
128
239
|
}
|
|
240
|
+
});
|
|
241
|
+
/**
|
|
242
|
+
* Build a CopilotKit middleware instance with custom options.
|
|
243
|
+
*
|
|
244
|
+
* Use this when you want to override the default state-exposure behavior
|
|
245
|
+
* (for example to hide a sensitive key, or to use an explicit allowlist).
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* import { createCopilotkitMiddleware } from "@copilotkit/sdk-js/langgraph";
|
|
250
|
+
*
|
|
251
|
+
* const middleware = createCopilotkitMiddleware({
|
|
252
|
+
* exposeState: ["liked", "todos"],
|
|
253
|
+
* });
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
const createCopilotkitMiddleware = (options = {}) => {
|
|
257
|
+
return createMiddleware(buildMiddlewareInput(options.exposeState ?? false));
|
|
129
258
|
};
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
259
|
+
/**
|
|
260
|
+
* Default CopilotKit middleware singleton — does NOT surface user state
|
|
261
|
+
* to the LLM. Pass `exposeState: true` (or an allowlist) to
|
|
262
|
+
* {@link createCopilotkitMiddleware} to opt in.
|
|
263
|
+
*/
|
|
264
|
+
const copilotkitMiddleware = createCopilotkitMiddleware();
|
|
134
265
|
|
|
135
266
|
//#endregion
|
|
136
|
-
export { copilotkitMiddleware };
|
|
267
|
+
export { copilotkitMiddleware, createCopilotkitMiddleware, zodState };
|
|
137
268
|
//# sourceMappingURL=middleware.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.mjs","names":[],"sources":["../../src/langgraph/middleware.ts"],"sourcesContent":["import { createMiddleware, AIMessage, SystemMessage } from \"langchain\";\nimport type { InteropZodObject } from \"@langchain/core/utils/types\";\nimport * as z from \"zod\";\n\nconst createAppContextBeforeAgent = (state, runtime) => {\n const messages = state.messages;\n\n if (!messages || messages.length === 0) {\n return;\n }\n\n // Get app context from runtime\n const appContext = state[\"copilotkit\"]?.context ?? runtime?.context;\n\n // Check if appContext is missing or empty\n const isEmptyContext =\n !appContext ||\n (typeof appContext === \"string\" && appContext.trim() === \"\") ||\n (typeof appContext === \"object\" && Object.keys(appContext).length === 0);\n\n if (isEmptyContext) {\n return;\n }\n\n // Create the context content\n const contextContent =\n typeof appContext === \"string\"\n ? appContext\n : JSON.stringify(appContext, null, 2);\n const contextMessageContent = `App Context:\\n${contextContent}`;\n const contextMessagePrefix = \"App Context:\\n\";\n\n // Helper to get message content as string\n const getContentString = (msg: any): string | null => {\n if (typeof msg.content === \"string\") return msg.content;\n if (Array.isArray(msg.content) && msg.content[0]?.text)\n return msg.content[0].text;\n return null;\n };\n\n // Find the first system/developer message (not our context message) to determine\n // where to insert our context message (right after it)\n let firstSystemIndex = -1;\n\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg._getType?.();\n if (type === \"system\" || type === \"developer\") {\n const content = getContentString(msg);\n // Skip if this is our own context message\n if (content?.startsWith(contextMessagePrefix)) {\n continue;\n }\n firstSystemIndex = i;\n break;\n }\n }\n\n // Check if our context message already exists\n let existingContextIndex = -1;\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg._getType?.();\n if (type === \"system\" || type === \"developer\") {\n const content = getContentString(msg);\n if (content?.startsWith(contextMessagePrefix)) {\n existingContextIndex = i;\n break;\n }\n }\n }\n\n // Create the context message\n const contextMessage = new SystemMessage({ content: contextMessageContent });\n\n let updatedMessages;\n\n if (existingContextIndex !== -1) {\n // Replace existing context message\n updatedMessages = [...messages];\n updatedMessages[existingContextIndex] = contextMessage;\n } else {\n // Insert after the first system message, or at position 0 if no system message\n const insertIndex = firstSystemIndex !== -1 ? firstSystemIndex + 1 : 0;\n updatedMessages = [\n ...messages.slice(0, insertIndex),\n contextMessage,\n ...messages.slice(insertIndex),\n ];\n }\n\n return {\n ...state,\n messages: updatedMessages,\n };\n};\n\n/**\n * CopilotKit Middleware for LangGraph agents.\n *\n * Enables:\n * - Dynamic frontend tools from state.tools\n * - Context provided from CopilotKit useCopilotReadable\n *\n * Works with any agent (prebuilt or custom).\n *\n * @example\n * ```typescript\n * import { createAgent } from \"langchain\";\n * import { copilotkitMiddleware } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const agent = createAgent({\n * model: \"gpt-4o\",\n * tools: [backendTool],\n * middleware: [copilotkitMiddleware],\n * });\n * ```\n */\nconst copilotKitStateSchema = z.object({\n copilotkit: z\n .object({\n actions: z.array(z.any()),\n context: z.any().optional(),\n interceptedToolCalls: z.array(z.any()).optional(),\n originalAIMessageId: z.string().optional(),\n })\n .optional(),\n});\n\nconst middlewareInput = {\n name: \"CopilotKitMiddleware\",\n\n stateSchema: copilotKitStateSchema as unknown as InteropZodObject,\n\n // Inject frontend tools before model call\n wrapModelCall: async (request, handler) => {\n const frontendTools = request.state[\"copilotkit\"]?.actions ?? [];\n\n if (frontendTools.length === 0) {\n return handler(request);\n }\n\n const existingTools = request.tools || [];\n const mergedTools = [...existingTools, ...frontendTools];\n\n return handler({\n ...request,\n tools: mergedTools,\n });\n },\n\n beforeAgent: createAppContextBeforeAgent,\n\n // Restore frontend tool calls to AIMessage before agent exits\n afterAgent: (state) => {\n const interceptedToolCalls = state[\"copilotkit\"]?.interceptedToolCalls;\n const originalMessageId = state[\"copilotkit\"]?.originalAIMessageId;\n\n if (!interceptedToolCalls?.length || !originalMessageId) {\n return;\n }\n\n let messageFound = false;\n const updatedMessages = state.messages.map((msg: any) => {\n if (AIMessage.isInstance(msg) && msg.id === originalMessageId) {\n messageFound = true;\n const existingToolCalls = msg.tool_calls || [];\n return new AIMessage({\n content: msg.content,\n tool_calls: [...existingToolCalls, ...interceptedToolCalls],\n id: msg.id,\n });\n }\n return msg;\n });\n\n // Only clear intercepted state if we successfully restored the tool calls\n if (!messageFound) {\n console.warn(\n `CopilotKit: Could not find message with id ${originalMessageId} to restore tool calls`,\n );\n return;\n }\n\n return {\n messages: updatedMessages,\n copilotkit: {\n ...state[\"copilotkit\"],\n interceptedToolCalls: undefined,\n originalAIMessageId: undefined,\n },\n };\n },\n\n // Intercept frontend tool calls after model returns, before ToolNode executes\n afterModel: (state) => {\n const frontendTools = state[\"copilotkit\"]?.actions ?? [];\n if (frontendTools.length === 0) return;\n\n const frontendToolNames = new Set(\n frontendTools.map((t: any) => t.function?.name || t.name),\n );\n\n const lastMessage = state.messages[state.messages.length - 1];\n if (!AIMessage.isInstance(lastMessage) || !lastMessage.tool_calls?.length) {\n return;\n }\n\n const backendToolCalls: any[] = [];\n const frontendToolCalls: any[] = [];\n\n for (const call of lastMessage.tool_calls) {\n if (frontendToolNames.has(call.name)) {\n frontendToolCalls.push(call);\n } else {\n backendToolCalls.push(call);\n }\n }\n\n if (frontendToolCalls.length === 0) return;\n\n const updatedAIMessage = new AIMessage({\n content: lastMessage.content,\n tool_calls: backendToolCalls,\n id: lastMessage.id,\n });\n\n return {\n messages: [...state.messages.slice(0, -1), updatedAIMessage],\n copilotkit: {\n ...state[\"copilotkit\"],\n interceptedToolCalls: frontendToolCalls,\n originalAIMessageId: lastMessage.id,\n },\n };\n },\n} as any;\nconst createCopilotKitMiddleware = () => {\n return createMiddleware(middlewareInput);\n};\n\nexport const copilotkitMiddleware = createCopilotKitMiddleware();\n"],"mappings":";;;;AAIA,MAAM,+BAA+B,OAAO,YAAY;CACtD,MAAM,WAAW,MAAM;AAEvB,KAAI,CAAC,YAAY,SAAS,WAAW,EACnC;CAIF,MAAM,aAAa,MAAM,eAAe,WAAW,SAAS;AAQ5D,KAJE,CAAC,cACA,OAAO,eAAe,YAAY,WAAW,MAAM,KAAK,MACxD,OAAO,eAAe,YAAY,OAAO,KAAK,WAAW,CAAC,WAAW,EAGtE;CAQF,MAAM,wBAAwB,iBAH5B,OAAO,eAAe,WAClB,aACA,KAAK,UAAU,YAAY,MAAM,EAAE;CAEzC,MAAM,uBAAuB;CAG7B,MAAM,oBAAoB,QAA4B;AACpD,MAAI,OAAO,IAAI,YAAY,SAAU,QAAO,IAAI;AAChD,MAAI,MAAM,QAAQ,IAAI,QAAQ,IAAI,IAAI,QAAQ,IAAI,KAChD,QAAO,IAAI,QAAQ,GAAG;AACxB,SAAO;;CAKT,IAAI,mBAAmB;AAEvB,MAAK,IA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EAAG;EAEpC,MAAM,mBAAmB,IAAI,UAAU;GACrC,SAAS,YAAY;GACrB,YAAY;GACZ,IAAI,YAAY;GACjB,CAAC;AAEF,SAAO;GACL,UAAU,CAAC,GAAG,MAAM,SAAS,MAAM,GAAG,GAAG,EAAE,iBAAiB;GAC5D,YAAY;IACV,GAAG,MAAM;IACT,sBAAsB;IACtB,qBAAqB,YAAY;IAClC;GACF;;CAEJ;AACD,MAAM,mCAAmC;AACvC,QAAO,iBAAiB,gBAAgB;;AAG1C,MAAa,uBAAuB,4BAA4B"}
|
|
1
|
+
{"version":3,"file":"middleware.mjs","names":[],"sources":["../../src/langgraph/middleware.ts"],"sourcesContent":["import { createMiddleware, AIMessage, SystemMessage } from \"langchain\";\nimport type { InteropZodObject } from \"@langchain/core/utils/types\";\nimport type {\n StandardJSONSchemaV1,\n StandardSchemaV1,\n} from \"@standard-schema/spec\";\nimport * as z from \"zod\";\n\ntype WithJsonSchema<T> = T extends { \"~standard\": infer S }\n ? Omit<T, \"~standard\"> & {\n \"~standard\": S &\n StandardJSONSchemaV1.Props<\n S extends StandardSchemaV1.Props<infer I, any> ? I : unknown,\n S extends StandardSchemaV1.Props<any, infer O> ? O : unknown\n >;\n }\n : T;\n\n/**\n * Augment a Standard-Schema–compatible schema (e.g. Zod) with a\n * `~standard.jsonSchema.input` hook so LangGraph's\n * `getJsonSchemaFromSchema` (called from `StateSchema.getJsonSchema`)\n * can serialize the field.\n *\n * Without this, Zod v4 fields carry `~standard.validate` + `vendor` only,\n * and `isStandardJSONSchema()` returns false, so the field is silently\n * dropped from the graph's `output_schema`. That makes AG-UI\n * `STATE_SNAPSHOT` events filter the field out of the payload sent to\n * the frontend even though the underlying thread state has the value.\n *\n * Use this on any custom state field you want visible to the frontend\n * via `useAgent().state.*`.\n *\n * @example\n * ```ts\n * import { zodState } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const stateSchema = z.object({\n * todos: zodState(z.array(TodoSchema).default(() => [])),\n * });\n * ```\n */\nexport function zodState<T extends object>(schema: T): WithJsonSchema<T> {\n const std = (schema as { \"~standard\"?: { jsonSchema?: unknown } })[\n \"~standard\"\n ];\n if (std && typeof std === \"object\" && !(\"jsonSchema\" in std)) {\n let cached: Record<string, unknown> | undefined;\n std.jsonSchema = {\n input: () => {\n if (cached) return cached;\n // Prefer zod-v4's native `toJSONSchema` when available. Falls back to\n // an empty object, which is sufficient for the field to appear in the\n // graph's output_schema (langgraph-api treats it as an opaque field).\n try {\n const maybeV4ToJsonSchema = (\n z as unknown as {\n toJSONSchema?: (s: unknown) => Record<string, unknown>;\n }\n ).toJSONSchema;\n cached =\n typeof maybeV4ToJsonSchema === \"function\"\n ? maybeV4ToJsonSchema(schema)\n : {};\n } catch {\n cached = {};\n }\n return cached;\n },\n };\n }\n return schema as WithJsonSchema<T>;\n}\n\n/**\n * Internal/framework state keys that should never be auto-surfaced to the\n * LLM as user-facing state. These are reducer-managed message buckets,\n * CopilotKit/AG-UI plumbing, or graph-internal scaffolding.\n */\nconst RESERVED_STATE_KEYS: ReadonlySet<string> = new Set([\n \"messages\",\n \"copilotkit\",\n \"ag-ui\",\n \"tools\",\n \"structured_response\",\n \"thread_id\",\n \"remaining_steps\",\n]);\n\n/**\n * Controls how user-defined state keys are surfaced into the LLM prompt\n * on every model call. Off by default to avoid leaking arbitrary state\n * into prompts; opt in explicitly.\n *\n * - `false` (default) — never surface state.\n * - `true` — every state key not in the reserved internal set and not\n * prefixed with `_` is JSON-serialized into a \"Current agent state:\"\n * note appended to the system prompt.\n * - `string[]` — only surface the named keys (use this when you want\n * explicit control over what the LLM sees, e.g. `[\"liked\", \"todos\"]`).\n */\nexport type ExposeStateOption = boolean | readonly string[];\n\nconst buildStateNote = (\n state: Record<string, unknown>,\n expose: ExposeStateOption,\n): string | null => {\n if (expose === false) return null;\n\n const allow: ReadonlySet<string> | null = Array.isArray(expose)\n ? new Set(expose)\n : null;\n\n const snapshot: Record<string, unknown> = {};\n for (const key of Object.keys(state)) {\n if (\n allow\n ? !allow.has(key)\n : RESERVED_STATE_KEYS.has(key) || key.startsWith(\"_\")\n ) {\n continue;\n }\n const value = state[key];\n if (\n value === undefined ||\n value === null ||\n value === \"\" ||\n (Array.isArray(value) && value.length === 0) ||\n (typeof value === \"object\" &&\n !Array.isArray(value) &&\n Object.keys(value as Record<string, unknown>).length === 0)\n ) {\n continue;\n }\n snapshot[key] = value;\n }\n\n if (Object.keys(snapshot).length === 0) return null;\n\n let body: string;\n try {\n body = JSON.stringify(snapshot, null, 2);\n } catch {\n body = String(snapshot);\n }\n return `Current agent state:\\n${body}`;\n};\n\nconst applyStateNote = (request: any, expose: ExposeStateOption): any => {\n const note = buildStateNote(\n (request.state ?? {}) as Record<string, unknown>,\n expose,\n );\n if (!note) return request;\n\n const existing = request.systemPrompt;\n if (existing == null) {\n return { ...request, systemPrompt: new SystemMessage({ content: note }) };\n }\n // existing may be a string OR a SystemMessage\n const baseText =\n typeof existing === \"string\"\n ? existing\n : typeof existing.content === \"string\"\n ? existing.content\n : String(existing.content);\n return {\n ...request,\n systemPrompt: new SystemMessage({ content: `${baseText}\\n\\n${note}` }),\n };\n};\n\nconst createAppContextBeforeAgent = (state, runtime) => {\n const messages = state.messages;\n\n if (!messages || messages.length === 0) {\n return;\n }\n\n // Get app context from runtime\n const appContext = state[\"copilotkit\"]?.context ?? runtime?.context;\n\n // Check if appContext is missing or empty\n const isEmptyContext =\n !appContext ||\n (typeof appContext === \"string\" && appContext.trim() === \"\") ||\n (typeof appContext === \"object\" && Object.keys(appContext).length === 0);\n\n if (isEmptyContext) {\n return;\n }\n\n // Create the context content\n const contextContent =\n typeof appContext === \"string\"\n ? appContext\n : JSON.stringify(appContext, null, 2);\n const contextMessageContent = `App Context:\\n${contextContent}`;\n const contextMessagePrefix = \"App Context:\\n\";\n\n // Helper to get message content as string\n const getContentString = (msg: any): string | null => {\n if (typeof msg.content === \"string\") return msg.content;\n if (Array.isArray(msg.content) && msg.content[0]?.text)\n return msg.content[0].text;\n return null;\n };\n\n // Find the first system/developer message (not our context message) to determine\n // where to insert our context message (right after it)\n let firstSystemIndex = -1;\n\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg._getType?.();\n if (type === \"system\" || type === \"developer\") {\n const content = getContentString(msg);\n // Skip if this is our own context message\n if (content?.startsWith(contextMessagePrefix)) {\n continue;\n }\n firstSystemIndex = i;\n break;\n }\n }\n\n // Check if our context message already exists\n let existingContextIndex = -1;\n for (let i = 0; i < messages.length; i++) {\n const msg = messages[i];\n const type = msg._getType?.();\n if (type === \"system\" || type === \"developer\") {\n const content = getContentString(msg);\n if (content?.startsWith(contextMessagePrefix)) {\n existingContextIndex = i;\n break;\n }\n }\n }\n\n // Create the context message\n const contextMessage = new SystemMessage({ content: contextMessageContent });\n\n let updatedMessages;\n\n if (existingContextIndex !== -1) {\n // Replace existing context message\n updatedMessages = [...messages];\n updatedMessages[existingContextIndex] = contextMessage;\n } else {\n // Insert after the first system message, or at position 0 if no system message\n const insertIndex = firstSystemIndex !== -1 ? firstSystemIndex + 1 : 0;\n updatedMessages = [\n ...messages.slice(0, insertIndex),\n contextMessage,\n ...messages.slice(insertIndex),\n ];\n }\n\n return {\n ...state,\n messages: updatedMessages,\n };\n};\n\n/**\n * CopilotKit Middleware for LangGraph agents.\n *\n * Enables:\n * - Dynamic frontend tools from state.tools\n * - Context provided from CopilotKit useCopilotReadable\n *\n * Works with any agent (prebuilt or custom).\n *\n * @example\n * ```typescript\n * import { createAgent } from \"langchain\";\n * import { copilotkitMiddleware } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const agent = createAgent({\n * model: \"gpt-4o\",\n * tools: [backendTool],\n * middleware: [copilotkitMiddleware],\n * });\n * ```\n */\nconst copilotKitStateSchema = z.object({\n copilotkit: zodState(\n z\n .object({\n actions: z.array(z.any()),\n context: z.any().optional(),\n interceptedToolCalls: z.array(z.any()).optional(),\n originalAIMessageId: z.string().optional(),\n })\n .optional(),\n ),\n});\n\nconst buildMiddlewareInput = (exposeState: ExposeStateOption) => ({\n name: \"CopilotKitMiddleware\",\n\n stateSchema: copilotKitStateSchema as unknown as InteropZodObject,\n\n // Inject frontend tools and surface user state before model call\n wrapModelCall: async (request: any, handler: (req: any) => Promise<any>) => {\n request = applyStateNote(request, exposeState);\n const frontendTools = request.state[\"copilotkit\"]?.actions ?? [];\n\n if (frontendTools.length === 0) {\n return handler(request);\n }\n\n const existingTools = request.tools || [];\n const mergedTools = [...existingTools, ...frontendTools];\n\n return handler({\n ...request,\n tools: mergedTools,\n });\n },\n\n beforeAgent: createAppContextBeforeAgent,\n\n // Restore frontend tool calls to AIMessage before agent exits\n afterAgent: (state) => {\n const interceptedToolCalls = state[\"copilotkit\"]?.interceptedToolCalls;\n const originalMessageId = state[\"copilotkit\"]?.originalAIMessageId;\n\n if (!interceptedToolCalls?.length || !originalMessageId) {\n return;\n }\n\n let messageFound = false;\n const updatedMessages = state.messages.map((msg: any) => {\n if (AIMessage.isInstance(msg) && msg.id === originalMessageId) {\n messageFound = true;\n const existingToolCalls = msg.tool_calls || [];\n return new AIMessage({\n content: msg.content,\n tool_calls: [...existingToolCalls, ...interceptedToolCalls],\n id: msg.id,\n });\n }\n return msg;\n });\n\n // Only clear intercepted state if we successfully restored the tool calls\n if (!messageFound) {\n console.warn(\n `CopilotKit: Could not find message with id ${originalMessageId} to restore tool calls`,\n );\n return;\n }\n\n return {\n messages: updatedMessages,\n copilotkit: {\n ...state[\"copilotkit\"],\n interceptedToolCalls: undefined,\n originalAIMessageId: undefined,\n },\n };\n },\n\n // Intercept frontend tool calls after model returns, before ToolNode executes\n afterModel: (state) => {\n const frontendTools = state[\"copilotkit\"]?.actions ?? [];\n if (frontendTools.length === 0) return;\n\n const frontendToolNames = new Set(\n frontendTools.map((t: any) => t.function?.name || t.name),\n );\n\n const lastMessage = state.messages[state.messages.length - 1];\n if (!AIMessage.isInstance(lastMessage) || !lastMessage.tool_calls?.length) {\n return;\n }\n\n const backendToolCalls: any[] = [];\n const frontendToolCalls: any[] = [];\n\n for (const call of lastMessage.tool_calls) {\n if (frontendToolNames.has(call.name)) {\n frontendToolCalls.push(call);\n } else {\n backendToolCalls.push(call);\n }\n }\n\n if (frontendToolCalls.length === 0) return;\n\n const updatedAIMessage = new AIMessage({\n content: lastMessage.content,\n tool_calls: backendToolCalls,\n id: lastMessage.id,\n });\n\n return {\n messages: [...state.messages.slice(0, -1), updatedAIMessage],\n copilotkit: {\n ...state[\"copilotkit\"],\n interceptedToolCalls: frontendToolCalls,\n originalAIMessageId: lastMessage.id,\n },\n };\n },\n});\n\n/**\n * Build a CopilotKit middleware instance with custom options.\n *\n * Use this when you want to override the default state-exposure behavior\n * (for example to hide a sensitive key, or to use an explicit allowlist).\n *\n * @example\n * ```typescript\n * import { createCopilotkitMiddleware } from \"@copilotkit/sdk-js/langgraph\";\n *\n * const middleware = createCopilotkitMiddleware({\n * exposeState: [\"liked\", \"todos\"],\n * });\n * ```\n */\nexport const createCopilotkitMiddleware = (\n options: { exposeState?: ExposeStateOption } = {},\n) => {\n const exposeState = options.exposeState ?? false;\n return createMiddleware(buildMiddlewareInput(exposeState) as any);\n};\n\n/**\n * Default CopilotKit middleware singleton — does NOT surface user state\n * to the LLM. Pass `exposeState: true` (or an allowlist) to\n * {@link createCopilotkitMiddleware} to opt in.\n */\nexport const copilotkitMiddleware = createCopilotkitMiddleware();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,SAAgB,SAA2B,QAA8B;CACvE,MAAM,MAAO,OACX;AAEF,KAAI,OAAO,OAAO,QAAQ,YAAY,EAAE,gBAAgB,MAAM;EAC5D,IAAI;AACJ,MAAI,aAAa,EACf,aAAa;AACX,OAAI,OAAQ,QAAO;AAInB,OAAI;IACF,MAAM,sBACJ,EAGA;AACF,aACE,OAAO,wBAAwB,aAC3B,oBAAoB,OAAO,GAC3B,EAAE;WACF;AACN,aAAS,EAAE;;AAEb,UAAO;KAEV;;AAEH,QAAO;;;;;;;AAQT,MAAM,sBAA2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