@artifactshare/cli 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +11 -0
- package/dist/cli.d.ts +9 -0
- package/dist/cli.js +15940 -0
- package/dist/cursor-acp-C19O-3Za.js +414 -0
- package/dist/cursor-acp-entry.js +2 -409
- package/dist/index.js +4 -15861
- package/dist/runtime-CgvHR_2f.js +37 -0
- package/package.json +2 -2
- package/skills/artifactshare/SKILL.md +13 -1
- package/skills/artifactshare/artifactshare.mdc +12 -1
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { d as previewsDir, p as processAlive, t as CURSOR_ACP_ACK_TIMEOUT_MS } from "./cursor-notification-Hz6fuACK.js";
|
|
2
|
+
import { closeSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from "node:fs";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { join, resolve } from "node:path";
|
|
6
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
7
|
+
import { createServer } from "node:http";
|
|
8
|
+
import { createInterface } from "node:readline";
|
|
9
|
+
//#region src/preview/cursor-permission.ts
|
|
10
|
+
const MAX_PERMISSION_DISPLAY_LENGTH = 12e3;
|
|
11
|
+
function formatCursorPermissionRequest(toolCall) {
|
|
12
|
+
let serialized;
|
|
13
|
+
try {
|
|
14
|
+
serialized = JSON.stringify(toolCall ?? {}, null, 2);
|
|
15
|
+
} catch {
|
|
16
|
+
serialized = String(toolCall);
|
|
17
|
+
}
|
|
18
|
+
const sanitized = [...serialized].filter((character) => {
|
|
19
|
+
const code = character.charCodeAt(0);
|
|
20
|
+
if (code === 9 || code === 10 || code === 13) return true;
|
|
21
|
+
if (code < 32 || code >= 127 && code <= 159) return false;
|
|
22
|
+
if (code === 1564 || code === 65279) return false;
|
|
23
|
+
if (code >= 8203 && code <= 8207) return false;
|
|
24
|
+
if (code >= 8234 && code <= 8238) return false;
|
|
25
|
+
if (code >= 8288 && code <= 8303) return false;
|
|
26
|
+
return true;
|
|
27
|
+
}).join("");
|
|
28
|
+
if (sanitized.length <= MAX_PERMISSION_DISPLAY_LENGTH) return sanitized;
|
|
29
|
+
return `${sanitized.slice(0, MAX_PERMISSION_DISPLAY_LENGTH)}\n… (truncated)`;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/cursor-acp.ts
|
|
33
|
+
async function runCursorAcp() {
|
|
34
|
+
const input = process.argv[2];
|
|
35
|
+
if (!input || input === "--help" || input === "-h") {
|
|
36
|
+
process.stdout.write("Usage: artifactshare-preview-cursor <file> [preview options]\n");
|
|
37
|
+
process.exit(input ? 0 : 1);
|
|
38
|
+
}
|
|
39
|
+
const cwd = process.cwd();
|
|
40
|
+
const stateDir = join(previewsDir(), "cursor-acp");
|
|
41
|
+
const statePath = join(stateDir, `${createHash("sha256").update(cwd).digest("hex")}.json`);
|
|
42
|
+
mkdirSync(stateDir, {
|
|
43
|
+
recursive: true,
|
|
44
|
+
mode: 448
|
|
45
|
+
});
|
|
46
|
+
const lockPath = `${statePath}.lock`;
|
|
47
|
+
function acquireWorkspaceLock() {
|
|
48
|
+
for (let attempt = 0; attempt < 2; attempt++) try {
|
|
49
|
+
const handle = openSync(lockPath, "wx", 384);
|
|
50
|
+
writeSync(handle, String(process.pid));
|
|
51
|
+
closeSync(handle);
|
|
52
|
+
return;
|
|
53
|
+
} catch {
|
|
54
|
+
let owner = 0;
|
|
55
|
+
let initializing = false;
|
|
56
|
+
try {
|
|
57
|
+
owner = Number.parseInt(readFileSync(lockPath, "utf8").trim(), 10);
|
|
58
|
+
if (!Number.isInteger(owner) || owner <= 0) {
|
|
59
|
+
const lock = statSync(lockPath, { throwIfNoEntry: false });
|
|
60
|
+
initializing = lock !== void 0 && Date.now() - lock.mtimeMs < 5e3;
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
const lock = statSync(lockPath, { throwIfNoEntry: false });
|
|
64
|
+
initializing = lock !== void 0 && Date.now() - lock.mtimeMs < 5e3;
|
|
65
|
+
}
|
|
66
|
+
if (initializing || Number.isInteger(owner) && owner > 0 && processAlive(owner)) throw new Error("A managed Cursor preview is already running in this workspace.");
|
|
67
|
+
rmSync(lockPath, { force: true });
|
|
68
|
+
}
|
|
69
|
+
throw new Error("Could not acquire the managed Cursor workspace lock.");
|
|
70
|
+
}
|
|
71
|
+
function releaseWorkspaceLock() {
|
|
72
|
+
try {
|
|
73
|
+
if (readFileSync(lockPath, "utf8").trim() === String(process.pid)) rmSync(lockPath, { force: true });
|
|
74
|
+
} catch {}
|
|
75
|
+
}
|
|
76
|
+
acquireWorkspaceLock();
|
|
77
|
+
process.once("exit", releaseWorkspaceLock);
|
|
78
|
+
let prior = {};
|
|
79
|
+
try {
|
|
80
|
+
prior = JSON.parse(readFileSync(statePath, "utf8"));
|
|
81
|
+
} catch {}
|
|
82
|
+
const agent = spawn("agent", ["acp"], {
|
|
83
|
+
cwd,
|
|
84
|
+
env: process.env,
|
|
85
|
+
stdio: [
|
|
86
|
+
"pipe",
|
|
87
|
+
"pipe",
|
|
88
|
+
"inherit"
|
|
89
|
+
]
|
|
90
|
+
});
|
|
91
|
+
let nextId = 1;
|
|
92
|
+
let busy = false;
|
|
93
|
+
let agentAvailable = true;
|
|
94
|
+
let stopping = false;
|
|
95
|
+
let interrupted = false;
|
|
96
|
+
let fatalAgentFailure = false;
|
|
97
|
+
let bridge;
|
|
98
|
+
let preview;
|
|
99
|
+
let sessionId = "";
|
|
100
|
+
let promptAccepted;
|
|
101
|
+
let cancelActivePermission;
|
|
102
|
+
const pending = /* @__PURE__ */ new Map();
|
|
103
|
+
function failAgent(error) {
|
|
104
|
+
if (!agentAvailable) return;
|
|
105
|
+
agentAvailable = false;
|
|
106
|
+
if (!stopping) fatalAgentFailure = true;
|
|
107
|
+
cancelActivePermission?.();
|
|
108
|
+
promptAccepted?.reject(error);
|
|
109
|
+
promptAccepted = void 0;
|
|
110
|
+
for (const waiter of pending.values()) waiter.reject(error);
|
|
111
|
+
pending.clear();
|
|
112
|
+
bridge?.close();
|
|
113
|
+
if (!stopping && preview?.exitCode === null) preview.kill("SIGTERM");
|
|
114
|
+
}
|
|
115
|
+
agent.once("error", failAgent);
|
|
116
|
+
agent.stdin.on("error", failAgent);
|
|
117
|
+
agent.once("exit", () => {
|
|
118
|
+
setImmediate(() => failAgent(/* @__PURE__ */ new Error("Cursor ACP process exited.")));
|
|
119
|
+
});
|
|
120
|
+
function send(method, params) {
|
|
121
|
+
if (!agentAvailable || !agent.stdin.writable) return Promise.reject(/* @__PURE__ */ new Error("Cursor ACP process is unavailable."));
|
|
122
|
+
const id = nextId++;
|
|
123
|
+
return new Promise((resolveRequest, reject) => {
|
|
124
|
+
pending.set(id, {
|
|
125
|
+
resolve: resolveRequest,
|
|
126
|
+
reject
|
|
127
|
+
});
|
|
128
|
+
agent.stdin.write(`${JSON.stringify({
|
|
129
|
+
jsonrpc: "2.0",
|
|
130
|
+
id,
|
|
131
|
+
method,
|
|
132
|
+
params
|
|
133
|
+
})}\n`, (error) => {
|
|
134
|
+
if (!error) return;
|
|
135
|
+
pending.delete(id);
|
|
136
|
+
reject(error);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function respond(id, result) {
|
|
141
|
+
if (!agentAvailable || !agent.stdin.writable) return;
|
|
142
|
+
agent.stdin.write(`${JSON.stringify({
|
|
143
|
+
jsonrpc: "2.0",
|
|
144
|
+
id,
|
|
145
|
+
result
|
|
146
|
+
})}\n`);
|
|
147
|
+
}
|
|
148
|
+
function respondError(id, code, message) {
|
|
149
|
+
if (!agentAvailable || !agent.stdin.writable) return;
|
|
150
|
+
agent.stdin.write(`${JSON.stringify({
|
|
151
|
+
jsonrpc: "2.0",
|
|
152
|
+
id,
|
|
153
|
+
error: {
|
|
154
|
+
code,
|
|
155
|
+
message
|
|
156
|
+
}
|
|
157
|
+
})}\n`);
|
|
158
|
+
}
|
|
159
|
+
async function answerPermission(message) {
|
|
160
|
+
const options = Array.isArray(message.params?.options) ? message.params.options : [];
|
|
161
|
+
const allow = options.find((option) => typeof option?.optionId === "string" && (option.kind === "allow_once" || option.optionId === "allow-once"));
|
|
162
|
+
const reject = options.find((option) => typeof option?.optionId === "string" && (option.kind === "reject_once" || option.optionId === "reject-once"));
|
|
163
|
+
let approved = false;
|
|
164
|
+
if (process.stdin.isTTY && allow) {
|
|
165
|
+
const requestDetails = formatCursorPermissionRequest(message.params?.toolCall);
|
|
166
|
+
const terminal = createInterface({
|
|
167
|
+
input: process.stdin,
|
|
168
|
+
output: process.stderr
|
|
169
|
+
});
|
|
170
|
+
approved = await new Promise((resolveAnswer) => {
|
|
171
|
+
let settled = false;
|
|
172
|
+
const finish = (answer) => {
|
|
173
|
+
if (settled) return;
|
|
174
|
+
settled = true;
|
|
175
|
+
cancelActivePermission = void 0;
|
|
176
|
+
terminal.close();
|
|
177
|
+
resolveAnswer(answer);
|
|
178
|
+
};
|
|
179
|
+
cancelActivePermission = () => finish(false);
|
|
180
|
+
terminal.once("SIGINT", () => finish(false));
|
|
181
|
+
terminal.once("close", () => finish(false));
|
|
182
|
+
terminal.question(`\nCursor tool request:\n${requestDetails}\nAllow once? [y/N] `, (answer) => {
|
|
183
|
+
finish(answer.trim().toLowerCase() === "y");
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
const selected = approved ? allow : reject;
|
|
188
|
+
respond(message.id, selected ? { outcome: {
|
|
189
|
+
outcome: "selected",
|
|
190
|
+
optionId: selected.optionId
|
|
191
|
+
} } : { outcome: { outcome: "cancelled" } });
|
|
192
|
+
}
|
|
193
|
+
let permissionQueue = Promise.resolve();
|
|
194
|
+
createInterface({ input: agent.stdout }).on("line", (line) => {
|
|
195
|
+
let message;
|
|
196
|
+
try {
|
|
197
|
+
message = JSON.parse(line);
|
|
198
|
+
} catch {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (typeof message.id === "number" && (message.result !== void 0 || message.error !== void 0) && !message.method) {
|
|
202
|
+
const waiter = pending.get(message.id);
|
|
203
|
+
if (!waiter) return;
|
|
204
|
+
pending.delete(message.id);
|
|
205
|
+
if (message.error) waiter.reject(new Error(message.error.message ?? "Cursor ACP request failed."));
|
|
206
|
+
else waiter.resolve(message.result);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (message.method === "session/request_permission" && typeof message.id === "number") permissionQueue = permissionQueue.then(() => answerPermission(message)).catch(() => respond(message.id, { outcome: { outcome: "cancelled" } }));
|
|
210
|
+
else if ((message.method === "cursor/ask_question" || message.method === "cursor/create_plan") && typeof message.id === "number") respond(message.id, { outcome: { outcome: "cancelled" } });
|
|
211
|
+
else if (message.method === "session/update" && promptAccepted && message.params?.sessionId === sessionId && [
|
|
212
|
+
"agent_message_chunk",
|
|
213
|
+
"agent_thought_chunk",
|
|
214
|
+
"tool_call",
|
|
215
|
+
"tool_call_update",
|
|
216
|
+
"plan"
|
|
217
|
+
].includes(message.params?.update?.sessionUpdate)) {
|
|
218
|
+
promptAccepted?.resolve();
|
|
219
|
+
promptAccepted = void 0;
|
|
220
|
+
} else if (typeof message.id === "number" && message.method) respondError(message.id, -32601, `Unsupported ACP request: ${message.method}`);
|
|
221
|
+
});
|
|
222
|
+
async function initializeSession() {
|
|
223
|
+
await send("initialize", {
|
|
224
|
+
protocolVersion: 1,
|
|
225
|
+
clientCapabilities: {
|
|
226
|
+
fs: {
|
|
227
|
+
readTextFile: false,
|
|
228
|
+
writeTextFile: false
|
|
229
|
+
},
|
|
230
|
+
terminal: false
|
|
231
|
+
},
|
|
232
|
+
clientInfo: {
|
|
233
|
+
name: "artifactshare-preview-cursor",
|
|
234
|
+
version: "1"
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
await send("authenticate", { methodId: "cursor_login" });
|
|
238
|
+
if (prior.session_id && prior.cwd === cwd) try {
|
|
239
|
+
await send("session/load", {
|
|
240
|
+
sessionId: prior.session_id,
|
|
241
|
+
cwd,
|
|
242
|
+
mcpServers: []
|
|
243
|
+
});
|
|
244
|
+
return prior.session_id;
|
|
245
|
+
} catch {
|
|
246
|
+
const created = await send("session/new", {
|
|
247
|
+
cwd,
|
|
248
|
+
mcpServers: []
|
|
249
|
+
});
|
|
250
|
+
if (typeof created?.sessionId !== "string") throw new Error("Cursor ACP did not return a session id.");
|
|
251
|
+
return created.sessionId;
|
|
252
|
+
}
|
|
253
|
+
const created = await send("session/new", {
|
|
254
|
+
cwd,
|
|
255
|
+
mcpServers: []
|
|
256
|
+
});
|
|
257
|
+
if (typeof created?.sessionId !== "string") throw new Error("Cursor ACP did not return a session id.");
|
|
258
|
+
return created.sessionId;
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
sessionId = await initializeSession();
|
|
262
|
+
} catch (error) {
|
|
263
|
+
agent.kill();
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
const temp = `${statePath}.${process.pid}.tmp`;
|
|
267
|
+
writeFileSync(temp, JSON.stringify({
|
|
268
|
+
schema_version: 1,
|
|
269
|
+
session_id: sessionId,
|
|
270
|
+
cwd
|
|
271
|
+
}, null, 2), { mode: 384 });
|
|
272
|
+
renameSync(temp, statePath);
|
|
273
|
+
const token = randomBytes(32).toString("hex");
|
|
274
|
+
const server = createServer((request, response) => {
|
|
275
|
+
if (request.method !== "POST" || request.url !== "/preview-batch") {
|
|
276
|
+
response.writeHead(404).end();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (request.headers.authorization !== `Bearer ${token}`) {
|
|
280
|
+
response.writeHead(401).end();
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (busy) {
|
|
284
|
+
response.writeHead(409).end();
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
busy = true;
|
|
288
|
+
let bodyComplete = false;
|
|
289
|
+
request.once("aborted", () => {
|
|
290
|
+
if (!bodyComplete) busy = false;
|
|
291
|
+
});
|
|
292
|
+
let body = "";
|
|
293
|
+
request.setEncoding("utf8");
|
|
294
|
+
request.on("data", (chunk) => {
|
|
295
|
+
body += chunk;
|
|
296
|
+
if (body.length > 16384) request.destroy();
|
|
297
|
+
});
|
|
298
|
+
request.on("end", async () => {
|
|
299
|
+
bodyComplete = true;
|
|
300
|
+
let event;
|
|
301
|
+
try {
|
|
302
|
+
event = JSON.parse(body);
|
|
303
|
+
} catch {
|
|
304
|
+
busy = false;
|
|
305
|
+
response.writeHead(400).end();
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (event?.event !== "preview.batch_ready" || typeof event.preview_session_id !== "string" || typeof event.batch_id !== "string") {
|
|
309
|
+
busy = false;
|
|
310
|
+
response.writeHead(400).end();
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (!agentAvailable) {
|
|
314
|
+
busy = false;
|
|
315
|
+
response.writeHead(503).end();
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const filePath = resolve(cwd, input);
|
|
319
|
+
const text = `Artifact Share preview batch ready for ${JSON.stringify(filePath)}. event=preview.batch_ready preview_session_id=${event.preview_session_id} batch_id=${event.batch_id}. Run: npm exec --yes --package=@artifactshare/cli -- artifactshare preview next --session ${event.preview_session_id} --json. Fetch all comment text only with preview next.`;
|
|
320
|
+
const accepted = new Promise((resolveAccepted, reject) => {
|
|
321
|
+
promptAccepted = {
|
|
322
|
+
resolve: resolveAccepted,
|
|
323
|
+
reject
|
|
324
|
+
};
|
|
325
|
+
});
|
|
326
|
+
const prompt = send("session/prompt", {
|
|
327
|
+
sessionId,
|
|
328
|
+
prompt: [{
|
|
329
|
+
type: "text",
|
|
330
|
+
text
|
|
331
|
+
}]
|
|
332
|
+
});
|
|
333
|
+
const promptTimer = setTimeout(() => {
|
|
334
|
+
failAgent(/* @__PURE__ */ new Error("Cursor ACP preview prompt timed out."));
|
|
335
|
+
agent.kill("SIGTERM");
|
|
336
|
+
}, CURSOR_ACP_ACK_TIMEOUT_MS);
|
|
337
|
+
promptTimer.unref();
|
|
338
|
+
accepted.then(() => clearTimeout(promptTimer), () => clearTimeout(promptTimer));
|
|
339
|
+
prompt.then((result) => {
|
|
340
|
+
if (result?.stopReason === "end_turn") promptAccepted?.resolve();
|
|
341
|
+
else promptAccepted?.reject(/* @__PURE__ */ new Error("Cursor ACP did not complete the preview prompt."));
|
|
342
|
+
}).catch((error) => promptAccepted?.reject(error)).finally(() => {
|
|
343
|
+
busy = false;
|
|
344
|
+
promptAccepted = void 0;
|
|
345
|
+
});
|
|
346
|
+
try {
|
|
347
|
+
await accepted;
|
|
348
|
+
response.writeHead(202).end();
|
|
349
|
+
} catch {
|
|
350
|
+
response.writeHead(agentAvailable ? 502 : 503).end();
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
bridge = server;
|
|
355
|
+
await new Promise((resolveListen, reject) => {
|
|
356
|
+
server.once("error", reject);
|
|
357
|
+
server.listen(0, "127.0.0.1", resolveListen);
|
|
358
|
+
});
|
|
359
|
+
const address = server.address();
|
|
360
|
+
if (!address || typeof address === "string") throw new Error("Cursor ACP bridge did not bind.");
|
|
361
|
+
const target = {
|
|
362
|
+
schema_version: 1,
|
|
363
|
+
endpoint: `http://127.0.0.1:${address.port}/preview-batch`,
|
|
364
|
+
token,
|
|
365
|
+
pid: process.pid,
|
|
366
|
+
session_id: sessionId,
|
|
367
|
+
cwd
|
|
368
|
+
};
|
|
369
|
+
preview = spawn(process.execPath, [
|
|
370
|
+
fileURLToPath(new URL("./index.js", import.meta.url)),
|
|
371
|
+
"preview",
|
|
372
|
+
input,
|
|
373
|
+
...process.argv.slice(3)
|
|
374
|
+
], {
|
|
375
|
+
cwd,
|
|
376
|
+
env: {
|
|
377
|
+
...process.env,
|
|
378
|
+
CODEX_THREAD_ID: "",
|
|
379
|
+
CODEX_SESSION_ID: "",
|
|
380
|
+
CLAUDE_CODE_SESSION_ID: "",
|
|
381
|
+
ARTIFACTSHARE_CURSOR_ACP_TARGET: JSON.stringify(target)
|
|
382
|
+
},
|
|
383
|
+
stdio: "inherit"
|
|
384
|
+
});
|
|
385
|
+
process.once("SIGINT", () => {
|
|
386
|
+
stopping = true;
|
|
387
|
+
interrupted = true;
|
|
388
|
+
process.exitCode = 130;
|
|
389
|
+
cancelActivePermission?.();
|
|
390
|
+
setTimeout(() => {
|
|
391
|
+
if (preview?.exitCode === null) preview.kill("SIGTERM");
|
|
392
|
+
if (agent.exitCode === null) agent.kill("SIGTERM");
|
|
393
|
+
}, 2e3).unref();
|
|
394
|
+
});
|
|
395
|
+
process.once("SIGTERM", () => {
|
|
396
|
+
stopping = true;
|
|
397
|
+
cancelActivePermission?.();
|
|
398
|
+
preview?.kill("SIGTERM");
|
|
399
|
+
agent.kill("SIGTERM");
|
|
400
|
+
});
|
|
401
|
+
const previewExit = await new Promise((resolveExit) => preview.once("exit", (code, signal) => resolveExit({
|
|
402
|
+
code,
|
|
403
|
+
signal
|
|
404
|
+
})));
|
|
405
|
+
if (previewExit.signal === "SIGINT") interrupted = true;
|
|
406
|
+
stopping = true;
|
|
407
|
+
server.close();
|
|
408
|
+
agent.stdin.end();
|
|
409
|
+
agent.kill();
|
|
410
|
+
releaseWorkspaceLock();
|
|
411
|
+
process.exitCode = interrupted ? 130 : fatalAgentFailure ? 1 : previewExit.code ?? 1;
|
|
412
|
+
}
|
|
413
|
+
//#endregion
|
|
414
|
+
export { runCursorAcp };
|