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。

20.8 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import { buildUserProvider, type ModelRegistry, type ProviderRuntimeModelConfig } from '@cindy/model-providers';
import { customProviderModelConfigFromCatalogModel } from '../customProviders';

import {
  firstProviderChatModel,
  areProviderRequestUrlsAllowed,
  canSendHydratedApiKey,
  connectionTestCanUseSaved,
  modelFetchCanReuseSavedCredentials,
  providerConnectionTestRequestSignature,
  providerModelFetchRequestSignature,
  resolveProviderConnectionProbeRoute,
  restoreHydratedApiKey,
  type SavedProviderProbeBaseline,
} from '../providerModelFetch';

const fields = {
  baseUrl: ' https://api.example/v1 ',
  requestPath: ' /responses ',
  modelsUrl: ' /models ',
  apiKey: ' secret-a ',
  headers: [
    { name: 'Authorization', value: 'Bearer stale' },
    { name: 'X-Tenant', value: 'acme' },
  ],
};

it('excludes exact public speech identities from chat probes without guessing unknown names', () => {
  const speech = { id: 'gpt-4o-mini-tts' };
  const unknown = { id: 'private-tts-service' };
  expect(firstProviderChatModel([speech])).toBeUndefined();
  expect(firstProviderChatModel([speech, unknown])).toBe(unknown);
  expect(firstProviderChatModel([{ ...unknown, mode: 'audio_speech' }])).toBeUndefined();
  expect(firstProviderChatModel([{ ...speech, mode: 'chat' }])?.id).toBe(speech.id);
});

describe('active catalog modes in connection probes', () => {
  const registry: ModelRegistry = { schemaVersion: 5, updatedAt: '2026-10-12T06:00:00.000Z',
    baseModels: [{ id: 'new-server-voice', aliases: [], defaults: { mode: 'audio_speech' } }], models: [],
  };
  function project(models: ProviderRuntimeModelConfig[]) {
    return buildUserProvider({ id: 'speech', name: 'Speech', runtimes: { codex: {
      baseUrl: fields.baseUrl.trim(), models,
    } } }, { modelRegistry: registry }).models.codex!;
  }

  it('uses the effective mode after restoring an ID-only saved config without materializing it', () => {
    const catalogModels = project([{ id: 'new-server-voice', name: 'Voice' }]);
    const models = catalogModels.map(model => customProviderModelConfigFromCatalogModel(model, 'codex'));
    expect(catalogModels[0].mode).toBe('audio_speech');
    expect(models[0].mode).toBeUndefined();
    expect(firstProviderChatModel(models, catalogModels)).toBeUndefined();
    expect(firstProviderChatModel([{ ...models[0], mode: 'chat' }], catalogModels)?.id).toBe(models[0].id);
    expect(firstProviderChatModel([{ ...models[0], discoveredMetadata: { mode: 'chat' } }], catalogModels)?.id).toBe(models[0].id);
    expect(models).toEqual([{ id: 'new-server-voice', name: 'Voice' }]);
  });

  it('does not restore a saved explicit type after the user resets it to automatic', () => {
    const catalogModels = project([{ id: 'private-model', name: 'Private', mode: 'audio_speech' }]);
    const automatic = { id: 'private-model', name: 'Private' };
    expect(firstProviderChatModel([automatic], catalogModels)).toBe(automatic);
  });

  it('uses the same active classification for route, saved-probe eligibility and request signatures', () => {
    const models: ProviderRuntimeModelConfig[] = [
      { id: 'new-server-voice', name: 'Voice', route: { baseUrl: 'https://speech.example.test/v1', wireProtocol: 'openai-chat' } },
      { id: 'chat-model', name: 'Chat', route: { baseUrl: 'https://chat.example.test/v1', wireProtocol: 'openai-chat' } },
    ];
    const catalogModels = project(models);
    const probe = { ...fields, wireProtocol: 'openai-responses' as const, models, catalogModels };
    expect(firstProviderChatModel(models, catalogModels)?.id).toBe('chat-model');
    expect(resolveProviderConnectionProbeRoute('codex', probe)).toMatchObject({
      baseUrl: 'https://chat.example.test/v1', wireProtocol: 'openai-chat',
    });
    expect(connectionTestCanUseSaved(probe, { ...fields, wireProtocol: 'openai-responses',
      authMode: 'apiKey', modelRoute: models[1].route }, 'apiKey')).toBe(true);
    expect(JSON.parse(providerConnectionTestRequestSignature(probe, 'apiKey')).modelId).toBe('chat-model');
    expect(providerConnectionTestRequestSignature(probe, 'apiKey')).not.toBe(
      providerConnectionTestRequestSignature({ ...probe, catalogModels: [] }, 'apiKey'),
    );
  });
});

