|
| 1 | +export interface AdapterModelEntry { |
| 2 | + id: string; |
| 3 | + label?: string; |
| 4 | +} |
| 5 | + |
| 6 | +/** |
| 7 | + * Per-adapter model list supplied by the operator via env, so the agent model |
| 8 | + * picker can offer models the server cannot CLI-discover (e.g. gateway models). |
| 9 | + * JSON object: adapterType -> [{ id, label? }]. Returns null when unset; throws |
| 10 | + * loudly on malformed input. |
| 11 | + */ |
| 12 | +export function parseAdapterModelsEnv( |
| 13 | + env: Record<string, string | undefined> = process.env, |
| 14 | +): Record<string, AdapterModelEntry[]> | null { |
| 15 | + const raw = env.PAPERCLIP_ADAPTER_MODELS?.trim(); |
| 16 | + if (!raw) return null; |
| 17 | + let parsed: unknown; |
| 18 | + try { |
| 19 | + parsed = JSON.parse(raw); |
| 20 | + } catch (e) { |
| 21 | + throw new Error(`PAPERCLIP_ADAPTER_MODELS must be valid JSON: ${(e as Error).message}`); |
| 22 | + } |
| 23 | + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { |
| 24 | + throw new Error("PAPERCLIP_ADAPTER_MODELS must be a JSON object mapping adapterType to an array of {id,label}"); |
| 25 | + } |
| 26 | + const out: Record<string, AdapterModelEntry[]> = {}; |
| 27 | + for (const [type, list] of Object.entries(parsed as Record<string, unknown>)) { |
| 28 | + if (!Array.isArray(list)) { |
| 29 | + throw new Error(`PAPERCLIP_ADAPTER_MODELS[${type}] must be an array`); |
| 30 | + } |
| 31 | + out[type] = list.map((m) => { |
| 32 | + const o = m as Record<string, unknown>; |
| 33 | + if (typeof o.id !== "string" || !o.id) { |
| 34 | + throw new Error(`PAPERCLIP_ADAPTER_MODELS[${type}] entries require a non-empty string id`); |
| 35 | + } |
| 36 | + return { id: o.id, label: typeof o.label === "string" ? o.label : o.id }; |
| 37 | + }); |
| 38 | + } |
| 39 | + return out; |
| 40 | +} |
0 commit comments