|
1 | 1 | import Foundation |
2 | 2 | import OpenAIService |
| 3 | +import SuggestionModel |
3 | 4 | import XcodeInspector |
| 5 | +import Preferences |
4 | 6 |
|
5 | 7 | final class DynamicContextController { |
6 | 8 | let chatGPTService: any ChatGPTServiceType |
7 | | - |
| 9 | + |
8 | 10 | init(chatGPTService: any ChatGPTServiceType) { |
9 | 11 | self.chatGPTService = chatGPTService |
10 | 12 | } |
11 | | - |
| 13 | + |
12 | 14 | func updatePromptToMatchContent(systemPrompt: String) async throws { |
13 | | - |
| 15 | + let language = UserDefaults.shared.value(for: \.chatGPTLanguage) |
| 16 | + let content = getEditorInformation() |
| 17 | + let relativePath = content.documentURL.path |
| 18 | + .replacingOccurrences(of: content.projectURL.path, with: "") |
| 19 | + let selectionRange = content.editorContent?.selections.first ?? .outOfScope |
| 20 | + let contextualSystemPrompt = """ |
| 21 | + \(language.isEmpty ? "" : "You must always reply in \(language)") |
| 22 | + \(systemPrompt) |
| 23 | +
|
| 24 | + Active Document Context:### |
| 25 | + Document Relative Path: \(relativePath) |
| 26 | + Selection Range Start: \ |
| 27 | + Line \(selectionRange.start.line) \ |
| 28 | + Character \(selectionRange.start.character) |
| 29 | + Selection Range End: \ |
| 30 | + Line \(selectionRange.end.line) \ |
| 31 | + Character \(selectionRange.end.character) |
| 32 | + Cursor Position: Same as selection range end |
| 33 | + Selected Code (start from line \(selectionRange.end.line)):```\(content.language.rawValue) |
| 34 | + \(content.selectedContent) |
| 35 | + ``` |
| 36 | +
|
| 37 | + Line Annotations: |
| 38 | + \(content.editorContent?.lineAnnotations.map { "- \($0)" }.joined(separator: "\n") ?? "N/A") |
| 39 | + ### |
| 40 | + """ |
| 41 | + print(contextualSystemPrompt) |
| 42 | + await chatGPTService.mutateSystemPrompt(contextualSystemPrompt) |
14 | 43 | } |
15 | 44 | } |
16 | 45 |
|
17 | 46 | extension DynamicContextController { |
18 | | - func getEditorInformation() -> Any? { |
19 | | - guard let editor = XcodeInspector.shared.focusedEditor else { return nil } |
20 | | - let content = editor.content |
21 | | - |
22 | | - return nil |
| 47 | + struct Information { |
| 48 | + let editorContent: SourceEditor.Content? |
| 49 | + let selectedContent: String |
| 50 | + let documentURL: URL |
| 51 | + let projectURL: URL |
| 52 | + let language: CodeLanguage |
| 53 | + } |
| 54 | + |
| 55 | + func getEditorInformation() -> Information { |
| 56 | + let editorContent = XcodeInspector.shared.focusedEditor?.content |
| 57 | + let documentURL = XcodeInspector.shared.activeDocumentURL |
| 58 | + let projectURL = XcodeInspector.shared.activeProjectURL |
| 59 | + let language = languageIdentifierFromFileURL(documentURL) |
| 60 | + |
| 61 | + if let editorContent, let range = editorContent.selections.first { |
| 62 | + let startIndex = min( |
| 63 | + max(0, range.start.line), |
| 64 | + editorContent.lines.endIndex - 1 |
| 65 | + ) |
| 66 | + let endIndex = min( |
| 67 | + max(startIndex, range.end.line), |
| 68 | + editorContent.lines.endIndex - 1 |
| 69 | + ) |
| 70 | + let selectedContent = editorContent.lines[startIndex...endIndex] |
| 71 | + return .init( |
| 72 | + editorContent: editorContent, |
| 73 | + selectedContent: selectedContent.joined(), |
| 74 | + documentURL: documentURL, |
| 75 | + projectURL: projectURL, |
| 76 | + language: language |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + return .init( |
| 81 | + editorContent: editorContent, |
| 82 | + selectedContent: "", |
| 83 | + documentURL: documentURL, |
| 84 | + projectURL: projectURL, |
| 85 | + language: language |
| 86 | + ) |
23 | 87 | } |
24 | 88 | } |
| 89 | + |
0 commit comments