Skip to content

Commit c840513

Browse files
committed
Revert to previous content on error
1 parent 8960cb5 commit c840513

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

Core/Sources/PromptToCodeService/PromptToCodeService.swift

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ public final class PromptToCodeService: ObservableObject {
88
var promptToCodeAPI: PromptToCodeAPI {
99
designatedPromptToCodeAPI ?? OpenAIPromptToCodeAPI()
1010
}
11+
1112
var runningAPI: PromptToCodeAPI?
1213

1314
@Published public var oldCode: String?
1415
@Published public var code: String
1516
@Published public var isResponding: Bool = false
1617
@Published public var description: String = ""
18+
public var oldDescription: String?
1719
public var canRevert: Bool { oldCode != nil }
1820
public var selectionRange: CursorRange
1921
public var language: CopilotLanguage
@@ -39,31 +41,48 @@ public final class PromptToCodeService: ObservableObject {
3941
runningAPI = api
4042
isResponding = true
4143
let toBemodified = code
44+
oldDescription = description
4245
oldCode = code
4346
code = ""
4447
description = ""
4548
defer { isResponding = false }
46-
let stream = try await api.modifyCode(
47-
code: toBemodified,
48-
language: language,
49-
indentSize: indentSize,
50-
usesTabsForIndentation: usesTabsForIndentation,
51-
requirement: prompt
52-
)
53-
for try await fragment in stream {
54-
Task { @MainActor in
55-
code = fragment.code
56-
description = fragment.description
49+
do {
50+
let stream = try await api.modifyCode(
51+
code: toBemodified,
52+
language: language,
53+
indentSize: indentSize,
54+
usesTabsForIndentation: usesTabsForIndentation,
55+
requirement: prompt
56+
)
57+
for try await fragment in stream {
58+
Task { @MainActor in
59+
code = fragment.code
60+
description = fragment.description
61+
}
5762
}
63+
} catch {
64+
if let oldCode {
65+
code = oldCode
66+
}
67+
if let oldDescription {
68+
description = oldDescription
69+
}
70+
oldCode = nil
71+
oldDescription = nil
72+
throw error
5873
}
5974
}
6075

6176
public func revert() {
6277
guard let oldCode = oldCode else { return }
6378
code = oldCode
79+
if let oldDescription {
80+
description = oldDescription
81+
}
6482
self.oldCode = nil
83+
self.oldDescription = nil
6584
}
66-
85+
6786
public func generateCompletion() -> CopilotCompletion {
6887
.init(
6988
text: code,
@@ -73,7 +92,7 @@ public final class PromptToCodeService: ObservableObject {
7392
displayText: code
7493
)
7594
}
76-
95+
7796
public func stopResponding() {
7897
runningAPI?.stopResponding()
7998
isResponding = false
@@ -88,19 +107,19 @@ protocol PromptToCodeAPI {
88107
usesTabsForIndentation: Bool,
89108
requirement: String
90109
) async throws -> AsyncThrowingStream<(code: String, description: String), Error>
91-
110+
92111
func stopResponding()
93112
}
94113

95114
final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
96115
var service: (any ChatGPTServiceType)?
97-
116+
98117
func stopResponding() {
99118
Task {
100119
await service?.stopReceivingMessage()
101120
}
102121
}
103-
122+
104123
func modifyCode(
105124
code: String,
106125
language: CopilotLanguage,
@@ -198,7 +217,7 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
198217
}
199218

200219
let description = extractDescriptionFromMarkdown(content, startIndex: endIndex)
201-
220+
202221
return (code, description)
203222
}
204223
}
@@ -207,7 +226,7 @@ final class CopilotPromptToCodeAPI: PromptToCodeAPI {
207226
func stopResponding() {
208227
fatalError()
209228
}
210-
229+
211230
func modifyCode(
212231
code: String,
213232
language: CopilotLanguage,

0 commit comments

Comments
 (0)