@artifactshare/cli 0.12.0 → 0.13.1
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 +24 -0
- package/README.md +65 -29
- package/dist/cursor-acp-entry.d.ts +1 -0
- package/dist/cursor-acp-entry.js +413 -0
- package/dist/cursor-notification-BBGHJ5v5.js +520 -0
- package/dist/index.js +813 -443
- package/package.json +6 -3
- package/skills/artifactshare/SKILL.md +87 -61
- package/skills/artifactshare/artifactshare.mdc +6 -6
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import { chmodSync, closeSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync, writeSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
5
|
+
//#region src/preview/contract.ts
|
|
6
|
+
/** Custom header required on every mutating request. Its presence forces a
|
|
7
|
+
* CORS preflight from any cross-origin caller, and the server sends no CORS
|
|
8
|
+
* headers, so cross-origin writes fail in the browser. */
|
|
9
|
+
const PREVIEW_MUTATION_HEADER = "x-artifactshare-preview";
|
|
10
|
+
/** Identification endpoint used for stale-session detection and reuse. */
|
|
11
|
+
const PREVIEW_SESSION_ENDPOINT = "/__preview/session";
|
|
12
|
+
function isPreviewSessionIdentity(value) {
|
|
13
|
+
if (typeof value !== "object" || value === null) return false;
|
|
14
|
+
const record = value;
|
|
15
|
+
return record.service === "artifactshare-preview" && typeof record.session_id === "string" && typeof record.realpath === "string" && typeof record.share_port === "number";
|
|
16
|
+
}
|
|
17
|
+
function isPreviewDoneItemInput(value) {
|
|
18
|
+
if (typeof value !== "object" || value === null) return false;
|
|
19
|
+
const record = value;
|
|
20
|
+
return typeof record.thread === "string" && typeof record.generation === "number" && Number.isInteger(record.generation) && (record.outcome === "fixed" || record.outcome === "skipped") && (record.note === void 0 || typeof record.note === "string");
|
|
21
|
+
}
|
|
22
|
+
const ANNOTATION_STATUSES = /* @__PURE__ */ new Set([
|
|
23
|
+
"draft",
|
|
24
|
+
"requested",
|
|
25
|
+
"in_progress",
|
|
26
|
+
"resolved",
|
|
27
|
+
"dismissed"
|
|
28
|
+
]);
|
|
29
|
+
function isThreadMessage(value) {
|
|
30
|
+
if (typeof value !== "object" || value === null) return false;
|
|
31
|
+
const record = value;
|
|
32
|
+
return typeof record.id === "string" && (record.author === "human" || record.author === "agent") && typeof record.body === "string" && typeof record.created_at === "string";
|
|
33
|
+
}
|
|
34
|
+
/** A saved annotations file is quarantined rather than trusted, so every entry
|
|
35
|
+
* has to be checked: one malformed record would otherwise crash rendering and
|
|
36
|
+
* delivery with no way back. */
|
|
37
|
+
function isPreviewAnnotation(value) {
|
|
38
|
+
if (typeof value !== "object" || value === null) return false;
|
|
39
|
+
const record = value;
|
|
40
|
+
return typeof record.thread === "string" && Number.isInteger(record.generation) && typeof record.status === "string" && ANNOTATION_STATUSES.has(record.status) && isPreviewAnchor(record.anchor) && typeof record.comment === "string" && Array.isArray(record.messages) && record.messages.every(isThreadMessage) && (record.batch_id === null || typeof record.batch_id === "string") && typeof record.created_at === "string" && typeof record.updated_at === "string" && (record.summary === null || typeof record.summary === "string");
|
|
41
|
+
}
|
|
42
|
+
function isPreviewAnchor(value) {
|
|
43
|
+
if (typeof value !== "object" || value === null) return false;
|
|
44
|
+
const record = value;
|
|
45
|
+
if (record.kind === "artifact") return true;
|
|
46
|
+
if (record.state !== "attached" && record.state !== "orphaned") return false;
|
|
47
|
+
const optionalString = (name) => record[name] === void 0 || typeof record[name] === "string";
|
|
48
|
+
if (record.kind === "element") return optionalString("ownText") && optionalString("tagName") && typeof record.selector === "string" && typeof record.label === "string" && typeof record.contextText === "string";
|
|
49
|
+
if (record.kind === "text") return typeof record.quotedText === "string" && typeof record.prefixText === "string" && typeof record.suffixText === "string" && (record.textStart === null || Number.isInteger(record.textStart)) && (record.textEnd === null || Number.isInteger(record.textEnd)) && (record.cssPath === null || typeof record.cssPath === "string");
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/preview/notification.ts
|
|
54
|
+
const FAILURE_CODES = /* @__PURE__ */ new Set([
|
|
55
|
+
"target_unavailable",
|
|
56
|
+
"rejected",
|
|
57
|
+
"timeout",
|
|
58
|
+
"invalid_response",
|
|
59
|
+
"adapter_error"
|
|
60
|
+
]);
|
|
61
|
+
function defaultPreviewNotificationRegistration() {
|
|
62
|
+
return {
|
|
63
|
+
provider: "generic",
|
|
64
|
+
transport: "long_poll",
|
|
65
|
+
capability: "wait",
|
|
66
|
+
target: null,
|
|
67
|
+
registered_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function samePreviewNotificationRegistration(left, right) {
|
|
71
|
+
return left.provider === right.provider && left.transport === right.transport && left.capability === right.capability && left.target === right.target;
|
|
72
|
+
}
|
|
73
|
+
function isPreviewNotificationRegistration(value) {
|
|
74
|
+
if (typeof value !== "object" || value === null) return false;
|
|
75
|
+
const record = value;
|
|
76
|
+
return typeof record.provider === "string" && typeof record.transport === "string" && (record.capability === "push" || record.capability === "wait" || record.capability === "manual") && (record.target === null || typeof record.target === "string") && typeof record.registered_at === "string";
|
|
77
|
+
}
|
|
78
|
+
function normalizeResult(value) {
|
|
79
|
+
if (typeof value !== "object" || value === null) return {
|
|
80
|
+
status: "failed",
|
|
81
|
+
code: "invalid_response",
|
|
82
|
+
retryable: false
|
|
83
|
+
};
|
|
84
|
+
const record = value;
|
|
85
|
+
if (record.status === "accepted") return { status: "accepted" };
|
|
86
|
+
if (record.status === "failed" && typeof record.code === "string" && FAILURE_CODES.has(record.code) && typeof record.retryable === "boolean") {
|
|
87
|
+
const fallback = isPreviewNotificationRegistration(record.fallback) ? record.fallback : void 0;
|
|
88
|
+
return {
|
|
89
|
+
status: "failed",
|
|
90
|
+
code: record.code,
|
|
91
|
+
retryable: record.retryable,
|
|
92
|
+
...fallback ? { fallback } : {}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
status: "failed",
|
|
97
|
+
code: "invalid_response",
|
|
98
|
+
retryable: false
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function createPreviewNotificationCoordinator(options) {
|
|
102
|
+
const { sessionId, store } = options;
|
|
103
|
+
let registration = options.registration;
|
|
104
|
+
const timeoutMs = options.timeoutMs ?? 1e4;
|
|
105
|
+
let waitConnected = false;
|
|
106
|
+
const dispatches = /* @__PURE__ */ new Map();
|
|
107
|
+
store.recoverInterruptedBatch();
|
|
108
|
+
function refreshRegistration() {
|
|
109
|
+
if (registration.capability !== "push") return;
|
|
110
|
+
const fallback = options.adapter?.fallbackIfUnavailable?.();
|
|
111
|
+
if (!fallback) return;
|
|
112
|
+
registration = fallback;
|
|
113
|
+
options.onRegistrationChange?.(registration);
|
|
114
|
+
}
|
|
115
|
+
function projection() {
|
|
116
|
+
refreshRegistration();
|
|
117
|
+
const active = store.activeBatch();
|
|
118
|
+
const latest = store.latestBatch();
|
|
119
|
+
const state = active ? active.state : latest ? latest.state : registration.capability === "push" || registration.capability === "wait" && waitConnected ? "waiting" : "manual_required";
|
|
120
|
+
return {
|
|
121
|
+
provider: registration.provider,
|
|
122
|
+
transport: registration.transport,
|
|
123
|
+
capability: registration.capability,
|
|
124
|
+
state,
|
|
125
|
+
...active?.failure_code ? { failure_code: active.failure_code } : {}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
async function runDispatch(batchId) {
|
|
129
|
+
store.markDispatchStarted(batchId);
|
|
130
|
+
if (!options.adapter) {
|
|
131
|
+
const result = {
|
|
132
|
+
status: "failed",
|
|
133
|
+
code: "target_unavailable",
|
|
134
|
+
retryable: true
|
|
135
|
+
};
|
|
136
|
+
store.markDispatchFailed(batchId, result.code, result.retryable);
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
let timer;
|
|
140
|
+
try {
|
|
141
|
+
const timeout = new Promise((resolve) => {
|
|
142
|
+
timer = setTimeout(() => resolve({
|
|
143
|
+
status: "failed",
|
|
144
|
+
code: "timeout",
|
|
145
|
+
retryable: true
|
|
146
|
+
}), timeoutMs);
|
|
147
|
+
});
|
|
148
|
+
const result = normalizeResult(await Promise.race([options.adapter.dispatch({
|
|
149
|
+
event: "preview.batch_ready",
|
|
150
|
+
preview_session_id: sessionId,
|
|
151
|
+
batch_id: batchId
|
|
152
|
+
}), timeout]));
|
|
153
|
+
if (result.status === "accepted") store.markDispatchAccepted(batchId);
|
|
154
|
+
else {
|
|
155
|
+
store.markDispatchFailed(batchId, result.code, result.retryable);
|
|
156
|
+
if (result.fallback) {
|
|
157
|
+
registration = result.fallback;
|
|
158
|
+
options.onRegistrationChange?.(registration);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
} catch {
|
|
163
|
+
const result = {
|
|
164
|
+
status: "failed",
|
|
165
|
+
code: "adapter_error",
|
|
166
|
+
retryable: true
|
|
167
|
+
};
|
|
168
|
+
store.markDispatchFailed(batchId, result.code, result.retryable);
|
|
169
|
+
return result;
|
|
170
|
+
} finally {
|
|
171
|
+
if (timer) clearTimeout(timer);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
get registration() {
|
|
176
|
+
return registration;
|
|
177
|
+
},
|
|
178
|
+
projection,
|
|
179
|
+
setWaitConnected(connected) {
|
|
180
|
+
waitConnected = connected;
|
|
181
|
+
},
|
|
182
|
+
async notifyBatch(batchId) {
|
|
183
|
+
refreshRegistration();
|
|
184
|
+
if (registration.capability !== "push") {
|
|
185
|
+
if (registration.capability === "manual" || !waitConnected) store.markManualRequired(batchId);
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const key = `${sessionId}:${batchId}`;
|
|
189
|
+
const existing = dispatches.get(key);
|
|
190
|
+
if (existing) return await existing;
|
|
191
|
+
const pending = runDispatch(batchId);
|
|
192
|
+
dispatches.set(key, pending);
|
|
193
|
+
const result = await pending;
|
|
194
|
+
dispatches.set(key, result);
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/preview/session.ts
|
|
201
|
+
function previewsDir(env = process.env) {
|
|
202
|
+
const override = env.ARTIFACTSHARE_CONFIG_HOME;
|
|
203
|
+
const base = override !== void 0 && override !== "" ? override : join(homedir(), ".artifactshare");
|
|
204
|
+
return join(base, "previews");
|
|
205
|
+
}
|
|
206
|
+
/** A recorded pid that still exists means the process is alive, even when its
|
|
207
|
+
* port is not answering probes. */
|
|
208
|
+
function processAlive(pid) {
|
|
209
|
+
try {
|
|
210
|
+
process.kill(pid, 0);
|
|
211
|
+
return true;
|
|
212
|
+
} catch (error) {
|
|
213
|
+
return error?.code === "EPERM";
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function tokenFingerprint(token) {
|
|
217
|
+
return createHash("sha256").update(token).digest("hex").slice(0, 16);
|
|
218
|
+
}
|
|
219
|
+
function sessionIdForPath(realpath) {
|
|
220
|
+
return createHash("sha256").update(realpath).digest("hex").slice(0, 16);
|
|
221
|
+
}
|
|
222
|
+
/** Session ids are the 16-hex prefix of a sha256; anything else could escape
|
|
223
|
+
* the previews directory when interpolated into a path. */
|
|
224
|
+
function isSessionId(value) {
|
|
225
|
+
return /^[0-9a-f]{16}$/.test(value);
|
|
226
|
+
}
|
|
227
|
+
function sessionFilePath(sessionId, env = process.env) {
|
|
228
|
+
if (!isSessionId(sessionId)) throw new Error("invalid preview session id");
|
|
229
|
+
return join(previewsDir(env), `${sessionId}.json`);
|
|
230
|
+
}
|
|
231
|
+
function annotationsFilePath(sessionId, env = process.env) {
|
|
232
|
+
if (!isSessionId(sessionId)) throw new Error("invalid preview session id");
|
|
233
|
+
return join(previewsDir(env), `annotations-${sessionId}.json`);
|
|
234
|
+
}
|
|
235
|
+
function previewRealpath(input) {
|
|
236
|
+
try {
|
|
237
|
+
return {
|
|
238
|
+
ok: true,
|
|
239
|
+
realpath: realpathSync(input)
|
|
240
|
+
};
|
|
241
|
+
} catch {
|
|
242
|
+
return {
|
|
243
|
+
ok: false,
|
|
244
|
+
reason: "not_found"
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/** A source deleted or renamed while its server runs has no realpath of its
|
|
249
|
+
* own, but its directory usually still does, and the recorded identity was
|
|
250
|
+
* built from the resolved path. */
|
|
251
|
+
function previewIdentityPath(input) {
|
|
252
|
+
const real = previewRealpath(input);
|
|
253
|
+
if (real.ok) return real.realpath;
|
|
254
|
+
const absolute = resolve(input);
|
|
255
|
+
const parent = previewRealpath(dirname(absolute));
|
|
256
|
+
return parent.ok ? join(parent.realpath, basename(absolute)) : absolute;
|
|
257
|
+
}
|
|
258
|
+
function writeSessionFile(session, env = process.env) {
|
|
259
|
+
const dir = previewsDir(env);
|
|
260
|
+
mkdirSync(dir, {
|
|
261
|
+
recursive: true,
|
|
262
|
+
mode: 448
|
|
263
|
+
});
|
|
264
|
+
const path = sessionFilePath(session.session_id, env);
|
|
265
|
+
const payload = {
|
|
266
|
+
schema_version: 2,
|
|
267
|
+
...session
|
|
268
|
+
};
|
|
269
|
+
const temp = join(dir, `.session-${randomUUID()}.tmp`);
|
|
270
|
+
writeFileSync(temp, JSON.stringify(payload, null, 2), { mode: 384 });
|
|
271
|
+
renameSync(temp, path);
|
|
272
|
+
chmodSync(path, 384);
|
|
273
|
+
return path;
|
|
274
|
+
}
|
|
275
|
+
function readSessionFile(sessionId, env = process.env) {
|
|
276
|
+
let parsed;
|
|
277
|
+
try {
|
|
278
|
+
parsed = JSON.parse(readFileSync(sessionFilePath(sessionId, env), "utf8"));
|
|
279
|
+
} catch {
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
283
|
+
const record = parsed;
|
|
284
|
+
if (record.schema_version !== 1 && record.schema_version !== 2 || typeof record.session_id !== "string" || typeof record.realpath !== "string" || typeof record.port !== "number" || typeof record.share_port !== "number" || typeof record.pid !== "number" || typeof record.started_at !== "string" || !isSessionCredentials(record.credentials)) return null;
|
|
285
|
+
if (record.schema_version === 1) return {
|
|
286
|
+
schema_version: 2,
|
|
287
|
+
session_id: record.session_id,
|
|
288
|
+
realpath: record.realpath,
|
|
289
|
+
port: record.port,
|
|
290
|
+
share_port: record.share_port,
|
|
291
|
+
pid: record.pid,
|
|
292
|
+
started_at: record.started_at,
|
|
293
|
+
credentials: record.credentials,
|
|
294
|
+
agent_notification: {
|
|
295
|
+
provider: "generic",
|
|
296
|
+
transport: "legacy",
|
|
297
|
+
capability: "manual",
|
|
298
|
+
target: null,
|
|
299
|
+
registered_at: record.started_at
|
|
300
|
+
},
|
|
301
|
+
legacy: true
|
|
302
|
+
};
|
|
303
|
+
if (!isPreviewNotificationRegistration(record.agent_notification)) return null;
|
|
304
|
+
return parsed;
|
|
305
|
+
}
|
|
306
|
+
function updateSessionNotificationRegistration(sessionId, registration, env = process.env) {
|
|
307
|
+
const current = readSessionFile(sessionId, env);
|
|
308
|
+
if (!current || current.pid !== process.pid) return;
|
|
309
|
+
writeSessionFile({
|
|
310
|
+
session_id: current.session_id,
|
|
311
|
+
realpath: current.realpath,
|
|
312
|
+
port: current.port,
|
|
313
|
+
share_port: current.share_port,
|
|
314
|
+
pid: current.pid,
|
|
315
|
+
started_at: current.started_at,
|
|
316
|
+
credentials: current.credentials,
|
|
317
|
+
agent_notification: registration
|
|
318
|
+
}, env);
|
|
319
|
+
}
|
|
320
|
+
function isSessionCredentials(value) {
|
|
321
|
+
if (typeof value !== "object" || value === null) return false;
|
|
322
|
+
const record = value;
|
|
323
|
+
const optional = (name) => record[name] === null || typeof record[name] === "string";
|
|
324
|
+
return optional("profile") && optional("base_url") && optional("token_fingerprint") && typeof record.cwd === "string";
|
|
325
|
+
}
|
|
326
|
+
function removeSessionFile(sessionId, env = process.env) {
|
|
327
|
+
rmSync(sessionFilePath(sessionId, env), { force: true });
|
|
328
|
+
}
|
|
329
|
+
async function resolveLiveSession(filePath, fetchImpl = globalThis.fetch, env = process.env) {
|
|
330
|
+
const session = readSessionFile(sessionIdForPath(previewIdentityPath(filePath)), env);
|
|
331
|
+
if (!session) return { state: "none" };
|
|
332
|
+
return await probeSession(session, fetchImpl, env);
|
|
333
|
+
}
|
|
334
|
+
/** Probe a record the caller already holds. `--session <id>` names one exactly,
|
|
335
|
+
* and re-deriving its id from the recorded path would follow a symlink that has
|
|
336
|
+
* since been repointed and miss the record the caller asked for. */
|
|
337
|
+
async function probeSession(session, fetchImpl = globalThis.fetch, env = process.env) {
|
|
338
|
+
const sessionId = session.session_id;
|
|
339
|
+
let identity = null;
|
|
340
|
+
let answered = false;
|
|
341
|
+
try {
|
|
342
|
+
const response = await fetchImpl(`http://127.0.0.1:${session.port}${PREVIEW_SESSION_ENDPOINT}`, { signal: AbortSignal.timeout(2e3) });
|
|
343
|
+
answered = true;
|
|
344
|
+
if (response.ok) identity = await response.json().catch(() => null);
|
|
345
|
+
} catch (error) {
|
|
346
|
+
identity = null;
|
|
347
|
+
answered = isConnectionRefused(error);
|
|
348
|
+
}
|
|
349
|
+
if (isPreviewSessionIdentity(identity) && identity.session_id === session.session_id && identity.realpath === session.realpath && identity.share_port === session.share_port) {
|
|
350
|
+
if (session.legacy) return {
|
|
351
|
+
state: "legacy",
|
|
352
|
+
session
|
|
353
|
+
};
|
|
354
|
+
return {
|
|
355
|
+
state: "live",
|
|
356
|
+
session
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
if (!answered) return {
|
|
360
|
+
state: "unverified",
|
|
361
|
+
session
|
|
362
|
+
};
|
|
363
|
+
removeSessionFile(sessionId, env);
|
|
364
|
+
return {
|
|
365
|
+
state: "none",
|
|
366
|
+
reclaimed: true
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
/** A refused connection proves nothing listens on the recorded port. */
|
|
370
|
+
function isConnectionRefused(error) {
|
|
371
|
+
const causeCode = error?.cause?.code;
|
|
372
|
+
const code = error?.code;
|
|
373
|
+
return causeCode === "ECONNREFUSED" || code === "ECONNREFUSED";
|
|
374
|
+
}
|
|
375
|
+
/** Claim the right to start a session for this path. The lock file is created
|
|
376
|
+
* exclusively, so two `preview <same file>` invocations racing before either
|
|
377
|
+
* writes its session file cannot both win and end up sharing one annotation
|
|
378
|
+
* store. */
|
|
379
|
+
function claimSessionStart(sessionId, env = process.env) {
|
|
380
|
+
const dir = previewsDir(env);
|
|
381
|
+
mkdirSync(dir, {
|
|
382
|
+
recursive: true,
|
|
383
|
+
mode: 448
|
|
384
|
+
});
|
|
385
|
+
const lockPath = join(dir, `${sessionId}.lock`);
|
|
386
|
+
try {
|
|
387
|
+
const handle = openSync(lockPath, "wx", 384);
|
|
388
|
+
writeSync(handle, String(process.pid));
|
|
389
|
+
closeSync(handle);
|
|
390
|
+
} catch {
|
|
391
|
+
return { ok: false };
|
|
392
|
+
}
|
|
393
|
+
return {
|
|
394
|
+
ok: true,
|
|
395
|
+
release: () => {
|
|
396
|
+
rmSync(lockPath, { force: true });
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/** Clears an abandoned claim and reports whether none remains. A live
|
|
401
|
+
* claimant means a start is still in flight: freeing its lock would let a
|
|
402
|
+
* second server open the same annotation store. */
|
|
403
|
+
function releaseStaleClaim(sessionId, env = process.env) {
|
|
404
|
+
const lockPath = join(previewsDir(env), `${sessionId}.lock`);
|
|
405
|
+
let recorded;
|
|
406
|
+
try {
|
|
407
|
+
recorded = readFileSync(lockPath, "utf8").trim();
|
|
408
|
+
} catch {
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
const pid = Number.parseInt(recorded, 10);
|
|
412
|
+
if (Number.isInteger(pid) && pid > 0 && processAlive(pid)) return false;
|
|
413
|
+
rmSync(lockPath, { force: true });
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
//#endregion
|
|
417
|
+
//#region src/preview/cursor-notification.ts
|
|
418
|
+
const PREVIEW_SESSION_PATTERN = /^[0-9a-f]{16}$/;
|
|
419
|
+
const BATCH_ID_PATTERN = /^[a-z0-9_-]{1,128}$/i;
|
|
420
|
+
const TOKEN_PATTERN = /^[0-9a-f]{64}$/;
|
|
421
|
+
const CURSOR_ACP_ACK_TIMEOUT_MS = 6e4;
|
|
422
|
+
const CURSOR_ACP_FETCH_TIMEOUT_MS = 65e3;
|
|
423
|
+
const CURSOR_ACP_NOTIFICATION_TIMEOUT_MS = 7e4;
|
|
424
|
+
function isCursorAcpTarget(value) {
|
|
425
|
+
if (typeof value !== "object" || value === null) return false;
|
|
426
|
+
const target = value;
|
|
427
|
+
if (target.schema_version !== 1 || typeof target.endpoint !== "string" || typeof target.token !== "string" || !TOKEN_PATTERN.test(target.token) || typeof target.pid !== "number" || !Number.isSafeInteger(target.pid) || typeof target.session_id !== "string" || target.session_id.length === 0 || typeof target.cwd !== "string" || target.cwd.length === 0) return false;
|
|
428
|
+
try {
|
|
429
|
+
const url = new URL(target.endpoint);
|
|
430
|
+
return url.protocol === "http:" && url.hostname === "127.0.0.1" && url.pathname === "/preview-batch" && url.username === "" && url.password === "";
|
|
431
|
+
} catch {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
function manual(now) {
|
|
436
|
+
return {
|
|
437
|
+
provider: "cursor",
|
|
438
|
+
transport: "manual",
|
|
439
|
+
capability: "manual",
|
|
440
|
+
target: null,
|
|
441
|
+
registered_at: now().toISOString()
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
function cursorNotificationRegistration(environment = process.env, now = () => /* @__PURE__ */ new Date()) {
|
|
445
|
+
const encoded = environment.ARTIFACTSHARE_CURSOR_ACP_TARGET?.trim();
|
|
446
|
+
if (encoded) {
|
|
447
|
+
try {
|
|
448
|
+
const target = JSON.parse(encoded);
|
|
449
|
+
if (isCursorAcpTarget(target) && processAlive(target.pid)) return {
|
|
450
|
+
provider: "cursor",
|
|
451
|
+
transport: "acp_managed",
|
|
452
|
+
capability: "push",
|
|
453
|
+
target: encoded,
|
|
454
|
+
registered_at: now().toISOString()
|
|
455
|
+
};
|
|
456
|
+
} catch {}
|
|
457
|
+
return manual(now);
|
|
458
|
+
}
|
|
459
|
+
if (!environment.CURSOR_AGENT) return null;
|
|
460
|
+
if (environment.ARTIFACTSHARE_CURSOR_FOREGROUND_WAIT === "1") return {
|
|
461
|
+
provider: "cursor",
|
|
462
|
+
transport: "foreground_wait",
|
|
463
|
+
capability: "wait",
|
|
464
|
+
target: null,
|
|
465
|
+
registered_at: now().toISOString()
|
|
466
|
+
};
|
|
467
|
+
return manual(now);
|
|
468
|
+
}
|
|
469
|
+
function createCursorAcpAdapter(encoded, fetchImpl = globalThis.fetch) {
|
|
470
|
+
let value;
|
|
471
|
+
try {
|
|
472
|
+
value = JSON.parse(encoded);
|
|
473
|
+
} catch {
|
|
474
|
+
throw new Error("Cursor ACP target must be valid JSON.");
|
|
475
|
+
}
|
|
476
|
+
if (!isCursorAcpTarget(value)) throw new Error("Cursor ACP target must be valid.");
|
|
477
|
+
const target = value;
|
|
478
|
+
return {
|
|
479
|
+
fallbackIfUnavailable() {
|
|
480
|
+
return processAlive(target.pid) ? null : manual(() => /* @__PURE__ */ new Date());
|
|
481
|
+
},
|
|
482
|
+
async dispatch(event) {
|
|
483
|
+
if (!PREVIEW_SESSION_PATTERN.test(event.preview_session_id) || !BATCH_ID_PATTERN.test(event.batch_id)) return {
|
|
484
|
+
status: "failed",
|
|
485
|
+
code: "invalid_response",
|
|
486
|
+
retryable: false
|
|
487
|
+
};
|
|
488
|
+
try {
|
|
489
|
+
const response = await fetchImpl(target.endpoint, {
|
|
490
|
+
method: "POST",
|
|
491
|
+
headers: {
|
|
492
|
+
authorization: `Bearer ${target.token}`,
|
|
493
|
+
"content-type": "application/json"
|
|
494
|
+
},
|
|
495
|
+
body: JSON.stringify(event),
|
|
496
|
+
signal: AbortSignal.timeout(CURSOR_ACP_FETCH_TIMEOUT_MS)
|
|
497
|
+
});
|
|
498
|
+
if (response.status === 202) return { status: "accepted" };
|
|
499
|
+
if (response.status === 409) return {
|
|
500
|
+
status: "failed",
|
|
501
|
+
code: "rejected",
|
|
502
|
+
retryable: true
|
|
503
|
+
};
|
|
504
|
+
return {
|
|
505
|
+
status: "failed",
|
|
506
|
+
code: response.status === 404 || response.status === 503 ? "target_unavailable" : "rejected",
|
|
507
|
+
retryable: true
|
|
508
|
+
};
|
|
509
|
+
} catch (error) {
|
|
510
|
+
return {
|
|
511
|
+
status: "failed",
|
|
512
|
+
code: error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError") ? "timeout" : "target_unavailable",
|
|
513
|
+
retryable: true
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
//#endregion
|
|
520
|
+
export { defaultPreviewNotificationRegistration as C, isPreviewAnchor as D, PREVIEW_SESSION_ENDPOINT as E, isPreviewAnnotation as O, createPreviewNotificationCoordinator as S, PREVIEW_MUTATION_HEADER as T, resolveLiveSession as _, annotationsFilePath as a, updateSessionNotificationRegistration as b, isSessionId as c, previewsDir as d, probeSession as f, removeSessionFile as g, releaseStaleClaim as h, cursorNotificationRegistration as i, isPreviewDoneItemInput as k, previewIdentityPath as l, readSessionFile as m, CURSOR_ACP_NOTIFICATION_TIMEOUT_MS as n, claimSessionStart as o, processAlive as p, createCursorAcpAdapter as r, isConnectionRefused as s, CURSOR_ACP_ACK_TIMEOUT_MS as t, previewRealpath as u, sessionIdForPath as v, samePreviewNotificationRegistration as w, writeSessionFile as x, tokenFingerprint as y };
|