@leavewhite9088/harness-bus 0.2.0 → 0.2.1
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/ARCHITECTURE.md +81 -81
- package/LICENSE +21 -21
- package/README.md +86 -86
- package/SETUP_FOR_AI.md +79 -79
- package/package.json +4 -2
- package/packages/agentd/agentd.config.json +3 -1
- package/packages/agentd/cli/commands.mjs +854 -810
- package/packages/agentd/cli/format.mjs +294 -238
- package/packages/agentd/cli/hbus.mjs +14 -14
- package/packages/agentd/cli/index.mjs +4 -4
- package/packages/agentd/cli/skill/SKILL.md +66 -61
- package/packages/agentd/cli/skill/references/custom.md +12 -0
- package/packages/agentd/cli/skill/references/playbooks.md +89 -86
- package/packages/agentd/cli/skill/references/routing.md +43 -35
- package/packages/agentd/cli/skill-install.mjs +157 -140
- package/packages/agentd/cli/update.mjs +221 -221
- package/packages/agentd/client/index.mjs +3 -3
- package/packages/agentd/contexts/executor.md +13 -12
- package/packages/agentd/contexts/templates/architect.md +12 -12
- package/packages/agentd/contexts/templates/coder.md +12 -12
- package/packages/agentd/contexts/templates/debugger.md +12 -12
- package/packages/agentd/contexts/templates/researcher.md +12 -12
- package/packages/agentd/contexts/templates/reviewer.md +12 -12
- package/packages/agentd/contexts/templates/tester.md +12 -12
- package/packages/agentd/core/base/data-dir.mjs +39 -39
- package/packages/agentd/core/base/index.mjs +5 -5
- package/packages/agentd/core/base/protocol.mjs +13 -13
- package/packages/agentd/core/env/config.mjs +52 -1
- package/packages/agentd/core/env/index.mjs +6 -1
- package/packages/agentd/core/env/install.mjs +82 -4
- package/packages/agentd/core/pi/index.mjs +1 -1
- package/packages/agentd/core/pi/rpc.mjs +206 -206
- package/packages/agentd/core/pool/acp/client.mjs +7 -9
- package/packages/agentd/core/pool/acp/process.mjs +27 -18
- package/packages/agentd/core/pool/acp/run-tracker.mjs +293 -293
- package/packages/agentd/core/pool/acp/sessions.mjs +465 -465
- package/packages/agentd/core/pool/acp/turn-runner.mjs +5 -3
- package/packages/agentd/core/pool/index.mjs +2 -2
- package/packages/agentd/core/pool/instances.mjs +3 -5
- package/packages/agentd/core/pool/probe.mjs +91 -85
- package/packages/agentd/core/pool/registry.mjs +45 -1
- package/packages/agentd/core/pool/support/compaction.mjs +2 -43
- package/packages/agentd/core/pool/support/index.mjs +1 -1
- package/packages/agentd/core/prompts/index.mjs +12 -12
- package/packages/agentd/core/prompts/rules.mjs +22 -22
- package/packages/agentd/core/prompts/templates.mjs +172 -172
- package/packages/agentd/core/providers/index.mjs +14 -14
- package/packages/agentd/core/providers/store.mjs +206 -206
- package/packages/agentd/core/records/cleanup.mjs +51 -51
- package/packages/agentd/core/records/index.mjs +8 -8
- package/packages/agentd/core/records/metrics.mjs +73 -73
- package/packages/agentd/core/records/recovery.mjs +39 -39
- package/packages/agentd/core/records/transcripts.mjs +122 -122
- package/packages/agentd/core/threads/index.mjs +18 -17
- package/packages/agentd/core/threads/manager.mjs +498 -476
- package/packages/agentd/core/threads/paths.mjs +78 -78
- package/packages/agentd/core/threads/store.mjs +146 -125
- package/packages/agentd/daemon/agentd.mjs +58 -58
- package/packages/agentd/daemon/events.mjs +126 -126
- package/packages/agentd/daemon/guard.mjs +127 -127
- package/packages/agentd/daemon/lifecycle.mjs +61 -61
- package/packages/agentd/daemon/ops/index.mjs +15 -15
- package/packages/agentd/daemon/ops/providers.mjs +46 -46
- package/packages/agentd/daemon/ops/run.mjs +413 -400
- package/packages/agentd/daemon/ops/system.mjs +76 -69
- package/packages/agentd/daemon/ops/threads.mjs +156 -153
- package/packages/agentd/daemon/server.mjs +130 -130
- package/packages/agentd/package.json +1 -1
- package/packages/agentd/pi-adapter/acp-adapter.mjs +501 -501
- package/packages/agentd/scripts/restart-daemon.mjs +74 -74
- package/packages/agentd/scripts/run-isolated.mjs +42 -42
- package/packages/agentd/scripts/run-unit.mjs +28 -28
- package/packages/backend/src/daemon.mjs +69 -69
- package/packages/backend/src/server.mjs +270 -270
- package/packages/drivers/{agy.mjs → agy/index.mjs} +2 -1
- package/packages/drivers/{claude.mjs → claude/index.mjs} +23 -1
- package/packages/drivers/{codex.mjs → codex/index.mjs} +2 -1
- package/packages/drivers/{dsh.mjs → dsh/index.mjs} +3 -2
- package/packages/drivers/grok/index.mjs +96 -0
- package/packages/drivers/index.mjs +25 -12
- package/packages/drivers/{kimi.mjs → kimi/index.mjs} +2 -1
- package/packages/drivers/{mcode.mjs → mcode/index.mjs} +2 -1
- package/packages/drivers/{opencode.mjs → opencode/index.mjs} +2 -1
- package/packages/drivers/{pi.mjs → pi/index.mjs} +3 -2
- package/packages/drivers/probes/acp.mjs +109 -109
- package/packages/drivers/probes/index.mjs +131 -129
- package/packages/drivers/probes/provider.mjs +222 -222
- package/packages/drivers/qoder/index.mjs +32 -0
- package/packages/drivers/{bin.mjs → shared/bin.mjs} +2 -0
- package/packages/drivers/shared/index.mjs +2 -0
- package/packages/preflight/bin/preflight.mjs +116 -116
- package/packages/preflight/src/index.mjs +5 -5
- package/packages/preflight/src/report.mjs +18 -1
- package/packages/web/dist/apple-touch-icon.png +0 -0
- package/packages/web/dist/assets/index-CT8dIwav.css +2 -0
- package/packages/web/dist/assets/index-CypTUMFB.js +79 -0
- package/packages/web/dist/favicon-32.png +0 -0
- package/packages/web/dist/favicon.svg +19 -15
- package/packages/web/dist/index.html +5 -5
- package/packages/web/dist/assets/index-B2kdkHNw.js +0 -79
- package/packages/web/dist/assets/index-DQTyjC4x.css +0 -2
- /package/packages/drivers/{dsh-sessions.mjs → dsh/sessions.mjs} +0 -0
- /package/packages/drivers/{pi-catalog.mjs → pi/catalog.mjs} +0 -0
- /package/packages/drivers/{lib.mjs → shared/lib.mjs} +0 -0
|
@@ -1,222 +1,222 @@
|
|
|
1
|
-
import { spawnSync } from 'node:child_process';
|
|
2
|
-
import { createHash } from 'node:crypto';
|
|
3
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
import { openCodeHeaders, slash } from '../
|
|
6
|
-
import { AcpProcess } from './acp.mjs';
|
|
7
|
-
|
|
8
|
-
const clip = (s, n = 240) => String(s ?? '').replace(/\s+/g, ' ').trim().slice(0, n);
|
|
9
|
-
|
|
10
|
-
export const scrub = (text, secrets) =>
|
|
11
|
-
[...secrets].filter((s) => s && String(s).length > 6).reduce((t, s) => t.split(String(s)).join('***'), String(text ?? ''));
|
|
12
|
-
|
|
13
|
-
export async function checkEndpoint({ protocol, endpoint, apiKey, model, timeoutMs = 30_000, fetchImpl = fetch }) {
|
|
14
|
-
const t0 = Date.now();
|
|
15
|
-
const base = String(endpoint).replace(/\/+$/, '');
|
|
16
|
-
const anthropic = protocol === 'anthropic';
|
|
17
|
-
const url = anthropic ? `${base}/v1/messages` : `${base}/chat/completions`;
|
|
18
|
-
const extra = openCodeHeaders(endpoint, `probe:${protocol}:${model}`);
|
|
19
|
-
const headers = anthropic
|
|
20
|
-
? { 'content-type': 'application/json', 'user-agent': 'harness-bus/1.0', 'x-api-key': apiKey, authorization: `Bearer ${apiKey}`, 'anthropic-version': '2023-06-01', ...extra }
|
|
21
|
-
: { 'content-type': 'application/json', 'user-agent': 'harness-bus/1.0', authorization: `Bearer ${apiKey}`, ...extra };
|
|
22
|
-
try {
|
|
23
|
-
const res = await fetchImpl(url, {
|
|
24
|
-
method: 'POST',
|
|
25
|
-
headers,
|
|
26
|
-
body: JSON.stringify({ model, max_tokens: 16, messages: [{ role: 'user', content: 'Reply with the word OK.' }] }),
|
|
27
|
-
signal: AbortSignal.timeout(timeoutMs),
|
|
28
|
-
});
|
|
29
|
-
const text = await res.text();
|
|
30
|
-
let message = text;
|
|
31
|
-
try {
|
|
32
|
-
const j = JSON.parse(text);
|
|
33
|
-
message = j?.error?.message ?? j?.message ?? text;
|
|
34
|
-
} catch {
|
|
35
|
-
}
|
|
36
|
-
return { ok: res.ok, status: res.status, detail: res.ok ? 'answered' : scrub(clip(message), [apiKey]), ms: Date.now() - t0 };
|
|
37
|
-
} catch (e) {
|
|
38
|
-
const why = e.name === 'TimeoutError' ? `no answer within ${Math.round(timeoutMs / 1000)}s` : e.message;
|
|
39
|
-
return { ok: false, status: null, detail: scrub(`${why}${e.cause?.code ? ` (${e.cause.code})` : ''}`, [apiKey]), ms: Date.now() - t0 };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const contextOf = (m) => {
|
|
44
|
-
for (const k of ['context_length', 'context_window', 'max_model_len', 'max_input_tokens', 'contextWindow', 'input_token_limit', 'context_size']) {
|
|
45
|
-
const v = Number(m?.[k]);
|
|
46
|
-
if (Number.isFinite(v) && v >= 1000) return Math.round(v);
|
|
47
|
-
}
|
|
48
|
-
return null;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export async function discoverModels({ endpoints, apiKey, timeoutMs = 20_000, fetchImpl = fetch }) {
|
|
52
|
-
const found = new Set();
|
|
53
|
-
const contexts = {};
|
|
54
|
-
const errors = {};
|
|
55
|
-
await Promise.all(
|
|
56
|
-
Object.entries(endpoints ?? {})
|
|
57
|
-
.filter(([, url]) => url)
|
|
58
|
-
.map(async ([protocol, endpoint]) => {
|
|
59
|
-
const base = String(endpoint).replace(/\/+$/, '');
|
|
60
|
-
const anthropic = protocol === 'anthropic';
|
|
61
|
-
const url = anthropic ? `${base}/v1/models?limit=1000` : `${base}/models`;
|
|
62
|
-
const headers = anthropic ? { 'x-api-key': apiKey, authorization: `Bearer ${apiKey}`, 'anthropic-version': '2023-06-01' } : { authorization: `Bearer ${apiKey}` };
|
|
63
|
-
try {
|
|
64
|
-
const res = await fetchImpl(url, { headers, signal: AbortSignal.timeout(timeoutMs) });
|
|
65
|
-
const text = await res.text();
|
|
66
|
-
let json = null;
|
|
67
|
-
try {
|
|
68
|
-
json = JSON.parse(text);
|
|
69
|
-
} catch {
|
|
70
|
-
}
|
|
71
|
-
if (!res.ok) {
|
|
72
|
-
errors[protocol] = scrub(clip(json?.error?.message ?? json?.message ?? `HTTP ${res.status}`), [apiKey]);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
const rows = Array.isArray(json?.data) ? json.data : Array.isArray(json?.models) ? json.models : null;
|
|
76
|
-
if (!rows) {
|
|
77
|
-
errors[protocol] = 'the answer is not a list of models';
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
for (const m of rows) {
|
|
81
|
-
const id = typeof m === 'string' ? m : (m?.id ?? m?.name);
|
|
82
|
-
if (id) found.add(String(id));
|
|
83
|
-
const ctx = typeof m === 'object' ? contextOf(m) : null;
|
|
84
|
-
if (id && ctx) contexts[String(id)] = Math.max(contexts[String(id)] ?? 0, ctx);
|
|
85
|
-
}
|
|
86
|
-
} catch (e) {
|
|
87
|
-
errors[protocol] = scrub(e.name === 'TimeoutError' ? `no answer within ${Math.round(timeoutMs / 1000)}s` : e.message, [apiKey]);
|
|
88
|
-
}
|
|
89
|
-
}),
|
|
90
|
-
);
|
|
91
|
-
return { models: [...found].sort((a, b) => a.localeCompare(b)), contexts, errors };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function providerProtocolOf(driver, endpoints = {}) {
|
|
95
|
-
const cap = driver?.provider;
|
|
96
|
-
if (!cap) return null;
|
|
97
|
-
const list = Array.isArray(cap.protocols) && cap.protocols.length ? cap.protocols : [cap.protocol];
|
|
98
|
-
return list.find((p) => Boolean(endpoints?.[p])) ?? cap.protocol;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function applyProvider({ driver, provider, model, dir, env = {} }) {
|
|
102
|
-
const cap = driver.provider;
|
|
103
|
-
if (!cap) return { ok: false, skipped: 'this CLI has no way to take another provider' };
|
|
104
|
-
const protocol = providerProtocolOf(driver, provider.endpoints);
|
|
105
|
-
const endpoint = protocol ? provider.endpoints?.[protocol] : null;
|
|
106
|
-
if (!endpoint) {
|
|
107
|
-
const needed = Array.isArray(cap.protocols) && cap.protocols.length > 1 ? cap.protocols.join(' or ') : cap.protocol;
|
|
108
|
-
return { ok: false, skipped: `needs an ${needed} endpoint, and this provider has none` };
|
|
109
|
-
}
|
|
110
|
-
const applied = cap.apply({ provider, endpoint, model, dir: slash(dir), env, protocol });
|
|
111
|
-
mkdirSync(dir, { recursive: true });
|
|
112
|
-
for (const f of applied.files ?? []) writeFileSync(join(dir, f.name), f.content, 'utf8');
|
|
113
|
-
return { ok: true, env: applied.env ?? {}, args: applied.args ?? [], model: applied.model ?? null, applied };
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function syncGlobalProvider({ driver, install, piAdapterPath, applied, dir, env = {} }) {
|
|
117
|
-
const g = applied?.global;
|
|
118
|
-
if (!g) return;
|
|
119
|
-
const launch = driver.launch(install, { piAdapterPath });
|
|
120
|
-
if (launch.missing?.length || !launch.tool) {
|
|
121
|
-
throw new Error(`${driver.type} cannot register provider: ${launch.missing?.join(', ') || 'missing CLI tool'}`);
|
|
122
|
-
}
|
|
123
|
-
const stampFile = dir ? join(dir, 'global-provider.sha') : null;
|
|
124
|
-
const stamp = createHash('sha256').update(JSON.stringify({ add: g.add, contexts: g.contexts ?? null })).digest('hex');
|
|
125
|
-
if (stampFile && existsSync(stampFile) && readFileSync(stampFile, 'utf8').trim() === stamp && g.isRegistered?.()) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
const runTool = (args) =>
|
|
129
|
-
spawnSync(launch.tool.cmd, [...launch.tool.args, ...args], { env, encoding: 'utf8', windowsHide: true, timeout: 60_000 });
|
|
130
|
-
if (g.remove) runTool(g.remove);
|
|
131
|
-
const r = runTool(g.add);
|
|
132
|
-
if (r.status !== 0) {
|
|
133
|
-
throw new Error(`registering provider in ${driver.type} failed: ${clip(r.stderr || r.stdout)}`);
|
|
134
|
-
}
|
|
135
|
-
g.afterAdd?.();
|
|
136
|
-
if (stampFile) {
|
|
137
|
-
mkdirSync(dir, { recursive: true });
|
|
138
|
-
writeFileSync(stampFile, `${stamp}\n`, 'utf8');
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function removeGlobalProvider({ driver, install, piAdapterPath, providerId }) {
|
|
143
|
-
const cap = driver?.provider;
|
|
144
|
-
if (!cap || cap.scope !== 'global') return;
|
|
145
|
-
const applied = cap.apply({ provider: { id: providerId, apiKey: 'placeholder', models: ['m'] }, endpoint: 'https://example.invalid', model: 'm' });
|
|
146
|
-
if (!applied?.global?.remove) return;
|
|
147
|
-
if (typeof applied.global.isRegistered === 'function' && !applied.global.isRegistered()) return;
|
|
148
|
-
const launch = driver.launch(install, { piAdapterPath });
|
|
149
|
-
if (launch.missing?.length || !launch.tool) return;
|
|
150
|
-
spawnSync(launch.tool.cmd, [...launch.tool.args, ...applied.global.remove], {
|
|
151
|
-
env: { ...process.env, ...applied.env },
|
|
152
|
-
encoding: 'utf8',
|
|
153
|
-
windowsHide: true,
|
|
154
|
-
timeout: 30_000,
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export async function testProvider({ driver, install, piAdapterPath, provider, model, workRoot, timeoutMs = 90_000 }) {
|
|
159
|
-
const type = driver.type;
|
|
160
|
-
const t0 = Date.now();
|
|
161
|
-
const out = (ok, detail, extra = {}) => ({ type, ok, detail: scrub(detail, [provider.apiKey]), ms: Date.now() - t0, ...extra });
|
|
162
|
-
const skip = (why) => out(false, why, { skipped: why });
|
|
163
|
-
|
|
164
|
-
const launch = driver.launch(install, { piAdapterPath });
|
|
165
|
-
if (launch.missing?.length) return skip(`not installed: ${launch.missing.join(', ')}`);
|
|
166
|
-
|
|
167
|
-
const cwd = join(workRoot, 'work');
|
|
168
|
-
mkdirSync(cwd, { recursive: true });
|
|
169
|
-
const base = {
|
|
170
|
-
...process.env,
|
|
171
|
-
...launch.env,
|
|
172
|
-
...(driver.instanceEnv?.({ id: `provider-test-${type}`, sessionsDir: join(workRoot, 'conv'), conversationsDir: join(workRoot, 'conv') }) ?? {}),
|
|
173
|
-
...(driver.envFn?.() ?? {}),
|
|
174
|
-
...(driver.spawnEnv?.({ cwd, mcpServers: [] }) ?? {}),
|
|
175
|
-
};
|
|
176
|
-
// Use an isolated temporary id for global-scope CLIs (mcode) so cleanup never deletes a live provider entry.
|
|
177
|
-
const testTarget =
|
|
178
|
-
driver.provider?.scope === 'global'
|
|
179
|
-
? { ...provider, id: `probe-${provider.id}-${Date.now().toString(36)}` }
|
|
180
|
-
: provider;
|
|
181
|
-
const ap = applyProvider({ driver, provider: testTarget, model, dir: join(workRoot, 'config'), env: base });
|
|
182
|
-
if (!ap.ok) return skip(ap.skipped);
|
|
183
|
-
|
|
184
|
-
const env = { ...base, ...ap.env };
|
|
185
|
-
const g = ap.applied.global;
|
|
186
|
-
const tool = (args) => spawnSync(launch.tool.cmd, [...launch.tool.args, ...args], { env, encoding: 'utf8', windowsHide: true, timeout: 60_000 });
|
|
187
|
-
let proc = null;
|
|
188
|
-
let added = false;
|
|
189
|
-
try {
|
|
190
|
-
if (g) {
|
|
191
|
-
if (!launch.tool) return skip('this CLI cannot be driven from the command line here');
|
|
192
|
-
if (g.remove) tool(g.remove);
|
|
193
|
-
const r = tool(g.add);
|
|
194
|
-
added = r.status === 0;
|
|
195
|
-
if (!added) return out(false, `adding the provider to the CLI failed: ${clip(r.stderr || r.stdout)}`);
|
|
196
|
-
g.afterAdd?.();
|
|
197
|
-
}
|
|
198
|
-
proc = AcpProcess.start({ cmd: launch.cmd, args: [...launch.args, ...ap.args], env, cwd });
|
|
199
|
-
const init = await proc.request('initialize', { protocolVersion: 1, clientCapabilities: { fs: { readTextFile: false, writeTextFile: false } } }, 90_000);
|
|
200
|
-
if (init.error) return out(false, `did not start: ${init.error.message}${proc.stderr.length ? ` | ${proc.stderr.slice(-2).join(' | ')}` : ''}`);
|
|
201
|
-
const sess = await proc.request('session/new', { cwd, mcpServers: [] }, 90_000);
|
|
202
|
-
const sid = sess.result?.sessionId;
|
|
203
|
-
if (!sid) return out(false, `no session: ${sess.error?.message ?? 'no session id'}`);
|
|
204
|
-
if (ap.model) {
|
|
205
|
-
const set = await proc.request('session/set_config_option', { sessionId: sid, configId: 'model', value: ap.model }, 30_000);
|
|
206
|
-
if (set.error) return out(false, `the CLI does not offer the model: ${set.error.message}`);
|
|
207
|
-
}
|
|
208
|
-
const r = await proc.prompt(sid, 'Reply with exactly the word PONG and nothing else.', timeoutMs);
|
|
209
|
-
if (r.error) return out(false, r.error);
|
|
210
|
-
if (!/PONG/i.test(r.text)) {
|
|
211
|
-
const cliErr = !r.text ? driver.turnError?.({ sessionId: sid, env }) : null;
|
|
212
|
-
if (cliErr) return out(false, cliErr);
|
|
213
|
-
return out(false, r.text ? `unexpected answer: ${clip(r.text, 120)}` : 'the CLI ended the turn with no text (the provider refused it, or answered nothing)');
|
|
214
|
-
}
|
|
215
|
-
return out(true, 'answered through the provider');
|
|
216
|
-
} catch (e) {
|
|
217
|
-
return out(false, e.message);
|
|
218
|
-
} finally {
|
|
219
|
-
await proc?.stop().catch(() => {});
|
|
220
|
-
if (g && added) tool(g.remove);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { openCodeHeaders, slash } from '../shared/index.mjs';
|
|
6
|
+
import { AcpProcess } from './acp.mjs';
|
|
7
|
+
|
|
8
|
+
const clip = (s, n = 240) => String(s ?? '').replace(/\s+/g, ' ').trim().slice(0, n);
|
|
9
|
+
|
|
10
|
+
export const scrub = (text, secrets) =>
|
|
11
|
+
[...secrets].filter((s) => s && String(s).length > 6).reduce((t, s) => t.split(String(s)).join('***'), String(text ?? ''));
|
|
12
|
+
|
|
13
|
+
export async function checkEndpoint({ protocol, endpoint, apiKey, model, timeoutMs = 30_000, fetchImpl = fetch }) {
|
|
14
|
+
const t0 = Date.now();
|
|
15
|
+
const base = String(endpoint).replace(/\/+$/, '');
|
|
16
|
+
const anthropic = protocol === 'anthropic';
|
|
17
|
+
const url = anthropic ? `${base}/v1/messages` : `${base}/chat/completions`;
|
|
18
|
+
const extra = openCodeHeaders(endpoint, `probe:${protocol}:${model}`);
|
|
19
|
+
const headers = anthropic
|
|
20
|
+
? { 'content-type': 'application/json', 'user-agent': 'harness-bus/1.0', 'x-api-key': apiKey, authorization: `Bearer ${apiKey}`, 'anthropic-version': '2023-06-01', ...extra }
|
|
21
|
+
: { 'content-type': 'application/json', 'user-agent': 'harness-bus/1.0', authorization: `Bearer ${apiKey}`, ...extra };
|
|
22
|
+
try {
|
|
23
|
+
const res = await fetchImpl(url, {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
headers,
|
|
26
|
+
body: JSON.stringify({ model, max_tokens: 16, messages: [{ role: 'user', content: 'Reply with the word OK.' }] }),
|
|
27
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
28
|
+
});
|
|
29
|
+
const text = await res.text();
|
|
30
|
+
let message = text;
|
|
31
|
+
try {
|
|
32
|
+
const j = JSON.parse(text);
|
|
33
|
+
message = j?.error?.message ?? j?.message ?? text;
|
|
34
|
+
} catch {
|
|
35
|
+
}
|
|
36
|
+
return { ok: res.ok, status: res.status, detail: res.ok ? 'answered' : scrub(clip(message), [apiKey]), ms: Date.now() - t0 };
|
|
37
|
+
} catch (e) {
|
|
38
|
+
const why = e.name === 'TimeoutError' ? `no answer within ${Math.round(timeoutMs / 1000)}s` : e.message;
|
|
39
|
+
return { ok: false, status: null, detail: scrub(`${why}${e.cause?.code ? ` (${e.cause.code})` : ''}`, [apiKey]), ms: Date.now() - t0 };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const contextOf = (m) => {
|
|
44
|
+
for (const k of ['context_length', 'context_window', 'max_model_len', 'max_input_tokens', 'contextWindow', 'input_token_limit', 'context_size']) {
|
|
45
|
+
const v = Number(m?.[k]);
|
|
46
|
+
if (Number.isFinite(v) && v >= 1000) return Math.round(v);
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export async function discoverModels({ endpoints, apiKey, timeoutMs = 20_000, fetchImpl = fetch }) {
|
|
52
|
+
const found = new Set();
|
|
53
|
+
const contexts = {};
|
|
54
|
+
const errors = {};
|
|
55
|
+
await Promise.all(
|
|
56
|
+
Object.entries(endpoints ?? {})
|
|
57
|
+
.filter(([, url]) => url)
|
|
58
|
+
.map(async ([protocol, endpoint]) => {
|
|
59
|
+
const base = String(endpoint).replace(/\/+$/, '');
|
|
60
|
+
const anthropic = protocol === 'anthropic';
|
|
61
|
+
const url = anthropic ? `${base}/v1/models?limit=1000` : `${base}/models`;
|
|
62
|
+
const headers = anthropic ? { 'x-api-key': apiKey, authorization: `Bearer ${apiKey}`, 'anthropic-version': '2023-06-01' } : { authorization: `Bearer ${apiKey}` };
|
|
63
|
+
try {
|
|
64
|
+
const res = await fetchImpl(url, { headers, signal: AbortSignal.timeout(timeoutMs) });
|
|
65
|
+
const text = await res.text();
|
|
66
|
+
let json = null;
|
|
67
|
+
try {
|
|
68
|
+
json = JSON.parse(text);
|
|
69
|
+
} catch {
|
|
70
|
+
}
|
|
71
|
+
if (!res.ok) {
|
|
72
|
+
errors[protocol] = scrub(clip(json?.error?.message ?? json?.message ?? `HTTP ${res.status}`), [apiKey]);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const rows = Array.isArray(json?.data) ? json.data : Array.isArray(json?.models) ? json.models : null;
|
|
76
|
+
if (!rows) {
|
|
77
|
+
errors[protocol] = 'the answer is not a list of models';
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
for (const m of rows) {
|
|
81
|
+
const id = typeof m === 'string' ? m : (m?.id ?? m?.name);
|
|
82
|
+
if (id) found.add(String(id));
|
|
83
|
+
const ctx = typeof m === 'object' ? contextOf(m) : null;
|
|
84
|
+
if (id && ctx) contexts[String(id)] = Math.max(contexts[String(id)] ?? 0, ctx);
|
|
85
|
+
}
|
|
86
|
+
} catch (e) {
|
|
87
|
+
errors[protocol] = scrub(e.name === 'TimeoutError' ? `no answer within ${Math.round(timeoutMs / 1000)}s` : e.message, [apiKey]);
|
|
88
|
+
}
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
return { models: [...found].sort((a, b) => a.localeCompare(b)), contexts, errors };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function providerProtocolOf(driver, endpoints = {}) {
|
|
95
|
+
const cap = driver?.provider;
|
|
96
|
+
if (!cap) return null;
|
|
97
|
+
const list = Array.isArray(cap.protocols) && cap.protocols.length ? cap.protocols : [cap.protocol];
|
|
98
|
+
return list.find((p) => Boolean(endpoints?.[p])) ?? cap.protocol;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function applyProvider({ driver, provider, model, dir, env = {} }) {
|
|
102
|
+
const cap = driver.provider;
|
|
103
|
+
if (!cap) return { ok: false, skipped: 'this CLI has no way to take another provider' };
|
|
104
|
+
const protocol = providerProtocolOf(driver, provider.endpoints);
|
|
105
|
+
const endpoint = protocol ? provider.endpoints?.[protocol] : null;
|
|
106
|
+
if (!endpoint) {
|
|
107
|
+
const needed = Array.isArray(cap.protocols) && cap.protocols.length > 1 ? cap.protocols.join(' or ') : cap.protocol;
|
|
108
|
+
return { ok: false, skipped: `needs an ${needed} endpoint, and this provider has none` };
|
|
109
|
+
}
|
|
110
|
+
const applied = cap.apply({ provider, endpoint, model, dir: slash(dir), env, protocol });
|
|
111
|
+
mkdirSync(dir, { recursive: true });
|
|
112
|
+
for (const f of applied.files ?? []) writeFileSync(join(dir, f.name), f.content, 'utf8');
|
|
113
|
+
return { ok: true, env: applied.env ?? {}, args: applied.args ?? [], model: applied.model ?? null, applied };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function syncGlobalProvider({ driver, install, piAdapterPath, applied, dir, env = {} }) {
|
|
117
|
+
const g = applied?.global;
|
|
118
|
+
if (!g) return;
|
|
119
|
+
const launch = driver.launch(install, { piAdapterPath });
|
|
120
|
+
if (launch.missing?.length || !launch.tool) {
|
|
121
|
+
throw new Error(`${driver.type} cannot register provider: ${launch.missing?.join(', ') || 'missing CLI tool'}`);
|
|
122
|
+
}
|
|
123
|
+
const stampFile = dir ? join(dir, 'global-provider.sha') : null;
|
|
124
|
+
const stamp = createHash('sha256').update(JSON.stringify({ add: g.add, contexts: g.contexts ?? null })).digest('hex');
|
|
125
|
+
if (stampFile && existsSync(stampFile) && readFileSync(stampFile, 'utf8').trim() === stamp && g.isRegistered?.()) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const runTool = (args) =>
|
|
129
|
+
spawnSync(launch.tool.cmd, [...launch.tool.args, ...args], { env, encoding: 'utf8', windowsHide: true, timeout: 60_000 });
|
|
130
|
+
if (g.remove) runTool(g.remove);
|
|
131
|
+
const r = runTool(g.add);
|
|
132
|
+
if (r.status !== 0) {
|
|
133
|
+
throw new Error(`registering provider in ${driver.type} failed: ${clip(r.stderr || r.stdout)}`);
|
|
134
|
+
}
|
|
135
|
+
g.afterAdd?.();
|
|
136
|
+
if (stampFile) {
|
|
137
|
+
mkdirSync(dir, { recursive: true });
|
|
138
|
+
writeFileSync(stampFile, `${stamp}\n`, 'utf8');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function removeGlobalProvider({ driver, install, piAdapterPath, providerId }) {
|
|
143
|
+
const cap = driver?.provider;
|
|
144
|
+
if (!cap || cap.scope !== 'global') return;
|
|
145
|
+
const applied = cap.apply({ provider: { id: providerId, apiKey: 'placeholder', models: ['m'] }, endpoint: 'https://example.invalid', model: 'm' });
|
|
146
|
+
if (!applied?.global?.remove) return;
|
|
147
|
+
if (typeof applied.global.isRegistered === 'function' && !applied.global.isRegistered()) return;
|
|
148
|
+
const launch = driver.launch(install, { piAdapterPath });
|
|
149
|
+
if (launch.missing?.length || !launch.tool) return;
|
|
150
|
+
spawnSync(launch.tool.cmd, [...launch.tool.args, ...applied.global.remove], {
|
|
151
|
+
env: { ...process.env, ...applied.env },
|
|
152
|
+
encoding: 'utf8',
|
|
153
|
+
windowsHide: true,
|
|
154
|
+
timeout: 30_000,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function testProvider({ driver, install, piAdapterPath, provider, model, workRoot, timeoutMs = 90_000 }) {
|
|
159
|
+
const type = driver.type;
|
|
160
|
+
const t0 = Date.now();
|
|
161
|
+
const out = (ok, detail, extra = {}) => ({ type, ok, detail: scrub(detail, [provider.apiKey]), ms: Date.now() - t0, ...extra });
|
|
162
|
+
const skip = (why) => out(false, why, { skipped: why });
|
|
163
|
+
|
|
164
|
+
const launch = driver.launch(install, { piAdapterPath });
|
|
165
|
+
if (launch.missing?.length) return skip(`not installed: ${launch.missing.join(', ')}`);
|
|
166
|
+
|
|
167
|
+
const cwd = join(workRoot, 'work');
|
|
168
|
+
mkdirSync(cwd, { recursive: true });
|
|
169
|
+
const base = {
|
|
170
|
+
...process.env,
|
|
171
|
+
...launch.env,
|
|
172
|
+
...(driver.instanceEnv?.({ id: `provider-test-${type}`, sessionsDir: join(workRoot, 'conv'), conversationsDir: join(workRoot, 'conv') }) ?? {}),
|
|
173
|
+
...(driver.envFn?.() ?? {}),
|
|
174
|
+
...(driver.spawnEnv?.({ cwd, mcpServers: [] }) ?? {}),
|
|
175
|
+
};
|
|
176
|
+
// Use an isolated temporary id for global-scope CLIs (mcode) so cleanup never deletes a live provider entry.
|
|
177
|
+
const testTarget =
|
|
178
|
+
driver.provider?.scope === 'global'
|
|
179
|
+
? { ...provider, id: `probe-${provider.id}-${Date.now().toString(36)}` }
|
|
180
|
+
: provider;
|
|
181
|
+
const ap = applyProvider({ driver, provider: testTarget, model, dir: join(workRoot, 'config'), env: base });
|
|
182
|
+
if (!ap.ok) return skip(ap.skipped);
|
|
183
|
+
|
|
184
|
+
const env = { ...base, ...ap.env };
|
|
185
|
+
const g = ap.applied.global;
|
|
186
|
+
const tool = (args) => spawnSync(launch.tool.cmd, [...launch.tool.args, ...args], { env, encoding: 'utf8', windowsHide: true, timeout: 60_000 });
|
|
187
|
+
let proc = null;
|
|
188
|
+
let added = false;
|
|
189
|
+
try {
|
|
190
|
+
if (g) {
|
|
191
|
+
if (!launch.tool) return skip('this CLI cannot be driven from the command line here');
|
|
192
|
+
if (g.remove) tool(g.remove);
|
|
193
|
+
const r = tool(g.add);
|
|
194
|
+
added = r.status === 0;
|
|
195
|
+
if (!added) return out(false, `adding the provider to the CLI failed: ${clip(r.stderr || r.stdout)}`);
|
|
196
|
+
g.afterAdd?.();
|
|
197
|
+
}
|
|
198
|
+
proc = AcpProcess.start({ cmd: launch.cmd, args: [...launch.args, ...ap.args], env, cwd });
|
|
199
|
+
const init = await proc.request('initialize', { protocolVersion: 1, clientCapabilities: { fs: { readTextFile: false, writeTextFile: false } } }, 90_000);
|
|
200
|
+
if (init.error) return out(false, `did not start: ${init.error.message}${proc.stderr.length ? ` | ${proc.stderr.slice(-2).join(' | ')}` : ''}`);
|
|
201
|
+
const sess = await proc.request('session/new', { cwd, mcpServers: [] }, 90_000);
|
|
202
|
+
const sid = sess.result?.sessionId;
|
|
203
|
+
if (!sid) return out(false, `no session: ${sess.error?.message ?? 'no session id'}`);
|
|
204
|
+
if (ap.model) {
|
|
205
|
+
const set = await proc.request('session/set_config_option', { sessionId: sid, configId: 'model', value: ap.model }, 30_000);
|
|
206
|
+
if (set.error) return out(false, `the CLI does not offer the model: ${set.error.message}`);
|
|
207
|
+
}
|
|
208
|
+
const r = await proc.prompt(sid, 'Reply with exactly the word PONG and nothing else.', timeoutMs);
|
|
209
|
+
if (r.error) return out(false, r.error);
|
|
210
|
+
if (!/PONG/i.test(r.text)) {
|
|
211
|
+
const cliErr = !r.text ? driver.turnError?.({ sessionId: sid, env }) : null;
|
|
212
|
+
if (cliErr) return out(false, cliErr);
|
|
213
|
+
return out(false, r.text ? `unexpected answer: ${clip(r.text, 120)}` : 'the CLI ended the turn with no text (the provider refused it, or answered nothing)');
|
|
214
|
+
}
|
|
215
|
+
return out(true, 'answered through the provider');
|
|
216
|
+
} catch (e) {
|
|
217
|
+
return out(false, e.message);
|
|
218
|
+
} finally {
|
|
219
|
+
await proc?.stop().catch(() => {});
|
|
220
|
+
if (g && added) tool(g.remove);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { NO_RULES, RULES_IN_PROMPT, packageDir } from '../shared/index.mjs';
|
|
2
|
+
|
|
3
|
+
const PKG = '@qoder-ai/qodercli';
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
type: 'qoder',
|
|
7
|
+
label: 'Qoder',
|
|
8
|
+
description: 'Alibaba Qoder CLI; native ACP, uses its own qodercli login and model catalog.',
|
|
9
|
+
authHint: '运行 "qodercli login" 完成登录,或设置环境变量 QODER_PERSONAL_ACCESS_TOKEN',
|
|
10
|
+
|
|
11
|
+
packages: [{ name: PKG, version: '1.1.105' }],
|
|
12
|
+
verified: {
|
|
13
|
+
on: '2026-12-04',
|
|
14
|
+
note: 'ACP v1 over stdio, --yolo, --append-system-prompt-file, MCP servers, /compact and session/resume checked on 1.1.105',
|
|
15
|
+
},
|
|
16
|
+
capabilities: { compact: '/compact', rules: 'arg', mcp: 'acp', mcpStart: 'eager' },
|
|
17
|
+
|
|
18
|
+
launch(install) {
|
|
19
|
+
const missing = [];
|
|
20
|
+
const dir = packageDir(install, PKG, missing);
|
|
21
|
+
return {
|
|
22
|
+
cmd: install.node,
|
|
23
|
+
args: [`${dir}/bundle/qodercli.js`, '--acp', '--yolo'],
|
|
24
|
+
env: {},
|
|
25
|
+
missing,
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
rules({ file }) {
|
|
30
|
+
return file ? { ...NO_RULES, args: ['--append-system-prompt-file', file] } : RULES_IN_PROMPT;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
@@ -26,11 +26,13 @@ export function defaultBinDirs(env = process.env, platform = process.platform) {
|
|
|
26
26
|
if (platform === 'win32') {
|
|
27
27
|
const profile = env.USERPROFILE || home;
|
|
28
28
|
add('local', env.LOCALAPPDATA ? join(env.LOCALAPPDATA, 'agy', 'bin') : join(profile, 'AppData', 'Local', 'agy', 'bin'));
|
|
29
|
+
add('local', join(profile, '.grok', 'bin'));
|
|
29
30
|
add('local', join(profile, '.local', 'bin'));
|
|
30
31
|
add('local', env.APPDATA ? join(env.APPDATA, 'npm') : join(profile, 'AppData', 'Roaming', 'npm'));
|
|
31
32
|
add('local', join(profile, '.volta', 'bin'));
|
|
32
33
|
} else {
|
|
33
34
|
const unixHome = env.HOME || home;
|
|
35
|
+
add('local', join(unixHome, '.grok', 'bin'));
|
|
34
36
|
add('local', join(unixHome, '.local', 'bin'));
|
|
35
37
|
add('local', '/opt/homebrew/bin');
|
|
36
38
|
add('local', '/usr/local/bin');
|