import { DEFAULT_SYSTEM_PROMPT, RECENCY_HINT } from "./constants"; import type { ApiMessage, ChatTurn } from "./types"; /** * Turns stored turns into the message array sent to the model. * * Only completed question/answer pairs are replayed. The intermediate tool * messages from a previous turn are intentionally dropped: they are noise once * the answer is written, and replaying them would re-send large page contents * on every subsequent request. */ export function buildMessages(systemPrompt: string, turns: ChatTurn[], prompt: string): ApiMessage[] { const messages: ApiMessage[] = [{ role: "system", content: systemPrompt }]; for (const turn of turns) { if (!turn.response) continue; messages.push({ role: "user", content: turn.prompt }); messages.push({ role: "assistant", content: turn.response }); } messages.push({ role: "user", content: prompt }); return messages; } /** Uses the user's prompt if they set one, otherwise Lam's default. */ export function composeSystemPrompt(customPrompt: string, webEnabled: boolean): string { const parts = [customPrompt.trim() || DEFAULT_SYSTEM_PROMPT]; if (webEnabled) parts.push(RECENCY_HINT); return parts.join("\n\n"); }