@copilotkitnext/runtime 0.0.22-alpha.0 → 0.0.22-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-LDTC5BLU.mjs → chunk-ELMYXVVN.mjs} +210 -314
- package/dist/chunk-ELMYXVVN.mjs.map +1 -0
- package/dist/express.d.mts +1 -1
- package/dist/express.d.ts +1 -1
- package/dist/express.js +6 -12
- package/dist/express.js.map +1 -1
- package/dist/express.mjs +1 -1
- package/dist/index.d.mts +5 -64
- package/dist/index.d.ts +5 -64
- package/dist/index.js +211 -323
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -3
- package/dist/index.mjs.map +1 -1
- package/dist/{runtime-njBGN6lZ.d.mts → runtime-BEcxV64L.d.mts} +1 -29
- package/dist/{runtime-njBGN6lZ.d.ts → runtime-BEcxV64L.d.ts} +1 -29
- package/package.json +7 -7
- package/dist/chunk-LDTC5BLU.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -21,7 +21,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
AgentRunner: () => AgentRunner,
|
|
24
|
-
AgentRunnerBase: () => AgentRunnerBase,
|
|
25
24
|
CopilotRuntime: () => CopilotRuntime,
|
|
26
25
|
InMemoryAgentRunner: () => InMemoryAgentRunner,
|
|
27
26
|
VERSION: () => VERSION,
|
|
@@ -34,7 +33,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
34
33
|
// package.json
|
|
35
34
|
var package_default = {
|
|
36
35
|
name: "@copilotkitnext/runtime",
|
|
37
|
-
version: "0.0.22-alpha.
|
|
36
|
+
version: "0.0.22-alpha.1",
|
|
38
37
|
description: "Server-side runtime package for CopilotKit2",
|
|
39
38
|
main: "dist/index.js",
|
|
40
39
|
types: "dist/index.d.ts",
|
|
@@ -78,9 +77,9 @@ var package_default = {
|
|
|
78
77
|
vitest: "^3.0.5"
|
|
79
78
|
},
|
|
80
79
|
dependencies: {
|
|
81
|
-
"@ag-ui/client": "0.0.
|
|
82
|
-
"@ag-ui/core": "0.0.
|
|
83
|
-
"@ag-ui/encoder": "0.0.
|
|
80
|
+
"@ag-ui/client": "0.0.41-alpha.0",
|
|
81
|
+
"@ag-ui/core": "0.0.41-alpha.0",
|
|
82
|
+
"@ag-ui/encoder": "0.0.41-alpha.0",
|
|
84
83
|
"@copilotkitnext/shared": "workspace:*",
|
|
85
84
|
cors: "^2.8.5",
|
|
86
85
|
express: "^4.21.2",
|
|
@@ -96,354 +95,244 @@ var package_default = {
|
|
|
96
95
|
}
|
|
97
96
|
};
|
|
98
97
|
|
|
99
|
-
// src/runner/in-memory-runner.ts
|
|
100
|
-
var import_rxjs2 = require("rxjs");
|
|
101
|
-
|
|
102
|
-
// src/runner/base.ts
|
|
103
|
-
var import_rxjs = require("rxjs");
|
|
104
|
-
|
|
105
98
|
// src/runner/agent-runner.ts
|
|
106
99
|
var AgentRunner = class {
|
|
107
100
|
};
|
|
108
101
|
|
|
109
|
-
// src/runner/
|
|
110
|
-
var
|
|
111
|
-
var import_shared = require("@copilotkitnext/shared");
|
|
112
|
-
|
|
113
|
-
// src/runner/utils.ts
|
|
102
|
+
// src/runner/in-memory.ts
|
|
103
|
+
var import_rxjs = require("rxjs");
|
|
114
104
|
var import_client = require("@ag-ui/client");
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const msgId = event.messageId;
|
|
120
|
-
if (typeof msgId === "string") ids.add(msgId);
|
|
121
|
-
if (event.type === import_client.EventType.RUN_STARTED) {
|
|
122
|
-
const e = event;
|
|
123
|
-
for (const m of e.input?.messages ?? []) {
|
|
124
|
-
if (m?.id) ids.add(m.id);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return ids;
|
|
130
|
-
}
|
|
131
|
-
function sanitizeRunStarted(event, input, historicIds) {
|
|
132
|
-
if (event.input) return event;
|
|
133
|
-
const sanitizedMessages = input.messages ? input.messages.filter((m) => !historicIds.has(m.id)) : void 0;
|
|
134
|
-
const updatedInput = {
|
|
135
|
-
...input,
|
|
136
|
-
...sanitizedMessages !== void 0 ? { messages: sanitizedMessages } : {}
|
|
137
|
-
};
|
|
138
|
-
return { ...event, input: updatedInput };
|
|
139
|
-
}
|
|
140
|
-
function deriveThreadMetadata(params) {
|
|
141
|
-
const { threadId, runs, isRunning, resourceId, properties } = params;
|
|
142
|
-
const createdAt = runs.length > 0 ? Math.min(...runs.map((r) => r.createdAt)) : Date.now();
|
|
143
|
-
const lastActivityAt = runs.length > 0 ? Math.max(...runs.map((r) => r.createdAt)) : createdAt;
|
|
144
|
-
let messageCount = 0;
|
|
145
|
-
let firstMessage;
|
|
146
|
-
for (const run of runs) {
|
|
147
|
-
for (const event of run.events) {
|
|
148
|
-
if (event.role && typeof event.content === "string") {
|
|
149
|
-
messageCount += 1;
|
|
150
|
-
if (!firstMessage) firstMessage = event.content;
|
|
151
|
-
}
|
|
152
|
-
if (event.type === import_client.EventType.RUN_STARTED) {
|
|
153
|
-
const e = event;
|
|
154
|
-
for (const m of e.input?.messages ?? []) {
|
|
155
|
-
messageCount += 1;
|
|
156
|
-
if (!firstMessage && typeof m.content === "string") {
|
|
157
|
-
firstMessage = m.content;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return {
|
|
164
|
-
threadId,
|
|
165
|
-
createdAt,
|
|
166
|
-
lastActivityAt,
|
|
167
|
-
isRunning,
|
|
168
|
-
messageCount,
|
|
169
|
-
firstMessage,
|
|
170
|
-
resourceId,
|
|
171
|
-
properties
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
function matchesScope(thread, scope) {
|
|
175
|
-
if (!scope) return true;
|
|
176
|
-
const { resourceId, properties } = scope;
|
|
177
|
-
if (resourceId !== void 0) {
|
|
178
|
-
if (Array.isArray(resourceId)) {
|
|
179
|
-
if (!resourceId.includes(thread.resourceId ?? "")) return false;
|
|
180
|
-
} else {
|
|
181
|
-
if ((thread.resourceId ?? "") !== resourceId) return false;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
if (properties) {
|
|
185
|
-
const t = thread.properties ?? {};
|
|
186
|
-
for (const [k, v] of Object.entries(properties)) {
|
|
187
|
-
if (t[k] !== v) return false;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return true;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// src/runner/base.ts
|
|
194
|
-
var AgentRunnerBase = class extends AgentRunner {
|
|
195
|
-
active = /* @__PURE__ */ new Map();
|
|
196
|
-
threadScope = /* @__PURE__ */ new Map();
|
|
197
|
-
constructor() {
|
|
198
|
-
super();
|
|
105
|
+
var import_shared = require("@copilotkitnext/shared");
|
|
106
|
+
var InMemoryEventStore = class {
|
|
107
|
+
constructor(threadId) {
|
|
108
|
+
this.threadId = threadId;
|
|
199
109
|
}
|
|
110
|
+
/** The subject that current consumers subscribe to. */
|
|
111
|
+
subject = null;
|
|
112
|
+
/** True while a run is actively producing events. */
|
|
113
|
+
isRunning = false;
|
|
114
|
+
/** Current run ID */
|
|
115
|
+
currentRunId = null;
|
|
116
|
+
/** Historic completed runs */
|
|
117
|
+
historicRuns = [];
|
|
118
|
+
/** Currently running agent instance (if any). */
|
|
119
|
+
agent = null;
|
|
120
|
+
/** Subject returned from run() while the run is active. */
|
|
121
|
+
runSubject = null;
|
|
122
|
+
/** True once stop() has been requested but the run has not yet finalized. */
|
|
123
|
+
stopRequested = false;
|
|
124
|
+
/** Reference to the events emitted in the current run. */
|
|
125
|
+
currentEvents = null;
|
|
126
|
+
};
|
|
127
|
+
var GLOBAL_STORE = /* @__PURE__ */ new Map();
|
|
128
|
+
var InMemoryAgentRunner = class extends AgentRunner {
|
|
200
129
|
run(request) {
|
|
201
|
-
|
|
202
|
-
if (
|
|
203
|
-
|
|
130
|
+
let existingStore = GLOBAL_STORE.get(request.threadId);
|
|
131
|
+
if (!existingStore) {
|
|
132
|
+
existingStore = new InMemoryEventStore(request.threadId);
|
|
133
|
+
GLOBAL_STORE.set(request.threadId, existingStore);
|
|
204
134
|
}
|
|
205
|
-
|
|
135
|
+
const store = existingStore;
|
|
136
|
+
if (store.isRunning) {
|
|
206
137
|
throw new Error("Thread already running");
|
|
207
138
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const onEvent = (event) => {
|
|
221
|
-
let processed = event;
|
|
222
|
-
if (event.type === import_client2.EventType.RUN_STARTED) {
|
|
223
|
-
processed = sanitizeRunStarted(event, input, historicIds);
|
|
139
|
+
store.isRunning = true;
|
|
140
|
+
store.currentRunId = request.input.runId;
|
|
141
|
+
store.agent = request.agent;
|
|
142
|
+
store.stopRequested = false;
|
|
143
|
+
const seenMessageIds = /* @__PURE__ */ new Set();
|
|
144
|
+
const currentRunEvents = [];
|
|
145
|
+
store.currentEvents = currentRunEvents;
|
|
146
|
+
const historicMessageIds = /* @__PURE__ */ new Set();
|
|
147
|
+
for (const run of store.historicRuns) {
|
|
148
|
+
for (const event of run.events) {
|
|
149
|
+
if ("messageId" in event && typeof event.messageId === "string") {
|
|
150
|
+
historicMessageIds.add(event.messageId);
|
|
224
151
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
152
|
+
if (event.type === import_client.EventType.RUN_STARTED) {
|
|
153
|
+
const runStarted = event;
|
|
154
|
+
const messages = runStarted.input?.messages ?? [];
|
|
155
|
+
for (const message of messages) {
|
|
156
|
+
historicMessageIds.add(message.id);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const nextSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
162
|
+
const prevSubject = store.subject;
|
|
163
|
+
store.subject = nextSubject;
|
|
164
|
+
const runSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
165
|
+
store.runSubject = runSubject;
|
|
166
|
+
const runAgent = async () => {
|
|
167
|
+
const lastRun = store.historicRuns[store.historicRuns.length - 1];
|
|
168
|
+
const parentRunId = lastRun?.runId ?? null;
|
|
229
169
|
try {
|
|
230
|
-
await agent.runAgent(input, {
|
|
231
|
-
onEvent: ({ event }) =>
|
|
170
|
+
await request.agent.runAgent(request.input, {
|
|
171
|
+
onEvent: ({ event }) => {
|
|
172
|
+
let processedEvent = event;
|
|
173
|
+
if (event.type === import_client.EventType.RUN_STARTED) {
|
|
174
|
+
const runStartedEvent = event;
|
|
175
|
+
if (!runStartedEvent.input) {
|
|
176
|
+
const sanitizedMessages = request.input.messages ? request.input.messages.filter(
|
|
177
|
+
(message) => !historicMessageIds.has(message.id)
|
|
178
|
+
) : void 0;
|
|
179
|
+
const updatedInput = {
|
|
180
|
+
...request.input,
|
|
181
|
+
...sanitizedMessages !== void 0 ? { messages: sanitizedMessages } : {}
|
|
182
|
+
};
|
|
183
|
+
processedEvent = {
|
|
184
|
+
...runStartedEvent,
|
|
185
|
+
input: updatedInput
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
runSubject.next(processedEvent);
|
|
190
|
+
nextSubject.next(processedEvent);
|
|
191
|
+
currentRunEvents.push(processedEvent);
|
|
192
|
+
},
|
|
232
193
|
onNewMessage: ({ message }) => {
|
|
194
|
+
if (!seenMessageIds.has(message.id)) {
|
|
195
|
+
seenMessageIds.add(message.id);
|
|
196
|
+
}
|
|
233
197
|
},
|
|
234
198
|
onRunStartedEvent: () => {
|
|
199
|
+
if (request.input.messages) {
|
|
200
|
+
for (const message of request.input.messages) {
|
|
201
|
+
if (!seenMessageIds.has(message.id)) {
|
|
202
|
+
seenMessageIds.add(message.id);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
235
206
|
}
|
|
236
207
|
});
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
208
|
+
const appendedEvents = (0, import_shared.finalizeRunEvents)(currentRunEvents, {
|
|
209
|
+
stopRequested: store.stopRequested
|
|
210
|
+
});
|
|
211
|
+
for (const event of appendedEvents) {
|
|
212
|
+
runSubject.next(event);
|
|
213
|
+
nextSubject.next(event);
|
|
214
|
+
}
|
|
215
|
+
if (store.currentRunId) {
|
|
216
|
+
const compactedEvents = (0, import_client.compactEvents)(currentRunEvents);
|
|
217
|
+
store.historicRuns.push({
|
|
218
|
+
threadId: request.threadId,
|
|
219
|
+
runId: store.currentRunId,
|
|
220
|
+
parentRunId,
|
|
221
|
+
events: compactedEvents,
|
|
222
|
+
createdAt: Date.now()
|
|
223
|
+
});
|
|
242
224
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
225
|
+
store.currentEvents = null;
|
|
226
|
+
store.currentRunId = null;
|
|
227
|
+
store.agent = null;
|
|
228
|
+
store.runSubject = null;
|
|
229
|
+
store.stopRequested = false;
|
|
230
|
+
store.isRunning = false;
|
|
231
|
+
runSubject.complete();
|
|
232
|
+
nextSubject.complete();
|
|
233
|
+
} catch {
|
|
234
|
+
const appendedEvents = (0, import_shared.finalizeRunEvents)(currentRunEvents, {
|
|
235
|
+
stopRequested: store.stopRequested
|
|
251
236
|
});
|
|
252
|
-
for (const
|
|
253
|
-
runSubject.next(
|
|
254
|
-
|
|
237
|
+
for (const event of appendedEvents) {
|
|
238
|
+
runSubject.next(event);
|
|
239
|
+
nextSubject.next(event);
|
|
255
240
|
}
|
|
256
|
-
if (currentRunEvents.length > 0) {
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
241
|
+
if (store.currentRunId && currentRunEvents.length > 0) {
|
|
242
|
+
const compactedEvents = (0, import_client.compactEvents)(currentRunEvents);
|
|
243
|
+
store.historicRuns.push({
|
|
244
|
+
threadId: request.threadId,
|
|
245
|
+
runId: store.currentRunId,
|
|
246
|
+
parentRunId,
|
|
247
|
+
events: compactedEvents,
|
|
248
|
+
createdAt: Date.now()
|
|
249
|
+
});
|
|
260
250
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
251
|
+
store.currentEvents = null;
|
|
252
|
+
store.currentRunId = null;
|
|
253
|
+
store.agent = null;
|
|
254
|
+
store.runSubject = null;
|
|
255
|
+
store.stopRequested = false;
|
|
256
|
+
store.isRunning = false;
|
|
265
257
|
runSubject.complete();
|
|
258
|
+
nextSubject.complete();
|
|
266
259
|
}
|
|
267
|
-
}
|
|
260
|
+
};
|
|
261
|
+
if (prevSubject) {
|
|
262
|
+
prevSubject.subscribe({
|
|
263
|
+
next: (e) => nextSubject.next(e),
|
|
264
|
+
error: (err) => nextSubject.error(err),
|
|
265
|
+
complete: () => {
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
runAgent();
|
|
268
270
|
return runSubject.asObservable();
|
|
269
271
|
}
|
|
270
272
|
connect(request) {
|
|
271
|
-
const
|
|
272
|
-
const
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
if (
|
|
286
|
-
|
|
287
|
-
return;
|
|
273
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
274
|
+
const connectionSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
275
|
+
if (!store) {
|
|
276
|
+
connectionSubject.complete();
|
|
277
|
+
return connectionSubject.asObservable();
|
|
278
|
+
}
|
|
279
|
+
const allHistoricEvents = [];
|
|
280
|
+
for (const run of store.historicRuns) {
|
|
281
|
+
allHistoricEvents.push(...run.events);
|
|
282
|
+
}
|
|
283
|
+
const compactedEvents = (0, import_client.compactEvents)(allHistoricEvents);
|
|
284
|
+
const emittedMessageIds = /* @__PURE__ */ new Set();
|
|
285
|
+
for (const event of compactedEvents) {
|
|
286
|
+
connectionSubject.next(event);
|
|
287
|
+
if ("messageId" in event && typeof event.messageId === "string") {
|
|
288
|
+
emittedMessageIds.add(event.messageId);
|
|
288
289
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
next: (
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
subject.next(e);
|
|
296
|
-
if (e.type === import_client2.EventType.RUN_FINISHED || e.type === import_client2.EventType.RUN_ERROR) {
|
|
297
|
-
if (!completed) {
|
|
298
|
-
completed = true;
|
|
299
|
-
subject.complete();
|
|
300
|
-
}
|
|
290
|
+
}
|
|
291
|
+
if (store.subject && (store.isRunning || store.stopRequested)) {
|
|
292
|
+
store.subject.subscribe({
|
|
293
|
+
next: (event) => {
|
|
294
|
+
if ("messageId" in event && typeof event.messageId === "string" && emittedMessageIds.has(event.messageId)) {
|
|
295
|
+
return;
|
|
301
296
|
}
|
|
297
|
+
connectionSubject.next(event);
|
|
302
298
|
},
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
if (!completed) {
|
|
306
|
-
completed = true;
|
|
307
|
-
subject.complete();
|
|
308
|
-
}
|
|
309
|
-
}
|
|
299
|
+
complete: () => connectionSubject.complete(),
|
|
300
|
+
error: (err) => connectionSubject.error(err)
|
|
310
301
|
});
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
}
|
|
314
|
-
async isRunning(request) {
|
|
315
|
-
return this.isRunningState(request.threadId);
|
|
316
|
-
}
|
|
317
|
-
async stop(request) {
|
|
318
|
-
const ctx = this.active.get(request.threadId);
|
|
319
|
-
if (!ctx?.agent) return false;
|
|
320
|
-
if (ctx.stopRequested) return false;
|
|
321
|
-
ctx.stopRequested = true;
|
|
322
|
-
await this.releaseRun(request.threadId);
|
|
323
|
-
try {
|
|
324
|
-
ctx.agent.abortRun();
|
|
325
|
-
return true;
|
|
326
|
-
} catch {
|
|
327
|
-
ctx.stopRequested = false;
|
|
328
|
-
return false;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
async listThreads(request) {
|
|
332
|
-
const limit = request.limit ?? 20;
|
|
333
|
-
const offset = request.offset ?? 0;
|
|
334
|
-
const page = await this.pageThreads({ scope: request.scope, limit: limit + offset, offset: 0 });
|
|
335
|
-
const resultThreads = [];
|
|
336
|
-
for (const id of page.threadIds) {
|
|
337
|
-
const md = await this.getThreadMetadata(id, request.scope);
|
|
338
|
-
if (!md) continue;
|
|
339
|
-
if (request.scope ? matchesScope(md, request.scope) : true) {
|
|
340
|
-
resultThreads.push(md);
|
|
341
|
-
}
|
|
302
|
+
} else {
|
|
303
|
+
connectionSubject.complete();
|
|
342
304
|
}
|
|
343
|
-
|
|
344
|
-
const sliced = resultThreads.slice(offset, offset + limit);
|
|
345
|
-
return { threads: sliced, total: page.total };
|
|
305
|
+
return connectionSubject.asObservable();
|
|
346
306
|
}
|
|
347
|
-
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
const isRunning = await this.isRunningState(threadId);
|
|
351
|
-
const s = this.threadScope.get(threadId) ?? null;
|
|
352
|
-
return deriveThreadMetadata({
|
|
353
|
-
threadId,
|
|
354
|
-
runs,
|
|
355
|
-
isRunning,
|
|
356
|
-
resourceId: s?.resourceId ?? void 0,
|
|
357
|
-
properties: s?.properties
|
|
358
|
-
});
|
|
307
|
+
isRunning(request) {
|
|
308
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
309
|
+
return Promise.resolve(store?.isRunning ?? false);
|
|
359
310
|
}
|
|
360
|
-
|
|
361
|
-
const
|
|
362
|
-
if (
|
|
363
|
-
|
|
311
|
+
stop(request) {
|
|
312
|
+
const store = GLOBAL_STORE.get(request.threadId);
|
|
313
|
+
if (!store || !store.isRunning) {
|
|
314
|
+
return Promise.resolve(false);
|
|
364
315
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
await this.closeLive(threadId);
|
|
316
|
+
if (store.stopRequested) {
|
|
317
|
+
return Promise.resolve(false);
|
|
368
318
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
runs = /* @__PURE__ */ new Map();
|
|
377
|
-
channels = /* @__PURE__ */ new Map();
|
|
378
|
-
constructor() {
|
|
379
|
-
super();
|
|
380
|
-
}
|
|
381
|
-
// Live channel helpers
|
|
382
|
-
ensureChannel(threadId) {
|
|
383
|
-
let s = this.channels.get(threadId);
|
|
384
|
-
if (!s || s.closed) {
|
|
385
|
-
s = new import_rxjs2.ReplaySubject(Infinity);
|
|
386
|
-
this.channels.set(threadId, s);
|
|
319
|
+
store.stopRequested = true;
|
|
320
|
+
store.isRunning = false;
|
|
321
|
+
const agent = store.agent;
|
|
322
|
+
if (!agent) {
|
|
323
|
+
store.stopRequested = false;
|
|
324
|
+
store.isRunning = false;
|
|
325
|
+
return Promise.resolve(false);
|
|
387
326
|
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
this.state.set(threadId, entry);
|
|
397
|
-
return true;
|
|
398
|
-
}
|
|
399
|
-
async releaseRun(threadId) {
|
|
400
|
-
const entry = this.state.get(threadId) ?? { isRunning: false, runId: null };
|
|
401
|
-
entry.isRunning = false;
|
|
402
|
-
this.state.set(threadId, entry);
|
|
403
|
-
}
|
|
404
|
-
async isRunningState(threadId) {
|
|
405
|
-
return this.state.get(threadId)?.isRunning ?? false;
|
|
406
|
-
}
|
|
407
|
-
async listRuns(threadId) {
|
|
408
|
-
return this.runs.get(threadId) ?? [];
|
|
409
|
-
}
|
|
410
|
-
async saveRun(threadId, runId, events, input, parentRunId) {
|
|
411
|
-
const list = this.runs.get(threadId) ?? [];
|
|
412
|
-
list.push({ runId, events, createdAt: Date.now() });
|
|
413
|
-
this.runs.set(threadId, list);
|
|
414
|
-
}
|
|
415
|
-
async pageThreads(params) {
|
|
416
|
-
const all = [];
|
|
417
|
-
for (const [id, arr] of this.runs.entries()) {
|
|
418
|
-
const last = arr.length > 0 ? Math.max(...arr.map((r) => r.createdAt)) : 0;
|
|
419
|
-
all.push({ id, last });
|
|
327
|
+
try {
|
|
328
|
+
agent.abortRun();
|
|
329
|
+
return Promise.resolve(true);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
console.error("Failed to abort agent run", error);
|
|
332
|
+
store.stopRequested = false;
|
|
333
|
+
store.isRunning = true;
|
|
334
|
+
return Promise.resolve(false);
|
|
420
335
|
}
|
|
421
|
-
all.sort((a, b) => b.last - a.last);
|
|
422
|
-
const total = all.length;
|
|
423
|
-
const offset = params.offset ?? 0;
|
|
424
|
-
const limit = params.limit ?? 20;
|
|
425
|
-
const ids = all.slice(offset, offset + limit).map((x) => x.id);
|
|
426
|
-
return { threadIds: ids, total };
|
|
427
|
-
}
|
|
428
|
-
async deleteThreadStorage(threadId) {
|
|
429
|
-
this.runs.delete(threadId);
|
|
430
|
-
this.state.delete(threadId);
|
|
431
|
-
}
|
|
432
|
-
publishLive(threadId, event) {
|
|
433
|
-
const s = this.ensureChannel(threadId);
|
|
434
|
-
s.next(event);
|
|
435
|
-
}
|
|
436
|
-
completeLive(threadId) {
|
|
437
|
-
const s = this.ensureChannel(threadId);
|
|
438
|
-
if (!s.closed) s.complete();
|
|
439
|
-
}
|
|
440
|
-
subscribeLive(threadId) {
|
|
441
|
-
return this.ensureChannel(threadId).asObservable();
|
|
442
|
-
}
|
|
443
|
-
async closeLive(threadId) {
|
|
444
|
-
const s = this.channels.get(threadId);
|
|
445
|
-
if (s && !s.closed) s.complete();
|
|
446
|
-
this.channels.delete(threadId);
|
|
447
336
|
}
|
|
448
337
|
};
|
|
449
338
|
|
|
@@ -475,7 +364,7 @@ var import_hono = require("hono");
|
|
|
475
364
|
var import_cors = require("hono/cors");
|
|
476
365
|
|
|
477
366
|
// src/handlers/handle-run.ts
|
|
478
|
-
var
|
|
367
|
+
var import_client2 = require("@ag-ui/client");
|
|
479
368
|
var import_encoder = require("@ag-ui/encoder");
|
|
480
369
|
async function handleRunAgent({
|
|
481
370
|
runtime,
|
|
@@ -522,7 +411,7 @@ async function handleRunAgent({
|
|
|
522
411
|
let input;
|
|
523
412
|
try {
|
|
524
413
|
const requestBody = await request.json();
|
|
525
|
-
input =
|
|
414
|
+
input = import_client2.RunAgentInputSchema.parse(requestBody);
|
|
526
415
|
} catch {
|
|
527
416
|
return new Response(
|
|
528
417
|
JSON.stringify({
|
|
@@ -791,7 +680,7 @@ async function callAfterRequestMiddleware({
|
|
|
791
680
|
}
|
|
792
681
|
|
|
793
682
|
// src/handlers/handle-connect.ts
|
|
794
|
-
var
|
|
683
|
+
var import_client3 = require("@ag-ui/client");
|
|
795
684
|
var import_encoder2 = require("@ag-ui/encoder");
|
|
796
685
|
async function handleConnectAgent({
|
|
797
686
|
runtime,
|
|
@@ -820,7 +709,7 @@ async function handleConnectAgent({
|
|
|
820
709
|
let input;
|
|
821
710
|
try {
|
|
822
711
|
const requestBody = await request.json();
|
|
823
|
-
input =
|
|
712
|
+
input = import_client3.RunAgentInputSchema.parse(requestBody);
|
|
824
713
|
} catch {
|
|
825
714
|
return new Response(
|
|
826
715
|
JSON.stringify({
|
|
@@ -915,7 +804,7 @@ async function handleConnectAgent({
|
|
|
915
804
|
}
|
|
916
805
|
|
|
917
806
|
// src/handlers/handle-stop.ts
|
|
918
|
-
var
|
|
807
|
+
var import_client4 = require("@ag-ui/client");
|
|
919
808
|
async function handleStopAgent({
|
|
920
809
|
runtime,
|
|
921
810
|
request,
|
|
@@ -953,7 +842,7 @@ async function handleStopAgent({
|
|
|
953
842
|
JSON.stringify({
|
|
954
843
|
stopped: true,
|
|
955
844
|
interrupt: {
|
|
956
|
-
type:
|
|
845
|
+
type: import_client4.EventType.RUN_ERROR,
|
|
957
846
|
message: "Run stopped by user",
|
|
958
847
|
code: "STOPPED"
|
|
959
848
|
}
|
|
@@ -1294,7 +1183,6 @@ var import_shared5 = require("@copilotkitnext/shared");
|
|
|
1294
1183
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1295
1184
|
0 && (module.exports = {
|
|
1296
1185
|
AgentRunner,
|
|
1297
|
-
AgentRunnerBase,
|
|
1298
1186
|
CopilotRuntime,
|
|
1299
1187
|
InMemoryAgentRunner,
|
|
1300
1188
|
VERSION,
|