Files
OmniRoute/open-sse/utils/ollamaTransform.ts
Praveen K Palaniswamy 65e81158ab fix(ollama): route models by advertised capability (#11088)
Landed with the design call resolved per the owner's pick — **option 1**: the synced store is now endpoint-agnostic (persistDiscoveredModels and managedModelImport no longer drop non-chat models at write time), and chat selectability moved to read time (auto-pool expansion in autoStrategy applies filterChatSelectableModels; the models-route projection already had its chatOnly filter). Your discovery test now passes end-to-end (3/3): /api/show capabilities persist per connection and image/embedding requests route through the advertising host.

Reconciliation notes: conflicted areas merged onto the current tip (adobe discovery import, requestedModel preflight signature, resolvedProvider fast-path coexists with the synced-route override — explicit resolution wins); carried base-red drains (#10055 memoization, #11071 test variants) dropped as already-landed; the managed-model-import exclusion test was propagated to the new contract (image/video models persist; the read filter still hides them from chat pickers — pinned by a new assertion). Full battery: 205/206 focused (the one red is a confirmed periodic-timer timing flake on the loaded devbox — 20/20 isolated), autoCombo vitest 30/30, combo suites 46/46, gates + typecheck clean.

Thank you @yourspraveen — the capability probe + routing design was right; it just needed the store contract opened up. Fixes #11087.
2026-08-23 11:45:01 -03:00

144 lines
5.1 KiB
TypeScript

import { CORS_HEADERS } from "./cors.ts";
import { getReadableReasoningValue } from "./reasoningFields.ts";
type PendingToolCall = {
id?: string;
function: {
name: string;
arguments: string;
};
};
// Transform OpenAI SSE stream to Ollama JSON lines format
export function transformToOllama(response, model) {
// Only successful SSE responses belong to the NDJSON transformer. Preserve errors,
// bodyless responses, and successful JSON responses without losing status/body/headers.
const contentType = String(response.headers?.get?.("content-type") || "").toLowerCase();
if (!response.ok || !response.body || !contentType.includes("text/event-stream")) return response;
let buffer = "";
let pendingToolCalls: Record<number, PendingToolCall> = {};
const completedToolCalls: PendingToolCall[] = [];
const transform = new TransformStream(
{
transform(chunk, controller) {
const text = new TextDecoder().decode(chunk);
buffer += text;
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (!line.startsWith("data:")) continue;
const data = line.slice(5).trim();
if (data === "[DONE]") {
const ollamaEnd =
JSON.stringify({ model, message: { role: "assistant", content: "" }, done: true }) +
"\n";
controller.enqueue(new TextEncoder().encode(ollamaEnd));
return;
}
try {
const parsed = JSON.parse(data);
const delta = parsed.choices?.[0]?.delta || {};
const content = delta.content || "";
const thinking = getReadableReasoningValue(delta);
const toolCalls = delta.tool_calls;
if (toolCalls) {
for (const tc of toolCalls) {
const idx = tc.index;
const toolCallId = tc.id != null ? String(tc.id) : tc.id;
// T37: Prevent merging tool_calls on same index if ID changes
if (
pendingToolCalls[idx] &&
toolCallId &&
pendingToolCalls[idx].id !== toolCallId
) {
completedToolCalls.push(pendingToolCalls[idx]);
delete pendingToolCalls[idx];
}
if (!pendingToolCalls[idx]) {
pendingToolCalls[idx] = {
id: toolCallId,
function: { name: "", arguments: "" },
};
}
if (tc.function?.name) pendingToolCalls[idx].function.name += tc.function.name;
if (tc.function?.arguments)
pendingToolCalls[idx].function.arguments += tc.function.arguments;
}
}
if (thinking) {
const ollama =
JSON.stringify({
model,
message: { role: "assistant", content: "", thinking },
done: false,
}) + "\n";
controller.enqueue(new TextEncoder().encode(ollama));
}
if (content) {
const ollama =
JSON.stringify({ model, message: { role: "assistant", content }, done: false }) +
"\n";
controller.enqueue(new TextEncoder().encode(ollama));
}
const finishReason = parsed.choices?.[0]?.finish_reason;
if (finishReason === "tool_calls" || finishReason === "stop") {
const toolCallsArr = [...completedToolCalls, ...Object.values(pendingToolCalls)];
if (toolCallsArr.length > 0) {
const formattedCalls = toolCallsArr.map((tc) => ({
function: {
name: tc.function.name,
arguments: JSON.parse(tc.function.arguments || "{}"),
},
}));
const ollama =
JSON.stringify({
model,
message: { role: "assistant", content: "", tool_calls: formattedCalls },
done: true,
}) + "\n";
controller.enqueue(new TextEncoder().encode(ollama));
pendingToolCalls = {};
} else if (finishReason === "stop") {
const ollamaEnd =
JSON.stringify({
model,
message: { role: "assistant", content: "" },
done: true,
}) + "\n";
controller.enqueue(new TextEncoder().encode(ollamaEnd));
}
}
} catch (e) {
// Silently ignore parse errors
}
}
},
flush(controller) {
const ollamaEnd =
JSON.stringify({ model, message: { role: "assistant", content: "" }, done: true }) + "\n";
controller.enqueue(new TextEncoder().encode(ollamaEnd));
},
},
{ highWaterMark: 16384 },
{ highWaterMark: 16384 }
);
return new Response(response.body.pipeThrough(transform), {
headers: {
"Content-Type": "application/x-ndjson",
},
});
}