|
| 1 | +package de.tuda.stg.securecoder.engine.file.edit |
| 2 | + |
| 3 | +import de.tuda.stg.securecoder.engine.file.edit.Changes.SearchedText |
| 4 | +import de.tuda.stg.securecoder.engine.llm.ChatMessage |
| 5 | +import de.tuda.stg.securecoder.engine.llm.ChatMessage.Role |
| 6 | +import de.tuda.stg.securecoder.engine.llm.LlmClient |
| 7 | +import de.tuda.stg.securecoder.engine.llm.LLMDescription |
| 8 | +import de.tuda.stg.securecoder.engine.llm.chatStructured |
| 9 | +import de.tuda.stg.securecoder.filesystem.FileSystem |
| 10 | +import de.tuda.stg.securecoder.engine.llm.ChatExchange |
| 11 | +import kotlinx.serialization.Serializable |
| 12 | +import kotlinx.serialization.encodeToString |
| 13 | +import kotlinx.serialization.json.Json |
| 14 | +import kotlin.collections.plusAssign |
| 15 | + |
| 16 | +class StructuredEditFilesLlmWrapper( |
| 17 | + private val llmClient: LlmClient |
| 18 | +) { |
| 19 | + //TODO path => **uri** ; EditFilesLlmWrapper should be separate from the filesystem implementation |
| 20 | + private val prompt = """ |
| 21 | + Your task it is to produce code. The agent will just parse the code you produce. So dont do a extensive review in your final answer! |
| 22 | + |
| 23 | + It's acceptable to add multiple *search/REPLACE* sections if you need to change multiple parts of the file. |
| 24 | + To create a file: search must be empty and replace must contain the entire file content |
| 25 | + Each *search* pattern must match the existing source code exactly once, line for line, character for character, including all comments, docstrings, etc. |
| 26 | + Do not use a part of the line as *search* pattern. You must use full lines. |
| 27 | + Include enough lines to make code inside *search* pattern uniquely identifiable. A *search* pattern that produces multiple matches in the source code will be rejected as an error. |
| 28 | + Do not add backslashes to escape special characters. Write the code exactly as it should appear in the intended programming language. |
| 29 | + Do not use git diff style (+ and - at the beginning of the line) for *search/REPLACE* blocks. |
| 30 | + Do not use line numbers in *search/REPLACE* blocks. Do not enclose the *search/REPLACE* block or any of its components in triple quotes. Use only tags to separate the parameters. |
| 31 | + Do not use the same value for *search* and *REPLACE* parameters, as this will make no changes. |
| 32 | + |
| 33 | + If you need to edit a file again after making changes, use the latest version of the code that includes all your modifications applied during **current session**. |
| 34 | + """.trimIndent() |
| 35 | + |
| 36 | + |
| 37 | + suspend fun chat( |
| 38 | + messages: List<ChatMessage>, |
| 39 | + fileSystem: FileSystem, |
| 40 | + params: LlmClient.GenerationParams = LlmClient.GenerationParams(), |
| 41 | + onParseError: suspend (parseErrors: List<String>, llm: ChatExchange) -> Unit = { _, _ -> }, |
| 42 | + attempts: Int = 3 |
| 43 | + ): ChatResult { |
| 44 | + val messages = messages.toMutableList() |
| 45 | + appendPromptToLastSystem(messages) |
| 46 | + repeat(attempts) { |
| 47 | + val llmInput = messages.toList() |
| 48 | + val structured = llmClient.chatStructured<StructuredEdits>(llmInput, params) |
| 49 | + messages += ChatMessage(Role.Assistant, Json.encodeToString(structured)) |
| 50 | + when (val result = validateAndConvert(structured, fileSystem)) { |
| 51 | + is ParseResult.Ok -> return ChatResult(messages, result.value) |
| 52 | + is ParseResult.Err -> { |
| 53 | + messages += ChatMessage(Role.User, result.buildMessage()) |
| 54 | + onParseError(result.messages, ChatExchange(llmInput, messages.last().content)) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return ChatResult(messages, null) |
| 59 | + } |
| 60 | + |
| 61 | + data class ChatResult(val messages: List<ChatMessage>, val changes: Changes?) { |
| 62 | + fun changesMessage() = messages.last { it.role == Role.Assistant } |
| 63 | + } |
| 64 | + |
| 65 | + sealed interface ParseResult { |
| 66 | + data class Ok(val value: Changes) : ParseResult |
| 67 | + data class Err(val messages: List<String>) : ParseResult { |
| 68 | + fun buildMessage() = buildString { |
| 69 | + appendLine("Your previous output could not be applied.") |
| 70 | + appendLine("It violated the required format.") |
| 71 | + appendLine("Errors:") |
| 72 | + messages.forEach { appendLine(it) } |
| 73 | + appendLine("Respond again with ONLY edit blocks that strictly follow the rules. Do NOT include prose, markdown, or explanations.") |
| 74 | + appendLine("IMPORTANT: Resend the COMPLETE set of edits you intend to apply from your previous message") |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private suspend fun validateAndConvert(structured: StructuredEdits, fileSystem: FileSystem): ParseResult { |
| 80 | + val results = mutableListOf<Changes.SearchReplace>() |
| 81 | + val allErrors = mutableListOf<String>() |
| 82 | + if (structured.edits.isEmpty()) { |
| 83 | + allErrors += "No edits provided. Provide at least one edit block." |
| 84 | + return ParseResult.Err(allErrors) |
| 85 | + } |
| 86 | + for (e in structured.edits) { |
| 87 | + val file = e.filePath.trim() |
| 88 | + val searchPart = e.search |
| 89 | + val replacePart = e.replace |
| 90 | + if (file.isEmpty()) { |
| 91 | + allErrors += "`filePath` should not be empty" |
| 92 | + continue |
| 93 | + } |
| 94 | + if (searchPart == replacePart) { |
| 95 | + allErrors += "`search` and `replace` parameters are the same" |
| 96 | + continue |
| 97 | + } |
| 98 | + val replace = Changes.SearchReplace(file, SearchedText(searchPart), replacePart) |
| 99 | + val content = fileSystem.getFile(file)?.content() |
| 100 | + val match = ApplyChanges.match(content, replace.searchedText) |
| 101 | + if (match is Matcher.MatchResult.Error) { |
| 102 | + allErrors += ApplyChanges.buildErrorMessage(file, searchPart, match) |
| 103 | + continue |
| 104 | + } |
| 105 | + results += replace |
| 106 | + } |
| 107 | + if (results.isEmpty()) return ParseResult.Err(allErrors) |
| 108 | + return ParseResult.Ok(Changes(results)) |
| 109 | + } |
| 110 | + |
| 111 | + private fun appendPromptToLastSystem(messages: MutableList<ChatMessage>) { |
| 112 | + val lastSystemIndex = messages.indexOfLast { it.role == Role.System } |
| 113 | + if (lastSystemIndex >= 0) { |
| 114 | + val existing = messages[lastSystemIndex] |
| 115 | + val combined = "${existing.content}\n\n$prompt\n\nRespond ONLY with a JSON object that matches the provided schema. Do not include explanations." |
| 116 | + messages[lastSystemIndex] = ChatMessage(Role.System, combined) |
| 117 | + } else { |
| 118 | + messages += ChatMessage(Role.System, "$prompt\n\nRespond ONLY with a JSON object that matches the provided schema. Do not include explanations.") |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Serializable |
| 123 | + data class StructuredEdits( |
| 124 | + @LLMDescription("List of edit operations to apply") |
| 125 | + val edits: List<EditOperation> |
| 126 | + ) |
| 127 | + |
| 128 | + @Serializable |
| 129 | + data class EditOperation( |
| 130 | + @LLMDescription("The full **uri** of the file that will be modified") |
| 131 | + val filePath: String, |
| 132 | + @LLMDescription("A continuous, yet concise block of lines to search for in the existing source code (*search* pattern). If this section is empty, the lines from `replace` will be added to the end of the file.") |
| 133 | + val search: String, |
| 134 | + @LLMDescription("The lines to replace the existing code found using `search`. If this section is empty, the lines specified in `search` will be removed.") |
| 135 | + val replace: String, |
| 136 | + ) |
| 137 | +} |
0 commit comments