forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolHandler.java
More file actions
57 lines (53 loc) · 1.86 KB
/
ToolHandler.java
File metadata and controls
57 lines (53 loc) · 1.86 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk.json;
import java.util.concurrent.CompletableFuture;
/**
* Functional interface for handling tool invocations from the AI assistant.
* <p>
* When the assistant decides to use a tool, it invokes this handler with the
* tool's arguments. The handler should perform the requested action and return
* the result.
*
* <h2>Example Implementation</h2>
*
* <pre>{@code
* // Option 1: Type-safe access with records (recommended)
* record SearchArgs(String query) {
* }
*
* ToolHandler handler = invocation -> {
* SearchArgs args = invocation.getArgumentsAs(SearchArgs.class);
* String result = performSearch(args.query());
* return CompletableFuture.completedFuture(result);
* };
*
* // Option 2: Map-based access
* ToolHandler handler = invocation -> {
* Map<String, Object> args = invocation.getArguments();
* String query = (String) args.get("query");
* String result = performSearch(query);
* return CompletableFuture.completedFuture(result);
* };
* }</pre>
*
* @see ToolDefinition
* @see ToolInvocation
* @since 1.0.0
*/
@FunctionalInterface
public interface ToolHandler {
/**
* Invokes the tool with the given invocation context.
* <p>
* The returned object will be serialized to JSON and sent back to the assistant
* as the tool's result. This can be a {@code String}, {@code Map}, or any
* JSON-serializable object.
*
* @param invocation
* the invocation context containing arguments
* @return a future that completes with the tool's result
*/
CompletableFuture<Object> invoke(ToolInvocation invocation);
}