-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathICopilotTool.swift
More file actions
91 lines (85 loc) · 3.45 KB
/
ICopilotTool.swift
File metadata and controls
91 lines (85 loc) · 3.45 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
import ChatTab
import ConversationServiceProvider
import Foundation
import JSONRPC
import ChatAPIService
public protocol ToolContextProvider {
// MARK: insert_edit_into_file
var chatTabInfo: ChatTabInfo { get }
func updateFileEdits(by fileEdit: FileEdit) -> Void
func notifyChangeTextDocument(fileURL: URL, content: String, version: Int) async throws
func updateChatHistory(_ turnId: String, editAgentRounds: [AgentRound], fileEdits: [FileEdit])
}
public protocol ICopilotTool {
/**
* Invokes the Copilot tool with the given request.
* - Parameters:
* - request: The tool invocation request.
* - completion: Closure called with JSON-RPC response when tool execution completes.
* - contextProvider: Optional provider that supplies additional context information
* needed for tool execution, such as chat tab data and file editing capabilities.
* - Returns: Boolean indicating if the tool call has completed. True if the tool call is completed, false otherwise.
*/
func invokeTool(
_ request: InvokeClientToolRequest,
completion: @escaping (AnyJSONRPCResponse) -> Void,
contextProvider: ToolContextProvider?
) -> Bool
}
extension ICopilotTool {
/**
* Completes a tool response.
* - Parameters:
* - request: The original tool invocation request.
* - status: The completion status of the tool execution (success, error, or cancelled).
* - response: The string value to include in the response content.
* - completion: The completion handler to call with the response.
*/
func completeResponse(
_ request: InvokeClientToolRequest,
status: ToolInvocationStatus = .success,
response: String = "",
completion: @escaping (AnyJSONRPCResponse) -> Void
) {
completeResponses(
request,
status: status,
responses: [response],
completion: completion
)
}
///
/// Completes a tool response with multiple data entries.
/// - Parameters:
/// - request: The original tool invocation request.
/// - status: The completion status of the tool execution (success, error, or cancelled).
/// - responses: Array of string values to include in the response content.
/// - completion: The completion handler to call with the response.
///
func completeResponses(
_ request: InvokeClientToolRequest,
status: ToolInvocationStatus = .success,
responses: [String],
completion: @escaping (AnyJSONRPCResponse) -> Void
) {
let toolResult = LanguageModelToolResult(status: status, content: responses.map { response in
LanguageModelToolResult.Content(value: response)
})
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,
])
)
)
}
}
extension ChatService: ToolContextProvider {
public func updateChatHistory(_ turnId: String, editAgentRounds: [AgentRound], fileEdits: [FileEdit] = []) {
appendToolCallHistory(turnId: turnId, editAgentRounds: editAgentRounds, fileEdits: fileEdits)
}
}