@copilotkitnext/runtime 0.0.0-0.0.0-max-changeset-10101010101010-20260109191632
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/.cursor/rules/runtime.always.mdc +9 -0
- package/LICENSE +21 -0
- package/dist/chunk-3QLKFSXI.mjs +964 -0
- package/dist/chunk-3QLKFSXI.mjs.map +1 -0
- package/dist/express.d.mts +19 -0
- package/dist/express.d.ts +19 -0
- package/dist/express.js +1120 -0
- package/dist/express.js.map +1 -0
- package/dist/express.mjs +391 -0
- package/dist/express.mjs.map +1 -0
- package/dist/index.d.mts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +1221 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +257 -0
- package/dist/index.mjs.map +1 -0
- package/dist/runtime-BEcxV64L.d.mts +97 -0
- package/dist/runtime-BEcxV64L.d.ts +97 -0
- package/eslint.config.mjs +3 -0
- package/package.json +63 -0
- package/vitest.config.mjs +15 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AgentRunner: () => AgentRunner,
|
|
24
|
+
CopilotRuntime: () => CopilotRuntime,
|
|
25
|
+
InMemoryAgentRunner: () => InMemoryAgentRunner,
|
|
26
|
+
VERSION: () => VERSION,
|
|
27
|
+
createCopilotEndpoint: () => createCopilotEndpoint,
|
|
28
|
+
createCopilotEndpointSingleRoute: () => createCopilotEndpointSingleRoute,
|
|
29
|
+
finalizeRunEvents: () => import_shared5.finalizeRunEvents
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
|
|
33
|
+
// package.json
|
|
34
|
+
var package_default = {
|
|
35
|
+
name: "@copilotkitnext/runtime",
|
|
36
|
+
version: "0.0.0-0.0.0-max-changeset-10101010101010-20260109191632",
|
|
37
|
+
description: "Server-side runtime package for CopilotKit2",
|
|
38
|
+
main: "dist/index.js",
|
|
39
|
+
types: "dist/index.d.ts",
|
|
40
|
+
exports: {
|
|
41
|
+
".": {
|
|
42
|
+
types: "./dist/index.d.ts",
|
|
43
|
+
import: "./dist/index.mjs",
|
|
44
|
+
require: "./dist/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./express": {
|
|
47
|
+
types: "./dist/express.d.ts",
|
|
48
|
+
import: "./dist/express.mjs",
|
|
49
|
+
require: "./dist/express.js"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
publishConfig: {
|
|
53
|
+
access: "public"
|
|
54
|
+
},
|
|
55
|
+
scripts: {
|
|
56
|
+
build: "tsup",
|
|
57
|
+
prepublishOnly: "pnpm run build",
|
|
58
|
+
dev: "tsup --watch",
|
|
59
|
+
lint: "eslint . --max-warnings 0",
|
|
60
|
+
"check-types": "tsc --noEmit",
|
|
61
|
+
clean: "rm -rf dist",
|
|
62
|
+
test: "vitest run",
|
|
63
|
+
"test:watch": "vitest",
|
|
64
|
+
"test:coverage": "vitest run --coverage"
|
|
65
|
+
},
|
|
66
|
+
devDependencies: {
|
|
67
|
+
"@copilotkitnext/eslint-config": "workspace:*",
|
|
68
|
+
"@copilotkitnext/shared": "workspace:*",
|
|
69
|
+
"@copilotkitnext/typescript-config": "workspace:*",
|
|
70
|
+
"@types/cors": "^2.8.17",
|
|
71
|
+
"@types/express": "^4.17.21",
|
|
72
|
+
"@types/node": "^22.15.3",
|
|
73
|
+
eslint: "^9.30.0",
|
|
74
|
+
openai: "^5.9.0",
|
|
75
|
+
supertest: "^7.1.1",
|
|
76
|
+
tsup: "^8.5.0",
|
|
77
|
+
typescript: "5.8.2",
|
|
78
|
+
vitest: "^3.0.5"
|
|
79
|
+
},
|
|
80
|
+
dependencies: {
|
|
81
|
+
cors: "^2.8.5",
|
|
82
|
+
express: "^4.21.2",
|
|
83
|
+
hono: "^4.6.13",
|
|
84
|
+
rxjs: "7.8.1"
|
|
85
|
+
},
|
|
86
|
+
peerDependencies: {
|
|
87
|
+
"@ag-ui/client": "0.0.42",
|
|
88
|
+
"@ag-ui/core": "0.0.42",
|
|
89
|
+
"@ag-ui/encoder": "0.0.42",
|
|
90
|
+
"@copilotkitnext/shared": "workspace:*",
|
|
91
|
+
openai: "^5.9.0"
|
|
92
|
+
},
|
|
93
|
+
peerDependenciesMeta: {},
|
|
94
|
+
engines: {
|
|
95
|
+
node: ">=18"
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/runner/agent-runner.ts
|
|
100
|
+
var AgentRunner = class {
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/runner/in-memory.ts
|
|
104
|
+
var import_rxjs = require("rxjs");
|
|
105
|
+
var import_client = require("@ag-ui/client");
|
|
106
|
+
var import_shared = require("@copilotkitnext/shared");
|
|
107
|
+
var InMemoryEventStore = class {
|
|
108
|
+
constructor(threadId) {
|
|
109
|
+
this.threadId = threadId;
|
|
110
|
+
}
|
|
111
|
+
/** The subject that current consumers subscribe to. */
|
|
112
|
+
subject = null;
|
|
113
|
+
/** True while a run is actively producing events. */
|
|
114
|
+
isRunning = false;
|
|
115
|
+
/** Current run ID */
|
|
116
|
+
currentRunId = null;
|
|
117
|
+
/** Historic completed runs */
|
|
118
|
+
historicRuns = [];
|
|
119
|
+
/** Currently running agent instance (if any). */
|
|
120
|
+
agent = null;
|
|
121
|
+
/** Subject returned from run() while the run is active. */
|
|
122
|
+
runSubject = null;
|
|
123
|
+
/** True once stop() has been requested but the run has not yet finalized. */
|
|
124
|
+
stopRequested = false;
|
|
125
|
+
/** Reference to the events emitted in the current run. */
|
|
126
|
+
currentEvents = null;
|
|
127
|
+
};
|
|
128
|
+
var GLOBAL_STORE_KEY = /* @__PURE__ */ Symbol.for("@copilotkitnext/runtime/in-memory-store");
|
|
129
|
+
function getGlobalStore() {
|
|
130
|
+
const globalAny = globalThis;
|
|
131
|
+
if (!globalAny[GLOBAL_STORE_KEY]) {
|
|
132
|
+
globalAny[GLOBAL_STORE_KEY] = {
|
|
133
|
+
stores: /* @__PURE__ */ new Map(),
|
|
134
|
+
historicRunsBackup: /* @__PURE__ */ new Map()
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const data = globalAny[GLOBAL_STORE_KEY];
|
|
138
|
+
if (data.stores.size === 0 && data.historicRunsBackup.size > 0) {
|
|
139
|
+
for (const [threadId, historicRuns] of data.historicRunsBackup) {
|
|
140
|
+
const store = new InMemoryEventStore(threadId);
|
|
141
|
+
store.historicRuns = historicRuns;
|
|
142
|
+
data.stores.set(threadId, store);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return data.stores;
|
|
146
|
+
}
|
|
147
|
+
function backupHistoricRuns(threadId, historicRuns) {
|
|
148
|
+
const globalAny = globalThis;
|
|
149
|
+
if (globalAny[GLOBAL_STORE_KEY]) {
|
|
150
|
+
globalAny[GLOBAL_STORE_KEY].historicRunsBackup.set(threadId, historicRuns);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
var GLOBAL_STORE = getGlobalStore();
|
|
154
|
+
var InMemoryAgentRunner = class extends AgentRunner {
|
|
155
|
+
run(request) {
|
|
156
|
+
let existingStore = GLOBAL_STORE.get(request.threadId);
|
|
157
|
+
if (!existingStore) {
|
|
158
|
+
existingStore = new InMemoryEventStore(request.threadId);
|
|
159
|
+
GLOBAL_STORE.set(request.threadId, existingStore);
|
|
160
|
+
}
|
|
161
|
+
const store = existingStore;
|
|
162
|
+
if (store.isRunning) {
|
|
163
|
+
throw new Error("Thread already running");
|
|
164
|
+
}
|
|
165
|
+
store.isRunning = true;
|
|
166
|
+
store.currentRunId = request.input.runId;
|
|
167
|
+
store.agent = request.agent;
|
|
168
|
+
store.stopRequested = false;
|
|
169
|
+
const seenMessageIds = /* @__PURE__ */ new Set();
|
|
170
|
+
const currentRunEvents = [];
|
|
171
|
+
store.currentEvents = currentRunEvents;
|
|
172
|
+
const historicMessageIds = /* @__PURE__ */ new Set();
|
|
173
|
+
for (const run of store.historicRuns) {
|
|
174
|
+
for (const event of run.events) {
|
|
175
|
+
if ("messageId" in event && typeof event.messageId === "string") {
|
|
176
|
+
historicMessageIds.add(event.messageId);
|
|
177
|
+
}
|
|
178
|
+
if (event.type === import_client.EventType.RUN_STARTED) {
|
|
179
|
+
const runStarted = event;
|
|
180
|
+
const messages = runStarted.input?.messages ?? [];
|
|
181
|
+
for (const message of messages) {
|
|
182
|
+
historicMessageIds.add(message.id);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const nextSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
188
|
+
const prevSubject = store.subject;
|
|
189
|
+
store.subject = nextSubject;
|
|
190
|
+
const runSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
191
|
+
store.runSubject = runSubject;
|
|
192
|
+
const runAgent = async () => {
|
|
193
|
+
const lastRun = store.historicRuns[store.historicRuns.length - 1];
|
|
194
|
+
const parentRunId = lastRun?.runId ?? null;
|
|
195
|
+
try {
|
|
196
|
+
await request.agent.runAgent(request.input, {
|
|
197
|
+
onEvent: ({ event }) => {
|
|
198
|
+
let processedEvent = event;
|
|
199
|
+
if (event.type === import_client.EventType.RUN_STARTED) {
|
|
200
|
+
const runStartedEvent = event;
|
|
201
|
+
if (!runStartedEvent.input) {
|
|
202
|
+
const sanitizedMessages = request.input.messages ? request.input.messages.filter(
|
|
203
|
+
(message) => !historicMessageIds.has(message.id)
|
|
204
|
+
) : void 0;
|
|
205
|
+
const updatedInput = {
|
|
206
|
+
...request.input,
|
|
207
|
+
...sanitizedMessages !== void 0 ? { messages: sanitizedMessages } : {}
|
|
208
|
+
};
|
|
209
|
+
processedEvent = {
|
|
210
|
+
...runStartedEvent,
|
|
211
|
+
input: updatedInput
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
runSubject.next(processedEvent);
|
|
216
|
+
nextSubject.next(processedEvent);
|
|
217
|
+
currentRunEvents.push(processedEvent);
|
|
218
|
+
},
|
|
219
|
+
onNewMessage: ({ message }) => {
|
|
220
|
+
if (!seenMessageIds.has(message.id)) {
|
|
221
|
+
seenMessageIds.add(message.id);
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
onRunStartedEvent: () => {
|
|
225
|
+
if (request.input.messages) {
|
|
226
|
+
for (const message of request.input.messages) {
|
|
227
|
+
if (!seenMessageIds.has(message.id)) {
|
|
228
|
+
seenMessageIds.add(message.id);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
const appendedEvents = (0, import_shared.finalizeRunEvents)(currentRunEvents, {
|
|
235
|
+
stopRequested: store.stopRequested
|
|
236
|
+
});
|
|
237
|
+
for (const event of appendedEvents) {
|
|
238
|
+
runSubject.next(event);
|
|
239
|
+
nextSubject.next(event);
|
|
240
|
+
}
|
|
241
|
+
if (store.currentRunId) {
|
|
242
|
+
const compactedEvents = (0, import_client.compactEvents)(currentRunEvents);
|
|
243
|
+
store.historicRuns.push({
|
|
244
|
+
threadId: request.threadId,
|
|
245
|
+
runId: store.currentRunId,
|
|
246
|
+
parentRunId,
|
|
247
|
+
events: compactedEvents,
|
|
248
|
+
createdAt: Date.now()
|
|
249
|
+
});
|
|
250
|
+
backupHistoricRuns(request.threadId, store.historicRuns);
|
|
251
|
+
}
|
|
252
|
+
store.currentEvents = null;
|
|
253
|
+
store.currentRunId = null;
|
|
254
|
+
store.agent = null;
|
|
255
|
+
store.runSubject = null;
|
|
256
|
+
store.stopRequested = false;
|
|
257
|
+
store.isRunning = false;
|
|
258
|
+
runSubject.complete();
|
|
259
|
+
nextSubject.complete();
|
|
260
|
+
} catch {
|
|
261
|
+
const appendedEvents = (0, import_shared.finalizeRunEvents)(currentRunEvents, {
|
|
262
|
+
stopRequested: store.stopRequested
|
|
263
|
+
});
|
|
264
|
+
for (const event of appendedEvents) {
|
|
265
|
+
runSubject.next(event);
|
|
266
|
+
nextSubject.next(event);
|
|
267
|
+
}
|
|
268
|
+
if (store.currentRunId && currentRunEvents.length > 0) {
|
|
269
|
+
const compactedEvents = (0, import_client.compactEvents)(currentRunEvents);
|
|
270
|
+
store.historicRuns.push({
|
|
271
|
+
threadId: request.threadId,
|
|
272
|
+
runId: store.currentRunId,
|
|
273
|
+
parentRunId,
|
|
274
|
+
events: compactedEvents,
|
|
275
|
+
createdAt: Date.now()
|
|
276
|
+
});
|
|
277
|
+
backupHistoricRuns(request.threadId, store.historicRuns);
|
|
278
|
+
}
|
|
279
|
+
store.currentEvents = null;
|
|
280
|
+
store.currentRunId = null;
|
|
281
|
+
store.agent = null;
|
|
282
|
+
store.runSubject = null;
|
|
283
|
+
store.stopRequested = false;
|
|
284
|
+
store.isRunning = false;
|
|
285
|
+
runSubject.complete();
|
|
286
|
+
nextSubject.complete();
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
if (prevSubject) {
|
|
290
|
+
prevSubject.subscribe({
|
|
291
|
+
next: (e) => nextSubject.next(e),
|
|
292
|
+
error: (err) => nextSubject.error(err),
|
|
293
|
+
complete: () => {
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
runAgent();
|
|
298
|
+
return runSubject.asObservable();
|
|
299
|
+
}
|
|
300
|
+
connect(request) {
|
|
301
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
302
|
+
const connectionSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
303
|
+
if (!store) {
|
|
304
|
+
connectionSubject.complete();
|
|
305
|
+
return connectionSubject.asObservable();
|
|
306
|
+
}
|
|
307
|
+
const allHistoricEvents = [];
|
|
308
|
+
for (const run of store.historicRuns) {
|
|
309
|
+
allHistoricEvents.push(...run.events);
|
|
310
|
+
}
|
|
311
|
+
const compactedEvents = (0, import_client.compactEvents)(allHistoricEvents);
|
|
312
|
+
const emittedMessageIds = /* @__PURE__ */ new Set();
|
|
313
|
+
for (const event of compactedEvents) {
|
|
314
|
+
connectionSubject.next(event);
|
|
315
|
+
if ("messageId" in event && typeof event.messageId === "string") {
|
|
316
|
+
emittedMessageIds.add(event.messageId);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (store.subject && (store.isRunning || store.stopRequested)) {
|
|
320
|
+
store.subject.subscribe({
|
|
321
|
+
next: (event) => {
|
|
322
|
+
if ("messageId" in event && typeof event.messageId === "string" && emittedMessageIds.has(event.messageId)) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
connectionSubject.next(event);
|
|
326
|
+
},
|
|
327
|
+
complete: () => connectionSubject.complete(),
|
|
328
|
+
error: (err) => connectionSubject.error(err)
|
|
329
|
+
});
|
|
330
|
+
} else {
|
|
331
|
+
connectionSubject.complete();
|
|
332
|
+
}
|
|
333
|
+
return connectionSubject.asObservable();
|
|
334
|
+
}
|
|
335
|
+
isRunning(request) {
|
|
336
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
337
|
+
return Promise.resolve(store?.isRunning ?? false);
|
|
338
|
+
}
|
|
339
|
+
stop(request) {
|
|
340
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
341
|
+
if (!store || !store.isRunning) {
|
|
342
|
+
return Promise.resolve(false);
|
|
343
|
+
}
|
|
344
|
+
if (store.stopRequested) {
|
|
345
|
+
return Promise.resolve(false);
|
|
346
|
+
}
|
|
347
|
+
store.stopRequested = true;
|
|
348
|
+
store.isRunning = false;
|
|
349
|
+
const agent = store.agent;
|
|
350
|
+
if (!agent) {
|
|
351
|
+
store.stopRequested = false;
|
|
352
|
+
store.isRunning = false;
|
|
353
|
+
return Promise.resolve(false);
|
|
354
|
+
}
|
|
355
|
+
try {
|
|
356
|
+
agent.abortRun();
|
|
357
|
+
return Promise.resolve(true);
|
|
358
|
+
} catch (error) {
|
|
359
|
+
console.error("Failed to abort agent run", error);
|
|
360
|
+
store.stopRequested = false;
|
|
361
|
+
store.isRunning = true;
|
|
362
|
+
return Promise.resolve(false);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// src/runtime.ts
|
|
368
|
+
var VERSION = package_default.version;
|
|
369
|
+
var CopilotRuntime = class {
|
|
370
|
+
agents;
|
|
371
|
+
transcriptionService;
|
|
372
|
+
beforeRequestMiddleware;
|
|
373
|
+
afterRequestMiddleware;
|
|
374
|
+
runner;
|
|
375
|
+
constructor({
|
|
376
|
+
agents,
|
|
377
|
+
transcriptionService,
|
|
378
|
+
beforeRequestMiddleware,
|
|
379
|
+
afterRequestMiddleware,
|
|
380
|
+
runner
|
|
381
|
+
}) {
|
|
382
|
+
this.agents = agents;
|
|
383
|
+
this.transcriptionService = transcriptionService;
|
|
384
|
+
this.beforeRequestMiddleware = beforeRequestMiddleware;
|
|
385
|
+
this.afterRequestMiddleware = afterRequestMiddleware;
|
|
386
|
+
this.runner = runner ?? new InMemoryAgentRunner();
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// src/endpoints/hono.ts
|
|
391
|
+
var import_hono = require("hono");
|
|
392
|
+
var import_cors = require("hono/cors");
|
|
393
|
+
|
|
394
|
+
// src/handlers/handle-run.ts
|
|
395
|
+
var import_client2 = require("@ag-ui/client");
|
|
396
|
+
var import_encoder = require("@ag-ui/encoder");
|
|
397
|
+
async function handleRunAgent({
|
|
398
|
+
runtime,
|
|
399
|
+
request,
|
|
400
|
+
agentId
|
|
401
|
+
}) {
|
|
402
|
+
try {
|
|
403
|
+
const agents = await runtime.agents;
|
|
404
|
+
if (!agents[agentId]) {
|
|
405
|
+
return new Response(
|
|
406
|
+
JSON.stringify({
|
|
407
|
+
error: "Agent not found",
|
|
408
|
+
message: `Agent '${agentId}' does not exist`
|
|
409
|
+
}),
|
|
410
|
+
{
|
|
411
|
+
status: 404,
|
|
412
|
+
headers: { "Content-Type": "application/json" }
|
|
413
|
+
}
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
const registeredAgent = agents[agentId];
|
|
417
|
+
const agent = registeredAgent.clone();
|
|
418
|
+
if (agent && "headers" in agent) {
|
|
419
|
+
const shouldForward = (headerName) => {
|
|
420
|
+
const lower = headerName.toLowerCase();
|
|
421
|
+
return lower === "authorization" || lower.startsWith("x-");
|
|
422
|
+
};
|
|
423
|
+
const forwardableHeaders = {};
|
|
424
|
+
request.headers.forEach((value, key) => {
|
|
425
|
+
if (shouldForward(key)) {
|
|
426
|
+
forwardableHeaders[key] = value;
|
|
427
|
+
}
|
|
428
|
+
});
|
|
429
|
+
agent.headers = {
|
|
430
|
+
...agent.headers,
|
|
431
|
+
...forwardableHeaders
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
const stream = new TransformStream();
|
|
435
|
+
const writer = stream.writable.getWriter();
|
|
436
|
+
const encoder = new import_encoder.EventEncoder();
|
|
437
|
+
let streamClosed = false;
|
|
438
|
+
(async () => {
|
|
439
|
+
let input;
|
|
440
|
+
try {
|
|
441
|
+
const requestBody = await request.json();
|
|
442
|
+
input = import_client2.RunAgentInputSchema.parse(requestBody);
|
|
443
|
+
} catch {
|
|
444
|
+
return new Response(
|
|
445
|
+
JSON.stringify({
|
|
446
|
+
error: "Invalid request body"
|
|
447
|
+
}),
|
|
448
|
+
{ status: 400 }
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
agent.setMessages(input.messages);
|
|
452
|
+
agent.setState(input.state);
|
|
453
|
+
agent.threadId = input.threadId;
|
|
454
|
+
runtime.runner.run({
|
|
455
|
+
threadId: input.threadId,
|
|
456
|
+
agent,
|
|
457
|
+
input
|
|
458
|
+
}).subscribe({
|
|
459
|
+
next: async (event) => {
|
|
460
|
+
if (!request.signal.aborted && !streamClosed) {
|
|
461
|
+
try {
|
|
462
|
+
await writer.write(encoder.encode(event));
|
|
463
|
+
} catch (error) {
|
|
464
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
465
|
+
streamClosed = true;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
error: async (error) => {
|
|
471
|
+
console.error("Error running agent:", error);
|
|
472
|
+
if (!streamClosed) {
|
|
473
|
+
try {
|
|
474
|
+
await writer.close();
|
|
475
|
+
streamClosed = true;
|
|
476
|
+
} catch {
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
complete: async () => {
|
|
481
|
+
if (!streamClosed) {
|
|
482
|
+
try {
|
|
483
|
+
await writer.close();
|
|
484
|
+
streamClosed = true;
|
|
485
|
+
} catch {
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
})().catch((error) => {
|
|
491
|
+
console.error("Error running agent:", error);
|
|
492
|
+
console.error(
|
|
493
|
+
"Error stack:",
|
|
494
|
+
error instanceof Error ? error.stack : "No stack trace"
|
|
495
|
+
);
|
|
496
|
+
console.error("Error details:", {
|
|
497
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
498
|
+
message: error instanceof Error ? error.message : String(error),
|
|
499
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
500
|
+
});
|
|
501
|
+
if (!streamClosed) {
|
|
502
|
+
try {
|
|
503
|
+
writer.close();
|
|
504
|
+
streamClosed = true;
|
|
505
|
+
} catch {
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
return new Response(stream.readable, {
|
|
510
|
+
status: 200,
|
|
511
|
+
headers: {
|
|
512
|
+
"Content-Type": "text/event-stream",
|
|
513
|
+
"Cache-Control": "no-cache",
|
|
514
|
+
Connection: "keep-alive"
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
} catch (error) {
|
|
518
|
+
console.error("Error running agent:", error);
|
|
519
|
+
console.error(
|
|
520
|
+
"Error stack:",
|
|
521
|
+
error instanceof Error ? error.stack : "No stack trace"
|
|
522
|
+
);
|
|
523
|
+
console.error("Error details:", {
|
|
524
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
525
|
+
message: error instanceof Error ? error.message : String(error),
|
|
526
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
527
|
+
});
|
|
528
|
+
return new Response(
|
|
529
|
+
JSON.stringify({
|
|
530
|
+
error: "Failed to run agent",
|
|
531
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
532
|
+
}),
|
|
533
|
+
{
|
|
534
|
+
status: 500,
|
|
535
|
+
headers: { "Content-Type": "application/json" }
|
|
536
|
+
}
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// src/handlers/get-runtime-info.ts
|
|
542
|
+
async function handleGetRuntimeInfo({
|
|
543
|
+
runtime
|
|
544
|
+
}) {
|
|
545
|
+
try {
|
|
546
|
+
const agents = await runtime.agents;
|
|
547
|
+
const agentsDict = Object.entries(agents).reduce(
|
|
548
|
+
(acc, [name, agent]) => {
|
|
549
|
+
acc[name] = {
|
|
550
|
+
name,
|
|
551
|
+
description: agent.description,
|
|
552
|
+
className: agent.constructor.name
|
|
553
|
+
};
|
|
554
|
+
return acc;
|
|
555
|
+
},
|
|
556
|
+
{}
|
|
557
|
+
);
|
|
558
|
+
const runtimeInfo = {
|
|
559
|
+
version: VERSION,
|
|
560
|
+
agents: agentsDict,
|
|
561
|
+
audioFileTranscriptionEnabled: !!runtime.transcriptionService
|
|
562
|
+
};
|
|
563
|
+
return new Response(JSON.stringify(runtimeInfo), {
|
|
564
|
+
status: 200,
|
|
565
|
+
headers: { "Content-Type": "application/json" }
|
|
566
|
+
});
|
|
567
|
+
} catch (error) {
|
|
568
|
+
return new Response(
|
|
569
|
+
JSON.stringify({
|
|
570
|
+
error: "Failed to retrieve runtime information",
|
|
571
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
572
|
+
}),
|
|
573
|
+
{
|
|
574
|
+
status: 500,
|
|
575
|
+
headers: { "Content-Type": "application/json" }
|
|
576
|
+
}
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// src/handlers/handle-transcribe.ts
|
|
582
|
+
async function handleTranscribe({
|
|
583
|
+
runtime,
|
|
584
|
+
request
|
|
585
|
+
}) {
|
|
586
|
+
try {
|
|
587
|
+
if (!runtime.transcriptionService) {
|
|
588
|
+
return new Response(
|
|
589
|
+
JSON.stringify({
|
|
590
|
+
error: "Transcription service not configured",
|
|
591
|
+
message: "No transcription service has been configured in the runtime"
|
|
592
|
+
}),
|
|
593
|
+
{
|
|
594
|
+
status: 503,
|
|
595
|
+
headers: { "Content-Type": "application/json" }
|
|
596
|
+
}
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
const contentType = request.headers.get("content-type");
|
|
600
|
+
if (!contentType || !contentType.includes("multipart/form-data")) {
|
|
601
|
+
return new Response(
|
|
602
|
+
JSON.stringify({
|
|
603
|
+
error: "Invalid content type",
|
|
604
|
+
message: "Request must contain multipart/form-data with an audio file"
|
|
605
|
+
}),
|
|
606
|
+
{
|
|
607
|
+
status: 400,
|
|
608
|
+
headers: { "Content-Type": "application/json" }
|
|
609
|
+
}
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
const formData = await request.formData();
|
|
613
|
+
const audioFile = formData.get("audio");
|
|
614
|
+
if (!audioFile || !(audioFile instanceof File)) {
|
|
615
|
+
return new Response(
|
|
616
|
+
JSON.stringify({
|
|
617
|
+
error: "Missing audio file",
|
|
618
|
+
message: "No audio file found in form data. Please include an 'audio' field."
|
|
619
|
+
}),
|
|
620
|
+
{
|
|
621
|
+
status: 400,
|
|
622
|
+
headers: { "Content-Type": "application/json" }
|
|
623
|
+
}
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
const validAudioTypes = [
|
|
627
|
+
"audio/mpeg",
|
|
628
|
+
"audio/mp3",
|
|
629
|
+
"audio/mp4",
|
|
630
|
+
"audio/wav",
|
|
631
|
+
"audio/webm",
|
|
632
|
+
"audio/ogg",
|
|
633
|
+
"audio/flac",
|
|
634
|
+
"audio/aac"
|
|
635
|
+
];
|
|
636
|
+
const isValidType = validAudioTypes.includes(audioFile.type) || audioFile.type === "" || audioFile.type === "application/octet-stream";
|
|
637
|
+
if (!isValidType) {
|
|
638
|
+
return new Response(
|
|
639
|
+
JSON.stringify({
|
|
640
|
+
error: "Invalid file type",
|
|
641
|
+
message: `Unsupported audio file type: ${audioFile.type}. Supported types: ${validAudioTypes.join(", ")}, or files with unknown/empty types`
|
|
642
|
+
}),
|
|
643
|
+
{
|
|
644
|
+
status: 400,
|
|
645
|
+
headers: { "Content-Type": "application/json" }
|
|
646
|
+
}
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
const transcription = await runtime.transcriptionService.transcribeFile({
|
|
650
|
+
audioFile,
|
|
651
|
+
mimeType: audioFile.type,
|
|
652
|
+
size: audioFile.size
|
|
653
|
+
});
|
|
654
|
+
return new Response(
|
|
655
|
+
JSON.stringify({
|
|
656
|
+
text: transcription,
|
|
657
|
+
size: audioFile.size,
|
|
658
|
+
type: audioFile.type
|
|
659
|
+
}),
|
|
660
|
+
{
|
|
661
|
+
status: 200,
|
|
662
|
+
headers: { "Content-Type": "application/json" }
|
|
663
|
+
}
|
|
664
|
+
);
|
|
665
|
+
} catch (error) {
|
|
666
|
+
return new Response(
|
|
667
|
+
JSON.stringify({
|
|
668
|
+
error: "Transcription failed",
|
|
669
|
+
message: error instanceof Error ? error.message : "Unknown error occurred during transcription"
|
|
670
|
+
}),
|
|
671
|
+
{
|
|
672
|
+
status: 500,
|
|
673
|
+
headers: { "Content-Type": "application/json" }
|
|
674
|
+
}
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// src/endpoints/hono.ts
|
|
680
|
+
var import_shared3 = require("@copilotkitnext/shared");
|
|
681
|
+
|
|
682
|
+
// src/middleware.ts
|
|
683
|
+
var import_shared2 = require("@copilotkitnext/shared");
|
|
684
|
+
async function callBeforeRequestMiddleware({
|
|
685
|
+
runtime,
|
|
686
|
+
request,
|
|
687
|
+
path
|
|
688
|
+
}) {
|
|
689
|
+
const mw = runtime.beforeRequestMiddleware;
|
|
690
|
+
if (!mw) return;
|
|
691
|
+
if (typeof mw === "function") {
|
|
692
|
+
return mw({ runtime, request, path });
|
|
693
|
+
}
|
|
694
|
+
import_shared2.logger.warn({ mw }, "Unsupported beforeRequestMiddleware value \u2013 skipped");
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
async function callAfterRequestMiddleware({
|
|
698
|
+
runtime,
|
|
699
|
+
response,
|
|
700
|
+
path
|
|
701
|
+
}) {
|
|
702
|
+
const mw = runtime.afterRequestMiddleware;
|
|
703
|
+
if (!mw) return;
|
|
704
|
+
if (typeof mw === "function") {
|
|
705
|
+
return mw({ runtime, response, path });
|
|
706
|
+
}
|
|
707
|
+
import_shared2.logger.warn({ mw }, "Unsupported afterRequestMiddleware value \u2013 skipped");
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// src/handlers/handle-connect.ts
|
|
711
|
+
var import_client3 = require("@ag-ui/client");
|
|
712
|
+
var import_encoder2 = require("@ag-ui/encoder");
|
|
713
|
+
async function handleConnectAgent({
|
|
714
|
+
runtime,
|
|
715
|
+
request,
|
|
716
|
+
agentId
|
|
717
|
+
}) {
|
|
718
|
+
try {
|
|
719
|
+
const agents = await runtime.agents;
|
|
720
|
+
if (!agents[agentId]) {
|
|
721
|
+
return new Response(
|
|
722
|
+
JSON.stringify({
|
|
723
|
+
error: "Agent not found",
|
|
724
|
+
message: `Agent '${agentId}' does not exist`
|
|
725
|
+
}),
|
|
726
|
+
{
|
|
727
|
+
status: 404,
|
|
728
|
+
headers: { "Content-Type": "application/json" }
|
|
729
|
+
}
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
const stream = new TransformStream();
|
|
733
|
+
const writer = stream.writable.getWriter();
|
|
734
|
+
const encoder = new import_encoder2.EventEncoder();
|
|
735
|
+
let streamClosed = false;
|
|
736
|
+
(async () => {
|
|
737
|
+
let input;
|
|
738
|
+
try {
|
|
739
|
+
const requestBody = await request.json();
|
|
740
|
+
input = import_client3.RunAgentInputSchema.parse(requestBody);
|
|
741
|
+
} catch {
|
|
742
|
+
return new Response(
|
|
743
|
+
JSON.stringify({
|
|
744
|
+
error: "Invalid request body"
|
|
745
|
+
}),
|
|
746
|
+
{ status: 400 }
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
runtime.runner.connect({
|
|
750
|
+
threadId: input.threadId
|
|
751
|
+
}).subscribe({
|
|
752
|
+
next: async (event) => {
|
|
753
|
+
if (!request.signal.aborted && !streamClosed) {
|
|
754
|
+
try {
|
|
755
|
+
await writer.write(encoder.encode(event));
|
|
756
|
+
} catch (error) {
|
|
757
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
758
|
+
streamClosed = true;
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
},
|
|
763
|
+
error: async (error) => {
|
|
764
|
+
console.error("Error running agent:", error);
|
|
765
|
+
if (!streamClosed) {
|
|
766
|
+
try {
|
|
767
|
+
await writer.close();
|
|
768
|
+
streamClosed = true;
|
|
769
|
+
} catch {
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
},
|
|
773
|
+
complete: async () => {
|
|
774
|
+
if (!streamClosed) {
|
|
775
|
+
try {
|
|
776
|
+
await writer.close();
|
|
777
|
+
streamClosed = true;
|
|
778
|
+
} catch {
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
});
|
|
783
|
+
})().catch((error) => {
|
|
784
|
+
console.error("Error running agent:", error);
|
|
785
|
+
console.error(
|
|
786
|
+
"Error stack:",
|
|
787
|
+
error instanceof Error ? error.stack : "No stack trace"
|
|
788
|
+
);
|
|
789
|
+
console.error("Error details:", {
|
|
790
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
791
|
+
message: error instanceof Error ? error.message : String(error),
|
|
792
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
793
|
+
});
|
|
794
|
+
if (!streamClosed) {
|
|
795
|
+
try {
|
|
796
|
+
writer.close();
|
|
797
|
+
streamClosed = true;
|
|
798
|
+
} catch {
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
});
|
|
802
|
+
return new Response(stream.readable, {
|
|
803
|
+
status: 200,
|
|
804
|
+
headers: {
|
|
805
|
+
"Content-Type": "text/event-stream",
|
|
806
|
+
"Cache-Control": "no-cache",
|
|
807
|
+
Connection: "keep-alive"
|
|
808
|
+
}
|
|
809
|
+
});
|
|
810
|
+
} catch (error) {
|
|
811
|
+
console.error("Error running agent:", error);
|
|
812
|
+
console.error(
|
|
813
|
+
"Error stack:",
|
|
814
|
+
error instanceof Error ? error.stack : "No stack trace"
|
|
815
|
+
);
|
|
816
|
+
console.error("Error details:", {
|
|
817
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
818
|
+
message: error instanceof Error ? error.message : String(error),
|
|
819
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
820
|
+
});
|
|
821
|
+
return new Response(
|
|
822
|
+
JSON.stringify({
|
|
823
|
+
error: "Failed to run agent",
|
|
824
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
825
|
+
}),
|
|
826
|
+
{
|
|
827
|
+
status: 500,
|
|
828
|
+
headers: { "Content-Type": "application/json" }
|
|
829
|
+
}
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// src/handlers/handle-stop.ts
|
|
835
|
+
var import_client4 = require("@ag-ui/client");
|
|
836
|
+
async function handleStopAgent({
|
|
837
|
+
runtime,
|
|
838
|
+
request,
|
|
839
|
+
agentId,
|
|
840
|
+
threadId
|
|
841
|
+
}) {
|
|
842
|
+
try {
|
|
843
|
+
const agents = await runtime.agents;
|
|
844
|
+
if (!agents[agentId]) {
|
|
845
|
+
return new Response(
|
|
846
|
+
JSON.stringify({
|
|
847
|
+
error: "Agent not found",
|
|
848
|
+
message: `Agent '${agentId}' does not exist`
|
|
849
|
+
}),
|
|
850
|
+
{
|
|
851
|
+
status: 404,
|
|
852
|
+
headers: { "Content-Type": "application/json" }
|
|
853
|
+
}
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
const stopped = await runtime.runner.stop({ threadId });
|
|
857
|
+
if (!stopped) {
|
|
858
|
+
return new Response(
|
|
859
|
+
JSON.stringify({
|
|
860
|
+
stopped: false,
|
|
861
|
+
message: `No active run for thread '${threadId}'.`
|
|
862
|
+
}),
|
|
863
|
+
{
|
|
864
|
+
status: 200,
|
|
865
|
+
headers: { "Content-Type": "application/json" }
|
|
866
|
+
}
|
|
867
|
+
);
|
|
868
|
+
}
|
|
869
|
+
return new Response(
|
|
870
|
+
JSON.stringify({
|
|
871
|
+
stopped: true,
|
|
872
|
+
interrupt: {
|
|
873
|
+
type: import_client4.EventType.RUN_ERROR,
|
|
874
|
+
message: "Run stopped by user",
|
|
875
|
+
code: "STOPPED"
|
|
876
|
+
}
|
|
877
|
+
}),
|
|
878
|
+
{
|
|
879
|
+
status: 200,
|
|
880
|
+
headers: { "Content-Type": "application/json" }
|
|
881
|
+
}
|
|
882
|
+
);
|
|
883
|
+
} catch (error) {
|
|
884
|
+
console.error("Error stopping agent run:", error);
|
|
885
|
+
return new Response(
|
|
886
|
+
JSON.stringify({
|
|
887
|
+
error: "Failed to stop agent",
|
|
888
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
889
|
+
}),
|
|
890
|
+
{
|
|
891
|
+
status: 500,
|
|
892
|
+
headers: { "Content-Type": "application/json" }
|
|
893
|
+
}
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// src/endpoints/hono.ts
|
|
899
|
+
function createCopilotEndpoint({ runtime, basePath }) {
|
|
900
|
+
const app = new import_hono.Hono();
|
|
901
|
+
return app.basePath(basePath).use(
|
|
902
|
+
"*",
|
|
903
|
+
(0, import_cors.cors)({
|
|
904
|
+
origin: "*",
|
|
905
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"],
|
|
906
|
+
allowHeaders: ["*"]
|
|
907
|
+
})
|
|
908
|
+
).use("*", async (c, next) => {
|
|
909
|
+
const request = c.req.raw;
|
|
910
|
+
const path = c.req.path;
|
|
911
|
+
try {
|
|
912
|
+
const maybeModifiedRequest = await callBeforeRequestMiddleware({
|
|
913
|
+
runtime,
|
|
914
|
+
request,
|
|
915
|
+
path
|
|
916
|
+
});
|
|
917
|
+
if (maybeModifiedRequest) {
|
|
918
|
+
c.set("modifiedRequest", maybeModifiedRequest);
|
|
919
|
+
}
|
|
920
|
+
} catch (error) {
|
|
921
|
+
import_shared3.logger.error({ err: error, url: request.url, path }, "Error running before request middleware");
|
|
922
|
+
if (error instanceof Response) {
|
|
923
|
+
return error;
|
|
924
|
+
}
|
|
925
|
+
throw error;
|
|
926
|
+
}
|
|
927
|
+
await next();
|
|
928
|
+
}).use("*", async (c, next) => {
|
|
929
|
+
await next();
|
|
930
|
+
const response = c.res;
|
|
931
|
+
const path = c.req.path;
|
|
932
|
+
callAfterRequestMiddleware({
|
|
933
|
+
runtime,
|
|
934
|
+
response,
|
|
935
|
+
path
|
|
936
|
+
}).catch((error) => {
|
|
937
|
+
import_shared3.logger.error({ err: error, url: c.req.url, path }, "Error running after request middleware");
|
|
938
|
+
});
|
|
939
|
+
}).post("/agent/:agentId/run", async (c) => {
|
|
940
|
+
const agentId = c.req.param("agentId");
|
|
941
|
+
const request = c.get("modifiedRequest") || c.req.raw;
|
|
942
|
+
try {
|
|
943
|
+
return await handleRunAgent({
|
|
944
|
+
runtime,
|
|
945
|
+
request,
|
|
946
|
+
agentId
|
|
947
|
+
});
|
|
948
|
+
} catch (error) {
|
|
949
|
+
import_shared3.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
950
|
+
throw error;
|
|
951
|
+
}
|
|
952
|
+
}).post("/agent/:agentId/connect", async (c) => {
|
|
953
|
+
const agentId = c.req.param("agentId");
|
|
954
|
+
const request = c.get("modifiedRequest") || c.req.raw;
|
|
955
|
+
try {
|
|
956
|
+
return await handleConnectAgent({
|
|
957
|
+
runtime,
|
|
958
|
+
request,
|
|
959
|
+
agentId
|
|
960
|
+
});
|
|
961
|
+
} catch (error) {
|
|
962
|
+
import_shared3.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
963
|
+
throw error;
|
|
964
|
+
}
|
|
965
|
+
}).post("/agent/:agentId/stop/:threadId", async (c) => {
|
|
966
|
+
const agentId = c.req.param("agentId");
|
|
967
|
+
const threadId = c.req.param("threadId");
|
|
968
|
+
const request = c.get("modifiedRequest") || c.req.raw;
|
|
969
|
+
try {
|
|
970
|
+
return await handleStopAgent({
|
|
971
|
+
runtime,
|
|
972
|
+
request,
|
|
973
|
+
agentId,
|
|
974
|
+
threadId
|
|
975
|
+
});
|
|
976
|
+
} catch (error) {
|
|
977
|
+
import_shared3.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
978
|
+
throw error;
|
|
979
|
+
}
|
|
980
|
+
}).get("/info", async (c) => {
|
|
981
|
+
const request = c.get("modifiedRequest") || c.req.raw;
|
|
982
|
+
try {
|
|
983
|
+
return await handleGetRuntimeInfo({
|
|
984
|
+
runtime,
|
|
985
|
+
request
|
|
986
|
+
});
|
|
987
|
+
} catch (error) {
|
|
988
|
+
import_shared3.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
989
|
+
throw error;
|
|
990
|
+
}
|
|
991
|
+
}).post("/transcribe", async (c) => {
|
|
992
|
+
const request = c.get("modifiedRequest") || c.req.raw;
|
|
993
|
+
try {
|
|
994
|
+
return await handleTranscribe({
|
|
995
|
+
runtime,
|
|
996
|
+
request
|
|
997
|
+
});
|
|
998
|
+
} catch (error) {
|
|
999
|
+
import_shared3.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
1000
|
+
throw error;
|
|
1001
|
+
}
|
|
1002
|
+
}).notFound((c) => {
|
|
1003
|
+
return c.json({ error: "Not found" }, 404);
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// src/endpoints/hono-single.ts
|
|
1008
|
+
var import_hono2 = require("hono");
|
|
1009
|
+
var import_cors2 = require("hono/cors");
|
|
1010
|
+
var import_shared4 = require("@copilotkitnext/shared");
|
|
1011
|
+
|
|
1012
|
+
// src/endpoints/single-route-helpers.ts
|
|
1013
|
+
var METHOD_NAMES = [
|
|
1014
|
+
"agent/run",
|
|
1015
|
+
"agent/connect",
|
|
1016
|
+
"agent/stop",
|
|
1017
|
+
"info",
|
|
1018
|
+
"transcribe"
|
|
1019
|
+
];
|
|
1020
|
+
async function parseMethodCall(request) {
|
|
1021
|
+
const contentType = request.headers.get("content-type") || "";
|
|
1022
|
+
if (!contentType.includes("application/json")) {
|
|
1023
|
+
throw createResponseError("Single-route endpoint expects JSON payloads", 415);
|
|
1024
|
+
}
|
|
1025
|
+
let jsonEnvelope;
|
|
1026
|
+
try {
|
|
1027
|
+
jsonEnvelope = await request.clone().json();
|
|
1028
|
+
} catch (error) {
|
|
1029
|
+
throw createResponseError("Invalid JSON payload", 400);
|
|
1030
|
+
}
|
|
1031
|
+
const method = validateMethod(jsonEnvelope.method);
|
|
1032
|
+
return {
|
|
1033
|
+
method,
|
|
1034
|
+
params: jsonEnvelope.params,
|
|
1035
|
+
body: jsonEnvelope.body
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
function expectString(params, key) {
|
|
1039
|
+
const value = params?.[key];
|
|
1040
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
1041
|
+
return value;
|
|
1042
|
+
}
|
|
1043
|
+
throw createResponseError(`Missing or invalid parameter '${key}'`, 400);
|
|
1044
|
+
}
|
|
1045
|
+
function createJsonRequest(base, body) {
|
|
1046
|
+
if (body === void 0 || body === null) {
|
|
1047
|
+
throw createResponseError("Missing request body for JSON handler", 400);
|
|
1048
|
+
}
|
|
1049
|
+
const headers = new Headers(base.headers);
|
|
1050
|
+
headers.set("content-type", "application/json");
|
|
1051
|
+
headers.delete("content-length");
|
|
1052
|
+
const serializedBody = serializeJsonBody(body);
|
|
1053
|
+
return new Request(base.url, {
|
|
1054
|
+
method: "POST",
|
|
1055
|
+
headers,
|
|
1056
|
+
body: serializedBody,
|
|
1057
|
+
signal: base.signal
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
function createResponseError(message, status) {
|
|
1061
|
+
return new Response(
|
|
1062
|
+
JSON.stringify({
|
|
1063
|
+
error: "invalid_request",
|
|
1064
|
+
message
|
|
1065
|
+
}),
|
|
1066
|
+
{
|
|
1067
|
+
status,
|
|
1068
|
+
headers: {
|
|
1069
|
+
"Content-Type": "application/json"
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1074
|
+
function validateMethod(method) {
|
|
1075
|
+
if (!method) {
|
|
1076
|
+
throw createResponseError("Missing method field", 400);
|
|
1077
|
+
}
|
|
1078
|
+
if (METHOD_NAMES.includes(method)) {
|
|
1079
|
+
return method;
|
|
1080
|
+
}
|
|
1081
|
+
throw createResponseError(`Unsupported method '${method}'`, 400);
|
|
1082
|
+
}
|
|
1083
|
+
function serializeJsonBody(body) {
|
|
1084
|
+
if (typeof body === "string") {
|
|
1085
|
+
return body;
|
|
1086
|
+
}
|
|
1087
|
+
if (body instanceof Blob || body instanceof ArrayBuffer || body instanceof Uint8Array) {
|
|
1088
|
+
return body;
|
|
1089
|
+
}
|
|
1090
|
+
if (body instanceof FormData || body instanceof URLSearchParams) {
|
|
1091
|
+
return body;
|
|
1092
|
+
}
|
|
1093
|
+
return JSON.stringify(body);
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
// src/endpoints/hono-single.ts
|
|
1097
|
+
function createCopilotEndpointSingleRoute({ runtime, basePath }) {
|
|
1098
|
+
const app = new import_hono2.Hono();
|
|
1099
|
+
const routePath = normalizePath(basePath);
|
|
1100
|
+
return app.basePath(routePath).use(
|
|
1101
|
+
"*",
|
|
1102
|
+
(0, import_cors2.cors)({
|
|
1103
|
+
origin: "*",
|
|
1104
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"],
|
|
1105
|
+
allowHeaders: ["*"]
|
|
1106
|
+
})
|
|
1107
|
+
).use("*", async (c, next) => {
|
|
1108
|
+
const request = c.req.raw;
|
|
1109
|
+
const path = c.req.path;
|
|
1110
|
+
try {
|
|
1111
|
+
const maybeModifiedRequest = await callBeforeRequestMiddleware({
|
|
1112
|
+
runtime,
|
|
1113
|
+
request,
|
|
1114
|
+
path
|
|
1115
|
+
});
|
|
1116
|
+
if (maybeModifiedRequest) {
|
|
1117
|
+
c.set("modifiedRequest", maybeModifiedRequest);
|
|
1118
|
+
}
|
|
1119
|
+
} catch (error) {
|
|
1120
|
+
import_shared4.logger.error({ err: error, url: request.url, path }, "Error running before request middleware");
|
|
1121
|
+
if (error instanceof Response) {
|
|
1122
|
+
return error;
|
|
1123
|
+
}
|
|
1124
|
+
throw error;
|
|
1125
|
+
}
|
|
1126
|
+
await next();
|
|
1127
|
+
}).use("*", async (c, next) => {
|
|
1128
|
+
await next();
|
|
1129
|
+
const response = c.res;
|
|
1130
|
+
const path = c.req.path;
|
|
1131
|
+
callAfterRequestMiddleware({
|
|
1132
|
+
runtime,
|
|
1133
|
+
response,
|
|
1134
|
+
path
|
|
1135
|
+
}).catch((error) => {
|
|
1136
|
+
import_shared4.logger.error({ err: error, url: c.req.url, path }, "Error running after request middleware");
|
|
1137
|
+
});
|
|
1138
|
+
}).post("/", async (c) => {
|
|
1139
|
+
const request = c.get("modifiedRequest") || c.req.raw;
|
|
1140
|
+
let methodCall;
|
|
1141
|
+
try {
|
|
1142
|
+
methodCall = await parseMethodCall(request);
|
|
1143
|
+
} catch (error) {
|
|
1144
|
+
if (error instanceof Response) {
|
|
1145
|
+
import_shared4.logger.warn({ url: request.url }, "Invalid single-route payload");
|
|
1146
|
+
return error;
|
|
1147
|
+
}
|
|
1148
|
+
import_shared4.logger.warn({ err: error, url: request.url }, "Invalid single-route payload");
|
|
1149
|
+
return c.json(
|
|
1150
|
+
{
|
|
1151
|
+
error: "invalid_request",
|
|
1152
|
+
message: error instanceof Error ? error.message : "Invalid request payload"
|
|
1153
|
+
},
|
|
1154
|
+
400
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
try {
|
|
1158
|
+
switch (methodCall.method) {
|
|
1159
|
+
case "agent/run": {
|
|
1160
|
+
const agentId = expectString(methodCall.params, "agentId");
|
|
1161
|
+
const handlerRequest = createJsonRequest(request, methodCall.body);
|
|
1162
|
+
return await handleRunAgent({ runtime, request: handlerRequest, agentId });
|
|
1163
|
+
}
|
|
1164
|
+
case "agent/connect": {
|
|
1165
|
+
const agentId = expectString(methodCall.params, "agentId");
|
|
1166
|
+
const handlerRequest = createJsonRequest(request, methodCall.body);
|
|
1167
|
+
return await handleConnectAgent({ runtime, request: handlerRequest, agentId });
|
|
1168
|
+
}
|
|
1169
|
+
case "agent/stop": {
|
|
1170
|
+
const agentId = expectString(methodCall.params, "agentId");
|
|
1171
|
+
const threadId = expectString(methodCall.params, "threadId");
|
|
1172
|
+
return await handleStopAgent({ runtime, request, agentId, threadId });
|
|
1173
|
+
}
|
|
1174
|
+
case "info": {
|
|
1175
|
+
return await handleGetRuntimeInfo({ runtime, request });
|
|
1176
|
+
}
|
|
1177
|
+
case "transcribe": {
|
|
1178
|
+
return await handleTranscribe({ runtime, request });
|
|
1179
|
+
}
|
|
1180
|
+
default: {
|
|
1181
|
+
const exhaustiveCheck = methodCall.method;
|
|
1182
|
+
return exhaustiveCheck;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
} catch (error) {
|
|
1186
|
+
if (error instanceof Response) {
|
|
1187
|
+
return error;
|
|
1188
|
+
}
|
|
1189
|
+
import_shared4.logger.error({ err: error, url: request.url, method: methodCall.method }, "Error running single-route handler");
|
|
1190
|
+
throw error;
|
|
1191
|
+
}
|
|
1192
|
+
}).notFound((c) => {
|
|
1193
|
+
return c.json({ error: "Not found" }, 404);
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1196
|
+
function normalizePath(path) {
|
|
1197
|
+
if (!path) {
|
|
1198
|
+
throw new Error("basePath must be provided for single-route endpoint");
|
|
1199
|
+
}
|
|
1200
|
+
if (!path.startsWith("/")) {
|
|
1201
|
+
return `/${path}`;
|
|
1202
|
+
}
|
|
1203
|
+
if (path.length > 1 && path.endsWith("/")) {
|
|
1204
|
+
return path.slice(0, -1);
|
|
1205
|
+
}
|
|
1206
|
+
return path;
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
// src/runner/index.ts
|
|
1210
|
+
var import_shared5 = require("@copilotkitnext/shared");
|
|
1211
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1212
|
+
0 && (module.exports = {
|
|
1213
|
+
AgentRunner,
|
|
1214
|
+
CopilotRuntime,
|
|
1215
|
+
InMemoryAgentRunner,
|
|
1216
|
+
VERSION,
|
|
1217
|
+
createCopilotEndpoint,
|
|
1218
|
+
createCopilotEndpointSingleRoute,
|
|
1219
|
+
finalizeRunEvents
|
|
1220
|
+
});
|
|
1221
|
+
//# sourceMappingURL=index.js.map
|