@steve31415/baselib 3.71.0 → 3.73.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/dist/llm/complete.js +16 -4
- package/dist/llm/http.d.ts +6 -0
- package/dist/llm/http.js +24 -0
- package/dist/llm/index.d.ts +1 -1
- package/dist/llm/models.d.ts +8 -0
- package/dist/llm/models.js +5 -0
- package/dist/llm/providers/anthropic.js +2 -1
- package/dist/llm/providers/gemini.js +5 -2
- package/dist/llm/providers/openai.js +2 -1
- package/dist/llm/types.d.ts +26 -1
- package/package.json +1 -1
package/dist/llm/complete.js
CHANGED
|
@@ -5,6 +5,12 @@ const DEFAULT_MAX_TOKENS = 2048;
|
|
|
5
5
|
const DEFAULT_TEMPERATURE = 0.3;
|
|
6
6
|
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
7
7
|
const DEFAULT_THINKING_LEVELS = ['low', 'medium', 'high'];
|
|
8
|
+
/** Input media a model must accept to be tried: the requested model not
|
|
9
|
+
* taking one is rejected up front; a fallback not taking it is skipped. */
|
|
10
|
+
const MEDIA_GATES = [
|
|
11
|
+
{ kind: 'image', label: 'Image', accepts: (entry) => entry.supportsImages === true },
|
|
12
|
+
{ kind: 'audio', label: 'Audio', accepts: (entry) => entry.supportsAudio === true },
|
|
13
|
+
];
|
|
8
14
|
/**
|
|
9
15
|
* The effort level to send a model: none when the model has no effort
|
|
10
16
|
* control, otherwise the requested level clamped to the nearest one the
|
|
@@ -43,10 +49,14 @@ export async function llmComplete(request) {
|
|
|
43
49
|
// requested model not taking them is the caller's bug, so say so rather
|
|
44
50
|
// than silently answering from a fallback; fallbacks without image input
|
|
45
51
|
// are skipped in the loop below.
|
|
52
|
+
// Audio is gated the same way (only Gemini takes it).
|
|
46
53
|
const hasImages = (request.images?.length ?? 0) > 0;
|
|
47
|
-
|
|
54
|
+
const hasAudio = (request.audio?.length ?? 0) > 0;
|
|
55
|
+
for (const gate of MEDIA_GATES) {
|
|
56
|
+
if (!(gate.kind === 'image' ? hasImages : hasAudio) || gate.accepts(entry))
|
|
57
|
+
continue;
|
|
48
58
|
throw new LLMError({
|
|
49
|
-
message: `Model ${request.model} does not accept
|
|
59
|
+
message: `Model ${request.model} does not accept ${gate.kind} input. ${gate.label}-capable models: ${Object.keys(MODEL_REGISTRY).filter((m) => gate.accepts(MODEL_REGISTRY[m])).join(', ')}`,
|
|
50
60
|
provider: entry.provider,
|
|
51
61
|
model: entry.apiModelId,
|
|
52
62
|
isRetryable: false,
|
|
@@ -88,9 +98,10 @@ export async function llmComplete(request) {
|
|
|
88
98
|
const modelEntry = MODEL_REGISTRY[modelName];
|
|
89
99
|
if (!modelEntry)
|
|
90
100
|
continue;
|
|
91
|
-
|
|
101
|
+
const missing = MEDIA_GATES.find((gate) => (gate.kind === 'image' ? hasImages : hasAudio) && !gate.accepts(modelEntry));
|
|
102
|
+
if (missing) {
|
|
92
103
|
request.logger?.debug('llm.skip_provider', {
|
|
93
|
-
model: modelName, provider: modelEntry.provider, reason:
|
|
104
|
+
model: modelName, provider: modelEntry.provider, reason: `no ${missing.kind} input`,
|
|
94
105
|
});
|
|
95
106
|
continue;
|
|
96
107
|
}
|
|
@@ -108,6 +119,7 @@ export async function llmComplete(request) {
|
|
|
108
119
|
apiModelId: modelEntry.apiModelId,
|
|
109
120
|
prompt: request.prompt,
|
|
110
121
|
images: hasImages ? request.images : undefined,
|
|
122
|
+
audio: hasAudio ? request.audio : undefined,
|
|
111
123
|
systemPrompt: request.systemPrompt,
|
|
112
124
|
maxTokens,
|
|
113
125
|
temperature,
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Agent } from 'undici';
|
|
2
|
+
/** The one dispatcher every provider adapter passes to fetch. */
|
|
3
|
+
export declare function llmDispatcher(): Agent;
|
|
4
|
+
/** fetch() through llmDispatcher(). `dispatcher` is Node's (undici's)
|
|
5
|
+
* extension of RequestInit, absent from the DOM type. */
|
|
6
|
+
export declare const llmFetch: typeof fetch;
|
package/dist/llm/http.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// The HTTP dispatcher the provider adapters fetch through, so that an
|
|
2
|
+
// attempt's `timeoutMs` is its only deadline.
|
|
3
|
+
//
|
|
4
|
+
// Node's fetch is undici, and its global dispatcher drops a response whose
|
|
5
|
+
// headers have not arrived within 300 s (`headersTimeout`) or whose body
|
|
6
|
+
// stalls for 300 s (`bodyTimeout`). The adapters do not stream, so a provider
|
|
7
|
+
// sends no headers until the whole answer is ready: through the global
|
|
8
|
+
// dispatcher a call allowed 600 s failed at 300 s with "fetch failed /
|
|
9
|
+
// Headers Timeout Error", reported as a network error rather than a timeout,
|
|
10
|
+
// and failed over (reproduced 2026-11-23; test/llm/http.test.ts).
|
|
11
|
+
// Here both undici limits are off (0); each adapter's AbortController, armed
|
|
12
|
+
// at `timeoutMs` and held until the body is read, bounds the attempt instead.
|
|
13
|
+
// The connect timeout (10 s) stays: a provider that cannot be reached at all
|
|
14
|
+
// should fail over quickly whatever `timeoutMs` allows.
|
|
15
|
+
import { Agent } from 'undici';
|
|
16
|
+
let shared;
|
|
17
|
+
/** The one dispatcher every provider adapter passes to fetch. */
|
|
18
|
+
export function llmDispatcher() {
|
|
19
|
+
shared ??= new Agent({ headersTimeout: 0, bodyTimeout: 0 });
|
|
20
|
+
return shared;
|
|
21
|
+
}
|
|
22
|
+
/** fetch() through llmDispatcher(). `dispatcher` is Node's (undici's)
|
|
23
|
+
* extension of RequestInit, absent from the DOM type. */
|
|
24
|
+
export const llmFetch = (input, init) => fetch(input, { ...init, dispatcher: llmDispatcher() });
|
package/dist/llm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { llmComplete } from './complete.js';
|
|
2
2
|
export { MODEL_REGISTRY } from './models.js';
|
|
3
3
|
export type { ModelEntry } from './models.js';
|
|
4
|
-
export { LLMError, EFFORT_LEVELS, type Effort, type LLMRequest, type LLMResponse, type FailedAttempt, type FinishCategory, type ProviderName, type ApiKeys, type JsonSchema, type LLMImage, type LLMImageMediaType, } from './types.js';
|
|
4
|
+
export { LLMError, EFFORT_LEVELS, type Effort, type LLMRequest, type LLMResponse, type FailedAttempt, type FinishCategory, type ProviderName, type ApiKeys, type JsonSchema, type LLMImage, type LLMImageMediaType, type LLMAudio, type LLMAudioMediaType, } from './types.js';
|
package/dist/llm/models.d.ts
CHANGED
|
@@ -66,6 +66,14 @@ export interface ModelEntry {
|
|
|
66
66
|
* was verified live (test/helpers/llm.ts records the probe).
|
|
67
67
|
*/
|
|
68
68
|
supportsImages?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Whether this model accepts audio input (`LLMRequest.audio`). Defaults to
|
|
71
|
+
* `false`, gated exactly like `supportsImages`. Set only on models whose
|
|
72
|
+
* audio handling was verified live (test/helpers/llm.ts records the probe):
|
|
73
|
+
* the Gemini 3.x entries. No Anthropic or OpenAI model in the registry
|
|
74
|
+
* takes audio.
|
|
75
|
+
*/
|
|
76
|
+
supportsAudio?: boolean;
|
|
69
77
|
}
|
|
70
78
|
/**
|
|
71
79
|
* Output budget below which a `thinksByDefault` model is considered at risk of
|
package/dist/llm/models.js
CHANGED
|
@@ -35,6 +35,7 @@ export const MODEL_REGISTRY = {
|
|
|
35
35
|
provider: 'gemini',
|
|
36
36
|
apiModelId: 'gemini-3.5-flash-lite',
|
|
37
37
|
supportsImages: true,
|
|
38
|
+
supportsAudio: true,
|
|
38
39
|
fallbacks: ['gpt-5.6-luna', 'claude-haiku-4-5'],
|
|
39
40
|
// Default thinking_level is `minimal` — reasoning overhead is negligible,
|
|
40
41
|
// so no `thinksByDefault` headroom warning.
|
|
@@ -47,6 +48,7 @@ export const MODEL_REGISTRY = {
|
|
|
47
48
|
provider: 'gemini',
|
|
48
49
|
apiModelId: 'gemini-3.6-flash',
|
|
49
50
|
supportsImages: true,
|
|
51
|
+
supportsAudio: true,
|
|
50
52
|
fallbacks: ['gpt-5.6-luna', 'claude-haiku-4-5'],
|
|
51
53
|
// Thinks by default (thinking_level defaults to `medium`); reasoning
|
|
52
54
|
// tokens are billed as output and count against maxOutputTokens.
|
|
@@ -83,6 +85,7 @@ export const MODEL_REGISTRY = {
|
|
|
83
85
|
provider: 'gemini',
|
|
84
86
|
apiModelId: 'gemini-3.1-flash-lite',
|
|
85
87
|
supportsImages: true,
|
|
88
|
+
supportsAudio: true,
|
|
86
89
|
fallbacks: ['claude-sonnet-4-6'],
|
|
87
90
|
supportsEffort: true,
|
|
88
91
|
},
|
|
@@ -90,6 +93,7 @@ export const MODEL_REGISTRY = {
|
|
|
90
93
|
provider: 'gemini',
|
|
91
94
|
apiModelId: 'gemini-3-flash-preview',
|
|
92
95
|
supportsImages: true,
|
|
96
|
+
supportsAudio: true,
|
|
93
97
|
fallbacks: ['claude-haiku-4-5', 'gpt-5.6-luna'],
|
|
94
98
|
supportsEffort: true,
|
|
95
99
|
thinkingLevels: ['minimal', 'low', 'medium', 'high'],
|
|
@@ -214,6 +218,7 @@ export const MODEL_REGISTRY = {
|
|
|
214
218
|
provider: 'gemini',
|
|
215
219
|
apiModelId: 'gemini-3.1-pro-preview',
|
|
216
220
|
supportsImages: true,
|
|
221
|
+
supportsAudio: true,
|
|
217
222
|
fallbacks: ['claude-opus-5-5', 'gpt-5.6-sol'],
|
|
218
223
|
// Thinks by default (thinking_level defaults to `high`).
|
|
219
224
|
thinksByDefault: true,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LLMError } from '../types.js';
|
|
2
|
+
import { llmFetch } from '../http.js';
|
|
2
3
|
/** Classify Anthropic's `stop_reason` (already lowercase from the API). */
|
|
3
4
|
function classifyFinish(stopReason) {
|
|
4
5
|
switch (stopReason) {
|
|
@@ -130,7 +131,7 @@ export const anthropicProvider = {
|
|
|
130
131
|
let response;
|
|
131
132
|
let responseText;
|
|
132
133
|
try {
|
|
133
|
-
response = await
|
|
134
|
+
response = await llmFetch(endpoint, {
|
|
134
135
|
method: 'POST',
|
|
135
136
|
headers: {
|
|
136
137
|
'Content-Type': 'application/json',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LLMError } from '../types.js';
|
|
2
|
+
import { llmFetch } from '../http.js';
|
|
2
3
|
/** Gemini `finishReason` enum values that mean the model was content-blocked. */
|
|
3
4
|
const GEMINI_DECLINE_REASONS = new Set([
|
|
4
5
|
'safety', 'recitation', 'prohibited_content', 'blocklist', 'spii', 'image_safety',
|
|
@@ -65,6 +66,7 @@ export const geminiProvider = {
|
|
|
65
66
|
provider: 'gemini', model: request.apiModelId, endpoint,
|
|
66
67
|
...(request.callSite ? { callSite: request.callSite } : {}),
|
|
67
68
|
...(request.images ? { images: request.images.length } : {}),
|
|
69
|
+
...(request.audio ? { audio: request.audio.length } : {}),
|
|
68
70
|
});
|
|
69
71
|
const startTime = Date.now();
|
|
70
72
|
const generationConfig = {
|
|
@@ -96,10 +98,11 @@ export const geminiProvider = {
|
|
|
96
98
|
generationConfig.thinkingConfig = { thinkingBudget: request.thinkingBudget };
|
|
97
99
|
}
|
|
98
100
|
const body = {
|
|
99
|
-
// Images go as inline-data parts ahead of the prompt's text part.
|
|
101
|
+
// Images, then audio, go as inline-data parts ahead of the prompt's text part.
|
|
100
102
|
contents: [{
|
|
101
103
|
parts: [
|
|
102
104
|
...(request.images ?? []).map((image) => ({ inlineData: { mimeType: image.mediaType, data: image.data } })),
|
|
105
|
+
...(request.audio ?? []).map((clip) => ({ inlineData: { mimeType: clip.mediaType, data: clip.data } })),
|
|
103
106
|
{ text: request.prompt },
|
|
104
107
|
],
|
|
105
108
|
}],
|
|
@@ -113,7 +116,7 @@ export const geminiProvider = {
|
|
|
113
116
|
let response;
|
|
114
117
|
let responseText;
|
|
115
118
|
try {
|
|
116
|
-
response = await
|
|
119
|
+
response = await llmFetch(`${endpoint}?key=${request.apiKey}`, {
|
|
117
120
|
method: 'POST',
|
|
118
121
|
headers: { 'Content-Type': 'application/json' },
|
|
119
122
|
body: JSON.stringify(body),
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LLMError } from '../types.js';
|
|
2
|
+
import { llmFetch } from '../http.js';
|
|
2
3
|
/** True when a JSON-schema `type` value denotes (or includes) an object. */
|
|
3
4
|
function isObjectSchema(type) {
|
|
4
5
|
return type === 'object' || (Array.isArray(type) && type.includes('object'));
|
|
@@ -140,7 +141,7 @@ export const openaiProvider = {
|
|
|
140
141
|
let response;
|
|
141
142
|
let responseText;
|
|
142
143
|
try {
|
|
143
|
-
response = await
|
|
144
|
+
response = await llmFetch(endpoint, {
|
|
144
145
|
method: 'POST',
|
|
145
146
|
headers: {
|
|
146
147
|
'Content-Type': 'application/json',
|
package/dist/llm/types.d.ts
CHANGED
|
@@ -21,6 +21,17 @@ export interface LLMImage {
|
|
|
21
21
|
mediaType: LLMImageMediaType;
|
|
22
22
|
data: string;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Audio media types Gemini accepted in the live probe of 2026-11-21 (Ogg with
|
|
26
|
+
* or without a `codecs=opus` parameter, MP3, MP4/AAC, WAV): see
|
|
27
|
+
* test/helpers/llm.ts. Pass the bare type; parameters are not needed.
|
|
28
|
+
*/
|
|
29
|
+
export type LLMAudioMediaType = 'audio/ogg' | 'audio/mpeg' | 'audio/mp4' | 'audio/wav';
|
|
30
|
+
/** A sound recording sent beside the prompt: raw base64, no `data:` URL prefix. */
|
|
31
|
+
export interface LLMAudio {
|
|
32
|
+
mediaType: LLMAudioMediaType;
|
|
33
|
+
data: string;
|
|
34
|
+
}
|
|
24
35
|
/** Request to llmComplete(). */
|
|
25
36
|
export interface LLMRequest {
|
|
26
37
|
/** Model name from the registry (e.g. 'claude-sonnet-4-6', 'gemini-2.5-flash'). */
|
|
@@ -34,6 +45,14 @@ export interface LLMRequest {
|
|
|
34
45
|
* call is rejected up front), and failover skips fallbacks that are not.
|
|
35
46
|
*/
|
|
36
47
|
images?: LLMImage[];
|
|
48
|
+
/**
|
|
49
|
+
* Audio for the model to listen to beside the prompt (a voice message to
|
|
50
|
+
* transcribe), placed before the prompt text like `images`. Gated the same
|
|
51
|
+
* way, by the registry's `supportsAudio`: only Gemini models take it, so a
|
|
52
|
+
* request with audio has no cross-provider failover — a Gemini outage
|
|
53
|
+
* fails the call, and the caller retries later.
|
|
54
|
+
*/
|
|
55
|
+
audio?: LLMAudio[];
|
|
37
56
|
/** Optional system prompt. */
|
|
38
57
|
systemPrompt?: string;
|
|
39
58
|
/** Max output tokens. Default: 2048. */
|
|
@@ -46,7 +65,11 @@ export interface LLMRequest {
|
|
|
46
65
|
apiKeys: ApiKeys;
|
|
47
66
|
/** Logger instance for structured logging. */
|
|
48
67
|
logger?: Logger;
|
|
49
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* Timeout per provider attempt, including response body download, in ms.
|
|
70
|
+
* Default: 60000. The only deadline on an attempt: no undici 300 s
|
|
71
|
+
* headers/body limit applies (since 3.73.0, src/llm/http.ts).
|
|
72
|
+
*/
|
|
50
73
|
timeoutMs?: number;
|
|
51
74
|
/**
|
|
52
75
|
* @deprecated Use `effort`. Gemini's token cap on hidden reasoning. Gemini
|
|
@@ -211,6 +234,8 @@ export interface ProviderCallRequest {
|
|
|
211
234
|
prompt: string;
|
|
212
235
|
/** Present and non-empty only for a `supportsImages` model (gated in complete.ts). */
|
|
213
236
|
images?: LLMImage[];
|
|
237
|
+
/** Present and non-empty only for a `supportsAudio` model (gated in complete.ts). */
|
|
238
|
+
audio?: LLMAudio[];
|
|
214
239
|
systemPrompt?: string;
|
|
215
240
|
maxTokens: number;
|
|
216
241
|
temperature: number;
|
package/package.json
CHANGED