@copilotkit/runtime 1.56.2 → 1.56.4-canary.1777529757
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/agent/converters/tanstack.cjs +121 -25
- package/dist/agent/converters/tanstack.cjs.map +1 -1
- package/dist/agent/converters/tanstack.d.cts.map +1 -1
- package/dist/agent/converters/tanstack.d.mts.map +1 -1
- package/dist/agent/converters/tanstack.mjs +121 -25
- package/dist/agent/converters/tanstack.mjs.map +1 -1
- 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/agent-integrations/langgraph/agent.cjs +8 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.cjs.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.cts.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.d.mts.map +1 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.mjs +8 -1
- package/dist/lib/runtime/agent-integrations/langgraph/agent.mjs.map +1 -1
- 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 +7 -7
- package/dist/package.mjs +7 -7
- package/dist/v2/index.d.cts +2 -2
- package/dist/v2/index.d.mts +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 +8 -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 +8 -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 -1
- 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/endpoints/express.cjs +5 -5
- package/dist/v2/runtime/endpoints/express.cjs.map +1 -1
- package/dist/v2/runtime/endpoints/express.mjs +5 -5
- package/dist/v2/runtime/endpoints/express.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.cjs +3 -2
- package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-connect.mjs +3 -2
- 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 +24 -4
- package/dist/v2/runtime/handlers/intelligence/connect.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/connect.mjs +25 -5
- package/dist/v2/runtime/handlers/intelligence/connect.mjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.cjs +111 -26
- package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/intelligence/run.mjs +111 -26
- package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/intelligence-utils.cjs +7 -3
- package/dist/v2/runtime/handlers/shared/intelligence-utils.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/intelligence-utils.mjs +7 -3
- package/dist/v2/runtime/handlers/shared/intelligence-utils.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/index.d.cts +1 -1
- package/dist/v2/runtime/index.d.mts +1 -2
- package/dist/v2/runtime/index.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.cjs +6 -8
- package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.cts +16 -21
- package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.d.mts +16 -21
- package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
- package/dist/v2/runtime/intelligence-platform/client.mjs +6 -8
- package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
- package/dist/v2/runtime/runner/agent-runner.cjs.map +1 -1
- package/dist/v2/runtime/runner/agent-runner.d.cts +0 -1
- package/dist/v2/runtime/runner/agent-runner.d.cts.map +1 -1
- package/dist/v2/runtime/runner/agent-runner.d.mts +0 -1
- package/dist/v2/runtime/runner/agent-runner.d.mts.map +1 -1
- package/dist/v2/runtime/runner/agent-runner.mjs.map +1 -1
- package/dist/v2/runtime/runner/index.d.cts +1 -1
- package/dist/v2/runtime/runner/index.d.mts +1 -1
- package/dist/v2/runtime/runner/intelligence.cjs +47 -10
- package/dist/v2/runtime/runner/intelligence.cjs.map +1 -1
- package/dist/v2/runtime/runner/intelligence.d.cts +8 -1
- package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -1
- package/dist/v2/runtime/runner/intelligence.d.mts +8 -1
- package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -1
- package/dist/v2/runtime/runner/intelligence.mjs +47 -10
- package/dist/v2/runtime/runner/intelligence.mjs.map +1 -1
- package/dist/v2/runtime/telemetry/instance-created.cjs +33 -0
- package/dist/v2/runtime/telemetry/instance-created.cjs.map +1 -0
- package/dist/v2/runtime/telemetry/instance-created.mjs +33 -0
- package/dist/v2/runtime/telemetry/instance-created.mjs.map +1 -0
- package/dist/v2/runtime/telemetry/telemetry-client.cjs +1 -38
- package/dist/v2/runtime/telemetry/telemetry-client.cjs.map +1 -1
- package/dist/v2/runtime/telemetry/telemetry-client.mjs +1 -37
- package/dist/v2/runtime/telemetry/telemetry-client.mjs.map +1 -1
- package/package.json +8 -8
- package/src/agent/__tests__/agent-test-helpers.ts +31 -1
- package/src/agent/__tests__/converter-tanstack.test.ts +280 -0
- package/src/agent/converters/tanstack.ts +167 -10
- 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/agent-integrations/langgraph/agent.ts +8 -1
- package/src/lib/runtime/copilot-runtime.ts +20 -4
- package/src/lib/runtime/retry-utils.ts +41 -1
- package/src/v2/runtime/__tests__/express-fetch-bridge.test.ts +1 -1
- package/src/v2/runtime/__tests__/express-single-telemetry.integration.test.ts +65 -0
- package/src/v2/runtime/__tests__/express-telemetry.integration.test.ts +101 -0
- package/src/v2/runtime/__tests__/fetch-router.test.ts +22 -0
- package/src/v2/runtime/__tests__/handle-connect.test.ts +183 -23
- package/src/v2/runtime/__tests__/handle-run.test.ts +411 -33
- package/src/v2/runtime/__tests__/handle-threads.test.ts +66 -4
- package/src/v2/runtime/__tests__/hono-single-telemetry.integration.test.ts +46 -0
- package/src/v2/runtime/__tests__/hono-telemetry.integration.test.ts +99 -0
- 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__/intelligence-run-telemetry.test.ts +194 -0
- package/src/v2/runtime/__tests__/runtime.test.ts +3 -1
- package/src/v2/runtime/__tests__/sse-response-telemetry.test.ts +108 -0
- package/src/v2/runtime/__tests__/telemetry.test.ts +0 -61
- 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 +7 -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/endpoints/express.ts +9 -3
- package/src/v2/runtime/handlers/__tests__/handle-debug-events.test.ts +176 -0
- package/src/v2/runtime/handlers/handle-connect.ts +2 -1
- 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 +48 -11
- package/src/v2/runtime/handlers/intelligence/run.ts +162 -21
- package/src/v2/runtime/handlers/shared/intelligence-utils.ts +21 -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 +33 -37
- package/src/v2/runtime/intelligence-platform/client.ts +37 -40
- package/src/v2/runtime/runner/__tests__/intelligence-runner.test.ts +66 -8
- package/src/v2/runtime/runner/agent-runner.ts +0 -1
- package/src/v2/runtime/runner/intelligence.ts +74 -15
- package/src/v2/runtime/telemetry/__tests__/instance-created.test.ts +96 -0
- package/src/v2/runtime/telemetry/instance-created.ts +44 -0
- package/src/v2/runtime/telemetry/telemetry-client.ts +1 -57
- package/dist/v2/runtime/intelligence-platform/index.d.mts +0 -2
- package/dist/v2/runtime/telemetry/utils.cjs +0 -15
- package/dist/v2/runtime/telemetry/utils.cjs.map +0 -1
- package/dist/v2/runtime/telemetry/utils.mjs +0 -14
- package/dist/v2/runtime/telemetry/utils.mjs.map +0 -1
- package/src/v2/runtime/telemetry/utils.ts +0 -15
|
@@ -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({
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration test: Hono single-route adapter (deprecated path) + telemetry.
|
|
3
|
+
*
|
|
4
|
+
* `createCopilotEndpointSingleRoute` is the legacy direct single-route entry
|
|
5
|
+
* point, superseded by `createCopilotHonoHandler({ mode: "single-route" })`
|
|
6
|
+
* but still exported.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
9
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
10
|
+
|
|
11
|
+
import { telemetry } from "../telemetry";
|
|
12
|
+
import { createCopilotEndpointSingleRoute } from "../endpoints/hono-single";
|
|
13
|
+
import { CopilotRuntime } from "../core/runtime";
|
|
14
|
+
|
|
15
|
+
function makeAgent(): AbstractAgent {
|
|
16
|
+
const a: unknown = { execute: async () => ({ events: [] }) };
|
|
17
|
+
(a as { clone: () => unknown }).clone = () => makeAgent();
|
|
18
|
+
return a as AbstractAgent;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe("Hono single-route adapter — telemetry firing (integration)", () => {
|
|
22
|
+
let captureSpy: ReturnType<typeof vi.spyOn>;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
captureSpy = vi.spyOn(telemetry, "capture").mockResolvedValue(undefined);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
captureSpy.mockRestore();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("fires instance_created on handler creation", async () => {
|
|
33
|
+
const runtime = new CopilotRuntime({ agents: { default: makeAgent() } });
|
|
34
|
+
createCopilotEndpointSingleRoute({ runtime, basePath: "/api/copilotkit" });
|
|
35
|
+
|
|
36
|
+
await vi.waitFor(() => {
|
|
37
|
+
expect(captureSpy).toHaveBeenCalledWith(
|
|
38
|
+
"oss.runtime.instance_created",
|
|
39
|
+
expect.objectContaining({
|
|
40
|
+
agentsAmount: 1,
|
|
41
|
+
"cloud.api_key_provided": false,
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration test: Hono adapter + telemetry.
|
|
3
|
+
*
|
|
4
|
+
* Asserts both ends of the path-to-production chain that the v2 refactor
|
|
5
|
+
* previously broke (2ac4a40b5, 2026-03-29):
|
|
6
|
+
* 1. `oss.runtime.instance_created` fires once per handler factory.
|
|
7
|
+
* 2. `oss.runtime.copilot_request_created` fires when a real HTTP request
|
|
8
|
+
* flows through the handler.
|
|
9
|
+
*
|
|
10
|
+
* If either regresses, this test fails.
|
|
11
|
+
*/
|
|
12
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
13
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
14
|
+
|
|
15
|
+
import { telemetry } from "../telemetry";
|
|
16
|
+
import { createCopilotHonoHandler } from "../endpoints/hono";
|
|
17
|
+
import { CopilotRuntime } from "../core/runtime";
|
|
18
|
+
|
|
19
|
+
function makeAgent(): AbstractAgent {
|
|
20
|
+
const a: unknown = {
|
|
21
|
+
execute: async () => ({ events: [] }),
|
|
22
|
+
};
|
|
23
|
+
(a as { clone: () => unknown }).clone = () => makeAgent();
|
|
24
|
+
return a as AbstractAgent;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("Hono adapter — telemetry firing (integration)", () => {
|
|
28
|
+
let captureSpy: ReturnType<typeof vi.spyOn>;
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
captureSpy = vi.spyOn(telemetry, "capture").mockResolvedValue(undefined);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
afterEach(() => {
|
|
35
|
+
captureSpy.mockRestore();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("fires instance_created on handler creation (multi-route)", async () => {
|
|
39
|
+
const runtime = new CopilotRuntime({ agents: { default: makeAgent() } });
|
|
40
|
+
createCopilotHonoHandler({ runtime, basePath: "/" });
|
|
41
|
+
|
|
42
|
+
await vi.waitFor(() => {
|
|
43
|
+
expect(captureSpy).toHaveBeenCalledWith(
|
|
44
|
+
"oss.runtime.instance_created",
|
|
45
|
+
expect.objectContaining({
|
|
46
|
+
agentsAmount: 1,
|
|
47
|
+
actionsAmount: 0,
|
|
48
|
+
endpointsAmount: 0,
|
|
49
|
+
"cloud.api_key_provided": false,
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("fires copilot_request_created when a real HTTP request hits the handler", async () => {
|
|
56
|
+
const runtime = new CopilotRuntime({ agents: { default: makeAgent() } });
|
|
57
|
+
const endpoint = createCopilotHonoHandler({ runtime, basePath: "/" });
|
|
58
|
+
|
|
59
|
+
await endpoint.fetch(
|
|
60
|
+
new Request("https://example.com/agent/default/run", {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers: { "Content-Type": "application/json" },
|
|
63
|
+
body: JSON.stringify({ messages: [], state: {}, threadId: "t1" }),
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(captureSpy).toHaveBeenCalledWith(
|
|
68
|
+
"oss.runtime.copilot_request_created",
|
|
69
|
+
expect.objectContaining({
|
|
70
|
+
requestType: "run",
|
|
71
|
+
"cloud.api_key_provided": false,
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("includes cloud.public_api_key on request when header is present", async () => {
|
|
77
|
+
const runtime = new CopilotRuntime({ agents: { default: makeAgent() } });
|
|
78
|
+
const endpoint = createCopilotHonoHandler({ runtime, basePath: "/" });
|
|
79
|
+
|
|
80
|
+
await endpoint.fetch(
|
|
81
|
+
new Request("https://example.com/agent/default/run", {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
"Content-Type": "application/json",
|
|
85
|
+
"x-copilotcloud-public-api-key": "ck_pub_test_123",
|
|
86
|
+
},
|
|
87
|
+
body: JSON.stringify({ messages: [], state: {}, threadId: "t1" }),
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(captureSpy).toHaveBeenCalledWith(
|
|
92
|
+
"oss.runtime.copilot_request_created",
|
|
93
|
+
expect.objectContaining({
|
|
94
|
+
"cloud.api_key_provided": true,
|
|
95
|
+
"cloud.public_api_key": "ck_pub_test_123",
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -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
|
+
);
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, afterAll } from "vitest";
|
|
2
|
+
import type { ServerHandle } from "../servers/types";
|
|
3
|
+
import { readSSEStream } from "../helpers/sse-reader";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Envelope shape returned in the debug SSE stream.
|
|
7
|
+
*/
|
|
8
|
+
interface DebugEnvelope {
|
|
9
|
+
timestamp: number;
|
|
10
|
+
agentId: string;
|
|
11
|
+
threadId: string;
|
|
12
|
+
runId: string;
|
|
13
|
+
event: { type: string; [key: string]: unknown };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Parse debug envelopes from SSE payload text.
|
|
18
|
+
* Each `data:` line contains a JSON DebugEventEnvelope.
|
|
19
|
+
*/
|
|
20
|
+
function parseDebugEnvelopes(ssePayload: string): DebugEnvelope[] {
|
|
21
|
+
const envelopes: DebugEnvelope[] = [];
|
|
22
|
+
for (const line of ssePayload.split("\n")) {
|
|
23
|
+
if (!line.startsWith("data:")) continue;
|
|
24
|
+
const json = line.slice("data:".length).trim();
|
|
25
|
+
if (!json) continue;
|
|
26
|
+
try {
|
|
27
|
+
envelopes.push(JSON.parse(json));
|
|
28
|
+
} catch {
|
|
29
|
+
// skip malformed lines
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return envelopes;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Read from a long-lived debug SSE stream until we see a RUN_FINISHED envelope
|
|
37
|
+
* or a timeout elapses. Returns the raw text accumulated.
|
|
38
|
+
*
|
|
39
|
+
* IMPORTANT: The debug SSE stream is long-lived and never closes on its own.
|
|
40
|
+
* We MUST use a timeout to stop reading, and cancel the reader afterwards.
|
|
41
|
+
*/
|
|
42
|
+
async function readDebugStream(
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
+
stream: ReadableStream<any>,
|
|
45
|
+
opts: { waitMs?: number } = {},
|
|
46
|
+
): Promise<string> {
|
|
47
|
+
const waitMs = opts.waitMs ?? 4_000;
|
|
48
|
+
const reader = stream.getReader();
|
|
49
|
+
const decoder = new TextDecoder();
|
|
50
|
+
let output = "";
|
|
51
|
+
let stopped = false;
|
|
52
|
+
|
|
53
|
+
const timer = setTimeout(() => {
|
|
54
|
+
stopped = true;
|
|
55
|
+
reader.cancel().catch(() => {});
|
|
56
|
+
}, waitMs);
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
while (!stopped) {
|
|
60
|
+
const result = await reader.read().catch(() => ({
|
|
61
|
+
done: true as const,
|
|
62
|
+
value: undefined,
|
|
63
|
+
}));
|
|
64
|
+
if (result.done) break;
|
|
65
|
+
if (result.value) {
|
|
66
|
+
output +=
|
|
67
|
+
typeof result.value === "string"
|
|
68
|
+
? result.value
|
|
69
|
+
: decoder.decode(result.value as Uint8Array, { stream: true });
|
|
70
|
+
if (output.includes("RUN_FINISHED")) {
|
|
71
|
+
stopped = true;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
} finally {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
// Do NOT await reader.cancel() — on tee'd streams (created by response.clone()
|
|
79
|
+
// inside the fetch handler), awaiting cancel hangs indefinitely because the
|
|
80
|
+
// other tee branch is never consumed.
|
|
81
|
+
reader.cancel().catch(() => {});
|
|
82
|
+
output += decoder.decode();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return output;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Shared debug-events integration test suite.
|
|
90
|
+
*
|
|
91
|
+
* @param name Display name, e.g. "Express"
|
|
92
|
+
* @param factory Creates & starts the server; returns a handle
|
|
93
|
+
*/
|
|
94
|
+
export function debugEventsSuite(
|
|
95
|
+
name: string,
|
|
96
|
+
factory: (opts?: {
|
|
97
|
+
capturedHeaders?: Record<string, string>[];
|
|
98
|
+
}) => Promise<ServerHandle & { handler?: (r: Request) => Promise<Response> }>,
|
|
99
|
+
) {
|
|
100
|
+
describe(`[${name}] Debug Events`, () => {
|
|
101
|
+
let handle: ServerHandle & { handler?: (r: Request) => Promise<Response> };
|
|
102
|
+
let doFetch: (
|
|
103
|
+
input: RequestInfo | URL,
|
|
104
|
+
init?: RequestInit,
|
|
105
|
+
) => Promise<Response>;
|
|
106
|
+
|
|
107
|
+
beforeAll(async () => {
|
|
108
|
+
handle = await factory();
|
|
109
|
+
doFetch = handle.handler
|
|
110
|
+
? (input, init) =>
|
|
111
|
+
handle.handler!(
|
|
112
|
+
new Request(
|
|
113
|
+
typeof input === "string" || input instanceof URL
|
|
114
|
+
? input
|
|
115
|
+
: input,
|
|
116
|
+
init,
|
|
117
|
+
),
|
|
118
|
+
)
|
|
119
|
+
: fetch;
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
afterAll(async () => {
|
|
123
|
+
await handle?.close();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const url = (path: string) => `${handle.baseUrl}${handle.basePath}${path}`;
|
|
127
|
+
|
|
128
|
+
// ─── SSE Format + Events + Envelope Structure ────────────────────
|
|
129
|
+
// Combined into a single test to avoid orphaned debug-stream subscribers
|
|
130
|
+
// across tests (the debug SSE endpoint is long-lived and its cleanup
|
|
131
|
+
// depends on the request signal being aborted).
|
|
132
|
+
|
|
133
|
+
it("streams debug event envelopes with correct structure during an agent run", async () => {
|
|
134
|
+
const controller = new AbortController();
|
|
135
|
+
|
|
136
|
+
// Start the debug stream. For real HTTP servers, fetch blocks until
|
|
137
|
+
// the first chunk arrives, so we also start the agent run concurrently.
|
|
138
|
+
const debugFetchPromise = doFetch(url("/cpk-debug-events"), {
|
|
139
|
+
signal: controller.signal,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Give the subscription a tick to register
|
|
143
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
144
|
+
|
|
145
|
+
// Trigger an agent run. We start it AND begin consuming its stream
|
|
146
|
+
// concurrently with reading the debug stream.
|
|
147
|
+
const runRes = await doFetch(url("/agent/default/run"), {
|
|
148
|
+
method: "POST",
|
|
149
|
+
headers: { "Content-Type": "application/json" },
|
|
150
|
+
body: JSON.stringify({
|
|
151
|
+
threadId: "t-debug-1",
|
|
152
|
+
runId: "r-debug-1",
|
|
153
|
+
messages: [],
|
|
154
|
+
state: {},
|
|
155
|
+
tools: [],
|
|
156
|
+
context: [],
|
|
157
|
+
forwardedProps: {},
|
|
158
|
+
}),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Consume the run stream and the debug stream concurrently.
|
|
162
|
+
// Both are needed: the debug stream blocks until events arrive,
|
|
163
|
+
// and the run stream must be consumed to avoid backpressure.
|
|
164
|
+
const [debugRes, runPayload] = await Promise.all([
|
|
165
|
+
debugFetchPromise,
|
|
166
|
+
readSSEStream(runRes.body!),
|
|
167
|
+
]);
|
|
168
|
+
|
|
169
|
+
// ── SSE response format ──
|
|
170
|
+
expect(debugRes.status).toBe(200);
|
|
171
|
+
expect(debugRes.headers.get("content-type")).toContain(
|
|
172
|
+
"text/event-stream",
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
// Run completed — events should be buffered in the debug stream.
|
|
176
|
+
expect(runPayload).toContain("RUN_FINISHED");
|
|
177
|
+
|
|
178
|
+
// Read the debug stream (events are already in the buffer)
|
|
179
|
+
const debugPayload = await readDebugStream(debugRes.body!, {
|
|
180
|
+
waitMs: 4_000,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const envelopes = parseDebugEnvelopes(debugPayload);
|
|
184
|
+
|
|
185
|
+
// ── Events flow through ──
|
|
186
|
+
expect(envelopes.length).toBeGreaterThan(0);
|
|
187
|
+
|
|
188
|
+
const eventTypes = envelopes.map((e) => e.event.type);
|
|
189
|
+
expect(eventTypes).toContain("RUN_STARTED");
|
|
190
|
+
expect(eventTypes).toContain("RUN_FINISHED");
|
|
191
|
+
|
|
192
|
+
// ── Envelope structure ──
|
|
193
|
+
for (const envelope of envelopes) {
|
|
194
|
+
expect(typeof envelope.timestamp).toBe("number");
|
|
195
|
+
expect(envelope.timestamp).toBeGreaterThan(0);
|
|
196
|
+
expect(envelope.agentId).toBe("default");
|
|
197
|
+
expect(typeof envelope.threadId).toBe("string");
|
|
198
|
+
expect(typeof envelope.runId).toBe("string");
|
|
199
|
+
expect(envelope.event).toBeDefined();
|
|
200
|
+
expect(typeof envelope.event.type).toBe("string");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Full event sequence
|
|
204
|
+
expect(eventTypes).toContain("TEXT_MESSAGE_START");
|
|
205
|
+
expect(eventTypes).toContain("TEXT_MESSAGE_CONTENT");
|
|
206
|
+
expect(eventTypes).toContain("TEXT_MESSAGE_END");
|
|
207
|
+
|
|
208
|
+
// Clean up: abort the request so the debug subscriber is removed
|
|
209
|
+
controller.abort();
|
|
210
|
+
}, 15_000);
|
|
211
|
+
|
|
212
|
+
// Regression guard for agentId forwarding on /connect lives in a
|
|
213
|
+
// dedicated unit test (sse-connect-agent-id.test.ts) — driving /connect
|
|
214
|
+
// through the integration runtime doesn't emit events in a
|
|
215
|
+
// test-friendly way, so the unit test feeds a synthetic observable
|
|
216
|
+
// into handleSseConnect and asserts the envelope carries the route
|
|
217
|
+
// agentId rather than the literal string "connect".
|
|
218
|
+
|
|
219
|
+
// ─── HTTP Method Validation ──────────────────────────────────────
|
|
220
|
+
|
|
221
|
+
it("POST /cpk-debug-events returns 405", async () => {
|
|
222
|
+
const res = await doFetch(url("/cpk-debug-events"), { method: "POST" });
|
|
223
|
+
expect(res.status).toBe(405);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Production guard test -- only needs the fetch-direct handler since
|
|
230
|
+
* it doesn't require a real server. Tests that NODE_ENV=production
|
|
231
|
+
* returns 404 for the debug-events endpoint.
|
|
232
|
+
*/
|
|
233
|
+
export function debugEventsProductionGuardSuite(
|
|
234
|
+
createHandler: () => { handler: (r: Request) => Promise<Response> },
|
|
235
|
+
baseUrl: string,
|
|
236
|
+
basePath: string,
|
|
237
|
+
) {
|
|
238
|
+
describe("[Fetch] Debug Events – production guard", () => {
|
|
239
|
+
it("returns 404 when NODE_ENV=production", async () => {
|
|
240
|
+
const originalEnv = process.env.NODE_ENV;
|
|
241
|
+
try {
|
|
242
|
+
process.env.NODE_ENV = "production";
|
|
243
|
+
const { handler } = createHandler();
|
|
244
|
+
const res = await handler(
|
|
245
|
+
new Request(`${baseUrl}${basePath}/cpk-debug-events`),
|
|
246
|
+
);
|
|
247
|
+
expect(res.status).toBe(404);
|
|
248
|
+
} finally {
|
|
249
|
+
process.env.NODE_ENV = originalEnv;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
}
|