Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CursorDriver.ts

253 lines9.8 KB

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 Effect.gen(function* () {
152 const settings = yield* serverSettings.getSettings;
153 const usageLimits = yield* readCursorUsageLimits(
154 effectiveConfig,
155 processEnv,
156 settings.cursorKeychainUsageEnabled,
157 );
158 return { ...snapshot, usageLimits };
159 }),
160 ),
161 Effect.map(stampIdentity),
162 Effect.provideService(HttpClient.HttpClient, httpClient),
163 Effect.provideService(Crypto.Crypto, crypto),
164 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
165 Effect.provideService(FileSystem.FileSystem, fileSystem),
166 Effect.provideService(Path.Path, path),
167 );
168​
169 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
170 const managedSnapshot = yield* makeManagedServerProvider<
171 ProviderSnapshotSettings<CursorSettings>
172 >({
173 resolveMaintenance,
174 getSettings: snapshotSettings.getSettings,
175 streamSettings: snapshotSettings.streamSettings,
176 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
177 initialSnapshot: (settings) =>
178 buildInitialCursorProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
179 checkProvider,
180 // Model catalog and capabilities come exclusively from Cursor's
181 // list_available_models extension method during provider checks.
182 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
183 resolveMaintenance().pipe(
184 Effect.flatMap((maintenanceCapabilities) =>
185 enrichCursorSnapshot({
186 settings: settings.provider,
187 snapshot: currentSnapshot,
188 maintenanceCapabilities,
189 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
190 publishSnapshot,
191 stampIdentity,
192 httpClient,
193 }),
194 ),
195 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
196 ),
197 }).pipe(
198 Effect.mapError(
199 (cause) =>
200 new ProviderDriverError({
201 driver: DRIVER_KIND,
202 instanceId,
203 detail: `Failed to build Cursor snapshot: ${cause.message ?? String(cause)}`,
204 cause,
205 }),
206 ),
207 );
208​
209 const { snapshot, onAvailableCommands, snapshotForCwd } =
210 yield* makeCursorCommandCatalog(managedSnapshot);
211 const adapter = yield* makeCursorAdapter(effectiveConfig, {
212 environment: processEnv,
213 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
214 instanceId,
215 onAvailableCommands: (commands, cwd) =>
216 discoverCursorSkills(cwd, processEnv).pipe(
217 Effect.provideService(FileSystem.FileSystem, fileSystem),
218 Effect.provideService(Path.Path, path),
219 Effect.flatMap((skills) => onAvailableCommands(commands, cwd, skills)),
220 ),
221 });
222​
223 return {
224 instanceId,
225 driverKind: DRIVER_KIND,
226 continuationIdentity,
227 displayName,
228 accentColor,
229 enabled,
230 snapshot,
231 invalidateCaches: modelDiscovery.invalidate,
232 snapshotForCwd: (cwd) =>
233 !effectiveConfig.enabled
234 ? snapshot.getSnapshot
235 : probeCursorSkills(cwd, processEnv).pipe(
236 Effect.provideService(FileSystem.FileSystem, fileSystem),
237 Effect.provideService(Path.Path, path),
238 Effect.mapError(
239 (cause) =>
240 new ProviderDriverError({
241 driver: DRIVER_KIND,
242 instanceId,
243 detail: `Failed to discover Cursor skills for '${cwd}'`,
244 cause,
245 }),
246 ),
247 Effect.flatMap((skills) => snapshotForCwd(cwd, skills)),
248 ),
249 adapter,
250 textGeneration,
251 } satisfies ProviderInstance;
252 }),
253};