@gtrabanco/pi-nan-provider 1.6.0 → 1.6.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 +67 -7
- package/README.es.md +128 -26
- package/README.md +118 -24
- package/package.json +1 -1
- package/scripts/generate-models.ts +17 -0
- package/scripts/model-facts.ts +247 -0
- package/scripts/models.generated.ts +4 -4
- package/src/debug-command.ts +11 -6
- package/src/debug-report.ts +464 -74
- package/src/http-attempts.ts +615 -0
- package/src/index.ts +26 -0
- package/src/model-facts-core.ts +424 -0
- package/src/model-facts.ts +98 -0
- package/src/provider-factory.ts +21 -11
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NaN model-facts schema, provenance table and builder (issue #47).
|
|
3
|
+
*
|
|
4
|
+
* The provider already knows the fleet's limits, but they live in prose and in
|
|
5
|
+
* pi-only capability data. This module turns them into one typed record that a
|
|
6
|
+
* consumer can read: facts only (what a model *is*), never policy (how a
|
|
7
|
+
* consumer should batch or route work), and every cell carrying a provenance id
|
|
8
|
+
* from the closed set below.
|
|
9
|
+
*
|
|
10
|
+
* Why a separate module from `src/model-facts.ts`: this file must stay free of
|
|
11
|
+
* catalog imports so `scripts/generate-models.ts` can validate the freshly
|
|
12
|
+
* generated entries before they are written. The public frozen record lives in
|
|
13
|
+
* `src/model-facts.ts`, which imports the generated catalog and this builder.
|
|
14
|
+
*
|
|
15
|
+
* Provenance model: a cell's value is always paired with a `NanFactSource` id.
|
|
16
|
+
* `NAN_FACT_SOURCES` resolves each id to its citation (URL and check date, or a
|
|
17
|
+
* dated measurement already recorded in the repo). Three ids point into the
|
|
18
|
+
* entry's own `notes` — `override`, `compat` and `catalog-cost` — because the
|
|
19
|
+
* generated catalog already records those provenance notes there; duplicating
|
|
20
|
+
* them per cell would fork the same truth into two places.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { OpenAICompletionsCompat, ThinkingLevelMap } from "@earendil-works/pi-ai";
|
|
24
|
+
import type { GeneratedModelEntry } from "./fetch-models.ts";
|
|
25
|
+
|
|
26
|
+
// ── Documented value sets (membership-pinned) ───────────────────────────────
|
|
27
|
+
|
|
28
|
+
/** Tool protocol the model's tool calling rides on (docs `capabilities`). */
|
|
29
|
+
export type NanToolCalling = "openai-tools" | "xml" | "unmeasured";
|
|
30
|
+
export const NAN_TOOL_CALLING_VALUES: readonly NanToolCalling[] = ["openai-tools", "xml", "unmeasured"];
|
|
31
|
+
|
|
32
|
+
/** Whether `max_tokens` caps reasoning + answer, or only the visible answer. */
|
|
33
|
+
export type NanMaxTokensSemantics = "bounds-total" | "bounds-visible-answer" | "unmeasured";
|
|
34
|
+
export const NAN_MAX_TOKENS_SEMANTICS_VALUES: readonly NanMaxTokensSemantics[] = [
|
|
35
|
+
"bounds-total",
|
|
36
|
+
"bounds-visible-answer",
|
|
37
|
+
"unmeasured",
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
/** Whether the gateway honours a `thinking_budget` on the model. */
|
|
41
|
+
export type NanThinkingBudget = "honoured" | "ignored" | "unmeasured";
|
|
42
|
+
export const NAN_THINKING_BUDGET_VALUES: readonly NanThinkingBudget[] = [
|
|
43
|
+
"honoured",
|
|
44
|
+
"ignored",
|
|
45
|
+
"unmeasured",
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
/** Whether the prompt cache engages automatically or needs an opt-in. */
|
|
49
|
+
export type NanCacheMode = "automatic" | "opt-in" | "unsupported" | "unmeasured";
|
|
50
|
+
export const NAN_CACHE_MODE_VALUES: readonly NanCacheMode[] = [
|
|
51
|
+
"automatic",
|
|
52
|
+
"opt-in",
|
|
53
|
+
"unsupported",
|
|
54
|
+
"unmeasured",
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
/** How NaN bills the model: credits standard, credits faster (larger), or quota. */
|
|
58
|
+
export type NanBillingClass = "quota-only" | "credit-standard" | "credit-larger";
|
|
59
|
+
export const NAN_BILLING_CLASS_VALUES: readonly NanBillingClass[] = [
|
|
60
|
+
"quota-only",
|
|
61
|
+
"credit-standard",
|
|
62
|
+
"credit-larger",
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
/** A tri-state capability fact: measured true, measured false, or not measured. */
|
|
66
|
+
export type NanTriState = boolean | "unmeasured";
|
|
67
|
+
|
|
68
|
+
// ── Provenance ──────────────────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Closed set of provenance ids. Every exported cell names one of these, and
|
|
72
|
+
* `NAN_MODEL_FACTS_META.sources` resolves it for a consumer. Adding a fact
|
|
73
|
+
* means adding a citation here first; a cell cannot be sourced from nowhere.
|
|
74
|
+
*/
|
|
75
|
+
export const NAN_FACT_SOURCES = Object.freeze({
|
|
76
|
+
"models.dev":
|
|
77
|
+
"https://models.dev/api.json, provider \"nan\" — the numeric limits, modalities and reasoning flag come from the generated catalog (fetch date in NAN_MODEL_FACTS_META.fetchedAt)",
|
|
78
|
+
override:
|
|
79
|
+
"scripts/manual-overrides.ts MANUAL_OVERRIDES — the value is a recorded override, and the entry's notes carry its per-model provenance note (source URL and date)",
|
|
80
|
+
compat:
|
|
81
|
+
"the shared NAN_COMPAT constant in scripts/generate-models.ts — the compat note in the entry's notes carries its provenance (2026-09-04 config, 2026-09-13 stream close, 2026-09-16 streaming usage)",
|
|
82
|
+
"catalog-cost":
|
|
83
|
+
"the shared NAN_CATALOG_COST constant in scripts/generate-models.ts — the cost note in the entry's notes records the credit-pool decision (NaN bills credits, not per-token USD)",
|
|
84
|
+
docs: "https://nan.builders/docs/models (checked 2026-11-23)",
|
|
85
|
+
"docs-reasoning": "https://nan.builders/docs/models#controlling-reasoning (checked 2026-11-23)",
|
|
86
|
+
"docs-cross-model":
|
|
87
|
+
"https://nan.builders/docs/models (checked 2026-11-23) — stated on the deepseek-v4-flash model card, which names the GLM models, qwen3.6 and gemma4",
|
|
88
|
+
"model-guide": "https://nan.builders/model-guide (NaN community model guide, November 2026)",
|
|
89
|
+
"measured-18":
|
|
90
|
+
"issue #18 cold-prompt probe, 2026-10-01 — endpoint-wide (Cloudflare 120 s proxy read timeout), not a model capability",
|
|
91
|
+
"measured-36":
|
|
92
|
+
"issue #36 closing probe, 2026-11-18 — qwen3.8-flash with max_tokens=64 returned completion_tokens=64 (reasoning_tokens=64, empty content), so max_tokens bounds the total again",
|
|
93
|
+
none: "no source: the cell is the \"unmeasured\" sentinel or a field the catalog does not declare; the entry's notes explain why",
|
|
94
|
+
} as const);
|
|
95
|
+
|
|
96
|
+
/** Provenance id of a single exported cell. */
|
|
97
|
+
export type NanFactSource = keyof typeof NAN_FACT_SOURCES;
|
|
98
|
+
|
|
99
|
+
// ── Cells ───────────────────────────────────────────────────────────────────
|
|
100
|
+
|
|
101
|
+
/** Cells the generated catalog already carried (issue #47, "already carried"). */
|
|
102
|
+
export const CARRIED_FACT_CELLS = [
|
|
103
|
+
"contextWindow",
|
|
104
|
+
"maxTokens",
|
|
105
|
+
"input",
|
|
106
|
+
"cost",
|
|
107
|
+
"reasoning",
|
|
108
|
+
"reasoningEffortValues",
|
|
109
|
+
"thinkingLevelMap",
|
|
110
|
+
"compat",
|
|
111
|
+
] as const;
|
|
112
|
+
|
|
113
|
+
/** Cells this issue promotes out of README prose into data. */
|
|
114
|
+
export const NEW_FACT_CELLS = [
|
|
115
|
+
"toolCalling",
|
|
116
|
+
"maxTokensSemantics",
|
|
117
|
+
"thinkingBudget",
|
|
118
|
+
"coldCeilingTokens",
|
|
119
|
+
"cacheMode",
|
|
120
|
+
"billingClass",
|
|
121
|
+
"parallelToolCalls",
|
|
122
|
+
"structuredOutput",
|
|
123
|
+
] as const;
|
|
124
|
+
|
|
125
|
+
/** Every exported fact cell. A record row carries one provenance id per entry. */
|
|
126
|
+
export const FACT_CELLS = [...CARRIED_FACT_CELLS, ...NEW_FACT_CELLS] as const;
|
|
127
|
+
export type NanCarriedFactCell = (typeof CARRIED_FACT_CELLS)[number];
|
|
128
|
+
export type NanNewFactCell = (typeof NEW_FACT_CELLS)[number];
|
|
129
|
+
export type NanFactCell = (typeof FACT_CELLS)[number];
|
|
130
|
+
|
|
131
|
+
// ── The exported record ─────────────────────────────────────────────────────
|
|
132
|
+
|
|
133
|
+
/** Cost rates the catalog carries (all pinned to 0 by the credit-pool decision). */
|
|
134
|
+
export interface NanModelCostFact {
|
|
135
|
+
input: number;
|
|
136
|
+
output: number;
|
|
137
|
+
cacheRead: number;
|
|
138
|
+
cacheWrite: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* One model's facts. Plain data only: no pi Model object, no `api`/`baseUrl`,
|
|
143
|
+
* no policy (batching, routing ladders), no rate limits — deliberately out of
|
|
144
|
+
* scope per issue #47 (S18/C15): the docs publish rpm/concurrency figures
|
|
145
|
+
* (checked 2026-11-23), so their absence here is the frozen scope decision,
|
|
146
|
+
* and adding the field is a future owner decision.
|
|
147
|
+
*/
|
|
148
|
+
export interface NanModelFacts {
|
|
149
|
+
id: string;
|
|
150
|
+
name: string;
|
|
151
|
+
contextWindow: number;
|
|
152
|
+
maxTokens: number;
|
|
153
|
+
input: readonly ("text" | "image")[];
|
|
154
|
+
cost: Readonly<NanModelCostFact>;
|
|
155
|
+
reasoning: boolean;
|
|
156
|
+
/** Empty array = the effort parameter is accepted but depth is model-managed. */
|
|
157
|
+
reasoningEffortValues: readonly string[];
|
|
158
|
+
/** Empty map = no thinking level is mapped for this model. */
|
|
159
|
+
thinkingLevelMap: Readonly<ThinkingLevelMap>;
|
|
160
|
+
compat: Readonly<OpenAICompletionsCompat>;
|
|
161
|
+
toolCalling: NanToolCalling;
|
|
162
|
+
maxTokensSemantics: NanMaxTokensSemantics;
|
|
163
|
+
thinkingBudget: NanThinkingBudget;
|
|
164
|
+
/**
|
|
165
|
+
* Per-model cold-prompt ceiling. Always `"unmeasured"` today: the only
|
|
166
|
+
* measurement is endpoint-wide, so it ships as `NAN_ENDPOINT_FACTS` rather
|
|
167
|
+
* than as a per-model claim.
|
|
168
|
+
*/
|
|
169
|
+
coldCeilingTokens: number | "unmeasured";
|
|
170
|
+
cacheMode: NanCacheMode;
|
|
171
|
+
billingClass: NanBillingClass;
|
|
172
|
+
parallelToolCalls: NanTriState;
|
|
173
|
+
structuredOutput: NanTriState;
|
|
174
|
+
/** One provenance id per exported cell. */
|
|
175
|
+
provenance: Readonly<Record<NanFactCell, NanFactSource>>;
|
|
176
|
+
/** The generated catalog's provenance notes (they carry the override/compat/cost citations). */
|
|
177
|
+
notes: readonly string[];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Build-time metadata every facts record is stamped with. */
|
|
181
|
+
export interface ModelFactsBuildMeta {
|
|
182
|
+
source: string;
|
|
183
|
+
modelsDevProvider: string;
|
|
184
|
+
fetchedAt: string;
|
|
185
|
+
modelCount: number;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Public metadata: the build stamp plus the citation table. */
|
|
189
|
+
export interface NanModelFactsMeta extends ModelFactsBuildMeta {
|
|
190
|
+
sources: typeof NAN_FACT_SOURCES;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The measured, endpoint-wide cold-prompt ceiling (not a model capability). */
|
|
194
|
+
export interface NanEndpointFacts {
|
|
195
|
+
/** Conservative ceiling below the last working cold measurement (208,036 tokens). */
|
|
196
|
+
coldCacheCeilingTokens: number;
|
|
197
|
+
/** Date the cold probes ran. */
|
|
198
|
+
coldCeilingMeasuredAt: string;
|
|
199
|
+
/** What the number describes — never a per-model capability. */
|
|
200
|
+
coldCeilingScope: string;
|
|
201
|
+
/** The measurement itself, as recorded in the repo. */
|
|
202
|
+
coldCeilingSources: string;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ── The manual declaration a model must ship ────────────────────────────────
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The eight new facts plus one provenance id per fact. Declared per model in
|
|
209
|
+
* `scripts/model-facts.ts` (the manual-override path), validated at generation
|
|
210
|
+
* time and again when the public record is built.
|
|
211
|
+
*/
|
|
212
|
+
export interface ModelFactsDeclaration {
|
|
213
|
+
toolCalling: NanToolCalling;
|
|
214
|
+
maxTokensSemantics: NanMaxTokensSemantics;
|
|
215
|
+
thinkingBudget: NanThinkingBudget;
|
|
216
|
+
coldCeilingTokens: number | "unmeasured";
|
|
217
|
+
cacheMode: NanCacheMode;
|
|
218
|
+
billingClass: NanBillingClass;
|
|
219
|
+
parallelToolCalls: NanTriState;
|
|
220
|
+
structuredOutput: NanTriState;
|
|
221
|
+
sources: Readonly<Record<NanNewFactCell, NanFactSource>>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Declarations keyed by model id. */
|
|
225
|
+
export type ModelFactsDeclarations = Readonly<Record<string, ModelFactsDeclaration>>;
|
|
226
|
+
|
|
227
|
+
// ── Builder ─────────────────────────────────────────────────────────────────
|
|
228
|
+
|
|
229
|
+
/** The fields of an override entry that change a carried cell's provenance. */
|
|
230
|
+
const CARRIED_OVERRIDE_FIELDS = [
|
|
231
|
+
"contextWindow",
|
|
232
|
+
"maxTokens",
|
|
233
|
+
"input",
|
|
234
|
+
"reasoning",
|
|
235
|
+
"cost",
|
|
236
|
+
"reasoningEffortValues",
|
|
237
|
+
"thinkingLevelMap",
|
|
238
|
+
"maxTokensField",
|
|
239
|
+
] as const;
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Reduce an override map to the field names each model declares. Keeping this
|
|
243
|
+
* as a key list (instead of the override objects) keeps this module free of a
|
|
244
|
+
* runtime import from `scripts/`, so the generator can use it safely.
|
|
245
|
+
*/
|
|
246
|
+
export function overrideFieldKeys(
|
|
247
|
+
overrides: Readonly<Record<string, object>>,
|
|
248
|
+
): Readonly<Record<string, readonly string[]>> {
|
|
249
|
+
const keys: Record<string, readonly string[]> = {};
|
|
250
|
+
for (const [id, override] of Object.entries(overrides)) {
|
|
251
|
+
keys[id] = Object.keys(override);
|
|
252
|
+
}
|
|
253
|
+
return keys;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const VALUED_NEW_FACTS: Record<string, readonly unknown[]> = {
|
|
257
|
+
toolCalling: NAN_TOOL_CALLING_VALUES,
|
|
258
|
+
maxTokensSemantics: NAN_MAX_TOKENS_SEMANTICS_VALUES,
|
|
259
|
+
thinkingBudget: NAN_THINKING_BUDGET_VALUES,
|
|
260
|
+
cacheMode: NAN_CACHE_MODE_VALUES,
|
|
261
|
+
billingClass: NAN_BILLING_CLASS_VALUES,
|
|
262
|
+
parallelToolCalls: [true, false, "unmeasured"],
|
|
263
|
+
structuredOutput: [true, false, "unmeasured"],
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
function validateDeclaration(
|
|
267
|
+
id: string,
|
|
268
|
+
declaration: ModelFactsDeclaration,
|
|
269
|
+
problems: string[],
|
|
270
|
+
): void {
|
|
271
|
+
for (const [cell, allowed] of Object.entries(VALUED_NEW_FACTS)) {
|
|
272
|
+
const value = (declaration as unknown as Record<string, unknown>)[cell];
|
|
273
|
+
if (!allowed.includes(value)) {
|
|
274
|
+
problems.push(
|
|
275
|
+
`model-facts: ${id}: ${cell} value ${JSON.stringify(value)} is not one of ${allowed
|
|
276
|
+
.map((v) => JSON.stringify(v))
|
|
277
|
+
.join(", ")}`,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const cold = declaration.coldCeilingTokens;
|
|
282
|
+
if (cold !== "unmeasured" && !(typeof cold === "number" && Number.isFinite(cold) && cold > 0)) {
|
|
283
|
+
problems.push(
|
|
284
|
+
`model-facts: ${id}: coldCeilingTokens must be a positive number or "unmeasured" (got ${JSON.stringify(cold)})`,
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
const sources = declaration.sources as Readonly<Record<string, unknown>> | undefined;
|
|
288
|
+
for (const cell of NEW_FACT_CELLS) {
|
|
289
|
+
const source = sources?.[cell];
|
|
290
|
+
if (typeof source !== "string" || !Object.hasOwn(NAN_FACT_SOURCES, source)) {
|
|
291
|
+
problems.push(
|
|
292
|
+
`model-facts: ${id}: ${cell} has no provenance source (expected one of ${Object.keys(
|
|
293
|
+
NAN_FACT_SOURCES,
|
|
294
|
+
).join(", ")}, got ${JSON.stringify(source)})`,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function carriedSource(
|
|
301
|
+
declaredFields: readonly string[],
|
|
302
|
+
field: (typeof CARRIED_OVERRIDE_FIELDS)[number],
|
|
303
|
+
fallback: NanFactSource,
|
|
304
|
+
): NanFactSource {
|
|
305
|
+
return declaredFields.includes(field) ? "override" : fallback;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Build the per-model facts record from generated catalog entries plus the
|
|
310
|
+
* manual declarations. Throws (aggregating every problem) when the catalog and
|
|
311
|
+
* the declarations disagree, when a documented value is outside its union, or
|
|
312
|
+
* when a cell has no provenance — the generator turns that throw into a
|
|
313
|
+
* non-zero exit, so a published catalog can never carry an unsourced cell.
|
|
314
|
+
*/
|
|
315
|
+
export function buildModelFacts(
|
|
316
|
+
entries: readonly GeneratedModelEntry[],
|
|
317
|
+
declarations: ModelFactsDeclarations,
|
|
318
|
+
overrideKeys: Readonly<Record<string, readonly string[]>>,
|
|
319
|
+
meta: ModelFactsBuildMeta,
|
|
320
|
+
): Record<string, NanModelFacts> {
|
|
321
|
+
const problems: string[] = [];
|
|
322
|
+
const entryIds = new Set(entries.map((entry) => entry.id));
|
|
323
|
+
|
|
324
|
+
for (const id of entryIds) {
|
|
325
|
+
if (!Object.hasOwn(declarations, id)) {
|
|
326
|
+
problems.push(`model-facts: ${id}: catalog model has no facts declaration in scripts/model-facts.ts`);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
for (const id of Object.keys(declarations)) {
|
|
330
|
+
if (!entryIds.has(id)) {
|
|
331
|
+
problems.push(`model-facts: ${id}: declared in scripts/model-facts.ts but absent from the catalog`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const record: Record<string, NanModelFacts> = {};
|
|
336
|
+
for (const entry of [...entries].sort((a, b) => a.id.localeCompare(b.id))) {
|
|
337
|
+
const declaration = declarations[entry.id];
|
|
338
|
+
if (!declaration) continue; // already reported above
|
|
339
|
+
validateDeclaration(entry.id, declaration, problems);
|
|
340
|
+
|
|
341
|
+
const declaredFields = overrideKeys[entry.id] ?? [];
|
|
342
|
+
const provenance: Record<NanFactCell, NanFactSource> = {
|
|
343
|
+
contextWindow: carriedSource(declaredFields, "contextWindow", "models.dev"),
|
|
344
|
+
maxTokens: carriedSource(declaredFields, "maxTokens", "models.dev"),
|
|
345
|
+
input: carriedSource(declaredFields, "input", "models.dev"),
|
|
346
|
+
reasoning: carriedSource(declaredFields, "reasoning", "models.dev"),
|
|
347
|
+
cost: carriedSource(declaredFields, "cost", "catalog-cost"),
|
|
348
|
+
compat: carriedSource(declaredFields, "maxTokensField", entry.compat ? "compat" : "none"),
|
|
349
|
+
reasoningEffortValues: declaredFields.includes("reasoningEffortValues") ? "override" : "none",
|
|
350
|
+
thinkingLevelMap: declaredFields.includes("thinkingLevelMap") ? "override" : "none",
|
|
351
|
+
toolCalling: declaration.sources.toolCalling,
|
|
352
|
+
maxTokensSemantics: declaration.sources.maxTokensSemantics,
|
|
353
|
+
thinkingBudget: declaration.sources.thinkingBudget,
|
|
354
|
+
coldCeilingTokens: declaration.sources.coldCeilingTokens,
|
|
355
|
+
cacheMode: declaration.sources.cacheMode,
|
|
356
|
+
billingClass: declaration.sources.billingClass,
|
|
357
|
+
parallelToolCalls: declaration.sources.parallelToolCalls,
|
|
358
|
+
structuredOutput: declaration.sources.structuredOutput,
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
// An `unmeasured` new-fact cell means "no claim". Sourcing it from a
|
|
362
|
+
// document would read as a claim that the document says so.
|
|
363
|
+
for (const cell of NEW_FACT_CELLS) {
|
|
364
|
+
const value = declaration[cell];
|
|
365
|
+
if (value === "unmeasured" && provenance[cell] !== "none") {
|
|
366
|
+
problems.push(
|
|
367
|
+
`model-facts: ${entry.id}: ${cell} is "unmeasured" but is sourced as ${JSON.stringify(
|
|
368
|
+
provenance[cell],
|
|
369
|
+
)} — an unmeasured cell carries no source`,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
record[entry.id] = {
|
|
375
|
+
id: entry.id,
|
|
376
|
+
name: entry.name,
|
|
377
|
+
contextWindow: entry.contextWindow,
|
|
378
|
+
maxTokens: entry.maxTokens,
|
|
379
|
+
input: [...entry.input],
|
|
380
|
+
cost: { ...entry.cost },
|
|
381
|
+
reasoning: entry.reasoning,
|
|
382
|
+
reasoningEffortValues: [...(entry.reasoningEffortValues ?? [])],
|
|
383
|
+
thinkingLevelMap: { ...(entry.thinkingLevelMap ?? {}) },
|
|
384
|
+
compat: { ...(entry.compat ?? {}) },
|
|
385
|
+
toolCalling: declaration.toolCalling,
|
|
386
|
+
maxTokensSemantics: declaration.maxTokensSemantics,
|
|
387
|
+
thinkingBudget: declaration.thinkingBudget,
|
|
388
|
+
coldCeilingTokens: declaration.coldCeilingTokens,
|
|
389
|
+
cacheMode: declaration.cacheMode,
|
|
390
|
+
billingClass: declaration.billingClass,
|
|
391
|
+
parallelToolCalls: declaration.parallelToolCalls,
|
|
392
|
+
structuredOutput: declaration.structuredOutput,
|
|
393
|
+
provenance,
|
|
394
|
+
notes: [...(entry.notes ?? [])],
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (problems.length > 0) {
|
|
399
|
+
throw new Error(problems.join("\n"));
|
|
400
|
+
}
|
|
401
|
+
if (Object.keys(record).length !== meta.modelCount) {
|
|
402
|
+
throw new Error(
|
|
403
|
+
`model-facts: built ${Object.keys(record).length} entries but the catalog reports ${meta.modelCount}`,
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
return record;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Deep-freeze the built record so a consumer cannot mutate another's view. */
|
|
410
|
+
export function freezeModelFacts(
|
|
411
|
+
facts: Record<string, NanModelFacts>,
|
|
412
|
+
): Readonly<Record<string, NanModelFacts>> {
|
|
413
|
+
for (const entry of Object.values(facts)) {
|
|
414
|
+
Object.freeze(entry.cost);
|
|
415
|
+
Object.freeze(entry.compat);
|
|
416
|
+
Object.freeze(entry.input);
|
|
417
|
+
Object.freeze(entry.reasoningEffortValues);
|
|
418
|
+
Object.freeze(entry.thinkingLevelMap);
|
|
419
|
+
Object.freeze(entry.provenance);
|
|
420
|
+
Object.freeze(entry.notes);
|
|
421
|
+
Object.freeze(entry);
|
|
422
|
+
}
|
|
423
|
+
return Object.freeze(facts);
|
|
424
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The public model-facts catalog (issue #47).
|
|
3
|
+
*
|
|
4
|
+
* A frozen, typed record keyed by model id: one entry per billed chat model,
|
|
5
|
+
* facts only — what the model *is* (limits, modalities, cost rates, reasoning
|
|
6
|
+
* contract, tool protocol, `max_tokens` semantics, thinking budget, cache mode,
|
|
7
|
+
* billing class), never how a consumer should batch or route work.
|
|
8
|
+
*
|
|
9
|
+
* Derivation, and why the two-layer contract holds: the record is built from
|
|
10
|
+
* the shipped generated catalog plus the manual facts layer, both of which are
|
|
11
|
+
* baked at publish time. Reading it performs no network call, and it never
|
|
12
|
+
* feeds back into provider registration — the read-time overlay
|
|
13
|
+
* (`withStaleCatalogOverlay`, issue #19) stays the only thing that shapes the
|
|
14
|
+
* pi models a session sees.
|
|
15
|
+
*
|
|
16
|
+
* Every cell is paired with a provenance id; `NAN_MODEL_FACTS_META.sources`
|
|
17
|
+
* resolves it to a citation. The cold-prompt ceiling is the one number that is
|
|
18
|
+
* not per-model by measurement, so it ships as `NAN_ENDPOINT_FACTS` instead.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { GENERATED_CATALOG_META, NAN_GENERATED_MODELS } from "../scripts/models.generated.ts";
|
|
22
|
+
import { MANUAL_OVERRIDES } from "../scripts/manual-overrides.ts";
|
|
23
|
+
import { MODEL_FACTS } from "../scripts/model-facts.ts";
|
|
24
|
+
import {
|
|
25
|
+
buildModelFacts,
|
|
26
|
+
freezeModelFacts,
|
|
27
|
+
NAN_FACT_SOURCES,
|
|
28
|
+
overrideFieldKeys,
|
|
29
|
+
type ModelFactsBuildMeta,
|
|
30
|
+
type NanEndpointFacts,
|
|
31
|
+
type NanModelFacts,
|
|
32
|
+
type NanModelFactsMeta,
|
|
33
|
+
} from "./model-facts-core.ts";
|
|
34
|
+
|
|
35
|
+
const BUILD_META: ModelFactsBuildMeta = {
|
|
36
|
+
source: GENERATED_CATALOG_META.source,
|
|
37
|
+
modelsDevProvider: GENERATED_CATALOG_META.modelsDevProvider,
|
|
38
|
+
fetchedAt: GENERATED_CATALOG_META.fetchedAt,
|
|
39
|
+
modelCount: GENERATED_CATALOG_META.modelCount,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* One entry per billed chat model, keyed by model id, deep-frozen.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { NAN_MODEL_FACTS } from "@gtrabanco/pi-nan-provider";
|
|
47
|
+
* const facts = NAN_MODEL_FACTS["deepseek-v4-flash"];
|
|
48
|
+
* facts.contextWindow; // 1048576
|
|
49
|
+
* facts.toolCalling; // "openai-tools"
|
|
50
|
+
* facts.maxTokensSemantics // "bounds-total"
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export const NAN_MODEL_FACTS: Readonly<Record<string, NanModelFacts>> = freezeModelFacts(
|
|
54
|
+
buildModelFacts(NAN_GENERATED_MODELS, MODEL_FACTS, overrideFieldKeys(MANUAL_OVERRIDES), BUILD_META),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The measured cold-prompt ceiling, which is endpoint-wide rather than a model
|
|
59
|
+
* capability: Cloudflare's 120 s proxy read timeout answers HTTP 524 past it,
|
|
60
|
+
* whatever model is called. Use it to size a first cold request; do not read it
|
|
61
|
+
* as a per-model limit (every `coldCeilingTokens` cell stays `"unmeasured"`).
|
|
62
|
+
*/
|
|
63
|
+
export const NAN_ENDPOINT_FACTS: NanEndpointFacts = Object.freeze({
|
|
64
|
+
coldCacheCeilingTokens: 200_000,
|
|
65
|
+
coldCeilingMeasuredAt: "2026-10-01",
|
|
66
|
+
coldCeilingScope:
|
|
67
|
+
"endpoint-wide practical ceiling for cold (uncached) prompts — Cloudflare's 120 s proxy read timeout answers HTTP 524 past it, regardless of model; it is NOT a model capability, so no per-model coldCeilingTokens value is claimed",
|
|
68
|
+
coldCeilingSources:
|
|
69
|
+
"measured 2026-10-01 (issue #18): a cold 208,036-token prompt answered in 12.9 s; ~220,000 and ~286,000 tokens returned HTTP 524 origin_response_timeout at ~126 s. The shipped ceiling is the conservative 200,000 midpoint, not a measured boundary.",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/** Build stamp (models.dev fetch date) plus the provenance citation table. */
|
|
73
|
+
export const NAN_MODEL_FACTS_META: NanModelFactsMeta = Object.freeze({
|
|
74
|
+
...BUILD_META,
|
|
75
|
+
sources: NAN_FACT_SOURCES,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
export { NAN_FACT_SOURCES };
|
|
79
|
+
export {
|
|
80
|
+
CARRIED_FACT_CELLS,
|
|
81
|
+
FACT_CELLS,
|
|
82
|
+
NEW_FACT_CELLS,
|
|
83
|
+
type ModelFactsBuildMeta,
|
|
84
|
+
type ModelFactsDeclaration,
|
|
85
|
+
type ModelFactsDeclarations,
|
|
86
|
+
type NanBillingClass,
|
|
87
|
+
type NanCacheMode,
|
|
88
|
+
type NanEndpointFacts,
|
|
89
|
+
type NanFactCell,
|
|
90
|
+
type NanFactSource,
|
|
91
|
+
type NanMaxTokensSemantics,
|
|
92
|
+
type NanModelCostFact,
|
|
93
|
+
type NanModelFacts,
|
|
94
|
+
type NanModelFactsMeta,
|
|
95
|
+
type NanThinkingBudget,
|
|
96
|
+
type NanToolCalling,
|
|
97
|
+
type NanTriState,
|
|
98
|
+
} from "./model-facts-core.ts";
|
package/src/provider-factory.ts
CHANGED
|
@@ -41,6 +41,7 @@ import { withContextOverflowClassification } from "./context-overflow-classifier
|
|
|
41
41
|
import { withReasoningOnlyStreamGuard } from "./reasoning-only-stream-guard.ts";
|
|
42
42
|
import { createCacheAnomalyFileSink, withCacheAnomalyCapture, type CacheAnomalySink } from "./cache-anomaly.ts";
|
|
43
43
|
import { createResponseRayFileSink, type ResponseRaySink } from "./response-rays.ts";
|
|
44
|
+
import { createHttpAttemptFileSink, withHttpAttemptCapture, type HttpAttemptSink } from "./http-attempts.ts";
|
|
44
45
|
import { NAN_IMAGE_API, baselineImageModels, createNanImagesApi } from "./images.ts";
|
|
45
46
|
import {
|
|
46
47
|
baselineModels,
|
|
@@ -77,6 +78,8 @@ export interface NanCompatibleProviderOptions {
|
|
|
77
78
|
cacheAnomalySink?: CacheAnomalySink;
|
|
78
79
|
/** Sink for the per-response cf-ray sidecar (issue #35 S11); defaults to the agent-dir JSONL file. */
|
|
79
80
|
responseRaySink?: ResponseRaySink;
|
|
81
|
+
/** Sink for the HTTP attempt sidecar (issue #49); defaults to the agent-dir JSONL file. */
|
|
82
|
+
httpAttemptSink?: HttpAttemptSink;
|
|
80
83
|
}
|
|
81
84
|
|
|
82
85
|
/**
|
|
@@ -306,18 +309,25 @@ export async function createNanCompatibleProvider(
|
|
|
306
309
|
// middle → guard (marker detection), innermost → sanitizer (payload).
|
|
307
310
|
// The guard's onProviderStreamEvent hook sees the raw response chunks
|
|
308
311
|
// (NOT onPayload, which only receives the outgoing request params).
|
|
309
|
-
// Outermost:
|
|
310
|
-
//
|
|
311
|
-
//
|
|
312
|
-
//
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
312
|
+
// Outermost: the HTTP attempt tap (issue #49) injects `options.fetch`
|
|
313
|
+
// and observes every attempt, including the retries pi-ai hides and the
|
|
314
|
+
// 429s that never reach `onResponse`. It only adds the fetch wrapper and
|
|
315
|
+
// observation hooks; the stream is untouched.
|
|
316
|
+
// Next: cache-anomaly evidence capture (issue #35), which adds the
|
|
317
|
+
// `cf-ray`/id/created observation and the response-ray sidecar.
|
|
318
|
+
// Then: classifier (error rewrites), guard (marker detection), sanitizer
|
|
319
|
+
// (payload) — innermost.
|
|
320
|
+
api: withHttpAttemptCapture(
|
|
321
|
+
withCacheAnomalyCapture(
|
|
322
|
+
withContextOverflowClassification(
|
|
323
|
+
withReasoningOnlyStreamGuard(wrapApiForStrictSanitization(apiFactory())),
|
|
324
|
+
),
|
|
325
|
+
{
|
|
326
|
+
sink: options.cacheAnomalySink ?? createCacheAnomalyFileSink(),
|
|
327
|
+
raySink: options.responseRaySink ?? createResponseRayFileSink(),
|
|
328
|
+
},
|
|
316
329
|
),
|
|
317
|
-
{
|
|
318
|
-
sink: options.cacheAnomalySink ?? createCacheAnomalyFileSink(),
|
|
319
|
-
raySink: options.responseRaySink ?? createResponseRayFileSink(),
|
|
320
|
-
},
|
|
330
|
+
{ sink: options.httpAttemptSink ?? createHttpAttemptFileSink() },
|
|
321
331
|
),
|
|
322
332
|
}),
|
|
323
333
|
overlayMap,
|