diff --git a/server/src/app-config/README.md b/server/src/app-config/README.md index 1dc354d3b..cac2e0866 100644 --- a/server/src/app-config/README.md +++ b/server/src/app-config/README.md @@ -173,7 +173,8 @@ TREK_DB_PRE_MIGRATE_SNAPSHOT(_KEEP) (read when a migration run starts; the legacy-upgrade suites set it per file), TREK_DB_IGNORE_SQLITE_FILE (read when a Postgres boot finds no TREK data), ADMIN_PASSWORD, IDEMPOTENCY_TTL_SECONDS, MCP_RATE_LIMIT (request-path check), -LLM_TIMEOUT_MS, LLM_MAX_OUTPUT_TOKENS, NOMINATIM_URL, VAPID_* (resolved per use by the Web Push key +LLM_TIMEOUT_MS, LLM_MAX_OUTPUT_TOKENS, OPENROUTER_PROVIDER (optional upstream slug for chat and extraction), +NOMINATIM_URL, VAPID_* (resolved per use by the Web Push key service, which the notification suites build without the container). ## Exemptions — raw `process.env` stays diff --git a/server/src/app-config/derive.ts b/server/src/app-config/derive.ts index 12831ba3d..458a68793 100644 --- a/server/src/app-config/derive.ts +++ b/server/src/app-config/derive.ts @@ -327,6 +327,7 @@ export function deriveIntegrations(raw: RawEnv) { * request whose prompt plus cap overruns its context (#2677). */ llmMaxOutputTokens: positiveIntOr(raw.LLM_MAX_OUTPUT_TOKENS, 0) || undefined, + openRouterProvider: raw.OPENROUTER_PROVIDER?.trim() || undefined, /** * The operator's word on live flight status, as the admin setting spells it * ('true'/'false'), or undefined when unset or not a boolean: then the admin diff --git a/server/src/app-config/env.schema.ts b/server/src/app-config/env.schema.ts index 91762cb45..5f3ecaee7 100644 --- a/server/src/app-config/env.schema.ts +++ b/server/src/app-config/env.schema.ts @@ -170,6 +170,7 @@ export const envSchema = z.object({ LLM_TIMEOUT_MS: integer(1, 2_147_483_647, 'must be a whole number of milliseconds between 1 and 2147483647'), // Below 256 not even one reservation fits, so a value that small is a typo, not a choice. LLM_MAX_OUTPUT_TOKENS: integer(256, 1_000_000, 'must be a whole number of tokens between 256 and 1000000'), + OPENROUTER_PROVIDER: anyString, KITINERARY_EXTRACTOR_PATH: anyString, // The OS search path. Not configuration anybody sets for TREK — it is here so // the kitinerary probe can resolve its binary to an absolute path itself diff --git a/server/src/nest/ai-connections/providers/openai-chat.client.ts b/server/src/nest/ai-connections/providers/openai-chat.client.ts index c78e0da8b..2bf9df07a 100644 --- a/server/src/nest/ai-connections/providers/openai-chat.client.ts +++ b/server/src/nest/ai-connections/providers/openai-chat.client.ts @@ -1,3 +1,4 @@ +import { openRouterRequestOptions } from '../../../utils/openRouterRequest'; import { logWarn } from '../../audit/audit-log.logger'; import type { AiChatClient, @@ -194,6 +195,7 @@ export class OpenAiChatClient implements AiChatClient { private send(request: AiChatRequest, signal: AbortSignal): Promise> { const body = { model: this.model, + ...openRouterRequestOptions(this.baseUrl, this.model), stream: true, stream_options: { include_usage: true }, messages: toChatMessages(request, this.model), diff --git a/server/src/nest/llm-parse/clients/openai-compatible.client.ts b/server/src/nest/llm-parse/clients/openai-compatible.client.ts index cdd266992..b6a9db92d 100644 --- a/server/src/nest/llm-parse/clients/openai-compatible.client.ts +++ b/server/src/nest/llm-parse/clients/openai-compatible.client.ts @@ -1,4 +1,5 @@ import { readEnv } from '../../../app-config'; +import { openRouterRequestOptions } from '../../../utils/openRouterRequest'; import { safeFetchLlm } from '../../../utils/ssrfGuard'; import { parseLenientJson, toReservationList } from '../lenient-json'; import type { LlmExtractionClient, LlmExtractionInput } from '../llm-provider.interface'; @@ -113,6 +114,7 @@ export class OpenAiCompatibleClient implements LlmExtractionClient { const buildBody = (shape: RequestShape) => { const baseBody = { model: input.model, + ...openRouterRequestOptions(base, input.model), [shape.tokenParam]: maxTokens, // Extraction is a deterministic task — Ollama defaults to 0.7, which makes // small models (NuExtract) drop fields or return empty. Pin to 0, and only diff --git a/server/src/utils/openRouterRequest.ts b/server/src/utils/openRouterRequest.ts new file mode 100644 index 000000000..420635658 --- /dev/null +++ b/server/src/utils/openRouterRequest.ts @@ -0,0 +1,13 @@ +import { readEnv } from '../app-config'; + +/** Options shared by chat and extraction. They apply only to OpenRouter's own endpoint. */ +export function openRouterRequestOptions(baseUrl: string, model: string) { + if (baseUrl !== 'https://openrouter.ai/api/v1') return {}; + const upstream = readEnv().integrations.openRouterProvider; + if (!upstream) return {}; + const deepSeekFlash = /^deepseek\/deepseek-v4\.1-flash(?:-1017)?(?:[:@].+)?$/.test(model); + return { + provider: { only: [upstream], allow_fallbacks: false }, + ...(deepSeekFlash ? { reasoning: { effort: 'low' } } : {}), + }; +}