import { OLLAMA_URL } from "../config.js"; import { getActiveChatModel } from "../runtime-config.js"; import { err } from "../log.js"; import { ensureOllama } from "./server.js"; export async function runAsk(question: string): Promise { await ensureOllama(); const model = getActiveChatModel(); const body = JSON.stringify({ model: model.ollamaTag, messages: [ { role: "system", content: "You are an expert programmer. Give concise, practical answers. Include code examples when helpful.", }, { role: "user", content: question }, ], stream: true, }); let res: Response; try { res = await fetch(`${OLLAMA_URL}/v1/chat/completions`, { method: "POST", headers: { "Content-Type": "application/json" }, body, }); } catch { err("Ollama not running. Start it with: localcode start"); } if (!res!.ok) { err(`Server returned ${res!.status}`); } // Stream the response const reader = res!.body?.getReader(); if (!reader) { err("No response body"); } const decoder = new TextDecoder(); let buffer = ""; while (true) { const { done, value } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); const lines = buffer.split("\n"); buffer = lines.pop()!; for (const line of lines) { if (!line.startsWith("data: ")) continue; const data = line.slice(6); if (data === "[DONE]") continue; try { const json = JSON.parse(data) as { choices?: { delta?: { content?: string } }[]; }; const content = json.choices?.[0]?.delta?.content; if (content) process.stdout.write(content); } catch { // skip malformed chunks } } } process.stdout.write("\n"); }