diff --git a/package.json b/package.json index cd49293b..10034932 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "fetch-h2": "^3.0.2", "json5": "^2.2.3", "marked": "^4.0.8", - "refact-chat-js": "~v2.0.3-alpha.13", + "refact-chat-js": "~v2.0.3-alpha.14", "uuid": "^9.0.1", "vscode-languageclient": "^7.0.0" }, diff --git a/src/codeLens.ts b/src/codeLens.ts index 6172d0be..e9725597 100644 --- a/src/codeLens.ts +++ b/src/codeLens.ts @@ -9,6 +9,8 @@ import { type ChatMessage, type ToolUse, setInputValue, + isUserMessage, + UserMessage, } from "refact-chat-js/dist/events"; @@ -107,24 +109,94 @@ export class LensProvider implements vscode.CodeLensProvider } } -const sendCodeLensToChat = (messages: {content: string; role: string;}[], relative_path: string, text: string, auto_submit: boolean = false) => { - if (!global || !global.side_panel || !global.side_panel._view) { +const sendCodeLensToChat = ( + messages: ChatMessage[], + relative_path: string, + text: string, + auto_submit: boolean = false +) => { + if (!global?.side_panel?._view || !messages) { return; } + const firstMessage = messages[0]; + const isOnlyOneUserMessage = messages.length === 1 && isUserMessage(firstMessage); const cursor = vscode.window.activeTextEditor?.selection.active.line ?? null; - const messageBlock = messages.find((message: {content: string; role: string;}) => message.role === "user")?.content + if (!isOnlyOneUserMessage) { + const formattedMessages = formatMultipleMessagesForCodeLens(messages, relative_path, cursor, text); + postMessageToWebview({ + messages: formattedMessages, send_immediately: auto_submit + }); + return; + } + + const messageBlock = createMessageBlock(firstMessage, relative_path, cursor, text); + postMessageToWebview({ + value: messageBlock, send_immediately: auto_submit + }); +}; + +const postMessageToWebview = ({messages, value, send_immediately}: {messages?: ChatMessage[], value?: string, send_immediately: boolean}) => { + if (!global.side_panel?._view) { + return; + }; + + const eventMessage = setInputValue({ + messages, + value, + send_immediately + }); + global.side_panel._view.webview.postMessage(eventMessage); +}; + +const replaceVariablesInText = ( + text: string, + relative_path: string, + cursor: number | null, + code_selection: string +) => { + return text .replace("%CURRENT_FILE%", relative_path) - .replace("%CURSOR_LINE%", cursor ? (cursor + 1).toString() : "") - .replace("%CODE_SELECTION%", text); + .replace('%CURSOR_LINE%', cursor ? (cursor + 1).toString() : '') + .replace("%CODE_SELECTION%", code_selection) + .replace("%PROMPT_EXPLORATION_TOOLS%", ''); +}; - // TODO: send auto_submit somehow? - const message = setInputValue({ - value: messageBlock ? messageBlock : text, - send_immediately: auto_submit +const createMessageBlock = ( + message: UserMessage, + relative_path: string, + cursor: number | null, + text: string +) => { + if (typeof message.content === 'string') { + return replaceVariablesInText(message.content, relative_path, cursor, text); + } else { + return message.content.map(content => { + if (('type' in content) && content.type === 'text') { + return replaceVariablesInText(content.text, relative_path, cursor, text); + } + }).join("\n"); + } +}; + +const formatMultipleMessagesForCodeLens = ( + messages: ChatMessage[], + relative_path: string, + cursor: number | null, + text: string +) => { + return messages.map(message => { + if (isUserMessage(message)) { + if (typeof message.content === 'string') { + return { + ...message, + content: replaceVariablesInText(message.content, relative_path, cursor, text) + }; + } + } + return message; }); - global.side_panel._view.webview.postMessage(message); }; export async function code_lens_execute(code_lens: string, range: any) { @@ -134,7 +206,7 @@ export async function code_lens_execute(code_lens: string, range: any) { if (custom_code_lens) { const auto_submit = custom_code_lens[code_lens]["auto_submit"]; const new_tab = custom_code_lens[code_lens]["new_tab"]; - let messages: {content: string; role: string;}[] = custom_code_lens[code_lens]["messages"]; + let messages: ChatMessage[] = custom_code_lens[code_lens]["messages"]; const start_of_line = new vscode.Position(range.start.line, 0); const end_of_line = new vscode.Position(range.end.line + 1, 0);