Add ability to open images

This commit is contained in:
Saoud Rizwan
2024-09-10 17:18:21 -04:00
parent 3a4e8b8d96
commit 706ee8a0fe
6 changed files with 34 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ import fs from "fs/promises"
import { HistoryItem } from "../shared/HistoryItem"
import axios from "axios"
import { getTheme } from "../utils/getTheme"
import { openImage } from "../utils/open-image"
/*
https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
@@ -398,6 +399,9 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider {
const models = await this.getOllamaModels(message.text)
this.postMessageToWebview({ type: "ollamaModels", models })
break
case "openImage":
openImage(message.text!)
break
// Add more switch case statements here as more webview message commands
// are created within the webview context (i.e. inside media/main.js)
}

View File

@@ -17,6 +17,7 @@ export interface WebviewMessage {
| "exportTaskWithId"
| "resetState"
| "requestOllamaModels"
| "openImage"
text?: string
askResponse?: ClaudeAskResponse
apiConfiguration?: ApiConfiguration

20
src/utils/open-image.ts Normal file
View File

@@ -0,0 +1,20 @@
import * as path from "path"
import * as os from "os"
import * as vscode from "vscode"
export async function openImage(dataUri: string) {
const matches = dataUri.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/)
if (!matches) {
vscode.window.showErrorMessage("Invalid data URI format")
return
}
const [, format, base64Data] = matches
const imageBuffer = Buffer.from(base64Data, "base64")
const tempFilePath = path.join(os.tmpdir(), `temp_image_${Date.now()}.${format}`)
try {
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFilePath), imageBuffer)
await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(tempFilePath))
} catch (error) {
vscode.window.showErrorMessage(`Error opening image: ${error}`)
}
}