Files
History/server/llm.ts
T
kamilkrzywdaandOpenCode e2936e85bb History: AI-driven alternate-history simulator
Pax Historia-style sim where the LLM is the world engine.

- React 19 + Vite 6 + Tailwind v4 + shadcn/ui frontend
- Express single-port backend (/api + /sse + built client + SPA fallback)
- SQLite (better-sqlite3) persistence, one row per game
- MapLibre choropleth map (OpenFreeMap dark + ISO_A3 regions)
- Defensive turn loop: strip fences, balanced-{} extract, parse, clamp 0-100,
  retry-once nudge, generic 'unclear' fallback; never crashes on bad output
- 5 seed presets (wwii-1939, coldwar-1955, modern-2025, no-wwii-1940, aegis-2400)
- LLM client over native fetch w/ injectable transport (mesh-bot pattern);
  LLM_STUB=1 deterministic transport for tests/e2e
- Unit (vitest, 19) + deterministic Playwright e2e
- Multi-stage node:22-slim Docker + Traefik (history.dev.kkhost.pl:4321)

Co-Authored-By: OpenCode <opencode@users.noreply.github.com>
2026-10-10 21:20:23 +00:00

79 lines
2.7 KiB
TypeScript

// Thin OpenAI chat-completions client over native fetch (mirrors mesh-bot/src/llm).
// The transport is injectable so unit tests and the deterministic e2e stub can
// replace the network call. Config is read from env, never hardcoded:
// LLM_API_BASE (default http://192.168.1.70:1111/v1)
// LLM_MODEL (default qwen3.8-27b)
// LLM_TIMEOUT_MS (default 180000 — the local 27B model can be slow)
export interface ChatMessage {
role: 'system' | 'user' | 'assistant';
content: string;
}
/** A network call. The default implementation is native fetch. */
export type Transport = (url: string, init?: RequestInit) => Promise<Response>;
export interface ChatOptions {
maxTokens?: number;
temperature?: number;
}
export interface LlmClient {
chat(messages: ChatMessage[], opts?: ChatOptions): Promise<string>;
listModels(): Promise<string[]>;
}
export const DEFAULT_BASE = 'http://192.168.1.70:1111/v1';
export const DEFAULT_MODEL = 'qwen3.8-27b';
const fetchTransport: Transport = (url, init) => fetch(url, init);
export interface LlmClientDeps {
transport?: Transport;
apiBase?: string;
model?: string;
timeoutMs?: number;
}
export function createLlmClient(deps: LlmClientDeps = {}): LlmClient {
const transport = deps.transport ?? fetchTransport;
const apiBase = (deps.apiBase ?? process.env.LLM_API_BASE ?? DEFAULT_BASE).replace(/\/+$/, '');
const model = deps.model ?? process.env.LLM_MODEL ?? DEFAULT_MODEL;
const timeoutMs = parseInt(
deps.timeoutMs ? String(deps.timeoutMs) : (process.env.LLM_TIMEOUT_MS ?? '180000'),
10,
);
async function chat(messages: ChatMessage[], opts: ChatOptions = {}): Promise<string> {
const res = await transport(`${apiBase}/chat/completions`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ model, messages, ...opts }),
signal: AbortSignal.timeout(timeoutMs),
});
if (!res.ok) {
const text = await res.text().catch(() => '');
throw new Error(`chat failed: HTTP ${res.status} ${text.slice(0, 200)}`);
}
const body = (await res.json()) as {
choices?: { message?: { content?: string } }[];
};
const content = body.choices?.[0]?.message?.content;
if (typeof content !== 'string') {
throw new Error('chat failed: no choices[0].message.content in response');
}
return content;
}
async function listModels(): Promise<string[]> {
const res = await transport(`${apiBase}/models`, {
signal: AbortSignal.timeout(timeoutMs),
});
if (!res.ok) throw new Error(`listModels failed: HTTP ${res.status}`);
const body = (await res.json()) as { data?: { id: string }[] };
return (body.data ?? []).map((m) => m.id);
}
return { chat, listModels };
}