Restore Cursor adapter on stacked branch
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
12d7a5c411
commit
e3c98ba52a
10 files changed
+2504
-12
No files matched your search
@@ -0,0 +1,655 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import { PassThrough } from "node:stream";
|
||||
import readline from "node:readline";
|
||||
|
||||
import { ApprovalRequestId, ThreadId } from "@t3tools/contracts";
|
||||
import { assert, describe, it } from "@effect/vitest";
|
||||
import { Effect, Fiber, Stream } from "effect";
|
||||
|
||||
import { ProviderAdapterValidationError } from "../Errors.ts";
|
||||
import { CursorAdapter } from "../Services/CursorAdapter.ts";
|
||||
import { makeCursorAdapterLive } from "./CursorAdapter.ts";
|
||||
|
||||
const THREAD_ID = ThreadId.makeUnsafe("thread-cursor-1");
|
||||
const RESUME_THREAD_ID = ThreadId.makeUnsafe("thread-cursor-resume");
|
||||
const LEGACY_RESUME_THREAD_ID = ThreadId.makeUnsafe("thread-cursor-legacy");
|
||||
|
||||
class FakeCursorAcpProcess extends EventEmitter {
|
||||
readonly stdin = new PassThrough();
|
||||
readonly stdout = new PassThrough();
|
||||
readonly stderr = new PassThrough();
|
||||
readonly requests: Array<{ method: string; params: unknown }> = [];
|
||||
killed = false;
|
||||
|
||||
private readonly input = readline.createInterface({ input: this.stdin });
|
||||
private permissionRequestId = 700;
|
||||
lastPermissionSelection: string | undefined;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.input.on("line", (line) => {
|
||||
const message = JSON.parse(line) as Record<string, unknown>;
|
||||
if (typeof message.method === "string") {
|
||||
this.handleRequest(message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.id === this.permissionRequestId) {
|
||||
const optionId =
|
||||
(message.result as { outcome?: { optionId?: unknown } } | undefined)?.outcome?.optionId;
|
||||
if (typeof optionId === "string") {
|
||||
this.lastPermissionSelection = optionId;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
kill(): boolean {
|
||||
if (this.killed) {
|
||||
return true;
|
||||
}
|
||||
this.killed = true;
|
||||
this.emit("exit", 0, null);
|
||||
return true;
|
||||
}
|
||||
|
||||
emitPermissionRequest(): void {
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id: this.permissionRequestId,
|
||||
method: "session/request_permission",
|
||||
params: {
|
||||
sessionId: "acp-session-1",
|
||||
toolCall: {
|
||||
toolCallId: "tool-perm-1",
|
||||
kind: "execute",
|
||||
title: "`pwd`",
|
||||
},
|
||||
options: [
|
||||
{ optionId: "allow-once", kind: "allow_once" },
|
||||
{ optionId: "allow-always", kind: "allow_always" },
|
||||
{ optionId: "reject-once", kind: "reject_once" },
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private handleRequest(message: Record<string, unknown>): void {
|
||||
const method = message.method;
|
||||
const id = message.id;
|
||||
if (typeof method !== "string" || (typeof id !== "string" && typeof id !== "number")) {
|
||||
return;
|
||||
}
|
||||
this.requests.push({ method, params: message.params });
|
||||
|
||||
switch (method) {
|
||||
case "initialize": {
|
||||
const protocolVersion = (message.params as { protocolVersion?: unknown } | undefined)
|
||||
?.protocolVersion;
|
||||
if (typeof protocolVersion !== "number") {
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
error: {
|
||||
code: -32602,
|
||||
message: "Invalid params",
|
||||
data: {
|
||||
_errors: [],
|
||||
protocolVersion: {
|
||||
_errors: ["Invalid input: expected number, received undefined"],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
protocolVersion: 1,
|
||||
agentCapabilities: {
|
||||
loadSession: true,
|
||||
},
|
||||
authMethods: [{ id: "cursor_login" }],
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
case "authenticate":
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {},
|
||||
});
|
||||
return;
|
||||
case "session/new":
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
sessionId: "acp-session-1",
|
||||
modes: {
|
||||
currentModeId: "agent",
|
||||
availableModes: [{ id: "agent" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
case "session/load":
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {},
|
||||
});
|
||||
return;
|
||||
case "session/set_model":
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {},
|
||||
});
|
||||
return;
|
||||
case "session/prompt": {
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "acp-session-1",
|
||||
update: {
|
||||
sessionUpdate: "agent_thought_chunk",
|
||||
content: {
|
||||
type: "text",
|
||||
text: "thinking",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "acp-session-1",
|
||||
update: {
|
||||
sessionUpdate: "agent_message_chunk",
|
||||
content: {
|
||||
type: "text",
|
||||
text: "hello",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "acp-session-1",
|
||||
update: {
|
||||
sessionUpdate: "tool_call",
|
||||
toolCallId: "tool-1",
|
||||
kind: "execute",
|
||||
title: "`pwd`",
|
||||
rawInput: { command: "pwd" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "acp-session-1",
|
||||
update: {
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId: "tool-1",
|
||||
status: "completed",
|
||||
rawOutput: {
|
||||
exitCode: 0,
|
||||
stdout: "/tmp/project",
|
||||
stderr: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result: {
|
||||
stopReason: "end_turn",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
case "session/cancel":
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
error: {
|
||||
code: -32601,
|
||||
message: "Method not found",
|
||||
},
|
||||
});
|
||||
return;
|
||||
default:
|
||||
this.emitServerMessage({
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
error: {
|
||||
code: -32601,
|
||||
message: `Unhandled method: ${method}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private emitServerMessage(message: unknown): void {
|
||||
this.stdout.write(`${JSON.stringify(message)}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
describe("CursorAdapterLive", () => {
|
||||
it.effect("returns validation error for non-cursor provider on startSession", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
const result = yield* adapter
|
||||
.startSession({
|
||||
provider: "codex",
|
||||
threadId: THREAD_ID,
|
||||
runtimeMode: "full-access",
|
||||
})
|
||||
.pipe(Effect.result);
|
||||
|
||||
assert.equal(result._tag, "Failure");
|
||||
if (result._tag !== "Failure") {
|
||||
return;
|
||||
}
|
||||
assert.deepEqual(
|
||||
result.failure,
|
||||
new ProviderAdapterValidationError({
|
||||
provider: "cursor",
|
||||
operation: "startSession",
|
||||
issue: "Expected provider 'cursor' but received 'codex'.",
|
||||
}),
|
||||
);
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("maps ACP prompt/update events into canonical runtime events", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
|
||||
const eventsFiber = yield* Stream.take(adapter.streamEvents, 13).pipe(
|
||||
Stream.runCollect,
|
||||
Effect.forkChild,
|
||||
);
|
||||
|
||||
const session = yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: THREAD_ID,
|
||||
cwd: "/tmp/project",
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
const turn = yield* adapter.sendTurn({
|
||||
threadId: session.threadId,
|
||||
input: "hello",
|
||||
attachments: [],
|
||||
});
|
||||
|
||||
const events = Array.from(yield* Fiber.join(eventsFiber));
|
||||
assert.deepEqual(
|
||||
events.map((event) => event.type),
|
||||
[
|
||||
"session.configured",
|
||||
"auth.status",
|
||||
"auth.status",
|
||||
"session.started",
|
||||
"thread.started",
|
||||
"session.state.changed",
|
||||
"turn.started",
|
||||
"content.delta",
|
||||
"content.delta",
|
||||
"item.started",
|
||||
"item.completed",
|
||||
"item.completed",
|
||||
"turn.completed",
|
||||
],
|
||||
);
|
||||
|
||||
const turnStarted = events[6];
|
||||
assert.equal(turnStarted?.type, "turn.started");
|
||||
if (turnStarted?.type === "turn.started") {
|
||||
assert.equal(String(turnStarted.turnId), String(turn.turnId));
|
||||
}
|
||||
|
||||
const completion = events[12];
|
||||
assert.equal(completion?.type, "turn.completed");
|
||||
if (completion?.type === "turn.completed") {
|
||||
assert.equal(completion.payload.state, "completed");
|
||||
}
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("passes requested model to ACP process startup", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
let createProcessInput:
|
||||
| {
|
||||
readonly binaryPath: string;
|
||||
readonly cwd: string;
|
||||
readonly env: NodeJS.ProcessEnv;
|
||||
readonly model?: string;
|
||||
}
|
||||
| undefined;
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: (input) => {
|
||||
createProcessInput = input;
|
||||
return fake as never;
|
||||
},
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: THREAD_ID,
|
||||
model: "composer-1.5",
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
assert.deepEqual(createProcessInput?.model, "composer-1.5");
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("writes provider-native observability records when enabled", () => {
|
||||
const nativeEvents: Array<{
|
||||
event?: {
|
||||
provider?: string;
|
||||
method?: string;
|
||||
threadId?: string;
|
||||
};
|
||||
}> = [];
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
nativeEventLogger: {
|
||||
filePath: "memory://cursor-native-events",
|
||||
write: (event) => {
|
||||
nativeEvents.push(event as (typeof nativeEvents)[number]);
|
||||
return Effect.void;
|
||||
},
|
||||
close: () => Effect.void,
|
||||
},
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
const session = yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: THREAD_ID,
|
||||
cwd: "/tmp/project",
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
assert.equal(nativeEvents.length > 0, true);
|
||||
assert.equal(nativeEvents.some((record) => record.event?.provider === "cursor"), true);
|
||||
assert.equal(nativeEvents.some((record) => record.event?.threadId === session.threadId), true);
|
||||
assert.equal(nativeEvents.some((record) => record.event?.method === "cursor/acp/response"), true);
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("resumes ACP session using resumeCursor.acpSessionId", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
const session = yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: RESUME_THREAD_ID,
|
||||
cwd: "/tmp/project",
|
||||
resumeCursor: {
|
||||
acpSessionId: "acp-session-resume",
|
||||
},
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
const methods = new Set(fake.requests.map((request) => request.method));
|
||||
assert.equal(methods.has("session/load"), true);
|
||||
assert.equal(methods.has("session/new"), false);
|
||||
|
||||
const loadRequest = fake.requests.find((request) => request.method === "session/load");
|
||||
assert.deepEqual(loadRequest?.params, {
|
||||
sessionId: "acp-session-resume",
|
||||
cwd: "/tmp/project",
|
||||
mcpServers: [],
|
||||
});
|
||||
assert.equal(session.threadId, RESUME_THREAD_ID);
|
||||
assert.deepEqual(session.resumeCursor, {
|
||||
acpSessionId: "acp-session-resume",
|
||||
});
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("accepts legacy resumeCursor.sessionId for ACP session resume", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
const session = yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: LEGACY_RESUME_THREAD_ID,
|
||||
cwd: "/tmp/project",
|
||||
resumeCursor: {
|
||||
sessionId: "acp-session-legacy",
|
||||
},
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
const loadRequest = fake.requests.find((request) => request.method === "session/load");
|
||||
assert.deepEqual(loadRequest?.params, {
|
||||
sessionId: "acp-session-legacy",
|
||||
cwd: "/tmp/project",
|
||||
mcpServers: [],
|
||||
});
|
||||
assert.equal(session.threadId, LEGACY_RESUME_THREAD_ID);
|
||||
assert.deepEqual(session.resumeCursor, {
|
||||
acpSessionId: "acp-session-legacy",
|
||||
});
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("bridges permission requests to request.opened/request.resolved", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
|
||||
const session = yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: THREAD_ID,
|
||||
runtimeMode: "approval-required",
|
||||
});
|
||||
|
||||
// consume startup events
|
||||
yield* Stream.take(adapter.streamEvents, 6).pipe(Stream.runDrain);
|
||||
|
||||
fake.emitPermissionRequest();
|
||||
|
||||
const opened = yield* Stream.runHead(adapter.streamEvents);
|
||||
assert.equal(opened._tag, "Some");
|
||||
if (opened._tag !== "Some") {
|
||||
return;
|
||||
}
|
||||
assert.equal(opened.value.type, "request.opened");
|
||||
if (opened.value.type !== "request.opened") {
|
||||
return;
|
||||
}
|
||||
const runtimeRequestId = opened.value.requestId;
|
||||
assert.equal(typeof runtimeRequestId, "string");
|
||||
if (runtimeRequestId === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
yield* adapter.respondToRequest(
|
||||
session.threadId,
|
||||
ApprovalRequestId.makeUnsafe(runtimeRequestId),
|
||||
"acceptForSession",
|
||||
);
|
||||
|
||||
const resolved = yield* Stream.runHead(adapter.streamEvents);
|
||||
assert.equal(resolved._tag, "Some");
|
||||
if (resolved._tag !== "Some") {
|
||||
return;
|
||||
}
|
||||
assert.equal(resolved.value.type, "request.resolved");
|
||||
if (resolved.value.type !== "request.resolved") {
|
||||
return;
|
||||
}
|
||||
assert.equal(resolved.value.payload.decision, "acceptForSession");
|
||||
assert.equal(fake.lastPermissionSelection, "allow-always");
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("auto-approves cursor permission requests when approval policy is never", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
|
||||
yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: THREAD_ID,
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
// consume startup events
|
||||
yield* Stream.take(adapter.streamEvents, 6).pipe(Stream.runDrain);
|
||||
|
||||
fake.emitPermissionRequest();
|
||||
|
||||
const resolved = yield* Stream.runHead(adapter.streamEvents);
|
||||
assert.equal(resolved._tag, "Some");
|
||||
if (resolved._tag !== "Some") {
|
||||
return;
|
||||
}
|
||||
|
||||
assert.equal(resolved.value.type, "request.resolved");
|
||||
if (resolved.value.type !== "request.resolved") {
|
||||
return;
|
||||
}
|
||||
|
||||
assert.equal(resolved.value.payload.decision, "acceptForSession");
|
||||
assert.equal(fake.lastPermissionSelection, "allow-always");
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("rejects empty prompt input before starting a turn", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
const session = yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: THREAD_ID,
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
yield* Stream.take(adapter.streamEvents, 6).pipe(Stream.runDrain);
|
||||
|
||||
const result = yield* adapter
|
||||
.sendTurn({
|
||||
threadId: session.threadId,
|
||||
input: " ",
|
||||
attachments: [],
|
||||
})
|
||||
.pipe(Effect.result);
|
||||
|
||||
assert.equal(result._tag, "Failure");
|
||||
if (result._tag !== "Failure") {
|
||||
return;
|
||||
}
|
||||
assert.deepEqual(
|
||||
result.failure,
|
||||
new ProviderAdapterValidationError({
|
||||
provider: "cursor",
|
||||
operation: "sendTurn",
|
||||
issue: "Turn input must be non-empty.",
|
||||
}),
|
||||
);
|
||||
|
||||
assert.equal(fake.requests.some((request) => request.method === "session/prompt"), false);
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("keeps tool_call item types consistent through tool_call_update", () => {
|
||||
const fake = new FakeCursorAcpProcess();
|
||||
const layer = makeCursorAdapterLive({
|
||||
createProcess: () => fake as never,
|
||||
});
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* CursorAdapter;
|
||||
|
||||
const eventsFiber = yield* Stream.take(adapter.streamEvents, 13).pipe(
|
||||
Stream.runCollect,
|
||||
Effect.forkChild,
|
||||
);
|
||||
|
||||
const session = yield* adapter.startSession({
|
||||
provider: "cursor",
|
||||
threadId: THREAD_ID,
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
|
||||
yield* adapter.sendTurn({
|
||||
threadId: session.threadId,
|
||||
input: "hello",
|
||||
attachments: [],
|
||||
});
|
||||
|
||||
const events = Array.from(yield* Fiber.join(eventsFiber));
|
||||
const started = events.find(
|
||||
(event) => event.type === "item.started" && String(event.itemId) === "tool-1",
|
||||
);
|
||||
const completed = events.find(
|
||||
(event) => event.type === "item.completed" && String(event.itemId) === "tool-1",
|
||||
);
|
||||
|
||||
assert.equal(started?.type, "item.started");
|
||||
assert.equal(completed?.type, "item.completed");
|
||||
if (started?.type !== "item.started" || completed?.type !== "item.completed") {
|
||||
return;
|
||||
}
|
||||
|
||||
assert.equal(started.payload.itemType, "command_execution");
|
||||
assert.equal(completed.payload.itemType, "command_execution");
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
@@ -5,6 +5,7 @@ import { assertFailure } from "@effect/vitest/utils";
|
||||
import { Effect, Layer, Stream } from "effect";
|
||||
|
||||
import { CodexAdapter, CodexAdapterShape } from "../Services/CodexAdapter.ts";
|
||||
import { CursorAdapter, CursorAdapterShape } from "../Services/CursorAdapter.ts";
|
||||
import { ProviderAdapterRegistry } from "../Services/ProviderAdapterRegistry.ts";
|
||||
import { ProviderAdapterRegistryLive } from "./ProviderAdapterRegistry.ts";
|
||||
import { ProviderUnsupportedError } from "../Errors.ts";
|
||||
@@ -27,11 +28,30 @@ const fakeCodexAdapter: CodexAdapterShape = {
|
||||
streamEvents: Stream.empty,
|
||||
};
|
||||
|
||||
const fakeCursorAdapter: CursorAdapterShape = {
|
||||
provider: "cursor",
|
||||
capabilities: { sessionModelSwitch: "unsupported" },
|
||||
startSession: vi.fn(),
|
||||
sendTurn: vi.fn(),
|
||||
interruptTurn: vi.fn(),
|
||||
respondToRequest: vi.fn(),
|
||||
respondToUserInput: vi.fn(),
|
||||
stopSession: vi.fn(),
|
||||
listSessions: vi.fn(),
|
||||
hasSession: vi.fn(),
|
||||
readThread: vi.fn(),
|
||||
rollbackThread: vi.fn(),
|
||||
stopAll: vi.fn(),
|
||||
streamEvents: Stream.empty,
|
||||
};
|
||||
const layer = it.layer(
|
||||
Layer.mergeAll(
|
||||
Layer.provide(
|
||||
ProviderAdapterRegistryLive,
|
||||
Layer.succeed(CodexAdapter, fakeCodexAdapter),
|
||||
Layer.mergeAll(
|
||||
Layer.succeed(CodexAdapter, fakeCodexAdapter),
|
||||
Layer.succeed(CursorAdapter, fakeCursorAdapter),
|
||||
),
|
||||
),
|
||||
NodeServices.layer,
|
||||
),
|
||||
@@ -42,10 +62,12 @@ layer("ProviderAdapterRegistryLive", (it) => {
|
||||
Effect.gen(function* () {
|
||||
const registry = yield* ProviderAdapterRegistry;
|
||||
const codex = yield* registry.getByProvider("codex");
|
||||
const cursor = yield* registry.getByProvider("cursor");
|
||||
assert.equal(codex, fakeCodexAdapter);
|
||||
assert.equal(cursor, fakeCursorAdapter);
|
||||
|
||||
const providers = yield* registry.listProviders();
|
||||
assert.deepEqual(providers, ["codex"]);
|
||||
assert.deepEqual(providers, ["codex", "cursor"]);
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
type ProviderAdapterRegistryShape,
|
||||
} from "../Services/ProviderAdapterRegistry.ts";
|
||||
import { CodexAdapter } from "../Services/CodexAdapter.ts";
|
||||
import { CursorAdapter } from "../Services/CursorAdapter.ts";
|
||||
|
||||
export interface ProviderAdapterRegistryLiveOptions {
|
||||
readonly adapters?: ReadonlyArray<ProviderAdapterShape<ProviderAdapterError>>;
|
||||
@@ -26,7 +27,7 @@ const makeProviderAdapterRegistry = (options?: ProviderAdapterRegistryLiveOption
|
||||
const adapters =
|
||||
options?.adapters !== undefined
|
||||
? options.adapters
|
||||
: [yield* CodexAdapter];
|
||||
: [yield* CodexAdapter, yield* CursorAdapter];
|
||||
const byProvider = new Map(adapters.map((adapter) => [adapter.provider, adapter]));
|
||||
|
||||
const getByProvider: ProviderAdapterRegistryShape["getByProvider"] = (provider) => {
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
import { Schema } from "effect";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
CursorAcpPermissionRequest,
|
||||
CursorAcpSessionPromptResult,
|
||||
CursorAcpSessionUpdateNotification,
|
||||
} from "./CursorAdapter.ts";
|
||||
|
||||
describe("Cursor ACP schemas", () => {
|
||||
it("decodes session/update thought and message chunks", () => {
|
||||
const thought = Schema.decodeUnknownSync(CursorAcpSessionUpdateNotification)({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "sess-1",
|
||||
update: {
|
||||
sessionUpdate: "agent_thought_chunk",
|
||||
content: {
|
||||
type: "text",
|
||||
text: "thinking",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(thought.params.update.sessionUpdate).toBe("agent_thought_chunk");
|
||||
|
||||
const message = Schema.decodeUnknownSync(CursorAcpSessionUpdateNotification)({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "sess-1",
|
||||
update: {
|
||||
sessionUpdate: "agent_message_chunk",
|
||||
content: {
|
||||
type: "text",
|
||||
text: "hello",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(message.params.update.sessionUpdate).toBe("agent_message_chunk");
|
||||
});
|
||||
|
||||
it("decodes tool call lifecycle updates", () => {
|
||||
const started = Schema.decodeUnknownSync(CursorAcpSessionUpdateNotification)({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "sess-1",
|
||||
update: {
|
||||
sessionUpdate: "tool_call",
|
||||
toolCallId: "tool-1",
|
||||
title: "Terminal",
|
||||
kind: "execute",
|
||||
status: "pending",
|
||||
rawInput: { command: "pwd" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(started.params.update.sessionUpdate).toBe("tool_call");
|
||||
|
||||
const completed = Schema.decodeUnknownSync(CursorAcpSessionUpdateNotification)({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "sess-1",
|
||||
update: {
|
||||
sessionUpdate: "tool_call_update",
|
||||
toolCallId: "tool-1",
|
||||
status: "completed",
|
||||
rawOutput: {
|
||||
exitCode: 0,
|
||||
stdout: "ok",
|
||||
stderr: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(completed.params.update.sessionUpdate).toBe("tool_call_update");
|
||||
});
|
||||
|
||||
it("decodes permission requests", () => {
|
||||
const decoded = Schema.decodeUnknownSync(CursorAcpPermissionRequest)({
|
||||
jsonrpc: "2.0",
|
||||
id: 9,
|
||||
method: "session/request_permission",
|
||||
params: {
|
||||
sessionId: "sess-1",
|
||||
toolCall: {
|
||||
toolCallId: "tool-1",
|
||||
kind: "execute",
|
||||
},
|
||||
options: [
|
||||
{ optionId: "allow-once", name: "Allow once", kind: "allow_once" },
|
||||
{ optionId: "reject-once", name: "Reject", kind: "reject_once" },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
expect(decoded.method).toBe("session/request_permission");
|
||||
expect(decoded.params.options).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("decodes prompt completion result payload", () => {
|
||||
const decoded = Schema.decodeUnknownSync(CursorAcpSessionPromptResult)({
|
||||
stopReason: "end_turn",
|
||||
});
|
||||
|
||||
expect(decoded.stopReason).toBe("end_turn");
|
||||
});
|
||||
|
||||
it("rejects unsupported update types", () => {
|
||||
expect(() =>
|
||||
Schema.decodeUnknownSync(CursorAcpSessionUpdateNotification)({
|
||||
jsonrpc: "2.0",
|
||||
method: "session/update",
|
||||
params: {
|
||||
sessionId: "sess-1",
|
||||
update: {
|
||||
sessionUpdate: "unknown_update",
|
||||
},
|
||||
},
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* CursorAdapter - Cursor ACP implementation of the generic provider adapter contract.
|
||||
*
|
||||
* Defines ACP JSON-RPC schemas used by the Cursor adapter layer.
|
||||
*
|
||||
* @module CursorAdapter
|
||||
*/
|
||||
import { Schema, ServiceMap } from "effect";
|
||||
|
||||
import type { ProviderAdapterError } from "../Errors.ts";
|
||||
import type { ProviderAdapterShape } from "./ProviderAdapter.ts";
|
||||
|
||||
export const CursorAcpJsonRpcId = Schema.Union([Schema.String, Schema.Int]);
|
||||
export type CursorAcpJsonRpcId = typeof CursorAcpJsonRpcId.Type;
|
||||
|
||||
export const CursorAcpTextContent = Schema.Struct({
|
||||
type: Schema.Literal("text"),
|
||||
text: Schema.String,
|
||||
});
|
||||
export type CursorAcpTextContent = typeof CursorAcpTextContent.Type;
|
||||
|
||||
export const CursorAcpSessionUpdate = Schema.Union([
|
||||
Schema.Struct({
|
||||
sessionUpdate: Schema.Literal("available_commands_update"),
|
||||
availableCommands: Schema.Array(
|
||||
Schema.Struct({
|
||||
name: Schema.String,
|
||||
description: Schema.optional(Schema.String),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
Schema.Struct({
|
||||
sessionUpdate: Schema.Literal("agent_thought_chunk"),
|
||||
content: CursorAcpTextContent,
|
||||
}),
|
||||
Schema.Struct({
|
||||
sessionUpdate: Schema.Literal("agent_message_chunk"),
|
||||
content: CursorAcpTextContent,
|
||||
}),
|
||||
Schema.Struct({
|
||||
sessionUpdate: Schema.Literal("tool_call"),
|
||||
toolCallId: Schema.String,
|
||||
title: Schema.optional(Schema.String),
|
||||
kind: Schema.optional(Schema.String),
|
||||
status: Schema.optional(Schema.String),
|
||||
rawInput: Schema.optional(Schema.Unknown),
|
||||
}),
|
||||
Schema.Struct({
|
||||
sessionUpdate: Schema.Literal("tool_call_update"),
|
||||
toolCallId: Schema.String,
|
||||
status: Schema.String,
|
||||
rawOutput: Schema.optional(Schema.Unknown),
|
||||
}),
|
||||
]);
|
||||
export type CursorAcpSessionUpdate = typeof CursorAcpSessionUpdate.Type;
|
||||
|
||||
export const CursorAcpSessionUpdateNotification = Schema.Struct({
|
||||
jsonrpc: Schema.optional(Schema.Literal("2.0")),
|
||||
method: Schema.Literal("session/update"),
|
||||
params: Schema.Struct({
|
||||
sessionId: Schema.String,
|
||||
update: CursorAcpSessionUpdate,
|
||||
}),
|
||||
});
|
||||
export type CursorAcpSessionUpdateNotification = typeof CursorAcpSessionUpdateNotification.Type;
|
||||
|
||||
export const CursorAcpPermissionOption = Schema.Struct({
|
||||
optionId: Schema.String,
|
||||
name: Schema.optional(Schema.String),
|
||||
kind: Schema.optional(Schema.String),
|
||||
});
|
||||
export type CursorAcpPermissionOption = typeof CursorAcpPermissionOption.Type;
|
||||
|
||||
export const CursorAcpPermissionRequest = Schema.Struct({
|
||||
jsonrpc: Schema.optional(Schema.Literal("2.0")),
|
||||
id: CursorAcpJsonRpcId,
|
||||
method: Schema.Literal("session/request_permission"),
|
||||
params: Schema.Struct({
|
||||
sessionId: Schema.String,
|
||||
toolCall: Schema.optional(Schema.Unknown),
|
||||
options: Schema.Array(CursorAcpPermissionOption),
|
||||
}),
|
||||
});
|
||||
export type CursorAcpPermissionRequest = typeof CursorAcpPermissionRequest.Type;
|
||||
|
||||
export const CursorAcpInitializeResult = Schema.Struct({
|
||||
protocolVersion: Schema.optional(Schema.Int),
|
||||
agentCapabilities: Schema.optional(Schema.Unknown),
|
||||
authMethods: Schema.optional(Schema.Array(Schema.Unknown)),
|
||||
});
|
||||
export type CursorAcpInitializeResult = typeof CursorAcpInitializeResult.Type;
|
||||
|
||||
export const CursorAcpSessionNewResult = Schema.Struct({
|
||||
sessionId: Schema.String,
|
||||
modes: Schema.optional(Schema.Unknown),
|
||||
});
|
||||
export type CursorAcpSessionNewResult = typeof CursorAcpSessionNewResult.Type;
|
||||
|
||||
export const CursorAcpSessionPromptResult = Schema.Struct({
|
||||
stopReason: Schema.optional(Schema.String),
|
||||
});
|
||||
export type CursorAcpSessionPromptResult = typeof CursorAcpSessionPromptResult.Type;
|
||||
|
||||
export interface CursorAdapterShape extends ProviderAdapterShape<ProviderAdapterError> {
|
||||
readonly provider: "cursor";
|
||||
}
|
||||
|
||||
export class CursorAdapter extends ServiceMap.Service<CursorAdapter, CursorAdapterShape>()(
|
||||
"t3/provider/Services/CursorAdapter",
|
||||
) {}
|
||||
@@ -19,6 +19,7 @@ import { OrchestrationProjectionSnapshotQueryLive } from "./orchestration/Layers
|
||||
import { ProviderRuntimeIngestionLive } from "./orchestration/Layers/ProviderRuntimeIngestion";
|
||||
import { ProviderUnsupportedError } from "./provider/Errors";
|
||||
import { makeCodexAdapterLive } from "./provider/Layers/CodexAdapter";
|
||||
import { makeCursorAdapterLive } from "./provider/Layers/CursorAdapter";
|
||||
import { ProviderAdapterRegistryLive } from "./provider/Layers/ProviderAdapterRegistry";
|
||||
import { makeProviderServiceLive } from "./provider/Layers/ProviderService";
|
||||
import { ProviderSessionDirectoryLive } from "./provider/Layers/ProviderSessionDirectory";
|
||||
@@ -56,8 +57,12 @@ export function makeServerProviderLayer(): Layer.Layer<
|
||||
const codexAdapterLayer = makeCodexAdapterLive(
|
||||
nativeEventLogger ? { nativeEventLogger } : undefined,
|
||||
);
|
||||
const cursorAdapterLayer = makeCursorAdapterLive(
|
||||
nativeEventLogger ? { nativeEventLogger } : undefined,
|
||||
);
|
||||
const adapterRegistryLayer = ProviderAdapterRegistryLive.pipe(
|
||||
Layer.provide(codexAdapterLayer),
|
||||
Layer.provide(cursorAdapterLayer),
|
||||
Layer.provideMerge(providerSessionDirectoryLayer),
|
||||
);
|
||||
return makeProviderServiceLive(
|
||||
|
||||
@@ -47,6 +47,13 @@ const MODEL_PROVIDER_SETTINGS: Array<{
|
||||
placeholder: "your-codex-model-slug",
|
||||
example: "gpt-6.7-codex-ultra-preview",
|
||||
},
|
||||
{
|
||||
provider: "cursor",
|
||||
title: "Cursor",
|
||||
description: "Save additional Cursor model slugs for the picker and `/model` command.",
|
||||
placeholder: "your-cursor-model-slug",
|
||||
example: "openai/gpt-oss-120b",
|
||||
},
|
||||
] as const;
|
||||
|
||||
function getCustomModelsForProvider(
|
||||
|
||||
@@ -514,14 +514,9 @@ describe("isLatestTurnSettled", () => {
|
||||
});
|
||||
|
||||
describe("PROVIDER_OPTIONS", () => {
|
||||
it("keeps Claude Code and Cursor visible as unavailable placeholders in the stack base", () => {
|
||||
it("advertises codex and cursor on the cursor branch while keeping Claude Code as coming soon", () => {
|
||||
const claude = PROVIDER_OPTIONS.find((option) => option.value === "claudeCode");
|
||||
const cursor = PROVIDER_OPTIONS.find((option) => option.value === "cursor");
|
||||
expect(PROVIDER_OPTIONS).toEqual([
|
||||
{ value: "codex", label: "Codex", available: true },
|
||||
{ value: "claudeCode", label: "Claude Code", available: false },
|
||||
{ value: "cursor", label: "Cursor", available: false },
|
||||
]);
|
||||
expect(claude).toEqual({
|
||||
value: "claudeCode",
|
||||
label: "Claude Code",
|
||||
@@ -529,8 +524,8 @@ describe("PROVIDER_OPTIONS", () => {
|
||||
});
|
||||
expect(cursor).toEqual({
|
||||
value: "cursor",
|
||||
label: "Cursor",
|
||||
available: false,
|
||||
label: "Cursor Agent",
|
||||
available: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -19,7 +19,7 @@ export const PROVIDER_OPTIONS: Array<{
|
||||
}> = [
|
||||
{ value: "codex", label: "Codex", available: true },
|
||||
{ value: "claudeCode", label: "Claude Code", available: false },
|
||||
{ value: "cursor", label: "Cursor", available: false },
|
||||
{ value: "cursor", label: "Cursor Agent", available: true },
|
||||
];
|
||||
|
||||
export interface WorkLogEntry {
|
||||
|
||||
Reference in new issue
Block a user