Skip to content

Commit f66aa15

Browse files
committed
Update RequestStrategy to provide StreamStopStrategy
1 parent 1f2861b commit f66aa15

7 files changed

Lines changed: 81 additions & 8 deletions

File tree

Core/Sources/SuggestionService/RequestStrategies/CodeLlamaFillInTheMiddleRequestStrategy.swift

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CodeCompletionService
12
import CopilotForXcodeKit
23
import Foundation
34
import Fundamental
@@ -20,6 +21,10 @@ struct CodeLlamaFillInTheMiddleRequestStrategy: RequestStrategy {
2021
)
2122
}
2223

24+
func createStreamStopStrategy() -> some StreamStopStrategy {
25+
FIMStreamStopStrategy(prefix: prefix)
26+
}
27+
2328
func createRawSuggestionPostProcessor() -> some RawSuggestionPostProcessingStrategy {
2429
DefaultRawSuggestionPostProcessingStrategy(codeWrappingTags: nil)
2530
}
@@ -92,8 +97,41 @@ struct CodeLlamaFillInTheMiddleWithSystemPromptRequestStrategy: RequestStrategy
9297
return prompt
9398
}
9499

100+
func createStreamStopStrategy() -> some StreamStopStrategy {
101+
strategy.createStreamStopStrategy()
102+
}
103+
95104
func createRawSuggestionPostProcessor() -> some RawSuggestionPostProcessingStrategy {
96-
DefaultRawSuggestionPostProcessingStrategy(codeWrappingTags: nil)
105+
strategy.createRawSuggestionPostProcessor()
106+
}
107+
}
108+
109+
struct FIMStreamStopStrategy: StreamStopStrategy {
110+
let prefix: [String]
111+
112+
func shouldStop(
113+
existedLines: [String],
114+
currentLine: String,
115+
proposedLineLimit: Int
116+
) -> StreamStopStrategyResult {
117+
if let prefixLastLine = prefix.last {
118+
if let lastLineIndex = existedLines.lastIndex(of: prefixLastLine) {
119+
if existedLines.count >= lastLineIndex + 1 + proposedLineLimit {
120+
return .stop(appendingNewContent: true)
121+
}
122+
return .continue
123+
} else {
124+
if existedLines.count >= proposedLineLimit {
125+
return .stop(appendingNewContent: true)
126+
}
127+
return .continue
128+
}
129+
} else {
130+
if existedLines.count >= proposedLineLimit {
131+
return .stop(appendingNewContent: true)
132+
}
133+
return .continue
134+
}
97135
}
98136
}
99137

Core/Sources/SuggestionService/RequestStrategies/ContinueRequestStrategy.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CodeCompletionService
12
import CopilotForXcodeKit
23
import Foundation
34
import Fundamental
@@ -21,14 +22,21 @@ struct ContinueRequestStrategy: RequestStrategy {
2122
suffix: suffix
2223
)
2324
}
24-
25+
2526
func createRawSuggestionPostProcessor() -> DefaultRawSuggestionPostProcessingStrategy {
2627
DefaultRawSuggestionPostProcessingStrategy(codeWrappingTags: (
2728
Tag.openingCode,
2829
Tag.closingCode
2930
))
3031
}
3132

33+
func createStreamStopStrategy() -> some StreamStopStrategy {
34+
OpeningTagBasedStreamStopStrategy(
35+
openingTag: Tag.openingCode,
36+
toleranceIfNoOpeningTagFound: 4
37+
)
38+
}
39+
3240
enum Tag {
3341
public static let openingCode = "<Code3721>"
3442
public static let closingCode = "</Code3721>"

Core/Sources/SuggestionService/RequestStrategies/DefaultRequestStrategy.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CodeCompletionService
12
import CopilotForXcodeKit
23
import Foundation
34
import Fundamental
@@ -23,6 +24,13 @@ struct DefaultRequestStrategy: RequestStrategy {
2324
)
2425
}
2526

