@copilotkitnext/sqlite-runner 0.0.22-alpha.0 → 0.0.22-alpha.2
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/.turbo/turbo-build.log +10 -10
- package/dist/index.d.mts +16 -36
- package/dist/index.d.ts +16 -36
- package/dist/index.js +275 -113
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +282 -114
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/sqlite-runner.ts +402 -138
- package/.turbo/turbo-test.log +0 -32
package/src/sqlite-runner.ts
CHANGED
|
@@ -1,39 +1,77 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
AgentRunner,
|
|
3
|
+
finalizeRunEvents,
|
|
4
|
+
type AgentRunnerConnectRequest,
|
|
5
|
+
type AgentRunnerIsRunningRequest,
|
|
6
|
+
type AgentRunnerRunRequest,
|
|
7
|
+
type AgentRunnerStopRequest,
|
|
8
|
+
} from "@copilotkitnext/runtime";
|
|
4
9
|
import { Observable, ReplaySubject } from "rxjs";
|
|
10
|
+
import {
|
|
11
|
+
AbstractAgent,
|
|
12
|
+
BaseEvent,
|
|
13
|
+
RunAgentInput,
|
|
14
|
+
EventType,
|
|
15
|
+
RunStartedEvent,
|
|
16
|
+
compactEvents,
|
|
17
|
+
} from "@ag-ui/client";
|
|
18
|
+
import Database from "better-sqlite3";
|
|
5
19
|
|
|
6
20
|
const SCHEMA_VERSION = 1;
|
|
7
21
|
|
|
22
|
+
interface AgentRunRecord {
|
|
23
|
+
id: number;
|
|
24
|
+
thread_id: string;
|
|
25
|
+
run_id: string;
|
|
26
|
+
parent_run_id: string | null;
|
|
27
|
+
events: BaseEvent[];
|
|
28
|
+
input: RunAgentInput;
|
|
29
|
+
created_at: number;
|
|
30
|
+
version: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
8
33
|
export interface SqliteAgentRunnerOptions {
|
|
9
34
|
dbPath?: string;
|
|
10
35
|
}
|
|
11
|
-
|
|
36
|
+
|
|
37
|
+
interface ActiveConnectionContext {
|
|
38
|
+
subject: ReplaySubject<BaseEvent>;
|
|
39
|
+
agent?: AbstractAgent;
|
|
40
|
+
runSubject?: ReplaySubject<BaseEvent>;
|
|
41
|
+
currentEvents?: BaseEvent[];
|
|
42
|
+
stopRequested?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Active connections for streaming events and stop support
|
|
46
|
+
const ACTIVE_CONNECTIONS = new Map<string, ActiveConnectionContext>();
|
|
47
|
+
|
|
48
|
+
export class SqliteAgentRunner extends AgentRunner {
|
|
12
49
|
private db: any;
|
|
13
|
-
private channels = new Map<string, ReplaySubject<BaseEvent>>();
|
|
14
50
|
|
|
15
51
|
constructor(options: SqliteAgentRunnerOptions = {}) {
|
|
16
52
|
super();
|
|
17
53
|
const dbPath = options.dbPath ?? ":memory:";
|
|
54
|
+
|
|
18
55
|
if (!Database) {
|
|
19
56
|
throw new Error(
|
|
20
57
|
'better-sqlite3 is required for SqliteAgentRunner but was not found.\n' +
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
58
|
+
'Please install it in your project:\n' +
|
|
59
|
+
' npm install better-sqlite3\n' +
|
|
60
|
+
' or\n' +
|
|
61
|
+
' pnpm add better-sqlite3\n' +
|
|
62
|
+
' or\n' +
|
|
63
|
+
' yarn add better-sqlite3\n\n' +
|
|
64
|
+
'If you don\'t need persistence, use InMemoryAgentRunner instead.'
|
|
28
65
|
);
|
|
29
66
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.
|
|
67
|
+
|
|
68
|
+
this.db = new Database(dbPath);
|
|
69
|
+
this.initializeSchema();
|
|
33
70
|
}
|
|
34
71
|
|
|
35
|
-
private
|
|
36
|
-
|
|
72
|
+
private initializeSchema(): void {
|
|
73
|
+
// Create the agent_runs table
|
|
74
|
+
this.db.exec(`
|
|
37
75
|
CREATE TABLE IF NOT EXISTS agent_runs (
|
|
38
76
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
39
77
|
thread_id TEXT NOT NULL,
|
|
@@ -46,7 +84,8 @@ export class SqliteAgentRunner extends AgentRunnerBase {
|
|
|
46
84
|
)
|
|
47
85
|
`);
|
|
48
86
|
|
|
49
|
-
|
|
87
|
+
// Create run_state table to track active runs
|
|
88
|
+
this.db.exec(`
|
|
50
89
|
CREATE TABLE IF NOT EXISTS run_state (
|
|
51
90
|
thread_id TEXT PRIMARY KEY,
|
|
52
91
|
is_running INTEGER DEFAULT 0,
|
|
@@ -55,179 +94,404 @@ export class SqliteAgentRunner extends AgentRunnerBase {
|
|
|
55
94
|
)
|
|
56
95
|
`);
|
|
57
96
|
|
|
58
|
-
|
|
97
|
+
// Create indexes for efficient queries
|
|
98
|
+
this.db.exec(`
|
|
59
99
|
CREATE INDEX IF NOT EXISTS idx_thread_id ON agent_runs(thread_id);
|
|
60
100
|
CREATE INDEX IF NOT EXISTS idx_parent_run_id ON agent_runs(parent_run_id);
|
|
61
101
|
`);
|
|
62
102
|
|
|
63
|
-
|
|
103
|
+
// Create schema version table
|
|
104
|
+
this.db.exec(`
|
|
64
105
|
CREATE TABLE IF NOT EXISTS schema_version (
|
|
65
106
|
version INTEGER PRIMARY KEY,
|
|
66
107
|
applied_at INTEGER NOT NULL
|
|
67
108
|
)
|
|
68
109
|
`);
|
|
69
110
|
|
|
70
|
-
|
|
111
|
+
// Check and set schema version
|
|
112
|
+
const currentVersion = this.db
|
|
71
113
|
.prepare("SELECT version FROM schema_version ORDER BY version DESC LIMIT 1")
|
|
72
114
|
.get() as { version: number } | undefined;
|
|
115
|
+
|
|
73
116
|
if (!currentVersion || currentVersion.version < SCHEMA_VERSION) {
|
|
74
|
-
db
|
|
117
|
+
this.db
|
|
75
118
|
.prepare("INSERT OR REPLACE INTO schema_version (version, applied_at) VALUES (?, ?)")
|
|
76
119
|
.run(SCHEMA_VERSION, Date.now());
|
|
77
120
|
}
|
|
78
121
|
}
|
|
79
122
|
|
|
80
|
-
|
|
81
|
-
protected async acquireRun(threadId: string, runId: string): Promise<boolean> {
|
|
82
|
-
const row = this.db.prepare("SELECT is_running FROM run_state WHERE thread_id = ?").get(threadId) as
|
|
83
|
-
| { is_running: number }
|
|
84
|
-
| undefined;
|
|
85
|
-
if (row?.is_running === 1) return false;
|
|
86
|
-
this.db
|
|
87
|
-
.prepare(
|
|
88
|
-
"INSERT OR REPLACE INTO run_state (thread_id, is_running, current_run_id, updated_at) VALUES (?, ?, ?, ?)",
|
|
89
|
-
)
|
|
90
|
-
.run(threadId, 1, runId, Date.now());
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
protected async releaseRun(threadId: string): Promise<void> {
|
|
95
|
-
this.db
|
|
96
|
-
.prepare("INSERT OR REPLACE INTO run_state (thread_id, is_running, current_run_id, updated_at) VALUES (?, 0, NULL, ?)")
|
|
97
|
-
.run(threadId, Date.now());
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
protected async isRunningState(threadId: string): Promise<boolean> {
|
|
101
|
-
const row = this.db.prepare("SELECT is_running FROM run_state WHERE thread_id = ?").get(threadId) as
|
|
102
|
-
| { is_running: number }
|
|
103
|
-
| undefined;
|
|
104
|
-
return row?.is_running === 1;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
protected async listRuns(threadId: string): Promise<Array<{ runId: string; events: BaseEvent[]; createdAt: number }>> {
|
|
108
|
-
const rows = this.db
|
|
109
|
-
.prepare(
|
|
110
|
-
"SELECT run_id, events, created_at FROM agent_runs WHERE thread_id = ? ORDER BY created_at ASC",
|
|
111
|
-
)
|
|
112
|
-
.all(threadId) as Array<{ run_id: string; events: string; created_at: number }>;
|
|
113
|
-
return rows.map((r) => ({ runId: r.run_id, events: JSON.parse(r.events), createdAt: r.created_at }));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
protected async saveRun(
|
|
123
|
+
private storeRun(
|
|
117
124
|
threadId: string,
|
|
118
125
|
runId: string,
|
|
119
126
|
events: BaseEvent[],
|
|
120
127
|
input: RunAgentInput,
|
|
121
|
-
parentRunId
|
|
122
|
-
):
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
128
|
+
parentRunId?: string | null
|
|
129
|
+
): void {
|
|
130
|
+
// Compact ONLY the events from this run
|
|
131
|
+
const compactedEvents = compactEvents(events);
|
|
132
|
+
|
|
133
|
+
const stmt = this.db.prepare(`
|
|
134
|
+
INSERT INTO agent_runs (thread_id, run_id, parent_run_id, events, input, created_at, version)
|
|
135
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
136
|
+
`);
|
|
137
|
+
|
|
126
138
|
stmt.run(
|
|
127
139
|
threadId,
|
|
128
140
|
runId,
|
|
129
141
|
parentRunId ?? null,
|
|
130
|
-
JSON.stringify(
|
|
142
|
+
JSON.stringify(compactedEvents), // Store only this run's compacted events
|
|
131
143
|
JSON.stringify(input),
|
|
132
144
|
Date.now(),
|
|
133
|
-
SCHEMA_VERSION
|
|
145
|
+
SCHEMA_VERSION
|
|
134
146
|
);
|
|
135
147
|
}
|
|
136
148
|
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
149
|
+
private getHistoricRuns(threadId: string): AgentRunRecord[] {
|
|
150
|
+
const stmt = this.db.prepare(`
|
|
151
|
+
WITH RECURSIVE run_chain AS (
|
|
152
|
+
-- Base case: find the root runs (those without parent)
|
|
153
|
+
SELECT * FROM agent_runs
|
|
154
|
+
WHERE thread_id = ? AND parent_run_id IS NULL
|
|
155
|
+
|
|
156
|
+
UNION ALL
|
|
157
|
+
|
|
158
|
+
-- Recursive case: find children of current level
|
|
159
|
+
SELECT ar.* FROM agent_runs ar
|
|
160
|
+
INNER JOIN run_chain rc ON ar.parent_run_id = rc.run_id
|
|
161
|
+
WHERE ar.thread_id = ?
|
|
146
162
|
)
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
163
|
+
SELECT * FROM run_chain
|
|
164
|
+
ORDER BY created_at ASC
|
|
165
|
+
`);
|
|
150
166
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
167
|
+
const rows = stmt.all(threadId, threadId) as any[];
|
|
168
|
+
|
|
169
|
+
return rows.map(row => ({
|
|
170
|
+
id: row.id,
|
|
171
|
+
thread_id: row.thread_id,
|
|
172
|
+
run_id: row.run_id,
|
|
173
|
+
parent_run_id: row.parent_run_id,
|
|
174
|
+
events: JSON.parse(row.events),
|
|
175
|
+
input: JSON.parse(row.input),
|
|
176
|
+
created_at: row.created_at,
|
|
177
|
+
version: row.version
|
|
178
|
+
}));
|
|
154
179
|
}
|
|
155
180
|
|
|
156
|
-
private
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
181
|
+
private getLatestRunId(threadId: string): string | null {
|
|
182
|
+
const stmt = this.db.prepare(`
|
|
183
|
+
SELECT run_id FROM agent_runs
|
|
184
|
+
WHERE thread_id = ?
|
|
185
|
+
ORDER BY created_at DESC
|
|
186
|
+
LIMIT 1
|
|
187
|
+
`);
|
|
164
188
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
// previous runs' events to new concurrent connections.
|
|
168
|
-
if (event.type === 'RUN_STARTED') {
|
|
169
|
-
const s = new ReplaySubject<BaseEvent>(Infinity);
|
|
170
|
-
this.channels.set(threadId, s);
|
|
171
|
-
}
|
|
172
|
-
this.ensureChannel(threadId).next(event);
|
|
189
|
+
const result = stmt.get(threadId) as { run_id: string } | undefined;
|
|
190
|
+
return result?.run_id ?? null;
|
|
173
191
|
}
|
|
174
192
|
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
|
|
193
|
+
private setRunState(threadId: string, isRunning: boolean, runId?: string): void {
|
|
194
|
+
const stmt = this.db.prepare(`
|
|
195
|
+
INSERT OR REPLACE INTO run_state (thread_id, is_running, current_run_id, updated_at)
|
|
196
|
+
VALUES (?, ?, ?, ?)
|
|
197
|
+
`);
|
|
198
|
+
stmt.run(threadId, isRunning ? 1 : 0, runId ?? null, Date.now());
|
|
178
199
|
}
|
|
179
200
|
|
|
180
|
-
|
|
181
|
-
|
|
201
|
+
private getRunState(threadId: string): { isRunning: boolean; currentRunId: string | null } {
|
|
202
|
+
const stmt = this.db.prepare(`
|
|
203
|
+
SELECT is_running, current_run_id FROM run_state WHERE thread_id = ?
|
|
204
|
+
`);
|
|
205
|
+
const result = stmt.get(threadId) as { is_running: number; current_run_id: string | null } | undefined;
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
isRunning: result?.is_running === 1,
|
|
209
|
+
currentRunId: result?.current_run_id ?? null
|
|
210
|
+
};
|
|
182
211
|
}
|
|
183
212
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
213
|
+
run(request: AgentRunnerRunRequest): Observable<BaseEvent> {
|
|
214
|
+
// Check if thread is already running in database
|
|
215
|
+
const runState = this.getRunState(request.threadId);
|
|
216
|
+
if (runState.isRunning) {
|
|
217
|
+
throw new Error("Thread already running");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Mark thread as running in database
|
|
221
|
+
this.setRunState(request.threadId, true, request.input.runId);
|
|
189
222
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
for (const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
223
|
+
// Track seen message IDs and current run events in memory for this run
|
|
224
|
+
const seenMessageIds = new Set<string>();
|
|
225
|
+
const currentRunEvents: BaseEvent[] = [];
|
|
226
|
+
|
|
227
|
+
// Get all previously seen message IDs from historic runs
|
|
228
|
+
const historicRuns = this.getHistoricRuns(request.threadId);
|
|
229
|
+
const historicMessageIds = new Set<string>();
|
|
230
|
+
for (const run of historicRuns) {
|
|
231
|
+
for (const event of run.events) {
|
|
232
|
+
if ('messageId' in event && typeof event.messageId === 'string') {
|
|
233
|
+
historicMessageIds.add(event.messageId);
|
|
234
|
+
}
|
|
235
|
+
if (event.type === EventType.RUN_STARTED) {
|
|
236
|
+
const runStarted = event as RunStartedEvent;
|
|
237
|
+
const messages = runStarted.input?.messages ?? [];
|
|
238
|
+
for (const message of messages) {
|
|
239
|
+
historicMessageIds.add(message.id);
|
|
240
|
+
}
|
|
203
241
|
}
|
|
204
242
|
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Get or create subject for this thread's connections
|
|
246
|
+
const nextSubject = new ReplaySubject<BaseEvent>(Infinity);
|
|
247
|
+
const prevConnection = ACTIVE_CONNECTIONS.get(request.threadId);
|
|
248
|
+
const prevSubject = prevConnection?.subject;
|
|
249
|
+
|
|
250
|
+
// Create a subject for run() return value
|
|
251
|
+
const runSubject = new ReplaySubject<BaseEvent>(Infinity);
|
|
252
|
+
|
|
253
|
+
// Update the active connection for this thread
|
|
254
|
+
ACTIVE_CONNECTIONS.set(request.threadId, {
|
|
255
|
+
subject: nextSubject,
|
|
256
|
+
agent: request.agent,
|
|
257
|
+
runSubject,
|
|
258
|
+
currentEvents: currentRunEvents,
|
|
259
|
+
stopRequested: false,
|
|
260
|
+
});
|
|
205
261
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
262
|
+
// Helper function to run the agent and handle errors
|
|
263
|
+
const runAgent = async () => {
|
|
264
|
+
// Get parent run ID for chaining
|
|
265
|
+
const parentRunId = this.getLatestRunId(request.threadId);
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
await request.agent.runAgent(request.input, {
|
|
269
|
+
onEvent: ({ event }) => {
|
|
270
|
+
let processedEvent: BaseEvent = event;
|
|
271
|
+
if (event.type === EventType.RUN_STARTED) {
|
|
272
|
+
const runStartedEvent = event as RunStartedEvent;
|
|
273
|
+
if (!runStartedEvent.input) {
|
|
274
|
+
const sanitizedMessages = request.input.messages
|
|
275
|
+
? request.input.messages.filter(
|
|
276
|
+
(message) => !historicMessageIds.has(message.id),
|
|
277
|
+
)
|
|
278
|
+
: undefined;
|
|
279
|
+
const updatedInput = {
|
|
280
|
+
...request.input,
|
|
281
|
+
...(sanitizedMessages !== undefined
|
|
282
|
+
? { messages: sanitizedMessages }
|
|
283
|
+
: {}),
|
|
284
|
+
};
|
|
285
|
+
processedEvent = {
|
|
286
|
+
...runStartedEvent,
|
|
287
|
+
input: updatedInput,
|
|
288
|
+
} as RunStartedEvent;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
runSubject.next(processedEvent); // For run() return - only agent events
|
|
293
|
+
nextSubject.next(processedEvent); // For connect() / store - all events
|
|
294
|
+
currentRunEvents.push(processedEvent); // Accumulate for database storage
|
|
295
|
+
},
|
|
296
|
+
onNewMessage: ({ message }) => {
|
|
297
|
+
// Called for each new message
|
|
298
|
+
if (!seenMessageIds.has(message.id)) {
|
|
299
|
+
seenMessageIds.add(message.id);
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
onRunStartedEvent: () => {
|
|
303
|
+
// Mark input messages as seen without emitting duplicates
|
|
304
|
+
if (request.input.messages) {
|
|
305
|
+
for (const message of request.input.messages) {
|
|
306
|
+
if (!seenMessageIds.has(message.id)) {
|
|
307
|
+
seenMessageIds.add(message.id);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const connection = ACTIVE_CONNECTIONS.get(request.threadId);
|
|
315
|
+
const appendedEvents = finalizeRunEvents(currentRunEvents, {
|
|
316
|
+
stopRequested: connection?.stopRequested ?? false,
|
|
317
|
+
});
|
|
318
|
+
for (const event of appendedEvents) {
|
|
319
|
+
runSubject.next(event);
|
|
320
|
+
nextSubject.next(event);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Store the run in database
|
|
324
|
+
this.storeRun(
|
|
325
|
+
request.threadId,
|
|
326
|
+
request.input.runId,
|
|
327
|
+
currentRunEvents,
|
|
328
|
+
request.input,
|
|
329
|
+
parentRunId
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
// Mark run as complete in database
|
|
333
|
+
this.setRunState(request.threadId, false);
|
|
334
|
+
|
|
335
|
+
if (connection) {
|
|
336
|
+
connection.agent = undefined;
|
|
337
|
+
connection.runSubject = undefined;
|
|
338
|
+
connection.currentEvents = undefined;
|
|
339
|
+
connection.stopRequested = false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Complete the subjects
|
|
343
|
+
runSubject.complete();
|
|
344
|
+
nextSubject.complete();
|
|
345
|
+
|
|
346
|
+
ACTIVE_CONNECTIONS.delete(request.threadId);
|
|
347
|
+
} catch {
|
|
348
|
+
const connection = ACTIVE_CONNECTIONS.get(request.threadId);
|
|
349
|
+
const appendedEvents = finalizeRunEvents(currentRunEvents, {
|
|
350
|
+
stopRequested: connection?.stopRequested ?? false,
|
|
351
|
+
});
|
|
352
|
+
for (const event of appendedEvents) {
|
|
353
|
+
runSubject.next(event);
|
|
354
|
+
nextSubject.next(event);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Store the run even if it failed (partial events)
|
|
358
|
+
if (currentRunEvents.length > 0) {
|
|
359
|
+
this.storeRun(
|
|
360
|
+
request.threadId,
|
|
361
|
+
request.input.runId,
|
|
362
|
+
currentRunEvents,
|
|
363
|
+
request.input,
|
|
364
|
+
parentRunId
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Mark run as complete in database
|
|
369
|
+
this.setRunState(request.threadId, false);
|
|
370
|
+
|
|
371
|
+
if (connection) {
|
|
372
|
+
connection.agent = undefined;
|
|
373
|
+
connection.runSubject = undefined;
|
|
374
|
+
connection.currentEvents = undefined;
|
|
375
|
+
connection.stopRequested = false;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Don't emit error to the subject, just complete it
|
|
379
|
+
// This allows subscribers to get events emitted before the error
|
|
380
|
+
runSubject.complete();
|
|
381
|
+
nextSubject.complete();
|
|
382
|
+
|
|
383
|
+
ACTIVE_CONNECTIONS.delete(request.threadId);
|
|
210
384
|
}
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
// Bridge previous events if they exist
|
|
388
|
+
if (prevSubject) {
|
|
389
|
+
prevSubject.subscribe({
|
|
390
|
+
next: (e) => nextSubject.next(e),
|
|
391
|
+
error: (err) => nextSubject.error(err),
|
|
392
|
+
complete: () => {
|
|
393
|
+
// Don't complete nextSubject here - it needs to stay open for new events
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Start the agent execution immediately (not lazily)
|
|
399
|
+
runAgent();
|
|
400
|
+
|
|
401
|
+
// Return the run subject (only agent events, no injected messages)
|
|
402
|
+
return runSubject.asObservable();
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
connect(request: AgentRunnerConnectRequest): Observable<BaseEvent> {
|
|
406
|
+
const connectionSubject = new ReplaySubject<BaseEvent>(Infinity);
|
|
211
407
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
408
|
+
// Load historic runs from database
|
|
409
|
+
const historicRuns = this.getHistoricRuns(request.threadId);
|
|
410
|
+
|
|
411
|
+
// Collect all historic events from database
|
|
412
|
+
const allHistoricEvents: BaseEvent[] = [];
|
|
413
|
+
for (const run of historicRuns) {
|
|
414
|
+
allHistoricEvents.push(...run.events);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Compact all events together before emitting
|
|
418
|
+
const compactedEvents = compactEvents(allHistoricEvents);
|
|
419
|
+
|
|
420
|
+
// Emit compacted events and track message IDs
|
|
421
|
+
const emittedMessageIds = new Set<string>();
|
|
422
|
+
for (const event of compactedEvents) {
|
|
423
|
+
connectionSubject.next(event);
|
|
424
|
+
if ('messageId' in event && typeof event.messageId === 'string') {
|
|
425
|
+
emittedMessageIds.add(event.messageId);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Bridge active run to connection if exists
|
|
430
|
+
const activeConnection = ACTIVE_CONNECTIONS.get(request.threadId);
|
|
431
|
+
const runState = this.getRunState(request.threadId);
|
|
432
|
+
|
|
433
|
+
if (activeConnection && (runState.isRunning || activeConnection.stopRequested)) {
|
|
434
|
+
activeConnection.subject.subscribe({
|
|
435
|
+
next: (event) => {
|
|
436
|
+
// Skip message events that we've already emitted from historic
|
|
437
|
+
if ('messageId' in event && typeof event.messageId === 'string' && emittedMessageIds.has(event.messageId)) {
|
|
438
|
+
return;
|
|
221
439
|
}
|
|
440
|
+
connectionSubject.next(event);
|
|
222
441
|
},
|
|
223
|
-
|
|
224
|
-
|
|
442
|
+
complete: () => connectionSubject.complete(),
|
|
443
|
+
error: (err) => connectionSubject.error(err)
|
|
225
444
|
});
|
|
226
|
-
}
|
|
227
|
-
|
|
445
|
+
} else {
|
|
446
|
+
// No active run, complete after historic events
|
|
447
|
+
connectionSubject.complete();
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return connectionSubject.asObservable();
|
|
228
451
|
}
|
|
229
452
|
|
|
453
|
+
isRunning(request: AgentRunnerIsRunningRequest): Promise<boolean> {
|
|
454
|
+
const runState = this.getRunState(request.threadId);
|
|
455
|
+
return Promise.resolve(runState.isRunning);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
stop(request: AgentRunnerStopRequest): Promise<boolean | undefined> {
|
|
459
|
+
const runState = this.getRunState(request.threadId);
|
|
460
|
+
if (!runState.isRunning) {
|
|
461
|
+
return Promise.resolve(false);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const connection = ACTIVE_CONNECTIONS.get(request.threadId);
|
|
465
|
+
const agent = connection?.agent;
|
|
466
|
+
|
|
467
|
+
if (!connection || !agent) {
|
|
468
|
+
return Promise.resolve(false);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (connection.stopRequested) {
|
|
472
|
+
return Promise.resolve(false);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
connection.stopRequested = true;
|
|
476
|
+
this.setRunState(request.threadId, false);
|
|
477
|
+
|
|
478
|
+
try {
|
|
479
|
+
agent.abortRun();
|
|
480
|
+
return Promise.resolve(true);
|
|
481
|
+
} catch (error) {
|
|
482
|
+
console.error("Failed to abort sqlite agent run", error);
|
|
483
|
+
connection.stopRequested = false;
|
|
484
|
+
this.setRunState(request.threadId, true);
|
|
485
|
+
return Promise.resolve(false);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Close the database connection (for cleanup)
|
|
491
|
+
*/
|
|
230
492
|
close(): void {
|
|
231
|
-
if (this.db)
|
|
493
|
+
if (this.db) {
|
|
494
|
+
this.db.close();
|
|
495
|
+
}
|
|
232
496
|
}
|
|
233
497
|
}
|
package/.turbo/turbo-test.log
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
> @copilotkitnext/sqlite-runner@0.0.21 test /Users/mme/Projects/CopilotKit2/threads-final-review/packages/sqlite-runner
|
|
4
|
-
> vitest run
|
|
5
|
-
|
|
6
|
-
[?25l
|
|
7
|
-
[1m[46m RUN [49m[22m [36mv3.2.4 [39m[90m/Users/mme/Projects/CopilotKit2/threads-final-review/packages/sqlite-runner[39m
|
|
8
|
-
|
|
9
|
-
[?2026h
|
|
10
|
-
[1m[33m ❯ [39m[22msrc/__tests__/sqlite-runner.test.ts[2m [queued][22m
|
|
11
|
-
|
|
12
|
-
[2m Test Files [22m[1m[32m0 passed[39m[22m[90m (2)[39m
|
|
13
|
-
[2m Tests [22m[1m[32m0 passed[39m[22m[90m (0)[39m
|
|
14
|
-
[2m Start at [22m17:14:55
|
|
15
|
-
[2m Duration [22m311ms
|
|
16
|
-
[?2026l[?2026h[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K
|
|
17
|
-
[1m[33m ❯ [39m[22msrc/__tests__/sqlite-runner.e2e.test.ts[2m [queued][22m
|
|
18
|
-
[1m[33m ❯ [39m[22msrc/__tests__/sqlite-runner.test.ts[2m 0/7[22m
|
|
19
|
-
|
|
20
|
-
[2m Test Files [22m[1m[32m0 passed[39m[22m[90m (2)[39m
|
|
21
|
-
[2m Tests [22m[1m[32m0 passed[39m[22m[90m (7)[39m
|
|
22
|
-
[2m Start at [22m17:14:55
|
|
23
|
-
[2m Duration [22m513ms
|
|
24
|
-
[?2026l[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K[1A[K [32m✓[39m src/__tests__/sqlite-runner.e2e.test.ts [2m([22m[2m8 tests[22m[2m)[22m[32m 22[2mms[22m[39m
|
|
25
|
-
[32m✓[39m src/__tests__/sqlite-runner.test.ts [2m([22m[2m7 tests[22m[2m)[22m[32m 88[2mms[22m[39m
|
|
26
|
-
|
|
27
|
-
[2m Test Files [22m [1m[32m2 passed[39m[22m[90m (2)[39m
|
|
28
|
-
[2m Tests [22m [1m[32m15 passed[39m[22m[90m (15)[39m
|
|
29
|
-
[2m Start at [22m 17:14:55
|
|
30
|
-
[2m Duration [22m 677ms[2m (transform 134ms, setup 0ms, collect 433ms, tests 110ms, environment 0ms, prepare 150ms)[22m
|
|
31
|
-
|
|
32
|
-
[?25h
|