@causyne/agent 1.20.5 → 1.20.8
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/README.md +25 -3
- package/SECURITY.md +1 -4
- package/package.json +1 -1
- package/src/acp/agentCatalog.js +9 -3
- package/src/acp/agentCatalog.ts +20 -4
- package/src/acp/client.js +174 -25
- package/src/acp/client.ts +165 -24
- package/src/acp/protocol.js +116 -0
- package/src/acp/protocol.ts +163 -0
- package/src/agentLoop.js +180 -8
- package/src/agentLoop.ts +185 -8
- package/src/commands.js +25 -10
- package/src/dynamicCatalogue.js +568 -77
- package/src/dynamicCatalogue.ts +608 -79
- package/src/providers/acp.js +141 -19
- package/src/providers/acp.ts +142 -19
- package/src/providers/antigravity.d.ts +7 -1
- package/src/providers/antigravity.js +135 -18
- package/src/providers/antigravity.ts +139 -18
- package/src/providers/base.d.ts +1 -0
- package/src/providers/pi.js +5 -1
- package/src/providers/pi.ts +5 -1
- package/src/tui/activityLayout.js +9 -0
- package/src/tui/activityLayout.ts +10 -0
- package/src/tui/chatRenderer.d.ts +2 -0
- package/src/tui/chatRenderer.js +15 -2
- package/src/tui/codeStreamRenderer.js +16 -0
- package/src/tui/codeStreamRenderer.ts +17 -0
- package/src/tui/composerControls.js +65 -0
- package/src/tui/composerControls.ts +106 -0
- package/src/tui/modelPicker.js +288 -69
- package/src/tui/modelPicker.ts +333 -72
- package/src/tui/prompt.js +23 -25
- package/src/tui/text.d.ts +2 -1
package/README.md
CHANGED
|
@@ -12,11 +12,33 @@ documentation is intentionally concise while the project is evolving.
|
|
|
12
12
|
|
|
13
13
|
Learn more at [causyne.com](https://causyne.com/).
|
|
14
14
|
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
This build is provided specifically for debugging, testing, and architectural evaluation. If you are exploring or debugging the harness, you can install it using the automated script or directly through npm.
|
|
18
|
+
|
|
19
|
+
For macOS, Linux, or WSL:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
curl -fsSL https://causyne.com/install.sh | bash
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
For Windows PowerShell:
|
|
26
|
+
|
|
27
|
+
```powershell
|
|
28
|
+
irm https://causyne.com/install.ps1 | iex
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Alternatively, you can install the global package via npm:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm install -g @causyne/agent
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Please treat this release as an experimental preview built for debugging and hands-on testing rather than day-to-day production workloads. As the developer behind Causyne, I would be genuinely grateful for your feedback, edge-case findings, and diagnostic reports. If you run into unexpected behavior or have thoughts on how the runtime behaves, please open an issue on GitHub or reach out directly to help@causyne.com.
|
|
38
|
+
|
|
15
39
|
## Contact
|
|
16
40
|
|
|
17
|
-
|
|
18
|
-
- Product support and troubleshooting: [help@causyne.com](mailto:help@causyne.com)
|
|
19
|
-
- Partnerships and direct communication: [ikbal@causyne.com](mailto:ikbal@causyne.com)
|
|
41
|
+
For general inquiries and business communication, reach out to contact@causyne.com. For technical questions, bug reports, and diagnostic feedback, email help@causyne.com. For security disclosures and vulnerability reports, email security@causyne.com. For partnerships and direct collaboration, write to ikbal@causyne.com.
|
|
20
42
|
|
|
21
43
|
## Development
|
|
22
44
|
|
package/SECURITY.md
CHANGED
|
@@ -18,7 +18,4 @@ cannot prove that a repository contains no secrets.
|
|
|
18
18
|
|
|
19
19
|
## Reporting a vulnerability
|
|
20
20
|
|
|
21
|
-
Please avoid publishing exploit details in a public issue.
|
|
22
|
-
private security-reporting channel when it becomes available. Until that channel
|
|
23
|
-
is published, retain the report privately rather than exposing user data or a
|
|
24
|
-
working exploit.
|
|
21
|
+
Please avoid publishing exploit details in a public issue. Report any security concern or vulnerability directly to security@causyne.com so it can be investigated and resolved promptly.
|
package/package.json
CHANGED
package/src/acp/agentCatalog.js
CHANGED
|
@@ -259,6 +259,7 @@ export async function testPeerHandshake(options) {
|
|
|
259
259
|
const clientOptions = {
|
|
260
260
|
command: options.command,
|
|
261
261
|
cwd,
|
|
262
|
+
env: { ...process.env, CI: '1', BROWSER: 'false', NO_BROWSER: '1' },
|
|
262
263
|
promptTimeoutMs: options.timeoutMs || 15000
|
|
263
264
|
};
|
|
264
265
|
if (options.args) {
|
|
@@ -275,13 +276,16 @@ export async function testPeerHandshake(options) {
|
|
|
275
276
|
const authMethods = Array.isArray(init.authMethods)
|
|
276
277
|
? init.authMethods.map((m) => (typeof m === 'object' && m !== null && 'id' in m ? String(m.id) : String(m)))
|
|
277
278
|
: [];
|
|
278
|
-
// Attempt session/new to ensure agent is fully operational
|
|
279
|
+
// Attempt session/new to ensure agent is fully operational and extract live models
|
|
279
280
|
try {
|
|
280
|
-
await client.newSession(cwd);
|
|
281
|
+
const sessionId = await client.newSession(cwd);
|
|
282
|
+
const sessionModels = client.getSessionModels(sessionId);
|
|
281
283
|
return {
|
|
282
284
|
ready: true,
|
|
283
285
|
...(agentInfo ? { agentInfo } : {}),
|
|
284
|
-
authMethods
|
|
286
|
+
authMethods,
|
|
287
|
+
...(sessionModels.availableModels.length > 0 ? { availableModels: sessionModels.availableModels } : {}),
|
|
288
|
+
...(sessionModels.currentModelId ? { currentModelId: sessionModels.currentModelId } : {})
|
|
285
289
|
};
|
|
286
290
|
}
|
|
287
291
|
catch (sessionErr) {
|
|
@@ -372,6 +376,8 @@ export async function detectAllPeers(cwd = process.cwd()) {
|
|
|
372
376
|
args: activeArgs,
|
|
373
377
|
...(handshake.agentInfo ? { agentInfo: handshake.agentInfo } : {}),
|
|
374
378
|
...(handshake.authMethods ? { authMethods: handshake.authMethods } : {}),
|
|
379
|
+
...(handshake.availableModels ? { availableModels: handshake.availableModels } : {}),
|
|
380
|
+
...(handshake.currentModelId ? { currentModelId: handshake.currentModelId } : {}),
|
|
375
381
|
details: 'Operational and ready for delegation.'
|
|
376
382
|
});
|
|
377
383
|
}
|
package/src/acp/agentCatalog.ts
CHANGED
|
@@ -48,6 +48,8 @@ export interface PeerDetectionResult {
|
|
|
48
48
|
details?: string;
|
|
49
49
|
agentInfo?: { name: string; version: string };
|
|
50
50
|
authMethods?: string[];
|
|
51
|
+
availableModels?: string[];
|
|
52
|
+
currentModelId?: string;
|
|
51
53
|
setupInstruction?: string;
|
|
52
54
|
}
|
|
53
55
|
|
|
@@ -309,12 +311,21 @@ export async function testPeerHandshake(
|
|
|
309
311
|
ready: boolean;
|
|
310
312
|
agentInfo?: { name: string; version: string };
|
|
311
313
|
authMethods?: string[];
|
|
314
|
+
availableModels?: string[];
|
|
315
|
+
currentModelId?: string;
|
|
312
316
|
error?: string;
|
|
313
317
|
}> {
|
|
314
318
|
const cwd = options.cwd || process.cwd();
|
|
315
|
-
const clientOptions: {
|
|
319
|
+
const clientOptions: {
|
|
320
|
+
command: string;
|
|
321
|
+
args?: string[];
|
|
322
|
+
cwd: string;
|
|
323
|
+
env: NodeJS.ProcessEnv;
|
|
324
|
+
promptTimeoutMs: number;
|
|
325
|
+
} = {
|
|
316
326
|
command: options.command,
|
|
317
327
|
cwd,
|
|
328
|
+
env: { ...process.env, CI: '1', BROWSER: 'false', NO_BROWSER: '1' },
|
|
318
329
|
promptTimeoutMs: options.timeoutMs || 15000
|
|
319
330
|
};
|
|
320
331
|
if (options.args) {
|
|
@@ -336,13 +347,16 @@ export async function testPeerHandshake(
|
|
|
336
347
|
? init.authMethods.map((m) => (typeof m === 'object' && m !== null && 'id' in m ? String((m as { id: unknown }).id) : String(m)))
|
|
337
348
|
: [];
|
|
338
349
|
|
|
339
|
-
// Attempt session/new to ensure agent is fully operational
|
|
350
|
+
// Attempt session/new to ensure agent is fully operational and extract live models
|
|
340
351
|
try {
|
|
341
|
-
await client.newSession(cwd);
|
|
352
|
+
const sessionId = await client.newSession(cwd);
|
|
353
|
+
const sessionModels = client.getSessionModels(sessionId);
|
|
342
354
|
return {
|
|
343
355
|
ready: true,
|
|
344
356
|
...(agentInfo ? { agentInfo } : {}),
|
|
345
|
-
authMethods
|
|
357
|
+
authMethods,
|
|
358
|
+
...(sessionModels.availableModels.length > 0 ? { availableModels: sessionModels.availableModels } : {}),
|
|
359
|
+
...(sessionModels.currentModelId ? { currentModelId: sessionModels.currentModelId } : {})
|
|
346
360
|
};
|
|
347
361
|
} catch (sessionErr: unknown) {
|
|
348
362
|
const msg = sessionErr instanceof Error ? sessionErr.message : String(sessionErr);
|
|
@@ -439,6 +453,8 @@ export async function detectAllPeers(cwd: string = process.cwd()): Promise<PeerD
|
|
|
439
453
|
args: activeArgs,
|
|
440
454
|
...(handshake.agentInfo ? { agentInfo: handshake.agentInfo } : {}),
|
|
441
455
|
...(handshake.authMethods ? { authMethods: handshake.authMethods } : {}),
|
|
456
|
+
...(handshake.availableModels ? { availableModels: handshake.availableModels } : {}),
|
|
457
|
+
...(handshake.currentModelId ? { currentModelId: handshake.currentModelId } : {}),
|
|
442
458
|
details: 'Operational and ready for delegation.'
|
|
443
459
|
});
|
|
444
460
|
} else if (handshake.error && /auth/i.test(handshake.error)) {
|
package/src/acp/client.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import fs from 'node:fs/promises';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
-
import { ACP_ERROR_CODES, ACP_METHODS, ACP_PROTOCOL_VERSION, isJsonRpcRequest, isJsonRpcResponse } from './protocol.js';
|
|
4
|
+
import { ACP_ERROR_CODES, ACP_METHODS, ACP_PROTOCOL_VERSION, extractAcpSessionModels, isJsonRpcRequest, isJsonRpcResponse } from './protocol.js';
|
|
5
|
+
function createAbortError(message = 'ACP operation aborted by user') {
|
|
6
|
+
const err = new Error(message);
|
|
7
|
+
err.name = 'AbortError';
|
|
8
|
+
return err;
|
|
9
|
+
}
|
|
5
10
|
const MAX_STDERR_CHARS = 4000;
|
|
6
11
|
const MAX_UPDATE_CHARS = 200_000;
|
|
7
12
|
export class AcpClient {
|
|
@@ -14,6 +19,12 @@ export class AcpClient {
|
|
|
14
19
|
closed = false;
|
|
15
20
|
exitError = null;
|
|
16
21
|
initResult = null;
|
|
22
|
+
sessionModels = new Map();
|
|
23
|
+
latestSessionModels = {
|
|
24
|
+
availableModels: [],
|
|
25
|
+
modelLabels: {},
|
|
26
|
+
usesConfigOption: false
|
|
27
|
+
};
|
|
17
28
|
constructor(options) {
|
|
18
29
|
this.options = options;
|
|
19
30
|
}
|
|
@@ -29,7 +40,55 @@ export class AcpClient {
|
|
|
29
40
|
get authMethods() {
|
|
30
41
|
return this.initResult?.authMethods;
|
|
31
42
|
}
|
|
32
|
-
|
|
43
|
+
get discoveredModels() {
|
|
44
|
+
return [...this.latestSessionModels.availableModels];
|
|
45
|
+
}
|
|
46
|
+
get currentModelId() {
|
|
47
|
+
return this.latestSessionModels.currentModelId;
|
|
48
|
+
}
|
|
49
|
+
get modelLabels() {
|
|
50
|
+
return { ...this.latestSessionModels.modelLabels };
|
|
51
|
+
}
|
|
52
|
+
getSessionModels(sessionId) {
|
|
53
|
+
if (sessionId && this.sessionModels.has(sessionId)) {
|
|
54
|
+
const state = this.sessionModels.get(sessionId);
|
|
55
|
+
return {
|
|
56
|
+
currentModelId: state.currentModelId,
|
|
57
|
+
availableModels: [...state.availableModels],
|
|
58
|
+
modelLabels: { ...state.modelLabels },
|
|
59
|
+
usesConfigOption: state.usesConfigOption,
|
|
60
|
+
configOptionId: state.configOptionId
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
currentModelId: this.latestSessionModels.currentModelId,
|
|
65
|
+
availableModels: [...this.latestSessionModels.availableModels],
|
|
66
|
+
modelLabels: { ...this.latestSessionModels.modelLabels },
|
|
67
|
+
usesConfigOption: this.latestSessionModels.usesConfigOption,
|
|
68
|
+
configOptionId: this.latestSessionModels.configOptionId
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
recordSessionModels(sessionId, payload) {
|
|
72
|
+
const extracted = extractAcpSessionModels(payload);
|
|
73
|
+
if (extracted.availableModels.length === 0 && !extracted.currentModelId) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const existing = this.sessionModels.get(sessionId);
|
|
77
|
+
const merged = {
|
|
78
|
+
currentModelId: extracted.currentModelId ?? existing?.currentModelId,
|
|
79
|
+
availableModels: extracted.availableModels.length > 0
|
|
80
|
+
? extracted.availableModels
|
|
81
|
+
: (existing?.availableModels ?? []),
|
|
82
|
+
modelLabels: { ...(existing?.modelLabels ?? {}), ...extracted.modelLabels },
|
|
83
|
+
usesConfigOption: extracted.usesConfigOption || Boolean(existing?.usesConfigOption),
|
|
84
|
+
configOptionId: extracted.configOptionId ?? existing?.configOptionId
|
|
85
|
+
};
|
|
86
|
+
this.sessionModels.set(sessionId, merged);
|
|
87
|
+
this.latestSessionModels = merged;
|
|
88
|
+
}
|
|
89
|
+
async start(signal) {
|
|
90
|
+
if (signal?.aborted)
|
|
91
|
+
throw createAbortError();
|
|
33
92
|
if (this.child)
|
|
34
93
|
throw new Error('client already started');
|
|
35
94
|
const args = this.options.args ?? [];
|
|
@@ -42,6 +101,8 @@ export class AcpClient {
|
|
|
42
101
|
this.child = child;
|
|
43
102
|
child.stdout.setEncoding('utf8');
|
|
44
103
|
child.stdout.on('data', (chunk) => {
|
|
104
|
+
if (this.closed)
|
|
105
|
+
return;
|
|
45
106
|
this.buffer += chunk;
|
|
46
107
|
void this.drain();
|
|
47
108
|
});
|
|
@@ -50,8 +111,8 @@ export class AcpClient {
|
|
|
50
111
|
this.stderrTail = `${this.stderrTail}${chunk}`.slice(-MAX_STDERR_CHARS);
|
|
51
112
|
});
|
|
52
113
|
child.on('error', (error) => this.failAll(new Error(`agent process failed: ${error.message}`)));
|
|
53
|
-
child.on('exit', (code,
|
|
54
|
-
const detail = this.stderrTail.trim() || `exit code ${code ?? 'null'}${
|
|
114
|
+
child.on('exit', (code, exitSignal) => {
|
|
115
|
+
const detail = this.stderrTail.trim() || `exit code ${code ?? 'null'}${exitSignal ? ` signal ${exitSignal}` : ''}`;
|
|
55
116
|
this.failAll(new Error(`agent process exited: ${detail}`));
|
|
56
117
|
});
|
|
57
118
|
const hasFsRead = Boolean(this.options.fsReadHandler || this.options.cwd);
|
|
@@ -65,18 +126,19 @@ export class AcpClient {
|
|
|
65
126
|
},
|
|
66
127
|
clientInfo: { name: 'Causyne Agent', version: this.options.clientVersion ?? '0.0.0' }
|
|
67
128
|
};
|
|
68
|
-
const result = await this.request(ACP_METHODS.INITIALIZE, params, 30_000);
|
|
129
|
+
const result = await this.request(ACP_METHODS.INITIALIZE, params, 30_000, signal);
|
|
69
130
|
this.initResult = result;
|
|
70
131
|
return result;
|
|
71
132
|
}
|
|
72
|
-
async newSession(cwd, mcpServers = []) {
|
|
133
|
+
async newSession(cwd, mcpServers = [], signal) {
|
|
73
134
|
const result = await this.request(ACP_METHODS.NEW_SESSION, {
|
|
74
135
|
cwd,
|
|
75
136
|
mcpServers
|
|
76
|
-
}, 30_000);
|
|
137
|
+
}, 30_000, signal);
|
|
77
138
|
if (!result || typeof result.sessionId !== 'string' || !result.sessionId) {
|
|
78
139
|
throw new Error('agent did not return a session id');
|
|
79
140
|
}
|
|
141
|
+
this.recordSessionModels(result.sessionId, result);
|
|
80
142
|
return result.sessionId;
|
|
81
143
|
}
|
|
82
144
|
async authenticate(methodId, params = {}) {
|
|
@@ -85,11 +147,36 @@ export class AcpClient {
|
|
|
85
147
|
params
|
|
86
148
|
}, 30_000);
|
|
87
149
|
}
|
|
88
|
-
async setSessionModel(sessionId, modelId) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
150
|
+
async setSessionModel(sessionId, modelId, signal) {
|
|
151
|
+
const state = this.sessionModels.get(sessionId) ?? this.latestSessionModels;
|
|
152
|
+
try {
|
|
153
|
+
const res = await this.request(ACP_METHODS.SET_MODEL, {
|
|
154
|
+
sessionId,
|
|
155
|
+
modelId
|
|
156
|
+
}, 5_000, signal);
|
|
157
|
+
this.recordSessionModels(sessionId, res);
|
|
158
|
+
const updated = this.sessionModels.get(sessionId);
|
|
159
|
+
if (updated) {
|
|
160
|
+
updated.currentModelId = modelId;
|
|
161
|
+
this.latestSessionModels = updated;
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
if (signal?.aborted || (err instanceof Error && err.name === 'AbortError')) {
|
|
167
|
+
throw err;
|
|
168
|
+
}
|
|
169
|
+
if (state.usesConfigOption) {
|
|
170
|
+
const res = await this.request(ACP_METHODS.SET_CONFIG_OPTION, {
|
|
171
|
+
sessionId,
|
|
172
|
+
configId: state.configOptionId || 'model',
|
|
173
|
+
value: modelId
|
|
174
|
+
}, 5_000, signal);
|
|
175
|
+
this.recordSessionModels(sessionId, res);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
throw err;
|
|
179
|
+
}
|
|
93
180
|
}
|
|
94
181
|
async setSessionMode(sessionId, modeId) {
|
|
95
182
|
await this.request(ACP_METHODS.SET_MODE, {
|
|
@@ -98,15 +185,21 @@ export class AcpClient {
|
|
|
98
185
|
}, 30_000);
|
|
99
186
|
}
|
|
100
187
|
async loadSession(sessionId, cwd) {
|
|
101
|
-
|
|
188
|
+
const res = await this.request(ACP_METHODS.LOAD, {
|
|
102
189
|
sessionId,
|
|
103
190
|
cwd: cwd || this.options.cwd
|
|
104
191
|
}, 30_000);
|
|
192
|
+
this.recordSessionModels(sessionId, res);
|
|
193
|
+
return res;
|
|
105
194
|
}
|
|
106
|
-
async prompt(sessionId, input) {
|
|
195
|
+
async prompt(sessionId, input, signal) {
|
|
196
|
+
if (signal?.aborted)
|
|
197
|
+
throw createAbortError();
|
|
107
198
|
const blocks = typeof input === 'string' ? [{ type: 'text', text: input }] : input;
|
|
108
199
|
let collected = '';
|
|
109
200
|
const collector = (update) => {
|
|
201
|
+
if (signal?.aborted)
|
|
202
|
+
return;
|
|
110
203
|
// handleLine already notifies options.onUpdate for this session. Calling
|
|
111
204
|
// it again here duplicated every live chunk and file-action frame.
|
|
112
205
|
if (update.sessionUpdate === 'agent_message_chunk' && update.content.type === 'text') {
|
|
@@ -115,21 +208,33 @@ export class AcpClient {
|
|
|
115
208
|
};
|
|
116
209
|
const previous = this.updateListener;
|
|
117
210
|
this.updateListener = (sessionIdFromEvent, update) => {
|
|
211
|
+
if (signal?.aborted)
|
|
212
|
+
return;
|
|
118
213
|
previous?.(sessionIdFromEvent, update);
|
|
119
214
|
if (sessionIdFromEvent === sessionId)
|
|
120
215
|
collector(update);
|
|
121
216
|
};
|
|
217
|
+
const onAbort = () => {
|
|
218
|
+
try {
|
|
219
|
+
this.notifyCancel(sessionId);
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
// ignore cancel write failure
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
122
226
|
try {
|
|
123
227
|
const response = await this.request(ACP_METHODS.PROMPT, {
|
|
124
228
|
sessionId,
|
|
125
229
|
prompt: blocks
|
|
126
|
-
}, this.options.promptTimeoutMs ?? 15 * 60 * 1000);
|
|
230
|
+
}, this.options.promptTimeoutMs ?? 15 * 60 * 1000, signal);
|
|
127
231
|
return {
|
|
128
232
|
stopReason: response?.stopReason ?? 'end_turn',
|
|
129
233
|
text: collected
|
|
130
234
|
};
|
|
131
235
|
}
|
|
132
236
|
finally {
|
|
237
|
+
signal?.removeEventListener('abort', onAbort);
|
|
133
238
|
this.updateListener = previous;
|
|
134
239
|
}
|
|
135
240
|
}
|
|
@@ -145,21 +250,46 @@ export class AcpClient {
|
|
|
145
250
|
return;
|
|
146
251
|
this.closed = true;
|
|
147
252
|
const child = this.child;
|
|
253
|
+
this.child = null;
|
|
148
254
|
this.failAll(new Error('client closed'));
|
|
149
255
|
if (!child)
|
|
150
256
|
return;
|
|
257
|
+
if (child.exitCode !== null || child.signalCode !== null)
|
|
258
|
+
return;
|
|
151
259
|
await new Promise((resolve) => {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
260
|
+
let settled = false;
|
|
261
|
+
const done = () => {
|
|
262
|
+
if (settled)
|
|
263
|
+
return;
|
|
264
|
+
settled = true;
|
|
157
265
|
clearTimeout(timer);
|
|
158
266
|
resolve();
|
|
159
|
-
}
|
|
160
|
-
|
|
267
|
+
};
|
|
268
|
+
const timer = setTimeout(() => {
|
|
269
|
+
try {
|
|
270
|
+
child.kill('SIGKILL');
|
|
271
|
+
}
|
|
272
|
+
catch { /* ignore */ }
|
|
273
|
+
done();
|
|
274
|
+
}, 250);
|
|
275
|
+
if (typeof timer.unref === 'function')
|
|
276
|
+
timer.unref();
|
|
277
|
+
child.once('exit', done);
|
|
278
|
+
try {
|
|
279
|
+
child.stdin.destroy();
|
|
280
|
+
child.stdout.destroy();
|
|
281
|
+
child.stderr.destroy();
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
// ignore stream destroy errors
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
child.kill('SIGTERM');
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
done();
|
|
291
|
+
}
|
|
161
292
|
});
|
|
162
|
-
this.child = null;
|
|
163
293
|
}
|
|
164
294
|
updateListener = null;
|
|
165
295
|
write(message) {
|
|
@@ -202,6 +332,7 @@ export class AcpClient {
|
|
|
202
332
|
if (method === ACP_METHODS.UPDATE) {
|
|
203
333
|
const params = parsed.params;
|
|
204
334
|
if (params && typeof params.sessionId === 'string' && params.update) {
|
|
335
|
+
this.recordSessionModels(params.sessionId, params.update);
|
|
205
336
|
this.updateListener?.(params.sessionId, params.update);
|
|
206
337
|
this.options.onUpdate?.(params.sessionId, params.update);
|
|
207
338
|
}
|
|
@@ -318,6 +449,7 @@ export class AcpClient {
|
|
|
318
449
|
if (!pending)
|
|
319
450
|
return;
|
|
320
451
|
clearTimeout(pending.timer);
|
|
452
|
+
pending.cleanup?.();
|
|
321
453
|
this.pending.delete(response.id);
|
|
322
454
|
if (response.error) {
|
|
323
455
|
const err = new Error(`${pending.method} failed: ${response.error.message}`);
|
|
@@ -328,22 +460,38 @@ export class AcpClient {
|
|
|
328
460
|
}
|
|
329
461
|
pending.resolve(response.result);
|
|
330
462
|
}
|
|
331
|
-
request(method, params, timeoutMs) {
|
|
463
|
+
request(method, params, timeoutMs, signal) {
|
|
464
|
+
if (signal?.aborted)
|
|
465
|
+
return Promise.reject(createAbortError());
|
|
332
466
|
if (!this.child || this.closed)
|
|
333
467
|
return Promise.reject(new Error('agent process is not running'));
|
|
334
468
|
if (this.exitError)
|
|
335
469
|
return Promise.reject(this.exitError);
|
|
336
470
|
const id = this.nextId++;
|
|
337
471
|
return new Promise((resolve, reject) => {
|
|
472
|
+
const cleanup = () => {
|
|
473
|
+
signal?.removeEventListener('abort', onAbort);
|
|
474
|
+
};
|
|
475
|
+
const onAbort = () => {
|
|
476
|
+
clearTimeout(timer);
|
|
477
|
+
cleanup();
|
|
478
|
+
this.pending.delete(id);
|
|
479
|
+
reject(createAbortError());
|
|
480
|
+
};
|
|
338
481
|
const timer = setTimeout(() => {
|
|
482
|
+
cleanup();
|
|
339
483
|
this.pending.delete(id);
|
|
340
484
|
reject(new Error(`${method} timed out after ${timeoutMs}ms`));
|
|
341
485
|
}, timeoutMs);
|
|
486
|
+
if (typeof timer.unref === 'function')
|
|
487
|
+
timer.unref();
|
|
488
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
342
489
|
this.pending.set(id, {
|
|
343
490
|
resolve: (value) => resolve(value),
|
|
344
491
|
reject,
|
|
345
492
|
timer,
|
|
346
|
-
method
|
|
493
|
+
method,
|
|
494
|
+
cleanup
|
|
347
495
|
});
|
|
348
496
|
this.write({ jsonrpc: '2.0', id, method, params });
|
|
349
497
|
});
|
|
@@ -352,6 +500,7 @@ export class AcpClient {
|
|
|
352
500
|
this.exitError = error;
|
|
353
501
|
for (const [id, pending] of this.pending) {
|
|
354
502
|
clearTimeout(pending.timer);
|
|
503
|
+
pending.cleanup?.();
|
|
355
504
|
pending.reject(error);
|
|
356
505
|
this.pending.delete(id);
|
|
357
506
|
}
|