forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptToCodeService.swift
More file actions
191 lines (173 loc) · 6.37 KB
/
PromptToCodeService.swift
File metadata and controls
191 lines (173 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import FocusedCodeFinder
import Foundation
import OpenAIService
import SuggestionModel
import XcodeInspector
public final class PromptToCodeService: ObservableObject {
var designatedPromptToCodeAPI: PromptToCodeAPI?
var promptToCodeAPI: PromptToCodeAPI {
if let designatedPromptToCodeAPI {
return designatedPromptToCodeAPI
}
return OpenAIPromptToCodeAPI()
}
var runningAPI: PromptToCodeAPI?
public indirect enum HistoryNode: Equatable {
case empty
case node(code: String, description: String, previous: HistoryNode)
mutating func enqueue(code: String, description: String) {
let current = self
self = .node(code: code, description: description, previous: current)
}
mutating func pop() -> (code: String, description: String)? {
switch self {
case .empty:
return nil
case let .node(code, description, previous):
self = previous
return (code, description)
}
}
}
@Published public var history: HistoryNode
@Published public var code: String
@Published public var isResponding: Bool = false
@Published public var description: String = ""
@Published public var isContinuous = false
@Published public var selectionRange: CursorRange?
public var canRevert: Bool { history != .empty }
public var language: CodeLanguage
public var indentSize: Int
public var usesTabsForIndentation: Bool
public var projectRootURL: URL
public var fileURL: URL
public var allCode: String
public var extraSystemPrompt: String?
public var generateDescriptionRequirement: Bool?
public init(
code: String,
selectionRange: CursorRange?,
language: CodeLanguage,
identSize: Int,
usesTabsForIndentation: Bool,
projectRootURL: URL,
fileURL: URL,
allCode: String,
extraSystemPrompt: String? = nil,
generateDescriptionRequirement: Bool?
) {
self.code = code
self.selectionRange = selectionRange
self.language = language
indentSize = identSize
self.usesTabsForIndentation = usesTabsForIndentation
self.projectRootURL = projectRootURL
self.fileURL = fileURL
self.allCode = allCode
history = .empty
self.extraSystemPrompt = extraSystemPrompt
self.generateDescriptionRequirement = generateDescriptionRequirement
}
public func modifyCode(prompt: String) async throws {
let api = promptToCodeAPI
runningAPI = api
isResponding = true
let toBeModified = code
history.enqueue(code: code, description: description)
code = ""
description = ""
defer { isResponding = false }
do {
let stream = try await api.modifyCode(
code: toBeModified,
language: language,
indentSize: indentSize,
usesTabsForIndentation: usesTabsForIndentation,
requirement: prompt,
projectRootURL: projectRootURL,
fileURL: fileURL,
allCode: allCode,
extraSystemPrompt: extraSystemPrompt,
generateDescriptionRequirement: generateDescriptionRequirement
)
for try await fragment in stream {
code = fragment.code
description = fragment.description
}
if code.isEmpty, description.isEmpty {
revert()
}
} catch is CancellationError {
return
} catch {
if (error as NSError).code == NSURLErrorCancelled {
return
}
revert()
throw error
}
}
public func revert() {
guard let (code, description) = history.pop() else { return }
self.code = code
self.description = description
}
public func stopResponding() {
runningAPI?.stopResponding()
isResponding = false
}
public func toggleAttachOrDetachToCode() {
if selectionRange != nil {
selectionRange = nil
} else {
let inspector = XcodeInspector.shared
guard let editor = XcodeInspector.shared.focusedEditorContent
else { return }
if let range = editor.editorContent?.selections.first, range.start != range.end {
selectionRange = range
} else {
let focusedCodeFinder: FocusedCodeFinder = {
switch language {
case .builtIn(.swift):
return SwiftFocusedCodeFinder()
default:
return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 10)
}
}()
let cursorPosition = editor.editorContent?.cursorPosition ?? .zero
let codeContext = focusedCodeFinder.findFocusedCode(
containingRange: .init(start: cursorPosition, end: cursorPosition),
activeDocumentContext: .init(
filePath: editor.documentURL.path,
relativePath: editor.relativePath,
language: editor.language,
fileContent: editor.editorContent?.content ?? "",
lines: editor.editorContent?.lines ?? [],
selectedCode: editor.editorContent?.selectedContent ?? "",
selectionRange: editor.editorContent?.selections.first ?? .zero,
lineAnnotations: [],
imports: []
)
)
fileURL = editor.documentURL
projectRootURL = editor.projectURL
selectionRange = codeContext.contextRange
}
}
}
}
protocol PromptToCodeAPI {
func modifyCode(
code: String,
language: CodeLanguage,
indentSize: Int,
usesTabsForIndentation: Bool,
requirement: String,
projectRootURL: URL,
fileURL: URL,
allCode: String,
extraSystemPrompt: String?,
generateDescriptionRequirement: Bool?
) async throws -> AsyncThrowingStream<(code: String, description: String), Error>
func stopResponding()
}