|
| 1 | +import Foundation |
| 2 | +import Parsing |
| 3 | + |
| 4 | +protocol RawSuggestionPostProcessingStrategy { |
| 5 | + func postProcess(rawSuggestion: String, infillPrefix: String, suffix: [String]) -> String |
| 6 | +} |
| 7 | + |
| 8 | +extension RawSuggestionPostProcessingStrategy { |
| 9 | + func removeTrailingNewlinesAndWhitespace(from string: String) -> String { |
| 10 | + var text = string[...] |
| 11 | + while let last = text.last, last.isNewline || last.isWhitespace { |
| 12 | + text = text.dropLast(1) |
| 13 | + } |
| 14 | + return String(text) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +struct DefaultRawSuggestionPostProcessingStrategy: RawSuggestionPostProcessingStrategy { |
| 19 | + let openingCodeTag: String |
| 20 | + let closingCodeTag: String |
| 21 | + |
| 22 | + func postProcess(rawSuggestion: String, infillPrefix: String, suffix: [String]) -> String { |
| 23 | + var suggestion = extractSuggestion(from: rawSuggestion) |
| 24 | + removePrefix(from: &suggestion, infillPrefix: infillPrefix) |
| 25 | + removeSuffix(from: &suggestion, suffix: suffix) |
| 26 | + return removeTrailingNewlinesAndWhitespace(from: infillPrefix + suggestion) |
| 27 | + } |
| 28 | + |
| 29 | + func extractSuggestion(from response: String) -> String { |
| 30 | + let escapedMarkdownCodeBlock = removeLeadingAndTrailingMarkdownCodeBlockMark(from: response) |
| 31 | + let escapedTags = extractEnclosingSuggestion( |
| 32 | + from: escapedMarkdownCodeBlock, |
| 33 | + openingTag: openingCodeTag, |
| 34 | + closingTag: closingCodeTag |
| 35 | + ) |
| 36 | + |
| 37 | + return escapedTags |
| 38 | + } |
| 39 | + |
| 40 | + func removePrefix(from suggestion: inout String, infillPrefix: String) { |
| 41 | + if suggestion.hasPrefix(infillPrefix) { |
| 42 | + suggestion.removeFirst(infillPrefix.count) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Window-mapping the lines in suggestion and the suffix to remove the common suffix. |
| 47 | + func removeSuffix(from suggestion: inout String, suffix: [String]) { |
| 48 | + let suggestionLines = suggestion.breakLines(appendLineBreakToLastLine: true) |
| 49 | + if let last = suggestionLines.last, let lastIndex = suffix.firstIndex(of: last) { |
| 50 | + var i = lastIndex - 1 |
| 51 | + var j = suggestionLines.endIndex - 2 |
| 52 | + while i >= 0, j >= 0, suffix[i] == suggestionLines[j] { |
| 53 | + i -= 1 |
| 54 | + j -= 1 |
| 55 | + } |
| 56 | + if i < 0 { |
| 57 | + let endIndex = max(j, 0) |
| 58 | + suggestion = suggestionLines[...endIndex].joined() |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Extract suggestions that is enclosed in tags. |
| 64 | + fileprivate func extractEnclosingSuggestion( |
| 65 | + from response: String, |
| 66 | + openingTag: String, |
| 67 | + closingTag: String |
| 68 | + ) -> String { |
| 69 | + let case_openingTagAtTheStart_parseEverythingInsideTheTag = Parse(input: Substring.self) { |
| 70 | + openingTag |
| 71 | + |
| 72 | + OneOf { // parse until tags or the end |
| 73 | + Parse { |
| 74 | + OneOf { |
| 75 | + PrefixUpTo(openingTag) |
| 76 | + PrefixUpTo(closingTag) |
| 77 | + } |
| 78 | + Skip { |
| 79 | + Rest() |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + Rest() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + let case_noTagAtTheStart_parseEverythingBeforeTheTag = Parse(input: Substring.self) { |
| 88 | + OneOf { |
| 89 | + PrefixUpTo(openingTag) |
| 90 | + PrefixUpTo(closingTag) |
| 91 | + } |
| 92 | + |
| 93 | + Skip { |
| 94 | + Rest() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + let parser = Parse(input: Substring.self) { |
| 99 | + OneOf { |
| 100 | + case_openingTagAtTheStart_parseEverythingInsideTheTag |
| 101 | + case_noTagAtTheStart_parseEverythingBeforeTheTag |
| 102 | + Rest() |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + var text = response[...] |
| 107 | + do { |
| 108 | + let suggestion = try parser.parse(&text) |
| 109 | + return String(suggestion) |
| 110 | + } catch { |
| 111 | + return response |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// If the response starts with markdown code block, we should remove it. |
| 116 | + fileprivate func removeLeadingAndTrailingMarkdownCodeBlockMark(from response: String) |
| 117 | + -> String |
| 118 | + { |
| 119 | + let leadingMarkdownCodeBlockMarkParser = Parse(input: Substring.self) { |
| 120 | + Skip { |
| 121 | + Many { |
| 122 | + OneOf { |
| 123 | + " " |
| 124 | + "\n" |
| 125 | + } |
| 126 | + } |
| 127 | + "```" |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + let messagePrefixingMarkdownCodeBlockMarkParser = Parse(input: Substring.self) { |
| 132 | + Skip { |
| 133 | + PrefixThrough(":") |
| 134 | + "\n```" |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + let removePrefixMarkdownCodeBlockMark = Parse(input: Substring.self) { |
| 139 | + Skip { |
| 140 | + OneOf { |
| 141 | + leadingMarkdownCodeBlockMarkParser |
| 142 | + messagePrefixingMarkdownCodeBlockMarkParser |
| 143 | + } |
| 144 | + PrefixThrough("\n") |
| 145 | + } |
| 146 | + OneOf { |
| 147 | + Parse { |
| 148 | + PrefixUpTo("```") |
| 149 | + Skip { Rest() } |
| 150 | + } |
| 151 | + Rest() |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + do { |
| 156 | + var response = response[...] |
| 157 | + let suggestion = try removePrefixMarkdownCodeBlockMark.parse(&response) |
| 158 | + return String(suggestion) |
| 159 | + } catch { |
| 160 | + return response |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
0 commit comments