@gtrabanco/pi-nan-provider 1.5.1 → 1.5.3
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/AGENTS.md +26 -2
- package/CONTRIBUTING.md +10 -1
- package/README.es.md +43 -1
- package/README.md +42 -1
- package/package.json +1 -1
- package/scripts/generate-models.ts +19 -3
- package/scripts/manual-overrides.ts +15 -1
- package/scripts/models.generated.ts +5 -5
- package/src/cache-anomaly.ts +277 -0
- package/src/debug-command.ts +124 -0
- package/src/debug-report.ts +601 -0
- package/src/index.ts +2 -0
- package/src/openai-compat-sanitizer.ts +20 -2
- package/src/provider-factory.ts +43 -4
- package/src/zip.ts +114 -0
package/src/provider-factory.ts
CHANGED
|
@@ -39,6 +39,7 @@ import type {
|
|
|
39
39
|
import { providerAttributionEnabled, stripProviderAttribution } from "./attribution.ts";
|
|
40
40
|
import { withContextOverflowClassification } from "./context-overflow-classifier.ts";
|
|
41
41
|
import { withReasoningOnlyStreamGuard } from "./reasoning-only-stream-guard.ts";
|
|
42
|
+
import { createCacheAnomalyFileSink, withCacheAnomalyCapture, type CacheAnomalySink } from "./cache-anomaly.ts";
|
|
42
43
|
import { NAN_IMAGE_API, baselineImageModels, createNanImagesApi } from "./images.ts";
|
|
43
44
|
import {
|
|
44
45
|
baselineModels,
|
|
@@ -50,6 +51,7 @@ import {
|
|
|
50
51
|
applyOverlay,
|
|
51
52
|
type OverlayEntry,
|
|
52
53
|
NAN_GENERATED_MODELS,
|
|
54
|
+
NON_CHAT_MODEL_IDS,
|
|
53
55
|
} from "./fetch-models.ts";
|
|
54
56
|
import { resolveOpenAICompletionsApi } from "./pi-ai-loader.ts";
|
|
55
57
|
import { sanitizeOpenAICompatPayload } from "./openai-compat-sanitizer.ts";
|
|
@@ -70,6 +72,8 @@ export interface NanCompatibleProviderOptions {
|
|
|
70
72
|
timeoutMs?: number;
|
|
71
73
|
/** Injectable for tests; defaults to global fetch. */
|
|
72
74
|
fetchImpl?: typeof fetch;
|
|
75
|
+
/** Sink for the issue #35 cache-anomaly evidence; defaults to the agent-dir JSONL file. */
|
|
76
|
+
cacheAnomalySink?: CacheAnomalySink;
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
/**
|
|
@@ -115,6 +119,19 @@ function modelSupportsUsageInStreaming(model: unknown): boolean {
|
|
|
115
119
|
return isObject(compat) && compat.supportsUsageInStreaming === true;
|
|
116
120
|
}
|
|
117
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Whether the model's effective compat sends the `max_completion_tokens`
|
|
124
|
+
* request field instead of the classic `max_tokens` (issue #34). The model
|
|
125
|
+
* object here is pi-ai's post-registry view — baseline catalog, live-merge
|
|
126
|
+
* refresh, `models.json` override and the #19 stale-store overlay all flow
|
|
127
|
+
* through `compat`, so this reads the value the user actually runs with.
|
|
128
|
+
*/
|
|
129
|
+
function modelSendsMaxCompletionTokens(model: unknown): boolean {
|
|
130
|
+
if (!isObject(model)) return false;
|
|
131
|
+
const compat = model.compat;
|
|
132
|
+
return isObject(compat) && compat.maxTokensField === "max_completion_tokens";
|
|
133
|
+
}
|
|
134
|
+
|
|
118
135
|
export function wrapApiForStrictSanitization(api: ProviderStreams): ProviderStreams {
|
|
119
136
|
const withSanitizer = <TOptions extends object | undefined>(options: TOptions): TOptions => {
|
|
120
137
|
const userOnPayload = isObject(options) ? (options.onPayload as unknown) : undefined;
|
|
@@ -123,6 +140,7 @@ export function wrapApiForStrictSanitization(api: ProviderStreams): ProviderStre
|
|
|
123
140
|
onPayload: async (payload: unknown, model: unknown) => {
|
|
124
141
|
const sanitized = sanitizeOpenAICompatPayload(payload, {
|
|
125
142
|
preserveStreamOptions: modelSupportsUsageInStreaming(model),
|
|
143
|
+
preserveMaxCompletionTokens: modelSendsMaxCompletionTokens(model),
|
|
126
144
|
});
|
|
127
145
|
if (typeof userOnPayload === "function") {
|
|
128
146
|
const userResult = await (userOnPayload as (p: unknown, m: unknown) => unknown)(sanitized, model);
|
|
@@ -181,16 +199,31 @@ function withStaleCatalogOverlay(
|
|
|
181
199
|
// read without rebuilding the catalog.
|
|
182
200
|
const policy = <T extends { headers?: Record<string, string> }>(m: T): T =>
|
|
183
201
|
providerAttributionEnabled() ? m : stripProviderAttribution(m);
|
|
202
|
+
// Issue #32: a pre-#15 models-store.json persists non-chat endpoint ids
|
|
203
|
+
// (kokoro, whisper, rerank, …) as CHAT entries. The #19 overlay corrects
|
|
204
|
+
// capabilities but cannot drop ids, so those entries surfaced as chat models
|
|
205
|
+
// and a catalog refresh did not clear them. Drop them on the read paths,
|
|
206
|
+
// keyed on the runtime source of truth (NON_CHAT_MODEL_IDS), but ONLY for
|
|
207
|
+
// chat-type entries: flux-2-klein / qwen-image-2.1 are legitimate baseline
|
|
208
|
+
// IMAGE models (type: "image") and must survive. Healthy paths are already
|
|
209
|
+
// clean (baselineModels and mergeLiveWithGenerated exclude non-chat ids),
|
|
210
|
+
// so this filter is a no-op outside the stale-restore scenario.
|
|
211
|
+
const isStoredNonChatChatEntry = <T extends { id: string; type?: string }>(m: T): boolean =>
|
|
212
|
+
m.type !== "image" && NON_CHAT_MODEL_IDS[m.id] !== undefined;
|
|
184
213
|
return {
|
|
185
214
|
...provider,
|
|
186
215
|
getModels: () => {
|
|
187
216
|
const models = provider.getModels();
|
|
188
|
-
return models
|
|
217
|
+
return models
|
|
218
|
+
.filter((m) => !isStoredNonChatChatEntry(m))
|
|
219
|
+
.map((m) => policy(applyOverlay(m, overlayMap))) as typeof models;
|
|
189
220
|
},
|
|
190
221
|
getAllModels: () => {
|
|
191
222
|
const all = provider.getAllModels?.();
|
|
192
223
|
if (!all) return [];
|
|
193
|
-
return all
|
|
224
|
+
return all
|
|
225
|
+
.filter((m) => !isStoredNonChatChatEntry(m))
|
|
226
|
+
.map((m) => policy(applyOverlay(m, overlayMap)));
|
|
194
227
|
},
|
|
195
228
|
};
|
|
196
229
|
}
|
|
@@ -268,8 +301,14 @@ export async function createNanCompatibleProvider(
|
|
|
268
301
|
// middle → guard (marker detection), innermost → sanitizer (payload).
|
|
269
302
|
// The guard's onProviderStreamEvent hook sees the raw response chunks
|
|
270
303
|
// (NOT onPayload, which only receives the outgoing request params).
|
|
271
|
-
|
|
272
|
-
|
|
304
|
+
// Outermost: cache-anomaly evidence capture (issue #35). It only adds
|
|
305
|
+
// observation hooks (onResponse for `cf-ray`, onProviderStreamEvent for
|
|
306
|
+
// the raw id/created pair) and never rewrites the stream.
|
|
307
|
+
api: withCacheAnomalyCapture(
|
|
308
|
+
withContextOverflowClassification(
|
|
309
|
+
withReasoningOnlyStreamGuard(wrapApiForStrictSanitization(apiFactory())),
|
|
310
|
+
),
|
|
311
|
+
{ sink: options.cacheAnomalySink ?? createCacheAnomalyFileSink() },
|
|
273
312
|
),
|
|
274
313
|
}),
|
|
275
314
|
overlayMap,
|
package/src/zip.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal deterministic ZIP writer (store method, no compression).
|
|
3
|
+
*
|
|
4
|
+
* `/nan-debug --emit` must produce a shareable archive that is reproducible
|
|
5
|
+
* byte-for-byte from the same inputs (issue #35 S15). `Bun.Archive` is
|
|
6
|
+
* Bun-only, and this package also runs under Node, so the container is built
|
|
7
|
+
* here with `node:buffer` only: sorted entries, fixed 1980-01-01 timestamps,
|
|
8
|
+
* stored (uncompressed) payloads and the UTF-8 name flag. That also keeps the
|
|
9
|
+
* output readable with any unzip tool.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Buffer } from "node:buffer";
|
|
13
|
+
|
|
14
|
+
const LOCAL_FILE_HEADER = 0x04034b50;
|
|
15
|
+
const CENTRAL_DIRECTORY_HEADER = 0x02014b50;
|
|
16
|
+
const END_OF_CENTRAL_DIRECTORY = 0x06054b50;
|
|
17
|
+
const VERSION_NEEDED = 20;
|
|
18
|
+
const UTF8_FLAG = 0x0800;
|
|
19
|
+
const STORED_METHOD = 0;
|
|
20
|
+
/** 1980-01-01 00:00:00, the DOS epoch: fixed for reproducible archives. */
|
|
21
|
+
const DOS_TIME = 0;
|
|
22
|
+
const DOS_DATE = 0x0021;
|
|
23
|
+
|
|
24
|
+
const CRC_TABLE: Uint32Array = (() => {
|
|
25
|
+
const table = new Uint32Array(256);
|
|
26
|
+
for (let n = 0; n < 256; n++) {
|
|
27
|
+
let c = n;
|
|
28
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
29
|
+
table[n] = c >>> 0;
|
|
30
|
+
}
|
|
31
|
+
return table;
|
|
32
|
+
})();
|
|
33
|
+
|
|
34
|
+
/** CRC-32 (IEEE 802.3), as required by the ZIP container. */
|
|
35
|
+
export function crc32(data: Uint8Array): number {
|
|
36
|
+
let crc = 0xffffffff;
|
|
37
|
+
for (let i = 0; i < data.length; i++) {
|
|
38
|
+
crc = CRC_TABLE[(crc ^ data[i]!) & 0xff]! ^ (crc >>> 8);
|
|
39
|
+
}
|
|
40
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ZipEntry {
|
|
44
|
+
name: string;
|
|
45
|
+
data: Uint8Array | string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Build a store-method zip from the given entries. Entries are sorted by name
|
|
50
|
+
* so the output does not depend on object/insertion order.
|
|
51
|
+
*/
|
|
52
|
+
export function createZip(entries: readonly ZipEntry[]): Buffer {
|
|
53
|
+
const sorted = [...entries].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
54
|
+
const locals: Buffer[] = [];
|
|
55
|
+
const centrals: Buffer[] = [];
|
|
56
|
+
let offset = 0;
|
|
57
|
+
|
|
58
|
+
for (const entry of sorted) {
|
|
59
|
+
const name = Buffer.from(entry.name, "utf8");
|
|
60
|
+
const data = typeof entry.data === "string" ? Buffer.from(entry.data, "utf8") : Buffer.from(entry.data);
|
|
61
|
+
const crc = crc32(data);
|
|
62
|
+
|
|
63
|
+
const local = Buffer.alloc(30 + name.length);
|
|
64
|
+
local.writeUInt32LE(LOCAL_FILE_HEADER, 0);
|
|
65
|
+
local.writeUInt16LE(VERSION_NEEDED, 4);
|
|
66
|
+
local.writeUInt16LE(UTF8_FLAG, 6);
|
|
67
|
+
local.writeUInt16LE(STORED_METHOD, 8);
|
|
68
|
+
local.writeUInt16LE(DOS_TIME, 10);
|
|
69
|
+
local.writeUInt16LE(DOS_DATE, 12);
|
|
70
|
+
local.writeUInt32LE(crc, 14);
|
|
71
|
+
local.writeUInt32LE(data.length, 18);
|
|
72
|
+
local.writeUInt32LE(data.length, 22);
|
|
73
|
+
local.writeUInt16LE(name.length, 26);
|
|
74
|
+
local.writeUInt16LE(0, 28);
|
|
75
|
+
name.copy(local, 30);
|
|
76
|
+
locals.push(local, data);
|
|
77
|
+
|
|
78
|
+
const central = Buffer.alloc(46 + name.length);
|
|
79
|
+
central.writeUInt32LE(CENTRAL_DIRECTORY_HEADER, 0);
|
|
80
|
+
central.writeUInt16LE(VERSION_NEEDED, 4);
|
|
81
|
+
central.writeUInt16LE(VERSION_NEEDED, 6);
|
|
82
|
+
central.writeUInt16LE(UTF8_FLAG, 8);
|
|
83
|
+
central.writeUInt16LE(STORED_METHOD, 10);
|
|
84
|
+
central.writeUInt16LE(DOS_TIME, 12);
|
|
85
|
+
central.writeUInt16LE(DOS_DATE, 14);
|
|
86
|
+
central.writeUInt32LE(crc, 16);
|
|
87
|
+
central.writeUInt32LE(data.length, 20);
|
|
88
|
+
central.writeUInt32LE(data.length, 24);
|
|
89
|
+
central.writeUInt16LE(name.length, 28);
|
|
90
|
+
central.writeUInt16LE(0, 30);
|
|
91
|
+
central.writeUInt16LE(0, 32);
|
|
92
|
+
central.writeUInt16LE(0, 34);
|
|
93
|
+
central.writeUInt16LE(0, 36);
|
|
94
|
+
central.writeUInt32LE(0, 38);
|
|
95
|
+
central.writeUInt32LE(offset, 42);
|
|
96
|
+
name.copy(central, 46);
|
|
97
|
+
centrals.push(central);
|
|
98
|
+
|
|
99
|
+
offset += local.length + data.length;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const centralDirectory = Buffer.concat(centrals);
|
|
103
|
+
const end = Buffer.alloc(22);
|
|
104
|
+
end.writeUInt32LE(END_OF_CENTRAL_DIRECTORY, 0);
|
|
105
|
+
end.writeUInt16LE(0, 4);
|
|
106
|
+
end.writeUInt16LE(0, 6);
|
|
107
|
+
end.writeUInt16LE(sorted.length, 8);
|
|
108
|
+
end.writeUInt16LE(sorted.length, 10);
|
|
109
|
+
end.writeUInt32LE(centralDirectory.length, 12);
|
|
110
|
+
end.writeUInt32LE(offset, 16);
|
|
111
|
+
end.writeUInt16LE(0, 20);
|
|
112
|
+
|
|
113
|
+
return Buffer.concat([...locals, centralDirectory, end]);
|
|
114
|
+
}
|