@causyne/agent 1.20.5 → 1.20.7
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/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/providers/acp.js +106 -17
- package/src/providers/acp.ts +104 -17
- package/src/providers/antigravity.js +35 -6
- package/src/providers/antigravity.ts +40 -6
- 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/prompt.js +23 -25
- package/src/tui/text.d.ts +2 -1
package/src/providers/acp.js
CHANGED
|
@@ -17,6 +17,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
17
17
|
args;
|
|
18
18
|
activeClient = null;
|
|
19
19
|
activeSessionId = null;
|
|
20
|
+
currentTurnAborted = false;
|
|
20
21
|
currentTurnChunkHandler = null;
|
|
21
22
|
currentTurnReasoningHandler = null;
|
|
22
23
|
currentTurnNativeActionHandler = null;
|
|
@@ -45,7 +46,11 @@ export class AcpProvider extends LLMProvider {
|
|
|
45
46
|
const modelIsPeerName = !configuredModel || configuredModel === 'acp' || configuredModel === this.peerId;
|
|
46
47
|
this.model = canonicalModelId(modelIsPeerName ? peerDefault : configuredModel);
|
|
47
48
|
const profile = getPeerProfile(this.peerId);
|
|
48
|
-
if (
|
|
49
|
+
if (config.command) {
|
|
50
|
+
this.command = config.command;
|
|
51
|
+
this.args = config.args || ['acp'];
|
|
52
|
+
}
|
|
53
|
+
else if (profile) {
|
|
49
54
|
this.command = profile.command;
|
|
50
55
|
this.args = profile.args || [];
|
|
51
56
|
}
|
|
@@ -56,7 +61,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
56
61
|
this.args = preset.args || [];
|
|
57
62
|
}
|
|
58
63
|
else {
|
|
59
|
-
this.command =
|
|
64
|
+
this.command = this.peerId;
|
|
60
65
|
this.args = config.args || ['acp'];
|
|
61
66
|
}
|
|
62
67
|
}
|
|
@@ -267,9 +272,41 @@ export class AcpProvider extends LLMProvider {
|
|
|
267
272
|
}
|
|
268
273
|
}
|
|
269
274
|
}
|
|
275
|
+
if (calls.length === 0) {
|
|
276
|
+
const openIndex = text.lastIndexOf('<tool_call>');
|
|
277
|
+
if (openIndex !== -1 && text.indexOf('</tool_call>', openIndex) === -1) {
|
|
278
|
+
const partial = text.slice(openIndex + '<tool_call>'.length).trim();
|
|
279
|
+
for (const suffix of ['', '}', '}}', '}}}', '"}', '"}}', '"}}}', '"}]}}', '"]}}', '"]}}}']) {
|
|
280
|
+
try {
|
|
281
|
+
const parsed = JSON.parse(partial + suffix);
|
|
282
|
+
const name = parsed.name || parsed.tool || parsed.function?.name;
|
|
283
|
+
if (name) {
|
|
284
|
+
const rawArgs = parsed.arguments !== undefined ? parsed.arguments : (parsed.parameters !== undefined ? parsed.parameters : (parsed.input !== undefined ? parsed.input : (parsed.function?.arguments || {})));
|
|
285
|
+
calls.push({
|
|
286
|
+
id: `acp_call_${Date.now()}_0`,
|
|
287
|
+
type: 'function',
|
|
288
|
+
function: {
|
|
289
|
+
name,
|
|
290
|
+
arguments: typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs || {})
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
// try next candidate
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
270
302
|
return calls;
|
|
271
303
|
}
|
|
272
|
-
async getOrCreateClient() {
|
|
304
|
+
async getOrCreateClient(signal) {
|
|
305
|
+
if (signal?.aborted) {
|
|
306
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
307
|
+
abortErr.name = 'AbortError';
|
|
308
|
+
throw abortErr;
|
|
309
|
+
}
|
|
273
310
|
if (this.activeClient && this.activeSessionId) {
|
|
274
311
|
return { client: this.activeClient, sessionId: this.activeSessionId };
|
|
275
312
|
}
|
|
@@ -277,14 +314,18 @@ export class AcpProvider extends LLMProvider {
|
|
|
277
314
|
command: this.command,
|
|
278
315
|
args: this.args,
|
|
279
316
|
cwd: process.cwd(),
|
|
280
|
-
promptTimeoutMs:
|
|
317
|
+
promptTimeoutMs: 900_000,
|
|
281
318
|
permissionHandler: async (req) => {
|
|
319
|
+
if (this.currentTurnAborted || signal?.aborted)
|
|
320
|
+
return { outcome: 'cancelled' };
|
|
282
321
|
const allowOpt = req.options?.find(o => o.kind === 'allow_once' || o.kind === 'allow_always' || o.optionId === 'allow');
|
|
283
322
|
if (allowOpt)
|
|
284
323
|
return { outcome: 'selected', optionId: allowOpt.optionId };
|
|
285
324
|
return { outcome: 'cancelled' };
|
|
286
325
|
},
|
|
287
326
|
onUpdate: (_sessionId, update) => {
|
|
327
|
+
if (this.currentTurnAborted)
|
|
328
|
+
return;
|
|
288
329
|
if (update && typeof update === 'object') {
|
|
289
330
|
const updateObj = update;
|
|
290
331
|
if (updateObj.sessionUpdate === 'agent_thought_chunk') {
|
|
@@ -333,17 +374,21 @@ export class AcpProvider extends LLMProvider {
|
|
|
333
374
|
}
|
|
334
375
|
}
|
|
335
376
|
});
|
|
336
|
-
await client.start();
|
|
337
|
-
const sessionId = await client.newSession(process.cwd());
|
|
377
|
+
await client.start(signal);
|
|
378
|
+
const sessionId = await client.newSession(process.cwd(), [], signal);
|
|
338
379
|
let targetModel = this.model;
|
|
339
380
|
if (this.peerId === 'opencode' && targetModel !== this.peerId && !targetModel.startsWith('opencode/')) {
|
|
340
381
|
targetModel = `opencode/${targetModel}`;
|
|
341
382
|
}
|
|
342
383
|
if (targetModel && targetModel !== 'acp' && targetModel !== this.peerId) {
|
|
343
384
|
try {
|
|
344
|
-
await client.setSessionModel(sessionId, targetModel);
|
|
385
|
+
await client.setSessionModel(sessionId, targetModel, signal);
|
|
345
386
|
}
|
|
346
|
-
catch {
|
|
387
|
+
catch (err) {
|
|
388
|
+
if (this.currentTurnAborted || signal?.aborted || err?.name === 'AbortError') {
|
|
389
|
+
await client.close();
|
|
390
|
+
throw err;
|
|
391
|
+
}
|
|
347
392
|
// peer does not support setSessionModel
|
|
348
393
|
}
|
|
349
394
|
}
|
|
@@ -351,37 +396,72 @@ export class AcpProvider extends LLMProvider {
|
|
|
351
396
|
this.activeSessionId = sessionId;
|
|
352
397
|
return { client, sessionId };
|
|
353
398
|
}
|
|
399
|
+
abort() {
|
|
400
|
+
this.currentTurnAborted = true;
|
|
401
|
+
this.currentTurnChunkHandler = null;
|
|
402
|
+
this.currentTurnReasoningHandler = null;
|
|
403
|
+
this.currentTurnNativeActionHandler = null;
|
|
404
|
+
this.liveFileActions.clear();
|
|
405
|
+
if (this.activeClient && this.activeSessionId) {
|
|
406
|
+
try {
|
|
407
|
+
this.activeClient.notifyCancel(this.activeSessionId);
|
|
408
|
+
}
|
|
409
|
+
catch {
|
|
410
|
+
// ignore cancel write error
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
void this.close();
|
|
414
|
+
}
|
|
354
415
|
async close() {
|
|
355
|
-
|
|
416
|
+
const client = this.activeClient;
|
|
417
|
+
this.activeClient = null;
|
|
418
|
+
this.activeSessionId = null;
|
|
419
|
+
if (client) {
|
|
356
420
|
try {
|
|
357
|
-
await
|
|
421
|
+
await client.close();
|
|
358
422
|
}
|
|
359
423
|
catch {
|
|
360
424
|
// ignore close error
|
|
361
425
|
}
|
|
362
|
-
this.activeClient = null;
|
|
363
|
-
this.activeSessionId = null;
|
|
364
426
|
}
|
|
365
427
|
}
|
|
366
428
|
async chat(messages, options = {}) {
|
|
429
|
+
this.currentTurnAborted = Boolean(options.signal?.aborted);
|
|
430
|
+
if (this.currentTurnAborted) {
|
|
431
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
432
|
+
abortErr.name = 'AbortError';
|
|
433
|
+
throw abortErr;
|
|
434
|
+
}
|
|
367
435
|
this.currentTurnChunkHandler = options.onChunk || null;
|
|
368
436
|
this.currentTurnReasoningHandler = options.onReasoningChunk || null;
|
|
369
437
|
this.currentTurnNativeActionHandler = options.onNativeAction || null;
|
|
370
438
|
this.liveFileActions.clear();
|
|
439
|
+
const onAbort = () => {
|
|
440
|
+
this.abort();
|
|
441
|
+
};
|
|
442
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
371
443
|
try {
|
|
372
|
-
const { client, sessionId } = await this.getOrCreateClient();
|
|
444
|
+
const { client, sessionId } = await this.getOrCreateClient(options.signal);
|
|
373
445
|
const supportsImages = client.agentCapabilities?.promptCapabilities?.image === true;
|
|
374
446
|
const promptBlocks = this.formatPromptBlocks(messages, options.tools, supportsImages);
|
|
375
447
|
const promptText = promptBlocks.filter(b => b.type === 'text').map(b => b.text).join('\n\n');
|
|
376
448
|
let result;
|
|
377
449
|
try {
|
|
378
|
-
result = await client.prompt(sessionId, promptBlocks);
|
|
450
|
+
result = await client.prompt(sessionId, promptBlocks, options.signal);
|
|
379
451
|
}
|
|
380
452
|
catch (promptErr) {
|
|
381
|
-
|
|
453
|
+
if (this.currentTurnAborted || options.signal?.aborted || promptErr?.name === 'AbortError') {
|
|
454
|
+
throw promptErr;
|
|
455
|
+
}
|
|
456
|
+
// If peer failed before any user abort, try recreating client once in case connection dropped
|
|
382
457
|
await this.close();
|
|
383
|
-
const fresh = await this.getOrCreateClient();
|
|
384
|
-
result = await fresh.client.prompt(fresh.sessionId, promptBlocks);
|
|
458
|
+
const fresh = await this.getOrCreateClient(options.signal);
|
|
459
|
+
result = await fresh.client.prompt(fresh.sessionId, promptBlocks, options.signal);
|
|
460
|
+
}
|
|
461
|
+
if (this.currentTurnAborted || options.signal?.aborted) {
|
|
462
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
463
|
+
abortErr.name = 'AbortError';
|
|
464
|
+
throw abortErr;
|
|
385
465
|
}
|
|
386
466
|
let rawContent = (result?.text || '').trim();
|
|
387
467
|
const toolCalls = this.extractToolCalls(rawContent);
|
|
@@ -398,14 +478,23 @@ export class AcpProvider extends LLMProvider {
|
|
|
398
478
|
if (toolCalls.length > 0) {
|
|
399
479
|
response.toolCalls = toolCalls;
|
|
400
480
|
}
|
|
481
|
+
if (result?.stopReason === 'max_tokens' || result?.stopReason === 'max_turn_requests') {
|
|
482
|
+
response.incompleteTurn = true;
|
|
483
|
+
}
|
|
401
484
|
return response;
|
|
402
485
|
}
|
|
403
486
|
catch (err) {
|
|
404
487
|
await this.close();
|
|
488
|
+
if (this.currentTurnAborted || options.signal?.aborted || err?.name === 'AbortError') {
|
|
489
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
490
|
+
abortErr.name = 'AbortError';
|
|
491
|
+
throw abortErr;
|
|
492
|
+
}
|
|
405
493
|
const msg = err instanceof Error ? err.message : String(err);
|
|
406
494
|
throw new Error(`ACP Peer Agent (${this.peerId}) Error: ${msg}. Make sure "${this.command}" is installed and running.`);
|
|
407
495
|
}
|
|
408
496
|
finally {
|
|
497
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
409
498
|
this.currentTurnChunkHandler = null;
|
|
410
499
|
this.currentTurnReasoningHandler = null;
|
|
411
500
|
this.currentTurnNativeActionHandler = null;
|
package/src/providers/acp.ts
CHANGED
|
@@ -19,6 +19,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
19
19
|
args: string[];
|
|
20
20
|
private activeClient: AcpClient | null = null;
|
|
21
21
|
private activeSessionId: string | null = null;
|
|
22
|
+
private currentTurnAborted = false;
|
|
22
23
|
private currentTurnChunkHandler: ((chunk: string) => void) | null = null;
|
|
23
24
|
private currentTurnReasoningHandler: ((chunk: string) => void) | null = null;
|
|
24
25
|
private currentTurnNativeActionHandler: ((action: ChatResponseAction) => void) | null = null;
|
|
@@ -48,7 +49,10 @@ export class AcpProvider extends LLMProvider {
|
|
|
48
49
|
this.model = canonicalModelId(modelIsPeerName ? peerDefault : configuredModel);
|
|
49
50
|
|
|
50
51
|
const profile = getPeerProfile(this.peerId);
|
|
51
|
-
if (
|
|
52
|
+
if (config.command) {
|
|
53
|
+
this.command = config.command;
|
|
54
|
+
this.args = config.args || ['acp'];
|
|
55
|
+
} else if (profile) {
|
|
52
56
|
this.command = profile.command;
|
|
53
57
|
this.args = profile.args || [];
|
|
54
58
|
} else {
|
|
@@ -57,7 +61,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
57
61
|
this.command = preset.command;
|
|
58
62
|
this.args = preset.args || [];
|
|
59
63
|
} else {
|
|
60
|
-
this.command =
|
|
64
|
+
this.command = this.peerId;
|
|
61
65
|
this.args = config.args || ['acp'];
|
|
62
66
|
}
|
|
63
67
|
}
|
|
@@ -275,10 +279,42 @@ export class AcpProvider extends LLMProvider {
|
|
|
275
279
|
}
|
|
276
280
|
}
|
|
277
281
|
|
|
282
|
+
if (calls.length === 0) {
|
|
283
|
+
const openIndex = text.lastIndexOf('<tool_call>');
|
|
284
|
+
if (openIndex !== -1 && text.indexOf('</tool_call>', openIndex) === -1) {
|
|
285
|
+
const partial = text.slice(openIndex + '<tool_call>'.length).trim();
|
|
286
|
+
for (const suffix of ['', '}', '}}', '}}}', '"}', '"}}', '"}}}', '"}]}}', '"]}}', '"]}}}']) {
|
|
287
|
+
try {
|
|
288
|
+
const parsed = JSON.parse(partial + suffix);
|
|
289
|
+
const name = parsed.name || parsed.tool || parsed.function?.name;
|
|
290
|
+
if (name) {
|
|
291
|
+
const rawArgs = parsed.arguments !== undefined ? parsed.arguments : (parsed.parameters !== undefined ? parsed.parameters : (parsed.input !== undefined ? parsed.input : (parsed.function?.arguments || {})));
|
|
292
|
+
calls.push({
|
|
293
|
+
id: `acp_call_${Date.now()}_0`,
|
|
294
|
+
type: 'function',
|
|
295
|
+
function: {
|
|
296
|
+
name,
|
|
297
|
+
arguments: typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs || {})
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
} catch {
|
|
303
|
+
// try next candidate
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
278
309
|
return calls;
|
|
279
310
|
}
|
|
280
311
|
|
|
281
|
-
private async getOrCreateClient(): Promise<{ client: AcpClient; sessionId: string }> {
|
|
312
|
+
private async getOrCreateClient(signal?: AbortSignal): Promise<{ client: AcpClient; sessionId: string }> {
|
|
313
|
+
if (signal?.aborted) {
|
|
314
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
315
|
+
abortErr.name = 'AbortError';
|
|
316
|
+
throw abortErr;
|
|
317
|
+
}
|
|
282
318
|
if (this.activeClient && this.activeSessionId) {
|
|
283
319
|
return { client: this.activeClient, sessionId: this.activeSessionId };
|
|
284
320
|
}
|
|
@@ -287,13 +323,15 @@ export class AcpProvider extends LLMProvider {
|
|
|
287
323
|
command: this.command,
|
|
288
324
|
args: this.args,
|
|
289
325
|
cwd: process.cwd(),
|
|
290
|
-
promptTimeoutMs:
|
|
326
|
+
promptTimeoutMs: 900_000,
|
|
291
327
|
permissionHandler: async (req: RequestPermissionRequest) => {
|
|
328
|
+
if (this.currentTurnAborted || signal?.aborted) return { outcome: 'cancelled' };
|
|
292
329
|
const allowOpt = req.options?.find(o => o.kind === 'allow_once' || o.kind === 'allow_always' || o.optionId === 'allow');
|
|
293
330
|
if (allowOpt) return { outcome: 'selected', optionId: allowOpt.optionId };
|
|
294
331
|
return { outcome: 'cancelled' };
|
|
295
332
|
},
|
|
296
333
|
onUpdate: (_sessionId: string, update: SessionUpdate) => {
|
|
334
|
+
if (this.currentTurnAborted) return;
|
|
297
335
|
if (update && typeof update === 'object') {
|
|
298
336
|
const updateObj = update as any;
|
|
299
337
|
if (updateObj.sessionUpdate === 'agent_thought_chunk') {
|
|
@@ -337,8 +375,8 @@ export class AcpProvider extends LLMProvider {
|
|
|
337
375
|
}
|
|
338
376
|
});
|
|
339
377
|
|
|
340
|
-
await client.start();
|
|
341
|
-
const sessionId = await client.newSession(process.cwd());
|
|
378
|
+
await client.start(signal);
|
|
379
|
+
const sessionId = await client.newSession(process.cwd(), [], signal);
|
|
342
380
|
|
|
343
381
|
let targetModel = this.model;
|
|
344
382
|
if (this.peerId === 'opencode' && targetModel !== this.peerId && !targetModel.startsWith('opencode/')) {
|
|
@@ -346,8 +384,12 @@ export class AcpProvider extends LLMProvider {
|
|
|
346
384
|
}
|
|
347
385
|
if (targetModel && targetModel !== 'acp' && targetModel !== this.peerId) {
|
|
348
386
|
try {
|
|
349
|
-
await client.setSessionModel(sessionId, targetModel);
|
|
350
|
-
} catch {
|
|
387
|
+
await client.setSessionModel(sessionId, targetModel, signal);
|
|
388
|
+
} catch (err: any) {
|
|
389
|
+
if (this.currentTurnAborted || signal?.aborted || err?.name === 'AbortError') {
|
|
390
|
+
await client.close();
|
|
391
|
+
throw err;
|
|
392
|
+
}
|
|
351
393
|
// peer does not support setSessionModel
|
|
352
394
|
}
|
|
353
395
|
}
|
|
@@ -357,25 +399,52 @@ export class AcpProvider extends LLMProvider {
|
|
|
357
399
|
return { client, sessionId };
|
|
358
400
|
}
|
|
359
401
|
|
|
402
|
+
abort(): void {
|
|
403
|
+
this.currentTurnAborted = true;
|
|
404
|
+
this.currentTurnChunkHandler = null;
|
|
405
|
+
this.currentTurnReasoningHandler = null;
|
|
406
|
+
this.currentTurnNativeActionHandler = null;
|
|
407
|
+
this.liveFileActions.clear();
|
|
408
|
+
if (this.activeClient && this.activeSessionId) {
|
|
409
|
+
try {
|
|
410
|
+
this.activeClient.notifyCancel(this.activeSessionId);
|
|
411
|
+
} catch {
|
|
412
|
+
// ignore cancel write error
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
void this.close();
|
|
416
|
+
}
|
|
417
|
+
|
|
360
418
|
async close(): Promise<void> {
|
|
361
|
-
|
|
419
|
+
const client = this.activeClient;
|
|
420
|
+
this.activeClient = null;
|
|
421
|
+
this.activeSessionId = null;
|
|
422
|
+
if (client) {
|
|
362
423
|
try {
|
|
363
|
-
await
|
|
424
|
+
await client.close();
|
|
364
425
|
} catch {
|
|
365
426
|
// ignore close error
|
|
366
427
|
}
|
|
367
|
-
this.activeClient = null;
|
|
368
|
-
this.activeSessionId = null;
|
|
369
428
|
}
|
|
370
429
|
}
|
|
371
430
|
|
|
372
431
|
override async chat(messages: any[], options: ChatOptions = {}): Promise<ChatResponse> {
|
|
432
|
+
this.currentTurnAborted = Boolean(options.signal?.aborted);
|
|
433
|
+
if (this.currentTurnAborted) {
|
|
434
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
435
|
+
abortErr.name = 'AbortError';
|
|
436
|
+
throw abortErr;
|
|
437
|
+
}
|
|
373
438
|
this.currentTurnChunkHandler = options.onChunk || null;
|
|
374
439
|
this.currentTurnReasoningHandler = options.onReasoningChunk || null;
|
|
375
440
|
this.currentTurnNativeActionHandler = options.onNativeAction || null;
|
|
376
441
|
this.liveFileActions.clear();
|
|
442
|
+
const onAbort = (): void => {
|
|
443
|
+
this.abort();
|
|
444
|
+
};
|
|
445
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
377
446
|
try {
|
|
378
|
-
const { client, sessionId } = await this.getOrCreateClient();
|
|
447
|
+
const { client, sessionId } = await this.getOrCreateClient(options.signal);
|
|
379
448
|
|
|
380
449
|
const supportsImages = client.agentCapabilities?.promptCapabilities?.image === true;
|
|
381
450
|
const promptBlocks = this.formatPromptBlocks(messages, options.tools, supportsImages);
|
|
@@ -383,12 +452,21 @@ export class AcpProvider extends LLMProvider {
|
|
|
383
452
|
|
|
384
453
|
let result;
|
|
385
454
|
try {
|
|
386
|
-
result = await client.prompt(sessionId, promptBlocks);
|
|
455
|
+
result = await client.prompt(sessionId, promptBlocks, options.signal);
|
|
387
456
|
} catch (promptErr: any) {
|
|
388
|
-
|
|
457
|
+
if (this.currentTurnAborted || options.signal?.aborted || promptErr?.name === 'AbortError') {
|
|
458
|
+
throw promptErr;
|
|
459
|
+
}
|
|
460
|
+
// If peer failed before any user abort, try recreating client once in case connection dropped
|
|
389
461
|
await this.close();
|
|
390
|
-
const fresh = await this.getOrCreateClient();
|
|
391
|
-
result = await fresh.client.prompt(fresh.sessionId, promptBlocks);
|
|
462
|
+
const fresh = await this.getOrCreateClient(options.signal);
|
|
463
|
+
result = await fresh.client.prompt(fresh.sessionId, promptBlocks, options.signal);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (this.currentTurnAborted || options.signal?.aborted) {
|
|
467
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
468
|
+
abortErr.name = 'AbortError';
|
|
469
|
+
throw abortErr;
|
|
392
470
|
}
|
|
393
471
|
|
|
394
472
|
let rawContent = (result?.text || '').trim();
|
|
@@ -408,12 +486,21 @@ export class AcpProvider extends LLMProvider {
|
|
|
408
486
|
if (toolCalls.length > 0) {
|
|
409
487
|
response.toolCalls = toolCalls;
|
|
410
488
|
}
|
|
489
|
+
if (result?.stopReason === 'max_tokens' || result?.stopReason === 'max_turn_requests') {
|
|
490
|
+
response.incompleteTurn = true;
|
|
491
|
+
}
|
|
411
492
|
return response;
|
|
412
493
|
} catch (err: any) {
|
|
413
494
|
await this.close();
|
|
495
|
+
if (this.currentTurnAborted || options.signal?.aborted || err?.name === 'AbortError') {
|
|
496
|
+
const abortErr = new Error('ACP request aborted by user');
|
|
497
|
+
abortErr.name = 'AbortError';
|
|
498
|
+
throw abortErr;
|
|
499
|
+
}
|
|
414
500
|
const msg = err instanceof Error ? err.message : String(err);
|
|
415
501
|
throw new Error(`ACP Peer Agent (${this.peerId}) Error: ${msg}. Make sure "${this.command}" is installed and running.`);
|
|
416
502
|
} finally {
|
|
503
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
417
504
|
this.currentTurnChunkHandler = null;
|
|
418
505
|
this.currentTurnReasoningHandler = null;
|
|
419
506
|
this.currentTurnNativeActionHandler = null;
|
|
@@ -399,12 +399,16 @@ export function mapNativeToolToCausyne(name, rawParams, index) {
|
|
|
399
399
|
}
|
|
400
400
|
export class AntigravityProvider extends LLMProvider {
|
|
401
401
|
command;
|
|
402
|
+
printTimeout;
|
|
402
403
|
currentConversationId = null;
|
|
403
404
|
lastTaskId = null;
|
|
404
405
|
constructor(config = {}) {
|
|
405
406
|
super(config);
|
|
406
407
|
this.model = config.model || 'Gemini 3.8 Flash (Low)';
|
|
407
408
|
this.command = config.command || resolveAgyBinary();
|
|
409
|
+
this.printTimeout = typeof config.printTimeout === 'string' && config.printTimeout.trim()
|
|
410
|
+
? config.printTimeout.trim()
|
|
411
|
+
: '1800s';
|
|
408
412
|
this.visionModels = ['*'];
|
|
409
413
|
this.audioModels = [];
|
|
410
414
|
}
|
|
@@ -639,10 +643,10 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
639
643
|
msg.includes('status 429');
|
|
640
644
|
}
|
|
641
645
|
async _executeOnce(messages, options = {}) {
|
|
642
|
-
const
|
|
643
|
-
if (
|
|
646
|
+
const currentSessionKey = options.conversationId || options.taskId || null;
|
|
647
|
+
if (currentSessionKey && currentSessionKey !== this.lastTaskId) {
|
|
644
648
|
this.currentConversationId = null;
|
|
645
|
-
this.lastTaskId =
|
|
649
|
+
this.lastTaskId = currentSessionKey;
|
|
646
650
|
}
|
|
647
651
|
if (messages.length <= 1) {
|
|
648
652
|
this.currentConversationId = null;
|
|
@@ -700,7 +704,7 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
700
704
|
'stream-json',
|
|
701
705
|
'--disable-slash-commands',
|
|
702
706
|
'--print-timeout',
|
|
703
|
-
launchOnly ? '90s' :
|
|
707
|
+
launchOnly ? '90s' : this.printTimeout,
|
|
704
708
|
'--model',
|
|
705
709
|
modelSlug,
|
|
706
710
|
'--effort',
|
|
@@ -721,6 +725,7 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
721
725
|
let stderrBuffer = '';
|
|
722
726
|
let streamedResponse = '';
|
|
723
727
|
let finalResultObj = null;
|
|
728
|
+
let sawToolAfterLastText = false;
|
|
724
729
|
const nativeActions = [];
|
|
725
730
|
const attemptedNativeTools = [];
|
|
726
731
|
let childClosed = false;
|
|
@@ -754,7 +759,9 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
754
759
|
catch {
|
|
755
760
|
// ignore kill errors
|
|
756
761
|
}
|
|
757
|
-
|
|
762
|
+
const abortErr = new Error('Turn aborted by user or timeout.');
|
|
763
|
+
abortErr.name = 'AbortError';
|
|
764
|
+
safeReject(abortErr);
|
|
758
765
|
};
|
|
759
766
|
if (options.signal) {
|
|
760
767
|
if (options.signal.aborted) {
|
|
@@ -826,6 +833,7 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
826
833
|
}
|
|
827
834
|
}
|
|
828
835
|
if (name && (state === 'ACTIVE' || state === 'DONE' || state === 'ERROR')) {
|
|
836
|
+
sawToolAfterLastText = true;
|
|
829
837
|
const detail = describeNativeAction(toolInfo) || describeNativeAction({ parameters: rawParams });
|
|
830
838
|
const action = {
|
|
831
839
|
name,
|
|
@@ -856,6 +864,15 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
856
864
|
}
|
|
857
865
|
}
|
|
858
866
|
else {
|
|
867
|
+
if (sawToolAfterLastText && streamedResponse.length > 0 && !/\s$/.test(streamedResponse) && !/^\s/.test(delta)) {
|
|
868
|
+
streamedResponse += '\n';
|
|
869
|
+
if (typeof options.onChunk === 'function') {
|
|
870
|
+
options.onChunk('\n');
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
if (delta.trim().length > 0) {
|
|
874
|
+
sawToolAfterLastText = false;
|
|
875
|
+
}
|
|
859
876
|
streamedResponse += delta;
|
|
860
877
|
if (typeof options.onChunk === 'function') {
|
|
861
878
|
options.onChunk(delta);
|
|
@@ -987,6 +1004,10 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
987
1004
|
: 'Antigravity returned no response.'));
|
|
988
1005
|
return;
|
|
989
1006
|
}
|
|
1007
|
+
const timedOutMidTurn = /print timeout after .* with turn in progress|returning partial output/i.test(stderrBuffer);
|
|
1008
|
+
const finalResponseText = typeof finalResultObj?.response === 'string' ? finalResultObj.response.trim() : '';
|
|
1009
|
+
const endedAfterToolWithoutFinalResponse = sawToolAfterLastText && !finalResponseText && deniedActions.length === 0 && toolCalls.length === 0;
|
|
1010
|
+
const missingResultEvent = finalResultObj === null && deniedActions.length === 0 && toolCalls.length === 0;
|
|
990
1011
|
const promptTokens = Number(finalResultObj?.usage?.input_tokens) || Math.ceil(promptText.length / 4);
|
|
991
1012
|
const completionTokens = Number(finalResultObj?.usage?.output_tokens) || Math.ceil(rawContent.length / 4);
|
|
992
1013
|
const thinkingTokens = Number(finalResultObj?.usage?.thinking_tokens) || 0;
|
|
@@ -1007,6 +1028,9 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
1007
1028
|
if (nativeActions.length > 0) {
|
|
1008
1029
|
response.externalActions = nativeActions.filter((a) => a.status !== 'running');
|
|
1009
1030
|
}
|
|
1031
|
+
if (timedOutMidTurn || endedAfterToolWithoutFinalResponse || missingResultEvent) {
|
|
1032
|
+
response.incompleteTurn = true;
|
|
1033
|
+
}
|
|
1010
1034
|
safeResolve(response);
|
|
1011
1035
|
});
|
|
1012
1036
|
});
|
|
@@ -1015,11 +1039,16 @@ export class AntigravityProvider extends LLMProvider {
|
|
|
1015
1039
|
const maxRetries = options.maxRetries ?? (options.taskIntent === 'launch' ? 1 : 2);
|
|
1016
1040
|
let attempt = 0;
|
|
1017
1041
|
while (attempt <= maxRetries) {
|
|
1042
|
+
const hadConversation = Boolean(this.currentConversationId);
|
|
1018
1043
|
try {
|
|
1019
1044
|
return await this._executeOnce(messages, options);
|
|
1020
1045
|
}
|
|
1021
1046
|
catch (err) {
|
|
1022
|
-
if (
|
|
1047
|
+
if (options.signal?.aborted || err?.name === 'AbortError') {
|
|
1048
|
+
throw err;
|
|
1049
|
+
}
|
|
1050
|
+
const canRetryResetConversation = hadConversation && !this.currentConversationId && err?.code !== 'AGY_NATIVE_ACTION_DENIED';
|
|
1051
|
+
if (attempt < maxRetries && (this._isTransientError(err.message) || canRetryResetConversation)) {
|
|
1023
1052
|
attempt++;
|
|
1024
1053
|
const delay = attempt * 1200;
|
|
1025
1054
|
await new Promise(r => setTimeout(r, delay));
|