All files / src/ai registry.ts

87.35% Statements 76/87
73.68% Branches 14/19
100% Functions 7/7
87.35% Lines 76/87

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 882x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 2x 2x 8x 2x 8x 8x 8x               8x 8x     8x 2x 8x 8x 8x 8x 8x 8x 8x 6x 6x 8x 8x     8x 8x 8x 2x 11x 11x 9x 11x 1x 1x 8x 8x 8x 8x 8x 8x 2x 9x 9x 9x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import type { ProviderConfig, ProvidersFile, ProviderMeta } from './types';
 
let cachedProviders: ProviderConfig[] | null = null;
 
function loadBuiltinProviders(extensionUri: vscode.Uri): ProviderConfig[] {
  const filePath = vscode.Uri.joinPath(extensionUri, 'providers.json').fsPath;
  try {
    const raw = fs.readFileSync(filePath, 'utf-8');
    const data = JSON.parse(raw) as ProvidersFile;
    return data.providers ?? [];
  } catch {
    return [];
  }
}
 
function loadUserProviders(): ProviderConfig[] {
  const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
  if (!workspaceRoot) { return []; }

  const filePath = path.join(workspaceRoot, '.code-review', 'providers.json');
  if (!fs.existsSync(filePath)) { return []; }

  try {
    const raw = fs.readFileSync(filePath, 'utf-8');
    const data = JSON.parse(raw) as ProvidersFile;
    return data.providers ?? [];
  } catch {
    return [];
  }
}
 
function mergeProviders(
  builtin: ProviderConfig[],
  user: ProviderConfig[]
): ProviderConfig[] {
  const map = new Map<string, ProviderConfig>();
 
  for (const p of builtin) {
    map.set(p.id, p);
  }
 
  for (const p of user) {
    map.set(p.id, p);
  }
 
  return Array.from(map.values());
}
 
export function getProviders(extensionUri?: vscode.Uri): ProviderConfig[] {
  if (cachedProviders) { return cachedProviders; }
 
  if (!extensionUri) {
    return [];
  }
 
  const builtin = loadBuiltinProviders(extensionUri);
  const user = loadUserProviders();
  cachedProviders = mergeProviders(builtin, user);
  return cachedProviders;
}
 
export function invalidateProviderCache(): void {
  cachedProviders = null;
}
 
export function getProviderById(
  extensionUri: vscode.Uri,
  id: string
): ProviderConfig | undefined {
  return getProviders(extensionUri).find(p => p.id === id);
}
 
export function getAllProviderMeta(
  extensionUri: vscode.Uri
): Record<string, ProviderMeta> {
  const result: Record<string, ProviderMeta> = {};
  for (const p of getProviders(extensionUri)) {
    result[p.id] = {
      name: p.name,
      models: p.models,
    };
  }
  return result;
}