refactor(hosts): Simplify the host provider interface for callers (#5057)

* refactor(hosts): Simplify the host provider interface for callers

- Streamline API to reduce complexity for consuming code
- Update all references across core, controller, and integration modules
- Consolidate provider patterns for easier usage
- Rename host-providers.ts to host-provider.ts

# Conflicts:
#	src/core/controller/index.ts
#	src/extension.ts
#	src/integrations/git/commit-message-generator.ts

* Add comment

* In the tests, reset the HostProvider to prevent it complaining that it is being reinitialized

* Update comment

* Fix imports
This commit is contained in:
Sarah Fortune authored and GitHub committed 2025-07-21 12:20:50 -07:00
1 parent c970b8030e
commit 8d5985c8fd
30 files changed
+223 -172

No files matched your search

+3 -3
View File
@@ -2,7 +2,7 @@ import { Anthropic } from "@anthropic-ai/sdk"
import os from "os"
import * as path from "path"
import * as vscode from "vscode"
import { getHostBridgeProvider } from "@/hosts/host-providers"
import { HostProvider } from "@/hosts/host-provider"
import { ShowTextDocumentRequest, ShowTextDocumentOptions, ShowMessageRequest, ShowMessageType } from "@/shared/proto/host/window"
import { writeFile } from "@utils/fs"
@@ -41,14 +41,14 @@ export async function downloadTask(dateTs: number, conversationHistory: Anthropi
try {
// Write content to the selected location
await writeFile(saveUri.fsPath, markdownContent)
await getHostBridgeProvider().windowClient.showTextDocument(
await HostProvider.window.showTextDocument(
ShowTextDocumentRequest.create({
path: saveUri.fsPath,
options: ShowTextDocumentOptions.create({ preview: true }),
}),
)
} catch (error) {
getHostBridgeProvider().windowClient.showMessage({
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: `Failed to save markdown file: ${error instanceof Error ? error.message : String(error)}`,
})
+5 -5
View File
@@ -2,14 +2,14 @@ import * as path from "path"
import * as os from "os"
import * as vscode from "vscode"
import { arePathsEqual } from "@utils/path"
import { getHostBridgeProvider } from "@/hosts/host-providers"
import { HostProvider } from "@/hosts/host-provider"
import { ShowMessageRequest, ShowMessageType } from "@/shared/proto/host/window"
import { writeFile } from "@utils/fs"
export async function openImage(dataUri: string) {
const matches = dataUri.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/)
if (!matches) {
getHostBridgeProvider().windowClient.showMessage({
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: "Invalid data URI format",
})
@@ -22,7 +22,7 @@ export async function openImage(dataUri: string) {
await writeFile(tempFilePath, new Uint8Array(imageBuffer))
await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(tempFilePath))
} catch (error) {
getHostBridgeProvider().windowClient.showMessage({
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: `Error opening image: ${error}`,
})
@@ -50,12 +50,12 @@ export async function openFile(absolutePath: string) {
}
} catch {} // not essential, sometimes tab operations fail
await getHostBridgeProvider().windowClient.showTextDocument({
await HostProvider.window.showTextDocument({
path: uri.fsPath,
options: { preview: false },
})
} catch (error) {
getHostBridgeProvider().windowClient.showMessage({
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: `Could not open file!`,
})
+6 -6
View File
@@ -2,7 +2,7 @@ import * as vscode from "vscode"
import fs from "fs/promises"
import * as path from "path"
import sizeOf from "image-size"
import { getHostBridgeProvider } from "@/hosts/host-providers"
import { HostProvider } from "@/hosts/host-provider"
import { ShowMessageRequest, ShowMessageType, ShowOpenDialogueRequest } from "@/shared/proto/host/window"
/**
@@ -13,7 +13,7 @@ export async function selectFiles(imagesAllowed: boolean): Promise<{ images: str
const IMAGE_EXTENSIONS = ["png", "jpg", "jpeg", "webp"] // supported by anthropic and openrouter
const OTHER_FILE_EXTENSIONS = ["xml", "json", "txt", "log", "md", "docx", "ipynb", "pdf", "xlsx", "csv"]
const showDialogueResponse = await getHostBridgeProvider().windowClient.showOpenDialogue(
const showDialogueResponse = await HostProvider.window.showOpenDialogue(
ShowOpenDialogueRequest.create({
canSelectMany: true,
openLabel: "Select",
@@ -46,7 +46,7 @@ export async function selectFiles(imagesAllowed: boolean): Promise<{ images: str
const dimensions = sizeOf(uint8Array) // Get dimensions from Uint8Array
if (dimensions.width! > 7500 || dimensions.height! > 7500) {
console.warn(`Image dimensions exceed 7500px, skipping: ${filePath}`)
getHostBridgeProvider().windowClient.showMessage({
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: `Image too large: ${path.basename(filePath)} was skipped (dimensions exceed 7500px).`,
})
@@ -54,7 +54,7 @@ export async function selectFiles(imagesAllowed: boolean): Promise<{ images: str
}
} catch (error) {
console.error(`Error reading file or getting dimensions for ${filePath}:`, error)
getHostBridgeProvider().windowClient.showMessage({
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: `Could not read dimensions for ${path.basename(filePath)}, skipping.`,
})
@@ -72,7 +72,7 @@ export async function selectFiles(imagesAllowed: boolean): Promise<{ images: str
const stats = await fs.stat(filePath)
if (stats.size > 20 * 1000 * 1024) {
console.warn(`File too large, skipping: ${filePath}`)
getHostBridgeProvider().windowClient.showMessage({
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: `File too large: ${path.basename(filePath)} was skipped (size exceeds 20MB).`,
})
@@ -80,7 +80,7 @@ export async function selectFiles(imagesAllowed: boolean): Promise<{ images: str
}
} catch (error) {
console.error(`Error checking file size for ${filePath}:`, error)
getHostBridgeProvider().windowClient.showMessage({
HostProvider.window.showMessage({
type: ShowMessageType.ERROR,
message: `Could not check file size for ${path.basename(filePath)}, skipping.`,
})