Skip to content

Commit 8f9bc5c

Browse files
committed
Support indentation recovering in prompt to code service
1 parent ecd74a8 commit 8f9bc5c

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Core/Sources/PromptToCodeService/OpenAIPromptToCodeService.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
163163
}
164164
}()
165165

166+
let indentation = getCommonLeadingSpaceCount(code)
167+
166168
let secondMessage = """
167-
Requirements:###
168-
\(requirement)
169-
###
169+
I will update the code you just provided.
170+
It looks like every line has an indentation of \(indentation) spaces, I will keep that.
171+
172+
What is your requirement?
170173
"""
171174

172-
let configuration = UserPreferenceChatGPTConfiguration()
175+
let configuration = UserPreferenceChatGPTConfiguration()
173176
.overriding(.init(temperature: 0))
174177
let memory = AutoManagedChatGPTMemory(
175178
systemPrompt: systemPrompt,
@@ -255,6 +258,19 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
255258

256259
return (code, description)
257260
}
261+
262+
func getCommonLeadingSpaceCount(_ code: String) -> Int {
263+
let lines = code.split(separator: "\n")
264+
guard !lines.isEmpty else { return 0 }
265+
var commonCount = Int.max
266+
for line in lines {
267+
let count = line.prefix(while: { $0 == " " }).count
268+
commonCount = min(commonCount, count)
269+
if commonCount == 0 { break }
270+
}
271+
return commonCount
272+
}
273+
258274
func extractAnnotations(
259275
editorInformation: EditorInformation,
260276
source: PromptToCodeSource

0 commit comments

Comments
 (0)