Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

client.ts

614 lines24.1 KB

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​
25const decodeElicitationRequest = Schema.decodeUnknownEffect(AcpSchema.ElicitationRequest);
26​
27export interface AcpClientOptions {
28 readonly logIncoming?: boolean;
29 readonly logOutgoing?: boolean;
30 readonly logger?: (event: AcpProtocol.AcpProtocolLogEvent) => Effect.Effect<void, never>;
31 /** Transforms child output before protocol logging and parsing. */
32 readonly transformStdout?: (
33 stdout: ChildProcessSpawner.ChildProcessHandle["stdout"],
34 ) => AcpProtocol.AcpStdio["stdin"];
35 /** Transforms decoded session updates before buffering or delivery. */
36 readonly transformSessionUpdate?: (
37 notification: AcpSchema.SessionNotification,
38 ) => AcpSchema.SessionNotification;
39 /** Reports input failures and process exits, even between requests. */
40 readonly onTermination?: (error: AcpError.AcpError) => Effect.Effect<void, never, never>;
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` and `elicitation/create`.
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` and `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>;
309​
310interface AcpNotificationHandlers {
311 readonly sessionUpdate: BufferedNotificationHandler<AcpSchema.SessionNotification>;
312 readonly elicitationComplete: BufferedNotificationHandler<AcpSchema.ElicitationCompleteNotification>;
314​
315interface BufferedNotificationHandler<A> {
316 readonly handlers: Array<(notification: A) => Effect.Effect<void, AcpError.AcpError>>;
317 readonly pending: Array<A>;
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(registration.handlers, (handler) => handler(notification).pipe(Effect.ignore), {
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 "elicitation/create": (payload) =>
435 decodeElicitationRequest(payload).pipe(
436 Effect.mapError((cause) =>
437 AcpError.AcpRequestError.invalidExtensionPayload(
438 "elicitation/create",
439 cause,
440 ).toProtocolError(),
441 ),
442 Effect.flatMap((request) =>
443 runHandler(coreHandlers.elicitation, request, "elicitation/create"),
444 ),
445 Effect.map(({ action, _meta }) => ({
446 ...action,
447 ...(_meta !== undefined ? { _meta } : {}),
448 })),
449 ),
450 [CLIENT_METHODS.fs_read_text_file]: (payload) =>
451 runHandler(coreHandlers.readTextFile, payload, CLIENT_METHODS.fs_read_text_file),
452 [CLIENT_METHODS.fs_write_text_file]: (payload) =>
453 runHandler(coreHandlers.writeTextFile, payload, CLIENT_METHODS.fs_write_text_file).pipe(
454 Effect.map((result) => result ?? {}),
455 ),
456 [CLIENT_METHODS.terminal_create]: (payload) =>
457 runHandler(coreHandlers.createTerminal, payload, CLIENT_METHODS.terminal_create),
458 [CLIENT_METHODS.terminal_output]: (payload) =>
459 runHandler(coreHandlers.terminalOutput, payload, CLIENT_METHODS.terminal_output),
460 [CLIENT_METHODS.terminal_wait_for_exit]: (payload) =>
461 runHandler(
462 coreHandlers.terminalWaitForExit,
463 payload,
464 CLIENT_METHODS.terminal_wait_for_exit,
465 ),
466 [CLIENT_METHODS.terminal_kill]: (payload) =>
467 runHandler(coreHandlers.terminalKill, payload, CLIENT_METHODS.terminal_kill).pipe(
468 Effect.map((result) => result ?? {}),
469 ),
470 [CLIENT_METHODS.terminal_release]: (payload) =>
471 runHandler(coreHandlers.terminalRelease, payload, CLIENT_METHODS.terminal_release).pipe(
472 Effect.map((result) => result ?? {}),
473 ),
474 }),
475 );
476​
477 yield* RpcServer.make(AcpRpcs.ClientRpcs).pipe(
478 Effect.provideService(RpcServer.Protocol, transport.serverProtocol),
479 Effect.provide(clientHandlerLayer),
480 Effect.forkScoped,
481 );
482​
483 let nextRpcRequestId = 2 ** 32;
484 const rpc = yield* RpcClient.make(AcpRpcs.AgentRpcs, {
485 generateRequestId: () => RpcMessage.RequestId(nextRpcRequestId++),
486 }).pipe(Effect.provideService(RpcClient.Protocol, transport.clientProtocol));
487​
488 return AcpClient.of({
489 raw: {
490 notifications: transport.incoming,
491 request: transport.request,
492 notify: transport.notify,
493 },
494 agent: {
495 initialize: (payload) =>
496 callRpc(AGENT_METHODS.initialize, rpc[AGENT_METHODS.initialize](payload)),
497 authenticate: (payload) =>
498 callRpc(AGENT_METHODS.authenticate, rpc[AGENT_METHODS.authenticate](payload)),
499 logout: (payload) => callRpc(AGENT_METHODS.logout, rpc[AGENT_METHODS.logout](payload)),
500 createSession: (payload) =>
501 callRpc(AGENT_METHODS.session_new, rpc[AGENT_METHODS.session_new](payload)),
502 loadSession: (payload) =>
503 callRpc(AGENT_METHODS.session_load, rpc[AGENT_METHODS.session_load](payload)),
504 listSessions: (payload) =>
505 callRpc(AGENT_METHODS.session_list, rpc[AGENT_METHODS.session_list](payload)),
506 forkSession: (payload) =>
507 callRpc(AGENT_METHODS.session_fork, rpc[AGENT_METHODS.session_fork](payload)),
508 resumeSession: (payload) =>
509 callRpc(AGENT_METHODS.session_resume, rpc[AGENT_METHODS.session_resume](payload)),
510 closeSession: (payload) =>
511 callRpc(AGENT_METHODS.session_close, rpc[AGENT_METHODS.session_close](payload)),
512 setSessionModel: (payload) =>
513 callRpc(AGENT_METHODS.session_set_model, rpc[AGENT_METHODS.session_set_model](payload)),
514 setSessionConfigOption: (payload) =>
515 callRpc(
516 AGENT_METHODS.session_set_config_option,
517 rpc[AGENT_METHODS.session_set_config_option](payload),
518 ),
519 prompt: (payload) =>
520 callRpc(AGENT_METHODS.session_prompt, rpc[AGENT_METHODS.session_prompt](payload)),
521 cancel: (payload) => transport.notify(AGENT_METHODS.session_cancel, payload),
522 },
523 handleRequestPermission: (handler) =>
524 Effect.suspend(() => {
525 coreHandlers.requestPermission = handler;
526 return Effect.void;
527 }),
528 handleElicitation: (handler) =>
529 Effect.suspend(() => {
530 coreHandlers.elicitation = handler;
531 return Effect.void;
532 }),
533 handleReadTextFile: (handler) =>
534 Effect.suspend(() => {
535 coreHandlers.readTextFile = handler;
536 return Effect.void;
537 }),
538 handleWriteTextFile: (handler) =>
539 Effect.suspend(() => {
540 coreHandlers.writeTextFile = handler;
541 return Effect.void;
542 }),
543 handleCreateTerminal: (handler) =>
544 Effect.suspend(() => {
545 coreHandlers.createTerminal = handler;
546 return Effect.void;
547 }),
548 handleTerminalOutput: (handler) =>
549 Effect.suspend(() => {
550 coreHandlers.terminalOutput = handler;
551 return Effect.void;
552 }),
553 handleTerminalWaitForExit: (handler) =>
554 Effect.suspend(() => {
555 coreHandlers.terminalWaitForExit = handler;
556 return Effect.void;
557 }),
558 handleTerminalKill: (handler) =>
559 Effect.suspend(() => {
560 coreHandlers.terminalKill = handler;
561 return Effect.void;
562 }),
563 handleTerminalRelease: (handler) =>
564 Effect.suspend(() => {
565 coreHandlers.terminalRelease = handler;
566 return Effect.void;
567 }),
568 handleSessionUpdate: (handler) =>
569 Effect.suspend(() => {
570 notificationHandlers.sessionUpdate.handlers.push(handler);
571 return flushBufferedNotifications(notificationHandlers.sessionUpdate);
572 }),
573 handleElicitationComplete: (handler) =>
574 Effect.suspend(() => {
575 notificationHandlers.elicitationComplete.handlers.push(handler);
576 return flushBufferedNotifications(notificationHandlers.elicitationComplete);
577 }),
578 handleUnknownExtRequest: (handler) =>
579 Effect.suspend(() => {
580 unknownExtRequestHandler = handler;
581 return Effect.void;
582 }),
583 handleUnknownExtNotification: (handler) =>
584 Effect.suspend(() => {
585 unknownExtNotificationHandler = handler;
586 return Effect.void;
587 }),
588 handleExtRequest: (method, payload, handler) =>
589 Effect.suspend(() => {
590 extRequestHandlers.set(method, decodeExtRequestRegistration(method, payload, handler));
591 return Effect.void;
592 }),
593 handleExtNotification: (method, payload, handler) =>
594 Effect.suspend(() => {
595 extNotificationHandlers.set(
596 method,
597 decodeExtNotificationRegistration(method, payload, handler),
598 );
599 return Effect.void;
600 }),
601 });
602});
603​
604export const layerChildProcess = (
605 handle: ChildProcessSpawner.ChildProcessHandle,
606 options: AcpClientOptions = {},
607): Layer.Layer<AcpClient> => {
608 const stdio = {
609 ...makeChildStdio(handle),
610 stdin: options.transformStdout?.(handle.stdout) ?? handle.stdout,
611 };
612 const terminationError = makeTerminationError(handle);
613 return Layer.effect(AcpClient, make(stdio, options, terminationError));
614};