File tree Expand file tree Collapse file tree
Core/Sources/CodeCompletionService Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -39,18 +39,38 @@ public actor AzureOpenAIService {
3939}
4040
4141extension 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}
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import Storage
55protocol CodeCompletionServiceType {
66 func getCompletion(
77 _ request: PromptStrategy
8- ) async throws -> String
8+ ) async throws -> AsyncStream < String >
99}
1010
1111extension 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
Original file line number Diff line number Diff line change @@ -25,12 +25,22 @@ public struct GoogleGeminiService {
2525}
2626
2727extension 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
Original file line number Diff line number Diff line change @@ -44,18 +44,38 @@ public actor OpenAIService {
4444}
4545
4646extension OpenAIService : CodeCompletionServiceType {
47- func getCompletion( _ request: PromptStrategy ) async throws -> String {
47+ func getCompletion( _ request: PromptStrategy ) async throws -> AsyncStream < String > {
4848 switch endpoint {
4949 case . chatCompletion:
5050 let messages = createMessages ( from: request)
5151 CodeCompletionLogger . logger. logPrompt ( messages. map {
5252 ( $0. content, $0. role. rawValue)
5353 } )
54- return try await sendMessages ( messages)
54+ return AsyncStream< String> { continuation in
55+ let task = Task {
56+ let result = try await sendMessages ( messages)
57+ try Task . checkCancellation ( )
58+ continuation. yield ( result)
59+ continuation. finish ( )
60+ }
61+ continuation. onTermination = { _ in
62+ task. cancel ( )
63+ }
64+ }
5565 case . completion:
5666 let prompt = createPrompt ( from: request)
5767 CodeCompletionLogger . logger. logPrompt ( [ ( prompt, " user " ) ] )
58- return try await sendPrompt ( prompt)
68+ return AsyncStream< String> { continuation in
69+ let task = Task {
70+ let result = try await sendPrompt ( prompt)
71+ try Task . checkCancellation ( )
72+ continuation. yield ( result)
73+ continuation. finish ( )
74+ }
75+ continuation. onTermination = { _ in
76+ task. cancel ( )
77+ }
78+ }
5979 }
6080 }
6181}
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ actor TabbyService {
2727}
2828
2929extension TabbyService : CodeCompletionServiceType {
30- func getCompletion( _ request: PromptStrategy ) async throws -> String {
30+ func getCompletion( _ request: PromptStrategy ) async throws -> AsyncStream < String > {
3131 let prefix = request. prefix. joined ( )
3232 let suffix = request. suffix. joined ( )
3333 let clipboard = request. relevantCodeSnippets. map ( \. content) . joined ( separator: " \n \n " )
@@ -46,7 +46,17 @@ extension TabbyService: CodeCompletionServiceType {
4646 ( suffix, " suffix " ) ,
4747 ( clipboard, " clipboard " ) ,
4848 ] )
49- return try await send ( requestBody)
49+ return AsyncStream< String> { continuation in
50+ let task = Task {
51+ let result = try await send ( requestBody)
52+ try Task . checkCancellation ( )
53+ continuation. yield ( result)
54+ continuation. finish ( )
55+ }
56+ continuation. onTermination = { _ in
57+ task. cancel ( )
58+ }
59+ }
5060 }
5161}
5262
You can’t perform that action at this time.
0 commit comments