@causyne/agent 1.13.0 → 1.14.0
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/package.json +1 -1
- package/src/acp/agentCatalog.js +389 -0
- package/src/acp/agentCatalog.ts +455 -0
- package/src/acp/client.js +127 -4
- package/src/acp/client.ts +145 -5
- package/src/acp/peerProfiles.js +81 -3
- package/src/acp/peerProfiles.ts +85 -4
- package/src/acp/protocol.js +2 -0
- package/src/acp/protocol.ts +19 -0
- package/src/agentLoop.js +33 -12
- package/src/commands.js +108 -0
- package/src/entrypoint.js +11 -0
- package/src/providers/anthropic.js +5 -13
- package/src/providers/dashscope-byok.js +3 -4
- package/src/providers/deepseek.js +3 -4
- package/src/providers/gemini.js +17 -101
- package/src/providers/moonshot.js +3 -4
- package/src/providers/ollama.js +3 -4
- package/src/providers/openai-compatible.js +3 -4
- package/src/providers/qwen-byok.js +3 -4
- package/src/providers/xai.js +3 -4
- package/src/tools/peerAgent.js +115 -31
- package/src/tools/peerAgent.ts +143 -32
- package/src/tools/registry.js +144 -16
- package/src/tools/toolScanner.js +349 -0
- package/src/tools/toolScanner.ts +451 -0
- package/src/tools/toolSelfRepair.js +759 -0
- package/src/tools/toolSelfRepair.ts +846 -0
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Agent — ACP Agent Catalog & Zero-Token Discovery
|
|
3
|
+
*
|
|
4
|
+
* Provides built-in profiles, auth probing, and zero-token discovery for
|
|
5
|
+
* common external coding agents that support the Agent Client Protocol (ACP),
|
|
6
|
+
* such as Claude Code, Codex, OpenCode, Gemini CLI, Hermes, Cursor CLI,
|
|
7
|
+
* Qwen, Copilot, Kimi, and Goose.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { execFile } from 'node:child_process';
|
|
13
|
+
import { promisify } from 'node:util';
|
|
14
|
+
import { AcpClient } from './client.js';
|
|
15
|
+
import type { PeerProfile } from './peerProfiles.js';
|
|
16
|
+
|
|
17
|
+
const execFileAsync = promisify(execFile);
|
|
18
|
+
|
|
19
|
+
export interface BuiltinPeerPreset {
|
|
20
|
+
id: string;
|
|
21
|
+
label: string;
|
|
22
|
+
description: string;
|
|
23
|
+
command: string;
|
|
24
|
+
args: string[];
|
|
25
|
+
binaryName: string;
|
|
26
|
+
fallbackCommand?: string;
|
|
27
|
+
fallbackArgs?: string[];
|
|
28
|
+
authType: 'subscription' | 'oauth' | 'api_key' | 'none';
|
|
29
|
+
authProbe?: {
|
|
30
|
+
type: 'json' | 'output_match' | 'file_exists';
|
|
31
|
+
command?: string;
|
|
32
|
+
args?: string[];
|
|
33
|
+
filePath?: string;
|
|
34
|
+
positiveMatch?: string;
|
|
35
|
+
negativeMatch?: string;
|
|
36
|
+
jsonField?: string;
|
|
37
|
+
};
|
|
38
|
+
setupInstruction?: string;
|
|
39
|
+
timeoutSeconds?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface PeerDetectionResult {
|
|
43
|
+
id: string;
|
|
44
|
+
label: string;
|
|
45
|
+
status: 'ready' | 'needs_auth' | 'not_installed' | 'error';
|
|
46
|
+
command: string;
|
|
47
|
+
args: string[];
|
|
48
|
+
details?: string;
|
|
49
|
+
agentInfo?: { name: string; version: string };
|
|
50
|
+
authMethods?: string[];
|
|
51
|
+
setupInstruction?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const BUILTIN_PEER_PRESETS: Record<string, BuiltinPeerPreset> = {
|
|
55
|
+
opencode: {
|
|
56
|
+
id: 'opencode',
|
|
57
|
+
label: 'OpenCode',
|
|
58
|
+
description: 'OpenCode AI coding agent with support for free and local models',
|
|
59
|
+
command: 'opencode',
|
|
60
|
+
args: ['acp'],
|
|
61
|
+
binaryName: 'opencode',
|
|
62
|
+
fallbackCommand: 'npx',
|
|
63
|
+
fallbackArgs: ['-y', 'opencode-ai', 'acp'],
|
|
64
|
+
authType: 'none',
|
|
65
|
+
setupInstruction: 'Run "opencode auth login" or configure a model in ~/.config/opencode',
|
|
66
|
+
timeoutSeconds: 180
|
|
67
|
+
},
|
|
68
|
+
claude: {
|
|
69
|
+
id: 'claude',
|
|
70
|
+
label: 'Claude Code',
|
|
71
|
+
description: 'Anthropic Claude Code CLI via official ACP bridge adapter',
|
|
72
|
+
command: 'npx',
|
|
73
|
+
args: ['-y', '@agentclientprotocol/claude-agent-acp@0.70.0'],
|
|
74
|
+
binaryName: 'claude',
|
|
75
|
+
authType: 'subscription',
|
|
76
|
+
authProbe: {
|
|
77
|
+
type: 'json',
|
|
78
|
+
command: 'claude',
|
|
79
|
+
args: ['auth', 'status', '--json'],
|
|
80
|
+
jsonField: 'loggedIn'
|
|
81
|
+
},
|
|
82
|
+
setupInstruction: 'Run "claude login" to authenticate with your Anthropic subscription or set ANTHROPIC_API_KEY',
|
|
83
|
+
timeoutSeconds: 300
|
|
84
|
+
},
|
|
85
|
+
codex: {
|
|
86
|
+
id: 'codex',
|
|
87
|
+
label: 'Codex CLI',
|
|
88
|
+
description: 'OpenAI Codex CLI coding agent via ACP adapter',
|
|
89
|
+
command: 'npx',
|
|
90
|
+
args: ['-y', '@agentclientprotocol/codex-acp@1.6.0'],
|
|
91
|
+
binaryName: 'codex',
|
|
92
|
+
authType: 'subscription',
|
|
93
|
+
authProbe: {
|
|
94
|
+
type: 'output_match',
|
|
95
|
+
command: 'codex',
|
|
96
|
+
args: ['login', 'status'],
|
|
97
|
+
positiveMatch: 'logged in',
|
|
98
|
+
negativeMatch: 'not logged in'
|
|
99
|
+
},
|
|
100
|
+
setupInstruction: 'Run "codex login" or set OPENAI_API_KEY in your environment',
|
|
101
|
+
timeoutSeconds: 300
|
|
102
|
+
},
|
|
103
|
+
gemini: {
|
|
104
|
+
id: 'gemini',
|
|
105
|
+
label: 'Gemini CLI',
|
|
106
|
+
description: 'Google Gemini CLI agent with free tier model support',
|
|
107
|
+
command: 'gemini',
|
|
108
|
+
args: ['--acp'],
|
|
109
|
+
binaryName: 'gemini',
|
|
110
|
+
fallbackCommand: 'npx',
|
|
111
|
+
fallbackArgs: ['-y', '@google/gemini-cli', '--acp'],
|
|
112
|
+
authType: 'oauth',
|
|
113
|
+
authProbe: {
|
|
114
|
+
type: 'file_exists',
|
|
115
|
+
filePath: path.join(process.env.HOME || '', '.gemini', 'oauth_creds.json')
|
|
116
|
+
},
|
|
117
|
+
setupInstruction: 'Run "gemini auth" or set GEMINI_API_KEY in your environment',
|
|
118
|
+
timeoutSeconds: 240
|
|
119
|
+
},
|
|
120
|
+
hermes: {
|
|
121
|
+
id: 'hermes',
|
|
122
|
+
label: 'Hermes Agent',
|
|
123
|
+
description: 'Nous Research Hermes autonomous AI agent',
|
|
124
|
+
command: 'hermes',
|
|
125
|
+
args: ['acp'],
|
|
126
|
+
binaryName: 'hermes',
|
|
127
|
+
authType: 'api_key',
|
|
128
|
+
setupInstruction: 'Run "hermes acp --setup" to configure provider credentials',
|
|
129
|
+
timeoutSeconds: 240
|
|
130
|
+
},
|
|
131
|
+
cursor: {
|
|
132
|
+
id: 'cursor',
|
|
133
|
+
label: 'Cursor CLI',
|
|
134
|
+
description: 'Cursor CLI coding agent over ACP',
|
|
135
|
+
command: 'cursor-agent',
|
|
136
|
+
args: ['acp'],
|
|
137
|
+
binaryName: 'cursor-agent',
|
|
138
|
+
authType: 'subscription',
|
|
139
|
+
setupInstruction: 'Run "cursor-agent login" to sign in to your Cursor account',
|
|
140
|
+
timeoutSeconds: 180
|
|
141
|
+
},
|
|
142
|
+
qwen: {
|
|
143
|
+
id: 'qwen',
|
|
144
|
+
label: 'Qwen Code',
|
|
145
|
+
description: 'Alibaba Qwen Code AI assistant over ACP',
|
|
146
|
+
command: 'qwen',
|
|
147
|
+
args: ['--acp'],
|
|
148
|
+
binaryName: 'qwen',
|
|
149
|
+
authType: 'api_key',
|
|
150
|
+
setupInstruction: 'Set DASHSCOPE_API_KEY or run "qwen login"',
|
|
151
|
+
timeoutSeconds: 180
|
|
152
|
+
},
|
|
153
|
+
copilot: {
|
|
154
|
+
id: 'copilot',
|
|
155
|
+
label: 'GitHub Copilot CLI',
|
|
156
|
+
description: 'GitHub Copilot CLI agent over ACP',
|
|
157
|
+
command: 'copilot',
|
|
158
|
+
args: ['--acp', '--stdio'],
|
|
159
|
+
binaryName: 'copilot',
|
|
160
|
+
authType: 'subscription',
|
|
161
|
+
setupInstruction: 'Run "copilot auth" to link your GitHub Copilot subscription',
|
|
162
|
+
timeoutSeconds: 180
|
|
163
|
+
},
|
|
164
|
+
kimi: {
|
|
165
|
+
id: 'kimi',
|
|
166
|
+
label: 'Kimi CLI',
|
|
167
|
+
description: 'Moonshot AI Kimi coding assistant via ACP',
|
|
168
|
+
command: 'kimi',
|
|
169
|
+
args: ['acp'],
|
|
170
|
+
binaryName: 'kimi',
|
|
171
|
+
authType: 'api_key',
|
|
172
|
+
setupInstruction: 'Set MOONSHOT_API_KEY in your environment',
|
|
173
|
+
timeoutSeconds: 180
|
|
174
|
+
},
|
|
175
|
+
goose: {
|
|
176
|
+
id: 'goose',
|
|
177
|
+
label: 'Goose CLI',
|
|
178
|
+
description: 'Block Goose extensible AI coding agent',
|
|
179
|
+
command: 'goose',
|
|
180
|
+
args: ['acp'],
|
|
181
|
+
binaryName: 'goose',
|
|
182
|
+
authType: 'api_key',
|
|
183
|
+
setupInstruction: 'Run "goose configure" to set up providers',
|
|
184
|
+
timeoutSeconds: 180
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Check if a command or binary exists in PATH.
|
|
190
|
+
*/
|
|
191
|
+
export function isBinaryOnPath(binaryName: string): boolean {
|
|
192
|
+
if (path.isAbsolute(binaryName)) {
|
|
193
|
+
return fs.existsSync(binaryName);
|
|
194
|
+
}
|
|
195
|
+
const envPath = process.env.PATH || '';
|
|
196
|
+
const delimiter = process.platform === 'win32' ? ';' : ':';
|
|
197
|
+
const dirs = envPath.split(delimiter);
|
|
198
|
+
const extensions = process.platform === 'win32' ? ['.exe', '.cmd', '.bat', ''] : [''];
|
|
199
|
+
|
|
200
|
+
for (const dir of dirs) {
|
|
201
|
+
if (!dir) continue;
|
|
202
|
+
for (const ext of extensions) {
|
|
203
|
+
const full = path.join(dir, `${binaryName}${ext}`);
|
|
204
|
+
try {
|
|
205
|
+
if (fs.existsSync(full) && fs.statSync(full).isFile()) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
} catch {
|
|
209
|
+
// Skip inaccessible path segments
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Probe authorization state without spending model tokens.
|
|
218
|
+
*/
|
|
219
|
+
export async function probePresetAuth(preset: BuiltinPeerPreset): Promise<{ authenticated: boolean; reason?: string }> {
|
|
220
|
+
if (!preset.authProbe) {
|
|
221
|
+
return { authenticated: true };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const probe = preset.authProbe;
|
|
225
|
+
|
|
226
|
+
if (probe.type === 'file_exists' && probe.filePath) {
|
|
227
|
+
const exists = fs.existsSync(probe.filePath);
|
|
228
|
+
return {
|
|
229
|
+
authenticated: exists,
|
|
230
|
+
reason: exists ? 'OAuth credentials file detected' : `Credentials file not found at ${probe.filePath}`
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (probe.type === 'json' && probe.command) {
|
|
235
|
+
if (!isBinaryOnPath(probe.command)) {
|
|
236
|
+
return { authenticated: false, reason: `${probe.command} CLI not found on PATH` };
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
const { stdout } = await execFileAsync(probe.command, probe.args || [], { timeout: 8000 });
|
|
240
|
+
const parsed = JSON.parse(stdout.trim());
|
|
241
|
+
if (probe.jsonField && typeof parsed === 'object' && parsed !== null) {
|
|
242
|
+
const val = (parsed as Record<string, unknown>)[probe.jsonField];
|
|
243
|
+
const isAuth = Boolean(val);
|
|
244
|
+
return {
|
|
245
|
+
authenticated: isAuth,
|
|
246
|
+
reason: isAuth ? 'Valid CLI login session active' : 'CLI reports not logged in'
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return { authenticated: true, reason: 'CLI auth responded' };
|
|
250
|
+
} catch {
|
|
251
|
+
return { authenticated: false, reason: `Failed to probe ${probe.command} auth status` };
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (probe.type === 'output_match' && probe.command) {
|
|
256
|
+
if (!isBinaryOnPath(probe.command)) {
|
|
257
|
+
return { authenticated: false, reason: `${probe.command} CLI not found on PATH` };
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
const result = await execFileAsync(probe.command, probe.args || [], { timeout: 8000 }).catch((e) => e);
|
|
261
|
+
const combined = `${result?.stdout || ''}\n${result?.stderr || ''}`.toLowerCase();
|
|
262
|
+
if (probe.negativeMatch && combined.includes(probe.negativeMatch.toLowerCase())) {
|
|
263
|
+
return { authenticated: false, reason: 'CLI reported not logged in' };
|
|
264
|
+
}
|
|
265
|
+
if (probe.positiveMatch && combined.includes(probe.positiveMatch.toLowerCase())) {
|
|
266
|
+
return { authenticated: true, reason: 'CLI login detected' };
|
|
267
|
+
}
|
|
268
|
+
return { authenticated: false, reason: 'No active login detected' };
|
|
269
|
+
} catch {
|
|
270
|
+
return { authenticated: false, reason: `Failed to probe ${probe.command} auth` };
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return { authenticated: true };
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Execute a zero-token ACP handshake (initialize + session/new) with strict timeout.
|
|
279
|
+
* Never sends session/prompt, ensuring $0 token consumption.
|
|
280
|
+
*/
|
|
281
|
+
export async function testPeerHandshake(
|
|
282
|
+
options: {
|
|
283
|
+
command: string;
|
|
284
|
+
args?: string[];
|
|
285
|
+
cwd?: string;
|
|
286
|
+
timeoutMs?: number;
|
|
287
|
+
}
|
|
288
|
+
): Promise<{
|
|
289
|
+
ready: boolean;
|
|
290
|
+
agentInfo?: { name: string; version: string };
|
|
291
|
+
authMethods?: string[];
|
|
292
|
+
error?: string;
|
|
293
|
+
}> {
|
|
294
|
+
const cwd = options.cwd || process.cwd();
|
|
295
|
+
const clientOptions: { command: string; args?: string[]; cwd: string; promptTimeoutMs: number } = {
|
|
296
|
+
command: options.command,
|
|
297
|
+
cwd,
|
|
298
|
+
promptTimeoutMs: options.timeoutMs || 15000
|
|
299
|
+
};
|
|
300
|
+
if (options.args) {
|
|
301
|
+
clientOptions.args = options.args;
|
|
302
|
+
}
|
|
303
|
+
const client = new AcpClient(clientOptions);
|
|
304
|
+
|
|
305
|
+
const timeoutMs = options.timeoutMs || 12000;
|
|
306
|
+
try {
|
|
307
|
+
const init = await Promise.race([
|
|
308
|
+
client.start(),
|
|
309
|
+
new Promise<never>((_, reject) =>
|
|
310
|
+
setTimeout(() => reject(new Error(`ACP initialization timed out after ${timeoutMs}ms`)), timeoutMs)
|
|
311
|
+
)
|
|
312
|
+
]);
|
|
313
|
+
|
|
314
|
+
const agentInfo = init.agentInfo ? { name: init.agentInfo.name, version: init.agentInfo.version } : undefined;
|
|
315
|
+
const authMethods = Array.isArray(init.authMethods)
|
|
316
|
+
? init.authMethods.map((m) => (typeof m === 'object' && m !== null && 'id' in m ? String((m as { id: unknown }).id) : String(m)))
|
|
317
|
+
: [];
|
|
318
|
+
|
|
319
|
+
// Attempt session/new to ensure agent is fully operational
|
|
320
|
+
try {
|
|
321
|
+
await client.newSession(cwd);
|
|
322
|
+
return {
|
|
323
|
+
ready: true,
|
|
324
|
+
...(agentInfo ? { agentInfo } : {}),
|
|
325
|
+
authMethods
|
|
326
|
+
};
|
|
327
|
+
} catch (sessionErr: unknown) {
|
|
328
|
+
const msg = sessionErr instanceof Error ? sessionErr.message : String(sessionErr);
|
|
329
|
+
return {
|
|
330
|
+
ready: false,
|
|
331
|
+
...(agentInfo ? { agentInfo } : {}),
|
|
332
|
+
authMethods,
|
|
333
|
+
error: `Session creation failed: ${msg}`
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
} catch (initErr: unknown) {
|
|
337
|
+
const msg = initErr instanceof Error ? initErr.message : String(initErr);
|
|
338
|
+
return { ready: false, error: msg };
|
|
339
|
+
} finally {
|
|
340
|
+
await client.close().catch(() => {});
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Detect all built-in ACP presets available on this machine and report their readiness.
|
|
346
|
+
*/
|
|
347
|
+
export async function detectAllPeers(cwd: string = process.cwd()): Promise<PeerDetectionResult[]> {
|
|
348
|
+
const results: PeerDetectionResult[] = [];
|
|
349
|
+
|
|
350
|
+
for (const preset of Object.values(BUILTIN_PEER_PRESETS)) {
|
|
351
|
+
const hasDirectBinary = isBinaryOnPath(preset.binaryName);
|
|
352
|
+
const hasRunner = isBinaryOnPath(preset.command);
|
|
353
|
+
|
|
354
|
+
if (!hasDirectBinary && !hasRunner) {
|
|
355
|
+
results.push({
|
|
356
|
+
id: preset.id,
|
|
357
|
+
label: preset.label,
|
|
358
|
+
status: 'not_installed',
|
|
359
|
+
command: preset.command,
|
|
360
|
+
args: preset.args,
|
|
361
|
+
details: `Binary "${preset.binaryName}" and runner "${preset.command}" not found on PATH.`,
|
|
362
|
+
...(preset.setupInstruction ? { setupInstruction: preset.setupInstruction } : {})
|
|
363
|
+
});
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Determine actual command to execute
|
|
368
|
+
let activeCommand = preset.command;
|
|
369
|
+
let activeArgs = preset.args;
|
|
370
|
+
|
|
371
|
+
if (!hasDirectBinary && preset.fallbackCommand && isBinaryOnPath(preset.fallbackCommand)) {
|
|
372
|
+
activeCommand = preset.fallbackCommand;
|
|
373
|
+
activeArgs = preset.fallbackArgs || [];
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Run auth probe
|
|
377
|
+
const auth = await probePresetAuth(preset);
|
|
378
|
+
if (!auth.authenticated) {
|
|
379
|
+
results.push({
|
|
380
|
+
id: preset.id,
|
|
381
|
+
label: preset.label,
|
|
382
|
+
status: 'needs_auth',
|
|
383
|
+
command: activeCommand,
|
|
384
|
+
args: activeArgs,
|
|
385
|
+
details: auth.reason || 'Authentication required.',
|
|
386
|
+
...(preset.setupInstruction ? { setupInstruction: preset.setupInstruction } : {})
|
|
387
|
+
});
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Run zero-token handshake
|
|
392
|
+
const handshake = await testPeerHandshake({
|
|
393
|
+
command: activeCommand,
|
|
394
|
+
args: activeArgs,
|
|
395
|
+
cwd,
|
|
396
|
+
timeoutMs: 8000
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
if (handshake.ready) {
|
|
400
|
+
results.push({
|
|
401
|
+
id: preset.id,
|
|
402
|
+
label: preset.label,
|
|
403
|
+
status: 'ready',
|
|
404
|
+
command: activeCommand,
|
|
405
|
+
args: activeArgs,
|
|
406
|
+
...(handshake.agentInfo ? { agentInfo: handshake.agentInfo } : {}),
|
|
407
|
+
...(handshake.authMethods ? { authMethods: handshake.authMethods } : {}),
|
|
408
|
+
details: 'Operational and ready for delegation.'
|
|
409
|
+
});
|
|
410
|
+
} else if (handshake.error && /auth/i.test(handshake.error)) {
|
|
411
|
+
results.push({
|
|
412
|
+
id: preset.id,
|
|
413
|
+
label: preset.label,
|
|
414
|
+
status: 'needs_auth',
|
|
415
|
+
command: activeCommand,
|
|
416
|
+
args: activeArgs,
|
|
417
|
+
...(handshake.agentInfo ? { agentInfo: handshake.agentInfo } : {}),
|
|
418
|
+
...(handshake.authMethods ? { authMethods: handshake.authMethods } : {}),
|
|
419
|
+
details: handshake.error,
|
|
420
|
+
...(preset.setupInstruction ? { setupInstruction: preset.setupInstruction } : {})
|
|
421
|
+
});
|
|
422
|
+
} else {
|
|
423
|
+
results.push({
|
|
424
|
+
id: preset.id,
|
|
425
|
+
label: preset.label,
|
|
426
|
+
status: hasDirectBinary ? 'error' : 'not_installed',
|
|
427
|
+
command: activeCommand,
|
|
428
|
+
args: activeArgs,
|
|
429
|
+
details: handshake.error || 'ACP handshake did not complete.',
|
|
430
|
+
...(preset.setupInstruction ? { setupInstruction: preset.setupInstruction } : {})
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return results;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Convert a preset to a runnable PeerProfile.
|
|
440
|
+
*/
|
|
441
|
+
export function getPresetProfile(id: string): PeerProfile | null {
|
|
442
|
+
const preset = BUILTIN_PEER_PRESETS[id.toLowerCase()];
|
|
443
|
+
if (!preset) return null;
|
|
444
|
+
|
|
445
|
+
return {
|
|
446
|
+
id: preset.id,
|
|
447
|
+
label: preset.label,
|
|
448
|
+
command: preset.command,
|
|
449
|
+
args: preset.args,
|
|
450
|
+
cwd: null,
|
|
451
|
+
policy: 'read-only',
|
|
452
|
+
timeoutSeconds: preset.timeoutSeconds || 180,
|
|
453
|
+
env: {}
|
|
454
|
+
};
|
|
455
|
+
}
|
package/src/acp/client.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
2
4
|
import { ACP_ERROR_CODES, ACP_METHODS, ACP_PROTOCOL_VERSION, isJsonRpcRequest, isJsonRpcResponse } from './protocol.js';
|
|
3
5
|
const MAX_STDERR_CHARS = 4000;
|
|
4
6
|
const MAX_UPDATE_CHARS = 200_000;
|
|
@@ -11,12 +13,22 @@ export class AcpClient {
|
|
|
11
13
|
nextId = 1;
|
|
12
14
|
closed = false;
|
|
13
15
|
exitError = null;
|
|
16
|
+
initResult = null;
|
|
14
17
|
constructor(options) {
|
|
15
18
|
this.options = options;
|
|
16
19
|
}
|
|
17
20
|
get command() {
|
|
18
21
|
return this.options.command;
|
|
19
22
|
}
|
|
23
|
+
get agentInfo() {
|
|
24
|
+
return this.initResult?.agentInfo;
|
|
25
|
+
}
|
|
26
|
+
get agentCapabilities() {
|
|
27
|
+
return this.initResult?.agentCapabilities;
|
|
28
|
+
}
|
|
29
|
+
get authMethods() {
|
|
30
|
+
return this.initResult?.authMethods;
|
|
31
|
+
}
|
|
20
32
|
async start() {
|
|
21
33
|
if (this.child)
|
|
22
34
|
throw new Error('client already started');
|
|
@@ -42,16 +54,20 @@ export class AcpClient {
|
|
|
42
54
|
const detail = this.stderrTail.trim() || `exit code ${code ?? 'null'}${signal ? ` signal ${signal}` : ''}`;
|
|
43
55
|
this.failAll(new Error(`agent process exited: ${detail}`));
|
|
44
56
|
});
|
|
57
|
+
const hasFsRead = Boolean(this.options.fsReadHandler || this.options.cwd);
|
|
58
|
+
const hasFsWrite = Boolean(this.options.fsWriteHandler || this.options.cwd);
|
|
45
59
|
const params = {
|
|
46
60
|
protocolVersion: ACP_PROTOCOL_VERSION,
|
|
47
61
|
clientCapabilities: {
|
|
48
|
-
fs: { readTextFile:
|
|
62
|
+
fs: { readTextFile: hasFsRead, writeTextFile: hasFsWrite },
|
|
49
63
|
terminal: false,
|
|
50
|
-
session: { notices:
|
|
64
|
+
session: { notices: true }
|
|
51
65
|
},
|
|
52
66
|
clientInfo: { name: 'Causyne Agent', version: this.options.clientVersion ?? '0.0.0' }
|
|
53
67
|
};
|
|
54
|
-
|
|
68
|
+
const result = await this.request(ACP_METHODS.INITIALIZE, params, 30_000);
|
|
69
|
+
this.initResult = result;
|
|
70
|
+
return result;
|
|
55
71
|
}
|
|
56
72
|
async newSession(cwd, mcpServers = []) {
|
|
57
73
|
const result = await this.request(ACP_METHODS.NEW_SESSION, {
|
|
@@ -63,10 +79,35 @@ export class AcpClient {
|
|
|
63
79
|
}
|
|
64
80
|
return result.sessionId;
|
|
65
81
|
}
|
|
82
|
+
async authenticate(methodId, params = {}) {
|
|
83
|
+
return await this.request(ACP_METHODS.AUTHENTICATE, {
|
|
84
|
+
methodId,
|
|
85
|
+
params
|
|
86
|
+
}, 30_000);
|
|
87
|
+
}
|
|
88
|
+
async setSessionModel(sessionId, modelId) {
|
|
89
|
+
await this.request(ACP_METHODS.SET_MODEL, {
|
|
90
|
+
sessionId,
|
|
91
|
+
modelId
|
|
92
|
+
}, 30_000);
|
|
93
|
+
}
|
|
94
|
+
async setSessionMode(sessionId, modeId) {
|
|
95
|
+
await this.request(ACP_METHODS.SET_MODE, {
|
|
96
|
+
sessionId,
|
|
97
|
+
modeId
|
|
98
|
+
}, 30_000);
|
|
99
|
+
}
|
|
100
|
+
async loadSession(sessionId, cwd) {
|
|
101
|
+
return await this.request(ACP_METHODS.LOAD, {
|
|
102
|
+
sessionId,
|
|
103
|
+
cwd: cwd || this.options.cwd
|
|
104
|
+
}, 30_000);
|
|
105
|
+
}
|
|
66
106
|
async prompt(sessionId, text) {
|
|
67
107
|
const blocks = [{ type: 'text', text }];
|
|
68
108
|
let collected = '';
|
|
69
109
|
const collector = (update) => {
|
|
110
|
+
this.options.onUpdate?.(sessionId, update);
|
|
70
111
|
if (update.sessionUpdate === 'agent_message_chunk' && update.content.type === 'text') {
|
|
71
112
|
collected = `${collected}${update.content.text}`.slice(0, MAX_UPDATE_CHARS);
|
|
72
113
|
}
|
|
@@ -161,6 +202,7 @@ export class AcpClient {
|
|
|
161
202
|
const params = parsed.params;
|
|
162
203
|
if (params && typeof params.sessionId === 'string' && params.update) {
|
|
163
204
|
this.updateListener?.(params.sessionId, params.update);
|
|
205
|
+
this.options.onUpdate?.(params.sessionId, params.update);
|
|
164
206
|
}
|
|
165
207
|
}
|
|
166
208
|
}
|
|
@@ -184,6 +226,84 @@ export class AcpClient {
|
|
|
184
226
|
}
|
|
185
227
|
return;
|
|
186
228
|
}
|
|
229
|
+
if (request.method === ACP_METHODS.READ_TEXT_FILE) {
|
|
230
|
+
const params = request.params;
|
|
231
|
+
const targetPath = params?.path;
|
|
232
|
+
if (!targetPath || typeof targetPath !== 'string') {
|
|
233
|
+
this.write({
|
|
234
|
+
jsonrpc: '2.0',
|
|
235
|
+
id: request.id,
|
|
236
|
+
error: { code: ACP_ERROR_CODES.INVALID_PARAMS, message: 'Missing file path' }
|
|
237
|
+
});
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
let content;
|
|
242
|
+
if (this.options.fsReadHandler) {
|
|
243
|
+
const readOpts = {};
|
|
244
|
+
if (params?.line !== undefined)
|
|
245
|
+
readOpts.line = params.line;
|
|
246
|
+
if (params?.limit !== undefined)
|
|
247
|
+
readOpts.limit = params.limit;
|
|
248
|
+
content = await this.options.fsReadHandler(targetPath, readOpts);
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
const resolved = path.resolve(this.options.cwd, targetPath);
|
|
252
|
+
content = await fs.readFile(resolved, 'utf8');
|
|
253
|
+
}
|
|
254
|
+
this.write({
|
|
255
|
+
jsonrpc: '2.0',
|
|
256
|
+
id: request.id,
|
|
257
|
+
result: { content }
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
catch (err) {
|
|
261
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
262
|
+
this.write({
|
|
263
|
+
jsonrpc: '2.0',
|
|
264
|
+
id: request.id,
|
|
265
|
+
error: { code: ACP_ERROR_CODES.RESOURCE_NOT_FOUND, message: `Could not read file: ${msg}` }
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (request.method === ACP_METHODS.WRITE_TEXT_FILE) {
|
|
271
|
+
const params = request.params;
|
|
272
|
+
const targetPath = params?.path;
|
|
273
|
+
const content = params?.content;
|
|
274
|
+
if (!targetPath || typeof targetPath !== 'string' || typeof content !== 'string') {
|
|
275
|
+
this.write({
|
|
276
|
+
jsonrpc: '2.0',
|
|
277
|
+
id: request.id,
|
|
278
|
+
error: { code: ACP_ERROR_CODES.INVALID_PARAMS, message: 'Missing file path or content' }
|
|
279
|
+
});
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
if (this.options.fsWriteHandler) {
|
|
284
|
+
await this.options.fsWriteHandler(targetPath, content);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
const resolved = path.resolve(this.options.cwd, targetPath);
|
|
288
|
+
await fs.mkdir(path.dirname(resolved), { recursive: true });
|
|
289
|
+
await fs.writeFile(resolved, content, 'utf8');
|
|
290
|
+
}
|
|
291
|
+
this.write({
|
|
292
|
+
jsonrpc: '2.0',
|
|
293
|
+
id: request.id,
|
|
294
|
+
result: {}
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
catch (err) {
|
|
298
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
299
|
+
this.write({
|
|
300
|
+
jsonrpc: '2.0',
|
|
301
|
+
id: request.id,
|
|
302
|
+
error: { code: ACP_ERROR_CODES.INTERNAL_ERROR, message: `Could not write file: ${msg}` }
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
187
307
|
this.write({
|
|
188
308
|
jsonrpc: '2.0',
|
|
189
309
|
id: request.id,
|
|
@@ -199,7 +319,10 @@ export class AcpClient {
|
|
|
199
319
|
clearTimeout(pending.timer);
|
|
200
320
|
this.pending.delete(response.id);
|
|
201
321
|
if (response.error) {
|
|
202
|
-
|
|
322
|
+
const err = new Error(`${pending.method} failed: ${response.error.message}`);
|
|
323
|
+
err.code = response.error.code;
|
|
324
|
+
err.data = response.error.data;
|
|
325
|
+
pending.reject(err);
|
|
203
326
|
return;
|
|
204
327
|
}
|
|
205
328
|
pending.resolve(response.result);
|