migrate showSaveDialog hostBridge (#5080)
* migrate showSaveDialog hostBridge * rework proto type * update proto type names
This commit is contained in:
1 parent
35c0ced254
commit
58465d2e32
3 files changed
+58
-15
No files matched your search
+20
-1
@@ -12,6 +12,7 @@ service WindowService {
|
||||
rpc showTextDocument(ShowTextDocumentRequest) returns (TextEditorInfo);
|
||||
rpc showOpenDialogue(ShowOpenDialogueRequest) returns (SelectedResources);
|
||||
rpc showMessage(ShowMessageRequest) returns (SelectedResponse);
|
||||
rpc showSaveDialog(ShowSaveDialogRequest) returns (ShowSaveDialogResponse);
|
||||
}
|
||||
|
||||
message ShowTextDocumentRequest {
|
||||
@@ -70,4 +71,22 @@ message ShowMessageRequestOptions {
|
||||
|
||||
message SelectedResponse {
|
||||
optional string selected_option = 1;
|
||||
}
|
||||
}
|
||||
|
||||
message ShowSaveDialogRequest {
|
||||
cline.Metadata metadata = 1;
|
||||
optional ShowSaveDialogOptions options = 2;
|
||||
}
|
||||
|
||||
message ShowSaveDialogOptions {
|
||||
optional string default_path = 1;
|
||||
map<string, FileExtensionList> filters = 2;
|
||||
}
|
||||
|
||||
message FileExtensionList {
|
||||
repeated string extensions = 1;
|
||||
}
|
||||
|
||||
message ShowSaveDialogResponse {
|
||||
optional string selected_path = 1;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { window, Uri, SaveDialogOptions } from "vscode"
|
||||
import { ShowSaveDialogRequest, ShowSaveDialogResponse } from "@/shared/proto/index.host"
|
||||
|
||||
export async function showSaveDialog(request: ShowSaveDialogRequest): Promise<ShowSaveDialogResponse> {
|
||||
const { options } = request
|
||||
|
||||
const vscodeOptions: SaveDialogOptions = {}
|
||||
|
||||
if (options?.defaultPath) {
|
||||
vscodeOptions.defaultUri = Uri.file(options.defaultPath)
|
||||
}
|
||||
|
||||
if (options?.filters && Object.keys(options.filters).length > 0) {
|
||||
vscodeOptions.filters = {}
|
||||
Object.entries(options.filters).forEach(([name, extensionList]) => {
|
||||
vscodeOptions.filters![name] = extensionList.extensions
|
||||
})
|
||||
}
|
||||
|
||||
const selectedUri = await window.showSaveDialog(vscodeOptions)
|
||||
|
||||
return ShowSaveDialogResponse.create({
|
||||
selectedPath: selectedUri?.fsPath,
|
||||
})
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
import { Anthropic } from "@anthropic-ai/sdk"
|
||||
import os from "os"
|
||||
import * as path from "path"
|
||||
import * as vscode from "vscode"
|
||||
import { HostProvider } from "@/hosts/host-provider"
|
||||
import { ShowTextDocumentRequest, ShowTextDocumentOptions, ShowMessageRequest, ShowMessageType } from "@/shared/proto/host/window"
|
||||
import { ShowMessageType } from "@/shared/proto/host/window"
|
||||
import { writeFile } from "@utils/fs"
|
||||
|
||||
export async function downloadTask(dateTs: number, conversationHistory: Anthropic.MessageParam[]) {
|
||||
@@ -32,23 +31,23 @@ export async function downloadTask(dateTs: number, conversationHistory: Anthropi
|
||||
.join("---\n\n")
|
||||
|
||||
// Prompt user for save location
|
||||
const saveUri = await vscode.window.showSaveDialog({
|
||||
filters: { Markdown: ["md"] },
|
||||
defaultUri: vscode.Uri.file(path.join(os.homedir(), "Downloads", fileName)),
|
||||
const saveResponse = await HostProvider.window.showSaveDialog({
|
||||
options: {
|
||||
filters: { Markdown: { extensions: ["md"] } },
|
||||
defaultPath: path.join(os.homedir(), "Downloads", fileName),
|
||||
},
|
||||
})
|
||||
|
||||
if (saveUri) {
|
||||
if (saveResponse.selectedPath) {
|
||||
try {
|
||||
// Write content to the selected location
|
||||
await writeFile(saveUri.fsPath, markdownContent)
|
||||
await HostProvider.window.showTextDocument(
|
||||
ShowTextDocumentRequest.create({
|
||||
path: saveUri.fsPath,
|
||||
options: ShowTextDocumentOptions.create({ preview: true }),
|
||||
}),
|
||||
)
|
||||
await writeFile(saveResponse.selectedPath, markdownContent)
|
||||
await HostProvider.window.showTextDocument({
|
||||
path: saveResponse.selectedPath,
|
||||
options: { preview: true },
|
||||
})
|
||||
} catch (error) {
|
||||
HostProvider.window.showMessage({
|
||||
await HostProvider.window.showMessage({
|
||||
type: ShowMessageType.ERROR,
|
||||
message: `Failed to save markdown file: ${error instanceof Error ? error.message : String(error)}`,
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user