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 runPipe(prompt: string): Promise { await ensureOllama(); const model = getActiveChatModel(); // Read stdin const chunks: Buffer[] = []; for await (const chunk of process.stdin) { chunks.push(chunk as Buffer); } const input = Buffer.concat(chunks).toString("utf-8"); const body = JSON.stringify({ model: model.ollamaTag, messages: [ { role: "system", content: "You are an expert programmer. Output only code, no explanations.", }, { role: "user", content: `${prompt}\n\n\`\`\`\n${input}\n\`\`\`` }, ], stream: false, }); 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}`); } const data = (await res!.json()) as { choices?: { message?: { content?: string } }[]; }; const content = data.choices?.[0]?.message?.content ?? ""; process.stdout.write(content + "\n"); }