Files
TREK/server/src/nest/llm-parse/llm-config.ts
T
Maurice 90e35e6970 refactor(server): fold llmConfig and unsplash out of services/ (#1831)
Two of the three leftovers in be-misc-fold. The third (wikiService) needs a Zod
contract in @trek/shared first and is left for its own PR.

llmConfig.ts was pure movement: 70 lines of encryption-aware config shaping with
no state and no database, sitting in services/ while all four of its callers are
in src/nest. It is nest/llm-parse/llm-config.ts now, same functions, same file.

unsplashService.ts needed an actual provider. Its key resolution reads both the
env and the users table (env var, then the caller's stored key, then any
admin's), so the injected connection was unavoidable. Its own module because
trips and places both reach it, and because folding it into either would have
made the other import a trip/place service for an image search.

The four exports became methods on UnsplashService. The SSRF guard in
saveUnsplashCover is untouched — it still refuses any host but the Unsplash
image CDN and still goes through safeFetch — as is the unauthenticated-endpoint
fallback when no key resolves (#1449).

The env read stays live through RuntimeEnvService rather than a registerAs
token. readEnv() is what the function did before, and a boot-frozen token would
change when an operator's UNSPLASH_ACCESS_KEY takes effect.

Two test seams follow the code: the unsplash unit tests bind the four methods so
every existing assertion reads unchanged, and the trips controller's path mock of
the module becomes an injected stub.

services/ is down to 17 top-level files.
2026-08-07 19:34:31 +02:00

71 lines
2.6 KiB
TypeScript

import { maybe_encrypt_api_key, decrypt_api_key } from '../common/crypto/apiKeyCrypto';
/**
* Shared types + helpers for the `llm_parsing` addon configuration.
*
* Config can live in two places (resolution happens in
* server/src/nest/llm-parse/llm-config.resolver.ts):
* - instance-wide: the `llm_parsing` addon's `config` JSON (admin-set, wins)
* - per-user: the `llm_*` keys in the per-user settings table (fallback)
*
* The API key is encrypted at rest (reusing apiKeyCrypto) and never returned to
* the client in plaintext — it is masked with MASKED_VALUE, matching the
* per-user encrypted-settings pattern in settingsService.ts.
*/
export type LlmProvider = 'local' | 'openai' | 'anthropic';
/** Fully-resolved config the clients consume. */
export interface ResolvedLlmConfig {
provider: LlmProvider;
model: string;
baseUrl?: string;
apiKey?: string;
multimodal: boolean;
}
/** Shape of the admin instance config stored in `addons.config` (apiKey encrypted). */
export interface LlmAddonConfig {
provider?: LlmProvider;
model?: string;
baseUrl?: string;
apiKey?: string;
multimodal?: boolean;
}
export const LLM_PROVIDERS: LlmProvider[] = ['local', 'openai', 'anthropic'];
export const MASKED_VALUE = '••••••••';
/**
* Prepare an admin config blob for persistence: encrypt a freshly-entered apiKey,
* and preserve the previously-stored (already-encrypted) key when the client
* echoes back the mask sentinel (i.e. the user didn't change it).
*/
export function prepareLlmAddonConfigForWrite(
incoming: Record<string, unknown>,
existingStored: Record<string, unknown> | undefined,
): Record<string, unknown> {
const out: Record<string, unknown> = { ...incoming };
const key = incoming.apiKey;
if (key === undefined || key === null || key === '' || key === MASKED_VALUE) {
// Keep the existing encrypted key untouched (mask echoed or no key supplied).
if (existingStored && 'apiKey' in existingStored) out.apiKey = existingStored.apiKey;
else delete out.apiKey;
} else {
out.apiKey = maybe_encrypt_api_key(String(key)) ?? String(key);
}
return out;
}
/** Mask the apiKey for any client-facing response (never leak plaintext). */
export function maskLlmAddonConfig(config: Record<string, unknown>): Record<string, unknown> {
if (config && config.apiKey) return { ...config, apiKey: MASKED_VALUE };
return config;
}
/** Decrypt the stored apiKey for server-side use (resolver only). */
export function decryptLlmApiKey(stored: unknown): string | undefined {
if (!stored) return undefined;
return decrypt_api_key(stored) ?? undefined;
}