@copilotkit/runtime 1.56.1 → 1.56.2-canary.pin-to-send
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/graphql/resolvers/copilot.resolver.cjs +2 -1
- package/dist/graphql/resolvers/copilot.resolver.cjs.map +1 -1
- package/dist/graphql/resolvers/copilot.resolver.mjs +2 -1
- package/dist/graphql/resolvers/copilot.resolver.mjs.map +1 -1
- package/dist/graphql/resolvers/resolve-message-id.cjs +19 -0
- package/dist/graphql/resolvers/resolve-message-id.cjs.map +1 -0
- package/dist/graphql/resolvers/resolve-message-id.mjs +18 -0
- package/dist/graphql/resolvers/resolve-message-id.mjs.map +1 -0
- package/dist/lib/runtime/copilot-runtime.cjs +4 -2
- package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.mjs +4 -2
- package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
- package/dist/package.cjs +2 -2
- package/dist/package.mjs +2 -2
- package/dist/v2/runtime/core/debug-event-bus.cjs +36 -0
- package/dist/v2/runtime/core/debug-event-bus.cjs.map +1 -0
- package/dist/v2/runtime/core/debug-event-bus.d.cts +19 -0
- package/dist/v2/runtime/core/debug-event-bus.d.cts.map +1 -0
- package/dist/v2/runtime/core/debug-event-bus.d.mts +19 -0
- package/dist/v2/runtime/core/debug-event-bus.d.mts.map +1 -0
- package/dist/v2/runtime/core/debug-event-bus.mjs +35 -0
- package/dist/v2/runtime/core/debug-event-bus.mjs.map +1 -0
- package/dist/v2/runtime/core/fetch-handler.cjs +6 -0
- package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.d.cts.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.d.mts.map +1 -1
- package/dist/v2/runtime/core/fetch-handler.mjs +6 -0
- package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -1
- package/dist/v2/runtime/core/fetch-router.cjs +1 -0
- package/dist/v2/runtime/core/fetch-router.cjs.map +1 -1
- package/dist/v2/runtime/core/fetch-router.mjs +1 -0
- package/dist/v2/runtime/core/fetch-router.mjs.map +1 -1
- package/dist/v2/runtime/core/hooks.cjs.map +1 -1
- package/dist/v2/runtime/core/hooks.d.cts +2 -0
- package/dist/v2/runtime/core/hooks.d.cts.map +1 -1
- package/dist/v2/runtime/core/hooks.d.mts +2 -0
- package/dist/v2/runtime/core/hooks.d.mts.map +1 -1
- package/dist/v2/runtime/core/hooks.mjs.map +1 -1
- package/dist/v2/runtime/core/runtime.cjs +5 -0
- package/dist/v2/runtime/core/runtime.cjs.map +1 -1
- package/dist/v2/runtime/core/runtime.d.cts +5 -0
- package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
- package/dist/v2/runtime/core/runtime.d.mts +5 -0
- package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
- package/dist/v2/runtime/core/runtime.mjs +5 -0
- package/dist/v2/runtime/core/runtime.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.cjs +2 -0
- package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.mjs +2 -0
- package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-debug-events.cjs +33 -0
- package/dist/v2/runtime/handlers/handle-debug-events.cjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-debug-events.mjs +32 -0
- package/dist/v2/runtime/handlers/handle-debug-events.mjs.map +1 -0
- package/dist/v2/runtime/handlers/handle-run.cjs +1 -0
- package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.mjs +1 -0
- package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/connect.cjs +32 -2
- package/dist/v2/runtime/handlers/intelligence/connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/connect.mjs +31 -2
- package/dist/v2/runtime/handlers/intelligence/connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs +5 -1
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs +5 -1
- package/dist/v2/runtime/handlers/shared/resolve-intelligence-user.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs +21 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.mjs +21 -1
- package/dist/v2/runtime/handlers/shared/sse-response.mjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/connect.cjs +3 -1
- package/dist/v2/runtime/handlers/sse/connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/connect.mjs +3 -1
- package/dist/v2/runtime/handlers/sse/connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/run.cjs +3 -1
- package/dist/v2/runtime/handlers/sse/run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/sse/run.mjs +3 -1
- package/dist/v2/runtime/handlers/sse/run.mjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.cjs +2 -7
- package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.cts +1 -4
- package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.mts +1 -4
- package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs +2 -7
- package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
- package/dist/v2/runtime/runner/intelligence.cjs +17 -5
- package/dist/v2/runtime/runner/intelligence.cjs.map +1 -1
- package/dist/v2/runtime/runner/intelligence.d.cts +1 -0
- package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -1
- package/dist/v2/runtime/runner/intelligence.d.mts +1 -0
- package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -1
- package/dist/v2/runtime/runner/intelligence.mjs +17 -5
- package/dist/v2/runtime/runner/intelligence.mjs.map +1 -1
- package/package.json +3 -3
- package/src/agents/langgraph/__tests__/event-source.test.ts +256 -0
- package/src/graphql/resolvers/__tests__/resolve-message-id.test.ts +25 -0
- package/src/graphql/resolvers/copilot.resolver.ts +2 -1
- package/src/graphql/resolvers/resolve-message-id.ts +14 -0
- package/src/lib/runtime/__tests__/handle-service-adapter.test.ts +108 -0
- package/src/lib/runtime/__tests__/retry-utils.test.ts +137 -0
- package/src/lib/runtime/agent-integrations/langgraph/__tests__/dispatch-event-filtering.test.ts +190 -0
- package/src/lib/runtime/copilot-runtime.ts +20 -4
- package/src/lib/runtime/retry-utils.ts +41 -1
- package/src/v2/runtime/__tests__/fetch-router.test.ts +22 -0
- package/src/v2/runtime/__tests__/handle-connect.test.ts +58 -5
- package/src/v2/runtime/__tests__/handle-run.test.ts +31 -4
- package/src/v2/runtime/__tests__/handle-threads.test.ts +66 -4
- package/src/v2/runtime/__tests__/integration/node-servers.integration.test.ts +19 -0
- package/src/v2/runtime/__tests__/integration/suites/debug-events.suite.ts +253 -0
- package/src/v2/runtime/__tests__/runtime.test.ts +3 -1
- package/src/v2/runtime/core/__tests__/debug-event-bus.test.ts +156 -0
- package/src/v2/runtime/core/debug-event-bus.ts +45 -0
- package/src/v2/runtime/core/fetch-handler.ts +4 -0
- package/src/v2/runtime/core/fetch-router.ts +11 -0
- package/src/v2/runtime/core/hooks.ts +2 -1
- package/src/v2/runtime/core/runtime.ts +12 -0
- package/src/v2/runtime/handlers/__tests__/handle-debug-events.test.ts +176 -0
- package/src/v2/runtime/handlers/handle-connect.ts +2 -0
- package/src/v2/runtime/handlers/handle-debug-events.ts +52 -0
- package/src/v2/runtime/handlers/handle-run.ts +1 -0
- package/src/v2/runtime/handlers/intelligence/connect.ts +58 -1
- package/src/v2/runtime/handlers/shared/resolve-intelligence-user.ts +4 -1
- package/src/v2/runtime/handlers/shared/sse-response.ts +46 -0
- package/src/v2/runtime/handlers/sse/__tests__/sse-connect-agent-id.test.ts +71 -0
- package/src/v2/runtime/handlers/sse/connect.ts +6 -0
- package/src/v2/runtime/handlers/sse/run.ts +4 -0
- package/src/v2/runtime/intelligence-platform/__tests__/client.test.ts +13 -11
- package/src/v2/runtime/intelligence-platform/client.ts +3 -11
- package/src/v2/runtime/runner/__tests__/intelligence-runner.test.ts +51 -1
- package/src/v2/runtime/runner/intelligence.ts +27 -9
package/src/lib/runtime/agent-integrations/langgraph/__tests__/dispatch-event-filtering.test.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { EventType } from "@ag-ui/client";
|
|
2
2
|
import { LangGraphAgent } from "../agent";
|
|
3
|
+
import { CustomEventNames } from "../consts";
|
|
4
|
+
import { vi } from "vitest";
|
|
3
5
|
|
|
4
6
|
function createAgent() {
|
|
5
7
|
const agent = new LangGraphAgent({
|
|
@@ -33,6 +35,25 @@ function makeTextEvent(
|
|
|
33
35
|
};
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
function makeCustomEvent(name: string, value: any) {
|
|
39
|
+
return {
|
|
40
|
+
type: EventType.CUSTOM,
|
|
41
|
+
name,
|
|
42
|
+
value,
|
|
43
|
+
} as any;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Mock the parent class's langGraphDefaultMergeState for a single test,
|
|
48
|
+
* using vi.spyOn for automatic cleanup.
|
|
49
|
+
*/
|
|
50
|
+
function withMockedParentMerge(agent: LangGraphAgent, returnValue: any) {
|
|
51
|
+
const parentProto = Object.getPrototypeOf(Object.getPrototypeOf(agent));
|
|
52
|
+
return vi
|
|
53
|
+
.spyOn(parentProto, "langGraphDefaultMergeState")
|
|
54
|
+
.mockReturnValue(returnValue);
|
|
55
|
+
}
|
|
56
|
+
|
|
36
57
|
describe("dispatchEvent emit-messages filtering", () => {
|
|
37
58
|
it("suppresses message events when copilotkit:emit-messages is false", () => {
|
|
38
59
|
const { agent, events } = createAgent();
|
|
@@ -153,3 +174,172 @@ describe("dispatchEvent emit-tool-calls filtering", () => {
|
|
|
153
174
|
expect(events).toHaveLength(1);
|
|
154
175
|
});
|
|
155
176
|
});
|
|
177
|
+
|
|
178
|
+
// ---------- CopilotKit custom event dispatch ----------
|
|
179
|
+
|
|
180
|
+
describe("dispatchEvent custom CopilotKit events", () => {
|
|
181
|
+
it("manually_emit_message produces TextMessage event sequence", () => {
|
|
182
|
+
const { agent, events } = createAgent();
|
|
183
|
+
|
|
184
|
+
const result = agent.dispatchEvent(
|
|
185
|
+
makeCustomEvent(CustomEventNames.CopilotKitManuallyEmitMessage, {
|
|
186
|
+
message_id: "msg-manual-1",
|
|
187
|
+
message: "Hello from agent",
|
|
188
|
+
role: "assistant",
|
|
189
|
+
}),
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
expect(result).toBe(true);
|
|
193
|
+
expect(events).toHaveLength(3);
|
|
194
|
+
expect(events[0].type).toBe(EventType.TEXT_MESSAGE_START);
|
|
195
|
+
expect(events[0].messageId).toBe("msg-manual-1");
|
|
196
|
+
expect(events[0].role).toBe("assistant");
|
|
197
|
+
expect(events[1].type).toBe(EventType.TEXT_MESSAGE_CONTENT);
|
|
198
|
+
expect(events[1].delta).toBe("Hello from agent");
|
|
199
|
+
expect(events[2].type).toBe(EventType.TEXT_MESSAGE_END);
|
|
200
|
+
expect(events[2].messageId).toBe("msg-manual-1");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("manually_emit_tool_call produces ToolCall event sequence", () => {
|
|
204
|
+
const { agent, events } = createAgent();
|
|
205
|
+
|
|
206
|
+
const result = agent.dispatchEvent(
|
|
207
|
+
makeCustomEvent(CustomEventNames.CopilotKitManuallyEmitToolCall, {
|
|
208
|
+
id: "tc-manual-1",
|
|
209
|
+
name: "SearchTool",
|
|
210
|
+
args: { query: "test" },
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
expect(result).toBe(true);
|
|
215
|
+
expect(events).toHaveLength(3);
|
|
216
|
+
expect(events[0].type).toBe(EventType.TOOL_CALL_START);
|
|
217
|
+
expect(events[0].toolCallId).toBe("tc-manual-1");
|
|
218
|
+
expect(events[0].toolCallName).toBe("SearchTool");
|
|
219
|
+
expect(events[1].type).toBe(EventType.TOOL_CALL_ARGS);
|
|
220
|
+
expect(events[1].toolCallId).toBe("tc-manual-1");
|
|
221
|
+
expect(events[1].delta).toEqual({ query: "test" });
|
|
222
|
+
expect(events[2].type).toBe(EventType.TOOL_CALL_END);
|
|
223
|
+
expect(events[2].toolCallId).toBe("tc-manual-1");
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("manually_emit_state produces StateSnapshot event", () => {
|
|
227
|
+
const { agent, events } = createAgent();
|
|
228
|
+
|
|
229
|
+
// Mock getStateSnapshot since it depends on thread state
|
|
230
|
+
(agent as any).getStateSnapshot = (state: any) => ({
|
|
231
|
+
values: state.values,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const result = agent.dispatchEvent(
|
|
235
|
+
makeCustomEvent(
|
|
236
|
+
CustomEventNames.CopilotKitManuallyEmitIntermediateState,
|
|
237
|
+
{
|
|
238
|
+
progress: 75,
|
|
239
|
+
},
|
|
240
|
+
),
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
expect(result).toBe(true);
|
|
244
|
+
expect((agent as any).activeRun.manuallyEmittedState).toEqual({
|
|
245
|
+
progress: 75,
|
|
246
|
+
});
|
|
247
|
+
const snapshotEvents = events.filter(
|
|
248
|
+
(e) => e.type === EventType.STATE_SNAPSHOT,
|
|
249
|
+
);
|
|
250
|
+
expect(snapshotEvents.length).toBeGreaterThanOrEqual(1);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("copilotkit_exit produces Exit custom event", () => {
|
|
254
|
+
const { agent, events } = createAgent();
|
|
255
|
+
|
|
256
|
+
const result = agent.dispatchEvent(
|
|
257
|
+
makeCustomEvent(CustomEventNames.CopilotKitExit, {}),
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
expect(result).toBe(true);
|
|
261
|
+
expect(events).toHaveLength(1);
|
|
262
|
+
expect(events[0].type).toBe(EventType.CUSTOM);
|
|
263
|
+
// "Exit" is the hardcoded downstream name in agent.ts — not a constant,
|
|
264
|
+
// because it's the value the frontend listens for, not an internal enum.
|
|
265
|
+
expect(events[0].name).toBe("Exit");
|
|
266
|
+
expect(events[0].value).toBe(true);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("events without rawEvent pass through to subscriber", () => {
|
|
270
|
+
const { agent, events } = createAgent();
|
|
271
|
+
|
|
272
|
+
const result = agent.dispatchEvent({
|
|
273
|
+
type: EventType.TEXT_MESSAGE_START,
|
|
274
|
+
messageId: "msg-no-raw",
|
|
275
|
+
role: "assistant",
|
|
276
|
+
} as any);
|
|
277
|
+
|
|
278
|
+
expect(result).toBe(true);
|
|
279
|
+
expect(events).toHaveLength(1);
|
|
280
|
+
expect(events[0].messageId).toBe("msg-no-raw");
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// ---------- langGraphDefaultMergeState ----------
|
|
285
|
+
|
|
286
|
+
describe("langGraphDefaultMergeState", () => {
|
|
287
|
+
afterEach(() => {
|
|
288
|
+
vi.restoreAllMocks();
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("merges copilotkit actions from ag-ui tools", () => {
|
|
292
|
+
const { agent } = createAgent();
|
|
293
|
+
const tools = [{ name: "tool1" }, { name: "tool2" }];
|
|
294
|
+
|
|
295
|
+
withMockedParentMerge(agent, {
|
|
296
|
+
"ag-ui": { tools, context: [] },
|
|
297
|
+
tools: [],
|
|
298
|
+
messages: [],
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const result = agent.langGraphDefaultMergeState({} as any, [], {} as any);
|
|
302
|
+
expect(result.copilotkit).toBeDefined();
|
|
303
|
+
expect(result.copilotkit.actions).toEqual(expect.arrayContaining(tools));
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("merges copilotkit context from ag-ui", () => {
|
|
307
|
+
const { agent } = createAgent();
|
|
308
|
+
const context = [{ description: "user info", value: "test" }];
|
|
309
|
+
|
|
310
|
+
withMockedParentMerge(agent, {
|
|
311
|
+
"ag-ui": { tools: [], context },
|
|
312
|
+
tools: [],
|
|
313
|
+
messages: [],
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const result = agent.langGraphDefaultMergeState({} as any, [], {} as any);
|
|
317
|
+
expect(result.copilotkit.context).toEqual(context);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("handles missing ag-ui key without crashing", () => {
|
|
321
|
+
const { agent } = createAgent();
|
|
322
|
+
|
|
323
|
+
withMockedParentMerge(agent, { messages: [] });
|
|
324
|
+
|
|
325
|
+
const result = agent.langGraphDefaultMergeState({} as any, [], {} as any);
|
|
326
|
+
expect(result.copilotkit).toBeDefined();
|
|
327
|
+
expect(result.copilotkit.actions).toEqual([]);
|
|
328
|
+
expect(result.copilotkit.context).toEqual([]);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("deduplicates tools from returnedTools and ag-ui tools", () => {
|
|
332
|
+
const { agent } = createAgent();
|
|
333
|
+
const tool = { name: "SharedTool", id: "shared-1" };
|
|
334
|
+
|
|
335
|
+
withMockedParentMerge(agent, {
|
|
336
|
+
"ag-ui": { tools: [tool], context: [] },
|
|
337
|
+
tools: [tool],
|
|
338
|
+
messages: [],
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
const result = agent.langGraphDefaultMergeState({} as any, [], {} as any);
|
|
342
|
+
expect(result.copilotkit.actions).toHaveLength(1);
|
|
343
|
+
expect(result.copilotkit.actions[0].name).toBe("SharedTool");
|
|
344
|
+
});
|
|
345
|
+
});
|
|
@@ -463,10 +463,26 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {
|
|
|
463
463
|
}
|
|
464
464
|
|
|
465
465
|
if (isAgentsListEmpty) {
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
466
|
+
const languageModel = serviceAdapter.getLanguageModel?.();
|
|
467
|
+
if (languageModel) {
|
|
468
|
+
// Adapter exposes a pre-configured LanguageModel (e.g. OpenAI/Anthropic adapters)
|
|
469
|
+
agentsList.default = new BuiltInAgent({ model: languageModel });
|
|
470
|
+
} else if (serviceAdapter.provider && serviceAdapter.model) {
|
|
471
|
+
// Adapter exposes provider/model strings
|
|
472
|
+
agentsList.default = new BuiltInAgent({
|
|
473
|
+
model: `${serviceAdapter.provider}/${serviceAdapter.model}`,
|
|
474
|
+
});
|
|
475
|
+
} else {
|
|
476
|
+
throw new CopilotKitMisuseError({
|
|
477
|
+
message:
|
|
478
|
+
`Service adapter "${serviceAdapter.name ?? "unknown"}" does not provide model information. ` +
|
|
479
|
+
`When using adapters like LangChainAdapter without an explicit agents list, ` +
|
|
480
|
+
`please provide a default agent in the runtime config. Example:\n` +
|
|
481
|
+
` new CopilotRuntime({\n` +
|
|
482
|
+
` agents: { default: new BuiltInAgent({ model: "openai/gpt-4o" }) }\n` +
|
|
483
|
+
` })`,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
470
486
|
}
|
|
471
487
|
|
|
472
488
|
const actions = this.params?.actions;
|
|
@@ -7,6 +7,8 @@ export const RETRY_CONFIG = {
|
|
|
7
7
|
maxDelayMs: 5000,
|
|
8
8
|
// HTTP status codes that should be retried
|
|
9
9
|
retryableStatusCodes: [502, 503, 504, 408, 429],
|
|
10
|
+
// Maximum Retry-After value (in seconds) we're willing to honor
|
|
11
|
+
maxRetryAfterSeconds: 60,
|
|
10
12
|
// Network error patterns that should be retried
|
|
11
13
|
retryableErrorMessages: [
|
|
12
14
|
"fetch failed",
|
|
@@ -44,6 +46,28 @@ export function sleep(ms: number): Promise<void> {
|
|
|
44
46
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
45
47
|
}
|
|
46
48
|
|
|
49
|
+
// Parse the Retry-After header value into milliseconds.
|
|
50
|
+
// Returns undefined if the header is missing or unparseable.
|
|
51
|
+
export function parseRetryAfter(response: Response): number | undefined {
|
|
52
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
53
|
+
if (!retryAfter) return undefined;
|
|
54
|
+
|
|
55
|
+
// Try as seconds (integer)
|
|
56
|
+
const seconds = Number(retryAfter);
|
|
57
|
+
if (!Number.isNaN(seconds) && seconds >= 0) {
|
|
58
|
+
return seconds * 1000;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Try as HTTP-date
|
|
62
|
+
const date = Date.parse(retryAfter);
|
|
63
|
+
if (!Number.isNaN(date)) {
|
|
64
|
+
const delayMs = date - Date.now();
|
|
65
|
+
return delayMs > 0 ? delayMs : 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
47
71
|
// Calculate exponential backoff delay
|
|
48
72
|
export function calculateDelay(attempt: number): number {
|
|
49
73
|
const delay = RETRY_CONFIG.baseDelayMs * Math.pow(2, attempt);
|
|
@@ -67,7 +91,23 @@ export async function fetchWithRetry(
|
|
|
67
91
|
isRetryableError(null, response) &&
|
|
68
92
|
attempt < RETRY_CONFIG.maxRetries
|
|
69
93
|
) {
|
|
70
|
-
|
|
94
|
+
let delay = calculateDelay(attempt);
|
|
95
|
+
|
|
96
|
+
// Honor Retry-After header on 429 responses
|
|
97
|
+
if (response.status === 429) {
|
|
98
|
+
const retryAfterMs = parseRetryAfter(response);
|
|
99
|
+
if (retryAfterMs !== undefined) {
|
|
100
|
+
const maxMs = RETRY_CONFIG.maxRetryAfterSeconds * 1000;
|
|
101
|
+
if (retryAfterMs > maxMs) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`Server requested Retry-After of ${Math.ceil(retryAfterMs / 1000)}s ` +
|
|
104
|
+
`which exceeds the maximum of ${RETRY_CONFIG.maxRetryAfterSeconds}s`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
delay = retryAfterMs;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
71
111
|
logger?.warn(
|
|
72
112
|
`Request to ${url} failed with status ${response.status}. ` +
|
|
73
113
|
`Retrying attempt ${attempt + 1}/${RETRY_CONFIG.maxRetries + 1} in ${delay}ms.`,
|
|
@@ -135,6 +135,11 @@ describe("fetch-router", () => {
|
|
|
135
135
|
const result = matchRoute("/info", "/");
|
|
136
136
|
expect(result).toEqual({ method: "info" });
|
|
137
137
|
});
|
|
138
|
+
|
|
139
|
+
it("matches GET /cpk-debug-events", () => {
|
|
140
|
+
const result = matchRoute("/api/copilotkit/cpk-debug-events", basePath);
|
|
141
|
+
expect(result).toEqual({ method: "cpk-debug-events" });
|
|
142
|
+
});
|
|
138
143
|
});
|
|
139
144
|
|
|
140
145
|
describe("without basePath (suffix matching)", () => {
|
|
@@ -204,5 +209,22 @@ describe("fetch-router", () => {
|
|
|
204
209
|
const result = matchRoute("/api/v2/copilotkit/agent/a1/run");
|
|
205
210
|
expect(result).toEqual({ method: "agent/run", agentId: "a1" });
|
|
206
211
|
});
|
|
212
|
+
|
|
213
|
+
it("matches /cpk-debug-events suffix", () => {
|
|
214
|
+
const result = matchRoute("/api/copilotkit/cpk-debug-events");
|
|
215
|
+
expect(result).toEqual({ method: "cpk-debug-events" });
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("matches bare /cpk-debug-events", () => {
|
|
219
|
+
const result = matchRoute("/cpk-debug-events");
|
|
220
|
+
expect(result).toEqual({ method: "cpk-debug-events" });
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
describe("cpk-debug-events route with basePath", () => {
|
|
225
|
+
it("matches /cpk-debug-events with /api basePath", () => {
|
|
226
|
+
const result = matchRoute("/api/cpk-debug-events", "/api");
|
|
227
|
+
expect(result).toEqual({ method: "cpk-debug-events" });
|
|
228
|
+
});
|
|
207
229
|
});
|
|
208
230
|
});
|
|
@@ -228,7 +228,9 @@ describe("handleConnectAgent", () => {
|
|
|
228
228
|
afterRequestMiddleware: undefined,
|
|
229
229
|
runner,
|
|
230
230
|
mode: "intelligence",
|
|
231
|
-
identifyUser: vi
|
|
231
|
+
identifyUser: vi
|
|
232
|
+
.fn()
|
|
233
|
+
.mockResolvedValue({ id: "user-1", name: "User One" }),
|
|
232
234
|
intelligence: platform,
|
|
233
235
|
} as unknown as CopilotRuntime;
|
|
234
236
|
};
|
|
@@ -262,6 +264,7 @@ describe("handleConnectAgent", () => {
|
|
|
262
264
|
expect(platform.ɵconnectThread).toHaveBeenCalledWith({
|
|
263
265
|
threadId: "thread-1",
|
|
264
266
|
userId: "user-1",
|
|
267
|
+
runId: "run-1",
|
|
265
268
|
lastSeenEventId: null,
|
|
266
269
|
});
|
|
267
270
|
});
|
|
@@ -271,7 +274,15 @@ describe("handleConnectAgent", () => {
|
|
|
271
274
|
ɵconnectThread: vi.fn().mockResolvedValue({
|
|
272
275
|
mode: "bootstrap",
|
|
273
276
|
latestEventId: "event-2",
|
|
274
|
-
events: [
|
|
277
|
+
events: [
|
|
278
|
+
{
|
|
279
|
+
type: "RUN_STARTED",
|
|
280
|
+
threadId: "thread-1",
|
|
281
|
+
run_id: "backend-run-1",
|
|
282
|
+
input: { messages: [] },
|
|
283
|
+
},
|
|
284
|
+
{ type: "RUN_FINISHED" },
|
|
285
|
+
],
|
|
275
286
|
}),
|
|
276
287
|
};
|
|
277
288
|
const runtime = createIntelligenceRuntime(platform);
|
|
@@ -287,7 +298,23 @@ describe("handleConnectAgent", () => {
|
|
|
287
298
|
expect(body).toEqual({
|
|
288
299
|
mode: "bootstrap",
|
|
289
300
|
latestEventId: "event-2",
|
|
290
|
-
events: [
|
|
301
|
+
events: [
|
|
302
|
+
{
|
|
303
|
+
type: "RUN_STARTED",
|
|
304
|
+
threadId: "thread-1",
|
|
305
|
+
runId: "run-1",
|
|
306
|
+
input: {
|
|
307
|
+
messages: [],
|
|
308
|
+
threadId: "thread-1",
|
|
309
|
+
runId: "run-1",
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
type: "RUN_FINISHED",
|
|
314
|
+
threadId: "thread-1",
|
|
315
|
+
runId: "run-1",
|
|
316
|
+
},
|
|
317
|
+
],
|
|
291
318
|
});
|
|
292
319
|
});
|
|
293
320
|
|
|
@@ -310,6 +337,7 @@ describe("handleConnectAgent", () => {
|
|
|
310
337
|
expect(platform.ɵconnectThread).toHaveBeenCalledWith({
|
|
311
338
|
threadId: "thread-1",
|
|
312
339
|
userId: "user-1",
|
|
340
|
+
runId: "run-1",
|
|
313
341
|
lastSeenEventId: null,
|
|
314
342
|
});
|
|
315
343
|
});
|
|
@@ -349,6 +377,7 @@ describe("handleConnectAgent", () => {
|
|
|
349
377
|
expect(platform.ɵconnectThread).toHaveBeenCalledWith({
|
|
350
378
|
threadId: "thread-1",
|
|
351
379
|
userId: "user-1",
|
|
380
|
+
runId: "run-1",
|
|
352
381
|
lastSeenEventId: "event-9",
|
|
353
382
|
});
|
|
354
383
|
});
|
|
@@ -357,7 +386,9 @@ describe("handleConnectAgent", () => {
|
|
|
357
386
|
const platform = {
|
|
358
387
|
ɵconnectThread: vi.fn().mockResolvedValue(null),
|
|
359
388
|
};
|
|
360
|
-
const identifyUser = vi
|
|
389
|
+
const identifyUser = vi
|
|
390
|
+
.fn()
|
|
391
|
+
.mockResolvedValue({ id: "resolved-user", name: "Resolved User" });
|
|
361
392
|
const runtime = createIntelligenceRuntime(platform);
|
|
362
393
|
runtime.identifyUser = identifyUser;
|
|
363
394
|
const request = createConnectRequest(
|
|
@@ -377,6 +408,7 @@ describe("handleConnectAgent", () => {
|
|
|
377
408
|
expect(platform.ɵconnectThread).toHaveBeenCalledWith({
|
|
378
409
|
threadId: "thread-1",
|
|
379
410
|
userId: "resolved-user",
|
|
411
|
+
runId: "run-1",
|
|
380
412
|
lastSeenEventId: "event-9",
|
|
381
413
|
});
|
|
382
414
|
});
|
|
@@ -386,7 +418,28 @@ describe("handleConnectAgent", () => {
|
|
|
386
418
|
ɵconnectThread: vi.fn(),
|
|
387
419
|
};
|
|
388
420
|
const runtime = createIntelligenceRuntime(platform);
|
|
389
|
-
runtime.identifyUser = vi
|
|
421
|
+
runtime.identifyUser = vi
|
|
422
|
+
.fn()
|
|
423
|
+
.mockResolvedValue({ id: "", name: "User" });
|
|
424
|
+
|
|
425
|
+
const response = await handleConnectAgent({
|
|
426
|
+
runtime,
|
|
427
|
+
request: createConnectRequest(),
|
|
428
|
+
agentId: "my-agent",
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
expect(response.status).toBe(400);
|
|
432
|
+
expect(platform.ɵconnectThread).not.toHaveBeenCalled();
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it("returns 400 when identifyUser returns an invalid name", async () => {
|
|
436
|
+
const platform = {
|
|
437
|
+
ɵconnectThread: vi.fn(),
|
|
438
|
+
};
|
|
439
|
+
const runtime = createIntelligenceRuntime(platform);
|
|
440
|
+
runtime.identifyUser = vi
|
|
441
|
+
.fn()
|
|
442
|
+
.mockResolvedValue({ id: "user-1", name: "" });
|
|
390
443
|
|
|
391
444
|
const response = await handleConnectAgent({
|
|
392
445
|
runtime,
|
|
@@ -298,7 +298,9 @@ describe("handleRunAgent", () => {
|
|
|
298
298
|
generateThreadNames?: boolean;
|
|
299
299
|
identifyUser?: (
|
|
300
300
|
request: Request,
|
|
301
|
-
) =>
|
|
301
|
+
) =>
|
|
302
|
+
| { id: string; name: string }
|
|
303
|
+
| Promise<{ id: string; name: string }>;
|
|
302
304
|
},
|
|
303
305
|
) => {
|
|
304
306
|
const runner = Object.create(IntelligenceAgentRunner.prototype);
|
|
@@ -318,7 +320,8 @@ describe("handleRunAgent", () => {
|
|
|
318
320
|
generateThreadNames: options?.generateThreadNames ?? false,
|
|
319
321
|
intelligence: platform,
|
|
320
322
|
identifyUser:
|
|
321
|
-
options?.identifyUser ??
|
|
323
|
+
options?.identifyUser ??
|
|
324
|
+
vi.fn().mockResolvedValue({ id: "user-1", name: "User One" }),
|
|
322
325
|
} as unknown as CopilotRuntime;
|
|
323
326
|
};
|
|
324
327
|
|
|
@@ -395,7 +398,9 @@ describe("handleRunAgent", () => {
|
|
|
395
398
|
.fn()
|
|
396
399
|
.mockResolvedValue({ joinToken: "jt-123", joinCode: "jc-123" }),
|
|
397
400
|
};
|
|
398
|
-
const identifyUser = vi
|
|
401
|
+
const identifyUser = vi
|
|
402
|
+
.fn()
|
|
403
|
+
.mockResolvedValue({ id: "resolved-user", name: "Resolved User" });
|
|
399
404
|
const runtime = createIntelligenceRuntime(agent, platform, {
|
|
400
405
|
identifyUser,
|
|
401
406
|
});
|
|
@@ -870,7 +875,29 @@ describe("handleRunAgent", () => {
|
|
|
870
875
|
ɵacquireThreadLock: vi.fn(),
|
|
871
876
|
};
|
|
872
877
|
const runtime = createIntelligenceRuntime(agent, platform, {
|
|
873
|
-
identifyUser: vi.fn().mockResolvedValue({ id: "" }),
|
|
878
|
+
identifyUser: vi.fn().mockResolvedValue({ id: "", name: "User" }),
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
const response = await handleRunAgent({
|
|
882
|
+
runtime,
|
|
883
|
+
request: createRunRequest(),
|
|
884
|
+
agentId: "my-agent",
|
|
885
|
+
});
|
|
886
|
+
|
|
887
|
+
expect(response.status).toBe(400);
|
|
888
|
+
expect(platform.getOrCreateThread).not.toHaveBeenCalled();
|
|
889
|
+
expect(platform.ɵacquireThreadLock).not.toHaveBeenCalled();
|
|
890
|
+
});
|
|
891
|
+
|
|
892
|
+
it("returns 400 when identifyUser returns an invalid name", async () => {
|
|
893
|
+
const agent = createAgentForIntelligence();
|
|
894
|
+
const platform = {
|
|
895
|
+
getOrCreateThread: vi.fn(),
|
|
896
|
+
getThreadMessages: vi.fn(),
|
|
897
|
+
ɵacquireThreadLock: vi.fn(),
|
|
898
|
+
};
|
|
899
|
+
const runtime = createIntelligenceRuntime(agent, platform, {
|
|
900
|
+
identifyUser: vi.fn().mockResolvedValue({ id: "user-1", name: "" }),
|
|
874
901
|
});
|
|
875
902
|
|
|
876
903
|
const response = await handleRunAgent({
|
|
@@ -10,12 +10,13 @@ import {
|
|
|
10
10
|
import { CopilotRuntime } from "../core/runtime";
|
|
11
11
|
|
|
12
12
|
describe("thread handlers", () => {
|
|
13
|
-
const createIdentifyUser = () =>
|
|
13
|
+
const createIdentifyUser = () =>
|
|
14
|
+
vi.fn().mockResolvedValue({ id: "user-1", name: "User One" });
|
|
14
15
|
|
|
15
16
|
const createIntelligenceRuntime = (options?: {
|
|
16
17
|
identifyUser?: (
|
|
17
18
|
request: Request,
|
|
18
|
-
) => { id: string } | Promise<{ id: string }>;
|
|
19
|
+
) => { id: string; name: string } | Promise<{ id: string; name: string }>;
|
|
19
20
|
intelligence?: Record<string, unknown>;
|
|
20
21
|
}) =>
|
|
21
22
|
({
|
|
@@ -92,7 +93,25 @@ describe("thread handlers", () => {
|
|
|
92
93
|
};
|
|
93
94
|
const runtime = createIntelligenceRuntime({
|
|
94
95
|
intelligence,
|
|
95
|
-
identifyUser: vi.fn().mockResolvedValue({ id: "" }),
|
|
96
|
+
identifyUser: vi.fn().mockResolvedValue({ id: "", name: "User" }),
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const response = await handleListThreads({
|
|
100
|
+
runtime,
|
|
101
|
+
request: new Request("https://example.com/threads?agentId=agent-1"),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
expect(response.status).toBe(400);
|
|
105
|
+
expect(intelligence.listThreads).not.toHaveBeenCalled();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("returns 400 when identifyUser returns an invalid name for thread list", async () => {
|
|
109
|
+
const intelligence = {
|
|
110
|
+
listThreads: vi.fn(),
|
|
111
|
+
};
|
|
112
|
+
const runtime = createIntelligenceRuntime({
|
|
113
|
+
intelligence,
|
|
114
|
+
identifyUser: vi.fn().mockResolvedValue({ id: "user-1", name: "" }),
|
|
96
115
|
});
|
|
97
116
|
|
|
98
117
|
const response = await handleListThreads({
|
|
@@ -258,7 +277,50 @@ describe("thread handlers", () => {
|
|
|
258
277
|
};
|
|
259
278
|
const runtime = createIntelligenceRuntime({
|
|
260
279
|
intelligence,
|
|
261
|
-
identifyUser: vi.fn().mockResolvedValue({ id: "" }),
|
|
280
|
+
identifyUser: vi.fn().mockResolvedValue({ id: "", name: "User" }),
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const updateResponse = await handleUpdateThread({
|
|
284
|
+
runtime,
|
|
285
|
+
request: createMutationRequest("/threads/thread-1", "PATCH", {
|
|
286
|
+
agentId: "agent-1",
|
|
287
|
+
}),
|
|
288
|
+
threadId: "thread-1",
|
|
289
|
+
});
|
|
290
|
+
expect(updateResponse.status).toBe(400);
|
|
291
|
+
|
|
292
|
+
const archiveResponse = await handleArchiveThread({
|
|
293
|
+
runtime,
|
|
294
|
+
request: createMutationRequest("/threads/thread-1/archive", "POST", {
|
|
295
|
+
agentId: "agent-1",
|
|
296
|
+
}),
|
|
297
|
+
threadId: "thread-1",
|
|
298
|
+
});
|
|
299
|
+
expect(archiveResponse.status).toBe(400);
|
|
300
|
+
|
|
301
|
+
const deleteResponse = await handleDeleteThread({
|
|
302
|
+
runtime,
|
|
303
|
+
request: createMutationRequest("/threads/thread-1", "DELETE", {
|
|
304
|
+
agentId: "agent-1",
|
|
305
|
+
}),
|
|
306
|
+
threadId: "thread-1",
|
|
307
|
+
});
|
|
308
|
+
expect(deleteResponse.status).toBe(400);
|
|
309
|
+
|
|
310
|
+
expect(intelligence.updateThread).not.toHaveBeenCalled();
|
|
311
|
+
expect(intelligence.archiveThread).not.toHaveBeenCalled();
|
|
312
|
+
expect(intelligence.deleteThread).not.toHaveBeenCalled();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("returns 400 when identifyUser returns an invalid name for thread mutations", async () => {
|
|
316
|
+
const intelligence = {
|
|
317
|
+
updateThread: vi.fn(),
|
|
318
|
+
archiveThread: vi.fn(),
|
|
319
|
+
deleteThread: vi.fn(),
|
|
320
|
+
};
|
|
321
|
+
const runtime = createIntelligenceRuntime({
|
|
322
|
+
intelligence,
|
|
323
|
+
identifyUser: vi.fn().mockResolvedValue({ id: "user-1", name: "" }),
|
|
262
324
|
});
|
|
263
325
|
|
|
264
326
|
const updateResponse = await handleUpdateThread({
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
|
|
11
11
|
import { multiEndpointSuite } from "./suites/multi-endpoint.suite";
|
|
12
12
|
import { singleEndpointSuite } from "./suites/single-endpoint.suite";
|
|
13
|
+
import {
|
|
14
|
+
debugEventsSuite,
|
|
15
|
+
debugEventsProductionGuardSuite,
|
|
16
|
+
} from "./suites/debug-events.suite";
|
|
13
17
|
|
|
14
18
|
// Server factories
|
|
15
19
|
import { createExpressMultiServer } from "./servers/express-multi";
|
|
@@ -37,3 +41,18 @@ singleEndpointSuite("Node", createNodeSingleServer);
|
|
|
37
41
|
singleEndpointSuite("Fetch", (opts) =>
|
|
38
42
|
Promise.resolve(createFetchDirectHandler("single-route", opts)),
|
|
39
43
|
);
|
|
44
|
+
|
|
45
|
+
// ─── Debug Events ───────────────────────────────────────────────────
|
|
46
|
+
|
|
47
|
+
debugEventsSuite("Express", createExpressMultiServer);
|
|
48
|
+
debugEventsSuite("Hono", createHonoMultiServer);
|
|
49
|
+
debugEventsSuite("Node", createNodeMultiServer);
|
|
50
|
+
debugEventsSuite("Fetch", (opts) =>
|
|
51
|
+
Promise.resolve(createFetchDirectHandler("multi-route", opts)),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
debugEventsProductionGuardSuite(
|
|
55
|
+
() => createFetchDirectHandler("multi-route"),
|
|
56
|
+
"http://localhost",
|
|
57
|
+
"/api/copilotkit",
|
|
58
|
+
);
|