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

client.ts

616 lines24.2 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(
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.transformSessionUpdate
419 ? { transformSessionUpdate: options.transformSessionUpdate }
420 : {}),
421 ...(options.onTermination ? { onTermination: options.onTermination } : {}),
422 onNotification: dispatchNotification,
423 onExtRequest: dispatchExtRequest,
424 });
425​
426 const clientHandlerLayer = AcpRpcs.ClientRpcs.toLayer(
427 AcpRpcs.ClientRpcs.of({
428 [CLIENT_METHODS.session_request_permission]: (payload) =>
429 runHandler(
430 coreHandlers.requestPermission,
431 payload,
432 CLIENT_METHODS.session_request_permission,
433 ),
434 [CLIENT_METHODS.session_elicitation]: (payload) =>
435 runHandler(coreHandlers.elicitation, payload, CLIENT_METHODS.session_elicitation),
436 "elicitation/create": (payload) =>
437 decodeElicitationRequest(payload).pipe(
438 Effect.mapError((cause) =>
439 AcpError.AcpRequestError.invalidExtensionPayload(
440 "elicitation/create",
441 cause,
442 ).toProtocolError(),
443 ),
444 Effect.flatMap((request) =>
445 runHandler(coreHandlers.elicitation, request, "elicitation/create"),
446 ),
447 Effect.map(({ action, _meta }) => ({
448 ...action,
449 ...(_meta !== undefined ? { _meta } : {}),
450 })),
451 ),
452 [CLIENT_METHODS.fs_read_text_file]: (payload) =>
453 runHandler(coreHandlers.readTextFile, payload, CLIENT_METHODS.fs_read_text_file),
454 [CLIENT_METHODS.fs_write_text_file]: (payload) =>
455 runHandler(coreHandlers.writeTextFile, payload, CLIENT_METHODS.fs_write_text_file).pipe(
456 Effect.map((result) => result ?? {}),
457 ),
458 [CLIENT_METHODS.terminal_create]: (payload) =>
459 runHandler(coreHandlers.createTerminal, payload, CLIENT_METHODS.terminal_create),
460 [CLIENT_METHODS.terminal_output]: (payload) =>
461 runHandler(coreHandlers.terminalOutput, payload, CLIENT_METHODS.terminal_output),
462 [CLIENT_METHODS.terminal_wait_for_exit]: (payload) =>
463 runHandler(
464 coreHandlers.terminalWaitForExit,
465 payload,
466 CLIENT_METHODS.terminal_wait_for_exit,
467 ),
468 [CLIENT_METHODS.terminal_kill]: (payload) =>
469 runHandler(coreHandlers.terminalKill, payload, CLIENT_METHODS.terminal_kill).pipe(
470 Effect.map((result) => result ?? {}),
471 ),
472 [CLIENT_METHODS.terminal_release]: (payload) =>
473 runHandler(coreHandlers.terminalRelease, payload, CLIENT_METHODS.terminal_release).pipe(
474 Effect.map((result) => result ?? {}),
475 ),
476 }),
477 );
478​
479 yield* RpcServer.make(AcpRpcs.ClientRpcs).pipe(
480 Effect.provideService(RpcServer.Protocol, transport.serverProtocol),
481 Effect.provide(clientHandlerLayer),
482 Effect.forkScoped,
483 );
484​
485 let nextRpcRequestId = 2 ** 32;
486 const rpc = yield* RpcClient.make(AcpRpcs.AgentRpcs, {
487 generateRequestId: () => RpcMessage.RequestId(nextRpcRequestId++),
488 }).pipe(Effect.provideService(RpcClient.Protocol, transport.clientProtocol));
489​
490 return AcpClient.of({
491 raw: {
492 notifications: transport.incoming,
493 request: transport.request,
494 notify: transport.notify,
495 },
496 agent: {
497 initialize: (payload) =>
498 callRpc(AGENT_METHODS.initialize, rpc[AGENT_METHODS.initialize](payload)),
499 authenticate: (payload) =>
500 callRpc(AGENT_METHODS.authenticate, rpc[AGENT_METHODS.authenticate](payload)),
501 logout: (payload) => callRpc(AGENT_METHODS.logout, rpc[AGENT_METHODS.logout](payload)),
502 createSession: (payload) =>
503 callRpc(AGENT_METHODS.session_new, rpc[AGENT_METHODS.session_new](payload)),
504 loadSession: (payload) =>
505 callRpc(AGENT_METHODS.session_load, rpc[AGENT_METHODS.session_load](payload)),
506 listSessions: (payload) =>
507 callRpc(AGENT_METHODS.session_list, rpc[AGENT_METHODS.session_list](payload)),
508 forkSession: (payload) =>
509 callRpc(AGENT_METHODS.session_fork, rpc[AGENT_METHODS.session_fork](payload)),
510 resumeSession: (payload) =>
511 callRpc(AGENT_METHODS.session_resume, rpc[AGENT_METHODS.session_resume](payload)),
512 closeSession: (payload) =>
513 callRpc(AGENT_METHODS.session_close, rpc[AGENT_METHODS.session_close](payload)),
514 setSessionModel: (payload) =>
515 callRpc(AGENT_METHODS.session_set_model, rpc[AGENT_METHODS.session_set_model](payload)),
516 setSessionConfigOption: (payload) =>
517 callRpc(
518 AGENT_METHODS.session_set_config_option,
519 rpc[AGENT_METHODS.session_set_config_option](payload),
520 ),
521 prompt: (payload) =>
522 callRpc(AGENT_METHODS.session_prompt, rpc[AGENT_METHODS.session_prompt](payload)),
523 cancel: (payload) => transport.notify(AGENT_METHODS.session_cancel, payload),
524 },
525 handleRequestPermission: (handler) =>
526 Effect.suspend(() => {
527 coreHandlers.requestPermission = handler;
528 return Effect.void;
529 }),
530 handleElicitation: (handler) =>
531 Effect.suspend(() => {
532 coreHandlers.elicitation = handler;
533 return Effect.void;
534 }),
535 handleReadTextFile: (handler) =>
536 Effect.suspend(() => {
537 coreHandlers.readTextFile = handler;
538 return Effect.void;
539 }),
540 handleWriteTextFile: (handler) =>
541 Effect.suspend(() => {
542 coreHandlers.writeTextFile = handler;
543 return Effect.void;
544 }),
545 handleCreateTerminal: (handler) =>
546 Effect.suspend(() => {
547 coreHandlers.createTerminal = handler;
548 return Effect.void;
549 }),
550 handleTerminalOutput: (handler) =>
551 Effect.suspend(() => {
552 coreHandlers.terminalOutput = handler;
553 return Effect.void;
554 }),
555 handleTerminalWaitForExit: (handler) =>
556 Effect.suspend(() => {
557 coreHandlers.terminalWaitForExit = handler;
558 return Effect.void;
559 }),
560 handleTerminalKill: (handler) =>
561 Effect.suspend(() => {
562 coreHandlers.terminalKill = handler;
563 return Effect.void;
564 }),
565 handleTerminalRelease: (handler) =>
566 Effect.suspend(() => {
567 coreHandlers.terminalRelease = handler;
568 return Effect.void;
569 }),
570 handleSessionUpdate: (handler) =>
571 Effect.suspend(() => {
572 notificationHandlers.sessionUpdate.handlers.push(handler);
573 return flushBufferedNotifications(notificationHandlers.sessionUpdate);
574 }),
575 handleElicitationComplete: (handler) =>
576 Effect.suspend(() => {
577 notificationHandlers.elicitationComplete.handlers.push(handler);
578 return flushBufferedNotifications(notificationHandlers.elicitationComplete);
579 }),
580 handleUnknownExtRequest: (handler) =>
581 Effect.suspend(() => {
582 unknownExtRequestHandler = handler;
583 return Effect.void;
584 }),
585 handleUnknownExtNotification: (handler) =>
586 Effect.suspend(() => {
587 unknownExtNotificationHandler = handler;
588 return Effect.void;
589 }),
590 handleExtRequest: (method, payload, handler) =>
591 Effect.suspend(() => {
592 extRequestHandlers.set(method, decodeExtRequestRegistration(method, payload, handler));
593 return Effect.void;
594 }),
595 handleExtNotification: (method, payload, handler) =>
596 Effect.suspend(() => {
597 extNotificationHandlers.set(
598 method,
599 decodeExtNotificationRegistration(method, payload, handler),
600 );
601 return Effect.void;
602 }),
603 });
604});
605​
606export const layerChildProcess = (
607 handle: ChildProcessSpawner.ChildProcessHandle,
608 options: AcpClientOptions = {},
609): Layer.Layer<AcpClient> => {
610 const stdio = {
611 ...makeChildStdio(handle),
612 stdin: options.transformStdout?.(handle.stdout) ?? handle.stdout,
613 };
614 const terminationError = makeTerminationError(handle);
615 return Layer.effect(AcpClient, make(stdio, options, terminationError));
616};