@copilotkitnext/runtime 1.51.4 → 1.51.5-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/endpoints/express-single.cjs +187 -0
- package/dist/endpoints/express-single.cjs.map +1 -0
- package/dist/endpoints/express-single.d.cts +15 -0
- package/dist/endpoints/express-single.d.cts.map +1 -0
- package/dist/endpoints/express-single.d.mts +15 -0
- package/dist/endpoints/express-single.d.mts.map +1 -0
- package/dist/endpoints/express-single.mjs +184 -0
- package/dist/endpoints/express-single.mjs.map +1 -0
- package/dist/endpoints/express-utils.cjs +118 -0
- package/dist/endpoints/express-utils.cjs.map +1 -0
- package/dist/endpoints/express-utils.mjs +116 -0
- package/dist/endpoints/express-utils.mjs.map +1 -0
- package/dist/endpoints/express.cjs +165 -0
- package/dist/endpoints/express.cjs.map +1 -0
- package/dist/endpoints/express.d.cts +15 -0
- package/dist/endpoints/express.d.cts.map +1 -0
- package/dist/endpoints/express.d.mts +15 -0
- package/dist/endpoints/express.d.mts.map +1 -0
- package/dist/endpoints/express.mjs +162 -0
- package/dist/endpoints/express.mjs.map +1 -0
- package/dist/endpoints/hono-single.cjs +140 -0
- package/dist/endpoints/hono-single.cjs.map +1 -0
- package/dist/endpoints/hono-single.d.cts +31 -0
- package/dist/endpoints/hono-single.d.cts.map +1 -0
- package/dist/endpoints/hono-single.d.mts +31 -0
- package/dist/endpoints/hono-single.d.mts.map +1 -0
- package/dist/endpoints/hono-single.mjs +139 -0
- package/dist/endpoints/hono-single.mjs.map +1 -0
- package/dist/endpoints/hono.cjs +152 -0
- package/dist/endpoints/hono.cjs.map +1 -0
- package/dist/endpoints/hono.d.cts +47 -0
- package/dist/endpoints/hono.d.cts.map +1 -0
- package/dist/endpoints/hono.d.mts +47 -0
- package/dist/endpoints/hono.d.mts.map +1 -0
- package/dist/endpoints/hono.mjs +151 -0
- package/dist/endpoints/hono.mjs.map +1 -0
- package/dist/endpoints/index.cjs +2 -0
- package/dist/endpoints/index.d.mts +2 -0
- package/dist/endpoints/index.mjs +4 -0
- package/dist/endpoints/single-route-helpers.cjs +67 -0
- package/dist/endpoints/single-route-helpers.cjs.map +1 -0
- package/dist/endpoints/single-route-helpers.mjs +64 -0
- package/dist/endpoints/single-route-helpers.mjs.map +1 -0
- package/dist/express.cjs +6 -0
- package/dist/express.d.cts +3 -0
- package/dist/express.d.mts +3 -19
- package/dist/express.mjs +3 -477
- package/dist/handlers/get-runtime-info.cjs +36 -0
- package/dist/handlers/get-runtime-info.cjs.map +1 -0
- package/dist/handlers/get-runtime-info.mjs +36 -0
- package/dist/handlers/get-runtime-info.mjs.map +1 -0
- package/dist/handlers/handle-connect.cjs +102 -0
- package/dist/handlers/handle-connect.cjs.map +1 -0
- package/dist/handlers/handle-connect.mjs +101 -0
- package/dist/handlers/handle-connect.mjs.map +1 -0
- package/dist/handlers/handle-run.cjs +114 -0
- package/dist/handlers/handle-run.cjs.map +1 -0
- package/dist/handlers/handle-run.mjs +113 -0
- package/dist/handlers/handle-run.mjs.map +1 -0
- package/dist/handlers/handle-stop.cjs +46 -0
- package/dist/handlers/handle-stop.cjs.map +1 -0
- package/dist/handlers/handle-stop.mjs +45 -0
- package/dist/handlers/handle-stop.mjs.map +1 -0
- package/dist/handlers/handle-transcribe.cjs +111 -0
- package/dist/handlers/handle-transcribe.cjs.map +1 -0
- package/dist/handlers/handle-transcribe.mjs +110 -0
- package/dist/handlers/handle-transcribe.mjs.map +1 -0
- package/dist/handlers/header-utils.cjs +25 -0
- package/dist/handlers/header-utils.cjs.map +1 -0
- package/dist/handlers/header-utils.mjs +24 -0
- package/dist/handlers/header-utils.mjs.map +1 -0
- package/dist/index.cjs +25 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.mts +9 -139
- package/dist/index.mjs +10 -335
- package/dist/middleware.cjs +29 -0
- package/dist/middleware.cjs.map +1 -0
- package/dist/middleware.d.cts +24 -0
- package/dist/middleware.d.cts.map +1 -0
- package/dist/middleware.d.mts +24 -0
- package/dist/middleware.d.mts.map +1 -0
- package/dist/middleware.mjs +27 -0
- package/dist/middleware.mjs.map +1 -0
- package/dist/package.cjs +12 -0
- package/dist/package.cjs.map +1 -0
- package/dist/package.mjs +6 -0
- package/dist/package.mjs.map +1 -0
- package/dist/runner/agent-runner.cjs +7 -0
- package/dist/runner/agent-runner.cjs.map +1 -0
- package/dist/runner/agent-runner.d.cts +28 -0
- package/dist/runner/agent-runner.d.cts.map +1 -0
- package/dist/runner/agent-runner.d.mts +28 -0
- package/dist/runner/agent-runner.d.mts.map +1 -0
- package/dist/runner/agent-runner.mjs +6 -0
- package/dist/runner/agent-runner.mjs.map +1 -0
- package/dist/runner/in-memory.cjs +230 -0
- package/dist/runner/in-memory.cjs.map +1 -0
- package/dist/runner/in-memory.d.cts +14 -0
- package/dist/runner/in-memory.d.cts.map +1 -0
- package/dist/runner/in-memory.d.mts +14 -0
- package/dist/runner/in-memory.d.mts.map +1 -0
- package/dist/runner/in-memory.mjs +229 -0
- package/dist/runner/in-memory.mjs.map +1 -0
- package/dist/runner/index.cjs +4 -0
- package/dist/runner/index.d.cts +4 -0
- package/dist/runner/index.d.mts +4 -0
- package/dist/runner/index.mjs +5 -0
- package/dist/runtime.cjs +27 -0
- package/dist/runtime.cjs.map +1 -0
- package/dist/runtime.d.cts +43 -0
- package/dist/runtime.d.cts.map +1 -0
- package/dist/runtime.d.mts +43 -0
- package/dist/runtime.d.mts.map +1 -0
- package/dist/runtime.mjs +26 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/transcription-service/transcription-service.cjs +7 -0
- package/dist/transcription-service/transcription-service.cjs.map +1 -0
- package/dist/transcription-service/transcription-service.d.cts +14 -0
- package/dist/transcription-service/transcription-service.d.cts.map +1 -0
- package/dist/transcription-service/transcription-service.d.mts +14 -0
- package/dist/transcription-service/transcription-service.d.mts.map +1 -0
- package/dist/transcription-service/transcription-service.mjs +6 -0
- package/dist/transcription-service/transcription-service.mjs.map +1 -0
- package/package.json +25 -22
- package/tsdown.config.ts +16 -0
- package/dist/chunk-5GKH3W25.mjs +0 -1054
- package/dist/chunk-5GKH3W25.mjs.map +0 -1
- package/dist/express.d.ts +0 -19
- package/dist/express.js +0 -1292
- package/dist/express.js.map +0 -1
- package/dist/express.mjs.map +0 -1
- package/dist/index.d.ts +0 -139
- package/dist/index.js +0 -1387
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/runtime-BsuyYT2l.d.mts +0 -98
- package/dist/runtime-BsuyYT2l.d.ts +0 -98
package/dist/express.mjs
CHANGED
|
@@ -1,478 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
callBeforeRequestMiddleware,
|
|
4
|
-
createJsonRequest,
|
|
5
|
-
expectString,
|
|
6
|
-
handleConnectAgent,
|
|
7
|
-
handleGetRuntimeInfo,
|
|
8
|
-
handleRunAgent,
|
|
9
|
-
handleStopAgent,
|
|
10
|
-
handleTranscribe,
|
|
11
|
-
parseMethodCall
|
|
12
|
-
} from "./chunk-5GKH3W25.mjs";
|
|
1
|
+
import { createCopilotEndpointExpress } from "./endpoints/express.mjs";
|
|
2
|
+
import { createCopilotEndpointSingleRouteExpress } from "./endpoints/express-single.mjs";
|
|
13
3
|
|
|
14
|
-
|
|
15
|
-
import express from "express";
|
|
16
|
-
import cors from "cors";
|
|
17
|
-
import { logger as logger2 } from "@copilotkitnext/shared";
|
|
18
|
-
|
|
19
|
-
// src/endpoints/express-utils.ts
|
|
20
|
-
import { Readable } from "stream";
|
|
21
|
-
import { pipeline } from "stream";
|
|
22
|
-
import { promisify } from "util";
|
|
23
|
-
import { logger } from "@copilotkitnext/shared";
|
|
24
|
-
var streamPipeline = promisify(pipeline);
|
|
25
|
-
var METHODS_WITHOUT_BODY = /* @__PURE__ */ new Set(["GET", "HEAD"]);
|
|
26
|
-
function createFetchRequestFromExpress(req) {
|
|
27
|
-
const method = req.method?.toUpperCase() ?? "GET";
|
|
28
|
-
const origin = buildOrigin(req);
|
|
29
|
-
const url = `${origin}${req.originalUrl ?? req.url ?? ""}`;
|
|
30
|
-
const headers = new Headers();
|
|
31
|
-
for (const [key, value] of Object.entries(req.headers)) {
|
|
32
|
-
if (value === void 0) continue;
|
|
33
|
-
if (Array.isArray(value)) {
|
|
34
|
-
value.forEach((v) => headers.append(key, v));
|
|
35
|
-
} else {
|
|
36
|
-
headers.set(key, value);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
const init = {
|
|
40
|
-
method,
|
|
41
|
-
headers
|
|
42
|
-
};
|
|
43
|
-
const hasParsedBody = req.body !== void 0 && req.body !== null;
|
|
44
|
-
const streamConsumed = isStreamConsumed(req, hasParsedBody);
|
|
45
|
-
if (!METHODS_WITHOUT_BODY.has(method)) {
|
|
46
|
-
const canStreamBody = req.readable !== false && !streamConsumed;
|
|
47
|
-
if (canStreamBody) {
|
|
48
|
-
init.body = Readable.toWeb(req);
|
|
49
|
-
init.duplex = "half";
|
|
50
|
-
} else if (hasParsedBody) {
|
|
51
|
-
const { body, contentType } = synthesizeBody(req.body);
|
|
52
|
-
if (contentType) {
|
|
53
|
-
headers.set("content-type", contentType);
|
|
54
|
-
}
|
|
55
|
-
headers.delete("content-length");
|
|
56
|
-
if (body !== void 0) {
|
|
57
|
-
init.body = body;
|
|
58
|
-
}
|
|
59
|
-
logger.info(
|
|
60
|
-
{
|
|
61
|
-
url,
|
|
62
|
-
method,
|
|
63
|
-
readable: req.readable,
|
|
64
|
-
readableEnded: req.readableEnded,
|
|
65
|
-
complete: req.complete
|
|
66
|
-
},
|
|
67
|
-
"Express request stream already consumed; synthesized body from parsed content"
|
|
68
|
-
);
|
|
69
|
-
} else {
|
|
70
|
-
headers.delete("content-length");
|
|
71
|
-
logger.warn(
|
|
72
|
-
{ url, method },
|
|
73
|
-
"Request stream already consumed but no body was available; sending empty body"
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const controller = new AbortController();
|
|
78
|
-
const abort = () => controller.abort();
|
|
79
|
-
req.on("aborted", abort);
|
|
80
|
-
req.on("error", abort);
|
|
81
|
-
req.on("close", () => {
|
|
82
|
-
if (req.aborted) {
|
|
83
|
-
abort();
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
init.signal = controller.signal;
|
|
87
|
-
try {
|
|
88
|
-
return new Request(url, init);
|
|
89
|
-
} catch (error) {
|
|
90
|
-
if (error instanceof TypeError && /disturbed|locked/i.test(error.message)) {
|
|
91
|
-
headers.delete("content-length");
|
|
92
|
-
delete init.duplex;
|
|
93
|
-
if (hasParsedBody) {
|
|
94
|
-
const { body, contentType } = synthesizeBody(req.body);
|
|
95
|
-
if (contentType) {
|
|
96
|
-
headers.set("content-type", contentType);
|
|
97
|
-
}
|
|
98
|
-
init.body = body;
|
|
99
|
-
logger.info(
|
|
100
|
-
{ url, method },
|
|
101
|
-
"Request stream disturbed while constructing Request; reused parsed body"
|
|
102
|
-
);
|
|
103
|
-
} else {
|
|
104
|
-
init.body = void 0;
|
|
105
|
-
logger.warn(
|
|
106
|
-
{ url, method },
|
|
107
|
-
"Request stream was disturbed; falling back to empty body"
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
return new Request(url, init);
|
|
111
|
-
}
|
|
112
|
-
throw error;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
async function sendFetchResponse(res, response) {
|
|
116
|
-
res.status(response.status);
|
|
117
|
-
response.headers.forEach((value, key) => {
|
|
118
|
-
if (key.toLowerCase() === "content-length" && response.body !== null) {
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
res.setHeader(key, value);
|
|
122
|
-
});
|
|
123
|
-
if (!response.body) {
|
|
124
|
-
res.end();
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
const nodeStream = Readable.fromWeb(response.body);
|
|
128
|
-
try {
|
|
129
|
-
await streamPipeline(nodeStream, res);
|
|
130
|
-
} catch (error) {
|
|
131
|
-
res.destroy(error);
|
|
132
|
-
throw error;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
function buildOrigin(req) {
|
|
136
|
-
const protocol = req.protocol || (req.secure ? "https" : "http");
|
|
137
|
-
const host = req.get("host") ?? "localhost";
|
|
138
|
-
return `${protocol}://${host}`;
|
|
139
|
-
}
|
|
140
|
-
function isStreamConsumed(req, hasParsedBody) {
|
|
141
|
-
const state = req._readableState;
|
|
142
|
-
return Boolean(
|
|
143
|
-
hasParsedBody || req.readableEnded || req.complete || state?.ended || state?.endEmitted
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
function synthesizeBody(body) {
|
|
147
|
-
if (Buffer.isBuffer(body) || body instanceof Uint8Array) {
|
|
148
|
-
return { body };
|
|
149
|
-
}
|
|
150
|
-
if (typeof body === "string") {
|
|
151
|
-
return { body };
|
|
152
|
-
}
|
|
153
|
-
if (typeof body === "object" && body !== void 0) {
|
|
154
|
-
return { body: JSON.stringify(body), contentType: "application/json" };
|
|
155
|
-
}
|
|
156
|
-
return {};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// src/endpoints/express.ts
|
|
160
|
-
function createCopilotEndpointExpress({
|
|
161
|
-
runtime,
|
|
162
|
-
basePath
|
|
163
|
-
}) {
|
|
164
|
-
const router = express.Router();
|
|
165
|
-
const normalizedBase = normalizeBasePath(basePath);
|
|
166
|
-
router.use(
|
|
167
|
-
cors({
|
|
168
|
-
origin: "*",
|
|
169
|
-
methods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"],
|
|
170
|
-
allowedHeaders: ["*"]
|
|
171
|
-
})
|
|
172
|
-
);
|
|
173
|
-
router.post(
|
|
174
|
-
joinPath(normalizedBase, "/agent/:agentId/run"),
|
|
175
|
-
createRouteHandler(runtime, async ({ request, req }) => {
|
|
176
|
-
const agentId = req.params.agentId;
|
|
177
|
-
return handleRunAgent({ runtime, request, agentId });
|
|
178
|
-
})
|
|
179
|
-
);
|
|
180
|
-
router.post(
|
|
181
|
-
joinPath(normalizedBase, "/agent/:agentId/connect"),
|
|
182
|
-
createRouteHandler(runtime, async ({ request, req }) => {
|
|
183
|
-
const agentId = req.params.agentId;
|
|
184
|
-
return handleConnectAgent({ runtime, request, agentId });
|
|
185
|
-
})
|
|
186
|
-
);
|
|
187
|
-
router.post(
|
|
188
|
-
joinPath(normalizedBase, "/agent/:agentId/stop/:threadId"),
|
|
189
|
-
createRouteHandler(runtime, async ({ request, req }) => {
|
|
190
|
-
const agentId = req.params.agentId;
|
|
191
|
-
const threadId = req.params.threadId;
|
|
192
|
-
return handleStopAgent({ runtime, request, agentId, threadId });
|
|
193
|
-
})
|
|
194
|
-
);
|
|
195
|
-
router.get(
|
|
196
|
-
joinPath(normalizedBase, "/info"),
|
|
197
|
-
createRouteHandler(runtime, async ({ request }) => {
|
|
198
|
-
return handleGetRuntimeInfo({ runtime, request });
|
|
199
|
-
})
|
|
200
|
-
);
|
|
201
|
-
router.post(
|
|
202
|
-
joinPath(normalizedBase, "/transcribe"),
|
|
203
|
-
createRouteHandler(runtime, async ({ request }) => {
|
|
204
|
-
return handleTranscribe({ runtime, request });
|
|
205
|
-
})
|
|
206
|
-
);
|
|
207
|
-
router.use(joinPath(normalizedBase, "*"), (req, res) => {
|
|
208
|
-
res.status(404).json({ error: "Not found" });
|
|
209
|
-
});
|
|
210
|
-
return router;
|
|
211
|
-
}
|
|
212
|
-
function createRouteHandler(runtime, factory) {
|
|
213
|
-
return async (req, res, next) => {
|
|
214
|
-
const path = req.originalUrl ?? req.path;
|
|
215
|
-
let request = createFetchRequestFromExpress(req);
|
|
216
|
-
try {
|
|
217
|
-
const maybeModifiedRequest = await callBeforeRequestMiddleware({
|
|
218
|
-
runtime,
|
|
219
|
-
request,
|
|
220
|
-
path
|
|
221
|
-
});
|
|
222
|
-
if (maybeModifiedRequest) {
|
|
223
|
-
request = maybeModifiedRequest;
|
|
224
|
-
}
|
|
225
|
-
} catch (error) {
|
|
226
|
-
logger2.error(
|
|
227
|
-
{ err: error, url: request.url, path },
|
|
228
|
-
"Error running before request middleware"
|
|
229
|
-
);
|
|
230
|
-
if (error instanceof Response) {
|
|
231
|
-
try {
|
|
232
|
-
await sendFetchResponse(res, error);
|
|
233
|
-
} catch (streamError) {
|
|
234
|
-
next(streamError);
|
|
235
|
-
}
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
next(error);
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
try {
|
|
242
|
-
const response = await factory({ request, req });
|
|
243
|
-
await sendFetchResponse(res, response);
|
|
244
|
-
callAfterRequestMiddleware({ runtime, response, path }).catch((error) => {
|
|
245
|
-
logger2.error(
|
|
246
|
-
{ err: error, url: req.originalUrl ?? req.url, path },
|
|
247
|
-
"Error running after request middleware"
|
|
248
|
-
);
|
|
249
|
-
});
|
|
250
|
-
} catch (error) {
|
|
251
|
-
if (error instanceof Response) {
|
|
252
|
-
try {
|
|
253
|
-
await sendFetchResponse(res, error);
|
|
254
|
-
} catch (streamError) {
|
|
255
|
-
next(streamError);
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
callAfterRequestMiddleware({ runtime, response: error, path }).catch(
|
|
259
|
-
(mwError) => {
|
|
260
|
-
logger2.error(
|
|
261
|
-
{ err: mwError, url: req.originalUrl ?? req.url, path },
|
|
262
|
-
"Error running after request middleware"
|
|
263
|
-
);
|
|
264
|
-
}
|
|
265
|
-
);
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
logger2.error(
|
|
269
|
-
{ err: error, url: request.url, path },
|
|
270
|
-
"Error running request handler"
|
|
271
|
-
);
|
|
272
|
-
next(error);
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
function normalizeBasePath(path) {
|
|
277
|
-
if (!path) {
|
|
278
|
-
throw new Error("basePath must be provided for Express endpoint");
|
|
279
|
-
}
|
|
280
|
-
if (!path.startsWith("/")) {
|
|
281
|
-
return `/${path}`;
|
|
282
|
-
}
|
|
283
|
-
if (path.length > 1 && path.endsWith("/")) {
|
|
284
|
-
return path.slice(0, -1);
|
|
285
|
-
}
|
|
286
|
-
return path;
|
|
287
|
-
}
|
|
288
|
-
function joinPath(basePath, suffix) {
|
|
289
|
-
if (basePath === "/") {
|
|
290
|
-
return suffix.startsWith("/") ? suffix : `/${suffix}`;
|
|
291
|
-
}
|
|
292
|
-
if (!suffix) {
|
|
293
|
-
return basePath;
|
|
294
|
-
}
|
|
295
|
-
if (suffix === "*") {
|
|
296
|
-
return `${basePath}/*`;
|
|
297
|
-
}
|
|
298
|
-
return `${basePath}${suffix.startsWith("/") ? suffix : `/${suffix}`}`;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// src/endpoints/express-single.ts
|
|
302
|
-
import express2 from "express";
|
|
303
|
-
import cors2 from "cors";
|
|
304
|
-
import { logger as logger3 } from "@copilotkitnext/shared";
|
|
305
|
-
function createCopilotEndpointSingleRouteExpress({
|
|
306
|
-
runtime,
|
|
307
|
-
basePath
|
|
308
|
-
}) {
|
|
309
|
-
const router = express2.Router();
|
|
310
|
-
const routePath = normalizeSingleRoutePath(basePath);
|
|
311
|
-
router.use(
|
|
312
|
-
cors2({
|
|
313
|
-
origin: "*",
|
|
314
|
-
methods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"],
|
|
315
|
-
allowedHeaders: ["*"]
|
|
316
|
-
})
|
|
317
|
-
);
|
|
318
|
-
router.post(routePath, createSingleRouteHandler(runtime));
|
|
319
|
-
router.use((req, res) => {
|
|
320
|
-
res.status(404).json({ error: "Not found" });
|
|
321
|
-
});
|
|
322
|
-
return router;
|
|
323
|
-
}
|
|
324
|
-
function createSingleRouteHandler(runtime) {
|
|
325
|
-
return async (req, res, next) => {
|
|
326
|
-
const path = req.originalUrl ?? req.path;
|
|
327
|
-
let request = createFetchRequestFromExpress(req);
|
|
328
|
-
try {
|
|
329
|
-
const maybeModifiedRequest = await callBeforeRequestMiddleware({
|
|
330
|
-
runtime,
|
|
331
|
-
request,
|
|
332
|
-
path
|
|
333
|
-
});
|
|
334
|
-
if (maybeModifiedRequest) {
|
|
335
|
-
request = maybeModifiedRequest;
|
|
336
|
-
}
|
|
337
|
-
} catch (error) {
|
|
338
|
-
logger3.error(
|
|
339
|
-
{ err: error, url: request.url, path },
|
|
340
|
-
"Error running before request middleware"
|
|
341
|
-
);
|
|
342
|
-
if (error instanceof Response) {
|
|
343
|
-
try {
|
|
344
|
-
await sendFetchResponse(res, error);
|
|
345
|
-
} catch (streamError) {
|
|
346
|
-
next(streamError);
|
|
347
|
-
}
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
next(error);
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
let methodCall;
|
|
354
|
-
try {
|
|
355
|
-
methodCall = await parseMethodCall(request);
|
|
356
|
-
} catch (error) {
|
|
357
|
-
if (error instanceof Response) {
|
|
358
|
-
logger3.warn({ url: request.url }, "Invalid single-route payload");
|
|
359
|
-
try {
|
|
360
|
-
await sendFetchResponse(res, error);
|
|
361
|
-
} catch (streamError) {
|
|
362
|
-
next(streamError);
|
|
363
|
-
}
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
logger3.warn(
|
|
367
|
-
{ err: error, url: request.url },
|
|
368
|
-
"Invalid single-route payload"
|
|
369
|
-
);
|
|
370
|
-
res.status(400).json({
|
|
371
|
-
error: "invalid_request",
|
|
372
|
-
message: error instanceof Error ? error.message : "Invalid request payload"
|
|
373
|
-
});
|
|
374
|
-
return;
|
|
375
|
-
}
|
|
376
|
-
try {
|
|
377
|
-
let response;
|
|
378
|
-
switch (methodCall.method) {
|
|
379
|
-
case "agent/run": {
|
|
380
|
-
const agentId = expectString(methodCall.params, "agentId");
|
|
381
|
-
const handlerRequest = createJsonRequest(request, methodCall.body);
|
|
382
|
-
response = await handleRunAgent({
|
|
383
|
-
runtime,
|
|
384
|
-
request: handlerRequest,
|
|
385
|
-
agentId
|
|
386
|
-
});
|
|
387
|
-
break;
|
|
388
|
-
}
|
|
389
|
-
case "agent/connect": {
|
|
390
|
-
const agentId = expectString(methodCall.params, "agentId");
|
|
391
|
-
const handlerRequest = createJsonRequest(request, methodCall.body);
|
|
392
|
-
response = await handleConnectAgent({
|
|
393
|
-
runtime,
|
|
394
|
-
request: handlerRequest,
|
|
395
|
-
agentId
|
|
396
|
-
});
|
|
397
|
-
break;
|
|
398
|
-
}
|
|
399
|
-
case "agent/stop": {
|
|
400
|
-
const agentId = expectString(methodCall.params, "agentId");
|
|
401
|
-
const threadId = expectString(methodCall.params, "threadId");
|
|
402
|
-
response = await handleStopAgent({
|
|
403
|
-
runtime,
|
|
404
|
-
request,
|
|
405
|
-
agentId,
|
|
406
|
-
threadId
|
|
407
|
-
});
|
|
408
|
-
break;
|
|
409
|
-
}
|
|
410
|
-
case "info": {
|
|
411
|
-
response = await handleGetRuntimeInfo({ runtime, request });
|
|
412
|
-
break;
|
|
413
|
-
}
|
|
414
|
-
case "transcribe": {
|
|
415
|
-
const handlerRequest = createJsonRequest(request, methodCall.body);
|
|
416
|
-
response = await handleTranscribe({
|
|
417
|
-
runtime,
|
|
418
|
-
request: handlerRequest
|
|
419
|
-
});
|
|
420
|
-
break;
|
|
421
|
-
}
|
|
422
|
-
default: {
|
|
423
|
-
const exhaustive = methodCall.method;
|
|
424
|
-
return exhaustive;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
await sendFetchResponse(res, response);
|
|
428
|
-
callAfterRequestMiddleware({ runtime, response, path }).catch((error) => {
|
|
429
|
-
logger3.error(
|
|
430
|
-
{ err: error, url: req.originalUrl ?? req.url, path },
|
|
431
|
-
"Error running after request middleware"
|
|
432
|
-
);
|
|
433
|
-
});
|
|
434
|
-
} catch (error) {
|
|
435
|
-
if (error instanceof Response) {
|
|
436
|
-
try {
|
|
437
|
-
await sendFetchResponse(res, error);
|
|
438
|
-
} catch (streamError) {
|
|
439
|
-
next(streamError);
|
|
440
|
-
return;
|
|
441
|
-
}
|
|
442
|
-
callAfterRequestMiddleware({ runtime, response: error, path }).catch(
|
|
443
|
-
(mwError) => {
|
|
444
|
-
logger3.error(
|
|
445
|
-
{ err: mwError, url: req.originalUrl ?? req.url, path },
|
|
446
|
-
"Error running after request middleware"
|
|
447
|
-
);
|
|
448
|
-
}
|
|
449
|
-
);
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
logger3.error(
|
|
453
|
-
{ err: error, url: request.url, path },
|
|
454
|
-
"Error running single-route handler"
|
|
455
|
-
);
|
|
456
|
-
next(error);
|
|
457
|
-
}
|
|
458
|
-
};
|
|
459
|
-
}
|
|
460
|
-
function normalizeSingleRoutePath(path) {
|
|
461
|
-
if (!path) {
|
|
462
|
-
throw new Error(
|
|
463
|
-
"basePath must be provided for Express single-route endpoint"
|
|
464
|
-
);
|
|
465
|
-
}
|
|
466
|
-
if (!path.startsWith("/")) {
|
|
467
|
-
return `/${path}`;
|
|
468
|
-
}
|
|
469
|
-
if (path.length > 1 && path.endsWith("/")) {
|
|
470
|
-
return path.slice(0, -1);
|
|
471
|
-
}
|
|
472
|
-
return path;
|
|
473
|
-
}
|
|
474
|
-
export {
|
|
475
|
-
createCopilotEndpointExpress,
|
|
476
|
-
createCopilotEndpointSingleRouteExpress
|
|
477
|
-
};
|
|
478
|
-
//# sourceMappingURL=express.mjs.map
|
|
4
|
+
export { createCopilotEndpointExpress, createCopilotEndpointSingleRouteExpress };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const require_runtime = require('../runtime.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/handlers/get-runtime-info.ts
|
|
4
|
+
async function handleGetRuntimeInfo({ runtime }) {
|
|
5
|
+
try {
|
|
6
|
+
const agents = await runtime.agents;
|
|
7
|
+
const runtimeInfo = {
|
|
8
|
+
version: require_runtime.VERSION,
|
|
9
|
+
agents: Object.entries(agents).reduce((acc, [name, agent]) => {
|
|
10
|
+
acc[name] = {
|
|
11
|
+
name,
|
|
12
|
+
description: agent.description,
|
|
13
|
+
className: agent.constructor.name
|
|
14
|
+
};
|
|
15
|
+
return acc;
|
|
16
|
+
}, {}),
|
|
17
|
+
audioFileTranscriptionEnabled: !!runtime.transcriptionService
|
|
18
|
+
};
|
|
19
|
+
return new Response(JSON.stringify(runtimeInfo), {
|
|
20
|
+
status: 200,
|
|
21
|
+
headers: { "Content-Type": "application/json" }
|
|
22
|
+
});
|
|
23
|
+
} catch (error) {
|
|
24
|
+
return new Response(JSON.stringify({
|
|
25
|
+
error: "Failed to retrieve runtime information",
|
|
26
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
27
|
+
}), {
|
|
28
|
+
status: 500,
|
|
29
|
+
headers: { "Content-Type": "application/json" }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
exports.handleGetRuntimeInfo = handleGetRuntimeInfo;
|
|
36
|
+
//# sourceMappingURL=get-runtime-info.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-runtime-info.cjs","names":["VERSION"],"sources":["../../src/handlers/get-runtime-info.ts"],"sourcesContent":["import { CopilotRuntime } from \"../runtime\";\nimport { AgentDescription, RuntimeInfo } from \"@copilotkitnext/shared\";\nimport { VERSION } from \"../runtime\";\n\ninterface HandleGetRuntimeInfoParameters {\n runtime: CopilotRuntime;\n request: Request;\n}\n\nexport async function handleGetRuntimeInfo({\n runtime,\n}: HandleGetRuntimeInfoParameters) {\n try {\n const agents = await runtime.agents;\n\n const agentsDict = Object.entries(agents).reduce(\n (acc, [name, agent]) => {\n acc[name] = {\n name,\n description: agent.description,\n className: agent.constructor.name,\n };\n return acc;\n },\n {} as Record<string, AgentDescription>,\n );\n\n const runtimeInfo: RuntimeInfo = {\n version: VERSION,\n agents: agentsDict,\n audioFileTranscriptionEnabled: !!runtime.transcriptionService,\n };\n\n return new Response(JSON.stringify(runtimeInfo), {\n status: 200,\n headers: { \"Content-Type\": \"application/json\" },\n });\n } catch (error) {\n return new Response(\n JSON.stringify({\n error: \"Failed to retrieve runtime information\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n }),\n {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n}\n"],"mappings":";;;AASA,eAAsB,qBAAqB,EACzC,WACiC;AACjC,KAAI;EACF,MAAM,SAAS,MAAM,QAAQ;EAc7B,MAAM,cAA2B;GAC/B,SAASA;GACT,QAdiB,OAAO,QAAQ,OAAO,CAAC,QACvC,KAAK,CAAC,MAAM,WAAW;AACtB,QAAI,QAAQ;KACV;KACA,aAAa,MAAM;KACnB,WAAW,MAAM,YAAY;KAC9B;AACD,WAAO;MAET,EAAE,CACH;GAKC,+BAA+B,CAAC,CAAC,QAAQ;GAC1C;AAED,SAAO,IAAI,SAAS,KAAK,UAAU,YAAY,EAAE;GAC/C,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CAAC;UACK,OAAO;AACd,SAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU;GACnD,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { VERSION } from "../runtime.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/handlers/get-runtime-info.ts
|
|
4
|
+
async function handleGetRuntimeInfo({ runtime }) {
|
|
5
|
+
try {
|
|
6
|
+
const agents = await runtime.agents;
|
|
7
|
+
const runtimeInfo = {
|
|
8
|
+
version: VERSION,
|
|
9
|
+
agents: Object.entries(agents).reduce((acc, [name, agent]) => {
|
|
10
|
+
acc[name] = {
|
|
11
|
+
name,
|
|
12
|
+
description: agent.description,
|
|
13
|
+
className: agent.constructor.name
|
|
14
|
+
};
|
|
15
|
+
return acc;
|
|
16
|
+
}, {}),
|
|
17
|
+
audioFileTranscriptionEnabled: !!runtime.transcriptionService
|
|
18
|
+
};
|
|
19
|
+
return new Response(JSON.stringify(runtimeInfo), {
|
|
20
|
+
status: 200,
|
|
21
|
+
headers: { "Content-Type": "application/json" }
|
|
22
|
+
});
|
|
23
|
+
} catch (error) {
|
|
24
|
+
return new Response(JSON.stringify({
|
|
25
|
+
error: "Failed to retrieve runtime information",
|
|
26
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
27
|
+
}), {
|
|
28
|
+
status: 500,
|
|
29
|
+
headers: { "Content-Type": "application/json" }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { handleGetRuntimeInfo };
|
|
36
|
+
//# sourceMappingURL=get-runtime-info.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-runtime-info.mjs","names":[],"sources":["../../src/handlers/get-runtime-info.ts"],"sourcesContent":["import { CopilotRuntime } from \"../runtime\";\nimport { AgentDescription, RuntimeInfo } from \"@copilotkitnext/shared\";\nimport { VERSION } from \"../runtime\";\n\ninterface HandleGetRuntimeInfoParameters {\n runtime: CopilotRuntime;\n request: Request;\n}\n\nexport async function handleGetRuntimeInfo({\n runtime,\n}: HandleGetRuntimeInfoParameters) {\n try {\n const agents = await runtime.agents;\n\n const agentsDict = Object.entries(agents).reduce(\n (acc, [name, agent]) => {\n acc[name] = {\n name,\n description: agent.description,\n className: agent.constructor.name,\n };\n return acc;\n },\n {} as Record<string, AgentDescription>,\n );\n\n const runtimeInfo: RuntimeInfo = {\n version: VERSION,\n agents: agentsDict,\n audioFileTranscriptionEnabled: !!runtime.transcriptionService,\n };\n\n return new Response(JSON.stringify(runtimeInfo), {\n status: 200,\n headers: { \"Content-Type\": \"application/json\" },\n });\n } catch (error) {\n return new Response(\n JSON.stringify({\n error: \"Failed to retrieve runtime information\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n }),\n {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n}\n"],"mappings":";;;AASA,eAAsB,qBAAqB,EACzC,WACiC;AACjC,KAAI;EACF,MAAM,SAAS,MAAM,QAAQ;EAc7B,MAAM,cAA2B;GAC/B,SAAS;GACT,QAdiB,OAAO,QAAQ,OAAO,CAAC,QACvC,KAAK,CAAC,MAAM,WAAW;AACtB,QAAI,QAAQ;KACV;KACA,aAAa,MAAM;KACnB,WAAW,MAAM,YAAY;KAC9B;AACD,WAAO;MAET,EAAE,CACH;GAKC,+BAA+B,CAAC,CAAC,QAAQ;GAC1C;AAED,SAAO,IAAI,SAAS,KAAK,UAAU,YAAY,EAAE;GAC/C,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CAAC;UACK,OAAO;AACd,SAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU;GACnD,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
2
|
+
const require_header_utils = require('./header-utils.cjs');
|
|
3
|
+
let _ag_ui_client = require("@ag-ui/client");
|
|
4
|
+
let _ag_ui_encoder = require("@ag-ui/encoder");
|
|
5
|
+
|
|
6
|
+
//#region src/handlers/handle-connect.ts
|
|
7
|
+
async function handleConnectAgent({ runtime, request, agentId }) {
|
|
8
|
+
try {
|
|
9
|
+
if (!(await runtime.agents)[agentId]) return new Response(JSON.stringify({
|
|
10
|
+
error: "Agent not found",
|
|
11
|
+
message: `Agent '${agentId}' does not exist`
|
|
12
|
+
}), {
|
|
13
|
+
status: 404,
|
|
14
|
+
headers: { "Content-Type": "application/json" }
|
|
15
|
+
});
|
|
16
|
+
let input;
|
|
17
|
+
try {
|
|
18
|
+
const requestBody = await request.json();
|
|
19
|
+
input = _ag_ui_client.RunAgentInputSchema.parse(requestBody);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error("Invalid connect request body:", error);
|
|
22
|
+
return new Response(JSON.stringify({
|
|
23
|
+
error: "Invalid request body",
|
|
24
|
+
details: error instanceof Error ? error.message : String(error)
|
|
25
|
+
}), {
|
|
26
|
+
status: 400,
|
|
27
|
+
headers: { "Content-Type": "application/json" }
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const stream = new TransformStream();
|
|
31
|
+
const writer = stream.writable.getWriter();
|
|
32
|
+
const encoder = new _ag_ui_encoder.EventEncoder();
|
|
33
|
+
let streamClosed = false;
|
|
34
|
+
(async () => {
|
|
35
|
+
const forwardableHeaders = require_header_utils.extractForwardableHeaders(request);
|
|
36
|
+
runtime.runner.connect({
|
|
37
|
+
threadId: input.threadId,
|
|
38
|
+
headers: forwardableHeaders
|
|
39
|
+
}).subscribe({
|
|
40
|
+
next: async (event) => {
|
|
41
|
+
if (!request.signal.aborted && !streamClosed) try {
|
|
42
|
+
await writer.write(encoder.encode(event));
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error instanceof Error && error.name === "AbortError") streamClosed = true;
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
error: async (error) => {
|
|
48
|
+
console.error("Error running agent:", error);
|
|
49
|
+
if (!streamClosed) try {
|
|
50
|
+
await writer.close();
|
|
51
|
+
streamClosed = true;
|
|
52
|
+
} catch {}
|
|
53
|
+
},
|
|
54
|
+
complete: async () => {
|
|
55
|
+
if (!streamClosed) try {
|
|
56
|
+
await writer.close();
|
|
57
|
+
streamClosed = true;
|
|
58
|
+
} catch {}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
})().catch((error) => {
|
|
62
|
+
console.error("Error running agent:", error);
|
|
63
|
+
console.error("Error stack:", error instanceof Error ? error.stack : "No stack trace");
|
|
64
|
+
console.error("Error details:", {
|
|
65
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
66
|
+
message: error instanceof Error ? error.message : String(error),
|
|
67
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
68
|
+
});
|
|
69
|
+
if (!streamClosed) try {
|
|
70
|
+
writer.close();
|
|
71
|
+
streamClosed = true;
|
|
72
|
+
} catch {}
|
|
73
|
+
});
|
|
74
|
+
return new Response(stream.readable, {
|
|
75
|
+
status: 200,
|
|
76
|
+
headers: {
|
|
77
|
+
"Content-Type": "text/event-stream",
|
|
78
|
+
"Cache-Control": "no-cache",
|
|
79
|
+
Connection: "keep-alive"
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error("Error running agent:", error);
|
|
84
|
+
console.error("Error stack:", error instanceof Error ? error.stack : "No stack trace");
|
|
85
|
+
console.error("Error details:", {
|
|
86
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
87
|
+
message: error instanceof Error ? error.message : String(error),
|
|
88
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
89
|
+
});
|
|
90
|
+
return new Response(JSON.stringify({
|
|
91
|
+
error: "Failed to run agent",
|
|
92
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
93
|
+
}), {
|
|
94
|
+
status: 500,
|
|
95
|
+
headers: { "Content-Type": "application/json" }
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
//#endregion
|
|
101
|
+
exports.handleConnectAgent = handleConnectAgent;
|
|
102
|
+
//# sourceMappingURL=handle-connect.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handle-connect.cjs","names":["RunAgentInputSchema","EventEncoder","extractForwardableHeaders"],"sources":["../../src/handlers/handle-connect.ts"],"sourcesContent":["import { RunAgentInput, RunAgentInputSchema } from \"@ag-ui/client\";\nimport { EventEncoder } from \"@ag-ui/encoder\";\nimport { CopilotRuntime } from \"../runtime\";\nimport { extractForwardableHeaders } from \"./header-utils\";\n\ninterface ConnectAgentParameters {\n request: Request;\n runtime: CopilotRuntime;\n agentId: string;\n}\n\nexport async function handleConnectAgent({\n runtime,\n request,\n agentId,\n}: ConnectAgentParameters) {\n try {\n const agents = await runtime.agents;\n\n // Check if the requested agent exists\n if (!agents[agentId]) {\n return new Response(\n JSON.stringify({\n error: \"Agent not found\",\n message: `Agent '${agentId}' does not exist`,\n }),\n {\n status: 404,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n // Parse and validate input BEFORE creating the stream\n // so we can return a proper error response\n let input: RunAgentInput;\n try {\n const requestBody = await request.json();\n input = RunAgentInputSchema.parse(requestBody);\n } catch (error) {\n console.error(\"Invalid connect request body:\", error);\n return new Response(\n JSON.stringify({\n error: \"Invalid request body\",\n details: error instanceof Error ? error.message : String(error),\n }),\n {\n status: 400,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n\n const stream = new TransformStream();\n const writer = stream.writable.getWriter();\n const encoder = new EventEncoder();\n let streamClosed = false;\n\n // Process the agent connect in the background\n (async () => {\n const forwardableHeaders = extractForwardableHeaders(request);\n\n runtime.runner\n .connect({\n threadId: input.threadId,\n headers: forwardableHeaders,\n })\n .subscribe({\n next: async (event) => {\n if (!request.signal.aborted && !streamClosed) {\n try {\n await writer.write(encoder.encode(event));\n } catch (error) {\n if (error instanceof Error && error.name === \"AbortError\") {\n streamClosed = true;\n }\n }\n }\n },\n error: async (error) => {\n console.error(\"Error running agent:\", error);\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n complete: async () => {\n if (!streamClosed) {\n try {\n await writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n },\n });\n })().catch((error) => {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n if (!streamClosed) {\n try {\n writer.close();\n streamClosed = true;\n } catch {\n // Stream already closed\n }\n }\n });\n\n // Return the SSE response\n return new Response(stream.readable, {\n status: 200,\n headers: {\n \"Content-Type\": \"text/event-stream\",\n \"Cache-Control\": \"no-cache\",\n Connection: \"keep-alive\",\n },\n });\n } catch (error) {\n console.error(\"Error running agent:\", error);\n console.error(\n \"Error stack:\",\n error instanceof Error ? error.stack : \"No stack trace\",\n );\n console.error(\"Error details:\", {\n name: error instanceof Error ? error.name : \"Unknown\",\n message: error instanceof Error ? error.message : String(error),\n cause: error instanceof Error ? error.cause : undefined,\n });\n\n return new Response(\n JSON.stringify({\n error: \"Failed to run agent\",\n message: error instanceof Error ? error.message : \"Unknown error\",\n }),\n {\n status: 500,\n headers: { \"Content-Type\": \"application/json\" },\n },\n );\n }\n}\n"],"mappings":";;;;;;AAWA,eAAsB,mBAAmB,EACvC,SACA,SACA,WACyB;AACzB,KAAI;AAIF,MAAI,EAHW,MAAM,QAAQ,QAGjB,SACV,QAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,UAAU,QAAQ;GAC5B,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF;EAKH,IAAI;AACJ,MAAI;GACF,MAAM,cAAc,MAAM,QAAQ,MAAM;AACxC,WAAQA,kCAAoB,MAAM,YAAY;WACvC,OAAO;AACd,WAAQ,MAAM,iCAAiC,MAAM;AACrD,UAAO,IAAI,SACT,KAAK,UAAU;IACb,OAAO;IACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;IAChE,CAAC,EACF;IACE,QAAQ;IACR,SAAS,EAAE,gBAAgB,oBAAoB;IAChD,CACF;;EAGH,MAAM,SAAS,IAAI,iBAAiB;EACpC,MAAM,SAAS,OAAO,SAAS,WAAW;EAC1C,MAAM,UAAU,IAAIC,6BAAc;EAClC,IAAI,eAAe;AAGnB,GAAC,YAAY;GACX,MAAM,qBAAqBC,+CAA0B,QAAQ;AAE7D,WAAQ,OACL,QAAQ;IACP,UAAU,MAAM;IAChB,SAAS;IACV,CAAC,CACD,UAAU;IACT,MAAM,OAAO,UAAU;AACrB,SAAI,CAAC,QAAQ,OAAO,WAAW,CAAC,aAC9B,KAAI;AACF,YAAM,OAAO,MAAM,QAAQ,OAAO,MAAM,CAAC;cAClC,OAAO;AACd,UAAI,iBAAiB,SAAS,MAAM,SAAS,aAC3C,gBAAe;;;IAKvB,OAAO,OAAO,UAAU;AACtB,aAAQ,MAAM,wBAAwB,MAAM;AAC5C,SAAI,CAAC,aACH,KAAI;AACF,YAAM,OAAO,OAAO;AACpB,qBAAe;aACT;;IAKZ,UAAU,YAAY;AACpB,SAAI,CAAC,aACH,KAAI;AACF,YAAM,OAAO,OAAO;AACpB,qBAAe;aACT;;IAKb,CAAC;MACF,CAAC,OAAO,UAAU;AACpB,WAAQ,MAAM,wBAAwB,MAAM;AAC5C,WAAQ,MACN,gBACA,iBAAiB,QAAQ,MAAM,QAAQ,iBACxC;AACD,WAAQ,MAAM,kBAAkB;IAC9B,MAAM,iBAAiB,QAAQ,MAAM,OAAO;IAC5C,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;IAC/D,OAAO,iBAAiB,QAAQ,MAAM,QAAQ;IAC/C,CAAC;AACF,OAAI,CAAC,aACH,KAAI;AACF,WAAO,OAAO;AACd,mBAAe;WACT;IAIV;AAGF,SAAO,IAAI,SAAS,OAAO,UAAU;GACnC,QAAQ;GACR,SAAS;IACP,gBAAgB;IAChB,iBAAiB;IACjB,YAAY;IACb;GACF,CAAC;UACK,OAAO;AACd,UAAQ,MAAM,wBAAwB,MAAM;AAC5C,UAAQ,MACN,gBACA,iBAAiB,QAAQ,MAAM,QAAQ,iBACxC;AACD,UAAQ,MAAM,kBAAkB;GAC9B,MAAM,iBAAiB,QAAQ,MAAM,OAAO;GAC5C,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;GAC/D,OAAO,iBAAiB,QAAQ,MAAM,QAAQ;GAC/C,CAAC;AAEF,SAAO,IAAI,SACT,KAAK,UAAU;GACb,OAAO;GACP,SAAS,iBAAiB,QAAQ,MAAM,UAAU;GACnD,CAAC,EACF;GACE,QAAQ;GACR,SAAS,EAAE,gBAAgB,oBAAoB;GAChD,CACF"}
|