feat: add cursor local adapter across server ui and cli

This commit is contained in:
Dotta
2026-03-05 06:31:22 -06:00
parent b4a02ebc3f
commit 8a85173150
35 changed files with 1871 additions and 20 deletions

View File

@@ -1,6 +1,10 @@
import { describe, expect, it } from "vitest";
import { sessionCodec as claudeSessionCodec } from "@paperclipai/adapter-claude-local/server";
import { sessionCodec as codexSessionCodec, isCodexUnknownSessionError } from "@paperclipai/adapter-codex-local/server";
import {
sessionCodec as cursorSessionCodec,
isCursorUnknownSessionError,
} from "@paperclipai/adapter-cursor-local/server";
import {
sessionCodec as opencodeSessionCodec,
isOpenCodeUnknownSessionError,
@@ -60,6 +64,24 @@ describe("adapter session codecs", () => {
});
expect(opencodeSessionCodec.getDisplayId?.(serialized ?? null)).toBe("opencode-session-1");
});
it("normalizes cursor session params with cwd", () => {
const parsed = cursorSessionCodec.deserialize({
session_id: "cursor-session-1",
cwd: "/tmp/cursor",
});
expect(parsed).toEqual({
sessionId: "cursor-session-1",
cwd: "/tmp/cursor",
});
const serialized = cursorSessionCodec.serialize(parsed);
expect(serialized).toEqual({
sessionId: "cursor-session-1",
cwd: "/tmp/cursor",
});
expect(cursorSessionCodec.getDisplayId?.(serialized ?? null)).toBe("cursor-session-1");
});
});
describe("codex resume recovery detection", () => {
@@ -101,3 +123,26 @@ describe("opencode resume recovery detection", () => {
).toBe(false);
});
});
describe("cursor resume recovery detection", () => {
it("detects unknown session errors from cursor output", () => {
expect(
isCursorUnknownSessionError(
"",
"Error: unknown session id abc",
),
).toBe(true);
expect(
isCursorUnknownSessionError(
"",
"chat abc not found",
),
).toBe(true);
expect(
isCursorUnknownSessionError(
"{\"type\":\"result\",\"subtype\":\"success\"}",
"",
),
).toBe(false);
});
});

View File

@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { testEnvironment } from "@paperclipai/adapter-cursor-local/server";
describe("cursor environment diagnostics", () => {
it("creates a missing working directory when cwd is absolute", async () => {
const cwd = path.join(
os.tmpdir(),
`paperclip-cursor-local-cwd-${Date.now()}-${Math.random().toString(16).slice(2)}`,
"workspace",
);
await fs.rm(path.dirname(cwd), { recursive: true, force: true });
const result = await testEnvironment({
companyId: "company-1",
adapterType: "cursor",
config: {
command: process.execPath,
cwd,
},
});
expect(result.checks.some((check) => check.code === "cursor_cwd_valid")).toBe(true);
expect(result.checks.some((check) => check.level === "error")).toBe(false);
const stats = await fs.stat(cwd);
expect(stats.isDirectory()).toBe(true);
await fs.rm(path.dirname(cwd), { recursive: true, force: true });
});
});

View File

