( options: StreamDiffLinesPayload, llm: ILLM, abortController: AbortController, overridePrompt: ChatMessage[] | undefined, rulesToInclude: RuleWithSource[] | undefined, )
| 75 | } |
| 76 | |
| 77 | export async function* streamDiffLines( |
| 78 | options: StreamDiffLinesPayload, |
| 79 | llm: ILLM, |
| 80 | abortController: AbortController, |
| 81 | overridePrompt: ChatMessage[] | undefined, |
| 82 | rulesToInclude: RuleWithSource[] | undefined, |
| 83 | ): AsyncGenerator<DiffLine> { |
| 84 | const { type, prefix, highlighted, suffix, input, language } = options; |
| 85 | |
| 86 | // Strip common indentation for the LLM, then add back after generation |
| 87 | let oldLines = |
| 88 | highlighted.length > 0 |
| 89 | ? highlighted.split("\n") |
| 90 | : // When highlighted is empty, we need to combine last line of prefix and first line of suffix to determine the line being edited |
| 91 | [(prefix + suffix).split("\n")[prefix.split("\n").length - 1]]; |
| 92 | |
| 93 | // But if that line is empty, we can assume we are insertion-only |
| 94 | if (oldLines.length === 1 && oldLines[0].trim() === "") { |
| 95 | oldLines = []; |
| 96 | } |
| 97 | |
| 98 | // Defaults to creating an edit prompt |
| 99 | // For apply can be overridden with simply apply prompt |
| 100 | let prompt = |
| 101 | overridePrompt ?? |
| 102 | (type === "apply" |
| 103 | ? constructApplyPrompt(oldLines.join("\n"), options.newCode, llm) |
| 104 | : constructEditPrompt(prefix, highlighted, suffix, llm, input, language)); |
| 105 | |
| 106 | // Rules can be included with edit prompt |
| 107 | // If any rules are present this will result in using chat instead of legacy completion |
| 108 | const systemMessage = |
| 109 | rulesToInclude || llm.baseChatSystemMessage |
| 110 | ? getSystemMessageWithRules({ |
| 111 | availableRules: rulesToInclude ?? [], |
| 112 | userMessage: |
| 113 | typeof prompt === "string" |
| 114 | ? ({ |
| 115 | role: "user", |
| 116 | content: prompt, |
| 117 | } as UserChatMessage) |
| 118 | : (findLast( |
| 119 | prompt, |
| 120 | (msg) => msg.role === "user" || msg.role === "tool", |
| 121 | ) as UserChatMessage | ToolResultChatMessage | undefined), |
| 122 | baseSystemMessage: llm.baseChatSystemMessage, |
| 123 | contextItems: [], |
| 124 | }).systemMessage |
| 125 | : undefined; |
| 126 | |
| 127 | if (systemMessage) { |
| 128 | if (typeof prompt === "string") { |
| 129 | prompt = [ |
| 130 | { |
| 131 | role: "system", |
| 132 | content: systemMessage, |
| 133 | }, |
| 134 | { |
no test coverage detected