-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathGetTerminalOutputTool.swift
More file actions
33 lines (30 loc) · 1.27 KB
/
GetTerminalOutputTool.swift
File metadata and controls
33 lines (30 loc) · 1.27 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
import ConversationServiceProvider
import Foundation
import JSONRPC
import Terminal
public class GetTerminalOutputTool: ICopilotTool {
public func invokeTool(_ request: InvokeClientToolRequest, completion: @escaping (AnyJSONRPCResponse) -> Void, contextProvider: (any ToolContextProvider)?) -> Bool {
var result: String = ""
if let input = request.params?.input as? [String: AnyCodable], let terminalId = input["id"]?.value as? String{
let session = TerminalSessionManager.shared.getSession(for: terminalId)
result = session?.getCommandOutput() ?? "Terminal id \(terminalId) not found"
} else {
result = "Invalid arguments for \(ToolName.getTerminalOutput.rawValue) tool call"
}
let toolResult = LanguageModelToolResult(content: [
.init(value: result)
])
let jsonResult = try? JSONEncoder().encode(toolResult)
let jsonValue = (try? JSONDecoder().decode(JSONValue.self, from: jsonResult ?? Data())) ?? JSONValue.null
completion(
AnyJSONRPCResponse(
id: request.id,
result: JSONValue.array([
jsonValue,
JSONValue.null
])
)
)
return true
}
}