describe('providerModelFetchRequestSignature', () => {
  it('invalidates an in-flight result when the auth mode changes', () => {
    expect(providerModelFetchRequestSignature(fields, 'apiKey')).not.toBe(
      providerModelFetchRequestSignature(fields, 'oauth'),
    );
    expect(providerModelFetchRequestSignature(fields, 'oauth')).not.toBe(
      providerModelFetchRequestSignature(fields, 'none'),
    );
  });

  it('tracks only the API key that is effective for the selected auth mode', () => {
    const changed = { ...fields, apiKey: 'secret-b' };
    expect(providerModelFetchRequestSignature(fields, 'apiKey')).not.toBe(
      providerModelFetchRequestSignature(changed, 'apiKey'),
    );
    expect(providerModelFetchRequestSignature(fields, 'oauth')).toBe(
      providerModelFetchRequestSignature(changed, 'oauth'),
    );
  });

  it('uses credential-stripped headers when credentials are not supplied from the form', () => {
    const changedCredential = {
      ...fields,
      headers: [
        { name: 'Authorization', value: 'Bearer changed' },
        { name: 'X-Tenant', value: 'acme' },
      ],
    };
    const changedEffectiveHeader = {
      ...fields,
      headers: [
        { name: 'Authorization', value: 'Bearer stale' },
        { name: 'X-Tenant', value: 'other' },
      ],
    };
    for (const authMode of ['oauth', 'none'] as const) {
      expect(providerModelFetchRequestSignature(fields, authMode)).toBe(
        providerModelFetchRequestSignature(changedCredential, authMode),
      );
      expect(providerModelFetchRequestSignature(fields, authMode)).not.toBe(
        providerModelFetchRequestSignature(changedEffectiveHeader, authMode),
      );
    }
  });
});

describe('providerConnectionTestRequestSignature', () => {
  const connectionFields = {
    ...fields,
    wireProtocol: 'openai-responses' as const,
    models: [{ id: ' model-a ' }, { id: 'model-b' }],
  };

  it('invalidates a probe when request path, protocol, model, or auth changes', () => {
    const original = providerConnectionTestRequestSignature(connectionFields, 'apiKey');
    expect(
      providerConnectionTestRequestSignature(
        { ...connectionFields, requestPath: '/chat/completions' },
        'apiKey',
      ),
    ).not.toBe(original);
    expect(
      providerConnectionTestRequestSignature(
        { ...connectionFields, wireProtocol: 'openai-chat' },
        'apiKey',
      ),
    ).not.toBe(original);
    expect(
      providerConnectionTestRequestSignature(
        { ...connectionFields, models: [{ id: 'model-c' }] },
        'apiKey',
      ),
    ).not.toBe(original);
    expect(providerConnectionTestRequestSignature(connectionFields, 'none')).not.toBe(original);
    expect(
      providerConnectionTestRequestSignature(
        { ...connectionFields, models: [{ id: 'model-a', piApi: 'anthropic-messages' }] },
        'apiKey',
      ),
    ).not.toBe(original);
    expect(
      providerConnectionTestRequestSignature(
        {
          ...connectionFields,
          models: [
            {
              id: 'model-a',
              route: {
                baseUrl: 'https://api.example/anthropic',
                wireProtocol: 'anthropic-messages',
              },
            },
          ],
        },
        'apiKey',
      ),
    ).not.toBe(original);
  });
});

describe('resolveProviderConnectionProbeRoute', () => {
  it.each(['claude-code', 'codex'] as const)(
    'uses the first model route for %s instead of the provider default',
    (agent) => {
      const modelWireProtocol =
        agent === 'claude-code' ? ('anthropic-messages' as const) : ('openai-responses' as const);
      expect(
        resolveProviderConnectionProbeRoute(agent, {
          baseUrl: 'https://api.example/provider',
          requestPath: '/provider-path',
          wireProtocol: agent === 'claude-code' ? 'anthropic-messages' : 'openai-chat',
          models: [
            {
              id: 'model-a',
              route: {
                baseUrl: 'https://api.example/model',
                wireProtocol: modelWireProtocol,
                requestPath: '/model-responses',
              },
            },
          ],
        }),
      ).toEqual({
        baseUrl: 'https://api.example/model',
        wireProtocol: modelWireProtocol,
        requestPath: '/model-responses',
      });
    },
  );

  it('keeps Pi on its explicit per-model protocol resolver', () => {
    expect(
      resolveProviderConnectionProbeRoute('pi', {
        baseUrl: 'https://api.example/provider',
        requestPath: '/ignored',
        wireProtocol: 'openai-chat',
        models: [{ id: 'model-a', piApi: 'openai-responses' }],
      }),
    ).toEqual({
      baseUrl: 'https://api.example/provider',
      wireProtocol: 'openai-responses',
    });
  });
});

