Skip to content

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

3.6 KB
Raw Blame
import { isClaudeHarnessProvider } from './providerTypes.js';
import { commandBasename } from './providerModels.js';

/**
 * Subscription-quota FAMILY identity: which provider configs belong to the same
 * paid plan (`claude`, `codex`, `agy`, `grok`).
 *
 * This is the pure half of the registry `server/services/providerUsage.js`
 * drives — that service attaches a `fetch` per family, and those fetches spawn
 * PTYs to scrape a CLI's `/usage` panel. Identity has no business dragging that
 * graph in: pricing, cost attribution, and route validation all need to answer
 * "which plan is this provider on?" without being able to start a subprocess.
 *
 * Distinct from `lib/providerVendors.js`, which is argv-shaped and includes
 * vendors with no subscription quota to meter (opencode, kimi, cursor).
 */

/**
 * A provider config belongs to at most one family. CLI/TUI commands are matched
 * by binary basename; the Grok/Kimi-style API providers by id or endpoint.
 * Local-runtime CLI wrappers (Ollama, MTPLX, llama.cpp, vLLM) are local/free and have no
 * subscription quota, so they map to no family (see `familyForProvider`).
 */
export const PROVIDER_FAMILIES = [
  {
    id: 'claude',
    label: 'Claude Code',
    idPattern: /claude/i,
    matches: isClaudeHarnessProvider
  },
  {
    id: 'codex',
    label: 'Codex',
    idPattern: /codex/i,
    matches: (p) => commandBasename(p.command) === 'codex'
  },
  {
    id: 'agy',
    label: 'Antigravity',
    idPattern: /antigravity|(^|[-_])agy([-_]|$)/i,
    matches: (p) => commandBasename(p.command) === 'agy' || /antigravity/i.test(p.id || '')
  },
  {
    id: 'grok',
    label: 'Grok',
    idPattern: /grok/i,
    matches: (p) => commandBasename(p.command) === 'grok' || /grok/i.test(p.id || '')
  }
];

/** Every family id, for schemas that must accept only a real family. */
export const PROVIDER_FAMILY_IDS = PROVIDER_FAMILIES.map((f) => f.id);

/** The human label for a family id, falling back to the id itself. Pure. */
export const familyLabel = (id) => PROVIDER_FAMILIES.find((f) => f.id === id)?.label || id;

/**
 * The family id a single provider config belongs to, or null for one that
 * belongs to none (a local-runtime wrapper, a pay-as-you-go API provider).
 * The inverse of `resolveEnabledFamilies`, so cost reporting can attribute a
 * provider's spend to the subscription that actually covered it.
 */
export function familyForProvider(provider) {
  if (!provider || provider.ollamaBacked === true || provider.lmstudioBacked === true || provider.mtplxBacked === true || provider.llamaBacked === true || provider.vllmBacked === true || provider.sglangBacked === true) return null;
  return PROVIDER_FAMILIES.find((f) => f.matches(provider))?.id ?? null;
}

/**
 * The family a provider ID alone identifies, or `null` when the id says nothing.
 *
 * Strictly weaker than `familyForProvider`, and deliberately so: the BINARY is
 * what really identifies a family, and a schema validating a stored config has
 * only the id — the provider list is service state a Zod schema cannot reach.
 * So this answers the one question that CAN be settled from an id, and answers
 * `null` for everything else, letting the caller fall through to the live
 * check. Used by `quotaBurnValidation.js` to reject a burn step pinned to
 * another family's subscription before it reaches disk; a provider whose id
 * names no family is accepted there and resolved at dispatch time instead.
 */
export function familyForProviderId(id) {
  const value = typeof id === 'string' ? id.trim() : '';
  if (!value) return null;
  return PROVIDER_FAMILIES.find((f) => f.idPattern.test(value))?.id ?? null;
}