27+
func createStreamStopStrategy() -> some StreamStopStrategy {
28+
OpeningTagBasedStreamStopStrategy(
29+
openingTag: Tag.openingCode,
30+
toleranceIfNoOpeningTagFound: 4
31+
)
32+
}
33+
2634
func createRawSuggestionPostProcessor() -> DefaultRawSuggestionPostProcessingStrategy {
2735
DefaultRawSuggestionPostProcessingStrategy(codeWrappingTags: (
2836
Tag.openingCode,

Core/Sources/SuggestionService/RequestStrategies/NaiveRequestStrategy.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CodeCompletionService
12
import CopilotForXcodeKit
23
import Foundation
34
import Fundamental
@@ -8,7 +9,7 @@ struct NaiveRequestStrategy: RequestStrategy {
89
var sourceRequest: SuggestionRequest
910
var prefix: [String]
1011
var suffix: [String]
11-
12+
1213
var shouldSkip: Bool {
1314
prefix.last?.trimmingCharacters(in: .whitespaces) == "}"
1415
}
@@ -20,11 +21,15 @@ struct NaiveRequestStrategy: RequestStrategy {
2021
suffix: suffix
2122
)
2223
}
23-
24+
2425
func createRawSuggestionPostProcessor() -> some RawSuggestionPostProcessingStrategy {
2526
NoOpRawSuggestionPostProcessingStrategy()
2627
}
2728

29+
func createStreamStopStrategy() -> some StreamStopStrategy {
30+
DefaultStreamStopStrategy()
31+
}
32+
2833
struct Request: PromptStrategy {
2934
let systemPrompt: String = ""
3035
var sourceRequest: SuggestionRequest
@@ -34,7 +39,7 @@ struct NaiveRequestStrategy: RequestStrategy {
3439
var relevantCodeSnippets: [RelevantCodeSnippet] { sourceRequest.relevantCodeSnippets }
3540
var stopWords: [String] { ["\n\n"] }
3641
var language: CodeLanguage? { sourceRequest.language }
37-
42+
3843
var suggestionPrefix: SuggestionPrefix {
3944
guard let prefix = prefix.last else { return .empty }
4045
return .unchanged(prefix).curlyBracesLineBreak()
@@ -71,9 +76,9 @@ struct NaiveRequestStrategy: RequestStrategy {
7176

7277
return [.init(role: .user, content: """
7378
File path: \(filePath)
74-
79+
7580
---
76-
81+
7782
\(code)
7883
""".trimmingCharacters(in: .whitespacesAndNewlines))]
7984
}

Core/Sources/SuggestionService/RequestStrategies/TabbyRequestStrategy.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CodeCompletionService
12
import CopilotForXcodeKit
23
import Foundation
34
import Fundamental
@@ -24,6 +25,10 @@ struct TabbyRequestStrategy: RequestStrategy {
2425
NoOpRawSuggestionPostProcessingStrategy()
2526
}
2627

28+
func createStreamStopStrategy() -> some StreamStopStrategy {
29+
NeverStreamStopStrategy()
30+
}
31+
2732
struct Prompt: PromptStrategy {
2833
let systemPrompt: String = ""
2934
var sourceRequest: SuggestionRequest

Core/Sources/SuggestionService/RequestStrategy.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CodeCompletionService
12
import CopilotForXcodeKit
23
import Foundation
34
import Fundamental
@@ -8,6 +9,7 @@ import Parsing
89
protocol RequestStrategy {
910
associatedtype Prompt: PromptStrategy
1011
associatedtype RawSuggestionPostProcessor: RawSuggestionPostProcessingStrategy
12+
associatedtype SomeStreamStopStrategy: StreamStopStrategy
1113

1214
init(sourceRequest: SuggestionRequest, prefix: [String], suffix: [String])
1315

@@ -17,6 +19,9 @@ protocol RequestStrategy {
1719
/// Create a prompt to generate code completion.
1820
func createPrompt() -> Prompt
1921

22+
/// Control how a stream should stop early.
23+
func createStreamStopStrategy() -> SomeStreamStopStrategy
24+
2025
/// The AI model may not return a suggestion in a ideal format. You can use it to reformat the
2126
/// suggestions.
2227
func createRawSuggestionPostProcessor() -> RawSuggestionPostProcessor

Core/Sources/SuggestionService/Service.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,32 @@ actor Service {
4646

4747
let prompt = strategy.createPrompt()
4848
let postProcessor = strategy.createRawSuggestionPostProcessor()
49+
let stopStream = strategy.createStreamStopStrategy()
4950

5051
let suggestedCodeSnippets: [String]
5152

5253
switch getModel() {
5354
case let .chatModel(model):
5455
CodeCompletionLogger.logger.logModel(model)
5556
suggestedCodeSnippets = try await service.getCompletions(
56-
prompt,
57+
prompt,
58+
streamStopStrategy: stopStream,
5759
model: model,
5860
count: 1
5961
)
6062
case let .completionModel(model):
6163
CodeCompletionLogger.logger.logModel(model)
6264
suggestedCodeSnippets = try await service.getCompletions(
6365
prompt,
66+
streamStopStrategy: stopStream,
6467
model: model,
6568
count: 1
6669
)
6770
case let .tabbyModel(model):
6871
CodeCompletionLogger.logger.logModel(model)
6972
suggestedCodeSnippets = try await service.getCompletions(
7073
prompt,
74+
streamStopStrategy: stopStream,
7175
model: model,
7276
count: 1
7377
)

0 commit comments

Comments
 (0)