feat: Enhanced Ollama Integration (Model Fetching/Pulling)
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-12-12 08:57:30 +01:00
parent 93063f772d
commit a184115d28
2 changed files with 169 additions and 15 deletions
+45
View File
@@ -210,6 +210,51 @@ export async function registerRoutes(app: Express): Promise<Server> {
res.json({ success: true });
});
app.post("/api/admin/ollama/tags", isAdmin, async (req, res) => {
const { baseUrl } = req.body;
// Default to localhost:11434 if not provided, or stored setting?
// Client should send the value from the input field.
const url = (baseUrl || "http://localhost:11434").replace(/\/$/, "") + "/api/tags";
try {
const resp = await fetch(url);
if (!resp.ok) throw new Error(`Ollama Error: ${resp.statusText}`);
const data = await resp.json();
res.json(data);
} catch (e: any) {
console.error("Ollama Tags Error:", e);
res.status(500).json({ error: "Failed to fetch Ollama tags: " + e.message });
}
});
app.post("/api/admin/ollama/pull", isAdmin, async (req, res) => {
const { baseUrl, model } = req.body;
if (!model) return res.status(400).json({ error: "Model name required" });
const url = (baseUrl || "http://localhost:11434").replace(/\/$/, "") + "/api/pull";
console.log(`Pulling Ollama model ${model} from ${url}...`);
try {
// connecting to ollama
const resp = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: model, stream: false }),
});
if (!resp.ok) {
const errText = await resp.text();
throw new Error(`Ollama Pull Error: ${errText}`);
}
const data = await resp.json();
res.json(data);
} catch (e: any) {
console.error("Ollama Pull Error:", e);
res.status(500).json({ error: "Failed to pull model: " + e.message });
}
});
// --- AI Routes ---
app.post("/api/ai/chat", async (req, res) => {
if (!req.isAuthenticated()) return res.sendStatus(401);