-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathInsertEditIntoFileTool.swift
More file actions
126 lines (105 loc) · 4.67 KB
/
InsertEditIntoFileTool.swift
File metadata and controls
126 lines (105 loc) · 4.67 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
import ConversationServiceProvider
import AppKit
import JSONRPC
import Foundation
import XcodeInspector
import Logger
import AXHelper
public class InsertEditIntoFileTool: ICopilotTool {
public static let name = ToolName.insertEditIntoFile
public func invokeTool(
_ request: InvokeClientToolRequest,
completion: @escaping (AnyJSONRPCResponse) -> Void,
chatHistoryUpdater: ChatHistoryUpdater?,
contextProvider: (any ToolContextProvider)?
) -> Bool {
guard let params = request.params,
let input = request.params?.input,
let code = input["code"]?.value as? String,
let filePath = input["filePath"]?.value as? String,
let contextProvider
else {
return true
}
let fileURL = URL(fileURLWithPath: filePath)
do {
let originalContent = try String(contentsOf: fileURL, encoding: .utf8)
try InsertEditIntoFileTool.applyEdit(for: fileURL, content: code, contextProvider: contextProvider)
contextProvider.updateFileEdits(
by: .init(fileURL: fileURL, originalContent: originalContent, modifiedContent: code, toolName: InsertEditIntoFileTool.name)
)
let editAgentRounds: [AgentRound] = [
.init(
roundId: params.roundId,
reply: "",
toolCalls: [
.init(
id: params.toolCallId,
name: params.name,
status: .completed,
invokeParams: params
)
]
)
]
if let chatHistoryUpdater {
chatHistoryUpdater(params.turnId, editAgentRounds)
}
completeResponse(request, response: code, completion: completion)
} catch {
Logger.client.error("Failed to apply edits, \(error)")
completeResponse(request, response: error.localizedDescription, completion: completion)
}
return true
}
public static func applyEdit(for fileURL: URL, content: String, contextProvider: (any ToolContextProvider), xcodeInstance: XcodeAppInstanceInspector) throws {
/// wait a while for opening file in xcode. (3 seconds)
var retryCount = 6
while retryCount > 0 {
guard xcodeInstance.realtimeDocumentURL != fileURL else { break }
retryCount -= 1
/// Failed to get the target documentURL
if retryCount == 0 {
return
}
Thread.sleep(forTimeInterval: 0.5)
}
guard xcodeInstance.realtimeDocumentURL == fileURL
else { throw NSError(domain: "The file \(fileURL) is not opened in Xcode", code: 0)}
/// keep change
guard let element: AXUIElement = try? xcodeInstance.appElement.copyValue(key: kAXFocusedUIElementAttribute)
else {
throw NSError(domain: "Failed to access xcode element", code: 0)
}
let value: String = (try? element.copyValue(key: kAXValueAttribute)) ?? ""
let lines = value.components(separatedBy: .newlines)
var isInjectedSuccess = false
try AXHelper().injectUpdatedCodeWithAccessibilityAPI(
.init(
content: content,
newSelection: nil,
modifications: [
.deletedSelection(
.init(start: .init(line: 0, character: 0), end: .init(line: lines.count - 1, character: (lines.last?.count ?? 100) - 1))
),
.inserted(0, [content])
]
),
focusElement: element,
onSuccess: {
isInjectedSuccess = true
}
)
if !isInjectedSuccess {
throw NSError(domain: "Failed to apply edit", code: 0)
}
}
public static func applyEdit(for fileURL: URL, content: String, contextProvider: (any ToolContextProvider)) throws {
guard let xcodeInstance = Utils.getXcode(by: contextProvider.chatTabInfo.workspacePath)
else {
throw NSError(domain: "The workspace \(contextProvider.chatTabInfo.workspacePath) is not opened in xcode", code: 0, userInfo: nil)
}
try Utils.openFileInXcode(fileURL: fileURL, xcodeInstance: xcodeInstance)
try applyEdit(for: fileURL, content: content, contextProvider: contextProvider, xcodeInstance: xcodeInstance)
}
}