All files / src/objects modelOrigin.ts

100% Statements 29/29
75.86% Branches 22/29
100% Functions 6/6
100% Lines 26/26

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 127 128 129 130 131 132 133 134 135 136 137                                      110x         110x                                 110x   110x               546x 546x 77x 546x 546x                 356x 356x 4x   3x   1x         132x         211x                                     110x                           4x 4x 2x     110x                   2x 2x 2x 2x 2x        
import type { InferenceBackend, Model } from "@/objects/Model";
import { APP_NAME } from "@/routes/const";
 
/**
 * Who made a model, and who runs it for this deployment.
 *
 * Two different facts, and every page that names a model's "provider" means
 * one of them. The **maker** (OpenAI, Qwen, Amazon) is what a person choosing
 * a model compares; it is the catalogue's provider, or — for a model
 * registered under the platform that serves it — the maker its id names
 * (`qwen.qwen3-…`, `amazon.titan-…`). **Who runs it** is the vendor's own
 * service on a `direct` deployment, and on any other the platform: CHAIN-R
 * Cloud. The cloud CHAIN-R runs on is a sub-processor
 * (`docs/business/sub-processors.md`), not something a person choosing a
 * model needs, and no page names it. svc-ai says the same
 * (`inference_routes.SERVING_PLATFORM`).
 */
 
/** What runs the models on any backend but `direct`. */
export const SERVING_PLATFORM = `${APP_NAME} Cloud`;
 
/** The makers a catalogue provider key or a Bedrock id prefix names. Held to
 *  svc-ai's `ModelEnum.Provider` and the routed model ids by
 *  `modelOrigin.test.ts`. */
export const MODEL_MAKERS: Record<string, string> = {
  amazon: "Amazon",
  anthropic: "Anthropic",
  cohere: "Cohere",
  deepseek: "DeepSeek",
  google: "Google",
  groq: "Groq",
  local: APP_NAME,
  mistral: "Mistral",
  moonshotai: "Moonshot AI",
  nvidia: "NVIDIA",
  openai: "OpenAI",
  qwen: "Qwen",
};
 
/** Catalogue providers that are a serving platform rather than a maker: a
 *  model registered under one names its maker at the start of its id. */
const SERVING_PROVIDERS: ReadonlySet<string> = new Set(["bedrock"]);
 
const PROFILE_PREFIX = /^(eu|global|us|apac|jp|au|in|ca)\./;
 
/** The maker's key: the provider, or the vendor a served model's id names
 *  (`eu.amazon.nova-…` → `amazon`); `""` when an id names none. */
export function makerKey(
  provider: string | null | undefined,
  modelName: string | null | undefined,
): string {
  const key = (provider ?? "").toLowerCase();
  if (!SERVING_PROVIDERS.has(key)) return key;
  const bare = (modelName ?? "").toLowerCase().replace(PROFILE_PREFIX, "");
  const dot = bare.indexOf(".");
  return dot > 0 ? bare.slice(0, dot) : "";
}
 
/** The maker's name, from the raw keys a trace or a call records. */
export function makerLabel(
  provider: string | null | undefined,
  modelName?: string | null,
  displayName?: string | null,
): string {
  const key = makerKey(provider, modelName);
  if (MODEL_MAKERS[key]) return MODEL_MAKERS[key];
  if (SERVING_PROVIDERS.has((provider ?? "").toLowerCase())) {
    // A served model whose id names no known maker: say who runs it.
    return key ? key.charAt(0).toUpperCase() + key.slice(1) : SERVING_PLATFORM;
  }
  return displayName || provider || "Unknown maker";
}
 
/** A catalogue model's maker key (`makerKey`), for its mark. */
export function modelMakerKey(m: Model): string {
  return makerKey(m.provider?.name, m.model_name);
}
 
/** Who made a catalogue model. */
export function modelMaker(m: Model): string {
  return makerLabel(m.provider?.name, m.model_name, m.provider?.display_name);
}
 
/** A service that runs other vendors' models: every svc-ai inference
 *  backend but `direct`, where each vendor runs its own. Held to svc-ai's
 *  `inference_routes.BACKENDS` by `platformFacts.test.ts`. */
export interface InferenceService {
  name: string;
  /** Whether the vendors whose models it runs can see prompts and answers. */
  vendorsSeePrompts: boolean;
  /** Makers (keys) whose models the service's own provider runs: the
   *  "vendors never see prompts" guarantee is about other vendors, so the
   *  Data tab makes no such claim for these. */
  ownMakers: readonly string[];
}
 
export const INFERENCE_SERVICES: Record<
  Exclude<InferenceBackend, "direct">,
  InferenceService
> = {
  bedrock: {
    name: SERVING_PLATFORM,
    // Each vendor's model runs in accounts the vendor cannot access, so the
    // vendor never sees prompts or answers (checked 8 October 2026; the
    // provider is named in the sub-processor list).
    vendorsSeePrompts: false,
    // Its own models (Titan embeddings) are run by the provider itself.
    ownMakers: ["amazon"],
  },
};
 
/** Who runs a catalogue model for this deployment. */
export function servedBy(m: Model): string {
  const backend = m.inference_backend;
  if (backend && backend !== "direct") return INFERENCE_SERVICES[backend].name;
  return modelMaker(m);
}
 
const REGION_PLACES: Record<string, string> = {
  uk: "the UK",
  eu: "the EU",
  us: "the US",
  global: "any region",
};
 
/** One line saying who made a model and where it runs, e.g. "Made by Qwen ·
 *  runs on CHAIN-R Cloud in the UK". */
export function modelOrigin(m: Model): string {
  const maker = modelMaker(m);
  const served = servedBy(m);
  const place = m.data_region ? REGION_PLACES[m.data_region] : undefined;
  const where = place ? ` in ${place}` : "";
  return served === maker
    ? `Made and run by ${maker}${where}`
    : `Made by ${maker} · runs on ${served}${where}`;
}