@openagents-org/agent-launcher 0.2.219 → 0.2.221
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/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/node_modules/ws/lib/receiver.js +706 -0
- package/node_modules/ws/lib/sender.js +602 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +554 -0
- package/node_modules/ws/lib/websocket.js +1393 -0
- package/node_modules/ws/package.json +70 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +5 -2
- package/src/adapters/claude.js +13 -9
- package/src/adapters/codearts.js +1 -0
- package/src/adapters/codex.js +49 -9
- package/src/adapters/kimi-stream.js +78 -5
- package/src/adapters/kimi.js +114 -24
- package/src/adapters/mimo-code.js +1 -0
- package/src/adapters/openclaw.js +24 -19
- package/src/adapters/opencode.js +55 -2
- package/src/adapters/pi-launcher-provider.mjs +10 -24
- package/src/adapters/pi-provider-config.js +36 -0
- package/src/adapters/pi.js +6 -1
- package/src/adapters/utils.js +11 -0
- package/src/cli-models.js +39 -2
- package/src/daemon.js +4 -2
- package/src/installer.js +144 -23
- package/src/native-build.js +132 -0
- package/src/probe.js +64 -4
- package/src/utils.js +123 -12
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The per-platform npm package that holds an agent CLI's native build.
|
|
5
|
+
*
|
|
6
|
+
* @openai/codex, @anthropic-ai/claude-code and opencode-ai are thin npm
|
|
7
|
+
* wrappers: the executable itself is in one optional dependency per platform
|
|
8
|
+
* (`@openai/codex-win32-x64`, `@anthropic-ai/claude-code-darwin-arm64`, …).
|
|
9
|
+
* npm leaves an optional dependency out WITHOUT failing the install — when its
|
|
10
|
+
* download fails (it is the big one, tens of megabytes) or when optional
|
|
11
|
+
* dependencies are turned off (`omit=optional` in an npmrc). The wrapper is
|
|
12
|
+
* then all there is: codex throws "Missing optional dependency
|
|
13
|
+
* @openai/codex-win32-x64" on every run, and the claude-code / opencode-ai
|
|
14
|
+
* postinstall finds nothing to copy and leaves the package's placeholder,
|
|
15
|
+
* which read as "postinstall did not run".
|
|
16
|
+
*
|
|
17
|
+
* Only the packages listed here are checked. Many CLIs also list optional
|
|
18
|
+
* platform packages they can run without (node-pty, sharp, esbuild, bun), and
|
|
19
|
+
* a missing one of those must not fail an install.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const fs = require('fs');
|
|
23
|
+
const path = require('path');
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Package names each wrapper accepts for this platform, in the order its own
|
|
27
|
+
* loader tries them; the first is the one to install when none is present.
|
|
28
|
+
* Mirrors bin/codex.js, claude-code's install.cjs and opencode-ai's
|
|
29
|
+
* postinstall.mjs.
|
|
30
|
+
*/
|
|
31
|
+
const NATIVE_BUILDS = {
|
|
32
|
+
// codex.js maps every Linux target to a musl build published under the
|
|
33
|
+
// plain linux-<arch> name.
|
|
34
|
+
'@openai/codex': ({ os, cpu }) => [`@openai/codex-${os}-${cpu}`],
|
|
35
|
+
'@anthropic-ai/claude-code': ({ os, cpu, musl }) => [
|
|
36
|
+
`@anthropic-ai/claude-code-${os}-${cpu}${os === 'linux' && musl ? '-musl' : ''}`,
|
|
37
|
+
],
|
|
38
|
+
// opencode-ai takes any of its variants (baseline = no AVX2) and calls
|
|
39
|
+
// win32 "windows".
|
|
40
|
+
'opencode-ai': ({ os, cpu, musl }) => {
|
|
41
|
+
const base = `opencode-${os === 'win32' ? 'windows' : os}-${cpu}`;
|
|
42
|
+
if (os === 'linux') {
|
|
43
|
+
return musl
|
|
44
|
+
? [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
|
|
45
|
+
: [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`];
|
|
46
|
+
}
|
|
47
|
+
return [base, `${base}-baseline`];
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Wrappers whose postinstall copies the native build over their own bin
|
|
53
|
+
* placeholder. Once it has, the CLI runs without the per-platform package:
|
|
54
|
+
* opencode-ai's postinstall, finding that npm left it out, installs it into a
|
|
55
|
+
* temp directory, copies the binary from there and deletes the directory. A
|
|
56
|
+
* missing package next to a real binary is a working install. codex is not
|
|
57
|
+
* listed: bin/codex.js resolves the package on every run.
|
|
58
|
+
*/
|
|
59
|
+
const COPIED_INTO_BIN = new Set(['@anthropic-ai/claude-code', 'opencode-ai']);
|
|
60
|
+
|
|
61
|
+
/** musl libc, detected the way claude-code's install.cjs does it. */
|
|
62
|
+
function isMusl() {
|
|
63
|
+
if (process.platform !== 'linux') return false;
|
|
64
|
+
try {
|
|
65
|
+
const report = process.report && typeof process.report.getReport === 'function'
|
|
66
|
+
? process.report.getReport()
|
|
67
|
+
: null;
|
|
68
|
+
return report != null && report.header && report.header.glibcVersionRuntime === undefined;
|
|
69
|
+
} catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function readJson(file) {
|
|
75
|
+
try { return JSON.parse(fs.readFileSync(file, 'utf-8')); } catch { return null; }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Where `name` would be found by a require() made from inside `pkgDir`: its own
|
|
80
|
+
* node_modules first, then each parent's — nested or hoisted, as npm put it.
|
|
81
|
+
*/
|
|
82
|
+
function findInstalled(pkgDir, name) {
|
|
83
|
+
let dir = path.resolve(pkgDir);
|
|
84
|
+
for (;;) {
|
|
85
|
+
if (path.basename(dir) !== 'node_modules') {
|
|
86
|
+
const candidate = path.join(dir, 'node_modules', ...name.split('/'));
|
|
87
|
+
if (fs.existsSync(path.join(candidate, 'package.json'))) return candidate;
|
|
88
|
+
}
|
|
89
|
+
const parent = path.dirname(dir);
|
|
90
|
+
if (parent === dir) return null;
|
|
91
|
+
dir = parent;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The native build an installed wrapper package needs on this machine.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} pkgDir the wrapper's directory (…/node_modules/@openai/codex)
|
|
99
|
+
* @returns {{ pkgName: string, version: string, name: string, spec: string,
|
|
100
|
+
* accepted: string[], installed: string|null, target: string,
|
|
101
|
+
* copiedIntoBin: boolean } | null}
|
|
102
|
+
* null when the package is not one of NATIVE_BUILDS, or lists no build for
|
|
103
|
+
* this platform (nothing to check). `name`/`spec` are what to install —
|
|
104
|
+
* spec as the wrapper lists it, `npm:` aliases included. `copiedIntoBin`:
|
|
105
|
+
* see COPIED_INTO_BIN.
|
|
106
|
+
*/
|
|
107
|
+
function nativeBuildFor(pkgDir, { platform = process.platform, arch = process.arch, musl = isMusl() } = {}) {
|
|
108
|
+
const pkg = readJson(path.join(pkgDir, 'package.json'));
|
|
109
|
+
if (!pkg || !NATIVE_BUILDS[pkg.name]) return null;
|
|
110
|
+
const optional = (pkg.optionalDependencies && typeof pkg.optionalDependencies === 'object')
|
|
111
|
+
? pkg.optionalDependencies
|
|
112
|
+
: {};
|
|
113
|
+
const accepted = NATIVE_BUILDS[pkg.name]({ os: platform, cpu: arch, musl }).filter((n) => optional[n]);
|
|
114
|
+
if (!accepted.length) return null;
|
|
115
|
+
let installed = null;
|
|
116
|
+
for (const n of accepted) {
|
|
117
|
+
installed = findInstalled(pkgDir, n);
|
|
118
|
+
if (installed) break;
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
pkgName: pkg.name,
|
|
122
|
+
version: pkg.version || '',
|
|
123
|
+
name: accepted[0],
|
|
124
|
+
spec: String(optional[accepted[0]]),
|
|
125
|
+
accepted,
|
|
126
|
+
installed,
|
|
127
|
+
target: `${platform}-${arch}${platform === 'linux' && musl ? '-musl' : ''}`,
|
|
128
|
+
copiedIntoBin: COPIED_INTO_BIN.has(pkg.name),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
module.exports = { nativeBuildFor, findInstalled, isMusl, NATIVE_BUILDS, COPIED_INTO_BIN };
|
package/src/probe.js
CHANGED
|
@@ -64,6 +64,8 @@ const CODE = {
|
|
|
64
64
|
BAD_MODEL: 'bad_model',
|
|
65
65
|
SERVICE_UNAVAILABLE: 'service_unavailable',
|
|
66
66
|
CLI_ERROR: 'cli_error',
|
|
67
|
+
INSTALL_INCOMPLETE: 'install_incomplete',
|
|
68
|
+
ENDPOINT_INCOMPATIBLE: 'endpoint_incompatible',
|
|
67
69
|
};
|
|
68
70
|
|
|
69
71
|
const BENIGN_OUTPUT = BENIGN_CLI_OUTPUT;
|
|
@@ -133,6 +135,11 @@ function authFlavor(entry, agentEnv) {
|
|
|
133
135
|
*/
|
|
134
136
|
/** Classify an error by its OUTPUT TEXT alone; null when nothing matches. */
|
|
135
137
|
function classifyOutputText(text) {
|
|
138
|
+
// npm left out the per-platform package holding the native build (codex:
|
|
139
|
+
// "Missing optional dependency @openai/codex-win32-x64"), or the package's
|
|
140
|
+
// placeholder is what ran. Reinstalling fixes it; read as a CLI error it
|
|
141
|
+
// was a stack trace with no next step.
|
|
142
|
+
if (/missing optional dependency|native binary not installed|postinstall (script )?(was|did) not run/.test(text)) return CODE.INSTALL_INCOMPLETE;
|
|
136
143
|
// Relays word this their own way and answer 403 rather than 402, so the
|
|
137
144
|
// English-and-402 pattern alone read "out of credit" as "bad credentials"
|
|
138
145
|
// — the one misreading that sends a user to re-authenticate a working key.
|
|
@@ -234,6 +241,19 @@ function buildGuidance(code, entry, agentEnv, extra = {}) {
|
|
|
234
241
|
case CODE.SERVICE_UNAVAILABLE:
|
|
235
242
|
lines.push(`The model provider for ${label} is temporarily unavailable. Wait a few minutes and test again.`);
|
|
236
243
|
break;
|
|
244
|
+
case CODE.ENDPOINT_INCOMPATIBLE:
|
|
245
|
+
lines.push(`${label}'s endpoint takes Responses API requests but refuses ${extra.param ? `the "${extra.param}" field` : 'a field'} that ${label} sends with every one of them, so every run fails there. ${label} has no setting to leave it out.`);
|
|
246
|
+
lines.push(type === 'codex'
|
|
247
|
+
? 'Point the agent at an endpoint that supports the full Responses API, or edit the agent and set Agent capabilities to "Chat only · Chat Completions API" (it then answers without running tools on the device).'
|
|
248
|
+
: 'Point the agent at an endpoint that supports the full Responses API.');
|
|
249
|
+
break;
|
|
250
|
+
case CODE.INSTALL_INCOMPLETE:
|
|
251
|
+
lines.push(`${label} is installed, but not completely: the part that holds its native program for this computer is missing (npm can leave it out without failing the install). Reinstall ${label} from the launcher's Install page.`);
|
|
252
|
+
{
|
|
253
|
+
const cmd = installCommandFor(entry);
|
|
254
|
+
if (cmd) lines.push(`Or reinstall manually: ${cmd}`);
|
|
255
|
+
}
|
|
256
|
+
break;
|
|
237
257
|
case CODE.VERSION_INCOMPATIBLE:
|
|
238
258
|
lines.push(`The installed ${label} version is not the one OpenAgents supports. Reinstall it from the launcher's Install page.`);
|
|
239
259
|
{
|
|
@@ -263,14 +283,14 @@ function buildGuidance(code, entry, agentEnv, extra = {}) {
|
|
|
263
283
|
async function directApiVerdict(agentEnv, entry, model, baseUrl, protocol) {
|
|
264
284
|
const res = await callLLM(agentEnv, model, baseUrl, protocol);
|
|
265
285
|
if (!res || res.success) return null;
|
|
266
|
-
const code =
|
|
286
|
+
const code = directFailureCode(res.error, protocol);
|
|
267
287
|
if (code === CODE.CLI_ERROR) return null;
|
|
268
288
|
return {
|
|
269
289
|
ok: false,
|
|
270
290
|
method: 'llm_api',
|
|
271
291
|
code,
|
|
272
292
|
message: tail(res.error, OUTPUT_CAP),
|
|
273
|
-
guidance: buildGuidance(code, entry, agentEnv),
|
|
293
|
+
guidance: buildGuidance(code, entry, agentEnv, { param: refusedParam(res.error) }),
|
|
274
294
|
};
|
|
275
295
|
}
|
|
276
296
|
|
|
@@ -305,6 +325,10 @@ function directBaseUrl(entry, env) {
|
|
|
305
325
|
* sent /chat/completions and reported broken.
|
|
306
326
|
*/
|
|
307
327
|
function directProtocol(type, env) {
|
|
328
|
+
// A Codex agent on a third-party endpoint in Responses mode runs the CLI
|
|
329
|
+
// against <base>/responses (adapters/codex.js); a chat/completions test
|
|
330
|
+
// passed for endpoints that then refused every run.
|
|
331
|
+
if (type === 'codex') return codexResponsesMode(env) ? 'responses' : undefined;
|
|
308
332
|
if (type === 'deepseek') {
|
|
309
333
|
// dsh calls a gateway in the format its URL says (see classifyDshEndpoint);
|
|
310
334
|
// DeepSeek's own host is left to the client, which knows it.
|
|
@@ -321,6 +345,42 @@ function directProtocol(type, env) {
|
|
|
321
345
|
return null;
|
|
322
346
|
}
|
|
323
347
|
|
|
348
|
+
/** Whether a Codex agent runs the CLI on a custom Responses provider (as adapters/codex.js decides it). */
|
|
349
|
+
function codexResponsesMode(env) {
|
|
350
|
+
if (String(env.CODEX_WIRE_API || '').toLowerCase() !== 'responses') return false;
|
|
351
|
+
const base = String(env.OPENAI_BASE_URL || env.LLM_BASE_URL || '').trim();
|
|
352
|
+
return !!base && !base.includes('api.openai.com');
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// The Responses test sends what the Codex CLI sends (utils.js). An endpoint
|
|
356
|
+
// that refuses one of those fields will refuse every Codex turn: that is not
|
|
357
|
+
// a bad key or a bad model. Only read so for that test — the chat test's own
|
|
358
|
+
// fields (max_completion_tokens) are not the agent's.
|
|
359
|
+
const RESPONSES_PARAM_REFUSED = /unsupported .{0,40}parameter|unsupported_parameter|unknown parameter|unrecognized request argument|unrecognized (key|field)|additional properties|extra (inputs|fields) are not permitted/i;
|
|
360
|
+
|
|
361
|
+
// The one field the Responses test adds that the CLI never sends (utils.js
|
|
362
|
+
// asks again without it when it is refused). A refusal of it is about the
|
|
363
|
+
// test, not about every Codex turn.
|
|
364
|
+
const RESPONSES_PROBE_ONLY = 'max_output_tokens';
|
|
365
|
+
|
|
366
|
+
/** The code for a failed direct test: the general reading, or the Responses-only one above. */
|
|
367
|
+
function directFailureCode(error, protocol) {
|
|
368
|
+
const text = String(error || '');
|
|
369
|
+
if (protocol === 'responses' && RESPONSES_PARAM_REFUSED.test(text) && !/with this model|reasoning[._]/i.test(text)) {
|
|
370
|
+
// The field the refusal names, or — when it names none — its text.
|
|
371
|
+
const param = refusedParam(text);
|
|
372
|
+
const probeOnly = param ? param === RESPONSES_PROBE_ONLY : text.includes(RESPONSES_PROBE_ONLY);
|
|
373
|
+
if (!probeOnly) return CODE.ENDPOINT_INCOMPATIBLE;
|
|
374
|
+
}
|
|
375
|
+
return classifyFailure(error, {});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** The field a refusing endpoint named (`"param":"client_metadata"`), or ''. */
|
|
379
|
+
function refusedParam(error) {
|
|
380
|
+
const m = /"param"\s*:\s*"([^"]{1,64})"/.exec(String(error || ''));
|
|
381
|
+
return m ? m[1] : '';
|
|
382
|
+
}
|
|
383
|
+
|
|
324
384
|
/**
|
|
325
385
|
* One direct call to the agent's endpoint, on the model the agent runs on.
|
|
326
386
|
* Never rejects.
|
|
@@ -705,11 +765,11 @@ async function probeAgentType(connector, type, opts = {}) {
|
|
|
705
765
|
reply: tail(res.response || '', REPLY_CAP),
|
|
706
766
|
});
|
|
707
767
|
}
|
|
708
|
-
const code =
|
|
768
|
+
const code = directFailureCode(res && res.error, protocol);
|
|
709
769
|
return done({
|
|
710
770
|
ok: false, method: 'llm_api', code,
|
|
711
771
|
message: tail(res && res.error, OUTPUT_CAP) || 'LLM API test failed',
|
|
712
|
-
guidance: buildGuidance(code, entry, agentEnv),
|
|
772
|
+
guidance: buildGuidance(code, entry, agentEnv, { param: refusedParam(res && res.error) }),
|
|
713
773
|
});
|
|
714
774
|
}
|
|
715
775
|
|
package/src/utils.js
CHANGED
|
@@ -22,6 +22,7 @@ const VENDOR_HOSTS = /(^|\.)(openai\.com|moonshot\.ai|deepseek\.com|anthropic\.c
|
|
|
22
22
|
const MODEL_REJECTED =
|
|
23
23
|
/model.{0,60}?(is not available|does not exist|not found|unknown model|invalid model|no such model|not supported)|model_not_found/is;
|
|
24
24
|
|
|
25
|
+
const crypto = require('crypto');
|
|
25
26
|
const { networkErrorText } = require('./adapters/utils');
|
|
26
27
|
|
|
27
28
|
/** One HTTP request. Resolves { status, data } or { error }; never rejects. */
|
|
@@ -119,6 +120,100 @@ function completionResult(res, { isAnthropic, model }) {
|
|
|
119
120
|
}
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
/**
|
|
124
|
+
* The one field the Responses test adds that the Codex CLI does not send
|
|
125
|
+
* (codex-api's ResponsesApiRequest has no such field): it caps what the test
|
|
126
|
+
* costs. An endpoint refusing it says nothing about the agent, so the test
|
|
127
|
+
* asks again without it.
|
|
128
|
+
*/
|
|
129
|
+
const PROBE_ONLY_FIELD = 'max_output_tokens';
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The Responses API test, sent the way the Codex CLI sends every turn:
|
|
133
|
+
* streamed, `store: false`, `client_metadata`, the input as message items. A
|
|
134
|
+
* relay that refuses one of those ("Unsupported Responses request
|
|
135
|
+
* parameter", param client_metadata) passed a plain chat/completions test
|
|
136
|
+
* and then failed every message; one that only streams failed a non-streamed
|
|
137
|
+
* test and then chatted fine.
|
|
138
|
+
*
|
|
139
|
+
* @returns {Promise<{ res: object, result: object }>} the last raw response and its reading
|
|
140
|
+
*/
|
|
141
|
+
async function requestResponses(url, headers, model) {
|
|
142
|
+
const body = {
|
|
143
|
+
model,
|
|
144
|
+
stream: true,
|
|
145
|
+
input: [{ type: 'message', role: 'user', content: [{ type: 'input_text', text: 'Say hi in 5 words.' }] }],
|
|
146
|
+
store: false,
|
|
147
|
+
client_metadata: { session_id: crypto.randomUUID() },
|
|
148
|
+
[PROBE_ONLY_FIELD]: 16,
|
|
149
|
+
};
|
|
150
|
+
const send = () => requestCompletion(url, {
|
|
151
|
+
headers: { ...headers, Accept: 'text/event-stream' },
|
|
152
|
+
body: JSON.stringify(body),
|
|
153
|
+
});
|
|
154
|
+
const res = await send();
|
|
155
|
+
const { result, refusal } = readResponses(res, model);
|
|
156
|
+
// The field the refusal names, or — when it names none — its text.
|
|
157
|
+
const refused = refusal && (refusal.param ? refusal.param === PROBE_ONLY_FIELD : refusal.text.includes(PROBE_ONLY_FIELD));
|
|
158
|
+
if (!refused) return { res, result };
|
|
159
|
+
delete body[PROBE_ONLY_FIELD];
|
|
160
|
+
const again = await send();
|
|
161
|
+
return { res: again, result: readResponses(again, model).result };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Read a streamed Responses reply the way the Codex CLI reads it: good once
|
|
166
|
+
* `response.completed` arrives — or `response.incomplete`, the cap this test
|
|
167
|
+
* sets — bad on `response.failed` or an `error` event. A stream that ends
|
|
168
|
+
* before either fails the CLI's turn too ("stream closed before
|
|
169
|
+
* response.completed"), and so does a single JSON body in place of a stream.
|
|
170
|
+
*
|
|
171
|
+
* @returns {{ result: object, refusal: { param: string, text: string } | null }}
|
|
172
|
+
* the reading, and — only for an HTTP error or a failed/error event — what
|
|
173
|
+
* the endpoint refused, whole: the field it names (`param`, '' when it
|
|
174
|
+
* names none) and its full text. Never read off anything else: a stream's
|
|
175
|
+
* response.created event, and a JSON body, echo the request's own fields.
|
|
176
|
+
*/
|
|
177
|
+
function readResponses(res, model) {
|
|
178
|
+
if (res.error) return { result: { success: false, error: res.error }, refusal: null };
|
|
179
|
+
const data = String(res.data == null ? '' : res.data);
|
|
180
|
+
if (res.status >= 400) {
|
|
181
|
+
let param = '';
|
|
182
|
+
try {
|
|
183
|
+
const parsed = JSON.parse(data);
|
|
184
|
+
param = String((parsed.error && parsed.error.param) || parsed.param || '');
|
|
185
|
+
} catch {}
|
|
186
|
+
return { result: { success: false, error: `HTTP ${res.status}: ${data.slice(0, 200)}` }, refusal: { param, text: data } };
|
|
187
|
+
}
|
|
188
|
+
let text = '';
|
|
189
|
+
for (const line of data.split(/\r?\n/)) {
|
|
190
|
+
const m = /^data:\s?(.*)$/.exec(line);
|
|
191
|
+
if (!m) continue;
|
|
192
|
+
let evt;
|
|
193
|
+
try { evt = JSON.parse(m[1]); } catch { continue; }
|
|
194
|
+
const type = evt && evt.type;
|
|
195
|
+
if (type === 'response.output_text.delta' && typeof evt.delta === 'string') {
|
|
196
|
+
text += evt.delta;
|
|
197
|
+
} else if (type === 'response.completed' || type === 'response.incomplete') {
|
|
198
|
+
const result = { success: true, model: (evt.response && evt.response.model) || model || '?', response: text.slice(0, 80) };
|
|
199
|
+
return { result, refusal: null };
|
|
200
|
+
} else if (type === 'response.failed' || type === 'error') {
|
|
201
|
+
const err = (evt.response && evt.response.error) || evt.error || evt;
|
|
202
|
+
// param and code first, so a long message is what the cut drops: the
|
|
203
|
+
// probe reads the refused field off this text (probe.js refusedParam).
|
|
204
|
+
const shown = JSON.stringify({ param: err.param, code: err.code, ...err }).slice(0, 200);
|
|
205
|
+
return {
|
|
206
|
+
result: { success: false, error: `${type}: ${shown}` },
|
|
207
|
+
refusal: { param: String(err.param || ''), text: JSON.stringify(err) },
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (/^\s*[{[]/.test(data)) {
|
|
212
|
+
return { result: { success: false, error: `Answered with a JSON body, not the event stream the Codex CLI reads: ${data.slice(0, 200)}` }, refusal: null };
|
|
213
|
+
}
|
|
214
|
+
return { result: { success: false, error: `Stream ended before response.completed: ${data.slice(0, 200)}` }, refusal: null };
|
|
215
|
+
}
|
|
216
|
+
|
|
122
217
|
/**
|
|
123
218
|
* Test an LLM connection by sending a minimal inference request.
|
|
124
219
|
*
|
|
@@ -131,8 +226,10 @@ function completionResult(res, { isAnthropic, model }) {
|
|
|
131
226
|
* ANTHROPIC_MODEL), which `env` alone does not say; see agent-model.js.
|
|
132
227
|
* @param {string} [opts.baseUrl] - The endpoint, when the caller knows it from
|
|
133
228
|
* a variable of the agent's own (DEEPSEEK_BASE_URL); see probe.js.
|
|
134
|
-
* @param {'anthropic'|'openai'} [opts.protocol] - The API to speak,
|
|
135
|
-
* agent's config names it (Pi's PI_API_FORMAT
|
|
229
|
+
* @param {'anthropic'|'openai'|'responses'} [opts.protocol] - The API to speak,
|
|
230
|
+
* when the agent's config names it (Pi's PI_API_FORMAT, Codex's
|
|
231
|
+
* CODEX_WIRE_API) rather than its env shape. 'responses' is OpenAI's
|
|
232
|
+
* Responses API, asked the way the Codex CLI asks it (see below).
|
|
136
233
|
* @returns {Promise<{success: boolean, model?: string, response?: string, error?: string}>}
|
|
137
234
|
*/
|
|
138
235
|
async function testLLMConnection(env, opts = {}) {
|
|
@@ -154,7 +251,12 @@ async function testLLMConnection(env, opts = {}) {
|
|
|
154
251
|
? opts.protocol === 'anthropic'
|
|
155
252
|
: hasAnthropicConfig || baseUrl.includes('anthropic');
|
|
156
253
|
|
|
157
|
-
|
|
254
|
+
const isResponses = opts.protocol === 'responses';
|
|
255
|
+
// The Codex CLI calls <base_url>/responses on the URL as configured
|
|
256
|
+
// (adapters/codex.js): an endpoint at /codex answered /codex/responses and
|
|
257
|
+
// 404'd the /codex/v1/responses this used to test.
|
|
258
|
+
if (isResponses) baseUrl = baseUrl.replace(/\/+$/, '');
|
|
259
|
+
else if (!isAnthropic && !baseUrl.endsWith('/v1')) {
|
|
158
260
|
baseUrl += '/v1';
|
|
159
261
|
}
|
|
160
262
|
|
|
@@ -181,7 +283,11 @@ async function testLLMConnection(env, opts = {}) {
|
|
|
181
283
|
}
|
|
182
284
|
} catch {}
|
|
183
285
|
const body = JSON.stringify({
|
|
184
|
-
|
|
286
|
+
// Only for an agent with no model set. claude-sonnet-4-20250514 was the
|
|
287
|
+
// default here and Anthropic retired it on 2026-06-15: the test then
|
|
288
|
+
// failed with a 404 "not_found_error" for a key that works. Any 200 is
|
|
289
|
+
// a pass, so a reply that is all thinking within 32 tokens still counts.
|
|
290
|
+
model: env.ANTHROPIC_MODEL || model || 'claude-sonnet-5-5',
|
|
185
291
|
max_tokens: 32,
|
|
186
292
|
messages: [{ role: 'user', content: 'Say hi in 5 words.' }],
|
|
187
293
|
});
|
|
@@ -208,14 +314,19 @@ async function testLLMConnection(env, opts = {}) {
|
|
|
208
314
|
|
|
209
315
|
let last = null;
|
|
210
316
|
for (const candidate of candidates) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
317
|
+
let res;
|
|
318
|
+
if (isResponses) {
|
|
319
|
+
({ res, result: last } = await requestResponses(`${baseUrl}/responses`, headers, candidate));
|
|
320
|
+
} else {
|
|
321
|
+
const requestBody = {
|
|
322
|
+
model: candidate,
|
|
323
|
+
messages: [{ role: 'user', content: 'Say hi in 5 words.' }],
|
|
324
|
+
};
|
|
325
|
+
if (hasKimiConfig) requestBody.max_tokens = 32;
|
|
326
|
+
else requestBody.max_completion_tokens = 32;
|
|
327
|
+
res = await requestCompletion(`${baseUrl}/chat/completions`, { headers, body: JSON.stringify(requestBody) });
|
|
328
|
+
last = completionResult(res, { isAnthropic: false, model: candidate });
|
|
329
|
+
}
|
|
219
330
|
if (last.success) return last;
|
|
220
331
|
// Only a model WE picked moves on to the next one. A configured model is
|
|
221
332
|
// sent as configured and its rejection reported as-is.
|