908 lines
31 KiB
TypeScript
908 lines
31 KiB
TypeScript
/**
|
|
* CursorAdapterLive — Cursor CLI (`agent acp`) via ACP.
|
|
*
|
|
* @module CursorAdapterLive
|
|
*/
|
|
import * as nodePath from "node:path";
|
|
|
|
import {
|
|
ApprovalRequestId,
|
|
EventId,
|
|
type ProviderApprovalDecision,
|
|
type ProviderInteractionMode,
|
|
type ProviderRuntimeEvent,
|
|
type ProviderSession,
|
|
type ProviderUserInputAnswers,
|
|
RuntimeRequestId,
|
|
type RuntimeMode,
|
|
type ThreadId,
|
|
TurnId,
|
|
} from "@t3tools/contracts";
|
|
import {
|
|
DateTime,
|
|
Deferred,
|
|
Effect,
|
|
Fiber,
|
|
FileSystem,
|
|
Layer,
|
|
Queue,
|
|
Random,
|
|
Scope,
|
|
Stream,
|
|
} from "effect";
|
|
import { ChildProcessSpawner } from "effect/unstable/process";
|
|
import type * as EffectAcpSchema from "effect-acp/schema";
|
|
|
|
import { resolveAttachmentPath } from "../../attachmentStore.ts";
|
|
import { ServerConfig } from "../../config.ts";
|
|
import { ServerSettingsService } from "../../serverSettings.ts";
|
|
import {
|
|
ProviderAdapterProcessError,
|
|
ProviderAdapterRequestError,
|
|
ProviderAdapterSessionNotFoundError,
|
|
ProviderAdapterValidationError,
|
|
} from "../Errors.ts";
|
|
import { AcpSessionRuntime, type AcpSessionRuntimeShape } from "../acp/AcpSessionRuntime.ts";
|
|
import { acpPermissionOutcome, mapAcpToAdapterError } from "../acp/AcpAdapterSupport.ts";
|
|
import {
|
|
makeAcpContentDeltaEvent,
|
|
makeAcpPlanUpdatedEvent,
|
|
makeAcpRequestOpenedEvent,
|
|
makeAcpRequestResolvedEvent,
|
|
makeAcpToolCallEvent,
|
|
} from "../acp/AcpCoreRuntimeEvents.ts";
|
|
import {
|
|
type AcpSessionMode,
|
|
type AcpSessionModeState,
|
|
parsePermissionRequest,
|
|
} from "../acp/AcpRuntimeModel.ts";
|
|
import { makeAcpNativeLoggers } from "../acp/AcpNativeLogging.ts";
|
|
import {
|
|
CursorAskQuestionRequest,
|
|
CursorCreatePlanRequest,
|
|
CursorUpdateTodosRequest,
|
|
extractAskQuestions,
|
|
extractPlanMarkdown,
|
|
extractTodosAsPlan,
|
|
} from "../acp/CursorAcpExtension.ts";
|
|
import { CursorAdapter, type CursorAdapterShape } from "../Services/CursorAdapter.ts";
|
|
import { resolveCursorAcpModelId } from "./CursorProvider.ts";
|
|
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
|
|
|
|
const PROVIDER = "cursor" as const;
|
|
const CURSOR_RESUME_VERSION = 1 as const;
|
|
const ACP_PLAN_MODE_ALIASES = ["plan", "architect"];
|
|
const ACP_IMPLEMENT_MODE_ALIASES = ["code", "agent", "default", "chat", "implement"];
|
|
const ACP_APPROVAL_MODE_ALIASES = ["ask"];
|
|
|
|
export interface CursorAdapterLiveOptions {
|
|
readonly nativeEventLogPath?: string;
|
|
readonly nativeEventLogger?: EventNdjsonLogger;
|
|
}
|
|
|
|
interface PendingApproval {
|
|
readonly decision: Deferred.Deferred<ProviderApprovalDecision>;
|
|
readonly kind: string | "unknown";
|
|
}
|
|
|
|
interface PendingUserInput {
|
|
readonly answers: Deferred.Deferred<ProviderUserInputAnswers>;
|
|
}
|
|
|
|
interface CursorSessionContext {
|
|
readonly threadId: ThreadId;
|
|
session: ProviderSession;
|
|
readonly acp: AcpSessionRuntimeShape;
|
|
notificationFiber: Fiber.Fiber<void, never> | undefined;
|
|
readonly pendingApprovals: Map<ApprovalRequestId, PendingApproval>;
|
|
readonly pendingUserInputs: Map<ApprovalRequestId, PendingUserInput>;
|
|
readonly turns: Array<{ id: TurnId; items: Array<unknown> }>;
|
|
lastPlanFingerprint: string | undefined;
|
|
activeTurnId: TurnId | undefined;
|
|
stopped: boolean;
|
|
}
|
|
|
|
function settlePendingApprovalsAsCancelled(
|
|
pendingApprovals: ReadonlyMap<ApprovalRequestId, PendingApproval>,
|
|
): Effect.Effect<void> {
|
|
const pendingEntries = Array.from(pendingApprovals.values());
|
|
return Effect.forEach(
|
|
pendingEntries,
|
|
(pending) => Deferred.succeed(pending.decision, "cancel").pipe(Effect.ignore),
|
|
{
|
|
discard: true,
|
|
},
|
|
);
|
|
}
|
|
|
|
function settlePendingUserInputsAsEmptyAnswers(
|
|
pendingUserInputs: ReadonlyMap<ApprovalRequestId, PendingUserInput>,
|
|
): Effect.Effect<void> {
|
|
const pendingEntries = Array.from(pendingUserInputs.values());
|
|
return Effect.forEach(
|
|
pendingEntries,
|
|
(pending) => Deferred.succeed(pending.answers, {}).pipe(Effect.ignore),
|
|
{
|
|
discard: true,
|
|
},
|
|
);
|
|
}
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
function parseCursorResume(raw: unknown): { sessionId: string } | undefined {
|
|
if (!isRecord(raw)) return undefined;
|
|
if (raw.schemaVersion !== CURSOR_RESUME_VERSION) return undefined;
|
|
if (typeof raw.sessionId !== "string" || !raw.sessionId.trim()) return undefined;
|
|
return { sessionId: raw.sessionId.trim() };
|
|
}
|
|
|
|
function normalizeModeSearchText(mode: AcpSessionMode): string {
|
|
return [mode.id, mode.name, mode.description]
|
|
.filter((value): value is string => typeof value === "string" && value.length > 0)
|
|
.join(" ")
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, " ")
|
|
.trim();
|
|
}
|
|
|
|
function findModeByAliases(
|
|
modes: ReadonlyArray<AcpSessionMode>,
|
|
aliases: ReadonlyArray<string>,
|
|
): AcpSessionMode | undefined {
|
|
const normalizedAliases = aliases.map((alias) => alias.toLowerCase());
|
|
for (const alias of normalizedAliases) {
|
|
const exact = modes.find((mode) => {
|
|
const id = mode.id.toLowerCase();
|
|
const name = mode.name.toLowerCase();
|
|
return id === alias || name === alias;
|
|
});
|
|
if (exact) {
|
|
return exact;
|
|
}
|
|
}
|
|
for (const alias of normalizedAliases) {
|
|
const partial = modes.find((mode) => normalizeModeSearchText(mode).includes(alias));
|
|
if (partial) {
|
|
return partial;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
function isPlanMode(mode: AcpSessionMode): boolean {
|
|
return findModeByAliases([mode], ACP_PLAN_MODE_ALIASES) !== undefined;
|
|
}
|
|
|
|
function resolveRequestedModeId(input: {
|
|
readonly interactionMode: ProviderInteractionMode | undefined;
|
|
readonly runtimeMode: RuntimeMode;
|
|
readonly modeState: AcpSessionModeState | undefined;
|
|
}): string | undefined {
|
|
const modeState = input.modeState;
|
|
if (!modeState) {
|
|
return undefined;
|
|
}
|
|
|
|
if (input.interactionMode === "plan") {
|
|
return findModeByAliases(modeState.availableModes, ACP_PLAN_MODE_ALIASES)?.id;
|
|
}
|
|
|
|
if (input.runtimeMode === "approval-required") {
|
|
return (
|
|
findModeByAliases(modeState.availableModes, ACP_APPROVAL_MODE_ALIASES)?.id ??
|
|
findModeByAliases(modeState.availableModes, ACP_IMPLEMENT_MODE_ALIASES)?.id ??
|
|
modeState.availableModes.find((mode) => !isPlanMode(mode))?.id ??
|
|
modeState.currentModeId
|
|
);
|
|
}
|
|
|
|
return (
|
|
findModeByAliases(modeState.availableModes, ACP_IMPLEMENT_MODE_ALIASES)?.id ??
|
|
findModeByAliases(modeState.availableModes, ACP_APPROVAL_MODE_ALIASES)?.id ??
|
|
modeState.availableModes.find((mode) => !isPlanMode(mode))?.id ??
|
|
modeState.currentModeId
|
|
);
|
|
}
|
|
|
|
function makeCursorAdapter(options?: CursorAdapterLiveOptions) {
|
|
return Effect.gen(function* () {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
const serverConfig = yield* Effect.service(ServerConfig);
|
|
const serverSettingsService = yield* ServerSettingsService;
|
|
const nativeEventLogger =
|
|
options?.nativeEventLogger ??
|
|
(options?.nativeEventLogPath !== undefined
|
|
? yield* makeEventNdjsonLogger(options.nativeEventLogPath, {
|
|
stream: "native",
|
|
})
|
|
: undefined);
|
|
|
|
const sessions = new Map<ThreadId, CursorSessionContext>();
|
|
const runtimeEventQueue = yield* Queue.unbounded<ProviderRuntimeEvent>();
|
|
|
|
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 offerRuntimeEvent = (event: ProviderRuntimeEvent) =>
|
|
Queue.offer(runtimeEventQueue, event).pipe(Effect.asVoid);
|
|
|
|
const logNative = (
|
|
threadId: ThreadId,
|
|
method: string,
|
|
payload: unknown,
|
|
_source: "acp.jsonrpc" | "acp.cursor.extension",
|
|
) =>
|
|
Effect.gen(function* () {
|
|
if (!nativeEventLogger) return;
|
|
const observedAt = new Date().toISOString();
|
|
yield* nativeEventLogger.write(
|
|
{
|
|
observedAt,
|
|
event: {
|
|
id: crypto.randomUUID(),
|
|
kind: "notification",
|
|
provider: PROVIDER,
|
|
createdAt: observedAt,
|
|
method,
|
|
threadId,
|
|
payload,
|
|
},
|
|
},
|
|
threadId,
|
|
);
|
|
});
|
|
|
|
const emitPlanUpdate = (
|
|
ctx: CursorSessionContext,
|
|
payload: {
|
|
readonly explanation?: string | null;
|
|
readonly plan: ReadonlyArray<{
|
|
readonly step: string;
|
|
readonly status: "pending" | "inProgress" | "completed";
|
|
}>;
|
|
},
|
|
rawPayload: unknown,
|
|
source: "acp.jsonrpc" | "acp.cursor.extension",
|
|
method: string,
|
|
) =>
|
|
Effect.gen(function* () {
|
|
const fingerprint = `${ctx.activeTurnId ?? "no-turn"}:${JSON.stringify(payload)}`;
|
|
if (ctx.lastPlanFingerprint === fingerprint) {
|
|
return;
|
|
}
|
|
ctx.lastPlanFingerprint = fingerprint;
|
|
yield* offerRuntimeEvent(
|
|
makeAcpPlanUpdatedEvent({
|
|
stamp: yield* makeEventStamp(),
|
|
provider: PROVIDER,
|
|
threadId: ctx.threadId,
|
|
turnId: ctx.activeTurnId,
|
|
payload,
|
|
source,
|
|
method,
|
|
rawPayload,
|
|
}),
|
|
);
|
|
});
|
|
|
|
const requireSession = (
|
|
threadId: ThreadId,
|
|
): Effect.Effect<CursorSessionContext, ProviderAdapterSessionNotFoundError> => {
|
|
const ctx = sessions.get(threadId);
|
|
if (!ctx || ctx.stopped) {
|
|
return Effect.fail(
|
|
new ProviderAdapterSessionNotFoundError({ provider: PROVIDER, threadId }),
|
|
);
|
|
}
|
|
return Effect.succeed(ctx);
|
|
};
|
|
|
|
const stopSessionInternal = (ctx: CursorSessionContext) =>
|
|
Effect.gen(function* () {
|
|
if (ctx.stopped) return;
|
|
ctx.stopped = true;
|
|
yield* settlePendingApprovalsAsCancelled(ctx.pendingApprovals);
|
|
yield* settlePendingUserInputsAsEmptyAnswers(ctx.pendingUserInputs);
|
|
if (ctx.notificationFiber) {
|
|
yield* Fiber.interrupt(ctx.notificationFiber);
|
|
}
|
|
yield* Effect.ignore(ctx.acp.close);
|
|
sessions.delete(ctx.threadId);
|
|
yield* offerRuntimeEvent({
|
|
type: "session.exited",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: ctx.threadId,
|
|
payload: { exitKind: "graceful" },
|
|
});
|
|
});
|
|
|
|
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}'.`,
|
|
});
|
|
}
|
|
if (!input.cwd?.trim()) {
|
|
return yield* new ProviderAdapterValidationError({
|
|
provider: PROVIDER,
|
|
operation: "startSession",
|
|
issue: "cwd is required and must be non-empty.",
|
|
});
|
|
}
|
|
|
|
const cwd = nodePath.resolve(input.cwd.trim());
|
|
const cursorModelSelection =
|
|
input.modelSelection?.provider === "cursor" ? input.modelSelection : undefined;
|
|
const existing = sessions.get(input.threadId);
|
|
if (existing && !existing.stopped) {
|
|
yield* stopSessionInternal(existing);
|
|
}
|
|
|
|
const spawnOptions = yield* serverSettingsService.getSettings.pipe(
|
|
Effect.map((settings) => settings.providers.cursor),
|
|
Effect.map((cursorSettings) => ({
|
|
command: cursorSettings.binaryPath,
|
|
args: [
|
|
...(cursorSettings.apiEndpoint ? (["-e", cursorSettings.apiEndpoint] as const) : []),
|
|
"acp",
|
|
],
|
|
cwd,
|
|
})),
|
|
Effect.mapError(
|
|
(error) =>
|
|
new ProviderAdapterProcessError({
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
detail: error.message,
|
|
cause: error,
|
|
}),
|
|
),
|
|
);
|
|
|
|
const pendingApprovals = new Map<ApprovalRequestId, PendingApproval>();
|
|
const pendingUserInputs = new Map<ApprovalRequestId, PendingUserInput>();
|
|
let ctx!: CursorSessionContext;
|
|
|
|
const resumeSessionId = parseCursorResume(input.resumeCursor)?.sessionId;
|
|
const acpNativeLoggers = makeAcpNativeLoggers({
|
|
nativeEventLogger,
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
});
|
|
|
|
const acpContextScope = yield* Scope.make("sequential");
|
|
const acpContext = yield* Layer.build(
|
|
AcpSessionRuntime.layer({
|
|
spawn: spawnOptions,
|
|
cwd,
|
|
...(resumeSessionId ? { resumeSessionId } : {}),
|
|
clientInfo: { name: "t3-code", version: "0.0.0" },
|
|
authMethodId: "cursor_login",
|
|
...acpNativeLoggers,
|
|
}).pipe(
|
|
Layer.provide(
|
|
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, childProcessSpawner),
|
|
),
|
|
),
|
|
).pipe(
|
|
Effect.provideService(Scope.Scope, acpContextScope),
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ProviderAdapterProcessError({
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
detail: cause.message,
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
const acp = yield* Effect.service(AcpSessionRuntime).pipe(Effect.provide(acpContext));
|
|
const started = yield* Effect.gen(function* () {
|
|
yield* acp.handleExtRequest("cursor/ask_question", CursorAskQuestionRequest, (params) =>
|
|
Effect.gen(function* () {
|
|
yield* logNative(
|
|
input.threadId,
|
|
"cursor/ask_question",
|
|
params,
|
|
"acp.cursor.extension",
|
|
);
|
|
const requestId = ApprovalRequestId.makeUnsafe(crypto.randomUUID());
|
|
const runtimeRequestId = RuntimeRequestId.makeUnsafe(requestId);
|
|
const answers = yield* Deferred.make<ProviderUserInputAnswers>();
|
|
pendingUserInputs.set(requestId, { answers });
|
|
yield* offerRuntimeEvent({
|
|
type: "user-input.requested",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
turnId: ctx?.activeTurnId,
|
|
requestId: runtimeRequestId,
|
|
payload: { questions: extractAskQuestions(params) },
|
|
raw: {
|
|
source: "acp.cursor.extension",
|
|
method: "cursor/ask_question",
|
|
payload: params,
|
|
},
|
|
});
|
|
const resolved = yield* Deferred.await(answers);
|
|
pendingUserInputs.delete(requestId);
|
|
yield* offerRuntimeEvent({
|
|
type: "user-input.resolved",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
turnId: ctx?.activeTurnId,
|
|
requestId: runtimeRequestId,
|
|
payload: { answers: resolved },
|
|
});
|
|
return { answers: resolved };
|
|
}),
|
|
);
|
|
yield* acp.handleExtRequest("cursor/create_plan", CursorCreatePlanRequest, (params) =>
|
|
Effect.gen(function* () {
|
|
yield* logNative(
|
|
input.threadId,
|
|
"cursor/create_plan",
|
|
params,
|
|
"acp.cursor.extension",
|
|
);
|
|
yield* offerRuntimeEvent({
|
|
type: "turn.proposed.completed",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
turnId: ctx?.activeTurnId,
|
|
payload: { planMarkdown: extractPlanMarkdown(params) },
|
|
raw: {
|
|
source: "acp.cursor.extension",
|
|
method: "cursor/create_plan",
|
|
payload: params,
|
|
},
|
|
});
|
|
return { accepted: true } as const;
|
|
}),
|
|
);
|
|
yield* acp.handleExtNotification(
|
|
"cursor/update_todos",
|
|
CursorUpdateTodosRequest,
|
|
(params) =>
|
|
Effect.gen(function* () {
|
|
yield* logNative(
|
|
input.threadId,
|
|
"cursor/update_todos",
|
|
params,
|
|
"acp.cursor.extension",
|
|
);
|
|
if (ctx) {
|
|
yield* emitPlanUpdate(
|
|
ctx,
|
|
extractTodosAsPlan(params),
|
|
params,
|
|
"acp.cursor.extension",
|
|
"cursor/update_todos",
|
|
);
|
|
}
|
|
}),
|
|
);
|
|
yield* acp.handleRequestPermission((params) =>
|
|
Effect.gen(function* () {
|
|
yield* logNative(input.threadId, "session/request_permission", params, "acp.jsonrpc");
|
|
const permissionRequest = parsePermissionRequest(params);
|
|
const requestId = ApprovalRequestId.makeUnsafe(crypto.randomUUID());
|
|
const runtimeRequestId = RuntimeRequestId.makeUnsafe(requestId);
|
|
const decision = yield* Deferred.make<ProviderApprovalDecision>();
|
|
pendingApprovals.set(requestId, {
|
|
decision,
|
|
kind: permissionRequest.kind,
|
|
});
|
|
yield* offerRuntimeEvent(
|
|
makeAcpRequestOpenedEvent({
|
|
stamp: yield* makeEventStamp(),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
turnId: ctx?.activeTurnId,
|
|
requestId: runtimeRequestId,
|
|
permissionRequest,
|
|
detail: permissionRequest.detail ?? JSON.stringify(params).slice(0, 2000),
|
|
args: params,
|
|
source: "acp.jsonrpc",
|
|
method: "session/request_permission",
|
|
rawPayload: params,
|
|
}),
|
|
);
|
|
const resolved = yield* Deferred.await(decision);
|
|
pendingApprovals.delete(requestId);
|
|
yield* offerRuntimeEvent(
|
|
makeAcpRequestResolvedEvent({
|
|
stamp: yield* makeEventStamp(),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
turnId: ctx?.activeTurnId,
|
|
requestId: runtimeRequestId,
|
|
permissionRequest,
|
|
decision: resolved,
|
|
}),
|
|
);
|
|
return {
|
|
outcome:
|
|
resolved === "cancel"
|
|
? ({ outcome: "cancelled" } as const)
|
|
: {
|
|
outcome: "selected" as const,
|
|
optionId: acpPermissionOutcome(resolved),
|
|
},
|
|
};
|
|
}),
|
|
);
|
|
return yield* acp.start();
|
|
}).pipe(
|
|
Effect.mapError((error) =>
|
|
mapAcpToAdapterError(PROVIDER, input.threadId, "session/start", error),
|
|
),
|
|
);
|
|
|
|
const now = yield* nowIso;
|
|
const session: ProviderSession = {
|
|
provider: PROVIDER,
|
|
status: "ready",
|
|
runtimeMode: input.runtimeMode,
|
|
cwd,
|
|
model: cursorModelSelection?.model,
|
|
threadId: input.threadId,
|
|
resumeCursor: {
|
|
schemaVersion: CURSOR_RESUME_VERSION,
|
|
sessionId: started.sessionId,
|
|
},
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
};
|
|
|
|
ctx = {
|
|
threadId: input.threadId,
|
|
session,
|
|
acp,
|
|
notificationFiber: undefined,
|
|
pendingApprovals,
|
|
pendingUserInputs,
|
|
turns: [],
|
|
lastPlanFingerprint: undefined,
|
|
activeTurnId: undefined,
|
|
stopped: false,
|
|
};
|
|
|
|
const nf = yield* Stream.runDrain(
|
|
Stream.mapEffect(acp.events, (event) =>
|
|
Effect.gen(function* () {
|
|
switch (event._tag) {
|
|
case "ModeChanged":
|
|
return;
|
|
case "PlanUpdated":
|
|
yield* logNative(ctx.threadId, "session/update", event.rawPayload, "acp.jsonrpc");
|
|
yield* emitPlanUpdate(
|
|
ctx,
|
|
event.payload,
|
|
event.rawPayload,
|
|
"acp.jsonrpc",
|
|
"session/update",
|
|
);
|
|
return;
|
|
case "ToolCallUpdated":
|
|
yield* logNative(ctx.threadId, "session/update", event.rawPayload, "acp.jsonrpc");
|
|
yield* offerRuntimeEvent(
|
|
makeAcpToolCallEvent({
|
|
stamp: yield* makeEventStamp(),
|
|
provider: PROVIDER,
|
|
threadId: ctx.threadId,
|
|
turnId: ctx.activeTurnId,
|
|
toolCall: event.toolCall,
|
|
rawPayload: event.rawPayload,
|
|
}),
|
|
);
|
|
return;
|
|
case "ContentDelta":
|
|
yield* logNative(ctx.threadId, "session/update", event.rawPayload, "acp.jsonrpc");
|
|
yield* offerRuntimeEvent(
|
|
makeAcpContentDeltaEvent({
|
|
stamp: yield* makeEventStamp(),
|
|
provider: PROVIDER,
|
|
threadId: ctx.threadId,
|
|
turnId: ctx.activeTurnId,
|
|
text: event.text,
|
|
rawPayload: event.rawPayload,
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
}),
|
|
),
|
|
).pipe(Effect.forkChild);
|
|
|
|
ctx.notificationFiber = nf;
|
|
sessions.set(input.threadId, ctx);
|
|
|
|
yield* offerRuntimeEvent({
|
|
type: "session.started",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
payload: { resume: started.initializeResult },
|
|
});
|
|
yield* offerRuntimeEvent({
|
|
type: "session.state.changed",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
payload: { state: "ready", reason: "Cursor ACP session ready" },
|
|
});
|
|
yield* offerRuntimeEvent({
|
|
type: "thread.started",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
payload: { providerThreadId: started.sessionId },
|
|
});
|
|
|
|
return session;
|
|
});
|
|
|
|
const sendTurn: CursorAdapterShape["sendTurn"] = (input) =>
|
|
Effect.gen(function* () {
|
|
const ctx = yield* requireSession(input.threadId);
|
|
const turnId = TurnId.makeUnsafe(crypto.randomUUID());
|
|
const turnModelSelection =
|
|
input.modelSelection?.provider === "cursor" ? input.modelSelection : undefined;
|
|
const model = resolveCursorAcpModelId(
|
|
turnModelSelection?.model ?? ctx.session.model,
|
|
turnModelSelection?.options,
|
|
);
|
|
|
|
yield* ctx.acp
|
|
.setModel(model)
|
|
.pipe(
|
|
Effect.mapError((error) =>
|
|
mapAcpToAdapterError(PROVIDER, input.threadId, "session/set_config_option", error),
|
|
),
|
|
);
|
|
ctx.activeTurnId = turnId;
|
|
ctx.lastPlanFingerprint = undefined;
|
|
ctx.session = {
|
|
...ctx.session,
|
|
activeTurnId: turnId,
|
|
updatedAt: yield* nowIso,
|
|
};
|
|
|
|
const requestedModeId = resolveRequestedModeId({
|
|
interactionMode: input.interactionMode,
|
|
runtimeMode: ctx.session.runtimeMode,
|
|
modeState: yield* ctx.acp.getModeState,
|
|
});
|
|
if (requestedModeId) {
|
|
yield* Effect.ignore(
|
|
ctx.acp
|
|
.setMode(requestedModeId)
|
|
.pipe(
|
|
Effect.mapError((error) =>
|
|
mapAcpToAdapterError(PROVIDER, input.threadId, "session/set_mode", error),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
yield* offerRuntimeEvent({
|
|
type: "turn.started",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
turnId,
|
|
payload: { model },
|
|
});
|
|
|
|
const promptParts: Array<EffectAcpSchema.ContentBlock> = [];
|
|
if (input.input?.trim()) {
|
|
promptParts.push({ type: "text", text: input.input.trim() });
|
|
}
|
|
if (input.attachments && input.attachments.length > 0) {
|
|
for (const attachment of input.attachments) {
|
|
const attachmentPath = resolveAttachmentPath({
|
|
attachmentsDir: serverConfig.attachmentsDir,
|
|
attachment,
|
|
});
|
|
if (!attachmentPath) {
|
|
return yield* new ProviderAdapterRequestError({
|
|
provider: PROVIDER,
|
|
method: "session/prompt",
|
|
detail: `Invalid attachment id '${attachment.id}'.`,
|
|
});
|
|
}
|
|
const bytes = yield* fileSystem.readFile(attachmentPath).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ProviderAdapterRequestError({
|
|
provider: PROVIDER,
|
|
method: "session/prompt",
|
|
detail: cause.message,
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
promptParts.push({
|
|
type: "image",
|
|
data: Buffer.from(bytes).toString("base64"),
|
|
mimeType: attachment.mimeType,
|
|
});
|
|
}
|
|
}
|
|
|
|
if (promptParts.length === 0) {
|
|
return yield* new ProviderAdapterValidationError({
|
|
provider: PROVIDER,
|
|
operation: "sendTurn",
|
|
issue: "Turn requires non-empty text or attachments.",
|
|
});
|
|
}
|
|
|
|
const result = yield* ctx.acp
|
|
.prompt({
|
|
prompt: promptParts,
|
|
})
|
|
.pipe(
|
|
Effect.mapError((error) =>
|
|
mapAcpToAdapterError(PROVIDER, input.threadId, "session/prompt", error),
|
|
),
|
|
);
|
|
|
|
ctx.turns.push({ id: turnId, items: [{ prompt: promptParts, result }] });
|
|
ctx.session = {
|
|
...ctx.session,
|
|
activeTurnId: turnId,
|
|
updatedAt: yield* nowIso,
|
|
model,
|
|
};
|
|
|
|
yield* offerRuntimeEvent({
|
|
type: "turn.completed",
|
|
...(yield* makeEventStamp()),
|
|
provider: PROVIDER,
|
|
threadId: input.threadId,
|
|
turnId,
|
|
payload: {
|
|
state: result.stopReason === "cancelled" ? "cancelled" : "completed",
|
|
stopReason: result.stopReason ?? null,
|
|
},
|
|
});
|
|
|
|
return {
|
|
threadId: input.threadId,
|
|
turnId,
|
|
resumeCursor: ctx.session.resumeCursor,
|
|
};
|
|
});
|
|
|
|
const interruptTurn: CursorAdapterShape["interruptTurn"] = (threadId) =>
|
|
Effect.gen(function* () {
|
|
const ctx = yield* requireSession(threadId);
|
|
yield* settlePendingApprovalsAsCancelled(ctx.pendingApprovals);
|
|
yield* Effect.ignore(
|
|
ctx.acp.cancel.pipe(
|
|
Effect.mapError((error) =>
|
|
mapAcpToAdapterError(PROVIDER, threadId, "session/cancel", error),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
const respondToRequest: CursorAdapterShape["respondToRequest"] = (
|
|
threadId,
|
|
requestId,
|
|
decision,
|
|
) =>
|
|
Effect.gen(function* () {
|
|
const ctx = yield* requireSession(threadId);
|
|
const pending = ctx.pendingApprovals.get(requestId);
|
|
if (!pending) {
|
|
return yield* new ProviderAdapterRequestError({
|
|
provider: PROVIDER,
|
|
method: "session/request_permission",
|
|
detail: `Unknown pending approval request: ${requestId}`,
|
|
});
|
|
}
|
|
yield* Deferred.succeed(pending.decision, decision);
|
|
});
|
|
|
|
const respondToUserInput: CursorAdapterShape["respondToUserInput"] = (
|
|
threadId,
|
|
requestId,
|
|
answers,
|
|
) =>
|
|
Effect.gen(function* () {
|
|
const ctx = yield* requireSession(threadId);
|
|
const pending = ctx.pendingUserInputs.get(requestId);
|
|
if (!pending) {
|
|
return yield* new ProviderAdapterRequestError({
|
|
provider: PROVIDER,
|
|
method: "cursor/ask_question",
|
|
detail: `Unknown pending user-input request: ${requestId}`,
|
|
});
|
|
}
|
|
yield* Deferred.succeed(pending.answers, answers);
|
|
});
|
|
|
|
const readThread: CursorAdapterShape["readThread"] = (threadId) =>
|
|
Effect.gen(function* () {
|
|
const ctx = yield* requireSession(threadId);
|
|
return { threadId, turns: ctx.turns };
|
|
});
|
|
|
|
const rollbackThread: CursorAdapterShape["rollbackThread"] = (threadId, numTurns) =>
|
|
Effect.gen(function* () {
|
|
const ctx = yield* requireSession(threadId);
|
|
if (!Number.isInteger(numTurns) || numTurns < 1) {
|
|
return yield* new ProviderAdapterValidationError({
|
|
provider: PROVIDER,
|
|
operation: "rollbackThread",
|
|
issue: "numTurns must be an integer >= 1.",
|
|
});
|
|
}
|
|
const nextLength = Math.max(0, ctx.turns.length - numTurns);
|
|
ctx.turns.splice(nextLength);
|
|
return { threadId, turns: ctx.turns };
|
|
});
|
|
|
|
const stopSession: CursorAdapterShape["stopSession"] = (threadId) =>
|
|
Effect.gen(function* () {
|
|
const ctx = yield* requireSession(threadId);
|
|
yield* stopSessionInternal(ctx);
|
|
});
|
|
|
|
const listSessions: CursorAdapterShape["listSessions"] = () =>
|
|
Effect.sync(() => Array.from(sessions.values(), (c) => ({ ...c.session })));
|
|
|
|
const hasSession: CursorAdapterShape["hasSession"] = (threadId) =>
|
|
Effect.sync(() => {
|
|
const c = sessions.get(threadId);
|
|
return c !== undefined && !c.stopped;
|
|
});
|
|
|
|
const stopAll: CursorAdapterShape["stopAll"] = () =>
|
|
Effect.forEach(sessions.values(), stopSessionInternal, { discard: true });
|
|
|
|
yield* Effect.addFinalizer(() =>
|
|
Effect.forEach(sessions.values(), stopSessionInternal, { discard: true }).pipe(
|
|
Effect.tap(() => Queue.shutdown(runtimeEventQueue)),
|
|
),
|
|
);
|
|
|
|
return {
|
|
provider: PROVIDER,
|
|
capabilities: { sessionModelSwitch: "in-session" },
|
|
startSession,
|
|
sendTurn,
|
|
interruptTurn,
|
|
readThread,
|
|
rollbackThread,
|
|
respondToRequest,
|
|
respondToUserInput,
|
|
stopSession,
|
|
listSessions,
|
|
hasSession,
|
|
stopAll,
|
|
streamEvents: Stream.fromQueue(runtimeEventQueue),
|
|
} satisfies CursorAdapterShape;
|
|
});
|
|
}
|
|
|
|
export const CursorAdapterLive = Layer.effect(CursorAdapter, makeCursorAdapter());
|
|
|
|
export function makeCursorAdapterLive(opts?: CursorAdapterLiveOptions) {
|
|
return Layer.effect(CursorAdapter, makeCursorAdapter(opts));
|
|
}
|