describe('areProviderRequestUrlsAllowed', () => {
  it('keeps unsaved no-auth probes and model discovery on loopback', () => {
    expect(
      areProviderRequestUrlsAllowed(
        'none',
        'http://127.0.0.1:4000/v1',
        'http://localhost:4000/v1/models',
      ),
    ).toBe(true);
    expect(areProviderRequestUrlsAllowed('none', 'https://proxy.example/v1')).toBe(false);
    expect(
      areProviderRequestUrlsAllowed(
        'none',
        'http://localhost:4000/v1',
        'https://models.example/v1/models',
      ),
    ).toBe(false);
  });

  it('does not apply the loopback restriction to authenticated requests', () => {
    expect(areProviderRequestUrlsAllowed('apiKey', 'https://api.example/v1')).toBe(true);
    expect(areProviderRequestUrlsAllowed('oauth', 'https://api.example/v1')).toBe(true);
  });
});

// 一个纯自定义鉴权头供应商的编辑态基线:headers 为空(密文头 main-only,不回读进表单)。
const headerAuthBaseline: SavedProviderProbeBaseline = {
  baseUrl: 'https://gw.example/v1',
  requestPath: '/responses',
  modelsUrl: 'https://gw.example/v1/models',
  wireProtocol: 'openai-responses',
  authMode: 'none',
  apiKey: '',
  headers: [],
};

