@myrialabs/clopen 0.4.31 → 0.4.33
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 +31 -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/069_create_ssh_client_tables.ts +116 -0
- package/backend/database/migrations/index.ts +14 -0
- package/backend/database/queries/db-client-connection-queries.ts +7 -2
- package/backend/database/queries/file-audit-log-queries.ts +1 -0
- package/backend/database/queries/index.ts +3 -0
- package/backend/database/queries/ssh-connection-queries.ts +244 -0
- package/backend/database/queries/ssh-known-host-queries.ts +67 -0
- package/backend/database/queries/ssh-port-forward-queries.ts +113 -0
- package/backend/db-client/connection-manager.ts +8 -1
- 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/message-converter.ts +18 -12
- package/backend/engine/adapters/codex/patch-rollout.test.ts +88 -0
- package/backend/engine/adapters/codex/patch-rollout.ts +120 -109
- package/backend/engine/adapters/codex/stream.ts +78 -22
- package/backend/engine/adapters/copilot/stream.ts +97 -53
- package/backend/engine/adapters/cursor/stream.ts +75 -35
- package/backend/engine/adapters/opencode/server.ts +511 -134
- 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 +92 -59
- package/backend/engine/adapters/run-registry.test.ts +75 -0
- package/backend/engine/adapters/run-registry.ts +63 -0
- package/backend/engine/config-revision.ts +150 -0
- package/backend/engine/docs/adapter-contract.md +47 -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/hot-reload.ts +99 -0
- package/backend/engine/index.ts +96 -24
- package/backend/engine/types.ts +17 -5
- package/backend/files/file-watcher.ts +283 -51
- package/backend/files/git-events.test.ts +110 -0
- package/backend/git/git-dirs.test.ts +132 -0
- package/backend/git/git-dirs.ts +0 -0
- package/backend/git/git-executor.ts +22 -3
- package/backend/git/git-service.ts +1 -1
- package/backend/git/nested-repos.test.ts +224 -0
- package/backend/git/nested-repos.ts +386 -0
- package/backend/http/audio.ts +5 -32
- package/backend/http/bearer-auth.ts +47 -0
- package/backend/http/files-download.test.ts +146 -0
- package/backend/http/files-download.ts +109 -0
- package/backend/http/files-upload.ts +3 -29
- package/backend/http/ssh-sftp.ts +133 -0
- package/backend/index.ts +23 -2
- package/backend/mcp/README.md +34 -6
- package/backend/mcp/index.ts +8 -8
- package/backend/mcp/internal/config.ts +69 -19
- package/backend/mcp/internal/project-context.test.ts +40 -0
- package/backend/mcp/internal/project-context.ts +80 -7
- package/backend/mcp/internal/remote-server.test.ts +17 -2
- package/backend/mcp/internal/remote-server.ts +36 -3
- package/backend/mcp/internal/servers/helper.ts +48 -18
- package/backend/preview/browser/browser-video-capture.ts +51 -4
- package/backend/snapshot/gitignore.ts +10 -174
- package/backend/ssh/client-pool.ts +196 -0
- package/backend/ssh/connect.ts +375 -0
- package/backend/ssh/disk-usage.test.ts +134 -0
- package/backend/ssh/disk-usage.ts +214 -0
- package/backend/ssh/forwards.ts +309 -0
- package/backend/ssh/health.ts +141 -0
- package/backend/ssh/known-hosts.ts +96 -0
- package/backend/ssh/pty-backend.ts +215 -0
- package/backend/ssh/ptykit.ts +66 -0
- package/backend/ssh/sftp.test.ts +242 -0
- package/backend/ssh/sftp.ts +967 -0
- package/backend/ssh/socks5.test.ts +167 -0
- package/backend/ssh/socks5.ts +164 -0
- package/backend/ssh/tunnel.ts +87 -0
- package/backend/ws/chat/stream.ts +24 -0
- package/backend/ws/db-client/connections.ts +1 -0
- package/backend/ws/engine/codex/accounts.ts +0 -12
- 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/files/watch.ts +5 -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/index.ts +4 -0
- package/backend/ws/ssh/access.ts +31 -0
- package/backend/ws/ssh/connections.ts +224 -0
- package/backend/ws/ssh/forwards.ts +109 -0
- package/backend/ws/ssh/index.ts +18 -0
- package/backend/ws/ssh/sftp.ts +259 -0
- package/backend/ws/ssh/tunnel.ts +93 -0
- package/dist/assets/{index-DYwQ5bP8.js → index-BF4buYmI.js} +1 -1
- package/dist/assets/{index-DUc5T21e.js → index-BW0DA8NV.js} +1 -1
- package/dist/assets/main-BbwWLJRN.js +1057 -0
- package/dist/assets/main-CCvRMXlP.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/database/schema.ts +49 -0
- package/shared/types/db-client/connection.ts +6 -0
- package/shared/types/ssh/connection.ts +80 -0
- package/shared/types/ssh/forward.ts +46 -0
- package/shared/types/ssh/index.ts +8 -0
- package/shared/types/ssh/known-host.ts +19 -0
- package/shared/types/ssh/sftp.ts +141 -0
- package/shared/utils/logger.ts +1 -0
- package/backend/ws/engine/restart.ts +0 -54
- package/dist/assets/main-VgdjTBLR.css +0 -1
- package/dist/assets/main-r0iv2PDz.js +0 -1044
|
@@ -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,12 +334,12 @@ 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
|
|
|
321
341
|
try {
|
|
322
|
-
const mcpConfig = getCopilotMcpConfig(mcpProfileFilter);
|
|
342
|
+
const mcpConfig = getCopilotMcpConfig(mcpProfileFilter, options.mcpContext);
|
|
323
343
|
|
|
324
344
|
const baseConfig: ResumeSessionConfig = {
|
|
325
345
|
onPermissionRequest: (request, invocation) =>
|
|
@@ -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
|
/**
|
|
@@ -35,6 +35,7 @@ import { getCursorMcpConfig } from '../../../mcp';
|
|
|
35
35
|
import { subagentQueries } from '$backend/database/queries';
|
|
36
36
|
import { readSubagentMd } from '$backend/subagents/store';
|
|
37
37
|
import { buildJsonPrompt, extractJson } from '../../structured-helpers';
|
|
38
|
+
import { EngineRuns } from '../run-registry';
|
|
38
39
|
import { getActiveCursorAccount, resolveCursorApiKey } from './credential';
|
|
39
40
|
import { getCursorStore } from './environment';
|
|
40
41
|
import { forkCursorAgent } from './session-fork';
|
|
@@ -97,16 +98,28 @@ function buildCursorModelSelection(modelId: string, reasoningEffort?: string): M
|
|
|
97
98
|
return { id: modelId, params: [{ id: paramId, value }] };
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
/**
|
|
102
|
+
* One stream in flight on this instance. This engine instance is shared by every
|
|
103
|
+
* chat session of a project, so the run and agent belong here, not in instance
|
|
104
|
+
* fields — those would hold only the most recently started stream, and
|
|
105
|
+
* cancelling any chat would then cancel another chat's Cursor run.
|
|
106
|
+
*/
|
|
107
|
+
interface CursorRunState {
|
|
108
|
+
/** Identity of the run — the controller the caller passed to streamQuery. */
|
|
109
|
+
controller: AbortController;
|
|
110
|
+
run: Run | null;
|
|
111
|
+
agent: SDKAgent | null;
|
|
112
|
+
}
|
|
113
|
+
|
|
100
114
|
export class CursorEngine implements AIEngine {
|
|
101
115
|
readonly name = 'cursor' as const;
|
|
102
116
|
private _isInitialized = false;
|
|
103
|
-
private
|
|
104
|
-
|
|
105
|
-
private
|
|
106
|
-
private pendingAsks = new Map<string, PendingAsk>();
|
|
117
|
+
private runs = new EngineRuns<CursorRunState>();
|
|
118
|
+
/** Parked AskUserQuestion callbacks → the run that asked. */
|
|
119
|
+
private pendingAsks = new Map<string, PendingAsk & { owner: CursorRunState }>();
|
|
107
120
|
|
|
108
121
|
get isInitialized(): boolean { return this._isInitialized; }
|
|
109
|
-
get isActive(): boolean { return this.
|
|
122
|
+
get isActive(): boolean { return this.runs.isActive; }
|
|
110
123
|
|
|
111
124
|
async initialize(): Promise<void> {
|
|
112
125
|
if (this._isInitialized) return;
|
|
@@ -115,7 +128,7 @@ export class CursorEngine implements AIEngine {
|
|
|
115
128
|
}
|
|
116
129
|
|
|
117
130
|
async dispose(): Promise<void> {
|
|
118
|
-
await this.
|
|
131
|
+
await this.stopRuns(this.runs.all());
|
|
119
132
|
this.pendingAsks.clear();
|
|
120
133
|
this._isInitialized = false;
|
|
121
134
|
}
|
|
@@ -153,7 +166,9 @@ export class CursorEngine implements AIEngine {
|
|
|
153
166
|
}
|
|
154
167
|
const apiKey = resolveCursorApiKey(account);
|
|
155
168
|
|
|
156
|
-
|
|
169
|
+
const controller = abortController || new AbortController();
|
|
170
|
+
const runState: CursorRunState = { controller, run: null, agent: null };
|
|
171
|
+
this.runs.add(runState);
|
|
157
172
|
const resolvedProjectPath = resolveOsPath(projectPath);
|
|
158
173
|
|
|
159
174
|
// ── Active Profile scoping + artifact materialization ──
|
|
@@ -168,7 +183,7 @@ export class CursorEngine implements AIEngine {
|
|
|
168
183
|
|
|
169
184
|
// ── Tools: AskUserQuestion (in-process custom tool) + MCP (native config) ──
|
|
170
185
|
const askTool: SDKCustomTool = createAskUserQuestionTool({
|
|
171
|
-
register: (id, entry) => this.pendingAsks.set(id, entry),
|
|
186
|
+
register: (id, entry) => this.pendingAsks.set(id, { ...entry, owner: runState }),
|
|
172
187
|
unregister: (id) => this.pendingAsks.delete(id),
|
|
173
188
|
// Emit the tool_use with the COMPLETE questions from execute's args.
|
|
174
189
|
emit: (id, questions) => {
|
|
@@ -177,7 +192,7 @@ export class CursorEngine implements AIEngine {
|
|
|
177
192
|
},
|
|
178
193
|
});
|
|
179
194
|
const customTools: Record<string, SDKCustomTool> = { AskUserQuestion: askTool };
|
|
180
|
-
const mcpServers = getCursorMcpConfig(mcpProfileFilter);
|
|
195
|
+
const mcpServers = getCursorMcpConfig(mcpProfileFilter, options.mcpContext);
|
|
181
196
|
const agents = await this.buildAgents();
|
|
182
197
|
|
|
183
198
|
const { Agent } = await loadEngineSdk<typeof import('@cursor/sdk')>('cursor', '@cursor/sdk');
|
|
@@ -220,11 +235,11 @@ export class CursorEngine implements AIEngine {
|
|
|
220
235
|
...(agents ? { agents } : {}),
|
|
221
236
|
});
|
|
222
237
|
} catch (error) {
|
|
223
|
-
this.
|
|
238
|
+
this.runs.remove(runState);
|
|
224
239
|
handleStreamError(error);
|
|
225
240
|
return;
|
|
226
241
|
}
|
|
227
|
-
|
|
242
|
+
runState.agent = agent;
|
|
228
243
|
const sessionId = agent.agentId;
|
|
229
244
|
|
|
230
245
|
const converter = createCursorMessageConverter({ engine: engineMeta, sessionId });
|
|
@@ -260,11 +275,11 @@ export class CursorEngine implements AIEngine {
|
|
|
260
275
|
}
|
|
261
276
|
},
|
|
262
277
|
});
|
|
263
|
-
|
|
278
|
+
runState.run = run;
|
|
264
279
|
|
|
265
280
|
// Abort wiring: cancelling the stream cancels the Cursor run.
|
|
266
281
|
onAbort = () => { run.cancel().catch(() => { /* already terminal */ }); };
|
|
267
|
-
|
|
282
|
+
controller.signal.addEventListener('abort', onAbort, { once: true });
|
|
268
283
|
|
|
269
284
|
// Drive the pull stream + result in the background, funnelling into the queue.
|
|
270
285
|
const settle = (async () => {
|
|
@@ -287,38 +302,63 @@ export class CursorEngine implements AIEngine {
|
|
|
287
302
|
} catch (error) {
|
|
288
303
|
handleStreamError(error);
|
|
289
304
|
} finally {
|
|
290
|
-
if (onAbort)
|
|
305
|
+
if (onAbort) controller.signal.removeEventListener('abort', onAbort);
|
|
291
306
|
try { agent.close(); } catch { /* fire-and-forget */ }
|
|
292
|
-
|
|
293
|
-
this.
|
|
294
|
-
this.
|
|
295
|
-
this.pendingAsks.clear();
|
|
307
|
+
// Retire THIS run only — another chat session of the same project may
|
|
308
|
+
// still be streaming on this instance.
|
|
309
|
+
this.forgetRun(runState);
|
|
296
310
|
}
|
|
297
311
|
}
|
|
298
312
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
313
|
+
/** Drop a finished run and the asks only it could answer. */
|
|
314
|
+
private forgetRun(runState: CursorRunState): void {
|
|
315
|
+
this.runs.remove(runState);
|
|
316
|
+
for (const [id, pending] of this.pendingAsks) {
|
|
317
|
+
if (pending.owner === runState) this.pendingAsks.delete(id);
|
|
303
318
|
}
|
|
304
|
-
|
|
319
|
+
runState.run = null;
|
|
320
|
+
runState.agent = null;
|
|
321
|
+
}
|
|
305
322
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
this.
|
|
323
|
+
/**
|
|
324
|
+
* Cancel the run whose AbortController is `owner`, and only that run.
|
|
325
|
+
*/
|
|
326
|
+
async cancel(owner: AbortController): Promise<void> {
|
|
327
|
+
await this.stopRuns(this.runs.select(owner));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Tear down the given runs. `cancel` passes the one run it was asked to
|
|
332
|
+
* stop; `dispose` passes them all. Nothing else may reach this.
|
|
333
|
+
*/
|
|
334
|
+
private async stopRuns(targets: CursorRunState[]): Promise<void> {
|
|
335
|
+
for (const runState of targets) {
|
|
336
|
+
// Release the parked AskUserQuestion callbacks of THIS run so it can
|
|
337
|
+
// settle. Another run's parked ask is left alone — it is still live and
|
|
338
|
+
// still waiting for a real answer.
|
|
339
|
+
for (const [id, pending] of this.pendingAsks) {
|
|
340
|
+
if (pending.owner !== runState) continue;
|
|
341
|
+
pending.resolve('User did not answer the question.');
|
|
342
|
+
this.pendingAsks.delete(id);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Abort the local controller first (cuts the for-await loop), then RPC.
|
|
346
|
+
if (!runState.controller.signal.aborted) runState.controller.abort();
|
|
311
347
|
|
|
312
|
-
|
|
313
|
-
|
|
348
|
+
const run = runState.run;
|
|
349
|
+
this.forgetRun(runState);
|
|
350
|
+
if (run) {
|
|
351
|
+
try { await run.cancel(); } catch { /* may already be terminal */ }
|
|
352
|
+
}
|
|
314
353
|
}
|
|
315
|
-
this.activeRun = null;
|
|
316
|
-
this.activeAgent = null;
|
|
317
354
|
}
|
|
318
355
|
|
|
319
|
-
async interrupt(): Promise<void> {
|
|
320
|
-
|
|
321
|
-
|
|
356
|
+
async interrupt(owner: AbortController): Promise<void> {
|
|
357
|
+
const targets = this.runs.select(owner);
|
|
358
|
+
for (const runState of targets) {
|
|
359
|
+
if (runState.run) {
|
|
360
|
+
try { await runState.run.cancel(); } catch { /* ignore */ }
|
|
361
|
+
}
|
|
322
362
|
}
|
|
323
363
|
}
|
|
324
364
|
|