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

CursorDriver.ts

172 lines6.6 KB

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 { Duration, Effect, FileSystem, Path, Schema, Stream } from "effect";
17import { HttpClient } from "effect/unstable/http";
18import { ChildProcessSpawner } from "effect/unstable/process";
19​
20import { ServerConfig } from "../../config.ts";
21import { makeCursorTextGeneration } from "../../textGeneration/CursorTextGeneration.ts";
22import { ProviderDriverError } from "../Errors.ts";
23import { makeCursorAdapter } from "../Layers/CursorAdapter.ts";
24import {
25 buildInitialCursorProviderSnapshot,
26 checkCursorProviderStatus,
27 enrichCursorSnapshot,
28} from "../Layers/CursorProvider.ts";
29import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
30import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
31import {
32 defaultProviderContinuationIdentity,
33 type ProviderDriver,
34 type ProviderInstance,
35} from "../ProviderDriver.ts";
36import type { ServerProviderDraft } from "../providerSnapshot.ts";
37import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
38import {
39 makeProviderMaintenanceCapabilities,
40 makeStaticProviderMaintenanceResolver,
41 resolveProviderMaintenanceCapabilitiesEffect,
42} from "../providerMaintenance.ts";
43​
44const DRIVER_KIND = ProviderDriverKind.make("cursor");
45const SNAPSHOT_REFRESH_INTERVAL = Duration.minutes(5);
46const UPDATE = makeStaticProviderMaintenanceResolver(
47 makeProviderMaintenanceCapabilities({
48 provider: DRIVER_KIND,
49 packageName: null,
50 updateExecutable: "agent",
51 updateArgs: ["update"],
52 updateLockKey: "cursor-agent",
53 }),
54);
55​
56export type CursorDriverEnv =
57 | ChildProcessSpawner.ChildProcessSpawner
58 | FileSystem.FileSystem
59 | HttpClient.HttpClient
60 | Path.Path
61 | ProviderEventLoggers
62 | ServerConfig;
63​
64const withInstanceIdentity =
65 (input: {
66 readonly instanceId: ProviderInstance["instanceId"];
67 readonly displayName: string | undefined;
68 readonly accentColor: string | undefined;
69 readonly continuationGroupKey: string;
70 }) =>
71 (snapshot: ServerProviderDraft): ServerProvider => ({
72 ...snapshot,
73 instanceId: input.instanceId,
74 driver: DRIVER_KIND,
75 ...(input.displayName ? { displayName: input.displayName } : {}),
76 ...(input.accentColor ? { accentColor: input.accentColor } : {}),
77 continuation: { groupKey: input.continuationGroupKey },
78 });
79​
80export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
81 driverKind: DRIVER_KIND,
82 metadata: {
83 displayName: "Cursor",
84 supportsMultipleInstances: true,
85 },
86 configSchema: CursorSettings,
87 defaultConfig: (): CursorSettings => Schema.decodeSync(CursorSettings)({}),
88 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
89 Effect.gen(function* () {
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 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 displayName,
103 accentColor,
104 continuationGroupKey: continuationIdentity.continuationKey,
105 });
106 const effectiveConfig = { ...config, enabled } satisfies CursorSettings;
107 const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
108 binaryPath: effectiveConfig.binaryPath,
109 env: processEnv,
110 });
111​
112 const adapter = yield* makeCursorAdapter(effectiveConfig, {
113 environment: processEnv,
114 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
115 instanceId,
116 });
117 const textGeneration = yield* makeCursorTextGeneration(effectiveConfig, processEnv);
118​
119 const checkProvider = checkCursorProviderStatus(effectiveConfig, processEnv).pipe(
120 Effect.map(stampIdentity),
121 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
122 Effect.provideService(FileSystem.FileSystem, fileSystem),
123 Effect.provideService(Path.Path, path),
124 );
125​
126 const snapshot = yield* makeManagedServerProvider<CursorSettings>({
127 maintenanceCapabilities,
128 getSettings: Effect.succeed(effectiveConfig),
129 streamSettings: Stream.never,
130 haveSettingsChanged: () => false,
131 initialSnapshot: (settings) => stampIdentity(buildInitialCursorProviderSnapshot(settings)),
132 checkProvider,
133 // Preserve the background ACP model-capability probe that used to
134 // live on `CursorProviderLive`. Only fires when the snapshot reports
135 // an authenticated, enabled provider with at least one non-custom
136 // model whose capabilities haven't been captured yet.
137 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
138 enrichCursorSnapshot({
139 settings,
140 environment: processEnv,
141 snapshot: currentSnapshot,
142 maintenanceCapabilities,
143 publishSnapshot,
144 stampIdentity,
145 httpClient,
146 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)),
147 refreshInterval: SNAPSHOT_REFRESH_INTERVAL,
148 }).pipe(
149 Effect.mapError(
150 (cause) =>
151 new ProviderDriverError({
152 driver: DRIVER_KIND,
153 instanceId,
154 detail: `Failed to build Cursor snapshot: ${cause.message ?? String(cause)}`,
155 cause,
156 }),
157 ),
158 );
159​
160 return {
161 instanceId,
162 driverKind: DRIVER_KIND,
163 continuationIdentity,
164 displayName,
165 accentColor,
166 enabled,
167 snapshot,
168 adapter,
169 textGeneration,
170 } satisfies ProviderInstance;
171 }),
172};