1- import SuggestionModel
2- import GitHubCopilotService
31import Foundation
2+ import GitHubCopilotService
43import OpenAIService
4+ import Preferences
5+ import SuggestionModel
56
67final class OpenAIPromptToCodeAPI : PromptToCodeAPI {
78 var service : ( any ChatGPTServiceType ) ?
@@ -24,54 +25,137 @@ final class OpenAIPromptToCodeAPI: PromptToCodeAPI {
2425 extraSystemPrompt: String ?
2526 ) async throws -> AsyncThrowingStream < ( code: String , description: String ) , Error > {
2627 let userPreferredLanguage = UserDefaults . shared. value ( for: \. chatGPTLanguage)
27- let textLanguage = userPreferredLanguage. isEmpty ? " " : " in \( userPreferredLanguage) "
28-
29- let prompt = {
30- let indentRule = usesTabsForIndentation ? " \( indentSize) tabs " : " \( indentSize) spaces "
31- if code. isEmpty {
32- return """
33- You are a senior programer in writing code in \( language. rawValue) .
34-
35- File url: \( fileURL)
36-
37- \( extraSystemPrompt ?? " " )
28+ let textLanguage = {
29+ if !UserDefaults. shared
30+ . value ( for: \. promptToCodeGenerateDescriptionInUserPreferredLanguage)
31+ {
32+ return " "
33+ }
34+ return userPreferredLanguage. isEmpty ? " " : " in \( userPreferredLanguage) "
35+ } ( )
3836
39- Please write a piece of code that meets my requirements. The indentation should be \(
40- indentRule
41- ) .
37+ let systemPrompt = {
38+ switch language {
39+ case . builtIn( . markdown) , . plaintext:
40+ if code. isEmpty {
41+ return """
42+ You are good at writing in \( language. rawValue) .
43+ You are editing file: \( fileURL. lastPathComponent) .
44+ \( extraSystemPrompt ?? " " )
45+ """
46+ } else {
47+ return """
48+ You are good at writing in \( language. rawValue) .
49+ You are editing file: \( fileURL. lastPathComponent) .
50+ \( extraSystemPrompt ?? " " )
51+ """
52+ }
53+ default :
54+ if code. isEmpty {
55+ return """
56+ You are a senior programer in writing in \( language. rawValue) .
57+ You are editing file: \( fileURL. lastPathComponent) .
58+ \( extraSystemPrompt ?? " " )
59+ """
60+ } else {
61+ return """
62+ You are a senior programer in writing in \( language. rawValue) .
63+ You are editing file: \( fileURL. lastPathComponent) .
64+ \( extraSystemPrompt ?? " " )
65+ """
66+ }
67+ }
68+ } ( )
4269
43- Please reply to me start with the code block, followed by a clear and concise description in 1-3 sentences about what you did \(
44- textLanguage
45- ) .
70+ let firstMessage : String ? = {
71+ if code. isEmpty { return nil }
72+ switch language {
73+ case . builtIn( . markdown) , . plaintext:
74+ return """
75+ Content: \" " "
76+ \( code)
77+ \" " "
4678 """
47- } else {
79+ default :
4880 return """
49- # Description
50-
51- You are a senior programer in writing code in \( language. rawValue) .
52-
53- File url: \( fileURL)
54-
55- \( extraSystemPrompt ?? " " )
56-
57- Please mutate the following code fragment with my requirements. Keep the original indentation. Do not add comments unless told to.
58-
59- Please reply to me start with the code block followed by a clear and concise description about what you did in 1-3 sentences \(
60- textLanguage
61- ) .
62-
63- # Code
64-
65- ``` \( language. rawValue)
81+ Code:```
6682 \( code)
6783 ```
6884 """
6985 }
7086 } ( )
7187
72- let chatGPTService = ChatGPTService ( systemPrompt: prompt, temperature: 0.3 )
88+ let formattedRequirement = requirement. split ( separator: " \n " )
89+ . map { " \( $0) " }
90+ . joined ( separator: " \n " )
91+
92+ let secondMessage : String = {
93+ func generateDescription( index: Int ) -> String {
94+ return UserDefaults . shared. value ( for: \. promptToCodeGenerateDescription)
95+ ? """
96+ \( index) . After the code block, write a clear and concise description \
97+ in 1-3 sentences about what you did in step 1 \( textLanguage) .
98+ \( index + 1 ) . Reply with the result.
99+ """
100+ : " \( index) . Reply with the result. "
101+ }
102+ switch language {
103+ case . builtIn( . markdown) , . plaintext:
104+ if code. isEmpty {
105+ return """
106+ 1. Write the content that meets my requirements.
107+ Requirements: \" " "
108+ Write the content;
109+ \( formattedRequirement)
110+ \" " "
111+ 2. Embed the new content in a markdown code block.
112+ \( generateDescription ( index: 3 ) )
113+ """
114+ } else {
115+ return """
116+ 1. Update the content to meet my requirements.
117+ Requirements: \" " "
118+ \( formattedRequirement)
119+ \" " "
120+ 2. Format the updated content to use the original indentation. Especially the first line.
121+ 3. Embed the updated content in a markdown code block.
122+ \( generateDescription ( index: 4 ) )
123+ """
124+ }
125+ default :
126+ if code. isEmpty {
127+ return """
128+ 1. Write the code that meets my requirements.
129+ Requirements: \" " "
130+ Write the code in \( language. rawValue) ;
131+ \( formattedRequirement)
132+ \" " "
133+ 2. Embed the code in a markdown code block.
134+ \( generateDescription ( index: 3 ) )
135+ """
136+ } else {
137+ return """
138+ 1. Update the code to meet my requirements.
139+ Requirements: \" " "
140+ Update the code;
141+ \( formattedRequirement)
142+ \" " "
143+ 2. Format the updated code to use the original indentation. Especially the first line.
144+ 3. Embed the updated code in a markdown code block.
145+ \( generateDescription ( index: 4 ) )
146+ """
147+ }
148+ }
149+ } ( )
150+
151+ let chatGPTService = ChatGPTService ( systemPrompt: systemPrompt, temperature: 0.3 )
73152 service = chatGPTService
74- let stream = try await chatGPTService. send ( content: requirement)
153+ if let firstMessage {
154+ await chatGPTService. mutateHistory { history in
155+ history. append ( . init( role: . user, content: firstMessage) )
156+ }
157+ }
158+ let stream = try await chatGPTService. send ( content: secondMessage)
75159 return . init { continuation in
76160 Task {
77161 var content = " "
0 commit comments