The best way to control Claude Code, Codex and more from any computer or phone

main
ts 224 lines 8.48 KB
Raw
1 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Config from "effect/Config";
4 import * as Console from "effect/Console";
5 import * as Effect from "effect/Effect";
6 import * as Layer from "effect/Layer";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Option from "effect/Option";
9 import * as Path from "effect/Path";
10 import * as Redacted from "effect/Redacted";
11 import * as Schema from "effect/Schema";
12 import * as Struct from "effect/Struct";
13 import { Command, Flag } from "effect/cli";
14 import * as CursorSdk from "@t3tools/provider-cursor/server/CursorSdk";
15
16 import {
17 recordCursorAgentSdkReplayTranscript,
18 type CursorAgentSdkReplayTranscript,
19 } from "../src/orchestration-v2/Adapters/CursorAdapterV2.testkit.ts";
20 import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
21 import {
22 CURSOR_MODEL_SELECTION,
23 MESSAGE_STEERING_INITIAL_PROMPT,
24 MESSAGE_STEERING_STEER_PROMPT,
25 MULTI_TURN_FIRST_PROMPT,
26 MULTI_TURN_SECOND_PROMPT,
27 PROVIDER_THREAD_RESUME_FIRST_PROMPT,
28 PROVIDER_THREAD_RESUME_SECOND_PROMPT,
29 PROPOSED_PLAN_PROMPT,
30 READ_ONLY_NEVER_POLICY,
31 SIMPLE_PROMPT,
32 SKILL_INVOCATION_CURSOR_MESSAGE,
33 SUBAGENT_PROMPT,
34 TODO_LIST_PROMPT,
35 TOOL_CALL_READ_ONLY_PROMPT,
36 TURN_INTERRUPT_MID_TOOL_PROMPT,
37 WORKSPACE_NEVER_POLICY,
38 } from "../src/orchestration-v2/testkit/fixtures/shared.ts";
39 import { skillInvocationInput } from "../src/orchestration-v2/testkit/fixtures/skill_invocation/input.ts";
40 import {
41 cursorReplayPromptsForWorkspace,
42 cursorReplayTranscriptCwd,
43 shouldSeedCursorReplayWorkspace,
44 } from "./cursorReplayRecordingWorkspace.ts";
45
46 const RECORDINGS = {
47 simple: {
48 prompts: [SIMPLE_PROMPT],
49 output: "../src/orchestration-v2/testkit/fixtures/simple/cursor_transcript.ndjson",
50 },
51 multi_turn: {
52 prompts: [MULTI_TURN_FIRST_PROMPT, MULTI_TURN_SECOND_PROMPT],
53 output: "../src/orchestration-v2/testkit/fixtures/multi_turn/cursor_transcript.ndjson",
54 },
55 message_steering: {
56 prompts: [MESSAGE_STEERING_INITIAL_PROMPT, MESSAGE_STEERING_STEER_PROMPT],
57 output: "../src/orchestration-v2/testkit/fixtures/message_steering/cursor_transcript.ndjson",
58 steerAfterRunStartPromptIndex: 0,
59 },
60 provider_thread_resume: {
61 prompts: [PROVIDER_THREAD_RESUME_FIRST_PROMPT, PROVIDER_THREAD_RESUME_SECOND_PROMPT],
62 output:
63 "../src/orchestration-v2/testkit/fixtures/provider_thread_resume/cursor_transcript.ndjson",
64 restartBeforePromptIndex: 1,
65 },
66 queued_turn: {
67 prompts: [MULTI_TURN_FIRST_PROMPT, MULTI_TURN_SECOND_PROMPT],
68 output: "../src/orchestration-v2/testkit/fixtures/queued_turn/cursor_transcript.ndjson",
69 },
70 proposed_plan: {
71 prompts: [PROPOSED_PLAN_PROMPT],
72 output: "../src/orchestration-v2/testkit/fixtures/proposed_plan/cursor_transcript.ndjson",
73 interactionMode: "plan",
74 runtimePolicyOverride: READ_ONLY_NEVER_POLICY,
75 },
76 todo_list: {
77 prompts: [TODO_LIST_PROMPT],
78 output: "../src/orchestration-v2/testkit/fixtures/todo_list/cursor_transcript.ndjson",
79 runtimePolicyOverride: READ_ONLY_NEVER_POLICY,
80 },
81 subagent: {
82 prompts: [SUBAGENT_PROMPT],
83 output: "../src/orchestration-v2/testkit/fixtures/subagent/cursor_transcript.ndjson",
84 runtimePolicyOverride: READ_ONLY_NEVER_POLICY,
85 },
86 tool_call_read_only: {
87 prompts: [TOOL_CALL_READ_ONLY_PROMPT],
88 output: "../src/orchestration-v2/testkit/fixtures/tool_call_read_only/cursor_transcript.ndjson",
89 runtimePolicyOverride: READ_ONLY_NEVER_POLICY,
90 },
91 turn_interrupt_mid_tool: {
92 prompts: [TURN_INTERRUPT_MID_TOOL_PROMPT],
93 output:
94 "../src/orchestration-v2/testkit/fixtures/turn_interrupt_mid_tool/cursor_transcript.ndjson",
95 interruptAfterToolStart: true,
96 runtimePolicyOverride: WORKSPACE_NEVER_POLICY,
97 },
98 skill_invocation: {
99 prompts: [SKILL_INVOCATION_CURSOR_MESSAGE],
100 output: "../src/orchestration-v2/testkit/fixtures/skill_invocation/cursor_transcript.ndjson",
101 workspaceFiles: skillInvocationInput().workspaceFiles,
102 },
103 } as const;
104
105 type RecordingName = keyof typeof RECORDINGS;
106 const RECORDING_NAMES = Struct.keys(RECORDINGS);
107
108 const encodeUnknownJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
109
110 function encodeTranscriptNdjson(transcript: CursorAgentSdkReplayTranscript): string {
111 const { entries, ...metadata } = transcript;
112 return [
113 encodeUnknownJsonString({ type: "transcript_start", ...metadata }),
114 ...entries.map((entry) => encodeUnknownJsonString(entry)),
115 "",
116 ].join("\n");
117 }
118
119 const writeFixtureFiles = Effect.fn("writeFixtureFiles")(function* (cwd: string) {
120 const fs = yield* FileSystem.FileSystem;
121 const path = yield* Path.Path;
122 yield* fs.writeFileString(
123 path.join(cwd, "package.json"),
124 encodeUnknownJsonString({
125 name: "cursor-read-only-fixture",
126 private: true,
127 scripts: { typecheck: "tsc --noEmit" },
128 }),
129 );
130 yield* fs.writeFileString(
131 path.join(cwd, "tsconfig.json"),
132 encodeUnknownJsonString({
133 compilerOptions: {
134 module: "ESNext",
135 strict: true,
136 target: "ES2022",
137 },
138 }),
139 );
140 });
141
142 const recordScenario = Effect.fn("recordScenario")(function* (
143 scenario: RecordingName,
144 out: string | undefined,
145 ) {
146 const fs = yield* FileSystem.FileSystem;
147 const path = yield* Path.Path;
148 const apiKey = yield* Config.Redacted("CURSOR_API_KEY");
149 const model = yield* Config.NonEmptyString("T3_CURSOR_REPLAY_MODEL").pipe(
150 Config.withDefault(CURSOR_MODEL_SELECTION.model),
151 );
152 const configuredCwd = yield* Config.NonEmptyString("T3_CURSOR_REPLAY_CWD").pipe(Config.option);
153 const recording = RECORDINGS[scenario];
154
155 const owned = Option.isNone(configuredCwd);
156 const cwd = Option.isSome(configuredCwd)
157 ? configuredCwd.value
158 : yield* checkpointWorkspace(
159 `cursor-agent-sdk-record-${scenario}`,
160 "workspaceFiles" in recording ? recording.workspaceFiles : undefined,
161 );
162 if (shouldSeedCursorReplayWorkspace({ scenario, owned })) {
163 yield* writeFixtureFiles(cwd);
164 }
165 const transcriptCwd = cursorReplayTranscriptCwd(scenario);
166 const transcript = yield* recordCursorAgentSdkReplayTranscript({
167 scenario,
168 prompts: cursorReplayPromptsForWorkspace({
169 scenario,
170 configuredPrompts: recording.prompts,
171 packageJsonPath: path.join(cwd, "package.json"),
172 tsconfigPath: path.join(cwd, "tsconfig.json"),
173 }),
174 ...(scenario === "tool_call_read_only" ? { transcriptPrompts: recording.prompts } : {}),
175 modelSelection: { ...CURSOR_MODEL_SELECTION, model },
176 cwd,
177 ...(transcriptCwd === undefined ? {} : { transcriptCwd }),
178 apiKey: Redacted.value(apiKey),
179 ...("interactionMode" in recording ? { interactionMode: recording.interactionMode } : {}),
180 ...("runtimePolicyOverride" in recording
181 ? { runtimePolicyOverride: recording.runtimePolicyOverride }
182 : {}),
183 ...("interruptAfterToolStart" in recording
184 ? { interruptAfterToolStart: recording.interruptAfterToolStart }
185 : {}),
186 ...("steerAfterRunStartPromptIndex" in recording
187 ? { steerAfterRunStartPromptIndex: recording.steerAfterRunStartPromptIndex }
188 : {}),
189 ...("restartBeforePromptIndex" in recording
190 ? { restartBeforePromptIndex: recording.restartBeforePromptIndex }
191 : {}),
192 });
193
194 const outputPath = out ?? (yield* path.fromFileUrl(new URL(recording.output, import.meta.url)));
195 yield* fs.makeDirectory(path.dirname(outputPath), { recursive: true });
196 yield* fs.writeFileString(outputPath, encodeTranscriptNdjson(transcript));
197 yield* Console.log(
198 `Wrote ${transcript.entries.length} Cursor SDK replay entries to ${outputPath}`,
199 );
200 }, Effect.scoped);
201
202 const recordCursorReplayCommand = Command.make(
203 "record-cursor-agent-sdk-replay-fixture",
204 {
205 scenario: Flag.Literals("scenario", RECORDING_NAMES).pipe(
206 Flag.withDescription("Scenario to record (or T3_CURSOR_REPLAY_SCENARIO)."),
207 Flag.withFallbackConfig(Config.Literals(RECORDING_NAMES, "T3_CURSOR_REPLAY_SCENARIO")),
208 ),
209 out: Flag.String("out").pipe(
210 Flag.optional,
211 Flag.withDescription("Write the transcript here instead of over the checked-in fixture."),
212 ),
213 },
214 ({ scenario, out }) => recordScenario(scenario, Option.getOrUndefined(out)),
215 ).pipe(
216 Command.withDescription(
217 "Record a Cursor Agent SDK replay fixture live. Reads CURSOR_API_KEY, plus optional T3_CURSOR_REPLAY_MODEL and T3_CURSOR_REPLAY_CWD.",
218 ),
219 );
220
221 Command.run(recordCursorReplayCommand, { version: "0.0.0" }).pipe(
222 Effect.provide(CursorSdk.layer.pipe(Layer.provideMerge(NodeServices.layer))),
223 NodeRuntime.runMain,
224 );