@steve31415/baselib 3.72.0 → 3.74.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/http.d.ts +12 -0
- package/dist/http.js +2 -0
- package/dist/llm/http.d.ts +6 -0
- package/dist/llm/http.js +24 -0
- package/dist/llm/providers/anthropic.js +2 -1
- package/dist/llm/providers/gemini.js +2 -1
- package/dist/llm/providers/openai.js +2 -1
- package/dist/llm/types.d.ts +5 -1
- package/package.json +1 -1
package/dist/http.d.ts
CHANGED
|
@@ -6,6 +6,18 @@ export interface RequestLoggingOptions {
|
|
|
6
6
|
* `?app=`, so one service's sockets can be told apart by app); fields
|
|
7
7
|
* whose value is undefined are left out. */
|
|
8
8
|
meta?: (c: Context) => Record<string, unknown>;
|
|
9
|
+
/** Await `logger.flush()` after the exit line, before the response goes
|
|
10
|
+
* out, for requests where it returns true. On request-billed Cloud Run
|
|
11
|
+
* the CPU is throttled to almost nothing once the response is sent, so
|
|
12
|
+
* lines left for the shipper's batch timer — the handler's last lines and
|
|
13
|
+
* this exit line itself — often wait for the next request, and their
|
|
14
|
+
* 10 s ship attempt times out (radar's 5-minute scheduler pokes, task
|
|
15
|
+
* acb5a5e1, 2026-11-24). A handler's own `await logger.flush()` cannot
|
|
16
|
+
* cover the exit line, which is logged after the handler returns. Use it
|
|
17
|
+
* for machine callers (Scheduler pokes, `/internal/*`), not for user
|
|
18
|
+
* requests: it adds one Axiom round trip (bounded by the shipper's
|
|
19
|
+
* attempt timeout) to the response. */
|
|
20
|
+
flush?: (c: Context) => boolean;
|
|
9
21
|
}
|
|
10
22
|
/** One INFO line per request on exit (method, path, status, elapsed,
|
|
11
23
|
* identity, plus `options.meta`); /health excluded. Entry-point detail
|
package/dist/http.js
CHANGED
|
@@ -26,6 +26,8 @@ export function requestLogging(logger, options = {}) {
|
|
|
26
26
|
...(identity ? { identity: identity.email } : {}),
|
|
27
27
|
...Object.fromEntries(extra),
|
|
28
28
|
});
|
|
29
|
+
if (options.flush?.(c))
|
|
30
|
+
await logger.flush();
|
|
29
31
|
});
|
|
30
32
|
}
|
|
31
33
|
/** The address of the client that connected to Google's edge, read from
|
|
@@ -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() });
|
|
@@ -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',
|
|
@@ -115,7 +116,7 @@ export const geminiProvider = {
|
|
|
115
116
|
let response;
|
|
116
117
|
let responseText;
|
|
117
118
|
try {
|
|
118
|
-
response = await
|
|
119
|
+
response = await llmFetch(`${endpoint}?key=${request.apiKey}`, {
|
|
119
120
|
method: 'POST',
|
|
120
121
|
headers: { 'Content-Type': 'application/json' },
|
|
121
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
|
@@ -65,7 +65,11 @@ export interface LLMRequest {
|
|
|
65
65
|
apiKeys: ApiKeys;
|
|
66
66
|
/** Logger instance for structured logging. */
|
|
67
67
|
logger?: Logger;
|
|
68
|
-
/**
|
|
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
|
+
*/
|
|
69
73
|
timeoutMs?: number;
|
|
70
74
|
/**
|
|
71
75
|
* @deprecated Use `effort`. Gemini's token cap on hidden reasoning. Gemini
|
package/package.json
CHANGED