From 20b3ccffb93d4b96086ae7d1725208e6e53b37f4 Mon Sep 17 00:00:00 2001 From: Oleg Klimov Date: Thu, 9 Nov 2023 09:39:27 +0100 Subject: [PATCH] renames --- src/chatTab.ts | 31 +--- src/sidebar.ts | 404 ++++++++++++++++++++++--------------------------- 2 files changed, 186 insertions(+), 249 deletions(-) diff --git a/src/chatTab.ts b/src/chatTab.ts index 516bb819..baa1ec2b 100644 --- a/src/chatTab.ts +++ b/src/chatTab.ts @@ -32,7 +32,7 @@ export class ChatTab { this.chat_id = chat_id; } - public static async activate_from_outside( + public static async clear_and_repopulate_chat( question: string, editor: vscode.TextEditor | undefined, attach_default: boolean, @@ -229,7 +229,7 @@ export class ChatTab { } } - async chat_post_question( // User presses Enter + async post_question_and_communicate_answer( // User presses Enter question: string, model: string, model_function: string, @@ -249,7 +249,7 @@ export class ChatTab { // }); // return; // } - console.log(`chat_post_question saved messages backup: ${restore_messages_backup.length}`); + console.log(`post_question_and_communicate_answer saved messages backup: ${restore_messages_backup.length}`); this.messages = restore_messages_backup; await chat_model_set(model, model_function); // successfully used model, save it @@ -336,31 +336,6 @@ export class ChatTab { stack_this._answer_to_div(role0, answer, messages_backup); // send message inside answer_role = role0; } - // if (delta) { - // answer += delta; - // let valid_html = false; - // let html = ""; - // try { - // let raw_html = answer; - // let backtick_backtick_backtick_count = (answer.match(/```/g) || []).length; - // if (backtick_backtick_backtick_count % 2 === 1) { - // raw_html = answer + "\n```"; - // } - // html = marked.parse(raw_html); - // valid_html = true; - // } catch (e) { - // valid_html = false; - // } - // if (valid_html) { - // global.side_panel?._view?.webview.postMessage({ - // command: "chat-post-answer", - // answer_html: html, - // answer_raw: answer, - // have_editor: Boolean(stack_this.working_on_snippet_editor), - // messages_backup: messages_backup, - // }); - // } - // } if (json["metering_balance"]) { global.user_metering_balance = json["metering_balance"]; if (global.side_panel) { diff --git a/src/sidebar.ts b/src/sidebar.ts index d7f0a8dd..62610ba9 100644 --- a/src/sidebar.ts +++ b/src/sidebar.ts @@ -27,7 +27,7 @@ export async function open_chat_tab( return; } global.side_panel.goto_chat(chat); - await ChatTab.activate_from_outside( + await ChatTab.clear_and_repopulate_chat( question, editor, attach_default, @@ -144,215 +144,205 @@ export class PanelWebview implements vscode.WebviewViewProvider { } console.log(`RECEIVED JS2TS: ${JSON.stringify(data)}`); switch (data.type) { - case "focus_back_to_editor": { - vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'); - break; - } - case "open_new_chat": { - let question = data.question; - if (!question) { - question = ""; - } - let editor = vscode.window.activeTextEditor; - let attach_default = !!vscode.window.activeTextEditor; - await open_chat_tab( - question, - editor, - attach_default, - data.chat_model, - [], // messages - "", // chat id - ); - break; - } - case "delete_chat": { - const chat_id = data.chat_id; - await this.make_sure_have_chat_history_provider().deleteChatEntry(chat_id); - break; - } - case "button_hf_open_tokens": { - vscode.env.openExternal(vscode.Uri.parse(`https://huggingface.co/settings/tokens`)); - break; - } - case "button_hf_save": { - await vscode.workspace.getConfiguration().update('refactai.apiKey', data.hf_api_key, vscode.ConfigurationTarget.Global); - await vscode.workspace.getConfiguration().update('refactai.addressURL', "HF", vscode.ConfigurationTarget.Global); - break; - } - case "button_refact_save": { - await vscode.workspace.getConfiguration().update('refactai.apiKey', data.refact_api_key, vscode.ConfigurationTarget.Global); - await vscode.workspace.getConfiguration().update('refactai.addressURL', "Refact", vscode.ConfigurationTarget.Global); - break; - } - case "button_refact_open_streamlined": { - vscode.commands.executeCommand('refactaicmd.login'); - break; - } - case "button_refact_save": { - await vscode.workspace.getConfiguration().update('refactai.apiKey', data.refact_api_key, vscode.ConfigurationTarget.Global); - await vscode.workspace.getConfiguration().update('refactai.addressURL', "SMC", vscode.ConfigurationTarget.Global); - break; - } - case "save_enterprise": { - await vscode.workspace.getConfiguration().update('refactai.infurl', data.endpoint, vscode.ConfigurationTarget.Global); - await vscode.workspace.getConfiguration().update('refactai.apiKey', data.apikey, vscode.ConfigurationTarget.Global); - break; - } - case "save_selfhosted": { - await vscode.workspace.getConfiguration().update('refactai.infurl', data.endpoint, vscode.ConfigurationTarget.Global); - await vscode.workspace.getConfiguration().update('refactai.apiKey', 'aaa', vscode.ConfigurationTarget.Global); - break; - } - case "privacy": { - vscode.commands.executeCommand("refactaicmd.privacySettings"); - break; - } - case "js2ts_report_bug": { - vscode.env.openExternal(vscode.Uri.parse(`https://github.com/smallcloudai/refact-vscode/issues`)); - break; - } - case "js2ts_discord": { - vscode.env.openExternal(vscode.Uri.parse(`https://www.smallcloud.ai/discord`)); - break; - } - case "js2ts_logout": { - vscode.commands.executeCommand("refactaicmd.logout"); - break; + case "focus_back_to_editor": { + vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'); + break; + } + case "open_new_chat": { + let question = data.question; + if (!question) { + question = ""; } - case "js2ts_goto_profile": { - vscode.env.openExternal(vscode.Uri.parse(`https://refact.smallcloud.ai/account?utm_source=plugin&utm_medium=vscode&utm_campaign=account`)); - break; + let editor = vscode.window.activeTextEditor; + let attach_default = !!vscode.window.activeTextEditor; + await open_chat_tab( + question, + editor, + attach_default, + data.chat_model, + [], // messages + "", // chat id + ); + break; + } + case "delete_chat": { + const chat_id = data.chat_id; + await this.make_sure_have_chat_history_provider().deleteChatEntry(chat_id); + break; + } + case "button_hf_open_tokens": { + vscode.env.openExternal(vscode.Uri.parse(`https://huggingface.co/settings/tokens`)); + break; + } + case "button_hf_save": { + await vscode.workspace.getConfiguration().update('refactai.apiKey', data.hf_api_key, vscode.ConfigurationTarget.Global); + await vscode.workspace.getConfiguration().update('refactai.addressURL', "HF", vscode.ConfigurationTarget.Global); + break; + } + case "button_refact_save": { + await vscode.workspace.getConfiguration().update('refactai.apiKey', data.refact_api_key, vscode.ConfigurationTarget.Global); + await vscode.workspace.getConfiguration().update('refactai.addressURL', "Refact", vscode.ConfigurationTarget.Global); + break; + } + case "button_refact_open_streamlined": { + vscode.commands.executeCommand('refactaicmd.login'); + break; + } + case "button_refact_save": { + await vscode.workspace.getConfiguration().update('refactai.apiKey', data.refact_api_key, vscode.ConfigurationTarget.Global); + await vscode.workspace.getConfiguration().update('refactai.addressURL', "SMC", vscode.ConfigurationTarget.Global); + break; + } + case "save_enterprise": { + await vscode.workspace.getConfiguration().update('refactai.infurl', data.endpoint, vscode.ConfigurationTarget.Global); + await vscode.workspace.getConfiguration().update('refactai.apiKey', data.apikey, vscode.ConfigurationTarget.Global); + break; + } + case "save_selfhosted": { + await vscode.workspace.getConfiguration().update('refactai.infurl', data.endpoint, vscode.ConfigurationTarget.Global); + await vscode.workspace.getConfiguration().update('refactai.apiKey', 'aaa', vscode.ConfigurationTarget.Global); + break; + } + case "privacy": { + vscode.commands.executeCommand("refactaicmd.privacySettings"); + break; + } + case "js2ts_report_bug": { + vscode.env.openExternal(vscode.Uri.parse(`https://github.com/smallcloudai/refact-vscode/issues`)); + break; + } + case "js2ts_discord": { + vscode.env.openExternal(vscode.Uri.parse(`https://www.smallcloud.ai/discord`)); + break; + } + case "js2ts_logout": { + vscode.commands.executeCommand("refactaicmd.logout"); + break; + } + case "js2ts_goto_profile": { + vscode.env.openExternal(vscode.Uri.parse(`https://refact.smallcloud.ai/account?utm_source=plugin&utm_medium=vscode&utm_campaign=account`)); + break; + } + case "js2ts_refresh_login": { + global.user_logged_in = ""; + global.user_active_plan = ""; + this.update_webview(); + await userLogin.inference_login(); + break; + } + case "openSettings": { + vscode.commands.executeCommand("refactaicmd.openSettings"); + break; + } + case "openKeys": { + vscode.commands.executeCommand('workbench.action.openGlobalKeybindings', '@ext:smallcloud.refact'); + break; + } + case "open-new-file": { + vscode.workspace.openTextDocument().then((document) => { + vscode.window.showTextDocument(document, vscode.ViewColumn.Active) + .then((editor) => { + editor.edit((editBuilder) => { + editBuilder.insert(new vscode.Position(0, 0), data.value); + }); + }); + }); + break; + } + case "diff-paste-back": { + if (!this.chat?.working_on_snippet_editor) { + return; } - case "js2ts_refresh_login": { - global.user_logged_in = ""; - global.user_active_plan = ""; - this.update_webview(); - await userLogin.inference_login(); - break; + await vscode.window.showTextDocument( + this.chat?.working_on_snippet_editor.document, + this.chat?.working_on_snippet_column + ); + let state = estate.state_of_document( + this.chat?.working_on_snippet_editor.document + ); + if (!state) { + return; } - case "openSettings": { - vscode.commands.executeCommand("refactaicmd.openSettings"); - break; + let editor = state.editor; + if (state.get_mode() !== estate.Mode.Normal) { + return; } - case "openKeys": { - vscode.commands.executeCommand('workbench.action.openGlobalKeybindings', '@ext:smallcloud.refact'); - break; + if (!this.chat?.working_on_snippet_range) { + return; } - case "open-new-file": { - vscode.workspace.openTextDocument().then((document) => { - vscode.window.showTextDocument(document, vscode.ViewColumn.Active) - .then((editor) => { - editor.edit((editBuilder) => { - editBuilder.insert(new vscode.Position(0, 0), data.value); - }); - }); - }); - break; + let verify_snippet = editor.document.getText( + this.chat?.working_on_snippet_range! + ); + if (verify_snippet !== this.chat?.working_on_snippet_code) { + return; } - case "diff-paste-back": { - if (!this.chat?.working_on_snippet_editor) { - return; - } - await vscode.window.showTextDocument( - this.chat?.working_on_snippet_editor.document, - this.chat?.working_on_snippet_column - ); - let state = estate.state_of_document( - this.chat?.working_on_snippet_editor.document - ); - if (!state) { - return; - } - let editor = state.editor; - if (state.get_mode() !== estate.Mode.Normal) { - return; - } - if (!this.chat?.working_on_snippet_range) { - return; - } - let verify_snippet = editor.document.getText( - this.chat?.working_on_snippet_range! - ); - if (verify_snippet !== this.chat?.working_on_snippet_code) { - return; - } - let text = editor.document.getText(); - let snippet_ofs0 = editor.document.offsetAt( - this.chat?.working_on_snippet_range.start - ); - let snippet_ofs1 = editor.document.offsetAt( - this.chat?.working_on_snippet_range.end - ); - let modif_doc: string = text.substring(0, snippet_ofs0) + data.value + text.substring(snippet_ofs1); - [modif_doc] = crlf.cleanup_cr_lf(modif_doc, []); - state.showing_diff_modif_doc = modif_doc; - state.showing_diff_move_cursor = true; - estate.switch_mode(state, estate.Mode.Diff); + let text = editor.document.getText(); + let snippet_ofs0 = editor.document.offsetAt( + this.chat?.working_on_snippet_range.start + ); + let snippet_ofs1 = editor.document.offsetAt( + this.chat?.working_on_snippet_range.end + ); + let modif_doc: string = text.substring(0, snippet_ofs0) + data.value + text.substring(snippet_ofs1); + [modif_doc] = crlf.cleanup_cr_lf(modif_doc, []); + state.showing_diff_modif_doc = modif_doc; + state.showing_diff_move_cursor = true; + estate.switch_mode(state, estate.Mode.Diff); + break; + } + case "chat-question-enter-hit": { + if (!this.chat) { break; } - case "chat-question-enter-hit": { - if (!this.chat) { - break; - } - if (this.chat && data.chat_messages_backup.length < this.chat.get_messages().length) { - console.log(`oops, we need ${data.chat_messages_backup.length} messages, in chat that already added ${this.chat.get_messages().length}`); - await ChatTab.activate_from_outside( - data.chat_question, - undefined, - data.chat_attach_file, - data.chat_model, - data.chat_messages_backup, - ); - } - await this.chat.chat_post_question( + if (this.chat && data.chat_messages_backup.length < this.chat.get_messages().length) { + console.log(`oops, we need ${data.chat_messages_backup.length} messages, in chat that already added ${this.chat.get_messages().length}`); + await ChatTab.clear_and_repopulate_chat( data.chat_question, - data.chat_model, - "", + undefined, data.chat_attach_file, + data.chat_model, data.chat_messages_backup, - ); - break; + ); } - case "restore_chat": { - const chat_id = data.chat_id; - if (!chat_id) { - break; - } - let editor = vscode.window.activeTextEditor; - let chat: Chat | undefined = await this.make_sure_have_chat_history_provider().lookup_chat(chat_id); - if (!chat) { - console.log(`Chat ${chat_id} not found, cannot restore`); - break; - } - // FIXME: use ChatTab.activate_from_outside directly? - await open_chat_tab( - "", - editor, - true, - data.chat_model, - chat.messages, - chat_id, + await this.chat.post_question_and_communicate_answer( + data.chat_question, + data.chat_model, + "", + data.chat_attach_file, + data.chat_messages_backup, ); + break; + } + case "restore_chat": { + const chat_id = data.chat_id; + if (!chat_id) { break; } - case "stop-clicked": { - this.chat?.cancellationTokenSource.cancel(); + let editor = vscode.window.activeTextEditor; + let chat: Chat | undefined = await this.make_sure_have_chat_history_provider().lookup_chat(chat_id); + if (!chat) { + console.log(`Chat ${chat_id} not found, cannot restore`); break; } - // case "reset-messages": { - // if (this.chat?.messages) { - // this.chat.messages = data.messages_backup; - // this.make_sure_have_chat_history_provider().assign_messages_backup( - // this.chat?.chat_id, - // this.chat.messages, - // ); - // } - // break; - // } - case "back-from-chat": { - this.goto_main(); - this.chat = null; - } + await open_chat_tab( + "", + editor, + true, + data.chat_model, + chat.messages, + chat_id, + ); + break; + } + case "stop-clicked": { + this.chat?.cancellationTokenSource.cancel(); + break; + } + case "back-from-chat": { + this.goto_main(); + this.chat = null; + break; + } } } @@ -555,34 +545,6 @@ export class PanelWebview implements vscode.WebviewViewProvider { `; } - // private _getHtmlForHistoryWebview(webview: vscode.Webview) { - // const scriptUri = webview.asWebviewUri( - // vscode.Uri.joinPath( - // this._context.extensionUri, - // "assets", - // "chat_history.js" - // ) - // ); - // const nonce = this.getNonce(); - // return ` - // - // - // - // - // - // - - // Presets - // - // - // - // - // `; - // } - getNonce() { let text = ""; const possible =