@causyne/agent 1.20.0 → 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 +13 -9
- package/SECURITY.md +1 -4
- package/package.json +1 -1
- package/src/acp/client.js +176 -26
- package/src/acp/client.ts +167 -25
- package/src/acp/protocol.js +116 -0
- package/src/acp/protocol.ts +163 -0
- package/src/acp/server.js +5 -6
- package/src/acp/server.ts +4 -6
- package/src/agentLoop.d.ts +1 -0
- package/src/agentLoop.js +681 -245
- package/src/agentLoop.ts +708 -262
- package/src/agentPolicy.js +9 -0
- package/src/agentPolicy.ts +9 -0
- package/src/approvalPolicy.js +55 -0
- package/src/approvalPolicy.ts +66 -0
- package/src/browser/browserRuntime.js +217 -14
- package/src/browser/browserRuntime.ts +255 -14
- package/src/browser/causyneBrowser.js +1215 -68
- package/src/browser/causyneBrowser.ts +1340 -72
- package/src/cliMode.js +52 -29
- package/src/commands.js +141 -17
- package/src/config.js +22 -12
- package/src/continuum/ui/continuumPicker.js +18 -1
- package/src/continuum/ui/continuumPicker.ts +19 -1
- package/src/dynamicCatalogue.js +87 -20
- package/src/dynamicCatalogue.ts +85 -15
- package/src/launchIntent.js +40 -0
- package/src/launchIntent.ts +49 -0
- package/src/memoryManager.js +26 -2
- package/src/modelCatalogue.js +2 -0
- package/src/planCache.js +320 -11
- package/src/planCache.ts +402 -13
- package/src/providers/acp.js +173 -18
- package/src/providers/acp.ts +169 -19
- package/src/providers/antigravity.d.ts +5 -1
- package/src/providers/antigravity.js +674 -34
- package/src/providers/antigravity.ts +707 -35
- package/src/providers/base.d.ts +10 -0
- package/src/providers/factory.js +2 -0
- package/src/providers/pi.js +161 -0
- package/src/providers/pi.ts +148 -0
- package/src/provisioning/capabilityProvisioner.js +17 -0
- package/src/provisioning/capabilityTools.js +1 -1
- package/src/sandbox.js +30 -39
- package/src/sessionJournal.js +53 -10
- package/src/sessionJournal.ts +64 -10
- package/src/taskShape.js +9 -9
- package/src/taskShape.ts +9 -9
- package/src/timeMachine.js +124 -0
- package/src/timeMachine.ts +141 -0
- package/src/toolPacks.js +4 -10
- package/src/tools/archive.js +196 -59
- package/src/tools/archive.ts +220 -0
- package/src/tools/browserAutomator.js +78 -9
- package/src/tools/browserAutomator.ts +90 -9
- package/src/tools/desktopOpen.js +114 -31
- package/src/tools/desktopOpen.ts +144 -0
- package/src/tools/gitExec.js +5 -3
- package/src/tools/nativeEngine.js +4 -3
- package/src/tools/nativeEngine.ts +5 -3
- package/src/tools/openSourceToolProvisioner.js +5 -4
- package/src/tools/openSourceToolProvisioner.ts +5 -4
- package/src/tools/registry.js +3 -0
- package/src/tools/screenshot.js +55 -71
- package/src/tools/screenshot.ts +70 -0
- package/src/tools/sysRun.js +284 -117
- package/src/tools/sysRun.ts +347 -0
- package/src/tools/toolSelfRepair.js +109 -2
- package/src/tools/toolSelfRepair.ts +100 -2
- package/src/tools/updatePlan.js +49 -1
- package/src/tools/updatePlan.ts +43 -1
- package/src/tools/viewImage.js +93 -78
- package/src/tools/viewImage.ts +116 -0
- package/src/tools/webFetch.js +20 -6
- package/src/tools/webFetch.ts +26 -6
- package/src/tui/activityLayout.js +9 -0
- package/src/tui/activityLayout.ts +10 -0
- package/src/tui/block.d.ts +1 -0
- package/src/tui/chatRenderer.d.ts +23 -0
- package/src/tui/chatRenderer.js +49 -49
- package/src/tui/clipboardSync.js +28 -1
- package/src/tui/clipboardSync.ts +26 -1
- package/src/tui/codeProgress.js +299 -0
- package/src/tui/codeProgress.ts +304 -0
- package/src/tui/codeStreamRenderer.js +132 -0
- package/src/tui/codeStreamRenderer.ts +141 -0
- package/src/tui/commandPresentation.js +11 -0
- package/src/tui/commandPresentation.ts +18 -0
- package/src/tui/completion.js +45 -13
- package/src/tui/composerControls.js +131 -0
- package/src/tui/composerControls.ts +184 -0
- package/src/tui/diff.d.ts +2 -0
- package/src/tui/home.js +4 -2
- package/src/tui/homeTransition.js +21 -0
- package/src/tui/homeTransition.ts +27 -0
- package/src/tui/homeView.js +62 -0
- package/src/tui/homeView.ts +91 -0
- package/src/tui/layout.js +46 -0
- package/src/tui/layout.ts +52 -0
- package/src/tui/messageActionPlacement.js +16 -0
- package/src/tui/messageActionPlacement.ts +25 -0
- package/src/tui/messageInteraction.js +221 -0
- package/src/tui/messageInteraction.ts +275 -0
- package/src/tui/modelPicker.js +4 -24
- package/src/tui/modelPicker.ts +4 -20
- package/src/tui/mouseHover.js +6 -0
- package/src/tui/mouseHover.ts +5 -0
- package/src/tui/prompt.js +175 -101
- package/src/tui/resizeRecovery.js +15 -0
- package/src/tui/resizeRecovery.ts +27 -0
- package/src/tui/scrollInteraction.js +97 -0
- package/src/tui/scrollInteraction.ts +189 -0
- package/src/tui/sessionLayout.js +12 -0
- package/src/tui/sessionLayout.ts +12 -0
- package/src/tui/sessionPicker.js +111 -16
- package/src/tui/sessionPicker.ts +121 -16
- package/src/tui/sessionSidebar.js +78 -0
- package/src/tui/sessionSidebar.ts +95 -0
- package/src/tui/setup.js +1 -1
- package/src/tui/setup.ts +1 -1
- package/src/tui/stageSurface.js +120 -58
- package/src/tui/text.d.ts +6 -0
- package/src/tui/textLayout.js +23 -0
- package/src/tui/textLayout.ts +28 -0
- package/src/tui/viewport.js +204 -279
- package/src/tui/viewport.ts +326 -0
- package/src/types/contracts.ts +54 -0
- package/src/utils/paths.js +5 -4
- package/src/utils/paths.ts +5 -5
package/src/providers/acp.js
CHANGED
|
@@ -17,8 +17,11 @@ 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;
|
|
23
|
+
currentTurnNativeActionHandler = null;
|
|
24
|
+
liveFileActions = new Map();
|
|
22
25
|
constructor(config = {}) {
|
|
23
26
|
super(config);
|
|
24
27
|
const providerStr = String(config.provider || '').toLowerCase().trim();
|
|
@@ -43,7 +46,11 @@ export class AcpProvider extends LLMProvider {
|
|
|
43
46
|
const modelIsPeerName = !configuredModel || configuredModel === 'acp' || configuredModel === this.peerId;
|
|
44
47
|
this.model = canonicalModelId(modelIsPeerName ? peerDefault : configuredModel);
|
|
45
48
|
const profile = getPeerProfile(this.peerId);
|
|
46
|
-
if (
|
|
49
|
+
if (config.command) {
|
|
50
|
+
this.command = config.command;
|
|
51
|
+
this.args = config.args || ['acp'];
|
|
52
|
+
}
|
|
53
|
+
else if (profile) {
|
|
47
54
|
this.command = profile.command;
|
|
48
55
|
this.args = profile.args || [];
|
|
49
56
|
}
|
|
@@ -54,7 +61,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
54
61
|
this.args = preset.args || [];
|
|
55
62
|
}
|
|
56
63
|
else {
|
|
57
|
-
this.command =
|
|
64
|
+
this.command = this.peerId;
|
|
58
65
|
this.args = config.args || ['acp'];
|
|
59
66
|
}
|
|
60
67
|
}
|
|
@@ -63,7 +70,7 @@ export class AcpProvider extends LLMProvider {
|
|
|
63
70
|
}
|
|
64
71
|
formatConversation(messages, tools = []) {
|
|
65
72
|
const lines = [];
|
|
66
|
-
lines.push('[System Instructions]\nYou are acting as an AI reasoning engine for Causyne Agent over ACP. Do NOT invoke your own internal host CLI tools or execute commands directly. When an action or tool is required, output an explicit tool call in this exact format:\n<tool_call>{"name": "tool_name", "arguments": { ... }}</tool_call>\nCausyne Agent will execute the tool in its secure environment and return the results back to you.');
|
|
73
|
+
lines.push('[System Instructions]\nYou are acting as an AI reasoning engine for Causyne Agent over ACP. Do NOT invoke your own internal host CLI tools or execute commands directly. When an action or tool is required, output an explicit tool call in this exact format:\n<tool_call>{"name": "tool_name", "arguments": { ... }}</tool_call>\nCausyne Agent will execute the tool in its secure environment and return the results back to you.\n\n[Language & Communication Directive]\nDynamically reflect and match the exact language used by the user in their prompt. If the user writes in English, you must respond strictly in English. Default to English for all responses and reasoning. Never default to Indonesian.');
|
|
67
74
|
if (tools && tools.length > 0) {
|
|
68
75
|
const toolSummaries = tools.map(t => {
|
|
69
76
|
const name = t.name || t.function?.name || 'unknown';
|
|
@@ -74,6 +81,8 @@ export class AcpProvider extends LLMProvider {
|
|
|
74
81
|
}).join('\n');
|
|
75
82
|
lines.push(`[Available Tools]\nYou may invoke tools when needed by outputting:\n<tool_call>{"name": "tool_name", "arguments": { ... }}</tool_call>\n\n${toolSummaries}`);
|
|
76
83
|
}
|
|
84
|
+
const seenToolOutputs = new Set();
|
|
85
|
+
const seenAssistantCalls = new Set();
|
|
77
86
|
for (const msg of messages) {
|
|
78
87
|
const role = msg.role === 'user'
|
|
79
88
|
? 'User'
|
|
@@ -91,10 +100,22 @@ export class AcpProvider extends LLMProvider {
|
|
|
91
100
|
else if (Array.isArray(msg.content)) {
|
|
92
101
|
text = msg.content.map((c) => typeof c === 'string' ? c : (c?.text || '')).filter(Boolean).join('\n');
|
|
93
102
|
}
|
|
103
|
+
if (msg.role === 'tool' && text.trim()) {
|
|
104
|
+
const normalized = text.trim();
|
|
105
|
+
if (seenToolOutputs.has(normalized)) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
seenToolOutputs.add(normalized);
|
|
109
|
+
}
|
|
94
110
|
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
|
95
111
|
for (const tc of msg.tool_calls) {
|
|
96
112
|
const fn = tc.function || {};
|
|
97
113
|
const fnArgs = typeof fn.arguments === 'string' ? fn.arguments : JSON.stringify(fn.arguments || {});
|
|
114
|
+
const callSig = `${fn.name}:${fnArgs}`;
|
|
115
|
+
if (seenAssistantCalls.has(callSig)) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
seenAssistantCalls.add(callSig);
|
|
98
119
|
text += `\n<tool_call>{"name": "${fn.name}", "arguments": ${fnArgs}}</tool_call>`;
|
|
99
120
|
}
|
|
100
121
|
}
|
|
@@ -124,6 +145,8 @@ export class AcpProvider extends LLMProvider {
|
|
|
124
145
|
}).join('\n');
|
|
125
146
|
pending.push(`[Available Tools]\nYou may invoke tools when needed by outputting:\n<tool_call>{"name": "tool_name", "arguments": { ... }}</tool_call>\n\n${toolSummaries}`);
|
|
126
147
|
}
|
|
148
|
+
const seenToolOutputs = new Set();
|
|
149
|
+
const seenAssistantCalls = new Set();
|
|
127
150
|
for (const msg of messages) {
|
|
128
151
|
const role = this._roleLabel(msg);
|
|
129
152
|
let text = '';
|
|
@@ -133,10 +156,22 @@ export class AcpProvider extends LLMProvider {
|
|
|
133
156
|
else if (Array.isArray(msg.content)) {
|
|
134
157
|
text = msg.content.map((c) => typeof c === 'string' ? c : (c?.text || '')).filter(Boolean).join('\n');
|
|
135
158
|
}
|
|
159
|
+
if (msg.role === 'tool' && text.trim()) {
|
|
160
|
+
const normalized = text.trim();
|
|
161
|
+
if (seenToolOutputs.has(normalized)) {
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
seenToolOutputs.add(normalized);
|
|
165
|
+
}
|
|
136
166
|
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
|
137
167
|
for (const tc of msg.tool_calls) {
|
|
138
168
|
const fn = tc.function || {};
|
|
139
169
|
const fnArgs = typeof fn.arguments === 'string' ? fn.arguments : JSON.stringify(fn.arguments || {});
|
|
170
|
+
const callSig = `${fn.name}:${fnArgs}`;
|
|
171
|
+
if (seenAssistantCalls.has(callSig)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
seenAssistantCalls.add(callSig);
|
|
140
175
|
text += `\n<tool_call>{"name": "${fn.name}", "arguments": ${fnArgs}}</tool_call>`;
|
|
141
176
|
}
|
|
142
177
|
}
|
|
@@ -237,9 +272,41 @@ export class AcpProvider extends LLMProvider {
|
|
|
237
272
|
}
|
|
238
273
|
}
|
|
239
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
|
+
}
|
|
240
302
|
return calls;
|
|
241
303
|
}
|
|
242
|
-
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
|
+
}
|
|
243
310
|
if (this.activeClient && this.activeSessionId) {
|
|
244
311
|
return { client: this.activeClient, sessionId: this.activeSessionId };
|
|
245
312
|
}
|
|
@@ -247,14 +314,18 @@ export class AcpProvider extends LLMProvider {
|
|
|
247
314
|
command: this.command,
|
|
248
315
|
args: this.args,
|
|
249
316
|
cwd: process.cwd(),
|
|
250
|
-
promptTimeoutMs:
|
|
317
|
+
promptTimeoutMs: 900_000,
|
|
251
318
|
permissionHandler: async (req) => {
|
|
319
|
+
if (this.currentTurnAborted || signal?.aborted)
|
|
320
|
+
return { outcome: 'cancelled' };
|
|
252
321
|
const allowOpt = req.options?.find(o => o.kind === 'allow_once' || o.kind === 'allow_always' || o.optionId === 'allow');
|
|
253
322
|
if (allowOpt)
|
|
254
323
|
return { outcome: 'selected', optionId: allowOpt.optionId };
|
|
255
324
|
return { outcome: 'cancelled' };
|
|
256
325
|
},
|
|
257
326
|
onUpdate: (_sessionId, update) => {
|
|
327
|
+
if (this.currentTurnAborted)
|
|
328
|
+
return;
|
|
258
329
|
if (update && typeof update === 'object') {
|
|
259
330
|
const updateObj = update;
|
|
260
331
|
if (updateObj.sessionUpdate === 'agent_thought_chunk') {
|
|
@@ -263,6 +334,37 @@ export class AcpProvider extends LLMProvider {
|
|
|
263
334
|
this.currentTurnReasoningHandler(thought);
|
|
264
335
|
}
|
|
265
336
|
}
|
|
337
|
+
else if (updateObj.sessionUpdate === 'agent_message_chunk') {
|
|
338
|
+
const text = updateObj.content?.type === 'text' ? updateObj.content.text : '';
|
|
339
|
+
if (text && this.currentTurnChunkHandler)
|
|
340
|
+
this.currentTurnChunkHandler(text);
|
|
341
|
+
}
|
|
342
|
+
else if (updateObj.sessionUpdate === 'tool_call' || updateObj.sessionUpdate === 'tool_call_update') {
|
|
343
|
+
const id = String(updateObj.toolCallId || '');
|
|
344
|
+
const earlier = this.liveFileActions.get(id);
|
|
345
|
+
const kind = updateObj.kind || (earlier?.name === 'fs_edit' ? 'edit' : earlier?.name === 'fs_delete' ? 'delete' : earlier?.name === 'fs_move' ? 'move' : '');
|
|
346
|
+
const name = kind === 'edit' ? 'fs_edit' : kind === 'delete' ? 'fs_delete' : kind === 'move' ? 'fs_move' : '';
|
|
347
|
+
if (name && id) {
|
|
348
|
+
const diff = Array.isArray(updateObj.content)
|
|
349
|
+
? updateObj.content.find((item) => item?.type === 'diff')
|
|
350
|
+
: null;
|
|
351
|
+
const input = updateObj.rawInput && typeof updateObj.rawInput === 'object' ? updateObj.rawInput : {};
|
|
352
|
+
const parameters = {
|
|
353
|
+
...(earlier?.parameters || {}), ...input,
|
|
354
|
+
...(diff?.path ? { path: diff.path } : {}),
|
|
355
|
+
...(diff?.newText ? { content: diff.newText } : {}),
|
|
356
|
+
...(updateObj.locations?.[0]?.path ? { path: updateObj.locations[0].path } : {})
|
|
357
|
+
};
|
|
358
|
+
const status = updateObj.status === 'completed' ? 'completed' : updateObj.status === 'failed' ? 'failed' : 'running';
|
|
359
|
+
const action = { name, status, parameters,
|
|
360
|
+
detail: String(parameters.path || updateObj.title || '') };
|
|
361
|
+
if (status === 'running')
|
|
362
|
+
this.liveFileActions.set(id, action);
|
|
363
|
+
else
|
|
364
|
+
this.liveFileActions.delete(id);
|
|
365
|
+
this.currentTurnNativeActionHandler?.(action);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
266
368
|
else if ('textDelta' in updateObj) {
|
|
267
369
|
const delta = updateObj.textDelta;
|
|
268
370
|
if (delta && typeof delta === 'string' && this.currentTurnChunkHandler) {
|
|
@@ -272,17 +374,21 @@ export class AcpProvider extends LLMProvider {
|
|
|
272
374
|
}
|
|
273
375
|
}
|
|
274
376
|
});
|
|
275
|
-
await client.start();
|
|
276
|
-
const sessionId = await client.newSession(process.cwd());
|
|
377
|
+
await client.start(signal);
|
|
378
|
+
const sessionId = await client.newSession(process.cwd(), [], signal);
|
|
277
379
|
let targetModel = this.model;
|
|
278
380
|
if (this.peerId === 'opencode' && targetModel !== this.peerId && !targetModel.startsWith('opencode/')) {
|
|
279
381
|
targetModel = `opencode/${targetModel}`;
|
|
280
382
|
}
|
|
281
383
|
if (targetModel && targetModel !== 'acp' && targetModel !== this.peerId) {
|
|
282
384
|
try {
|
|
283
|
-
await client.setSessionModel(sessionId, targetModel);
|
|
385
|
+
await client.setSessionModel(sessionId, targetModel, signal);
|
|
284
386
|
}
|
|
285
|
-
catch {
|
|
387
|
+
catch (err) {
|
|
388
|
+
if (this.currentTurnAborted || signal?.aborted || err?.name === 'AbortError') {
|
|
389
|
+
await client.close();
|
|
390
|
+
throw err;
|
|
391
|
+
}
|
|
286
392
|
// peer does not support setSessionModel
|
|
287
393
|
}
|
|
288
394
|
}
|
|
@@ -290,35 +396,72 @@ export class AcpProvider extends LLMProvider {
|
|
|
290
396
|
this.activeSessionId = sessionId;
|
|
291
397
|
return { client, sessionId };
|
|
292
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
|
+
}
|
|
293
415
|
async close() {
|
|
294
|
-
|
|
416
|
+
const client = this.activeClient;
|
|
417
|
+
this.activeClient = null;
|
|
418
|
+
this.activeSessionId = null;
|
|
419
|
+
if (client) {
|
|
295
420
|
try {
|
|
296
|
-
await
|
|
421
|
+
await client.close();
|
|
297
422
|
}
|
|
298
423
|
catch {
|
|
299
424
|
// ignore close error
|
|
300
425
|
}
|
|
301
|
-
this.activeClient = null;
|
|
302
|
-
this.activeSessionId = null;
|
|
303
426
|
}
|
|
304
427
|
}
|
|
305
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
|
+
}
|
|
306
435
|
this.currentTurnChunkHandler = options.onChunk || null;
|
|
307
436
|
this.currentTurnReasoningHandler = options.onReasoningChunk || null;
|
|
437
|
+
this.currentTurnNativeActionHandler = options.onNativeAction || null;
|
|
438
|
+
this.liveFileActions.clear();
|
|
439
|
+
const onAbort = () => {
|
|
440
|
+
this.abort();
|
|
441
|
+
};
|
|
442
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
308
443
|
try {
|
|
309
|
-
const { client, sessionId } = await this.getOrCreateClient();
|
|
444
|
+
const { client, sessionId } = await this.getOrCreateClient(options.signal);
|
|
310
445
|
const supportsImages = client.agentCapabilities?.promptCapabilities?.image === true;
|
|
311
446
|
const promptBlocks = this.formatPromptBlocks(messages, options.tools, supportsImages);
|
|
312
447
|
const promptText = promptBlocks.filter(b => b.type === 'text').map(b => b.text).join('\n\n');
|
|
313
448
|
let result;
|
|
314
449
|
try {
|
|
315
|
-
result = await client.prompt(sessionId, promptBlocks);
|
|
450
|
+
result = await client.prompt(sessionId, promptBlocks, options.signal);
|
|
316
451
|
}
|
|
317
452
|
catch (promptErr) {
|
|
318
|
-
|
|
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
|
|
319
457
|
await this.close();
|
|
320
|
-
const fresh = await this.getOrCreateClient();
|
|
321
|
-
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;
|
|
322
465
|
}
|
|
323
466
|
let rawContent = (result?.text || '').trim();
|
|
324
467
|
const toolCalls = this.extractToolCalls(rawContent);
|
|
@@ -335,15 +478,27 @@ export class AcpProvider extends LLMProvider {
|
|
|
335
478
|
if (toolCalls.length > 0) {
|
|
336
479
|
response.toolCalls = toolCalls;
|
|
337
480
|
}
|
|
481
|
+
if (result?.stopReason === 'max_tokens' || result?.stopReason === 'max_turn_requests') {
|
|
482
|
+
response.incompleteTurn = true;
|
|
483
|
+
}
|
|
338
484
|
return response;
|
|
339
485
|
}
|
|
340
486
|
catch (err) {
|
|
341
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
|
+
}
|
|
342
493
|
const msg = err instanceof Error ? err.message : String(err);
|
|
343
494
|
throw new Error(`ACP Peer Agent (${this.peerId}) Error: ${msg}. Make sure "${this.command}" is installed and running.`);
|
|
344
495
|
}
|
|
345
496
|
finally {
|
|
497
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
346
498
|
this.currentTurnChunkHandler = null;
|
|
499
|
+
this.currentTurnReasoningHandler = null;
|
|
500
|
+
this.currentTurnNativeActionHandler = null;
|
|
501
|
+
this.liveFileActions.clear();
|
|
347
502
|
}
|
|
348
503
|
}
|
|
349
504
|
}
|