Open History to see the last commit
1import * as Deferred from "effect/Deferred";
2import * as Effect from "effect/Effect";
3import * as Exit from "effect/Exit";
4import * as Fiber from "effect/Fiber";
5import * as Layer from "effect/Layer";
6import * as Queue from "effect/Queue";
7import * as Ref from "effect/Ref";
8import * as Schema from "effect/Schema";
9import * as Scope from "effect/Scope";
10
11import { assert, it } from "@effect/vitest";
12
13import * as AcpAgent from "./agent.ts";
14import * as AcpSchema from "./_generated/schema.gen.ts";
15import type * as AcpProtocol from "./protocol.ts";
16import {
17 encodeJsonl,
18 jsonRpcNotification,
19 jsonRpcRequest,
20 jsonRpcResponse,
21} from "./_internal/shared.ts";
22import { makeInMemoryStdio } from "./_internal/stdio.ts";
23
24const RequestPermissionRequest = jsonRpcRequest(
25 "session/request_permission",
26 AcpSchema.RequestPermissionRequest,
27);
28const InitializeRequest = jsonRpcRequest("initialize", AcpSchema.InitializeRequest);
29const InitializeResponse = jsonRpcResponse(AcpSchema.InitializeResponse);
30const RequestPermissionResponse = jsonRpcResponse(AcpSchema.RequestPermissionResponse);
31const SessionCancelNotification = jsonRpcNotification(
32 "session/cancel",
33 AcpSchema.CancelSessionNotification,
34);
35const ExtPingNotification = jsonRpcNotification("x/ping", Schema.Struct({ count: Schema.Number }));
36const ExtRequest = jsonRpcRequest("x/test", Schema.Struct({ hello: Schema.String }));
37const ExtResponse = jsonRpcResponse(Schema.Struct({ ok: Schema.Boolean }));
38const decodeRequestPermissionRequest = Schema.decodeEffect(
39 Schema.fromJsonString(RequestPermissionRequest),
40);
41const decodeInitializeResponse = Schema.decodeEffect(Schema.fromJsonString(InitializeResponse));
42const decodeExtResponse = Schema.decodeEffect(Schema.fromJsonString(ExtResponse));
43
44it.effect("effect-acp agent handles core agent requests and outbound client requests", () =>
45 Effect.gen(function* () {
46 const { stdio, input, output } = yield* makeInMemoryStdio();
47 const cancelNotifications = yield* Ref.make<Array<string>>([]);
48 const extNotifications = yield* Ref.make<Array<number>>([]);
49 const requestContexts = yield* Ref.make<Array<AcpProtocol.AcpRequestContext>>([]);
50 const cancelReceived = yield* Deferred.make<void>();
51 const extReceived = yield* Deferred.make<void>();
52 const scope = yield* Scope.make();
53 const context = yield* Layer.buildWithScope(AcpAgent.layer(stdio), scope);
54
55 yield* Effect.gen(function* () {
56 const agent = yield* AcpAgent.AcpAgent;
57
58 yield* agent.handleInitialize((_request, requestContext) =>
59 Ref.update(requestContexts, (current) => [...current, requestContext]).pipe(
60 Effect.as({
61 protocolVersion: 2,
62 capabilities: {},
63 info: {
64 name: "mock-agent",
65 version: "0.0.0",
66 },
67 }),
68 ),
69 );
70 yield* agent.handleExtRequest(
71 "x/test",
72 Schema.Struct({ hello: Schema.String }),
73 (_payload, requestContext) =>
74 Ref.update(requestContexts, (current) => [...current, requestContext]).pipe(
75 Effect.as({ ok: true }),
76 ),
77 );
78 yield* agent.handleCancel((notification) =>
79 Ref.update(cancelNotifications, (current) => [...current, notification.sessionId]).pipe(
80 Effect.andThen(Deferred.succeed(cancelReceived, undefined)),
81 ),
82 );
83 yield* agent.handleExtNotification(
84 "x/ping",
85 Schema.Struct({ count: Schema.Number }),
86 (payload) =>
87 Ref.update(extNotifications, (current) => [...current, payload.count]).pipe(
88 Effect.andThen(Deferred.succeed(extReceived, undefined)),
89 ),
90 );
91
92 const permissionFiber = yield* agent.client
93 .requestPermission({
94 sessionId: "session-1",
95 title: "Allow mock action",
96 subject: {
97 type: "tool_call",
98 toolCall: {
99 toolCallId: "tool-1",
100 title: "Allow mock action",
101 },
102 },
103 options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
104 })
105 .pipe(Effect.forkScoped);
106
107 const permissionRequest = yield* decodeRequestPermissionRequest(yield* Queue.take(output));
108 assert.equal(permissionRequest.jsonrpc, "2.0");
109 assert.equal(permissionRequest.method, "session/request_permission");
110 assert.deepEqual(permissionRequest.params, {
111 sessionId: "session-1",
112 title: "Allow mock action",
113 subject: {
114 type: "tool_call",
115 toolCall: {
116 toolCallId: "tool-1",
117 title: "Allow mock action",
118 },
119 },
120 options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
121 });
122 assert.deepEqual(permissionRequest.headers, []);
123
124 yield* Queue.offer(
125 input,
126 yield* encodeJsonl(RequestPermissionResponse, {
127 jsonrpc: "2.0",
128 id: permissionRequest.id,
129 result: {
130 outcome: {
131 outcome: "selected",
132 optionId: "allow",
133 },
134 },
135 }),
136 );
137
138 const permission = yield* Fiber.join(permissionFiber);
139 assert.equal(permission.outcome.outcome, "selected");
140
141 yield* Queue.offer(
142 input,
143 yield* encodeJsonl(InitializeRequest, {
144 jsonrpc: "2.0",
145 id: 2,
146 method: "initialize",
147 params: {
148 protocolVersion: 2,
149 capabilities: {},
150 info: {
151 name: "effect-acp-test",
152 version: "0.0.0",
153 },
154 },
155 headers: [],
156 }),
157 );
158
159 const initResponse = yield* decodeInitializeResponse(yield* Queue.take(output));
160 assert.deepEqual(initResponse, {
161 jsonrpc: "2.0",
162 id: 2,
163 result: {
164 protocolVersion: 2,
165 capabilities: {},
166 info: {
167 name: "mock-agent",
168 version: "0.0.0",
169 },
170 },
171 });
172
173 yield* Queue.offer(
174 input,
175 yield* encodeJsonl(ExtRequest, {
176 jsonrpc: "2.0",
177 id: "extension-3",
178 method: "x/test",
179 params: { hello: "world" },
180 headers: [],
181 }),
182 );
183 const extResponse = yield* decodeExtResponse(yield* Queue.take(output));
184 assert.deepEqual(extResponse, {
185 jsonrpc: "2.0",
186 id: "extension-3",
187 result: { ok: true },
188 });
189 assert.deepEqual(yield* Ref.get(requestContexts), [
190 { requestId: "$t3:jsonrpc:number:2", method: "initialize" },
191 { requestId: "extension-3", method: "x/test" },
192 ]);
193
194 yield* Queue.offer(
195 input,
196 yield* encodeJsonl(SessionCancelNotification, {
197 jsonrpc: "2.0",
198 method: "session/cancel",
199 params: {
200 sessionId: "session-1",
201 },
202 }),
203 );
204 yield* Queue.offer(
205 input,
206 yield* encodeJsonl(ExtPingNotification, {
207 jsonrpc: "2.0",
208 method: "x/ping",
209 params: { count: 2 },
210 }),
211 );
212
213 yield* Deferred.await(cancelReceived);
214 yield* Deferred.await(extReceived);
215 assert.deepEqual(yield* Ref.get(cancelNotifications), ["session-1"]);
216 assert.deepEqual(yield* Ref.get(extNotifications), [2]);
217 }).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
218 }),
219);
220
221it.effect("effect-acp agent uses distinct ids for RPC calls and extension requests", () =>
222 Effect.gen(function* () {
223 const { stdio, input, output } = yield* makeInMemoryStdio();
224 const scope = yield* Scope.make();
225 const context = yield* Layer.buildWithScope(AcpAgent.layer(stdio), scope);
226
227 yield* Effect.gen(function* () {
228 const agent = yield* AcpAgent.AcpAgent;
229
230 const permissionFiber = yield* agent.client
231 .requestPermission({
232 sessionId: "session-1",
233 title: "Allow mock action",
234 subject: {
235 type: "tool_call",
236 toolCall: {
237 toolCallId: "tool-1",
238 title: "Allow mock action",
239 },
240 },
241 options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
242 })
243 .pipe(Effect.forkScoped);
244 const extFiber = yield* agent.client
245 .extRequest("x/test", { hello: "world" })
246 .pipe(Effect.forkScoped);
247
248 const firstOutbound = yield* Queue.take(output);
249 const secondOutbound = yield* Queue.take(output);
250
251 const decodedPermission = Schema.decodeEffect(
252 Schema.fromJsonString(RequestPermissionRequest),
253 );
254 const decodedExt = Schema.decodeEffect(Schema.fromJsonString(ExtRequest));
255 const firstIsPermission = yield* decodedPermission(firstOutbound).pipe(
256 Effect.match({
257 onFailure: () => false,
258 onSuccess: () => true,
259 }),
260 );
261
262 const permissionRequest = firstIsPermission
263 ? yield* decodedPermission(firstOutbound)
264 : yield* decodedPermission(secondOutbound);
265 const extRequest = firstIsPermission
266 ? yield* decodedExt(secondOutbound)
267 : yield* decodedExt(firstOutbound);
268
269 assert.notEqual(permissionRequest.id, extRequest.id);
270
271 yield* Queue.offer(
272 input,
273 yield* encodeJsonl(RequestPermissionResponse, {
274 jsonrpc: "2.0",
275 id: permissionRequest.id,
276 result: {
277 outcome: {
278 outcome: "selected",
279 optionId: "allow",
280 },
281 },
282 }),
283 );
284 yield* Queue.offer(
285 input,
286 yield* encodeJsonl(ExtResponse, {
287 jsonrpc: "2.0",
288 id: extRequest.id,
289 result: { ok: true },
290 }),
291 );
292
293 const permission = yield* Fiber.join(permissionFiber);
294 assert.equal(permission.outcome.outcome, "selected");
295 assert.deepEqual(yield* Fiber.join(extFiber), { ok: true });
296 }).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
297 }),
298);