From 2593a4e3e619130ed214f20d5fa7e181ae76a297 Mon Sep 17 00:00:00 2001 From: Test Date: Tue, 5 Nov 2024 10:35:42 -0800 Subject: [PATCH] Update multifileEdit.ts --- core/commands/slash/multifileEdit.ts | 50 +++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/core/commands/slash/multifileEdit.ts b/core/commands/slash/multifileEdit.ts index d045e0a2f..797f27bf2 100644 --- a/core/commands/slash/multifileEdit.ts +++ b/core/commands/slash/multifileEdit.ts @@ -1,13 +1,43 @@ import { SlashCommand } from "../.."; +import { getFullyQualifiedPath } from "../../../extensions/vscode/src/util/util"; import { stripImages } from "../../llm/images"; const MultiFileEditSlashCommand: SlashCommand = { name: "multifile-edit", description: "Edit multiple files in the codebase at once", - run: async function* ({ llm, contextItems, input }) { - const filesToEditStr = contextItems.map((item) => item.content).join("\n"); + run: async function* ({ llm, contextItems, selectedCode, input, ide }) { + const selectedCodeStr = ( + await Promise.all( + selectedCode.map(async (item) => { + // Split the range string from the filename, e.g. `filename.ts (1-2) -> [filename.ts, (1-2)]` + const [filepath, rangeStr] = item.filepath.split(" "); + const fullPath = getFullyQualifiedPath(filepath); - const content = createPrompt(filesToEditStr, input); + if (!fullPath) { + return ""; + } + + const codeStr = await ide.readRangeInFile(fullPath, item.range); + + // Include the filepath so the model can map it correctly with codeblock outputs + return `\`\`\`${fullPath} ${rangeStr}\n${codeStr}\`\`\``; + }), + ) + ).join("\n"); + + const fileContextItemsStr = contextItems + .filter((item) => item.uri?.type === "file") + .map((item) => item.content) + .join("\n"); + + const filesToEditStr = fileContextItemsStr + "\n" + selectedCodeStr; + + const additionalContextStr = contextItems + .filter((item) => item.uri?.type !== "file") + .map((item) => `${item.description}: ${item.content}`) + .join("\n"); + + const content = createPrompt(filesToEditStr, additionalContextStr, input); for await (const chunk of llm.streamChat([{ role: "user", content }])) { yield stripImages(chunk.content); @@ -15,7 +45,11 @@ const MultiFileEditSlashCommand: SlashCommand = { }, }; -function createPrompt(filesToEditStr: string, input: string): string { +function createPrompt( + filesToEditStr: string, + additionalContextStr: string, + input: string, +): string { return ` You are an AI assistant designed to help software engineers make multi-file edits in their codebase. Your task is to generate the necessary code changes for multiple files based on the engineer's request. Follow these guidelines: @@ -54,12 +88,18 @@ Now, let's make the changes based on the engineer's request: ${input} -Here are the files to edit: +Here are the files to base your edits on: ${filesToEditStr} +And here is any additional context the engineer has provided: + + +${additionalContextStr} + + Please provide the multi-file edit details based on the engineer's request. `; }