|
| 1 | +import * as vscode from "vscode" |
| 2 | +import * as fs from "fs/promises" |
| 3 | +import * as path from "path" |
| 4 | +import { ClineProvidersConfig, CustomProviderConfig } from "../../shared/api" |
| 5 | +import { GlobalFileNames } from "../../core/webview/ClineProvider" |
| 6 | +import { customProviderConfigSchema } from "./CustomProvidersSchema" |
| 7 | + |
| 8 | +export class CustomProvidersFileService { |
| 9 | + public readonly providersFilePath: string |
| 10 | + private readonly secrets: vscode.SecretStorage |
| 11 | + |
| 12 | + constructor(storagePath: string, secrets: vscode.SecretStorage) { |
| 13 | + this.providersFilePath = path.join(storagePath, GlobalFileNames.customProviders) |
| 14 | + this.secrets = secrets |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Add a new custom provider with validation |
| 19 | + */ |
| 20 | + public async addProvider(provider: CustomProviderConfig): Promise<void> { |
| 21 | + try { |
| 22 | + const secretKey = `${provider.name}_API_KEY` |
| 23 | + |
| 24 | + // Store API key in secrets |
| 25 | + if (!provider.apiKey) { |
| 26 | + throw new Error("API key is required") |
| 27 | + } |
| 28 | + |
| 29 | + console.log(`Storing API key for provider ${provider.name}`) |
| 30 | + await this.secrets.store(secretKey, provider.apiKey) |
| 31 | + |
| 32 | + // Verify the key was stored |
| 33 | + const storedKey = await this.secrets.get(secretKey) |
| 34 | + if (!storedKey) { |
| 35 | + throw new Error(`Failed to store API key for provider ${provider.name}`) |
| 36 | + } |
| 37 | + |
| 38 | + // Save provider config without the actual API key |
| 39 | + const providerConfig = { |
| 40 | + ...provider, |
| 41 | + apiKey: `\${${secretKey}}`, // Store placeholder |
| 42 | + request: { |
| 43 | + ...provider.request, |
| 44 | + headers: { |
| 45 | + ...provider.request.headers, |
| 46 | + Authorization: `Bearer \${${secretKey}}`, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + const providers = await this.readProviders() |
| 52 | + providers[provider.name] = providerConfig |
| 53 | + await this.writeProviders(providers) |
| 54 | + } catch (error) { |
| 55 | + console.error("Error adding provider:", error) |
| 56 | + throw error |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Delete a custom provider and its associated API key |
| 62 | + */ |
| 63 | + public async deleteProvider(name: string): Promise<void> { |
| 64 | + try { |
| 65 | + const providers = await this.readProviders() |
| 66 | + delete providers[name] |
| 67 | + await this.secrets.delete(`${name}_API_KEY`) |
| 68 | + await this.writeProviders(providers) |
| 69 | + } catch (error) { |
| 70 | + console.error("Error deleting provider:", error) |
| 71 | + throw new Error(`Failed to delete provider: ${error instanceof Error ? error.message : String(error)}`) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get all providers with their API keys from secrets storage |
| 77 | + */ |
| 78 | + public async getProviders(): Promise<Record<string, CustomProviderConfig>> { |
| 79 | + try { |
| 80 | + const providers = await this.readProviders() |
| 81 | + const providersWithKeys: Record<string, CustomProviderConfig> = {} |
| 82 | + |
| 83 | + for (const [name, provider] of Object.entries(providers)) { |
| 84 | + const secretKey = `${name}_API_KEY` |
| 85 | + const apiKey = await this.secrets.get(secretKey) |
| 86 | + |
| 87 | + if (!apiKey) { |
| 88 | + console.warn(`No API key found in secrets for provider: ${name}`) |
| 89 | + continue // Skip providers without API keys |
| 90 | + } |
| 91 | + |
| 92 | + providersWithKeys[name] = { |
| 93 | + ...provider, |
| 94 | + apiKey, |
| 95 | + request: { |
| 96 | + ...provider.request, |
| 97 | + headers: { |
| 98 | + ...provider.request.headers, |
| 99 | + Authorization: `Bearer ${apiKey}`, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return providersWithKeys |
| 106 | + } catch (error) { |
| 107 | + console.error("Error getting providers:", error) |
| 108 | + throw error |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Read providers from the configuration file |
| 114 | + */ |
| 115 | + public async readProviders(): Promise<Record<string, CustomProviderConfig>> { |
| 116 | + try { |
| 117 | + const content = await fs.readFile(this.providersFilePath, "utf-8") |
| 118 | + const config: ClineProvidersConfig = JSON.parse(content) |
| 119 | + return config.providers |
| 120 | + } catch (error) { |
| 121 | + if ((error as NodeJS.ErrnoException).code === "ENOENT") { |
| 122 | + return {} |
| 123 | + } |
| 124 | + console.error("Error reading providers:", error) |
| 125 | + throw new Error(`Failed to read providers: ${error instanceof Error ? error.message : String(error)}`) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Write providers to the configuration file |
| 131 | + */ |
| 132 | + public async writeProviders(providers: Record<string, CustomProviderConfig>): Promise<void> { |
| 133 | + try { |
| 134 | + const config: ClineProvidersConfig = { providers } |
| 135 | + const content = JSON.stringify(config, null, 2) |
| 136 | + console.log(`Writing providers to file: ${this.providersFilePath}`) // Added log |
| 137 | + console.log(`Content: ${content}`) // Added log |
| 138 | + await fs.writeFile(this.providersFilePath, content, "utf-8") |
| 139 | + console.log("Providers written successfully.") // Added log |
| 140 | + } catch (error) { |
| 141 | + console.error("Error writing providers:", error) |
| 142 | + throw new Error(`Failed to write providers: ${error instanceof Error ? error.message : String(error)}`) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments