@copilotkitnext/runtime 1.52.2-next.0 → 1.52.2-next.2
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/endpoints/express-single.cjs +4 -2
- package/dist/endpoints/express-single.cjs.map +1 -1
- package/dist/endpoints/express-single.mjs +4 -2
- package/dist/endpoints/express-single.mjs.map +1 -1
- package/dist/endpoints/express.cjs +4 -2
- package/dist/endpoints/express.cjs.map +1 -1
- package/dist/endpoints/express.mjs +4 -2
- package/dist/endpoints/express.mjs.map +1 -1
- package/dist/endpoints/hono-single.cjs +1 -1
- package/dist/endpoints/hono-single.cjs.map +1 -1
- package/dist/endpoints/hono-single.mjs +1 -1
- package/dist/endpoints/hono-single.mjs.map +1 -1
- package/dist/endpoints/hono.cjs +1 -1
- package/dist/endpoints/hono.cjs.map +1 -1
- package/dist/endpoints/hono.mjs +1 -1
- package/dist/endpoints/hono.mjs.map +1 -1
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -1
- package/dist/middleware-sse-parser.cjs +137 -0
- package/dist/middleware-sse-parser.cjs.map +1 -0
- package/dist/middleware-sse-parser.d.cts +21 -0
- package/dist/middleware-sse-parser.d.cts.map +1 -0
- package/dist/middleware-sse-parser.d.mts +21 -0
- package/dist/middleware-sse-parser.d.mts.map +1 -0
- package/dist/middleware-sse-parser.mjs +136 -0
- package/dist/middleware-sse-parser.mjs.map +1 -0
- package/dist/middleware.cjs +6 -1
- package/dist/middleware.cjs.map +1 -1
- package/dist/middleware.d.cts +7 -0
- package/dist/middleware.d.cts.map +1 -1
- package/dist/middleware.d.mts +7 -0
- package/dist/middleware.d.mts.map +1 -1
- package/dist/middleware.mjs +6 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/runner/index.cjs +1 -0
- package/dist/runner/index.d.cts +1 -0
- package/dist/runner/index.d.mts +1 -0
- package/dist/runner/index.mjs +1 -0
- package/dist/runner/intelligence.cjs +115 -0
- package/dist/runner/intelligence.cjs.map +1 -0
- package/dist/runner/intelligence.d.cts +25 -0
- package/dist/runner/intelligence.d.cts.map +1 -0
- package/dist/runner/intelligence.d.mts +25 -0
- package/dist/runner/intelligence.d.mts.map +1 -0
- package/dist/runner/intelligence.mjs +114 -0
- package/dist/runner/intelligence.mjs.map +1 -0
- package/package.json +8 -5
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
let _copilotkitnext_shared = require("@copilotkitnext/shared");
|
|
3
|
+
|
|
4
|
+
//#region src/middleware-sse-parser.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parse a cloned SSE Response body into structured messages.
|
|
7
|
+
* Returns empty results for non-SSE responses.
|
|
8
|
+
*/
|
|
9
|
+
async function parseSSEResponse(response) {
|
|
10
|
+
if (!(response.headers.get("content-type") ?? "").includes("text/event-stream")) return { messages: [] };
|
|
11
|
+
let text;
|
|
12
|
+
try {
|
|
13
|
+
text = await response.text();
|
|
14
|
+
} catch {
|
|
15
|
+
_copilotkitnext_shared.logger.warn("Failed to read SSE response body in afterRequestMiddleware");
|
|
16
|
+
return { messages: [] };
|
|
17
|
+
}
|
|
18
|
+
if (!text.trim()) return { messages: [] };
|
|
19
|
+
let threadId;
|
|
20
|
+
let runId;
|
|
21
|
+
const messagesById = /* @__PURE__ */ new Map();
|
|
22
|
+
const toolCallsById = /* @__PURE__ */ new Map();
|
|
23
|
+
const toolCallParent = /* @__PURE__ */ new Map();
|
|
24
|
+
let snapshotMessages;
|
|
25
|
+
for (const line of text.split("\n")) {
|
|
26
|
+
const trimmed = line.trim();
|
|
27
|
+
if (!trimmed.startsWith("data:")) continue;
|
|
28
|
+
let event;
|
|
29
|
+
try {
|
|
30
|
+
event = JSON.parse(trimmed.slice(5).trim());
|
|
31
|
+
} catch {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
switch (event.type) {
|
|
35
|
+
case "RUN_STARTED":
|
|
36
|
+
threadId = event.threadId;
|
|
37
|
+
runId = event.runId;
|
|
38
|
+
break;
|
|
39
|
+
case "MESSAGES_SNAPSHOT":
|
|
40
|
+
if (Array.isArray(event.messages)) snapshotMessages = event.messages;
|
|
41
|
+
break;
|
|
42
|
+
case "TEXT_MESSAGE_START":
|
|
43
|
+
messagesById.set(event.messageId, {
|
|
44
|
+
id: event.messageId,
|
|
45
|
+
role: event.role ?? "assistant",
|
|
46
|
+
content: ""
|
|
47
|
+
});
|
|
48
|
+
break;
|
|
49
|
+
case "TEXT_MESSAGE_CONTENT": {
|
|
50
|
+
const msg = messagesById.get(event.messageId);
|
|
51
|
+
if (msg) msg.content = (msg.content ?? "") + (event.delta ?? "");
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case "TEXT_MESSAGE_CHUNK":
|
|
55
|
+
if (event.messageId) {
|
|
56
|
+
const existing = messagesById.get(event.messageId);
|
|
57
|
+
if (existing) existing.content = (existing.content ?? "") + (event.delta ?? "");
|
|
58
|
+
else messagesById.set(event.messageId, {
|
|
59
|
+
id: event.messageId,
|
|
60
|
+
role: event.role ?? "assistant",
|
|
61
|
+
content: event.delta ?? ""
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
case "TOOL_CALL_START": {
|
|
66
|
+
const tc = {
|
|
67
|
+
id: event.toolCallId,
|
|
68
|
+
name: event.toolCallName,
|
|
69
|
+
args: ""
|
|
70
|
+
};
|
|
71
|
+
toolCallsById.set(event.toolCallId, tc);
|
|
72
|
+
if (event.parentMessageId) toolCallParent.set(event.toolCallId, event.parentMessageId);
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case "TOOL_CALL_ARGS": {
|
|
76
|
+
const tc = toolCallsById.get(event.toolCallId);
|
|
77
|
+
if (tc) tc.args += event.delta ?? "";
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case "TOOL_CALL_CHUNK":
|
|
81
|
+
if (event.toolCallId) {
|
|
82
|
+
let tc = toolCallsById.get(event.toolCallId);
|
|
83
|
+
if (!tc) {
|
|
84
|
+
tc = {
|
|
85
|
+
id: event.toolCallId,
|
|
86
|
+
name: event.toolCallName ?? "",
|
|
87
|
+
args: ""
|
|
88
|
+
};
|
|
89
|
+
toolCallsById.set(event.toolCallId, tc);
|
|
90
|
+
if (event.parentMessageId) toolCallParent.set(event.toolCallId, event.parentMessageId);
|
|
91
|
+
}
|
|
92
|
+
if (event.toolCallName) tc.name = event.toolCallName;
|
|
93
|
+
tc.args += event.delta ?? "";
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
case "TOOL_CALL_END": {
|
|
97
|
+
const tc = toolCallsById.get(event.toolCallId);
|
|
98
|
+
const parentId = toolCallParent.get(event.toolCallId);
|
|
99
|
+
if (tc && parentId) {
|
|
100
|
+
const parent = messagesById.get(parentId);
|
|
101
|
+
if (parent) {
|
|
102
|
+
parent.toolCalls = parent.toolCalls ?? [];
|
|
103
|
+
parent.toolCalls.push(tc);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case "TOOL_CALL_RESULT":
|
|
109
|
+
messagesById.set(event.messageId, {
|
|
110
|
+
id: event.messageId,
|
|
111
|
+
role: "tool",
|
|
112
|
+
content: event.content,
|
|
113
|
+
toolCallId: event.toolCallId
|
|
114
|
+
});
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (const [toolCallId, tc] of toolCallsById) {
|
|
119
|
+
const parentId = toolCallParent.get(toolCallId);
|
|
120
|
+
if (!parentId) continue;
|
|
121
|
+
const parent = messagesById.get(parentId);
|
|
122
|
+
if (!parent) continue;
|
|
123
|
+
if (!parent.toolCalls?.some((t) => t.id === tc.id)) {
|
|
124
|
+
parent.toolCalls = parent.toolCalls ?? [];
|
|
125
|
+
parent.toolCalls.push(tc);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
messages: snapshotMessages ?? [...messagesById.values()],
|
|
130
|
+
threadId,
|
|
131
|
+
runId
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
//#endregion
|
|
136
|
+
exports.parseSSEResponse = parseSSEResponse;
|
|
137
|
+
//# sourceMappingURL=middleware-sse-parser.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware-sse-parser.cjs","names":[],"sources":["../src/middleware-sse-parser.ts"],"sourcesContent":["import { logger } from \"@copilotkitnext/shared\";\n\nexport interface ParsedSSEResult {\n messages: Message[];\n threadId?: string;\n runId?: string;\n}\n\n/** Minimal message shape reconstructed from AG-UI events. */\nexport interface Message {\n id: string;\n role: string;\n content?: string;\n toolCalls?: ToolCall[];\n toolCallId?: string;\n}\n\ninterface ToolCall {\n id: string;\n name: string;\n args: string;\n}\n\n/**\n * Parse a cloned SSE Response body into structured messages.\n * Returns empty results for non-SSE responses.\n */\nexport async function parseSSEResponse(\n response: Response,\n): Promise<ParsedSSEResult> {\n const contentType = response.headers.get(\"content-type\") ?? \"\";\n if (!contentType.includes(\"text/event-stream\")) {\n return { messages: [] };\n }\n\n let text: string;\n try {\n text = await response.text();\n } catch {\n logger.warn(\"Failed to read SSE response body in afterRequestMiddleware\");\n return { messages: [] };\n }\n\n if (!text.trim()) {\n return { messages: [] };\n }\n\n let threadId: string | undefined;\n let runId: string | undefined;\n const messagesById = new Map<string, Message>();\n const toolCallsById = new Map<string, ToolCall>();\n const toolCallParent = new Map<string, string>(); // toolCallId → messageId\n let snapshotMessages: Message[] | undefined;\n\n for (const line of text.split(\"\\n\")) {\n const trimmed = line.trim();\n if (!trimmed.startsWith(\"data:\")) continue;\n\n let event: Record<string, any>;\n try {\n event = JSON.parse(trimmed.slice(5).trim());\n } catch {\n continue;\n }\n\n switch (event.type) {\n case \"RUN_STARTED\":\n threadId = event.threadId;\n runId = event.runId;\n break;\n\n case \"MESSAGES_SNAPSHOT\":\n if (Array.isArray(event.messages)) {\n snapshotMessages = event.messages;\n }\n break;\n\n case \"TEXT_MESSAGE_START\":\n messagesById.set(event.messageId, {\n id: event.messageId,\n role: event.role ?? \"assistant\",\n content: \"\",\n });\n break;\n\n case \"TEXT_MESSAGE_CONTENT\": {\n const msg = messagesById.get(event.messageId);\n if (msg) {\n msg.content = (msg.content ?? \"\") + (event.delta ?? \"\");\n }\n break;\n }\n\n case \"TEXT_MESSAGE_CHUNK\": {\n // Chunk format: combined start+content. First chunk creates the\n // message, subsequent chunks append delta to content.\n if (event.messageId) {\n const existing = messagesById.get(event.messageId);\n if (existing) {\n existing.content = (existing.content ?? \"\") + (event.delta ?? \"\");\n } else {\n messagesById.set(event.messageId, {\n id: event.messageId,\n role: event.role ?? \"assistant\",\n content: event.delta ?? \"\",\n });\n }\n }\n break;\n }\n\n case \"TOOL_CALL_START\": {\n const tc: ToolCall = {\n id: event.toolCallId,\n name: event.toolCallName,\n args: \"\",\n };\n toolCallsById.set(event.toolCallId, tc);\n if (event.parentMessageId) {\n toolCallParent.set(event.toolCallId, event.parentMessageId);\n }\n break;\n }\n\n case \"TOOL_CALL_ARGS\": {\n const tc = toolCallsById.get(event.toolCallId);\n if (tc) {\n tc.args += event.delta ?? \"\";\n }\n break;\n }\n\n case \"TOOL_CALL_CHUNK\": {\n // Chunk format: combined start+args. First chunk for a given\n // toolCallId creates the tool call, subsequent chunks append delta.\n if (event.toolCallId) {\n let tc = toolCallsById.get(event.toolCallId);\n if (!tc) {\n tc = {\n id: event.toolCallId,\n name: event.toolCallName ?? \"\",\n args: \"\",\n };\n toolCallsById.set(event.toolCallId, tc);\n if (event.parentMessageId) {\n toolCallParent.set(event.toolCallId, event.parentMessageId);\n }\n }\n if (event.toolCallName) {\n tc.name = event.toolCallName;\n }\n tc.args += event.delta ?? \"\";\n }\n break;\n }\n\n case \"TOOL_CALL_END\": {\n const tc = toolCallsById.get(event.toolCallId);\n const parentId = toolCallParent.get(event.toolCallId);\n if (tc && parentId) {\n const parent = messagesById.get(parentId);\n if (parent) {\n parent.toolCalls = parent.toolCalls ?? [];\n parent.toolCalls.push(tc);\n }\n }\n break;\n }\n\n case \"TOOL_CALL_RESULT\":\n messagesById.set(event.messageId, {\n id: event.messageId,\n role: \"tool\",\n content: event.content,\n toolCallId: event.toolCallId,\n });\n break;\n }\n }\n\n // Attach any tool calls not yet linked to their parent message.\n // This handles TOOL_CALL_CHUNK flows which don't emit TOOL_CALL_END.\n for (const [toolCallId, tc] of toolCallsById) {\n const parentId = toolCallParent.get(toolCallId);\n if (!parentId) continue;\n const parent = messagesById.get(parentId);\n if (!parent) continue;\n const alreadyAttached = parent.toolCalls?.some((t) => t.id === tc.id);\n if (!alreadyAttached) {\n parent.toolCalls = parent.toolCalls ?? [];\n parent.toolCalls.push(tc);\n }\n }\n\n // Prefer MESSAGES_SNAPSHOT if present (contains full history).\n // Otherwise reconstruct from individual events.\n const messages = snapshotMessages ?? [...messagesById.values()];\n\n return { messages, threadId, runId };\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/middleware-sse-parser.d.ts
|
|
2
|
+
/** Minimal message shape reconstructed from AG-UI events. */
|
|
3
|
+
interface Message {
|
|
4
|
+
id: string;
|
|
5
|
+
role: string;
|
|
6
|
+
content?: string;
|
|
7
|
+
toolCalls?: ToolCall[];
|
|
8
|
+
toolCallId?: string;
|
|
9
|
+
}
|
|
10
|
+
interface ToolCall {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
args: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Parse a cloned SSE Response body into structured messages.
|
|
17
|
+
* Returns empty results for non-SSE responses.
|
|
18
|
+
*/
|
|
19
|
+
//#endregion
|
|
20
|
+
export { Message };
|
|
21
|
+
//# sourceMappingURL=middleware-sse-parser.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware-sse-parser.d.cts","names":[],"sources":["../src/middleware-sse-parser.ts"],"mappings":";;UASiB,OAAA;EACf,EAAA;EACA,IAAA;EACA,OAAA;EACA,SAAA,GAAY,QAAA;EACZ,UAAA;AAAA;AAAA,UAGQ,QAAA;EACR,EAAA;EACA,IAAA;EACA,IAAA;AAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/middleware-sse-parser.d.ts
|
|
2
|
+
/** Minimal message shape reconstructed from AG-UI events. */
|
|
3
|
+
interface Message {
|
|
4
|
+
id: string;
|
|
5
|
+
role: string;
|
|
6
|
+
content?: string;
|
|
7
|
+
toolCalls?: ToolCall[];
|
|
8
|
+
toolCallId?: string;
|
|
9
|
+
}
|
|
10
|
+
interface ToolCall {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
args: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Parse a cloned SSE Response body into structured messages.
|
|
17
|
+
* Returns empty results for non-SSE responses.
|
|
18
|
+
*/
|
|
19
|
+
//#endregion
|
|
20
|
+
export { Message };
|
|
21
|
+
//# sourceMappingURL=middleware-sse-parser.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware-sse-parser.d.mts","names":[],"sources":["../src/middleware-sse-parser.ts"],"mappings":";;UASiB,OAAA;EACf,EAAA;EACA,IAAA;EACA,OAAA;EACA,SAAA,GAAY,QAAA;EACZ,UAAA;AAAA;AAAA,UAGQ,QAAA;EACR,EAAA;EACA,IAAA;EACA,IAAA;AAAA"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { logger } from "@copilotkitnext/shared";
|
|
2
|
+
|
|
3
|
+
//#region src/middleware-sse-parser.ts
|
|
4
|
+
/**
|
|
5
|
+
* Parse a cloned SSE Response body into structured messages.
|
|
6
|
+
* Returns empty results for non-SSE responses.
|
|
7
|
+
*/
|
|
8
|
+
async function parseSSEResponse(response) {
|
|
9
|
+
if (!(response.headers.get("content-type") ?? "").includes("text/event-stream")) return { messages: [] };
|
|
10
|
+
let text;
|
|
11
|
+
try {
|
|
12
|
+
text = await response.text();
|
|
13
|
+
} catch {
|
|
14
|
+
logger.warn("Failed to read SSE response body in afterRequestMiddleware");
|
|
15
|
+
return { messages: [] };
|
|
16
|
+
}
|
|
17
|
+
if (!text.trim()) return { messages: [] };
|
|
18
|
+
let threadId;
|
|
19
|
+
let runId;
|
|
20
|
+
const messagesById = /* @__PURE__ */ new Map();
|
|
21
|
+
const toolCallsById = /* @__PURE__ */ new Map();
|
|
22
|
+
const toolCallParent = /* @__PURE__ */ new Map();
|
|
23
|
+
let snapshotMessages;
|
|
24
|
+
for (const line of text.split("\n")) {
|
|
25
|
+
const trimmed = line.trim();
|
|
26
|
+
if (!trimmed.startsWith("data:")) continue;
|
|
27
|
+
let event;
|
|
28
|
+
try {
|
|
29
|
+
event = JSON.parse(trimmed.slice(5).trim());
|
|
30
|
+
} catch {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
switch (event.type) {
|
|
34
|
+
case "RUN_STARTED":
|
|
35
|
+
threadId = event.threadId;
|
|
36
|
+
runId = event.runId;
|
|
37
|
+
break;
|
|
38
|
+
case "MESSAGES_SNAPSHOT":
|
|
39
|
+
if (Array.isArray(event.messages)) snapshotMessages = event.messages;
|
|
40
|
+
break;
|
|
41
|
+
case "TEXT_MESSAGE_START":
|
|
42
|
+
messagesById.set(event.messageId, {
|
|
43
|
+
id: event.messageId,
|
|
44
|
+
role: event.role ?? "assistant",
|
|
45
|
+
content: ""
|
|
46
|
+
});
|
|
47
|
+
break;
|
|
48
|
+
case "TEXT_MESSAGE_CONTENT": {
|
|
49
|
+
const msg = messagesById.get(event.messageId);
|
|
50
|
+
if (msg) msg.content = (msg.content ?? "") + (event.delta ?? "");
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case "TEXT_MESSAGE_CHUNK":
|
|
54
|
+
if (event.messageId) {
|
|
55
|
+
const existing = messagesById.get(event.messageId);
|
|
56
|
+
if (existing) existing.content = (existing.content ?? "") + (event.delta ?? "");
|
|
57
|
+
else messagesById.set(event.messageId, {
|
|
58
|
+
id: event.messageId,
|
|
59
|
+
role: event.role ?? "assistant",
|
|
60
|
+
content: event.delta ?? ""
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
case "TOOL_CALL_START": {
|
|
65
|
+
const tc = {
|
|
66
|
+
id: event.toolCallId,
|
|
67
|
+
name: event.toolCallName,
|
|
68
|
+
args: ""
|
|
69
|
+
};
|
|
70
|
+
toolCallsById.set(event.toolCallId, tc);
|
|
71
|
+
if (event.parentMessageId) toolCallParent.set(event.toolCallId, event.parentMessageId);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case "TOOL_CALL_ARGS": {
|
|
75
|
+
const tc = toolCallsById.get(event.toolCallId);
|
|
76
|
+
if (tc) tc.args += event.delta ?? "";
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case "TOOL_CALL_CHUNK":
|
|
80
|
+
if (event.toolCallId) {
|
|
81
|
+
let tc = toolCallsById.get(event.toolCallId);
|
|
82
|
+
if (!tc) {
|
|
83
|
+
tc = {
|
|
84
|
+
id: event.toolCallId,
|
|
85
|
+
name: event.toolCallName ?? "",
|
|
86
|
+
args: ""
|
|
87
|
+
};
|
|
88
|
+
toolCallsById.set(event.toolCallId, tc);
|
|
89
|
+
if (event.parentMessageId) toolCallParent.set(event.toolCallId, event.parentMessageId);
|
|
90
|
+
}
|
|
91
|
+
if (event.toolCallName) tc.name = event.toolCallName;
|
|
92
|
+
tc.args += event.delta ?? "";
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
case "TOOL_CALL_END": {
|
|
96
|
+
const tc = toolCallsById.get(event.toolCallId);
|
|
97
|
+
const parentId = toolCallParent.get(event.toolCallId);
|
|
98
|
+
if (tc && parentId) {
|
|
99
|
+
const parent = messagesById.get(parentId);
|
|
100
|
+
if (parent) {
|
|
101
|
+
parent.toolCalls = parent.toolCalls ?? [];
|
|
102
|
+
parent.toolCalls.push(tc);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
case "TOOL_CALL_RESULT":
|
|
108
|
+
messagesById.set(event.messageId, {
|
|
109
|
+
id: event.messageId,
|
|
110
|
+
role: "tool",
|
|
111
|
+
content: event.content,
|
|
112
|
+
toolCallId: event.toolCallId
|
|
113
|
+
});
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const [toolCallId, tc] of toolCallsById) {
|
|
118
|
+
const parentId = toolCallParent.get(toolCallId);
|
|
119
|
+
if (!parentId) continue;
|
|
120
|
+
const parent = messagesById.get(parentId);
|
|
121
|
+
if (!parent) continue;
|
|
122
|
+
if (!parent.toolCalls?.some((t) => t.id === tc.id)) {
|
|
123
|
+
parent.toolCalls = parent.toolCalls ?? [];
|
|
124
|
+
parent.toolCalls.push(tc);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
messages: snapshotMessages ?? [...messagesById.values()],
|
|
129
|
+
threadId,
|
|
130
|
+
runId
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
//#endregion
|
|
135
|
+
export { parseSSEResponse };
|
|
136
|
+
//# sourceMappingURL=middleware-sse-parser.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware-sse-parser.mjs","names":[],"sources":["../src/middleware-sse-parser.ts"],"sourcesContent":["import { logger } from \"@copilotkitnext/shared\";\n\nexport interface ParsedSSEResult {\n messages: Message[];\n threadId?: string;\n runId?: string;\n}\n\n/** Minimal message shape reconstructed from AG-UI events. */\nexport interface Message {\n id: string;\n role: string;\n content?: string;\n toolCalls?: ToolCall[];\n toolCallId?: string;\n}\n\ninterface ToolCall {\n id: string;\n name: string;\n args: string;\n}\n\n/**\n * Parse a cloned SSE Response body into structured messages.\n * Returns empty results for non-SSE responses.\n */\nexport async function parseSSEResponse(\n response: Response,\n): Promise<ParsedSSEResult> {\n const contentType = response.headers.get(\"content-type\") ?? \"\";\n if (!contentType.includes(\"text/event-stream\")) {\n return { messages: [] };\n }\n\n let text: string;\n try {\n text = await response.text();\n } catch {\n logger.warn(\"Failed to read SSE response body in afterRequestMiddleware\");\n return { messages: [] };\n }\n\n if (!text.trim()) {\n return { messages: [] };\n }\n\n let threadId: string | undefined;\n let runId: string | undefined;\n const messagesById = new Map<string, Message>();\n const toolCallsById = new Map<string, ToolCall>();\n const toolCallParent = new Map<string, string>(); // toolCallId → messageId\n let snapshotMessages: Message[] | undefined;\n\n for (const line of text.split(\"\\n\")) {\n const trimmed = line.trim();\n if (!trimmed.startsWith(\"data:\")) continue;\n\n let event: Record<string, any>;\n try {\n event = JSON.parse(trimmed.slice(5).trim());\n } catch {\n continue;\n }\n\n switch (event.type) {\n case \"RUN_STARTED\":\n threadId = event.threadId;\n runId = event.runId;\n break;\n\n case \"MESSAGES_SNAPSHOT\":\n if (Array.isArray(event.messages)) {\n snapshotMessages = event.messages;\n }\n break;\n\n case \"TEXT_MESSAGE_START\":\n messagesById.set(event.messageId, {\n id: event.messageId,\n role: event.role ?? \"assistant\",\n content: \"\",\n });\n break;\n\n case \"TEXT_MESSAGE_CONTENT\": {\n const msg = messagesById.get(event.messageId);\n if (msg) {\n msg.content = (msg.content ?? \"\") + (event.delta ?? \"\");\n }\n break;\n }\n\n case \"TEXT_MESSAGE_CHUNK\": {\n // Chunk format: combined start+content. First chunk creates the\n // message, subsequent chunks append delta to content.\n if (event.messageId) {\n const existing = messagesById.get(event.messageId);\n if (existing) {\n existing.content = (existing.content ?? \"\") + (event.delta ?? \"\");\n } else {\n messagesById.set(event.messageId, {\n id: event.messageId,\n role: event.role ?? \"assistant\",\n content: event.delta ?? \"\",\n });\n }\n }\n break;\n }\n\n case \"TOOL_CALL_START\": {\n const tc: ToolCall = {\n id: event.toolCallId,\n name: event.toolCallName,\n args: \"\",\n };\n toolCallsById.set(event.toolCallId, tc);\n if (event.parentMessageId) {\n toolCallParent.set(event.toolCallId, event.parentMessageId);\n }\n break;\n }\n\n case \"TOOL_CALL_ARGS\": {\n const tc = toolCallsById.get(event.toolCallId);\n if (tc) {\n tc.args += event.delta ?? \"\";\n }\n break;\n }\n\n case \"TOOL_CALL_CHUNK\": {\n // Chunk format: combined start+args. First chunk for a given\n // toolCallId creates the tool call, subsequent chunks append delta.\n if (event.toolCallId) {\n let tc = toolCallsById.get(event.toolCallId);\n if (!tc) {\n tc = {\n id: event.toolCallId,\n name: event.toolCallName ?? \"\",\n args: \"\",\n };\n toolCallsById.set(event.toolCallId, tc);\n if (event.parentMessageId) {\n toolCallParent.set(event.toolCallId, event.parentMessageId);\n }\n }\n if (event.toolCallName) {\n tc.name = event.toolCallName;\n }\n tc.args += event.delta ?? \"\";\n }\n break;\n }\n\n case \"TOOL_CALL_END\": {\n const tc = toolCallsById.get(event.toolCallId);\n const parentId = toolCallParent.get(event.toolCallId);\n if (tc && parentId) {\n const parent = messagesById.get(parentId);\n if (parent) {\n parent.toolCalls = parent.toolCalls ?? [];\n parent.toolCalls.push(tc);\n }\n }\n break;\n }\n\n case \"TOOL_CALL_RESULT\":\n messagesById.set(event.messageId, {\n id: event.messageId,\n role: \"tool\",\n content: event.content,\n toolCallId: event.toolCallId,\n });\n break;\n }\n }\n\n // Attach any tool calls not yet linked to their parent message.\n // This handles TOOL_CALL_CHUNK flows which don't emit TOOL_CALL_END.\n for (const [toolCallId, tc] of toolCallsById) {\n const parentId = toolCallParent.get(toolCallId);\n if (!parentId) continue;\n const parent = messagesById.get(parentId);\n if (!parent) continue;\n const alreadyAttached = parent.toolCalls?.some((t) => t.id === tc.id);\n if (!alreadyAttached) {\n parent.toolCalls = parent.toolCalls ?? [];\n parent.toolCalls.push(tc);\n }\n }\n\n // Prefer MESSAGES_SNAPSHOT if present (contains full history).\n // Otherwise reconstruct from individual events.\n const messages = snapshotMessages ?? [...messagesById.values()];\n\n return { messages, threadId, runId };\n}\n"],"mappings":"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"}
|
package/dist/middleware.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
const require_middleware_sse_parser = require('./middleware-sse-parser.cjs');
|
|
2
3
|
let _copilotkitnext_shared = require("@copilotkitnext/shared");
|
|
3
4
|
|
|
4
5
|
//#region src/middleware.ts
|
|
@@ -15,10 +16,14 @@ async function callBeforeRequestMiddleware({ runtime, request, path }) {
|
|
|
15
16
|
async function callAfterRequestMiddleware({ runtime, response, path }) {
|
|
16
17
|
const mw = runtime.afterRequestMiddleware;
|
|
17
18
|
if (!mw) return;
|
|
19
|
+
const { messages, threadId, runId } = await require_middleware_sse_parser.parseSSEResponse(response);
|
|
18
20
|
if (typeof mw === "function") return mw({
|
|
19
21
|
runtime,
|
|
20
22
|
response,
|
|
21
|
-
path
|
|
23
|
+
path,
|
|
24
|
+
messages,
|
|
25
|
+
threadId,
|
|
26
|
+
runId
|
|
22
27
|
});
|
|
23
28
|
_copilotkitnext_shared.logger.warn({ mw }, "Unsupported afterRequestMiddleware value – skipped");
|
|
24
29
|
}
|
package/dist/middleware.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.cjs","names":[],"sources":["../src/middleware.ts"],"sourcesContent":["/**\n * Middleware support for CopilotKit Runtime.\n *\n * A middleware hook can be provided as either:\n * 1. A **callback function** executed in-process.\n * 2. A **webhook URL** (http/https). The runtime will `POST` a JSON payload\n * to the URL and, for *before* hooks, accept an optional modified\n * `Request` object in the response body.\n *\n * Two lifecycle hooks are available:\n * • `BEFORE_REQUEST` – runs *before* the request handler.\n * • `AFTER_REQUEST` – runs *after* the handler returns a `Response`.\n */\n\nimport type { CopilotRuntime } from \"./runtime\";\nimport type { MaybePromise } from \"@copilotkitnext/shared\";\nimport { logger } from \"@copilotkitnext/shared\";\n\n/* ------------------------------------------------------------------------------------------------\n * Public types\n * --------------------------------------------------------------------------------------------- */\n\nexport interface BeforeRequestMiddlewareParameters {\n runtime: CopilotRuntime;\n request: Request;\n path: string;\n}\nexport interface AfterRequestMiddlewareParameters {\n runtime: CopilotRuntime;\n response: Response;\n path: string;\n}\n\nexport type BeforeRequestMiddlewareFn = (\n params: BeforeRequestMiddlewareParameters,\n) => MaybePromise<Request | void>;\nexport type AfterRequestMiddlewareFn = (\n params: AfterRequestMiddlewareParameters,\n) => MaybePromise<void>;\n\n/**\n * A middleware value can be either a callback function or a webhook URL.\n */\nexport type BeforeRequestMiddleware = BeforeRequestMiddlewareFn;\nexport type AfterRequestMiddleware = AfterRequestMiddlewareFn;\n\n/** Lifecycle events emitted to webhook middleware. */\nexport enum CopilotKitMiddlewareEvent {\n BeforeRequest = \"BEFORE_REQUEST\",\n AfterRequest = \"AFTER_REQUEST\",\n}\n\n/** Stages used by the Middleware Webhook Protocol */\n/** Stages used by the CopilotKit webhook protocol */\nexport enum WebhookStage {\n BeforeRequest = \"before_request\",\n AfterRequest = \"after_request\",\n}\n\n/* ------------------------------------------------------------------------------------------------\n * Internal helpers – (de)serialisation\n * --------------------------------------------------------------------------------------------- */\n\nexport async function callBeforeRequestMiddleware({\n runtime,\n request,\n path,\n}: BeforeRequestMiddlewareParameters): Promise<Request | void> {\n const mw = runtime.beforeRequestMiddleware;\n if (!mw) return;\n\n // Function-based middleware (in-process)\n if (typeof mw === \"function\") {\n return (mw as BeforeRequestMiddlewareFn)({ runtime, request, path });\n }\n\n logger.warn({ mw }, \"Unsupported beforeRequestMiddleware value – skipped\");\n return;\n}\n\nexport async function callAfterRequestMiddleware({\n runtime,\n response,\n path,\n}:
|
|
1
|
+
{"version":3,"file":"middleware.cjs","names":["parseSSEResponse"],"sources":["../src/middleware.ts"],"sourcesContent":["/**\n * Middleware support for CopilotKit Runtime.\n *\n * A middleware hook can be provided as either:\n * 1. A **callback function** executed in-process.\n * 2. A **webhook URL** (http/https). The runtime will `POST` a JSON payload\n * to the URL and, for *before* hooks, accept an optional modified\n * `Request` object in the response body.\n *\n * Two lifecycle hooks are available:\n * • `BEFORE_REQUEST` – runs *before* the request handler.\n * • `AFTER_REQUEST` – runs *after* the handler returns a `Response`.\n */\n\nimport type { CopilotRuntime } from \"./runtime\";\nimport type { MaybePromise } from \"@copilotkitnext/shared\";\nimport { logger } from \"@copilotkitnext/shared\";\nimport { parseSSEResponse } from \"./middleware-sse-parser\";\nimport type { Message } from \"./middleware-sse-parser\";\n\n/* ------------------------------------------------------------------------------------------------\n * Public types\n * --------------------------------------------------------------------------------------------- */\n\nexport interface BeforeRequestMiddlewareParameters {\n runtime: CopilotRuntime;\n request: Request;\n path: string;\n}\nexport interface AfterRequestMiddlewareParameters {\n runtime: CopilotRuntime;\n response: Response;\n path: string;\n /** Reconstructed messages from the SSE stream (empty for non-SSE responses). */\n messages?: Message[];\n /** Thread ID extracted from the RUN_STARTED event. */\n threadId?: string;\n /** Run ID extracted from the RUN_STARTED event. */\n runId?: string;\n}\n\nexport type BeforeRequestMiddlewareFn = (\n params: BeforeRequestMiddlewareParameters,\n) => MaybePromise<Request | void>;\nexport type AfterRequestMiddlewareFn = (\n params: AfterRequestMiddlewareParameters,\n) => MaybePromise<void>;\n\n/**\n * A middleware value can be either a callback function or a webhook URL.\n */\nexport type BeforeRequestMiddleware = BeforeRequestMiddlewareFn;\nexport type AfterRequestMiddleware = AfterRequestMiddlewareFn;\n\n/** Lifecycle events emitted to webhook middleware. */\nexport enum CopilotKitMiddlewareEvent {\n BeforeRequest = \"BEFORE_REQUEST\",\n AfterRequest = \"AFTER_REQUEST\",\n}\n\n/** Stages used by the Middleware Webhook Protocol */\n/** Stages used by the CopilotKit webhook protocol */\nexport enum WebhookStage {\n BeforeRequest = \"before_request\",\n AfterRequest = \"after_request\",\n}\n\n/* ------------------------------------------------------------------------------------------------\n * Internal helpers – (de)serialisation\n * --------------------------------------------------------------------------------------------- */\n\nexport async function callBeforeRequestMiddleware({\n runtime,\n request,\n path,\n}: BeforeRequestMiddlewareParameters): Promise<Request | void> {\n const mw = runtime.beforeRequestMiddleware;\n if (!mw) return;\n\n // Function-based middleware (in-process)\n if (typeof mw === \"function\") {\n return (mw as BeforeRequestMiddlewareFn)({ runtime, request, path });\n }\n\n logger.warn({ mw }, \"Unsupported beforeRequestMiddleware value – skipped\");\n return;\n}\n\nexport async function callAfterRequestMiddleware({\n runtime,\n response,\n path,\n}: {\n runtime: CopilotRuntime;\n response: Response;\n path: string;\n}): Promise<void> {\n const mw = runtime.afterRequestMiddleware;\n if (!mw) return;\n\n const { messages, threadId, runId } = await parseSSEResponse(response);\n\n if (typeof mw === \"function\") {\n return (mw as AfterRequestMiddlewareFn)({\n runtime,\n response,\n path,\n messages,\n threadId,\n runId,\n });\n }\n\n logger.warn({ mw }, \"Unsupported afterRequestMiddleware value – skipped\");\n}\n"],"mappings":";;;;;AAuEA,eAAsB,4BAA4B,EAChD,SACA,SACA,QAC6D;CAC7D,MAAM,KAAK,QAAQ;AACnB,KAAI,CAAC,GAAI;AAGT,KAAI,OAAO,OAAO,WAChB,QAAQ,GAAiC;EAAE;EAAS;EAAS;EAAM,CAAC;AAGtE,+BAAO,KAAK,EAAE,IAAI,EAAE,sDAAsD;;AAI5E,eAAsB,2BAA2B,EAC/C,SACA,UACA,QAKgB;CAChB,MAAM,KAAK,QAAQ;AACnB,KAAI,CAAC,GAAI;CAET,MAAM,EAAE,UAAU,UAAU,UAAU,MAAMA,+CAAiB,SAAS;AAEtE,KAAI,OAAO,OAAO,WAChB,QAAQ,GAAgC;EACtC;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAGJ,+BAAO,KAAK,EAAE,IAAI,EAAE,qDAAqD"}
|
package/dist/middleware.d.cts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Message } from "./middleware-sse-parser.cjs";
|
|
1
2
|
import { CopilotRuntime } from "./runtime.cjs";
|
|
2
3
|
import { MaybePromise } from "@copilotkitnext/shared";
|
|
3
4
|
|
|
@@ -11,6 +12,12 @@ interface AfterRequestMiddlewareParameters {
|
|
|
11
12
|
runtime: CopilotRuntime;
|
|
12
13
|
response: Response;
|
|
13
14
|
path: string;
|
|
15
|
+
/** Reconstructed messages from the SSE stream (empty for non-SSE responses). */
|
|
16
|
+
messages?: Message[];
|
|
17
|
+
/** Thread ID extracted from the RUN_STARTED event. */
|
|
18
|
+
threadId?: string;
|
|
19
|
+
/** Run ID extracted from the RUN_STARTED event. */
|
|
20
|
+
runId?: string;
|
|
14
21
|
}
|
|
15
22
|
type BeforeRequestMiddlewareFn = (params: BeforeRequestMiddlewareParameters) => MaybePromise<Request | void>;
|
|
16
23
|
type AfterRequestMiddlewareFn = (params: AfterRequestMiddlewareParameters) => MaybePromise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.cts","names":[],"sources":["../src/middleware.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"middleware.d.cts","names":[],"sources":["../src/middleware.ts"],"mappings":";;;;;UAwBiB,iCAAA;EACf,OAAA,EAAS,cAAA;EACT,OAAA,EAAS,OAAA;EACT,IAAA;AAAA;AAAA,UAEe,gCAAA;EACf,OAAA,EAAS,cAAA;EACT,QAAA,EAAU,QAAA;EACV,IAAA;EAFS;EAIT,QAAA,GAAW,OAAA;EAHD;EAKV,QAAA;EAFA;EAIA,KAAA;AAAA;AAAA,KAGU,yBAAA,IACV,MAAA,EAAQ,iCAAA,KACL,YAAA,CAAa,OAAA;AAAA,KACN,wBAAA,IACV,MAAA,EAAQ,gCAAA,KACL,YAAA;;AALL;;KAUY,uBAAA,GAA0B,yBAAA;AAAA,KAC1B,sBAAA,GAAyB,wBAAA"}
|
package/dist/middleware.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Message } from "./middleware-sse-parser.mjs";
|
|
1
2
|
import { CopilotRuntime } from "./runtime.mjs";
|
|
2
3
|
import { MaybePromise } from "@copilotkitnext/shared";
|
|
3
4
|
|
|
@@ -11,6 +12,12 @@ interface AfterRequestMiddlewareParameters {
|
|
|
11
12
|
runtime: CopilotRuntime;
|
|
12
13
|
response: Response;
|
|
13
14
|
path: string;
|
|
15
|
+
/** Reconstructed messages from the SSE stream (empty for non-SSE responses). */
|
|
16
|
+
messages?: Message[];
|
|
17
|
+
/** Thread ID extracted from the RUN_STARTED event. */
|
|
18
|
+
threadId?: string;
|
|
19
|
+
/** Run ID extracted from the RUN_STARTED event. */
|
|
20
|
+
runId?: string;
|
|
14
21
|
}
|
|
15
22
|
type BeforeRequestMiddlewareFn = (params: BeforeRequestMiddlewareParameters) => MaybePromise<Request | void>;
|
|
16
23
|
type AfterRequestMiddlewareFn = (params: AfterRequestMiddlewareParameters) => MaybePromise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.mts","names":[],"sources":["../src/middleware.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"middleware.d.mts","names":[],"sources":["../src/middleware.ts"],"mappings":";;;;;UAwBiB,iCAAA;EACf,OAAA,EAAS,cAAA;EACT,OAAA,EAAS,OAAA;EACT,IAAA;AAAA;AAAA,UAEe,gCAAA;EACf,OAAA,EAAS,cAAA;EACT,QAAA,EAAU,QAAA;EACV,IAAA;EAFS;EAIT,QAAA,GAAW,OAAA;EAHD;EAKV,QAAA;EAFA;EAIA,KAAA;AAAA;AAAA,KAGU,yBAAA,IACV,MAAA,EAAQ,iCAAA,KACL,YAAA,CAAa,OAAA;AAAA,KACN,wBAAA,IACV,MAAA,EAAQ,gCAAA,KACL,YAAA;;AALL;;KAUY,uBAAA,GAA0B,yBAAA;AAAA,KAC1B,sBAAA,GAAyB,wBAAA"}
|
package/dist/middleware.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseSSEResponse } from "./middleware-sse-parser.mjs";
|
|
1
2
|
import { logger } from "@copilotkitnext/shared";
|
|
2
3
|
|
|
3
4
|
//#region src/middleware.ts
|
|
@@ -14,10 +15,14 @@ async function callBeforeRequestMiddleware({ runtime, request, path }) {
|
|
|
14
15
|
async function callAfterRequestMiddleware({ runtime, response, path }) {
|
|
15
16
|
const mw = runtime.afterRequestMiddleware;
|
|
16
17
|
if (!mw) return;
|
|
18
|
+
const { messages, threadId, runId } = await parseSSEResponse(response);
|
|
17
19
|
if (typeof mw === "function") return mw({
|
|
18
20
|
runtime,
|
|
19
21
|
response,
|
|
20
|
-
path
|
|
22
|
+
path,
|
|
23
|
+
messages,
|
|
24
|
+
threadId,
|
|
25
|
+
runId
|
|
21
26
|
});
|
|
22
27
|
logger.warn({ mw }, "Unsupported afterRequestMiddleware value – skipped");
|
|
23
28
|
}
|
package/dist/middleware.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.mjs","names":[],"sources":["../src/middleware.ts"],"sourcesContent":["/**\n * Middleware support for CopilotKit Runtime.\n *\n * A middleware hook can be provided as either:\n * 1. A **callback function** executed in-process.\n * 2. A **webhook URL** (http/https). The runtime will `POST` a JSON payload\n * to the URL and, for *before* hooks, accept an optional modified\n * `Request` object in the response body.\n *\n * Two lifecycle hooks are available:\n * • `BEFORE_REQUEST` – runs *before* the request handler.\n * • `AFTER_REQUEST` – runs *after* the handler returns a `Response`.\n */\n\nimport type { CopilotRuntime } from \"./runtime\";\nimport type { MaybePromise } from \"@copilotkitnext/shared\";\nimport { logger } from \"@copilotkitnext/shared\";\n\n/* ------------------------------------------------------------------------------------------------\n * Public types\n * --------------------------------------------------------------------------------------------- */\n\nexport interface BeforeRequestMiddlewareParameters {\n runtime: CopilotRuntime;\n request: Request;\n path: string;\n}\nexport interface AfterRequestMiddlewareParameters {\n runtime: CopilotRuntime;\n response: Response;\n path: string;\n}\n\nexport type BeforeRequestMiddlewareFn = (\n params: BeforeRequestMiddlewareParameters,\n) => MaybePromise<Request | void>;\nexport type AfterRequestMiddlewareFn = (\n params: AfterRequestMiddlewareParameters,\n) => MaybePromise<void>;\n\n/**\n * A middleware value can be either a callback function or a webhook URL.\n */\nexport type BeforeRequestMiddleware = BeforeRequestMiddlewareFn;\nexport type AfterRequestMiddleware = AfterRequestMiddlewareFn;\n\n/** Lifecycle events emitted to webhook middleware. */\nexport enum CopilotKitMiddlewareEvent {\n BeforeRequest = \"BEFORE_REQUEST\",\n AfterRequest = \"AFTER_REQUEST\",\n}\n\n/** Stages used by the Middleware Webhook Protocol */\n/** Stages used by the CopilotKit webhook protocol */\nexport enum WebhookStage {\n BeforeRequest = \"before_request\",\n AfterRequest = \"after_request\",\n}\n\n/* ------------------------------------------------------------------------------------------------\n * Internal helpers – (de)serialisation\n * --------------------------------------------------------------------------------------------- */\n\nexport async function callBeforeRequestMiddleware({\n runtime,\n request,\n path,\n}: BeforeRequestMiddlewareParameters): Promise<Request | void> {\n const mw = runtime.beforeRequestMiddleware;\n if (!mw) return;\n\n // Function-based middleware (in-process)\n if (typeof mw === \"function\") {\n return (mw as BeforeRequestMiddlewareFn)({ runtime, request, path });\n }\n\n logger.warn({ mw }, \"Unsupported beforeRequestMiddleware value – skipped\");\n return;\n}\n\nexport async function callAfterRequestMiddleware({\n runtime,\n response,\n path,\n}:
|
|
1
|
+
{"version":3,"file":"middleware.mjs","names":[],"sources":["../src/middleware.ts"],"sourcesContent":["/**\n * Middleware support for CopilotKit Runtime.\n *\n * A middleware hook can be provided as either:\n * 1. A **callback function** executed in-process.\n * 2. A **webhook URL** (http/https). The runtime will `POST` a JSON payload\n * to the URL and, for *before* hooks, accept an optional modified\n * `Request` object in the response body.\n *\n * Two lifecycle hooks are available:\n * • `BEFORE_REQUEST` – runs *before* the request handler.\n * • `AFTER_REQUEST` – runs *after* the handler returns a `Response`.\n */\n\nimport type { CopilotRuntime } from \"./runtime\";\nimport type { MaybePromise } from \"@copilotkitnext/shared\";\nimport { logger } from \"@copilotkitnext/shared\";\nimport { parseSSEResponse } from \"./middleware-sse-parser\";\nimport type { Message } from \"./middleware-sse-parser\";\n\n/* ------------------------------------------------------------------------------------------------\n * Public types\n * --------------------------------------------------------------------------------------------- */\n\nexport interface BeforeRequestMiddlewareParameters {\n runtime: CopilotRuntime;\n request: Request;\n path: string;\n}\nexport interface AfterRequestMiddlewareParameters {\n runtime: CopilotRuntime;\n response: Response;\n path: string;\n /** Reconstructed messages from the SSE stream (empty for non-SSE responses). */\n messages?: Message[];\n /** Thread ID extracted from the RUN_STARTED event. */\n threadId?: string;\n /** Run ID extracted from the RUN_STARTED event. */\n runId?: string;\n}\n\nexport type BeforeRequestMiddlewareFn = (\n params: BeforeRequestMiddlewareParameters,\n) => MaybePromise<Request | void>;\nexport type AfterRequestMiddlewareFn = (\n params: AfterRequestMiddlewareParameters,\n) => MaybePromise<void>;\n\n/**\n * A middleware value can be either a callback function or a webhook URL.\n */\nexport type BeforeRequestMiddleware = BeforeRequestMiddlewareFn;\nexport type AfterRequestMiddleware = AfterRequestMiddlewareFn;\n\n/** Lifecycle events emitted to webhook middleware. */\nexport enum CopilotKitMiddlewareEvent {\n BeforeRequest = \"BEFORE_REQUEST\",\n AfterRequest = \"AFTER_REQUEST\",\n}\n\n/** Stages used by the Middleware Webhook Protocol */\n/** Stages used by the CopilotKit webhook protocol */\nexport enum WebhookStage {\n BeforeRequest = \"before_request\",\n AfterRequest = \"after_request\",\n}\n\n/* ------------------------------------------------------------------------------------------------\n * Internal helpers – (de)serialisation\n * --------------------------------------------------------------------------------------------- */\n\nexport async function callBeforeRequestMiddleware({\n runtime,\n request,\n path,\n}: BeforeRequestMiddlewareParameters): Promise<Request | void> {\n const mw = runtime.beforeRequestMiddleware;\n if (!mw) return;\n\n // Function-based middleware (in-process)\n if (typeof mw === \"function\") {\n return (mw as BeforeRequestMiddlewareFn)({ runtime, request, path });\n }\n\n logger.warn({ mw }, \"Unsupported beforeRequestMiddleware value – skipped\");\n return;\n}\n\nexport async function callAfterRequestMiddleware({\n runtime,\n response,\n path,\n}: {\n runtime: CopilotRuntime;\n response: Response;\n path: string;\n}): Promise<void> {\n const mw = runtime.afterRequestMiddleware;\n if (!mw) return;\n\n const { messages, threadId, runId } = await parseSSEResponse(response);\n\n if (typeof mw === \"function\") {\n return (mw as AfterRequestMiddlewareFn)({\n runtime,\n response,\n path,\n messages,\n threadId,\n runId,\n });\n }\n\n logger.warn({ mw }, \"Unsupported afterRequestMiddleware value – skipped\");\n}\n"],"mappings":";;;;AAuEA,eAAsB,4BAA4B,EAChD,SACA,SACA,QAC6D;CAC7D,MAAM,KAAK,QAAQ;AACnB,KAAI,CAAC,GAAI;AAGT,KAAI,OAAO,OAAO,WAChB,QAAQ,GAAiC;EAAE;EAAS;EAAS;EAAM,CAAC;AAGtE,QAAO,KAAK,EAAE,IAAI,EAAE,sDAAsD;;AAI5E,eAAsB,2BAA2B,EAC/C,SACA,UACA,QAKgB;CAChB,MAAM,KAAK,QAAQ;AACnB,KAAI,CAAC,GAAI;CAET,MAAM,EAAE,UAAU,UAAU,UAAU,MAAM,iBAAiB,SAAS;AAEtE,KAAI,OAAO,OAAO,WAChB,QAAQ,GAAgC;EACtC;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;AAGJ,QAAO,KAAK,EAAE,IAAI,EAAE,qDAAqD"}
|
package/dist/package.cjs
CHANGED
package/dist/package.mjs
CHANGED
package/dist/runner/index.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
2
2
|
const require_agent_runner = require('./agent-runner.cjs');
|
|
3
3
|
const require_in_memory = require('./in-memory.cjs');
|
|
4
|
+
const require_intelligence = require('./intelligence.cjs');
|
|
4
5
|
let _copilotkitnext_shared = require("@copilotkitnext/shared");
|
package/dist/runner/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest } from "./agent-runner.cjs";
|
|
2
2
|
import { InMemoryAgentRunner } from "./in-memory.cjs";
|
|
3
|
+
import { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions } from "./intelligence.cjs";
|
|
3
4
|
import { finalizeRunEvents } from "@copilotkitnext/shared";
|
|
4
5
|
export { finalizeRunEvents };
|
package/dist/runner/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AgentRunner, AgentRunnerConnectRequest, AgentRunnerIsRunningRequest, AgentRunnerRunRequest, AgentRunnerStopRequest } from "./agent-runner.mjs";
|
|
2
2
|
import { InMemoryAgentRunner } from "./in-memory.mjs";
|
|
3
|
+
import { IntelligenceAgentRunner, IntelligenceAgentRunnerOptions } from "./intelligence.mjs";
|
|
3
4
|
import { finalizeRunEvents as finalizeRunEvents$1 } from "@copilotkitnext/shared";
|
|
4
5
|
export { finalizeRunEvents$1 as finalizeRunEvents };
|
package/dist/runner/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AgentRunner } from "./agent-runner.mjs";
|
|
2
2
|
import { InMemoryAgentRunner } from "./in-memory.mjs";
|
|
3
|
+
import { IntelligenceAgentRunner } from "./intelligence.mjs";
|
|
3
4
|
import { finalizeRunEvents as finalizeRunEvents$1 } from "@copilotkitnext/shared";
|
|
4
5
|
|
|
5
6
|
export { finalizeRunEvents$1 as finalizeRunEvents };
|