diff --git a/proto/host/window.proto b/proto/host/window.proto index fe84da3a..0f4b2887 100644 --- a/proto/host/window.proto +++ b/proto/host/window.proto @@ -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; -} \ No newline at end of file +} + +message ShowSaveDialogRequest { + cline.Metadata metadata = 1; + optional ShowSaveDialogOptions options = 2; +} + +message ShowSaveDialogOptions { + optional string default_path = 1; + map filters = 2; +} + +message FileExtensionList { + repeated string extensions = 1; +} + +message ShowSaveDialogResponse { + optional string selected_path = 1; +} diff --git a/src/hosts/vscode/hostbridge/window/showSaveDialog.ts b/src/hosts/vscode/hostbridge/window/showSaveDialog.ts new file mode 100644 index 00000000..3ccde072 --- /dev/null +++ b/src/hosts/vscode/hostbridge/window/showSaveDialog.ts @@ -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 { + 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, + }) +} diff --git a/src/integrations/misc/export-markdown.ts b/src/integrations/misc/export-markdown.ts index 8a90a327..3b235fbe 100644 --- a/src/integrations/misc/export-markdown.ts +++ b/src/integrations/misc/export-markdown.ts @@ -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)}`, })