feat: add cursor local adapter across server ui and cli
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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 });
|
||||
});
|
||||
});
|
||||
184
server/src/__tests__/cursor-local-adapter.test.ts
Normal file
184
server/src/__tests__/cursor-local-adapter.test.ts
Normal 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();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user