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 {
30 buildInitialCursorProviderSnapshot,
31 checkCursorProviderStatus,
32 enrichCursorSnapshot,
33} from "../Layers/CursorProvider.ts";
34import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
35import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
36import {
37 defaultProviderContinuationIdentity,
38 type ProviderDriver,
39 type ProviderInstance,
40} from "../ProviderDriver.ts";
41import { withInstanceIdentity } from "./instanceIdentity.ts";
42import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
43import {
44 makeCachedProviderMaintenanceResolution,
45 makeManualOnlyProviderMaintenanceCapabilities,
46 makeProviderMaintenanceCapabilities,
47 type ProviderMaintenanceCapabilitiesResolver,
48 resolveProviderMaintenanceCapabilitiesEffect,
49} from "../providerMaintenance.ts";
50import {
51 haveProviderSnapshotSettingsChanged,
52 makeProviderSnapshotSettingsSource,
53 type ProviderSnapshotSettings,
54} from "../providerUpdateSettings.ts";
55import { probeCursorSkills } from "./CursorSkills.ts";
56const decodeCursorSettings = Schema.decodeSync(CursorSettings);
57
58const DRIVER_KIND = ProviderDriverKind.make("cursor");
59// cursor-agent updates itself, so the resolved executable is its own updater.
60// No executable means nothing to update, not "whatever is on PATH".
61const UPDATE: ProviderMaintenanceCapabilitiesResolver = {
62 resolve: (context) =>
63 Effect.succeed(
64 context
65 ? makeProviderMaintenanceCapabilities({
66 provider: DRIVER_KIND,
67 packageName: null,
68 updateExecutable: context.resolvedCommandPath,
69 updateArgs: ["update"],
70 updateLockKey: "cursor-agent",
71 platform: context.platform,
72 })
73 : makeManualOnlyProviderMaintenanceCapabilities({
74 provider: DRIVER_KIND,
75 packageName: null,
76 }),
77 ),
78};
79
80export type CursorDriverEnv =
81 | BackgroundPolicy.BackgroundPolicy
82 | ChildProcessSpawner.ChildProcessSpawner
83 | Crypto.Crypto
84 | FileSystem.FileSystem
85 | HttpClient.HttpClient
86 | Path.Path
87 | ProviderEventLoggers
88 | ServerConfig
89 | ServerSettingsService;
90
91export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
92 driverKind: DRIVER_KIND,
93 metadata: {
94 displayName: "Cursor",
95 supportsMultipleInstances: true,
96 },
97 configSchema: CursorSettings,
98 defaultConfig: (): CursorSettings => decodeCursorSettings({}),
99 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
100 Effect.gen(function* () {
101 const crypto = yield* Crypto.Crypto;
102 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
103 const fileSystem = yield* FileSystem.FileSystem;
104 const path = yield* Path.Path;
105 const httpClient = yield* HttpClient.HttpClient;
106 const serverSettings = yield* ServerSettingsService;
107 const eventLoggers = yield* ProviderEventLoggers;
108 const processEnv = mergeProviderInstanceEnvironment(environment);
109 const continuationIdentity = defaultProviderContinuationIdentity({
110 driverKind: DRIVER_KIND,
111 instanceId,
112 });
113 const stampIdentity = withInstanceIdentity({
114 instanceId,
115 driverKind: DRIVER_KIND,
116 displayName,
117 accentColor,
118 continuationGroupKey: continuationIdentity.continuationKey,
119 });
120 const effectiveConfig = { ...config, enabled } satisfies CursorSettings;
121 const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
122 resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
123 binaryPath: effectiveConfig.binaryPath,
124 env: processEnv,
125 }).pipe(
126 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
127 Effect.provideService(FileSystem.FileSystem, fileSystem),
128 Effect.provideService(Path.Path, path),
129 ),
130 );
131
132 const adapter = yield* makeCursorAdapter(effectiveConfig, {
133 environment: processEnv,
134 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
135 instanceId,
136 });
137 const textGeneration = yield* makeCursorTextGeneration(effectiveConfig, processEnv);
138
139 const checkProvider = checkCursorProviderStatus(effectiveConfig, processEnv).pipe(
140 Effect.map(stampIdentity),
141 Effect.provideService(Crypto.Crypto, crypto),
142 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
143 Effect.provideService(FileSystem.FileSystem, fileSystem),
144 Effect.provideService(Path.Path, path),
145 );
146
147 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
148 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<CursorSettings>>({
149 resolveMaintenance,
150 getSettings: snapshotSettings.getSettings,
151 streamSettings: snapshotSettings.streamSettings,
152 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
153 initialSnapshot: (settings) =>
154 buildInitialCursorProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
155 checkProvider,
156 // Model catalog and capabilities come exclusively from Cursor's
157 // list_available_models extension method during provider checks.
158 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
159 resolveMaintenance().pipe(
160 Effect.flatMap((maintenanceCapabilities) =>
161 enrichCursorSnapshot({
162 settings: settings.provider,
163 snapshot: currentSnapshot,
164 maintenanceCapabilities,
165 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
166 publishSnapshot,
167 stampIdentity,
168 httpClient,
169 }),
170 ),
171 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
172 ),
173 }).pipe(
174 Effect.mapError(
175 (cause) =>
176 new ProviderDriverError({
177 driver: DRIVER_KIND,
178 instanceId,
179 detail: `Failed to build Cursor snapshot: ${cause.message ?? String(cause)}`,
180 cause,
181 }),
182 ),
183 );
184
185 return {
186 instanceId,
187 driverKind: DRIVER_KIND,
188 continuationIdentity,
189 displayName,
190 accentColor,
191 enabled,
192 snapshot,
193 snapshotForCwd: (cwd) =>
194 !effectiveConfig.enabled
195 ? snapshot.getSnapshot
196 : Effect.all([
197 snapshot.getSnapshot,
198 probeCursorSkills(cwd, processEnv).pipe(
199 Effect.provideService(FileSystem.FileSystem, fileSystem),
200 Effect.provideService(Path.Path, path),
201 Effect.mapError(
202 (cause) =>
203 new ProviderDriverError({
204 driver: DRIVER_KIND,
205 instanceId,
206 detail: `Failed to discover Cursor skills for '${cwd}'`,
207 cause,
208 }),
209 ),
210 ),
211 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills }))),
212 adapter,
213 textGeneration,
214 } satisfies ProviderInstance;
215 }),
216};