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