Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

17.1 KB
Raw Blame
/**
 * Shared vocabulary for the model configuration surface.
 *
 * A service's own endpoint is the source of truth for which models it serves;
 * models.dev enriches those rows with published metadata. This module owns what
 * both sides of the IPC boundary agree on: the wire API styles, how a published
 * `npm` adapter maps onto one, how a published record becomes a persisted
 * binding, and the shared capability and formatting helpers.
 *
 * Nothing here performs I/O, so the renderer, the Electron main process and the
 * tests share one implementation instead of re-deriving these rules per surface.
 */

import { isImageOnlyChatModel, type ImageGenerationBindings } from "./image-generation.js";
import { modelWireIdsEqual } from "./types/models.js";
import { publishedThinkingLevels } from "./thinking-levels.js";
import type {
  ModelLimitSource,
  ModelBinding,
  ModelInfo,
  ThinkingLevel,
} from "./types.js";

/** Wire protocol a provider row speaks. Mirrors the runtime adapter list. */
export const API_STYLES = [
  "chat_completions",
  "responses",
  "anthropic_messages",
  "google_generative_ai",
  "openai_codex_responses",
  "pi_messages",
  "opencode_go",
] as const;

export type CatalogApiStyle = (typeof API_STYLES)[number];

/**
 * How a model-list probe asks a service what it serves.
 *
 * This is deliberately narrower than {@link CatalogApiStyle}: a successful
 * `GET {base}/models` with `Authorization: Bearer` proves the endpoint answers
 * OpenAI-compatible discovery, and nothing about whether the generation call is
 * `/chat/completions`, `/responses` or something else.
 */
export type DiscoveryStyle = "openai_models" | "anthropic_models" | "google_models";

/**
 * Keep persisted provider rows editable when an older or newer client stored
 * an API style this renderer does not know yet. Runtime transport resolution
 * uses the same Chat Completions fallback for unknown styles.
 */
export function normalizeApiStyle(value?: string | null): CatalogApiStyle {
  return API_STYLES.some((style) => style === value)
    ? (value as CatalogApiStyle)
    : "chat_completions";
}

/**
 * models.dev publishes an `npm` adapter package per provider. That value is the
 * most reliable published signal for which wire API a provider speaks, so the
 * setup flow derives the API style from it instead of asking the user to guess.
 */
const ADAPTER_API_STYLES: ReadonlyArray<readonly [string, CatalogApiStyle]> = [
  ["@ai-sdk/google-vertex/anthropic", "anthropic_messages"],
  ["@ai-sdk/anthropic", "anthropic_messages"],
  ["@ai-sdk/google-vertex", "google_generative_ai"],
  ["@ai-sdk/google", "google_generative_ai"],
  ["@ai-sdk/openai", "responses"],
];

/**
 * Resolve the wire style for a published adapter package. Unknown and
 * OpenAI-compatible adapters fall back to chat completions, the broadest
 * interoperable surface.
 */
export function apiStyleForAdapter(npm?: string | null): CatalogApiStyle {
  const adapter = (npm ?? "").trim().toLowerCase();
  if (!adapter) return "chat_completions";
  for (const [name, style] of ADAPTER_API_STYLES) {
    if (adapter === name) return style;
  }
  return "chat_completions";
}

/** One published capability a model row can be checked against. */
export type ModelFilter = "reasoning" | "vision" | "tools" | "attachments" | "pdf";

/** Whether a published record satisfies a capability filter. */
export function modelMatchesFilter(model: ModelInfo, filter: ModelFilter): boolean {
  switch (filter) {
    case "reasoning":
      return model.reasoning === true || (model.supportedThinkingLevels?.length ?? 0) > 0;
    case "vision":
      return (
        model.capabilities.includes("vision") ||
        (model.modalities?.input?.includes("image") ?? false)
      );
    case "tools":
      return model.toolCall === true || model.capabilities.includes("tools");
    case "attachments":
      return model.attachment === true || model.capabilities.includes("attachments");
    case "pdf":
      return (
        model.capabilities.includes("pdf") ||
        (model.modalities?.input?.includes("pdf") ?? false)
      );
    default:
      return false;
  }
}

/**
 * Effective image input for a binding: the user override when set, otherwise the
 * published capability. One helper so settings, the composer and the transport
 * gate cannot drift into three different answers.
 */
