@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/src/providers/acp.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { canonicalModelId } from '../modelIds.js';
|
|
2
2
|
import { DEFAULT_MODEL_BY_PROVIDER } from '../modelCatalogue.js';
|
|
3
|
+
import { saveCachedModels } from '../dynamicCatalogue.js';
|
|
3
4
|
import { LLMProvider } from './base.js';
|
|
4
5
|
import { AcpClient } from '../acp/client.js';
|
|
5
6
|
import { getPeerProfile } from '../acp/peerProfiles.js';
|
|
@@ -17,6 +18,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
17
18
|
args;
|
|
18
19
|
activeClient = null;
|
|
19
20
|
activeSessionId = null;
|
|
21
|
+
currentTurnAborted = false;
|
|
20
22
|
currentTurnChunkHandler = null;
|
|
21
23
|
currentTurnReasoningHandler = null;
|
|
22
24
|
currentTurnNativeActionHandler = null;
|
|
@@ -45,7 +47,11 @@ export class AcpProvider extends LLMProvider {
|
|
|
45
47
|
const modelIsPeerName = !configuredModel || configuredModel === 'acp' || configuredModel === this.peerId;
|
|
46
48
|
this.model = canonicalModelId(modelIsPeerName ? peerDefault : configuredModel);
|
|
47
49
|
const profile = getPeerProfile(this.peerId);
|
|
48
|
-
if (
|
|
50
|
+
if (config.command) {
|
|
51
|
+
this.command = config.command;
|
|
52
|
+
this.args = config.args || ['acp'];
|
|
53
|
+
}
|
|
54
|
+
else if (profile) {
|
|
49
55
|
this.command = profile.command;
|
|
50
56
|
this.args = profile.args || [];
|
|
51
57
|
}
|
|
@@ -56,7 +62,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
56
62
|
this.args = preset.args || [];
|
|
57
63
|
}
|
|
58
64
|
else {
|
|
59
|
-
this.command =
|
|
65
|
+
this.command = this.peerId;
|
|
60
66
|
this.args = config.args || ['acp'];
|
|
61
67
|
}
|
|
62
68
|
}
|
|
@@ -267,9 +273,41 @@ export class AcpProvider extends LLMProvider {
|
|
|
267
273
|
}
|
|
268
274
|
}
|
|
269
275
|
}
|
|
276
|
+
if (calls.length === 0) {
|
|
277
|
+
const openIndex = text.lastIndexOf('<tool_call>');
|
|
278
|
+
if (openIndex !== -1 && text.indexOf('</tool_call>', openIndex) === -1) {
|
|
279
|
+
const partial = text.slice(openIndex + '<tool_call>'.length).trim();
|
|
280
|
+
for (const suffix of ['', '}', '}}', '}}}', '"}', '"}}', '"}}}', '"}]}}', '"]}}', '"]}}}']) {
|
|
281
|
+
try {
|
|
282
|
+
const parsed = JSON.parse(partial + suffix);
|
|
283
|
+
const name = parsed.name || parsed.tool || parsed.function?.name;
|
|
284
|
+
if (name) {
|
|
285
|
+
const rawArgs = parsed.arguments !== undefined ? parsed.arguments : (parsed.parameters !== undefined ? parsed.parameters : (parsed.input !== undefined ? parsed.input : (parsed.function?.arguments || {})));
|
|
286
|
+
calls.push({
|
|
287
|
+
id: `acp_call_${Date.now()}_0`,
|
|
288
|
+
type: 'function',
|
|
289
|
+
function: {
|
|
290
|
+
name,
|
|
291
|
+
arguments: typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs || {})
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
// try next candidate
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
270
303
|
return calls;
|
|
271
304
|
}
|
|
272
|
-
async getOrCreateClient() {
|
|
305
|
+
async getOrCreateClient(signal) {
|
|
306
|
+
if (signal?.aborted) {
|
|
307
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
308
|
+
abortErr.name = 'AbortError';
|
|
309
|
+
throw abortErr;
|
|
310
|
+
}
|
|
273
311
|
if (this.activeClient && this.activeSessionId) {
|
|
274
312
|
return { client: this.activeClient, sessionId: this.activeSessionId };
|
|
275
313
|
}
|
|
@@ -277,14 +315,18 @@ export class AcpProvider extends LLMProvider {
|
|
|
277
315
|
command: this.command,
|
|
278
316
|
args: this.args,
|
|
279
317
|
cwd: process.cwd(),
|
|
280
|
-
promptTimeoutMs:
|
|
318
|
+
promptTimeoutMs: 900_000,
|
|
281
319
|
permissionHandler: async (req) => {
|
|
320
|
+
if (this.currentTurnAborted || signal?.aborted)
|
|
321
|
+
return { outcome: 'cancelled' };
|
|
282
322
|
const allowOpt = req.options?.find(o => o.kind === 'allow_once' || o.kind === 'allow_always' || o.optionId === 'allow');
|
|
283
323
|
if (allowOpt)
|
|
284
324
|
return { outcome: 'selected', optionId: allowOpt.optionId };
|
|
285
325
|
return { outcome: 'cancelled' };
|
|
286
326
|
},
|
|
287
327
|
onUpdate: (_sessionId, update) => {
|
|
328
|
+
if (this.currentTurnAborted)
|
|
329
|
+
return;
|
|
288
330
|
if (update && typeof update === 'object') {
|
|
289
331
|
const updateObj = update;
|
|
290
332
|
if (updateObj.sessionUpdate === 'agent_thought_chunk') {
|
|
@@ -333,55 +375,126 @@ export class AcpProvider extends LLMProvider {
|
|
|
333
375
|
}
|
|
334
376
|
}
|
|
335
377
|
});
|
|
336
|
-
await client.start();
|
|
337
|
-
const sessionId = await client.newSession(process.cwd());
|
|
378
|
+
await client.start(signal);
|
|
379
|
+
const sessionId = await client.newSession(process.cwd(), [], signal);
|
|
380
|
+
const sessionModels = client.getSessionModels(sessionId);
|
|
381
|
+
const liveModels = sessionModels.availableModels;
|
|
382
|
+
if (liveModels.length > 0 && !process.env.NODE_TEST_CONTEXT) {
|
|
383
|
+
saveCachedModels(`acp/${this.peerId}`, liveModels, {
|
|
384
|
+
...(sessionModels.currentModelId ? { currentModel: sessionModels.currentModelId } : {})
|
|
385
|
+
});
|
|
386
|
+
}
|
|
338
387
|
let targetModel = this.model;
|
|
339
|
-
if (this.peerId === 'opencode' && targetModel !== this.peerId && !targetModel.
|
|
388
|
+
if (this.peerId === 'opencode' && targetModel !== this.peerId && !targetModel.includes('/')) {
|
|
340
389
|
targetModel = `opencode/${targetModel}`;
|
|
341
390
|
}
|
|
391
|
+
if (liveModels.length > 0 && targetModel && !liveModels.includes(targetModel)) {
|
|
392
|
+
const matched = liveModels.find((m) => m.toLowerCase() === targetModel.toLowerCase() || m.endsWith(`/${targetModel}`));
|
|
393
|
+
if (matched) {
|
|
394
|
+
targetModel = matched;
|
|
395
|
+
this.model = matched;
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
const staticDefault = DEFAULT_MODEL_BY_PROVIDER[`acp/${this.peerId}`];
|
|
399
|
+
if (targetModel === staticDefault || targetModel === this.peerId || targetModel === 'acp') {
|
|
400
|
+
targetModel = sessionModels.currentModelId || liveModels[0];
|
|
401
|
+
this.model = targetModel;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
342
405
|
if (targetModel && targetModel !== 'acp' && targetModel !== this.peerId) {
|
|
343
406
|
try {
|
|
344
|
-
await client.setSessionModel(sessionId, targetModel);
|
|
407
|
+
await client.setSessionModel(sessionId, targetModel, signal);
|
|
345
408
|
}
|
|
346
|
-
catch {
|
|
347
|
-
|
|
409
|
+
catch (err) {
|
|
410
|
+
if (this.currentTurnAborted || signal?.aborted || err?.name === 'AbortError') {
|
|
411
|
+
await client.close();
|
|
412
|
+
throw err;
|
|
413
|
+
}
|
|
414
|
+
// If the requested model is unavailable on the user's current account/tier,
|
|
415
|
+
// gracefully fall back to the peer's active or first advertised live model.
|
|
416
|
+
const fallbackModel = sessionModels.currentModelId || liveModels[0];
|
|
417
|
+
if (fallbackModel && fallbackModel !== targetModel) {
|
|
418
|
+
this.model = fallbackModel;
|
|
419
|
+
try {
|
|
420
|
+
await client.setSessionModel(sessionId, fallbackModel, signal);
|
|
421
|
+
}
|
|
422
|
+
catch {
|
|
423
|
+
// Peer already defaults to its own active session model
|
|
424
|
+
}
|
|
425
|
+
}
|
|
348
426
|
}
|
|
349
427
|
}
|
|
350
428
|
this.activeClient = client;
|
|
351
429
|
this.activeSessionId = sessionId;
|
|
352
430
|
return { client, sessionId };
|
|
353
431
|
}
|
|
432
|
+
abort() {
|
|
433
|
+
this.currentTurnAborted = true;
|
|
434
|
+
this.currentTurnChunkHandler = null;
|
|
435
|
+
this.currentTurnReasoningHandler = null;
|
|
436
|
+
this.currentTurnNativeActionHandler = null;
|
|
437
|
+
this.liveFileActions.clear();
|
|
438
|
+
if (this.activeClient && this.activeSessionId) {
|
|
439
|
+
try {
|
|
440
|
+
this.activeClient.notifyCancel(this.activeSessionId);
|
|
441
|
+
}
|
|
442
|
+
catch {
|
|
443
|
+
// ignore cancel write error
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
void this.close();
|
|
447
|
+
}
|
|
354
448
|
async close() {
|
|
355
|
-
|
|
449
|
+
const client = this.activeClient;
|
|
450
|
+
this.activeClient = null;
|
|
451
|
+
this.activeSessionId = null;
|
|
452
|
+
if (client) {
|
|
356
453
|
try {
|
|
357
|
-
await
|
|
454
|
+
await client.close();
|
|
358
455
|
}
|
|
359
456
|
catch {
|
|
360
457
|
// ignore close error
|
|
361
458
|
}
|
|
362
|
-
this.activeClient = null;
|
|
363
|
-
this.activeSessionId = null;
|
|
364
459
|
}
|
|
365
460
|
}
|
|
366
461
|
async chat(messages, options = {}) {
|
|
462
|
+
this.currentTurnAborted = Boolean(options.signal?.aborted);
|
|
463
|
+
if (this.currentTurnAborted) {
|
|
464
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
465
|
+
abortErr.name = 'AbortError';
|
|
466
|
+
throw abortErr;
|
|
467
|
+
}
|
|
367
468
|
this.currentTurnChunkHandler = options.onChunk || null;
|
|
368
469
|
this.currentTurnReasoningHandler = options.onReasoningChunk || null;
|
|
369
470
|
this.currentTurnNativeActionHandler = options.onNativeAction || null;
|
|
370
471
|
this.liveFileActions.clear();
|
|
472
|
+
const onAbort = () => {
|
|
473
|
+
this.abort();
|
|
474
|
+
};
|
|
475
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
371
476
|
try {
|
|
372
|
-
const { client, sessionId } = await this.getOrCreateClient();
|
|
477
|
+
const { client, sessionId } = await this.getOrCreateClient(options.signal);
|
|
373
478
|
const supportsImages = client.agentCapabilities?.promptCapabilities?.image === true;
|
|
374
479
|
const promptBlocks = this.formatPromptBlocks(messages, options.tools, supportsImages);
|
|
375
480
|
const promptText = promptBlocks.filter(b => b.type === 'text').map(b => b.text).join('\n\n');
|
|
376
481
|
let result;
|
|
377
482
|
try {
|
|
378
|
-
result = await client.prompt(sessionId, promptBlocks);
|
|
483
|
+
result = await client.prompt(sessionId, promptBlocks, options.signal);
|
|
379
484
|
}
|
|
380
485
|
catch (promptErr) {
|
|
381
|
-
|
|
486
|
+
if (this.currentTurnAborted || options.signal?.aborted || promptErr?.name === 'AbortError') {
|
|
487
|
+
throw promptErr;
|
|
488
|
+
}
|
|
489
|
+
// If peer failed before any user abort, try recreating client once in case connection dropped
|
|
382
490
|
await this.close();
|
|
383
|
-
const fresh = await this.getOrCreateClient();
|
|
384
|
-
result = await fresh.client.prompt(fresh.sessionId, promptBlocks);
|
|
491
|
+
const fresh = await this.getOrCreateClient(options.signal);
|
|
492
|
+
result = await fresh.client.prompt(fresh.sessionId, promptBlocks, options.signal);
|
|
493
|
+
}
|
|
494
|
+
if (this.currentTurnAborted || options.signal?.aborted) {
|
|
495
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
496
|
+
abortErr.name = 'AbortError';
|
|
497
|
+
throw abortErr;
|
|
385
498
|
}
|
|
386
499
|
let rawContent = (result?.text || '').trim();
|
|
387
500
|
const toolCalls = this.extractToolCalls(rawContent);
|
|
@@ -398,14 +511,23 @@ export class AcpProvider extends LLMProvider {
|
|
|
398
511
|
if (toolCalls.length > 0) {
|
|
399
512
|
response.toolCalls = toolCalls;
|
|
400
513
|
}
|
|
514
|
+
if (result?.stopReason === 'max_tokens' || result?.stopReason === 'max_turn_requests') {
|
|
515
|
+
response.incompleteTurn = true;
|
|
516
|
+
}
|
|
401
517
|
return response;
|
|
402
518
|
}
|
|
403
519
|
catch (err) {
|
|
404
520
|
await this.close();
|
|
521
|
+
if (this.currentTurnAborted || options.signal?.aborted || err?.name === 'AbortError') {
|
|
522
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
523
|
+
abortErr.name = 'AbortError';
|
|
524
|
+
throw abortErr;
|
|
525
|
+
}
|
|
405
526
|
const msg = err instanceof Error ? err.message : String(err);
|
|
406
527
|
throw new Error(`ACP Peer Agent (${this.peerId}) Error: ${msg}. Make sure "${this.command}" is installed and running.`);
|
|
407
528
|
}
|
|
408
529
|
finally {
|
|
530
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
409
531
|
this.currentTurnChunkHandler = null;
|
|
410
532
|
this.currentTurnReasoningHandler = null;
|
|
411
533
|
this.currentTurnNativeActionHandler = null;
|
package/src/providers/acp.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { canonicalModelId } from '../modelIds.js';
|
|
2
2
|
import { DEFAULT_MODEL_BY_PROVIDER } from '../modelCatalogue.js';
|
|
3
|
+
import { saveCachedModels } from '../dynamicCatalogue.js';
|
|
3
4
|
import { LLMProvider, type ProviderConfig, type ChatOptions, type ChatResponse, type ChatResponseToolCall, type ChatResponseAction } from './base.js';
|
|
4
5
|
import { AcpClient } from '../acp/client.js';
|
|
5
6
|
import { getPeerProfile } from '../acp/peerProfiles.js';
|
|
@@ -19,6 +20,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
19
20
|
args: string[];
|
|
20
21
|
private activeClient: AcpClient | null = null;
|
|
21
22
|
private activeSessionId: string | null = null;
|
|
23
|
+
private currentTurnAborted = false;
|
|
22
24
|
private currentTurnChunkHandler: ((chunk: string) => void) | null = null;
|
|
23
25
|
private currentTurnReasoningHandler: ((chunk: string) => void) | null = null;
|
|
24
26
|
private currentTurnNativeActionHandler: ((action: ChatResponseAction) => void) | null = null;
|
|
@@ -48,7 +50,10 @@ export class AcpProvider extends LLMProvider {
|
|
|
48
50
|
this.model = canonicalModelId(modelIsPeerName ? peerDefault : configuredModel);
|
|
49
51
|
|
|
50
52
|
const profile = getPeerProfile(this.peerId);
|
|
51
|
-
if (
|
|
53
|
+
if (config.command) {
|
|
54
|
+
this.command = config.command;
|
|
55
|
+
this.args = config.args || ['acp'];
|
|
56
|
+
} else if (profile) {
|
|
52
57
|
this.command = profile.command;
|
|
53
58
|
this.args = profile.args || [];
|
|
54
59
|
} else {
|
|
@@ -57,7 +62,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
57
62
|
this.command = preset.command;
|
|
58
63
|
this.args = preset.args || [];
|
|
59
64
|
} else {
|
|
60
|
-
this.command =
|
|
65
|
+
this.command = this.peerId;
|
|
61
66
|
this.args = config.args || ['acp'];
|
|
62
67
|
}
|
|
63
68
|
}
|
|
@@ -275,10 +280,42 @@ export class AcpProvider extends LLMProvider {
|
|
|
275
280
|
}
|
|
276
281
|
}
|
|
277
282
|
|
|
283
|
+
if (calls.length === 0) {
|
|
284
|
+
const openIndex = text.lastIndexOf('<tool_call>');
|
|
285
|
+
if (openIndex !== -1 && text.indexOf('</tool_call>', openIndex) === -1) {
|
|
286
|
+
const partial = text.slice(openIndex + '<tool_call>'.length).trim();
|
|
287
|
+
for (const suffix of ['', '}', '}}', '}}}', '"}', '"}}', '"}}}', '"}]}}', '"]}}', '"]}}}']) {
|
|
288
|
+
try {
|
|
289
|
+
const parsed = JSON.parse(partial + suffix);
|
|
290
|
+
const name = parsed.name || parsed.tool || parsed.function?.name;
|
|
291
|
+
if (name) {
|
|
292
|
+
const rawArgs = parsed.arguments !== undefined ? parsed.arguments : (parsed.parameters !== undefined ? parsed.parameters : (parsed.input !== undefined ? parsed.input : (parsed.function?.arguments || {})));
|
|
293
|
+
calls.push({
|
|
294
|
+
id: `acp_call_${Date.now()}_0`,
|
|
295
|
+
type: 'function',
|
|
296
|
+
function: {
|
|
297
|
+
name,
|
|
298
|
+
arguments: typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs || {})
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
} catch {
|
|
304
|
+
// try next candidate
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
278
310
|
return calls;
|
|
279
311
|
}
|
|
280
312
|
|
|
281
|
-
private async getOrCreateClient(): Promise<{ client: AcpClient; sessionId: string }> {
|
|
313
|
+
private async getOrCreateClient(signal?: AbortSignal): Promise<{ client: AcpClient; sessionId: string }> {
|
|
314
|
+
if (signal?.aborted) {
|
|
315
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
316
|
+
abortErr.name = 'AbortError';
|
|
317
|
+
throw abortErr;
|
|
318
|
+
}
|
|
282
319
|
if (this.activeClient && this.activeSessionId) {
|
|
283
320
|
return { client: this.activeClient, sessionId: this.activeSessionId };
|
|
284
321
|
}
|
|
@@ -287,13 +324,15 @@ export class AcpProvider extends LLMProvider {
|
|
|
287
324
|
command: this.command,
|
|
288
325
|
args: this.args,
|
|
289
326
|
cwd: process.cwd(),
|
|
290
|
-
promptTimeoutMs:
|
|
327
|
+
promptTimeoutMs: 900_000,
|
|
291
328
|
permissionHandler: async (req: RequestPermissionRequest) => {
|
|
329
|
+
if (this.currentTurnAborted || signal?.aborted) return { outcome: 'cancelled' };
|
|
292
330
|
const allowOpt = req.options?.find(o => o.kind === 'allow_once' || o.kind === 'allow_always' || o.optionId === 'allow');
|
|
293
331
|
if (allowOpt) return { outcome: 'selected', optionId: allowOpt.optionId };
|
|
294
332
|
return { outcome: 'cancelled' };
|
|
295
333
|
},
|
|
296
334
|
onUpdate: (_sessionId: string, update: SessionUpdate) => {
|
|
335
|
+
if (this.currentTurnAborted) return;
|
|
297
336
|
if (update && typeof update === 'object') {
|
|
298
337
|
const updateObj = update as any;
|
|
299
338
|
if (updateObj.sessionUpdate === 'agent_thought_chunk') {
|
|
@@ -337,18 +376,57 @@ export class AcpProvider extends LLMProvider {
|
|
|
337
376
|
}
|
|
338
377
|
});
|
|
339
378
|
|
|
340
|
-
await client.start();
|
|
341
|
-
const sessionId = await client.newSession(process.cwd());
|
|
379
|
+
await client.start(signal);
|
|
380
|
+
const sessionId = await client.newSession(process.cwd(), [], signal);
|
|
381
|
+
|
|
382
|
+
const sessionModels = client.getSessionModels(sessionId);
|
|
383
|
+
const liveModels = sessionModels.availableModels;
|
|
384
|
+
if (liveModels.length > 0 && !process.env.NODE_TEST_CONTEXT) {
|
|
385
|
+
saveCachedModels(`acp/${this.peerId}`, liveModels, {
|
|
386
|
+
...(sessionModels.currentModelId ? { currentModel: sessionModels.currentModelId } : {})
|
|
387
|
+
});
|
|
388
|
+
}
|
|
342
389
|
|
|
343
390
|
let targetModel = this.model;
|
|
344
|
-
if (this.peerId === 'opencode' && targetModel !== this.peerId && !targetModel.
|
|
391
|
+
if (this.peerId === 'opencode' && targetModel !== this.peerId && !targetModel.includes('/')) {
|
|
345
392
|
targetModel = `opencode/${targetModel}`;
|
|
346
393
|
}
|
|
394
|
+
|
|
395
|
+
if (liveModels.length > 0 && targetModel && !liveModels.includes(targetModel)) {
|
|
396
|
+
const matched = liveModels.find(
|
|
397
|
+
(m) => m.toLowerCase() === targetModel.toLowerCase() || m.endsWith(`/${targetModel}`)
|
|
398
|
+
);
|
|
399
|
+
if (matched) {
|
|
400
|
+
targetModel = matched;
|
|
401
|
+
this.model = matched;
|
|
402
|
+
} else {
|
|
403
|
+
const staticDefault = (DEFAULT_MODEL_BY_PROVIDER as Record<string, string>)[`acp/${this.peerId}`];
|
|
404
|
+
if (targetModel === staticDefault || targetModel === this.peerId || targetModel === 'acp') {
|
|
405
|
+
targetModel = sessionModels.currentModelId || liveModels[0]!;
|
|
406
|
+
this.model = targetModel;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
347
411
|
if (targetModel && targetModel !== 'acp' && targetModel !== this.peerId) {
|
|
348
412
|
try {
|
|
349
|
-
await client.setSessionModel(sessionId, targetModel);
|
|
350
|
-
} catch {
|
|
351
|
-
|
|
413
|
+
await client.setSessionModel(sessionId, targetModel, signal);
|
|
414
|
+
} catch (err: any) {
|
|
415
|
+
if (this.currentTurnAborted || signal?.aborted || err?.name === 'AbortError') {
|
|
416
|
+
await client.close();
|
|
417
|
+
throw err;
|
|
418
|
+
}
|
|
419
|
+
// If the requested model is unavailable on the user's current account/tier,
|
|
420
|
+
// gracefully fall back to the peer's active or first advertised live model.
|
|
421
|
+
const fallbackModel = sessionModels.currentModelId || liveModels[0];
|
|
422
|
+
if (fallbackModel && fallbackModel !== targetModel) {
|
|
423
|
+
this.model = fallbackModel;
|
|
424
|
+
try {
|
|
425
|
+
await client.setSessionModel(sessionId, fallbackModel, signal);
|
|
426
|
+
} catch {
|
|
427
|
+
// Peer already defaults to its own active session model
|
|
428
|
+
}
|
|
429
|
+
}
|
|
352
430
|
}
|
|
353
431
|
}
|
|
354
432
|
|
|
@@ -357,25 +435,52 @@ export class AcpProvider extends LLMProvider {
|
|
|
357
435
|
return { client, sessionId };
|
|
358
436
|
}
|
|
359
437
|
|
|
438
|
+
abort(): void {
|
|
439
|
+
this.currentTurnAborted = true;
|
|
440
|
+
this.currentTurnChunkHandler = null;
|
|
441
|
+
this.currentTurnReasoningHandler = null;
|
|
442
|
+
this.currentTurnNativeActionHandler = null;
|
|
443
|
+
this.liveFileActions.clear();
|
|
444
|
+
if (this.activeClient && this.activeSessionId) {
|
|
445
|
+
try {
|
|
446
|
+
this.activeClient.notifyCancel(this.activeSessionId);
|
|
447
|
+
} catch {
|
|
448
|
+
// ignore cancel write error
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
void this.close();
|
|
452
|
+
}
|
|
453
|
+
|
|
360
454
|
async close(): Promise<void> {
|
|
361
|
-
|
|
455
|
+
const client = this.activeClient;
|
|
456
|
+
this.activeClient = null;
|
|
457
|
+
this.activeSessionId = null;
|
|
458
|
+
if (client) {
|
|
362
459
|
try {
|
|
363
|
-
await
|
|
460
|
+
await client.close();
|
|
364
461
|
} catch {
|
|
365
462
|
// ignore close error
|
|
366
463
|
}
|
|
367
|
-
this.activeClient = null;
|
|
368
|
-
this.activeSessionId = null;
|
|
369
464
|
}
|
|
370
465
|
}
|
|
371
466
|
|
|
372
467
|
override async chat(messages: any[], options: ChatOptions = {}): Promise<ChatResponse> {
|
|
468
|
+
this.currentTurnAborted = Boolean(options.signal?.aborted);
|
|
469
|
+
if (this.currentTurnAborted) {
|
|
470
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
471
|
+
abortErr.name = 'AbortError';
|
|
472
|
+
throw abortErr;
|
|
473
|
+
}
|
|
373
474
|
this.currentTurnChunkHandler = options.onChunk || null;
|
|
374
475
|
this.currentTurnReasoningHandler = options.onReasoningChunk || null;
|
|
375
476
|
this.currentTurnNativeActionHandler = options.onNativeAction || null;
|
|
376
477
|
this.liveFileActions.clear();
|
|
478
|
+
const onAbort = (): void => {
|
|
479
|
+
this.abort();
|
|
480
|
+
};
|
|
481
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
377
482
|
try {
|
|
378
|
-
const { client, sessionId } = await this.getOrCreateClient();
|
|
483
|
+
const { client, sessionId } = await this.getOrCreateClient(options.signal);
|
|
379
484
|
|
|
380
485
|
const supportsImages = client.agentCapabilities?.promptCapabilities?.image === true;
|
|
381
486
|
const promptBlocks = this.formatPromptBlocks(messages, options.tools, supportsImages);
|
|
@@ -383,12 +488,21 @@ export class AcpProvider extends LLMProvider {
|
|
|
383
488
|
|
|
384
489
|
let result;
|
|
385
490
|
try {
|
|
386
|
-
result = await client.prompt(sessionId, promptBlocks);
|
|
491
|
+
result = await client.prompt(sessionId, promptBlocks, options.signal);
|
|
387
492
|
} catch (promptErr: any) {
|
|
388
|
-
|
|
493
|
+
if (this.currentTurnAborted || options.signal?.aborted || promptErr?.name === 'AbortError') {
|
|
494
|
+
throw promptErr;
|
|
495
|
+
}
|
|
496
|
+
// If peer failed before any user abort, try recreating client once in case connection dropped
|
|
389
497
|
await this.close();
|
|
390
|
-
const fresh = await this.getOrCreateClient();
|
|
391
|
-
result = await fresh.client.prompt(fresh.sessionId, promptBlocks);
|
|
498
|
+
const fresh = await this.getOrCreateClient(options.signal);
|
|
499
|
+
result = await fresh.client.prompt(fresh.sessionId, promptBlocks, options.signal);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (this.currentTurnAborted || options.signal?.aborted) {
|
|
503
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
504
|
+
abortErr.name = 'AbortError';
|
|
505
|
+
throw abortErr;
|
|
392
506
|
}
|
|
393
507
|
|
|
394
508
|
let rawContent = (result?.text || '').trim();
|
|
@@ -408,12 +522,21 @@ export class AcpProvider extends LLMProvider {
|
|
|
408
522
|
if (toolCalls.length > 0) {
|
|
409
523
|
response.toolCalls = toolCalls;
|
|
410
524
|
}
|
|
525
|
+
if (result?.stopReason === 'max_tokens' || result?.stopReason === 'max_turn_requests') {
|
|
526
|
+
response.incompleteTurn = true;
|
|
527
|
+
}
|
|
411
528
|
return response;
|
|
412
529
|
} catch (err: any) {
|
|
413
530
|
await this.close();
|
|
531
|
+
if (this.currentTurnAborted || options.signal?.aborted || err?.name === 'AbortError') {
|
|
532
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
533
|
+
abortErr.name = 'AbortError';
|
|
534
|
+
throw abortErr;
|
|
535
|
+
}
|
|
414
536
|
const msg = err instanceof Error ? err.message : String(err);
|
|
415
537
|
throw new Error(`ACP Peer Agent (${this.peerId}) Error: ${msg}. Make sure "${this.command}" is installed and running.`);
|
|
416
538
|
} finally {
|
|
539
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
417
540
|
this.currentTurnChunkHandler = null;
|
|
418
541
|
this.currentTurnReasoningHandler = null;
|
|
419
542
|
this.currentTurnNativeActionHandler = null;
|
|
@@ -2,8 +2,14 @@ export const BASELINE_ANTIGRAVITY_MODELS: Record<string, string>;
|
|
|
2
2
|
export const ANTIGRAVITY_MODELS: string[];
|
|
3
3
|
export const ANTIGRAVITY_MODEL_IDS: Record<string, string>;
|
|
4
4
|
export const ANTIGRAVITY_LABEL_BY_ID: Record<string, string>;
|
|
5
|
+
export function synthesizeAgySlug(input: string): string;
|
|
5
6
|
export function registerAntigravityModel(slug: string, label?: string): void;
|
|
6
|
-
export function
|
|
7
|
+
export function replaceAntigravityModels(items: Array<{ id: string; label?: string } | string>): string[];
|
|
8
|
+
export function parseAgyModelsOutput(
|
|
9
|
+
stdout: string,
|
|
10
|
+
options?: { replaceActiveList?: boolean }
|
|
11
|
+
): Array<{ id: string; label: string }>;
|
|
12
|
+
export function isAntigravityOnboarded(): boolean;
|
|
7
13
|
export function resolveAgyModelId(model: string | null | undefined): string;
|
|
8
14
|
export function resolveAgyEffort(model: string | null | undefined): string;
|
|
9
15
|
export function isAntigravityProvider(provider: string | null | undefined): boolean;
|