Merge pull request #5465 from continuedev/dallin/edit-rules

include rules in edit requests
This commit is contained in:
Dallin Romney 2025-05-02 17:05:34 -07:00 • committed by GitHub
commit f22dd925af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 74 additions and 53 deletions

View file

@ -63,32 +63,50 @@ import { llmStreamChat } from "./llm/streamChat";
import type { FromCoreProtocol, ToCoreProtocol } from "./protocol";
import type { IMessenger, Message } from "./protocol/messenger";
// This function is used for jetbrains inline edit and apply
async function* streamDiffLinesGenerator(
configHandler: ConfigHandler,
abortedMessageIds: Set<string>,
msg: Message<ToCoreProtocol["streamDiffLines"][0]>,
): AsyncGenerator<DiffLine> {
const data = msg.data;
const {
highlighted,
prefix,
suffix,
input,
language,
modelTitle,
includeRulesInSystemMessage,
} = msg.data;
const { config } = await configHandler.loadConfig();
if (!config) {
throw new Error("Failed to load config");
}
const llm = config.selectedModelByRole.chat;
// Title can be an edit, chat, or apply model
// Fall back to chat
const llm =
config.modelsByRole.edit.find((m) => m.title === modelTitle) ??
config.modelsByRole.apply.find((m) => m.title === modelTitle) ??
config.modelsByRole.chat.find((m) => m.title === modelTitle) ??
config.selectedModelByRole.chat;
if (!llm) {
throw new Error("No chat model selected");
throw new Error("No model selected");
}
// rules included for edit, NOT apply
const rules = includeRulesInSystemMessage ? config.rules : undefined;
for await (const diffLine of streamDiffLines({
highlighted: data.highlighted,
prefix: data.prefix,
suffix: data.suffix,
highlighted,
prefix,
suffix,
llm,
rules: config.rules,
input: data.input,
language: data.language,
rulesToInclude: rules,
input,
language,
onlyOneInsertion: false,
overridePrompt: undefined,
})) {

View file

@ -24,7 +24,7 @@ import { findLast } from "../util/findLast";
import { Telemetry } from "../util/posthog";
import { recursiveStream } from "./recursiveStream";
function constructPrompt(
function constructEditPrompt(
prefix: string,
highlighted: string,
suffix: string,
@ -67,7 +67,7 @@ export async function* streamDiffLines({
language,
onlyOneInsertion,
overridePrompt,
rules,
rulesToInclude,
}: {
prefix: string;
highlighted: string;
@ -77,7 +77,7 @@ export async function* streamDiffLines({
language: string | undefined;
onlyOneInsertion: boolean;
overridePrompt: ChatMessage[] | undefined;
rules: RuleWithSource[];
rulesToInclude: RuleWithSource[] | undefined;
}): AsyncGenerator<DiffLine> {
void Telemetry.capture(
"inlineEdit",
@ -100,38 +100,38 @@ export async function* streamDiffLines({
oldLines = [];
}
// Defaults to creating an edit prompt
// For apply can be overridden with simply apply prompt
let prompt =
overridePrompt ??
constructPrompt(prefix, highlighted, suffix, llm, input, language);
constructEditPrompt(prefix, highlighted, suffix, llm, input, language);
// Rules will be included with edit prompt
// Rules can be included with edit prompt
// If any rules are present this will result in using chat instead of legacy completion
const lastUserMessage =
typeof prompt === "string"
? ({
role: "user",
content: prompt,
} as UserChatMessage)
: (findLast(
prompt,
(msg) => msg.role === "user" || msg.role === "tool",
) as UserChatMessage | ToolResultChatMessage | undefined);
const systemMessage = getSystemMessageWithRules({
rules,
userMessage: lastUserMessage,
baseSystemMessage: undefined,
});
const systemMessage = rulesToInclude
? getSystemMessageWithRules({
rules: rulesToInclude,
userMessage:
typeof prompt === "string"
? ({
role: "user",
content: prompt,
} as UserChatMessage)
: (findLast(
prompt,
(msg) => msg.role === "user" || msg.role === "tool",
) as UserChatMessage | ToolResultChatMessage | undefined),
baseSystemMessage: undefined,
})
: undefined;
if (systemMessage) {
if (typeof prompt === "string") {
// Removed system prompt because it was causing it to be used for Apply
// We should bring it back only for Edit
prompt = [
// {
// role: "system",
// content: systemMessage,
// },
{
role: "system",
content: systemMessage,
},
{
role: "user",
content: prompt,
@ -142,10 +142,10 @@ export async function* streamDiffLines({
if (curSysMsg) {
curSysMsg.content = systemMessage + "\n\n" + curSysMsg.content;
} else {
// prompt.unshift({
// role: "system",
// content: systemMessage,
// });
prompt.unshift({
role: "system",
content: systemMessage,
});
}
}
}

View file

@ -150,6 +150,7 @@ export type ToCoreFromIdeOrWebviewProtocol = {
input: string;
language: string | undefined;
modelTitle: string | undefined;
includeRulesInSystemMessage: boolean;
},
AsyncGenerator<DiffLine>,
];

View file

@ -596,7 +596,7 @@ class IdeProtocolClient(
diffStreamService.register(diffStreamHandler, editor)
diffStreamHandler.streamDiffLinesToEditor(
prompt, prefix, highlighted, suffix, llmTitle
prompt, prefix, highlighted, suffix, llmTitle, false
)
respond(null)

View file

@ -87,7 +87,7 @@ class DiffStreamHandler(
}
fun streamDiffLinesToEditor(
input: String, prefix: String, highlighted: String, suffix: String, modelTitle: String
input: String, prefix: String, highlighted: String, suffix: String, modelTitle: String, includeRulesInSystemMessage: Boolean
) {
isRunning = true
this.sendUpdate("streaming")
@ -96,7 +96,7 @@ class DiffStreamHandler(
val virtualFile = getVirtualFile()
continuePluginService.coreMessenger?.request(
"streamDiffLines", createRequestParams(input, prefix, highlighted, suffix, virtualFile, modelTitle), null
"streamDiffLines", createRequestParams(input, prefix, highlighted, suffix, virtualFile, modelTitle, includeRulesInSystemMessage), null
) { response ->
if (!isRunning) return@request
@ -274,7 +274,8 @@ class DiffStreamHandler(
highlighted: String,
suffix: String,
virtualFile: VirtualFile?,
modelTitle: String
modelTitle: String,
includeRulesInSystemMessage: Boolean
): Map<String, Any?> {
return mapOf(
"input" to input,
@ -282,7 +283,8 @@ class DiffStreamHandler(
"highlighted" to highlighted,
"suffix" to suffix,
"language" to virtualFile?.fileType?.name,
"modelTitle" to modelTitle
"modelTitle" to modelTitle,
"includeRulesInSystemMessage" to includeRulesInSystemMessage,
)
}

View file

@ -222,7 +222,7 @@ fun openInlineEdit(project: Project?, editor: Editor) {
val selectedModelStrippedOfCaret = (comboBoxRef.get().selectedItem as String).removeSuffix(DOWN_ARROW)
customPanelRef.get().enter()
diffStreamHandler.streamDiffLinesToEditor(
textArea.text, prefix, highlighted, suffix, selectedModelStrippedOfCaret
textArea.text, prefix, highlighted, suffix, selectedModelStrippedOfCaret, true
)
}

View file

@ -181,7 +181,7 @@ export class ApplyManager {
range: rangeToApplyTo,
newCode: text,
toolCallId,
rules: config.rules,
rulesToInclude: undefined, // No rules for apply
});
}
}

View file

@ -226,7 +226,7 @@ const getCommandsMap: (
llm,
onlyOneInsertion,
range,
rules: config.rules,
rulesToInclude: config.rules,
});
}

View file

@ -285,7 +285,7 @@ export class VerticalDiffManager {
range,
newCode,
toolCallId,
rules,
rulesToInclude,
}: {
input: string;
llm: ILLM;
@ -295,7 +295,7 @@ export class VerticalDiffManager {
range?: vscode.Range;
newCode?: string;
toolCallId?: string;
rules: RuleWithSource[];
rulesToInclude: undefined | RuleWithSource[];
}): Promise<string | undefined> {
vscode.commands.executeCommand("setContext", "continue.diffVisible", true);
@ -448,7 +448,7 @@ export class VerticalDiffManager {
prefix,
suffix,
llm,
rules,
rulesToInclude,
input,
language: getMarkdownLanguageTagForFile(fileUri),
onlyOneInsertion: !!onlyOneInsertion,

View file

@ -226,7 +226,7 @@ export class VsCodeMessenger {
new vscode.Position(start.line, start.character),
new vscode.Position(end.line, end.character),
),
rules: config.rules,
rulesToInclude: config.rules,
});
return fileAfterEdit;
});

View file

@ -331,7 +331,7 @@ export class QuickEdit {
llm: model,
quickEdit: this.previousInput,
range: this.range,
rules,
rulesToInclude: rules,
});
}