import type { ChatRole } from "@/types/llm-chat"; import type { LLMProviderWithSecret } from "@/types/llm-provider"; /** * Condense a multi-turn conversation into a single, self-contained retrieval * query. The rewriter reuses the user's selected provider/model but runs in * non-streaming, non-thinking mode so the extra LLM call stays cheap. * * The rewrite prompt is intentionally baked in and not user-facing — callers * should only provide the chat history and the latest user turn. */ const REWRITE_SYSTEM_PROMPT = "你是一个检索 query 重写助手。根据对话历史,将用户最后一轮提问改写成一个完整、自包含、不依赖上下文的检索问题,用于向量检索知识库。\n" + "规则:\n" + "1. 如果最后一轮提问已经足够独立(例如包含完整实体和动作),直接原样返回。\n" + "2. 如果提问依赖上下文(如「它」「那个」「怎么做」「Java 呢」),请结合历史补全成完整问题。\n" + "3. 只输出重写后的 query 本身,不要解释、不要加引号、不要编号、不要换行。\n" + "4. 不要回答用户的问题,不要生成多条,只输出一句话。"; const REWRITE_TIMEOUT_MS = 10_000; const REWRITE_MAX_CHARS = 200; // Keep the last 3 Q-A pairs; older turns rarely improve rewrite quality and // just inflate prompt tokens. const REWRITE_HISTORY_TAIL = 6; export interface RewriteHistoryMsg { role: ChatRole; content: string; } export async function rewriteQuery( provider: LLMProviderWithSecret, model: string, history: RewriteHistoryMsg[], lastUserContent: string, signal?: AbortSignal, ): Promise { const recent = history.slice(-REWRITE_HISTORY_TAIL); const messages = [ { role: "system" as const, content: REWRITE_SYSTEM_PROMPT }, ...recent.map((m) => ({ role: m.role, content: m.content })), { role: "user" as const, content: `请将下面这条用户提问改写为独立检索 query:\n<<<\n${lastUserContent}\n>>>`, }, ]; const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), REWRITE_TIMEOUT_MS); const onParentAbort = () => controller.abort(); signal?.addEventListener("abort", onParentAbort, { once: true }); try { const resp = await fetch(`${provider.baseUrl}/chat/completions`, { method: "POST", headers: { Authorization: `Bearer ${provider.apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify({ model, messages, stream: false, temperature: 0, max_tokens: 256, // DashScope / Qwen3 switch to disable thinking; vanilla OpenAI // providers ignore unknown fields. enable_thinking: false, }), signal: controller.signal, }); if (!resp.ok) { const body = await resp.text().catch(() => ""); throw new Error( `rewrite ${resp.status}: ${body.slice(0, 200) || resp.statusText}`, ); } const json = (await resp.json()) as { choices?: Array<{ message?: { content?: string } }>; }; const raw = json.choices?.[0]?.message?.content?.trim(); if (!raw) throw new Error("empty rewrite output"); const firstLine = raw .split("\n") .map((s) => s.trim()) .find(Boolean) ?? ""; const cleaned = firstLine .replace(/^[「"'『`]+|[」"'』`]+$/g, "") .slice(0, REWRITE_MAX_CHARS); if (!cleaned) throw new Error("rewrite output normalized to empty"); return cleaned; } finally { clearTimeout(timer); signal?.removeEventListener("abort", onParentAbort); } }