Skip to content

Commit 0b93f92

Browse files
committed
Move MessageScopeParser to ChatContextCollector
1 parent c2837ee commit 0b93f92

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

Core/Sources/ChatService/DynamicContextController.swift

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ChatContextCollector
22
import Foundation
33
import OpenAIService
4-
import Parsing
54
import Preferences
65
import XcodeInspector
76

@@ -65,17 +64,17 @@ final class DynamicContextController {
6564
}
6665
return contexts
6766
}
68-
67+
6968
let extraSystemPrompt = contexts
7069
.map(\.systemPrompt)
7170
.filter { !$0.isEmpty }
7271
.joined(separator: "\n\n")
73-
72+
7473
let contextPrompts = contexts
7574
.flatMap(\.retrievedContent)
7675
.filter { !$0.content.isEmpty }
7776
.sorted { $0.priority > $1.priority }
78-
77+
7978
let contextualSystemPrompt = """
8079
\(language.isEmpty ? "" : "You must always reply in \(language)")
8180
\(systemPrompt)\(extraSystemPrompt.isEmpty ? "" : "\n\(extraSystemPrompt)")
@@ -88,30 +87,8 @@ final class DynamicContextController {
8887

8988
extension DynamicContextController {
9089
static func parseScopes(_ prompt: inout String) -> Set<String> {
91-
guard !prompt.isEmpty else { return [] }
92-
do {
93-
let parser = Parse {
94-
"@"
95-
Many {
96-
Prefix { $0.isLetter }
97-
} separator: {
98-
"+"
99-
} terminator: {
100-
" "
101-
}
102-
Skip {
103-
Many {
104-
" "
105-
}
106-
}
107-
Rest()
108-
}
109-
let (scopes, rest) = try parser.parse(prompt)
110-
prompt = String(rest)
111-
return Set(scopes.map(String.init))
112-
} catch {
113-
return []
114-
}
90+
let parser = MessageScopeParser()
91+
return parser(&prompt)
11592
}
11693
}
11794

Pro

Submodule Pro updated from 51ea02d to 7292e08

Tool/Sources/ChatContextCollector/ChatContextCollector.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import OpenAIService
3+
import Parsing
34

45
public struct ChatContext {
56
public struct RetrievedContent {
@@ -39,3 +40,38 @@ public protocol ChatContextCollector {
3940
) async -> ChatContext
4041
}
4142

43+
public struct MessageScopeParser {
44+
public init() {}
45+
46+
public func callAsFunction(_ content: inout String) -> Set<String> {
47+
return parseScopes(&content)
48+
}
49+
50+
func parseScopes(_ prompt: inout String) -> Set<String> {
51+
guard !prompt.isEmpty else { return [] }
52+
do {
53+
let parser = Parse {
54+
"@"
55+
Many {
56+
Prefix { $0.isLetter }
57+
} separator: {
58+
"+"
59+
} terminator: {
60+
" "
61+
}
62+
Skip {
63+
Many {
64+
" "
65+
}
66+
}
67+
Rest()
68+
}
69+
let (scopes, rest) = try parser.parse(prompt)
70+
prompt = String(rest)
71+
return Set(scopes.map(String.init))
72+
} catch {
73+
return []
74+
}
75+
}
76+
}
77+

0 commit comments

Comments
 (0)