@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/gemini.js
CHANGED
|
@@ -14,7 +14,9 @@ const BLOCK_REASON = new RegExp(`^[A-Z_]{1,${MAX_BLOCK_REASON}}$`);
|
|
|
14
14
|
const RULES = {
|
|
15
15
|
reservedBodyKeys: ['contents', 'systemInstruction'],
|
|
16
16
|
toolBodyKeys: ['tools', 'toolConfig', 'cachedContent'],
|
|
17
|
-
replyLimitPaths: [
|
|
17
|
+
replyLimitPaths: [
|
|
18
|
+
['generationConfig', 'maxOutputTokens'],
|
|
19
|
+
],
|
|
18
20
|
fixedHeaders: [],
|
|
19
21
|
structuredOutputs: ['prompt', 'json', 'schema'],
|
|
20
22
|
};
|
|
@@ -23,7 +25,14 @@ const GENERATION_CONFIG = 'generationConfig';
|
|
|
23
25
|
/** The finish reasons of a reply cut off by the token limit: `CONTINUATION` stops at the per-request limit before the model is done. */
|
|
24
26
|
const CUT_OFF = new Set(['MAX_TOKENS', 'CONTINUATION']);
|
|
25
27
|
/** The finish reasons of a reply the safety filter stopped. */
|
|
26
|
-
const FILTERED = new Set([
|
|
28
|
+
const FILTERED = new Set([
|
|
29
|
+
'SAFETY',
|
|
30
|
+
'RECITATION',
|
|
31
|
+
'BLOCKLIST',
|
|
32
|
+
'PROHIBITED_CONTENT',
|
|
33
|
+
'SPII',
|
|
34
|
+
'IMAGE_SAFETY',
|
|
35
|
+
]);
|
|
27
36
|
/** A `key` query parameter, in any letter case, would put the key in the URL: it travels only in the header `auth` names. */
|
|
28
37
|
function keyParameterIssues(id, query) {
|
|
29
38
|
return Object.keys(query)
|
|
@@ -42,7 +51,11 @@ function generationConfigIssues(id, extraBody) {
|
|
|
42
51
|
return Object.entries(extraBody).flatMap(([key, value]) => {
|
|
43
52
|
if (bodyKeyForm(key) !== bodyKeyForm(GENERATION_CONFIG))
|
|
44
53
|
return [];
|
|
45
|
-
return isRecord(value)
|
|
54
|
+
return isRecord(value)
|
|
55
|
+
? spellingIssues(id, key, GENERATION_CONFIG)
|
|
56
|
+
: [
|
|
57
|
+
{ path: `${id}.${issuePath(['extraBody', key])}`, message: 'must be an object' },
|
|
58
|
+
];
|
|
46
59
|
});
|
|
47
60
|
}
|
|
48
61
|
/** The generation config of a request, or undefined when it would be empty. */
|
|
@@ -50,8 +63,12 @@ function generationConfig({ temperature, maxTokens, structuredOutput }, output)
|
|
|
50
63
|
return unlessEmpty({
|
|
51
64
|
temperature: temperature ?? undefined,
|
|
52
65
|
maxOutputTokens: maxTokens,
|
|
53
|
-
responseMimeType: structuredOutput === 'prompt'
|
|
54
|
-
|
|
66
|
+
responseMimeType: structuredOutput === 'prompt'
|
|
67
|
+
? undefined
|
|
68
|
+
: 'application/json',
|
|
69
|
+
responseJsonSchema: structuredOutput === 'schema'
|
|
70
|
+
? output.schema
|
|
71
|
+
: undefined,
|
|
55
72
|
});
|
|
56
73
|
}
|
|
57
74
|
/**
|
|
@@ -62,12 +79,18 @@ function readReply(reply, { id, model }, redact) {
|
|
|
62
79
|
const fail = replyFailure(id);
|
|
63
80
|
if (!isRecord(reply))
|
|
64
81
|
throw fail('invalid-response', 'reply is not a JSON object');
|
|
65
|
-
const blocked = isRecord(reply.promptFeedback)
|
|
82
|
+
const blocked = isRecord(reply.promptFeedback)
|
|
83
|
+
? reply.promptFeedback.blockReason
|
|
84
|
+
: undefined;
|
|
66
85
|
if (blocked !== undefined && blocked !== null) {
|
|
67
|
-
const quoted = typeof blocked === 'string' && BLOCK_REASON.test(blocked)
|
|
86
|
+
const quoted = typeof blocked === 'string' && BLOCK_REASON.test(blocked)
|
|
87
|
+
? `: ${snippet(blocked, MAX_BLOCK_REASON, redact)}`
|
|
88
|
+
: '';
|
|
68
89
|
throw fail('refusal', `prompt was blocked${quoted}`);
|
|
69
90
|
}
|
|
70
|
-
const candidate = Array.isArray(reply.candidates)
|
|
91
|
+
const candidate = Array.isArray(reply.candidates)
|
|
92
|
+
? reply.candidates[0]
|
|
93
|
+
: undefined;
|
|
71
94
|
if (!isRecord(candidate))
|
|
72
95
|
throw fail('invalid-response', 'reply has no candidates');
|
|
73
96
|
const reason = candidate.finishReason;
|
|
@@ -79,13 +102,17 @@ function readReply(reply, { id, model }, redact) {
|
|
|
79
102
|
throw fail('refusal', 'reply was stopped by the safety filter');
|
|
80
103
|
if (reason !== 'STOP')
|
|
81
104
|
throw fail('invalid-response', endedEarly(reason, redact));
|
|
82
|
-
const content = isRecord(candidate.content)
|
|
105
|
+
const content = isRecord(candidate.content)
|
|
106
|
+
? candidate.content
|
|
107
|
+
: {};
|
|
83
108
|
const text = joinedText(content.parts, part => !part.thought);
|
|
84
109
|
if (text === undefined)
|
|
85
110
|
throw fail('invalid-response', 'reply has no text');
|
|
86
111
|
return {
|
|
87
112
|
text,
|
|
88
|
-
model: typeof reply.modelVersion === 'string' && reply.modelVersion !== ''
|
|
113
|
+
model: typeof reply.modelVersion === 'string' && reply.modelVersion !== ''
|
|
114
|
+
? reply.modelVersion
|
|
115
|
+
: model,
|
|
89
116
|
usage: tokenUsage(reply.usageMetadata, ['promptTokenCount'], ['candidatesTokenCount', 'thoughtsTokenCount']),
|
|
90
117
|
};
|
|
91
118
|
}
|
|
@@ -98,8 +125,15 @@ function readReply(reply, { id, model }, redact) {
|
|
|
98
125
|
*/
|
|
99
126
|
export function gemini(options) {
|
|
100
127
|
const { id, model, auth = { header: 'x-goog-api-key' }, query, temperature, maxTokens, contextTokens, systemPrompt = 'native', structuredOutput = 'json' } = options;
|
|
101
|
-
const modelId = typeof model === 'string'
|
|
102
|
-
|
|
128
|
+
const modelId = typeof model === 'string'
|
|
129
|
+
? model.replace(/^models\//, '')
|
|
130
|
+
: '';
|
|
131
|
+
const issues = [
|
|
132
|
+
...(isRecord(query)
|
|
133
|
+
? keyParameterIssues(id, query)
|
|
134
|
+
: []),
|
|
135
|
+
...generationConfigIssues(id, options.extraBody),
|
|
136
|
+
];
|
|
103
137
|
if (!MODEL_ID.test(modelId))
|
|
104
138
|
issues.push({ path: `${id}.model`, message: 'must be a Gemini model id, such as gemini-3.8-flash' });
|
|
105
139
|
// The path is built before the checks, harmlessly: nothing is sent when they fail.
|
|
@@ -117,8 +151,21 @@ export function gemini(options) {
|
|
|
117
151
|
url,
|
|
118
152
|
headers,
|
|
119
153
|
body: mergeBody({
|
|
120
|
-
contents: messages.map(message => ({
|
|
121
|
-
|
|
154
|
+
contents: messages.map(message => ({
|
|
155
|
+
role: message.role === 'assistant'
|
|
156
|
+
? 'model'
|
|
157
|
+
: 'user',
|
|
158
|
+
parts: [
|
|
159
|
+
{ text: message.content },
|
|
160
|
+
],
|
|
161
|
+
})),
|
|
162
|
+
systemInstruction: system === undefined
|
|
163
|
+
? undefined
|
|
164
|
+
: {
|
|
165
|
+
parts: [
|
|
166
|
+
{ text: system },
|
|
167
|
+
],
|
|
168
|
+
},
|
|
122
169
|
generationConfig: generationConfig({ temperature, maxTokens, structuredOutput }, request.output),
|
|
123
170
|
}, extraBody),
|
|
124
171
|
signal: request.signal,
|
package/dist/providers/http.js
CHANGED
|
@@ -16,7 +16,10 @@ const ESCAPED_PRINTABLE_ASCII = /\\u00(2[0-9a-fA-F]|[3-6][0-9a-fA-F]|7[0-9a-eA-E
|
|
|
16
16
|
/** Every form in which a server may echo the secret. */
|
|
17
17
|
function echoedForms(secret) {
|
|
18
18
|
const asJson = JSON.stringify(secret).slice(1, -1);
|
|
19
|
-
return [secret, asJson].flatMap(form => [
|
|
19
|
+
return [secret, asJson].flatMap(form => [
|
|
20
|
+
form,
|
|
21
|
+
form.replaceAll('/', '\\/'),
|
|
22
|
+
]);
|
|
20
23
|
}
|
|
21
24
|
/** Go and .NET write `&`, `<`, `>` and `+` as escapes in JSON, so a key echoed by them would not match as is. */
|
|
22
25
|
function decodeEscapedAscii(text) {
|
|
@@ -70,13 +73,20 @@ export function snippet(text, max, redact = []) {
|
|
|
70
73
|
const written = redact.filter(value => value !== '').flatMap(echoedForms);
|
|
71
74
|
if (written.length === 0)
|
|
72
75
|
return JSON.stringify(head.slice(0, max));
|
|
73
|
-
const forms = [
|
|
76
|
+
const forms = [
|
|
77
|
+
...new Set([
|
|
78
|
+
...written,
|
|
79
|
+
...written.map(decodeEscapedAscii),
|
|
80
|
+
]),
|
|
81
|
+
];
|
|
74
82
|
return JSON.stringify(redactForms(decodeEscapedAscii(head), forms).slice(0, max));
|
|
75
83
|
}
|
|
76
84
|
/** The URL of an endpoint: the base URL without its trailing slashes, the endpoint's path, then the query. */
|
|
77
85
|
export function endpointUrl(baseUrl, path, query = {}) {
|
|
78
86
|
const search = new URLSearchParams(query).toString();
|
|
79
|
-
return `${baseUrl.replace(/\/+$/, '')}${path}${search === ''
|
|
87
|
+
return `${baseUrl.replace(/\/+$/, '')}${path}${search === ''
|
|
88
|
+
? ''
|
|
89
|
+
: `?${search}`}`;
|
|
80
90
|
}
|
|
81
91
|
/**
|
|
82
92
|
* What a provider redacts from server text, each value once and no empty one: its key; its base URL's
|
|
@@ -96,14 +106,21 @@ export function redactionsOf(baseUrl, apiKey, headers = {}) {
|
|
|
96
106
|
catch {
|
|
97
107
|
// A path with a stray `%` is redacted as written.
|
|
98
108
|
}
|
|
99
|
-
const values = Object.values(headers).flatMap(value => [
|
|
100
|
-
|
|
109
|
+
const values = Object.values(headers).flatMap(value => [
|
|
110
|
+
value,
|
|
111
|
+
value.trim(),
|
|
112
|
+
]);
|
|
113
|
+
return [
|
|
114
|
+
...new Set([apiKey ?? '', path, decoded, ...values]),
|
|
115
|
+
].filter(value => value.trim() !== '');
|
|
101
116
|
}
|
|
102
117
|
/** The scheme, host and port of a URL: no credentials, path, query or fragment. */
|
|
103
118
|
export function originOf(url) {
|
|
104
119
|
try {
|
|
105
120
|
const { origin } = new URL(url);
|
|
106
|
-
return origin === 'null'
|
|
121
|
+
return origin === 'null'
|
|
122
|
+
? UNKNOWN_ENDPOINT
|
|
123
|
+
: origin;
|
|
107
124
|
}
|
|
108
125
|
catch {
|
|
109
126
|
return UNKNOWN_ENDPOINT;
|
|
@@ -120,8 +137,12 @@ export function httpOrigin(url) {
|
|
|
120
137
|
const { protocol, hostname, port } = new URL(url);
|
|
121
138
|
if (protocol !== 'http:' && protocol !== 'https:')
|
|
122
139
|
return undefined;
|
|
123
|
-
const host = hostname.length > 1 && hostname.endsWith('.')
|
|
124
|
-
|
|
140
|
+
const host = hostname.length > 1 && hostname.endsWith('.')
|
|
141
|
+
? hostname.slice(0, -1)
|
|
142
|
+
: hostname;
|
|
143
|
+
return `${protocol}//${host}${port === ''
|
|
144
|
+
? ''
|
|
145
|
+
: `:${port}`}`;
|
|
125
146
|
}
|
|
126
147
|
/** The error's `code`, or its cause's, as ` (ECONNREFUSED)`, when it is a plain identifier. Never throws. */
|
|
127
148
|
function codeSuffix(error) {
|
|
@@ -197,7 +218,9 @@ export async function postJson(options) {
|
|
|
197
218
|
}
|
|
198
219
|
if (!response.ok) {
|
|
199
220
|
const { status } = response;
|
|
200
|
-
const redirect = status >= 300 && status < 400
|
|
221
|
+
const redirect = status >= 300 && status < 400
|
|
222
|
+
? ' (redirects are not followed)'
|
|
223
|
+
: '';
|
|
201
224
|
throw fail('http', `HTTP ${status}${redirect}: ${snippet(await readErrorText(response), MAX_ERROR_BODY, redact)}`, status);
|
|
202
225
|
}
|
|
203
226
|
let reply;
|
|
@@ -5,6 +5,18 @@ export function shapeMessages(request, mode) {
|
|
|
5
5
|
return { system: request.system, messages };
|
|
6
6
|
const [first, ...rest] = messages;
|
|
7
7
|
return first?.role === 'user'
|
|
8
|
-
? {
|
|
9
|
-
|
|
8
|
+
? {
|
|
9
|
+
system: undefined,
|
|
10
|
+
messages: [
|
|
11
|
+
{ role: 'user', content: `${request.system}\n\n${first.content}` },
|
|
12
|
+
...rest,
|
|
13
|
+
],
|
|
14
|
+
}
|
|
15
|
+
: {
|
|
16
|
+
system: undefined,
|
|
17
|
+
messages: [
|
|
18
|
+
{ role: 'user', content: request.system },
|
|
19
|
+
...messages,
|
|
20
|
+
],
|
|
21
|
+
};
|
|
10
22
|
}
|
package/dist/providers/ollama.js
CHANGED
|
@@ -9,9 +9,17 @@ import { checkFits, contextBudget } from "./size.js";
|
|
|
9
9
|
* 200 with the rendered template instead of a reply.
|
|
10
10
|
*/
|
|
11
11
|
const RULES = {
|
|
12
|
-
reservedBodyKeys: [
|
|
12
|
+
reservedBodyKeys: [
|
|
13
|
+
'model',
|
|
14
|
+
'messages',
|
|
15
|
+
'stream',
|
|
16
|
+
'truncate',
|
|
17
|
+
'shift',
|
|
18
|
+
],
|
|
13
19
|
toolBodyKeys: ['tools', '_debug_render_only'],
|
|
14
|
-
replyLimitPaths: [
|
|
20
|
+
replyLimitPaths: [
|
|
21
|
+
['options', 'num_predict'],
|
|
22
|
+
],
|
|
15
23
|
fixedHeaders: [],
|
|
16
24
|
structuredOutputs: ['prompt', 'json', 'schema'],
|
|
17
25
|
};
|
|
@@ -31,12 +39,18 @@ function numCtxIssues(id, extraBody) {
|
|
|
31
39
|
return Object.entries(extraBody).flatMap(([key, value]) => {
|
|
32
40
|
if (bodyKeyForm(key) !== OPTIONS)
|
|
33
41
|
return [];
|
|
34
|
-
if (!isRecord(value))
|
|
35
|
-
return [
|
|
42
|
+
if (!isRecord(value)) {
|
|
43
|
+
return [
|
|
44
|
+
{ path: `${id}.${issuePath(['extraBody', key])}`, message: 'must be an object' },
|
|
45
|
+
];
|
|
46
|
+
}
|
|
36
47
|
const numCtx = Object.keys(value)
|
|
37
48
|
.filter(name => bodyKeyForm(name) === 'numctx')
|
|
38
49
|
.map(name => ({ path: `${id}.${issuePath(['extraBody', key, name])}`, message: 'is set from contextTokens: set contextTokens instead' }));
|
|
39
|
-
return [
|
|
50
|
+
return [
|
|
51
|
+
...spellingIssues(id, key, OPTIONS),
|
|
52
|
+
...numCtx,
|
|
53
|
+
];
|
|
40
54
|
});
|
|
41
55
|
}
|
|
42
56
|
function format(mode, output) {
|
|
@@ -61,12 +75,16 @@ function readReply(reply, { id, model }, redact) {
|
|
|
61
75
|
throw fail('invalid-response', 'reply has not finished');
|
|
62
76
|
if (reply.done_reason === 'length')
|
|
63
77
|
throw fail('truncated', 'reply was cut off by the token limit or the context');
|
|
64
|
-
const message = isRecord(reply.message)
|
|
78
|
+
const message = isRecord(reply.message)
|
|
79
|
+
? reply.message
|
|
80
|
+
: {};
|
|
65
81
|
if (typeof message.content !== 'string')
|
|
66
82
|
throw fail('invalid-response', 'reply has no text');
|
|
67
83
|
return {
|
|
68
84
|
text: message.content,
|
|
69
|
-
model: typeof reply.model === 'string' && reply.model !== ''
|
|
85
|
+
model: typeof reply.model === 'string' && reply.model !== ''
|
|
86
|
+
? reply.model
|
|
87
|
+
: model,
|
|
70
88
|
usage: tokenUsage(reply, ['prompt_eval_count'], ['eval_count']),
|
|
71
89
|
};
|
|
72
90
|
}
|
|
@@ -99,7 +117,12 @@ export function ollama(options) {
|
|
|
99
117
|
headers,
|
|
100
118
|
body: mergeBody({
|
|
101
119
|
model,
|
|
102
|
-
messages: system === undefined
|
|
120
|
+
messages: system === undefined
|
|
121
|
+
? messages
|
|
122
|
+
: [
|
|
123
|
+
{ role: 'system', content: system },
|
|
124
|
+
...messages,
|
|
125
|
+
],
|
|
103
126
|
stream: false,
|
|
104
127
|
truncate: false,
|
|
105
128
|
shift: false,
|
|
@@ -34,7 +34,10 @@ const RULES = {
|
|
|
34
34
|
'documents',
|
|
35
35
|
'data_sources',
|
|
36
36
|
],
|
|
37
|
-
replyLimitPaths: [
|
|
37
|
+
replyLimitPaths: [
|
|
38
|
+
['max_tokens'],
|
|
39
|
+
['max_completion_tokens'],
|
|
40
|
+
],
|
|
38
41
|
fixedHeaders: [],
|
|
39
42
|
structuredOutputs: ['prompt', 'json', 'schema'],
|
|
40
43
|
};
|
|
@@ -42,8 +45,12 @@ const MAX_TOKENS_FIELDS = new Set(['max_tokens', 'max_completion_tokens']);
|
|
|
42
45
|
function responseFormat(mode, output) {
|
|
43
46
|
if (mode === 'json')
|
|
44
47
|
return { type: 'json_object' };
|
|
45
|
-
if (mode === 'schema')
|
|
46
|
-
return {
|
|
48
|
+
if (mode === 'schema') {
|
|
49
|
+
return {
|
|
50
|
+
type: 'json_schema',
|
|
51
|
+
json_schema: { name: output.name, schema: output.schema, strict: true },
|
|
52
|
+
};
|
|
53
|
+
}
|
|
47
54
|
return undefined;
|
|
48
55
|
}
|
|
49
56
|
/** Finish reasons that end a reply before the model is done, besides the token limit and the content filter. */
|
|
@@ -52,7 +59,9 @@ const ENDED_EARLY = new Set(['error', 'insufficient_system_resource', 'aborted']
|
|
|
52
59
|
const CUT_OFF = new Set(['length', 'model_length']);
|
|
53
60
|
/** The status a server error carries in its `code`, when that is an HTTP error status. */
|
|
54
61
|
function statusOf(code) {
|
|
55
|
-
return typeof code === 'number' && Number.isInteger(code) && code >= 400 && code <= 599
|
|
62
|
+
return typeof code === 'number' && Number.isInteger(code) && code >= 400 && code <= 599
|
|
63
|
+
? code
|
|
64
|
+
: undefined;
|
|
56
65
|
}
|
|
57
66
|
/**
|
|
58
67
|
* Whether a message shows a tool the server ran: a non-empty `executed_tools` (Groq's built-in tools), or
|
|
@@ -67,7 +76,9 @@ function ranTool(message) {
|
|
|
67
76
|
}
|
|
68
77
|
/** The text of a message's content: a string, or the `text` parts of an array of parts (Mistral), joined; thinking parts are skipped. */
|
|
69
78
|
function textOf(content) {
|
|
70
|
-
return typeof content === 'string'
|
|
79
|
+
return typeof content === 'string'
|
|
80
|
+
? content
|
|
81
|
+
: joinedText(content, part => part.type === 'text');
|
|
71
82
|
}
|
|
72
83
|
/**
|
|
73
84
|
* Turn a chat completion reply into text, or a `ProviderError`. Every property is read defensively: the
|
|
@@ -82,20 +93,34 @@ function readReply(reply, { id, model }, redact) {
|
|
|
82
93
|
throw fail('invalid-response', 'reply is not a JSON object');
|
|
83
94
|
const { error } = reply;
|
|
84
95
|
if (typeof error === 'string' || isRecord(error)) {
|
|
85
|
-
const text = typeof error === 'string'
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
96
|
+
const text = typeof error === 'string'
|
|
97
|
+
? error
|
|
98
|
+
: error.message;
|
|
99
|
+
const quoted = typeof text === 'string'
|
|
100
|
+
? `: ${snippet(text, MAX_MODEL_TEXT, redact)}`
|
|
101
|
+
: '';
|
|
102
|
+
const status = isRecord(error)
|
|
103
|
+
? statusOf(error.code)
|
|
104
|
+
: undefined;
|
|
105
|
+
throw status === undefined
|
|
106
|
+
? fail('invalid-response', `server error${quoted}`)
|
|
107
|
+
: fail('http', `server error ${status}${quoted}`, status);
|
|
89
108
|
}
|
|
90
|
-
const choice = Array.isArray(reply.choices)
|
|
109
|
+
const choice = Array.isArray(reply.choices)
|
|
110
|
+
? reply.choices[0]
|
|
111
|
+
: undefined;
|
|
91
112
|
if (!isRecord(choice))
|
|
92
113
|
throw fail('invalid-response', 'reply has no choices');
|
|
93
|
-
const message = isRecord(choice.message)
|
|
114
|
+
const message = isRecord(choice.message)
|
|
115
|
+
? choice.message
|
|
116
|
+
: {};
|
|
94
117
|
if (ranTool(message))
|
|
95
118
|
throw fail('invalid-response', 'the server ran a tool for this reply, which quorum never allows');
|
|
96
119
|
if (message.refusal) {
|
|
97
120
|
const refusal = message.refusal;
|
|
98
|
-
throw fail('refusal', typeof refusal === 'string'
|
|
121
|
+
throw fail('refusal', typeof refusal === 'string'
|
|
122
|
+
? `model refused: ${snippet(refusal, MAX_MODEL_TEXT, redact)}`
|
|
123
|
+
: 'model refused');
|
|
99
124
|
}
|
|
100
125
|
const reason = choice.finish_reason;
|
|
101
126
|
if (reason === 'content_filter')
|
|
@@ -109,7 +134,9 @@ function readReply(reply, { id, model }, redact) {
|
|
|
109
134
|
throw fail('invalid-response', 'reply has no message content');
|
|
110
135
|
return {
|
|
111
136
|
text,
|
|
112
|
-
model: typeof reply.model === 'string' && reply.model !== ''
|
|
137
|
+
model: typeof reply.model === 'string' && reply.model !== ''
|
|
138
|
+
? reply.model
|
|
139
|
+
: model,
|
|
113
140
|
usage: tokenUsage(reply.usage, ['prompt_tokens'], ['completion_tokens']),
|
|
114
141
|
};
|
|
115
142
|
}
|
|
@@ -122,7 +149,11 @@ function readReply(reply, { id, model }, redact) {
|
|
|
122
149
|
*/
|
|
123
150
|
export function openaiCompatible(options) {
|
|
124
151
|
const { id, model, auth = 'bearer', temperature = 0, maxTokens, contextTokens, maxTokensField = 'max_tokens', systemPrompt = 'native', structuredOutput = 'json' } = options;
|
|
125
|
-
const fieldIssues = MAX_TOKENS_FIELDS.has(maxTokensField)
|
|
152
|
+
const fieldIssues = MAX_TOKENS_FIELDS.has(maxTokensField)
|
|
153
|
+
? []
|
|
154
|
+
: [
|
|
155
|
+
{ path: `${id}.maxTokensField`, message: 'must be max_tokens or max_completion_tokens' },
|
|
156
|
+
];
|
|
126
157
|
const { url, headers, redact, extraBody } = prepareHttp(options, { ...RULES, auth }, '/chat/completions', fieldIssues);
|
|
127
158
|
const budget = contextBudget(contextTokens, maxTokens);
|
|
128
159
|
return {
|
|
@@ -138,7 +169,12 @@ export function openaiCompatible(options) {
|
|
|
138
169
|
headers,
|
|
139
170
|
body: mergeBody({
|
|
140
171
|
model,
|
|
141
|
-
messages: system === undefined
|
|
172
|
+
messages: system === undefined
|
|
173
|
+
? messages
|
|
174
|
+
: [
|
|
175
|
+
{ role: 'system', content: system },
|
|
176
|
+
...messages,
|
|
177
|
+
],
|
|
142
178
|
response_format: responseFormat(structuredOutput, request.output),
|
|
143
179
|
temperature: temperature ?? undefined,
|
|
144
180
|
[maxTokensField]: maxTokens,
|