77import com .agentclientprotocol .sdk .annotation .UnstableAcpApi ;
88import com .agentclientprotocol .sdk .json .TypeRef ;
99import com .agentclientprotocol .sdk .spec .AcpSchema ;
10+ import com .agentclientprotocol .sdk .spec .ExtensionMethods ;
1011import com .agentclientprotocol .sdk .util .Assert ;
1112
1213/**
1314 * The setters of {@link AcpAgent.AsyncAgentBuilder} that take an {@link AgentAwareHandler}: one
14- * overload of each typed request setter but {@code promptHandler}, registering a handler that also
15- * receives the agent. Kept apart from the builder only to keep that class small; it is part of the
15+ * overload of each typed request setter but {@code promptHandler}, and of each extension handler
16+ * setter, registering a handler that also receives the agent. Kept apart from the builder only to keep that class small; it is part of the
1617 * builder's API.
1718 */
1819abstract class AsyncAgentAwareSetters <B > {
@@ -25,6 +26,10 @@ abstract class AsyncAgentAwareSetters<B> {
2526 abstract <T > B request (String method , TypeRef <T > requestType ,
2627 AgentHandlers .RequestHandler <T > handler );
2728
29+ /** Registers the handler of one notification method. */
30+ abstract <T > B notification (String method , TypeRef <T > notificationType ,
31+ AgentHandlers .NotificationHandler <T > handler );
32+
2833 /**
2934 * Sets the handler for {@code initialize}, as {@link AcpAgent.AsyncAgentBuilder#initializeHandler(AcpAgent.InitializeHandler)} does, for a handler
3035 * that also needs the agent it serves, for example to send session updates
@@ -239,4 +244,79 @@ public B disableProviderHandler(
239244 }, handler ::handle );
240245 }
241246
247+ /**
248+ * Registers the handler for a custom extension request ({@code _}-prefixed method name, ACP v1
249+ * Extensibility) from the client, its params read as the given type, as
250+ * {@link AcpAgent.AsyncAgentBuilder#extRequestHandler(String, TypeRef, AcpAgent.ExtRequestHandler)}
251+ * does, for a handler that also needs the agent it serves, for example to call the client back
252+ * ({@link AcpAsyncAgent#sendExtRequest(String, Object, TypeRef)}) or send session updates. Name
253+ * the agent parameter {@code self}, not the name the built agent is assigned to.
254+ * @param <T> the params type
255+ * @param <R> the result type, any type the JSON mapper can write
256+ * @param method the method name, which must start with {@code _} (for example
257+ * {@code _example.com/workspace/buffers})
258+ * @param paramsType the type the params are read as; must not be null
259+ * @param handler the handler; it receives the params and the agent; must not be null
260+ * @return this builder
261+ * @throws IllegalArgumentException if the method name does not start with {@code _}
262+ */
263+ public <T , R > B extRequestHandler (String method , TypeRef <T > paramsType , AgentAwareHandler <T , R > handler ) {
264+ ExtensionMethods .requireExtension (method );
265+ Assert .notNull (paramsType , "Params type must not be null" );
266+ Assert .notNull (handler , "Handler must not be null" );
267+ return request (method , paramsType , handler ::handle );
268+ }
269+
270+ /**
271+ * Registers the handler for a custom extension request ({@code _}-prefixed method name) from
272+ * the client, its params delivered as the raw JSON value (a {@code Map}, {@code List},
273+ * {@code String}, {@code Number} or {@code Boolean}), for a handler that also needs the agent
274+ * it serves.
275+ * @param <R> the result type, any type the JSON mapper can write
276+ * @param method the method name, which must start with {@code _}
277+ * @param handler the handler; it receives the params and the agent; must not be null
278+ * @return this builder
279+ * @throws IllegalArgumentException if the method name does not start with {@code _}
280+ * @see #extRequestHandler(String, TypeRef, AgentAwareHandler)
281+ */
282+ public <R > B extRequestHandler (String method , AgentAwareHandler <Object , R > handler ) {
283+ return extRequestHandler (method , AgentHandlers .RAW_PARAMS , handler );
284+ }
285+
286+ /**
287+ * Registers the handler for a custom extension notification ({@code _}-prefixed method name)
288+ * from the client, its params read as the given type, as
289+ * {@link AcpAgent.AsyncAgentBuilder#extNotificationHandler(String, TypeRef, AcpAgent.ExtNotificationHandler)}
290+ * does, for a handler that also needs the agent it serves, for example to answer with a
291+ * notification of its own ({@link AcpAsyncAgent#sendExtNotification(String, Object)}). Its
292+ * {@code Mono} completes when the notification is handled; a handler that fails is only
293+ * logged.
294+ * @param <T> the params type
295+ * @param method the method name, which must start with {@code _}
296+ * @param paramsType the type the params are read as; must not be null
297+ * @param handler the handler; it receives the params and the agent; must not be null
298+ * @return this builder
299+ * @throws IllegalArgumentException if the method name does not start with {@code _}
300+ */
301+ public <T > B extNotificationHandler (String method , TypeRef <T > paramsType , AgentAwareHandler <T , Void > handler ) {
302+ ExtensionMethods .requireExtension (method );
303+ Assert .notNull (paramsType , "Params type must not be null" );
304+ Assert .notNull (handler , "Handler must not be null" );
305+ return notification (method , paramsType , handler ::handle );
306+ }
307+
308+ /**
309+ * Registers the handler for a custom extension notification ({@code _}-prefixed method name)
310+ * from the client, its params delivered as the raw JSON value, for a handler that also needs
311+ * the agent it serves.
312+ * @param method the method name, which must start with {@code _}
313+ * @param handler the handler; it receives the params and the agent; must not be null
314+ * @return this builder
315+ * @throws IllegalArgumentException if the method name does not start with {@code _}
316+ * @see #extNotificationHandler(String, TypeRef, AgentAwareHandler)
317+ */
318+ public B extNotificationHandler (String method , AgentAwareHandler <Object , Void > handler ) {
319+ return extNotificationHandler (method , AgentHandlers .RAW_PARAMS , handler );
320+ }
321+
242322}
0 commit comments