import { chatCompletion } from "./litellm.js"; const ROUTER_MODEL = "router-classifier"; const CHAT_MODEL = "auto"; function systemPrompt(knownRepos) { return [ "You are a routing classifier for a chat bot. Given a user message, decide whether it is:", '- "chat": a question, discussion, or anything that just needs a text reply.', '- "code_task": a request to change a specific code repository (add/edit/fix something)', " where the repository is clearly one of the known repositories below.", "", `Known repositories: ${knownRepos.join(", ") || "(none configured)"}`, "", "Reply with ONLY a JSON object, nothing else:", '{"type":"chat"}', 'or', '{"type":"code_task","repo":"owner/repo","instruction":"clear imperative instruction"}', "", "If it sounds like a code change but you can't confidently match it to one of the known", 'repositories, reply {"type":"chat"} instead of guessing.', ].join("\n"); } function parseDecision(raw) { try { const cleaned = raw.trim().replace(/^```(?:json)?\n?/, "").replace(/```$/, ""); const parsed = JSON.parse(cleaned); if (parsed.type === "code_task" && parsed.repo && parsed.instruction) { return parsed; } } catch { // fall through to chat — an unparseable classification is not a reason to edit a repo } return { type: "chat" }; } export async function routeMessage(text, knownRepos) { const raw = await chatCompletion(ROUTER_MODEL, `${systemPrompt(knownRepos)}\n\nMessage: ${text}`); return parseDecision(raw); } export async function chatReply(text) { return chatCompletion(CHAT_MODEL, text); }