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

CursorDriver.ts

193 lines7.6 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 {
30 buildInitialCursorProviderSnapshot,
31 checkCursorProviderStatus,
32 enrichCursorSnapshot,
33} from "../Layers/CursorProvider.ts";
34import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
35import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
36import {
37 defaultProviderContinuationIdentity,
38 type ProviderDriver,
39 type ProviderInstance,
40} from "../ProviderDriver.ts";
41import { withInstanceIdentity } from "./instanceIdentity.ts";
42import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
43import {
44 makeProviderMaintenanceCapabilities,
45 type ProviderMaintenanceCapabilitiesResolver,
46 resolveProviderMaintenanceCapabilitiesEffect,
47} from "../providerMaintenance.ts";
48import {
49 haveProviderSnapshotSettingsChanged,
50 makeProviderSnapshotSettingsSource,
51 type ProviderSnapshotSettings,
52} from "../providerUpdateSettings.ts";
53import { probeCursorSkills } from "./CursorSkills.ts";
54const decodeCursorSettings = Schema.decodeSync(CursorSettings);
55​
56const DRIVER_KIND = ProviderDriverKind.make("cursor");
57const UPDATE: ProviderMaintenanceCapabilitiesResolver = {
58 resolve: (options) =>
59 makeProviderMaintenanceCapabilities({
60 provider: DRIVER_KIND,
61 packageName: null,
62 updateExecutable: options?.binaryPath?.trim() || "cursor-agent",
63 updateArgs: ["update"],
64 updateLockKey: "cursor-agent",
65 }),
66};
67​
68export type CursorDriverEnv =
69 | BackgroundPolicy.BackgroundPolicy
70 | ChildProcessSpawner.ChildProcessSpawner
71 | Crypto.Crypto
72 | FileSystem.FileSystem
73 | HttpClient.HttpClient
74 | Path.Path
75 | ProviderEventLoggers
76 | ServerConfig
77 | ServerSettingsService;
78​
79export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
80 driverKind: DRIVER_KIND,
81 metadata: {
82 displayName: "Cursor",
83 supportsMultipleInstances: true,
84 },
85 configSchema: CursorSettings,
86 defaultConfig: (): CursorSettings => decodeCursorSettings({}),
87 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
88 Effect.gen(function* () {
89 const crypto = yield* Crypto.Crypto;
90 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
91 const fileSystem = yield* FileSystem.FileSystem;
92 const path = yield* Path.Path;
93 const httpClient = yield* HttpClient.HttpClient;
94 const serverSettings = yield* ServerSettingsService;
95 const eventLoggers = yield* ProviderEventLoggers;
96 const processEnv = mergeProviderInstanceEnvironment(environment);
97 const continuationIdentity = defaultProviderContinuationIdentity({
98 driverKind: DRIVER_KIND,
99 instanceId,
100 });
101 const stampIdentity = withInstanceIdentity({
102 instanceId,
103 driverKind: DRIVER_KIND,
104 displayName,
105 accentColor,
106 continuationGroupKey: continuationIdentity.continuationKey,
107 });
108 const effectiveConfig = { ...config, enabled } satisfies CursorSettings;
109 const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
110 binaryPath: effectiveConfig.binaryPath,
111 env: processEnv,
112 });
113​
114 const adapter = yield* makeCursorAdapter(effectiveConfig, {
115 environment: processEnv,
116 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
117 instanceId,
118 });
119 const textGeneration = yield* makeCursorTextGeneration(effectiveConfig, processEnv);
120​
121 const checkProvider = checkCursorProviderStatus(effectiveConfig, processEnv).pipe(
122 Effect.map(stampIdentity),
123 Effect.provideService(Crypto.Crypto, crypto),
124 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
125 Effect.provideService(FileSystem.FileSystem, fileSystem),
126 Effect.provideService(Path.Path, path),
127 );
128​
129 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
130 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<CursorSettings>>({
131 maintenanceCapabilities,
132 getSettings: snapshotSettings.getSettings,
133 streamSettings: snapshotSettings.streamSettings,
134 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
135 initialSnapshot: (settings) =>
136 buildInitialCursorProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
137 checkProvider,
138 // Model catalog and capabilities come exclusively from Cursor's
139 // list_available_models extension method during provider checks.
140 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
141 enrichCursorSnapshot({
142 settings: settings.provider,
143 snapshot: currentSnapshot,
144 maintenanceCapabilities,
145 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
146 publishSnapshot,
147 stampIdentity,
148 httpClient,
149 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)),
150 }).pipe(
151 Effect.mapError(
152 (cause) =>
153 new ProviderDriverError({
154 driver: DRIVER_KIND,
155 instanceId,
156 detail: `Failed to build Cursor snapshot: ${cause.message ?? String(cause)}`,
157 cause,
158 }),
159 ),
160 );
161​
162 return {
163 instanceId,
164 driverKind: DRIVER_KIND,
165 continuationIdentity,
166 displayName,
167 accentColor,
168 enabled,
169 snapshot,
170 snapshotForCwd: (cwd) =>
171 !effectiveConfig.enabled
172 ? snapshot.getSnapshot
173 : Effect.all([
174 snapshot.getSnapshot,
175 probeCursorSkills(cwd, processEnv).pipe(
176 Effect.provideService(FileSystem.FileSystem, fileSystem),
177 Effect.provideService(Path.Path, path),
178 Effect.mapError(
179 (cause) =>
180 new ProviderDriverError({
181 driver: DRIVER_KIND,
182 instanceId,
183 detail: `Failed to discover Cursor skills for '${cwd}'`,
184 cause,
185 }),
186 ),
187 ),
188 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills }))),
189 adapter,
190 textGeneration,
191 } satisfies ProviderInstance;
192 }),
193};