From d64eff4d255c58c39d1a4f8a8a5587121c62d7d3 Mon Sep 17 00:00:00 2001 From: Marc McIntosh Date: Thu, 23 Nov 2023 16:54:07 +0100 Subject: [PATCH] Revert "Toolbox2 messages in thread" --- package.json | 22 ------ src/extension.ts | 15 +---- src/rconsoleCommands.ts | 65 ++++++------------ src/rconsoleProvider.ts | 145 +++++++--------------------------------- 4 files changed, 47 insertions(+), 200 deletions(-) diff --git a/package.json b/package.json index d2c878687..79dcb7595 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,6 @@ "engines": { "vscode": "^1.69.0" }, - "enabledApiProposals": [ - "contribCommentThreadAdditionalMenu" - ], "categories": [ "Programming Languages", "Snippets", @@ -142,15 +139,6 @@ }, "properties": [], "commands": [ - { - "command": "refactaicmd.sendChatToSidebar", - "title": "Open in Sidebar", - "enablement": "refactaicmd.openSidebarButtonEnabled" - }, - { - "command": "refactaicmd.closeInlineChat", - "title": "Close" - }, { "command": "refactaicmd.activateToolbox", "title": "Activate", @@ -233,16 +221,6 @@ "submenu": "refact-access-menu", "group": "z_commands" } - ], - "comments/commentThread/additionalActions": [ - { - "command": "refactaicmd.sendChatToSidebar", - "group": "inline@1" - }, - { - "command": "refactaicmd.closeInlineChat", - "group": "inline@2" - } ] }, "submenus": [ diff --git a/src/extension.ts b/src/extension.ts index 47ddf6daf..1f296e6a3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -35,7 +35,6 @@ declare global { var chat_models: string[]; var have_caps: boolean; var comment_disposables: vscode.Disposable[]; - var comment_file_uri: vscode.Uri|undefined; } async function pressed_call_chat() { @@ -58,17 +57,8 @@ async function pressed_escape() completionProvider.on_esc_pressed(); let editor = vscode.window.activeTextEditor; if (global.comment_disposables) { - let original_editor_uri = rconsoleProvider.refact_console_close(); - if (original_editor_uri !== undefined) { - // find editor - let original_editor = vscode.window.visibleTextEditors.find((e) => { - return e.document.uri === original_editor_uri; - }); - if (original_editor) { - editor = original_editor; - } - } - // don't return, remove all other things too -- we are here because Esc in the comment thread + rconsoleProvider.refact_console_close(); + return; } if (editor) { let state = estate.state_of_editor(editor, "pressed_escape"); @@ -122,7 +112,6 @@ async function code_lens_clicked(arg0: any) } if (arg0 === "APPROVE") { await interactiveDiff.like_and_accept(editor); - rconsoleProvider.refact_console_close(); } else if (arg0 === "REJECT") { await pressed_escape(); // might return to highlight } else if (arg0 === "RERUN") { diff --git a/src/rconsoleCommands.ts b/src/rconsoleCommands.ts index 5bb573128..dc5ce1b3c 100644 --- a/src/rconsoleCommands.ts +++ b/src/rconsoleCommands.ts @@ -7,12 +7,6 @@ import * as interactiveDiff from "./interactiveDiff"; import * as estate from "./estate"; -export type ThreadCallback = (role: string, answer: string) => void; -export type Messages = [string, string][]; -export type ThreadEndCallback = (messages: Messages) => void; - - - export let commands_available: { [key: string]: string } = { "shorter": "Make code shorter", "bugs": "Find and fix bugs", @@ -59,10 +53,10 @@ function get_chars(str: string): Set } export function get_hints( - msgs: Messages, + msgs: [string, string][], unfinished_text: string, selected_range: vscode.Range -): [string, string, string] { +): [string, string] { if (unfinished_text.startsWith("/")) { let cmd_score: { [key: string]: number } = {}; for (let cmd in commands_available) { @@ -78,20 +72,20 @@ export function get_hints( let text = commands_available[cmd] || ""; result += `/${cmd} ${text}
\n`; } - return [result, "Available commands:", top3[0][0]]; + return [result, "Available commands:"]; } else { if (!selected_range.isEmpty) { let lines_n = selected_range.end.line - selected_range.start.line + 1; - return [`How to change these ${lines_n} lines? Also try "explain this" or commands starting with \"/\".`, "🪄 Selected text", ""]; + return [`How to change these ${lines_n} lines? Also try "explain this" or commands starting with \"/\".`, "🪄 Selected text"]; } else { - return [`What would you like to generate? Also try commands starting with \"/\".`, "🪄 New Code", ""]; + return [`What would you like to generate? Also try commands starting with \"/\".`, "🪄 New Code"]; } } } export function initial_messages(working_on_attach_filename: string, working_on_attach_code: string) { - let messages: Messages = []; + let messages: [string, string][] = []; let single_file_json = JSON.stringify([{ "file_name": working_on_attach_filename, "file_content": working_on_attach_code, @@ -101,12 +95,10 @@ export function initial_messages(working_on_attach_filename: string, working_on_ } export async function stream_chat_without_visible_chat( - messages: Messages, + messages: [string, string][], editor: vscode.TextEditor, selected_range: vscode.Range, - cancelToken: vscode.CancellationToken, - thread_callback: ThreadCallback, - end_thread_callback: ThreadEndCallback, + cancelToken: vscode.CancellationToken ) { let state = estate.state_of_editor(editor, "invisible_chat"); if (!state) { @@ -153,9 +145,6 @@ export async function stream_chat_without_visible_chat( if (role0) { answer_role = role0; } - if(answer_role && answer) { - thread_callback(answer_role, answer); - } } } @@ -175,21 +164,11 @@ export async function stream_chat_without_visible_chat( largest_block = block; } } - - end_thread_callback(messages); - - if (largest_block) { - chatTab.diff_paste_back( - editor, - selected_range, - largest_block, - ); - } else { - let state = estate.state_of_document(editor.document); - if (state) { - await estate.switch_mode(state, estate.Mode.Normal); - } - } + chatTab.diff_paste_back( + editor, + selected_range, + largest_block, + ); } else { let state = estate.state_of_editor(editor, "streaming_end_callback"); if (state) { @@ -210,7 +189,7 @@ export async function stream_chat_without_visible_chat( )); } -function _run_command(cmd: string, doc_uri: string, messages: Messages, update_thread_callback: ThreadCallback, end_thread_callback: ThreadEndCallback) +function _run_command(cmd: string, doc_uri: string) { let text = commands_available[cmd] || ""; let editor = vscode.window.visibleTextEditors.find((editor) => { @@ -221,30 +200,24 @@ function _run_command(cmd: string, doc_uri: string, messages: Messages, update_t return; } let [official_selection, working_on_attach_code, working_on_attach_filename, code_snippet] = chatTab.attach_code_from_editor(editor); - // let messages: [string, string][] = initial_messages(working_on_attach_filename, working_on_attach_code); - const messageWithUserInput = [ - ...messages - ]; - messageWithUserInput.push(["user", "```\n" + code_snippet + "\n```\n\n" + text + "\n"]); + let messages: [string, string][] = initial_messages(working_on_attach_filename, working_on_attach_code); + messages.push(["user", "```\n" + code_snippet + "\n```\n\n" + text + "\n"]); let cancellationTokenSource = new vscode.CancellationTokenSource(); let cancellationToken = cancellationTokenSource.token; rconsoleCommands.stream_chat_without_visible_chat( - messageWithUserInput, + messages, editor, official_selection, cancellationToken, - update_thread_callback, - end_thread_callback ); } export function register_commands(): vscode.Disposable[] { let dispos = []; - for (let cmd in commands_available) { - let d = vscode.commands.registerCommand('refactaicmd.cmd_' + cmd, (doc_uri, messages: Messages, update_thread_callback: ThreadCallback, end_thread_callback: ThreadEndCallback) => { - _run_command(cmd, doc_uri, messages, update_thread_callback, end_thread_callback); + let d = vscode.commands.registerCommand('refactaicmd.cmd_' + cmd, (doc_uri) => { + _run_command(cmd, doc_uri); }); dispos.push(d); } diff --git a/src/rconsoleProvider.ts b/src/rconsoleProvider.ts index 233687c00..9a655c3b2 100644 --- a/src/rconsoleProvider.ts +++ b/src/rconsoleProvider.ts @@ -5,7 +5,7 @@ import * as sidebar from "./sidebar"; import * as chatTab from "./chatTab"; -export class MyCommentAuthorInformation implements vscode.CommentAuthorInformation { +class MyCommentAuthorInformation implements vscode.CommentAuthorInformation { name: string; iconPath?: vscode.Uri; constructor(name: string, iconPath?: vscode.Uri) { @@ -14,7 +14,7 @@ export class MyCommentAuthorInformation implements vscode.CommentAuthorInformati } } -export class MyComment implements vscode.Comment { +class MyComment implements vscode.Comment { body: vscode.MarkdownString; mode: vscode.CommentMode; author: vscode.CommentAuthorInformation; @@ -48,19 +48,7 @@ export class MyComment implements vscode.Comment { // } // } -function message_to_comment(author: string, text: string) { - return new MyComment(text, vscode.CommentMode.Preview, new MyCommentAuthorInformation(author)); -} - -// function format_messages(messages: rconsoleCommands.Messages) { -// return messages.filter(([role, _]) => { -// return role !== "context_file"; -// }).map(([author, text]) => { -// return message_to_comment(author, text); -// }); -// } - -export function refact_console_close(): vscode.Uri|undefined +export function refact_console_close() { if (global.comment_disposables) { for (let d of global.comment_disposables) { @@ -68,15 +56,11 @@ export function refact_console_close(): vscode.Uri|undefined } } global.comment_disposables = []; - let ret = global.comment_file_uri; - global.comment_file_uri = undefined; - return ret; } export async function open_refact_console_between_lines(editor: vscode.TextEditor) { refact_console_close(); - global.comment_file_uri = editor.document.uri; let [official_selection, working_on_attach_code, working_on_attach_filename, code_snippet] = chatTab.attach_code_from_editor(editor); let cc = vscode.comments.createCommentController("refactai-test", "RefactAI Test Comments"); cc.commentingRangeProvider = { @@ -89,7 +73,6 @@ export async function open_refact_console_between_lines(editor: vscode.TextEdito return Promise.resolve(); }; let my_comments: vscode.Comment[] = []; - let thread: vscode.CommentThread = cc.createCommentThread( editor.document.uri, new vscode.Range(official_selection.start.line, 0, official_selection.end.line, 0), @@ -98,42 +81,11 @@ export async function open_refact_console_between_lines(editor: vscode.TextEdito thread.canReply = true; thread.label = "Refact Console (F1)"; thread.collapsibleState = vscode.CommentThreadCollapsibleState.Expanded; - global.comment_disposables.push(thread); global.comment_disposables.push(cc); let messages: [string, string][] = rconsoleCommands.initial_messages(working_on_attach_filename, working_on_attach_code); - // TODO: - // * always operate on messages (done) - // * write a function that translates messages to comments, call it often (done) - // * split this function - - const update_thread_callback: rconsoleCommands.ThreadCallback = (author_role, answer) => { - // if (thread.canReply) { thread.canReply = false; } - const lastPost = thread.comments.length > 0 ? thread.comments[thread.comments.length - 1] : null; - const comment = message_to_comment(author_role, answer); - - if(lastPost && lastPost.author.name === author_role && lastPost instanceof MyComment) { - - const previouseComments = thread.comments.slice(0, -1); - thread.comments = [ - ...previouseComments, - comment - ]; - } else { - thread.comments = [ - ...thread.comments, - comment, - ]; - } - }; - - const end_thread_callback = (response_messages: [string, string][]) => { - messages = response_messages; - vscode.commands.executeCommand("setContext", "refactaicmd.openSidebarButtonEnabled", true); - }; - let text = ""; let hint_debounce: NodeJS.Timeout|undefined; let did1 = vscode.workspace.onDidChangeTextDocument(async e => { if (e.document.uri.scheme !== "comment") { @@ -142,51 +94,25 @@ export async function open_refact_console_between_lines(editor: vscode.TextEdito if (my_comments.length === 0) { return; } - text = e.document.getText(); + let text = e.document.getText(); console.log("onDidChangeTextDocument", text); // let y = e.document.fileName; // "/commentinput-8d64259c-9607-4048-a9dc-a73f621e750d-1.md" - let top1 = ""; - if (messages.length === 1) { - let hint, author; - [hint, author, top1] = rconsoleCommands.get_hints(messages, text, official_selection); - my_comments[0] = message_to_comment(author, text); - if (hint_debounce) { - clearTimeout(hint_debounce); - } - hint_debounce = setTimeout(async () => { - // this is a heavy operation, changes the layout and lags the UI - thread.comments = my_comments; - }, 200); - } + let [hint, author] = rconsoleCommands.get_hints(messages, text, official_selection); + my_comments[0] = new MyComment(hint, vscode.CommentMode.Preview, new MyCommentAuthorInformation(author)); await vscode.commands.executeCommand('setContext', 'refactcx.runEsc', true); - if (top1 && text.match(/\/[a-zA-Z0-9_]+[\t ]$/)) { - let comment_editor = vscode.window.visibleTextEditors.find((e1) => { - return e1.document.uri === e.document.uri; - }); - if (comment_editor) { - await comment_editor.edit(edit => { - edit.delete(new vscode.Range(0, 0, 1000, 0)); - edit.insert(new vscode.Position(0, 0), "/" + top1); - }); - } - return; + if (hint_debounce) { + clearTimeout(hint_debounce); } - + hint_debounce = setTimeout(async () => { + // this is a heavy operation, changes the layout and lags the UI + thread.comments = my_comments; + }, 200); if (text.includes("\n")) { - let comment_editor = vscode.window.visibleTextEditors.find((e1) => { - return e1.document.uri === e.document.uri; - }); let first_line = text.split("\n")[0]; if (first_line.startsWith("/")) { for (let cmd in rconsoleCommands.commands_available) { if (first_line.startsWith("/" + cmd)) { - vscode.commands.executeCommand("setContext", "refactaicmd.openSidebarButtonEnabled", false); - if (comment_editor) { - await comment_editor.edit(edit => { - edit.delete(new vscode.Range(0, 0, 1000, 0)); - }); - } - activate_cmd(cmd, editor, messages, update_thread_callback, end_thread_callback); + activate_cmd(cmd, editor); return; } } @@ -207,40 +133,24 @@ export async function open_refact_console_between_lines(editor: vscode.TextEdito await vscode.commands.executeCommand('setContext', 'refactcx.runEsc', true); function initial_message() { - let [hint, author, _top1] = rconsoleCommands.get_hints(messages, "", official_selection); - const hint_comment = message_to_comment(author, hint); - my_comments.push(hint_comment); + let [hint, author] = rconsoleCommands.get_hints(messages, "", official_selection); + my_comments.push(new MyComment(hint, vscode.CommentMode.Preview, new MyCommentAuthorInformation(author))); thread.comments = my_comments; } // This trick puts cursor into the input box, possibly VS thinks the only use for // the thread is to ask user if there are no messages. But then we add a message. await new Promise(resolve => setTimeout(resolve, 100)); initial_message(); - - - - global.comment_disposables.push(vscode.commands.registerCommand("refactaicmd.sendChatToSidebar", async (e) => { - let question = "```\n" + code_snippet + "\n```\n\n" + text; - activate_chat(messages, question, editor, false); - })); - - global.comment_disposables.push(vscode.commands.registerCommand("refactaicmd.closeInlineChat", (...args) => { - global.comment_disposables.forEach(disposable => { - disposable.dispose(); - }); - global.comment_disposables = []; - })); } -function activate_cmd(cmd: string, editor: vscode.TextEditor, messages: rconsoleCommands.Messages, update_thread_callback: rconsoleCommands.ThreadCallback, end_thread_callback: rconsoleCommands.ThreadEndCallback) +function activate_cmd(cmd: string, editor: vscode.TextEditor) { console.log(`activate_cmd refactaicmd.cmd_${cmd}`); - - vscode.commands.executeCommand("setContext", "refactaicmd.runningChat", true); - vscode.commands.executeCommand("refactaicmd.cmd_" + cmd, editor.document.uri.toString(), messages, update_thread_callback, end_thread_callback); + refact_console_close(); + vscode.commands.executeCommand("refactaicmd.cmd_" + cmd, editor.document.uri.toString()); } -async function activate_chat(messages: [string, string][], question: string, editor: vscode.TextEditor, new_question = true) +async function activate_chat(messages: [string, string][], question: string, editor: vscode.TextEditor) { console.log(`activate_chat question.length=${question.length}`); refact_console_close(); @@ -261,14 +171,11 @@ async function activate_chat(messages: [string, string][], question: string, edi if (!chat) { return; } - if(new_question) { - await chat.post_question_and_communicate_answer( - question, - "", - "", - false, - messages, - ); - } - + await chat.post_question_and_communicate_answer( + question, + "", + "", + false, + messages, + ); }