Open History to see the last commit
1/**
2 * CursorDriver — `ProviderDriver` for the Cursor Agent SDK runtime.
3 *
4 * Provider status, model discovery, orchestration, and text generation use the
5 * official Cursor SDK with an instance browser login or CURSOR_API_KEY.
6 *
7 * @module provider/Drivers/CursorDriver
8 */
9import { ProviderDriverKind, ProviderSetupError } from "@t3tools/contracts";
10import { CursorSettings } from "../settings.ts";
11import * as Effect from "effect/Effect";
12import * as Crypto from "effect/Crypto";
13import * as FileSystem from "effect/FileSystem";
14import * as Path from "effect/Path";
15import * as Schema from "effect/Schema";
16import * as HttpClient from "effect/http/HttpClient";
17import { readCursorUsageLimits } from "./usageLimits.ts";
18
19import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
20import { makeCursorTextGeneration } from "./textGeneration.ts";
21import { CursorAdapterV2Driver, type CursorAdapterV2DriverEnv } from "./adapter.ts";
22import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
23import { buildInitialCursorProviderSnapshot, checkCursorProviderStatus } from "./status.ts";
24import * as CursorSdkCatalog from "./CursorSdkCatalog.ts";
25import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider";
26import {
27 defaultProviderContinuationIdentity,
28 type ProviderDriver,
29 type ProviderInstance,
30} from "@t3tools/provider-core/server/driver";
31import { withInstanceIdentity } from "@t3tools/provider-core/server/instanceIdentity";
32import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
33import { makeManualOnlyProviderMaintenanceCapabilities } from "@t3tools/provider-core/server/maintenanceResolver";
34import {
35 haveProviderSnapshotSettingsChanged,
36 makeProviderSnapshotSettingsSource,
37 type ProviderSnapshotSettings,
38} from "@t3tools/provider-core/server/snapshotSettings";
39import { probeCursorSkills } from "./skills.ts";
40import { makeCursorAuth } from "./auth.ts";
41import * as CursorCredentialStore from "./credentialStore.ts";
42import * as CursorAgentSdk from "./CursorAgentSdk.ts";
43import * as CursorSdk from "./CursorSdk.ts";
44const decodeCursorSettings = Schema.decodeSync(CursorSettings);
45const isSdkRunnerError = Schema.is(CursorAgentSdk.CursorAgentSdkRunnerError);
46
47const DRIVER_KIND = ProviderDriverKind.make("cursor");
48const MAINTENANCE_CAPABILITIES = makeManualOnlyProviderMaintenanceCapabilities({
49 provider: DRIVER_KIND,
50 packageName: null,
51});
52
53export type CursorDriverEnv =
54 | CursorAdapterV2DriverEnv
55 | Crypto.Crypto
56 | FileSystem.FileSystem
57 | Path.Path
58 | HttpClient.HttpClient
59 | CursorSdk.CursorSdk
60 | ProviderHost.ProviderHost;
61
62export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
63 driverKind: DRIVER_KIND,
64 metadata: {
65 displayName: "Cursor",
66 supportsMultipleInstances: true,
67 },
68 configSchema: CursorSettings,
69 defaultConfig: (): CursorSettings => decodeCursorSettings({}),
70 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
71 Effect.gen(function* () {
72 const host = yield* ProviderHost.ProviderHost;
73 const fileSystem = yield* FileSystem.FileSystem;
74 const path = yield* Path.Path;
75 const httpClient = yield* HttpClient.HttpClient;
76 const sdkRunner = yield* CursorAgentSdk.CursorAgentSdkRunner;
77 const processEnv = mergeProviderInstanceEnvironment(environment);
78 const continuationIdentity = defaultProviderContinuationIdentity({
79 driverKind: DRIVER_KIND,
80 instanceId,
81 });
82 const stampIdentity = withInstanceIdentity({
83 instanceId,
84 driverKind: DRIVER_KIND,
85 displayName: displayName ?? "Cursor",
86 accentColor,
87 continuationGroupKey: continuationIdentity.continuationKey,
88 });
89 const effectiveConfig = { ...config, enabled } satisfies CursorSettings;
90 const credentials = yield* CursorCredentialStore.makeCursorCredentialStore(
91 instanceId,
92 path.join(
93 host.paths.stateDir,
94 "provider-auth",
95 encodeURIComponent(instanceId),
96 "cursor.json",
97 ),
98 ).pipe(
99 Effect.mapError(
100 (cause) =>
101 new ProviderDriverError({
102 driver: DRIVER_KIND,
103 instanceId,
104 detail: "Could not open the Cursor credential store.",
105 cause,
106 }),
107 ),
108 );
109 const auth = yield* makeCursorAuth({
110 instanceId,
111 displayName: displayName ?? "Cursor",
112 enabled,
113 ...(processEnv.CURSOR_API_KEY ? { apiKey: processEnv.CURSOR_API_KEY } : {}),
114 store: credentials.store,
115 credentialBinding: credentials.binding,
116 onChanged: (signedIn): Effect.Effect<void, ProviderSetupError> =>
117 snapshot.refresh.pipe(
118 Effect.flatMap((provider) =>
119 !signedIn || provider.auth.status === "authenticated"
120 ? Effect.void
121 : Effect.fail(
122 new ProviderSetupError({
123 instanceId,
124 operation: "start",
125 detail: provider.message ?? "Could not verify the Cursor sign-in. Try again.",
126 }),
127 ),
128 ),
129 ),
130 });
131 const stampSnapshot: typeof stampIdentity = (draft) =>
132 stampIdentity({
133 ...draft,
134 setup: { canAuthenticate: !auth.usesApiKey, canInstall: false },
135 auth: {
136 ...draft.auth,
137 canLogout: !auth.usesApiKey,
138 },
139 });
140
141 const orchestrationAdapter = yield* CursorAdapterV2Driver.create({
142 instanceId,
143 displayName,
144 accentColor,
145 environment,
146 enabled,
147 config,
148 }).pipe(
149 Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {
150 ...sdkRunner,
151 open: (input) =>
152 auth.requireApiKey.pipe(
153 Effect.flatMap((apiKey) =>
154 Effect.acquireRelease(
155 sdkRunner
156 .open({ ...input, options: { ...input.options, apiKey } })
157 .pipe(
158 Effect.flatMap((session) =>
159 Effect.cached(session.close).pipe(
160 Effect.map((close) => ({ ...session, close })),
161 ),
162 ),
163 ),
164 (session) => session.close.pipe(Effect.ignore),
165 ),
166 ),
167 auth.withAccess,
168 Effect.mapError((cause) =>
169 isSdkRunnerError(cause)
170 ? cause
171 : new CursorAgentSdk.CursorAgentSdkRunnerError({ method: "open", cause }),
172 ),
173 ),
174 }),
175 Effect.mapError(
176 (cause) =>
177 new ProviderDriverError({
178 driver: DRIVER_KIND,
179 instanceId,
180 detail: "Failed to build Cursor orchestration adapter.",
181 cause,
182 }),
183 ),
184 );
185 const textGeneration = yield* makeCursorTextGeneration(
186 effectiveConfig,
187 processEnv,
188 auth.requireApiKey,
189 auth.withAccess,
190 );
191
192 const checkProvider = auth.readApiKey.pipe(
193 Effect.orElseSucceed(() => undefined),
194 Effect.flatMap((apiKey) =>
195 checkCursorProviderStatus(
196 effectiveConfig,
197 {
198 ...processEnv,
199 CURSOR_API_KEY: apiKey,
200 },
201 auth.usesApiKey ? "api-key" : "browser",
202 ).pipe(
203 Effect.flatMap((snapshot) =>
204 effectiveConfig.enabled &&
205 snapshot.installed &&
206 snapshot.auth.status === "authenticated"
207 ? host.settings.get.pipe(
208 Effect.flatMap((settings) =>
209 readCursorUsageLimits(
210 effectiveConfig,
211 { ...processEnv, CURSOR_API_KEY: apiKey },
212 settings.cursorKeychainUsageEnabled,
213 ),
214 ),
215 Effect.map((usageLimits) => ({ ...snapshot, usageLimits })),
216 )
217 : Effect.succeed(snapshot),
218 ),
219 ),
220 ),
221 Effect.provideService(HttpClient.HttpClient, httpClient),
222 Effect.provideService(FileSystem.FileSystem, fileSystem),
223 Effect.provideService(Path.Path, path),
224 Effect.map(stampSnapshot),
225 Effect.provide(CursorSdkCatalog.layer),
226 );
227
228 const snapshotSettings = yield* makeProviderSnapshotSettingsSource(effectiveConfig);
229 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<CursorSettings>>({
230 resolveMaintenance: () => Effect.succeed(MAINTENANCE_CAPABILITIES),
231 getSettings: snapshotSettings.getSettings,
232 streamSettings: snapshotSettings.streamSettings,
233 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
234 initialSnapshot: (settings) =>
235 buildInitialCursorProviderSnapshot(settings.provider).pipe(Effect.map(stampSnapshot)),
236 checkProvider,
237 }).pipe(
238 Effect.mapError(
239 (cause) =>
240 new ProviderDriverError({
241 driver: DRIVER_KIND,
242 instanceId,
243 detail: "Failed to build Cursor snapshot.",
244 cause,
245 }),
246 ),
247 );
248
249 return {
250 instanceId,
251 driverKind: DRIVER_KIND,
252 continuationIdentity,
253 displayName,
254 accentColor,
255 enabled,
256 auth: auth.controller,
257 snapshot,
258 snapshotForCwd: (cwd) =>
259 !effectiveConfig.enabled
260 ? snapshot.getSnapshot
261 : Effect.all([
262 snapshot.getSnapshot,
263 probeCursorSkills(cwd, processEnv).pipe(
264 Effect.provideService(FileSystem.FileSystem, fileSystem),
265 Effect.provideService(Path.Path, path),
266 Effect.mapError(
267 (cause) =>
268 new ProviderDriverError({
269 driver: DRIVER_KIND,
270 instanceId,
271 detail: `Failed to discover Cursor skills for '${cwd}'`,
272 cause,
273 }),
274 ),
275 ),
276 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills }))),
277 orchestrationAdapter,
278 textGeneration,
279 } satisfies ProviderInstance;
280 }),
281};