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 and require CURSOR_API_KEY in the provider instance
6 * environment.
7 *
8 * @module provider/Drivers/CursorDriver
9 */
10import { CursorSettings, ProviderDriverKind } from "@t3tools/contracts";
11import * as Effect from "effect/Effect";
12import * as FileSystem from "effect/FileSystem";
13import * as Path from "effect/Path";
14import * as Schema from "effect/Schema";
15
16import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
17import { ServerConfig } from "../../config.ts";
18import { ServerSettingsService } from "../../serverSettings.ts";
19import { makeCursorTextGeneration } from "../../textGeneration/CursorTextGeneration.ts";
20import {
21 CursorAdapterV2Driver,
22 type CursorAdapterV2DriverEnv,
23} from "../../orchestration-v2/Adapters/CursorAdapterV2.ts";
24import { ProviderDriverError } from "../Errors.ts";
25import {
26 buildInitialCursorProviderSnapshot,
27 checkCursorProviderStatus,
28} from "../Layers/CursorProvider.ts";
29import { CursorSdkCatalogLive } from "../Layers/CursorSdkCatalog.ts";
30import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
31import {
32 defaultProviderContinuationIdentity,
33 type ProviderDriver,
34 type ProviderInstance,
35} from "../ProviderDriver.ts";
36import { withInstanceIdentity } from "./instanceIdentity.ts";
37import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
38import { makeManualOnlyProviderMaintenanceCapabilities } from "../providerMaintenance.ts";
39import {
40 haveProviderSnapshotSettingsChanged,
41 makeProviderSnapshotSettingsSource,
42 type ProviderSnapshotSettings,
43} from "../providerUpdateSettings.ts";
44import { probeCursorSkills } from "./CursorSkills.ts";
45const decodeCursorSettings = Schema.decodeSync(CursorSettings);
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 | FileSystem.FileSystem
56 | Path.Path
57 | BackgroundPolicy.BackgroundPolicy
58 | ServerConfig
59 | ServerSettingsService;
60
61export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
62 driverKind: DRIVER_KIND,
63 metadata: {
64 displayName: "Cursor",
65 supportsMultipleInstances: true,
66 },
67 configSchema: CursorSettings,
68 defaultConfig: (): CursorSettings => decodeCursorSettings({}),
69 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
70 Effect.gen(function* () {
71 const serverSettings = yield* ServerSettingsService;
72 const fileSystem = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const processEnv = mergeProviderInstanceEnvironment(environment);
75 const continuationIdentity = defaultProviderContinuationIdentity({
76 driverKind: DRIVER_KIND,
77 instanceId,
78 });
79 const stampIdentity = withInstanceIdentity({
80 instanceId,
81 driverKind: DRIVER_KIND,
82 displayName,
83 accentColor,
84 continuationGroupKey: continuationIdentity.continuationKey,
85 });
86 const effectiveConfig = { ...config, enabled } satisfies CursorSettings;
87
88 const orchestrationAdapter = yield* CursorAdapterV2Driver.create({
89 instanceId,
90 displayName,
91 accentColor,
92 environment,
93 enabled,
94 config,
95 }).pipe(
96 Effect.mapError(
97 (cause) =>
98 new ProviderDriverError({
99 driver: DRIVER_KIND,
100 instanceId,
101 detail: "Failed to build Cursor orchestration adapter.",
102 cause,
103 }),
104 ),
105 );
106 const textGeneration = yield* makeCursorTextGeneration(effectiveConfig, processEnv);
107
108 const checkProvider = checkCursorProviderStatus(effectiveConfig, processEnv).pipe(
109 Effect.map(stampIdentity),
110 Effect.provide(CursorSdkCatalogLive),
111 );
112
113 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
114 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<CursorSettings>>({
115 resolveMaintenance: () => Effect.succeed(MAINTENANCE_CAPABILITIES),
116 getSettings: snapshotSettings.getSettings,
117 streamSettings: snapshotSettings.streamSettings,
118 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
119 initialSnapshot: (settings) =>
120 buildInitialCursorProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
121 checkProvider,
122 }).pipe(
123 Effect.mapError(
124 (cause) =>
125 new ProviderDriverError({
126 driver: DRIVER_KIND,
127 instanceId,
128 detail: `Failed to build Cursor snapshot: ${cause.message ?? String(cause)}`,
129 cause,
130 }),
131 ),
132 );
133
134 return {
135 instanceId,
136 driverKind: DRIVER_KIND,
137 continuationIdentity,
138 displayName,
139 accentColor,
140 enabled,
141 snapshot,
142 snapshotForCwd: (cwd) =>
143 !effectiveConfig.enabled
144 ? snapshot.getSnapshot
145 : Effect.all([
146 snapshot.getSnapshot,
147 probeCursorSkills(cwd, processEnv).pipe(
148 Effect.provideService(FileSystem.FileSystem, fileSystem),
149 Effect.provideService(Path.Path, path),
150 Effect.mapError(
151 (cause) =>
152 new ProviderDriverError({
153 driver: DRIVER_KIND,
154 instanceId,
155 detail: `Failed to discover Cursor skills for '${cwd}'`,
156 cause,
157 }),
158 ),
159 ),
160 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills }))),
161 orchestrationAdapter,
162 textGeneration,
163 } satisfies ProviderInstance;
164 }),
165};