Skip to content

Commit 73800cb

Browse files
committed
Add fallback solution when prompt to code failed to extract code
1 parent 850cab3 commit 73800cb

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Core/Sources/PromptToCodeService/OpenAIPromptToCodeAPI.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
6262
6363
# Code
6464
65-
```
65+
```\(language.rawValue)
6666
\(code)
6767
```
6868
"""
@@ -75,10 +75,16 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
7575
return .init { continuation in
7676
Task {
7777
var content = ""
78+
var extracted = extractCodeAndDescription(from: content)
7879
do {
7980
for try await fragment in stream {
8081
content.append(fragment)
81-
continuation.yield(extractCodeAndDescription(from: content))
82+
extracted = extractCodeAndDescription(from: content)
83+
if !content.isEmpty, extracted.code.isEmpty {
84+
continuation.yield((code: content, description: ""))
85+
} else {
86+
continuation.yield(extracted)
87+
}
8288
}
8389
continuation.finish()
8490
} catch {
@@ -134,3 +140,4 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
134140
return (code, description)
135141
}
136142
}
143+

0 commit comments

Comments
 (0)