Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* How large a context window a provider/model gets for planning, and where that
* number came from (a user override, a reported window, or a ladder guess the
* card must label as assumed) — plus the "(32K ctx)" option label built on it.
*
* The rungs — the vendor constants, the known-model table, the per-provider
* fallback, the catalog read and the live-daemon merge — are re-exported from the pure leaf
* `server/lib/providerContextWindows.js`, the same ones the server's
* `effectiveContextWindow` budgets with. `resolveModelContextWindow` is the
* client's own walk over them, because the UI also needs to know WHERE the
* number came from; it and `isLikelyLargeContextProvider` are what can still
* drift from the server (the walk order, and the client's own local-endpoint
* test), so keep both beside their server twins in `stageRunner.js`.
*
* The Ollama `num_ctx` ceiling is shared with the SERVER's dispatch gate too,
* so the meter cannot promise a window a run would then be refused for (#7466).
* Its second rung — the ambient `OLLAMA_CONTEXT_LENGTH` the server launched the
* daemon with — is server env the browser cannot read, so it arrives as
* `runtimeContextWindow` on the readiness payload and is folded in with
* `withRuntimeContextWindow` at the two call sites that budget (#7472).
*
* Re-exported by `./providers.js` for existing `utils/providers` imports.
*/
import { formatContextLength } from './formatters.js';
import { isLocalEndpoint } from './providerEndpoints.js';
import { isApiProvider, isProcessProvider } from './providerTypes.js';
import {
DEFAULT_LARGE_CONTEXT_WINDOW,
catalogModelContextWindow,
knownModelContextWindow,
knownProviderContextWindow,
} from '../../../server/lib/providerContextWindows.js';
// The Ollama `num_ctx` ceiling, from the module the server's budgeter and its
// pre-dispatch gate resolve it through (#7466). Reached at the toolkit's
// dependency-free `internal/` leaf for the same reason `providerTypes.js`
// already takes `isOllamaBackedProvider` from there.
import { clampToRuntimeContextWindow } from '../../../server/lib/aiToolkit/internal/ollamaBacked.js';
export {
// Stamps the window PortOS holds a local Ollama daemon at onto a provider
// projection, so the clamp above sees the rung the browser cannot resolve for
// itself: the ambient `OLLAMA_CONTEXT_LENGTH` this install launched the daemon
// with. The number arrives as `runtimeContextWindow` on the readiness payload,
// beside the served windows `mergeObservedContextWindows` folds in (#7472).
withRuntimeContextWindow,
} from '../../../server/lib/aiToolkit/internal/ollamaBacked.js';
export {
DEFAULT_LARGE_CONTEXT_WINDOW,
CODEX_CONTEXT_WINDOW,
GEMINI_CONTEXT_WINDOW,
GROK_CONTEXT_WINDOW,
KIMI_CONTEXT_WINDOW,
knownModelContextWindow,
knownProviderContextWindow,
catalogModelContextWindow,
// Folds a daemon's live `/v1/models` windows (off the readiness payload) onto
// the catalog rung, exactly as the server's budgeter does — shared rather than
// re-walked so the card cannot rank the observation differently.
mergeObservedContextWindows,
} from '../../../server/lib/providerContextWindows.js';
export const isLikelyLargeContextProvider = (provider) => {
if (isProcessProvider(provider)) return true;
return isApiProvider(provider) && !isLocalEndpoint(provider.endpoint);
};
/**
* Where a resolved context window came from — three states, because that is
* what the UI actually distinguishes:
*
* - `REPORTED` — a real window for this model/provider (catalog, known-model
* table, vendor default, or Ollama's num_ctx). Which of those it was does not
* change what the card says, so they collapse into one state rather than
* growing an enum member per rung.
* - `OVERRIDE` — a number the user typed; worth labelling as theirs.
* - `ASSUMED` — nobody reported one, so the ladder GUESSED. This is the state
* that has to be visible: a card printing "128K ctx" for a model whose real
* window is 1M reads as a measured fact with nothing to say otherwise.
*/
export const CONTEXT_WINDOW_SOURCE = Object.freeze({
OVERRIDE: 'override',
REPORTED: 'reported',
ASSUMED: 'assumed',
});
/**
* Which window this provider/model CLAIMS, before the runtime ceiling.
* Walks the same rungs, in the same order, as `claimedContextWindow` in
* server/services/stageRunner.js — the rungs are shared, so the order and the
* local-endpoint test are what could drift, and the card would then promise a
* budget the budgeter won't use.
*/
const claimedModelContextWindow = (provider, model) => {
const explicit = Number(provider?.contextWindow);
if (Number.isFinite(explicit) && explicit > 0) {
return { tokens: explicit, source: CONTEXT_WINDOW_SOURCE.OVERRIDE };
}
const catalog = catalogModelContextWindow(provider, model);
if (catalog) return { tokens: catalog, source: CONTEXT_WINDOW_SOURCE.REPORTED };
const known = knownModelContextWindow(model);
if (known) return { tokens: known, source: CONTEXT_WINDOW_SOURCE.REPORTED };
const providerKnown = knownProviderContextWindow(provider);
if (providerKnown) return { tokens: providerKnown, source: CONTEXT_WINDOW_SOURCE.REPORTED };
const numCtx = Number(provider?.numCtx);
if (Number.isFinite(numCtx) && numCtx > 0) {
return { tokens: numCtx, source: CONTEXT_WINDOW_SOURCE.REPORTED };
}
return isLikelyLargeContextProvider(provider)
? { tokens: DEFAULT_LARGE_CONTEXT_WINDOW, source: CONTEXT_WINDOW_SOURCE.ASSUMED }
: { tokens: null, source: null };
};
/**
* The planning context window for a provider/model AND where it came from —
* the claimed window held to what an Ollama daemon was LAUNCHED with, exactly
* as the server's budgeter and its pre-dispatch gate resolve it.
*
* `{ tokens: null, source: null }` means nothing is known (an unrecognized model
* on a local backend); the budgeter applies its own conservative floor there.
*
* @param {object|null|undefined} provider
* @param {string|null|undefined} model
* @returns {{tokens: number|null, source: string|null}}
*/
export const resolveModelContextWindow = (provider, model) => {
const claimed = claimedModelContextWindow(provider, model);
const tokens = clampToRuntimeContextWindow(provider, claimed.tokens);
// A clamped window is no longer whichever rung claimed it — what the card is
// now printing is the daemon's own `num_ctx`, which is a REPORTED window even
// when the number it replaced was a user override or a guess.
return tokens === claimed.tokens
? claimed
: { tokens, source: CONTEXT_WINDOW_SOURCE.REPORTED };
};
export const effectiveModelContextWindow = (provider, model) =>
resolveModelContextWindow(provider, model).tokens;
/**
* Display label for a model `<option>`: the id plus a "(32K ctx)" parenthetical
* when the model's context window is known. The option's `value` stays the raw
* id — only the label carries the annotation.
*
* Resolution is deliberately narrower than {@link resolveModelContextWindow}:
* only rungs that describe THIS model — the live local probe (`ctxById` from
* `useLocalModels`), the window `provider`'s catalog reported for it, then the
* known-model table. The provider-level and assumed rungs are excluded on
* purpose: they would stamp the same guessed number onto every option in the
* list, which says nothing and reads as fact.
*
* The daemon's launch ceiling is NOT one of those guesses — it applies to every
* option alike, and the picker is exactly where someone decides which model
* "fits", so a `(40K ctx)` option beside a card promising 8K is two numbers
* presented as fact in one form. Clamped for that reason (#7466).
*
* Take `provider` rather than a pre-merged map so every picker gets catalog
* windows for free — merging at the call site is what left the fallback-model
* and manuscript-override selects labelling a 1M model as if it were unknown.
*
* @param {string} id
* @param {Record<string, number>} [ctxById] — live windows for local models
* @param {object} [provider] — the provider whose catalog lists this model
* @returns {string}
*/
export const modelOptionLabel = (id, ctxById, provider) => {
const ctx = clampToRuntimeContextWindow(
provider,
ctxById?.[id] || catalogModelContextWindow(provider, id) || knownModelContextWindow(id),
);
const label = formatContextLength(ctx);
return label ? `${id} (${label})` : id;
};