|
| 1 | +/** |
| 2 | + * CursorAdapter types and internal helpers. |
| 3 | + * |
| 4 | + * @module CursorAdapter.types |
| 5 | + */ |
| 6 | +import * as nodePath from "node:path"; |
| 7 | + |
| 8 | +import { |
| 9 | + ApprovalRequestId, |
| 10 | + type CursorModelOptions, |
| 11 | + EventId, |
| 12 | + type ProviderApprovalDecision, |
| 13 | + type ProviderInteractionMode, |
| 14 | + type ProviderSession, |
| 15 | + type ProviderUserInputAnswers, |
| 16 | + RuntimeRequestId, |
| 17 | + type RuntimeMode, |
| 18 | + type ThreadId, |
| 19 | + TurnId, |
| 20 | +} from "@bigcode/contracts"; |
| 21 | +import { |
| 22 | + DateTime, |
| 23 | + Deferred, |
| 24 | + Effect, |
| 25 | + Exit, |
| 26 | + Fiber, |
| 27 | + FileSystem, |
| 28 | + Layer, |
| 29 | + Option, |
| 30 | + PubSub, |
| 31 | + Random, |
| 32 | + Scope, |
| 33 | + Semaphore, |
| 34 | + Stream, |
| 35 | + SynchronizedRef, |
| 36 | +} from "effect"; |
| 37 | +import { ChildProcessSpawner } from "effect/unstable/process"; |
| 38 | +import type * as EffectAcpSchema from "effect-acp/schema"; |
| 39 | + |
| 40 | +import { resolveAttachmentPath } from "../../attachments/attachmentStore.ts"; |
| 41 | +import { ServerConfig } from "../../startup/config.ts"; |
| 42 | +import { ServerSettingsService } from "../../ws/serverSettings.ts"; |
| 43 | +import { |
| 44 | + ProviderAdapterProcessError, |
| 45 | + ProviderAdapterRequestError, |
| 46 | + ProviderAdapterSessionNotFoundError, |
| 47 | + ProviderAdapterValidationError, |
| 48 | +} from "../Errors.ts"; |
| 49 | +import { acpPermissionOutcome, mapAcpToAdapterError } from "../acp/AcpAdapterSupport.ts"; |
| 50 | +import { type AcpSessionRuntimeShape } from "../acp/AcpSessionRuntime.ts"; |
| 51 | +import { |
| 52 | + makeAcpAssistantItemEvent, |
| 53 | + makeAcpContentDeltaEvent, |
| 54 | + makeAcpPlanUpdatedEvent, |
| 55 | + makeAcpRequestOpenedEvent, |
| 56 | + makeAcpRequestResolvedEvent, |
| 57 | + makeAcpToolCallEvent, |
| 58 | +} from "../acp/AcpCoreRuntimeEvents.ts"; |
| 59 | +import { |
| 60 | + type AcpSessionMode, |
| 61 | + type AcpSessionModeState, |
| 62 | + parsePermissionRequest, |
| 63 | +} from "../acp/AcpRuntimeModel.ts"; |
| 64 | +import { makeAcpNativeLoggers } from "../acp/AcpNativeLogging.ts"; |
| 65 | +import { applyCursorAcpModelSelection, makeCursorAcpRuntime } from "../acp/CursorAcpSupport.ts"; |
| 66 | +import { |
| 67 | + CursorAskQuestionRequest, |
| 68 | + CursorCreatePlanRequest, |
| 69 | + CursorUpdateTodosRequest, |
| 70 | + extractAskQuestions, |
| 71 | + extractPlanMarkdown, |
| 72 | + extractTodosAsPlan, |
| 73 | +} from "../acp/CursorAcpExtension.ts"; |
| 74 | +import { CursorAdapter } from "../Services/CursorAdapter.ts"; |
| 75 | +import { resolveCursorAcpBaseModelId } from "./CursorProvider.ts"; |
| 76 | +import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts"; |
| 77 | + |
| 78 | +export const PROVIDER = "cursor" as const; |
| 79 | +export const CURSOR_RESUME_VERSION = 1 as const; |
| 80 | +export const ACP_PLAN_MODE_ALIASES = ["plan", "architect"]; |
| 81 | +export const ACP_IMPLEMENT_MODE_ALIASES = ["code", "agent", "default", "chat", "implement"]; |
| 82 | +export const ACP_APPROVAL_MODE_ALIASES = ["ask"]; |
| 83 | + |
| 84 | +export interface CursorAdapterLiveOptions { |
| 85 | + readonly nativeEventLogPath?: string; |
| 86 | + readonly nativeEventLogger?: EventNdjsonLogger; |
| 87 | +} |
| 88 | + |
| 89 | +export interface PendingApproval { |
| 90 | + readonly decision: Deferred.Deferred<ProviderApprovalDecision>; |
| 91 | + readonly kind: string | "unknown"; |
| 92 | +} |
| 93 | + |
| 94 | +export interface PendingUserInput { |
| 95 | + readonly answers: Deferred.Deferred<ProviderUserInputAnswers>; |
| 96 | +} |
| 97 | + |
| 98 | +export interface CursorSessionContext { |
| 99 | + readonly threadId: ThreadId; |
| 100 | + session: ProviderSession; |
| 101 | + readonly scope: Scope.Closeable; |
| 102 | + readonly acp: AcpSessionRuntimeShape; |
| 103 | + notificationFiber: Fiber.Fiber<void, never> | undefined; |
| 104 | + readonly pendingApprovals: Map<ApprovalRequestId, PendingApproval>; |
| 105 | + readonly pendingUserInputs: Map<ApprovalRequestId, PendingUserInput>; |
| 106 | + readonly turns: Array<{ id: TurnId; items: Array<unknown> }>; |
| 107 | + lastPlanFingerprint: string | undefined; |
| 108 | + activeTurnId: TurnId | undefined; |
| 109 | + stopped: boolean; |
| 110 | +} |
| 111 | + |
| 112 | +export function settlePendingApprovalsAsCancelled( |
| 113 | + pendingApprovals: ReadonlyMap<ApprovalRequestId, PendingApproval>, |
| 114 | +): Effect.Effect<void> { |
| 115 | + const pendingEntries = Array.from(pendingApprovals.values()); |
| 116 | + return Effect.forEach( |
| 117 | + pendingEntries, |
| 118 | + (pending) => Deferred.succeed(pending.decision, "cancel").pipe(Effect.ignore), |
| 119 | + { discard: true }, |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +export function settlePendingUserInputsAsEmptyAnswers( |
| 124 | + pendingUserInputs: ReadonlyMap<ApprovalRequestId, PendingUserInput>, |
| 125 | +): Effect.Effect<void> { |
| 126 | + const pendingEntries = Array.from(pendingUserInputs.values()); |
| 127 | + return Effect.forEach( |
| 128 | + pendingEntries, |
| 129 | + (pending) => Deferred.succeed(pending.answers, {}).pipe(Effect.ignore), |
| 130 | + { discard: true }, |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +export function isRecord(value: unknown): value is Record<string, unknown> { |
| 135 | + return typeof value === "object" && value !== null && !Array.isArray(value); |
| 136 | +} |
| 137 | + |
| 138 | +export function parseCursorResume(raw: unknown): { sessionId: string } | undefined { |
| 139 | + if (!isRecord(raw)) return undefined; |
| 140 | + if (raw.schemaVersion !== CURSOR_RESUME_VERSION) return undefined; |
| 141 | + if (typeof raw.sessionId !== "string" || !raw.sessionId.trim()) return undefined; |
| 142 | + return { sessionId: raw.sessionId.trim() }; |
| 143 | +} |
| 144 | + |
| 145 | +export function normalizeModeSearchText(mode: AcpSessionMode): string { |
| 146 | + return [mode.id, mode.name, mode.description] |
| 147 | + .filter((value): value is string => typeof value === "string" && value.length > 0) |
| 148 | + .join(" ") |
| 149 | + .toLowerCase() |
| 150 | + .replace(/[^a-z0-9]+/g, " ") |
| 151 | + .trim(); |
| 152 | +} |
| 153 | + |
| 154 | +export function findModeByAliases( |
| 155 | + modes: ReadonlyArray<AcpSessionMode>, |
| 156 | + aliases: ReadonlyArray<string>, |
| 157 | +): AcpSessionMode | undefined { |
| 158 | + const normalizedAliases = aliases.map((alias) => alias.toLowerCase()); |
| 159 | + for (const alias of normalizedAliases) { |
| 160 | + const exact = modes.find((mode) => { |
| 161 | + const id = mode.id.toLowerCase(); |
| 162 | + const name = mode.name.toLowerCase(); |
| 163 | + return id === alias || name === alias; |
| 164 | + }); |
| 165 | + if (exact) { |
| 166 | + return exact; |
| 167 | + } |
| 168 | + } |
| 169 | + for (const alias of normalizedAliases) { |
| 170 | + const partial = modes.find((mode) => normalizeModeSearchText(mode).includes(alias)); |
| 171 | + if (partial) { |
| 172 | + return partial; |
| 173 | + } |
| 174 | + } |
| 175 | + return undefined; |
| 176 | +} |
| 177 | + |
| 178 | +export function isPlanMode(mode: AcpSessionMode): boolean { |
| 179 | + return findModeByAliases([mode], ACP_PLAN_MODE_ALIASES) !== undefined; |
| 180 | +} |
| 181 | + |
| 182 | +export function resolveRequestedModeId(input: { |
| 183 | + readonly interactionMode: ProviderInteractionMode | undefined; |
| 184 | + readonly runtimeMode: RuntimeMode; |
| 185 | + readonly modeState: AcpSessionModeState | undefined; |
| 186 | +}): string | undefined { |
| 187 | + const modeState = input.modeState; |
| 188 | + if (!modeState) { |
| 189 | + return undefined; |
| 190 | + } |
| 191 | + |
| 192 | + if (input.interactionMode === "plan") { |
| 193 | + return findModeByAliases(modeState.availableModes, ACP_PLAN_MODE_ALIASES)?.id; |
| 194 | + } |
| 195 | + |
| 196 | + if (input.runtimeMode === "approval-required") { |
| 197 | + return ( |
| 198 | + findModeByAliases(modeState.availableModes, ACP_APPROVAL_MODE_ALIASES)?.id ?? |
| 199 | + findModeByAliases(modeState.availableModes, ACP_IMPLEMENT_MODE_ALIASES)?.id ?? |
| 200 | + modeState.availableModes.find((mode) => !isPlanMode(mode))?.id ?? |
| 201 | + modeState.currentModeId |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + return ( |
| 206 | + findModeByAliases(modeState.availableModes, ACP_IMPLEMENT_MODE_ALIASES)?.id ?? |
| 207 | + findModeByAliases(modeState.availableModes, ACP_APPROVAL_MODE_ALIASES)?.id ?? |
| 208 | + modeState.availableModes.find((mode) => !isPlanMode(mode))?.id ?? |
| 209 | + modeState.currentModeId |
| 210 | + ); |
| 211 | +} |
| 212 | + |
| 213 | +export function applyRequestedSessionConfiguration<E>(input: { |
| 214 | + readonly runtime: AcpSessionRuntimeShape; |
| 215 | + readonly runtimeMode: RuntimeMode; |
| 216 | + readonly interactionMode: ProviderInteractionMode | undefined; |
| 217 | + readonly modelSelection: |
| 218 | + | { |
| 219 | + readonly model: string; |
| 220 | + readonly options?: CursorModelOptions | null | undefined; |
| 221 | + } |
| 222 | + | undefined; |
| 223 | + readonly mapError: (context: { |
| 224 | + readonly cause: import("effect-acp/errors").AcpError; |
| 225 | + readonly method: "session/set_config_option" | "session/set_mode"; |
| 226 | + }) => E; |
| 227 | +}): Effect.Effect<void, E> { |
| 228 | + return Effect.gen(function* () { |
| 229 | + if (input.modelSelection) { |
| 230 | + yield* applyCursorAcpModelSelection({ |
| 231 | + runtime: input.runtime, |
| 232 | + model: input.modelSelection.model, |
| 233 | + modelOptions: input.modelSelection.options, |
| 234 | + mapError: ({ cause }) => |
| 235 | + input.mapError({ |
| 236 | + cause, |
| 237 | + method: "session/set_config_option", |
| 238 | + }), |
| 239 | + }); |
| 240 | + } |
| 241 | + |
| 242 | + const requestedModeId = resolveRequestedModeId({ |
| 243 | + interactionMode: input.interactionMode, |
| 244 | + runtimeMode: input.runtimeMode, |
| 245 | + modeState: yield* input.runtime.getModeState, |
| 246 | + }); |
| 247 | + if (!requestedModeId) { |
| 248 | + return; |
| 249 | + } |
| 250 | + |
| 251 | + yield* input.runtime.setMode(requestedModeId).pipe( |
| 252 | + Effect.mapError((cause) => |
| 253 | + input.mapError({ |
| 254 | + cause, |
| 255 | + method: "session/set_mode", |
| 256 | + }), |
| 257 | + ), |
| 258 | + ); |
| 259 | + }); |
| 260 | +} |
| 261 | + |
| 262 | +export function selectAutoApprovedPermissionOption( |
| 263 | + request: EffectAcpSchema.RequestPermissionRequest, |
| 264 | +): string | undefined { |
| 265 | + const allowAlwaysOption = request.options.find((option) => option.kind === "allow_always"); |
| 266 | + if (typeof allowAlwaysOption?.optionId === "string" && allowAlwaysOption.optionId.trim()) { |
| 267 | + return allowAlwaysOption.optionId.trim(); |
| 268 | + } |
| 269 | + |
| 270 | + const allowOnceOption = request.options.find((option) => option.kind === "allow_once"); |
| 271 | + if (typeof allowOnceOption?.optionId === "string" && allowOnceOption.optionId.trim()) { |
| 272 | + return allowOnceOption.optionId.trim(); |
| 273 | + } |
| 274 | + |
| 275 | + return undefined; |
| 276 | +} |
| 277 | + |
| 278 | +// Re-export everything needed by CursorAdapter.ts |
| 279 | +export { |
| 280 | + nodePath, |
| 281 | + ApprovalRequestId, |
| 282 | + EventId, |
| 283 | + RuntimeRequestId, |
| 284 | + TurnId, |
| 285 | + DateTime, |
| 286 | + Deferred, |
| 287 | + Effect, |
| 288 | + Exit, |
| 289 | + Fiber, |
| 290 | + FileSystem, |
| 291 | + Layer, |
| 292 | + Option, |
| 293 | + PubSub, |
| 294 | + Random, |
| 295 | + Scope, |
| 296 | + Semaphore, |
| 297 | + Stream, |
| 298 | + SynchronizedRef, |
| 299 | + ChildProcessSpawner, |
| 300 | + resolveAttachmentPath, |
| 301 | + ServerConfig, |
| 302 | + ServerSettingsService, |
| 303 | + ProviderAdapterProcessError, |
| 304 | + ProviderAdapterRequestError, |
| 305 | + ProviderAdapterSessionNotFoundError, |
| 306 | + ProviderAdapterValidationError, |
| 307 | + acpPermissionOutcome, |
| 308 | + mapAcpToAdapterError, |
| 309 | + makeAcpAssistantItemEvent, |
| 310 | + makeAcpContentDeltaEvent, |
| 311 | + makeAcpPlanUpdatedEvent, |
| 312 | + makeAcpRequestOpenedEvent, |
| 313 | + makeAcpRequestResolvedEvent, |
| 314 | + makeAcpToolCallEvent, |
| 315 | + parsePermissionRequest, |
| 316 | + makeAcpNativeLoggers, |
| 317 | + applyCursorAcpModelSelection, |
| 318 | + makeCursorAcpRuntime, |
| 319 | + CursorAskQuestionRequest, |
| 320 | + CursorCreatePlanRequest, |
| 321 | + CursorUpdateTodosRequest, |
| 322 | + extractAskQuestions, |
| 323 | + extractPlanMarkdown, |
| 324 | + extractTodosAsPlan, |
| 325 | + CursorAdapter, |
| 326 | + resolveCursorAcpBaseModelId, |
| 327 | + makeEventNdjsonLogger, |
| 328 | +}; |
0 commit comments