describe('modelFetchCanReuseSavedCredentials', () => {
  it('reuses saved credentials only when the request target endpoint is unchanged', () => {
    expect(
      modelFetchCanReuseSavedCredentials(
        { baseUrl: ' https://gw.example/v1 ', modelsUrl: ' https://gw.example/v1/models ' },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(true);
    // baseUrl 改到新主机 → 不复用(否则已存密文头会被并到用户新填的任意主机上,外泄凭证)。
    expect(
      modelFetchCanReuseSavedCredentials(
        { baseUrl: 'https://evil.example/v1', modelsUrl: 'https://gw.example/v1/models' },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(false);
    // modelsUrl 改动同样是新请求目标 → 不复用。
    expect(
      modelFetchCanReuseSavedCredentials(
        { baseUrl: 'https://gw.example/v1', modelsUrl: 'https://other.example/models' },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(false);
    // 鉴权模式变了(none → apiKey)→ 语义不同,不复用。
    expect(
      modelFetchCanReuseSavedCredentials(
        { baseUrl: 'https://gw.example/v1', modelsUrl: 'https://gw.example/v1/models' },
        headerAuthBaseline,
        'apiKey',
      ),
    ).toBe(false);
  });

  it('keeps saved credentials when only the inference request path changes', () => {
    expect(
      modelFetchCanReuseSavedCredentials(
        {
          baseUrl: headerAuthBaseline.baseUrl,
          requestPath: '/tenant/acme/models',
          modelsUrl: headerAuthBaseline.modelsUrl,
        },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(true);
  });
});

describe('canSendHydratedApiKey', () => {
  const apiKeyBaseline: SavedProviderProbeBaseline = {
    ...headerAuthBaseline,
    authMode: 'apiKey',
    apiKey: 'saved-key',
  };
  const requestTarget = {
    baseUrl: apiKeyBaseline.baseUrl,
    requestPath: apiKeyBaseline.requestPath,
    modelsUrl: apiKeyBaseline.modelsUrl,
  };

  it('keeps an untouched hydrated key on the saved base/models endpoint', () => {
    expect(canSendHydratedApiKey(requestTarget, apiKeyBaseline, 'apiKey', 0)).toBe(true);
    expect(
      canSendHydratedApiKey(
        { ...requestTarget, baseUrl: 'https://new.example/v1' },
        apiKeyBaseline,
        'apiKey',
        0,
      ),
    ).toBe(false);
    expect(
      canSendHydratedApiKey(
        { ...requestTarget, modelsUrl: 'https://new.example/models' },
        apiKeyBaseline,
        'apiKey',
        0,
      ),
    ).toBe(false);
  });

  it('allows a key after the user explicitly edits it', () => {
    expect(
      canSendHydratedApiKey(
        { ...requestTarget, baseUrl: 'https://new.example/v1' },
        apiKeyBaseline,
        'apiKey',
        1,
      ),
    ).toBe(true);
  });
  it('allows the hydrated key when only requestPath changes', () => {
    expect(
      canSendHydratedApiKey(
        {
          baseUrl: apiKeyBaseline.baseUrl,
          requestPath: '/tenant/acme/models',
          modelsUrl: apiKeyBaseline.modelsUrl,
        },
        apiKeyBaseline,
        'apiKey',
        0,
      ),
    ).toBe(true);
  });

  it('still blocks a changed model-discovery endpoint until the key is edited', () => {
    expect(
      canSendHydratedApiKey(
        { baseUrl: 'https://new.example/v1', modelsUrl: apiKeyBaseline.modelsUrl },
        apiKeyBaseline,
        'apiKey',
        0,
      ),
    ).toBe(false);
    expect(
      canSendHydratedApiKey(
        { baseUrl: 'https://new.example/v1', modelsUrl: apiKeyBaseline.modelsUrl },
        apiKeyBaseline,
        'apiKey',
        1,
      ),
    ).toBe(true);
  });
});

describe('restoreHydratedApiKey', () => {
  const baseline: SavedProviderProbeBaseline = {
    ...headerAuthBaseline,
    authMode: 'apiKey',
    apiKey: 'saved-key',
  };

  it('restores a cleared hydrated key after returning to the saved endpoint', () => {
    const reverted = {
      baseUrl: baseline.baseUrl,
      modelsUrl: baseline.modelsUrl,
      apiKey: '',
    };
    expect(restoreHydratedApiKey(reverted, baseline, 'apiKey', 0).apiKey).toBe('saved-key');
  });

  it('does not overwrite an explicit key edit or a different endpoint', () => {
    expect(
      restoreHydratedApiKey(
        { baseUrl: baseline.baseUrl, modelsUrl: baseline.modelsUrl, apiKey: '' },
        baseline,
        'apiKey',
        1,
      ).apiKey,
    ).toBe('');
    expect(
      restoreHydratedApiKey(
        { baseUrl: 'https://new.example/v1', modelsUrl: baseline.modelsUrl, apiKey: '' },
        baseline,
        'apiKey',
        0,
      ).apiKey,
    ).toBe('');
  });
});

describe('connectionTestCanUseSaved', () => {
  const connForm = {
    baseUrl: ' https://gw.example/v1 ',
    requestPath: ' /responses ',
    modelsUrl: 'https://gw.example/v1/models',
    apiKey: '',
    headers: [] as ReadonlyArray<{ name: string; value: string }>,
    wireProtocol: 'openai-responses' as const,
    models: [{ id: 'm-1' }],
  };

  it('uses the saved probe when endpoint, protocol, auth mode and credential material are all unchanged', () => {
    expect(connectionTestCanUseSaved(connForm, headerAuthBaseline, 'none')).toBe(true);
  });

  it('falls back to adhoc when the first model protocol override changed', () => {
    expect(
      connectionTestCanUseSaved(
        { ...connForm, models: [{ id: 'm-1', piApi: 'anthropic-messages' }] },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(false);
    expect(
      connectionTestCanUseSaved(
        { ...connForm, models: [{ id: 'm-1', piApi: 'anthropic-messages' }] },
        { ...headerAuthBaseline, modelPiApi: 'anthropic-messages' },
        'none',
      ),
    ).toBe(true);
  });

  it('falls back to adhoc when the first model route changed', () => {
    const modelRoute = {
      baseUrl: 'https://gw.example/anthropic',
      wireProtocol: 'anthropic-messages' as const,
    };
    expect(
      connectionTestCanUseSaved(
        { ...connForm, models: [{ id: 'm-1', route: modelRoute }] },
        { ...headerAuthBaseline, modelRoute },
        'none',
      ),
    ).toBe(true);
    expect(
      connectionTestCanUseSaved(
        {
          ...connForm,
          models: [
            {
              id: 'm-1',
              route: { ...modelRoute, baseUrl: 'https://gw.example/anthropic-v2' },
            },
          ],
        },
        { ...headerAuthBaseline, modelRoute },
        'none',
      ),
    ).toBe(false);
  });

  it('falls back to adhoc when endpoint, protocol or auth mode changed', () => {
    expect(
      connectionTestCanUseSaved(
        { ...connForm, baseUrl: 'https://gw.example/v2' },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(false);
    expect(
      connectionTestCanUseSaved({ ...connForm, requestPath: '/chat' }, headerAuthBaseline, 'none'),
    ).toBe(false);
    expect(
      connectionTestCanUseSaved(
        { ...connForm, wireProtocol: 'openai-chat' },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(false);
    expect(connectionTestCanUseSaved(connForm, headerAuthBaseline, 'apiKey')).toBe(false);
  });

  it('falls back to adhoc when the user changed the api key so the new key is what gets tested', () => {
    const apiKeyBaseline: SavedProviderProbeBaseline = {
      ...headerAuthBaseline,
      authMode: 'apiKey',
      apiKey: 'saved-key',
    };
    expect(
      connectionTestCanUseSaved({ ...connForm, apiKey: 'saved-key' }, apiKeyBaseline, 'apiKey'),
    ).toBe(true);
    expect(
      connectionTestCanUseSaved({ ...connForm, apiKey: 'new-key' }, apiKeyBaseline, 'apiKey'),
    ).toBe(false);
  });

  it('falls back to adhoc when the user edited a request header (new header material takes precedence)', () => {
    expect(
      connectionTestCanUseSaved(
        { ...connForm, headers: [{ name: 'X-Tenant', value: 'acme' }] },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(false);
    // 编辑态的空白占位行(name 为空)与基线空头视为一致 → 仍走 saved 探测。
    expect(
      connectionTestCanUseSaved(
        { ...connForm, headers: [{ name: '', value: '' }] },
        headerAuthBaseline,
        'none',
      ),
    ).toBe(true);
  });
});

it('uses the same eligible chat model for probe ID, route and request signature', () => {
  const media = { id: 'image', mode: 'image_generation', route: { baseUrl: 'https://image.example', wireProtocol: 'openai-chat' as const } };
  const chat = { id: 'flux-image-x', route: { baseUrl: 'https://chat.example', wireProtocol: 'openai-chat' as const } };
  const input = { ...fields, wireProtocol: 'openai-chat' as const, models: [media, chat] };
  expect(firstProviderChatModel(input.models)).toBe(chat);
  expect(resolveProviderConnectionProbeRoute('codex', input)?.baseUrl).toBe('https://chat.example');
  expect(providerConnectionTestRequestSignature(input, 'apiKey')).toBe(providerConnectionTestRequestSignature({ ...input, models: [chat] }, 'apiKey'));
  expect(firstProviderChatModel([media])).toBeUndefined();
});


it.each(['pi', 'codex', 'claude-code'] as const)('retains Vertex SDK identity for the %s connection test', agent => {
  expect(resolveProviderConnectionProbeRoute(agent, { baseUrl: 'https://us-central1-aiplatform.googleapis.com',
    requestPath: '', wireProtocol: 'google-generative-ai',
    models: [{ id: 'gemini-fixture', piApi: 'google-vertex' }] })).toEqual({
      baseUrl: 'https://us-central1-aiplatform.googleapis.com', wireProtocol: 'google-generative-ai', api: 'google-vertex',
    });
});


it('invalidates a connection test when the actual SDK changes under the same display protocol', () => {
  const fields = { baseUrl: 'https://example.test', requestPath: '', modelsUrl: '', apiKey: 'fixture', headers: [],
    wireProtocol: 'google-generative-ai' as const, models: [{ id: 'gemini', api: 'google-generative-ai' as const }] };
  const changed = { ...fields, models: [{ id: 'gemini', api: 'google-vertex' as const }] };
  expect(providerConnectionTestRequestSignature(fields, 'apiKey'))
    .not.toBe(providerConnectionTestRequestSignature(changed, 'apiKey'));
});

it.each(['claude-code', 'codex', 'pi'] as const)('projects ID-only template models before an edited %s probe', async agent => {
  const { BUNDLED_CATALOG } = await import('@cindy/model-providers');
  const preset = (BUNDLED_CATALOG.presets ?? []).find(p => p.id === 'google-gemini-api')!;
  const runtime = preset.runtimes[agent]!;
  for (const id of [runtime.models[0].id, 'gemini-future-deployment']) {
    expect(resolveProviderConnectionProbeRoute(agent, {
      catalogPresetId: preset.id, baseUrl: runtime.baseUrl,
      wireProtocol: runtime.wireProtocol!, requestPath: runtime.requestPath ?? '', models: [{ id }],
    }, BUNDLED_CATALOG.presets)).toMatchObject({
      api: 'google-generative-ai', wireProtocol: 'google-generative-ai',
      baseUrl: 'https://generativelanguage.googleapis.com/v1beta',
    });
  }
});