forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreviewPromptToCodeService.swift
More file actions
85 lines (76 loc) · 2.96 KB
/
PreviewPromptToCodeService.swift
File metadata and controls
85 lines (76 loc) · 2.96 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
import Foundation
import ModificationBasic
import SuggestionBasic
public final class PreviewModificationAgent: ModificationAgent {
public func send(_ request: Request) -> AsyncThrowingStream<Response, any Error> {
AsyncThrowingStream { continuation in
let task = Task {
do {
let stream = try await modifyCode(
code: request.code,
requirement: request.requirement,
source: .init(
language: request.source.language,
documentURL: request.source.documentURL,
projectRootURL: request.source.projectRootURL,
content: request.source.content,
lines: request.source.lines,
range: request.range
),
isDetached: request.isDetached,
extraSystemPrompt: request.extraSystemPrompt,
generateDescriptionRequirement: false
)
for try await (code, description) in stream {
continuation.yield(.code(code))
}
continuation.finish()
} catch {
continuation.finish(throwing: error)
}
}
continuation.onTermination = { _ in
task.cancel()
}
}
}
public init() {}
func modifyCode(
code: String,
requirement: String,
source: PromptToCodeSource,
isDetached: Bool,
extraSystemPrompt: String?,
generateDescriptionRequirement: Bool?
) async throws -> AsyncThrowingStream<(code: String, description: String), Error> {
return AsyncThrowingStream { continuation in
Task {
let code = """
struct Cat {
var name: String
}
print("Hello world!")
"""
let description = "I have created a struct `Cat`."
var resultCode = ""
var resultDescription = ""
do {
for character in code {
try await Task.sleep(nanoseconds: 50_000_000)
resultCode.append(character)
continuation.yield((resultCode, resultDescription))
}
for character in description {
try await Task.sleep(nanoseconds: 50_000_000)
resultDescription.append(character)
continuation.yield((resultCode, resultDescription))
}
continuation.finish()
} catch {
continuation.finish(throwing: error)
}
}
}
}
public func stopResponding() {}
}