agent/client separation
This commit is contained in:
1 parent
b750558c10
commit
ffccd63d1e
14 files changed
+649
-1202
No files matched your search
@@ -1,31 +1,33 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* Minimal NDJSON JSON-RPC "agent" for ACP client tests.
|
||||
* Reads stdin lines; writes responses/notifications to stdout.
|
||||
*/
|
||||
import * as readline from "node:readline";
|
||||
import { appendFileSync } from "node:fs";
|
||||
|
||||
import { AGENT_METHODS, CLIENT_METHODS } from "effect-acp/schema";
|
||||
import * as Effect from "effect/Effect";
|
||||
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
||||
|
||||
import * as EffectAcpAgent from "effect-acp/agent";
|
||||
import * as AcpError from "effect-acp/errors";
|
||||
import type * as AcpSchema from "effect-acp/schema";
|
||||
|
||||
const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
|
||||
const requestLogPath = process.env.T3_ACP_REQUEST_LOG_PATH;
|
||||
const emitToolCalls = process.env.T3_ACP_EMIT_TOOL_CALLS === "1";
|
||||
const emitAskQuestion = process.env.T3_ACP_EMIT_ASK_QUESTION === "1";
|
||||
const failSetConfigOption = process.env.T3_ACP_FAIL_SET_CONFIG_OPTION === "1";
|
||||
const exitOnSetConfigOption = process.env.T3_ACP_EXIT_ON_SET_CONFIG_OPTION === "1";
|
||||
const sessionId = "mock-session-1";
|
||||
|
||||
let currentModeId = "ask";
|
||||
let currentModelId = "default";
|
||||
let nextRequestId = 1;
|
||||
const cancelledSessions = new Set<string>();
|
||||
|
||||
function configOptions() {
|
||||
function configOptions(): ReadonlyArray<AcpSchema.SessionConfigOption> {
|
||||
return [
|
||||
{
|
||||
id: "model",
|
||||
name: "Model",
|
||||
category: "model",
|
||||
type: "select",
|
||||
type: "select" as const,
|
||||
currentValue: currentModelId,
|
||||
options: [
|
||||
{ value: "default", name: "Auto" },
|
||||
@@ -37,7 +39,7 @@ function configOptions() {
|
||||
];
|
||||
}
|
||||
|
||||
const availableModes = [
|
||||
const availableModes: ReadonlyArray<AcpSchema.SessionMode> = [
|
||||
{
|
||||
id: "ask",
|
||||
name: "Ask",
|
||||
@@ -54,227 +56,113 @@ const availableModes = [
|
||||
description: "Write and modify code with full tool access",
|
||||
},
|
||||
];
|
||||
const pendingPermissionRequests = new Map();
|
||||
const pendingAskQuestionRequests = new Map();
|
||||
const cancelledPromptRequestIds = new Set<number | string>();
|
||||
|
||||
function send(obj: unknown) {
|
||||
process.stdout.write(`${JSON.stringify(obj)}\n`);
|
||||
}
|
||||
|
||||
function modeState() {
|
||||
function modeState(): AcpSchema.SessionModeState {
|
||||
return {
|
||||
currentModeId,
|
||||
availableModes,
|
||||
};
|
||||
}
|
||||
|
||||
function sendSessionUpdate(update: unknown, session = sessionId) {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
method: CLIENT_METHODS.session_update,
|
||||
params: {
|
||||
sessionId: session,
|
||||
update,
|
||||
},
|
||||
});
|
||||
}
|
||||
const program = Effect.gen(function* () {
|
||||
const agent = yield* EffectAcpAgent.AcpAgent;
|
||||
|
||||
rl.on("line", (line) => {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) return;
|
||||
let msg;
|
||||
try {
|
||||
msg = JSON.parse(trimmed);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (!msg || typeof msg !== "object") return;
|
||||
if (requestLogPath) {
|
||||
appendFileSync(requestLogPath, `${JSON.stringify(msg)}\n`, "utf8");
|
||||
}
|
||||
yield* agent.handleInitialize(() =>
|
||||
Effect.succeed({
|
||||
protocolVersion: 1,
|
||||
agentCapabilities: { loadSession: true },
|
||||
}),
|
||||
);
|
||||
|
||||
const rpcMessage = msg as {
|
||||
id?: number | string;
|
||||
method?: string;
|
||||
params?: Record<string, unknown>;
|
||||
};
|
||||
const id = rpcMessage.id;
|
||||
const method = rpcMessage.method;
|
||||
yield* agent.handleAuthenticate(() => Effect.succeed({}));
|
||||
|
||||
if (method === undefined && id !== undefined && pendingPermissionRequests.has(id)) {
|
||||
const pending = pendingPermissionRequests.get(id);
|
||||
pendingPermissionRequests.delete(id);
|
||||
const outcome = (rpcMessage.params?.outcome ??
|
||||
(msg as { result?: { outcome?: unknown } }).result?.outcome) as
|
||||
| { outcome?: unknown }
|
||||
| undefined;
|
||||
const cancelled =
|
||||
cancelledPromptRequestIds.has(pending.promptRequestId) ||
|
||||
(typeof outcome === "object" &&
|
||||
outcome !== null &&
|
||||
"outcome" in outcome &&
|
||||
outcome.outcome === "cancelled");
|
||||
cancelledPromptRequestIds.delete(pending.promptRequestId);
|
||||
sendSessionUpdate(
|
||||
{
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId: pending.toolCallId,
|
||||
title: "Terminal",
|
||||
kind: "execute",
|
||||
status: "completed",
|
||||
rawOutput: {
|
||||
exitCode: 0,
|
||||
stdout: '{ "name": "t3" }',
|
||||
stderr: "",
|
||||
yield* agent.handleCreateSession(() =>
|
||||
Effect.succeed({
|
||||
sessionId,
|
||||
modes: modeState(),
|
||||
configOptions: configOptions(),
|
||||
}),
|
||||
);
|
||||
|
||||
yield* agent.handleLoadSession((request) =>
|
||||
agent.client
|
||||
.sessionUpdate({
|
||||
sessionId: String(request.sessionId ?? sessionId),
|
||||
update: {
|
||||
sessionUpdate: "user_message_chunk",
|
||||
content: { type: "text", text: "replay" },
|
||||
},
|
||||
},
|
||||
pending.sessionId,
|
||||
);
|
||||
sendSessionUpdate(
|
||||
{
|
||||
sessionUpdate: "agent_message_chunk",
|
||||
content: { type: "text", text: "hello from mock" },
|
||||
},
|
||||
pending.sessionId,
|
||||
);
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id: pending.promptRequestId,
|
||||
result: { stopReason: cancelled ? "cancelled" : "end_turn" },
|
||||
});
|
||||
return;
|
||||
}
|
||||
})
|
||||
.pipe(
|
||||
Effect.as({
|
||||
modes: modeState(),
|
||||
configOptions: configOptions(),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
if (method === undefined && id !== undefined && pendingAskQuestionRequests.has(id)) {
|
||||
const pending = pendingAskQuestionRequests.get(id);
|
||||
pendingAskQuestionRequests.delete(id);
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id: pending.promptRequestId,
|
||||
result: { stopReason: "end_turn" },
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === AGENT_METHODS.initialize && id !== undefined) {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
protocolVersion: 1,
|
||||
agentCapabilities: { loadSession: true },
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === AGENT_METHODS.authenticate && id !== undefined) {
|
||||
send({ jsonrpc: "2.0", id, result: { authenticated: true } });
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === AGENT_METHODS.session_new && id !== undefined) {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
sessionId,
|
||||
modes: modeState(),
|
||||
configOptions: configOptions(),
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === AGENT_METHODS.session_load && id !== undefined) {
|
||||
const requestedSessionId = rpcMessage.params?.sessionId ?? sessionId;
|
||||
sendSessionUpdate(
|
||||
{
|
||||
sessionUpdate: "user_message_chunk",
|
||||
content: { type: "text", text: "replay" },
|
||||
},
|
||||
String(requestedSessionId),
|
||||
);
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
modes: modeState(),
|
||||
configOptions: configOptions(),
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === AGENT_METHODS.session_set_config_option && id !== undefined) {
|
||||
if (exitOnSetConfigOption) {
|
||||
process.exit(7);
|
||||
}
|
||||
if (failSetConfigOption) {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
error: {
|
||||
code: -32602,
|
||||
message: "Mock invalid params for session/set_config_option",
|
||||
data: {
|
||||
method,
|
||||
params: rpcMessage.params,
|
||||
yield* agent.handleSetSessionConfigOption((request) =>
|
||||
Effect.gen(function* () {
|
||||
if (exitOnSetConfigOption) {
|
||||
return yield* Effect.sync(() => {
|
||||
process.exit(7);
|
||||
});
|
||||
}
|
||||
if (failSetConfigOption) {
|
||||
return yield* AcpError.AcpRequestError.invalidParams(
|
||||
"Mock invalid params for session/set_config_option",
|
||||
{
|
||||
method: "session/set_config_option",
|
||||
params: request,
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
const configId = rpcMessage.params?.configId;
|
||||
const value = rpcMessage.params?.value;
|
||||
if (configId === "model" && typeof value === "string") {
|
||||
currentModelId = value;
|
||||
}
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: { configOptions: configOptions() },
|
||||
});
|
||||
return;
|
||||
}
|
||||
);
|
||||
}
|
||||
if (request.configId === "model" && typeof request.value === "string") {
|
||||
currentModelId = request.value;
|
||||
}
|
||||
return {
|
||||
configOptions: configOptions(),
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
if (method === AGENT_METHODS.session_prompt && id !== undefined) {
|
||||
const requestedSessionId = String(rpcMessage.params?.sessionId ?? sessionId);
|
||||
if (emitToolCalls) {
|
||||
const toolCallId = "tool-call-1";
|
||||
const permissionRequestId = nextRequestId++;
|
||||
sendSessionUpdate(
|
||||
{
|
||||
sessionUpdate: "tool_call",
|
||||
toolCallId,
|
||||
title: "Terminal",
|
||||
kind: "execute",
|
||||
status: "pending",
|
||||
rawInput: {
|
||||
command: ["cat", "server/package.json"],
|
||||
yield* agent.handleCancel(({ sessionId }) =>
|
||||
Effect.sync(() => {
|
||||
cancelledSessions.add(String(sessionId));
|
||||
}),
|
||||
);
|
||||
|
||||
yield* agent.handlePrompt((request) =>
|
||||
Effect.gen(function* () {
|
||||
const requestedSessionId = String(request.sessionId ?? sessionId);
|
||||
|
||||
if (emitToolCalls) {
|
||||
const toolCallId = "tool-call-1";
|
||||
|
||||
yield* agent.client.sessionUpdate({
|
||||
sessionId: requestedSessionId,
|
||||
update: {
|
||||
sessionUpdate: "tool_call",
|
||||
toolCallId,
|
||||
title: "Terminal",
|
||||
kind: "execute",
|
||||
status: "pending",
|
||||
rawInput: {
|
||||
command: ["cat", "server/package.json"],
|
||||
},
|
||||
},
|
||||
},
|
||||
requestedSessionId,
|
||||
);
|
||||
sendSessionUpdate(
|
||||
{
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId,
|
||||
status: "in_progress",
|
||||
},
|
||||
requestedSessionId,
|
||||
);
|
||||
pendingPermissionRequests.set(permissionRequestId, {
|
||||
promptRequestId: id,
|
||||
sessionId: requestedSessionId,
|
||||
toolCallId,
|
||||
});
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id: permissionRequestId,
|
||||
method: CLIENT_METHODS.session_request_permission,
|
||||
params: {
|
||||
});
|
||||
|
||||
yield* agent.client.sessionUpdate({
|
||||
sessionId: requestedSessionId,
|
||||
update: {
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId,
|
||||
status: "in_progress",
|
||||
},
|
||||
});
|
||||
|
||||
const permission = yield* agent.client.requestPermission({
|
||||
sessionId: requestedSessionId,
|
||||
toolCall: {
|
||||
toolCallId,
|
||||
@@ -296,21 +184,41 @@ rl.on("line", (line) => {
|
||||
{ optionId: "allow-always", name: "Allow always", kind: "allow_always" },
|
||||
{ optionId: "reject-once", name: "Reject", kind: "reject_once" },
|
||||
],
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (emitAskQuestion) {
|
||||
const askQuestionRequestId = nextRequestId++;
|
||||
pendingAskQuestionRequests.set(askQuestionRequestId, {
|
||||
promptRequestId: id,
|
||||
sessionId: requestedSessionId,
|
||||
});
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id: askQuestionRequestId,
|
||||
method: "cursor/ask_question",
|
||||
params: {
|
||||
});
|
||||
|
||||
const cancelled =
|
||||
cancelledSessions.delete(requestedSessionId) ||
|
||||
permission.outcome.outcome === "cancelled";
|
||||
|
||||
yield* agent.client.sessionUpdate({
|
||||
sessionId: requestedSessionId,
|
||||
update: {
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId,
|
||||
title: "Terminal",
|
||||
kind: "execute",
|
||||
status: "completed",
|
||||
rawOutput: {
|
||||
exitCode: 0,
|
||||
stdout: '{ "name": "t3" }',
|
||||
stderr: "",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
yield* agent.client.sessionUpdate({
|
||||
sessionId: requestedSessionId,
|
||||
update: {
|
||||
sessionUpdate: "agent_message_chunk",
|
||||
content: { type: "text", text: "hello from mock" },
|
||||
},
|
||||
});
|
||||
|
||||
return { stopReason: cancelled ? "cancelled" : "end_turn" };
|
||||
}
|
||||
|
||||
if (emitAskQuestion) {
|
||||
yield* agent.client.extRequest("cursor/ask_question", {
|
||||
toolCallId: "ask-question-tool-call-1",
|
||||
title: "Question",
|
||||
questions: [
|
||||
@@ -323,83 +231,112 @@ rl.on("line", (line) => {
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
return { stopReason: "end_turn" };
|
||||
}
|
||||
|
||||
yield* agent.client.sessionUpdate({
|
||||
sessionId: requestedSessionId,
|
||||
update: {
|
||||
sessionUpdate: "plan",
|
||||
entries: [
|
||||
{
|
||||
content: "Inspect mock ACP state",
|
||||
priority: "high",
|
||||
status: "completed",
|
||||
},
|
||||
{
|
||||
content: "Implement the requested change",
|
||||
priority: "high",
|
||||
status: "in_progress",
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
sendSessionUpdate(
|
||||
{
|
||||
sessionUpdate: "plan",
|
||||
explanation: `Mock plan while in ${currentModeId}`,
|
||||
entries: [
|
||||
{
|
||||
content: "Inspect mock ACP state",
|
||||
priority: "high",
|
||||
status: "completed",
|
||||
},
|
||||
{
|
||||
content: "Implement the requested change",
|
||||
priority: "high",
|
||||
status: "in_progress",
|
||||
},
|
||||
],
|
||||
},
|
||||
requestedSessionId,
|
||||
);
|
||||
sendSessionUpdate(
|
||||
{
|
||||
sessionUpdate: "agent_message_chunk",
|
||||
content: { type: "text", text: "hello from mock" },
|
||||
},
|
||||
requestedSessionId,
|
||||
);
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: { stopReason: "end_turn" },
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
(method === AGENT_METHODS.session_set_mode || method === "session/mode/set") &&
|
||||
id !== undefined
|
||||
) {
|
||||
yield* agent.client.sessionUpdate({
|
||||
sessionId: requestedSessionId,
|
||||
update: {
|
||||
sessionUpdate: "agent_message_chunk",
|
||||
content: { type: "text", text: "hello from mock" },
|
||||
},
|
||||
});
|
||||
|
||||
return { stopReason: "end_turn" };
|
||||
}),
|
||||
);
|
||||
|
||||
yield* agent.handleUnknownExtRequest((method, params) => {
|
||||
if (method !== "session/mode/set") {
|
||||
return Effect.fail(AcpError.AcpRequestError.methodNotFound(method));
|
||||
}
|
||||
|
||||
const nextModeId =
|
||||
typeof rpcMessage.params?.modeId === "string"
|
||||
? rpcMessage.params.modeId
|
||||
: typeof rpcMessage.params?.mode === "string"
|
||||
? rpcMessage.params.mode
|
||||
typeof params === "object" &&
|
||||
params !== null &&
|
||||
"modeId" in params &&
|
||||
typeof params.modeId === "string"
|
||||
? params.modeId
|
||||
: typeof params === "object" &&
|
||||
params !== null &&
|
||||
"mode" in params &&
|
||||
typeof params.mode === "string"
|
||||
? params.mode
|
||||
: undefined;
|
||||
const requestedSessionId =
|
||||
typeof params === "object" &&
|
||||
params !== null &&
|
||||
"sessionId" in params &&
|
||||
typeof params.sessionId === "string"
|
||||
? params.sessionId
|
||||
: sessionId;
|
||||
|
||||
if (typeof nextModeId === "string" && nextModeId.trim()) {
|
||||
currentModeId = nextModeId.trim();
|
||||
sendSessionUpdate({
|
||||
sessionUpdate: "current_mode_update",
|
||||
currentModeId,
|
||||
});
|
||||
return agent.client
|
||||
.sessionUpdate({
|
||||
sessionId: requestedSessionId,
|
||||
update: {
|
||||
sessionUpdate: "current_mode_update",
|
||||
currentModeId,
|
||||
},
|
||||
})
|
||||
.pipe(Effect.as({}));
|
||||
}
|
||||
send({ jsonrpc: "2.0", id, result: {} });
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === AGENT_METHODS.session_cancel) {
|
||||
const cancelledSessionId = rpcMessage.params?.sessionId;
|
||||
for (const pending of pendingPermissionRequests.values()) {
|
||||
if (pending.sessionId === cancelledSessionId) {
|
||||
cancelledPromptRequestIds.add(pending.promptRequestId);
|
||||
}
|
||||
}
|
||||
if (id !== undefined) {
|
||||
send({ jsonrpc: "2.0", id, result: null });
|
||||
}
|
||||
return;
|
||||
}
|
||||
return Effect.succeed({});
|
||||
});
|
||||
|
||||
if (id !== undefined) {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
error: { code: -32601, message: `Unhandled method: ${String(method)}` },
|
||||
});
|
||||
}
|
||||
});
|
||||
return yield* Effect.never;
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
EffectAcpAgent.layerStdio(
|
||||
requestLogPath
|
||||
? {
|
||||
logIncoming: true,
|
||||
logger: (event) => {
|
||||
if (event.direction !== "incoming" || event.stage !== "raw") {
|
||||
return Effect.void;
|
||||
}
|
||||
if (typeof event.payload !== "string") {
|
||||
return Effect.void;
|
||||
}
|
||||
const payload = event.payload;
|
||||
return Effect.sync(() => {
|
||||
appendFileSync(
|
||||
requestLogPath,
|
||||
payload.endsWith("\n") ? payload : `${payload}\n`,
|
||||
"utf8",
|
||||
);
|
||||
});
|
||||
},
|
||||
}
|
||||
: {},
|
||||
),
|
||||
),
|
||||
Effect.scoped,
|
||||
Effect.provide(NodeServices.layer),
|
||||
);
|
||||
|
||||
NodeRuntime.runMain(program);
|
||||
Reference in new issue
Block a user