Skip to content

Commit bfc748a

Browse files
committed
Update MoveToCodeAroundLineFunction to get the code directly instead
1 parent ccd8ef6 commit bfc748a

5 files changed

Lines changed: 101 additions & 202 deletions

File tree

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/ActiveDocumentChatContextCollector.swift

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,7 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
4545
var functions = [any ChatGPTFunction]()
4646

4747
if !isSensitive {
48-
// When there is a line annotation not in the focused area, the bot can move the focus
49-
// area
50-
// to the code covering the line of the annotation.
51-
52-
if let focusedContext = context.focusedContext,
53-
!focusedContext.otherLineAnnotations.isEmpty
54-
{
55-
functions.append(MoveToCodeAroundLineFunction(contextCollector: self))
56-
}
57-
58-
if context.focusedContext == nil, !context.lineAnnotations.isEmpty {
59-
functions.append(MoveToCodeAroundLineFunction(contextCollector: self))
60-
}
48+
functions.append(GetCodeCodeAroundLineFunction(contextCollector: self))
6149
}
6250

6351
return .init(
@@ -89,9 +77,8 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
8977
let start = """
9078
## File and Code Scope
9179
92-
You can use the following context to answer my questions about the editing document \
93-
or code. The context shows only a part of the code in the editing document, and will \
94-
change during the conversation, so it may not match our conversation.
80+
You can use the following context to answer my questions about the editing document.\
81+
The context shows only a part of the code in the editing document.
9582
9683
\(
9784
context.focusedContext == nil
@@ -122,7 +109,9 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
122109
Ask the user to select the code in the editor to get help. Also tell them the file is in gitignore.
123110
"""
124111
: """
125-
Focused Code (start from line \(focusedContext.codeRange.start.line + 1)):
112+
Focused Code (from line \(
113+
focusedContext.codeRange.start.line + 1
114+
) to line \(focusedContext.codeRange.end.line + 1)):
126115
```\(context.language.rawValue)
127116
\(focusedContext.code)
128117
```
@@ -131,8 +120,8 @@ public final class ActiveDocumentChatContextCollector: ChatContextCollector {
131120
let fileAnnotations = focusedContext.otherLineAnnotations.isEmpty || isSensitive
132121
? ""
133122
: """
134-
Other Annotations:\"""
135-
(They are not inside the focused code. You don't known how to handle them until you get the code at the line)
123+
Out-of-scope Annotations:\"""
124+
(They are not inside the focused code. You can get the code at the line for details)
136125
\(
137126
focusedContext.otherLineAnnotations
138127
.map(convertAnnotationToText)

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/Functions/ExpandFocusRangeFunction.swift

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import ASTParser
2+
import Foundation
3+
import OpenAIService
4+
import SuggestionModel
5+
6+
struct GetCodeCodeAroundLineFunction: ChatGPTFunction {
7+
struct Arguments: Codable {
8+
var line: Int
9+
}
10+
11+
struct Result: ChatGPTFunctionResult {
12+
var range: CursorRange
13+
var content: String
14+
var language: CodeLanguage
15+
16+
var botReadableContent: String {
17+
"""
18+
Code in range \(range)
19+
```\(language.rawValue)
20+
\(content)
21+
```
22+
"""
23+
}
24+
}
25+
26+
struct E: Error, LocalizedError {
27+
var errorDescription: String?
28+
}
29+
30+
var name: String {
31+
"getCodeAtLine"
32+
}
33+
34+
var description: String {
35+
"Get the code at the given line. You must ONLY call it when the user give you a specific line or the user ask about an out of scope annotation."
36+
}
37+
38+
var argumentSchema: JSONSchemaValue { [
39+
.type: "object",
40+
.properties: [
41+
"line": [
42+
.type: "number",
43+
.description: "The line number in the file",
44+
],
45+
],
46+
.required: ["line"],
47+
] }
48+
49+
weak var contextCollector: ActiveDocumentChatContextCollector?
50+
51+
init(contextCollector: ActiveDocumentChatContextCollector) {
52+
self.contextCollector = contextCollector
53+
}
54+
55+
func prepare(reportProgress: @escaping (String) async -> Void) async {
56+
await reportProgress("Finding code around..")
57+
}
58+
59+
func call(
60+
arguments: Arguments,
61+
reportProgress: @escaping (String) async -> Void
62+
) async throws -> Result {
63+
guard var activeDocumentContext = contextCollector?.activeDocumentContext else {
64+
throw E(errorDescription: "No active document found.")
65+
}
66+
await reportProgress("Reading code around line \(arguments.line)..")
67+
activeDocumentContext.moveToCodeAroundLine(max(arguments.line - 1, 0))
68+
guard let newContext = activeDocumentContext.focusedContext else {
69+
let progress = "Failed to read code around line \(arguments.line)..)"
70+
await reportProgress(progress)
71+
throw E(errorDescription: progress)
72+
}
73+
let progress = "Finish reading code at \(newContext.codeRange)"
74+
await reportProgress(progress)
75+
return .init(
76+
range: newContext.codeRange,
77+
content: newContext.code
78+
.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline)
79+
.enumerated()
80+
.map {
81+
let (index, content) = $0
82+
if index + newContext.codeRange.start.line == arguments.line - 1 {
83+
return content + " // <--- line \(arguments.line)"
84+
} else {
85+
return content
86+
}
87+
}
88+
.joined(separator: "\n"),
89+
language: activeDocumentContext.language
90+
)
91+
}
92+
}
93+

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/Functions/MoveToCodeAroundLineFunction.swift

Lines changed: 0 additions & 68 deletions
This file was deleted.

Tool/Sources/ChatContextCollectors/ActiveDocumentChatContextCollector/Functions/MoveToFocusedCodeFunction.swift

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)