fix: Move Quick Refactor agent to toml file

This commit is contained in:
Petr Mironychev
2026-06-29 00:27:01 +02:00
parent 755263c4de
commit e200278f9a
11 changed files with 120 additions and 48 deletions

View File

@@ -185,7 +185,7 @@ void QuickRefactorHandler::prepareAndSendRequest(
}
session->systemPrompt()->setLayer(
QStringLiteral("refactor"), buildSystemPrompt(editor, range));
QStringLiteral("refactor"), buildContextLayer(editor, range));
client->setTransferTimeout(
static_cast<int>(Settings::generalSettings().requestTimeout() * 1000));
@@ -221,7 +221,7 @@ void QuickRefactorHandler::prepareAndSendRequest(
m_activeRequests[requestId] = {QJsonObject{{"id", requestId}}, session};
}
QString QuickRefactorHandler::buildSystemPrompt(
QString QuickRefactorHandler::buildContextLayer(
TextEditor::TextEditorWidget *editor, const Utils::Text::Range &range)
{
Q_UNUSED(range)
@@ -306,29 +306,20 @@ QString QuickRefactorHandler::buildSystemPrompt(
taggedContent = contextBefore + "<cursor>" + contextAfter;
}
QString systemPrompt = Context::EnvBlockFormatter::formatFile(
QString contextLayer = Context::EnvBlockFormatter::formatFile(
{documentInfo.filePath, documentInfo.mimeType});
systemPrompt += "\n# Code Context with Position Markers\n" + taggedContent;
contextLayer += "\n# Code Context with Position Markers\n" + taggedContent;
systemPrompt += "\n\n# Output Requirements\n## What to Generate:";
systemPrompt += cursor.hasSelection()
contextLayer += "\n\n# What to Generate:";
contextLayer += cursor.hasSelection()
? "\n- Generate ONLY the code that should REPLACE the selected text between "
"<selection_start> and <selection_end> markers"
"\n- Your output will completely replace the selected code"
: "\n- Generate ONLY the code that should be INSERTED at the <cursor> position"
"\n- Your output will be inserted at the cursor location";
systemPrompt += "\n\n## Formatting Rules:"
"\n- Output ONLY the code itself, without ANY explanations or descriptions"
"\n- Do NOT include markdown code blocks (no ```, no language tags)"
"\n- Do NOT add comments explaining what you changed"
"\n- Do NOT repeat existing code, be precise with context"
"\n- Do NOT send in answer <cursor> or </cursor> and other tags"
"\n- The output must be ready to insert directly into the editor as-is";
systemPrompt += "\n\n## Indentation and Whitespace:";
QString indentNote;
if (cursor.hasSelection()) {
QTextBlock startBlock = documentInfo.document->findBlock(cursor.selectionStart());
int leadingSpaces = 0;
@@ -338,12 +329,12 @@ QString QuickRefactorHandler::buildSystemPrompt(
else break;
}
if (leadingSpaces > 0) {
systemPrompt += QString("\n- CRITICAL: The code to replace starts with %1 spaces of indentation"
"\n- Your output MUST start with exactly %1 spaces (or equivalent tabs)"
"\n- Each line in your output must maintain this base indentation")
.arg(leadingSpaces);
indentNote = QString("\n- CRITICAL: The code to replace starts with %1 spaces of indentation"
"\n- Your output MUST start with exactly %1 spaces (or equivalent tabs)"
"\n- Each line in your output must maintain this base indentation")
.arg(leadingSpaces);
}
systemPrompt += "\n- PRESERVE all indentation from the original code";
indentNote += "\n- PRESERVE all indentation from the original code";
} else {
QTextBlock block = documentInfo.document->findBlock(cursorPos);
QString lineText = block.text();
@@ -354,26 +345,20 @@ QString QuickRefactorHandler::buildSystemPrompt(
else break;
}
if (leadingSpaces > 0) {
systemPrompt += QString("\n- CRITICAL: Current line has %1 spaces of indentation"
"\n- If generating multiline code, EVERY line must start with at least %1 spaces"
"\n- If generating single-line code, it will be inserted inline (no indentation needed)")
.arg(leadingSpaces);
indentNote = QString("\n- CRITICAL: Current line has %1 spaces of indentation"
"\n- If generating multiline code, EVERY line must start with at least %1 spaces"
"\n- If generating single-line code, it will be inserted inline (no indentation needed)")
.arg(leadingSpaces);
}
}
systemPrompt += "\n- Use the same indentation style (spaces or tabs) as the surrounding code"
"\n- Maintain consistent indentation for nested blocks"
"\n- Do NOT remove or reduce the base indentation level"
"\n\n## Code Style:"
"\n- Match the coding style of the surrounding code (naming, spacing, braces, etc.)"
"\n- Preserve the original code structure when possible"
"\n- Only change what is necessary to fulfill the user's request";
if (!indentNote.isEmpty())
contextLayer += "\n\n## Indentation:" + indentNote;
if (Settings::quickRefactorSettings().useOpenFilesInQuickRefactor()) {
systemPrompt += "\n\n" + m_contextManager.openedFilesContext({documentInfo.filePath});
contextLayer += "\n\n" + m_contextManager.openedFilesContext({documentInfo.filePath});
}
return systemPrompt;
return contextLayer;
}
void QuickRefactorHandler::cancelRequest()