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