@@ -22,19 +22,29 @@ public final class PromptToCodeService: ObservableObject {
2222 public var language : CopilotLanguage
2323 public var indentSize : Int
2424 public var usesTabsForIndentation : Bool
25+ public var projectRootURL : URL
26+ public var fileURL : URL
27+ public var allCode : String
2528
2629 public init (
2730 code: String ,
2831 selectionRange: CursorRange ,
2932 language: CopilotLanguage ,
3033 identSize: Int ,
3134 usesTabsForIndentation: Bool
35+ usesTabsForIndentation: Bool,
36+ projectRootURL: URL ,
37+ fileURL: URL ,
38+ allCode: String
3239 ) {
3340 self . code = code
3441 self . selectionRange = selectionRange
3542 self . language = language
3643 indentSize = identSize
3744 self . usesTabsForIndentation = usesTabsForIndentation
45+ self . projectRootURL = projectRootURL
46+ self . fileURL = fileURL
47+ self . allCode = allCode
3848 }
3949
4050 public func modifyCode( prompt: String ) async throws {
@@ -112,142 +122,11 @@ protocol PromptToCodeAPI {
112122 language: CopilotLanguage ,
113123 indentSize: Int ,
114124 usesTabsForIndentation: Bool ,
115- requirement: String
125+ requirement: String ,
126+ projectRootURL: URL ,
127+ fileURL: URL ,
128+ allCode: String
116129 ) async throws -> AsyncThrowingStream < ( code: String , description: String ) , Error >
117130
118131 func stopResponding( )
119132}
120-
121- final class OpenAIPromptToCodeAPI : PromptToCodeAPI {
122- var service : ( any ChatGPTServiceType ) ?
123-
124- func stopResponding( ) {
125- Task {
126- await service? . stopReceivingMessage ( )
127- }
128- }
129-
130- func modifyCode(
131- code: String ,
132- language: CopilotLanguage ,
133- indentSize: Int ,
134- usesTabsForIndentation: Bool ,
135- requirement: String
136- ) async throws -> AsyncThrowingStream < ( code: String , description: String ) , Error > {
137- let userPreferredLanguage = UserDefaults . shared. value ( for: \. chatGPTLanguage)
138- let textLanguage = userPreferredLanguage. isEmpty ? " " : " in \( userPreferredLanguage) "
139-
140- let prompt = {
141- let indentRule = usesTabsForIndentation ? " \( indentSize) tabs " : " \( indentSize) spaces "
142- if code. isEmpty {
143- return """
144- You are a senior programer in writing code in \( language. rawValue) .
145-
146- Please write a piece of code that meets my requirements. The indentation should be \(
147- indentRule
148- ) .
149-
150- Please reply to me start with the code block, followed by a short description in 1-3 sentences about what you did \(
151- textLanguage
152- ) .
153- """
154- } else {
155- return """
156- You are a senior programer in writing code in \( language. rawValue) .
157-
158- Please mutate the following code with my requirements. The indentation should be \(
159- indentRule
160- ) .
161-
162- Please reply to me start with the code block followed by a short description about what you did in 1-3 sentences \(
163- textLanguage
164- ) .
165-
166- ```
167- \( code)
168- ```
169- """
170- }
171- } ( )
172-
173- let chatGPTService = ChatGPTService ( systemPrompt: prompt)
174- service = chatGPTService
175- let stream = try await chatGPTService. send ( content: requirement)
176- return . init { continuation in
177- Task {
178- var content = " "
179- do {
180- for try await fragment in stream {
181- content. append ( fragment)
182- continuation. yield ( extractCodeAndDescription ( from: content) )
183- }
184- continuation. finish ( )
185- } catch {
186- continuation. finish ( throwing: error)
187- }
188- }
189- }
190- }
191-
192- func extractCodeAndDescription( from content: String ) -> ( code: String , description: String ) {
193- func extractCodeFromMarkdown( _ markdown: String ) -> ( code: String , endIndex: Int ) ? {
194- let codeBlockRegex = try ! NSRegularExpression (
195- pattern: #"```(?:\w+)?[\n]([\s\S]+?)[\n]```"# ,
196- options: . dotMatchesLineSeparators
197- )
198- let range = NSRange ( markdown. startIndex..< markdown. endIndex, in: markdown)
199- if let match = codeBlockRegex. firstMatch ( in: markdown, options: [ ] , range: range) {
200- let codeBlockRange = Range ( match. range ( at: 1 ) , in: markdown) !
201- return ( String ( markdown [ codeBlockRange] ) , match. range ( at: 0 ) . upperBound)
202- }
203-
204- let incompleteCodeBlockRegex = try ! NSRegularExpression (
205- pattern: #"```(?:\w+)?[\n]([\s\S]+?)$"# ,
206- options: . dotMatchesLineSeparators
207- )
208- let range2 = NSRange ( markdown. startIndex..< markdown. endIndex, in: markdown)
209- if let match = incompleteCodeBlockRegex. firstMatch (
210- in: markdown,
211- options: [ ] ,
212- range: range2
213- ) {
214- let codeBlockRange = Range ( match. range ( at: 1 ) , in: markdown) !
215- return ( String ( markdown [ codeBlockRange] ) , match. range ( at: 0 ) . upperBound)
216- }
217- return nil
218- }
219-
220- guard let ( code, endIndex) = extractCodeFromMarkdown ( content) else {
221- return ( " " , " " )
222- }
223-
224- func extractDescriptionFromMarkdown( _ markdown: String , startIndex: Int ) -> String {
225- let startIndex = markdown. index ( markdown. startIndex, offsetBy: startIndex)
226- guard startIndex < markdown. endIndex else { return " " }
227- let range = startIndex..< markdown. endIndex
228- let description = String ( markdown [ range] )
229- . trimmingCharacters ( in: . whitespacesAndNewlines)
230- return description
231- }
232-
233- let description = extractDescriptionFromMarkdown ( content, startIndex: endIndex)
234-
235- return ( code, description)
236- }
237- }
238-
239- final class CopilotPromptToCodeAPI : PromptToCodeAPI {
240- func stopResponding( ) {
241- fatalError ( )
242- }
243-
244- func modifyCode(
245- code: String ,
246- language: CopilotLanguage ,
247- indentSize: Int ,
248- usesTabsForIndentation: Bool ,
249- requirement: String
250- ) async throws -> AsyncThrowingStream < ( code: String , description: String ) , Error > {
251- fatalError ( )
252- }
253- }
0 commit comments