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