Skip to content

Commit 4cffaa4

Browse files
committed
Support reading the whole file with @file
1 parent c8222d5 commit 4cffaa4

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

Core/Sources/ChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@ import XcodeInspector
66
public struct ActiveDocumentChatContextCollector: ChatContextCollector {
77
public init() {}
88

9-
public func generateSystemPrompt(oldMessages: [String]) -> String {
9+
public func generateSystemPrompt(history: [String], content prompt: String) -> String {
1010
let content = getEditorInformation()
1111
let relativePath = content.documentURL.path
1212
.replacingOccurrences(of: content.projectURL.path, with: "")
1313
let selectionRange = content.editorContent?.selections.first ?? .outOfScope
1414
let editorContent = {
15+
if prompt.hasPrefix("@file") {
16+
return """
17+
File Content:```\(content.language.rawValue)
18+
\(content.editorContent?.content ?? "")
19+
```
20+
"""
21+
}
22+
1523
if selectionRange.start == selectionRange.end,
1624
UserDefaults.shared.value(for: \.embedFileContentInChatContextIfNoSelection)
1725
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
22

33
public protocol ChatContextCollector {
4-
func generateSystemPrompt(oldMessages: [String]) -> String
4+
func generateSystemPrompt(history: [String], content: String) -> String
55
}

Core/Sources/ChatService/ChatService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class ChatService: ObservableObject {
4747
try await contextController.updatePromptToMatchContent(systemPrompt: """
4848
\(systemPrompt)
4949
\(extraSystemPrompt)
50-
""")
50+
""", content: content)
5151

5252
_ = try await chatGPTService.send(content: content, summary: nil)
5353
}

Core/Sources/ChatService/DynamicContextController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class DynamicContextController {
1313
self.contextCollectors = contextCollectors
1414
}
1515

16-
func updatePromptToMatchContent(systemPrompt: String) async throws {
16+
func updatePromptToMatchContent(systemPrompt: String, content: String) async throws {
1717
let language = UserDefaults.shared.value(for: \.chatGPTLanguage)
1818
let oldMessages = (await chatGPTService.history).map(\.content)
1919
let contextualSystemPrompt = """
@@ -22,7 +22,7 @@ final class DynamicContextController {
2222
2323
\(
2424
contextCollectors
25-
.map { $0.generateSystemPrompt(oldMessages: oldMessages) }
25+
.map { $0.generateSystemPrompt(history: oldMessages, content: content) }
2626
.joined(separator: "\n")
2727
)
2828
"""

0 commit comments

Comments
 (0)