refactor: carry catalog tier on the model
Move the catalog tier from the x-nous-catalog-tier request header to a catalogTier property on each model. pi forwards only model.headers to the provider and copies them into the request options, so a header needed stripping on both the model and the options before every request; that adapter leaked for two audit rounds. A property never reaches the wire, survives pi's model store, and needs no wrapper, so TIER_HEADER, untier and withoutTierHeader are deleted and pi's stream passes through untouched. Models stored by the previous version come back without the property until the next interactive start rewrites the store; the CHANGELOG tells the user to start pi once interactively after upgrading.
This commit is contained in:
1 parent
a1735049b7
commit
cb5aecf815
5 files changed
+26
-73
No files matched your search
@@ -21,7 +21,6 @@ import {
|
||||
type OAuthCredential,
|
||||
type ProviderAuth,
|
||||
type ProviderAuthInteraction,
|
||||
type ProviderStreams,
|
||||
type RefreshModelsContext,
|
||||
} from "@earendil-works/pi-ai";
|
||||
import { setTimeout as sleep } from "node:timers/promises";
|
||||
@@ -173,11 +172,13 @@ const auth: ProviderAuth = {
|
||||
|
||||
const num = (v: string | number | undefined) => Number(v) || 0;
|
||||
|
||||
// Pi persists and restores model objects across runs, so the catalog tier travels ON the model (as an
|
||||
// inert header Nous ignores) rather than in a side table that restored models would miss.
|
||||
const TIER_HEADER = "x-nous-catalog-tier";
|
||||
// Pi persists and restores model objects across runs, so the catalog tier travels ON the model rather than in a
|
||||
// side table that restored models would miss. It is a plain property, not a header: pi forwards only `headers`
|
||||
// to the provider (copying them into the request options too), so a property needs no stripping before a request.
|
||||
type Tier = Exclude<Catalog, "full">;
|
||||
type TieredModel = Model<Api> & { catalogTier?: Tier };
|
||||
|
||||
export function toModel(item: ModelListItem): Model<Api> {
|
||||
export function toModel(item: ModelListItem): TieredModel {
|
||||
const id = item.id.toLowerCase();
|
||||
const tier = tierOf(item);
|
||||
const image = item.architecture?.input_modalities?.includes("image") ?? /(claude|gpt|gemini|grok|vl|vision)/.test(id);
|
||||
@@ -193,17 +194,17 @@ export function toModel(item: ModelListItem): Model<Api> {
|
||||
cost: { input: num(item.pricing?.prompt) * 1e6, output: num(item.pricing?.completion) * 1e6, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: item.context_length ?? 131072,
|
||||
maxTokens: 32768,
|
||||
...(tier ? { headers: { [TIER_HEADER]: tier } } : {}),
|
||||
catalogTier: tier,
|
||||
};
|
||||
}
|
||||
|
||||
const tierOfModel = (model: Model<Api>) => model.headers?.[TIER_HEADER] as Exclude<Catalog, "full"> | undefined;
|
||||
const tierOfModel = (model: Model<Api>) => (model as TieredModel).catalogTier;
|
||||
|
||||
const CATALOGS = ["full", "free", "promo"] as const;
|
||||
type Catalog = (typeof CATALOGS)[number];
|
||||
|
||||
/** Classify a live catalog entry. Nous marks free models with synthesizedFreeVariant and discounts with pricing.original. */
|
||||
export function tierOf(item: ModelListItem): Exclude<Catalog, "full"> | undefined {
|
||||
export function tierOf(item: ModelListItem): Tier | undefined {
|
||||
if (item.synthesizedFreeVariant || item.id.toLowerCase().endsWith(":free")) return "free";
|
||||
const now = Number(item.pricing?.prompt);
|
||||
const was = Number(item.pricing?.original?.prompt);
|
||||
@@ -239,25 +240,6 @@ export async function fetchModels(credential: Credential | undefined, signal: Ab
|
||||
return (payload.data ?? []).filter((m) => m.id && !/hermes/i.test(m.id)).map(toModel);
|
||||
}
|
||||
|
||||
// The tier header is metadata only; strip it before the request so it never reaches Nous. pi copies
|
||||
// model.headers into options.headers before calling the provider (pi-ai getAuth → prepareRequest) and the
|
||||
// client applies options.headers last, so BOTH the model and the options must be stripped.
|
||||
// (Nous free/discounted models 500 on an injected `tags.user` body field as of 2026-11-25, so no body
|
||||
// rewriting happens here — if that changes, chain options.onPayload.)
|
||||
const untier = <T extends { headers?: Record<string, string | null> } | undefined>(value: T): T => {
|
||||
if (!value?.headers?.[TIER_HEADER]) return value;
|
||||
const headers = { ...value.headers };
|
||||
delete headers[TIER_HEADER];
|
||||
return { ...value, headers };
|
||||
};
|
||||
|
||||
export function withoutTierHeader(api: ProviderStreams): ProviderStreams {
|
||||
return {
|
||||
stream: (model, context, options) => api.stream(untier(model), context, untier(options)),
|
||||
streamSimple: (model, context, options) => api.streamSimple(untier(model), context, untier(options)),
|
||||
};
|
||||
}
|
||||
|
||||
export default function nousPortal(pi: ExtensionAPI) {
|
||||
let models: Model<Api>[] = BASELINE.map(toModel);
|
||||
const flagPath = join(getAgentDir(), "nous-portal-catalog");
|
||||
@@ -307,10 +289,9 @@ export default function nousPortal(pi: ExtensionAPI) {
|
||||
models,
|
||||
// Honored by Models.getAvailable(): /model lists only what this returns.
|
||||
filterModels: (all) => all.filter(inCatalog),
|
||||
api: {
|
||||
// Strips the catalog-tier header only; no body rewriting (see untier).
|
||||
"openai-completions": withoutTierHeader(openAICompletionsApi()),
|
||||
},
|
||||
// pi's stream passes through untouched: no header or body rewriting. Nous free and discounted models
|
||||
// 500 on an injected `tags.user` body field (2026-11-25); if that changes, chain options.onPayload.
|
||||
api: { "openai-completions": openAICompletionsApi() },
|
||||
async fetchModels(context: RefreshModelsContext) {
|
||||
const fetched = await fetchModels(context.credential, context.signal);
|
||||
// Empty live result → keep the last good list. createProvider persists and publishes whatever is
|
||||
|
||||
Reference in new issue
Block a user