Open History to see the last commit
1/**
2 * CursorDriver — `ProviderDriver` for the Cursor Agent (`cursor-agent`) runtime.
3 *
4 * Cursor exposes an ACP-based CLI. Model catalog and capability refreshes
5 * happen during the managed provider status check via Cursor's
6 * `list_available_models` extension method.
7 *
8 * Text generation is supported via the ACP runtime — `makeCursorTextGeneration`
9 * drives `runtime.prompt` with a structured-output schema and collects the
10 * agent's `agent_message_chunk` stream into a single JSON blob.
11 *
12 * @module provider/Drivers/CursorDriver
13 */
14import { CursorSettings, ProviderDriverKind } from "@t3tools/contracts";
15import * as Crypto from "effect/Crypto";
16import * as Effect from "effect/Effect";
17import * as FileSystem from "effect/FileSystem";
18import * as Path from "effect/Path";
19import * as Schema from "effect/Schema";
20import { HttpClient } from "effect/unstable/http";
21import { ChildProcessSpawner } from "effect/unstable/process";
22
23import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
24import { ServerConfig } from "../../config.ts";
25import { ServerSettingsService } from "../../serverSettings.ts";
26import { makeCursorTextGeneration } from "../../textGeneration/CursorTextGeneration.ts";
27import { ProviderDriverError } from "../Errors.ts";
28import { makeCursorAdapter } from "../Layers/CursorAdapter.ts";
29import { readCursorUsageLimits } from "../Layers/cursorUsageLimits.ts";
30import {
31 buildInitialCursorProviderSnapshot,
32 checkCursorProviderStatus,
33 makeCursorModelDiscovery,
34 enrichCursorSnapshot,
35 makeCursorCommandCatalog,
36} from "../Layers/CursorProvider.ts";
37import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
38import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
39import {
40 defaultProviderContinuationIdentity,
41 type ProviderDriver,
42 type ProviderInstance,
43} from "../ProviderDriver.ts";
44import { withInstanceIdentity } from "./instanceIdentity.ts";
45import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
46import {
47 makeCachedProviderMaintenanceResolution,
48 makeManualOnlyProviderMaintenanceCapabilities,
49 makeProviderMaintenanceCapabilities,
50 type ProviderMaintenanceCapabilitiesResolver,
51 resolveProviderMaintenanceCapabilitiesEffect,
52} from "../providerMaintenance.ts";
53import {
54 haveProviderSnapshotSettingsChanged,
55 makeProviderSnapshotSettingsSource,
56 type ProviderSnapshotSettings,
57} from "../providerUpdateSettings.ts";
58import { discoverCursorSkills, probeCursorSkills } from "./CursorSkills.ts";
59const decodeCursorSettings = Schema.decodeSync(CursorSettings);
60
61const DRIVER_KIND = ProviderDriverKind.make("cursor");
62// cursor-agent updates itself, so the resolved executable is its own updater.
63// No executable means nothing to update, not "whatever is on PATH".
64const UPDATE: ProviderMaintenanceCapabilitiesResolver = {
65 resolve: (context) =>
66 Effect.succeed(
67 context
68 ? makeProviderMaintenanceCapabilities({
69 provider: DRIVER_KIND,
70 packageName: null,
71 updateExecutable: context.resolvedCommandPath,
72 updateArgs: ["update"],
73 updateLockKey: "cursor-agent",
74 platform: context.platform,
75 })
76 : makeManualOnlyProviderMaintenanceCapabilities({
77 provider: DRIVER_KIND,
78 packageName: null,
79 }),
80 ),
81};
82
83export type CursorDriverEnv =
84 | BackgroundPolicy.BackgroundPolicy
85 | ChildProcessSpawner.ChildProcessSpawner
86 | Crypto.Crypto
87 | FileSystem.FileSystem
88 | HttpClient.HttpClient
89 | Path.Path
90 | ProviderEventLoggers
91 | ServerConfig
92 | ServerSettingsService;
93
94export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
95 driverKind: DRIVER_KIND,
96 metadata: {
97 displayName: "Cursor",
98 supportsMultipleInstances: true,
99 },
100 configSchema: CursorSettings,
101 defaultConfig: (): CursorSettings => decodeCursorSettings({}),
102 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
103 Effect.gen(function* () {
104 const crypto = yield* Crypto.Crypto;
105 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
106 const fileSystem = yield* FileSystem.FileSystem;
107 const path = yield* Path.Path;
108 const httpClient = yield* HttpClient.HttpClient;
109 const serverSettings = yield* ServerSettingsService;
110 const eventLoggers = yield* ProviderEventLoggers;
111 const processEnv = mergeProviderInstanceEnvironment(environment);
112 const continuationIdentity = defaultProviderContinuationIdentity({
113 driverKind: DRIVER_KIND,
114 instanceId,
115 });
116 const stampIdentity = withInstanceIdentity({
117 instanceId,
118 driverKind: DRIVER_KIND,
119 displayName,
120 accentColor,
121 continuationGroupKey: continuationIdentity.continuationKey,
122 });
123 const effectiveConfig = { ...config, enabled } satisfies CursorSettings;
124 const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
125 resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
126 binaryPath: effectiveConfig.binaryPath,
127 env: processEnv,
128 }).pipe(
129 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
130 Effect.provideService(FileSystem.FileSystem, fileSystem),
131 Effect.provideService(Path.Path, path),
132 ),
133 );
134
135 const textGeneration = yield* makeCursorTextGeneration(effectiveConfig, processEnv);
136
137 const modelDiscovery = yield* makeCursorModelDiscovery(effectiveConfig, processEnv);
138 const checkProvider = checkCursorProviderStatus(
139 effectiveConfig,
140 processEnv,
141 modelDiscovery.discover,
142 ).pipe(
143 Effect.filterOrElse(
144 (snapshot) =>
145 !(
146 effectiveConfig.enabled &&
147 snapshot.installed &&
148 snapshot.auth.status === "authenticated"
149 ),
150 (snapshot) =>
151 readCursorUsageLimits(effectiveConfig, processEnv).pipe(
152 Effect.map((usageLimits) => ({ ...snapshot, usageLimits })),
153 ),
154 ),
155 Effect.map(stampIdentity),
156 Effect.provideService(HttpClient.HttpClient, httpClient),
157 Effect.provideService(Crypto.Crypto, crypto),
158 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
159 Effect.provideService(FileSystem.FileSystem, fileSystem),
160 Effect.provideService(Path.Path, path),
161 );
162
163 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
164 const managedSnapshot = yield* makeManagedServerProvider<
165 ProviderSnapshotSettings<CursorSettings>
166 >({
167 resolveMaintenance,
168 getSettings: snapshotSettings.getSettings,
169 streamSettings: snapshotSettings.streamSettings,
170 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
171 initialSnapshot: (settings) =>
172 buildInitialCursorProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
173 checkProvider,
174 // Model catalog and capabilities come exclusively from Cursor's
175 // list_available_models extension method during provider checks.
176 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
177 resolveMaintenance().pipe(
178 Effect.flatMap((maintenanceCapabilities) =>
179 enrichCursorSnapshot({
180 settings: settings.provider,
181 snapshot: currentSnapshot,
182 maintenanceCapabilities,
183 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
184 publishSnapshot,
185 stampIdentity,
186 httpClient,
187 }),
188 ),
189 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
190 ),
191 }).pipe(
192 Effect.mapError(
193 (cause) =>
194 new ProviderDriverError({
195 driver: DRIVER_KIND,
196 instanceId,
197 detail: `Failed to build Cursor snapshot: ${cause.message ?? String(cause)}`,
198 cause,
199 }),
200 ),
201 );
202
203 const { snapshot, onAvailableCommands, snapshotForCwd } =
204 yield* makeCursorCommandCatalog(managedSnapshot);
205 const adapter = yield* makeCursorAdapter(effectiveConfig, {
206 environment: processEnv,
207 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
208 instanceId,
209 onAvailableCommands: (commands, cwd) =>
210 discoverCursorSkills(cwd, processEnv).pipe(
211 Effect.provideService(FileSystem.FileSystem, fileSystem),
212 Effect.provideService(Path.Path, path),
213 Effect.flatMap((skills) => onAvailableCommands(commands, cwd, skills)),
214 ),
215 });
216
217 return {
218 instanceId,
219 driverKind: DRIVER_KIND,
220 continuationIdentity,
221 displayName,
222 accentColor,
223 enabled,
224 snapshot,
225 invalidateCaches: modelDiscovery.invalidate,
226 snapshotForCwd: (cwd) =>
227 !effectiveConfig.enabled
228 ? snapshot.getSnapshot
229 : probeCursorSkills(cwd, processEnv).pipe(
230 Effect.provideService(FileSystem.FileSystem, fileSystem),
231 Effect.provideService(Path.Path, path),
232 Effect.mapError(
233 (cause) =>
234 new ProviderDriverError({
235 driver: DRIVER_KIND,
236 instanceId,
237 detail: `Failed to discover Cursor skills for '${cwd}'`,
238 cause,
239 }),
240 ),
241 Effect.flatMap((skills) => snapshotForCwd(cwd, skills)),
242 ),
243 adapter,
244 textGeneration,
245 } satisfies ProviderInstance;
246 }),
247};