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 }));
37
38it.effect("effect-acp agent handles core agent requests and outbound client requests", () =>
39 Effect.gen(function* () {
40 const { stdio, input, output } = yield* makeInMemoryStdio();
41 const cancelNotifications = yield* Ref.make<Array<string>>([]);
42 const extNotifications = yield* Ref.make<Array<number>>([]);
43 const cancelReceived = yield* Deferred.make<void>();
44 const extReceived = yield* Deferred.make<void>();
45 const scope = yield* Scope.make();
46 const context = yield* Layer.buildWithScope(AcpAgent.layer(stdio), scope);
47
48 yield* Effect.gen(function* () {
49 const agent = yield* AcpAgent.AcpAgent;
50
51 yield* agent.handleInitialize(() =>
52 Effect.succeed({
53 protocolVersion: 1,
54 agentCapabilities: {},
55 agentInfo: {
56 name: "mock-agent",
57 version: "0.0.0",
58 },
59 }),
60 );
61 yield* agent.handleCancel((notification) =>
62 Ref.update(cancelNotifications, (current) => [...current, notification.sessionId]).pipe(
63 Effect.andThen(Deferred.succeed(cancelReceived, undefined)),
64 ),
65 );
66 yield* agent.handleExtNotification(
67 "x/ping",
68 Schema.Struct({ count: Schema.Number }),
69 (payload) =>
70 Ref.update(extNotifications, (current) => [...current, payload.count]).pipe(
71 Effect.andThen(Deferred.succeed(extReceived, undefined)),
72 ),
73 );
74
75 const permissionFiber = yield* agent.client
76 .requestPermission({
77 sessionId: "session-1",
78 toolCall: {
79 toolCallId: "tool-1",
80 title: "Allow mock action",
81 },
82 options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
83 })
84 .pipe(Effect.forkScoped);
85
86 const permissionRequest = yield* Schema.decodeEffect(
87 Schema.fromJsonString(RequestPermissionRequest),
88 )(yield* Queue.take(output));
89 assert.equal(permissionRequest.jsonrpc, "2.0");
90 assert.equal(permissionRequest.method, "session/request_permission");
91 assert.deepEqual(permissionRequest.params, {
92 sessionId: "session-1",
93 toolCall: {
94 toolCallId: "tool-1",
95 title: "Allow mock action",
96 },
97 options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
98 });
99 assert.deepEqual(permissionRequest.headers, []);
100
101 yield* Queue.offer(
102 input,
103 yield* encodeJsonl(RequestPermissionResponse, {
104 jsonrpc: "2.0",
105 id: permissionRequest.id,
106 result: {
107 outcome: {
108 outcome: "selected",
109 optionId: "allow",
110 },
111 },
112 }),
113 );
114
115 const permission = yield* Fiber.join(permissionFiber);
116 assert.equal(permission.outcome.outcome, "selected");
117
118 yield* Queue.offer(
119 input,
120 yield* encodeJsonl(InitializeRequest, {
121 jsonrpc: "2.0",
122 id: 2,
123 method: "initialize",
124 params: {
125 protocolVersion: 1,
126 clientCapabilities: {
127 fs: { readTextFile: false, writeTextFile: false },
128 terminal: false,
129 },
130 clientInfo: {
131 name: "effect-acp-test",
132 version: "0.0.0",
133 },
134 },
135 headers: [],
136 }),
137 );
138
139 const initResponse = yield* Schema.decodeEffect(Schema.fromJsonString(InitializeResponse))(
140 yield* Queue.take(output),
141 );
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);