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

CursorDriver.ts

179 lines6.7 KB

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