export function bindingSupportsImages(
  binding?: Pick<ModelBinding, "supportsImages"> | null,
  model?: ModelInfo | null,
): boolean {
  if (typeof binding?.supportsImages === "boolean") return binding.supportsImages;
  return model ? modelMatchesFilter(model, "vision") : false;
}

/** Effective document (PDF) input for a binding, override first. */
export function bindingSupportsDocuments(
  binding?: Pick<ModelBinding, "supportsDocuments"> | null,
  model?: ModelInfo | null,
): boolean {
  if (typeof binding?.supportsDocuments === "boolean") return binding.supportsDocuments;
  return model ? modelMatchesFilter(model, "pdf") : false;
}

const THINKING_ORDER: readonly ThinkingLevel[] = [
  "off",
  "minimal",
  "low",
  "medium",
  "high",
  "xhigh",
  "max",
];

/** Sort thinking levels into canonical ascending order, dropping duplicates. */
export function sortThinkingLevels(levels: readonly ThinkingLevel[]): ThinkingLevel[] {
  return [...new Set(levels)].sort(
    (left, right) => THINKING_ORDER.indexOf(left) - THINKING_ORDER.indexOf(right),
  );
}

export const CATALOG_DEFAULT_CONTEXT_WINDOW = 128_000;
export const CATALOG_DEFAULT_MAX_TOKENS = 8_192;

/**
 * Resolve a model's effective context window.
 *
 * `source` is the stored provenance of the configured value. A `catalog`
 * binding follows the published record, so a models.dev correction reaches a
 * binding that was saved before the fix; a `user` binding is the user's own
 * number and is never replaced, even when it equals the generic fallback.
 *
 * A binding without provenance keeps its stored value. Older records cannot
 * distinguish an intentional 128k override from the generic seed, so inferring
 * catalog ownership would risk overwriting a user choice.
 */
export function effectiveContextWindow(
  publishedContextWindow?: number | null,
  configuredContextWindow?: number | null,
  source?: ModelLimitSource | null,
): number | undefined {
  const published = positiveTokenCount(publishedContextWindow);
  const configured = positiveTokenCount(configuredContextWindow);

  if (source === "catalog") return published ?? configured;
  if (source === "user") return configured ?? published;
  if (configured === undefined) return published;
  return configured;
}

/**
 * Resolve a model's effective output cap.
 *
 * Output-cap provenance is independent from context-window provenance: a
 * `catalog` follows the published record and `user` is never replaced. A
 * legacy binding with no output-cap marker preserves its stored value, including
 * 8.2k: older records cannot distinguish that seed from an intentional user
 * choice. Changing a context window never changes output-cap ownership.
 */
export function effectiveMaxTokens(
  publishedMaxTokens?: number | null,
  configuredMaxTokens?: number | null,
  source?: ModelLimitSource | null,
): number | undefined {
  const published = positiveTokenCount(publishedMaxTokens);
  const configured = positiveTokenCount(configuredMaxTokens);

  if (source === "catalog") return published ?? configured;
  if (source === "user") return configured ?? published;
  if (configured === undefined) return published;
  return configured;
}

/** Token counts are whole positive numbers; anything else means "unset". */
function positiveTokenCount(value?: number | null): number | undefined {
  if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
    return undefined;
  }
  return Math.round(value);
}

/** Binding fields the limits resolver reads and rewrites. */
type BindingLimits = Pick<ModelBinding, "contextWindow"> &
  Partial<Pick<ModelBinding, "maxTokens" | "contextWindowSource" | "maxTokensSource">>;

/**
 * Resolve a saved binding's limits against its catalog baseline for
 * `modelConfigWithBinding`.
 *
 * That runtime helper applies the historical two-argument rule, which cannot
 * tell a hand-edited 128k from the generic seed, and it takes the binding's
 * output cap as written. Callers that know where a stored value came from
 * resolve both limits first: the returned baseline and binding carry the
 * source-aware window and cap, so a saved row reads and runs with the number the
 * catalog publishes once the model resolves — a row seeded before the record
 * existed stops showing the generic 8.2k output. An exported binding keeps the
 * catalog as its provenance whenever the catalog supplied the value, so a later
 * settings save cannot freeze an inherited value into a snapshot of its own.
 *
 * A `generic` baseline is the fallback for a lookup that found no record, not
 * a published limit, so it never replaces a saved limit: a catalog snapshot
 * taken while the record still resolved stays in force.
 */
