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