@copilotkitnext/runtime 0.0.21 → 0.0.22-alpha.0
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 → chunk-LDTC5BLU.mjs} +311 -207
- package/dist/chunk-LDTC5BLU.mjs.map +1 -0
- package/dist/express.d.mts +1 -1
- package/dist/express.d.ts +1 -1
- package/dist/express.js +9 -3
- package/dist/express.js.map +1 -1
- package/dist/express.mjs +1 -1
- package/dist/index.d.mts +64 -5
- package/dist/index.d.ts +64 -5
- package/dist/index.js +320 -208
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3 -1
- package/dist/index.mjs.map +1 -1
- package/dist/{runtime-BEcxV64L.d.mts → runtime-njBGN6lZ.d.mts} +29 -1
- package/dist/{runtime-BEcxV64L.d.ts → runtime-njBGN6lZ.d.ts} +29 -1
- package/package.json +4 -4
- package/dist/chunk-SZFA2SCT.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ 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,
|
|
24
25
|
CopilotRuntime: () => CopilotRuntime,
|
|
25
26
|
InMemoryAgentRunner: () => InMemoryAgentRunner,
|
|
26
27
|
VERSION: () => VERSION,
|
|
@@ -33,7 +34,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
33
34
|
// package.json
|
|
34
35
|
var package_default = {
|
|
35
36
|
name: "@copilotkitnext/runtime",
|
|
36
|
-
version: "0.0.
|
|
37
|
+
version: "0.0.22-alpha.0",
|
|
37
38
|
description: "Server-side runtime package for CopilotKit2",
|
|
38
39
|
main: "dist/index.js",
|
|
39
40
|
types: "dist/index.d.ts",
|
|
@@ -95,244 +96,354 @@ var package_default = {
|
|
|
95
96
|
}
|
|
96
97
|
};
|
|
97
98
|
|
|
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
|
+
|
|
98
105
|
// src/runner/agent-runner.ts
|
|
99
106
|
var AgentRunner = class {
|
|
100
107
|
};
|
|
101
108
|
|
|
102
|
-
// src/runner/
|
|
103
|
-
var
|
|
104
|
-
var import_client = require("@ag-ui/client");
|
|
109
|
+
// src/runner/base.ts
|
|
110
|
+
var import_client2 = require("@ag-ui/client");
|
|
105
111
|
var import_shared = require("@copilotkitnext/shared");
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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 {
|
|
129
|
-
run(request) {
|
|
130
|
-
let existingStore = GLOBAL_STORE.get(request.threadId);
|
|
131
|
-
if (!existingStore) {
|
|
132
|
-
existingStore = new InMemoryEventStore(request.threadId);
|
|
133
|
-
GLOBAL_STORE.set(request.threadId, existingStore);
|
|
134
|
-
}
|
|
135
|
-
const store = existingStore;
|
|
136
|
-
if (store.isRunning) {
|
|
137
|
-
throw new Error("Thread already running");
|
|
138
|
-
}
|
|
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);
|
|
112
|
+
|
|
113
|
+
// src/runner/utils.ts
|
|
114
|
+
var import_client = require("@ag-ui/client");
|
|
115
|
+
function buildHistoricMessageIdIndex(runs) {
|
|
116
|
+
const ids = /* @__PURE__ */ new Set();
|
|
117
|
+
for (const run of runs) {
|
|
118
|
+
for (const event of run.events) {
|
|
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);
|
|
151
125
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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;
|
|
157
158
|
}
|
|
158
159
|
}
|
|
159
160
|
}
|
|
160
161
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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();
|
|
199
|
+
}
|
|
200
|
+
run(request) {
|
|
201
|
+
const { threadId, input, agent, scope } = request;
|
|
202
|
+
if (scope !== void 0) {
|
|
203
|
+
if (!this.threadScope.has(threadId)) this.threadScope.set(threadId, scope ?? null);
|
|
204
|
+
}
|
|
205
|
+
if (this.active.has(threadId)) {
|
|
206
|
+
throw new Error("Thread already running");
|
|
207
|
+
}
|
|
164
208
|
const runSubject = new import_rxjs.ReplaySubject(Infinity);
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const
|
|
168
|
-
|
|
209
|
+
void (async () => {
|
|
210
|
+
this.active.set(threadId, { agent, stopRequested: false });
|
|
211
|
+
const acquired = await this.acquireRun(threadId, input.runId);
|
|
212
|
+
if (!acquired) {
|
|
213
|
+
this.active.delete(threadId);
|
|
214
|
+
runSubject.error(new Error("Thread already running"));
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const currentRunEvents = [];
|
|
218
|
+
const priorRuns = await this.listRuns(threadId);
|
|
219
|
+
const historicIds = buildHistoricMessageIdIndex(priorRuns);
|
|
220
|
+
const onEvent = (event) => {
|
|
221
|
+
let processed = event;
|
|
222
|
+
if (event.type === import_client2.EventType.RUN_STARTED) {
|
|
223
|
+
processed = sanitizeRunStarted(event, input, historicIds);
|
|
224
|
+
}
|
|
225
|
+
runSubject.next(processed);
|
|
226
|
+
currentRunEvents.push(processed);
|
|
227
|
+
this.publishLive(threadId, processed);
|
|
228
|
+
};
|
|
169
229
|
try {
|
|
170
|
-
await
|
|
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
|
-
},
|
|
230
|
+
await agent.runAgent(input, {
|
|
231
|
+
onEvent: ({ event }) => onEvent(event),
|
|
193
232
|
onNewMessage: ({ message }) => {
|
|
194
|
-
if (!seenMessageIds.has(message.id)) {
|
|
195
|
-
seenMessageIds.add(message.id);
|
|
196
|
-
}
|
|
197
233
|
},
|
|
198
234
|
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
|
-
}
|
|
206
235
|
}
|
|
207
236
|
});
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
});
|
|
237
|
+
const ctx = this.active.get(threadId);
|
|
238
|
+
const appended = (0, import_shared.finalizeRunEvents)(currentRunEvents, { stopRequested: ctx?.stopRequested ?? false });
|
|
239
|
+
for (const e of appended) {
|
|
240
|
+
runSubject.next(e);
|
|
241
|
+
this.publishLive(threadId, e);
|
|
224
242
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
} catch {
|
|
234
|
-
const appendedEvents = (0, import_shared.finalizeRunEvents)(currentRunEvents, {
|
|
235
|
-
stopRequested: store.stopRequested
|
|
243
|
+
const parentRunId = priorRuns.at(-1)?.runId ?? null;
|
|
244
|
+
const compacted = (0, import_client2.compactEvents)(currentRunEvents);
|
|
245
|
+
await this.saveRun(threadId, input.runId, compacted, input, parentRunId);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
const ctx = this.active.get(threadId);
|
|
248
|
+
const appended = (0, import_shared.finalizeRunEvents)(currentRunEvents, {
|
|
249
|
+
stopRequested: ctx?.stopRequested ?? false,
|
|
250
|
+
interruptionMessage: error instanceof Error ? error.message : void 0
|
|
236
251
|
});
|
|
237
|
-
for (const
|
|
238
|
-
runSubject.next(
|
|
239
|
-
|
|
252
|
+
for (const e of appended) {
|
|
253
|
+
runSubject.next(e);
|
|
254
|
+
this.publishLive(threadId, e);
|
|
240
255
|
}
|
|
241
|
-
if (
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
runId: store.currentRunId,
|
|
246
|
-
parentRunId,
|
|
247
|
-
events: compactedEvents,
|
|
248
|
-
createdAt: Date.now()
|
|
249
|
-
});
|
|
256
|
+
if (currentRunEvents.length > 0) {
|
|
257
|
+
const parentRunId = priorRuns.at(-1)?.runId ?? null;
|
|
258
|
+
const compacted = (0, import_client2.compactEvents)(currentRunEvents);
|
|
259
|
+
await this.saveRun(threadId, input.runId, compacted, input, parentRunId);
|
|
250
260
|
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
store.stopRequested = false;
|
|
256
|
-
store.isRunning = false;
|
|
261
|
+
} finally {
|
|
262
|
+
await this.releaseRun(threadId);
|
|
263
|
+
this.active.delete(threadId);
|
|
264
|
+
this.completeLive(threadId);
|
|
257
265
|
runSubject.complete();
|
|
258
|
-
nextSubject.complete();
|
|
259
266
|
}
|
|
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();
|
|
267
|
+
})();
|
|
270
268
|
return runSubject.asObservable();
|
|
271
269
|
}
|
|
272
270
|
connect(request) {
|
|
273
|
-
const
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
for (const event of compactedEvents) {
|
|
286
|
-
connectionSubject.next(event);
|
|
287
|
-
if ("messageId" in event && typeof event.messageId === "string") {
|
|
288
|
-
emittedMessageIds.add(event.messageId);
|
|
271
|
+
const { threadId } = request;
|
|
272
|
+
const subject = new import_rxjs.ReplaySubject(Infinity);
|
|
273
|
+
void (async () => {
|
|
274
|
+
const priorRuns = await this.listRuns(threadId);
|
|
275
|
+
const allHistoric = [];
|
|
276
|
+
for (const r of priorRuns) allHistoric.push(...r.events);
|
|
277
|
+
const compacted = (0, import_client2.compactEvents)(allHistoric);
|
|
278
|
+
const emittedIds = /* @__PURE__ */ new Set();
|
|
279
|
+
for (const e of compacted) {
|
|
280
|
+
subject.next(e);
|
|
281
|
+
const id = e.messageId;
|
|
282
|
+
if (typeof id === "string") emittedIds.add(id);
|
|
289
283
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
284
|
+
const running = await this.isRunningState(threadId);
|
|
285
|
+
if (!running) {
|
|
286
|
+
subject.complete();
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const live = this.subscribeLive(threadId);
|
|
290
|
+
let completed = false;
|
|
291
|
+
live.subscribe({
|
|
292
|
+
next: (e) => {
|
|
293
|
+
const id = e.messageId;
|
|
294
|
+
if (typeof id === "string" && emittedIds.has(id)) return;
|
|
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
|
+
}
|
|
296
301
|
}
|
|
297
|
-
connectionSubject.next(event);
|
|
298
302
|
},
|
|
299
|
-
|
|
300
|
-
|
|
303
|
+
error: (err) => subject.error(err),
|
|
304
|
+
complete: () => {
|
|
305
|
+
if (!completed) {
|
|
306
|
+
completed = true;
|
|
307
|
+
subject.complete();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
301
310
|
});
|
|
302
|
-
}
|
|
303
|
-
|
|
311
|
+
})();
|
|
312
|
+
return subject.asObservable();
|
|
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
|
+
}
|
|
304
342
|
}
|
|
305
|
-
|
|
343
|
+
resultThreads.sort((a, b) => b.lastActivityAt - a.lastActivityAt);
|
|
344
|
+
const sliced = resultThreads.slice(offset, offset + limit);
|
|
345
|
+
return { threads: sliced, total: page.total };
|
|
306
346
|
}
|
|
307
|
-
|
|
308
|
-
const
|
|
309
|
-
|
|
347
|
+
async getThreadMetadata(threadId, scope) {
|
|
348
|
+
const runs = await this.listRuns(threadId);
|
|
349
|
+
if (runs.length === 0) return null;
|
|
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
|
+
});
|
|
310
359
|
}
|
|
311
|
-
|
|
312
|
-
const
|
|
313
|
-
if (
|
|
314
|
-
|
|
360
|
+
async deleteThread(threadId, scope) {
|
|
361
|
+
const running = await this.isRunningState(threadId);
|
|
362
|
+
if (running) {
|
|
363
|
+
throw new Error("Cannot delete a running thread");
|
|
315
364
|
}
|
|
316
|
-
|
|
317
|
-
|
|
365
|
+
await this.deleteThreadStorage(threadId, scope);
|
|
366
|
+
if (this.closeLive) {
|
|
367
|
+
await this.closeLive(threadId);
|
|
318
368
|
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
369
|
+
this.threadScope.delete(threadId);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
// src/runner/in-memory-runner.ts
|
|
374
|
+
var InMemoryAgentRunner = class extends AgentRunnerBase {
|
|
375
|
+
state = /* @__PURE__ */ new Map();
|
|
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);
|
|
326
387
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
388
|
+
return s;
|
|
389
|
+
}
|
|
390
|
+
// Hooks implementation
|
|
391
|
+
async acquireRun(threadId, runId) {
|
|
392
|
+
const entry = this.state.get(threadId) ?? { isRunning: false, runId: null };
|
|
393
|
+
if (entry.isRunning) return false;
|
|
394
|
+
entry.isRunning = true;
|
|
395
|
+
entry.runId = runId;
|
|
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 });
|
|
335
420
|
}
|
|
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);
|
|
336
447
|
}
|
|
337
448
|
};
|
|
338
449
|
|
|
@@ -364,7 +475,7 @@ var import_hono = require("hono");
|
|
|
364
475
|
var import_cors = require("hono/cors");
|
|
365
476
|
|
|
366
477
|
// src/handlers/handle-run.ts
|
|
367
|
-
var
|
|
478
|
+
var import_client3 = require("@ag-ui/client");
|
|
368
479
|
var import_encoder = require("@ag-ui/encoder");
|
|
369
480
|
async function handleRunAgent({
|
|
370
481
|
runtime,
|
|
@@ -411,7 +522,7 @@ async function handleRunAgent({
|
|
|
411
522
|
let input;
|
|
412
523
|
try {
|
|
413
524
|
const requestBody = await request.json();
|
|
414
|
-
input =
|
|
525
|
+
input = import_client3.RunAgentInputSchema.parse(requestBody);
|
|
415
526
|
} catch {
|
|
416
527
|
return new Response(
|
|
417
528
|
JSON.stringify({
|
|
@@ -680,7 +791,7 @@ async function callAfterRequestMiddleware({
|
|
|
680
791
|
}
|
|
681
792
|
|
|
682
793
|
// src/handlers/handle-connect.ts
|
|
683
|
-
var
|
|
794
|
+
var import_client4 = require("@ag-ui/client");
|
|
684
795
|
var import_encoder2 = require("@ag-ui/encoder");
|
|
685
796
|
async function handleConnectAgent({
|
|
686
797
|
runtime,
|
|
@@ -709,7 +820,7 @@ async function handleConnectAgent({
|
|
|
709
820
|
let input;
|
|
710
821
|
try {
|
|
711
822
|
const requestBody = await request.json();
|
|
712
|
-
input =
|
|
823
|
+
input = import_client4.RunAgentInputSchema.parse(requestBody);
|
|
713
824
|
} catch {
|
|
714
825
|
return new Response(
|
|
715
826
|
JSON.stringify({
|
|
@@ -804,7 +915,7 @@ async function handleConnectAgent({
|
|
|
804
915
|
}
|
|
805
916
|
|
|
806
917
|
// src/handlers/handle-stop.ts
|
|
807
|
-
var
|
|
918
|
+
var import_client5 = require("@ag-ui/client");
|
|
808
919
|
async function handleStopAgent({
|
|
809
920
|
runtime,
|
|
810
921
|
request,
|
|
@@ -842,7 +953,7 @@ async function handleStopAgent({
|
|
|
842
953
|
JSON.stringify({
|
|
843
954
|
stopped: true,
|
|
844
955
|
interrupt: {
|
|
845
|
-
type:
|
|
956
|
+
type: import_client5.EventType.RUN_ERROR,
|
|
846
957
|
message: "Run stopped by user",
|
|
847
958
|
code: "STOPPED"
|
|
848
959
|
}
|
|
@@ -1183,6 +1294,7 @@ var import_shared5 = require("@copilotkitnext/shared");
|
|
|
1183
1294
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1184
1295
|
0 && (module.exports = {
|
|
1185
1296
|
AgentRunner,
|
|
1297
|
+
AgentRunnerBase,
|
|
1186
1298
|
CopilotRuntime,
|
|
1187
1299
|
InMemoryAgentRunner,
|
|
1188
1300
|
VERSION,
|