export function resolveBindingLimits<
  C extends { contextWindow?: number | null; maxTokens?: number | null; source?: string },
  B extends BindingLimits,
>(catalogConfig: C, binding: B): {
  catalogConfig: C;
  binding: B & Partial<Pick<ModelBinding, "contextWindowSource" | "maxTokensSource">>;
};
export function resolveBindingLimits<
  C extends { contextWindow?: number | null; maxTokens?: number | null; source?: string },
  B extends BindingLimits,
>(
  catalogConfig: C,
  binding: B | null | undefined,
): {
  catalogConfig: C;
  binding: (B & Partial<Pick<ModelBinding, "contextWindowSource" | "maxTokensSource">>) | null | undefined;
};
export function resolveBindingLimits(
  catalogConfig: { contextWindow?: number | null; maxTokens?: number | null; source?: string },
  binding: BindingLimits | null | undefined,
): {
  catalogConfig: { contextWindow?: number | null; maxTokens?: number | null; source?: string };
  binding: BindingLimits | null | undefined;
} {
  if (!binding) return { catalogConfig, binding };
  const publishedRecord = catalogConfig.source !== "generic";
  const published = publishedRecord
    ? positiveTokenCount(catalogConfig.contextWindow)
    : undefined;
  const publishedMax = publishedRecord
    ? positiveTokenCount(catalogConfig.maxTokens)
    : undefined;
  const contextWindowSource = binding.contextWindowSource ?? "user";
  const resolved = effectiveContextWindow(
    published,
    binding.contextWindow,
    contextWindowSource,
  );
  // Older rows lack independent output provenance, so preserve their stored cap
  // rather than guessing whether the generic seed was a deliberate user choice.
  const maxTokensSource = binding.maxTokensSource ?? "user";
  const resolvedMax = effectiveMaxTokens(publishedMax, binding.maxTokens, maxTokensSource);
  const resolvedMaxSource = maxTokensSource;
  if (resolved === undefined && resolvedMax === undefined) {
    return { catalogConfig, binding };
  }
  const inherited = contextWindowSource === "catalog" && published !== undefined;
  return {
    catalogConfig: {
      ...catalogConfig,
      ...(resolved !== undefined ? { contextWindow: resolved } : {}),
      ...(resolvedMax !== undefined ? { maxTokens: resolvedMax } : {}),
    },
    binding: {
      ...binding,
      ...(resolved !== undefined
        ? {
            contextWindow: resolved,
            contextWindowSource: inherited ? "catalog" as const : contextWindowSource,
          }
        : {}),
      ...(resolvedMax !== undefined
        ? {
            maxTokens: resolvedMax,
            ...(resolvedMaxSource ? { maxTokensSource: resolvedMaxSource } : {}),
          }
        : {}),
    },
  };
}

/**
 * Build the persisted binding for a published record. Published limits and
 * thinking levels seed a fresh binding, so a newly picked model needs no
 * manual token entry; the user can still configure the endpoint explicitly.
 */
export function bindingFromModelInfo(model: ModelInfo): ModelBinding {
  const thinkingLevels = sortThinkingLevels(publishedThinkingLevels(model));
  return {
    id: model.modelId,
    contextWindow:
      model.contextWindow || model.limit?.context || CATALOG_DEFAULT_CONTEXT_WINDOW,
    // The snapshot is a catalog value, not a user answer: a later models.dev
    // correction still reaches this binding.
    contextWindowSource: "catalog",
    maxTokens: model.maxTokens || model.limit?.output || CATALOG_DEFAULT_MAX_TOKENS,
    maxTokensSource: "catalog",
    thinkingLevels,
    defaultThinkingLevel: thinkingLevels.includes("medium")
      ? "medium"
      : (thinkingLevels[0] ?? null),
    ...(model.thinkingProtocol ? { thinkingProtocol: model.thinkingProtocol } : {}),
    // Absent overrides keep following models.dev, so a catalog correction still
    // reaches an already saved binding.
    supportsImages: null,
    supportsDocuments: null,
  };
}

/**
 * Binding for a hand-typed id the catalog does publish.
 *
 * The published record seeds limits and thinking levels exactly like a picked
 * model, but the stored id stays what the user typed: the id is the string a
 * request is addressed with, and a catalog spelling that differs in case or
 * separators must not silently retarget it.
 */
