Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

9.8 KB
Raw Blame
import { previousModelGenerations } from './modelGeneration.js';
import { sourceProviderForPreset } from './providerPresetIdentity.js';
import { providerEndpointBindings } from './providerEndpointTemplate.js';
import { declaredModelInterface } from './providerInterfaceRoutes.js';
import registryData from "../catalog/model-registry.json" with { type: "json" };
import generated from "../catalog/provider-models.json" with { type: "json" };
import { pickModelMetadata, type ModelMetadata } from "./modelMetadataLayers.js";
import type { ModelCost, PiModelApi, ProviderWireProtocol } from "./types.js";

export interface ProviderModelRecord extends ModelMetadata {
  /** Internal fallback provenance, not an upstream capability declaration. */
  inheritedFrom?: string;
  id: string;
  name: string;
  upstream: string;
  contextWindow?: number;
  maxOutput?: number;
  modalities?: { input: string[]; output: string[] };
  supportsImageInput?: boolean;
  /** Connection-scoped declaration; absent does not authorize a Fast request. */
  supportsFastMode?: boolean;
  reasoning?: boolean;
  efforts?: NonNullable<ModelMetadata["efforts"]>;
  defaultEffort?: ModelMetadata["defaultEffort"];
  cost?: ModelCost;
  execution: {
    pi: {
      api: string;
      headers?: Record<string, string>;
      thinkingLevelMap?: Record<string, string | null>;
      compat?: Record<string, unknown>;
      samplingParams?: Record<string, unknown>;
    };
  };
}

const sourceCatalog = generated as unknown as {
  schemaVersion: number;
  generatedAt: string;
  providers: Record<string, ProviderModelRecord[]>;
};

export const PROVIDER_MODEL_CATALOG = { ...sourceCatalog, providers: { ...sourceCatalog.providers } };

// Official per-model contracts override the pinned Pi serializer choice, without
// borrowing another supplier's metadata or adding unlisted models to an account.
for (const provider of new Set(registryData.interfaceProfiles.map(profile => profile.providerId))) {
  const rows = PROVIDER_MODEL_CATALOG.providers[provider];
  if (!rows) continue;
  PROVIDER_MODEL_CATALOG.providers[provider] = rows.map(row => {
    const declaration = declaredModelInterface(provider, row.id);
    if (!declaration || new URL(declaration.baseUrl).origin !== new URL(row.upstream).origin) return row;
    return { ...row, upstream: declaration.baseUrl, execution: { ...row.execution, pi: { ...row.execution.pi, api: declaration.api } } };
  });
}

const byEndpointAndId = new Map<string, ProviderModelRecord[]>();
const apisByEndpoint = new Map<string, Set<string>>();
const normalize = (url: string) => {
  const trimmed = url.trim();
  let end = trimmed.length;
  while (end > 0 && trimmed.charCodeAt(end - 1) === 47) end--;
  return trimmed.slice(0, end);
};
for (const rows of Object.values(PROVIDER_MODEL_CATALOG.providers)) {
  for (const row of rows) {
    const endpoint = normalize(row.upstream);
    const apis = apisByEndpoint.get(endpoint) ?? new Set<string>();
    apis.add(row.execution.pi.api);
    apisByEndpoint.set(endpoint, apis);
    const key = `${endpoint}\n${row.id}`;
    const existing = byEndpointAndId.get(key) ?? [];
    // Upstream may publish the exact same record in several subscription catalogs.
    // Duplicate evidence is not a conflict; differing records must stay ambiguous.
    if (!existing.some(candidate => JSON.stringify(candidate) === JSON.stringify(row))) {
      byEndpointAndId.set(key, [...existing, row]);
    }
  }
}

const generationRows = Object.values(PROVIDER_MODEL_CATALOG.providers).flat();
const manufacturerGenerationRows = ['openai', 'anthropic', 'google', 'xai', 'deepseek', 'qwen']
  .flatMap(provider => PROVIDER_MODEL_CATALOG.providers[provider] ?? []);

/** Exact route identity, shared by every harness. */
export function providerModelRecord(
  modelId: string,
  upstream: string,
  protocol?: ProviderWireProtocol | PiModelApi,
  allowMixedProtocol = false,
): ProviderModelRecord | undefined {
  const api = protocol === "openai-chat" ? "openai-completions" : protocol;
  // A mixed catalog declares per-model APIs. A single-protocol endpoint does not
  // override a caller's explicitly selected transport.
  const mixed = allowMixedProtocol && (apisByEndpoint.get(normalize(upstream))?.size ?? 0) > 1;
  const matches = byEndpointAndId
    .get(`${normalize(upstream)}\n${modelId}`)
    ?.filter((row) => mixed || api === undefined || row.execution.pi.api === api);
  return matches?.length === 1 ? matches[0] : undefined;
}

