fix(server): stream Ollama model pulls so long downloads finish

A non-streaming /api/pull holds the response headers until the download
ends, and Node fetch gives up on headers after five minutes, cancelling
every larger model pull. Request stream:true and read the NDJSON progress,
failing on an in-stream error or a missing success status (UF-008).
This commit is contained in:
shumeiko.it
2026-10-15 18:13:34 +02:00
parent 15e5bbe3f6
commit 3e1a188c03
2 changed files with 107 additions and 2 deletions

View File

@@ -70,6 +70,38 @@ function isValidOllamaModelRef(model: string): boolean {
return repository.split('/').every((segment) => segment.length > 0 && segment !== '.' && segment !== '..');
}
class OllamaPullError extends Error {}
/**
* Reads Ollama's NDJSON pull progress to the end and returns the final status.
* Throws OllamaPullError when Ollama reports an error line or the stream ends
* without "success".
*/
async function readOllamaPullStream(res: Response): Promise<Record<string, unknown>> {
if (!res.body) throw new OllamaPullError('Ollama returned an empty pull response');
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffered = '';
let last: Record<string, unknown> | null = null;
for (;;) {
const chunk = await reader.read();
buffered += chunk.done ? decoder.decode() : decoder.decode(chunk.value, { stream: true });
const lines = buffered.split('\n');
buffered = chunk.done ? '' : (lines.pop() ?? '');
for (const line of lines) {
if (!line.trim()) continue;
const status = JSON.parse(line) as Record<string, unknown>;
if (typeof status.error === 'string') throw new OllamaPullError(status.error);
last = status;
}
if (chunk.done) break;
}
if (!last || last.status !== 'success') {
throw new OllamaPullError('the pull stream ended before Ollama reported success');
}
return last;
}
export interface LocalInferenceRuntimeController {
getStatus(): ManagedOllamaStatus;
ensureReady(): Promise<ManagedOllamaReadyResult>;
@@ -327,14 +359,23 @@ export async function localInferenceRoutes(
const res = await fetch(`${OLLAMA_URL}/api/pull`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: model, stream: false }),
// Stream progress. With stream:false Ollama sends no headers until the
// download completes, and Node's fetch aborts after 300 s without
// headers, which cancelled every pull longer than five minutes.
body: JSON.stringify({ name: model, stream: true }),
signal: AbortSignal.timeout(45 * 60_000),
});
if (!res.ok) {
const text = await res.text();
return reply.code(502).send({ error: `Ollama pull failed: ${text}` });
}
const pullStatus = await res.json();
let pullStatus: Record<string, unknown>;
try {
pullStatus = await readOllamaPullStream(res);
} catch (error) {
if (!(error instanceof OllamaPullError)) throw error;
return reply.code(502).send({ error: `Ollama pull failed: ${error.message}` });
}
const installed = await probeOllama(OLLAMA_URL);
const installedModel = installed.models.find((name) => name === model || name === `${model}:latest`);
if (!installedModel) {