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

9.7 KB
Raw Blame
import { useCallback, useEffect, useState } from 'react';
import { createProviderPreset, getProviderCatalog } from '../services/api';
import socket from '../services/socket.js';
import { harnessLabel } from '../utils/providerHarnesses.js';
import { parseProviderRef } from '../utils/providerRef.js';

/**
 * Module-level fetch cache, same shape as `useToolUseModelIds`: `null` = never
 * fetched (or the last attempt failed and was cleared for retry), a Promise
 * once a fetch is in flight or has resolved. The compose popover, the
 * preset-first `ProviderModelSelector`, and every bespoke picker migrating
 * onto this hook (#7566) can all mount inside the same tree, so N mounts must
 * share ONE `/providers/catalog` fetch rather than firing N of them.
 */
let inFlight = null;
let generation = 0;
let listenersInstalled = false;

// Mounted hooks, notified when the shared cache is dropped so they re-read.
// The AI Providers page (#7567) toggles a harness, edits a service or saves a
// bootstrap app and every open picker's compose flow must reflect it without a
// reload — one invalidation reaches all of them.
const subscribers = new Set();

const EMPTY_CATALOG = Object.freeze({
  harnesses: [], services: [], bootstraps: [], compatibility: {}, effortLevels: {}, effortLevelsByModel: {}, presets: [],
});

function fetchCatalog() {
  const readGeneration = generation;
  inFlight ||= getProviderCatalog({ silent: true })
    .catch((err) => {
      console.warn(`⚠️ Provider catalog fetch failed: ${err?.message || err}`);
      if (readGeneration === generation) inFlight = null;
      return null;
    });
  return inFlight;
}

/** Test seam — drop the shared cache so each case starts from "never fetched". */
export function __resetProviderCatalogCache() {
  generation += 1;
  inFlight = null;
}

/**
 * Drop the shared catalog and make every mounted `useProviderCatalog` re-read
 * it. Call after a write that changes what compose offers: a harness
 * enablement toggle, a service create/edit/delete/refresh, a bootstrap save.
 */
export function invalidateProviderCatalog() {
  generation += 1;
  inFlight = null;
  for (const notify of subscribers) notify();
}

// Like the providers API cache, keep one pair of listeners for the lifetime of
// the shared cache, including between mounts. A reconnect can recover a write
// missed while disconnected; neither event fetches until a consumer is enabled.
function listenForProviderChanges() {
  if (listenersInstalled) return;
  socket.on('providers:changed', invalidateProviderCatalog);
  socket.on('connect', invalidateProviderCatalog);
  listenersInstalled = true;
}

/**
 * The composition catalog behind the preset-first `ProviderModelSelector` and
 * `ProviderComposePopover` (#7566): one fetch of `GET /api/providers/catalog`
 * per mount tree, shared like `useToolUseModelIds`, plus the lookups every
 * picker/popover needs over it.
 *
 * - `presets` / `harnesses` / `services` / `bootstraps` — the raw catalog rows.
 * - `compatiblePairs(harnessId)` — the enabled service instances a harness can
 *   be pointed at, narrowed by `compatibility`.
 * - `methodsFor(harnessId)` — the execution modes (`cli`/`tui`/`api`) that
 *   harness supports.
 * - `modelsFor(serviceSlug)` — the service's own model catalog.
 * - `effortLevelsFor(harnessId, model)` — the per-model ladder when one
 *   narrows the harness's own (Codex, Antigravity), else the harness ladder.
 * - `resolveRef(id)` — a preset record for a preset id, or a SYNTHESIZED
 *   display record for a composite id (`{ id, name, type, harnessId, method,
 *   serviceSlug, bootstrapId, models, effortLevels, effortLevelsByModel,
 *   enabled, unavailableReason, composite: true }` — `enabled` is false and
 *   `unavailableReason` names the switched-off harness or service so a saved
 *   pin renders with its reason; the ladders are the catalog's for that
 *   harness), named `"<harness label> · <METHOD> · <service label>"`,
 *   `" (free)"` suffixed for a free-plan service — `null` when the id resolves to
 *   neither grammar or the composite names a harness/service this catalog
 *   doesn't know. Never contacts the server: unlike
 *   `GET /providers/composites/:id`, this is a pure lookup over the fetched
 *   catalog, so it renders a saved value instantly without a round trip, and
 *   carries no ELIGIBILITY verdict — pair it with the readiness/composites
 *   endpoints when "is this composite runnable right now" matters.
 * - `savePreset(input)` — `POST /providers/presets` ("Save as preset"),
 *   returned rather than imported separately so the popover doesn't need its
 *   own service import.
 *
 * @param {boolean} [enabled] - Load the catalog only when true, e.g. gate on
 *   the compose popover's `open`. Fetches once and keeps the result if
 *   `enabled` later goes false.
 * @returns {{
 *   harnesses: object[], services: object[], bootstraps: object[], presets: object[],
 *   compatibility: Record<string, string[]>, effortLevels: Record<string, string[]>,
 *   effortLevelsByModel: Record<string, Record<string, string[]>>,
 *   loading: boolean,
 *   compatiblePairs: (harnessId: string) => object[],
 *   methodsFor: (harnessId: string) => string[],
 *   modelsFor: (serviceSlug: string) => Array<string|object>,
 *   effortLevelsFor: (harnessId: string, model?: string|null) => string[],
 *   resolveRef: (id: string) => object|null,
 *   savePreset: (input: object) => Promise<object>,
 * }}
 */
