-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathRunInTerminalTool.swift
More file actions
42 lines (39 loc) · 1.88 KB
/
RunInTerminalTool.swift
File metadata and controls
42 lines (39 loc) · 1.88 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
import ConversationServiceProvider
import Terminal
import XcodeInspector
import JSONRPC
public class RunInTerminalTool: ICopilotTool {
public func invokeTool(_ request: InvokeClientToolRequest, completion: @escaping (AnyJSONRPCResponse) -> Void, contextProvider: (any ToolContextProvider)?) -> Bool {
let params = request.params!
Task {
var currentDirectory: String = ""
if let workspacePath = contextProvider?.chatTabInfo.workspacePath,
let xcodeIntance = Utils.getXcode(by: workspacePath) {
currentDirectory = xcodeIntance.realtimeProjectURL?.path ?? xcodeIntance.projectRootURL?.path ?? ""
} else {
currentDirectory = await XcodeInspector.shared.safe.realtimeActiveProjectURL?.path ?? ""
}
if let input = params.input {
let command = input["command"]?.value as? String
let isBackground = input["isBackground"]?.value as? Bool
let toolId = params.toolCallId
let session = TerminalSessionManager.shared.createSession(for: toolId)
if isBackground == true {
session.executeCommand(
currentDirectory: currentDirectory,
command: command!) { result in
// do nothing
}
completeResponse(request, response: "Command is running in terminal with ID=\(toolId)", completion: completion)
} else {
session.executeCommand(
currentDirectory: currentDirectory,
command: command!) { result in
self.completeResponse(request, response: result.output, completion: completion)
}
}
}
}
return true
}
}