|
| 1 | +import type { ProcessedModel } from "$lib/server/models"; |
| 2 | +import type { GETModelResponse, GETModelsResponse } from "$lib/server/api/types"; |
| 3 | + |
| 4 | +// API responses must never serialize a model object directly: the raw |
| 5 | +// ProcessedModel carries server-only configuration (an `endpoints` array that |
| 6 | +// can hold per-model API keys for self-hosted deployments, prompt render |
| 7 | +// functions, etc.). Every field that leaves the server is listed explicitly. |
| 8 | + |
| 9 | +export function serializeModelSummary(model: ProcessedModel): GETModelsResponse[number] { |
| 10 | + return { |
| 11 | + id: model.id, |
| 12 | + name: model.name, |
| 13 | + websiteUrl: model.websiteUrl, |
| 14 | + modelUrl: model.modelUrl, |
| 15 | + datasetName: model.datasetName, |
| 16 | + datasetUrl: model.datasetUrl, |
| 17 | + displayName: model.displayName, |
| 18 | + description: model.description, |
| 19 | + logoUrl: model.logoUrl, |
| 20 | + promptExamples: model.promptExamples, |
| 21 | + preprompt: model.preprompt, |
| 22 | + multimodal: model.multimodal, |
| 23 | + multimodalAcceptedMimetypes: model.multimodalAcceptedMimetypes, |
| 24 | + supportsTools: (model as unknown as { supportsTools?: boolean }).supportsTools ?? false, |
| 25 | + supportsReasoning: |
| 26 | + (model as unknown as { supportsReasoning?: boolean }).supportsReasoning ?? false, |
| 27 | + supportsArtifacts: |
| 28 | + (model as unknown as { supportsArtifacts?: boolean }).supportsArtifacts ?? false, |
| 29 | + unlisted: model.unlisted, |
| 30 | + hasInferenceAPI: model.hasInferenceAPI, |
| 31 | + isRouter: model.isRouter, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +// Detail shape for GET /models/[namespace]([/model]): the summary plus the |
| 36 | +// heavyweight fields (providers is ~60KB across all models) that only the |
| 37 | +// per-model settings page needs, fetched on demand. |
| 38 | +export function serializeModelDetail(model: ProcessedModel): GETModelResponse { |
| 39 | + return { |
| 40 | + ...serializeModelSummary(model), |
| 41 | + providers: model.providers as unknown as Array<{ provider: string } & Record<string, unknown>>, |
| 42 | + parameters: model.parameters, |
| 43 | + }; |
| 44 | +} |
0 commit comments