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

CursorDriver.ts

148 lines5.8 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 { ChildProcessSpawner } from "effect/unstable/process";
18​
19import { ServerConfig } from "../../config.ts";
20import { makeCursorTextGeneration } from "../../textGeneration/CursorTextGeneration.ts";
21import { ProviderDriverError } from "../Errors.ts";
22import { makeCursorAdapter } from "../Layers/CursorAdapter.ts";
23import {
24 buildInitialCursorProviderSnapshot,
25 checkCursorProviderStatus,
26 enrichCursorSnapshot,
27} from "../Layers/CursorProvider.ts";
28import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
29import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
30import {
31 defaultProviderContinuationIdentity,
32 type ProviderDriver,
33 type ProviderInstance,
34} from "../ProviderDriver.ts";
35import type { ServerProviderDraft } from "../providerSnapshot.ts";
36import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
37​
38const DRIVER_KIND = ProviderDriverKind.make("cursor");
39const SNAPSHOT_REFRESH_INTERVAL = Duration.minutes(5);
40​
41export type CursorDriverEnv =
42 | ChildProcessSpawner.ChildProcessSpawner
43 | FileSystem.FileSystem
44 | Path.Path
45 | ProviderEventLoggers
46 | ServerConfig;
47​
48const withInstanceIdentity =
49 (input: {
50 readonly instanceId: ProviderInstance["instanceId"];
51 readonly displayName: string | undefined;
52 readonly accentColor: string | undefined;
53 readonly continuationGroupKey: string;
54 }) =>
55 (snapshot: ServerProviderDraft): ServerProvider => ({
56 ...snapshot,
57 instanceId: input.instanceId,
58 driver: DRIVER_KIND,
59 ...(input.displayName ? { displayName: input.displayName } : {}),
60 ...(input.accentColor ? { accentColor: input.accentColor } : {}),
61 continuation: { groupKey: input.continuationGroupKey },
62 });
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 => Schema.decodeSync(CursorSettings)({}),
72 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
73 Effect.gen(function* () {
74 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
75 const fileSystem = yield* FileSystem.FileSystem;
76 const path = yield* Path.Path;
77 const eventLoggers = yield* ProviderEventLoggers;
78 const processEnv = mergeProviderInstanceEnvironment(environment);
79 const continuationIdentity = defaultProviderContinuationIdentity({
80 driverKind: DRIVER_KIND,
81 instanceId,
82 });
83 const stampIdentity = withInstanceIdentity({
84 instanceId,
85 displayName,
86 accentColor,
87 continuationGroupKey: continuationIdentity.continuationKey,
88 });
89 const effectiveConfig = { ...config, enabled } satisfies CursorSettings;
90​
91 const adapter = yield* makeCursorAdapter(effectiveConfig, {
92 environment: processEnv,
93 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
94 instanceId,
95 });
96 const textGeneration = yield* makeCursorTextGeneration(effectiveConfig, processEnv);
97​
98 const checkProvider = checkCursorProviderStatus(effectiveConfig, processEnv).pipe(
99 Effect.map(stampIdentity),
100 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
101 Effect.provideService(FileSystem.FileSystem, fileSystem),
102 Effect.provideService(Path.Path, path),
103 );
104​
105 const snapshot = yield* makeManagedServerProvider<CursorSettings>({
106 getSettings: Effect.succeed(effectiveConfig),
107 streamSettings: Stream.never,
108 haveSettingsChanged: () => false,
109 initialSnapshot: (settings) => stampIdentity(buildInitialCursorProviderSnapshot(settings)),
110 checkProvider,
111 // Preserve the background ACP model-capability probe that used to
112 // live on `CursorProviderLive`. Only fires when the snapshot reports
113 // an authenticated, enabled provider with at least one non-custom
114 // model whose capabilities haven't been captured yet.
115 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
116 enrichCursorSnapshot({
117 settings,
118 environment: processEnv,
119 snapshot: currentSnapshot,
120 publishSnapshot,
121 stampIdentity,
122 }).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)),
123 refreshInterval: SNAPSHOT_REFRESH_INTERVAL,
124 }).pipe(
125 Effect.mapError(
126 (cause) =>
127 new ProviderDriverError({
128 driver: DRIVER_KIND,
129 instanceId,
130 detail: `Failed to build Cursor snapshot: ${cause.message ?? String(cause)}`,
131 cause,
132 }),
133 ),
134 );
135​
136 return {
137 instanceId,
138 driverKind: DRIVER_KIND,
139 continuationIdentity,
140 displayName,
141 accentColor,
142 enabled,
143 snapshot,
144 adapter,
145 textGeneration,
146 } satisfies ProviderInstance;
147 }),
148};