File tree Expand file tree Collapse file tree 3 files changed +42
-29
lines changed
Tool/Sources/ChatContextCollector Expand file tree Collapse file tree 3 files changed +42
-29
lines changed Original file line number Diff line number Diff line change 11import ChatContextCollector
22import Foundation
33import OpenAIService
4- import Parsing
54import Preferences
65import 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
8988extension 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
Original file line number Diff line number Diff line change 11import Foundation
22import OpenAIService
3+ import Parsing
34
45public 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+
You can’t perform that action at this time.
0 commit comments