All files / src/apps/user/pages/settings platformFacts.ts

100% Statements 27/27
83.87% Branches 26/31
100% Functions 5/5
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                                                3x                   3x                                               3x                               5x 4x 4x     5x           17x       17x 19x 17x 17x   17x 19x 19x 19x       19x 19x 17x   17x   10x 10x   3x         1x    
import type { Model } from "@/objects/Model";
import {
  INFERENCE_SERVICES,
  type InferenceService,
  makerKey,
  modelMaker,
} from "@/objects/modelOrigin";
 
/**
 * What the Data and Privacy tabs state about the platform.
 *
 * These are properties of the deployment, not choices a user makes, so the
 * tabs show them rather than offering toggles: the region selects, chunking
 * and cookie switches that stood here before saved to localStorage and
 * nothing read them. Each fact is pinned to the file that makes it true by
 * `platformFacts.test.ts`, so changing that file without this one fails a
 * test rather than leaving the page saying something the platform does not do.
 *
 * Kept to what a user needs to know about their data: where it is stored and
 * what leaves CHAIN-R, said by category. The services themselves — who hosts
 * the platform, who answers a web search — are not named: how the platform
 * is built and secured is no settings page's business, and the full list
 * goes to customers with the data processing agreement
 * (`docs/business/sub-processors.md`), and to anyone who asks.
 *
 * Model providers are not listed here: the Data tab reads them, with their
 * declared data region and who runs them, from the live model catalogue —
 * the embedding model knowledge is indexed with among them.
 */
 
/** Where the hosted platform stores data — the region in
 *  `infra/terraform/envs/dev`, as a place rather than a provider's code. */
export const STORAGE_LOCATION = "London, United Kingdom";
 
export interface OutsideService {
  /** What the platform uses it for. */
  purpose: string;
  /** What content it receives, in the user's terms — and who, by kind. */
  receives: string;
}
 
/** What leaves CHAIN-R other than to the models, by kind of service. */
export const OUTSIDE_SERVICES: readonly OutsideService[] = [
  {
    // svc-ai's web search: a search API first, then a metasearch fallback
    // (`DEFAULT_DDGS_BACKEND`) that sends the query to public engines.
    purpose: "Web search",
    receives:
      "The search queries an agent writes when it searches the web go to a web search service, or to public search engines when it is unavailable.",
  },
  {
    // svc-ai's `geocode` tool, and the tile server fe-core's maps load from
    // (`components/map/tiles.ts`).
    purpose: "Maps",
    receives:
      "Place names an agent looks up to draw a map go to a public geocoding service, and your browser loads the map itself from a map tile service.",
  },
  {
    // svc-core's feed widgets: weather, market data and RSS.
    purpose: "Dashboard feeds",
    receives:
      "The place, ticker or feed address a feed tile asks for, sent to that feed's publisher.",
  },
];
 
/** Subject line for asking for the full list of services. */
export const SERVICE_LIST_REQUEST = "Request: the services CHAIN-R uses";
 
export interface ProviderInUse {
  /** Who receives a prompt: the vendor, or the service running its models. */
  name: string;
  /** Each distinct region its models declare, `null` for one that declares
   *  none — shown as such rather than dropped, since an undeclared region is
   *  itself something a reader deciding on residency needs to know. */
  regions: (string | null)[];
  /** The vendors whose models it runs, when it is not their vendor. */
  runs: string[];
  /** What that means for those vendors, said once per service. */
  note?: string;
}
 
function noteFor(service: InferenceService, vendors: string[]): string | undefined {
  if (vendors.length === 0) return undefined;
  const list = vendors.join(" and ");
  const sees = service.vendorsSeePrompts
    ? `${vendors.length === 1 ? "receives" : "receive"} your prompts too`
    : `${vendors.length === 1 ? "does" : "do"} not receive your prompts or the answers`;
  return `Runs models from ${list}, which ${sees}.`;
}
 
/** Who receives the prompts the catalogue's models can answer: each model's
 *  vendor, or the service running it on this deployment. */
export function providersInUse(models: readonly Model[]): ProviderInUse[] {
  const byName = new Map<
    string,
    { regions: Set<string | null>; runs: Set<string>; service?: InferenceService }
  >();
  for (const m of models) {
    if (!m.is_available) continue;
    const maker = modelMaker(m);
    const backend = m.inference_backend;
    const service =
      backend && backend !== "direct" ? INFERENCE_SERVICES[backend] : undefined;
    const name = service?.name ?? maker;
    const entry = byName.get(name) ?? { regions: new Set(), runs: new Set(), service };
    entry.regions.add(m.data_region ?? null);
    // The note says the makers never see prompts; for the provider's own
    // models that is not a claim to make, so they are not listed in it — nor
    // a model whose id names no maker at all.
    const key = makerKey(m.provider?.name, m.model_name);
    if (service && key && !service.ownMakers.includes(key)) entry.runs.add(maker);
    byName.set(name, entry);
  }
  return [...byName.entries()]
    .map(([name, { regions, runs, service }]) => {
      const vendors = [...runs].sort();
      return {
        name,
        regions: [...regions].sort((a, b) => (a ?? "~").localeCompare(b ?? "~")),
        runs: vendors,
        note: service ? noteFor(service, vendors) : undefined,
      };
    })
    .sort((a, b) => a.name.localeCompare(b.name));
}