@tobi-or-not/quorum 0.1.0 → 0.3.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/LICENSE +202 -0
- package/README.md +5 -0
- package/dist/bench/fingerprint.d.ts +36 -0
- package/dist/bench/fingerprint.js +79 -0
- package/dist/bench/records.d.ts +62 -0
- package/dist/bench/records.js +161 -0
- package/dist/bench/report.d.ts +110 -0
- package/dist/bench/report.js +332 -0
- package/dist/bench/run-dir.d.ts +101 -0
- package/dist/bench/run-dir.js +379 -0
- package/dist/bench/runner.d.ts +80 -0
- package/dist/bench/runner.js +160 -0
- package/dist/bench/score.d.ts +30 -0
- package/dist/bench/score.js +38 -0
- package/dist/bench/stats.d.ts +51 -0
- package/dist/bench/stats.js +83 -0
- package/dist/bench/suite.d.ts +87 -0
- package/dist/bench/suite.js +286 -0
- package/dist/cli/bench.d.ts +32 -0
- package/dist/cli/bench.js +463 -0
- package/dist/cli/bin.js +9 -1
- package/dist/cli/io.d.ts +45 -0
- package/dist/cli/io.js +45 -0
- package/dist/cli/main.d.ts +7 -37
- package/dist/cli/main.js +47 -60
- package/dist/cli/print-config.js +38 -9
- package/dist/cli/render.d.ts +10 -0
- package/dist/cli/render.js +37 -16
- package/dist/config/claude-code.js +42 -11
- package/dist/config/env.js +135 -32
- package/dist/config/from-config.js +43 -9
- package/dist/config/load.d.ts +43 -0
- package/dist/config/load.js +205 -46
- package/dist/config/profiles.js +87 -20
- package/dist/config/resolve.js +194 -47
- package/dist/config/schema.js +98 -23
- package/dist/core/diff.d.ts +10 -0
- package/dist/core/diff.js +90 -17
- package/dist/core/errors.js +14 -5
- package/dist/core/flags.js +30 -6
- package/dist/core/output.js +49 -13
- package/dist/core/policy.js +6 -2
- package/dist/core/profile.js +42 -15
- package/dist/core/prompts.d.ts +3 -1
- package/dist/core/prompts.js +28 -8
- package/dist/core/quorum.js +128 -33
- package/dist/core/sanitize.d.ts +16 -1
- package/dist/core/sanitize.js +31 -5
- package/dist/core/stages/context.js +30 -5
- package/dist/core/stages/panel.js +55 -9
- package/dist/core/stages/research.js +13 -3
- package/dist/core/verify.js +6 -2
- package/dist/index.js +5 -1
- package/dist/providers/anthropic.js +28 -5
- package/dist/providers/build.js +57 -16
- package/dist/providers/claude-code.js +206 -51
- package/dist/providers/gemini.js +61 -14
- package/dist/providers/http.js +32 -9
- package/dist/providers/messages.js +14 -2
- package/dist/providers/ollama.js +31 -8
- package/dist/providers/openai-compatible.js +51 -15
- package/dist/providers/options.js +120 -40
- package/dist/providers/presets.js +143 -16
- package/dist/providers/reply.js +15 -5
- package/dist/providers/size.js +11 -3
- package/package.json +6 -5
package/dist/providers/build.js
CHANGED
|
@@ -18,11 +18,16 @@ const ADAPTERS = {
|
|
|
18
18
|
gemini,
|
|
19
19
|
ollama,
|
|
20
20
|
};
|
|
21
|
-
const PROTOCOLS = [
|
|
21
|
+
const PROTOCOLS = [
|
|
22
|
+
...Object.keys(ADAPTERS),
|
|
23
|
+
'claude-code',
|
|
24
|
+
];
|
|
22
25
|
const TOOL_MODEL = 'runs tools on the server by itself, which quorum never allows';
|
|
23
26
|
/** The spec's value, or the preset's when the spec leaves it unset: a field set to `undefined` is unset, `null` is set. */
|
|
24
27
|
function filled(value, preset) {
|
|
25
|
-
return value === undefined
|
|
28
|
+
return value === undefined
|
|
29
|
+
? preset
|
|
30
|
+
: value;
|
|
26
31
|
}
|
|
27
32
|
/**
|
|
28
33
|
* Whether `pattern` matches the whole of `model`, a visible ASCII id, in any ASCII letter case: `*` stands
|
|
@@ -37,24 +42,38 @@ function matchesModel(pattern, model) {
|
|
|
37
42
|
const TOOL_MODELS_AT_ORIGIN = new Map();
|
|
38
43
|
for (const { baseUrl, toolModels } of Object.values(presets)) {
|
|
39
44
|
const origin = httpOrigin(baseUrl);
|
|
40
|
-
if (origin !== undefined)
|
|
41
|
-
TOOL_MODELS_AT_ORIGIN.set(origin, [
|
|
45
|
+
if (origin !== undefined) {
|
|
46
|
+
TOOL_MODELS_AT_ORIGIN.set(origin, [
|
|
47
|
+
...TOOL_MODELS_AT_ORIGIN.get(origin) ?? [],
|
|
48
|
+
...toolModels,
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
42
51
|
}
|
|
43
52
|
/** The patterns that hold a model: the named preset's, and those of every preset whose base URL has the origin of `baseUrl`. */
|
|
44
53
|
function toolModelPatterns(preset, baseUrl) {
|
|
45
54
|
const origin = httpOrigin(baseUrl);
|
|
46
|
-
return [
|
|
55
|
+
return [
|
|
56
|
+
...preset?.toolModels ?? [],
|
|
57
|
+
...origin === undefined
|
|
58
|
+
? []
|
|
59
|
+
: TOOL_MODELS_AT_ORIGIN.get(origin) ?? [],
|
|
60
|
+
];
|
|
47
61
|
}
|
|
48
62
|
/** `value` as an object with no prototype when it is a record, so its fields are its own; any other value as it is, for the adapter to refuse. */
|
|
49
63
|
function ownRecord(value) {
|
|
50
|
-
return isRecord(value)
|
|
64
|
+
return isRecord(value)
|
|
65
|
+
? withoutPrototype(Object.entries(value))
|
|
66
|
+
: value;
|
|
51
67
|
}
|
|
52
68
|
/** The fields that make the builder's own decisions: the adapter is handed the others, and `maxTokensField` only when it takes it. */
|
|
53
69
|
const BUILDER_FIELDS = new Set(['preset', 'protocol', 'maxTokensField']);
|
|
54
70
|
/** The fields of the `claude-code` adapter alone. */
|
|
55
71
|
const CLAUDE_CODE_FIELDS = ['args', 'command'];
|
|
56
72
|
/** The fields no HTTP adapter is handed: the builder's own, and those of the `claude-code` adapter. */
|
|
57
|
-
const NOT_FOR_HTTP = new Set([
|
|
73
|
+
const NOT_FOR_HTTP = new Set([
|
|
74
|
+
...BUILDER_FIELDS,
|
|
75
|
+
...CLAUDE_CODE_FIELDS,
|
|
76
|
+
]);
|
|
58
77
|
/** The options of a `claude-code` adapter: the spec's own fields, and an environment from the context alone. */
|
|
59
78
|
function claudeCodeFields(entries, env) {
|
|
60
79
|
const fields = entries.filter(([key]) => !BUILDER_FIELDS.has(key) && key !== 'env');
|
|
@@ -87,11 +106,15 @@ export function buildProvider(spec, context = {}) {
|
|
|
87
106
|
const entries = Object.entries(spec);
|
|
88
107
|
const own = withoutPrototype(entries);
|
|
89
108
|
const { id, model, preset: name, protocol: named, maxTokensField } = own;
|
|
90
|
-
const preset = name === undefined
|
|
109
|
+
const preset = name === undefined
|
|
110
|
+
? undefined
|
|
111
|
+
: presetOf(name);
|
|
91
112
|
const issues = [];
|
|
92
113
|
if (name !== undefined && preset === undefined)
|
|
93
114
|
issues.push({ path: `${id}.preset`, message: `must be one of ${wordList(Object.keys(presets))}` });
|
|
94
|
-
let protocol = named === undefined
|
|
115
|
+
let protocol = named === undefined
|
|
116
|
+
? preset?.protocol
|
|
117
|
+
: undefined;
|
|
95
118
|
if (named === undefined && name === undefined)
|
|
96
119
|
issues.push({ path: `${id}.protocol`, message: 'is required without a preset' });
|
|
97
120
|
else if (named !== undefined && !(typeof named === 'string' && PROTOCOLS.includes(named)))
|
|
@@ -130,16 +153,31 @@ export function buildProvider(spec, context = {}) {
|
|
|
130
153
|
if (protocol === undefined || (!cli && typeof baseUrl !== 'string') || !usableModel)
|
|
131
154
|
throw new ConfigError(issues);
|
|
132
155
|
const fields = cli
|
|
133
|
-
? claudeCodeFields(entries, Object.hasOwn(context, 'env')
|
|
156
|
+
? claudeCodeFields(entries, Object.hasOwn(context, 'env')
|
|
157
|
+
? context.env
|
|
158
|
+
: undefined)
|
|
134
159
|
: [
|
|
135
160
|
...entries.filter(([key]) => !NOT_FOR_HTTP.has(key)),
|
|
136
161
|
['baseUrl', baseUrl],
|
|
137
|
-
[
|
|
138
|
-
|
|
139
|
-
|
|
162
|
+
[
|
|
163
|
+
'auth',
|
|
164
|
+
ownRecord(filled(own.auth, preset?.auth)),
|
|
165
|
+
],
|
|
166
|
+
[
|
|
167
|
+
'structuredOutput',
|
|
168
|
+
filled(own.structuredOutput, preset?.structuredOutput),
|
|
169
|
+
],
|
|
170
|
+
[
|
|
171
|
+
'temperature',
|
|
172
|
+
filled(own.temperature, preset?.temperature),
|
|
173
|
+
],
|
|
140
174
|
];
|
|
141
|
-
if (protocol === 'openai-compatible')
|
|
142
|
-
fields.push([
|
|
175
|
+
if (protocol === 'openai-compatible') {
|
|
176
|
+
fields.push([
|
|
177
|
+
'maxTokensField',
|
|
178
|
+
filled(maxTokensField, preset?.maxTokensField),
|
|
179
|
+
]);
|
|
180
|
+
}
|
|
143
181
|
let adapterIssues = [];
|
|
144
182
|
try {
|
|
145
183
|
const provider = makeAdapter(protocol, fields);
|
|
@@ -151,5 +189,8 @@ export function buildProvider(spec, context = {}) {
|
|
|
151
189
|
throw error;
|
|
152
190
|
adapterIssues = error.issues;
|
|
153
191
|
}
|
|
154
|
-
throw new ConfigError([
|
|
192
|
+
throw new ConfigError([
|
|
193
|
+
...issues,
|
|
194
|
+
...adapterIssues,
|
|
195
|
+
]);
|
|
155
196
|
}
|
|
@@ -33,7 +33,15 @@ export const VERSION_CHECK_MS = 10_000;
|
|
|
33
33
|
const AUTH_MODES = ['inherit', 'login', 'api-key'];
|
|
34
34
|
const STRUCTURED_OUTPUTS = ['prompt', 'schema'];
|
|
35
35
|
/** The fields of a provider that belong to an HTTP endpoint, none of which a CLI has. */
|
|
36
|
-
const HTTP_FIELDS = [
|
|
36
|
+
const HTTP_FIELDS = [
|
|
37
|
+
'baseUrl',
|
|
38
|
+
'headers',
|
|
39
|
+
'query',
|
|
40
|
+
'extraBody',
|
|
41
|
+
'temperature',
|
|
42
|
+
'maxTokens',
|
|
43
|
+
'systemPrompt',
|
|
44
|
+
];
|
|
37
45
|
const WINDOWS_SCRIPT = /\.(?:cmd|bat)$/i;
|
|
38
46
|
const PATH_SEPARATOR = /[/\\]/;
|
|
39
47
|
/**
|
|
@@ -42,7 +50,15 @@ const PATH_SEPARATOR = /[/\\]/;
|
|
|
42
50
|
* command. Nothing configures or drops one. The two empty values keep their two-argument form, which is
|
|
43
51
|
* the one verified live.
|
|
44
52
|
*/
|
|
45
|
-
const ISOLATION_FLAGS = [
|
|
53
|
+
const ISOLATION_FLAGS = [
|
|
54
|
+
'--tools',
|
|
55
|
+
'',
|
|
56
|
+
'--setting-sources',
|
|
57
|
+
'',
|
|
58
|
+
'--strict-mcp-config',
|
|
59
|
+
'--no-session-persistence',
|
|
60
|
+
'--disable-slash-commands',
|
|
61
|
+
];
|
|
46
62
|
/** The variables of a parent Claude Code session, which a nested run must not inherit: its messaging channel and its entry point. */
|
|
47
63
|
const SESSION_VARIABLES = new Set([
|
|
48
64
|
'CLAUDE_CODE_MESSAGING_TOKEN',
|
|
@@ -127,18 +143,37 @@ const MAX_RESULT_TEXT = 200;
|
|
|
127
143
|
/** The most characters of stderr that an error message quotes. */
|
|
128
144
|
const MAX_STDERR_TEXT = 500;
|
|
129
145
|
/** The flags `args` may hold, and the checks of their values. */
|
|
130
|
-
const ALLOWED_FLAGS = [
|
|
131
|
-
|
|
146
|
+
const ALLOWED_FLAGS = [
|
|
147
|
+
'--effort',
|
|
148
|
+
'--fallback-model',
|
|
149
|
+
'--max-budget-usd',
|
|
150
|
+
'--betas',
|
|
151
|
+
];
|
|
152
|
+
const EFFORTS = [
|
|
153
|
+
'low',
|
|
154
|
+
'medium',
|
|
155
|
+
'high',
|
|
156
|
+
'xhigh',
|
|
157
|
+
'max',
|
|
158
|
+
];
|
|
132
159
|
/** The most characters of a `--max-budget-usd` value: a decimal that long is finite. */
|
|
133
160
|
const MAX_BUDGET_CHARACTERS = 12;
|
|
134
161
|
const NOT_A_STRING = 'must be a string';
|
|
135
162
|
const NOT_A_FLAG = `is not a flag quorum passes to claude: use ${wordList(ALLOWED_FLAGS)}`;
|
|
136
163
|
const PLAIN_VALUE = 'must be visible ASCII characters that do not start with -';
|
|
137
164
|
const VALUE_PROBLEMS = {
|
|
138
|
-
'--effort': value => EFFORTS.includes(value)
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
'--
|
|
165
|
+
'--effort': value => EFFORTS.includes(value)
|
|
166
|
+
? undefined
|
|
167
|
+
: `must be ${wordList(EFFORTS)}`,
|
|
168
|
+
'--max-budget-usd': value => value.length <= MAX_BUDGET_CHARACTERS && /^\d+(?:\.\d+)?$/.test(value) && Number(value) > 0
|
|
169
|
+
? undefined
|
|
170
|
+
: 'must be a positive number of dollars, such as 0.50',
|
|
171
|
+
'--fallback-model': value => plainValue(value)
|
|
172
|
+
? undefined
|
|
173
|
+
: PLAIN_VALUE,
|
|
174
|
+
'--betas': value => plainValue(value)
|
|
175
|
+
? undefined
|
|
176
|
+
: PLAIN_VALUE,
|
|
142
177
|
};
|
|
143
178
|
function plainValue(value) {
|
|
144
179
|
return value !== '' && !NOT_VISIBLE_ASCII.test(value) && !value.startsWith('-');
|
|
@@ -182,32 +217,57 @@ function walkArgs(args, auth) {
|
|
|
182
217
|
problems.push({ index, message: 'needs a value' });
|
|
183
218
|
continue;
|
|
184
219
|
}
|
|
185
|
-
const value = separate
|
|
220
|
+
const value = separate
|
|
221
|
+
? args[index + 1]
|
|
222
|
+
: element.slice(flag.length + 1);
|
|
186
223
|
if (separate)
|
|
187
224
|
valueIndex = index + 1;
|
|
188
225
|
if (typeof value !== 'string') {
|
|
189
226
|
problems.push({ index: index + 1, message: NOT_A_STRING });
|
|
190
227
|
continue;
|
|
191
228
|
}
|
|
192
|
-
const flagProblem = seen.has(flag)
|
|
229
|
+
const flagProblem = seen.has(flag)
|
|
230
|
+
? 'repeats a flag'
|
|
231
|
+
: flag === '--betas' && auth !== 'api-key'
|
|
232
|
+
? 'is only for auth: api-key'
|
|
233
|
+
: undefined;
|
|
193
234
|
seen.add(flag);
|
|
194
|
-
const valueProblem = flagProblem === undefined
|
|
195
|
-
|
|
235
|
+
const valueProblem = flagProblem === undefined
|
|
236
|
+
? VALUE_PROBLEMS[flag](value)
|
|
237
|
+
: undefined;
|
|
238
|
+
if (flagProblem !== undefined) {
|
|
196
239
|
problems.push({ index, message: flagProblem });
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
240
|
+
}
|
|
241
|
+
else if (valueProblem !== undefined) {
|
|
242
|
+
problems.push({
|
|
243
|
+
index: separate
|
|
244
|
+
? index + 1
|
|
245
|
+
: index,
|
|
246
|
+
message: valueProblem,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
200
250
|
pairs.push([flag, value]);
|
|
251
|
+
}
|
|
201
252
|
}
|
|
202
253
|
return { pairs, problems };
|
|
203
254
|
}
|
|
204
255
|
/** The problems of `apiKey` given `auth`: set under another mode, where it would never be sent, or not visible ASCII. */
|
|
205
256
|
function apiKeyIssues(path, auth, apiKey) {
|
|
206
|
-
if (auth !== 'api-key')
|
|
207
|
-
return typeof apiKey === 'string' && apiKey !== ''
|
|
257
|
+
if (auth !== 'api-key') {
|
|
258
|
+
return typeof apiKey === 'string' && apiKey !== ''
|
|
259
|
+
? [
|
|
260
|
+
{ path, message: 'is set, but auth is not api-key, so it would never be sent' },
|
|
261
|
+
]
|
|
262
|
+
: [];
|
|
263
|
+
}
|
|
208
264
|
if (apiKey === undefined || apiKey === '')
|
|
209
265
|
return [];
|
|
210
|
-
return typeof apiKey === 'string' && !NOT_VISIBLE_ASCII.test(apiKey)
|
|
266
|
+
return typeof apiKey === 'string' && !NOT_VISIBLE_ASCII.test(apiKey)
|
|
267
|
+
? []
|
|
268
|
+
: [
|
|
269
|
+
{ path, message: 'must only contain visible ASCII characters' },
|
|
270
|
+
];
|
|
211
271
|
}
|
|
212
272
|
/**
|
|
213
273
|
* Every problem of the options, one issue per problem at `<id>.<field>`, in the order of the fields. No
|
|
@@ -255,8 +315,12 @@ export function claudeArgs(settings, systemPromptFile, schema) {
|
|
|
255
315
|
`--model=${model}`,
|
|
256
316
|
`--system-prompt-file=${systemPromptFile}`,
|
|
257
317
|
...ISOLATION_FLAGS,
|
|
258
|
-
...(structuredOutput === 'schema'
|
|
259
|
-
|
|
318
|
+
...(structuredOutput === 'schema'
|
|
319
|
+
? [`--json-schema=${JSON.stringify(schema)}`]
|
|
320
|
+
: []),
|
|
321
|
+
...(auth === 'api-key'
|
|
322
|
+
? ['--bare']
|
|
323
|
+
: []),
|
|
260
324
|
...walkArgs(args, auth).pairs.map(([flag, value]) => `${flag}=${value}`),
|
|
261
325
|
];
|
|
262
326
|
}
|
|
@@ -294,10 +358,14 @@ export function childEnv(env, auth, apiKey, tmpDir) {
|
|
|
294
358
|
};
|
|
295
359
|
const own = Object.entries(env).filter((entry) => typeof entry[1] === 'string');
|
|
296
360
|
const inheritedKey = (own.find(([name]) => name === KEY_VARIABLE) ?? own.find(([name]) => name.toUpperCase() === KEY_VARIABLE))?.[1].trim();
|
|
297
|
-
const key = typeof apiKey === 'string' && apiKey !== ''
|
|
361
|
+
const key = typeof apiKey === 'string' && apiKey !== ''
|
|
362
|
+
? apiKey
|
|
363
|
+
: inheritedKey;
|
|
298
364
|
return {
|
|
299
365
|
...Object.fromEntries(own.filter(([name]) => !removedVariable(name, auth, isolation))),
|
|
300
|
-
...(auth === 'api-key' && key !== undefined && key !== ''
|
|
366
|
+
...(auth === 'api-key' && key !== undefined && key !== ''
|
|
367
|
+
? { [KEY_VARIABLE]: key }
|
|
368
|
+
: {}),
|
|
301
369
|
...isolation,
|
|
302
370
|
};
|
|
303
371
|
}
|
|
@@ -310,8 +378,13 @@ export function childEnv(env, auth, apiKey, tmpDir) {
|
|
|
310
378
|
export function credentialValues(environment) {
|
|
311
379
|
const values = Object.entries(environment)
|
|
312
380
|
.filter(([name]) => REDACTED_VARIABLES.has(name.toUpperCase()))
|
|
313
|
-
.flatMap(([, value]) => [
|
|
314
|
-
|
|
381
|
+
.flatMap(([, value]) => [
|
|
382
|
+
value,
|
|
383
|
+
value.trim(),
|
|
384
|
+
]);
|
|
385
|
+
return [
|
|
386
|
+
...new Set(values),
|
|
387
|
+
].filter(value => value.trim() !== '');
|
|
315
388
|
}
|
|
316
389
|
const VERSION = /^(\d+)\.(\d+)\.(\d+)/;
|
|
317
390
|
/** A version an error message may quote: short plain numbers, so nothing else `claude` printed can come along. */
|
|
@@ -337,13 +410,17 @@ export function versionProblem(output) {
|
|
|
337
410
|
if (digits !== undefined && !isOlder(digits.map(Number)))
|
|
338
411
|
return undefined;
|
|
339
412
|
const found = digits?.join('.');
|
|
340
|
-
return `Claude Code ${MIN_CLAUDE_CODE_VERSION} or newer is required, found ${found !== undefined && QUOTABLE_VERSION.test(found)
|
|
413
|
+
return `Claude Code ${MIN_CLAUDE_CODE_VERSION} or newer is required, found ${found !== undefined && QUOTABLE_VERSION.test(found)
|
|
414
|
+
? found
|
|
415
|
+
: 'an unknown version'}`;
|
|
341
416
|
}
|
|
342
417
|
/** The parsed result on `stdout`, or undefined when it holds no JSON object whose `type` is `result`. */
|
|
343
418
|
function parseResult(stdout) {
|
|
344
419
|
try {
|
|
345
420
|
const parsed = JSON.parse(stdout.trim());
|
|
346
|
-
return isRecord(parsed) && parsed.type === 'result'
|
|
421
|
+
return isRecord(parsed) && parsed.type === 'result'
|
|
422
|
+
? parsed
|
|
423
|
+
: undefined;
|
|
347
424
|
}
|
|
348
425
|
catch {
|
|
349
426
|
return undefined;
|
|
@@ -360,7 +437,9 @@ function failureDetail(result, redact) {
|
|
|
360
437
|
const { errors } = result;
|
|
361
438
|
const text = typeof result.result === 'string'
|
|
362
439
|
? result.result
|
|
363
|
-
: Array.isArray(errors)
|
|
440
|
+
: Array.isArray(errors)
|
|
441
|
+
? errors.filter((error) => typeof error === 'string').join('; ')
|
|
442
|
+
: '';
|
|
364
443
|
return snippet(text, MAX_RESULT_TEXT, redact);
|
|
365
444
|
}
|
|
366
445
|
function isAboveZero(count) {
|
|
@@ -369,7 +448,9 @@ function isAboveZero(count) {
|
|
|
369
448
|
/** Whether the result counts a tool the server ran: a web search or fetch in `usage.server_tool_use`, or a web search in a model's usage. */
|
|
370
449
|
function ranServerTool(result, modelUsage) {
|
|
371
450
|
const { usage } = result;
|
|
372
|
-
const serverTools = isRecord(usage) && isRecord(usage.server_tool_use)
|
|
451
|
+
const serverTools = isRecord(usage) && isRecord(usage.server_tool_use)
|
|
452
|
+
? Object.values(usage.server_tool_use)
|
|
453
|
+
: [];
|
|
373
454
|
return serverTools.some(isAboveZero) || modelUsage.some(entry => isAboveZero(entry.webSearchRequests));
|
|
374
455
|
}
|
|
375
456
|
/** The counts of a model's usage that make up its input: the prompt, then what the cache read and what it wrote. */
|
|
@@ -419,12 +500,18 @@ function errorFailure(result, run, { id, redact }) {
|
|
|
419
500
|
const detail = failureDetail(result, redact);
|
|
420
501
|
if (typeof status === 'number' && Number.isInteger(status) && status >= 400 && status <= 599)
|
|
421
502
|
return fail('http', `HTTP ${status} from the model API: ${detail}`, status);
|
|
422
|
-
const reason = result.subtype !== 'success'
|
|
423
|
-
|
|
503
|
+
const reason = result.subtype !== 'success'
|
|
504
|
+
? result.subtype
|
|
505
|
+
: result.terminal_reason;
|
|
506
|
+
return fail('process', `claude failed${typeof reason === 'string' && REASON.test(reason)
|
|
507
|
+
? ` (${reason})`
|
|
508
|
+
: ''}: ${detail}`, run.code ?? undefined);
|
|
424
509
|
}
|
|
425
510
|
/** The signal's name in a message when it is one, else a generic word: the signal is the child's to name, never trusted. */
|
|
426
511
|
function signalName(signal) {
|
|
427
|
-
return SIGNAL.test(signal)
|
|
512
|
+
return SIGNAL.test(signal)
|
|
513
|
+
? signal
|
|
514
|
+
: 'a signal';
|
|
428
515
|
}
|
|
429
516
|
/**
|
|
430
517
|
* The reply of a result, or the `ProviderError` it amounts to, in this order: a prompt too long, a
|
|
@@ -445,16 +532,24 @@ function readFinished(result, run, context) {
|
|
|
445
532
|
throw fail('process', `claude was stopped by ${signalName(run.signal)} after its result`);
|
|
446
533
|
if (run.code !== null && run.code !== 0)
|
|
447
534
|
throw fail('process', `claude exited ${run.code} after its result`, run.code);
|
|
448
|
-
const modelUsage = isRecord(result.modelUsage)
|
|
535
|
+
const modelUsage = isRecord(result.modelUsage)
|
|
536
|
+
? Object.values(result.modelUsage).filter(isRecord)
|
|
537
|
+
: [];
|
|
449
538
|
if (ranServerTool(result, modelUsage))
|
|
450
539
|
throw fail('invalid-response', 'the server ran a tool for this reply, which quorum never allows');
|
|
451
540
|
if (result.stop_reason === 'max_tokens')
|
|
452
541
|
throw fail('truncated', 'reply was cut off by the token limit');
|
|
453
542
|
const text = replyText(result, context.structuredOutput, fail);
|
|
454
543
|
const usage = sumUsage(modelUsage, result.total_cost_usd);
|
|
455
|
-
const models = isRecord(result.modelUsage)
|
|
456
|
-
|
|
457
|
-
|
|
544
|
+
const models = isRecord(result.modelUsage)
|
|
545
|
+
? Object.keys(result.modelUsage)
|
|
546
|
+
: [];
|
|
547
|
+
const model = models.length === 1 && models[0] !== undefined
|
|
548
|
+
? models[0]
|
|
549
|
+
: context.model;
|
|
550
|
+
return usage === undefined
|
|
551
|
+
? { text, model }
|
|
552
|
+
: { text, model, usage };
|
|
458
553
|
}
|
|
459
554
|
/**
|
|
460
555
|
* Read what a finished `claude` left as a reply, or throw the `ProviderError` it amounts to. Without a
|
|
@@ -476,8 +571,12 @@ export function readResult(run, context) {
|
|
|
476
571
|
}
|
|
477
572
|
/** `error`'s system code as a message writes it, ` (ENOENT)`, or nothing when it has none or one that is no plain identifier. */
|
|
478
573
|
function codeSuffix(error) {
|
|
479
|
-
const code = isRecord(error)
|
|
480
|
-
|
|
574
|
+
const code = isRecord(error)
|
|
575
|
+
? error.code
|
|
576
|
+
: undefined;
|
|
577
|
+
return typeof code === 'string' && ERROR_CODE.test(code)
|
|
578
|
+
? ` (${code})`
|
|
579
|
+
: '';
|
|
481
580
|
}
|
|
482
581
|
/**
|
|
483
582
|
* Remove a run's directory. A failure is ignored: the directory holds only the system text, and a reply
|
|
@@ -495,7 +594,12 @@ export async function prepareRun(system, fail) {
|
|
|
495
594
|
let dir;
|
|
496
595
|
try {
|
|
497
596
|
dir = await mkdtemp(join(resolve(tmpdir()), RUN_DIRECTORY_PREFIX));
|
|
498
|
-
const run = {
|
|
597
|
+
const run = {
|
|
598
|
+
dir,
|
|
599
|
+
cwd: join(dir, 'cwd'),
|
|
600
|
+
tmp: join(dir, 'tmp'),
|
|
601
|
+
systemFile: join(dir, 'system.txt'),
|
|
602
|
+
};
|
|
499
603
|
await mkdir(run.cwd);
|
|
500
604
|
await mkdir(run.tmp);
|
|
501
605
|
await writeFile(run.systemFile, system, { mode: 0o600 });
|
|
@@ -541,14 +645,19 @@ export function readCapped(stream, max, onOverflow = () => { }) {
|
|
|
541
645
|
if (overflow)
|
|
542
646
|
return;
|
|
543
647
|
const room = max - bytes;
|
|
544
|
-
chunks.push(chunk.length > room
|
|
648
|
+
chunks.push(chunk.length > room
|
|
649
|
+
? chunk.subarray(0, room)
|
|
650
|
+
: chunk);
|
|
545
651
|
bytes += Math.min(chunk.length, room);
|
|
546
652
|
if (chunk.length > room) {
|
|
547
653
|
overflow = true;
|
|
548
654
|
onOverflow();
|
|
549
655
|
}
|
|
550
656
|
});
|
|
551
|
-
return {
|
|
657
|
+
return {
|
|
658
|
+
text: () => Buffer.concat(chunks).toString('utf8'),
|
|
659
|
+
overflowed: () => overflow,
|
|
660
|
+
};
|
|
552
661
|
}
|
|
553
662
|
/** Whether `claude` leads a process group of its own: on every platform but Windows, which has none. */
|
|
554
663
|
const GROUPED = process.platform !== 'win32';
|
|
@@ -611,7 +720,14 @@ function runChild(launch, input, signal, fail) {
|
|
|
611
720
|
const startFailure = (error) => fail('process', `could not start the claude CLI${codeSuffix(error)}`);
|
|
612
721
|
let child;
|
|
613
722
|
try {
|
|
614
|
-
child = spawn(launch.command, launch.args, {
|
|
723
|
+
child = spawn(launch.command, launch.args, {
|
|
724
|
+
cwd: launch.cwd,
|
|
725
|
+
env: launch.env,
|
|
726
|
+
stdio: 'pipe',
|
|
727
|
+
shell: false,
|
|
728
|
+
windowsHide: true,
|
|
729
|
+
detached: GROUPED,
|
|
730
|
+
});
|
|
615
731
|
}
|
|
616
732
|
catch (error) {
|
|
617
733
|
reject(startFailure(error));
|
|
@@ -666,7 +782,15 @@ function runChild(launch, input, signal, fail) {
|
|
|
666
782
|
});
|
|
667
783
|
child.once('close', (code, endedBy) => {
|
|
668
784
|
release();
|
|
669
|
-
resolve({
|
|
785
|
+
resolve({
|
|
786
|
+
run: {
|
|
787
|
+
stdout: stdout.text(),
|
|
788
|
+
stderr: stderr.text(),
|
|
789
|
+
code,
|
|
790
|
+
signal: endedBy,
|
|
791
|
+
},
|
|
792
|
+
overflow: stdout.overflowed(),
|
|
793
|
+
});
|
|
670
794
|
});
|
|
671
795
|
signal?.addEventListener('abort', stop, { once: true });
|
|
672
796
|
child.stdin?.on('error', () => { });
|
|
@@ -683,12 +807,19 @@ async function checkVersion(launch, signal, fail) {
|
|
|
683
807
|
const deadline = new AbortController();
|
|
684
808
|
const timer = setTimeout(() => deadline.abort(), VERSION_CHECK_MS);
|
|
685
809
|
try {
|
|
686
|
-
const stopped = signal === undefined
|
|
687
|
-
|
|
810
|
+
const stopped = signal === undefined
|
|
811
|
+
? deadline.signal
|
|
812
|
+
: AbortSignal.any([signal, deadline.signal]);
|
|
813
|
+
const { run } = await runChild({
|
|
814
|
+
...launch,
|
|
815
|
+
args: ['--version'],
|
|
816
|
+
}, '', stopped, fail);
|
|
688
817
|
throwIfAborted(signal, fail);
|
|
689
818
|
if (deadline.signal.aborted)
|
|
690
819
|
throw fail('process', `claude --version did not answer within ${VERSION_CHECK_MS / 1000} s`);
|
|
691
|
-
return versionProblem(run.code === 0 && run.signal === null
|
|
820
|
+
return versionProblem(run.code === 0 && run.signal === null
|
|
821
|
+
? run.stdout
|
|
822
|
+
: '');
|
|
692
823
|
}
|
|
693
824
|
finally {
|
|
694
825
|
clearTimeout(timer);
|
|
@@ -732,7 +863,9 @@ async function requireVersion(launch, signal, fail) {
|
|
|
732
863
|
const shared = versionChecks.get(launch.command);
|
|
733
864
|
let problem;
|
|
734
865
|
try {
|
|
735
|
-
problem = await (shared === undefined
|
|
866
|
+
problem = await (shared === undefined
|
|
867
|
+
? startVersionCheck(launch, signal, fail)
|
|
868
|
+
: orAbort(shared, signal, fail));
|
|
736
869
|
}
|
|
737
870
|
catch (error) {
|
|
738
871
|
if (shared === undefined)
|
|
@@ -782,7 +915,9 @@ export function claudeCode(options) {
|
|
|
782
915
|
if (issues.length > 0)
|
|
783
916
|
throw new ConfigError(issues);
|
|
784
917
|
const { id, model, contextTokens, auth = 'inherit', apiKey, structuredOutput = 'schema', command = 'claude' } = options;
|
|
785
|
-
const args = [
|
|
918
|
+
const args = [
|
|
919
|
+
...options.args ?? [],
|
|
920
|
+
];
|
|
786
921
|
const env = Object.fromEntries(Object.entries(options.env ?? process.env));
|
|
787
922
|
const budget = contextBudget(contextTokens, undefined, CLAUDE_CODE_TEMPLATE_TOKENS);
|
|
788
923
|
return {
|
|
@@ -791,7 +926,10 @@ export function claudeCode(options) {
|
|
|
791
926
|
async complete(request) {
|
|
792
927
|
const fail = replyFailure(id);
|
|
793
928
|
const message = messageOf(request, fail);
|
|
794
|
-
checkFits(id, {
|
|
929
|
+
checkFits(id, {
|
|
930
|
+
system: request.system,
|
|
931
|
+
messages: [message],
|
|
932
|
+
}, request.output, budget);
|
|
795
933
|
const { signal } = request;
|
|
796
934
|
throwIfAborted(signal, fail);
|
|
797
935
|
const run = await prepareRun(request.system, fail);
|
|
@@ -800,14 +938,31 @@ export function claudeCode(options) {
|
|
|
800
938
|
try {
|
|
801
939
|
throwIfAborted(signal, fail);
|
|
802
940
|
const environment = childEnv(env, auth, apiKey, run.tmp);
|
|
803
|
-
await requireVersion({
|
|
941
|
+
await requireVersion({
|
|
942
|
+
command,
|
|
943
|
+
args: [],
|
|
944
|
+
cwd: run.cwd,
|
|
945
|
+
env: environment,
|
|
946
|
+
live,
|
|
947
|
+
}, signal, fail);
|
|
804
948
|
throwIfAborted(signal, fail);
|
|
805
|
-
const launch = {
|
|
949
|
+
const launch = {
|
|
950
|
+
command,
|
|
951
|
+
args: claudeArgs({ model, auth, structuredOutput, args }, run.systemFile, request.output.schema),
|
|
952
|
+
cwd: run.cwd,
|
|
953
|
+
env: environment,
|
|
954
|
+
live,
|
|
955
|
+
};
|
|
806
956
|
const finished = await runChild(launch, message.content, signal, fail);
|
|
807
957
|
throwIfAborted(signal, fail);
|
|
808
958
|
if (finished.overflow)
|
|
809
959
|
throw fail('invalid-response', 'reply is over 16 MiB');
|
|
810
|
-
return readResult(finished.run, {
|
|
960
|
+
return readResult(finished.run, {
|
|
961
|
+
id,
|
|
962
|
+
model,
|
|
963
|
+
structuredOutput,
|
|
964
|
+
redact: credentialValues(environment),
|
|
965
|
+
});
|
|
811
966
|
}
|
|
812
967
|
finally {
|
|
813
968
|
await removeRun(run.dir);
|