Spaces:
Sleeping
Sleeping
Andrew
commited on
Commit
·
b13e794
1
Parent(s):
4bb32e9
(feat) Allow endpoint api overrides
Browse files- src/lib/server/models.ts +13 -3
src/lib/server/models.ts
CHANGED
|
@@ -9,6 +9,10 @@ import { makeRouterEndpoint } from "$lib/server/router/endpoint";
|
|
| 9 |
|
| 10 |
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
const sanitizeJSONEnv = (val: string, fallback: string) => {
|
| 13 |
const raw = (val ?? "").trim();
|
| 14 |
const unquoted = raw.startsWith("`") && raw.endsWith("`") ? raw.slice(1, -1) : raw;
|
|
@@ -276,7 +280,7 @@ const processModel = async (m: ModelConfig) => ({
|
|
| 276 |
|
| 277 |
const addEndpoint = (m: Awaited<ReturnType<typeof processModel>>) => ({
|
| 278 |
...m,
|
| 279 |
-
getEndpoint: async (): Promise<Endpoint> => {
|
| 280 |
if (!m.endpoints || m.endpoints.length === 0) {
|
| 281 |
throw new Error("No endpoints configured. This build requires OpenAI-compatible endpoints.");
|
| 282 |
}
|
|
@@ -285,7 +289,12 @@ const addEndpoint = (m: Awaited<ReturnType<typeof processModel>>) => ({
|
|
| 285 |
if (endpoint.type !== "openai") {
|
| 286 |
throw new Error("Only 'openai' endpoint type is supported in this build");
|
| 287 |
}
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
},
|
| 290 |
});
|
| 291 |
|
|
@@ -343,7 +352,8 @@ if (archBase) {
|
|
| 343 |
...aliasBase,
|
| 344 |
isRouter: true,
|
| 345 |
// getEndpoint uses the router wrapper regardless of the endpoints array
|
| 346 |
-
getEndpoint: async (): Promise<Endpoint> =>
|
|
|
|
| 347 |
} as ProcessedModel;
|
| 348 |
|
| 349 |
// Put alias first
|
|
|
|
| 9 |
|
| 10 |
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
| 11 |
|
| 12 |
+
export interface EndpointOptions {
|
| 13 |
+
apiKey?: string;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
const sanitizeJSONEnv = (val: string, fallback: string) => {
|
| 17 |
const raw = (val ?? "").trim();
|
| 18 |
const unquoted = raw.startsWith("`") && raw.endsWith("`") ? raw.slice(1, -1) : raw;
|
|
|
|
| 280 |
|
| 281 |
const addEndpoint = (m: Awaited<ReturnType<typeof processModel>>) => ({
|
| 282 |
...m,
|
| 283 |
+
getEndpoint: async (options?: EndpointOptions): Promise<Endpoint> => {
|
| 284 |
if (!m.endpoints || m.endpoints.length === 0) {
|
| 285 |
throw new Error("No endpoints configured. This build requires OpenAI-compatible endpoints.");
|
| 286 |
}
|
|
|
|
| 289 |
if (endpoint.type !== "openai") {
|
| 290 |
throw new Error("Only 'openai' endpoint type is supported in this build");
|
| 291 |
}
|
| 292 |
+
const overrideApiKey = options?.apiKey;
|
| 293 |
+
return await endpoints.openai({
|
| 294 |
+
...endpoint,
|
| 295 |
+
model: m,
|
| 296 |
+
...(overrideApiKey ? { apiKey: overrideApiKey } : {}),
|
| 297 |
+
});
|
| 298 |
},
|
| 299 |
});
|
| 300 |
|
|
|
|
| 352 |
...aliasBase,
|
| 353 |
isRouter: true,
|
| 354 |
// getEndpoint uses the router wrapper regardless of the endpoints array
|
| 355 |
+
getEndpoint: async (options?: EndpointOptions): Promise<Endpoint> =>
|
| 356 |
+
makeRouterEndpoint(aliasModel, options),
|
| 357 |
} as ProcessedModel;
|
| 358 |
|
| 359 |
// Put alias first
|