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

CursorDriver.ts

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