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