Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Fixes 5: Assist reads the same Ollama check, so a busy Ollama keeps chats on it

Claude90e1a4f75d4f
diff --git a/src/server/assist/agent.ts b/src/server/assist/agent.ts
index 3b5967e5..19344962 100644
--- a/src/server/assist/agent.ts
+++ b/src/server/assist/agent.ts
@@ -29,6 +29,7 @@ import type { Engine, Generation, MediaKind } from "@/lib/catalog/types";
import { db } from "../db";
import { getGeneration } from "../generations";
import { offeredDefault } from "../model-access";
+import { ollamaModels, probeOllama as ollamaAnswers } from "../providers/llm";
import { engineMode, getSetting } from "../settings";
import { getSkill } from "../skills";
import { readRef } from "../storage";
@@ -364,17 +365,15 @@ export async function updateThread(id: string, patch: ThreadPatch): Promise<Assi
// ---------------------------------------------------------------------------
// Which LLM runs a chat: modes
-let ollamaProbe: { at: number; url: string; ok: boolean; models: string[] } | null = null;
+/**
+ * Whether Ollama answers, and the models it has: the check text steps use (providers/llm), so a busy Ollama (loading
+ * a model, writing a long answer) keeps chats on it instead of the offline planner.
+ */
async function probeOllama(): Promise<{ ok: boolean; models: string[] }> {
- const url = getSetting("ollamaUrl").replace(/\/+$/, "");
- if (ollamaProbe && ollamaProbe.url === url && Date.now() - ollamaProbe.at < 30_000) return ollamaProbe;
- const res = await fetch(`${url}/api/tags`, { signal: AbortSignal.timeout(800) })
- .then(async (r) => (r.ok ? ((await r.json().catch(() => ({}))) as { models?: { name?: string }[] }) : null))
- .catch(() => null);
- ollamaProbe = { at: Date.now(), url, ok: !!res, models: (res?.models ?? []).map((m) => m.name ?? "").filter(Boolean).slice(0, 50) };
- return ollamaProbe;
+ const ok = await ollamaAnswers();
+ return { ok, models: ok ? ollamaModels() : [] };
}
-const ollamaReachable = async () => (await probeOllama()).ok;
+const ollamaReachable = () => ollamaAnswers();
/**
* Which LLM drives new chats by default: Claude when a key is set (on open-source-only installs only when
diff --git a/src/server/providers/llm.ts b/src/server/providers/llm.ts
index cbb06c66..1128507d 100644
--- a/src/server/providers/llm.ts
+++ b/src/server/providers/llm.ts
@@ -39,32 +39,50 @@ export function textOutput(text: string, cues?: Cue[] | null, meta: Record<strin
// Which LLM
declare global {
- /** Reachability of Ollama: when it last answered, and when a longer look last went unanswered (./reach). */
- var __fuobeOllamaReach: Reach | undefined;
+ /** Reachability of Ollama, the models it listed when it last answered, when it last answered, and when a longer look last went unanswered (./reach). */
+ var __fuobeOllamaReach: (Reach & { models?: string[] }) | undefined;
var __fuobeLlmJobs: Map<string, LlmJob> | undefined;
}
const ollamaUrl = () => getSetting("ollamaUrl").replace(/\/+$/, "");
const PROBE_MS = 15_000;
+/** What GET /api/tags got: an answer (with the names of the models pulled), a no, or nothing in time. */
+function askOllama(url: string, timeoutMs: number) {
+ return ask<string[]>(`${url}/api/tags`, timeoutMs, {
+ read: async (r) => {
+ const models = ((await r.json()) as { models?: { name?: unknown }[] } | null)?.models;
+ return Array.isArray(models) ? models.map((m) => (typeof m?.name === "string" ? m.name : "")).filter(Boolean).slice(0, 50) : undefined;
+ },
+ });
+}
+
/**
* Is Ollama answering GET /api/tags? By the busy-vs-down rules ComfyUI has (./reach): a refused port or an HTTP
* error is down at once; no answer in time is busy when it answered lately, else it gets the longer look when its
* port takes a connection. Cached for 15 s (`force` checks now); one check runs at a time. Routing reads the cached
- * answer (see llmBackends).
+ * answer (see llmBackends), and so does Assist (with the models). A busy Ollama keeps the models it last listed.
*/
export async function probeOllama(force = false): Promise<boolean> {
const cur = globalThis.__fuobeOllamaReach;
const url = ollamaUrl();
if (!force && cur && cur.url === url && Date.now() - cur.at < PROBE_MS) return cur.ok;
return sharedCheck("ollama", url, async () => {
- const { reach } = await checkReach(url, globalThis.__fuobeOllamaReach, (ms) => ask(`${url}/api/tags`, ms));
+ const prev = globalThis.__fuobeOllamaReach?.url === url ? globalThis.__fuobeOllamaReach : undefined;
+ const { reach, asked } = await checkReach(url, prev, (ms) => askOllama(url, ms));
+ const models = (asked.answer === "yes" ? asked.value : undefined) ?? prev?.models;
// The address may have changed while this ran: the new one's check speaks for it.
- if (url === ollamaUrl()) globalThis.__fuobeOllamaReach = reach;
+ if (url === ollamaUrl()) globalThis.__fuobeOllamaReach = reach.ok && models ? { ...reach, models } : reach;
return reach.ok;
});
}
+/** The models the reachable Ollama listed when it last answered ([] when it's down, or no check has read them yet). */
+export function ollamaModels(): string[] {
+ const cur = globalThis.__fuobeOllamaReach;
+ return cur?.ok && cur.url === ollamaUrl() ? (cur.models ?? []) : [];
+}
+
/**
* Blocking check (reachSync): a few ms for an Ollama that answers or a refused port, at most `timeoutMs` otherwise.
* One that takes the connection but doesn't answer in time is busy for now, and probeOllama goes on in the background
diff --git a/tests/ollama-reach.test.ts b/tests/ollama-reach.test.ts
index 62311918..9f745822 100644
--- a/tests/ollama-reach.test.ts
+++ b/tests/ollama-reach.test.ts
@@ -163,6 +163,25 @@ describe("Ollama reachability under load", () => {
}
});
+ it("Assist reads the same check: a busy Ollama keeps chats on it, with the models it last listed", async () => {
+ const agent = await import("@/server/assist/agent");
+ expect(await llm.probeOllama(true)).toBe(true);
+ expect(llm.ollamaModels()).toEqual(["qwen3:8b"]);
+ // Busy: its list comes after Assist's old 0.8 s limit, which took Ollama for gone and chats to the offline planner.
+ delay = 1500;
+ expect(await llm.probeOllama(true)).toBe(true);
+ expect(await agent.availability()).toMatchObject({ ollama: true, ollamaModels: ["qwen3:8b"] });
+ setSetting("enhanceProvider", "auto");
+ try {
+ expect(await agent.resolveAgent()).toBe("ollama");
+ } finally {
+ setSetting("enhanceProvider", "ollama");
+ }
+ // A refused port is down for Assist too, with no models.
+ setSetting("ollamaUrl", `http://127.0.0.1:${await closedPort()}`);
+ expect(await agent.availability()).toMatchObject({ ollama: false, ollamaModels: [] });
+ });
+
it("a late Ollama that answers the longer look is reachable; a check of an address changed meanwhile keeps to itself", async () => {
delay = 1000;
const old = llm.probeOllama(true);