import type { NextApiRequest, NextApiResponse } from "next" // Get Ollama base URL from environment variable const OLLAMA_BASE_URL = process.env.OLLAMA_BASE_URL || "http://localhost:11434" export default async function handler( req: NextApiRequest, res: NextApiResponse ) { if (req.method !== "POST") { return res.status(405).json({ error: "Method not allowed" }) } const { name } = req.body if (!name || !name.trim()) { return res.status(400).json({ error: "Model name required" }) } try { const r = await fetch(`${OLLAMA_BASE_URL}/api/pull`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, stream: false }), }) if (r.ok) { res.status(200).json({ success: true, model: name }) } else { const err = await r.text() res.status(400).json({ error: err }) } } catch { res.status(500).json({ error: "Failed to pull model" }) } }