| 61 | * ``` |
| 62 | */ |
| 63 | export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHandler { |
| 64 | protected options: ApiHandlerOptions |
| 65 | private client: vscode.LanguageModelChat | null |
| 66 | private disposable: vscode.Disposable | null |
| 67 | private currentRequestCancellation: vscode.CancellationTokenSource | null |
| 68 | |
| 69 | constructor(options: ApiHandlerOptions) { |
| 70 | super() |
| 71 | this.options = options |
| 72 | this.client = null |
| 73 | this.disposable = null |
| 74 | this.currentRequestCancellation = null |
| 75 | |
| 76 | try { |
| 77 | // Listen for model changes and reset client |
| 78 | this.disposable = vscode.workspace.onDidChangeConfiguration((event) => { |
| 79 | if (event.affectsConfiguration("lm")) { |
| 80 | try { |
| 81 | this.client = null |
| 82 | this.ensureCleanState() |
| 83 | } catch (error) { |
| 84 | console.error("Error during configuration change cleanup:", error) |
| 85 | } |
| 86 | } |
| 87 | }) |
| 88 | this.initializeClient() |
| 89 | } catch (error) { |
| 90 | // Ensure cleanup if constructor fails |
| 91 | this.dispose() |
| 92 | |
| 93 | throw new Error( |
| 94 | `Roo Code <Language Model API>: Failed to initialize handler: ${error instanceof Error ? error.message : "Unknown error"}`, |
| 95 | ) |
| 96 | } |
| 97 | } |
| 98 | /** |
| 99 | * Initializes the VS Code Language Model client. |
| 100 | * This method is called during the constructor to set up the client. |
| 101 | * This useful when the client is not created yet and call getModel() before the client is created. |
| 102 | * @returns Promise<void> |
| 103 | * @throws Error when client initialization fails |
| 104 | */ |
| 105 | async initializeClient(): Promise<void> { |
| 106 | try { |
| 107 | // Check if the client is already initialized |
| 108 | if (this.client) { |
| 109 | console.debug("Roo Code <Language Model API>: Client already initialized") |
| 110 | return |
| 111 | } |
| 112 | // Create a new client instance |
| 113 | this.client = await this.createClient(this.options.vsCodeLmModelSelector || {}) |
| 114 | console.debug("Roo Code <Language Model API>: Client initialized successfully") |
| 115 | } catch (error) { |
| 116 | // Handle errors during client initialization |
| 117 | const errorMessage = error instanceof Error ? error.message : "Unknown error" |
| 118 | console.error("Roo Code <Language Model API>: Client initialization failed:", errorMessage) |
| 119 | throw new Error(`Roo Code <Language Model API>: Failed to initialize client: ${errorMessage}`) |
| 120 | } |
nothing calls this directly
no outgoing calls
no test coverage detected