@copilotkit/runtime 1.55.3-canary.1776260990 → 1.56.0
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/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/lib/index.cjs +1 -1
- package/dist/lib/index.d.cts +1 -1
- package/dist/lib/index.d.mts +1 -1
- package/dist/lib/index.mjs +1 -1
- package/dist/lib/integrations/shared.cjs +1 -1
- package/dist/lib/integrations/shared.d.cts +1 -1
- package/dist/lib/integrations/shared.d.mts +1 -1
- package/dist/lib/integrations/shared.mjs +1 -1
- package/dist/lib/runtime/copilot-runtime.cjs +1 -0
- package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.cts +13 -1
- package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.d.mts +13 -1
- package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
- package/dist/lib/runtime/copilot-runtime.mjs +1 -0
- package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
- package/dist/package.cjs +3 -3
- package/dist/package.mjs +3 -3
- package/dist/v2/runtime/core/runtime.cjs +12 -0
- package/dist/v2/runtime/core/runtime.cjs.map +1 -1
- package/dist/v2/runtime/core/runtime.d.cts +10 -1
- package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
- package/dist/v2/runtime/core/runtime.d.mts +10 -1
- package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
- package/dist/v2/runtime/core/runtime.mjs +13 -1
- package/dist/v2/runtime/core/runtime.mjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.cjs +7 -1
- package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
- package/dist/v2/runtime/handlers/handle-run.mjs +7 -1
- package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs +40 -1
- package/dist/v2/runtime/handlers/shared/sse-response.cjs.map +1 -1
- package/dist/v2/runtime/handlers/shared/sse-response.mjs +40 -1
- package/dist/v2/runtime/handlers/shared/sse-response.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/package.json +4 -4
- package/src/lib/runtime/copilot-runtime.ts +15 -0
- package/src/v2/runtime/__tests__/debug-sse-response.test.ts +302 -0
- package/src/v2/runtime/core/runtime.ts +27 -0
- package/src/v2/runtime/handlers/handle-run.ts +15 -1
- package/src/v2/runtime/handlers/shared/sse-response.ts +69 -0
- package/src/v2/runtime/handlers/sse/run.ts +9 -0
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { BaseEvent } from "@ag-ui/client";
|
|
2
2
|
import { EventEncoder } from "@ag-ui/encoder";
|
|
3
3
|
import { Observable, Subscription } from "rxjs";
|
|
4
|
+
import { ResolvedDebugConfig } from "@copilotkit/shared";
|
|
5
|
+
import {
|
|
6
|
+
createLogger,
|
|
7
|
+
type CopilotRuntimeLogger,
|
|
8
|
+
} from "../../../../lib/logger";
|
|
4
9
|
import { telemetry } from "../../telemetry";
|
|
5
10
|
|
|
6
11
|
interface CreateSseEventResponseParams {
|
|
@@ -8,17 +13,27 @@ interface CreateSseEventResponseParams {
|
|
|
8
13
|
observableFactory: () =>
|
|
9
14
|
| Promise<Observable<BaseEvent>>
|
|
10
15
|
| Observable<BaseEvent>;
|
|
16
|
+
debug?: ResolvedDebugConfig;
|
|
17
|
+
/** Pre-created logger instance to avoid creating a new pino logger per request. */
|
|
18
|
+
logger?: CopilotRuntimeLogger;
|
|
11
19
|
}
|
|
12
20
|
|
|
13
21
|
export function createSseEventResponse({
|
|
14
22
|
request,
|
|
15
23
|
observableFactory,
|
|
24
|
+
debug,
|
|
25
|
+
logger,
|
|
16
26
|
}: CreateSseEventResponseParams): Response {
|
|
17
27
|
const stream = new TransformStream();
|
|
18
28
|
const writer = stream.writable.getWriter();
|
|
19
29
|
const encoder = new EventEncoder();
|
|
20
30
|
let streamClosed = false;
|
|
21
31
|
|
|
32
|
+
const debugLogger = debug?.enabled
|
|
33
|
+
? (logger ??
|
|
34
|
+
createLogger({ level: "debug", component: "copilotkit-debug" }))
|
|
35
|
+
: undefined;
|
|
36
|
+
|
|
22
37
|
const closeStream = async () => {
|
|
23
38
|
if (!streamClosed) {
|
|
24
39
|
try {
|
|
@@ -50,10 +65,29 @@ export function createSseEventResponse({
|
|
|
50
65
|
|
|
51
66
|
telemetry.capture("oss.runtime.agent_execution_stream_started", {});
|
|
52
67
|
|
|
68
|
+
if (debug?.lifecycle) {
|
|
69
|
+
debugLogger!.debug("SSE stream opened");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let eventCount = 0;
|
|
73
|
+
let loggedEventCount = 0;
|
|
74
|
+
|
|
53
75
|
subscription = observable.subscribe({
|
|
54
76
|
next: async (event) => {
|
|
55
77
|
if (!request.signal.aborted && !streamClosed) {
|
|
56
78
|
try {
|
|
79
|
+
eventCount++;
|
|
80
|
+
if (debug?.events) {
|
|
81
|
+
loggedEventCount++;
|
|
82
|
+
if (debug.verbose) {
|
|
83
|
+
debugLogger!.debug({ event }, "Event emitted");
|
|
84
|
+
} else {
|
|
85
|
+
debugLogger!.debug(
|
|
86
|
+
{ type: event.type, ...summarizeEvent(event) },
|
|
87
|
+
"Event emitted",
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
57
91
|
await writer.write(encoder.encode(event));
|
|
58
92
|
} catch (error) {
|
|
59
93
|
if (error instanceof Error && error.name === "AbortError") {
|
|
@@ -66,11 +100,23 @@ export function createSseEventResponse({
|
|
|
66
100
|
telemetry.capture("oss.runtime.agent_execution_stream_errored", {
|
|
67
101
|
error: error instanceof Error ? error.message : String(error),
|
|
68
102
|
});
|
|
103
|
+
if (debug?.lifecycle) {
|
|
104
|
+
debugLogger!.debug(
|
|
105
|
+
{ error: error instanceof Error ? error.message : String(error) },
|
|
106
|
+
"SSE stream errored",
|
|
107
|
+
);
|
|
108
|
+
}
|
|
69
109
|
logError(error);
|
|
70
110
|
await closeStream();
|
|
71
111
|
},
|
|
72
112
|
complete: async () => {
|
|
73
113
|
telemetry.capture("oss.runtime.agent_execution_stream_ended", {});
|
|
114
|
+
if (debug?.lifecycle) {
|
|
115
|
+
debugLogger!.debug(
|
|
116
|
+
{ eventCount, loggedEventCount },
|
|
117
|
+
"SSE stream completed",
|
|
118
|
+
);
|
|
119
|
+
}
|
|
74
120
|
await closeStream();
|
|
75
121
|
},
|
|
76
122
|
});
|
|
@@ -98,3 +144,26 @@ export function createSseEventResponse({
|
|
|
98
144
|
},
|
|
99
145
|
});
|
|
100
146
|
}
|
|
147
|
+
|
|
148
|
+
function summarizeEvent(event: BaseEvent): Record<string, unknown> {
|
|
149
|
+
const e = event as any;
|
|
150
|
+
const summary: Record<string, unknown> = {};
|
|
151
|
+
|
|
152
|
+
if (e.messageId) summary.messageId = e.messageId;
|
|
153
|
+
if (e.toolCallId) summary.toolCallId = e.toolCallId;
|
|
154
|
+
if (e.toolCallName) summary.toolCallName = e.toolCallName;
|
|
155
|
+
if (e.role) summary.role = e.role;
|
|
156
|
+
if (e.delta != null && typeof e.delta === "string")
|
|
157
|
+
summary.deltaLength = e.delta.length;
|
|
158
|
+
if (e.snapshot && typeof e.snapshot === "object")
|
|
159
|
+
summary.snapshotKeys = Object.keys(e.snapshot);
|
|
160
|
+
if (e.delta && Array.isArray(e.delta))
|
|
161
|
+
summary.operationCount = e.delta.length;
|
|
162
|
+
if (e.threadId) summary.threadId = e.threadId;
|
|
163
|
+
if (e.runId) summary.runId = e.runId;
|
|
164
|
+
if (e.message) summary.message = e.message;
|
|
165
|
+
if (e.code) summary.code = e.code;
|
|
166
|
+
if (e.stepName) summary.stepName = e.stepName;
|
|
167
|
+
|
|
168
|
+
return summary;
|
|
169
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { AbstractAgent, RunAgentInput } from "@ag-ui/client";
|
|
2
|
+
import { ResolvedDebugConfig } from "@copilotkit/shared";
|
|
3
|
+
import { type CopilotRuntimeLogger } from "../../../../lib/logger";
|
|
2
4
|
import { CopilotRuntimeLike } from "../../core/runtime";
|
|
3
5
|
import { createSseEventResponse } from "../shared/sse-response";
|
|
4
6
|
|
|
@@ -7,6 +9,9 @@ interface HandleSseRunParams {
|
|
|
7
9
|
request: Request;
|
|
8
10
|
agent: AbstractAgent;
|
|
9
11
|
input: RunAgentInput;
|
|
12
|
+
debug?: ResolvedDebugConfig;
|
|
13
|
+
/** Pre-created logger instance to avoid creating a new pino logger per request. */
|
|
14
|
+
logger?: CopilotRuntimeLogger;
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
export function handleSseRun({
|
|
@@ -14,9 +19,13 @@ export function handleSseRun({
|
|
|
14
19
|
request,
|
|
15
20
|
agent,
|
|
16
21
|
input,
|
|
22
|
+
debug,
|
|
23
|
+
logger,
|
|
17
24
|
}: HandleSseRunParams): Response {
|
|
18
25
|
return createSseEventResponse({
|
|
19
26
|
request,
|
|
27
|
+
debug,
|
|
28
|
+
logger,
|
|
20
29
|
observableFactory: () =>
|
|
21
30
|
runtime.runner.run({
|
|
22
31
|
threadId: input.threadId,
|