Open History to see the last commit
1/**
2 * CursorDriver — `ProviderDriver` for the Cursor Agent (`agent`) runtime.
3 *
4 * Cursor exposes an ACP-based CLI. The driver is still a plain value, but
5 * its snapshot uses `makeManagedServerProvider`'s optional `enrichSnapshot`
6 * hook to run the slow ACP model-capability probe in the background without
7 * blocking the initial `ready`-state publish.
8 *
9 * Text generation is supported via the ACP runtime — `makeCursorTextGeneration`
10 * drives `runtime.prompt` with a structured-output schema and collects the
11 * agent's `agent_message_chunk` stream into a single JSON blob.
12 *
13 * @module provider/Drivers/CursorDriver
14 */
15import { CursorSettings, ProviderDriverKind, type ServerProvider } from "@t3tools/contracts";
16import * as Duration from "effect/Duration";
17import * as Crypto from "effect/Crypto";
18import * as Effect from "effect/Effect";
19import * as FileSystem from "effect/FileSystem";
20import * as Path from "effect/Path";
21import * as Schema from "effect/Schema";
22import * as Stream from "effect/Stream";
23import { HttpClient } from "effect/unstable/http";
24import { ChildProcessSpawner } from "effect/unstable/process";
25
26import { ServerConfig } from "../../config.ts";
27import { makeCursorTextGeneration } from "../../textGeneration/CursorTextGeneration.ts";
28import { ProviderDriverError } from "../Errors.ts";
29import { makeCursorAdapter } from "../Layers/CursorAdapter.ts";
30import {
31 buildInitialCursorProviderSnapshot,
32 checkCursorProviderStatus,
33 enrichCursorSnapshot,
34} from "../Layers/CursorProvider.ts";
35import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
36import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
37import {
38 defaultProviderContinuationIdentity,
39 type ProviderDriver,
40 type ProviderInstance,
41} from "../ProviderDriver.ts";
42import type { ServerProviderDraft } from "../providerSnapshot.ts";
43import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
44import {
45 makeProviderMaintenanceCapabilities,
46 makeStaticProviderMaintenanceResolver,
47 resolveProviderMaintenanceCapabilitiesEffect,
48} from "../providerMaintenance.ts";
49const decodeCursorSettings = Schema.decodeSync(CursorSettings);
50
51const DRIVER_KIND = ProviderDriverKind.make("cursor");
52const SNAPSHOT_REFRESH_INTERVAL = Duration.minutes(5);
53const UPDATE = makeStaticProviderMaintenanceResolver(
54 makeProviderMaintenanceCapabilities({
55 provider: DRIVER_KIND,
56 packageName: null,
57 updateExecutable: "agent",
58 updateArgs: ["update"],
59 updateLockKey: "cursor-agent",
60 }),
61);
62
63export type CursorDriverEnv =
64 | ChildProcessSpawner.ChildProcessSpawner
65 | Crypto.Crypto
66 | FileSystem.FileSystem
67 | HttpClient.HttpClient
68 | Path.Path
69 | ProviderEventLoggers
70 | ServerConfig;
71
72const withInstanceIdentity =
73 (input: {
74 readonly instanceId: ProviderInstance["instanceId"];
75 readonly displayName: string | undefined;
76 readonly accentColor: string | undefined;
77 readonly continuationGroupKey: string;
78 }) =>
79 (snapshot: ServerProviderDraft): ServerProvider => ({
80 ...snapshot,
81 instanceId: input.instanceId,
82 driver: DRIVER_KIND,
83 ...(input.displayName ? { displayName: input.displayName } : {}),
84 ...(input.accentColor ? { accentColor: input.accentColor } : {}),
85 continuation: { groupKey: input.continuationGroupKey },
86 });
87
88export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
89 driverKind: DRIVER_KIND,
90 metadata: {
91 displayName: "Cursor",
92 supportsMultipleInstances: true,
93 },
94 configSchema: CursorSettings,
95 defaultConfig: (): CursorSettings => decodeCursorSettings({}),
96 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
97 Effect.gen(function* () {
98 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
99 const fileSystem = yield* FileSystem.FileSystem;
100 const path = yield* Path.Path;
101 const httpClient = yield* HttpClient.HttpClient;
102 const eventLoggers = yield* ProviderEventLoggers;
103 const processEnv = mergeProviderInstanceEnvironment(environment);
104 const continuationIdentity = defaultProviderContinuationIdentity({
105 driverKind: DRIVER_KIND,
106 instanceId,
107 });
108 const stampIdentity = withInstanceIdentity({
109 instanceId,
110 displayName,
111 accentColor,
112 continuationGroupKey: continuationIdentity.continuationKey,
113 });
114 const effectiveConfig = { ...config, enabled } satisfies CursorSettings;
115 const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
116 binaryPath: effectiveConfig.binaryPath,
117 env: processEnv,
118 });
119
120 const adapter = yield* makeCursorAdapter(effectiveConfig, {
121 environment: processEnv,
122 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
123 instanceId,
124 });
125 const textGeneration = yield* makeCursorTextGeneration(effectiveConfig, processEnv);
126
127 const checkProvider = checkCursorProviderStatus(effectiveConfig, processEnv).pipe(
128 Effect.map(stampIdentity),
129 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
130 Effect.provideService(FileSystem.FileSystem, fileSystem),
131 Effect.provideService(Path.Path, path),
132 );
133
134 const snapshot = yield* makeManagedServerProvider<CursorSettings>({
135 maintenanceCapabilities,
136 getSettings: Effect.succeed(effectiveConfig),
137 streamSettings: Stream.never,
138 haveSettingsChanged: () => false,
139 initialSnapshot: (settings) =>
140 buildInitialCursorProviderSnapshot(settings).pipe(Effect.map(stampIdentity)),
141 checkProvider,
142 // Preserve the background ACP model-capability probe that used to
143 // live on `CursorProviderLive`. Only fires when the snapshot reports
144 // an authenticated, enabled provider with at least one non-custom
145 // model whose capabilities haven't been captured yet.
146 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
147 enrichCursorSnapshot({
148 settings,
149 environment: processEnv,
150 snapshot: currentSnapshot,
151 maintenanceCapabilities,
152 publishSnapshot,
153 stampIdentity,
154 httpClient,
155 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)),
156 refreshInterval: SNAPSHOT_REFRESH_INTERVAL,
157 }).pipe(
158 Effect.mapError(
159 (cause) =>
160 new ProviderDriverError({
161 driver: DRIVER_KIND,
162 instanceId,
163 detail: `Failed to build Cursor snapshot: ${cause.message ?? String(cause)}`,
164 cause,
165 }),
166 ),
167 );
168
169 return {
170 instanceId,
171 driverKind: DRIVER_KIND,
172 continuationIdentity,
173 displayName,
174 accentColor,
175 enabled,
176 snapshot,
177 adapter,
178 textGeneration,
179 } satisfies ProviderInstance;
180 }),
181};