Skip to content

Commit 8034ce4

Browse files
committed
Merge branch 'release/0.2.0'
2 parents 0fa0b1e + 95bd1e4 commit 8034ce4

32 files changed

+1244
-204
lines changed

Core/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
dependencies: [
1616
.package(
1717
url: "https://github.com/intitni/CopilotForXcodeKit",
18-
from: "0.4.0"
18+
from: "0.5.0"
1919
),
2020
.package(
2121
url: "https://github.com/pointfreeco/swift-dependencies",

Core/Sources/CodeCompletionService/AzureOpenAIService.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,38 @@ public actor AzureOpenAIService {
3939
}
4040

4141
extension AzureOpenAIService: CodeCompletionServiceType {
42-
func getCompletion(_ request: PromptStrategy) async throws -> String {
42+
func getCompletion(_ request: PromptStrategy) async throws -> AsyncStream<String> {
4343
switch endpoint {
4444
case .chatCompletion:
4545
let messages = createMessages(from: request)
4646
CodeCompletionLogger.logger.logPrompt(messages.map {
4747
($0.content, $0.role.rawValue)
4848
})
49-
return try await sendMessages(messages)
49+
return AsyncStream<String> { continuation in
50+
let task = Task {
51+
let result = try await sendMessages(messages)
52+
try Task.checkCancellation()
53+
continuation.yield(result)
54+
continuation.finish()
55+
}
56+
continuation.onTermination = { _ in
57+
task.cancel()
58+
}
59+
}
5060
case .completion:
5161
let prompt = createPrompt(from: request)
5262
CodeCompletionLogger.logger.logPrompt([(prompt, "user")])
53-
return try await sendPrompt(prompt)
63+
return AsyncStream<String> { continuation in
64+
let task = Task {
65+
let result = try await sendPrompt(prompt)
66+
try Task.checkCancellation()
67+
continuation.yield(result)
68+
continuation.finish()
69+
}
70+
continuation.onTermination = { _ in
71+
task.cancel()
72+
}
73+
}
5474
}
5575
}
5676
}

Core/Sources/CodeCompletionService/CodeCompletionService.swift

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import Fundamental
33
import Storage
44

55
protocol CodeCompletionServiceType {
6-
func getCompletion(
7-
_ request: PromptStrategy
8-
) async throws -> String
6+
associatedtype CompletionSequence: AsyncSequence where CompletionSequence.Element == String
7+
8+
func getCompletion(_ request: PromptStrategy) async throws -> CompletionSequence
99
}
1010

1111
extension CodeCompletionServiceType {
@@ -16,7 +16,12 @@ extension CodeCompletionServiceType {
1616
try await withThrowingTaskGroup(of: String.self) { group in
1717
for _ in 0..<max(1, count) {
1818
_ = group.addTaskUnlessCancelled {
19-
try await getCompletion(request)
19+
var result = ""
20+
let stream = try await getCompletion(request)
21+
for try await response in stream {
22+
result.append(response)
23+
}
24+
return result
2025
}
2126
}
2227

@@ -110,6 +115,18 @@ public struct CodeCompletionService {
110115
let result = try await service.getCompletions(prompt, count: count)
111116
try Task.checkCancellation()
112117
return result
118+
case .ollama:
119+
let service = OllamaService(
120+
url: model.endpoint,
121+
endpoint: .chatCompletion,
122+
modelName: model.info.modelName,
123+
stopWords: prompt.stopWords,
124+
keepAlive: model.info.ollamaInfo.keepAlive,
125+
format: .none
126+
)
127+
let result = try await service.getCompletions(prompt, count: count)
128+
try Task.checkCancellation()
129+
return result
113130
case .unknown:
114131
throw Error.unknownFormat
115132
}
@@ -145,6 +162,18 @@ public struct CodeCompletionService {
145162
let result = try await service.getCompletions(prompt, count: count)
146163
try Task.checkCancellation()
147164
return result
165+
case .ollama:
166+
let service = OllamaService(
167+
url: model.endpoint,
168+
endpoint: .completion,
169+
modelName: model.info.modelName,
170+
stopWords: prompt.stopWords,
171+
keepAlive: model.info.ollamaInfo.keepAlive,
172+
format: .none
173+
)
174+
let result = try await service.getCompletions(prompt, count: count)
175+
try Task.checkCancellation()
176+
return result
148177
case .unknown:
149178
throw Error.unknownFormat
150179
}

Core/Sources/CodeCompletionService/GoogleGeminiService.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@ public struct GoogleGeminiService {
2525
}
2626

2727
extension GoogleGeminiService: CodeCompletionServiceType {
28-
func getCompletion(_ request: PromptStrategy) async throws -> String {
28+
func getCompletion(_ request: PromptStrategy) async throws -> AsyncStream<String> {
2929
let messages = createMessages(from: request)
3030
CodeCompletionLogger.logger.logPrompt(messages.map {
3131
($0.parts.first?.text ?? "N/A", $0.role ?? "N/A")
3232
})
33-
return try await sendMessages(messages)
33+
return AsyncStream<String> { continuation in
34+
let task = Task {
35+
let result = try await sendMessages(messages)
36+
try Task.checkCancellation()
37+
continuation.yield(result)
38+
continuation.finish()
39+
}
40+
continuation.onTermination = { _ in
41+
task.cancel()
42+
}
43+
}
3444
}
3545
}
3646

0 commit comments

Comments
 (0)