@clipy/cli 0.15.0 → 0.16.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/README.md +53 -3
- package/dist/cdpClient.js +277 -0
- package/dist/cdpDiagnostics.js +644 -0
- package/dist/chromeExtensionCapture.js +197 -56
- package/dist/chromeForClipy.js +588 -111
- package/dist/index.js +1187 -125
- package/dist/linuxScreen.js +655 -0
- package/dist/macBridge.js +11 -1
- package/dist/processProbe.js +265 -0
- package/dist/skill.js +55 -21
- package/package.json +4 -4
|
@@ -13,9 +13,14 @@
|
|
|
13
13
|
* Nothing is kept in a CLI process between commands: start, stop, abort and
|
|
14
14
|
* status each reconnect, so `session start` can return and the agent can drive
|
|
15
15
|
* the tab with its own tools in between.
|
|
16
|
+
*
|
|
17
|
+
* Raw CDP (cdpClient.ts), not Playwright: this path attaches only to the two
|
|
18
|
+
* background tabs it opens, so a stuck tab elsewhere in the person's Chrome
|
|
19
|
+
* for Clipy cannot hang it, and it needs no Playwright install.
|
|
16
20
|
*/
|
|
17
21
|
import { randomUUID } from "node:crypto";
|
|
18
|
-
import {
|
|
22
|
+
import { CdpConnection, CdpTab } from "./cdpClient.js";
|
|
23
|
+
import { clipyExtensionId, startChrome } from "./chromeForClipy.js";
|
|
19
24
|
const AGENT_PAGE_PATH = "src/agent/agent.html";
|
|
20
25
|
const AGENT_PROTOCOL = 1;
|
|
21
26
|
const BUSY_STATES = new Set(["starting", "countdown", "recording", "paused", "uploading"]);
|
|
@@ -29,52 +34,57 @@ export class ExtensionUnavailableError extends Error {
|
|
|
29
34
|
* from stop is treated as transient and the file is kept. */
|
|
30
35
|
export class ExtensionRecordingEndedError extends Error {
|
|
31
36
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
/** The extension bounds capture setup (OFFSCREEN_PREPARE) at 120s, so a start
|
|
38
|
+
* can legitimately take that long; wait past it for the definite answer. */
|
|
39
|
+
const AGENT_START_TIMEOUT_MS = 150_000;
|
|
40
|
+
// A start that never got going leaves the extension idle; give its access
|
|
41
|
+
// check this long to begin before deciding nothing is coming.
|
|
42
|
+
const ORPHAN_IDLE_GRACE_MS = 15_000;
|
|
43
|
+
/** The agent page is opened in the background: a foreground tab would make
|
|
44
|
+
* the extension think the person left the recorded tab. */
|
|
45
|
+
async function connectAgent(port, env) {
|
|
46
|
+
const conn = await CdpConnection.connect(port);
|
|
47
|
+
let agent;
|
|
48
|
+
try {
|
|
49
|
+
agent = await CdpTab.openBackground(conn, `chrome-extension://${clipyExtensionId(env)}/${AGENT_PAGE_PATH}`);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
conn.close();
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
if (!(await agent.waitFor("typeof globalThis.clipyAgent === 'object'", 10_000))) {
|
|
56
|
+
await agent.close();
|
|
57
|
+
conn.close();
|
|
58
|
+
throw new ExtensionUnavailableError(`the Clipy extension (with agent support) is not installed in Chrome for Clipy. ` +
|
|
59
|
+
`Run \`clipy chrome setup\`, or install it from ${extensionStoreUrl(env)} in that window.`);
|
|
38
60
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
};
|
|
44
|
-
const missing = () => new ExtensionUnavailableError(`the Clipy extension (with agent support) is not installed in Chrome for Clipy. ` +
|
|
45
|
-
`Run \`clipy chrome setup\`, or install it from ${extensionStoreUrl(env)} in that window.`);
|
|
61
|
+
return { conn, agent };
|
|
62
|
+
}
|
|
63
|
+
async function withAgent(port, env, fn) {
|
|
64
|
+
const { conn, agent } = await connectAgent(port, env);
|
|
46
65
|
try {
|
|
47
|
-
await agent.
|
|
48
|
-
waitUntil: "load",
|
|
49
|
-
timeout: 15_000,
|
|
50
|
-
});
|
|
51
|
-
await agent.waitForFunction("typeof globalThis.clipyAgent === 'object'", undefined, { timeout: 10_000 });
|
|
66
|
+
return await fn({ conn, call: (expression, timeoutMs) => agent.evaluate(`globalThis.clipyAgent.${expression}`, timeoutMs) });
|
|
52
67
|
}
|
|
53
|
-
|
|
54
|
-
await close();
|
|
55
|
-
|
|
68
|
+
finally {
|
|
69
|
+
await agent.close();
|
|
70
|
+
conn.close();
|
|
56
71
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
call: (expression) => agent.evaluate(`globalThis.clipyAgent.${expression}`),
|
|
62
|
-
close,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
async function withAgent(chromium, port, env, fn) {
|
|
66
|
-
const conn = await connectAgent(chromium, port, env);
|
|
72
|
+
}
|
|
73
|
+
/** Opens a page in FRONT, for the steps a person has to do (install, sign in). */
|
|
74
|
+
export async function openTabForPerson(port, url) {
|
|
75
|
+
const conn = await CdpConnection.connect(port);
|
|
67
76
|
try {
|
|
68
|
-
|
|
77
|
+
const { targetId } = await conn.send("Target.createTarget", { url });
|
|
78
|
+
await conn.send("Target.activateTarget", { targetId }).catch(() => { });
|
|
69
79
|
}
|
|
70
80
|
finally {
|
|
71
|
-
|
|
81
|
+
conn.close();
|
|
72
82
|
}
|
|
73
83
|
}
|
|
74
84
|
/** Read-only: is the extension installed, current enough, and signed in? */
|
|
75
|
-
export async function extensionReadiness(
|
|
85
|
+
export async function extensionReadiness(port, env) {
|
|
76
86
|
try {
|
|
77
|
-
return await withAgent(
|
|
87
|
+
return await withAgent(port, env, async ({ call }) => {
|
|
78
88
|
const res = await call("status(undefined, true)");
|
|
79
89
|
if (!res.ok || !res.agent) {
|
|
80
90
|
return { installed: true, version: null, signedIn: false, email: null, recorderState: null, error: res.error ?? "the extension did not answer" };
|
|
@@ -133,12 +143,12 @@ async function resolveTabId(target, call) {
|
|
|
133
143
|
.catch(() => { });
|
|
134
144
|
}
|
|
135
145
|
}
|
|
136
|
-
export async function startExtensionRecording(
|
|
146
|
+
export async function startExtensionRecording(opts) {
|
|
137
147
|
const started = await startChrome(opts.home, opts.env, opts.platform, opts.port);
|
|
138
148
|
if (!started.ok)
|
|
139
149
|
throw new Error(`Chrome for Clipy is not available: ${started.error}`);
|
|
140
150
|
const port = started.state.port;
|
|
141
|
-
return withAgent(
|
|
151
|
+
return withAgent(port, opts.env, async ({ conn, call }) => {
|
|
142
152
|
const ready = await call("status(undefined, true)");
|
|
143
153
|
if (!ready.ok || !ready.agent)
|
|
144
154
|
throw new Error(ready.error ?? "the Clipy extension did not answer");
|
|
@@ -151,29 +161,64 @@ export async function startExtensionRecording(chromium, opts) {
|
|
|
151
161
|
if (ready.state && BUSY_STATES.has(ready.state.status)) {
|
|
152
162
|
throw new Error(`the Clipy extension is already ${ready.state.status}; stop or abort that recording first`);
|
|
153
163
|
}
|
|
154
|
-
const
|
|
164
|
+
const features = ready.agent.features ?? [];
|
|
165
|
+
const warnings = [];
|
|
166
|
+
if (opts.name && !features.includes("name")) {
|
|
167
|
+
warnings.push(`extension ${ready.agent.version} ignores --name; the recording gets an automatic title. Update the Clipy extension.`);
|
|
168
|
+
}
|
|
169
|
+
if (!features.includes("tab-audio")) {
|
|
170
|
+
warnings.push(`extension ${ready.agent.version} always records the tab's sound; any speech in it will replace your marks as the transcript. Update the Clipy extension.`);
|
|
171
|
+
}
|
|
172
|
+
if (!features.includes("narration")) {
|
|
173
|
+
warnings.push(`extension ${ready.agent.version} cannot receive marks; clipy mark will not reach this recording. Update the Clipy extension.`);
|
|
174
|
+
}
|
|
175
|
+
// In the background too: the agent drives this tab over CDP while the
|
|
176
|
+
// person keeps using whatever is in front, and tab capture does not need
|
|
177
|
+
// the tab to be visible.
|
|
178
|
+
const target = await CdpTab.openBackground(conn, opts.targetUrl);
|
|
155
179
|
try {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
catch {
|
|
180
|
+
// A new tab is a complete about:blank before the real page commits.
|
|
181
|
+
if (!(await target.waitFor("location.href !== 'about:blank' && document.readyState === 'complete'", 30_000))) {
|
|
160
182
|
opts.log("target page load timed out; recording its current state anyway");
|
|
161
183
|
}
|
|
162
|
-
await target.bringToFront().catch(() => { });
|
|
163
184
|
const tabId = await resolveTabId(target, call);
|
|
164
|
-
const
|
|
185
|
+
const startToken = randomUUID();
|
|
186
|
+
let res;
|
|
187
|
+
try {
|
|
188
|
+
res = await call(`start(${tabId}, ${Math.round(opts.maxSec)}, ${JSON.stringify(opts.name ?? null)}, ${opts.tabAudio === true}, ${JSON.stringify(startToken)})`, AGENT_START_TIMEOUT_MS);
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
// No answer from the extension: a start may still have gone through.
|
|
192
|
+
// Cancel it rather than leave an unowned recording to upload.
|
|
193
|
+
const orphan = await cancelOrphanedAgentStart(port, opts.env, opts.log, startToken);
|
|
194
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
195
|
+
if (orphan.uploaded)
|
|
196
|
+
throw new Error(`${message}; the recording finished and uploaded anyway: ${orphan.uploaded}`);
|
|
197
|
+
if (orphan.unresolved) {
|
|
198
|
+
throw new Error(`${message}; recording ${orphan.unresolved.recordingId} may still be running in Chrome for Clipy and could not be cancelled ` +
|
|
199
|
+
`(${orphan.unresolved.error}). Stop it from the Clipy extension in that window.`);
|
|
200
|
+
}
|
|
201
|
+
if (orphan.unchecked) {
|
|
202
|
+
throw new Error(`${message}; could not check whether the recording started anyway (${orphan.unchecked}). ` +
|
|
203
|
+
"Look at the Clipy extension in Chrome for Clipy and stop it there if it is recording.");
|
|
204
|
+
}
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
165
207
|
if (!res.ok || !res.agent?.activeRecordingId) {
|
|
166
208
|
throw new Error(explainStartError(res.error ?? `recording did not start (state: ${res.state?.status ?? "unknown"})`));
|
|
167
209
|
}
|
|
168
210
|
opts.log(`extension ${res.agent.version} recording tab ${tabId} (${res.agent.activeRecordingId})`);
|
|
169
211
|
return {
|
|
170
212
|
recordingId: res.agent.activeRecordingId,
|
|
171
|
-
startedAtEpochMs: Date.now(),
|
|
213
|
+
startedAtEpochMs: typeof res.state?.startedAt === "number" ? res.state.startedAt : Date.now(),
|
|
172
214
|
extensionVersion: res.agent.version,
|
|
215
|
+
features: res.agent.features ?? [],
|
|
173
216
|
cdpHttpUrl: `http://127.0.0.1:${port}`,
|
|
174
217
|
chromePid: started.state.pid,
|
|
175
218
|
port,
|
|
176
|
-
pageUrl: target.url(),
|
|
219
|
+
pageUrl: await target.url().catch(() => opts.targetUrl),
|
|
220
|
+
targetId: target.targetId,
|
|
221
|
+
warnings,
|
|
177
222
|
};
|
|
178
223
|
}
|
|
179
224
|
catch (error) {
|
|
@@ -182,11 +227,84 @@ export async function startExtensionRecording(chromium, opts) {
|
|
|
182
227
|
}
|
|
183
228
|
});
|
|
184
229
|
}
|
|
230
|
+
async function cancelOrphanedAgentStart(port, env, log, startToken) {
|
|
231
|
+
try {
|
|
232
|
+
return await withAgent(port, env, async ({ call }) => {
|
|
233
|
+
// Retried, then reported: a cancel that silently failed would leave a
|
|
234
|
+
// recording running with no session to stop or collect it.
|
|
235
|
+
const cancel = async (id) => {
|
|
236
|
+
let lastError = "";
|
|
237
|
+
for (let attempt = 1; attempt <= 3; attempt++) {
|
|
238
|
+
const res = await call(`cancel(${JSON.stringify(id)})`, 30_000).catch((error) => ({
|
|
239
|
+
ok: false,
|
|
240
|
+
error: error instanceof Error ? error.message : String(error),
|
|
241
|
+
}));
|
|
242
|
+
if (res.ok || /not active/.test(res.error ?? "")) {
|
|
243
|
+
log(`cancelled recording ${id}, which started after the CLI stopped waiting for it`);
|
|
244
|
+
return {};
|
|
245
|
+
}
|
|
246
|
+
if (/^already uploaded: /.test(res.error ?? ""))
|
|
247
|
+
return { uploaded: res.error.replace(/^already uploaded: /, "") };
|
|
248
|
+
lastError = res.error ?? "cancel failed";
|
|
249
|
+
if (attempt < 3)
|
|
250
|
+
await new Promise((r) => setTimeout(r, 500 * attempt));
|
|
251
|
+
}
|
|
252
|
+
return { unresolved: { recordingId: id, error: lastError } };
|
|
253
|
+
};
|
|
254
|
+
// Refuse the start outright if it has not passed the access check yet
|
|
255
|
+
// (that check has no timeout of its own).
|
|
256
|
+
const first = await call(`abandonStart(${JSON.stringify(startToken)})`, 10_000).catch(() => null);
|
|
257
|
+
const canAbandon = Boolean(first?.agent?.features?.includes("abandon-start"));
|
|
258
|
+
// While the extension is busy starting, keep watching through its whole
|
|
259
|
+
// setup window: a recording can still begin near the end of it.
|
|
260
|
+
const idleDeadline = Date.now() + ORPHAN_IDLE_GRACE_MS;
|
|
261
|
+
const hardDeadline = Date.now() + AGENT_START_TIMEOUT_MS;
|
|
262
|
+
let sawStart = false;
|
|
263
|
+
for (;;) {
|
|
264
|
+
const status = await call("status()", 10_000);
|
|
265
|
+
const mine = status.agent?.lastStart?.token === startToken ? status.agent.lastStart.recordingId : null;
|
|
266
|
+
if (mine) {
|
|
267
|
+
if (status.agent?.activeRecordingId === mine)
|
|
268
|
+
return await cancel(mine);
|
|
269
|
+
const result = (await call(`status(${JSON.stringify(mine)})`, 10_000)).agent?.result;
|
|
270
|
+
if (result?.status === "complete")
|
|
271
|
+
return { uploaded: result.shareUrl ?? result.publicId ?? mine };
|
|
272
|
+
// Still retrying the upload: cancelling drops the queued replay. An
|
|
273
|
+
// error result already means nothing was uploaded.
|
|
274
|
+
return result?.status === "recovering" ? await cancel(mine) : {};
|
|
275
|
+
}
|
|
276
|
+
// An extension without start tokens: any agent recording now live is ours.
|
|
277
|
+
if (!status.agent?.features?.includes("start-token")) {
|
|
278
|
+
const id = status.agent?.agentControlled ? status.agent.activeRecordingId : null;
|
|
279
|
+
if (id)
|
|
280
|
+
return await cancel(id);
|
|
281
|
+
}
|
|
282
|
+
// Our start is still in the extension's hands (possibly its access
|
|
283
|
+
// check): keep watching, since it may yet begin or be refused.
|
|
284
|
+
const pending = status.agent?.pendingStart === startToken;
|
|
285
|
+
const busy = pending || Boolean(status.state && BUSY_STATES.has(status.state.status));
|
|
286
|
+
if (canAbandon && !pending && !mine)
|
|
287
|
+
return {};
|
|
288
|
+
if (sawStart && !busy)
|
|
289
|
+
return {};
|
|
290
|
+
sawStart ||= busy;
|
|
291
|
+
const now = Date.now();
|
|
292
|
+
if (now >= hardDeadline || (!busy && now >= idleDeadline))
|
|
293
|
+
return {};
|
|
294
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
return { unchecked: error instanceof Error ? error.message : String(error) };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
185
302
|
/** Stop (or collect an auto-stopped) recording and wait for its upload. */
|
|
186
|
-
export async function stopExtensionRecording(
|
|
187
|
-
return withAgent(
|
|
303
|
+
export async function stopExtensionRecording(opts) {
|
|
304
|
+
return withAgent(opts.port, opts.env, async ({ call }) => {
|
|
188
305
|
const id = JSON.stringify(opts.recordingId);
|
|
189
|
-
const
|
|
306
|
+
const narration = opts.notes?.length ? JSON.stringify({ notes: opts.notes }) : "undefined";
|
|
307
|
+
const stopped = await call(`stop(${id}, ${narration})`);
|
|
190
308
|
if (!stopped.ok) {
|
|
191
309
|
const message = stopped.error ?? "the extension refused to stop the recording";
|
|
192
310
|
throw /not active/.test(message) ? new ExtensionRecordingEndedError(message) : new Error(message);
|
|
@@ -205,24 +323,47 @@ export async function stopExtensionRecording(chromium, opts) {
|
|
|
205
323
|
const reason = res.state?.status === "error" && res.state.message ? `: ${res.state.message}` : "";
|
|
206
324
|
throw new ExtensionRecordingEndedError(`the recording ended without an upload${reason}`);
|
|
207
325
|
}
|
|
208
|
-
if (Date.now() >= deadline)
|
|
209
|
-
|
|
326
|
+
if (Date.now() >= deadline) {
|
|
327
|
+
// Plain Error, not ended: the session is kept so a later stop can collect it.
|
|
328
|
+
throw new Error(result?.status === "recovering"
|
|
329
|
+
? "the extension is still retrying the upload; run `clipy session stop` again shortly to collect the link"
|
|
330
|
+
: "timed out waiting for the extension to finish uploading");
|
|
331
|
+
}
|
|
210
332
|
await new Promise((r) => setTimeout(r, 1000));
|
|
211
333
|
res = await call(`status(${id})`);
|
|
212
334
|
}
|
|
213
335
|
});
|
|
214
336
|
}
|
|
337
|
+
/** Hands one mark to the extension as it happens, so a recording that stops
|
|
338
|
+
* on its own (--max) still uploads with it. Best effort: the CLI keeps its own
|
|
339
|
+
* copy and sends everything again at stop. */
|
|
340
|
+
export async function sendExtensionMark(opts) {
|
|
341
|
+
return withAgent(opts.port, opts.env, async ({ call }) => {
|
|
342
|
+
const args = [JSON.stringify(opts.recordingId), Math.round(opts.startMs), JSON.stringify(opts.text)];
|
|
343
|
+
if (opts.summary)
|
|
344
|
+
args.push(JSON.stringify(opts.summary));
|
|
345
|
+
const res = await call(`mark(${args.join(", ")})`);
|
|
346
|
+
// "not active": the recording already stopped on its own (--max) and
|
|
347
|
+
// uploaded, so nothing sent now can reach it.
|
|
348
|
+
return { ok: res.ok, ended: !res.ok && /not active/.test(res.error ?? ""), error: res.error };
|
|
349
|
+
});
|
|
350
|
+
}
|
|
215
351
|
/** Resolves once the extension has cancelled the recording or confirms it is
|
|
216
352
|
* no longer active; throws when the extension could not be asked. */
|
|
217
|
-
|
|
218
|
-
|
|
353
|
+
/** The recording reached the library before the abort; `session stop` collects it. */
|
|
354
|
+
export class ExtensionAlreadyUploadedError extends Error {
|
|
355
|
+
}
|
|
356
|
+
export async function abortExtensionRecording(opts) {
|
|
357
|
+
await withAgent(opts.port, opts.env, async ({ call }) => {
|
|
219
358
|
const res = await call(`cancel(${JSON.stringify(opts.recordingId)})`);
|
|
359
|
+
if (!res.ok && /^already uploaded/.test(res.error ?? ""))
|
|
360
|
+
throw new ExtensionAlreadyUploadedError(res.error);
|
|
220
361
|
if (!res.ok && !/not active/.test(res.error ?? ""))
|
|
221
362
|
throw new Error(res.error ?? "cancel failed");
|
|
222
363
|
});
|
|
223
364
|
}
|
|
224
|
-
export async function extensionRecordingStatus(
|
|
225
|
-
return withAgent(
|
|
365
|
+
export async function extensionRecordingStatus(opts) {
|
|
366
|
+
return withAgent(opts.port, opts.env, async ({ call }) => {
|
|
226
367
|
const res = await call(`status(${JSON.stringify(opts.recordingId)})`);
|
|
227
368
|
if (!res.ok || !res.agent)
|
|
228
369
|
throw new Error(res.error ?? "the extension did not answer");
|