|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { VSBuffer } from 'vs/base/common/buffer'; |
| 7 | +import { joinPath } from 'vs/base/common/resources'; |
| 8 | +import { ServicesAccessor } from 'vs/editor/browser/editorExtensions'; |
| 9 | +import { localize } from 'vs/nls'; |
| 10 | +import { Action2, registerAction2 } from 'vs/platform/actions/common/actions'; |
| 11 | +import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs'; |
| 12 | +import { IFileService } from 'vs/platform/files/common/files'; |
| 13 | +import { INTERACTIVE_SESSION_CATEGORY } from 'vs/workbench/contrib/chat/browser/actions/chatActions'; |
| 14 | +import { IChatWidgetService } from 'vs/workbench/contrib/chat/browser/chat'; |
| 15 | +import { IChatEditorOptions } from 'vs/workbench/contrib/chat/browser/chatEditor'; |
| 16 | +import { ChatEditorInput } from 'vs/workbench/contrib/chat/browser/chatEditorInput'; |
| 17 | +import { isSerializableSessionData } from 'vs/workbench/contrib/chat/common/chatModel'; |
| 18 | +import { IChatService } from 'vs/workbench/contrib/chat/common/chatService'; |
| 19 | +import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; |
| 20 | + |
| 21 | +const defaultFileName = 'chat.json'; |
| 22 | +const filters = [{ name: localize('interactiveSession.file.label', "Chat Session"), extensions: ['json'] }]; |
| 23 | + |
| 24 | +export function registerInteractiveSessionExportActions() { |
| 25 | + registerAction2(class ExportInteractiveSessionAction extends Action2 { |
| 26 | + constructor() { |
| 27 | + super({ |
| 28 | + id: 'workbench.action.interactiveSession.export', |
| 29 | + category: INTERACTIVE_SESSION_CATEGORY, |
| 30 | + title: { |
| 31 | + value: localize('interactiveSession.export.label', "Export Session") + '...', |
| 32 | + original: 'Export Session...' |
| 33 | + }, |
| 34 | + f1: true, |
| 35 | + }); |
| 36 | + } |
| 37 | + async run(accessor: ServicesAccessor, ...args: any[]) { |
| 38 | + const widgetService = accessor.get(IChatWidgetService); |
| 39 | + const fileDialogService = accessor.get(IFileDialogService); |
| 40 | + const fileService = accessor.get(IFileService); |
| 41 | + const interactiveSessionService = accessor.get(IChatService); |
| 42 | + |
| 43 | + const widget = widgetService.lastFocusedWidget; |
| 44 | + if (!widget || !widget.viewModel) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const defaultUri = joinPath(await fileDialogService.defaultFilePath(), defaultFileName); |
| 49 | + const result = await fileDialogService.showSaveDialog({ |
| 50 | + defaultUri, |
| 51 | + filters |
| 52 | + }); |
| 53 | + if (!result) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const model = interactiveSessionService.getSession(widget.viewModel.sessionId); |
| 58 | + if (!model) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Using toJSON on the model |
| 63 | + const content = VSBuffer.fromString(JSON.stringify(model, undefined, 2)); |
| 64 | + await fileService.writeFile(result, content); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + registerAction2(class ImportInteractiveSessionAction extends Action2 { |
| 69 | + constructor() { |
| 70 | + super({ |
| 71 | + id: 'workbench.action.interactiveSession.import', |
| 72 | + title: { |
| 73 | + value: localize('interactiveSession.import.label', "Import Session") + '...', |
| 74 | + original: 'Export Session...' |
| 75 | + }, |
| 76 | + category: INTERACTIVE_SESSION_CATEGORY, |
| 77 | + f1: true, |
| 78 | + }); |
| 79 | + } |
| 80 | + async run(accessor: ServicesAccessor, ...args: any[]) { |
| 81 | + const fileDialogService = accessor.get(IFileDialogService); |
| 82 | + const fileService = accessor.get(IFileService); |
| 83 | + const editorService = accessor.get(IEditorService); |
| 84 | + |
| 85 | + const defaultUri = joinPath(await fileDialogService.defaultFilePath(), defaultFileName); |
| 86 | + const result = await fileDialogService.showOpenDialog({ |
| 87 | + defaultUri, |
| 88 | + canSelectFiles: true, |
| 89 | + filters |
| 90 | + }); |
| 91 | + if (!result) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const content = await fileService.readFile(result[0]); |
| 96 | + try { |
| 97 | + const data = JSON.parse(content.value.toString()); |
| 98 | + if (!isSerializableSessionData(data)) { |
| 99 | + throw new Error('Invalid chat session data'); |
| 100 | + } |
| 101 | + |
| 102 | + await editorService.openEditor({ resource: ChatEditorInput.getNewEditorUri(), options: <IChatEditorOptions>{ target: { data }, pinned: true } }); |
| 103 | + } catch (err) { |
| 104 | + throw err; |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | +} |
0 commit comments