export default function useProviderCatalog(enabled = true) {
  const [state, setState] = useState({ catalog: null, loaded: false, generation });

  // A revision change must retrigger an already-loading picker too. Merely
  // setting loaded=false leaves its obsolete request as the only active read.
  useEffect(() => {
    listenForProviderChanges();
    const notify = () => setState((current) => ({ ...current, loaded: false, generation }));
    subscribers.add(notify);
    return () => { subscribers.delete(notify); };
  }, []);

  useEffect(() => {
    if (!enabled || state.loaded) return undefined;
    let canceled = false;
    const readGeneration = generation;
    fetchCatalog().then((catalog) => {
      if (!canceled && readGeneration === generation) {
        setState((current) => ({ ...current, catalog: catalog ?? current.catalog, loaded: true }));
      }
    });
    // A cancel (popover closed mid-flight) leaves `loaded` false, so reopening
    // re-reads rather than rendering a result that never arrived — free when
    // the first fetch already succeeded, since the shared cache still has it.
    return () => { canceled = true; };
  }, [enabled, state.loaded, state.generation]);

  const catalog = state.catalog || EMPTY_CATALOG;

  const compatiblePairs = useCallback((harnessId) => {
    const slugs = new Set(catalog.compatibility?.[harnessId] || []);
    return (catalog.services || []).filter((service) => slugs.has(service.slug) && service.enabled !== false);
  }, [catalog]);

  const methodsFor = useCallback(
    (harnessId) => [...((catalog.harnesses || []).find((harness) => harness.id === harnessId)?.modes || [])],
    [catalog],
  );

  const modelsFor = useCallback(
    (serviceSlug) => (catalog.services || []).find((service) => service.slug === serviceSlug)?.catalog?.models || [],
    [catalog],
  );

  const effortLevelsFor = useCallback((harnessId, model = null) => {
    const perModel = model ? catalog.effortLevelsByModel?.[harnessId]?.[model] : null;
    return perModel || catalog.effortLevels?.[harnessId] || [];
  }, [catalog]);

  const resolveRef = useCallback((id) => {
    if (!id) return null;
    const preset = (catalog.presets || []).find((p) => p.id === id);
    if (preset) return preset;
    const ref = parseProviderRef(id);
    if (ref?.kind !== 'composite') return null;
    const harness = (catalog.harnesses || []).find((h) => h.id === ref.harnessId);
    if (!harness) return null;
    const service = (catalog.services || []).find((s) => s.slug === ref.serviceSlug);
    if (!service) return null;
    const label = `${harnessLabel(ref.harnessId)} · ${ref.method.toUpperCase()} · ${service.label}${service.plan === 'free' ? ' (free)' : ''}`;
    // A saved composite whose harness or service has since been switched off
    // must stay VISIBLE with its reason and never be auto-replaced (#6368) —
    // so the record carries the verdict rather than vanishing from the lookup.
    const unavailableReason = harness.enabled === false
      ? `${harnessLabel(ref.harnessId)} is switched off`
      : service.enabled === false ? `${service.label} is switched off` : null;
    return {
      id,
      name: label,
      type: ref.method,
      harnessId: ref.harnessId,
      method: ref.method,
      serviceSlug: ref.serviceSlug,
      bootstrapId: ref.bootstrapSlug,
      models: service.catalog?.models || [],
      // The published ladders ride on the record, so `effortLevelsForProvider`'s
      // sanitized-record rung answers for a harness the client predicates
      // don't recognize — the server's catalog is the authority either way.
      effortLevels: catalog.effortLevels?.[ref.harnessId] || null,
      effortLevelsByModel: catalog.effortLevelsByModel?.[ref.harnessId] || null,
      enabled: !unavailableReason,
      unavailableReason,
      composite: true,
    };
  }, [catalog]);

  const savePreset = useCallback((input) => createProviderPreset(input), []);

  return {
    harnesses: catalog.harnesses,
    services: catalog.services,
    bootstraps: catalog.bootstraps,
    presets: catalog.presets,
    compatibility: catalog.compatibility,
    effortLevels: catalog.effortLevels,
    effortLevelsByModel: catalog.effortLevelsByModel,
    loading: enabled && !state.loaded,
    compatiblePairs,
    methodsFor,
    modelsFor,
    effortLevelsFor,
    resolveRef,
    savePreset,
  };
}