@copilotkit/sdk-js 2.0.0-next.1 → 2.0.0-rc.1
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/LICENSE +21 -0
- package/README.md +0 -1
- package/dist/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/header-propagation.cjs +37 -0
- package/dist/header-propagation.cjs.map +1 -0
- package/dist/header-propagation.d.cts +16 -0
- package/dist/header-propagation.d.cts.map +1 -0
- package/dist/header-propagation.d.mts +16 -0
- package/dist/header-propagation.d.mts.map +1 -0
- package/dist/header-propagation.mjs +35 -0
- package/dist/header-propagation.mjs.map +1 -0
- package/dist/index.cjs +5 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.mts +18 -0
- package/dist/index.mjs +3 -1
- package/dist/langchain.cjs +21 -0
- package/dist/langchain.cjs.map +1 -0
- package/dist/langchain.d.cts +139 -0
- package/dist/langchain.d.mts +140 -0
- package/dist/langchain.mjs +7 -23
- package/dist/langchain.mjs.map +1 -1
- package/dist/langgraph/middleware.cjs +433 -0
- package/dist/langgraph/middleware.cjs.map +1 -0
- package/dist/langgraph/middleware.d.cts +86 -0
- package/dist/langgraph/middleware.d.cts.map +1 -0
- package/dist/langgraph/middleware.d.mts +86 -0
- package/dist/langgraph/middleware.d.mts.map +1 -0
- package/dist/langgraph/middleware.mjs +429 -0
- package/dist/langgraph/middleware.mjs.map +1 -0
- package/dist/langgraph/state-schema.cjs +37 -0
- package/dist/langgraph/state-schema.cjs.map +1 -0
- package/dist/langgraph/state-schema.d.cts +118 -0
- package/dist/langgraph/state-schema.d.cts.map +1 -0
- package/dist/langgraph/state-schema.d.mts +118 -0
- package/dist/langgraph/state-schema.d.mts.map +1 -0
- package/dist/langgraph/state-schema.mjs +36 -0
- package/dist/langgraph/state-schema.mjs.map +1 -0
- package/dist/langgraph/types.cjs +60 -0
- package/dist/langgraph/types.cjs.map +1 -0
- package/dist/langgraph/types.d.cts +335 -0
- package/dist/langgraph/types.d.cts.map +1 -0
- package/dist/langgraph/types.d.mts +335 -0
- package/dist/langgraph/types.d.mts.map +1 -0
- package/dist/langgraph/types.mjs +57 -0
- package/dist/langgraph/types.mjs.map +1 -0
- package/dist/langgraph/utils.cjs +297 -0
- package/dist/langgraph/utils.cjs.map +1 -0
- package/dist/langgraph/utils.d.cts +209 -0
- package/dist/langgraph/utils.d.cts.map +1 -0
- package/dist/langgraph/utils.d.mts +209 -0
- package/dist/langgraph/utils.d.mts.map +1 -0
- package/dist/langgraph/utils.mjs +289 -0
- package/dist/langgraph/utils.mjs.map +1 -0
- package/dist/langgraph-middlewares.cjs +22 -0
- package/dist/langgraph-middlewares.d.cts +30 -0
- package/dist/langgraph-middlewares.d.mts +30 -0
- package/dist/langgraph-middlewares.mjs +5 -0
- package/dist/langgraph.cjs +21 -0
- package/dist/langgraph.d.cts +5 -0
- package/dist/langgraph.d.mts +5 -0
- package/dist/langgraph.mjs +6 -27
- package/package.json +50 -47
- package/src/__tests__/error-handling.test.ts +54 -20
- package/src/__tests__/header-propagation.test.ts +131 -0
- package/src/header-propagation.ts +61 -0
- package/src/index.ts +41 -0
- package/src/langchain.ts +168 -8
- package/src/langgraph/__tests__/middleware.a2ui-params.test.ts +139 -0
- package/src/langgraph/__tests__/middleware.native-a2ui.test.ts +118 -0
- package/src/langgraph/__tests__/middleware.properties.test.ts +357 -0
- package/src/langgraph/__tests__/middleware.test.ts +1141 -0
- package/src/langgraph/__tests__/state-schema.test.ts +85 -0
- package/src/langgraph/__tests__/utils.test.ts +363 -0
- package/src/langgraph/index.ts +1 -0
- package/src/langgraph/middleware.ts +698 -95
- package/src/langgraph/state-schema.ts +34 -0
- package/src/langgraph/types.ts +57 -0
- package/src/langgraph/utils.ts +76 -19
- package/src/langgraph-middlewares.ts +55 -0
- package/tsconfig.json +11 -2
- package/tsdown.config.ts +18 -0
- package/vitest.config.mjs +11 -0
- package/CHANGELOG.md +0 -2169
- package/dist/chunk-JLGOQLGS.mjs +0 -433
- package/dist/chunk-JLGOQLGS.mjs.map +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/langchain-2645763d.d.ts +0 -427
- package/dist/langchain.d.ts +0 -6
- package/dist/langchain.js +0 -284
- package/dist/langchain.js.map +0 -1
- package/dist/langgraph/index.d.ts +0 -11
- package/dist/langgraph.js +0 -476
- package/dist/langgraph.js.map +0 -1
- package/dist/langgraph.mjs.map +0 -1
- package/jest.config.js +0 -5
- package/tsup.config.ts +0 -23
|
@@ -0,0 +1,1141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Behavior tests for the CopilotKit LangGraph middleware.
|
|
3
|
+
*
|
|
4
|
+
* The contract these tests pin down (independent of how the middleware is
|
|
5
|
+
* implemented internally — we only assert on what the model handler
|
|
6
|
+
* observes and what state updates the middleware emits):
|
|
7
|
+
*
|
|
8
|
+
* - Frontend tools listed in `state.copilotkit.actions` reach the model
|
|
9
|
+
* alongside the agent's own tools. Empty actions = no change.
|
|
10
|
+
* - App context from `state.copilotkit.context` (or a namespaced
|
|
11
|
+
* runtime.context carrier, never the raw runtime.context) is appended to
|
|
12
|
+
* the leading system message as `"App Context:\n<json>"`.
|
|
13
|
+
* The model never sees a second system message.
|
|
14
|
+
* - `afterModel` peels frontend tool calls off the last AIMessage so the
|
|
15
|
+
* ToolNode does not execute them; `afterAgent` re-attaches them.
|
|
16
|
+
* - The opt-in `exposeState` knob surfaces user state into
|
|
17
|
+
* `request.systemPrompt` as a "Current agent state:" note. Default off;
|
|
18
|
+
* reserved internal keys / `_`-prefixed keys / empty values are filtered;
|
|
19
|
+
* allowlist forces an explicit subset; existing systemPrompt is kept.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { describe, it, expect } from "vitest";
|
|
23
|
+
import * as z from "zod";
|
|
24
|
+
import {
|
|
25
|
+
AIMessage,
|
|
26
|
+
HumanMessage,
|
|
27
|
+
SystemMessage,
|
|
28
|
+
} from "@langchain/core/messages";
|
|
29
|
+
import { FakeListChatModel } from "@langchain/core/utils/testing";
|
|
30
|
+
import {
|
|
31
|
+
createAgent,
|
|
32
|
+
createMiddleware,
|
|
33
|
+
dynamicSystemPromptMiddleware,
|
|
34
|
+
} from "langchain";
|
|
35
|
+
|
|
36
|
+
import {
|
|
37
|
+
copilotkitMiddleware,
|
|
38
|
+
createCopilotkitMiddleware,
|
|
39
|
+
zodState,
|
|
40
|
+
} from "../middleware";
|
|
41
|
+
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// Helpers
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
function makeRequest(overrides: any = {}): any {
|
|
47
|
+
return {
|
|
48
|
+
model: { _modelType: () => "fake" },
|
|
49
|
+
messages: [],
|
|
50
|
+
systemPrompt: undefined,
|
|
51
|
+
tools: [],
|
|
52
|
+
state: { messages: [] },
|
|
53
|
+
runtime: {},
|
|
54
|
+
...overrides,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function runWrap(middleware: any, request: any) {
|
|
59
|
+
let received: any = null;
|
|
60
|
+
const handler = async (req: any) => {
|
|
61
|
+
received = req;
|
|
62
|
+
return { content: "ok" } as any;
|
|
63
|
+
};
|
|
64
|
+
const result = await middleware.wrapModelCall(request, handler);
|
|
65
|
+
expect(received).not.toBeNull();
|
|
66
|
+
return { received, result };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function systemContents(messages: any[]): string[] {
|
|
70
|
+
const out: string[] = [];
|
|
71
|
+
for (const m of messages) {
|
|
72
|
+
if (m._getType?.() === "system") {
|
|
73
|
+
out.push(typeof m.content === "string" ? m.content : m.text);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function systemPromptText(request: any): string {
|
|
80
|
+
expect(typeof request.systemPrompt).toBe("string");
|
|
81
|
+
return request.systemPrompt;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
class CapturingFakeListChatModel extends FakeListChatModel {
|
|
85
|
+
readonly receivedMessages: Array<
|
|
86
|
+
Parameters<FakeListChatModel["_generate"]>[0]
|
|
87
|
+
> = [];
|
|
88
|
+
|
|
89
|
+
override async _generate(
|
|
90
|
+
...args: Parameters<FakeListChatModel["_generate"]>
|
|
91
|
+
) {
|
|
92
|
+
this.receivedMessages.push(args[0]);
|
|
93
|
+
return super._generate(...args);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
override bindTools() {
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
// Frontend-tool injection
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
|
|
105
|
+
describe("frontend tool injection", () => {
|
|
106
|
+
it("passes the request through unchanged when there are no frontend tools", async () => {
|
|
107
|
+
const backendTool = { name: "backend" };
|
|
108
|
+
const request = makeRequest({
|
|
109
|
+
state: { messages: [] },
|
|
110
|
+
tools: [backendTool],
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
114
|
+
|
|
115
|
+
expect(received.tools).toEqual([backendTool]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("merges frontend tools from state.copilotkit.actions with the agent's own tools", async () => {
|
|
119
|
+
const backend = { name: "backend" };
|
|
120
|
+
const fe = [{ name: "fe_one" }, { name: "fe_two" }];
|
|
121
|
+
const request = makeRequest({
|
|
122
|
+
state: { messages: [], copilotkit: { actions: fe } },
|
|
123
|
+
tools: [backend],
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
127
|
+
|
|
128
|
+
const names = received.tools.map((t: any) => t.name).sort();
|
|
129
|
+
expect(names).toEqual(["backend", "fe_one", "fe_two"]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("does not mutate the input request when merging frontend tools", async () => {
|
|
133
|
+
const request = makeRequest({
|
|
134
|
+
state: {
|
|
135
|
+
messages: [],
|
|
136
|
+
copilotkit: { actions: [{ name: "fe" }] },
|
|
137
|
+
},
|
|
138
|
+
tools: [{ name: "backend" }],
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
await runWrap(copilotkitMiddleware, request);
|
|
142
|
+
|
|
143
|
+
expect(request.tools.map((t: any) => t.name)).toEqual(["backend"]);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// ---------------------------------------------------------------------------
|
|
148
|
+
// exposeState — opt-in state surfacing
|
|
149
|
+
// ---------------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
describe("exposeState", () => {
|
|
152
|
+
it("surfaces state owned by a sibling middleware during an agent run", async () => {
|
|
153
|
+
const model = new CapturingFakeListChatModel({ responses: ["Hello"] });
|
|
154
|
+
const originalSystemMessage = new SystemMessage({
|
|
155
|
+
content: [
|
|
156
|
+
{
|
|
157
|
+
type: "text",
|
|
158
|
+
text: "You are a helpful assistant.",
|
|
159
|
+
cache_control: { type: "ephemeral" },
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
additional_kwargs: { provider: "anthropic" },
|
|
163
|
+
response_metadata: { model: "claude" },
|
|
164
|
+
id: "system-message-id",
|
|
165
|
+
name: "deep-agent",
|
|
166
|
+
});
|
|
167
|
+
const agentNameMiddleware = createMiddleware({
|
|
168
|
+
name: "AgentNameMiddleware",
|
|
169
|
+
stateSchema: z.object({ agentName: z.string().optional() }),
|
|
170
|
+
});
|
|
171
|
+
const middleware = createCopilotkitMiddleware({
|
|
172
|
+
exposeState: ["agentName"],
|
|
173
|
+
});
|
|
174
|
+
const agent = createAgent({
|
|
175
|
+
model,
|
|
176
|
+
tools: [],
|
|
177
|
+
systemPrompt: originalSystemMessage,
|
|
178
|
+
middleware: [agentNameMiddleware, middleware],
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
await agent.invoke({
|
|
182
|
+
messages: [new HumanMessage("What is your name?")],
|
|
183
|
+
agentName: "Mochi",
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const systemMessage = model.receivedMessages[0].find(
|
|
187
|
+
(message) => message._getType() === "system",
|
|
188
|
+
);
|
|
189
|
+
const systemContent = systemMessage?.content;
|
|
190
|
+
expect(Array.isArray(systemContent)).toBe(true);
|
|
191
|
+
if (!Array.isArray(systemContent)) {
|
|
192
|
+
throw new Error("Expected system message content blocks");
|
|
193
|
+
}
|
|
194
|
+
const textBlocks = systemContent.filter(
|
|
195
|
+
(block): block is { type: "text"; text: unknown } =>
|
|
196
|
+
typeof block === "object" &&
|
|
197
|
+
block !== null &&
|
|
198
|
+
"type" in block &&
|
|
199
|
+
block.type === "text",
|
|
200
|
+
);
|
|
201
|
+
expect(textBlocks.length).toBeGreaterThan(0);
|
|
202
|
+
expect(textBlocks.every((block) => typeof block.text === "string")).toBe(
|
|
203
|
+
true,
|
|
204
|
+
);
|
|
205
|
+
expect(textBlocks.map((block) => block.text).join("\n")).toContain(
|
|
206
|
+
'"agentName": "Mochi"',
|
|
207
|
+
);
|
|
208
|
+
expect(systemContent[0]).toEqual(originalSystemMessage.content[0]);
|
|
209
|
+
expect(systemMessage).toMatchObject({
|
|
210
|
+
additional_kwargs: originalSystemMessage.additional_kwargs,
|
|
211
|
+
response_metadata: originalSystemMessage.response_metadata,
|
|
212
|
+
id: originalSystemMessage.id,
|
|
213
|
+
name: originalSystemMessage.name,
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("is off by default — user state never lands in the system prompt", async () => {
|
|
218
|
+
const request = makeRequest({
|
|
219
|
+
state: { messages: [], liked: ["a", "b"] },
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
223
|
+
|
|
224
|
+
expect(received.systemPrompt).toBeUndefined();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("surfaces user state into the system prompt when set to true", async () => {
|
|
228
|
+
const middleware = createCopilotkitMiddleware({ exposeState: true });
|
|
229
|
+
const request = makeRequest({
|
|
230
|
+
state: { messages: [], liked: ["a", "b"] },
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const { received } = await runWrap(middleware, request);
|
|
234
|
+
|
|
235
|
+
const content = systemPromptText(received);
|
|
236
|
+
expect(content).toContain("Current agent state:");
|
|
237
|
+
expect(content).toContain('"liked"');
|
|
238
|
+
expect(content).toContain('"a"');
|
|
239
|
+
expect(content).toContain('"b"');
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("skips reserved internal keys when exposing state", async () => {
|
|
243
|
+
const middleware = createCopilotkitMiddleware({ exposeState: true });
|
|
244
|
+
const request = makeRequest({
|
|
245
|
+
state: {
|
|
246
|
+
messages: [new HumanMessage("hi")],
|
|
247
|
+
tools: [{ name: "x" }],
|
|
248
|
+
copilotkit: { actions: [] },
|
|
249
|
+
structured_response: { foo: "bar" },
|
|
250
|
+
thread_id: "t-1",
|
|
251
|
+
remaining_steps: 5,
|
|
252
|
+
"ag-ui": { context: [] },
|
|
253
|
+
liked: ["a"],
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
const { received } = await runWrap(middleware, request);
|
|
258
|
+
|
|
259
|
+
const body = systemPromptText(received);
|
|
260
|
+
expect(body).toContain('"liked"');
|
|
261
|
+
for (const reserved of [
|
|
262
|
+
"messages",
|
|
263
|
+
"tools",
|
|
264
|
+
"copilotkit",
|
|
265
|
+
"structured_response",
|
|
266
|
+
"thread_id",
|
|
267
|
+
"remaining_steps",
|
|
268
|
+
"ag-ui",
|
|
269
|
+
]) {
|
|
270
|
+
expect(body).not.toContain(`"${reserved}"`);
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("skips underscore-prefixed keys when exposing state", async () => {
|
|
275
|
+
const middleware = createCopilotkitMiddleware({ exposeState: true });
|
|
276
|
+
const request = makeRequest({
|
|
277
|
+
state: { messages: [], _internal: { secret: 1 }, visible: "ok" },
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const { received } = await runWrap(middleware, request);
|
|
281
|
+
const body = systemPromptText(received);
|
|
282
|
+
|
|
283
|
+
expect(body).not.toContain('"_internal"');
|
|
284
|
+
expect(body).toContain('"visible"');
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it.each([null, undefined, "", [], {}])(
|
|
288
|
+
"skips keys with empty value %p",
|
|
289
|
+
async (emptyValue) => {
|
|
290
|
+
const middleware = createCopilotkitMiddleware({ exposeState: true });
|
|
291
|
+
const request = makeRequest({
|
|
292
|
+
state: { messages: [], filled: ["x"], blank: emptyValue },
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
const { received } = await runWrap(middleware, request);
|
|
296
|
+
if (received.systemPrompt == null) return; // acceptable: nothing left
|
|
297
|
+
|
|
298
|
+
const body = systemPromptText(received);
|
|
299
|
+
expect(body).toContain('"filled"');
|
|
300
|
+
expect(body).not.toContain('"blank"');
|
|
301
|
+
},
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
it("emits no system prompt when only reserved keys are present", async () => {
|
|
305
|
+
const middleware = createCopilotkitMiddleware({ exposeState: true });
|
|
306
|
+
const request = makeRequest({
|
|
307
|
+
state: { messages: [new HumanMessage("hi")], tools: [] },
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
const { received } = await runWrap(middleware, request);
|
|
311
|
+
|
|
312
|
+
expect(received.systemPrompt).toBeUndefined();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("only includes named keys when given an allowlist", async () => {
|
|
316
|
+
const middleware = createCopilotkitMiddleware({ exposeState: ["liked"] });
|
|
317
|
+
const request = makeRequest({
|
|
318
|
+
state: {
|
|
319
|
+
messages: [],
|
|
320
|
+
liked: ["a"],
|
|
321
|
+
todos: [{ id: 1 }],
|
|
322
|
+
other: "x",
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
const { received } = await runWrap(middleware, request);
|
|
327
|
+
const body = systemPromptText(received);
|
|
328
|
+
|
|
329
|
+
expect(body).toContain('"liked"');
|
|
330
|
+
expect(body).not.toContain('"todos"');
|
|
331
|
+
expect(body).not.toContain('"other"');
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("honors an allowlist that explicitly names a normally-reserved key", async () => {
|
|
335
|
+
const middleware = createCopilotkitMiddleware({
|
|
336
|
+
exposeState: ["thread_id"],
|
|
337
|
+
});
|
|
338
|
+
const request = makeRequest({
|
|
339
|
+
state: { messages: [], thread_id: "t-42" },
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
const { received } = await runWrap(middleware, request);
|
|
343
|
+
const body = systemPromptText(received);
|
|
344
|
+
|
|
345
|
+
expect(body).toContain("t-42");
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("appends to an existing string systemPrompt without replacing it", async () => {
|
|
349
|
+
const middleware = createCopilotkitMiddleware({ exposeState: true });
|
|
350
|
+
const request = makeRequest({
|
|
351
|
+
state: { messages: [], liked: ["a"] },
|
|
352
|
+
systemPrompt: "You are a helpful assistant.",
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
const { received } = await runWrap(middleware, request);
|
|
356
|
+
const body = systemPromptText(received);
|
|
357
|
+
|
|
358
|
+
expect(body).toContain("You are a helpful assistant.");
|
|
359
|
+
expect(body).toContain("Current agent state:");
|
|
360
|
+
expect(body.indexOf("You are a helpful assistant.")).toBeLessThan(
|
|
361
|
+
body.indexOf("Current agent state:"),
|
|
362
|
+
);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("appends to an existing SystemMessage systemPrompt without replacing it", async () => {
|
|
366
|
+
const middleware = createCopilotkitMiddleware({ exposeState: true });
|
|
367
|
+
const request = makeRequest({
|
|
368
|
+
state: { messages: [], liked: ["a"] },
|
|
369
|
+
systemPrompt: new SystemMessage({ content: "base" }),
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
const { received } = await runWrap(middleware, request);
|
|
373
|
+
const body = systemPromptText(received);
|
|
374
|
+
|
|
375
|
+
expect(body).toContain("base");
|
|
376
|
+
expect(body).toContain("Current agent state:");
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("keeps state hidden when explicitly disabled", async () => {
|
|
380
|
+
const middleware = createCopilotkitMiddleware({ exposeState: false });
|
|
381
|
+
const request = makeRequest({
|
|
382
|
+
state: { messages: [], liked: ["a"] },
|
|
383
|
+
systemPrompt: "base",
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
const { received } = await runWrap(middleware, request);
|
|
387
|
+
|
|
388
|
+
expect(received.systemPrompt).toBe("base");
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("emits a parseable JSON snapshot in the note", async () => {
|
|
392
|
+
const middleware = createCopilotkitMiddleware({ exposeState: true });
|
|
393
|
+
const request = makeRequest({
|
|
394
|
+
state: {
|
|
395
|
+
messages: [],
|
|
396
|
+
liked: ["a", "b"],
|
|
397
|
+
count: 3,
|
|
398
|
+
nested: { k: "v" },
|
|
399
|
+
},
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
const { received } = await runWrap(middleware, request);
|
|
403
|
+
const body = systemPromptText(received);
|
|
404
|
+
|
|
405
|
+
const jsonPart = body.split("Current agent state:\n")[1];
|
|
406
|
+
expect(JSON.parse(jsonPart)).toEqual({
|
|
407
|
+
liked: ["a", "b"],
|
|
408
|
+
count: 3,
|
|
409
|
+
nested: { k: "v" },
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
// ---------------------------------------------------------------------------
|
|
415
|
+
// App Context — folded into the single leading system message
|
|
416
|
+
// ---------------------------------------------------------------------------
|
|
417
|
+
|
|
418
|
+
describe("app context in an agent run", () => {
|
|
419
|
+
// Anthropic and Gemini accept one leading system instruction only, so the
|
|
420
|
+
// model must never see a second system message.
|
|
421
|
+
async function runTurns(turns: number, context: unknown) {
|
|
422
|
+
const model = new CapturingFakeListChatModel({
|
|
423
|
+
responses: Array.from({ length: turns }, () => "ok"),
|
|
424
|
+
});
|
|
425
|
+
const agent = createAgent({
|
|
426
|
+
model,
|
|
427
|
+
tools: [],
|
|
428
|
+
systemPrompt: "You are a helpful assistant.",
|
|
429
|
+
middleware: [copilotkitMiddleware],
|
|
430
|
+
});
|
|
431
|
+
let messages: any[] = [];
|
|
432
|
+
for (let turn = 0; turn < turns; turn++) {
|
|
433
|
+
const result = await agent.invoke({
|
|
434
|
+
messages: [...messages, new HumanMessage(`turn ${turn}`)],
|
|
435
|
+
copilotkit: { context },
|
|
436
|
+
} as any);
|
|
437
|
+
messages = result.messages;
|
|
438
|
+
}
|
|
439
|
+
return { model, messages };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
it("sends exactly one system message, first, carrying prompt and context", async () => {
|
|
443
|
+
const { model } = await runTurns(2, [
|
|
444
|
+
{ description: "viewer role", value: "admin" },
|
|
445
|
+
]);
|
|
446
|
+
|
|
447
|
+
expect(model.receivedMessages).toHaveLength(2);
|
|
448
|
+
for (const received of model.receivedMessages) {
|
|
449
|
+
const types = received.map((m) => m._getType());
|
|
450
|
+
expect(types.filter((t) => t === "system")).toHaveLength(1);
|
|
451
|
+
expect(types[0]).toBe("system");
|
|
452
|
+
const [system] = systemContents(received);
|
|
453
|
+
expect(system.startsWith("You are a helpful assistant.\n\n")).toBe(true);
|
|
454
|
+
expect(system).toContain("App Context:\n");
|
|
455
|
+
expect(system).toContain("admin");
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it("puts the state note before the context in the one system message", async () => {
|
|
460
|
+
const model = new CapturingFakeListChatModel({ responses: ["ok"] });
|
|
461
|
+
const agent = createAgent({
|
|
462
|
+
model,
|
|
463
|
+
tools: [],
|
|
464
|
+
systemPrompt: "You are a helpful assistant.",
|
|
465
|
+
stateSchema: z.object({ liked: z.array(z.string()).optional() }),
|
|
466
|
+
middleware: [createCopilotkitMiddleware({ exposeState: ["liked"] })],
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
await agent.invoke({
|
|
470
|
+
messages: [new HumanMessage("hi")],
|
|
471
|
+
liked: ["tea"],
|
|
472
|
+
copilotkit: { context: "route=/dashboard" },
|
|
473
|
+
} as any);
|
|
474
|
+
|
|
475
|
+
const [received] = model.receivedMessages;
|
|
476
|
+
expect(received.map((m) => m._getType())).toEqual(["system", "human"]);
|
|
477
|
+
const [system] = systemContents(received);
|
|
478
|
+
expect(system.indexOf("You are a helpful assistant.")).toBe(0);
|
|
479
|
+
expect(system.indexOf("Current agent state:")).toBeGreaterThan(0);
|
|
480
|
+
expect(system.indexOf("App Context:\nroute=/dashboard")).toBeGreaterThan(
|
|
481
|
+
system.indexOf("Current agent state:"),
|
|
482
|
+
);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it("returns the request unchanged when context is empty", async () => {
|
|
486
|
+
const request = makeRequest({
|
|
487
|
+
state: { messages: [], copilotkit: { context: [] } },
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
491
|
+
|
|
492
|
+
expect(received.systemPrompt).toBeUndefined();
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
it("never puts a raw runtime.context into the prompt", async () => {
|
|
496
|
+
// LangGraph runtime context carries trusted run configuration (thread,
|
|
497
|
+
// tenant and user ids); it must not reach the model. See #7077.
|
|
498
|
+
for (const context of [
|
|
499
|
+
"route=/dashboard",
|
|
500
|
+
{ thread_id: "t-1", user_id: "u-1" },
|
|
501
|
+
]) {
|
|
502
|
+
const request = makeRequest({
|
|
503
|
+
state: { messages: [], copilotkit: {} },
|
|
504
|
+
runtime: { context },
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
508
|
+
|
|
509
|
+
expect(received.systemPrompt).toBeUndefined();
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
it.each([
|
|
514
|
+
["copilotkit", { copilotkit: { context: "route=/dashboard" } }],
|
|
515
|
+
["ag-ui", { "ag-ui": { context: "route=/dashboard" } }],
|
|
516
|
+
["legacy unnamespaced", { context: "route=/dashboard", user_id: "u-1" }],
|
|
517
|
+
])(
|
|
518
|
+
"reads context from a %s runtime.context carrier when state has none",
|
|
519
|
+
async (_name, context) => {
|
|
520
|
+
const request = makeRequest({
|
|
521
|
+
state: { messages: [] },
|
|
522
|
+
runtime: { context },
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
526
|
+
|
|
527
|
+
expect(systemPromptText(received)).toBe("App Context:\nroute=/dashboard");
|
|
528
|
+
},
|
|
529
|
+
);
|
|
530
|
+
|
|
531
|
+
it("drops an App Context message that an older release saved in the thread", async () => {
|
|
532
|
+
const request = makeRequest({
|
|
533
|
+
messages: [
|
|
534
|
+
new SystemMessage("App Context:\nstale"),
|
|
535
|
+
new HumanMessage("hi"),
|
|
536
|
+
],
|
|
537
|
+
state: { messages: [], copilotkit: { context: "fresh" } },
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
541
|
+
|
|
542
|
+
expect(systemContents(received.messages)).toEqual([]);
|
|
543
|
+
expect(systemPromptText(received)).toBe("App Context:\nfresh");
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it("does not write the context into the thread's messages", async () => {
|
|
547
|
+
const { messages } = await runTurns(1, "route=/dashboard");
|
|
548
|
+
|
|
549
|
+
expect(systemContents(messages)).toEqual([]);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
// The context lives only on request.systemMessage. A middleware listed after
|
|
553
|
+
// copilotkitMiddleware runs inside it, so it decides what reaches the model.
|
|
554
|
+
describe("with a second system-prompt middleware", () => {
|
|
555
|
+
const replaceSystemMessage = createMiddleware({
|
|
556
|
+
name: "ReplaceSystemMessage",
|
|
557
|
+
wrapModelCall: (request: any, handler: any) =>
|
|
558
|
+
handler({
|
|
559
|
+
...request,
|
|
560
|
+
systemMessage: new SystemMessage("Replaced prompt."),
|
|
561
|
+
}),
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
async function systemSeenByModel(middleware: any[]) {
|
|
565
|
+
const model = new CapturingFakeListChatModel({ responses: ["ok"] });
|
|
566
|
+
const agent = createAgent({
|
|
567
|
+
model,
|
|
568
|
+
tools: [],
|
|
569
|
+
systemPrompt: "You are a helpful assistant.",
|
|
570
|
+
middleware,
|
|
571
|
+
});
|
|
572
|
+
await agent.invoke({
|
|
573
|
+
messages: [new HumanMessage("hi")],
|
|
574
|
+
copilotkit: { context: "route=/dashboard" },
|
|
575
|
+
} as any);
|
|
576
|
+
const [received] = model.receivedMessages;
|
|
577
|
+
expect(systemContents(received)).toHaveLength(1);
|
|
578
|
+
return systemContents(received)[0];
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
it("keeps the context when a later middleware appends to the prompt", async () => {
|
|
582
|
+
const system = await systemSeenByModel([
|
|
583
|
+
copilotkitMiddleware,
|
|
584
|
+
dynamicSystemPromptMiddleware(() => "Answer in French."),
|
|
585
|
+
]);
|
|
586
|
+
|
|
587
|
+
expect(system).toContain("App Context:\nroute=/dashboard");
|
|
588
|
+
expect(system).toContain("Answer in French.");
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
it("keeps the context when an earlier middleware replaces the prompt", async () => {
|
|
592
|
+
const system = await systemSeenByModel([
|
|
593
|
+
replaceSystemMessage,
|
|
594
|
+
copilotkitMiddleware,
|
|
595
|
+
]);
|
|
596
|
+
|
|
597
|
+
expect(system).toBe("Replaced prompt.\n\nApp Context:\nroute=/dashboard");
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
it("loses the context when a later middleware replaces the prompt", async () => {
|
|
601
|
+
// Known limit: older releases kept the context in a thread message, so it
|
|
602
|
+
// survived this. List copilotkitMiddleware after any middleware that
|
|
603
|
+
// replaces the system message.
|
|
604
|
+
const system = await systemSeenByModel([
|
|
605
|
+
copilotkitMiddleware,
|
|
606
|
+
replaceSystemMessage,
|
|
607
|
+
]);
|
|
608
|
+
|
|
609
|
+
expect(system).toBe("Replaced prompt.");
|
|
610
|
+
});
|
|
611
|
+
});
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
// ---------------------------------------------------------------------------
|
|
615
|
+
// afterModel — frontend tool-call interception
|
|
616
|
+
// ---------------------------------------------------------------------------
|
|
617
|
+
|
|
618
|
+
describe("afterModel", () => {
|
|
619
|
+
it("is a no-op when there are no frontend tools", () => {
|
|
620
|
+
const state = {
|
|
621
|
+
messages: [
|
|
622
|
+
new HumanMessage("hi"),
|
|
623
|
+
new AIMessage({
|
|
624
|
+
content: "",
|
|
625
|
+
tool_calls: [{ id: "1", name: "backend_only", args: {} }],
|
|
626
|
+
}),
|
|
627
|
+
],
|
|
628
|
+
copilotkit: { actions: [] },
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
expect(copilotkitMiddleware.afterModel(state, {} as any)).toBeUndefined();
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
it("peels frontend tool calls off the last AIMessage and stashes them", () => {
|
|
635
|
+
const fe = { function: { name: "navigate" } };
|
|
636
|
+
const ai = new AIMessage({
|
|
637
|
+
content: "",
|
|
638
|
+
tool_calls: [
|
|
639
|
+
{ id: "1", name: "backend_search", args: { q: "hi" } },
|
|
640
|
+
{ id: "2", name: "navigate", args: { path: "/x" } },
|
|
641
|
+
],
|
|
642
|
+
id: "ai-1",
|
|
643
|
+
});
|
|
644
|
+
const state = {
|
|
645
|
+
messages: [new HumanMessage("hi"), ai],
|
|
646
|
+
copilotkit: { actions: [fe] },
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
const result = copilotkitMiddleware.afterModel(state, {} as any);
|
|
650
|
+
|
|
651
|
+
expect(result).toBeDefined();
|
|
652
|
+
const lastAI = result!.messages[result!.messages.length - 1] as AIMessage;
|
|
653
|
+
expect(lastAI.tool_calls?.map((tc: any) => tc.name)).toEqual([
|
|
654
|
+
"backend_search",
|
|
655
|
+
]);
|
|
656
|
+
|
|
657
|
+
const intercepted = result!.copilotkit.interceptedToolCalls;
|
|
658
|
+
expect(intercepted).toHaveLength(1);
|
|
659
|
+
expect(intercepted[0].id).toBe("2");
|
|
660
|
+
expect(intercepted[0].name).toBe("navigate");
|
|
661
|
+
expect(result!.copilotkit.originalAIMessageId).toBe("ai-1");
|
|
662
|
+
});
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
// ---------------------------------------------------------------------------
|
|
666
|
+
// afterAgent — frontend tool-call restoration
|
|
667
|
+
// ---------------------------------------------------------------------------
|
|
668
|
+
|
|
669
|
+
describe("afterAgent", () => {
|
|
670
|
+
it("returns no update when there is nothing intercepted", () => {
|
|
671
|
+
const state = {
|
|
672
|
+
messages: [
|
|
673
|
+
new HumanMessage("hi"),
|
|
674
|
+
new AIMessage({ content: "ok", id: "ai-1" }),
|
|
675
|
+
],
|
|
676
|
+
copilotkit: {},
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
expect(copilotkitMiddleware.afterAgent(state, {} as any)).toBeUndefined();
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it("restores intercepted tool calls onto the original AIMessage", () => {
|
|
683
|
+
const intercepted = [{ id: "2", name: "navigate", args: { path: "/x" } }];
|
|
684
|
+
const state = {
|
|
685
|
+
messages: [
|
|
686
|
+
new HumanMessage("hi"),
|
|
687
|
+
new AIMessage({ content: "", id: "ai-1" }),
|
|
688
|
+
],
|
|
689
|
+
copilotkit: {
|
|
690
|
+
interceptedToolCalls: intercepted,
|
|
691
|
+
originalAIMessageId: "ai-1",
|
|
692
|
+
},
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
const result = copilotkitMiddleware.afterAgent(state, {} as any);
|
|
696
|
+
|
|
697
|
+
expect(result).toBeDefined();
|
|
698
|
+
const restored = result!.messages.find(
|
|
699
|
+
(m: any) => m.id === "ai-1" && m._getType?.() === "ai",
|
|
700
|
+
) as AIMessage;
|
|
701
|
+
expect(restored.tool_calls?.map((tc: any) => tc.name)).toEqual([
|
|
702
|
+
"navigate",
|
|
703
|
+
]);
|
|
704
|
+
expect(result!.copilotkit.interceptedToolCalls).toBeUndefined();
|
|
705
|
+
expect(result!.copilotkit.originalAIMessageId).toBeUndefined();
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
it("preserves v1 metadata and content blocks through interception and restoration", () => {
|
|
709
|
+
const backendToolCall = {
|
|
710
|
+
id: "backend-1",
|
|
711
|
+
name: "search",
|
|
712
|
+
args: { query: "CopilotKit" },
|
|
713
|
+
};
|
|
714
|
+
const frontendToolCall = {
|
|
715
|
+
id: "frontend-1",
|
|
716
|
+
name: "navigate",
|
|
717
|
+
args: { path: "/results" },
|
|
718
|
+
};
|
|
719
|
+
const original = new AIMessage({
|
|
720
|
+
content: [
|
|
721
|
+
{ type: "reasoning", reasoning: "Search, then open the result." },
|
|
722
|
+
{ type: "tool_call", ...backendToolCall },
|
|
723
|
+
{ type: "tool_call", ...frontendToolCall },
|
|
724
|
+
{
|
|
725
|
+
type: "tool_call_chunk",
|
|
726
|
+
id: "stale-1",
|
|
727
|
+
name: "stale",
|
|
728
|
+
args: "{}",
|
|
729
|
+
},
|
|
730
|
+
],
|
|
731
|
+
tool_calls: [backendToolCall, frontendToolCall],
|
|
732
|
+
additional_kwargs: { provider_request_id: "request-1" },
|
|
733
|
+
response_metadata: {
|
|
734
|
+
output_version: "v1",
|
|
735
|
+
status: "completed",
|
|
736
|
+
},
|
|
737
|
+
usage_metadata: {
|
|
738
|
+
input_tokens: 10,
|
|
739
|
+
output_tokens: 5,
|
|
740
|
+
total_tokens: 15,
|
|
741
|
+
},
|
|
742
|
+
invalid_tool_calls: [
|
|
743
|
+
{
|
|
744
|
+
id: "invalid-1",
|
|
745
|
+
name: "broken_tool",
|
|
746
|
+
args: "{",
|
|
747
|
+
error: "Invalid JSON",
|
|
748
|
+
type: "invalid_tool_call",
|
|
749
|
+
},
|
|
750
|
+
],
|
|
751
|
+
id: "ai-v1",
|
|
752
|
+
name: "assistant-with-tools",
|
|
753
|
+
});
|
|
754
|
+
const state = {
|
|
755
|
+
messages: [new HumanMessage("open the search result"), original],
|
|
756
|
+
copilotkit: { actions: [{ name: "navigate" }] },
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
const intercepted = copilotkitMiddleware.afterModel(state, {} as any)!;
|
|
760
|
+
const interceptedMessage = intercepted.messages.at(-1) as AIMessage;
|
|
761
|
+
const interceptedContent = interceptedMessage.content as any[];
|
|
762
|
+
|
|
763
|
+
expect(interceptedMessage).toMatchObject({
|
|
764
|
+
additional_kwargs: original.additional_kwargs,
|
|
765
|
+
response_metadata: original.response_metadata,
|
|
766
|
+
usage_metadata: original.usage_metadata,
|
|
767
|
+
invalid_tool_calls: original.invalid_tool_calls,
|
|
768
|
+
id: original.id,
|
|
769
|
+
name: original.name,
|
|
770
|
+
tool_calls: [backendToolCall],
|
|
771
|
+
});
|
|
772
|
+
expect(
|
|
773
|
+
interceptedContent
|
|
774
|
+
.filter((block) => block.type === "tool_call")
|
|
775
|
+
.map((block) => block.name),
|
|
776
|
+
).toEqual(["search"]);
|
|
777
|
+
expect(
|
|
778
|
+
interceptedContent.some((block) => block.type === "tool_call_chunk"),
|
|
779
|
+
).toBe(false);
|
|
780
|
+
|
|
781
|
+
const restored = copilotkitMiddleware.afterAgent(intercepted, {} as any)!;
|
|
782
|
+
const restoredMessage = restored.messages.at(-1) as AIMessage;
|
|
783
|
+
const restoredContent = restoredMessage.content as any[];
|
|
784
|
+
|
|
785
|
+
expect(restoredMessage).toMatchObject({
|
|
786
|
+
additional_kwargs: original.additional_kwargs,
|
|
787
|
+
response_metadata: original.response_metadata,
|
|
788
|
+
usage_metadata: original.usage_metadata,
|
|
789
|
+
invalid_tool_calls: original.invalid_tool_calls,
|
|
790
|
+
id: original.id,
|
|
791
|
+
name: original.name,
|
|
792
|
+
tool_calls: [backendToolCall, frontendToolCall],
|
|
793
|
+
});
|
|
794
|
+
expect(
|
|
795
|
+
restoredContent
|
|
796
|
+
.filter((block) => block.type === "tool_call")
|
|
797
|
+
.map((block) => block.name),
|
|
798
|
+
).toEqual(["search", "navigate"]);
|
|
799
|
+
expect(restoredContent[0]).toEqual({
|
|
800
|
+
type: "reasoning",
|
|
801
|
+
reasoning: "Search, then open the result.",
|
|
802
|
+
});
|
|
803
|
+
});
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
// ---------------------------------------------------------------------------
|
|
807
|
+
// Auto-A2UI — middleware injects + executes generate_a2ui when the frontend
|
|
808
|
+
// registered a catalog (surfaced into state["ag-ui"].a2ui_schema)
|
|
809
|
+
// ---------------------------------------------------------------------------
|
|
810
|
+
//
|
|
811
|
+
// Contract: the developer passes nothing — using the middleware is enough.
|
|
812
|
+
// generate_a2ui is advertised to the model only when an A2UI catalog is
|
|
813
|
+
// present, is built from the agent's own (inferred) model, and is executed by
|
|
814
|
+
// the middleware itself (it is never in the agent's static tool registry).
|
|
815
|
+
|
|
816
|
+
async function runWrapTool(middleware: any, request: any) {
|
|
817
|
+
let received: any = null;
|
|
818
|
+
const handler = async (req: any) => {
|
|
819
|
+
received = req;
|
|
820
|
+
return { content: "tool-ok" } as any;
|
|
821
|
+
};
|
|
822
|
+
await middleware.wrapToolCall(request, handler);
|
|
823
|
+
return received;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
describe("auto-A2UI injection", () => {
|
|
827
|
+
it("does NOT advertise generate_a2ui when the inject flag is absent", async () => {
|
|
828
|
+
const request = makeRequest({
|
|
829
|
+
state: { messages: [], thread_id: "a2ui-off" },
|
|
830
|
+
tools: [{ name: "backend" }],
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
834
|
+
|
|
835
|
+
expect(received.tools.map((t: any) => t.name)).toEqual(["backend"]);
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
it("does NOT advertise generate_a2ui when a catalog is present but the flag is absent (opt-in)", async () => {
|
|
839
|
+
const request = makeRequest({
|
|
840
|
+
state: {
|
|
841
|
+
messages: [],
|
|
842
|
+
thread_id: "a2ui-noflag",
|
|
843
|
+
"ag-ui": { a2ui_schema: "<components/>" },
|
|
844
|
+
},
|
|
845
|
+
tools: [{ name: "backend" }],
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
849
|
+
|
|
850
|
+
const names = received.tools.map((t: any) => t.name);
|
|
851
|
+
expect(names).not.toContain("generate_a2ui");
|
|
852
|
+
expect(names).toContain("backend");
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
it("advertises generate_a2ui (alongside existing tools) when the flag is on", async () => {
|
|
856
|
+
const request = makeRequest({
|
|
857
|
+
state: {
|
|
858
|
+
messages: [],
|
|
859
|
+
thread_id: "a2ui-on",
|
|
860
|
+
"ag-ui": { a2ui_schema: "<components/>", inject_a2ui_tool: true },
|
|
861
|
+
},
|
|
862
|
+
tools: [{ name: "backend" }],
|
|
863
|
+
});
|
|
864
|
+
|
|
865
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
866
|
+
|
|
867
|
+
const names = received.tools.map((t: any) => t.name);
|
|
868
|
+
expect(names).toContain("backend");
|
|
869
|
+
expect(names).toContain("generate_a2ui");
|
|
870
|
+
});
|
|
871
|
+
|
|
872
|
+
it("binds to the catalog from copilotkit.context when the flag is on (runtime-proxy path)", async () => {
|
|
873
|
+
const request = makeRequest({
|
|
874
|
+
state: {
|
|
875
|
+
messages: [],
|
|
876
|
+
thread_id: "a2ui-ctx",
|
|
877
|
+
"ag-ui": { inject_a2ui_tool: true },
|
|
878
|
+
copilotkit: {
|
|
879
|
+
context: [
|
|
880
|
+
{
|
|
881
|
+
description:
|
|
882
|
+
"A2UI catalog capabilities: available catalog IDs and custom component definitions.",
|
|
883
|
+
value:
|
|
884
|
+
"Available A2UI catalog:\n- declarative-gen-ui-catalog\n - Card: {...}\n - Metric: {...}",
|
|
885
|
+
},
|
|
886
|
+
],
|
|
887
|
+
},
|
|
888
|
+
},
|
|
889
|
+
tools: [{ name: "backend" }],
|
|
890
|
+
});
|
|
891
|
+
|
|
892
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
893
|
+
|
|
894
|
+
const names = received.tools.map((t: any) => t.name);
|
|
895
|
+
expect(names).toContain("backend");
|
|
896
|
+
expect(names).toContain("generate_a2ui");
|
|
897
|
+
});
|
|
898
|
+
|
|
899
|
+
it("executes generate_a2ui via wrapToolCall using the inferred model", async () => {
|
|
900
|
+
const state = {
|
|
901
|
+
messages: [],
|
|
902
|
+
thread_id: "a2ui-exec",
|
|
903
|
+
"ag-ui": { a2ui_schema: "<components/>", inject_a2ui_tool: true },
|
|
904
|
+
};
|
|
905
|
+
// First the model call infers the model + stashes the built tool.
|
|
906
|
+
await runWrap(copilotkitMiddleware, makeRequest({ state, tools: [] }));
|
|
907
|
+
|
|
908
|
+
const received = await runWrapTool(copilotkitMiddleware, {
|
|
909
|
+
toolCall: { name: "generate_a2ui", id: "1", args: {} },
|
|
910
|
+
tool: undefined,
|
|
911
|
+
state,
|
|
912
|
+
runtime: {},
|
|
913
|
+
});
|
|
914
|
+
|
|
915
|
+
expect(received.tool).toBeDefined();
|
|
916
|
+
expect(received.tool.name).toBe("generate_a2ui");
|
|
917
|
+
});
|
|
918
|
+
|
|
919
|
+
it("leaves non-A2UI tool calls untouched", async () => {
|
|
920
|
+
const state = {
|
|
921
|
+
messages: [],
|
|
922
|
+
thread_id: "a2ui-other",
|
|
923
|
+
"ag-ui": { a2ui_schema: "<components/>", inject_a2ui_tool: true },
|
|
924
|
+
};
|
|
925
|
+
await runWrap(copilotkitMiddleware, makeRequest({ state, tools: [] }));
|
|
926
|
+
|
|
927
|
+
const backendTool = { name: "backend" };
|
|
928
|
+
const received = await runWrapTool(copilotkitMiddleware, {
|
|
929
|
+
toolCall: { name: "backend", id: "1", args: {} },
|
|
930
|
+
tool: backendTool,
|
|
931
|
+
state,
|
|
932
|
+
runtime: {},
|
|
933
|
+
});
|
|
934
|
+
|
|
935
|
+
expect(received.tool).toBe(backendTool);
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
it("stops executing generate_a2ui after the run ends (afterAgent clears the bridge)", async () => {
|
|
939
|
+
const state = {
|
|
940
|
+
messages: [],
|
|
941
|
+
thread_id: "a2ui-clean",
|
|
942
|
+
"ag-ui": { a2ui_schema: "<components/>", inject_a2ui_tool: true },
|
|
943
|
+
};
|
|
944
|
+
await runWrap(copilotkitMiddleware, makeRequest({ state, tools: [] }));
|
|
945
|
+
copilotkitMiddleware.afterAgent(state, {} as any);
|
|
946
|
+
|
|
947
|
+
const received = await runWrapTool(copilotkitMiddleware, {
|
|
948
|
+
toolCall: { name: "generate_a2ui", id: "1", args: {} },
|
|
949
|
+
tool: undefined,
|
|
950
|
+
state,
|
|
951
|
+
runtime: {},
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
expect(received.tool).toBeUndefined();
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
// --- A2UI injectA2UITool flag (forwarded → ag-ui state) ------------------
|
|
958
|
+
|
|
959
|
+
it("does NOT advertise generate_a2ui when inject_a2ui_tool is false", async () => {
|
|
960
|
+
const request = makeRequest({
|
|
961
|
+
state: {
|
|
962
|
+
messages: [],
|
|
963
|
+
thread_id: "a2ui-optout",
|
|
964
|
+
"ag-ui": { a2ui_schema: "<components/>", inject_a2ui_tool: false },
|
|
965
|
+
},
|
|
966
|
+
tools: [{ name: "backend" }],
|
|
967
|
+
});
|
|
968
|
+
|
|
969
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
970
|
+
|
|
971
|
+
const names = received.tools.map((t: any) => t.name);
|
|
972
|
+
expect(names).not.toContain("generate_a2ui");
|
|
973
|
+
expect(names).toContain("backend");
|
|
974
|
+
});
|
|
975
|
+
|
|
976
|
+
it("advertises generate_a2ui when inject_a2ui_tool is true", async () => {
|
|
977
|
+
const request = makeRequest({
|
|
978
|
+
state: {
|
|
979
|
+
messages: [],
|
|
980
|
+
thread_id: "a2ui-optin",
|
|
981
|
+
"ag-ui": { a2ui_schema: "<components/>", inject_a2ui_tool: true },
|
|
982
|
+
},
|
|
983
|
+
tools: [{ name: "backend" }],
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
987
|
+
|
|
988
|
+
expect(received.tools.map((t: any) => t.name)).toContain("generate_a2ui");
|
|
989
|
+
});
|
|
990
|
+
|
|
991
|
+
it("drops the runtime's render_a2ui when injecting our generate_a2ui", async () => {
|
|
992
|
+
const request = makeRequest({
|
|
993
|
+
state: {
|
|
994
|
+
messages: [],
|
|
995
|
+
thread_id: "a2ui-drop",
|
|
996
|
+
"ag-ui": { a2ui_schema: "<components/>", inject_a2ui_tool: true },
|
|
997
|
+
copilotkit: {
|
|
998
|
+
actions: [{ name: "render_a2ui" }, { name: "fe_tool" }],
|
|
999
|
+
},
|
|
1000
|
+
},
|
|
1001
|
+
tools: [],
|
|
1002
|
+
});
|
|
1003
|
+
|
|
1004
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
1005
|
+
|
|
1006
|
+
const names = received.tools.map((t: any) => t.name);
|
|
1007
|
+
expect(names).toContain("generate_a2ui");
|
|
1008
|
+
expect(names).toContain("fe_tool");
|
|
1009
|
+
expect(names).not.toContain("render_a2ui");
|
|
1010
|
+
});
|
|
1011
|
+
|
|
1012
|
+
it("does NOT double-inject when the agent already defines generate_a2ui", async () => {
|
|
1013
|
+
const request = makeRequest({
|
|
1014
|
+
state: {
|
|
1015
|
+
messages: [],
|
|
1016
|
+
thread_id: "a2ui-dup",
|
|
1017
|
+
"ag-ui": { a2ui_schema: "<components/>", inject_a2ui_tool: true },
|
|
1018
|
+
},
|
|
1019
|
+
tools: [{ name: "generate_a2ui" }],
|
|
1020
|
+
});
|
|
1021
|
+
|
|
1022
|
+
const { received } = await runWrap(copilotkitMiddleware, request);
|
|
1023
|
+
|
|
1024
|
+
const count = received.tools.filter(
|
|
1025
|
+
(t: any) => t.name === "generate_a2ui",
|
|
1026
|
+
).length;
|
|
1027
|
+
expect(count).toBe(1);
|
|
1028
|
+
});
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
// ---------------------------------------------------------------------------
|
|
1032
|
+
// zodState — Standard-Schema JSON-schema augmentation
|
|
1033
|
+
// ---------------------------------------------------------------------------
|
|
1034
|
+
//
|
|
1035
|
+
// Contract: a Zod v4 schema only carries `~standard.validate` + `vendor`, so
|
|
1036
|
+
// LangGraph's `isStandardJSONSchema()` returns false and the field is dropped
|
|
1037
|
+
// from `output_schema`. `zodState` patches `~standard.jsonSchema.input` onto
|
|
1038
|
+
// the schema so the field survives serialization and reaches the frontend
|
|
1039
|
+
// via AG-UI `STATE_SNAPSHOT` events.
|
|
1040
|
+
|
|
1041
|
+
type JsonSchema = {
|
|
1042
|
+
type?: string;
|
|
1043
|
+
properties?: Record<string, unknown>;
|
|
1044
|
+
};
|
|
1045
|
+
type StdJson = { jsonSchema?: { input: () => JsonSchema } };
|
|
1046
|
+
const standardOf = (s: object): StdJson | undefined =>
|
|
1047
|
+
(s as { "~standard"?: StdJson })["~standard"];
|
|
1048
|
+
const hasZodV4ToJsonSchema = (): boolean =>
|
|
1049
|
+
typeof (z as { toJSONSchema?: unknown }).toJSONSchema === "function";
|
|
1050
|
+
|
|
1051
|
+
describe("zodState", () => {
|
|
1052
|
+
it("attaches a ~standard.jsonSchema.input hook to a Zod schema", () => {
|
|
1053
|
+
const schema = z.object({ name: z.string() });
|
|
1054
|
+
expect(standardOf(schema)?.jsonSchema).toBeUndefined();
|
|
1055
|
+
|
|
1056
|
+
const wrapped = zodState(schema);
|
|
1057
|
+
|
|
1058
|
+
const std = standardOf(wrapped);
|
|
1059
|
+
expect(std?.jsonSchema).toBeDefined();
|
|
1060
|
+
expect(typeof std?.jsonSchema?.input).toBe("function");
|
|
1061
|
+
});
|
|
1062
|
+
|
|
1063
|
+
it("returns a plain object from input() (real JSON Schema when zod v4 is available, else `{}`)", () => {
|
|
1064
|
+
// The contract langgraph cares about is just "input() returns an
|
|
1065
|
+
// object" — `isStandardJSONSchema()` doesn't introspect the shape.
|
|
1066
|
+
// When zod v4's `toJSONSchema` is present we get the real schema;
|
|
1067
|
+
// otherwise we get `{}`, which is still enough for the field to
|
|
1068
|
+
// appear in `output_schema` (langgraph-api treats it as opaque).
|
|
1069
|
+
const schema = z.object({ todos: z.array(z.string()) });
|
|
1070
|
+
const wrapped = zodState(schema);
|
|
1071
|
+
|
|
1072
|
+
const json = standardOf(wrapped)?.jsonSchema?.input();
|
|
1073
|
+
|
|
1074
|
+
expect(json).toBeTypeOf("object");
|
|
1075
|
+
expect(json).not.toBeNull();
|
|
1076
|
+
if (hasZodV4ToJsonSchema()) {
|
|
1077
|
+
expect(json?.type).toBe("object");
|
|
1078
|
+
expect(json?.properties?.todos).toBeDefined();
|
|
1079
|
+
}
|
|
1080
|
+
});
|
|
1081
|
+
|
|
1082
|
+
it("caches the JSON Schema across calls (input() returns the same object)", () => {
|
|
1083
|
+
const schema = z.object({ x: z.string() });
|
|
1084
|
+
const wrapped = zodState(schema);
|
|
1085
|
+
const std = standardOf(wrapped);
|
|
1086
|
+
|
|
1087
|
+
const a = std?.jsonSchema?.input();
|
|
1088
|
+
const b = std?.jsonSchema?.input();
|
|
1089
|
+
|
|
1090
|
+
expect(a).toBe(b);
|
|
1091
|
+
});
|
|
1092
|
+
|
|
1093
|
+
it("does not overwrite an existing jsonSchema hook", () => {
|
|
1094
|
+
const schema = z.object({ x: z.string() });
|
|
1095
|
+
const preset: StdJson["jsonSchema"] = { input: () => ({}) };
|
|
1096
|
+
const std = standardOf(schema);
|
|
1097
|
+
expect(std).toBeDefined();
|
|
1098
|
+
std!.jsonSchema = preset;
|
|
1099
|
+
|
|
1100
|
+
zodState(schema);
|
|
1101
|
+
|
|
1102
|
+
expect(standardOf(schema)?.jsonSchema).toBe(preset);
|
|
1103
|
+
});
|
|
1104
|
+
|
|
1105
|
+
it("returns the same reference (mutates rather than copies)", () => {
|
|
1106
|
+
const schema = z.object({ x: z.string() });
|
|
1107
|
+
expect(zodState(schema)).toBe(schema);
|
|
1108
|
+
});
|
|
1109
|
+
|
|
1110
|
+
it("is a no-op for a value without ~standard metadata", () => {
|
|
1111
|
+
const plain = { foo: "bar" };
|
|
1112
|
+
expect(() => zodState(plain)).not.toThrow();
|
|
1113
|
+
expect(zodState(plain)).toBe(plain);
|
|
1114
|
+
});
|
|
1115
|
+
|
|
1116
|
+
it("works on optional / array / default-wrapped schemas (state-field shapes)", () => {
|
|
1117
|
+
const todos = zodState(
|
|
1118
|
+
z.array(z.object({ id: z.string(), text: z.string() })).default(() => []),
|
|
1119
|
+
);
|
|
1120
|
+
|
|
1121
|
+
const std = standardOf(todos);
|
|
1122
|
+
expect(std).toBeDefined();
|
|
1123
|
+
expect(std?.jsonSchema).toBeDefined();
|
|
1124
|
+
const json = std?.jsonSchema?.input();
|
|
1125
|
+
expect(json).toBeTypeOf("object");
|
|
1126
|
+
if (hasZodV4ToJsonSchema()) {
|
|
1127
|
+
expect(json?.type).toBe("array");
|
|
1128
|
+
}
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
it("makes the wrapped field pass a StandardJSONSchemaV1-style probe", () => {
|
|
1132
|
+
// Mirrors what LangGraph's isStandardJSONSchema() looks for: an `input`
|
|
1133
|
+
// function on `~standard.jsonSchema` that returns a plain object.
|
|
1134
|
+
const schema = zodState(z.object({ liked: z.array(z.string()) }));
|
|
1135
|
+
const std = standardOf(schema);
|
|
1136
|
+
|
|
1137
|
+
expect(std?.jsonSchema).toBeDefined();
|
|
1138
|
+
expect(typeof std?.jsonSchema?.input).toBe("function");
|
|
1139
|
+
expect(typeof std?.jsonSchema?.input()).toBe("object");
|
|
1140
|
+
});
|
|
1141
|
+
});
|