@copilotkitnext/runtime 0.0.20 → 0.0.21
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/chunk-SZFA2SCT.mjs +936 -0
- package/dist/chunk-SZFA2SCT.mjs.map +1 -0
- package/dist/express.d.mts +19 -0
- package/dist/express.d.ts +19 -0
- package/dist/express.js +1003 -0
- package/dist/express.js.map +1 -0
- package/dist/express.mjs +316 -0
- package/dist/express.mjs.map +1 -0
- package/dist/index.d.mts +33 -99
- package/dist/index.d.ts +33 -99
- package/dist/index.js +223 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +142 -837
- package/dist/index.mjs.map +1 -1
- package/dist/runtime-BEcxV64L.d.mts +97 -0
- package/dist/runtime-BEcxV64L.d.ts +97 -0
- package/package.json +15 -5
|
@@ -0,0 +1,936 @@
|
|
|
1
|
+
// src/runner/agent-runner.ts
|
|
2
|
+
var AgentRunner = class {
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
// src/runner/in-memory.ts
|
|
6
|
+
import { ReplaySubject } from "rxjs";
|
|
7
|
+
import {
|
|
8
|
+
EventType,
|
|
9
|
+
compactEvents
|
|
10
|
+
} from "@ag-ui/client";
|
|
11
|
+
import { finalizeRunEvents } from "@copilotkitnext/shared";
|
|
12
|
+
var InMemoryEventStore = class {
|
|
13
|
+
constructor(threadId) {
|
|
14
|
+
this.threadId = threadId;
|
|
15
|
+
}
|
|
16
|
+
/** The subject that current consumers subscribe to. */
|
|
17
|
+
subject = null;
|
|
18
|
+
/** True while a run is actively producing events. */
|
|
19
|
+
isRunning = false;
|
|
20
|
+
/** Current run ID */
|
|
21
|
+
currentRunId = null;
|
|
22
|
+
/** Historic completed runs */
|
|
23
|
+
historicRuns = [];
|
|
24
|
+
/** Currently running agent instance (if any). */
|
|
25
|
+
agent = null;
|
|
26
|
+
/** Subject returned from run() while the run is active. */
|
|
27
|
+
runSubject = null;
|
|
28
|
+
/** True once stop() has been requested but the run has not yet finalized. */
|
|
29
|
+
stopRequested = false;
|
|
30
|
+
/** Reference to the events emitted in the current run. */
|
|
31
|
+
currentEvents = null;
|
|
32
|
+
};
|
|
33
|
+
var GLOBAL_STORE = /* @__PURE__ */ new Map();
|
|
34
|
+
var InMemoryAgentRunner = class extends AgentRunner {
|
|
35
|
+
run(request) {
|
|
36
|
+
let existingStore = GLOBAL_STORE.get(request.threadId);
|
|
37
|
+
if (!existingStore) {
|
|
38
|
+
existingStore = new InMemoryEventStore(request.threadId);
|
|
39
|
+
GLOBAL_STORE.set(request.threadId, existingStore);
|
|
40
|
+
}
|
|
41
|
+
const store = existingStore;
|
|
42
|
+
if (store.isRunning) {
|
|
43
|
+
throw new Error("Thread already running");
|
|
44
|
+
}
|
|
45
|
+
store.isRunning = true;
|
|
46
|
+
store.currentRunId = request.input.runId;
|
|
47
|
+
store.agent = request.agent;
|
|
48
|
+
store.stopRequested = false;
|
|
49
|
+
const seenMessageIds = /* @__PURE__ */ new Set();
|
|
50
|
+
const currentRunEvents = [];
|
|
51
|
+
store.currentEvents = currentRunEvents;
|
|
52
|
+
const historicMessageIds = /* @__PURE__ */ new Set();
|
|
53
|
+
for (const run of store.historicRuns) {
|
|
54
|
+
for (const event of run.events) {
|
|
55
|
+
if ("messageId" in event && typeof event.messageId === "string") {
|
|
56
|
+
historicMessageIds.add(event.messageId);
|
|
57
|
+
}
|
|
58
|
+
if (event.type === EventType.RUN_STARTED) {
|
|
59
|
+
const runStarted = event;
|
|
60
|
+
const messages = runStarted.input?.messages ?? [];
|
|
61
|
+
for (const message of messages) {
|
|
62
|
+
historicMessageIds.add(message.id);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const nextSubject = new ReplaySubject(Infinity);
|
|
68
|
+
const prevSubject = store.subject;
|
|
69
|
+
store.subject = nextSubject;
|
|
70
|
+
const runSubject = new ReplaySubject(Infinity);
|
|
71
|
+
store.runSubject = runSubject;
|
|
72
|
+
const runAgent = async () => {
|
|
73
|
+
const lastRun = store.historicRuns[store.historicRuns.length - 1];
|
|
74
|
+
const parentRunId = lastRun?.runId ?? null;
|
|
75
|
+
try {
|
|
76
|
+
await request.agent.runAgent(request.input, {
|
|
77
|
+
onEvent: ({ event }) => {
|
|
78
|
+
let processedEvent = event;
|
|
79
|
+
if (event.type === EventType.RUN_STARTED) {
|
|
80
|
+
const runStartedEvent = event;
|
|
81
|
+
if (!runStartedEvent.input) {
|
|
82
|
+
const sanitizedMessages = request.input.messages ? request.input.messages.filter(
|
|
83
|
+
(message) => !historicMessageIds.has(message.id)
|
|
84
|
+
) : void 0;
|
|
85
|
+
const updatedInput = {
|
|
86
|
+
...request.input,
|
|
87
|
+
...sanitizedMessages !== void 0 ? { messages: sanitizedMessages } : {}
|
|
88
|
+
};
|
|
89
|
+
processedEvent = {
|
|
90
|
+
...runStartedEvent,
|
|
91
|
+
input: updatedInput
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
runSubject.next(processedEvent);
|
|
96
|
+
nextSubject.next(processedEvent);
|
|
97
|
+
currentRunEvents.push(processedEvent);
|
|
98
|
+
},
|
|
99
|
+
onNewMessage: ({ message }) => {
|
|
100
|
+
if (!seenMessageIds.has(message.id)) {
|
|
101
|
+
seenMessageIds.add(message.id);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
onRunStartedEvent: () => {
|
|
105
|
+
if (request.input.messages) {
|
|
106
|
+
for (const message of request.input.messages) {
|
|
107
|
+
if (!seenMessageIds.has(message.id)) {
|
|
108
|
+
seenMessageIds.add(message.id);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const appendedEvents = finalizeRunEvents(currentRunEvents, {
|
|
115
|
+
stopRequested: store.stopRequested
|
|
116
|
+
});
|
|
117
|
+
for (const event of appendedEvents) {
|
|
118
|
+
runSubject.next(event);
|
|
119
|
+
nextSubject.next(event);
|
|
120
|
+
}
|
|
121
|
+
if (store.currentRunId) {
|
|
122
|
+
const compactedEvents = compactEvents(currentRunEvents);
|
|
123
|
+
store.historicRuns.push({
|
|
124
|
+
threadId: request.threadId,
|
|
125
|
+
runId: store.currentRunId,
|
|
126
|
+
parentRunId,
|
|
127
|
+
events: compactedEvents,
|
|
128
|
+
createdAt: Date.now()
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
store.currentEvents = null;
|
|
132
|
+
store.currentRunId = null;
|
|
133
|
+
store.agent = null;
|
|
134
|
+
store.runSubject = null;
|
|
135
|
+
store.stopRequested = false;
|
|
136
|
+
store.isRunning = false;
|
|
137
|
+
runSubject.complete();
|
|
138
|
+
nextSubject.complete();
|
|
139
|
+
} catch {
|
|
140
|
+
const appendedEvents = finalizeRunEvents(currentRunEvents, {
|
|
141
|
+
stopRequested: store.stopRequested
|
|
142
|
+
});
|
|
143
|
+
for (const event of appendedEvents) {
|
|
144
|
+
runSubject.next(event);
|
|
145
|
+
nextSubject.next(event);
|
|
146
|
+
}
|
|
147
|
+
if (store.currentRunId && currentRunEvents.length > 0) {
|
|
148
|
+
const compactedEvents = compactEvents(currentRunEvents);
|
|
149
|
+
store.historicRuns.push({
|
|
150
|
+
threadId: request.threadId,
|
|
151
|
+
runId: store.currentRunId,
|
|
152
|
+
parentRunId,
|
|
153
|
+
events: compactedEvents,
|
|
154
|
+
createdAt: Date.now()
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
store.currentEvents = null;
|
|
158
|
+
store.currentRunId = null;
|
|
159
|
+
store.agent = null;
|
|
160
|
+
store.runSubject = null;
|
|
161
|
+
store.stopRequested = false;
|
|
162
|
+
store.isRunning = false;
|
|
163
|
+
runSubject.complete();
|
|
164
|
+
nextSubject.complete();
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
if (prevSubject) {
|
|
168
|
+
prevSubject.subscribe({
|
|
169
|
+
next: (e) => nextSubject.next(e),
|
|
170
|
+
error: (err) => nextSubject.error(err),
|
|
171
|
+
complete: () => {
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
runAgent();
|
|
176
|
+
return runSubject.asObservable();
|
|
177
|
+
}
|
|
178
|
+
connect(request) {
|
|
179
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
180
|
+
const connectionSubject = new ReplaySubject(Infinity);
|
|
181
|
+
if (!store) {
|
|
182
|
+
connectionSubject.complete();
|
|
183
|
+
return connectionSubject.asObservable();
|
|
184
|
+
}
|
|
185
|
+
const allHistoricEvents = [];
|
|
186
|
+
for (const run of store.historicRuns) {
|
|
187
|
+
allHistoricEvents.push(...run.events);
|
|
188
|
+
}
|
|
189
|
+
const compactedEvents = compactEvents(allHistoricEvents);
|
|
190
|
+
const emittedMessageIds = /* @__PURE__ */ new Set();
|
|
191
|
+
for (const event of compactedEvents) {
|
|
192
|
+
connectionSubject.next(event);
|
|
193
|
+
if ("messageId" in event && typeof event.messageId === "string") {
|
|
194
|
+
emittedMessageIds.add(event.messageId);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (store.subject && (store.isRunning || store.stopRequested)) {
|
|
198
|
+
store.subject.subscribe({
|
|
199
|
+
next: (event) => {
|
|
200
|
+
if ("messageId" in event && typeof event.messageId === "string" && emittedMessageIds.has(event.messageId)) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
connectionSubject.next(event);
|
|
204
|
+
},
|
|
205
|
+
complete: () => connectionSubject.complete(),
|
|
206
|
+
error: (err) => connectionSubject.error(err)
|
|
207
|
+
});
|
|
208
|
+
} else {
|
|
209
|
+
connectionSubject.complete();
|
|
210
|
+
}
|
|
211
|
+
return connectionSubject.asObservable();
|
|
212
|
+
}
|
|
213
|
+
isRunning(request) {
|
|
214
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
215
|
+
return Promise.resolve(store?.isRunning ?? false);
|
|
216
|
+
}
|
|
217
|
+
stop(request) {
|
|
218
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
219
|
+
if (!store || !store.isRunning) {
|
|
220
|
+
return Promise.resolve(false);
|
|
221
|
+
}
|
|
222
|
+
if (store.stopRequested) {
|
|
223
|
+
return Promise.resolve(false);
|
|
224
|
+
}
|
|
225
|
+
store.stopRequested = true;
|
|
226
|
+
store.isRunning = false;
|
|
227
|
+
const agent = store.agent;
|
|
228
|
+
if (!agent) {
|
|
229
|
+
store.stopRequested = false;
|
|
230
|
+
store.isRunning = false;
|
|
231
|
+
return Promise.resolve(false);
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
agent.abortRun();
|
|
235
|
+
return Promise.resolve(true);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
console.error("Failed to abort agent run", error);
|
|
238
|
+
store.stopRequested = false;
|
|
239
|
+
store.isRunning = true;
|
|
240
|
+
return Promise.resolve(false);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// package.json
|
|
246
|
+
var package_default = {
|
|
247
|
+
name: "@copilotkitnext/runtime",
|
|
248
|
+
version: "0.0.21",
|
|
249
|
+
description: "Server-side runtime package for CopilotKit2",
|
|
250
|
+
main: "dist/index.js",
|
|
251
|
+
types: "dist/index.d.ts",
|
|
252
|
+
exports: {
|
|
253
|
+
".": {
|
|
254
|
+
types: "./dist/index.d.ts",
|
|
255
|
+
import: "./dist/index.mjs",
|
|
256
|
+
require: "./dist/index.js"
|
|
257
|
+
},
|
|
258
|
+
"./express": {
|
|
259
|
+
types: "./dist/express.d.ts",
|
|
260
|
+
import: "./dist/express.mjs",
|
|
261
|
+
require: "./dist/express.js"
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
publishConfig: {
|
|
265
|
+
access: "public"
|
|
266
|
+
},
|
|
267
|
+
scripts: {
|
|
268
|
+
build: "tsup",
|
|
269
|
+
prepublishOnly: "pnpm run build",
|
|
270
|
+
dev: "tsup --watch",
|
|
271
|
+
lint: "eslint . --max-warnings 0",
|
|
272
|
+
"check-types": "tsc --noEmit",
|
|
273
|
+
clean: "rm -rf dist",
|
|
274
|
+
test: "vitest run",
|
|
275
|
+
"test:watch": "vitest",
|
|
276
|
+
"test:coverage": "vitest run --coverage"
|
|
277
|
+
},
|
|
278
|
+
devDependencies: {
|
|
279
|
+
"@copilotkitnext/eslint-config": "workspace:*",
|
|
280
|
+
"@copilotkitnext/typescript-config": "workspace:*",
|
|
281
|
+
"@types/cors": "^2.8.17",
|
|
282
|
+
"@types/express": "^4.17.21",
|
|
283
|
+
"@types/node": "^22.15.3",
|
|
284
|
+
eslint: "^9.30.0",
|
|
285
|
+
openai: "^5.9.0",
|
|
286
|
+
supertest: "^7.1.1",
|
|
287
|
+
tsup: "^8.5.0",
|
|
288
|
+
typescript: "5.8.2",
|
|
289
|
+
vitest: "^3.0.5"
|
|
290
|
+
},
|
|
291
|
+
dependencies: {
|
|
292
|
+
"@ag-ui/client": "0.0.40-alpha.11",
|
|
293
|
+
"@ag-ui/core": "0.0.40-alpha.11",
|
|
294
|
+
"@ag-ui/encoder": "0.0.40-alpha.11",
|
|
295
|
+
"@copilotkitnext/shared": "workspace:*",
|
|
296
|
+
cors: "^2.8.5",
|
|
297
|
+
express: "^4.21.2",
|
|
298
|
+
hono: "^4.6.13",
|
|
299
|
+
rxjs: "7.8.1"
|
|
300
|
+
},
|
|
301
|
+
peerDependencies: {
|
|
302
|
+
openai: "^5.9.0"
|
|
303
|
+
},
|
|
304
|
+
peerDependenciesMeta: {},
|
|
305
|
+
engines: {
|
|
306
|
+
node: ">=18"
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
// src/runtime.ts
|
|
311
|
+
var VERSION = package_default.version;
|
|
312
|
+
var CopilotRuntime = class {
|
|
313
|
+
agents;
|
|
314
|
+
transcriptionService;
|
|
315
|
+
beforeRequestMiddleware;
|
|
316
|
+
afterRequestMiddleware;
|
|
317
|
+
runner;
|
|
318
|
+
constructor({
|
|
319
|
+
agents,
|
|
320
|
+
transcriptionService,
|
|
321
|
+
beforeRequestMiddleware,
|
|
322
|
+
afterRequestMiddleware,
|
|
323
|
+
runner
|
|
324
|
+
}) {
|
|
325
|
+
this.agents = agents;
|
|
326
|
+
this.transcriptionService = transcriptionService;
|
|
327
|
+
this.beforeRequestMiddleware = beforeRequestMiddleware;
|
|
328
|
+
this.afterRequestMiddleware = afterRequestMiddleware;
|
|
329
|
+
this.runner = runner ?? new InMemoryAgentRunner();
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
// src/handlers/handle-run.ts
|
|
334
|
+
import {
|
|
335
|
+
RunAgentInputSchema
|
|
336
|
+
} from "@ag-ui/client";
|
|
337
|
+
import { EventEncoder } from "@ag-ui/encoder";
|
|
338
|
+
async function handleRunAgent({
|
|
339
|
+
runtime,
|
|
340
|
+
request,
|
|
341
|
+
agentId
|
|
342
|
+
}) {
|
|
343
|
+
try {
|
|
344
|
+
const agents = await runtime.agents;
|
|
345
|
+
if (!agents[agentId]) {
|
|
346
|
+
return new Response(
|
|
347
|
+
JSON.stringify({
|
|
348
|
+
error: "Agent not found",
|
|
349
|
+
message: `Agent '${agentId}' does not exist`
|
|
350
|
+
}),
|
|
351
|
+
{
|
|
352
|
+
status: 404,
|
|
353
|
+
headers: { "Content-Type": "application/json" }
|
|
354
|
+
}
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
const registeredAgent = agents[agentId];
|
|
358
|
+
const agent = registeredAgent.clone();
|
|
359
|
+
if (agent && "headers" in agent) {
|
|
360
|
+
const shouldForward = (headerName) => {
|
|
361
|
+
const lower = headerName.toLowerCase();
|
|
362
|
+
return lower === "authorization" || lower.startsWith("x-");
|
|
363
|
+
};
|
|
364
|
+
const forwardableHeaders = {};
|
|
365
|
+
request.headers.forEach((value, key) => {
|
|
366
|
+
if (shouldForward(key)) {
|
|
367
|
+
forwardableHeaders[key] = value;
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
agent.headers = {
|
|
371
|
+
...agent.headers,
|
|
372
|
+
...forwardableHeaders
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
const stream = new TransformStream();
|
|
376
|
+
const writer = stream.writable.getWriter();
|
|
377
|
+
const encoder = new EventEncoder();
|
|
378
|
+
let streamClosed = false;
|
|
379
|
+
(async () => {
|
|
380
|
+
let input;
|
|
381
|
+
try {
|
|
382
|
+
const requestBody = await request.json();
|
|
383
|
+
input = RunAgentInputSchema.parse(requestBody);
|
|
384
|
+
} catch {
|
|
385
|
+
return new Response(
|
|
386
|
+
JSON.stringify({
|
|
387
|
+
error: "Invalid request body"
|
|
388
|
+
}),
|
|
389
|
+
{ status: 400 }
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
agent.setMessages(input.messages);
|
|
393
|
+
agent.setState(input.state);
|
|
394
|
+
agent.threadId = input.threadId;
|
|
395
|
+
runtime.runner.run({
|
|
396
|
+
threadId: input.threadId,
|
|
397
|
+
agent,
|
|
398
|
+
input
|
|
399
|
+
}).subscribe({
|
|
400
|
+
next: async (event) => {
|
|
401
|
+
if (!request.signal.aborted && !streamClosed) {
|
|
402
|
+
try {
|
|
403
|
+
await writer.write(encoder.encode(event));
|
|
404
|
+
} catch (error) {
|
|
405
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
406
|
+
streamClosed = true;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
},
|
|
411
|
+
error: async (error) => {
|
|
412
|
+
console.error("Error running agent:", error);
|
|
413
|
+
if (!streamClosed) {
|
|
414
|
+
try {
|
|
415
|
+
await writer.close();
|
|
416
|
+
streamClosed = true;
|
|
417
|
+
} catch {
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
complete: async () => {
|
|
422
|
+
if (!streamClosed) {
|
|
423
|
+
try {
|
|
424
|
+
await writer.close();
|
|
425
|
+
streamClosed = true;
|
|
426
|
+
} catch {
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
})().catch((error) => {
|
|
432
|
+
console.error("Error running agent:", error);
|
|
433
|
+
console.error(
|
|
434
|
+
"Error stack:",
|
|
435
|
+
error instanceof Error ? error.stack : "No stack trace"
|
|
436
|
+
);
|
|
437
|
+
console.error("Error details:", {
|
|
438
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
439
|
+
message: error instanceof Error ? error.message : String(error),
|
|
440
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
441
|
+
});
|
|
442
|
+
if (!streamClosed) {
|
|
443
|
+
try {
|
|
444
|
+
writer.close();
|
|
445
|
+
streamClosed = true;
|
|
446
|
+
} catch {
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
return new Response(stream.readable, {
|
|
451
|
+
status: 200,
|
|
452
|
+
headers: {
|
|
453
|
+
"Content-Type": "text/event-stream",
|
|
454
|
+
"Cache-Control": "no-cache",
|
|
455
|
+
Connection: "keep-alive"
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
} catch (error) {
|
|
459
|
+
console.error("Error running agent:", error);
|
|
460
|
+
console.error(
|
|
461
|
+
"Error stack:",
|
|
462
|
+
error instanceof Error ? error.stack : "No stack trace"
|
|
463
|
+
);
|
|
464
|
+
console.error("Error details:", {
|
|
465
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
466
|
+
message: error instanceof Error ? error.message : String(error),
|
|
467
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
468
|
+
});
|
|
469
|
+
return new Response(
|
|
470
|
+
JSON.stringify({
|
|
471
|
+
error: "Failed to run agent",
|
|
472
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
473
|
+
}),
|
|
474
|
+
{
|
|
475
|
+
status: 500,
|
|
476
|
+
headers: { "Content-Type": "application/json" }
|
|
477
|
+
}
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// src/handlers/handle-connect.ts
|
|
483
|
+
import { RunAgentInputSchema as RunAgentInputSchema2 } from "@ag-ui/client";
|
|
484
|
+
import { EventEncoder as EventEncoder2 } from "@ag-ui/encoder";
|
|
485
|
+
async function handleConnectAgent({
|
|
486
|
+
runtime,
|
|
487
|
+
request,
|
|
488
|
+
agentId
|
|
489
|
+
}) {
|
|
490
|
+
try {
|
|
491
|
+
const agents = await runtime.agents;
|
|
492
|
+
if (!agents[agentId]) {
|
|
493
|
+
return new Response(
|
|
494
|
+
JSON.stringify({
|
|
495
|
+
error: "Agent not found",
|
|
496
|
+
message: `Agent '${agentId}' does not exist`
|
|
497
|
+
}),
|
|
498
|
+
{
|
|
499
|
+
status: 404,
|
|
500
|
+
headers: { "Content-Type": "application/json" }
|
|
501
|
+
}
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
const stream = new TransformStream();
|
|
505
|
+
const writer = stream.writable.getWriter();
|
|
506
|
+
const encoder = new EventEncoder2();
|
|
507
|
+
let streamClosed = false;
|
|
508
|
+
(async () => {
|
|
509
|
+
let input;
|
|
510
|
+
try {
|
|
511
|
+
const requestBody = await request.json();
|
|
512
|
+
input = RunAgentInputSchema2.parse(requestBody);
|
|
513
|
+
} catch {
|
|
514
|
+
return new Response(
|
|
515
|
+
JSON.stringify({
|
|
516
|
+
error: "Invalid request body"
|
|
517
|
+
}),
|
|
518
|
+
{ status: 400 }
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
runtime.runner.connect({
|
|
522
|
+
threadId: input.threadId
|
|
523
|
+
}).subscribe({
|
|
524
|
+
next: async (event) => {
|
|
525
|
+
if (!request.signal.aborted && !streamClosed) {
|
|
526
|
+
try {
|
|
527
|
+
await writer.write(encoder.encode(event));
|
|
528
|
+
} catch (error) {
|
|
529
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
530
|
+
streamClosed = true;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
},
|
|
535
|
+
error: async (error) => {
|
|
536
|
+
console.error("Error running agent:", error);
|
|
537
|
+
if (!streamClosed) {
|
|
538
|
+
try {
|
|
539
|
+
await writer.close();
|
|
540
|
+
streamClosed = true;
|
|
541
|
+
} catch {
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
},
|
|
545
|
+
complete: async () => {
|
|
546
|
+
if (!streamClosed) {
|
|
547
|
+
try {
|
|
548
|
+
await writer.close();
|
|
549
|
+
streamClosed = true;
|
|
550
|
+
} catch {
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
})().catch((error) => {
|
|
556
|
+
console.error("Error running agent:", error);
|
|
557
|
+
console.error(
|
|
558
|
+
"Error stack:",
|
|
559
|
+
error instanceof Error ? error.stack : "No stack trace"
|
|
560
|
+
);
|
|
561
|
+
console.error("Error details:", {
|
|
562
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
563
|
+
message: error instanceof Error ? error.message : String(error),
|
|
564
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
565
|
+
});
|
|
566
|
+
if (!streamClosed) {
|
|
567
|
+
try {
|
|
568
|
+
writer.close();
|
|
569
|
+
streamClosed = true;
|
|
570
|
+
} catch {
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
return new Response(stream.readable, {
|
|
575
|
+
status: 200,
|
|
576
|
+
headers: {
|
|
577
|
+
"Content-Type": "text/event-stream",
|
|
578
|
+
"Cache-Control": "no-cache",
|
|
579
|
+
Connection: "keep-alive"
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
} catch (error) {
|
|
583
|
+
console.error("Error running agent:", error);
|
|
584
|
+
console.error(
|
|
585
|
+
"Error stack:",
|
|
586
|
+
error instanceof Error ? error.stack : "No stack trace"
|
|
587
|
+
);
|
|
588
|
+
console.error("Error details:", {
|
|
589
|
+
name: error instanceof Error ? error.name : "Unknown",
|
|
590
|
+
message: error instanceof Error ? error.message : String(error),
|
|
591
|
+
cause: error instanceof Error ? error.cause : void 0
|
|
592
|
+
});
|
|
593
|
+
return new Response(
|
|
594
|
+
JSON.stringify({
|
|
595
|
+
error: "Failed to run agent",
|
|
596
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
597
|
+
}),
|
|
598
|
+
{
|
|
599
|
+
status: 500,
|
|
600
|
+
headers: { "Content-Type": "application/json" }
|
|
601
|
+
}
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// src/handlers/handle-stop.ts
|
|
607
|
+
import { EventType as EventType2 } from "@ag-ui/client";
|
|
608
|
+
async function handleStopAgent({
|
|
609
|
+
runtime,
|
|
610
|
+
request,
|
|
611
|
+
agentId,
|
|
612
|
+
threadId
|
|
613
|
+
}) {
|
|
614
|
+
try {
|
|
615
|
+
const agents = await runtime.agents;
|
|
616
|
+
if (!agents[agentId]) {
|
|
617
|
+
return new Response(
|
|
618
|
+
JSON.stringify({
|
|
619
|
+
error: "Agent not found",
|
|
620
|
+
message: `Agent '${agentId}' does not exist`
|
|
621
|
+
}),
|
|
622
|
+
{
|
|
623
|
+
status: 404,
|
|
624
|
+
headers: { "Content-Type": "application/json" }
|
|
625
|
+
}
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
const stopped = await runtime.runner.stop({ threadId });
|
|
629
|
+
if (!stopped) {
|
|
630
|
+
return new Response(
|
|
631
|
+
JSON.stringify({
|
|
632
|
+
stopped: false,
|
|
633
|
+
message: `No active run for thread '${threadId}'.`
|
|
634
|
+
}),
|
|
635
|
+
{
|
|
636
|
+
status: 200,
|
|
637
|
+
headers: { "Content-Type": "application/json" }
|
|
638
|
+
}
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
return new Response(
|
|
642
|
+
JSON.stringify({
|
|
643
|
+
stopped: true,
|
|
644
|
+
interrupt: {
|
|
645
|
+
type: EventType2.RUN_ERROR,
|
|
646
|
+
message: "Run stopped by user",
|
|
647
|
+
code: "STOPPED"
|
|
648
|
+
}
|
|
649
|
+
}),
|
|
650
|
+
{
|
|
651
|
+
status: 200,
|
|
652
|
+
headers: { "Content-Type": "application/json" }
|
|
653
|
+
}
|
|
654
|
+
);
|
|
655
|
+
} catch (error) {
|
|
656
|
+
console.error("Error stopping agent run:", error);
|
|
657
|
+
return new Response(
|
|
658
|
+
JSON.stringify({
|
|
659
|
+
error: "Failed to stop agent",
|
|
660
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
661
|
+
}),
|
|
662
|
+
{
|
|
663
|
+
status: 500,
|
|
664
|
+
headers: { "Content-Type": "application/json" }
|
|
665
|
+
}
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// src/handlers/get-runtime-info.ts
|
|
671
|
+
async function handleGetRuntimeInfo({
|
|
672
|
+
runtime
|
|
673
|
+
}) {
|
|
674
|
+
try {
|
|
675
|
+
const agents = await runtime.agents;
|
|
676
|
+
const agentsDict = Object.entries(agents).reduce(
|
|
677
|
+
(acc, [name, agent]) => {
|
|
678
|
+
acc[name] = {
|
|
679
|
+
name,
|
|
680
|
+
description: agent.description,
|
|
681
|
+
className: agent.constructor.name
|
|
682
|
+
};
|
|
683
|
+
return acc;
|
|
684
|
+
},
|
|
685
|
+
{}
|
|
686
|
+
);
|
|
687
|
+
const runtimeInfo = {
|
|
688
|
+
version: VERSION,
|
|
689
|
+
agents: agentsDict,
|
|
690
|
+
audioFileTranscriptionEnabled: !!runtime.transcriptionService
|
|
691
|
+
};
|
|
692
|
+
return new Response(JSON.stringify(runtimeInfo), {
|
|
693
|
+
status: 200,
|
|
694
|
+
headers: { "Content-Type": "application/json" }
|
|
695
|
+
});
|
|
696
|
+
} catch (error) {
|
|
697
|
+
return new Response(
|
|
698
|
+
JSON.stringify({
|
|
699
|
+
error: "Failed to retrieve runtime information",
|
|
700
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
701
|
+
}),
|
|
702
|
+
{
|
|
703
|
+
status: 500,
|
|
704
|
+
headers: { "Content-Type": "application/json" }
|
|
705
|
+
}
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// src/handlers/handle-transcribe.ts
|
|
711
|
+
async function handleTranscribe({
|
|
712
|
+
runtime,
|
|
713
|
+
request
|
|
714
|
+
}) {
|
|
715
|
+
try {
|
|
716
|
+
if (!runtime.transcriptionService) {
|
|
717
|
+
return new Response(
|
|
718
|
+
JSON.stringify({
|
|
719
|
+
error: "Transcription service not configured",
|
|
720
|
+
message: "No transcription service has been configured in the runtime"
|
|
721
|
+
}),
|
|
722
|
+
{
|
|
723
|
+
status: 503,
|
|
724
|
+
headers: { "Content-Type": "application/json" }
|
|
725
|
+
}
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
const contentType = request.headers.get("content-type");
|
|
729
|
+
if (!contentType || !contentType.includes("multipart/form-data")) {
|
|
730
|
+
return new Response(
|
|
731
|
+
JSON.stringify({
|
|
732
|
+
error: "Invalid content type",
|
|
733
|
+
message: "Request must contain multipart/form-data with an audio file"
|
|
734
|
+
}),
|
|
735
|
+
{
|
|
736
|
+
status: 400,
|
|
737
|
+
headers: { "Content-Type": "application/json" }
|
|
738
|
+
}
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
const formData = await request.formData();
|
|
742
|
+
const audioFile = formData.get("audio");
|
|
743
|
+
if (!audioFile || !(audioFile instanceof File)) {
|
|
744
|
+
return new Response(
|
|
745
|
+
JSON.stringify({
|
|
746
|
+
error: "Missing audio file",
|
|
747
|
+
message: "No audio file found in form data. Please include an 'audio' field."
|
|
748
|
+
}),
|
|
749
|
+
{
|
|
750
|
+
status: 400,
|
|
751
|
+
headers: { "Content-Type": "application/json" }
|
|
752
|
+
}
|
|
753
|
+
);
|
|
754
|
+
}
|
|
755
|
+
const validAudioTypes = [
|
|
756
|
+
"audio/mpeg",
|
|
757
|
+
"audio/mp3",
|
|
758
|
+
"audio/mp4",
|
|
759
|
+
"audio/wav",
|
|
760
|
+
"audio/webm",
|
|
761
|
+
"audio/ogg",
|
|
762
|
+
"audio/flac",
|
|
763
|
+
"audio/aac"
|
|
764
|
+
];
|
|
765
|
+
const isValidType = validAudioTypes.includes(audioFile.type) || audioFile.type === "" || audioFile.type === "application/octet-stream";
|
|
766
|
+
if (!isValidType) {
|
|
767
|
+
return new Response(
|
|
768
|
+
JSON.stringify({
|
|
769
|
+
error: "Invalid file type",
|
|
770
|
+
message: `Unsupported audio file type: ${audioFile.type}. Supported types: ${validAudioTypes.join(", ")}, or files with unknown/empty types`
|
|
771
|
+
}),
|
|
772
|
+
{
|
|
773
|
+
status: 400,
|
|
774
|
+
headers: { "Content-Type": "application/json" }
|
|
775
|
+
}
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
const transcription = await runtime.transcriptionService.transcribeFile({
|
|
779
|
+
audioFile,
|
|
780
|
+
mimeType: audioFile.type,
|
|
781
|
+
size: audioFile.size
|
|
782
|
+
});
|
|
783
|
+
return new Response(
|
|
784
|
+
JSON.stringify({
|
|
785
|
+
text: transcription,
|
|
786
|
+
size: audioFile.size,
|
|
787
|
+
type: audioFile.type
|
|
788
|
+
}),
|
|
789
|
+
{
|
|
790
|
+
status: 200,
|
|
791
|
+
headers: { "Content-Type": "application/json" }
|
|
792
|
+
}
|
|
793
|
+
);
|
|
794
|
+
} catch (error) {
|
|
795
|
+
return new Response(
|
|
796
|
+
JSON.stringify({
|
|
797
|
+
error: "Transcription failed",
|
|
798
|
+
message: error instanceof Error ? error.message : "Unknown error occurred during transcription"
|
|
799
|
+
}),
|
|
800
|
+
{
|
|
801
|
+
status: 500,
|
|
802
|
+
headers: { "Content-Type": "application/json" }
|
|
803
|
+
}
|
|
804
|
+
);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// src/middleware.ts
|
|
809
|
+
import { logger } from "@copilotkitnext/shared";
|
|
810
|
+
async function callBeforeRequestMiddleware({
|
|
811
|
+
runtime,
|
|
812
|
+
request,
|
|
813
|
+
path
|
|
814
|
+
}) {
|
|
815
|
+
const mw = runtime.beforeRequestMiddleware;
|
|
816
|
+
if (!mw) return;
|
|
817
|
+
if (typeof mw === "function") {
|
|
818
|
+
return mw({ runtime, request, path });
|
|
819
|
+
}
|
|
820
|
+
logger.warn({ mw }, "Unsupported beforeRequestMiddleware value \u2013 skipped");
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
async function callAfterRequestMiddleware({
|
|
824
|
+
runtime,
|
|
825
|
+
response,
|
|
826
|
+
path
|
|
827
|
+
}) {
|
|
828
|
+
const mw = runtime.afterRequestMiddleware;
|
|
829
|
+
if (!mw) return;
|
|
830
|
+
if (typeof mw === "function") {
|
|
831
|
+
return mw({ runtime, response, path });
|
|
832
|
+
}
|
|
833
|
+
logger.warn({ mw }, "Unsupported afterRequestMiddleware value \u2013 skipped");
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
// src/endpoints/single-route-helpers.ts
|
|
837
|
+
var METHOD_NAMES = [
|
|
838
|
+
"agent/run",
|
|
839
|
+
"agent/connect",
|
|
840
|
+
"agent/stop",
|
|
841
|
+
"info",
|
|
842
|
+
"transcribe"
|
|
843
|
+
];
|
|
844
|
+
async function parseMethodCall(request) {
|
|
845
|
+
const contentType = request.headers.get("content-type") || "";
|
|
846
|
+
if (!contentType.includes("application/json")) {
|
|
847
|
+
throw createResponseError("Single-route endpoint expects JSON payloads", 415);
|
|
848
|
+
}
|
|
849
|
+
let jsonEnvelope;
|
|
850
|
+
try {
|
|
851
|
+
jsonEnvelope = await request.clone().json();
|
|
852
|
+
} catch (error) {
|
|
853
|
+
throw createResponseError("Invalid JSON payload", 400);
|
|
854
|
+
}
|
|
855
|
+
const method = validateMethod(jsonEnvelope.method);
|
|
856
|
+
return {
|
|
857
|
+
method,
|
|
858
|
+
params: jsonEnvelope.params,
|
|
859
|
+
body: jsonEnvelope.body
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
function expectString(params, key) {
|
|
863
|
+
const value = params?.[key];
|
|
864
|
+
if (typeof value === "string" && value.trim().length > 0) {
|
|
865
|
+
return value;
|
|
866
|
+
}
|
|
867
|
+
throw createResponseError(`Missing or invalid parameter '${key}'`, 400);
|
|
868
|
+
}
|
|
869
|
+
function createJsonRequest(base, body) {
|
|
870
|
+
if (body === void 0 || body === null) {
|
|
871
|
+
throw createResponseError("Missing request body for JSON handler", 400);
|
|
872
|
+
}
|
|
873
|
+
const headers = new Headers(base.headers);
|
|
874
|
+
headers.set("content-type", "application/json");
|
|
875
|
+
headers.delete("content-length");
|
|
876
|
+
const serializedBody = serializeJsonBody(body);
|
|
877
|
+
return new Request(base.url, {
|
|
878
|
+
method: "POST",
|
|
879
|
+
headers,
|
|
880
|
+
body: serializedBody,
|
|
881
|
+
signal: base.signal
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
function createResponseError(message, status) {
|
|
885
|
+
return new Response(
|
|
886
|
+
JSON.stringify({
|
|
887
|
+
error: "invalid_request",
|
|
888
|
+
message
|
|
889
|
+
}),
|
|
890
|
+
{
|
|
891
|
+
status,
|
|
892
|
+
headers: {
|
|
893
|
+
"Content-Type": "application/json"
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
);
|
|
897
|
+
}
|
|
898
|
+
function validateMethod(method) {
|
|
899
|
+
if (!method) {
|
|
900
|
+
throw createResponseError("Missing method field", 400);
|
|
901
|
+
}
|
|
902
|
+
if (METHOD_NAMES.includes(method)) {
|
|
903
|
+
return method;
|
|
904
|
+
}
|
|
905
|
+
throw createResponseError(`Unsupported method '${method}'`, 400);
|
|
906
|
+
}
|
|
907
|
+
function serializeJsonBody(body) {
|
|
908
|
+
if (typeof body === "string") {
|
|
909
|
+
return body;
|
|
910
|
+
}
|
|
911
|
+
if (body instanceof Blob || body instanceof ArrayBuffer || body instanceof Uint8Array) {
|
|
912
|
+
return body;
|
|
913
|
+
}
|
|
914
|
+
if (body instanceof FormData || body instanceof URLSearchParams) {
|
|
915
|
+
return body;
|
|
916
|
+
}
|
|
917
|
+
return JSON.stringify(body);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
export {
|
|
921
|
+
handleRunAgent,
|
|
922
|
+
handleConnectAgent,
|
|
923
|
+
handleStopAgent,
|
|
924
|
+
AgentRunner,
|
|
925
|
+
InMemoryAgentRunner,
|
|
926
|
+
VERSION,
|
|
927
|
+
CopilotRuntime,
|
|
928
|
+
handleGetRuntimeInfo,
|
|
929
|
+
handleTranscribe,
|
|
930
|
+
callBeforeRequestMiddleware,
|
|
931
|
+
callAfterRequestMiddleware,
|
|
932
|
+
parseMethodCall,
|
|
933
|
+
expectString,
|
|
934
|
+
createJsonRequest
|
|
935
|
+
};
|
|
936
|
+
//# sourceMappingURL=chunk-SZFA2SCT.mjs.map
|