@copilotkitnext/runtime 0.0.17-alpha.0 → 0.0.17
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/index.d.mts +26 -7
- package/dist/index.d.ts +26 -7
- package/dist/index.js +284 -161
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +278 -154
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -24,14 +24,15 @@ __export(index_exports, {
|
|
|
24
24
|
CopilotRuntime: () => CopilotRuntime,
|
|
25
25
|
InMemoryAgentRunner: () => InMemoryAgentRunner,
|
|
26
26
|
VERSION: () => VERSION,
|
|
27
|
-
createCopilotEndpoint: () => createCopilotEndpoint
|
|
27
|
+
createCopilotEndpoint: () => createCopilotEndpoint,
|
|
28
|
+
finalizeRunEvents: () => finalizeRunEvents
|
|
28
29
|
});
|
|
29
30
|
module.exports = __toCommonJS(index_exports);
|
|
30
31
|
|
|
31
32
|
// package.json
|
|
32
33
|
var package_default = {
|
|
33
34
|
name: "@copilotkitnext/runtime",
|
|
34
|
-
version: "0.0.17
|
|
35
|
+
version: "0.0.17",
|
|
35
36
|
description: "Server-side runtime package for CopilotKit2",
|
|
36
37
|
main: "dist/index.js",
|
|
37
38
|
types: "dist/index.d.ts",
|
|
@@ -89,7 +90,130 @@ var AgentRunner = class {
|
|
|
89
90
|
|
|
90
91
|
// src/runner/in-memory.ts
|
|
91
92
|
var import_rxjs = require("rxjs");
|
|
93
|
+
var import_client2 = require("@ag-ui/client");
|
|
94
|
+
|
|
95
|
+
// src/runner/finalize-events.ts
|
|
96
|
+
var import_node_crypto = require("crypto");
|
|
92
97
|
var import_client = require("@ag-ui/client");
|
|
98
|
+
var defaultStopMessage = "Run stopped by user";
|
|
99
|
+
var defaultAbruptEndMessage = "Run ended without emitting a terminal event";
|
|
100
|
+
function finalizeRunEvents(events, options = {}) {
|
|
101
|
+
const { stopRequested = false, interruptionMessage } = options;
|
|
102
|
+
const resolvedStopMessage = interruptionMessage ?? defaultStopMessage;
|
|
103
|
+
const resolvedAbruptMessage = interruptionMessage && interruptionMessage !== defaultStopMessage ? interruptionMessage : defaultAbruptEndMessage;
|
|
104
|
+
const appended = [];
|
|
105
|
+
const openMessageIds = /* @__PURE__ */ new Set();
|
|
106
|
+
const openToolCalls = /* @__PURE__ */ new Map();
|
|
107
|
+
for (const event of events) {
|
|
108
|
+
switch (event.type) {
|
|
109
|
+
case import_client.EventType.TEXT_MESSAGE_START: {
|
|
110
|
+
const messageId = event.messageId;
|
|
111
|
+
if (typeof messageId === "string") {
|
|
112
|
+
openMessageIds.add(messageId);
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case import_client.EventType.TEXT_MESSAGE_END: {
|
|
117
|
+
const messageId = event.messageId;
|
|
118
|
+
if (typeof messageId === "string") {
|
|
119
|
+
openMessageIds.delete(messageId);
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case import_client.EventType.TOOL_CALL_START: {
|
|
124
|
+
const toolCallId = event.toolCallId;
|
|
125
|
+
if (typeof toolCallId === "string") {
|
|
126
|
+
openToolCalls.set(toolCallId, {
|
|
127
|
+
hasEnd: false,
|
|
128
|
+
hasResult: false
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case import_client.EventType.TOOL_CALL_END: {
|
|
134
|
+
const toolCallId = event.toolCallId;
|
|
135
|
+
const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
|
|
136
|
+
if (info) {
|
|
137
|
+
info.hasEnd = true;
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
case import_client.EventType.TOOL_CALL_RESULT: {
|
|
142
|
+
const toolCallId = event.toolCallId;
|
|
143
|
+
const info = toolCallId ? openToolCalls.get(toolCallId) : void 0;
|
|
144
|
+
if (info) {
|
|
145
|
+
info.hasResult = true;
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
default:
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const hasRunFinished = events.some((event) => event.type === import_client.EventType.RUN_FINISHED);
|
|
154
|
+
const hasRunError = events.some((event) => event.type === import_client.EventType.RUN_ERROR);
|
|
155
|
+
const hasTerminalEvent = hasRunFinished || hasRunError;
|
|
156
|
+
const terminalEventMissing = !hasTerminalEvent;
|
|
157
|
+
for (const messageId of openMessageIds) {
|
|
158
|
+
const endEvent = {
|
|
159
|
+
type: import_client.EventType.TEXT_MESSAGE_END,
|
|
160
|
+
messageId
|
|
161
|
+
};
|
|
162
|
+
events.push(endEvent);
|
|
163
|
+
appended.push(endEvent);
|
|
164
|
+
}
|
|
165
|
+
for (const [toolCallId, info] of openToolCalls) {
|
|
166
|
+
if (!info.hasEnd) {
|
|
167
|
+
const endEvent = {
|
|
168
|
+
type: import_client.EventType.TOOL_CALL_END,
|
|
169
|
+
toolCallId
|
|
170
|
+
};
|
|
171
|
+
events.push(endEvent);
|
|
172
|
+
appended.push(endEvent);
|
|
173
|
+
}
|
|
174
|
+
if (terminalEventMissing && !info.hasResult) {
|
|
175
|
+
const resultEvent = {
|
|
176
|
+
type: import_client.EventType.TOOL_CALL_RESULT,
|
|
177
|
+
toolCallId,
|
|
178
|
+
messageId: `${toolCallId ?? (0, import_node_crypto.randomUUID)()}-result`,
|
|
179
|
+
role: "tool",
|
|
180
|
+
content: JSON.stringify(
|
|
181
|
+
stopRequested ? {
|
|
182
|
+
status: "stopped",
|
|
183
|
+
reason: "stop_requested",
|
|
184
|
+
message: resolvedStopMessage
|
|
185
|
+
} : {
|
|
186
|
+
status: "error",
|
|
187
|
+
reason: "missing_terminal_event",
|
|
188
|
+
message: resolvedAbruptMessage
|
|
189
|
+
}
|
|
190
|
+
)
|
|
191
|
+
};
|
|
192
|
+
events.push(resultEvent);
|
|
193
|
+
appended.push(resultEvent);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (terminalEventMissing) {
|
|
197
|
+
if (stopRequested) {
|
|
198
|
+
const finishedEvent = {
|
|
199
|
+
type: import_client.EventType.RUN_FINISHED
|
|
200
|
+
};
|
|
201
|
+
events.push(finishedEvent);
|
|
202
|
+
appended.push(finishedEvent);
|
|
203
|
+
} else {
|
|
204
|
+
const errorEvent = {
|
|
205
|
+
type: import_client.EventType.RUN_ERROR,
|
|
206
|
+
message: resolvedAbruptMessage,
|
|
207
|
+
code: "INCOMPLETE_STREAM"
|
|
208
|
+
};
|
|
209
|
+
events.push(errorEvent);
|
|
210
|
+
appended.push(errorEvent);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return appended;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/runner/in-memory.ts
|
|
93
217
|
var InMemoryEventStore = class {
|
|
94
218
|
constructor(threadId) {
|
|
95
219
|
this.threadId = threadId;
|
|
@@ -98,12 +222,18 @@ var InMemoryEventStore = class {
|
|
|
98
222
|
subject = null;
|
|
99
223
|
/** True while a run is actively producing events. */
|
|
100
224
|
isRunning = false;
|
|
101
|
-
/** Lets stop() cancel the current producer. */
|
|
102
|
-
abortController = new AbortController();
|
|
103
225
|
/** Current run ID */
|
|
104
226
|
currentRunId = null;
|
|
105
227
|
/** Historic completed runs */
|
|
106
228
|
historicRuns = [];
|
|
229
|
+
/** Currently running agent instance (if any). */
|
|
230
|
+
agent = null;
|
|
231
|
+
/** Subject returned from run() while the run is active. */
|
|
232
|
+
runSubject = null;
|
|
233
|
+
/** True once stop() has been requested but the run has not yet finalized. */
|
|
234
|
+
stopRequested = false;
|
|
235
|
+
/** Reference to the events emitted in the current run. */
|
|
236
|
+
currentEvents = null;
|
|
107
237
|
};
|
|
108
238
|
var GLOBAL_STORE = /* @__PURE__ */ new Map();
|
|
109
239
|
var InMemoryAgentRunner = class extends AgentRunner {
|
|
@@ -119,15 +249,18 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
119
249
|
}
|
|
120
250
|
store.isRunning = true;
|
|
121
251
|
store.currentRunId = request.input.runId;
|
|
252
|
+
store.agent = request.agent;
|
|
253
|
+
store.stopRequested = false;
|
|
122
254
|
const seenMessageIds = /* @__PURE__ */ new Set();
|
|
123
255
|
const currentRunEvents = [];
|
|
256
|
+
store.currentEvents = currentRunEvents;
|
|
124
257
|
const historicMessageIds = /* @__PURE__ */ new Set();
|
|
125
258
|
for (const run of store.historicRuns) {
|
|
126
259
|
for (const event of run.events) {
|
|
127
260
|
if ("messageId" in event && typeof event.messageId === "string") {
|
|
128
261
|
historicMessageIds.add(event.messageId);
|
|
129
262
|
}
|
|
130
|
-
if (event.type ===
|
|
263
|
+
if (event.type === import_client2.EventType.RUN_STARTED) {
|
|
131
264
|
const runStarted = event;
|
|
132
265
|
const messages = runStarted.input?.messages ?? [];
|
|
133
266
|
for (const message of messages) {
|
|
@@ -139,8 +272,8 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
139
272
|
const nextSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
140
273
|
const prevSubject = store.subject;
|
|
141
274
|
store.subject = nextSubject;
|
|
142
|
-
store.abortController = new AbortController();
|
|
143
275
|
const runSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
276
|
+
store.runSubject = runSubject;
|
|
144
277
|
const runAgent = async () => {
|
|
145
278
|
const lastRun = store.historicRuns[store.historicRuns.length - 1];
|
|
146
279
|
const parentRunId = lastRun?.runId ?? null;
|
|
@@ -148,7 +281,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
148
281
|
await request.agent.runAgent(request.input, {
|
|
149
282
|
onEvent: ({ event }) => {
|
|
150
283
|
let processedEvent = event;
|
|
151
|
-
if (event.type ===
|
|
284
|
+
if (event.type === import_client2.EventType.RUN_STARTED) {
|
|
152
285
|
const runStartedEvent = event;
|
|
153
286
|
if (!runStartedEvent.input) {
|
|
154
287
|
const sanitizedMessages = request.input.messages ? request.input.messages.filter(
|
|
@@ -183,8 +316,15 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
183
316
|
}
|
|
184
317
|
}
|
|
185
318
|
});
|
|
319
|
+
const appendedEvents = finalizeRunEvents(currentRunEvents, {
|
|
320
|
+
stopRequested: store.stopRequested
|
|
321
|
+
});
|
|
322
|
+
for (const event of appendedEvents) {
|
|
323
|
+
runSubject.next(event);
|
|
324
|
+
nextSubject.next(event);
|
|
325
|
+
}
|
|
186
326
|
if (store.currentRunId) {
|
|
187
|
-
const compactedEvents = (0,
|
|
327
|
+
const compactedEvents = (0, import_client2.compactEvents)(currentRunEvents);
|
|
188
328
|
store.historicRuns.push({
|
|
189
329
|
threadId: request.threadId,
|
|
190
330
|
runId: store.currentRunId,
|
|
@@ -193,13 +333,24 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
193
333
|
createdAt: Date.now()
|
|
194
334
|
});
|
|
195
335
|
}
|
|
196
|
-
store.
|
|
336
|
+
store.currentEvents = null;
|
|
197
337
|
store.currentRunId = null;
|
|
338
|
+
store.agent = null;
|
|
339
|
+
store.runSubject = null;
|
|
340
|
+
store.stopRequested = false;
|
|
341
|
+
store.isRunning = false;
|
|
198
342
|
runSubject.complete();
|
|
199
343
|
nextSubject.complete();
|
|
200
344
|
} catch {
|
|
345
|
+
const appendedEvents = finalizeRunEvents(currentRunEvents, {
|
|
346
|
+
stopRequested: store.stopRequested
|
|
347
|
+
});
|
|
348
|
+
for (const event of appendedEvents) {
|
|
349
|
+
runSubject.next(event);
|
|
350
|
+
nextSubject.next(event);
|
|
351
|
+
}
|
|
201
352
|
if (store.currentRunId && currentRunEvents.length > 0) {
|
|
202
|
-
const compactedEvents = (0,
|
|
353
|
+
const compactedEvents = (0, import_client2.compactEvents)(currentRunEvents);
|
|
203
354
|
store.historicRuns.push({
|
|
204
355
|
threadId: request.threadId,
|
|
205
356
|
runId: store.currentRunId,
|
|
@@ -208,8 +359,12 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
208
359
|
createdAt: Date.now()
|
|
209
360
|
});
|
|
210
361
|
}
|
|
211
|
-
store.
|
|
362
|
+
store.currentEvents = null;
|
|
212
363
|
store.currentRunId = null;
|
|
364
|
+
store.agent = null;
|
|
365
|
+
store.runSubject = null;
|
|
366
|
+
store.stopRequested = false;
|
|
367
|
+
store.isRunning = false;
|
|
213
368
|
runSubject.complete();
|
|
214
369
|
nextSubject.complete();
|
|
215
370
|
}
|
|
@@ -236,7 +391,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
236
391
|
for (const run of store.historicRuns) {
|
|
237
392
|
allHistoricEvents.push(...run.events);
|
|
238
393
|
}
|
|
239
|
-
const compactedEvents = (0,
|
|
394
|
+
const compactedEvents = (0, import_client2.compactEvents)(allHistoricEvents);
|
|
240
395
|
const emittedMessageIds = /* @__PURE__ */ new Set();
|
|
241
396
|
for (const event of compactedEvents) {
|
|
242
397
|
connectionSubject.next(event);
|
|
@@ -244,7 +399,7 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
244
399
|
emittedMessageIds.add(event.messageId);
|
|
245
400
|
}
|
|
246
401
|
}
|
|
247
|
-
if (store.subject && store.isRunning) {
|
|
402
|
+
if (store.subject && (store.isRunning || store.stopRequested)) {
|
|
248
403
|
store.subject.subscribe({
|
|
249
404
|
next: (event) => {
|
|
250
405
|
if ("messageId" in event && typeof event.messageId === "string" && emittedMessageIds.has(event.messageId)) {
|
|
@@ -264,9 +419,31 @@ var InMemoryAgentRunner = class extends AgentRunner {
|
|
|
264
419
|
const store = GLOBAL_STORE.get(request.threadId);
|
|
265
420
|
return Promise.resolve(store?.isRunning ?? false);
|
|
266
421
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
422
|
+
stop(request) {
|
|
423
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
424
|
+
if (!store || !store.isRunning) {
|
|
425
|
+
return Promise.resolve(false);
|
|
426
|
+
}
|
|
427
|
+
if (store.stopRequested) {
|
|
428
|
+
return Promise.resolve(false);
|
|
429
|
+
}
|
|
430
|
+
store.stopRequested = true;
|
|
431
|
+
store.isRunning = false;
|
|
432
|
+
const agent = store.agent;
|
|
433
|
+
if (!agent) {
|
|
434
|
+
store.stopRequested = false;
|
|
435
|
+
store.isRunning = false;
|
|
436
|
+
return Promise.resolve(false);
|
|
437
|
+
}
|
|
438
|
+
try {
|
|
439
|
+
agent.abortRun();
|
|
440
|
+
return Promise.resolve(true);
|
|
441
|
+
} catch (error) {
|
|
442
|
+
console.error("Failed to abort agent run", error);
|
|
443
|
+
store.stopRequested = false;
|
|
444
|
+
store.isRunning = true;
|
|
445
|
+
return Promise.resolve(false);
|
|
446
|
+
}
|
|
270
447
|
}
|
|
271
448
|
};
|
|
272
449
|
|
|
@@ -298,7 +475,7 @@ var import_hono = require("hono");
|
|
|
298
475
|
var import_cors = require("hono/cors");
|
|
299
476
|
|
|
300
477
|
// src/handlers/handle-run.ts
|
|
301
|
-
var
|
|
478
|
+
var import_client3 = require("@ag-ui/client");
|
|
302
479
|
var import_encoder = require("@ag-ui/encoder");
|
|
303
480
|
async function handleRunAgent({
|
|
304
481
|
runtime,
|
|
@@ -345,7 +522,7 @@ async function handleRunAgent({
|
|
|
345
522
|
let input;
|
|
346
523
|
try {
|
|
347
524
|
const requestBody = await request.json();
|
|
348
|
-
input =
|
|
525
|
+
input = import_client3.RunAgentInputSchema.parse(requestBody);
|
|
349
526
|
} catch {
|
|
350
527
|
return new Response(
|
|
351
528
|
JSON.stringify({
|
|
@@ -587,9 +764,6 @@ var import_shared2 = require("@copilotkitnext/shared");
|
|
|
587
764
|
|
|
588
765
|
// src/middleware.ts
|
|
589
766
|
var import_shared = require("@copilotkitnext/shared");
|
|
590
|
-
function isMiddlewareURL(value) {
|
|
591
|
-
return typeof value === "string" && /^https?:\/\//.test(value);
|
|
592
|
-
}
|
|
593
767
|
async function callBeforeRequestMiddleware({
|
|
594
768
|
runtime,
|
|
595
769
|
request,
|
|
@@ -600,77 +774,6 @@ async function callBeforeRequestMiddleware({
|
|
|
600
774
|
if (typeof mw === "function") {
|
|
601
775
|
return mw({ runtime, request, path });
|
|
602
776
|
}
|
|
603
|
-
if (isMiddlewareURL(mw)) {
|
|
604
|
-
const clone = request.clone();
|
|
605
|
-
const url = new URL(request.url);
|
|
606
|
-
const headersObj = {};
|
|
607
|
-
clone.headers.forEach((v, k) => {
|
|
608
|
-
headersObj[k] = v;
|
|
609
|
-
});
|
|
610
|
-
let bodyJson = void 0;
|
|
611
|
-
try {
|
|
612
|
-
bodyJson = await clone.json();
|
|
613
|
-
} catch {
|
|
614
|
-
}
|
|
615
|
-
const payload = {
|
|
616
|
-
method: request.method,
|
|
617
|
-
path: url.pathname,
|
|
618
|
-
query: url.search.startsWith("?") ? url.search.slice(1) : url.search,
|
|
619
|
-
headers: headersObj,
|
|
620
|
-
body: bodyJson
|
|
621
|
-
};
|
|
622
|
-
const ac = new AbortController();
|
|
623
|
-
const to = setTimeout(() => ac.abort(), 2e3);
|
|
624
|
-
let res;
|
|
625
|
-
try {
|
|
626
|
-
res = await fetch(mw, {
|
|
627
|
-
method: "POST",
|
|
628
|
-
headers: {
|
|
629
|
-
"content-type": "application/json",
|
|
630
|
-
"X-CopilotKit-Webhook-Stage": "before_request" /* BeforeRequest */
|
|
631
|
-
},
|
|
632
|
-
body: JSON.stringify(payload),
|
|
633
|
-
signal: ac.signal
|
|
634
|
-
});
|
|
635
|
-
} catch {
|
|
636
|
-
clearTimeout(to);
|
|
637
|
-
throw new Response(void 0, { status: 502 });
|
|
638
|
-
}
|
|
639
|
-
clearTimeout(to);
|
|
640
|
-
if (res.status >= 500) {
|
|
641
|
-
throw new Response(void 0, { status: 502 });
|
|
642
|
-
}
|
|
643
|
-
if (res.status >= 400) {
|
|
644
|
-
const errBody = await res.text();
|
|
645
|
-
throw new Response(errBody || null, {
|
|
646
|
-
status: res.status,
|
|
647
|
-
headers: {
|
|
648
|
-
"content-type": res.headers.get("content-type") || "application/json"
|
|
649
|
-
}
|
|
650
|
-
});
|
|
651
|
-
}
|
|
652
|
-
if (res.status === 204) return;
|
|
653
|
-
let json;
|
|
654
|
-
try {
|
|
655
|
-
json = await res.json();
|
|
656
|
-
} catch {
|
|
657
|
-
return;
|
|
658
|
-
}
|
|
659
|
-
if (json && typeof json === "object") {
|
|
660
|
-
const { headers, body } = json;
|
|
661
|
-
const init = {
|
|
662
|
-
method: request.method
|
|
663
|
-
};
|
|
664
|
-
if (headers) {
|
|
665
|
-
init.headers = headers;
|
|
666
|
-
}
|
|
667
|
-
if (body !== void 0 && request.method !== "GET" && request.method !== "HEAD") {
|
|
668
|
-
init.body = JSON.stringify(body);
|
|
669
|
-
}
|
|
670
|
-
return new Request(request.url, init);
|
|
671
|
-
}
|
|
672
|
-
return;
|
|
673
|
-
}
|
|
674
777
|
import_shared.logger.warn({ mw }, "Unsupported beforeRequestMiddleware value \u2013 skipped");
|
|
675
778
|
return;
|
|
676
779
|
}
|
|
@@ -684,50 +787,11 @@ async function callAfterRequestMiddleware({
|
|
|
684
787
|
if (typeof mw === "function") {
|
|
685
788
|
return mw({ runtime, response, path });
|
|
686
789
|
}
|
|
687
|
-
if (isMiddlewareURL(mw)) {
|
|
688
|
-
const clone = response.clone();
|
|
689
|
-
const headersObj = {};
|
|
690
|
-
clone.headers.forEach((v, k) => {
|
|
691
|
-
headersObj[k] = v;
|
|
692
|
-
});
|
|
693
|
-
let body = "";
|
|
694
|
-
try {
|
|
695
|
-
body = await clone.text();
|
|
696
|
-
} catch {
|
|
697
|
-
}
|
|
698
|
-
const payload = {
|
|
699
|
-
status: clone.status,
|
|
700
|
-
headers: headersObj,
|
|
701
|
-
body
|
|
702
|
-
};
|
|
703
|
-
const ac = new AbortController();
|
|
704
|
-
const to = setTimeout(() => ac.abort(), 2e3);
|
|
705
|
-
let res;
|
|
706
|
-
try {
|
|
707
|
-
res = await fetch(mw, {
|
|
708
|
-
method: "POST",
|
|
709
|
-
headers: {
|
|
710
|
-
"content-type": "application/json",
|
|
711
|
-
"X-CopilotKit-Webhook-Stage": "after_request" /* AfterRequest */
|
|
712
|
-
},
|
|
713
|
-
body: JSON.stringify(payload),
|
|
714
|
-
signal: ac.signal
|
|
715
|
-
});
|
|
716
|
-
} finally {
|
|
717
|
-
clearTimeout(to);
|
|
718
|
-
}
|
|
719
|
-
if (!res.ok) {
|
|
720
|
-
throw new Error(
|
|
721
|
-
`after_request webhook ${mw} responded with ${res.status}`
|
|
722
|
-
);
|
|
723
|
-
}
|
|
724
|
-
return;
|
|
725
|
-
}
|
|
726
790
|
import_shared.logger.warn({ mw }, "Unsupported afterRequestMiddleware value \u2013 skipped");
|
|
727
791
|
}
|
|
728
792
|
|
|
729
793
|
// src/handlers/handle-connect.ts
|
|
730
|
-
var
|
|
794
|
+
var import_client4 = require("@ag-ui/client");
|
|
731
795
|
var import_encoder2 = require("@ag-ui/encoder");
|
|
732
796
|
async function handleConnectAgent({
|
|
733
797
|
runtime,
|
|
@@ -756,7 +820,7 @@ async function handleConnectAgent({
|
|
|
756
820
|
let input;
|
|
757
821
|
try {
|
|
758
822
|
const requestBody = await request.json();
|
|
759
|
-
input =
|
|
823
|
+
input = import_client4.RunAgentInputSchema.parse(requestBody);
|
|
760
824
|
} catch {
|
|
761
825
|
return new Response(
|
|
762
826
|
JSON.stringify({
|
|
@@ -850,11 +914,72 @@ async function handleConnectAgent({
|
|
|
850
914
|
}
|
|
851
915
|
}
|
|
852
916
|
|
|
853
|
-
// src/
|
|
854
|
-
|
|
917
|
+
// src/handlers/handle-stop.ts
|
|
918
|
+
var import_client5 = require("@ag-ui/client");
|
|
919
|
+
async function handleStopAgent({
|
|
855
920
|
runtime,
|
|
856
|
-
|
|
921
|
+
request,
|
|
922
|
+
agentId,
|
|
923
|
+
threadId
|
|
857
924
|
}) {
|
|
925
|
+
try {
|
|
926
|
+
const agents = await runtime.agents;
|
|
927
|
+
if (!agents[agentId]) {
|
|
928
|
+
return new Response(
|
|
929
|
+
JSON.stringify({
|
|
930
|
+
error: "Agent not found",
|
|
931
|
+
message: `Agent '${agentId}' does not exist`
|
|
932
|
+
}),
|
|
933
|
+
{
|
|
934
|
+
status: 404,
|
|
935
|
+
headers: { "Content-Type": "application/json" }
|
|
936
|
+
}
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
const stopped = await runtime.runner.stop({ threadId });
|
|
940
|
+
if (!stopped) {
|
|
941
|
+
return new Response(
|
|
942
|
+
JSON.stringify({
|
|
943
|
+
stopped: false,
|
|
944
|
+
message: `No active run for thread '${threadId}'.`
|
|
945
|
+
}),
|
|
946
|
+
{
|
|
947
|
+
status: 200,
|
|
948
|
+
headers: { "Content-Type": "application/json" }
|
|
949
|
+
}
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
return new Response(
|
|
953
|
+
JSON.stringify({
|
|
954
|
+
stopped: true,
|
|
955
|
+
interrupt: {
|
|
956
|
+
type: import_client5.EventType.RUN_ERROR,
|
|
957
|
+
message: "Run stopped by user",
|
|
958
|
+
code: "STOPPED"
|
|
959
|
+
}
|
|
960
|
+
}),
|
|
961
|
+
{
|
|
962
|
+
status: 200,
|
|
963
|
+
headers: { "Content-Type": "application/json" }
|
|
964
|
+
}
|
|
965
|
+
);
|
|
966
|
+
} catch (error) {
|
|
967
|
+
console.error("Error stopping agent run:", error);
|
|
968
|
+
return new Response(
|
|
969
|
+
JSON.stringify({
|
|
970
|
+
error: "Failed to stop agent",
|
|
971
|
+
message: error instanceof Error ? error.message : "Unknown error"
|
|
972
|
+
}),
|
|
973
|
+
{
|
|
974
|
+
status: 500,
|
|
975
|
+
headers: { "Content-Type": "application/json" }
|
|
976
|
+
}
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// src/endpoint.ts
|
|
982
|
+
function createCopilotEndpoint({ runtime, basePath }) {
|
|
858
983
|
const app = new import_hono.Hono();
|
|
859
984
|
return app.basePath(basePath).use(
|
|
860
985
|
"*",
|
|
@@ -876,10 +1001,7 @@ function createCopilotEndpoint({
|
|
|
876
1001
|
c.set("modifiedRequest", maybeModifiedRequest);
|
|
877
1002
|
}
|
|
878
1003
|
} catch (error) {
|
|
879
|
-
import_shared2.logger.error(
|
|
880
|
-
{ err: error, url: request.url, path },
|
|
881
|
-
"Error running before request middleware"
|
|
882
|
-
);
|
|
1004
|
+
import_shared2.logger.error({ err: error, url: request.url, path }, "Error running before request middleware");
|
|
883
1005
|
if (error instanceof Response) {
|
|
884
1006
|
return error;
|
|
885
1007
|
}
|
|
@@ -895,10 +1017,7 @@ function createCopilotEndpoint({
|
|
|
895
1017
|
response,
|
|
896
1018
|
path
|
|
897
1019
|
}).catch((error) => {
|
|
898
|
-
import_shared2.logger.error(
|
|
899
|
-
{ err: error, url: c.req.url, path },
|
|
900
|
-
"Error running after request middleware"
|
|
901
|
-
);
|
|
1020
|
+
import_shared2.logger.error({ err: error, url: c.req.url, path }, "Error running after request middleware");
|
|
902
1021
|
});
|
|
903
1022
|
}).post("/agent/:agentId/run", async (c) => {
|
|
904
1023
|
const agentId = c.req.param("agentId");
|
|
@@ -910,10 +1029,7 @@ function createCopilotEndpoint({
|
|
|
910
1029
|
agentId
|
|
911
1030
|
});
|
|
912
1031
|
} catch (error) {
|
|
913
|
-
import_shared2.logger.error(
|
|
914
|
-
{ err: error, url: request.url, path: c.req.path },
|
|
915
|
-
"Error running request handler"
|
|
916
|
-
);
|
|
1032
|
+
import_shared2.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
917
1033
|
throw error;
|
|
918
1034
|
}
|
|
919
1035
|
}).post("/agent/:agentId/connect", async (c) => {
|
|
@@ -926,10 +1042,22 @@ function createCopilotEndpoint({
|
|
|
926
1042
|
agentId
|
|
927
1043
|
});
|
|
928
1044
|
} catch (error) {
|
|
929
|
-
import_shared2.logger.error(
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
1045
|
+
import_shared2.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
1046
|
+
throw error;
|
|
1047
|
+
}
|
|
1048
|
+
}).post("/agent/:agentId/stop/:threadId", async (c) => {
|
|
1049
|
+
const agentId = c.req.param("agentId");
|
|
1050
|
+
const threadId = c.req.param("threadId");
|
|
1051
|
+
const request = c.get("modifiedRequest") || c.req.raw;
|
|
1052
|
+
try {
|
|
1053
|
+
return await handleStopAgent({
|
|
1054
|
+
runtime,
|
|
1055
|
+
request,
|
|
1056
|
+
agentId,
|
|
1057
|
+
threadId
|
|
1058
|
+
});
|
|
1059
|
+
} catch (error) {
|
|
1060
|
+
import_shared2.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
933
1061
|
throw error;
|
|
934
1062
|
}
|
|
935
1063
|
}).get("/info", async (c) => {
|
|
@@ -940,10 +1068,7 @@ function createCopilotEndpoint({
|
|
|
940
1068
|
request
|
|
941
1069
|
});
|
|
942
1070
|
} catch (error) {
|
|
943
|
-
import_shared2.logger.error(
|
|
944
|
-
{ err: error, url: request.url, path: c.req.path },
|
|
945
|
-
"Error running request handler"
|
|
946
|
-
);
|
|
1071
|
+
import_shared2.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
947
1072
|
throw error;
|
|
948
1073
|
}
|
|
949
1074
|
}).post("/transcribe", async (c) => {
|
|
@@ -954,10 +1079,7 @@ function createCopilotEndpoint({
|
|
|
954
1079
|
request
|
|
955
1080
|
});
|
|
956
1081
|
} catch (error) {
|
|
957
|
-
import_shared2.logger.error(
|
|
958
|
-
{ err: error, url: request.url, path: c.req.path },
|
|
959
|
-
"Error running request handler"
|
|
960
|
-
);
|
|
1082
|
+
import_shared2.logger.error({ err: error, url: request.url, path: c.req.path }, "Error running request handler");
|
|
961
1083
|
throw error;
|
|
962
1084
|
}
|
|
963
1085
|
}).notFound((c) => {
|
|
@@ -970,6 +1092,7 @@ function createCopilotEndpoint({
|
|
|
970
1092
|
CopilotRuntime,
|
|
971
1093
|
InMemoryAgentRunner,
|
|
972
1094
|
VERSION,
|
|
973
|
-
createCopilotEndpoint
|
|
1095
|
+
createCopilotEndpoint,
|
|
1096
|
+
finalizeRunEvents
|
|
974
1097
|
});
|
|
975
1098
|
//# sourceMappingURL=index.js.map
|