From 0c86d661c51b7572c3329ff3f9dc48f0ea314035 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 1 Mar 2026 11:44:19 +0000 Subject: [PATCH] feat(cursor): implement Cursor ACP provider adapter with V2 event emission - Replace old CLI stream-json schemas with ACP JSON-RPC 2.0 schemas - Implement full CursorAdapterLive layer with ACP process lifecycle - JSON-RPC multiplexer using Deferred for request/response correlation - Session lifecycle: initialize, authenticate, session/new, session/load - Turn control via session/prompt with streaming session/update consumption - Permission bridging via session/request_permission server requests - Emit V2 canonical events: content.delta, item.started/completed, request.opened/resolved - Interrupt support via process kill (session/cancel unsupported in ACP) - Resume support via session/load with persisted acpSessionId Co-authored-by: Julius Marminge --- .../src/provider/Layers/CursorAdapter.ts | 1056 +++++++++++++++++ .../src/provider/Services/CursorAdapter.ts | 242 ++-- 2 files changed, 1127 insertions(+), 171 deletions(-) create mode 100644 apps/server/src/provider/Layers/CursorAdapter.ts diff --git a/apps/server/src/provider/Layers/CursorAdapter.ts b/apps/server/src/provider/Layers/CursorAdapter.ts new file mode 100644 index 0000000000..a07fde1260 --- /dev/null +++ b/apps/server/src/provider/Layers/CursorAdapter.ts @@ -0,0 +1,1056 @@ +/** + * CursorAdapterLive - Scoped live implementation for the Cursor ACP provider adapter. + * + * Spawns `agent acp` child processes (one per session), implements JSON-RPC 2.0 + * multiplexing over stdio, and projects ACP notifications/requests into V2 + * canonical runtime events. + * + * @module CursorAdapterLive + */ +import { spawn, type ChildProcess } from "node:child_process"; +import readline from "node:readline"; + +import { + ApprovalRequestId, + type CanonicalItemType, + type CanonicalRequestType, + EventId, + type ProviderApprovalDecision, + type ProviderRuntimeEvent, + ProviderSessionId, + type ProviderSession, + ProviderThreadId, + ProviderTurnId, + RuntimeItemId, + RuntimeRequestId, + RuntimeSessionId, + ThreadId, + TurnId, +} from "@t3tools/contracts"; +import { Cause, DateTime, Deferred, Effect, Layer, Queue, Random, Ref, Stream } from "effect"; + +import { + ProviderAdapterProcessError, + ProviderAdapterRequestError, + ProviderAdapterSessionClosedError, + ProviderAdapterSessionNotFoundError, + ProviderAdapterValidationError, + type ProviderAdapterError, +} from "../Errors.ts"; +import { + CursorAdapter, + type CursorAdapterShape, + type AcpJsonRpcMessage, + type AcpSessionUpdate, +} from "../Services/CursorAdapter.ts"; +import { makeEventNdjsonLogger } from "./EventNdjsonLogger.ts"; + +const PROVIDER = "cursor" as const; +const ACP_BINARY = "agent"; +const ACP_ARGS = ["acp"]; +const DEFAULT_REQUEST_TIMEOUT_MS = 120_000; + +export interface CursorAdapterLiveOptions { + readonly binaryPath?: string; + readonly nativeEventLogPath?: string; +} + +function toMessage(cause: unknown, fallback: string): string { + if (cause instanceof Error && cause.message.length > 0) { + return cause.message; + } + if (typeof cause === "string") return cause; + if (cause && typeof cause === "object" && "message" in cause) { + return String((cause as { message: unknown }).message); + } + return fallback; +} + +function classifyToolItemType(kind: string | undefined, title: string | undefined): CanonicalItemType { + if (kind === "execute") return "command_execution"; + const normalized = (title ?? "").toLowerCase(); + if (normalized.includes("terminal") || normalized.includes("shell")) return "command_execution"; + if (normalized.includes("edit") || normalized.includes("write") || normalized.includes("file")) return "file_change"; + if (normalized.includes("search") || normalized.includes("grep") || normalized.includes("glob")) return "web_search"; + return "dynamic_tool_call"; +} + +function titleForItemType(itemType: CanonicalItemType): string { + switch (itemType) { + case "command_execution": return "Command run"; + case "file_change": return "File change"; + case "web_search": return "Search"; + case "mcp_tool_call": return "MCP tool call"; + case "dynamic_tool_call": return "Tool call"; + default: return "Item"; + } +} + +function classifyPermissionRequestType(kind: string | undefined): CanonicalRequestType { + if (kind === "execute") return "command_execution_approval"; + return "unknown"; +} + +interface PendingRpcRequest { + readonly deferred: Deferred.Deferred; + readonly timer: ReturnType; +} + +interface PendingPermission { + readonly jsonRpcId: number; + readonly requestId: ApprovalRequestId; + readonly requestType: CanonicalRequestType; + readonly toolCallId: string; +} + +interface CursorTurnState { + readonly turnId: ProviderTurnId; + readonly assistantItemId: string; + readonly startedAt: string; + readonly seenToolCallIds: Set; + emittedAssistantDelta: boolean; +} + +interface CursorSessionContext { + session: ProviderSession; + acpSessionId: string; + child: ChildProcess; + nextRpcId: number; + readonly pendingRpc: Map; + readonly pendingPermissions: Map; + turnState: CursorTurnState | undefined; + stopped: boolean; +} + +function safeParse(line: string): AcpJsonRpcMessage | undefined { + try { + return JSON.parse(line) as AcpJsonRpcMessage; + } catch { + return undefined; + } +} + +function makeCursorAdapter(options?: CursorAdapterLiveOptions) { + return Effect.gen(function* () { + const nativeEventLogger = + options?.nativeEventLogPath !== undefined + ? makeEventNdjsonLogger(options.nativeEventLogPath) + : undefined; + + const sessions = new Map(); + const runtimeEventQueue = yield* Queue.unbounded(); + + const nowIso = Effect.map(DateTime.now, DateTime.formatIso); + const nextEventId = Effect.map(Random.nextUUIDv4, (id) => EventId.makeUnsafe(id)); + const makeEventStamp = () => Effect.all({ eventId: nextEventId, createdAt: nowIso }); + + const offerEvent = (event: ProviderRuntimeEvent): Effect.Effect => + Queue.offer(runtimeEventQueue, event).pipe(Effect.asVoid); + + const makeV2Base = ( + context: CursorSessionContext, + stamp: { eventId: EventId; createdAt: string }, + ) => ({ + eventId: stamp.eventId, + provider: PROVIDER, + sessionId: RuntimeSessionId.makeUnsafe(context.session.sessionId), + createdAt: stamp.createdAt, + ...(context.session.threadId + ? { threadId: ThreadId.makeUnsafe(context.session.threadId) } + : {}), + providerRefs: { + providerSessionId: context.session.sessionId, + ...(context.session.threadId + ? { providerThreadId: context.session.threadId } + : {}), + ...(context.turnState + ? { providerTurnId: context.turnState.turnId } + : {}), + }, + }); + + const rpcRequest = ( + context: CursorSessionContext, + method: string, + params: unknown, + timeoutMs = DEFAULT_REQUEST_TIMEOUT_MS, + ): Effect.Effect => + Effect.gen(function* () { + if (context.stopped) { + return yield* new ProviderAdapterSessionClosedError({ + provider: PROVIDER, + sessionId: context.session.sessionId, + }); + } + + const id = context.nextRpcId++; + const message = JSON.stringify({ + jsonrpc: "2.0", + id, + method, + params, + }); + + nativeEventLogger?.write({ + observedAt: new Date().toISOString(), + event: { + direction: "client->server", + provider: PROVIDER, + sessionId: context.session.sessionId, + message: { jsonrpc: "2.0", id, method, params }, + }, + }); + + const deferred = yield* Deferred.make(); + + const timer = setTimeout(() => { + context.pendingRpc.delete(id); + Effect.runFork( + Deferred.fail( + deferred, + new ProviderAdapterRequestError({ + provider: PROVIDER, + method, + detail: `Timed out waiting for response (id=${id}).`, + }), + ), + ); + }, timeoutMs); + + context.pendingRpc.set(id, { deferred, timer }); + + yield* Effect.try({ + try: () => { + context.child.stdin!.write(`${message}\n`); + }, + catch: (err) => + new ProviderAdapterProcessError({ + provider: PROVIDER, + sessionId: context.session.sessionId, + detail: toMessage(err, "Failed to write to ACP stdin."), + cause: err instanceof Error ? err : undefined, + }), + }); + + return yield* Deferred.await(deferred); + }); + + const rpcRespond = (context: CursorSessionContext, id: number, result: unknown): void => { + if (context.stopped) return; + const message = JSON.stringify({ jsonrpc: "2.0", id, result }); + nativeEventLogger?.write({ + observedAt: new Date().toISOString(), + event: { + direction: "client->server", + provider: PROVIDER, + sessionId: context.session.sessionId, + message: { jsonrpc: "2.0", id, result }, + }, + }); + try { + context.child.stdin!.write(`${message}\n`); + } catch { + // Best-effort response delivery. + } + }; + + const resolveRpcRequest = (context: CursorSessionContext, id: number, result: unknown): void => { + const pending = context.pendingRpc.get(id); + if (!pending) return; + clearTimeout(pending.timer); + context.pendingRpc.delete(id); + Effect.runFork(Deferred.succeed(pending.deferred, result)); + }; + + const rejectRpcRequest = (context: CursorSessionContext, id: number, error: unknown): void => { + const pending = context.pendingRpc.get(id); + if (!pending) return; + clearTimeout(pending.timer); + context.pendingRpc.delete(id); + Effect.runFork( + Deferred.fail( + pending.deferred, + new ProviderAdapterRequestError({ + provider: PROVIDER, + method: "rpc", + detail: toMessage(error, "RPC request failed"), + cause: error instanceof Error ? error : undefined, + }), + ), + ); + }; + + const handleSessionUpdate = ( + context: CursorSessionContext, + update: AcpSessionUpdate, + ): Effect.Effect => + Effect.gen(function* () { + const ts = context.turnState; + + switch (update.sessionUpdate) { + case "agent_thought_chunk": { + if (!update.content?.text || update.content.text.length === 0) return; + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + ...(ts ? { turnId: TurnId.makeUnsafe(ts.turnId) } : {}), + type: "content.delta" as const, + payload: { + streamKind: "reasoning_text" as const, + delta: update.content.text, + }, + }); + return; + } + + case "agent_message_chunk": { + if (!update.content?.text || update.content.text.length === 0) return; + if (ts) ts.emittedAssistantDelta = true; + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + ...(ts + ? { + turnId: TurnId.makeUnsafe(ts.turnId), + itemId: RuntimeItemId.makeUnsafe(ts.assistantItemId), + } + : {}), + type: "content.delta" as const, + payload: { + streamKind: "assistant_text" as const, + delta: update.content.text, + }, + }); + return; + } + + case "tool_call": { + if (!update.toolCallId || !ts) return; + if (ts.seenToolCallIds.has(update.toolCallId)) return; + ts.seenToolCallIds.add(update.toolCallId); + + const itemType = classifyToolItemType(update.kind, update.title); + const detail = update.rawInput && typeof update.rawInput === "object" + ? (() => { + const cmd = (update.rawInput as Record).command; + return typeof cmd === "string" ? cmd : update.title; + })() + : update.title; + + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + turnId: TurnId.makeUnsafe(ts.turnId), + itemId: RuntimeItemId.makeUnsafe(update.toolCallId), + type: "item.started" as const, + payload: { + itemType, + status: "inProgress" as const, + title: titleForItemType(itemType), + ...(detail ? { detail: String(detail).slice(0, 400) } : {}), + }, + }); + return; + } + + case "tool_call_update": { + if (!update.toolCallId || !ts) return; + + if (update.status === "completed") { + const itemType = classifyToolItemType(undefined, undefined); + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + turnId: TurnId.makeUnsafe(ts.turnId), + itemId: RuntimeItemId.makeUnsafe(update.toolCallId), + type: "item.completed" as const, + payload: { + itemType, + status: "completed" as const, + title: titleForItemType(itemType), + ...(update.rawOutput ? { data: update.rawOutput } : {}), + }, + }); + } else if (update.status === "in_progress") { + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + turnId: TurnId.makeUnsafe(ts.turnId), + itemId: RuntimeItemId.makeUnsafe(update.toolCallId), + type: "item.updated" as const, + payload: { + itemType: "dynamic_tool_call" as const, + status: "inProgress" as const, + }, + }); + } + return; + } + + case "available_commands_update": + return; + + default: + return; + } + }); + + const handleServerRequest = ( + context: CursorSessionContext, + msg: AcpJsonRpcMessage, + ): Effect.Effect => + Effect.gen(function* () { + if (msg.method !== "session/request_permission" || msg.id === undefined) return; + + const params = msg.params as { + sessionId?: string; + toolCall?: { toolCallId?: string; title?: string; kind?: string }; + options?: ReadonlyArray<{ optionId: string; name: string }>; + } | undefined; + + const toolCallId = params?.toolCall?.toolCallId ?? `unknown-${msg.id}`; + const requestType = classifyPermissionRequestType(params?.toolCall?.kind); + const requestId = ApprovalRequestId.makeUnsafe(yield* Random.nextUUIDv4); + + const pending: PendingPermission = { + jsonRpcId: msg.id, + requestId, + requestType, + toolCallId, + }; + context.pendingPermissions.set(requestId, pending); + + const title = params?.toolCall?.title ?? "Permission requested"; + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + ...(context.turnState + ? { turnId: TurnId.makeUnsafe(context.turnState.turnId) } + : {}), + requestId: RuntimeRequestId.makeUnsafe(requestId), + type: "request.opened" as const, + payload: { + requestType, + detail: title, + args: params, + }, + }); + }); + + const handleStdoutLine = ( + context: CursorSessionContext, + line: string, + ): Effect.Effect => + Effect.gen(function* () { + const msg = safeParse(line); + if (!msg) return; + + nativeEventLogger?.write({ + observedAt: new Date().toISOString(), + event: { + direction: "server->client", + provider: PROVIDER, + sessionId: context.session.sessionId, + message: msg, + }, + }); + + const hasId = msg.id !== undefined; + const hasMethod = typeof msg.method === "string"; + const hasResult = "result" in msg; + const hasError = "error" in msg; + + if (hasId && (hasResult || hasError) && !hasMethod) { + if (hasError) { + rejectRpcRequest(context, msg.id!, msg.error); + } else { + resolveRpcRequest(context, msg.id!, msg.result); + } + return; + } + + if (hasId && hasMethod) { + yield* handleServerRequest(context, msg); + return; + } + + if (hasMethod && !hasId) { + if (msg.method === "session/update") { + const params = msg.params as { update?: AcpSessionUpdate } | undefined; + if (params?.update) { + yield* handleSessionUpdate(context, params.update); + } + } + return; + } + }); + + const startStdoutReader = (context: CursorSessionContext): void => { + const rl = readline.createInterface({ input: context.child.stdout! }); + rl.on("line", (line) => { + Effect.runFork( + handleStdoutLine(context, line).pipe( + Effect.catchCause((cause) => { + if (Cause.hasInterruptsOnly(cause) || context.stopped) return Effect.void; + return Effect.logWarning("ACP stdout line handling failed", { + cause: Cause.pretty(cause), + }); + }), + ), + ); + }); + rl.on("close", () => { + // stdout closed - process likely exiting + }); + }; + + const spawnAcpProcess = (binaryPath?: string): ChildProcess => { + const bin = binaryPath ?? options?.binaryPath ?? ACP_BINARY; + return spawn(bin, ACP_ARGS, { + stdio: ["pipe", "pipe", "pipe"], + env: { ...process.env, NO_COLOR: "1" }, + }); + }; + + const initializeAcp = ( + context: CursorSessionContext, + ): Effect.Effect => + rpcRequest(context, "initialize", { + protocolVersion: 1, + clientCapabilities: { + fs: { readTextFile: false, writeTextFile: false }, + terminal: false, + }, + clientInfo: { name: "t3-cursor-adapter", version: "1.0.0" }, + }); + + const authenticateAcp = ( + context: CursorSessionContext, + methodId: string, + ): Effect.Effect => + rpcRequest(context, "authenticate", { methodId }).pipe( + Effect.catch((error) => + Effect.gen(function* () { + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + type: "runtime.warning" as const, + payload: { + message: `Authentication warning: ${toMessage(error, "authenticate failed")}`, + }, + }); + return {}; + }), + ), + ); + + const createAcpSession = ( + context: CursorSessionContext, + cwd: string, + model?: string, + ): Effect.Effect => + Effect.gen(function* () { + const result = (yield* rpcRequest(context, "session/new", { + cwd, + mcpServers: [], + ...(model ? { model } : {}), + })) as { sessionId?: string; modes?: unknown }; + + if (!result.sessionId || typeof result.sessionId !== "string") { + return yield* new ProviderAdapterProcessError({ + provider: PROVIDER, + sessionId: context.session.sessionId, + detail: "session/new did not return a sessionId.", + }); + } + + return result.sessionId; + }); + + const loadAcpSession = ( + context: CursorSessionContext, + acpSessionId: string, + cwd: string, + ): Effect.Effect => + rpcRequest(context, "session/load", { + sessionId: acpSessionId, + cwd, + mcpServers: [], + }).pipe(Effect.asVoid); + + const stopSessionInternal = ( + context: CursorSessionContext, + opts?: { readonly emitExitEvent?: boolean }, + ): Effect.Effect => + Effect.gen(function* () { + if (context.stopped) return; + context.stopped = true; + + for (const [key, pending] of context.pendingPermissions) { + rpcRespond(context, pending.jsonRpcId, { + outcome: { outcome: "selected", optionId: "reject-once" }, + }); + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + requestId: RuntimeRequestId.makeUnsafe(pending.requestId), + type: "request.resolved" as const, + payload: { + requestType: pending.requestType, + decision: "cancel", + }, + }); + context.pendingPermissions.delete(key); + } + + if (context.turnState) { + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + turnId: TurnId.makeUnsafe(context.turnState.turnId), + type: "turn.completed" as const, + payload: { + state: "interrupted" as const, + errorMessage: "Session stopped.", + }, + }); + context.turnState = undefined; + } + + for (const [id, pending] of context.pendingRpc) { + clearTimeout(pending.timer); + Effect.runFork( + Deferred.fail( + pending.deferred, + new ProviderAdapterSessionClosedError({ + provider: PROVIDER, + sessionId: context.session.sessionId, + }), + ), + ); + context.pendingRpc.delete(id); + } + + yield* Effect.sync(() => { + try { + context.child.stdin!.end(); + } catch { /* best-effort */ } + context.child.kill("SIGTERM"); + }); + + const updatedAt = yield* nowIso; + context.session = { + ...context.session, + status: "closed", + activeTurnId: undefined, + updatedAt, + }; + + if (opts?.emitExitEvent !== false) { + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + type: "session.exited" as const, + payload: { + reason: "Session stopped", + exitKind: "graceful" as const, + }, + }); + } + + sessions.delete(context.session.sessionId); + }); + + const requireSession = ( + sessionId: ProviderSessionId, + ): Effect.Effect => { + const context = sessions.get(sessionId); + if (!context) { + return Effect.fail( + new ProviderAdapterSessionNotFoundError({ provider: PROVIDER, sessionId }), + ); + } + if (context.stopped || context.session.status === "closed") { + return Effect.fail( + new ProviderAdapterSessionClosedError({ provider: PROVIDER, sessionId }), + ); + } + return Effect.succeed(context); + }; + + const startSession: CursorAdapterShape["startSession"] = (input) => + Effect.gen(function* () { + if (input.provider !== undefined && input.provider !== PROVIDER) { + return yield* new ProviderAdapterValidationError({ + provider: PROVIDER, + operation: "startSession", + issue: `Expected provider '${PROVIDER}' but received '${input.provider}'.`, + }); + } + + const startedAt = yield* nowIso; + const sessionId = ProviderSessionId.makeUnsafe( + `cursor-session-${yield* Random.nextUUIDv4}`, + ); + const cursorOptions = input.providerOptions?.cursor; + const cwd = input.cwd ?? process.cwd(); + + const child = spawnAcpProcess(cursorOptions?.binaryPath); + + const session: ProviderSession = { + sessionId, + provider: PROVIDER, + status: "connecting", + cwd, + ...(input.model ? { model: input.model } : {}), + createdAt: startedAt, + updatedAt: startedAt, + }; + + const context: CursorSessionContext = { + session, + acpSessionId: "", + child, + nextRpcId: 1, + pendingRpc: new Map(), + pendingPermissions: new Map(), + turnState: undefined, + stopped: false, + }; + sessions.set(sessionId, context); + + child.once("exit", (code, signal) => { + if (context.stopped) return; + Effect.runFork( + Effect.gen(function* () { + const msg = `ACP process exited (code=${String(code)}, signal=${String(signal)})`; + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + type: "runtime.error" as const, + payload: { message: msg, class: "transport_error" as const }, + }); + yield* stopSessionInternal(context, { emitExitEvent: true }); + }), + ); + }); + + startStdoutReader(context); + + yield* initializeAcp(context); + const authMethodId = cursorOptions?.authMethodId ?? "cursor_login"; + yield* authenticateAcp(context, authMethodId); + + const existingCursor = input.resumeCursor as + | { acpSessionId?: string } + | undefined; + let acpSessionId: string; + + if (existingCursor?.acpSessionId) { + yield* loadAcpSession(context, existingCursor.acpSessionId, cwd); + acpSessionId = existingCursor.acpSessionId; + } else { + acpSessionId = yield* createAcpSession(context, cwd, input.model); + } + + context.acpSessionId = acpSessionId; + const threadId = ProviderThreadId.makeUnsafe(acpSessionId); + + const updatedAt = yield* nowIso; + context.session = { + ...context.session, + status: "ready", + threadId, + resumeCursor: { acpSessionId }, + updatedAt, + }; + + const sessionStartedStamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, sessionStartedStamp), + type: "session.started" as const, + payload: {}, + }); + + const threadStartedStamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, threadStartedStamp), + threadId: ThreadId.makeUnsafe(threadId), + type: "thread.started" as const, + payload: { providerThreadId: threadId }, + }); + + return { ...context.session }; + }); + + const sendTurn: CursorAdapterShape["sendTurn"] = (input) => + Effect.gen(function* () { + const context = yield* requireSession(input.sessionId); + + if (context.turnState) { + return yield* new ProviderAdapterValidationError({ + provider: PROVIDER, + operation: "sendTurn", + issue: `Session '${input.sessionId}' already has an active turn.`, + }); + } + + const turnId = ProviderTurnId.makeUnsafe(`cursor-turn-${yield* Random.nextUUIDv4}`); + + const turnState: CursorTurnState = { + turnId, + assistantItemId: `cursor-message-${yield* Random.nextUUIDv4}`, + startedAt: yield* nowIso, + seenToolCallIds: new Set(), + emittedAssistantDelta: false, + }; + + context.turnState = turnState; + const updatedAt = yield* nowIso; + context.session = { + ...context.session, + status: "running", + activeTurnId: turnId, + updatedAt, + }; + + const turnStartedStamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, turnStartedStamp), + turnId: TurnId.makeUnsafe(turnId), + type: "turn.started" as const, + payload: { + ...(input.model ? { model: input.model } : {}), + }, + }); + + const promptParts: Array<{ type: string; text: string }> = []; + if (input.input) { + promptParts.push({ type: "text", text: input.input }); + } + + Effect.runFork( + Effect.gen(function* () { + const result = yield* rpcRequest(context, "session/prompt", { + sessionId: context.acpSessionId, + prompt: promptParts, + }).pipe( + Effect.catch((error) => + Effect.gen(function* () { + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + turnId: TurnId.makeUnsafe(turnId), + type: "runtime.error" as const, + payload: { + message: toMessage(error, "Prompt failed"), + class: "provider_error" as const, + }, + }); + return { stopReason: "error" } as { stopReason?: string }; + }), + ), + ); + + const promptResult = result as { stopReason?: string }; + + if (context.turnState?.emittedAssistantDelta) { + const msgStamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, msgStamp), + turnId: TurnId.makeUnsafe(turnId), + itemId: RuntimeItemId.makeUnsafe(turnState.assistantItemId), + type: "item.completed" as const, + payload: { + itemType: "assistant_message" as const, + status: "completed" as const, + }, + }); + } + + const state: "completed" | "failed" | "cancelled" = + promptResult.stopReason === "cancelled" + ? "cancelled" + : promptResult.stopReason === "error" + ? "failed" + : "completed"; + + const completedStamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, completedStamp), + turnId: TurnId.makeUnsafe(turnId), + type: "turn.completed" as const, + payload: { + state, + ...(promptResult.stopReason ? { stopReason: promptResult.stopReason } : {}), + }, + }); + + context.turnState = undefined; + const doneAt = yield* nowIso; + context.session = { + ...context.session, + status: "ready", + activeTurnId: undefined, + updatedAt: doneAt, + }; + }), + ); + + return { + threadId: context.session.threadId, + turnId, + resumeCursor: context.session.resumeCursor, + }; + }); + + const interruptTurn: CursorAdapterShape["interruptTurn"] = (sessionId, _turnId) => + Effect.gen(function* () { + const context = yield* requireSession(sessionId); + + if (!context.turnState) return; + + const turnId = context.turnState.turnId; + context.stopped = true; + context.child.kill("SIGTERM"); + + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + turnId: TurnId.makeUnsafe(turnId), + type: "turn.completed" as const, + payload: { + state: "interrupted" as const, + errorMessage: "Turn interrupted by user.", + }, + }); + + context.turnState = undefined; + const updatedAt = yield* nowIso; + context.session = { + ...context.session, + status: "ready", + activeTurnId: undefined, + updatedAt, + }; + }); + + const respondToRequest: CursorAdapterShape["respondToRequest"] = ( + sessionId, + requestId, + decision, + ) => + Effect.gen(function* () { + const context = yield* requireSession(sessionId); + const pending = context.pendingPermissions.get(requestId); + if (!pending) { + return yield* new ProviderAdapterRequestError({ + provider: PROVIDER, + method: "session/request_permission", + detail: `Unknown pending permission request: ${requestId}`, + }); + } + + context.pendingPermissions.delete(requestId); + + const optionId = + decision === "accept" + ? "allow-once" + : decision === "acceptForSession" + ? "allow-always" + : "reject-once"; + + rpcRespond(context, pending.jsonRpcId, { + outcome: { outcome: "selected", optionId }, + }); + + const stamp = yield* makeEventStamp(); + yield* offerEvent({ + ...makeV2Base(context, stamp), + ...(context.turnState + ? { turnId: TurnId.makeUnsafe(context.turnState.turnId) } + : {}), + requestId: RuntimeRequestId.makeUnsafe(requestId), + type: "request.resolved" as const, + payload: { + requestType: pending.requestType, + decision, + }, + }); + }); + + const readThread: CursorAdapterShape["readThread"] = (sessionId) => + Effect.gen(function* () { + const context = yield* requireSession(sessionId); + const threadId = context.session.threadId; + if (!threadId) { + return yield* new ProviderAdapterValidationError({ + provider: PROVIDER, + operation: "readThread", + issue: "Session thread id is not initialized yet.", + }); + } + return { threadId, turns: [] }; + }); + + const rollbackThread: CursorAdapterShape["rollbackThread"] = (_sessionId, _numTurns) => + Effect.fail( + new ProviderAdapterValidationError({ + provider: PROVIDER, + operation: "rollbackThread", + issue: "Cursor ACP does not support thread rollback.", + }), + ); + + const stopSession: CursorAdapterShape["stopSession"] = (sessionId) => + Effect.gen(function* () { + const context = yield* requireSession(sessionId); + yield* stopSessionInternal(context, { emitExitEvent: true }); + }); + + const listSessions: CursorAdapterShape["listSessions"] = () => + Effect.sync(() => Array.from(sessions.values(), ({ session }) => ({ ...session }))); + + const hasSession: CursorAdapterShape["hasSession"] = (sessionId) => + Effect.sync(() => { + const context = sessions.get(sessionId); + return context !== undefined && !context.stopped; + }); + + const stopAll: CursorAdapterShape["stopAll"] = () => + Effect.forEach( + Array.from(sessions.values()), + (context) => stopSessionInternal(context, { emitExitEvent: true }), + { discard: true }, + ); + + yield* Effect.addFinalizer(() => + Effect.forEach( + Array.from(sessions.values()), + (context) => stopSessionInternal(context, { emitExitEvent: false }), + { discard: true }, + ).pipe(Effect.tap(() => Queue.shutdown(runtimeEventQueue))), + ); + + return { + provider: PROVIDER, + startSession, + sendTurn, + interruptTurn, + readThread, + rollbackThread, + respondToRequest, + stopSession, + listSessions, + hasSession, + stopAll, + streamEvents: Stream.fromQueue(runtimeEventQueue), + } satisfies CursorAdapterShape; + }); +} + +export const CursorAdapterLive = Layer.effect(CursorAdapter, makeCursorAdapter()); + +export function makeCursorAdapterLive(options?: CursorAdapterLiveOptions) { + return Layer.effect(CursorAdapter, makeCursorAdapter(options)); +} diff --git a/apps/server/src/provider/Services/CursorAdapter.ts b/apps/server/src/provider/Services/CursorAdapter.ts index c7ee46d46a..75c6e68cbf 100644 --- a/apps/server/src/provider/Services/CursorAdapter.ts +++ b/apps/server/src/provider/Services/CursorAdapter.ts @@ -1,11 +1,11 @@ /** - * CursorAdapter - Cursor CLI implementation of the generic provider adapter contract. + * CursorAdapter - Cursor ACP (`agent acp`) implementation of the generic provider adapter contract. * - * This service will own Cursor CLI (`agent`) process / stream-json semantics and emit + * This service owns Cursor ACP process / JSON-RPC 2.0 semantics and emits * canonical provider runtime events via the shared provider adapter contract. * - * This file intentionally defines raw Cursor stream-json schemas up front so the future - * layer implementation can decode/validate NDJSON lines in a single place. + * ACP schemas for decode/validation of JSON-RPC messages are defined here so + * the layer implementation can validate protocol boundary in a single place. * * @module CursorAdapter */ @@ -14,201 +14,101 @@ import { Schema, ServiceMap } from "effect"; import type { ProviderAdapterError } from "../Errors.ts"; import type { ProviderAdapterShape } from "./ProviderAdapter.ts"; -export const CursorCliSessionId = Schema.String.check(Schema.isNonEmpty()); -export type CursorCliSessionId = typeof CursorCliSessionId.Type; +export const AcpSessionId = Schema.String.check(Schema.isNonEmpty()); +export type AcpSessionId = typeof AcpSessionId.Type; -export const CursorCliTextContentPart = Schema.Struct({ +export const AcpTextContent = Schema.Struct({ type: Schema.Literal("text"), text: Schema.String, }); -export type CursorCliTextContentPart = typeof CursorCliTextContentPart.Type; +export type AcpTextContent = typeof AcpTextContent.Type; -export const CursorCliContentPart = Schema.Union([ - CursorCliTextContentPart, - Schema.Struct({ - type: Schema.String, - }), -]); -export type CursorCliContentPart = typeof CursorCliContentPart.Type; - -export const CursorCliUserMessage = Schema.Struct({ - role: Schema.Literal("user"), - content: Schema.Array(CursorCliContentPart), -}); -export type CursorCliUserMessage = typeof CursorCliUserMessage.Type; - -export const CursorCliAssistantMessage = Schema.Struct({ - role: Schema.Literal("assistant"), - content: Schema.Array(CursorCliContentPart), -}); -export type CursorCliAssistantMessage = typeof CursorCliAssistantMessage.Type; - -export const CursorCliToolCallResult = Schema.Struct({ - success: Schema.optional(Schema.Unknown), - failure: Schema.optional(Schema.Unknown), - rejected: Schema.optional(Schema.Unknown), -}); -export type CursorCliToolCallResult = typeof CursorCliToolCallResult.Type; - -export const CursorCliToolCallEntry = Schema.Struct({ - args: Schema.optional(Schema.Unknown), - result: Schema.optional(CursorCliToolCallResult), -}); -export type CursorCliToolCallEntry = typeof CursorCliToolCallEntry.Type; - -export const CursorCliFunctionToolCall = Schema.Struct({ +export const AcpSessionMode = Schema.Struct({ + id: Schema.String, name: Schema.String, - arguments: Schema.String, - result: Schema.optional(CursorCliToolCallResult), + description: Schema.optional(Schema.String), }); -export type CursorCliFunctionToolCall = typeof CursorCliFunctionToolCall.Type; +export type AcpSessionMode = typeof AcpSessionMode.Type; -export const CursorCliToolCallPayload = Schema.Struct({ - readToolCall: Schema.optional(CursorCliToolCallEntry), - writeToolCall: Schema.optional(CursorCliToolCallEntry), - editToolCall: Schema.optional(CursorCliToolCallEntry), - shellToolCall: Schema.optional(CursorCliToolCallEntry), - grepToolCall: Schema.optional(CursorCliToolCallEntry), - globToolCall: Schema.optional(CursorCliToolCallEntry), - function: Schema.optional(CursorCliFunctionToolCall), +export const AcpSessionModes = Schema.Struct({ + currentModeId: Schema.String, + availableModes: Schema.Array(AcpSessionMode), }); -export type CursorCliToolCallPayload = typeof CursorCliToolCallPayload.Type; +export type AcpSessionModes = typeof AcpSessionModes.Type; -export const CursorCliTokenUsage = Schema.Struct({ - inputTokens: Schema.optional(Schema.Number), - outputTokens: Schema.optional(Schema.Number), - cacheReadTokens: Schema.optional(Schema.Number), - cacheWriteTokens: Schema.optional(Schema.Number), +export const AcpInitializeResult = Schema.Struct({ + protocolVersion: Schema.Number, + agentCapabilities: Schema.optional(Schema.Unknown), + authMethods: Schema.optional(Schema.Array(Schema.Unknown)), }); -export type CursorCliTokenUsage = typeof CursorCliTokenUsage.Type; +export type AcpInitializeResult = typeof AcpInitializeResult.Type; -const CursorCliTimestampMs = { - timestamp_ms: Schema.optional(Schema.Number), -} as const; - -export const CursorCliSystemInitEvent = Schema.Struct({ - type: Schema.Literal("system"), - subtype: Schema.Literal("init"), - session_id: CursorCliSessionId, - apiKeySource: Schema.optional(Schema.String), - cwd: Schema.optional(Schema.String), - model: Schema.optional(Schema.String), - permissionMode: Schema.optional(Schema.String), - ...CursorCliTimestampMs, +export const AcpSessionNewResult = Schema.Struct({ + sessionId: AcpSessionId, + modes: Schema.optional(AcpSessionModes), }); -export type CursorCliSystemInitEvent = typeof CursorCliSystemInitEvent.Type; +export type AcpSessionNewResult = typeof AcpSessionNewResult.Type; -export const CursorCliUserEvent = Schema.Struct({ - type: Schema.Literal("user"), - message: CursorCliUserMessage, - session_id: CursorCliSessionId, - ...CursorCliTimestampMs, +export const AcpPromptResult = Schema.Struct({ + stopReason: Schema.optional(Schema.String), }); -export type CursorCliUserEvent = typeof CursorCliUserEvent.Type; +export type AcpPromptResult = typeof AcpPromptResult.Type; -export const CursorCliAssistantEvent = Schema.Struct({ - type: Schema.Literal("assistant"), - message: CursorCliAssistantMessage, - session_id: CursorCliSessionId, - ...CursorCliTimestampMs, +export const AcpPermissionOption = Schema.Struct({ + optionId: Schema.String, + name: Schema.String, + kind: Schema.optional(Schema.String), }); -export type CursorCliAssistantEvent = typeof CursorCliAssistantEvent.Type; +export type AcpPermissionOption = typeof AcpPermissionOption.Type; -export const CursorCliThinkingDeltaEvent = Schema.Struct({ - type: Schema.Literal("thinking"), - subtype: Schema.Literal("delta"), - text: Schema.String, - session_id: CursorCliSessionId, - ...CursorCliTimestampMs, +export const AcpPermissionToolCall = Schema.Struct({ + toolCallId: Schema.String, + title: Schema.optional(Schema.String), + kind: Schema.optional(Schema.String), + status: Schema.optional(Schema.String), + content: Schema.optional(Schema.Array(Schema.Unknown)), }); -export type CursorCliThinkingDeltaEvent = typeof CursorCliThinkingDeltaEvent.Type; +export type AcpPermissionToolCall = typeof AcpPermissionToolCall.Type; -export const CursorCliThinkingCompletedEvent = Schema.Struct({ - type: Schema.Literal("thinking"), - subtype: Schema.Literal("completed"), - session_id: CursorCliSessionId, - ...CursorCliTimestampMs, +export const AcpPermissionRequestParams = Schema.Struct({ + sessionId: AcpSessionId, + toolCall: AcpPermissionToolCall, + options: Schema.Array(AcpPermissionOption), }); -export type CursorCliThinkingCompletedEvent = typeof CursorCliThinkingCompletedEvent.Type; +export type AcpPermissionRequestParams = typeof AcpPermissionRequestParams.Type; -export const CursorCliToolCallStartedEvent = Schema.Struct({ - type: Schema.Literal("tool_call"), - subtype: Schema.Literal("started"), - call_id: Schema.String, - tool_call: CursorCliToolCallPayload, - session_id: CursorCliSessionId, - ...CursorCliTimestampMs, -}); -export type CursorCliToolCallStartedEvent = typeof CursorCliToolCallStartedEvent.Type; +export type AcpSessionUpdateType = + | "available_commands_update" + | "agent_thought_chunk" + | "agent_message_chunk" + | "tool_call" + | "tool_call_update"; -export const CursorCliToolCallCompletedEvent = Schema.Struct({ - type: Schema.Literal("tool_call"), - subtype: Schema.Literal("completed"), - call_id: Schema.String, - tool_call: CursorCliToolCallPayload, - session_id: CursorCliSessionId, - ...CursorCliTimestampMs, -}); -export type CursorCliToolCallCompletedEvent = typeof CursorCliToolCallCompletedEvent.Type; +export interface AcpSessionUpdate { + readonly sessionUpdate: AcpSessionUpdateType | string; + readonly content?: { type: string; text: string }; + readonly toolCallId?: string; + readonly title?: string; + readonly kind?: string; + readonly status?: string; + readonly rawInput?: unknown; + readonly rawOutput?: unknown; + readonly availableCommands?: ReadonlyArray<{ name: string; description: string }>; +} -export const CursorCliResultSuccessEvent = Schema.Struct({ - type: Schema.Literal("result"), - subtype: Schema.Literal("success"), - duration_ms: Schema.Number, - duration_api_ms: Schema.optional(Schema.Number), - is_error: Schema.Boolean, - result: Schema.String, - session_id: CursorCliSessionId, - request_id: Schema.optional(Schema.String), - usage: Schema.optional(CursorCliTokenUsage), - ...CursorCliTimestampMs, -}); -export type CursorCliResultSuccessEvent = typeof CursorCliResultSuccessEvent.Type; +export interface AcpJsonRpcMessage { + readonly jsonrpc?: string; + readonly id?: number; + readonly method?: string; + readonly params?: unknown; + readonly result?: unknown; + readonly error?: { code: number; message: string; data?: unknown }; +} -export const CursorCliConnectionEvent = Schema.Struct({ - type: Schema.Literal("connection"), - subtype: Schema.Literals(["reconnecting", "reconnected"]), - session_id: Schema.optional(CursorCliSessionId), - ...CursorCliTimestampMs, -}); -export type CursorCliConnectionEvent = typeof CursorCliConnectionEvent.Type; - -export const CursorCliRetryEvent = Schema.Struct({ - type: Schema.Literal("retry"), - subtype: Schema.Literals(["starting", "resuming"]), - session_id: Schema.optional(CursorCliSessionId), - ...CursorCliTimestampMs, -}); -export type CursorCliRetryEvent = typeof CursorCliRetryEvent.Type; - -export const CursorCliStreamEvent = Schema.Union([ - CursorCliSystemInitEvent, - CursorCliUserEvent, - CursorCliAssistantEvent, - CursorCliThinkingDeltaEvent, - CursorCliThinkingCompletedEvent, - CursorCliToolCallStartedEvent, - CursorCliToolCallCompletedEvent, - CursorCliResultSuccessEvent, - CursorCliConnectionEvent, - CursorCliRetryEvent, -]); -export type CursorCliStreamEvent = typeof CursorCliStreamEvent.Type; - -/** - * CursorAdapterShape - Service API for the Cursor provider adapter. - * - * `provider` is intentionally narrowed to `"cursor"` here. Until contracts add - * Cursor to `ProviderKind`, this shape is defined via `Omit<...,"provider">`. - */ -export interface CursorAdapterShape - extends Omit, "provider"> { +export interface CursorAdapterShape extends ProviderAdapterShape { readonly provider: "cursor"; } -/** - * CursorAdapter - Service tag for Cursor provider adapter operations. - */ export class CursorAdapter extends ServiceMap.Service()( "t3/provider/Services/CursorAdapter", ) {}