From d4db17e5fc0656437dd55b36ad723d37165f4de8 Mon Sep 17 00:00:00 2001 From: Christiaan Arnoldus Date: Wed, 1 Apr 2026 14:12:58 +0200 Subject: [PATCH] Some improvements for database defined models - strict parsing, an unrecognized property is probably a mistake - set the supportedChatApis property properly - add remove_from_body feature --- packages/db/src/schema-types.ts | 29 ++++++++++++++++------------- src/lib/providers/index.ts | 14 ++++++++++---- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/db/src/schema-types.ts b/packages/db/src/schema-types.ts index 65988a61da..b682159aa0 100644 --- a/packages/db/src/schema-types.ts +++ b/packages/db/src/schema-types.ts @@ -875,19 +875,22 @@ export const CustomLlmExtraHeadersSchema = z.record(z.string(), z.string()); export type CustomLlmExtraHeaders = z.infer; -export const CustomLlmDefinitionSchema = z.object({ - internal_id: z.string(), - display_name: z.string(), - context_length: z.number(), - max_completion_tokens: z.number(), - base_url: z.string(), - api_key: z.string(), - organization_ids: z.array(z.string()), - supports_image_input: z.boolean().optional(), - extra_headers: CustomLlmExtraHeadersSchema.optional(), - extra_body: CustomLlmExtraBodySchema.optional(), - opencode_settings: OpenCodeSettingsSchema.optional(), -}); +export const CustomLlmDefinitionSchema = z + .object({ + internal_id: z.string(), + display_name: z.string(), + context_length: z.number(), + max_completion_tokens: z.number(), + base_url: z.string(), + api_key: z.string(), + organization_ids: z.array(z.string()), + supports_image_input: z.boolean().optional(), + extra_headers: CustomLlmExtraHeadersSchema.optional(), + extra_body: CustomLlmExtraBodySchema.optional(), + remove_from_body: z.array(z.string()).optional(), + opencode_settings: OpenCodeSettingsSchema.optional(), + }) + .strict(); export type CustomLlmDefinition = z.infer; diff --git a/src/lib/providers/index.ts b/src/lib/providers/index.ts index b85cdc7325..e26e46fb72 100644 --- a/src/lib/providers/index.ts +++ b/src/lib/providers/index.ts @@ -125,12 +125,18 @@ export async function getProvider( id: 'custom', apiUrl: customLlm.base_url, apiKey: customLlm.api_key, - supportedChatApis: ['chat_completions', 'messages', 'responses'], + supportedChatApis: inferSupportedChatApis( + customLlm.opencode_settings?.ai_sdk_provider ?? 'openrouter' + ), transformRequest(context) { - Object.assign(context.request.body, customLlm.extra_body ?? {}); - for (const [key, value] of Object.entries(customLlm.extra_headers ?? {})) { - context.extraHeaders[key] = value; + if (customLlm.remove_from_body) { + const body = context.request.body as Record; + for (const key of customLlm.remove_from_body ?? []) { + delete body[key]; + } } + Object.assign(context.request.body, customLlm.extra_body ?? {}); + Object.assign(context.extraHeaders, customLlm.extra_headers ?? {}); context.request.body.model = customLlm.internal_id; }, },