@myrialabs/clopen 0.4.30 → 0.4.32
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/backend/auth/permissions.test.ts +0 -1
- package/backend/auth/permissions.ts +0 -4
- package/backend/chat/stream-manager.ts +28 -12
- package/backend/database/migrations/068_create_engine_config_revision.test.ts +121 -0
- package/backend/database/migrations/068_create_engine_config_revision.ts +117 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/engine/README.md +16 -0
- package/backend/engine/adapters/claude/stream.ts +89 -49
- package/backend/engine/adapters/cline/stream.ts +62 -24
- package/backend/engine/adapters/codex/stream.ts +51 -21
- package/backend/engine/adapters/copilot/stream.ts +96 -52
- package/backend/engine/adapters/cursor/stream.ts +74 -34
- package/backend/engine/adapters/opencode/server.ts +512 -126
- package/backend/engine/adapters/opencode/stream.ts +131 -79
- package/backend/engine/adapters/pi/stream.ts +63 -24
- package/backend/engine/adapters/qwen/stream.ts +91 -58
- package/backend/engine/adapters/run-registry.test.ts +75 -0
- package/backend/engine/adapters/run-registry.ts +63 -0
- package/backend/engine/bootstrap-default-engine.ts +67 -38
- package/backend/engine/config-revision.ts +150 -0
- package/backend/engine/docs/adapter-contract.md +40 -9
- package/backend/engine/docs/adding-an-engine.md +8 -0
- package/backend/engine/docs/frontend-and-chat.md +39 -0
- package/backend/engine/docs/lessons-learned.md +70 -0
- package/backend/engine/docs/quick-reference.md +3 -1
- package/backend/engine/engine-cli.ts +313 -0
- package/backend/engine/engine-setup.ts +40 -3
- package/backend/engine/hot-reload.ts +99 -0
- package/backend/engine/index.ts +96 -24
- package/backend/engine/install-recipes.test.ts +99 -5
- package/backend/engine/install-recipes.ts +47 -11
- package/backend/engine/install-runner.ts +34 -16
- package/backend/engine/stack-project.ts +72 -0
- package/backend/engine/types.ts +17 -5
- package/backend/git/git-executor.ts +22 -3
- package/backend/git/git-service.ts +1 -1
- package/backend/git/nested-repos.test.ts +121 -0
- package/backend/git/nested-repos.ts +301 -0
- package/backend/index.ts +7 -0
- package/backend/snapshot/gitignore.ts +10 -174
- package/backend/utils/path-enrich.ts +2 -0
- package/backend/ws/chat/stream.ts +24 -0
- package/backend/ws/engine/claude/accounts.ts +9 -4
- package/backend/ws/engine/codex/accounts.ts +8 -17
- package/backend/ws/engine/codex/status.ts +10 -17
- package/backend/ws/engine/copilot/accounts.ts +0 -10
- package/backend/ws/engine/cursor/accounts.ts +0 -10
- package/backend/ws/engine/index.ts +17 -2
- package/backend/ws/engine/opencode/providers.ts +0 -52
- package/backend/ws/files/read.ts +1 -1
- package/backend/ws/git/conflict.ts +1 -1
- package/backend/ws/git/diff.ts +1 -1
- package/backend/ws/git/staging.ts +1 -1
- package/backend/ws/git/status.ts +1 -1
- package/backend/ws/settings/crud.ts +1 -1
- package/backend/ws/stack/access.ts +5 -2
- package/dist/assets/{index-DYwQ5bP8.js → index-DSvGoxp2.js} +1 -1
- package/dist/assets/{index-DUc5T21e.js → index-hu17G6tX.js} +1 -1
- package/dist/assets/main-BccclZsC.js +1031 -0
- package/dist/assets/{main-VgdjTBLR.css → main-Ciuw0wF0.css} +1 -1
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/backend/ws/engine/restart.ts +0 -54
- package/dist/assets/main-r0iv2PDz.js +0 -1044
|
@@ -34,6 +34,7 @@ import { syncEngineArtifacts, buildArtifactsPromptContext } from '$backend/engin
|
|
|
34
34
|
import { artifactFilter } from '$backend/profiles';
|
|
35
35
|
import { resolvePermissionsFromDb, isToolAllowed } from '$backend/permissions';
|
|
36
36
|
import { buildJsonPrompt, extractJson } from '../../structured-helpers';
|
|
37
|
+
import { EngineRuns } from '../run-registry';
|
|
37
38
|
import { subagentQueries } from '$backend/database/queries';
|
|
38
39
|
import { readSubagentMd } from '$backend/subagents/store';
|
|
39
40
|
import { getClineAccountForProvider, parseClineCredential, resolveClineAuth } from './credential';
|
|
@@ -85,12 +86,24 @@ class EventQueue<T> {
|
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
|
|
89
|
+
/**
|
|
90
|
+
* One stream in flight on this instance. This engine instance is shared by every
|
|
91
|
+
* chat session of a project, so the agent belongs here, not in an instance field
|
|
92
|
+
* — a field would hold only the most recently started stream, and cancelling any
|
|
93
|
+
* chat would then abort another chat's agent.
|
|
94
|
+
*/
|
|
95
|
+
interface ClineRun {
|
|
96
|
+
/** Identity of the run — the controller the caller passed to streamQuery. */
|
|
97
|
+
controller: AbortController;
|
|
98
|
+
agent: ClineAgent | null;
|
|
99
|
+
}
|
|
100
|
+
|
|
88
101
|
export class ClineEngine implements AIEngine {
|
|
89
102
|
readonly name = 'cline' as const;
|
|
90
103
|
private _isInitialized = false;
|
|
91
|
-
private
|
|
92
|
-
|
|
93
|
-
private pendingAsks = new Map<string, PendingAsk>();
|
|
104
|
+
private runs = new EngineRuns<ClineRun>();
|
|
105
|
+
/** Parked AskUserQuestion callbacks → the run that asked. */
|
|
106
|
+
private pendingAsks = new Map<string, PendingAsk & { owner: ClineRun }>();
|
|
94
107
|
/**
|
|
95
108
|
* In-memory transcript keyed by the SDK session id EMITTED for each turn.
|
|
96
109
|
* Every turn stores its resulting transcript under a fresh id and never
|
|
@@ -101,7 +114,7 @@ export class ClineEngine implements AIEngine {
|
|
|
101
114
|
private sessions = new Map<string, AgentMessage[]>();
|
|
102
115
|
|
|
103
116
|
get isInitialized(): boolean { return this._isInitialized; }
|
|
104
|
-
get isActive(): boolean { return this.
|
|
117
|
+
get isActive(): boolean { return this.runs.isActive; }
|
|
105
118
|
|
|
106
119
|
async initialize(): Promise<void> {
|
|
107
120
|
if (this._isInitialized) return;
|
|
@@ -110,7 +123,7 @@ export class ClineEngine implements AIEngine {
|
|
|
110
123
|
}
|
|
111
124
|
|
|
112
125
|
async dispose(): Promise<void> {
|
|
113
|
-
await this.
|
|
126
|
+
await this.stopRuns(this.runs.all());
|
|
114
127
|
this.pendingAsks.clear();
|
|
115
128
|
this.sessions.clear();
|
|
116
129
|
this._isInitialized = false;
|
|
@@ -145,7 +158,9 @@ export class ClineEngine implements AIEngine {
|
|
|
145
158
|
const provider = parseClineCredential(account.credential)?.provider ?? providerSlug;
|
|
146
159
|
const auth = await resolveClineAuth(account);
|
|
147
160
|
|
|
148
|
-
|
|
161
|
+
const controller = abortController || new AbortController();
|
|
162
|
+
const run: ClineRun = { controller, agent: null };
|
|
163
|
+
this.runs.add(run);
|
|
149
164
|
const resolvedProjectPath = resolveOsPath(projectPath);
|
|
150
165
|
|
|
151
166
|
// ── Active Profile scoping + artifact materialization ──
|
|
@@ -169,7 +184,7 @@ export class ClineEngine implements AIEngine {
|
|
|
169
184
|
enableSubmitAndExit: false,
|
|
170
185
|
});
|
|
171
186
|
const askTool = await createAskUserQuestionTool({
|
|
172
|
-
register: (id, entry) => this.pendingAsks.set(id, entry),
|
|
187
|
+
register: (id, entry) => this.pendingAsks.set(id, { ...entry, owner: run }),
|
|
173
188
|
unregister: (id) => this.pendingAsks.delete(id),
|
|
174
189
|
});
|
|
175
190
|
const mcpTools = await buildClineMcpTools(options.mcpContext, mcpProfileFilter);
|
|
@@ -278,7 +293,7 @@ export class ClineEngine implements AIEngine {
|
|
|
278
293
|
toolPolicies,
|
|
279
294
|
maxIterations: options.maxTurns ?? 100,
|
|
280
295
|
});
|
|
281
|
-
|
|
296
|
+
run.agent = agent;
|
|
282
297
|
if (priorMessages?.length) agent.restore(priorMessages);
|
|
283
298
|
|
|
284
299
|
const engineMeta: MessageEngine = {
|
|
@@ -300,7 +315,7 @@ export class ClineEngine implements AIEngine {
|
|
|
300
315
|
|
|
301
316
|
// Abort wiring: cancelling the stream aborts the Cline run.
|
|
302
317
|
const onAbort = () => { agent.abort('Cancelled'); };
|
|
303
|
-
|
|
318
|
+
controller.signal.addEventListener('abort', onAbort, { once: true });
|
|
304
319
|
|
|
305
320
|
// Build the user turn (text + image attachments).
|
|
306
321
|
const promptText = prompt.content.filter(b => b.type === 'text').map(b => (b.type === 'text' ? b.text : '')).join('\n');
|
|
@@ -348,29 +363,52 @@ export class ClineEngine implements AIEngine {
|
|
|
348
363
|
} catch (error) {
|
|
349
364
|
handleStreamError(error);
|
|
350
365
|
} finally {
|
|
351
|
-
|
|
366
|
+
controller.signal.removeEventListener('abort', onAbort);
|
|
352
367
|
unsubscribe();
|
|
353
|
-
|
|
354
|
-
this.
|
|
355
|
-
this.
|
|
368
|
+
// Retire THIS run only — another chat session of the same project may
|
|
369
|
+
// still be streaming on this instance.
|
|
370
|
+
this.forgetRun(run);
|
|
356
371
|
}
|
|
357
372
|
}
|
|
358
373
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
374
|
+
/** Drop a finished run and the asks only it could answer. */
|
|
375
|
+
private forgetRun(run: ClineRun): void {
|
|
376
|
+
this.runs.remove(run);
|
|
377
|
+
for (const [id, pending] of this.pendingAsks) {
|
|
378
|
+
if (pending.owner === run) this.pendingAsks.delete(id);
|
|
362
379
|
}
|
|
363
|
-
|
|
364
|
-
|
|
380
|
+
run.agent = null;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Cancel the run whose AbortController is `owner`, and only that run.
|
|
385
|
+
*/
|
|
386
|
+
async cancel(owner: AbortController): Promise<void> {
|
|
387
|
+
await this.stopRuns(this.runs.select(owner));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Tear down the given runs. `cancel` passes the one run it was asked to
|
|
392
|
+
* stop; `dispose` passes them all. Nothing else may reach this.
|
|
393
|
+
*/
|
|
394
|
+
private async stopRuns(targets: ClineRun[]): Promise<void> {
|
|
395
|
+
for (const run of targets) {
|
|
396
|
+
if (!run.controller.signal.aborted) run.controller.abort();
|
|
397
|
+
const agent = run.agent;
|
|
398
|
+
// Drop only this run's parked asks; another run's are still live.
|
|
399
|
+
this.forgetRun(run);
|
|
400
|
+
if (agent) {
|
|
401
|
+
try { agent.abort('Cancelled'); } catch { /* may already be idle */ }
|
|
402
|
+
}
|
|
365
403
|
}
|
|
366
|
-
this.pendingAsks.clear();
|
|
367
|
-
this.activeAgent = null;
|
|
368
|
-
this.activeController = null;
|
|
369
404
|
}
|
|
370
405
|
|
|
371
|
-
async interrupt(): Promise<void> {
|
|
372
|
-
|
|
373
|
-
|
|
406
|
+
async interrupt(owner: AbortController): Promise<void> {
|
|
407
|
+
const targets = this.runs.select(owner);
|
|
408
|
+
for (const run of targets) {
|
|
409
|
+
if (run.agent) {
|
|
410
|
+
try { run.agent.abort('Interrupted'); } catch { /* ignore */ }
|
|
411
|
+
}
|
|
374
412
|
}
|
|
375
413
|
}
|
|
376
414
|
|
|
@@ -26,9 +26,10 @@ import type { AIEngine, EngineQueryOptions, StructuredGenerationOptions } from '
|
|
|
26
26
|
import { extractJson } from '../../structured-helpers';
|
|
27
27
|
import { engineQueries } from '$backend/database/queries/engine-queries';
|
|
28
28
|
import { resolveOsPath } from '$backend/utils/paths';
|
|
29
|
-
import {
|
|
29
|
+
import { resolveEngineCli } from '$backend/engine/engine-cli';
|
|
30
30
|
import { getCleanSpawnEnv } from '$backend/utils/index.js';
|
|
31
31
|
import { getCodexMcpConfig } from '../../../mcp';
|
|
32
|
+
import { EngineRuns } from '../run-registry';
|
|
32
33
|
import { artifactFilter } from '$backend/profiles';
|
|
33
34
|
import { syncSkills } from '$backend/skills';
|
|
34
35
|
import { syncEngineArtifacts, buildArtifactsPromptContext } from '$backend/engine/artifact-sync';
|
|
@@ -56,13 +57,26 @@ import path from 'node:path';
|
|
|
56
57
|
import os from 'node:os';
|
|
57
58
|
import fs from 'node:fs';
|
|
58
59
|
|
|
60
|
+
/** One stream in flight on this instance. */
|
|
61
|
+
interface CodexRun {
|
|
62
|
+
/** Identity of the run — the controller the caller passed to streamQuery. */
|
|
63
|
+
controller: AbortController;
|
|
64
|
+
thread: Thread | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
59
67
|
export class CodexEngine implements AIEngine {
|
|
60
68
|
readonly name = 'codex' as const;
|
|
61
69
|
|
|
62
70
|
private _isInitialized = false;
|
|
63
71
|
private codex: Codex | null = null;
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Every stream in flight, keyed by the AbortController its caller passed to
|
|
74
|
+
* streamQuery. One instance serves all chat sessions of a project, so the
|
|
75
|
+
* thread and controller of a stream belong to the run, not the instance — a
|
|
76
|
+
* field would hold only the most recent one and cancelling any chat would
|
|
77
|
+
* abort another chat's turn.
|
|
78
|
+
*/
|
|
79
|
+
private runs = new EngineRuns<CodexRun>();
|
|
66
80
|
/** Account ID currently baked into `this.codex`. Same trade-off as Copilot. */
|
|
67
81
|
private currentAccountId: number | null = null;
|
|
68
82
|
/**
|
|
@@ -78,7 +92,7 @@ export class CodexEngine implements AIEngine {
|
|
|
78
92
|
}
|
|
79
93
|
|
|
80
94
|
get isActive(): boolean {
|
|
81
|
-
return this.
|
|
95
|
+
return this.runs.isActive;
|
|
82
96
|
}
|
|
83
97
|
|
|
84
98
|
async initialize(accountId?: number, mcpProfileFilter?: Set<string>): Promise<void> {
|
|
@@ -104,7 +118,10 @@ export class CodexEngine implements AIEngine {
|
|
|
104
118
|
// option is passed directly; no filesystem mutation.
|
|
105
119
|
applyAccountAuth(account);
|
|
106
120
|
|
|
107
|
-
|
|
121
|
+
// Prefer the binary clopen manages (vendored in the SDK's platform package
|
|
122
|
+
// inside the stack dir) over an older copy on the user's PATH — the SDK
|
|
123
|
+
// would otherwise be pinned while the process it spawns drifts.
|
|
124
|
+
const codexCli = await resolveEngineCli('codex');
|
|
108
125
|
const mcpConfig = getCodexMcpConfig(mcpProfileFilter);
|
|
109
126
|
|
|
110
127
|
// Codex SDK takes config at construction. We pass `show_raw_agent_reasoning`
|
|
@@ -123,7 +140,7 @@ export class CodexEngine implements AIEngine {
|
|
|
123
140
|
const { Codex } = await loadEngineSdk<typeof import('@openai/codex-sdk')>('codex', '@openai/codex-sdk');
|
|
124
141
|
this.codex = new Codex({
|
|
125
142
|
apiKey: credential.kind === 'api_key' ? credential.apiKey : undefined,
|
|
126
|
-
...(
|
|
143
|
+
...(codexCli ? { codexPathOverride: codexCli.path } : {}),
|
|
127
144
|
env: { ...getCleanSpawnEnv(), CODEX_HOME: getCodexHomeDir() },
|
|
128
145
|
config: {
|
|
129
146
|
show_raw_agent_reasoning: true,
|
|
@@ -137,7 +154,7 @@ export class CodexEngine implements AIEngine {
|
|
|
137
154
|
}
|
|
138
155
|
|
|
139
156
|
async dispose(): Promise<void> {
|
|
140
|
-
await this.
|
|
157
|
+
await this.stopRuns(this.runs.all());
|
|
141
158
|
this.codex = null;
|
|
142
159
|
this.currentAccountId = null;
|
|
143
160
|
this.currentMcpFilterKey = null;
|
|
@@ -194,7 +211,9 @@ export class CodexEngine implements AIEngine {
|
|
|
194
211
|
applyAccountAuth(activeAccount);
|
|
195
212
|
}
|
|
196
213
|
|
|
197
|
-
|
|
214
|
+
const controller = abortController || new AbortController();
|
|
215
|
+
const run: CodexRun = { controller, thread: null };
|
|
216
|
+
this.runs.add(run);
|
|
198
217
|
|
|
199
218
|
const resolvedProjectPath = resolveOsPath(projectPath);
|
|
200
219
|
const state = createCodexState('', modelId);
|
|
@@ -234,7 +253,7 @@ export class CodexEngine implements AIEngine {
|
|
|
234
253
|
thread = this.codex.startThread(threadOptions);
|
|
235
254
|
}
|
|
236
255
|
|
|
237
|
-
|
|
256
|
+
run.thread = thread;
|
|
238
257
|
|
|
239
258
|
// Prompt-scoped engine: advertise the profile-scoped Skills/Commands/
|
|
240
259
|
// Subagents PER-SESSION via the prompt (not the shared global AGENTS.md /
|
|
@@ -242,11 +261,11 @@ export class CodexEngine implements AIEngine {
|
|
|
242
261
|
const artifactsContext = buildArtifactsPromptContext(profileId);
|
|
243
262
|
const input = await buildCodexInput(prompt, artifactsContext || undefined);
|
|
244
263
|
const { events } = await thread.runStreamed(input, {
|
|
245
|
-
signal:
|
|
264
|
+
signal: controller.signal,
|
|
246
265
|
});
|
|
247
266
|
|
|
248
267
|
for await (const event of events) {
|
|
249
|
-
if (
|
|
268
|
+
if (controller.signal.aborted) break;
|
|
250
269
|
|
|
251
270
|
switch (event.type) {
|
|
252
271
|
case 'thread.started':
|
|
@@ -281,7 +300,7 @@ export class CodexEngine implements AIEngine {
|
|
|
281
300
|
}
|
|
282
301
|
}
|
|
283
302
|
|
|
284
|
-
yield buildResultEvent(state,
|
|
303
|
+
yield buildResultEvent(state, controller.signal.aborted);
|
|
285
304
|
} catch (error) {
|
|
286
305
|
handleStreamError(error);
|
|
287
306
|
} finally {
|
|
@@ -292,23 +311,34 @@ export class CodexEngine implements AIEngine {
|
|
|
292
311
|
} catch (snapshotErr) {
|
|
293
312
|
debug.warn('engine', 'Codex: post-stream auth.json snapshot failed (non-fatal):', snapshotErr);
|
|
294
313
|
}
|
|
295
|
-
|
|
296
|
-
this.
|
|
314
|
+
// Retire THIS run only — another chat session of the same project may
|
|
315
|
+
// still be streaming on this instance.
|
|
316
|
+
this.runs.remove(run);
|
|
297
317
|
}
|
|
298
318
|
}
|
|
299
319
|
|
|
300
|
-
|
|
320
|
+
/**
|
|
321
|
+
* Cancel the run whose AbortController is `owner`, and only that run.
|
|
322
|
+
*/
|
|
323
|
+
async cancel(owner: AbortController): Promise<void> {
|
|
324
|
+
await this.stopRuns(this.runs.select(owner));
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Tear down the given runs. `cancel` passes the one run it was asked to
|
|
329
|
+
* stop; `dispose` passes them all. Nothing else may reach this.
|
|
330
|
+
*/
|
|
331
|
+
private async stopRuns(targets: CodexRun[]): Promise<void> {
|
|
301
332
|
// Abort the local controller so the for-await loop exits even if the
|
|
302
333
|
// SDK's signal propagation hangs.
|
|
303
|
-
|
|
304
|
-
|
|
334
|
+
for (const run of targets) {
|
|
335
|
+
if (!run.controller.signal.aborted) run.controller.abort();
|
|
336
|
+
this.runs.remove(run);
|
|
305
337
|
}
|
|
306
|
-
this.activeController = null;
|
|
307
|
-
this.activeThread = null;
|
|
308
338
|
}
|
|
309
339
|
|
|
310
|
-
async interrupt(): Promise<void> {
|
|
311
|
-
await this.
|
|
340
|
+
async interrupt(owner: AbortController): Promise<void> {
|
|
341
|
+
await this.stopRuns(this.runs.select(owner));
|
|
312
342
|
}
|
|
313
343
|
|
|
314
344
|
/**
|
|
@@ -24,6 +24,7 @@ import { engineQueries } from '$backend/database/queries/engine-queries';
|
|
|
24
24
|
import { resolveOsPath, getEngineUserConfigDir } from '$backend/utils/paths';
|
|
25
25
|
import { debug } from '$shared/utils/logger';
|
|
26
26
|
import { getCopilotMcpConfig } from '../../../mcp';
|
|
27
|
+
import { EngineRuns } from '../run-registry';
|
|
27
28
|
import { artifactFilter } from '$backend/profiles';
|
|
28
29
|
import { syncSkills } from '$backend/skills';
|
|
29
30
|
import { syncEngineArtifacts, buildArtifactsPromptContext } from '$backend/engine/artifact-sync';
|
|
@@ -107,13 +108,30 @@ interface PendingCopilotUserAnswer {
|
|
|
107
108
|
choices?: string[];
|
|
108
109
|
}
|
|
109
110
|
|
|
111
|
+
/**
|
|
112
|
+
* One stream in flight on this instance.
|
|
113
|
+
*/
|
|
114
|
+
interface CopilotRun {
|
|
115
|
+
/** Identity of the run — the controller the caller passed to streamQuery. */
|
|
116
|
+
controller: AbortController;
|
|
117
|
+
session: CopilotSession | null;
|
|
118
|
+
/** Correlates `onUserInputRequest` with the toolCallId seen on the event stream. */
|
|
119
|
+
pendingAskUserToolCallId: string | null;
|
|
120
|
+
}
|
|
121
|
+
|
|
110
122
|
export class CopilotEngine implements AIEngine {
|
|
111
123
|
readonly name = 'copilot' as const;
|
|
112
124
|
|
|
113
125
|
private _isInitialized = false;
|
|
114
126
|
private client: CopilotClient | null = null;
|
|
115
|
-
|
|
116
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Every stream in flight, keyed by the AbortController its caller passed to
|
|
129
|
+
* streamQuery. One instance serves all chat sessions of a project, so the
|
|
130
|
+
* Copilot session of a stream belongs to the run — a field would hold only
|
|
131
|
+
* the most recent one and cancelling any chat would abort another chat's
|
|
132
|
+
* session.
|
|
133
|
+
*/
|
|
134
|
+
private runs = new EngineRuns<CopilotRun>();
|
|
117
135
|
private modelsCache: ModelInfo[] | null = null;
|
|
118
136
|
/**
|
|
119
137
|
* Account ID currently baked into `this.client`. The Copilot SDK takes the
|
|
@@ -132,20 +150,20 @@ export class CopilotEngine implements AIEngine {
|
|
|
132
150
|
* NOT the toolCallId (see `node_modules/@github/copilot-sdk/dist/client.js::handleUserInputRequest`)
|
|
133
151
|
*
|
|
134
152
|
* To correlate the two we capture the most recent `toolCallId` from
|
|
135
|
-
* those events into `pendingAskUserToolCallId`, then read it
|
|
136
|
-
* the callback. Only one `ask_user` can be outstanding per session
|
|
137
|
-
* a time (the SDK blocks the agent's turn until the callback returns),
|
|
138
|
-
* so a single
|
|
153
|
+
* those events into the run's `pendingAskUserToolCallId`, then read it
|
|
154
|
+
* inside the callback. Only one `ask_user` can be outstanding per session
|
|
155
|
+
* at a time (the SDK blocks the agent's turn until the callback returns),
|
|
156
|
+
* so a single slot PER RUN is sufficient — but it cannot be a single slot
|
|
157
|
+
* per instance, which two concurrent chats of a project would share.
|
|
139
158
|
*/
|
|
140
|
-
private pendingUserAnswers = new Map<string, PendingCopilotUserAnswer>();
|
|
141
|
-
private pendingAskUserToolCallId: string | null = null;
|
|
159
|
+
private pendingUserAnswers = new Map<string, PendingCopilotUserAnswer & { owner: CopilotRun }>();
|
|
142
160
|
|
|
143
161
|
get isInitialized(): boolean {
|
|
144
162
|
return this._isInitialized;
|
|
145
163
|
}
|
|
146
164
|
|
|
147
165
|
get isActive(): boolean {
|
|
148
|
-
return this.
|
|
166
|
+
return this.runs.isActive;
|
|
149
167
|
}
|
|
150
168
|
|
|
151
169
|
async initialize(accountId?: number): Promise<void> {
|
|
@@ -178,7 +196,7 @@ export class CopilotEngine implements AIEngine {
|
|
|
178
196
|
}
|
|
179
197
|
|
|
180
198
|
async dispose(): Promise<void> {
|
|
181
|
-
await this.
|
|
199
|
+
await this.stopRuns(this.runs.all());
|
|
182
200
|
|
|
183
201
|
if (this.client) {
|
|
184
202
|
try {
|
|
@@ -242,7 +260,9 @@ export class CopilotEngine implements AIEngine {
|
|
|
242
260
|
throw new Error('Copilot client unavailable.');
|
|
243
261
|
}
|
|
244
262
|
|
|
245
|
-
|
|
263
|
+
const controller = abortController || new AbortController();
|
|
264
|
+
const run: CopilotRun = { controller, session: null, pendingAskUserToolCallId: null };
|
|
265
|
+
this.runs.add(run);
|
|
246
266
|
|
|
247
267
|
// Active Profile for this stream — scopes artifacts + connectors.
|
|
248
268
|
const profileId = options.mcpContext?.profileId;
|
|
@@ -299,13 +319,13 @@ export class CopilotEngine implements AIEngine {
|
|
|
299
319
|
if (event.type === 'assistant.message' && event.data.toolRequests) {
|
|
300
320
|
for (const req of event.data.toolRequests) {
|
|
301
321
|
if (req.name === 'ask_user') {
|
|
302
|
-
|
|
322
|
+
run.pendingAskUserToolCallId = req.toolCallId;
|
|
303
323
|
}
|
|
304
324
|
}
|
|
305
325
|
} else if (event.type === 'tool.execution_start' && event.data.toolName === 'ask_user') {
|
|
306
|
-
|
|
326
|
+
run.pendingAskUserToolCallId = event.data.toolCallId;
|
|
307
327
|
} else if (event.type === 'user_input.requested' && event.data.toolCallId) {
|
|
308
|
-
|
|
328
|
+
run.pendingAskUserToolCallId = event.data.toolCallId;
|
|
309
329
|
}
|
|
310
330
|
pushEvent(event);
|
|
311
331
|
};
|
|
@@ -314,7 +334,7 @@ export class CopilotEngine implements AIEngine {
|
|
|
314
334
|
finished = true;
|
|
315
335
|
pushEvent(null);
|
|
316
336
|
};
|
|
317
|
-
|
|
337
|
+
controller.signal.addEventListener('abort', onAbort, { once: true });
|
|
318
338
|
|
|
319
339
|
const { approveAll } = await loadEngineSdk<typeof import('@github/copilot-sdk')>('copilot', '@github/copilot-sdk');
|
|
320
340
|
|
|
@@ -343,8 +363,8 @@ export class CopilotEngine implements AIEngine {
|
|
|
343
363
|
// to a synthetic id so `cancel()` can still release the
|
|
344
364
|
// Promise — but log loudly because the chat UI's answer
|
|
345
365
|
// submission won't reach this entry.
|
|
346
|
-
const captured =
|
|
347
|
-
|
|
366
|
+
const captured = run.pendingAskUserToolCallId;
|
|
367
|
+
run.pendingAskUserToolCallId = null;
|
|
348
368
|
const toolCallId = captured ?? `copilot-ask-user-${crypto.randomUUID()}`;
|
|
349
369
|
|
|
350
370
|
if (captured) {
|
|
@@ -357,6 +377,7 @@ export class CopilotEngine implements AIEngine {
|
|
|
357
377
|
this.pendingUserAnswers.set(toolCallId, {
|
|
358
378
|
resolve,
|
|
359
379
|
choices: request.choices,
|
|
380
|
+
owner: run,
|
|
360
381
|
});
|
|
361
382
|
});
|
|
362
383
|
},
|
|
@@ -413,7 +434,7 @@ export class CopilotEngine implements AIEngine {
|
|
|
413
434
|
session = await this.client.createSession({ ...baseConfig } as SessionConfig);
|
|
414
435
|
}
|
|
415
436
|
|
|
416
|
-
|
|
437
|
+
run.session = session;
|
|
417
438
|
state.sessionId = session.sessionId;
|
|
418
439
|
|
|
419
440
|
// Send the prompt — fire-and-forget; events arrive via the queue.
|
|
@@ -440,7 +461,7 @@ export class CopilotEngine implements AIEngine {
|
|
|
440
461
|
|
|
441
462
|
// Drain the event queue.
|
|
442
463
|
while (!finished) {
|
|
443
|
-
if (
|
|
464
|
+
if (controller.signal.aborted) break;
|
|
444
465
|
|
|
445
466
|
const event: SessionEvent | null = queue.length > 0
|
|
446
467
|
? queue.shift()!
|
|
@@ -448,7 +469,7 @@ export class CopilotEngine implements AIEngine {
|
|
|
448
469
|
waiter = resolve;
|
|
449
470
|
});
|
|
450
471
|
|
|
451
|
-
if (!event ||
|
|
472
|
+
if (!event || controller.signal.aborted) break;
|
|
452
473
|
|
|
453
474
|
// Resolve the parent Agent (`task`) tool call id for events
|
|
454
475
|
// originating inside a sub-agent. Null for the root agent and
|
|
@@ -582,54 +603,77 @@ export class CopilotEngine implements AIEngine {
|
|
|
582
603
|
} catch (error) {
|
|
583
604
|
handleStreamError(error);
|
|
584
605
|
} finally {
|
|
585
|
-
|
|
606
|
+
controller.signal.removeEventListener('abort', onAbort);
|
|
586
607
|
|
|
587
|
-
if (
|
|
608
|
+
if (run.session) {
|
|
588
609
|
try {
|
|
589
|
-
await
|
|
610
|
+
await run.session.disconnect();
|
|
590
611
|
} catch (error) {
|
|
591
612
|
debug.warn('engine', 'Copilot session.disconnect failed (non-fatal):', error);
|
|
592
613
|
}
|
|
593
614
|
}
|
|
594
|
-
|
|
595
|
-
this.
|
|
615
|
+
// Retire THIS run only — another chat session of the same project may
|
|
616
|
+
// still be streaming on this instance.
|
|
617
|
+
this.forgetRun(run);
|
|
596
618
|
}
|
|
597
619
|
}
|
|
598
620
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
for (const [, pending] of this.pendingUserAnswers) {
|
|
605
|
-
pending.resolve({ answer: '', wasFreeform: false });
|
|
621
|
+
/** Drop a finished run and the questions only it could answer. */
|
|
622
|
+
private forgetRun(run: CopilotRun): void {
|
|
623
|
+
this.runs.remove(run);
|
|
624
|
+
for (const [toolCallId, pending] of this.pendingUserAnswers) {
|
|
625
|
+
if (pending.owner === run) this.pendingUserAnswers.delete(toolCallId);
|
|
606
626
|
}
|
|
607
|
-
|
|
608
|
-
|
|
627
|
+
run.session = null;
|
|
628
|
+
run.pendingAskUserToolCallId = null;
|
|
629
|
+
}
|
|
609
630
|
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
631
|
+
/**
|
|
632
|
+
* Cancel the run whose AbortController is `owner`, and only that run.
|
|
633
|
+
*/
|
|
634
|
+
async cancel(owner: AbortController): Promise<void> {
|
|
635
|
+
await this.stopRuns(this.runs.select(owner));
|
|
636
|
+
}
|
|
616
637
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
638
|
+
/**
|
|
639
|
+
* Tear down the given runs. `cancel` passes the one run it was asked to
|
|
640
|
+
* stop; `dispose` passes them all. Nothing else may reach this.
|
|
641
|
+
*/
|
|
642
|
+
private async stopRuns(targets: CopilotRun[]): Promise<void> {
|
|
643
|
+
for (const run of targets) {
|
|
644
|
+
// 1. Release THIS run's parked `onUserInputRequest` callbacks with an
|
|
645
|
+
// empty answer so the SDK isn't left blocked on a Promise that will
|
|
646
|
+
// never resolve. Empty `answer` + `wasFreeform: false` is the SDK-safe
|
|
647
|
+
// way to unblock without injecting fake user input. Another run's
|
|
648
|
+
// parked callback stays — its session is still live.
|
|
649
|
+
for (const [toolCallId, pending] of this.pendingUserAnswers) {
|
|
650
|
+
if (pending.owner !== run) continue;
|
|
651
|
+
pending.resolve({ answer: '', wasFreeform: false });
|
|
652
|
+
this.pendingUserAnswers.delete(toolCallId);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// 2. Abort the local stream first so the loop exits even if RPC hangs.
|
|
656
|
+
const session = run.session;
|
|
657
|
+
if (!run.controller.signal.aborted) run.controller.abort();
|
|
658
|
+
this.forgetRun(run);
|
|
659
|
+
|
|
660
|
+
// 3. Tell the Copilot server to stop processing (with timeout).
|
|
661
|
+
if (session) {
|
|
662
|
+
try {
|
|
663
|
+
await Promise.race([
|
|
664
|
+
session.abort(),
|
|
665
|
+
new Promise<void>(resolve => setTimeout(resolve, ABORT_TIMEOUT_MS)),
|
|
666
|
+
]);
|
|
667
|
+
debug.log('engine', 'Copilot session aborted:', session.sessionId);
|
|
668
|
+
} catch (error) {
|
|
669
|
+
debug.warn('engine', 'Copilot session.abort failed (non-fatal):', error);
|
|
670
|
+
}
|
|
627
671
|
}
|
|
628
672
|
}
|
|
629
673
|
}
|
|
630
674
|
|
|
631
|
-
async interrupt(): Promise<void> {
|
|
632
|
-
await this.
|
|
675
|
+
async interrupt(owner: AbortController): Promise<void> {
|
|
676
|
+
await this.stopRuns(this.runs.select(owner));
|
|
633
677
|
}
|
|
634
678
|
|
|
635
679
|
/**
|