@clipy/cli 0.12.0 → 0.13.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 +44 -11
- package/dist/browserLogin.js +2 -23
- package/dist/context/importCmd.js +147 -21
- package/dist/context/loom.js +30 -0
- package/dist/context/videoFetch.js +41 -20
- package/dist/context-core/index.js +1 -0
- package/dist/context-core/loom.js +293 -0
- package/dist/index.js +157 -42
- package/dist/openUrl.js +60 -0
- package/dist/proofFrames.js +4 -4
- package/dist/setup.js +203 -0
- package/dist/setupConfig.js +128 -0
- package/dist/setupTypes.js +7 -0
- package/dist/skill.js +26 -10
- package/package.json +5 -4
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// GENERATED from lib/context-core — do not edit here
|
|
2
|
+
/**
|
|
3
|
+
* Loom share links → title, duration and transcript, over plain HTTPS.
|
|
4
|
+
*
|
|
5
|
+
* Loom publishes both of these free and unauthenticated for a public share
|
|
6
|
+
* link, so the transcript half of a Loom import needs no yt-dlp, no ffmpeg and
|
|
7
|
+
* no browser — two fetches and the bundle compiles. yt-dlp is only reached for
|
|
8
|
+
* later on, and only if the server asks for frames.
|
|
9
|
+
*
|
|
10
|
+
* Everything that comes back is somebody else's content. Loom's own share page
|
|
11
|
+
* embeds instructions aimed at AI agents, so this surface is hostile input by
|
|
12
|
+
* default: titles and transcript text are returned as bounded DATA, never
|
|
13
|
+
* interpolated into a command, a path, or a prompt. Fencing them for an agent is
|
|
14
|
+
* the caller's job, and the caller must use an existing warning rather than
|
|
15
|
+
* writing one: `renderArecMarkdown` in ./arec for an AREC bundle, the
|
|
16
|
+
* `instructions` block in app/api/agent-context/_shared.ts for that JSON shape.
|
|
17
|
+
*
|
|
18
|
+
* Shared by construction. This lives in context-core because the CLI, the
|
|
19
|
+
* Next.js server and the Chrome extension all consume this directory, and two
|
|
20
|
+
* independent Loom fetchers would drift. That constrains it: zero runtime
|
|
21
|
+
* dependencies, no node builtins, and NO `process` — the extension bundles this
|
|
22
|
+
* through Vite, where `process` does not exist. The origin override is therefore
|
|
23
|
+
* injected by the caller rather than read from the environment here.
|
|
24
|
+
*/
|
|
25
|
+
/** Loom ids are exactly 32 hex chars. Same shape the web app validates. */
|
|
26
|
+
const LOOM_ID = /^[a-f0-9]{32}$/i;
|
|
27
|
+
const LOOM_HOSTS = new Set(["loom.com", "www.loom.com"]);
|
|
28
|
+
const DEFAULT_ORIGIN = "https://www.loom.com";
|
|
29
|
+
function resolveOrigin(opts) {
|
|
30
|
+
return (opts?.origin || DEFAULT_ORIGIN).replace(/\/+$/, "");
|
|
31
|
+
}
|
|
32
|
+
function resolveTimeout(opts) {
|
|
33
|
+
const requested = opts?.timeoutMs;
|
|
34
|
+
return typeof requested === "number" && Number.isFinite(requested) && requested > 0
|
|
35
|
+
? requested
|
|
36
|
+
: DEFAULT_TIMEOUT_MS;
|
|
37
|
+
}
|
|
38
|
+
/** Loom's GraphQL rejects a plain fetch UA. */
|
|
39
|
+
const BROWSER_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0 Safari/537.36";
|
|
40
|
+
/** Batch-caller default. Overridable per call via `LoomFetchOptions.timeoutMs`. */
|
|
41
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
42
|
+
/** A transcript is text. Anything past this is not one, and we will not buffer it. */
|
|
43
|
+
const MAX_TRANSCRIPT_BYTES = 8 * 1024 * 1024;
|
|
44
|
+
/** Loom caps a phrase at roughly a sentence; this is far past any real recording. */
|
|
45
|
+
const MAX_PHRASES = 20_000;
|
|
46
|
+
export function isLoomHost(host) {
|
|
47
|
+
return LOOM_HOSTS.has(host.toLowerCase());
|
|
48
|
+
}
|
|
49
|
+
/** Shell escaping and stray whitespace are noise, never meaning, in a URL. */
|
|
50
|
+
function deEscape(raw) {
|
|
51
|
+
return raw.trim().replace(/\\/g, "").replace(/\s+/g, "");
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The 32-hex video id, or null when this is not a single-video Loom link.
|
|
55
|
+
* `/share/<id>` and `/embed/<id>` are the two forms Loom hands out; folder and
|
|
56
|
+
* workspace URLs deliberately do not match.
|
|
57
|
+
*/
|
|
58
|
+
export function parseLoomId(raw) {
|
|
59
|
+
const cleaned = deEscape(raw);
|
|
60
|
+
let url = null;
|
|
61
|
+
try {
|
|
62
|
+
url = new URL(cleaned);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
url = null;
|
|
66
|
+
}
|
|
67
|
+
if (url && isLoomHost(url.hostname)) {
|
|
68
|
+
const segments = url.pathname.split("/").filter(Boolean);
|
|
69
|
+
// Loom appends a slug to some share links (/share/<id>-<slug>); the id is
|
|
70
|
+
// the leading hex run, and everything after the first dash is decoration.
|
|
71
|
+
const candidate = segments.length >= 2 && /^(share|embed)$/i.test(segments[0]) ? segments[1] : null;
|
|
72
|
+
if (candidate) {
|
|
73
|
+
const id = candidate.split("-")[0];
|
|
74
|
+
if (LOOM_ID.test(id))
|
|
75
|
+
return id.toLowerCase();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const loose = /loom\.com\/(?:share|embed)\/([a-f0-9]{32})/i.exec(cleaned);
|
|
79
|
+
return loose ? loose[1].toLowerCase() : null;
|
|
80
|
+
}
|
|
81
|
+
/** The one URL shape every manifest and every yt-dlp call uses. */
|
|
82
|
+
export function canonicalLoomUrl(videoId) {
|
|
83
|
+
return `https://www.loom.com/share/${videoId}`;
|
|
84
|
+
}
|
|
85
|
+
export class LoomError extends Error {
|
|
86
|
+
kind;
|
|
87
|
+
constructor(kind, message) {
|
|
88
|
+
super(message);
|
|
89
|
+
this.name = "LoomError";
|
|
90
|
+
this.kind = kind;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async function graphql(id, operationName, query, opts) {
|
|
94
|
+
const controller = new AbortController();
|
|
95
|
+
const timer = setTimeout(() => controller.abort(), resolveTimeout(opts));
|
|
96
|
+
let res;
|
|
97
|
+
try {
|
|
98
|
+
res = await fetch(`${resolveOrigin(opts)}/graphql`, {
|
|
99
|
+
method: "POST",
|
|
100
|
+
headers: {
|
|
101
|
+
"Content-Type": "application/json",
|
|
102
|
+
"User-Agent": BROWSER_UA,
|
|
103
|
+
Origin: DEFAULT_ORIGIN,
|
|
104
|
+
Referer: canonicalLoomUrl(id),
|
|
105
|
+
},
|
|
106
|
+
body: JSON.stringify({ operationName, variables: { videoId: id }, query }),
|
|
107
|
+
signal: controller.signal,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
throw new LoomError("unreachable", `could not reach Loom: ${e.message}`);
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
clearTimeout(timer);
|
|
115
|
+
}
|
|
116
|
+
if (!res.ok) {
|
|
117
|
+
throw new LoomError("unreachable", `Loom returned HTTP ${res.status} for ${operationName}.`);
|
|
118
|
+
}
|
|
119
|
+
let parsed;
|
|
120
|
+
try {
|
|
121
|
+
parsed = await res.json();
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
throw new LoomError("unreachable", `Loom returned a response for ${operationName} that was not JSON.`);
|
|
125
|
+
}
|
|
126
|
+
const data = parsed.data;
|
|
127
|
+
return (data && typeof data === "object" ? data : {});
|
|
128
|
+
}
|
|
129
|
+
function str(v, max) {
|
|
130
|
+
return typeof v === "string" && v.trim() ? v.trim().slice(0, max) : undefined;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Title + duration only. The same query can hand back a signed media URL, and
|
|
134
|
+
* deliberately is not asked for one: a transcript import downloads no media, so
|
|
135
|
+
* minting a media URL it will never use is surface for nothing.
|
|
136
|
+
*/
|
|
137
|
+
export async function fetchLoomMeta(id, opts) {
|
|
138
|
+
const data = await graphql(id, "GetVideoSource", "query GetVideoSource($videoId: ID!) { getVideo(id: $videoId) { __typename ... on RegularUserVideo { id name createdAt video_properties { duration width height } } } }", opts);
|
|
139
|
+
const video = data.getVideo;
|
|
140
|
+
if (!video || typeof video !== "object") {
|
|
141
|
+
throw new LoomError("not_found", "Loom has no video at that link. It may have been deleted, or the id may be wrong.");
|
|
142
|
+
}
|
|
143
|
+
const typename = video.__typename;
|
|
144
|
+
if (typename === "PrivateVideo") {
|
|
145
|
+
throw new LoomError("private", "this Loom video is private. Only public share links can be imported.");
|
|
146
|
+
}
|
|
147
|
+
if (typename === "VideoPasswordMissingOrIncorrect") {
|
|
148
|
+
throw new LoomError("private", "this Loom video is password-protected, which Clipy cannot open.");
|
|
149
|
+
}
|
|
150
|
+
if (typename !== "RegularUserVideo") {
|
|
151
|
+
throw new LoomError("not_found", `Loom returned a video type Clipy does not handle (${String(typename)}).`);
|
|
152
|
+
}
|
|
153
|
+
const props = (video.video_properties ?? {});
|
|
154
|
+
const duration = Number(props.duration);
|
|
155
|
+
return {
|
|
156
|
+
id: str(video.id, 64) ?? id,
|
|
157
|
+
title: str(video.name, 300) ?? `Loom recording ${id.slice(0, 8)}`,
|
|
158
|
+
durationMs: Number.isFinite(duration) && duration > 0 ? Math.round(duration * 1000) : 0,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* A URL Loom handed us is still a URL off the wire, and the response that
|
|
163
|
+
* carries it is the one thing an attacker who owns the GraphQL answer controls.
|
|
164
|
+
* So the transcript may only be fetched from the same place the answer came
|
|
165
|
+
* from: loom.com and its CDN subdomains normally, and exactly the configured
|
|
166
|
+
* origin's host when one is set. Pointing the origin elsewhere moves the
|
|
167
|
+
* allowlist; it never removes it.
|
|
168
|
+
*/
|
|
169
|
+
function assertFetchableCaptionUrl(raw, opts) {
|
|
170
|
+
let url;
|
|
171
|
+
try {
|
|
172
|
+
url = new URL(raw);
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
throw new LoomError("unreachable", "Loom returned a transcript location Clipy could not parse.");
|
|
176
|
+
}
|
|
177
|
+
const configured = resolveOrigin(opts);
|
|
178
|
+
const allowed = configured === DEFAULT_ORIGIN
|
|
179
|
+
? url.protocol === "https:" && /(^|\.)loom\.com$/i.test(url.hostname)
|
|
180
|
+
: url.host === new URL(configured).host;
|
|
181
|
+
if (!allowed) {
|
|
182
|
+
throw new LoomError("unreachable", `Loom returned a transcript location off its own CDN (${url.hostname}). Refusing to fetch it.`);
|
|
183
|
+
}
|
|
184
|
+
return url;
|
|
185
|
+
}
|
|
186
|
+
async function fetchText(url, opts) {
|
|
187
|
+
const controller = new AbortController();
|
|
188
|
+
const timer = setTimeout(() => controller.abort(), resolveTimeout(opts));
|
|
189
|
+
let res;
|
|
190
|
+
try {
|
|
191
|
+
res = await fetch(url, { headers: { "User-Agent": BROWSER_UA }, signal: controller.signal });
|
|
192
|
+
}
|
|
193
|
+
catch (e) {
|
|
194
|
+
throw new LoomError("unreachable", `could not download the Loom transcript: ${e.message}`);
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
clearTimeout(timer);
|
|
198
|
+
}
|
|
199
|
+
if (!res.ok)
|
|
200
|
+
throw new LoomError("unreachable", `downloading the Loom transcript returned HTTP ${res.status}.`);
|
|
201
|
+
const declared = Number(res.headers.get("content-length"));
|
|
202
|
+
if (Number.isFinite(declared) && declared > MAX_TRANSCRIPT_BYTES) {
|
|
203
|
+
throw new LoomError("unreachable", "the Loom transcript is implausibly large. Refusing to buffer it.");
|
|
204
|
+
}
|
|
205
|
+
const body = await res.text();
|
|
206
|
+
if (body.length > MAX_TRANSCRIPT_BYTES) {
|
|
207
|
+
throw new LoomError("unreachable", "the Loom transcript is implausibly large. Refusing to buffer it.");
|
|
208
|
+
}
|
|
209
|
+
return body;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Loom's own transcript for a public share link, or null when it has none.
|
|
213
|
+
*
|
|
214
|
+
* Verified across 191 production videos: 84% return one, 16% genuinely have no
|
|
215
|
+
* transcript recorded — so null is an ordinary answer here, not a malfunction.
|
|
216
|
+
*
|
|
217
|
+
* WebVTT is preferred over the phrase JSON because its cues carry a real END
|
|
218
|
+
* time. The phrase JSON only has a start, so its segment boundaries have to be
|
|
219
|
+
* inferred, and the sufficiency classifier reads those boundaries.
|
|
220
|
+
*/
|
|
221
|
+
export async function fetchLoomTranscript(id, opts) {
|
|
222
|
+
const data = await graphql(id, "FetchVideoTranscript", "query FetchVideoTranscript($videoId: ID!) { fetchVideoTranscript(videoId: $videoId) { __typename ... on VideoTranscriptDetails { source_url captions_source_url language } } }", opts);
|
|
223
|
+
const details = data.fetchVideoTranscript;
|
|
224
|
+
if (!details || typeof details !== "object")
|
|
225
|
+
return null;
|
|
226
|
+
const d = details;
|
|
227
|
+
// InvalidRequestWarning is what an unknown id gets. The caller has already
|
|
228
|
+
// resolved metadata by this point, so treat it as "no transcript".
|
|
229
|
+
if (d.__typename !== "VideoTranscriptDetails")
|
|
230
|
+
return null;
|
|
231
|
+
const language = str(d.language, 40);
|
|
232
|
+
const candidates = [];
|
|
233
|
+
const vttUrl = str(d.captions_source_url, 4000);
|
|
234
|
+
const jsonUrl = str(d.source_url, 4000);
|
|
235
|
+
if (vttUrl)
|
|
236
|
+
candidates.push({ format: "vtt", url: vttUrl });
|
|
237
|
+
if (jsonUrl)
|
|
238
|
+
candidates.push({ format: "phrases", url: jsonUrl });
|
|
239
|
+
// Both locations are separately signed and separately expirable, so a dead
|
|
240
|
+
// captions URL falls through to the phrase JSON rather than failing an import
|
|
241
|
+
// whose transcript was sitting right there. The failure is only reported if
|
|
242
|
+
// nothing worked.
|
|
243
|
+
let lastFailure = null;
|
|
244
|
+
for (const candidate of candidates) {
|
|
245
|
+
try {
|
|
246
|
+
const text = await fetchText(assertFetchableCaptionUrl(candidate.url, opts), opts);
|
|
247
|
+
if (text.trim())
|
|
248
|
+
return { format: candidate.format, text, ...(language ? { language } : {}) };
|
|
249
|
+
}
|
|
250
|
+
catch (e) {
|
|
251
|
+
if (!(e instanceof LoomError))
|
|
252
|
+
throw e;
|
|
253
|
+
lastFailure = e;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (lastFailure)
|
|
257
|
+
throw lastFailure;
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Loom's phrase JSON: `{phrases: [{ts, value}]}`, where `ts` is a start in
|
|
262
|
+
* seconds and there is no end. A phrase runs until the next one starts, and the
|
|
263
|
+
* last runs to the end of the video — inferring rather than inventing, because
|
|
264
|
+
* a fabricated end time would move a moment the classifier points at.
|
|
265
|
+
*/
|
|
266
|
+
export function parseLoomPhrases(json, durationMs = 0) {
|
|
267
|
+
const phrases = json?.phrases;
|
|
268
|
+
if (!Array.isArray(phrases))
|
|
269
|
+
return [];
|
|
270
|
+
const raw = [];
|
|
271
|
+
for (const item of phrases.slice(0, MAX_PHRASES)) {
|
|
272
|
+
if (!item || typeof item !== "object")
|
|
273
|
+
continue;
|
|
274
|
+
const ts = Number(item.ts);
|
|
275
|
+
const value = item.value;
|
|
276
|
+
if (!Number.isFinite(ts) || ts < 0 || typeof value !== "string")
|
|
277
|
+
continue;
|
|
278
|
+
const text = value.replace(/\s+/g, " ").trim();
|
|
279
|
+
if (!text)
|
|
280
|
+
continue;
|
|
281
|
+
raw.push({ startMs: Math.round(ts * 1000), text });
|
|
282
|
+
}
|
|
283
|
+
raw.sort((a, b) => a.startMs - b.startMs);
|
|
284
|
+
return raw.map((seg, i) => {
|
|
285
|
+
const next = raw[i + 1]?.startMs;
|
|
286
|
+
const fallback = durationMs > seg.startMs ? durationMs : seg.startMs;
|
|
287
|
+
return {
|
|
288
|
+
startMs: seg.startMs,
|
|
289
|
+
endMs: next !== undefined && next > seg.startMs ? next : fallback,
|
|
290
|
+
text: seg.text,
|
|
291
|
+
};
|
|
292
|
+
});
|
|
293
|
+
}
|