Skip to content

Commit 956f712

Browse files
committed
Add Scope enum to represent scopes
1 parent f528d9a commit 956f712

File tree

9 files changed

+43
-29
lines changed

9 files changed

+43
-29
lines changed

Core/Sources/ChatContextCollectors/SystemInfoChatContextCollector/SystemInfoChatContextCollector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class SystemInfoChatContextCollector: ChatContextCollector {
1313

1414
public func generateContext(
1515
history: [ChatMessage],
16-
scopes: Set<String>,
16+
scopes: Set<ChatContext.Scope>,
1717
content: String,
1818
configuration: ChatGPTConfiguration
1919
) -> ChatContext {

Core/Sources/ChatContextCollectors/WebChatContextCollector/WebChatContextCollector.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public final class WebChatContextCollector: ChatContextCollector {
99

1010
public func generateContext(
1111
history: [ChatMessage],
12-
scopes: Set<String>,
12+
scopes: Set<ChatContext.Scope>,
1313
content: String,
1414
configuration: ChatGPTConfiguration
1515
) -> ChatContext {
16-
guard scopes.contains("web") || scopes.contains("w") else { return .empty }
16+
guard scopes.contains(.web) else { return .empty }
1717
let links = Self.detectLinks(from: history) + Self.detectLinks(from: content)
1818
let functions: [(any ChatGPTFunction)?] = [
1919
SearchFunction(maxTokens: configuration.maxTokens),

Core/Sources/ChatService/ChatService.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ import OpenAIService
66
import Preferences
77

88
public final class ChatService: ObservableObject {
9-
public enum Scope: String, Equatable, CaseIterable {
10-
case file
11-
case code
12-
case sense
13-
case project
14-
case web
15-
}
9+
public typealias Scope = ChatContext.Scope
1610

1711
public let memory: ContextAwareAutoManagedChatGPTMemory
1812
public let configuration: OverridingChatGPTConfiguration
@@ -96,7 +90,7 @@ public final class ChatService: ObservableObject {
9690
}
9791

9892
public func send(content: String) async throws {
99-
memory.contextController.defaultScopes = Set(defaultScopes.map(\.rawValue))
93+
memory.contextController.defaultScopes = defaultScopes
10094
guard !isReceivingMessage else { throw CancellationError() }
10195
let handledInPlugin = try await pluginController.handleContent(content)
10296
if handledInPlugin { return }

Core/Sources/ChatService/ContextAwareAutoManagedChatGPTMemory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class ContextAwareAutoManagedChatGPTMemory: ChatGPTMemory {
2424
}
2525

2626
init(
27-
configuration: ChatGPTConfiguration,
27+
configuration: OverridingChatGPTConfiguration,
2828
functionProvider: ChatFunctionProvider
2929
) {
3030
memory = AutoManagedChatGPTMemory(

Core/Sources/ChatService/DynamicContextController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ final class DynamicContextController {
88
let contextCollectors: [ChatContextCollector]
99
let memory: AutoManagedChatGPTMemory
1010
let functionProvider: ChatFunctionProvider
11-
let configuration: ChatGPTConfiguration
12-
var defaultScopes = [] as Set<String>
11+
let configuration: OverridingChatGPTConfiguration
12+
var defaultScopes = [] as Set<ChatContext.Scope>
1313

1414
convenience init(
1515
memory: AutoManagedChatGPTMemory,
1616
functionProvider: ChatFunctionProvider,
17-
configuration: ChatGPTConfiguration,
17+
configuration: OverridingChatGPTConfiguration,
1818
contextCollectors: ChatContextCollector...
1919
) {
2020
self.init(
@@ -28,7 +28,7 @@ final class DynamicContextController {
2828
init(
2929
memory: AutoManagedChatGPTMemory,
3030
functionProvider: ChatFunctionProvider,
31-
configuration: ChatGPTConfiguration,
31+
configuration: OverridingChatGPTConfiguration,
3232
contextCollectors: [ChatContextCollector]
3333
) {
3434
self.memory = memory
@@ -86,7 +86,7 @@ final class DynamicContextController {
8686
}
8787

8888
extension DynamicContextController {
89-
static func parseScopes(_ prompt: inout String) -> Set<String> {
89+
static func parseScopes(_ prompt: inout String) -> Set<ChatContext.Scope> {
9090
let parser = MessageScopeParser()
9191
return parser(&prompt)
9292
}

Pro

Submodule Pro updated from e6d0cac to 0747cfb

Tool/Sources/ChatContextCollector/ChatContextCollector.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import OpenAIService
33
import Parsing
44

55
public struct ChatContext {
6+
public enum Scope: String, Equatable, CaseIterable {
7+
case file
8+
case code
9+
case sense
10+
case project
11+
case web
12+
}
13+
614
public struct RetrievedContent {
715
public var content: String
816
public var priority: Int
@@ -31,10 +39,22 @@ public struct ChatContext {
3139
}
3240
}
3341

42+
public extension ChatContext.Scope {
43+
init?(text: String) {
44+
for scope in Self.allCases {
45+
if scope.rawValue.hasPrefix(text.lowercased()) {
46+
self = scope
47+
return
48+
}
49+
}
50+
return nil
51+
}
52+
}
53+
3454
public protocol ChatContextCollector {
3555
func generateContext(
3656
history: [ChatMessage],
37-
scopes: Set<String>,
57+
scopes: Set<ChatContext.Scope>,
3858
content: String,
3959
configuration: ChatGPTConfiguration
4060
) async -> ChatContext
@@ -43,11 +63,11 @@ public protocol ChatContextCollector {
4363
public struct MessageScopeParser {
4464
public init() {}
4565

46-
public func callAsFunction(_ content: inout String) -> Set<String> {
66+
public func callAsFunction(_ content: inout String) -> Set<ChatContext.Scope> {
4767
return parseScopes(&content)
4868
}
4969

50-
func parseScopes(_ prompt: inout String) -> Set<String> {
70+
func parseScopes(_ prompt: inout String) -> Set<ChatContext.Scope> {
5171
guard !prompt.isEmpty else { return [] }
5272
do {
5373
let parser = Parse {
@@ -68,7 +88,7 @@ public struct MessageScopeParser {
6888
}
6989
let (scopes, rest) = try parser.parse(prompt)
7090
prompt = String(rest)
71-
return Set(scopes.map(String.init))
91+
return Set(scopes.map(String.init).compactMap(ChatContext.Scope.init(text:)))
7292
} catch {
7393
return []
7494
}

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
1414

1515
public func generateContext(
1616
history: [ChatMessage],
17-
scopes: Set<String>,
17+
scopes: Set<ChatContext.Scope>,
1818
content: String,
1919
configuration: ChatGPTConfiguration
2020
) -> ChatContext {
2121
guard let info = getEditorInformation() else { return .empty }
2222
let context = getActiveDocumentContext(info)
2323
activeDocumentContext = context
2424

25-
guard scopes.contains("code") || scopes.contains("c") else {
26-
if scopes.contains("file") || scopes.contains("f") {
25+
guard scopes.contains(.code) else {
26+
if scopes.contains(.file) {
2727
var removedCode = context
2828
removedCode.focusedContext = nil
2929
return .init(

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/LegacyActiveDocumentChatContextCollector.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public struct LegacyActiveDocumentChatContextCollector: ChatContextCollector {
1010

1111
public func generateContext(
1212
history: [ChatMessage],
13-
scopes: Set<String>,
13+
scopes: Set<ChatContext.Scope>,
1414
content: String,
1515
configuration: ChatGPTConfiguration
1616
) -> ChatContext {
1717
guard let content = getEditorInformation() else { return .empty }
1818
let relativePath = content.relativePath
1919
let selectionRange = content.editorContent?.selections.first ?? .outOfScope
2020
let editorContent = {
21-
if scopes.contains("file") || scopes.contains("f") {
21+
if scopes.contains(.file) {
2222
return """
2323
File Content:```\(content.language.rawValue)
2424
\(content.editorContent?.content ?? "")
@@ -49,7 +49,7 @@ public struct LegacyActiveDocumentChatContextCollector: ChatContextCollector {
4949
}
5050
}
5151

52-
if UserDefaults.shared.value(for: \.useCodeScopeByDefaultInChatContext) {
52+
if UserDefaults.shared.value(for: \.enableCodeScopeByDefaultInChatContext) {
5353
return """
5454
Selected Code \
5555
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
@@ -58,7 +58,7 @@ public struct LegacyActiveDocumentChatContextCollector: ChatContextCollector {
5858
"""
5959
}
6060

61-
if scopes.contains("selection") || scopes.contains("s") {
61+
if scopes.contains(.code) {
6262
return """
6363
Selected Code \
6464
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)

0 commit comments

Comments
 (0)