@@ -2,10 +2,11 @@ import Foundation
22import OpenAIService
33import Preferences
44import SuggestionModel
5+ import XcodeInspector
56
67public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
78 var service : ( any ChatGPTServiceType ) ?
8-
9+
910 public init ( ) { }
1011
1112 public func stopResponding( ) {
@@ -14,13 +15,9 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
1415
1516 public func modifyCode(
1617 code: String ,
17- language: CodeLanguage ,
18- indentSize: Int ,
19- usesTabsForIndentation: Bool ,
2018 requirement: String ,
21- projectRootURL: URL ,
22- fileURL: URL ,
23- allCode: String ,
19+ source: PromptToCodeSource ,
20+ isDetached: Bool ,
2421 extraSystemPrompt: String ? ,
2522 generateDescriptionRequirement: Bool ?
2623 ) async throws -> AsyncThrowingStream < ( code: String , description: String ) , Error > {
@@ -34,6 +31,22 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
3431 return userPreferredLanguage. isEmpty ? " " : " in \( userPreferredLanguage) "
3532 } ( )
3633
34+ let editor : EditorInformation = XcodeInspector . shared. focusedEditorContent ?? . init(
35+ editorContent: . init(
36+ content: source. allCode,
37+ lines: [ ] ,
38+ selections: [ source. range] ,
39+ cursorPosition: . outOfScope,
40+ lineAnnotations: [ ]
41+ ) ,
42+ selectedContent: code,
43+ selectedLines: [ ] ,
44+ documentURL: source. documentURL,
45+ projectURL: source. projectRootURL,
46+ relativePath: " " ,
47+ language: source. language
48+ )
49+
3750 let rule : String = {
3851 func generateDescription( index: Int ) -> String {
3952 let generateDescription = generateDescriptionRequirement ?? UserDefaults . shared
@@ -46,7 +59,7 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
4659 """
4760 : " \( index) . Reply with the result. "
4861 }
49- switch language {
62+ switch editor . language {
5063 case . builtIn( . markdown) , . plaintext:
5164 if code. isEmpty {
5265 return """
@@ -82,20 +95,20 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
8295 } ( )
8396
8497 let systemPrompt = {
85- switch language {
98+ switch editor . language {
8699 case . builtIn( . markdown) , . plaintext:
87100 if code. isEmpty {
88101 return """
89- You are good at writing in \( language. rawValue) .
90- The active file is: \( fileURL . lastPathComponent) .
102+ You are good at writing in \( editor . language. rawValue) .
103+ The active file is: \( editor . documentURL . lastPathComponent) .
91104 \( extraSystemPrompt ?? " " )
92105
93106 \( rule)
94107 """
95108 } else {
96109 return """
97- You are good at writing in \( language. rawValue) .
98- The active file is: \( fileURL . lastPathComponent) .
110+ You are good at writing in \( editor . language. rawValue) .
111+ The active file is: \( editor . documentURL . lastPathComponent) .
99112 \( extraSystemPrompt ?? " " )
100113
101114 \( rule)
@@ -104,16 +117,16 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
104117 default :
105118 if code. isEmpty {
106119 return """
107- You are a senior programer in writing in \( language. rawValue) .
108- The active file is: \( fileURL . lastPathComponent) .
120+ You are a senior programer in writing in \( editor . language. rawValue) .
121+ The active file is: \( editor . documentURL . lastPathComponent) .
109122 \( extraSystemPrompt ?? " " )
110123
111124 \( rule)
112125 """
113126 } else {
114127 return """
115- You are a senior programer in writing in \( language. rawValue) .
116- The active file is: \( fileURL . lastPathComponent) .
128+ You are a senior programer in writing in \( editor . language. rawValue) .
129+ The active file is: \( editor . documentURL . lastPathComponent) .
117130 \( extraSystemPrompt ?? " " )
118131
119132 \( rule)
@@ -125,6 +138,7 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
125138 let firstMessage: String ? = {
126139 if code. isEmpty { return nil }
127140 switch language {
141+ switch editor. language {
128142 case . builtIn( . markdown) , . plaintext:
129143 return """
130144 ```
@@ -161,9 +175,10 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
161175 if let firstMessage {
162176 await memory. mutateHistory { history in
163177 history. append ( . init( role: . user, content: firstMessage) )
178+ history. append ( . init( role: . assistant, content: secondMessage) )
164179 }
165180 }
166- let stream = try await chatGPTService. send ( content: secondMessage )
181+ let stream = try await chatGPTService. send ( content: requirement )
167182 return . init { continuation in
168183 Task {
169184 var content = " "
0 commit comments