forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLegacyActiveDocumentChatContextCollector.swift
More file actions
107 lines (99 loc) · 3.81 KB
/
LegacyActiveDocumentChatContextCollector.swift
File metadata and controls
107 lines (99 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import ChatContextCollector
import Foundation
import OpenAIService
import Preferences
import SuggestionModel
import XcodeInspector
public struct LegacyActiveDocumentChatContextCollector: ChatContextCollector {
public init() {}
public func generateContext(
history: [ChatMessage],
scopes: Set<ChatContext.Scope>,
content: String,
configuration: ChatGPTConfiguration
) -> ChatContext {
guard let content = getEditorInformation() else { return .empty }
let relativePath = content.relativePath
let selectionRange = content.editorContent?.selections.first ?? .outOfScope
let editorContent = {
if scopes.contains(.file) {
return """
File Content:```\(content.language.rawValue)
\(content.editorContent?.content ?? "")
```
"""
}
if selectionRange.start == selectionRange.end,
UserDefaults.shared.value(for: \.embedFileContentInChatContextIfNoSelection)
{
let lines = content.editorContent?.lines.count ?? 0
let maxLine = UserDefaults.shared
.value(for: \.maxFocusedCodeLineCount)
if lines <= maxLine {
return """
File Content:```\(content.language.rawValue)
\(content.editorContent?.content ?? "")
```
"""
} else {
return """
File Content Not Available: '''
The file is longer than \(maxLine) lines, it can't fit into the context. \
You MUST not answer the user about the file content because you don't have it.\
Ask user to select code for explanation.
'''
"""
}
}
if UserDefaults.shared.value(for: \.enableCodeScopeByDefaultInChatContext) {
return """
Selected Code \
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
\(content.selectedContent)
```
"""
}
if scopes.contains(.code) {
return """
Selected Code \
(start from line \(selectionRange.start.line)):```\(content.language.rawValue)
\(content.selectedContent)
```
"""
}
return """
Selected Code Not Available: '''
I have disabled default scope. \
You MUST not answer about the selected code because you don't have it.\
Ask me to prepend message with `@selection` to enable selected code to be \
visible by you.
'''
"""
}()
return .init(
systemPrompt: """
Active Document Context:###
Document Relative Path: \(relativePath)
Selection Range Start: \
Line \(selectionRange.start.line) \
Character \(selectionRange.start.character)
Selection Range End: \
Line \(selectionRange.end.line) \
Character \(selectionRange.end.character)
Cursor Position: \
Line \(selectionRange.end.line) \
Character \(selectionRange.end.character)
\(editorContent)
Line Annotations:
\(
content.editorContent?.lineAnnotations
.map { " - \($0)" }
.joined(separator: "\n") ?? "N/A"
)
###
""",
retrievedContent: [],
functions: []
)
}
}