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

CursorDriver.ts

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