import type { NextApiRequest, NextApiResponse } from "next" import fs from "fs" import path from "path" const CHAT_HISTORY_FILE = path.join(process.cwd(), "chat-history.json") interface ChatMessage { role: "user" | "assistant" | "system" content: string } // Get Ollama base URL from environment variable const OLLAMA_BASE_URL = process.env.OLLAMA_BASE_URL || "http://localhost:11434" // Read chat history from file function readChatHistory(): ChatMessage[] { try { if (fs.existsSync(CHAT_HISTORY_FILE)) { const data = fs.readFileSync(CHAT_HISTORY_FILE, "utf-8") const history = JSON.parse(data) // Convert stored messages to Ollama format (only user and assistant roles) return history.messages .filter((msg: any) => msg.role === "user" || msg.role === "assistant") .map((msg: any) => ({ role: msg.role, content: msg.content, })) } } catch (error) { console.error("Error reading chat history:", error) } return [] } export default async function handler( req: NextApiRequest, res: NextApiResponse ) { if (req.method !== "POST") { return res.status(405).json({ error: "Method not allowed" }) } const { model, prompt } = req.body try { // Get conversation history const conversationHistory = readChatHistory() // Add current user message to the conversation const messages = [ ...conversationHistory, { role: "user" as const, content: prompt }, ] // Use chat endpoint instead of generate to maintain conversation context const ollamaRes = await fetch(`${OLLAMA_BASE_URL}/api/chat`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ model, messages, stream: false, }), }) const data = await ollamaRes.json() res.status(200).json(data) } catch (err) { console.error("Ollama API error:", err) res.status(500).json({ error: "Failed to connect to Ollama" }) } }