Skip to content

Commit 4158c6b

Browse files
committed
Add ContextAwarePromptToCodeService
1 parent c03ce88 commit 4158c6b

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

Core/Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ let package = Package(
200200
.product(name: "OpenAIService", package: "Tool"),
201201
.product(name: "AppMonitoring", package: "Tool"),
202202
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
203-
]
203+
].pro([
204+
"ProService",
205+
])
204206
),
205207
.testTarget(name: "PromptToCodeServiceTests", dependencies: ["PromptToCodeService"]),
206208

Core/Sources/PromptToCodeService/OpenAIPromptToCodeService.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
166166

167167
let secondMessage = """
168168
I will update the code you just provided.
169-
It looks like every line has an indentation of \(indentation) spaces, I will keep that.
169+
Every line has an indentation of \(indentation) spaces, I will keep that.
170170
171171
What is your requirement?
172172
"""
@@ -212,8 +212,14 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
212212
}
213213
}
214214
}
215+
}
216+
217+
// MAKR: - Internal
215218

216-
func extractCodeAndDescription(from content: String) -> (code: String, description: String) {
219+
extension OpenAIPromptToCodeService {
220+
func extractCodeAndDescription(from content: String)
221+
-> (code: String, description: String)
222+
{
217223
func extractCodeFromMarkdown(_ markdown: String) -> (code: String, endIndex: Int)? {
218224
let codeBlockRegex = try! NSRegularExpression(
219225
pattern: #"```(?:\w+)?[\n]([\s\S]+?)[\n]```"#,
@@ -275,7 +281,8 @@ public final class OpenAIPromptToCodeService: PromptToCodeServiceType {
275281
editorInformation: EditorInformation,
276282
source: PromptToCodeSource
277283
) -> String {
278-
guard let annotations = editorInformation.editorContent?.lineAnnotations else { return "" }
284+
guard let annotations = editorInformation.editorContent?.lineAnnotations
285+
else { return "" }
279286
let all = annotations
280287
.lazy
281288
.filter { annotation in

Core/Sources/PromptToCodeService/PromptToCodeServiceType.swift

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ public struct PromptToCodeServiceDependencyKey: DependencyKey {
4242
public static let previewValue: PromptToCodeServiceType = PreviewPromptToCodeService()
4343
}
4444

45-
public struct PromptToCodeServiceFactoryDependencyKey: DependencyKey {
46-
public static let liveValue: () -> PromptToCodeServiceType = { OpenAIPromptToCodeService() }
47-
public static let previewValue: () -> PromptToCodeServiceType = { PreviewPromptToCodeService() }
48-
}
49-
5045
public extension DependencyValues {
5146
var promptToCodeService: PromptToCodeServiceType {
5247
get { self[PromptToCodeServiceDependencyKey.self] }
@@ -59,3 +54,57 @@ public extension DependencyValues {
5954
}
6055
}
6156

57+
#if canImport(ContextAwarePromptToCodeService)
58+
59+
import ContextAwarePromptToCodeService
60+
61+
extension ContextAwarePromptToCodeService: PromptToCodeServiceType {
62+
public func modifyCode(
63+
code: String,
64+
requirement: String,
65+
source: PromptToCodeSource,
66+
isDetached: Bool,
67+
extraSystemPrompt: String?,
68+
generateDescriptionRequirement: Bool?
69+
) async throws -> AsyncThrowingStream<(code: String, description: String), Error> {
70+
try await modifyCode(
71+
code: code,
72+
requirement: requirement,
73+
source: ContextAwarePromptToCodeService.Source(
74+
language: source.language,
75+
documentURL: source.documentURL,
76+
projectRootURL: source.projectRootURL,
77+
allCode: source.allCode,
78+
range: source.range
79+
),
80+
isDetached: isDetached,
81+
extraSystemPrompt: extraSystemPrompt,
82+
generateDescriptionRequirement: generateDescriptionRequirement
83+
)
84+
}
85+
}
86+
87+
public struct PromptToCodeServiceFactoryDependencyKey: DependencyKey {
88+
public static let liveValue: () -> PromptToCodeServiceType = {
89+
ContextAwarePromptToCodeService()
90+
}
91+
92+
public static let previewValue: () -> PromptToCodeServiceType = {
93+
PreviewPromptToCodeService()
94+
}
95+
}
96+
97+
#else
98+
99+
public struct PromptToCodeServiceFactoryDependencyKey: DependencyKey {
100+
public static let liveValue: () -> PromptToCodeServiceType = {
101+
OpenAIPromptToCodeService()
102+
}
103+
104+
public static let previewValue: () -> PromptToCodeServiceType = {
105+
PreviewPromptToCodeService()
106+
}
107+
}
108+
109+
#endif
110+

Pro

Submodule Pro updated from a66b238 to ed56bca

0 commit comments

Comments
 (0)