/** A new generation reuses parameters, but keeps its own connection and identity. */
export function providerModelGenerationRecord(modelId: string, upstream: string, protocol?: ProviderWireProtocol | PiModelApi, presetId?: string): ProviderModelRecord | undefined {
  const api = protocol === 'openai-chat' ? 'openai-completions' : protocol;
  // Transport must already be selected; inheritance never changes a connection's API.
  if (!api) return undefined;
  const endpointRows = generationRows
    .filter(row => normalize(row.upstream) === normalize(upstream) && row.execution.pi.api === api);
  // Manufacturer adapters are reusable across compatible relays. Never borrow another
  // relay's headers, endpoint, prices or adapter identity.
  const manufacturerRows = manufacturerGenerationRows
    .filter(row => row.execution.pi.api === api);
  const presetRows = presetId ? (PROVIDER_MODEL_CATALOG.providers[sourceProviderForPreset(presetId)] ?? [])
    .filter(row => row.execution.pi.api === api) : [];
  const candidates = [...manufacturerRows, ...presetRows, ...endpointRows];
  // A relay endpoint mismatch does not make a known model a new generation.
  // Reuse its own same-protocol adapter before considering older models.
  const previous = candidates.filter(row => row.id === modelId).at(-1)
    ?? previousModelGenerations(modelId, candidates, row => row.id).at(-1);
  if (!previous) return undefined;
  const { cost: _cost, execution, ...metadata } = previous;
  const { headers: _headers, ...parameters } = execution.pi;
  return { ...metadata, id: modelId, name: modelId, upstream, inheritedFrom: previous.id,
    execution: { pi: structuredClone(parameters) } };
}

/** Account-specific endpoints may still explicitly reference a maintained connection template. */
export function providerPresetModelRecord(presetId: string | undefined, modelId: string, api?: PiModelApi): ProviderModelRecord | undefined {
  if (!presetId) return undefined;
  const provider = sourceProviderForPreset(presetId);
  const matches = PROVIDER_MODEL_CATALOG.providers[provider]?.filter(row => row.id === modelId && (!api || row.execution.pi.api === api));
  return matches?.length === 1 ? matches[0] : undefined;
}

/** Preserve the upstream adapter identity, independently of a user's connection UUID. */
export function providerModelAdapterId(row: ProviderModelRecord, presetId?: string): string | undefined {
  const presetProvider = presetId ? sourceProviderForPreset(presetId) : undefined;
  const matches = Object.entries(PROVIDER_MODEL_CATALOG.providers).filter(([provider, rows]) => {
    const connectionRows = rows.filter(candidate => candidate.execution.pi.api === row.execution.pi.api &&
      (normalize(candidate.upstream) === normalize(row.upstream) ||
        (candidate.upstream.includes('{') && providerEndpointBindings(candidate.upstream, row.upstream) !== null) ||
        (row.inheritedFrom !== undefined && provider === presetProvider)));
    return connectionRows.some(candidate => candidate.id === row.id) ||
      // Only the matched connection/preset lends transport identity. Manufacturer
      // capability fallback on an unrelated relay must not lend its adapter.
      (row.inheritedFrom !== undefined && previousModelGenerations(row.id, connectionRows, candidate => candidate.id).length > 0);
  });
  // Duplicated subscription catalogs can share endpoints; never guess between distinct identities.
  return matches.length === 1 ? matches[0][0] : undefined;
}

export function providerModelMetadata(row: ProviderModelRecord): ModelMetadata {
  return {
    ...pickModelMetadata(row),
    name: row.name,
    contextWindow: row.contextWindow,
    ...(row.maxOutput ? { maxOutputTokens: row.maxOutput } : {}),
    modalities: row.modalities,
    supportsImageInput: row.supportsImageInput,
    ...(row.supportsFastMode !== undefined ? { supportsFastMode: row.supportsFastMode } : {}),
    efforts: row.efforts,
    defaultEffort: row.defaultEffort,
    ...(row.execution.pi.thinkingLevelMap?.off === null
      ? { reasoningRequired: true }
      : {}),
  };
}

/** Adapter only: reconstruct Pi's wire names from the same standard catalog used by UI/Codex. */
export function providerCatalogForPi() {
  return {
    generatedAt: PROVIDER_MODEL_CATALOG.generatedAt,
    providers: Object.fromEntries(
      Object.entries(PROVIDER_MODEL_CATALOG.providers).map(
        ([provider, rows]) => [
          provider,
          rows.map((row) => ({
            id: row.id,
            name: row.name,
            provider,
            baseUrl: row.upstream,
            contextWindow: row.contextWindow,
            maxTokens: row.maxOutput,
            input: row.modalities?.input,
            reasoning: row.reasoning,
            ...(row.cost ? { cost: row.cost } : {}),
            ...row.execution.pi,
          })),
        ],
      ),
    ),
  };
}

/** Offline membership for an exact, supported endpoint; never infer a proxy's inventory. */
export function providerModelsForRoute(
  upstream: string,
  protocol?: ProviderWireProtocol | PiModelApi,
): ProviderModelRecord[] {
  const api = protocol === "openai-chat" ? "openai-completions" : protocol;
  const models = Object.values(PROVIDER_MODEL_CATALOG.providers)
    .flat()
    .filter(
      (row) =>
        normalize(row.upstream) === normalize(upstream) &&
        (api === undefined || row.execution.pi.api === api),
    );
  return models.filter(
    (row) => providerModelRecord(row.id, upstream, protocol) === row,
  );
}