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

CursorAcpSupport.ts

138 lines4.3 KB

1import {
2 type CursorSettings,
3 type ProviderOptionSelection,
4 type RuntimeMode,
5} from "@t3tools/contracts";
6import * as Crypto from "effect/Crypto";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Scope from "effect/Scope";
10import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
11import type * as EffectAcpErrors from "effect-acp/errors";
12​
13import {
14 CURSOR_PARAMETERIZED_MODEL_PICKER_CAPABILITIES,
15 resolveCursorAcpBaseModelId,
16 resolveCursorAcpConfigUpdates,
17} from "../Layers/CursorProvider.ts";
18import * as AcpSessionRuntime from "./AcpSessionRuntime.ts";
19​
20type CursorAcpRuntimeCursorSettings = Pick<CursorSettings, "apiEndpoint" | "binaryPath">;
21​
22function cursorAcpPermissionArgs(runtimeMode?: RuntimeMode): ReadonlyArray<string> {
23 switch (runtimeMode) {
24 case "auto":
25 return ["--auto-review"];
26 case "full-access":
27 return ["--force"];
28 default:
29 return [];
30 }
32​
33export interface CursorAcpRuntimeInput extends Omit<
34 AcpSessionRuntime.AcpSessionRuntimeOptions,
35 "authMethodId" | "clientCapabilities" | "spawn"
36> {
37 readonly childProcessSpawner: ChildProcessSpawner.ChildProcessSpawner["Service"];
38 readonly cursorSettings: CursorAcpRuntimeCursorSettings | null | undefined;
39 readonly environment?: NodeJS.ProcessEnv;
40 readonly runtimeMode?: RuntimeMode;
42​
43export interface CursorAcpModelSelectionErrorContext {
44 readonly cause: EffectAcpErrors.AcpError;
45 readonly step: "set-config-option" | "set-model";
46 readonly configId?: string;
48​
49export function buildCursorAcpSpawnInput(
50 cursorSettings: CursorAcpRuntimeCursorSettings | null | undefined,
51 cwd: string,
52 environment?: NodeJS.ProcessEnv,
53 runtimeMode?: RuntimeMode,
54): AcpSessionRuntime.AcpSpawnInput {
55 return {
56 command: cursorSettings?.binaryPath || "cursor-agent",
57 args: [
58 ...(cursorSettings?.apiEndpoint ? (["-e", cursorSettings.apiEndpoint] as const) : []),
59 ...cursorAcpPermissionArgs(runtimeMode),
60 "acp",
61 ],
62 cwd,
63 ...(environment ? { env: environment } : {}),
64 };
66​
67export const makeCursorAcpRuntime = (
68 input: CursorAcpRuntimeInput,
69): Effect.Effect<
70 AcpSessionRuntime.AcpSessionRuntime["Service"],
71 EffectAcpErrors.AcpError,
72 Crypto.Crypto | Scope.Scope
73> =>
74 Effect.gen(function* () {
75 const acpContext = yield* Layer.build(
76 AcpSessionRuntime.layer({
77 ...input,
78 spawn: buildCursorAcpSpawnInput(
79 input.cursorSettings,
80 input.cwd,
81 input.environment,
82 input.runtimeMode,
83 ),
84 authMethodId: "cursor_login",
85 clientCapabilities: CURSOR_PARAMETERIZED_MODEL_PICKER_CAPABILITIES,
86 }).pipe(
87 Layer.provide(
88 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, input.childProcessSpawner),
89 ),
90 ),
91 );
92 return yield* Effect.service(AcpSessionRuntime.AcpSessionRuntime).pipe(
93 Effect.provide(acpContext),
94 );
95 });
96​
97interface CursorAcpModelSelectionRuntime {
98 readonly getConfigOptions: AcpSessionRuntime.AcpSessionRuntime["Service"]["getConfigOptions"];
99 readonly setConfigOption: (
100 configId: string,
101 value: string | boolean,
102 ) => Effect.Effect<unknown, EffectAcpErrors.AcpError>;
103 readonly setModel: (model: string) => Effect.Effect<unknown, EffectAcpErrors.AcpError>;
105​
106export function applyCursorAcpModelSelection<E>(input: {
107 readonly runtime: CursorAcpModelSelectionRuntime;
108 readonly model: string | null | undefined;
109 readonly selections: ReadonlyArray<ProviderOptionSelection> | null | undefined;
110 readonly mapError: (context: CursorAcpModelSelectionErrorContext) => E;
111}): Effect.Effect<void, E> {
112 return Effect.gen(function* () {
113 yield* input.runtime.setModel(resolveCursorAcpBaseModelId(input.model)).pipe(
114 Effect.mapError((cause) =>
115 input.mapError({
116 cause,
117 step: "set-model",
118 }),
119 ),
120 );
121​
122 const configUpdates = resolveCursorAcpConfigUpdates(
123 yield* input.runtime.getConfigOptions,
124 input.selections,
125 );
126 for (const update of configUpdates) {
127 yield* input.runtime.setConfigOption(update.configId, update.value).pipe(
128 Effect.mapError((cause) =>
129 input.mapError({
130 cause,
131 step: "set-config-option",
132 configId: update.configId,
133 }),
134 ),
135 );
136 }
137 });