Files
t3code/packages/effect-acp/src/agent.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

299 lines
9.8 KiB
TypeScript

import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import { assert, it } from "@effect/vitest";
import * as AcpAgent from "./agent.ts";
import * as AcpSchema from "./_generated/schema.gen.ts";
import type * as AcpProtocol from "./protocol.ts";
import {
encodeJsonl,
jsonRpcNotification,
jsonRpcRequest,
jsonRpcResponse,
} from "./_internal/shared.ts";
import { makeInMemoryStdio } from "./_internal/stdio.ts";
const RequestPermissionRequest = jsonRpcRequest(
"session/request_permission",
AcpSchema.RequestPermissionRequest,
);
const InitializeRequest = jsonRpcRequest("initialize", AcpSchema.InitializeRequest);
const InitializeResponse = jsonRpcResponse(AcpSchema.InitializeResponse);
const RequestPermissionResponse = jsonRpcResponse(AcpSchema.RequestPermissionResponse);
const SessionCancelNotification = jsonRpcNotification(
"session/cancel",
AcpSchema.CancelSessionNotification,
);
const ExtPingNotification = jsonRpcNotification("x/ping", Schema.Struct({ count: Schema.Number }));
const ExtRequest = jsonRpcRequest("x/test", Schema.Struct({ hello: Schema.String }));
const ExtResponse = jsonRpcResponse(Schema.Struct({ ok: Schema.Boolean }));
const decodeRequestPermissionRequest = Schema.decodeEffect(
Schema.fromJsonString(RequestPermissionRequest),
);
const decodeInitializeResponse = Schema.decodeEffect(Schema.fromJsonString(InitializeResponse));
const decodeExtResponse = Schema.decodeEffect(Schema.fromJsonString(ExtResponse));
it.effect("effect-acp agent handles core agent requests and outbound client requests", () =>
Effect.gen(function* () {
const { stdio, input, output } = yield* makeInMemoryStdio();
const cancelNotifications = yield* Ref.make<Array<string>>([]);
const extNotifications = yield* Ref.make<Array<number>>([]);
const requestContexts = yield* Ref.make<Array<AcpProtocol.AcpRequestContext>>([]);
const cancelReceived = yield* Deferred.make<void>();
const extReceived = yield* Deferred.make<void>();
const scope = yield* Scope.make();
const context = yield* Layer.buildWithScope(AcpAgent.layer(stdio), scope);
yield* Effect.gen(function* () {
const agent = yield* AcpAgent.AcpAgent;
yield* agent.handleInitialize((_request, requestContext) =>
Ref.update(requestContexts, (current) => [...current, requestContext]).pipe(
Effect.as({
protocolVersion: 2,
capabilities: {},
info: {
name: "mock-agent",
version: "0.0.0",
},
}),
),
);
yield* agent.handleExtRequest(
"x/test",
Schema.Struct({ hello: Schema.String }),
(_payload, requestContext) =>
Ref.update(requestContexts, (current) => [...current, requestContext]).pipe(
Effect.as({ ok: true }),
),
);
yield* agent.handleCancel((notification) =>
Ref.update(cancelNotifications, (current) => [...current, notification.sessionId]).pipe(
Effect.andThen(Deferred.succeed(cancelReceived, undefined)),
),
);
yield* agent.handleExtNotification(
"x/ping",
Schema.Struct({ count: Schema.Number }),
(payload) =>
Ref.update(extNotifications, (current) => [...current, payload.count]).pipe(
Effect.andThen(Deferred.succeed(extReceived, undefined)),
),
);
const permissionFiber = yield* agent.client
.requestPermission({
sessionId: "session-1",
title: "Allow mock action",
subject: {
type: "tool_call",
toolCall: {
toolCallId: "tool-1",
title: "Allow mock action",
},
},
options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
})
.pipe(Effect.forkScoped);
const permissionRequest = yield* decodeRequestPermissionRequest(yield* Queue.take(output));
assert.equal(permissionRequest.jsonrpc, "2.0");
assert.equal(permissionRequest.method, "session/request_permission");
assert.deepEqual(permissionRequest.params, {
sessionId: "session-1",
title: "Allow mock action",
subject: {
type: "tool_call",
toolCall: {
toolCallId: "tool-1",
title: "Allow mock action",
},
},
options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
});
assert.deepEqual(permissionRequest.headers, []);
yield* Queue.offer(
input,
yield* encodeJsonl(RequestPermissionResponse, {
jsonrpc: "2.0",
id: permissionRequest.id,
result: {
outcome: {
outcome: "selected",
optionId: "allow",
},
},
}),
);
const permission = yield* Fiber.join(permissionFiber);
assert.equal(permission.outcome.outcome, "selected");
yield* Queue.offer(
input,
yield* encodeJsonl(InitializeRequest, {
jsonrpc: "2.0",
id: 2,
method: "initialize",
params: {
protocolVersion: 2,
capabilities: {},
info: {
name: "effect-acp-test",
version: "0.0.0",
},
},
headers: [],
}),
);
const initResponse = yield* decodeInitializeResponse(yield* Queue.take(output));
assert.deepEqual(initResponse, {
jsonrpc: "2.0",
id: 2,
result: {
protocolVersion: 2,
capabilities: {},
info: {
name: "mock-agent",
version: "0.0.0",
},
},
});
yield* Queue.offer(
input,
yield* encodeJsonl(ExtRequest, {
jsonrpc: "2.0",
id: "extension-3",
method: "x/test",
params: { hello: "world" },
headers: [],
}),
);
const extResponse = yield* decodeExtResponse(yield* Queue.take(output));
assert.deepEqual(extResponse, {
jsonrpc: "2.0",
id: "extension-3",
result: { ok: true },
});
assert.deepEqual(yield* Ref.get(requestContexts), [
{ requestId: "$t3:jsonrpc:number:2", method: "initialize" },
{ requestId: "extension-3", method: "x/test" },
]);
yield* Queue.offer(
input,
yield* encodeJsonl(SessionCancelNotification, {
jsonrpc: "2.0",
method: "session/cancel",
params: {
sessionId: "session-1",
},
}),
);
yield* Queue.offer(
input,
yield* encodeJsonl(ExtPingNotification, {
jsonrpc: "2.0",
method: "x/ping",
params: { count: 2 },
}),
);
yield* Deferred.await(cancelReceived);
yield* Deferred.await(extReceived);
assert.deepEqual(yield* Ref.get(cancelNotifications), ["session-1"]);
assert.deepEqual(yield* Ref.get(extNotifications), [2]);
}).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
}),
);
it.effect("effect-acp agent uses distinct ids for RPC calls and extension requests", () =>
Effect.gen(function* () {
const { stdio, input, output } = yield* makeInMemoryStdio();
const scope = yield* Scope.make();
const context = yield* Layer.buildWithScope(AcpAgent.layer(stdio), scope);
yield* Effect.gen(function* () {
const agent = yield* AcpAgent.AcpAgent;
const permissionFiber = yield* agent.client
.requestPermission({
sessionId: "session-1",
title: "Allow mock action",
subject: {
type: "tool_call",
toolCall: {
toolCallId: "tool-1",
title: "Allow mock action",
},
},
options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
})
.pipe(Effect.forkScoped);
const extFiber = yield* agent.client
.extRequest("x/test", { hello: "world" })
.pipe(Effect.forkScoped);
const firstOutbound = yield* Queue.take(output);
const secondOutbound = yield* Queue.take(output);
const decodedPermission = Schema.decodeEffect(
Schema.fromJsonString(RequestPermissionRequest),
);
const decodedExt = Schema.decodeEffect(Schema.fromJsonString(ExtRequest));
const firstIsPermission = yield* decodedPermission(firstOutbound).pipe(
Effect.match({
onFailure: () => false,
onSuccess: () => true,
}),
);
const permissionRequest = firstIsPermission
? yield* decodedPermission(firstOutbound)
: yield* decodedPermission(secondOutbound);
const extRequest = firstIsPermission
? yield* decodedExt(secondOutbound)
: yield* decodedExt(firstOutbound);
assert.notEqual(permissionRequest.id, extRequest.id);
yield* Queue.offer(
input,
yield* encodeJsonl(RequestPermissionResponse, {
jsonrpc: "2.0",
id: permissionRequest.id,
result: {
outcome: {
outcome: "selected",
optionId: "allow",
},
},
}),
);
yield* Queue.offer(
input,
yield* encodeJsonl(ExtResponse, {
jsonrpc: "2.0",
id: extRequest.id,
result: { ok: true },
}),
);
const permission = yield* Fiber.join(permissionFiber);
assert.equal(permission.outcome.outcome, "selected");
assert.deepEqual(yield* Fiber.join(extFiber), { ok: true });
}).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void)));
}),
);