Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Stdio from "effect/Stdio";
4import * as Layer from "effect/Layer";
5import * as Schema from "effect/Schema";
6import * as Scope from "effect/Scope";
7import * as Stream from "effect/Stream";
8import * as RpcClient from "effect/unstable/rpc/RpcClient";
9import * as RpcMessage from "effect/unstable/rpc/RpcMessage";
10import * as RpcServer from "effect/unstable/rpc/RpcServer";
11import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
12
13import * as AcpError from "./errors.ts";
14import * as AcpProtocol from "./protocol.ts";
15import * as AcpRpcs from "./rpc.ts";
16import * as AcpSchema from "./_generated/schema.gen.ts";
17import { AGENT_METHODS, CLIENT_METHODS } from "./_generated/meta.gen.ts";
18import {
19 callRpc,
20 decodeExtNotificationRegistration,
21 decodeExtRequestRegistration,
22 runHandler,
23} from "./_internal/shared.ts";
24import { makeChildStdio, makeTerminationError } from "./_internal/stdio.ts";
25
26export interface AcpClientOptions {
27 readonly logIncoming?: boolean;
28 readonly logOutgoing?: boolean;
29 readonly logger?: (event: AcpProtocol.AcpProtocolLogEvent) => Effect.Effect<void, never>;
30 readonly onIncomingRequest?: AcpProtocol.AcpPatchedProtocolOptions["onIncomingRequest"];
31 readonly onTermination?: AcpProtocol.AcpPatchedProtocolOptions["onTermination"];
32 readonly onOutgoingResponseFailure?: AcpProtocol.AcpPatchedProtocolOptions["onOutgoingResponseFailure"];
33 readonly onOutgoingResponse?: AcpProtocol.AcpPatchedProtocolOptions["onOutgoingResponse"];
34}
35
36type AcpClientRaw = {
37 readonly notifications: Stream.Stream<AcpProtocol.AcpIncomingNotification>;
38 readonly request: (method: string, payload: unknown) => Effect.Effect<unknown, AcpError.AcpError>;
39 readonly notify: (method: string, payload: unknown) => Effect.Effect<void, AcpError.AcpError>;
40};
41
42export class AcpClient extends Context.Service<
43 AcpClient,
44 {
45 readonly raw: AcpClientRaw;
46 readonly agent: {
47 /**
48 * Initializes the ACP session and negotiates capabilities.
49 * @see https://agentclientprotocol.com/protocol/schema#initialize
50 */
51 readonly initialize: (
52 payload: AcpSchema.InitializeRequest,
53 ) => Effect.Effect<AcpSchema.InitializeResponse, AcpError.AcpError>;
54 /**
55 * Performs ACP authentication when the agent requires it.
56 * @see https://agentclientprotocol.com/protocol/schema#authenticate
57 */
58 readonly authenticate: (
59 payload: AcpSchema.AuthenticateRequest,
60 ) => Effect.Effect<AcpSchema.AuthenticateResponse, AcpError.AcpError>;
61 /**
62 * Logs out the current ACP identity.
63 * @see https://agentclientprotocol.com/protocol/schema#logout
64 */
65 readonly logout: (
66 payload: AcpSchema.LogoutRequest,
67 ) => Effect.Effect<AcpSchema.LogoutResponse, AcpError.AcpError>;
68 /**
69 * Starts a new ACP session.
70 * @see https://agentclientprotocol.com/protocol/schema#session/new
71 */
72 readonly createSession: (
73 payload: AcpSchema.NewSessionRequest,
74 ) => Effect.Effect<AcpSchema.NewSessionResponse, AcpError.AcpError>;
75 /**
76 * Loads a previously saved ACP session.
77 * @see https://agentclientprotocol.com/protocol/schema#session/load
78 */
79 readonly loadSession: (
80 payload: AcpSchema.LoadSessionRequest,
81 ) => Effect.Effect<AcpSchema.LoadSessionResponse, AcpError.AcpError>;
82 /**
83 * Lists available ACP sessions.
84 * @see https://agentclientprotocol.com/protocol/schema#session/list
85 */
86 readonly listSessions: (
87 payload: AcpSchema.ListSessionsRequest,
88 ) => Effect.Effect<AcpSchema.ListSessionsResponse, AcpError.AcpError>;
89 /**
90 * Forks an ACP session.
91 * @see https://agentclientprotocol.com/protocol/schema#session/fork
92 */
93 readonly forkSession: (
94 payload: AcpSchema.ForkSessionRequest,
95 ) => Effect.Effect<AcpSchema.ForkSessionResponse, AcpError.AcpError>;
96 /**
97 * Resumes an ACP session.
98 * @see https://agentclientprotocol.com/protocol/schema#session/resume
99 */
100 readonly resumeSession: (
101 payload: AcpSchema.ResumeSessionRequest,
102 ) => Effect.Effect<AcpSchema.ResumeSessionResponse, AcpError.AcpError>;
103 /**
104 * Closes an ACP session.
105 * @see https://agentclientprotocol.com/protocol/schema#session/close
106 */
107 readonly closeSession: (
108 payload: AcpSchema.CloseSessionRequest,
109 ) => Effect.Effect<AcpSchema.CloseSessionResponse, AcpError.AcpError>;
110 /**
111 * Selects the active model for a session.
112 * @see https://agentclientprotocol.com/protocol/schema#session/set_model
113 */
114 readonly setSessionModel: (
115 payload: AcpSchema.SetSessionModelRequest,
116 ) => Effect.Effect<AcpSchema.SetSessionModelResponse, AcpError.AcpError>;
117 /**
118 * Updates a session configuration option.
119 * @see https://agentclientprotocol.com/protocol/schema#session/set_config_option
120 */
121 readonly setSessionConfigOption: (
122 payload: AcpSchema.SetSessionConfigOptionRequest,
123 ) => Effect.Effect<AcpSchema.SetSessionConfigOptionResponse, AcpError.AcpError>;
124 /**
125 * Sends a prompt turn to the agent.
126 * @see https://agentclientprotocol.com/protocol/schema#session/prompt
127 */
128 readonly prompt: (
129 payload: AcpSchema.PromptRequest,
130 ) => Effect.Effect<AcpSchema.PromptResponse, AcpError.AcpError>;
131 /**
132 * Sends a real ACP `session/cancel` notification.
133 * @see https://agentclientprotocol.com/protocol/schema#session/cancel
134 */
135 readonly cancel: (
136 payload: AcpSchema.CancelNotification,
137 ) => Effect.Effect<void, AcpError.AcpError>;
138 };
139 /**
140 * Registers a handler for `session/request_permission`.
141 * @see https://agentclientprotocol.com/protocol/schema#session/request_permission
142 */
143 readonly handleRequestPermission: (
144 handler: (
145 request: AcpSchema.RequestPermissionRequest,
146 ) => Effect.Effect<AcpSchema.RequestPermissionResponse, AcpError.AcpError>,
147 ) => Effect.Effect<void>;
148 /**
149 * Registers a handler for `session/elicitation`.
150 * @see https://agentclientprotocol.com/protocol/schema#session/elicitation
151 */
152 readonly handleElicitation: (
153 handler: (
154 request: AcpSchema.ElicitationRequest,
155 ) => Effect.Effect<AcpSchema.ElicitationResponse, AcpError.AcpError>,
156 ) => Effect.Effect<void>;
157 /**
158 * Registers a handler for `fs/read_text_file`.
159 * @see https://agentclientprotocol.com/protocol/schema#fs/read_text_file
160 */
161 readonly handleReadTextFile: (
162 handler: (
163 request: AcpSchema.ReadTextFileRequest,
164 ) => Effect.Effect<AcpSchema.ReadTextFileResponse, AcpError.AcpError>,
165 ) => Effect.Effect<void>;
166 /**
167 * Registers a handler for `fs/write_text_file`.
168 * @see https://agentclientprotocol.com/protocol/schema#fs/write_text_file
169 */
170 readonly handleWriteTextFile: (
171 handler: (
172 request: AcpSchema.WriteTextFileRequest,
173 ) => Effect.Effect<AcpSchema.WriteTextFileResponse | void, AcpError.AcpError>,
174 ) => Effect.Effect<void>;
175 /**
176 * Registers a handler for `terminal/create`.
177 * @see https://agentclientprotocol.com/protocol/schema#terminal/create
178 */
179 readonly handleCreateTerminal: (
180 handler: (
181 request: AcpSchema.CreateTerminalRequest,
182 ) => Effect.Effect<AcpSchema.CreateTerminalResponse, AcpError.AcpError>,
183 ) => Effect.Effect<void>;
184 /**
185 * Registers a handler for `terminal/output`.
186 * @see https://agentclientprotocol.com/protocol/schema#terminal/output
187 */
188 readonly handleTerminalOutput: (
189 handler: (
190 request: AcpSchema.TerminalOutputRequest,
191 ) => Effect.Effect<AcpSchema.TerminalOutputResponse, AcpError.AcpError>,
192 ) => Effect.Effect<void>;
193 /**
194 * Registers a handler for `terminal/wait_for_exit`.
195 * @see https://agentclientprotocol.com/protocol/schema#terminal/wait_for_exit
196 */
197 readonly handleTerminalWaitForExit: (
198 handler: (
199 request: AcpSchema.WaitForTerminalExitRequest,
200 ) => Effect.Effect<AcpSchema.WaitForTerminalExitResponse, AcpError.AcpError>,
201 ) => Effect.Effect<void>;
202 /**
203 * Registers a handler for `terminal/kill`.
204 * @see https://agentclientprotocol.com/protocol/schema#terminal/kill
205 */
206 readonly handleTerminalKill: (
207 handler: (
208 request: AcpSchema.KillTerminalRequest,
209 ) => Effect.Effect<AcpSchema.KillTerminalResponse | void, AcpError.AcpError>,
210 ) => Effect.Effect<void>;
211 /**
212 * Registers a handler for `terminal/release`.
213 * @see https://agentclientprotocol.com/protocol/schema#terminal/release
214 */
215 readonly handleTerminalRelease: (
216 handler: (
217 request: AcpSchema.ReleaseTerminalRequest,
218 ) => Effect.Effect<AcpSchema.ReleaseTerminalResponse | void, AcpError.AcpError>,
219 ) => Effect.Effect<void>;
220 /**
221 * Registers a handler for `session/update`.
222 * @see https://agentclientprotocol.com/protocol/schema#session/update
223 */
224 readonly handleSessionUpdate: (
225 handler: (
226 notification: AcpSchema.SessionNotification,
227 ) => Effect.Effect<void, AcpError.AcpError>,
228 ) => Effect.Effect<void>;
229 /**
230 * Registers a handler for `session/elicitation/complete`.
231 * @see https://agentclientprotocol.com/protocol/schema#session/elicitation/complete
232 */
233 readonly handleElicitationComplete: (
234 handler: (
235 notification: AcpSchema.ElicitationCompleteNotification,
236 ) => Effect.Effect<void, AcpError.AcpError>,
237 ) => Effect.Effect<void>;
238 /**
239 * Registers a fallback extension request handler.
240 * @see https://agentclientprotocol.com/protocol/extensibility
241 */
242 readonly handleUnknownExtRequest: (
243 handler: (method: string, params: unknown) => Effect.Effect<unknown, AcpError.AcpError>,
244 ) => Effect.Effect<void>;
245 /**
246 * Registers a fallback extension notification handler.
247 * @see https://agentclientprotocol.com/protocol/extensibility
248 */
249 readonly handleUnknownExtNotification: (
250 handler: (method: string, params: unknown) => Effect.Effect<void, AcpError.AcpError>,
251 ) => Effect.Effect<void>;
252 /**
253 * Registers a typed extension request handler.
254 * @see https://agentclientprotocol.com/protocol/extensibility
255 */
256 readonly handleExtRequest: <A, I>(
257 method: string,
258 payload: Schema.Codec<A, I>,
259 handler: (payload: A) => Effect.Effect<unknown, AcpError.AcpError>,
260 ) => Effect.Effect<void>;
261 /**
262 * Registers a typed extension notification handler.
263 * @see https://agentclientprotocol.com/protocol/extensibility
264 */
265 readonly handleExtNotification: <A, I>(
266 method: string,
267 payload: Schema.Codec<A, I>,
268 handler: (payload: A) => Effect.Effect<void, AcpError.AcpError>,
269 ) => Effect.Effect<void>;
270 }
271>()("effect-acp/client/AcpClient") {}
272
273interface AcpCoreRequestHandlers {
274 requestPermission?: (
275 request: AcpSchema.RequestPermissionRequest,
276 ) => Effect.Effect<AcpSchema.RequestPermissionResponse, AcpError.AcpError>;
277 elicitation?: (
278 request: AcpSchema.ElicitationRequest,
279 ) => Effect.Effect<AcpSchema.ElicitationResponse, AcpError.AcpError>;
280 readTextFile?: (
281 request: AcpSchema.ReadTextFileRequest,
282 ) => Effect.Effect<AcpSchema.ReadTextFileResponse, AcpError.AcpError>;
283 writeTextFile?: (
284 request: AcpSchema.WriteTextFileRequest,
285 ) => Effect.Effect<AcpSchema.WriteTextFileResponse | void, AcpError.AcpError>;
286 createTerminal?: (
287 request: AcpSchema.CreateTerminalRequest,
288 ) => Effect.Effect<AcpSchema.CreateTerminalResponse, AcpError.AcpError>;
289 terminalOutput?: (
290 request: AcpSchema.TerminalOutputRequest,
291 ) => Effect.Effect<AcpSchema.TerminalOutputResponse, AcpError.AcpError>;
292 terminalWaitForExit?: (
293 request: AcpSchema.WaitForTerminalExitRequest,
294 ) => Effect.Effect<AcpSchema.WaitForTerminalExitResponse, AcpError.AcpError>;
295 terminalKill?: (
296 request: AcpSchema.KillTerminalRequest,
297 ) => Effect.Effect<AcpSchema.KillTerminalResponse | void, AcpError.AcpError>;
298 terminalRelease?: (
299 request: AcpSchema.ReleaseTerminalRequest,
300 ) => Effect.Effect<AcpSchema.ReleaseTerminalResponse | void, AcpError.AcpError>;
301}
302
303interface AcpNotificationHandlers {
304 readonly sessionUpdate: BufferedNotificationHandler<AcpSchema.SessionNotification>;
305 readonly elicitationComplete: BufferedNotificationHandler<AcpSchema.ElicitationCompleteNotification>;
306}
307
308interface BufferedNotificationHandler<A> {
309 readonly handlers: Array<(notification: A) => Effect.Effect<void, AcpError.AcpError>>;
310 readonly pending: Array<A>;
311}
312
313export const make = Effect.fn("effect-acp/AcpClient.make")(function* (
314 stdio: Stdio.Stdio,
315 options: AcpClientOptions = {},
316 terminationError?: Effect.Effect<AcpError.AcpError>,
317): Effect.fn.Return<AcpClient["Service"], never, Scope.Scope> {
318 const coreHandlers: AcpCoreRequestHandlers = {};
319 const notificationHandlers: AcpNotificationHandlers = {
320 sessionUpdate: { handlers: [], pending: [] },
321 elicitationComplete: { handlers: [], pending: [] },
322 };
323 const extRequestHandlers = new Map<
324 string,
325 (params: unknown) => Effect.Effect<unknown, AcpError.AcpError>
326 >();
327 const extNotificationHandlers = new Map<
328 string,
329 (params: unknown) => Effect.Effect<void, AcpError.AcpError>
330 >();
331 let unknownExtRequestHandler:
332 | ((method: string, params: unknown) => Effect.Effect<unknown, AcpError.AcpError>)
333 | undefined;
334 let unknownExtNotificationHandler:
335 | ((method: string, params: unknown) => Effect.Effect<void, AcpError.AcpError>)
336 | undefined;
337
338 const runNotificationHandlers = <A>(
339 registration: BufferedNotificationHandler<A>,
340 notification: A,
341 ) =>
342 Effect.forEach(
343 registration.handlers,
344 (handler) => handler(notification).pipe(Effect.catch(() => Effect.void)),
345 { discard: true },
346 );
347
348 const flushBufferedNotifications = <A>(registration: BufferedNotificationHandler<A>) =>
349 Effect.suspend(() => {
350 if (registration.handlers.length === 0 || registration.pending.length === 0) {
351 return Effect.void;
352 }
353 const pending = registration.pending.splice(0, registration.pending.length);
354 return Effect.forEach(
355 pending,
356 (notification) => runNotificationHandlers(registration, notification),
357 {
358 discard: true,
359 },
360 );
361 });
362
363 const dispatchNotification = (notification: AcpProtocol.AcpIncomingNotification) => {
364 switch (notification._tag) {
365 case "SessionUpdate": {
366 if (notificationHandlers.sessionUpdate.handlers.length === 0) {
367 notificationHandlers.sessionUpdate.pending.push(notification.params);
368 return Effect.void;
369 }
370 return runNotificationHandlers(notificationHandlers.sessionUpdate, notification.params);
371 }
372 case "ElicitationComplete": {
373 if (notificationHandlers.elicitationComplete.handlers.length === 0) {
374 notificationHandlers.elicitationComplete.pending.push(notification.params);
375 return Effect.void;
376 }
377 return runNotificationHandlers(
378 notificationHandlers.elicitationComplete,
379 notification.params,
380 );
381 }
382 case "ExtNotification": {
383 const handler = extNotificationHandlers.get(notification.method);
384 if (handler) {
385 return handler(notification.params);
386 }
387 return unknownExtNotificationHandler
388 ? unknownExtNotificationHandler(notification.method, notification.params)
389 : Effect.void;
390 }
391 }
392 };
393
394 const dispatchExtRequest = (method: string, params: unknown) => {
395 const handler = extRequestHandlers.get(method);
396 if (handler) {
397 return handler(params);
398 }
399 return unknownExtRequestHandler
400 ? unknownExtRequestHandler(method, params)
401 : Effect.fail(AcpError.AcpRequestError.methodNotFound(method));
402 };
403
404 const transport = yield* AcpProtocol.makeAcpPatchedProtocol({
405 stdio: stdio,
406 ...(terminationError ? { terminationError } : {}),
407 serverRequestMethods: new Set(AcpRpcs.ClientRpcs.requests.keys()),
408 ...(options.logIncoming !== undefined ? { logIncoming: options.logIncoming } : {}),
409 ...(options.logOutgoing !== undefined ? { logOutgoing: options.logOutgoing } : {}),
410 ...(options.logger ? { logger: options.logger } : {}),
411 ...(options.onIncomingRequest ? { onIncomingRequest: options.onIncomingRequest } : {}),
412 ...(options.onTermination ? { onTermination: options.onTermination } : {}),
413 ...(options.onOutgoingResponseFailure
414 ? { onOutgoingResponseFailure: options.onOutgoingResponseFailure }
415 : {}),
416 ...(options.onOutgoingResponse ? { onOutgoingResponse: options.onOutgoingResponse } : {}),
417 onNotification: dispatchNotification,
418 onExtRequest: dispatchExtRequest,
419 });
420
421 const clientHandlerLayer = AcpRpcs.ClientRpcs.toLayer(
422 AcpRpcs.ClientRpcs.of({
423 [CLIENT_METHODS.session_request_permission]: (payload) =>
424 runHandler(
425 coreHandlers.requestPermission,
426 payload,
427 CLIENT_METHODS.session_request_permission,
428 ),
429 [CLIENT_METHODS.session_elicitation]: (payload) =>
430 runHandler(coreHandlers.elicitation, payload, CLIENT_METHODS.session_elicitation),
431 [CLIENT_METHODS.fs_read_text_file]: (payload) =>
432 runHandler(coreHandlers.readTextFile, payload, CLIENT_METHODS.fs_read_text_file),
433 [CLIENT_METHODS.fs_write_text_file]: (payload) =>
434 runHandler(coreHandlers.writeTextFile, payload, CLIENT_METHODS.fs_write_text_file).pipe(
435 Effect.map((result) => result ?? {}),
436 ),
437 [CLIENT_METHODS.terminal_create]: (payload) =>
438 runHandler(coreHandlers.createTerminal, payload, CLIENT_METHODS.terminal_create),
439 [CLIENT_METHODS.terminal_output]: (payload) =>
440 runHandler(coreHandlers.terminalOutput, payload, CLIENT_METHODS.terminal_output),
441 [CLIENT_METHODS.terminal_wait_for_exit]: (payload) =>
442 runHandler(
443 coreHandlers.terminalWaitForExit,
444 payload,
445 CLIENT_METHODS.terminal_wait_for_exit,
446 ),
447 [CLIENT_METHODS.terminal_kill]: (payload) =>
448 runHandler(coreHandlers.terminalKill, payload, CLIENT_METHODS.terminal_kill).pipe(
449 Effect.map((result) => result ?? {}),
450 ),
451 [CLIENT_METHODS.terminal_release]: (payload) =>
452 runHandler(coreHandlers.terminalRelease, payload, CLIENT_METHODS.terminal_release).pipe(
453 Effect.map((result) => result ?? {}),
454 ),
455 }),
456 );
457
458 yield* RpcServer.make(AcpRpcs.ClientRpcs).pipe(
459 Effect.provideService(RpcServer.Protocol, transport.serverProtocol),
460 Effect.provide(clientHandlerLayer),
461 Effect.forkScoped,
462 );
463
464 let nextRpcRequestId = 2 ** 32;
465 const rpc = yield* RpcClient.make(AcpRpcs.AgentRpcs, {
466 generateRequestId: () => RpcMessage.RequestId(nextRpcRequestId++),
467 }).pipe(Effect.provideService(RpcClient.Protocol, transport.clientProtocol));
468
469 return AcpClient.of({
470 raw: {
471 notifications: transport.incoming,
472 request: transport.request,
473 notify: transport.notify,
474 },
475 agent: {
476 initialize: (payload) =>
477 callRpc(AGENT_METHODS.initialize, rpc[AGENT_METHODS.initialize](payload)),
478 authenticate: (payload) =>
479 callRpc(AGENT_METHODS.authenticate, rpc[AGENT_METHODS.authenticate](payload)),
480 logout: (payload) => callRpc(AGENT_METHODS.logout, rpc[AGENT_METHODS.logout](payload)),
481 createSession: (payload) =>
482 callRpc(AGENT_METHODS.session_new, rpc[AGENT_METHODS.session_new](payload)),
483 loadSession: (payload) =>
484 callRpc(AGENT_METHODS.session_load, rpc[AGENT_METHODS.session_load](payload)),
485 listSessions: (payload) =>
486 callRpc(AGENT_METHODS.session_list, rpc[AGENT_METHODS.session_list](payload)),
487 forkSession: (payload) =>
488 callRpc(AGENT_METHODS.session_fork, rpc[AGENT_METHODS.session_fork](payload)),
489 resumeSession: (payload) =>
490 callRpc(AGENT_METHODS.session_resume, rpc[AGENT_METHODS.session_resume](payload)),
491 closeSession: (payload) =>
492 callRpc(AGENT_METHODS.session_close, rpc[AGENT_METHODS.session_close](payload)),
493 setSessionModel: (payload) =>
494 callRpc(AGENT_METHODS.session_set_model, rpc[AGENT_METHODS.session_set_model](payload)),
495 setSessionConfigOption: (payload) =>
496 callRpc(
497 AGENT_METHODS.session_set_config_option,
498 rpc[AGENT_METHODS.session_set_config_option](payload),
499 ),
500 prompt: (payload) =>
501 callRpc(AGENT_METHODS.session_prompt, rpc[AGENT_METHODS.session_prompt](payload)),
502 cancel: (payload) => transport.notify(AGENT_METHODS.session_cancel, payload),
503 },
504 handleRequestPermission: (handler) =>
505 Effect.suspend(() => {
506 coreHandlers.requestPermission = handler;
507 return Effect.void;
508 }),
509 handleElicitation: (handler) =>
510 Effect.suspend(() => {
511 coreHandlers.elicitation = handler;
512 return Effect.void;
513 }),
514 handleReadTextFile: (handler) =>
515 Effect.suspend(() => {
516 coreHandlers.readTextFile = handler;
517 return Effect.void;
518 }),
519 handleWriteTextFile: (handler) =>
520 Effect.suspend(() => {
521 coreHandlers.writeTextFile = handler;
522 return Effect.void;
523 }),
524 handleCreateTerminal: (handler) =>
525 Effect.suspend(() => {
526 coreHandlers.createTerminal = handler;
527 return Effect.void;
528 }),
529 handleTerminalOutput: (handler) =>
530 Effect.suspend(() => {
531 coreHandlers.terminalOutput = handler;
532 return Effect.void;
533 }),
534 handleTerminalWaitForExit: (handler) =>
535 Effect.suspend(() => {
536 coreHandlers.terminalWaitForExit = handler;
537 return Effect.void;
538 }),
539 handleTerminalKill: (handler) =>
540 Effect.suspend(() => {
541 coreHandlers.terminalKill = handler;
542 return Effect.void;
543 }),
544 handleTerminalRelease: (handler) =>
545 Effect.suspend(() => {
546 coreHandlers.terminalRelease = handler;
547 return Effect.void;
548 }),
549 handleSessionUpdate: (handler) =>
550 Effect.suspend(() => {
551 notificationHandlers.sessionUpdate.handlers.push(handler);
552 return flushBufferedNotifications(notificationHandlers.sessionUpdate);
553 }),
554 handleElicitationComplete: (handler) =>
555 Effect.suspend(() => {
556 notificationHandlers.elicitationComplete.handlers.push(handler);
557 return flushBufferedNotifications(notificationHandlers.elicitationComplete);
558 }),
559 handleUnknownExtRequest: (handler) =>
560 Effect.suspend(() => {
561 unknownExtRequestHandler = handler;
562 return Effect.void;
563 }),
564 handleUnknownExtNotification: (handler) =>
565 Effect.suspend(() => {
566 unknownExtNotificationHandler = handler;
567 return Effect.void;
568 }),
569 handleExtRequest: (method, payload, handler) =>
570 Effect.suspend(() => {
571 extRequestHandlers.set(method, decodeExtRequestRegistration(method, payload, handler));
572 return Effect.void;
573 }),
574 handleExtNotification: (method, payload, handler) =>
575 Effect.suspend(() => {
576 extNotificationHandlers.set(
577 method,
578 decodeExtNotificationRegistration(method, payload, handler),
579 );
580 return Effect.void;
581 }),
582 });
583});
584
585export const layer = (stdio: Stdio.Stdio, options: AcpClientOptions = {}): Layer.Layer<AcpClient> =>
586 Layer.effect(AcpClient, make(stdio, options));
587
588export const layerChildProcess = (
589 handle: ChildProcessSpawner.ChildProcessHandle,
590 options: AcpClientOptions = {},
591): Layer.Layer<AcpClient> => {
592 const stdio = makeChildStdio(handle);
593 const terminationError = makeTerminationError(handle);
594 return Layer.effect(AcpClient, make(stdio, options, terminationError));
595};