mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-23 12:40:25 +02:00
Allow merging a catalog file with the api response
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { z } from "zod";
|
||||||
import config from "@server/lib/config";
|
import config from "@server/lib/config";
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import type { AiProviderType } from "@server/lib/aiProviderDefaults";
|
import type { AiProviderType } from "@server/lib/aiProviderDefaults";
|
||||||
@@ -54,16 +55,24 @@ export type AiModelCatalogEntry = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
type RawCatalogEntry = {
|
const catalogEntrySchema = z.object({
|
||||||
model: string;
|
model: z.string(),
|
||||||
provider: string;
|
provider: z.string(),
|
||||||
pricing?: {
|
pricing: z
|
||||||
in?: number | null;
|
.object({
|
||||||
out?: number | null;
|
in: z.number().nullable().optional(),
|
||||||
cache?: number | null;
|
out: z.number().nullable().optional(),
|
||||||
reasoning?: number | null;
|
cache: z.number().nullable().optional(),
|
||||||
};
|
reasoning: z.number().nullable().optional()
|
||||||
};
|
})
|
||||||
|
.optional()
|
||||||
|
});
|
||||||
|
|
||||||
|
const catalogFileSchema = z.object({
|
||||||
|
data: z.array(catalogEntrySchema).optional().default([])
|
||||||
|
});
|
||||||
|
|
||||||
|
type RawCatalogEntry = z.infer<typeof catalogEntrySchema>;
|
||||||
|
|
||||||
function normalizeCatalogProvider(raw: string): CatalogProvider | null {
|
function normalizeCatalogProvider(raw: string): CatalogProvider | null {
|
||||||
if (CATALOG_PROVIDER_SET.has(raw)) {
|
if (CATALOG_PROVIDER_SET.has(raw)) {
|
||||||
@@ -183,8 +192,14 @@ export class AiModelCatalog {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const raw = fs.readFileSync(filePath, "utf-8");
|
const raw = fs.readFileSync(filePath, "utf-8");
|
||||||
const parsed = JSON.parse(raw) as { data: RawCatalogEntry[] };
|
const result = catalogFileSchema.safeParse(JSON.parse(raw));
|
||||||
return (parsed.data ?? [])
|
if (!result.success) {
|
||||||
|
logger.warn(
|
||||||
|
`AI model catalog file at ${filePath} failed validation: ${result.error.message}`
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return result.data.data
|
||||||
.map(normalizeEntry)
|
.map(normalizeEntry)
|
||||||
.filter((e): e is AiModelCatalogEntry => e != null);
|
.filter((e): e is AiModelCatalogEntry => e != null);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -197,11 +212,15 @@ export class AiModelCatalog {
|
|||||||
upstreamUrl: string
|
upstreamUrl: string
|
||||||
): Promise<AiModelCatalogEntry[] | null> {
|
): Promise<AiModelCatalogEntry[] | null> {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get<{ data: RawCatalogEntry[] }>(
|
const res = await axios.get(upstreamUrl, { timeout: 15_000 });
|
||||||
upstreamUrl,
|
const result = catalogFileSchema.safeParse(res.data);
|
||||||
{ timeout: 15_000 }
|
if (!result.success) {
|
||||||
);
|
logger.warn(
|
||||||
return (res.data?.data ?? [])
|
`AI model catalog response from ${upstreamUrl} failed validation: ${result.error.message}`
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return result.data.data
|
||||||
.map(normalizeEntry)
|
.map(normalizeEntry)
|
||||||
.filter((e): e is AiModelCatalogEntry => e != null);
|
.filter((e): e is AiModelCatalogEntry => e != null);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@@ -213,22 +232,34 @@ export class AiModelCatalog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async refresh(): Promise<void> {
|
private async refresh(): Promise<void> {
|
||||||
const { file, upstream_url } = config.getRawConfig().ai.model_catalog;
|
const { file, merge_file, upstream_url } =
|
||||||
|
config.getRawConfig().ai.model_catalog;
|
||||||
|
|
||||||
const fetched = file
|
const fetched = file
|
||||||
? await this.fetchFromFile(file)
|
? await this.fetchFromFile(file)
|
||||||
: await this.fetchFromUpstream(upstream_url);
|
: await this.fetchFromUpstream(upstream_url);
|
||||||
|
|
||||||
if (fetched) {
|
if (!fetched) {
|
||||||
this.setEntries(fetched);
|
|
||||||
logger.debug(
|
|
||||||
`AI model catalog refreshed: ${this.entries.length} models loaded`
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"AI model catalog refresh failed; keeping previously loaded catalog in memory"
|
"AI model catalog refresh failed; keeping previously loaded catalog in memory"
|
||||||
);
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let merged = fetched;
|
||||||
|
if (merge_file) {
|
||||||
|
const mergeEntries = await this.fetchFromFile(merge_file);
|
||||||
|
if (mergeEntries) {
|
||||||
|
// Entries from the base catalog take precedence; the merge
|
||||||
|
// file only adds models not already present.
|
||||||
|
merged = [...fetched, ...mergeEntries];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setEntries(merged);
|
||||||
|
logger.debug(
|
||||||
|
`AI model catalog refreshed: ${this.entries.length} models loaded`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private scheduleNextRefresh(): void {
|
private scheduleNextRefresh(): void {
|
||||||
|
|||||||
@@ -113,7 +113,10 @@ export const configSchema = z
|
|||||||
.prefault({}),
|
.prefault({}),
|
||||||
remote_headers: z
|
remote_headers: z
|
||||||
.object({
|
.object({
|
||||||
user_id: z.string().optional().default("Remote-User-Id"),
|
user_id: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.default("Remote-User-Id"),
|
||||||
virtual_api_key_id: z
|
virtual_api_key_id: z
|
||||||
.string()
|
.string()
|
||||||
.optional()
|
.optional()
|
||||||
@@ -435,6 +438,10 @@ export const configSchema = z
|
|||||||
// pin the catalog to a local file instead of
|
// pin the catalog to a local file instead of
|
||||||
// fetching it from upstream_url.
|
// fetching it from upstream_url.
|
||||||
file: z.string().optional(),
|
file: z.string().optional(),
|
||||||
|
// No default - only used when an operator wants to
|
||||||
|
// merge the content of the json file with the upstream catalog. This is useful for adding
|
||||||
|
// custom models to the catalog without having to maintain a separate fork of the upstream catalog.
|
||||||
|
merge_file: z.string().optional(),
|
||||||
refresh_interval_min_hours: z
|
refresh_interval_min_hours: z
|
||||||
.number()
|
.number()
|
||||||
.positive()
|
.positive()
|
||||||
|
|||||||
Reference in New Issue
Block a user