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。

3.9 KB
Raw Blame
import { compatibilityProtocol } from '@cindy/model-compat/protocol';
import { PI_MODEL_APIS } from './types.js';
import type {
  AgentKind,
  CatalogModel,
  PiModelApi,
  Provider,
  ProviderWireProtocol,
} from './types.js';

function apiFromWireProtocol(protocol: ProviderWireProtocol | undefined): PiModelApi | null {
  return protocol === 'openai-chat' ? 'openai-completions' : (protocol ?? null);
}

/** Protocol comparison for one provider's logical row. Never borrow another provider's model. */
export function modelProtocolComparison(
  provider: Pick<Provider, 'id' | 'routing'> & Partial<Pick<Provider, 'source'>>,
  models: Partial<Record<AgentKind, CatalogModel>>,
) {
  // Canonical protocol is model metadata, never reverse-engineered from a harness configuration.
  const declared = Object.values(models)
    .map((m) => m.nativeApi)
    .filter((api): api is PiModelApi | null => api === null || PI_MODEL_APIS.some(value => value === api));
  const reference: PiModelApi | null = declared.length
    ? new Set(declared).size === 1
      ? declared[0]!
      : null
    : provider.id === 'anthropic' && models['claude-code']
      ? 'anthropic-messages'
      : provider.id === 'openai' && models.codex
        ? 'openai-responses'
        : null;
  const declaredApis = Object.values(models).map(m => m.nativeApis).filter(v => v !== undefined);
  const nativeApis = declaredApis.length ? declaredApis.every(v => JSON.stringify(v) === JSON.stringify(declaredApis[0]))
    ? declaredApis[0] : null : reference ? [reference] : null;
  const forAgent = (agent: AgentKind) => {
    const model = models[agent];
    if (!model) return null;
    const routing = provider.routing?.[agent];
    const outbound = model.api ?? (
      agent === 'pi'
        ? (model.piApi ?? apiFromWireProtocol(model.route?.wireProtocol ?? routing?.wireProtocol))
        : apiFromWireProtocol(
            model.route?.wireProtocol ??
              routing?.wireProtocol ??
              // A missing descriptor (e.g. a redacted remote view) is not evidence of a route.
              (routing?.authStrategy
                ? agent === 'codex'
                  ? 'openai-responses'
                  : 'anthropic-messages'
                : undefined),
          ));
    const harness: PiModelApi | null =
      agent === 'pi' ? outbound : agent === 'codex' ? 'openai-responses' : 'anthropic-messages';
    const outboundProtocol = compatibilityProtocol(outbound) ?? outbound;
    const harnessProtocol = compatibilityProtocol(harness) ?? harness;
    const localConversion = Boolean(agent !== 'pi' && outboundProtocol && harnessProtocol && outboundProtocol !== harnessProtocol);
    // Pi speaks the selected upstream API directly. Fixed-protocol harnesses
    // also need compatibility when the supplier converts the model's native API.
    // The location of that conversion never changes the default-on decision.
    const compatibility =
      localConversion || Boolean(agent !== 'pi' && nativeApis?.length && outboundProtocol && !nativeApis.some(api => (compatibilityProtocol(api) ?? api) === outboundProtocol));
    return {
      harness,
      outbound,
      localConversion,
      mode: compatibility
        ? ('compatibility' as const)
        : outbound && (nativeApis?.length || agent === 'pi')
          ? ('matching' as const)
          : ('unknown' as const),
    };
  };
  return { reference, nativeApis, forAgent };
}

/** Default-on engines use a configured protocol without harness or provider compatibility. */
export function nativeModelAgents(
  provider: Pick<Provider, 'id' | 'routing'> & Partial<Pick<Provider, 'source'>>,
  models: Partial<Record<AgentKind, CatalogModel>>,
): AgentKind[] {
  const comparison = modelProtocolComparison(provider, models);
  return (Object.keys(models) as AgentKind[]).filter(agent => {
    const protocol = comparison.forAgent(agent);
    return !!protocol?.outbound && protocol.mode === 'matching';
  });
}