All files / src/ai factory.ts

43.13% Statements 22/51
100% Branches 0/0
0% Functions 0/4
43.13% Lines 22/51

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 522x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                                           2x       2x           2x 2x  
import * as vscode from 'vscode';
import type { AIProvider } from './providers/base';
import type { ProviderMeta, ProviderProtocol } from './types';
import { OpenAICompatibleProvider } from './providers/openai-compatible';
import { GeminiProvider } from './providers/gemini';
import { ClaudeProvider } from './providers/claude';
import {
  getProviderById,
  getAllProviderMeta as getAllProviderMetaFromRegistry,
  invalidateProviderCache,
} from './registry';
 
const PROTOCOL_MAP: Record<ProviderProtocol, new (...args: any[]) => AIProvider> = {
  'openai-compatible': OpenAICompatibleProvider,
  gemini: GeminiProvider,
  claude: ClaudeProvider,
};
 
export function createProvider(
  providerId: string,
  apiKey: string,
  baseUrl: string,
  extensionUri: vscode.Uri
): AIProvider {
  const config = getProviderById(extensionUri, providerId);
  if (!config) {
    throw new Error(`未知的 Provider: ${providerId}`);
  }

  const Cls = PROTOCOL_MAP[config.protocol];
  if (!Cls) {
    throw new Error(`未知的协议类型: ${config.protocol}`);
  }

  if (config.protocol === 'openai-compatible') {
    return new Cls(apiKey, baseUrl, config.id, config.name);
  }
  return new Cls(apiKey, baseUrl);
}
 
export function getProviderModels(extensionUri: vscode.Uri, providerId: string): string[] {
  return getProviderById(extensionUri, providerId)?.models ?? [];
}
 
export function getAllProviderMeta(
  extensionUri: vscode.Uri
): Record<string, ProviderMeta> {
  return getAllProviderMetaFromRegistry(extensionUri);
}
 
export { invalidateProviderCache };