Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

handle cursor write exec requests

Raine Virta1b27816ce71b
diff --git a/src/providers/cursor/client.test.ts b/src/providers/cursor/client.test.ts
index b06da88..006d816 100644
--- a/src/providers/cursor/client.test.ts
+++ b/src/providers/cursor/client.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "bun:test";
-import { mkdtemp, writeFile } from "node:fs/promises";
+import { mkdtemp, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { decodeCursorStream, encodeConnectFrame, runCursorAgent } from "./client.ts";
@@ -117,6 +117,69 @@ describe("Cursor protocol client", () => {
expect(clientMessages[3]).toEqual({ execClientControlMessage: { streamClose: { id: 7 } } });
});
+ it("answers Cursor writeArgs by writing the file and closing the exec stream", async () => {
+ const sentFrames: Uint8Array[] = [];
+ const dir = await mkdtemp(join(tmpdir(), "cursor-write-"));
+ const file = join(dir, "history", "findings.md");
+
+ const upstream = await runCursorAgent({
+ prompt: "hello",
+ mode: "AGENT_MODE_AGENT",
+ conversationId: "conversation",
+ model: { modelId: "composer-2.5" },
+ auth: { accessToken: "token", source: "test" },
+ ctx: fakeCtx(),
+ proto: fakeProto,
+ openRunStream: async () => ({
+ readable: streamFromChunks([
+ frame({
+ message: {
+ case: "execServerMessage",
+ value: {
+ id: 10,
+ execId: "exec-write",
+ message: {
+ case: "writeArgs",
+ value: {
+ path: file,
+ fileText: "finding one\nfinding two\n",
+ returnFileContentAfterWrite: true,
+ },
+ },
+ },
+ },
+ }),
+ encodeConnectFrame(jsonBytes({}), 2),
+ ]),
+ status: Promise.resolve({ status: 200 }),
+ async write(frame) {
+ sentFrames.push(frame);
+ },
+ close() {},
+ }),
+ });
+ await drain(upstream);
+
+ const clientMessages = sentFrames.map(decodeFrameJson) as Array<Record<string, any>>;
+ expect(await readFile(file, "utf8")).toBe("finding one\nfinding two\n");
+ expect(clientMessages[1]).toEqual({ execClientControlMessage: { heartbeat: {} } });
+ expect(clientMessages[2]).toEqual({
+ execClientMessage: {
+ id: 10,
+ execId: "exec-write",
+ writeResult: {
+ success: {
+ path: file,
+ linesCreated: 3,
+ fileSize: 24,
+ fileContentAfterWrite: "finding one\nfinding two\n",
+ },
+ },
+ },
+ });
+ expect(clientMessages[3]).toEqual({ execClientControlMessage: { streamClose: { id: 10 } } });
+ });
+
it("answers Cursor grepArgs with glob file matches and closes the exec stream", async () => {
const sentFrames: Uint8Array[] = [];
const dir = await mkdtemp(join(tmpdir(), "cursor-grep-"));
diff --git a/src/providers/cursor/client.ts b/src/providers/cursor/client.ts
index 13ae75c..bc43abb 100644
--- a/src/providers/cursor/client.ts
+++ b/src/providers/cursor/client.ts
@@ -1,7 +1,7 @@
import { gunzipSync } from "node:zlib";
import http2 from "node:http2";
-import { readFile, stat } from "node:fs/promises";
-import { isAbsolute, join } from "node:path";
+import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
+import { dirname, isAbsolute, join } from "node:path";
import { cursorBaseUrl, cursorClientVersion } from "../../config.ts";
import type { RequestContext, TrafficCapture } from "../types.ts";
import type { Logger } from "../../log.ts";
@@ -388,6 +388,9 @@ async function processServerControlFrames(
} else if (oneof.value?.message?.case === "readArgs") {
await append({ execClientMessage: await buildReadResult(oneof.value) });
await append({ execClientControlMessage: { streamClose: { id: oneof.value.id } } });
+ } else if (oneof.value?.message?.case === "writeArgs") {
+ await append({ execClientMessage: await buildWriteResult(oneof.value) });
+ await append({ execClientControlMessage: { streamClose: { id: oneof.value.id } } });
} else if (oneof.value?.message?.case === "grepArgs") {
await append({ execClientMessage: await buildGrepResult(oneof.value) });
await append({ execClientControlMessage: { streamClose: { id: oneof.value.id } } });
@@ -461,6 +464,59 @@ async function buildReadResult(exec: {
};
}
+async function buildWriteResult(exec: {
+ id?: number;
+ execId?: string;
+ message?: { case?: string; value?: unknown };
+}): Promise<Record<string, unknown>> {
+ const args = asRecord(exec.message?.value);
+ const requestedPath = typeof args?.path === "string" ? args.path : "";
+ const returnContent = Boolean(args?.returnFileContentAfterWrite);
+ try {
+ if (!requestedPath) throw new Error("write path is required");
+ const content = writeContentFromArgs(args);
+ await mkdir(dirname(requestedPath), { recursive: true });
+ await writeFile(requestedPath, content);
+ const textContent = typeof content === "string" ? content : content.toString("utf8");
+ const success: Record<string, unknown> = {
+ path: requestedPath,
+ linesCreated: lineCount(textContent),
+ fileSize: Buffer.byteLength(content),
+ };
+ if (returnContent) success.fileContentAfterWrite = await readFile(requestedPath, "utf8");
+ return {
+ ...(typeof exec.id === "number" ? { id: exec.id } : {}),
+ ...(typeof exec.execId === "string" ? { execId: exec.execId } : {}),
+ writeResult: { success },
+ };
+ } catch (err) {
+ return {
+ ...(typeof exec.id === "number" ? { id: exec.id } : {}),
+ ...(typeof exec.execId === "string" ? { execId: exec.execId } : {}),
+ writeResult: {
+ error: {
+ path: requestedPath,
+ error: err instanceof Error ? err.message : String(err),
+ },
+ },
+ };
+ }
+}
+
+function writeContentFromArgs(args: Record<string, unknown> | undefined): string | Buffer {
+ if (typeof args?.fileText === "string") return args.fileText;
+ if (typeof args?.file_text === "string") return args.file_text;
+ if (typeof args?.fileBytes === "string") return Buffer.from(args.fileBytes, "base64");
+ if (typeof args?.file_bytes === "string") return Buffer.from(args.file_bytes, "base64");
+ if (args?.fileBytes instanceof Uint8Array) return Buffer.from(args.fileBytes);
+ if (args?.file_bytes instanceof Uint8Array) return Buffer.from(args.file_bytes);
+ return "";
+}
+
+function lineCount(content: string): number {
+ return content.length === 0 ? 0 : content.split("\n").length;
+}
+
async function runShellStream(
exec: {
id?: number;