Files
OmniRoute/open-sse/services/xaiMessageCap.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

130 lines
4.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* xAI rejects a request with HTTP 413 when chat history exceeds 800 items:
* "Chat history exceeds the 800-message limit; compact the conversation and retry."
*
* Token-based compression does not catch this: a long agent loop of tiny
* tool calls still fits a 256k500k window. Cap the arrays xAI actually
* counts — Chat Completions `messages` and Responses `input` — at the
* executor edge, after any chat→Responses expansion.
*/
import {
fixToolAdjacency,
fixToolPairs,
stripTrailingAssistantOrphanToolUse,
} from "./contextManager.ts";
export const XAI_CHAT_HISTORY_LIMIT = 800;
type HistoryItem = Record<string, unknown>;
function isSystemRole(item: HistoryItem): boolean {
return item.role === "system" || item.role === "developer";
}
function repairChatMessages(messages: HistoryItem[]): HistoryItem[] {
let result = fixToolPairs(messages);
result = fixToolAdjacency(result);
result = fixToolPairs(result);
return stripTrailingAssistantOrphanToolUse(result);
}
/**
* Keep system/developer messages plus the newest tail, then drop tool-call
* orphans created by the cut. If the repaired list is still over the limit
* (lots of system messages), take the newest `limit` items and repair again.
*/
export function capXaiChatMessages(
messages: HistoryItem[],
limit = XAI_CHAT_HISTORY_LIMIT
): HistoryItem[] {
if (!Array.isArray(messages) || messages.length <= limit) return messages;
const system = messages.filter(isSystemRole);
const nonSystem = messages.filter((item) => !isSystemRole(item));
const budget = Math.max(2, limit - system.length);
let result = repairChatMessages([...system, ...nonSystem.slice(-budget)]);
if (result.length > limit) {
result = repairChatMessages(result.slice(-limit));
}
return result;
}
function lastUserIndex(items: HistoryItem[]): number {
for (let i = items.length - 1; i >= 0; i--) {
if (items[i].role === "user") return i;
}
return -1;
}
/**
* Responses `input` expands one assistant+tools chat turn into many items
* (`function_call` + `function_call_output`). Drop orphans left by a tail cut:
* outputs whose call was dropped, and mid-history calls whose output was
* dropped. Trailing unmatched `function_call`s (the in-flight turn) stay.
*/
export function repairXaiResponsesInput(items: HistoryItem[]): HistoryItem[] {
const callIds = new Set<string>();
const outputIds = new Set<string>();
for (const item of items) {
if (typeof item.call_id !== "string") continue;
if (item.type === "function_call") callIds.add(item.call_id);
if (item.type === "function_call_output") outputIds.add(item.call_id);
}
const lastUser = lastUserIndex(items);
return items.filter((item, idx) => {
if (item.type === "function_call_output") {
return typeof item.call_id === "string" && callIds.has(item.call_id);
}
if (item.type === "function_call") {
if (typeof item.call_id === "string" && outputIds.has(item.call_id)) return true;
return lastUser < 0 || idx > lastUser;
}
return true;
});
}
export function capXaiResponsesInput(
input: HistoryItem[],
limit = XAI_CHAT_HISTORY_LIMIT
): HistoryItem[] {
if (!Array.isArray(input) || input.length <= limit) return input;
let result = repairXaiResponsesInput(input.slice(-limit));
if (result.length > limit) {
result = repairXaiResponsesInput(result.slice(-limit));
}
return result;
}
/**
* Cap whichever history array the body is using. No-op (same object /
* same array refs) when already within the limit.
*/
export function capXaiRequestHistory(
body: Record<string, unknown>
): Record<string, unknown> {
if (!body || typeof body !== "object") return body;
const next: Record<string, unknown> = { ...body };
let changed = false;
if (Array.isArray(body.messages)) {
const messages = capXaiChatMessages(body.messages as HistoryItem[]);
if (messages !== body.messages) {
next.messages = messages;
changed = true;
}
}
if (Array.isArray(body.input)) {
const input = capXaiResponsesInput(body.input as HistoryItem[]);
if (input !== body.input) {
next.input = input;
changed = true;
}
}
return changed ? next : body;
}