-
-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathActiveDocumentChatContextCollector.swift
More file actions
203 lines (175 loc) · 7.01 KB
/
ActiveDocumentChatContextCollector.swift
File metadata and controls
203 lines (175 loc) · 7.01 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import ASTParser
import ChatContextCollector
import Dependencies
import FocusedCodeFinder
import Foundation
import GitIgnoreCheck
import OpenAIService
import Preferences
import SuggestionModel
import XcodeInspector
public final class ActiveDocumentChatContextCollector: ChatContextCollector {
public init() {}
public var activeDocumentContext: ActiveDocumentContext?
@Dependency(\.gitIgnoredChecker) var gitIgnoredChecker
public func generateContext(
history: [ChatMessage],
scopes: Set<ChatContext.Scope>,
content: String,
configuration: ChatGPTConfiguration
) async -> ChatContext {
guard let info = getEditorInformation() else { return .empty }
let context = getActiveDocumentContext(info)
activeDocumentContext = context
let isSensitive = await gitIgnoredChecker.checkIfGitIgnored(fileURL: info.documentURL)
guard scopes.contains(.code)
else {
if scopes.contains(.file) {
var removedCode = context
removedCode.focusedContext = nil
return .init(
systemPrompt: extractSystemPrompt(removedCode, isSensitive: isSensitive),
retrievedContent: [],
functions: []
)
}
return .empty
}
var functions = [any ChatGPTFunction]()
if !isSensitive {
// When the bot is not focusing on any code, or the focusing area is not the user's
// selection, it can move the focus back to the user's selection.
if context.focusedContext == nil ||
!(context.focusedContext?.codeRange.contains(context.selectionRange) ?? false)
{
functions.append(MoveToFocusedCodeFunction(contextCollector: self))
}
// When there is a line annotation not in the focused area, the bot can move the focus
// area
// to the code covering the line of the annotation.
if let focusedContext = context.focusedContext,
!focusedContext.otherLineAnnotations.isEmpty
{
functions.append(MoveToCodeAroundLineFunction(contextCollector: self))
}
if context.focusedContext == nil, !context.lineAnnotations.isEmpty {
functions.append(MoveToCodeAroundLineFunction(contextCollector: self))
}
}
return .init(
systemPrompt: extractSystemPrompt(context, isSensitive: isSensitive),
retrievedContent: [],
functions: functions
)
}
func getActiveDocumentContext(_ info: EditorInformation) -> ActiveDocumentContext {
var activeDocumentContext = activeDocumentContext ?? .init(
documentURL: .init(fileURLWithPath: "/"),
relativePath: "",
language: .builtIn(.swift),
fileContent: "",
lines: [],
selectedCode: "",
selectionRange: .outOfScope,
lineAnnotations: [],
imports: [],
includes: []
)
activeDocumentContext.update(info)
return activeDocumentContext
}
func extractSystemPrompt(_ context: ActiveDocumentContext, isSensitive: Bool) -> String {
let start = """
## File and Code Scope
You can use the following context to answer my questions about the editing document \
or code. The context shows only a part of the code in the editing document, and will \
change during the conversation, so it may not match our conversation.
\(
context.focusedContext == nil
? ""
: "When you don't known what I am asking, I am probably referring to the code."
)
### Editing Document Context
"""
let relativePath = "Document Relative Path: \(context.relativePath)"
let language = "Language: \(context.language.rawValue)"
if let focusedContext = context.focusedContext {
let codeContext = focusedContext.context.isEmpty || isSensitive
? ""
: """
Focused Context:
```
\(focusedContext.context.map(\.signature).joined(separator: "\n"))
```
"""
let codeRange = "Focused Range [line, character]: \(focusedContext.codeRange)"
let code = context.selectionRange.isEmpty && isSensitive
? """
The file is in gitignore, you can't read the file.
Ask the user to select the code in the editor to get help. Also tell them the file is in gitignore.
"""
: """
Focused Code (start from line \(focusedContext.codeRange.start.line + 1)):
```\(context.language.rawValue)
\(focusedContext.code)
```
"""
let fileAnnotations = focusedContext.otherLineAnnotations.isEmpty || isSensitive
? ""
: """
Other Annotations:\"""
(They are not inside the focused code. You don't known how to handle them until you get the code at the line)
\(
focusedContext.otherLineAnnotations
.map(convertAnnotationToText)
.joined(separator: "\n")
)
\"""
"""
let codeAnnotations = focusedContext.lineAnnotations.isEmpty || isSensitive
? ""
: """
Annotations Inside Focused Range:\"""
\(
focusedContext.lineAnnotations
.map(convertAnnotationToText)
.joined(separator: "\n")
)
\"""
"""
return [
start,
relativePath,
language,
codeContext,
codeRange,
code,
codeAnnotations,
fileAnnotations,
]
.filter { !$0.isEmpty }
.joined(separator: "\n\n")
} else {
let selectionRange = "Selection Range [line, character]: \(context.selectionRange)"
let lineAnnotations = context.lineAnnotations.isEmpty || isSensitive
? ""
: """
Line Annotations:\"""
\(context.lineAnnotations.map(convertAnnotationToText).joined(separator: "\n"))
\"""
"""
return [
start,
relativePath,
language,
lineAnnotations,
selectionRange,
]
.filter { !$0.isEmpty }
.joined(separator: "\n\n")
}
}
func convertAnnotationToText(_ annotation: EditorInformation.LineAnnotation) -> String {
return "- Line \(annotation.line), \(annotation.type): \(annotation.message)"
}
}