@tobi-or-not/quorum 0.1.0 → 0.2.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 +4 -0
- package/dist/cli/bin.js +9 -1
- package/dist/cli/main.js +49 -12
- package/dist/cli/print-config.js +38 -9
- package/dist/cli/render.js +18 -6
- 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.js +163 -37
- 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.js +65 -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.js +20 -5
- package/dist/core/quorum.js +128 -33
- package/dist/core/sanitize.js +3 -1
- 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 +5 -4
package/dist/config/resolve.js
CHANGED
|
@@ -63,15 +63,36 @@ function atPresetOrigin(presetName, preset, baseUrl) {
|
|
|
63
63
|
/** The issue of a key variable that is not set. The variable is named only when it is an upper-case name. */
|
|
64
64
|
function unsetKey(provider, variable) {
|
|
65
65
|
return VARIABLE_NAME.test(variable)
|
|
66
|
-
? {
|
|
67
|
-
|
|
66
|
+
? {
|
|
67
|
+
path: configPath(['providers', provider]),
|
|
68
|
+
message: `${variable} is not set`,
|
|
69
|
+
}
|
|
70
|
+
: {
|
|
71
|
+
path: configPath(['providers', provider, 'apiKeyEnv']),
|
|
72
|
+
message: 'names a variable that is not set',
|
|
73
|
+
};
|
|
68
74
|
}
|
|
69
|
-
const NO_KEY = Object.freeze({
|
|
70
|
-
|
|
75
|
+
const NO_KEY = Object.freeze({
|
|
76
|
+
apiKey: undefined,
|
|
77
|
+
issues: Object.freeze([]),
|
|
78
|
+
warnings: Object.freeze([]),
|
|
79
|
+
});
|
|
80
|
+
const NO_CLAUDE = Object.freeze({
|
|
81
|
+
issues: Object.freeze([]),
|
|
82
|
+
warnings: Object.freeze([]),
|
|
83
|
+
});
|
|
71
84
|
/** The key a variable holds, or the issue that it is not set. */
|
|
72
85
|
function readVariable(name, variable, env) {
|
|
73
86
|
const apiKey = envValue(env, variable);
|
|
74
|
-
return {
|
|
87
|
+
return {
|
|
88
|
+
apiKey,
|
|
89
|
+
issues: apiKey === undefined
|
|
90
|
+
? [
|
|
91
|
+
unsetKey(name, variable),
|
|
92
|
+
]
|
|
93
|
+
: [],
|
|
94
|
+
warnings: [],
|
|
95
|
+
};
|
|
75
96
|
}
|
|
76
97
|
/**
|
|
77
98
|
* The key of a spec that a profile uses: its own `apiKey` (an empty one is unset, as a blank variable is),
|
|
@@ -82,19 +103,36 @@ function readVariable(name, variable, env) {
|
|
|
82
103
|
*/
|
|
83
104
|
function readKey(name, fields, env, presetKey) {
|
|
84
105
|
// A key variable no mode would send is refused where it is written, and never read.
|
|
85
|
-
if (isClaudeCode(fields.protocol) && fields.apiKeyEnv !== undefined && fields.auth !== 'api-key')
|
|
86
|
-
return {
|
|
106
|
+
if (isClaudeCode(fields.protocol) && fields.apiKeyEnv !== undefined && fields.auth !== 'api-key') {
|
|
107
|
+
return {
|
|
108
|
+
apiKey: undefined,
|
|
109
|
+
issues: [
|
|
110
|
+
{
|
|
111
|
+
path: configPath(['providers', name, 'apiKeyEnv']),
|
|
112
|
+
message: 'is set, but auth is not api-key',
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
warnings: [],
|
|
116
|
+
};
|
|
117
|
+
}
|
|
87
118
|
if (fields.apiKey !== undefined && fields.apiKey !== '')
|
|
88
119
|
return { apiKey: fields.apiKey, issues: [], warnings: [] };
|
|
89
120
|
if (fields.apiKeyEnv !== undefined)
|
|
90
121
|
return readVariable(name, fields.apiKeyEnv, env);
|
|
91
|
-
if (isClaudeCode(fields.protocol))
|
|
92
|
-
return fields.auth === 'api-key'
|
|
122
|
+
if (isClaudeCode(fields.protocol)) {
|
|
123
|
+
return fields.auth === 'api-key'
|
|
124
|
+
? readVariable(name, CLAUDE_KEY_VARIABLE, env)
|
|
125
|
+
: NO_KEY;
|
|
126
|
+
}
|
|
93
127
|
const preset = presetOf(fields.preset);
|
|
94
128
|
const variable = preset?.apiKeyEnv;
|
|
95
129
|
if (preset === undefined || variable === undefined)
|
|
96
130
|
return NO_KEY;
|
|
97
|
-
const notSent = (reason, remedy) => ({
|
|
131
|
+
const notSent = (reason, remedy) => ({
|
|
132
|
+
apiKey: undefined,
|
|
133
|
+
issues: [],
|
|
134
|
+
warnings: [`${configPath(['providers', name, 'baseUrl'])}: ${reason}, so ${variable} is not sent: set ${remedy} to send a key`],
|
|
135
|
+
});
|
|
98
136
|
if (!presetKey)
|
|
99
137
|
return notSent('is not the endpoint of the research provider', VOTER_KEY);
|
|
100
138
|
if (!atPresetOrigin(fields.preset, preset, baseUrlOf(fields)))
|
|
@@ -112,7 +150,10 @@ function cleartextWarnings(name, apiKey, baseUrl) {
|
|
|
112
150
|
function readClaude(name, fields, env, variable) {
|
|
113
151
|
if (!isClaudeCode(fields.protocol))
|
|
114
152
|
return NO_CLAUDE;
|
|
115
|
-
return {
|
|
153
|
+
return {
|
|
154
|
+
...claudeCodeCommand(name, fields, variable, env),
|
|
155
|
+
warnings: billingWarnings(name, fields, env),
|
|
156
|
+
};
|
|
116
157
|
}
|
|
117
158
|
/**
|
|
118
159
|
* One provider's spec, checked by the builder. A spec in use gets its key first, and its `claude` when it
|
|
@@ -122,12 +163,27 @@ function readClaude(name, fields, env, variable) {
|
|
|
122
163
|
*/
|
|
123
164
|
function checkSpec(name, entry, inUse, env, presetKey, command) {
|
|
124
165
|
const fields = ownFields(entry);
|
|
125
|
-
const key = inUse
|
|
126
|
-
|
|
166
|
+
const key = inUse
|
|
167
|
+
? readKey(name, fields, env, presetKey)
|
|
168
|
+
: NO_KEY;
|
|
169
|
+
const claude = inUse
|
|
170
|
+
? readClaude(name, fields, env, command)
|
|
171
|
+
: NO_CLAUDE;
|
|
127
172
|
const { apiKeyEnv: _apiKeyEnv, apiKey: _apiKey, ...rest } = fields;
|
|
128
173
|
// The builder refuses a spec with no model, and a spec it refuses is never kept.
|
|
129
|
-
const spec = {
|
|
130
|
-
|
|
174
|
+
const spec = {
|
|
175
|
+
...rest,
|
|
176
|
+
...key.apiKey === undefined
|
|
177
|
+
? {}
|
|
178
|
+
: { apiKey: key.apiKey },
|
|
179
|
+
...claude.command === undefined
|
|
180
|
+
? {}
|
|
181
|
+
: { command: claude.command },
|
|
182
|
+
};
|
|
183
|
+
const issues = [
|
|
184
|
+
...key.issues,
|
|
185
|
+
...claude.issues,
|
|
186
|
+
];
|
|
131
187
|
try {
|
|
132
188
|
buildProvider({ ...spec, id: name }, { env });
|
|
133
189
|
}
|
|
@@ -136,7 +192,15 @@ function checkSpec(name, entry, inUse, env, presetKey, command) {
|
|
|
136
192
|
throw error;
|
|
137
193
|
issues.push(...error.issues.map(({ path, message }) => ({ path: `providers.${path}`, message })));
|
|
138
194
|
}
|
|
139
|
-
return {
|
|
195
|
+
return {
|
|
196
|
+
spec,
|
|
197
|
+
issues,
|
|
198
|
+
warnings: [
|
|
199
|
+
...key.warnings,
|
|
200
|
+
...claude.warnings,
|
|
201
|
+
...cleartextWarnings(name, key.apiKey, baseUrlOf(fields)),
|
|
202
|
+
],
|
|
203
|
+
};
|
|
140
204
|
}
|
|
141
205
|
/** A copy of a spec, field by field, so the config's own objects stay out of the resolved config; or an issue at each field that cannot be copied, which never repeats what it holds. */
|
|
142
206
|
function copySpec(name, spec) {
|
|
@@ -144,13 +208,23 @@ function copySpec(name, spec) {
|
|
|
144
208
|
const issues = [];
|
|
145
209
|
for (const [field, value] of Object.entries(spec)) {
|
|
146
210
|
try {
|
|
147
|
-
copies.push([
|
|
211
|
+
copies.push([
|
|
212
|
+
field,
|
|
213
|
+
structuredClone(value),
|
|
214
|
+
]);
|
|
148
215
|
}
|
|
149
216
|
catch {
|
|
150
|
-
issues.push({
|
|
217
|
+
issues.push({
|
|
218
|
+
path: configPath(['providers', name, field]),
|
|
219
|
+
message: 'cannot be copied',
|
|
220
|
+
});
|
|
151
221
|
}
|
|
152
222
|
}
|
|
153
|
-
return issues.length > 0
|
|
223
|
+
return issues.length > 0
|
|
224
|
+
? { issues }
|
|
225
|
+
: {
|
|
226
|
+
copy: Object.fromEntries(copies),
|
|
227
|
+
};
|
|
154
228
|
}
|
|
155
229
|
/** Every provider of the config checked by the builder, and the specs and objects a profile uses. `withheld` names the provider that is never sent its preset's key variable, `command` is `QUORUM_CLAUDE_CODE_COMMAND`'s. An issue two providers share (the variable names no file) is reported once. */
|
|
156
230
|
function checkProviders(providers, inUse, env, withheld, command) {
|
|
@@ -186,9 +260,14 @@ function reportMissingModel(issues, builtIn, reportedVoter) {
|
|
|
186
260
|
return issues.flatMap((issue) => {
|
|
187
261
|
if (issue.message !== 'is required')
|
|
188
262
|
return [issue];
|
|
189
|
-
if (builtIn && issue.path === `providers.${DEFAULT_NAME}.model`)
|
|
190
|
-
return [
|
|
191
|
-
|
|
263
|
+
if (builtIn && issue.path === `providers.${DEFAULT_NAME}.model`) {
|
|
264
|
+
return [
|
|
265
|
+
{ path: '', message: NO_MODEL },
|
|
266
|
+
];
|
|
267
|
+
}
|
|
268
|
+
return reportedVoter !== undefined && issue.path === `providers.${reportedVoter}.model`
|
|
269
|
+
? []
|
|
270
|
+
: [issue];
|
|
192
271
|
});
|
|
193
272
|
}
|
|
194
273
|
/** The issue of a role whose variables name a model or an endpoint and set no context size: the variable to set, and why. */
|
|
@@ -207,29 +286,48 @@ function contextOfRole(role) {
|
|
|
207
286
|
function reportMissingContext(issues, { builtIn, roles, derived, layer }) {
|
|
208
287
|
const voterNames = namesModelOrEndpoint(layer.voter);
|
|
209
288
|
const shared = derived !== undefined && !voterNames && !setsContextSize(layer.voter);
|
|
210
|
-
const voterOwn = roles.voter !== roles.research && roles.voter !== derived
|
|
289
|
+
const voterOwn = roles.voter !== roles.research && roles.voter !== derived
|
|
290
|
+
? roles.voter
|
|
291
|
+
: undefined;
|
|
211
292
|
return issues.flatMap((issue) => {
|
|
212
293
|
const at = (name) => name !== undefined && issue.path === `providers.${name}.contextTokens`;
|
|
213
294
|
if (shared && at(derived))
|
|
214
295
|
return [];
|
|
215
296
|
if (issue.message !== CONTEXT_REQUIRED)
|
|
216
297
|
return [issue];
|
|
217
|
-
if (builtIn && at(DEFAULT_NAME))
|
|
218
|
-
return [
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
298
|
+
if (builtIn && at(DEFAULT_NAME)) {
|
|
299
|
+
return [
|
|
300
|
+
{ path: '', message: NO_CONTEXT },
|
|
301
|
+
];
|
|
302
|
+
}
|
|
303
|
+
if (at(derived) || at(voterOwn)) {
|
|
304
|
+
return voterNames
|
|
305
|
+
? [
|
|
306
|
+
contextOfRole('voter'),
|
|
307
|
+
]
|
|
308
|
+
: [issue];
|
|
309
|
+
}
|
|
310
|
+
if (at(roles.research)) {
|
|
311
|
+
return namesModelOrEndpoint(layer.research)
|
|
312
|
+
? [
|
|
313
|
+
contextOfRole('research'),
|
|
314
|
+
]
|
|
315
|
+
: [issue];
|
|
316
|
+
}
|
|
223
317
|
return [issue];
|
|
224
318
|
});
|
|
225
319
|
}
|
|
226
320
|
/** The provider of a config that defines none, chosen from the research variables before they are applied. */
|
|
227
321
|
function builtInProvider(research) {
|
|
228
|
-
return setsOnlyBaseUrl(research)
|
|
322
|
+
return setsOnlyBaseUrl(research)
|
|
323
|
+
? BASE_URL_PROVIDER
|
|
324
|
+
: DEFAULT_PROVIDER;
|
|
229
325
|
}
|
|
230
326
|
/** The origin of a provider's settings, as endpoints are compared; none for a provider object. */
|
|
231
327
|
function originOf(entry) {
|
|
232
|
-
return entry === undefined || isProvider(entry)
|
|
328
|
+
return entry === undefined || isProvider(entry)
|
|
329
|
+
? undefined
|
|
330
|
+
: httpOrigin(baseUrlOf(ownFields(entry)));
|
|
233
331
|
}
|
|
234
332
|
/** The issues of a selection's ids, each at most once and never naming an id: one for a form, one for a repeat. */
|
|
235
333
|
function selectionIssues({ ids, path }) {
|
|
@@ -242,8 +340,13 @@ function selectionIssues({ ids, path }) {
|
|
|
242
340
|
}
|
|
243
341
|
/** The providers research and the voter are bound to: `roles`, else the only provider, else research. Or the issues of the roles. */
|
|
244
342
|
function bindRoles(roles, names) {
|
|
245
|
-
const named = {
|
|
246
|
-
|
|
343
|
+
const named = {
|
|
344
|
+
research: own(roles, 'research'),
|
|
345
|
+
voter: own(roles, 'voter'),
|
|
346
|
+
};
|
|
347
|
+
const research = named.research ?? (names.length === 1
|
|
348
|
+
? names[0]
|
|
349
|
+
: undefined);
|
|
247
350
|
const voter = named.voter ?? research;
|
|
248
351
|
const issues = [];
|
|
249
352
|
if (research === undefined)
|
|
@@ -252,7 +355,12 @@ function bindRoles(roles, names) {
|
|
|
252
355
|
issues.push(unknownName('roles.research', 'provider', research, names));
|
|
253
356
|
if (named.voter !== undefined && !names.includes(named.voter))
|
|
254
357
|
issues.push(unknownName('roles.voter', 'provider', named.voter, names));
|
|
255
|
-
return research === undefined || voter === undefined || issues.length > 0
|
|
358
|
+
return research === undefined || voter === undefined || issues.length > 0
|
|
359
|
+
? { issues }
|
|
360
|
+
: {
|
|
361
|
+
roles: { research, voter },
|
|
362
|
+
issues,
|
|
363
|
+
};
|
|
256
364
|
}
|
|
257
365
|
/**
|
|
258
366
|
* Resolve a config and the environment into the providers and profiles a run uses: the `QUORUM_*`
|
|
@@ -275,30 +383,62 @@ export function resolveConfig(input, options) {
|
|
|
275
383
|
const layer = readEnv(env);
|
|
276
384
|
const defined = Object.entries(own(input, 'providers') ?? {});
|
|
277
385
|
const builtIn = defined.length === 0;
|
|
278
|
-
const entries = builtIn
|
|
386
|
+
const entries = builtIn
|
|
387
|
+
? [
|
|
388
|
+
[
|
|
389
|
+
DEFAULT_NAME,
|
|
390
|
+
builtInProvider(layer.research),
|
|
391
|
+
],
|
|
392
|
+
]
|
|
393
|
+
: defined;
|
|
279
394
|
const bound = bindRoles(own(input, 'roles'), entries.map(([name]) => name));
|
|
280
|
-
const refused = selection === undefined
|
|
281
|
-
|
|
282
|
-
|
|
395
|
+
const refused = selection === undefined
|
|
396
|
+
? []
|
|
397
|
+
: selectionIssues(selection);
|
|
398
|
+
if (layer.issues.length > 0 || refused.length > 0 || bound.issues.length > 0 || bound.roles === undefined) {
|
|
399
|
+
throw new ConfigError([
|
|
400
|
+
...layer.issues,
|
|
401
|
+
...refused,
|
|
402
|
+
...bound.issues,
|
|
403
|
+
]);
|
|
404
|
+
}
|
|
283
405
|
// The environment layer's profiles are read as own fields, so a polluted `Object.prototype` selects none
|
|
284
406
|
// through `QUORUM_PROFILES`; `options.selection` is read directly, as the caller gave it.
|
|
285
407
|
const ids = own(layer, 'profiles');
|
|
286
408
|
const applied = applyRoleOverrides(Object.fromEntries(entries), bound.roles, layer, builtIn);
|
|
287
409
|
const profiles = resolveProfiles({
|
|
288
410
|
profiles: own(input, 'profiles'),
|
|
289
|
-
selection: selection ?? (ids === undefined
|
|
411
|
+
selection: selection ?? (ids === undefined
|
|
412
|
+
? undefined
|
|
413
|
+
: { ids, path: 'QUORUM_PROFILES' }),
|
|
290
414
|
roles: applied.roles,
|
|
291
415
|
providers: Object.keys(applied.providers),
|
|
292
416
|
});
|
|
293
|
-
const inUse = new Set(profiles.profiles.flatMap(({ research, voters }) => [
|
|
417
|
+
const inUse = new Set(profiles.profiles.flatMap(({ research, voters }) => [
|
|
418
|
+
research,
|
|
419
|
+
...Object.values(voters),
|
|
420
|
+
]));
|
|
294
421
|
// The voter the variables derived is the one the role moved to. On another origin than research's, with no
|
|
295
422
|
// preset of its own, it is never sent a preset's key variable: that is research's key by another name (ADR-0013).
|
|
296
|
-
const derived = applied.roles.voter === bound.roles.voter
|
|
423
|
+
const derived = applied.roles.voter === bound.roles.voter
|
|
424
|
+
? undefined
|
|
425
|
+
: applied.roles.voter;
|
|
297
426
|
const elsewhere = derived !== undefined && own(layer.voter, 'preset') === undefined && originOf(applied.providers[derived]) !== originOf(applied.providers[applied.roles.research]);
|
|
298
427
|
const checked = checkProviders(applied.providers, inUse, env, elsewhere ? derived : undefined, own(layer, 'claudeCodeCommand'));
|
|
299
|
-
const reportedVoter = applied.issues.some(issue => issue.path === VOTER_MODEL)
|
|
300
|
-
|
|
301
|
-
|
|
428
|
+
const reportedVoter = applied.issues.some(issue => issue.path === VOTER_MODEL)
|
|
429
|
+
? derived
|
|
430
|
+
: undefined;
|
|
431
|
+
const reported = reportMissingContext(reportMissingModel(checked.issues, builtIn, reportedVoter), {
|
|
432
|
+
builtIn,
|
|
433
|
+
roles: applied.roles,
|
|
434
|
+
derived,
|
|
435
|
+
layer,
|
|
436
|
+
});
|
|
437
|
+
const issues = [
|
|
438
|
+
...applied.issues,
|
|
439
|
+
...profiles.issues,
|
|
440
|
+
...atVariables(reported, applied.roles, layer),
|
|
441
|
+
];
|
|
302
442
|
if (issues.length > 0)
|
|
303
443
|
throw new ConfigError(issues);
|
|
304
444
|
const concurrency = own(input, 'concurrency');
|
|
@@ -306,9 +446,16 @@ export function resolveConfig(input, options) {
|
|
|
306
446
|
const resolved = Object.freeze({
|
|
307
447
|
providers: deepFreeze(Object.fromEntries(checked.providers)),
|
|
308
448
|
profiles: Object.freeze([...profiles.profiles]),
|
|
309
|
-
...concurrency === undefined
|
|
310
|
-
|
|
311
|
-
|
|
449
|
+
...concurrency === undefined
|
|
450
|
+
? {}
|
|
451
|
+
: { concurrency },
|
|
452
|
+
...maxFindings === undefined
|
|
453
|
+
? {}
|
|
454
|
+
: { maxFindings },
|
|
455
|
+
warnings: Object.freeze([
|
|
456
|
+
...layer.warnings,
|
|
457
|
+
...checked.warnings,
|
|
458
|
+
]),
|
|
312
459
|
});
|
|
313
460
|
RESOLVED.add(resolved);
|
|
314
461
|
return resolved;
|
package/dist/config/schema.js
CHANGED
|
@@ -9,7 +9,13 @@ const NAME = /^[a-z][a-z0-9_-]*$/;
|
|
|
9
9
|
// eslint-disable-next-line regexp/use-ignore-case, regexp/prefer-w
|
|
10
10
|
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
11
11
|
/** Every protocol, as a record, so the type checker reports a protocol this list misses. */
|
|
12
|
-
const PROTOCOLS = {
|
|
12
|
+
const PROTOCOLS = {
|
|
13
|
+
'openai-compatible': true,
|
|
14
|
+
anthropic: true,
|
|
15
|
+
gemini: true,
|
|
16
|
+
ollama: true,
|
|
17
|
+
'claude-code': true,
|
|
18
|
+
};
|
|
13
19
|
const STRUCTURED_OUTPUTS = { prompt: true, json: true, schema: true };
|
|
14
20
|
const VERSION = 'must be 1, the only config version this quorum reads: upgrade quorum to read a later one';
|
|
15
21
|
const name = z.string().regex(NAME);
|
|
@@ -18,7 +24,14 @@ const roles = z.strictObject({
|
|
|
18
24
|
voter: name.optional().describe('The provider every lens votes with, unless the lens names its own'),
|
|
19
25
|
});
|
|
20
26
|
const auth = z.union([
|
|
21
|
-
z.enum([
|
|
27
|
+
z.enum([
|
|
28
|
+
'bearer',
|
|
29
|
+
'x-api-key',
|
|
30
|
+
'none',
|
|
31
|
+
'inherit',
|
|
32
|
+
'login',
|
|
33
|
+
'api-key',
|
|
34
|
+
]),
|
|
22
35
|
z.strictObject({
|
|
23
36
|
header: z.string().describe('The header that carries the key'),
|
|
24
37
|
prefix: z.string().optional().describe('The text sent before the key in that header'),
|
|
@@ -45,13 +58,19 @@ const providerShape = {
|
|
|
45
58
|
};
|
|
46
59
|
const prompt = z.union([
|
|
47
60
|
z.string(),
|
|
48
|
-
z.strictObject({
|
|
61
|
+
z.strictObject({
|
|
62
|
+
file: z.string().describe('The file that holds the template, relative to the directory of the config file'),
|
|
63
|
+
}),
|
|
49
64
|
], { error: 'must be a string or { file }' });
|
|
50
65
|
const rule = z.union([
|
|
51
66
|
z.enum(['unanimous', 'majority', 'any', 'off']),
|
|
52
67
|
z.literal(false),
|
|
53
|
-
z.strictObject({
|
|
54
|
-
|
|
68
|
+
z.strictObject({
|
|
69
|
+
minVotes: z.int().min(1).describe('The kept votes the level needs'),
|
|
70
|
+
}),
|
|
71
|
+
z.strictObject({
|
|
72
|
+
minRatio: z.number().gt(0).lte(1).describe('The share of the lenses whose kept votes the level needs, above 0 and at most 1'),
|
|
73
|
+
}),
|
|
55
74
|
], { error: 'must be unanimous, majority, any, off, false, { minVotes } or { minRatio }' });
|
|
56
75
|
/** A profile entry, around the schema of one prompt: a string or `{ file }` in a file, a string only in code. */
|
|
57
76
|
function profileSchema(promptForm) {
|
|
@@ -111,7 +130,10 @@ const providerObject = z.custom(value => typeof value === 'object' && value !==
|
|
|
111
130
|
ctx.issues.push({ code: 'custom', message: 'complete is not a function', input: object });
|
|
112
131
|
return z.NEVER;
|
|
113
132
|
}
|
|
114
|
-
return snapshot({
|
|
133
|
+
return snapshot({
|
|
134
|
+
model,
|
|
135
|
+
complete: request => Reflect.apply(complete, object, [request]),
|
|
136
|
+
});
|
|
115
137
|
});
|
|
116
138
|
/**
|
|
117
139
|
* `entry` named `id`, the config's key for it, when it is a snapshot the schema made of a provider object: a
|
|
@@ -151,7 +173,15 @@ export function configJsonSchema() {
|
|
|
151
173
|
return z.toJSONSchema(configFileSchema, { target: 'draft-07', io: 'input' });
|
|
152
174
|
}
|
|
153
175
|
/** What each type zod expects is called in a message. */
|
|
154
|
-
const TYPE_WORDS = {
|
|
176
|
+
const TYPE_WORDS = {
|
|
177
|
+
string: 'a string',
|
|
178
|
+
number: 'a number',
|
|
179
|
+
int: 'an integer',
|
|
180
|
+
boolean: 'true or false',
|
|
181
|
+
object: 'an object',
|
|
182
|
+
record: 'an object',
|
|
183
|
+
array: 'an array',
|
|
184
|
+
};
|
|
155
185
|
/**
|
|
156
186
|
* The project's wording of a zod issue, as `safeParse` takes it in its `error` option. A schema's own
|
|
157
187
|
* message (the version, a union) comes first, since zod applies it before this one. Unknown keys are
|
|
@@ -164,19 +194,29 @@ function wording(issue) {
|
|
|
164
194
|
return 'is required';
|
|
165
195
|
if (issue.expected === 'number' && typeof issue.input === 'number')
|
|
166
196
|
return 'must be a finite number';
|
|
167
|
-
return Object.hasOwn(TYPE_WORDS, issue.expected)
|
|
197
|
+
return Object.hasOwn(TYPE_WORDS, issue.expected)
|
|
198
|
+
? `must be ${TYPE_WORDS[issue.expected]}`
|
|
199
|
+
: undefined;
|
|
168
200
|
case 'invalid_value':
|
|
169
|
-
return `must be ${issue.values.length > 1
|
|
201
|
+
return `must be ${issue.values.length > 1
|
|
202
|
+
? 'one of '
|
|
203
|
+
: ''}${wordList(issue.values.map(String))}`;
|
|
170
204
|
case 'too_small':
|
|
171
205
|
// The one bound on a list is the lenses' one entry.
|
|
172
206
|
if (issue.origin === 'array')
|
|
173
207
|
return 'must not be empty';
|
|
174
|
-
return issue.inclusive
|
|
208
|
+
return issue.inclusive
|
|
209
|
+
? `must be at least ${issue.minimum}`
|
|
210
|
+
: `must be greater than ${issue.minimum}`;
|
|
175
211
|
case 'too_big':
|
|
176
|
-
return issue.inclusive
|
|
212
|
+
return issue.inclusive
|
|
213
|
+
? `must be at most ${issue.maximum}`
|
|
214
|
+
: `must be less than ${issue.maximum}`;
|
|
177
215
|
case 'invalid_format':
|
|
178
216
|
// No pattern of the schema has flags, so the pattern is the text between its slashes.
|
|
179
|
-
return issue.format === 'regex' && typeof issue.pattern === 'string'
|
|
217
|
+
return issue.format === 'regex' && typeof issue.pattern === 'string'
|
|
218
|
+
? `must match ${issue.pattern.slice(1, -1)}`
|
|
219
|
+
: undefined;
|
|
180
220
|
case 'invalid_key':
|
|
181
221
|
return `is a key that ${issue.issues[0]?.message ?? 'is not allowed'}`;
|
|
182
222
|
case 'invalid_union':
|
|
@@ -190,7 +230,9 @@ export function configPath(segments, prefix) {
|
|
|
190
230
|
const path = issuePath(segments);
|
|
191
231
|
if (prefix === undefined)
|
|
192
232
|
return path;
|
|
193
|
-
return path === ''
|
|
233
|
+
return path === ''
|
|
234
|
+
? prefix
|
|
235
|
+
: `${prefix}: ${path}`;
|
|
194
236
|
}
|
|
195
237
|
const belowRoot = (issue) => issue.path.length > 0;
|
|
196
238
|
const belowRootOrUnknown = (issue) => belowRoot(issue) || issue.code === 'unrecognized_keys';
|
|
@@ -208,15 +250,36 @@ function shapedOption(options) {
|
|
|
208
250
|
return undefined;
|
|
209
251
|
}
|
|
210
252
|
function issuesOf(issue, base, prefix) {
|
|
211
|
-
const path = [
|
|
253
|
+
const path = [
|
|
254
|
+
...base,
|
|
255
|
+
...issue.path,
|
|
256
|
+
];
|
|
212
257
|
if (issue.code === 'unrecognized_keys') {
|
|
213
|
-
const known = issue.keys.filter(key => isSafeSegment(key)).map(key => ({
|
|
214
|
-
|
|
258
|
+
const known = issue.keys.filter(key => isSafeSegment(key)).map(key => ({
|
|
259
|
+
path: configPath([...path, key], prefix),
|
|
260
|
+
message: 'is not a known field',
|
|
261
|
+
}));
|
|
262
|
+
return known.length < issue.keys.length
|
|
263
|
+
? [
|
|
264
|
+
...known,
|
|
265
|
+
{
|
|
266
|
+
path: configPath(path, prefix),
|
|
267
|
+
message: 'holds a field that is not known',
|
|
268
|
+
},
|
|
269
|
+
]
|
|
270
|
+
: known;
|
|
215
271
|
}
|
|
216
|
-
const shaped = issue.code === 'invalid_union'
|
|
272
|
+
const shaped = issue.code === 'invalid_union'
|
|
273
|
+
? shapedOption(issue.errors)
|
|
274
|
+
: undefined;
|
|
217
275
|
if (shaped !== undefined)
|
|
218
276
|
return shaped.flatMap(nested => issuesOf(nested, path, prefix));
|
|
219
|
-
return [
|
|
277
|
+
return [
|
|
278
|
+
{
|
|
279
|
+
path: configPath(path, prefix),
|
|
280
|
+
message: issue.message,
|
|
281
|
+
},
|
|
282
|
+
];
|
|
220
283
|
}
|
|
221
284
|
/**
|
|
222
285
|
* The issues of a failed parse, each at its path after `prefix`. An unknown key is one issue per key at
|
|
@@ -234,7 +297,11 @@ export function configIssues(error, prefix) {
|
|
|
234
297
|
*/
|
|
235
298
|
export function checkConfig(schema, value, prefix) {
|
|
236
299
|
const result = schema.safeParse(value, { error: wording });
|
|
237
|
-
return result.success
|
|
300
|
+
return result.success
|
|
301
|
+
? { config: result.data }
|
|
302
|
+
: {
|
|
303
|
+
issues: configIssues(result.error, prefix),
|
|
304
|
+
};
|
|
238
305
|
}
|
|
239
306
|
/**
|
|
240
307
|
* Check `value` against a config schema, with the project's wording.
|
|
@@ -255,7 +322,9 @@ function editDistance(from, to) {
|
|
|
255
322
|
for (let row = 1; row <= from.length; row++) {
|
|
256
323
|
const current = [row];
|
|
257
324
|
for (let column = 1; column <= to.length; column++) {
|
|
258
|
-
const substitution = previous[column - 1] + (from[row - 1] === to[column - 1]
|
|
325
|
+
const substitution = previous[column - 1] + (from[row - 1] === to[column - 1]
|
|
326
|
+
? 0
|
|
327
|
+
: 1);
|
|
259
328
|
current.push(Math.min(previous[column] + 1, current[column - 1] + 1, substitution));
|
|
260
329
|
}
|
|
261
330
|
previous = current;
|
|
@@ -282,11 +351,17 @@ function closestName(name, defined) {
|
|
|
282
351
|
*/
|
|
283
352
|
export function unknownName(path, kind, name, defined) {
|
|
284
353
|
const safe = isSafeSegment(name);
|
|
285
|
-
const suggestion = safe
|
|
354
|
+
const suggestion = safe
|
|
355
|
+
? closestName(name, defined)
|
|
356
|
+
: undefined;
|
|
286
357
|
if (suggestion !== undefined)
|
|
287
358
|
return { path, message: `unknown ${kind} "${name}" (did you mean "${suggestion}"?)` };
|
|
288
|
-
const subject = safe
|
|
359
|
+
const subject = safe
|
|
360
|
+
? `unknown ${kind} "${name}"`
|
|
361
|
+
: `names an unknown ${kind}`;
|
|
289
362
|
if (defined.length === 0)
|
|
290
363
|
return { path, message: subject };
|
|
291
|
-
return { path, message: `${subject}: the ${kind}s are ${wordList(defined.map(known => isSafeSegment(known)
|
|
364
|
+
return { path, message: `${subject}: the ${kind}s are ${wordList(defined.map(known => isSafeSegment(known)
|
|
365
|
+
? known
|
|
366
|
+
: '*'), 'and')}` };
|
|
292
367
|
}
|