export function bindingForCustomModelInfo(
  id: string,
  info: ModelInfo,
): ModelBinding {
  return { ...bindingFromModelInfo(info), id: id.trim() };
}

/** Binding for a model id the catalog does not publish. */
export function bindingForCustomModel(id: string): ModelBinding {
  return {
    id: id.trim(),
    // An unpublished model has no catalog value to inherit yet, so the seed
    // stays catalog-sourced: if models.dev describes the id later, the window
    // it publishes takes over.
    contextWindow: CATALOG_DEFAULT_CONTEXT_WINDOW,
    contextWindowSource: "catalog",
    maxTokens: CATALOG_DEFAULT_MAX_TOKENS,
    maxTokensSource: "catalog",
    thinkingLevels: [],
    defaultThinkingLevel: null,
    supportsImages: null,
    supportsDocuments: null,
  };
}

/**
 * Drop a fraction's trailing zeros: `200.0` reads `200` and `1.10` reads `1.1`.
 */
function trimFraction(value: string): string {
  if (!value.includes(".")) return value;
  return value.replace(/0+$/, "").replace(/\.$/, "");
}

/**
 * Compact token count for dense UI.
 *
 * Published context windows sit on values a single rounded decimal cannot tell
 * apart — 1,000,000, 1,048,576 and 1,050,000 all rendered as `1M`/`1.1M`, and
 * reading 1,050,000 as `1.1M` overstated the window by 50k tokens. Two decimals
 * at the `M` scale and one at the `K` scale keep neighbouring rows distinct
 * while staying short: `1M`, `1.05M`, `1.1M`, `262.1K`.
 *
 * A count of `0` is a real value here, so callers that render an unpublished
 * limit go through `formatTokenCount` instead.
 */
export function formatCompactTokenCount(tokens: number): string {
  if (tokens < 1_000) return String(tokens);
  const thousands = tokens / 1_000;
  // Rounding can push a `K` mantissa up to 1000 (999,999 -> `1000K`), which
  // reads as a scale error. Promote those to the `M` scale instead.
  if (Number(thousands.toFixed(1)) < 1_000) {
    return `${trimFraction(thousands.toFixed(1))}K`;
  }
  return `${trimFraction((tokens / 1_000_000).toFixed(2))}M`;
}

/**
 * Compact token count for a model limit, e.g. `200K`, `1.05M`.
 *
 * An absent or non-positive limit means the service never published one, so it
 * renders as an em dash rather than a number the user might trust.
 */
export function formatTokenCount(tokens?: number): string {
  if (!tokens || tokens <= 0) return "—";
  return formatCompactTokenCount(tokens);
}

/**
 * Per-million-token price for the picker's cost column. models.dev publishes
 * cost per million tokens already, so this only formats.
 */
export function formatModelPrice(cost?: ModelInfo["cost"]): string {
  const input = cost?.input;
  const output = cost?.output;
  if (input === undefined && output === undefined) return "—";
  const price = (value?: number) =>
    value === undefined
      ? "—"
      : value === 0
        ? "free"
        : `$${value < 1 ? value.toFixed(2) : String(value)}`;
  return `${price(input)} / ${price(output)}`;
}

/** The Composer only offers enabled providers that are ready to run. */
export function isChatProviderReady(provider: { enabled?: boolean; hasSecret?: boolean; authKind?: string }): boolean {
  return provider.enabled === true && (provider.hasSecret === true || provider.authKind === "none");
}

/** Discovery can filter or decorate a configured model; it never adds one. */
export function configuredChatModels(
  provider: { id: string; models?: readonly ModelBinding[]; defaultModelId?: string },
  discovered: readonly ModelInfo[] = [],
  imageGeneration?: ImageGenerationBindings | null,
) {
  const ids = (provider.models ?? []).map(({ id }) => id.trim()).filter(Boolean);
  const legacyId = provider.defaultModelId?.trim();
  return [...new Set(ids.length ? ids : legacyId ? [legacyId] : [])].flatMap((modelId) => {
    const matches = (model: ModelInfo) => modelWireIdsEqual(model.modelId, modelId);
    const metadata = discovered.find(matches);
    if (isImageOnlyChatModel(imageGeneration, provider.id, modelId, metadata) ||
      discovered.some((model) => matches(model) && model.discoveryState === "unlisted")) return [];
    const binding = provider.models?.find(({ id }) => modelWireIdsEqual(id, modelId));
    return [{ modelId, metadata, binding }];
  });
}