|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { Logger } from "../infrastructure/logger/logger"; |
| 3 | +import { Orchestrator } from "./../agents/orchestrator"; |
| 4 | + |
| 5 | +export class VSCodeSecretStorage { |
| 6 | + private readonly context: vscode.ExtensionContext; |
| 7 | + private readonly logger: Logger; |
| 8 | + private readonly orchestrator: Orchestrator; |
| 9 | + private static instance: VSCodeSecretStorage; |
| 10 | + |
| 11 | + constructor(context: vscode.ExtensionContext) { |
| 12 | + this.context = context; |
| 13 | + this.logger = new Logger("VSCodeSecretStorage"); |
| 14 | + this.orchestrator = Orchestrator.getInstance(); |
| 15 | + } |
| 16 | + |
| 17 | + static getInstance(context: vscode.ExtensionContext) { |
| 18 | + if (!VSCodeSecretStorage.instance) { |
| 19 | + VSCodeSecretStorage.instance = new VSCodeSecretStorage(context); |
| 20 | + } |
| 21 | + return VSCodeSecretStorage.instance; |
| 22 | + } |
| 23 | + |
| 24 | + async get(key: string): Promise<string | undefined> { |
| 25 | + try { |
| 26 | + return await this.context.secrets.get(key); |
| 27 | + } catch (error) { |
| 28 | + this.logger.error(`Error retrieving secret for key: ${key}`, error); |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + async store(key: string, value: string): Promise<void> { |
| 34 | + try { |
| 35 | + if (value?.length > 0) { |
| 36 | + await this.context.secrets.store(key, value); |
| 37 | + } |
| 38 | + } catch (error) { |
| 39 | + this.logger.error(`Error storing secret for key: ${key}`, error); |
| 40 | + throw error; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + async delete(key: string): Promise<void> { |
| 45 | + try { |
| 46 | + await this.context.secrets.delete(key); |
| 47 | + } catch (error) { |
| 48 | + this.logger.error(`Error deleting secret for key: ${key}`, error); |
| 49 | + throw error; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + onDidChange(): vscode.Disposable { |
| 54 | + return this.context.secrets.onDidChange((event) => { |
| 55 | + if (event.key.length > 0) { |
| 56 | + this.orchestrator.handleStatus({ |
| 57 | + type: "onSecretChange", |
| 58 | + data: event.key, |
| 59 | + timestamp: new Date().toISOString(), |
| 60 | + }); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | +} |
0 commit comments