115 lines
4.4 KiB
TypeScript
115 lines
4.4 KiB
TypeScript
import { loadSecretBundle } from "../config/secret-bundle.js";
|
|
import { loadRuntimeModelCatalog } from "../models/runtime-model-catalog.js";
|
|
|
|
export interface MetadataGenerationModelChoice {
|
|
id: string;
|
|
label: string;
|
|
}
|
|
|
|
export interface MetadataGenerationModelCatalog {
|
|
models: MetadataGenerationModelChoice[];
|
|
default: string | null;
|
|
}
|
|
|
|
export interface ResolvedMetadataGenerationModel {
|
|
readonly id: string;
|
|
readonly provider: string;
|
|
readonly model: string;
|
|
readonly disableThinking?: true;
|
|
readonly endpoint?: Readonly<{ baseUrl: string; apiVersion?: string }>;
|
|
readonly apiKeyEnv?: string;
|
|
readonly apiKey?: string;
|
|
}
|
|
|
|
export class MetadataGenerationModelUnavailableError extends Error {
|
|
constructor() {
|
|
super("metadata-generation model is unavailable");
|
|
this.name = "MetadataGenerationModelUnavailableError";
|
|
}
|
|
}
|
|
|
|
export interface MetadataGenerationModels {
|
|
catalog(): MetadataGenerationModelCatalog;
|
|
resolve(selection: string): ResolvedMetadataGenerationModel;
|
|
}
|
|
|
|
class RestartLoadedMetadataGenerationModels implements MetadataGenerationModels {
|
|
readonly #models: ReadonlyMap<string, ResolvedMetadataGenerationModel>;
|
|
readonly #catalog: MetadataGenerationModelCatalog;
|
|
|
|
constructor(models: ReadonlyMap<string, ResolvedMetadataGenerationModel>, defaultModel: string | null) {
|
|
this.#models = models;
|
|
this.#catalog = {
|
|
models: [...models.values()].map(({ id }) => ({ id, label: id })),
|
|
default: defaultModel,
|
|
};
|
|
}
|
|
|
|
catalog(): MetadataGenerationModelCatalog {
|
|
return { models: this.#catalog.models.map((choice) => ({ ...choice })), default: this.#catalog.default };
|
|
}
|
|
|
|
resolve(selection: string): ResolvedMetadataGenerationModel {
|
|
const model = this.#models.get(selection);
|
|
if (!model) throw new MetadataGenerationModelUnavailableError();
|
|
return model;
|
|
}
|
|
}
|
|
|
|
function invalid(message = "metadata-generation runtime catalog is invalid"): Error {
|
|
return new Error(message);
|
|
}
|
|
|
|
export function loadMetadataGenerationModels(options: {
|
|
catalogFile?: string;
|
|
secretsFile?: string;
|
|
}): MetadataGenerationModels {
|
|
const catalog = loadRuntimeModelCatalog(options.catalogFile);
|
|
const configured = catalog.metadataModels();
|
|
if (configured.length === 0) return new RestartLoadedMetadataGenerationModels(new Map(), null);
|
|
|
|
const requiresSecrets = configured.some((model) => model.authentication.mode === "secret_env");
|
|
let secrets: ReadonlyMap<string, string> = new Map();
|
|
if (requiresSecrets) {
|
|
if (!options.secretsFile) throw invalid("metadata-generation keyed models require THT_SECRETS_FILE");
|
|
try { secrets = loadSecretBundle(options.secretsFile); }
|
|
catch { throw invalid("metadata-generation secrets are unavailable"); }
|
|
}
|
|
|
|
const models = new Map<string, ResolvedMetadataGenerationModel>();
|
|
const labels = new Map<string, string>();
|
|
for (const configuredModel of configured) {
|
|
const adapter = configuredModel.metadataAdapter;
|
|
if (!adapter || configuredModel.authentication.mode === "pi_auth") throw invalid();
|
|
const apiKeyEnv = configuredModel.authentication.apiKeyEnv;
|
|
let apiKey: string | undefined;
|
|
if (configuredModel.authentication.mode === "secret_env") {
|
|
if (!apiKeyEnv) throw invalid();
|
|
apiKey = secrets.get(apiKeyEnv);
|
|
if (!apiKey) throw invalid(`metadata-generation model "${configuredModel.id}" secret "${apiKeyEnv}" is missing`);
|
|
if (apiKey.length > 16 * 1024 || /\s/u.test(apiKey)) {
|
|
throw invalid(`metadata-generation model "${configuredModel.id}" secret "${apiKeyEnv}" is unusable`);
|
|
}
|
|
}
|
|
labels.set(configuredModel.id, configuredModel.label);
|
|
models.set(configuredModel.id, Object.freeze({
|
|
id: configuredModel.id,
|
|
provider: adapter.litellmProvider,
|
|
model: configuredModel.upstreamModel,
|
|
...(configuredModel.metadataGeneration?.disableThinking === true
|
|
? { disableThinking: true as const } : {}),
|
|
...(configuredModel.endpoint ? { endpoint: Object.freeze({ ...configuredModel.endpoint }) } : {}),
|
|
...(apiKeyEnv ? { apiKeyEnv, apiKey } : {}),
|
|
}));
|
|
}
|
|
const result = new RestartLoadedMetadataGenerationModels(models, models.size ? catalog.defaultInteraction : null);
|
|
const safe = result.catalog();
|
|
return {
|
|
catalog: () => ({
|
|
default: safe.default,
|
|
models: safe.models.map((choice) => ({ ...choice, label: labels.get(choice.id) ?? choice.id })),
|
|
}),
|
|
resolve: (selection) => result.resolve(selection),
|
|
};
|
|
}
|