@gtrabanco/pi-nan-provider 1.5.2 → 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/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 +25 -2
- 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,
|
|
@@ -71,6 +72,8 @@ export interface NanCompatibleProviderOptions {
|
|
|
71
72
|
timeoutMs?: number;
|
|
72
73
|
/** Injectable for tests; defaults to global fetch. */
|
|
73
74
|
fetchImpl?: typeof fetch;
|
|
75
|
+
/** Sink for the issue #35 cache-anomaly evidence; defaults to the agent-dir JSONL file. */
|
|
76
|
+
cacheAnomalySink?: CacheAnomalySink;
|
|
74
77
|
}
|
|
75
78
|
|
|
76
79
|
/**
|
|
@@ -116,6 +119,19 @@ function modelSupportsUsageInStreaming(model: unknown): boolean {
|
|
|
116
119
|
return isObject(compat) && compat.supportsUsageInStreaming === true;
|
|
117
120
|
}
|
|
118
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
|
+
|
|
119
135
|
export function wrapApiForStrictSanitization(api: ProviderStreams): ProviderStreams {
|
|
120
136
|
const withSanitizer = <TOptions extends object | undefined>(options: TOptions): TOptions => {
|
|
121
137
|
const userOnPayload = isObject(options) ? (options.onPayload as unknown) : undefined;
|
|
@@ -124,6 +140,7 @@ export function wrapApiForStrictSanitization(api: ProviderStreams): ProviderStre
|
|
|
124
140
|
onPayload: async (payload: unknown, model: unknown) => {
|
|
125
141
|
const sanitized = sanitizeOpenAICompatPayload(payload, {
|
|
126
142
|
preserveStreamOptions: modelSupportsUsageInStreaming(model),
|
|
143
|
+
preserveMaxCompletionTokens: modelSendsMaxCompletionTokens(model),
|
|
127
144
|
});
|
|
128
145
|
if (typeof userOnPayload === "function") {
|
|
129
146
|
const userResult = await (userOnPayload as (p: unknown, m: unknown) => unknown)(sanitized, model);
|
|
@@ -284,8 +301,14 @@ export async function createNanCompatibleProvider(
|
|
|
284
301
|
// middle → guard (marker detection), innermost → sanitizer (payload).
|
|
285
302
|
// The guard's onProviderStreamEvent hook sees the raw response chunks
|
|
286
303
|
// (NOT onPayload, which only receives the outgoing request params).
|
|
287
|
-
|
|
288
|
-
|
|
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() },
|
|
289
312
|
),
|
|
290
313
|
}),
|
|
291
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
|
+
}
|