-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSessionHooks.java
More file actions
166 lines (152 loc) · 4.93 KB
/
SessionHooks.java
File metadata and controls
166 lines (152 loc) · 4.93 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk.json;
/**
* Hook handlers configuration for a session.
* <p>
* Hooks allow you to intercept and modify various session events including tool
* execution, user prompts, and session lifecycle events.
*
* <h2>Example Usage</h2>
*
* <pre>{@code
* var hooks = new SessionHooks().setOnPreToolUse((input, invocation) -> {
* System.out.println("Tool being called: " + input.getToolName());
* return CompletableFuture.completedFuture(PreToolUseHookOutput.allow());
* }).setOnPostToolUse((input, invocation) -> {
* System.out.println("Tool result: " + input.getToolResult());
* return CompletableFuture.completedFuture(null);
* }).setOnUserPromptSubmitted((input, invocation) -> {
* System.out.println("User prompt: " + input.prompt());
* return CompletableFuture.completedFuture(null);
* }).setOnSessionStart((input, invocation) -> {
* System.out.println("Session started: " + input.source());
* return CompletableFuture.completedFuture(null);
* }).setOnSessionEnd((input, invocation) -> {
* System.out.println("Session ended: " + input.reason());
* return CompletableFuture.completedFuture(null);
* });
*
* var session = client.createSession(new SessionConfig().setHooks(hooks)).get();
* }</pre>
*
* @since 1.0.6
*/
public class SessionHooks {
private PreToolUseHandler onPreToolUse;
private PostToolUseHandler onPostToolUse;
private UserPromptSubmittedHandler onUserPromptSubmitted;
private SessionStartHandler onSessionStart;
private SessionEndHandler onSessionEnd;
/**
* Gets the pre-tool-use handler.
*
* @return the handler, or {@code null} if not set
*/
public PreToolUseHandler getOnPreToolUse() {
return onPreToolUse;
}
/**
* Sets the handler called before a tool is executed.
*
* @param onPreToolUse
* the handler
* @return this instance for method chaining
*/
public SessionHooks setOnPreToolUse(PreToolUseHandler onPreToolUse) {
this.onPreToolUse = onPreToolUse;
return this;
}
/**
* Gets the post-tool-use handler.
*
* @return the handler, or {@code null} if not set
*/
public PostToolUseHandler getOnPostToolUse() {
return onPostToolUse;
}
/**
* Sets the handler called after a tool has been executed.
*
* @param onPostToolUse
* the handler
* @return this instance for method chaining
*/
public SessionHooks setOnPostToolUse(PostToolUseHandler onPostToolUse) {
this.onPostToolUse = onPostToolUse;
return this;
}
/**
* Gets the user-prompt-submitted handler.
*
* @return the handler, or {@code null} if not set
* @since 1.0.7
*/
public UserPromptSubmittedHandler getOnUserPromptSubmitted() {
return onUserPromptSubmitted;
}
/**
* Sets the handler called when the user submits a prompt.
*
* @param onUserPromptSubmitted
* the handler
* @return this instance for method chaining
* @since 1.0.7
*/
public SessionHooks setOnUserPromptSubmitted(UserPromptSubmittedHandler onUserPromptSubmitted) {
this.onUserPromptSubmitted = onUserPromptSubmitted;
return this;
}
/**
* Gets the session-start handler.
*
* @return the handler, or {@code null} if not set
* @since 1.0.7
*/
public SessionStartHandler getOnSessionStart() {
return onSessionStart;
}
/**
* Sets the handler called when a session starts.
*
* @param onSessionStart
* the handler
* @return this instance for method chaining
* @since 1.0.7
*/
public SessionHooks setOnSessionStart(SessionStartHandler onSessionStart) {
this.onSessionStart = onSessionStart;
return this;
}
/**
* Gets the session-end handler.
*
* @return the handler, or {@code null} if not set
* @since 1.0.7
*/
public SessionEndHandler getOnSessionEnd() {
return onSessionEnd;
}
/**
* Sets the handler called when a session ends.
*
* @param onSessionEnd
* the handler
* @return this instance for method chaining
* @since 1.0.7
*/
public SessionHooks setOnSessionEnd(SessionEndHandler onSessionEnd) {
this.onSessionEnd = onSessionEnd;
return this;
}
/**
* Returns whether any hooks are registered.
*
* @return {@code true} if at least one hook handler is set
*/
public boolean hasHooks() {
return onPreToolUse != null || onPostToolUse != null || onUserPromptSubmitted != null || onSessionStart != null
|| onSessionEnd != null;
}
}