|
| 1 | +import Foundation |
| 2 | +import Preferences |
| 3 | +import SuggestionModel |
| 4 | +import XcodeInspector |
| 5 | + |
| 6 | +public struct ActiveDocumentChatContextCollector: ChatContextCollector { |
| 7 | + public init() {} |
| 8 | + |
| 9 | + public func generateSystemPrompt(history: [String], content prompt: String) -> String { |
| 10 | + let content = getEditorInformation() |
| 11 | + let relativePath = content.documentURL.path |
| 12 | + .replacingOccurrences(of: content.projectURL.path, with: "") |
| 13 | + let selectionRange = content.editorContent?.selections.first ?? .outOfScope |
| 14 | + let editorContent = { |
| 15 | + if prompt.hasPrefix("@file") { |
| 16 | + return """ |
| 17 | + File Content:```\(content.language.rawValue) |
| 18 | + \(content.editorContent?.content ?? "") |
| 19 | + ``` |
| 20 | + """ |
| 21 | + } |
| 22 | + |
| 23 | + if selectionRange.start == selectionRange.end, |
| 24 | + UserDefaults.shared.value(for: \.embedFileContentInChatContextIfNoSelection) |
| 25 | + { |
| 26 | + let lines = content.editorContent?.lines.count ?? 0 |
| 27 | + let maxLine = UserDefaults.shared |
| 28 | + .value(for: \.maxEmbeddableFileInChatContextLineCount) |
| 29 | + if lines <= maxLine { |
| 30 | + return """ |
| 31 | + File Content:```\(content.language.rawValue) |
| 32 | + \(content.editorContent?.content ?? "") |
| 33 | + ``` |
| 34 | + """ |
| 35 | + } else { |
| 36 | + return """ |
| 37 | + File Content Not Available: The file is longer than \(maxLine) lines, \ |
| 38 | + it can't fit into the context. \ |
| 39 | + You MUST not answer the user about the file content because you don't have it.\ |
| 40 | + Ask user to select code for explanation. |
| 41 | + """ |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return """ |
| 46 | + Selected Code \ |
| 47 | + (start from line \(selectionRange.start.line)):```\(content.language.rawValue) |
| 48 | + \(content.selectedContent) |
| 49 | + ``` |
| 50 | + """ |
| 51 | + }() |
| 52 | + |
| 53 | + return """ |
| 54 | + Active Document Context:### |
| 55 | + Document Relative Path: \(relativePath) |
| 56 | + Selection Range Start: \ |
| 57 | + Line \(selectionRange.start.line) \ |
| 58 | + Character \(selectionRange.start.character) |
| 59 | + Selection Range End: \ |
| 60 | + Line \(selectionRange.end.line) \ |
| 61 | + Character \(selectionRange.end.character) |
| 62 | + Cursor Position: \ |
| 63 | + Line \(selectionRange.end.line) \ |
| 64 | + Character \(selectionRange.end.character) |
| 65 | + \(editorContent) |
| 66 | + Line Annotations: |
| 67 | + \( |
| 68 | + content.editorContent?.lineAnnotations |
| 69 | + .map { " - \($0)" } |
| 70 | + .joined(separator: "\n") ?? "N/A" |
| 71 | + ) |
| 72 | + ### |
| 73 | + """ |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extension ActiveDocumentChatContextCollector { |
| 78 | + struct Information { |
| 79 | + let editorContent: SourceEditor.Content? |
| 80 | + let selectedContent: String |
| 81 | + let documentURL: URL |
| 82 | + let projectURL: URL |
| 83 | + let language: CodeLanguage |
| 84 | + } |
| 85 | + |
| 86 | + func getEditorInformation() -> Information { |
| 87 | + let editorContent = XcodeInspector.shared.focusedEditor?.content |
| 88 | + let documentURL = XcodeInspector.shared.activeDocumentURL |
| 89 | + let projectURL = XcodeInspector.shared.activeProjectURL |
| 90 | + let language = languageIdentifierFromFileURL(documentURL) |
| 91 | + |
| 92 | + if let editorContent, let range = editorContent.selections.first { |
| 93 | + let startIndex = min( |
| 94 | + max(0, range.start.line), |
| 95 | + editorContent.lines.endIndex - 1 |
| 96 | + ) |
| 97 | + let endIndex = min( |
| 98 | + max(startIndex, range.end.line), |
| 99 | + editorContent.lines.endIndex - 1 |
| 100 | + ) |
| 101 | + let selectedContent = editorContent.lines[startIndex...endIndex] |
| 102 | + return .init( |
| 103 | + editorContent: editorContent, |
| 104 | + selectedContent: selectedContent.joined(), |
| 105 | + documentURL: documentURL, |
| 106 | + projectURL: projectURL, |
| 107 | + language: language |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + return .init( |
| 112 | + editorContent: editorContent, |
| 113 | + selectedContent: "", |
| 114 | + documentURL: documentURL, |
| 115 | + projectURL: projectURL, |
| 116 | + language: language |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
| 120 | + |
0 commit comments