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 ) { // GET → list all models if (req.method === "GET") { try { const r = await fetch(`${OLLAMA_BASE_URL}/api/tags`) const data = await r.json() res.status(200).json(data) } catch { res.status(500).json({ error: "Failed to fetch models" }) } return } // DELETE → remove a model if (req.method === "DELETE") { const { name } = req.body try { const r = await fetch(`${OLLAMA_BASE_URL}/api/delete`, { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name }), }) if (r.ok) { res.status(200).json({ success: true }) } else { const err = await r.text() res.status(400).json({ error: err }) } } catch { res.status(500).json({ error: "Failed to delete model" }) } return } res.status(405).json({ error: "Method not allowed" }) }