fix(ai): support optional OpenRouter upstream selection

This commit is contained in:
Maurice
2026-12-02 21:57:08 +01:00
parent a2cf911ce1
commit 59e5c5a3c9
6 changed files with 21 additions and 1 deletions
+2 -1
View File
@@ -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
+1
View File
@@ -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
+1
View File
@@ -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
@@ -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<ReadableStream<Uint8Array>> {
const body = {
model: this.model,
...openRouterRequestOptions(this.baseUrl, this.model),
stream: true,
stream_options: { include_usage: true },
messages: toChatMessages(request, this.model),
@@ -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
+13
View File
@@ -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' } } : {}),
};
}