@@ -0,0 +1,184 @@
import { describe, expect, it, vi } from "vitest";
import { isCursorUnknownSessionError, parseCursorJsonl } from "@paperclipai/adapter-cursor-local/server";
import { parseCursorStdoutLine } from "@paperclipai/adapter-cursor-local/ui";
import { printCursorStreamEvent } from "@paperclipai/adapter-cursor-local/cli";
describe("cursor parser", () => {
it("extracts session, summary, usage, cost, and terminal error message", () => {
const stdout = [
JSON.stringify({ type: "system", subtype: "init", session_id: "chat_123", model: "gpt-5" }),
JSON.stringify({
type: "assistant",
message: {
content: [{ type: "output_text", text: "hello" }],
},
}),
JSON.stringify({
type: "result",
subtype: "success",
session_id: "chat_123",
usage: {
input_tokens: 100,
cached_input_tokens: 25,
output_tokens: 40,
},
total_cost_usd: 0.001,
result: "Task complete",
}),
JSON.stringify({ type: "error", message: "model access denied" }),
].join("\n");
const parsed = parseCursorJsonl(stdout);
expect(parsed.sessionId).toBe("chat_123");
expect(parsed.summary).toBe("hello");
expect(parsed.usage).toEqual({
inputTokens: 100,
cachedInputTokens: 25,
outputTokens: 40,
});
expect(parsed.costUsd).toBeCloseTo(0.001, 6);
expect(parsed.errorMessage).toBe("model access denied");
});
});
describe("cursor stale session detection", () => {
it("treats missing/unknown session messages as an unknown session error", () => {
expect(isCursorUnknownSessionError("", "unknown session id chat_123")).toBe(true);
expect(isCursorUnknownSessionError("", "chat abc not found")).toBe(true);
});
});
describe("cursor ui stdout parser", () => {
it("parses assistant, thinking, and tool lifecycle events", () => {
const ts = "2026-03-05T00:00:00.000Z";
expect(
parseCursorStdoutLine(
JSON.stringify({
type: "assistant",
message: {
content: [
{ type: "output_text", text: "I will run a command." },
{ type: "thinking", text: "Checking repository state" },
{ type: "tool_call", name: "bash", input: { command: "ls -1" } },
{ type: "tool_result", tool_use_id: "tool_1", output: "AGENTS.md\n", status: "ok" },
],
},
}),
ts,
),
).toEqual([
{ kind: "assistant", ts, text: "I will run a command." },
{ kind: "thinking", ts, text: "Checking repository state" },
{ kind: "tool_call", ts, name: "bash", input: { command: "ls -1" } },
{ kind: "tool_result", ts, toolUseId: "tool_1", content: "AGENTS.md\n", isError: false },
]);
});
it("parses result usage and errors", () => {
const ts = "2026-03-05T00:00:00.000Z";
expect(
parseCursorStdoutLine(
JSON.stringify({
type: "result",
subtype: "success",
result: "Done",
usage: {
input_tokens: 10,
output_tokens: 5,
cached_input_tokens: 2,
},
total_cost_usd: 0.00042,
is_error: false,
}),
ts,
),
).toEqual([
{
kind: "result",
ts,
text: "Done",
inputTokens: 10,
outputTokens: 5,
cachedTokens: 2,
costUsd: 0.00042,
subtype: "success",
isError: false,
errors: [],
},
]);
});
});
function stripAnsi(value: string): string {
return value.replace(/\x1b\[[0-9;]*m/g, "");
}
describe("cursor cli formatter", () => {
it("prints init, assistant, tool, and result events", () => {
const spy = vi.spyOn(console, "log").mockImplementation(() => {});
try {
printCursorStreamEvent(
JSON.stringify({ type: "system", subtype: "init", session_id: "chat_abc", model: "gpt-5" }),
false,
);
printCursorStreamEvent(
JSON.stringify({
type: "assistant",
message: {
content: [{ type: "output_text", text: "hello" }],
},
}),
false,
);
printCursorStreamEvent(
JSON.stringify({
type: "assistant",
message: {
content: [{ type: "tool_call", name: "bash", input: { command: "ls -1" } }],
},
}),
false,
);
printCursorStreamEvent(
JSON.stringify({
type: "assistant",
message: {
content: [{ type: "tool_result", output: "AGENTS.md", status: "ok" }],
},
}),
false,
);
printCursorStreamEvent(
JSON.stringify({
type: "result",
subtype: "success",
result: "Done",
usage: { input_tokens: 10, output_tokens: 5, cached_input_tokens: 2 },
total_cost_usd: 0.00042,
}),
false,
);
const lines = spy.mock.calls
.map((call) => call.map((v) => String(v)).join(" "))
.map(stripAnsi);
expect(lines).toEqual(
expect.arrayContaining([
"Cursor init (session: chat_abc, model: gpt-5)",
"assistant: hello",
"tool_call: bash",
"tool_result",
"AGENTS.md",
"result: subtype=success",
"tokens: in=10 out=5 cached=2 cost=$0.000420",
"assistant: Done",
]),
);
} finally {
spy.mockRestore();
}
});
});

View File

@@ -11,6 +11,12 @@ import {
sessionCodec as codexSessionCodec,
} from "@paperclipai/adapter-codex-local/server";
import { agentConfigurationDoc as codexAgentConfigurationDoc, models as codexModels } from "@paperclipai/adapter-codex-local";
import {
execute as cursorExecute,
testEnvironment as cursorTestEnvironment,
sessionCodec as cursorSessionCodec,
} from "@paperclipai/adapter-cursor-local/server";
import { agentConfigurationDoc as cursorAgentConfigurationDoc, models as cursorModels } from "@paperclipai/adapter-cursor-local";
import {
execute as opencodeExecute,
testEnvironment as opencodeTestEnvironment,
@@ -60,6 +66,16 @@ const opencodeLocalAdapter: ServerAdapterModule = {
agentConfigurationDoc: opencodeAgentConfigurationDoc,
};
const cursorLocalAdapter: ServerAdapterModule = {
type: "cursor",
execute: cursorExecute,
testEnvironment: cursorTestEnvironment,
sessionCodec: cursorSessionCodec,
models: cursorModels,
supportsLocalAgentJwt: true,
agentConfigurationDoc: cursorAgentConfigurationDoc,
};
const openclawAdapter: ServerAdapterModule = {
type: "openclaw",
execute: openclawExecute,
@@ -70,7 +86,7 @@ const openclawAdapter: ServerAdapterModule = {
};
const adaptersByType = new Map<string, ServerAdapterModule>(
[claudeLocalAdapter, codexLocalAdapter, opencodeLocalAdapter, openclawAdapter, processAdapter, httpAdapter].map((a) => [a.type, a]),
[claudeLocalAdapter, codexLocalAdapter, opencodeLocalAdapter, cursorLocalAdapter, openclawAdapter, processAdapter, httpAdapter].map((a) => [a.type, a]),
);
export function getServerAdapter(type: string): ServerAdapterModule {

View File

@@ -36,6 +36,7 @@ import {
DEFAULT_CODEX_LOCAL_BYPASS_APPROVALS_AND_SANDBOX,
DEFAULT_CODEX_LOCAL_MODEL,
} from "@paperclipai/adapter-codex-local";
import { DEFAULT_CURSOR_LOCAL_MODEL } from "@paperclipai/adapter-cursor-local";
import { DEFAULT_OPENCODE_LOCAL_MODEL } from "@paperclipai/adapter-opencode-local";
export function agentRoutes(db: Db) {
@@ -43,6 +44,7 @@ export function agentRoutes(db: Db) {
claude_local: "instructionsFilePath",
codex_local: "instructionsFilePath",
opencode_local: "instructionsFilePath",
cursor: "instructionsFilePath",
};
const KNOWN_INSTRUCTIONS_PATH_KEYS = new Set(["instructionsFilePath", "agentsMdPath"]);
@@ -196,6 +198,9 @@ export function agentRoutes(db: Db) {
if (adapterType === "opencode_local" && !asNonEmptyString(next.model)) {
next.model = DEFAULT_OPENCODE_LOCAL_MODEL;
}
if (adapterType === "cursor" && !asNonEmptyString(next.model)) {
next.model = DEFAULT_CURSOR_LOCAL_MODEL;
}
return next;
}

View File

@@ -74,6 +74,10 @@ const ADAPTER_DEFAULT_RULES_BY_TYPE: Record<string, Array<{ path: string[]; valu
{ path: ["timeoutSec"], value: 0 },
{ path: ["graceSec"], value: 15 },
],
cursor: [
{ path: ["timeoutSec"], value: 0 },
{ path: ["graceSec"], value: 15 },
],
claude_local: [
{ path: ["timeoutSec"], value: 0 },
{ path: ["graceSec"], value: 15 },