@clipy/cli 0.17.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/LICENSE-APACHE +202 -0
- package/README.md +265 -12
- package/dist/chromeForClipy.js +1 -1
- package/dist/context/importCmd.js +4 -1
- package/dist/context/readCmd.js +33 -21
- package/dist/context/ytdlp.js +108 -28
- package/dist/context-core/arec.js +66 -6
- package/dist/index.js +1180 -98
- package/dist/library.js +7 -3
- package/dist/local/arec.js +186 -0
- package/dist/local/commands.js +1103 -0
- package/dist/local/deps.js +42 -0
- package/dist/local/media.js +154 -0
- package/dist/local/pipeline.js +530 -0
- package/dist/local/ports.js +1 -0
- package/dist/local/speechEngine.js +260 -0
- package/dist/local/transcriptFile.js +54 -0
- package/dist/local/vendor/capture/capture.js +442 -0
- package/dist/local/vendor/capture/check.js +103 -0
- package/dist/local/vendor/capture/children.js +69 -0
- package/dist/local/vendor/capture/ffmpegArgs.js +108 -0
- package/dist/local/vendor/capture/index.js +36 -0
- package/dist/local/vendor/capture/proc.js +71 -0
- package/dist/local/vendor/capture/scripts.js +107 -0
- package/dist/local/vendor/capture/sidecar.js +199 -0
- package/dist/local/vendor/capture/sources.js +272 -0
- package/dist/local/vendor/capture/types.js +1 -0
- package/dist/local/vendor/capture/virtualDisplay.js +170 -0
- package/dist/local/vendor/capture/xauth.js +68 -0
- package/dist/local/vendor/config/errors.js +12 -0
- package/dist/local/vendor/config/index.js +241 -0
- package/dist/local/vendor/config/paths.js +37 -0
- package/dist/local/vendor/config/presets.js +61 -0
- package/dist/local/vendor/config/schema.js +66 -0
- package/dist/local/vendor/moments/generated/drawStrokes.js +127 -0
- package/dist/local/vendor/moments/generated/keyMomentsCursorContext.js +568 -0
- package/dist/local/vendor/moments/generated/keyMomentsFusion.js +406 -0
- package/dist/local/vendor/moments/generated/localMomentPlan.js +178 -0
- package/dist/local/vendor/moments/generated/types.js +1 -0
- package/dist/local/vendor/moments/index.js +147 -0
- package/dist/local/vendor/moments/llm.js +158 -0
- package/dist/local/vendor/transcribe/audio.js +251 -0
- package/dist/local/vendor/transcribe/chunking.js +250 -0
- package/dist/local/vendor/transcribe/index.js +89 -0
- package/dist/local/vendor/transcribe/local.js +221 -0
- package/dist/local/vendor/transcribe/localWorker.js +79 -0
- package/dist/local/vendor/transcribe/models.js +369 -0
- package/dist/local/vendor/transcribe/providers/assemblyai.js +61 -0
- package/dist/local/vendor/transcribe/providers/common.js +105 -0
- package/dist/local/vendor/transcribe/providers/deepgram.js +59 -0
- package/dist/local/vendor/transcribe/providers/elevenlabs.js +43 -0
- package/dist/local/vendor/transcribe/providers/gemini.js +102 -0
- package/dist/local/vendor/transcribe/providers/http.js +214 -0
- package/dist/local/vendor/transcribe/providers/openai.js +48 -0
- package/dist/local/vendor/transcribe/tar.js +53 -0
- package/dist/local/vendor/transcribe/types.js +1 -0
- package/dist/macBridge.js +78 -9
- package/dist/recheck.js +934 -0
- package/dist/recheckCmd.js +279 -0
- package/dist/skill.js +94 -3
- package/package.json +22 -11
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns one recording into a local AREC folder, the same layout the Mac app's
|
|
3
|
+
* local mode writes:
|
|
4
|
+
*
|
|
5
|
+
* recording.mp4 the video (moved or copied in)
|
|
6
|
+
* recording.cursor.json interaction sidecar, when there is one
|
|
7
|
+
* transcript.json timestamped segments
|
|
8
|
+
* moments.json the planned moments
|
|
9
|
+
* frames/<ms>.jpg one screenshot per moment, named by timestamp_ms
|
|
10
|
+
* frames.json frame index
|
|
11
|
+
* metadata.json what ran, how long, where it came from
|
|
12
|
+
* status.json transcribing | selecting | extracting | ready | failed
|
|
13
|
+
* recording.arec the document an agent reads
|
|
14
|
+
*
|
|
15
|
+
* This is what `clipy record|session|proof|context import --local` run in
|
|
16
|
+
* place of an upload. Nothing here talks to Clipy. The only network traffic is
|
|
17
|
+
* what a configured transcription or model provider does.
|
|
18
|
+
*
|
|
19
|
+
* Notes, marks and proof captions arrive as `narration`: they are added to the
|
|
20
|
+
* transcript (labelled as narration) and each one also gets a key moment and
|
|
21
|
+
* a frame, so an agent sees what the driver pointed out.
|
|
22
|
+
*/
|
|
23
|
+
import { mkdir, rename, stat, writeFile } from "node:fs/promises";
|
|
24
|
+
import { basename, join, resolve } from "node:path";
|
|
25
|
+
import { describePlannerError, planMoments, } from "./vendor/moments/index.js";
|
|
26
|
+
import { renderLocalArec } from "./arec.js";
|
|
27
|
+
import { extractFrame, hasAudioStream, placeVideo, probeDurationMs } from "./media.js";
|
|
28
|
+
/** The planner accepts at most this many interaction events (the hosted limit). */
|
|
29
|
+
export const MOMENT_EVENT_BUDGET = 8000;
|
|
30
|
+
const INTERACTION_SELECTION = "bounded sample; full stream retained locally";
|
|
31
|
+
const SIDECAR_KEYS = ["kind", "time_ms", "x", "y", "button", "key_code", "delta_x", "delta_y", "active"];
|
|
32
|
+
const MANDATORY_KINDS = new Set(["down", "up", "key_down", "key_up", "annotation"]);
|
|
33
|
+
function strideSample(items, budget) {
|
|
34
|
+
if (budget <= 0 || items.length === 0)
|
|
35
|
+
return [];
|
|
36
|
+
const stride = Math.max(1, Math.ceil(items.length / budget));
|
|
37
|
+
const out = [];
|
|
38
|
+
for (let i = 0; i < items.length && out.length < budget; i += stride)
|
|
39
|
+
out.push(items[i]);
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
function cleanEvent(event) {
|
|
43
|
+
if (!Number.isFinite(event.time_ms) || event.time_ms < 0)
|
|
44
|
+
return null;
|
|
45
|
+
const out = {};
|
|
46
|
+
for (const key of SIDECAR_KEYS) {
|
|
47
|
+
const value = event[key];
|
|
48
|
+
if (value === undefined || value === null)
|
|
49
|
+
continue;
|
|
50
|
+
if (typeof value === "number" && !Number.isFinite(value))
|
|
51
|
+
continue;
|
|
52
|
+
out[key] = value;
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The planner's interaction input, bounded like the Mac's `moment_input`: key,
|
|
58
|
+
* annotation and button edges are gesture evidence and are kept in full;
|
|
59
|
+
* scrolls and moves are strided into what is left of the 8000-event budget.
|
|
60
|
+
*/
|
|
61
|
+
export function momentInteractions(events, budget = MOMENT_EVENT_BUDGET) {
|
|
62
|
+
const clean = events.map(cleanEvent).filter((e) => e !== null);
|
|
63
|
+
const mandatory = clean.filter((e) => MANDATORY_KINDS.has(e.kind)).slice(0, budget);
|
|
64
|
+
const remaining = budget - mandatory.length;
|
|
65
|
+
const scrolls = clean.filter((e) => e.kind === "scroll");
|
|
66
|
+
const moves = clean.filter((e) => e.kind === "move");
|
|
67
|
+
const sampledScrolls = strideSample(scrolls, Math.min(Math.floor(remaining / 2), scrolls.length));
|
|
68
|
+
const sampledMoves = strideSample(moves, remaining - sampledScrolls.length);
|
|
69
|
+
return [...mandatory, ...sampledScrolls, ...sampledMoves].sort((a, b) => a.time_ms - b.time_ms);
|
|
70
|
+
}
|
|
71
|
+
function captureForPlanner(geometry) {
|
|
72
|
+
if (!geometry)
|
|
73
|
+
return undefined;
|
|
74
|
+
const { x, y, width, height, output_width, output_height } = geometry;
|
|
75
|
+
if (![x, y, width, height].every((n) => typeof n === "number" && Number.isFinite(n)))
|
|
76
|
+
return undefined;
|
|
77
|
+
if (width <= 0 || height <= 0)
|
|
78
|
+
return undefined;
|
|
79
|
+
const capture = { x, y, width, height };
|
|
80
|
+
if (Number.isInteger(output_width) && (output_width ?? 0) > 0)
|
|
81
|
+
capture.output_width = output_width;
|
|
82
|
+
if (Number.isInteger(output_height) && (output_height ?? 0) > 0)
|
|
83
|
+
capture.output_height = output_height;
|
|
84
|
+
return capture;
|
|
85
|
+
}
|
|
86
|
+
/** The planner refuses a segment longer than this; longer ones are split, never cut. */
|
|
87
|
+
const MAX_SEGMENT_CHARS = 10_000;
|
|
88
|
+
/**
|
|
89
|
+
* Splits one segment's text into pieces of at most MAX_SEGMENT_CHARS, at
|
|
90
|
+
* whitespace where possible, sharing out its time span by character offset.
|
|
91
|
+
* No text is dropped.
|
|
92
|
+
*/
|
|
93
|
+
function splitLongSegment(seg) {
|
|
94
|
+
const text = seg.text;
|
|
95
|
+
if (text.length <= MAX_SEGMENT_CHARS)
|
|
96
|
+
return [seg];
|
|
97
|
+
const pieces = [];
|
|
98
|
+
let from = 0;
|
|
99
|
+
while (from < text.length) {
|
|
100
|
+
let to = Math.min(from + MAX_SEGMENT_CHARS, text.length);
|
|
101
|
+
if (to < text.length) {
|
|
102
|
+
const space = text.lastIndexOf(" ", to);
|
|
103
|
+
if (space > from + MAX_SEGMENT_CHARS / 2)
|
|
104
|
+
to = space;
|
|
105
|
+
}
|
|
106
|
+
const piece = text.slice(from, to).trim();
|
|
107
|
+
if (piece)
|
|
108
|
+
pieces.push({ text: piece, from, to });
|
|
109
|
+
from = to;
|
|
110
|
+
}
|
|
111
|
+
const span = seg.endMs - seg.startMs;
|
|
112
|
+
return pieces.map((p) => ({
|
|
113
|
+
startMs: seg.startMs + Math.round((span * p.from) / text.length),
|
|
114
|
+
endMs: seg.startMs + Math.round((span * p.to) / text.length),
|
|
115
|
+
text: p.text,
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
/** Segments the planner accepts: inside the recording, start before end, none over its length limit. */
|
|
119
|
+
export function boundedSegments(segments, durationMs) {
|
|
120
|
+
return segments
|
|
121
|
+
.filter((s) => Number.isFinite(s.startMs) && Number.isFinite(s.endMs) && s.text.trim())
|
|
122
|
+
.flatMap((s) => {
|
|
123
|
+
const startMs = Math.min(Math.max(0, Math.round(s.startMs)), durationMs);
|
|
124
|
+
const endMs = Math.min(Math.max(startMs, Math.round(s.endMs)), durationMs);
|
|
125
|
+
return splitLongSegment({ startMs, endMs, text: s.text });
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/** Label that keeps a note apart from recognized speech in the transcript. */
|
|
129
|
+
export const NARRATION_PREFIX = "Narration: ";
|
|
130
|
+
/** Notes as transcript segments: each runs until the next note, at most 5 s. */
|
|
131
|
+
export function narrationSegments(notes, durationMs) {
|
|
132
|
+
const sorted = notes
|
|
133
|
+
.filter((n) => Number.isFinite(n.startMs) && n.text.trim())
|
|
134
|
+
.map((n) => ({ ...n, startMs: Math.min(Math.max(0, Math.round(n.startMs)), durationMs) }))
|
|
135
|
+
.sort((a, b) => a.startMs - b.startMs);
|
|
136
|
+
return sorted.map((n, i) => {
|
|
137
|
+
const next = sorted[i + 1]?.startMs ?? durationMs;
|
|
138
|
+
const fallbackEnd = Math.max(n.startMs, Math.min(n.startMs + 5000, Math.max(next, n.startMs + 500), durationMs));
|
|
139
|
+
const endMs = n.endMs !== undefined && Number.isFinite(n.endMs) ? Math.max(n.startMs, Math.min(Math.round(n.endMs), durationMs)) : fallbackEnd;
|
|
140
|
+
return { startMs: n.startMs, endMs, text: `${NARRATION_PREFIX}${n.text.trim()}` };
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/** One moment per note, skipping any instant a planned moment already covers. */
|
|
144
|
+
export function narrationMoments(notes, planned, durationMs) {
|
|
145
|
+
const taken = new Set(planned.map((m) => Math.round(m.timestamp_ms)));
|
|
146
|
+
const out = [];
|
|
147
|
+
for (const n of notes) {
|
|
148
|
+
if (!Number.isFinite(n.startMs) || !n.text.trim())
|
|
149
|
+
continue;
|
|
150
|
+
const t = Math.min(Math.max(0, Math.round(n.startMs)), Math.max(0, durationMs - 1));
|
|
151
|
+
if (taken.has(t))
|
|
152
|
+
continue;
|
|
153
|
+
taken.add(t);
|
|
154
|
+
const frameAt = Math.min(Math.max(0, Math.round(n.frameAtMs ?? t)), Math.max(0, durationMs - 1));
|
|
155
|
+
const text = n.text.trim().replace(/\s+/g, " ");
|
|
156
|
+
out.push({
|
|
157
|
+
timestamp_ms: t,
|
|
158
|
+
frame_at_ms: frameAt,
|
|
159
|
+
reason: `Narration: ${text.length > 300 ? `${text.slice(0, 297)}...` : text}`,
|
|
160
|
+
source: "narration",
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
/** Start, middle and end samples for a plan that found nothing (the Mac's overview_moments). */
|
|
166
|
+
export function overviewMoments(durationMs) {
|
|
167
|
+
if (durationMs <= 0)
|
|
168
|
+
return [];
|
|
169
|
+
const times = [...new Set([0, Math.floor(durationMs / 2), Math.max(0, durationMs - 250)])].sort((a, b) => a - b);
|
|
170
|
+
return times.map((t) => ({
|
|
171
|
+
timestamp_ms: t,
|
|
172
|
+
frame_at_ms: t,
|
|
173
|
+
reason: "Overview sample; not a selected key moment or a visual interpretation.",
|
|
174
|
+
overview: true,
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
export function frameFileName(timestampMs) {
|
|
178
|
+
return `frames/${String(Math.round(timestampMs)).padStart(12, "0")}.jpg`;
|
|
179
|
+
}
|
|
180
|
+
function isLocalHost(baseUrl) {
|
|
181
|
+
try {
|
|
182
|
+
const host = new URL(baseUrl).hostname;
|
|
183
|
+
return host === "localhost" || host === "127.0.0.1" || host === "::1" || host === "[::1]";
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function hostOf(baseUrl) {
|
|
190
|
+
try {
|
|
191
|
+
return new URL(baseUrl).host;
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return "a custom endpoint";
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function transcriptionWhere(config) {
|
|
198
|
+
switch (config.kind) {
|
|
199
|
+
case "none":
|
|
200
|
+
case "local":
|
|
201
|
+
return null;
|
|
202
|
+
case "openai-compatible":
|
|
203
|
+
return isLocalHost(config.baseUrl) ? null : hostOf(config.baseUrl);
|
|
204
|
+
default:
|
|
205
|
+
return config.kind;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* One sentence per destination, matching what the code actually sent. With no
|
|
210
|
+
* provider nothing leaves; a model provider gets the transcript text, click
|
|
211
|
+
* labels and a bounded sample of pointer and key events (plus the capture
|
|
212
|
+
* area); a transcription provider gets the audio. The video and screenshots
|
|
213
|
+
* never leave.
|
|
214
|
+
*/
|
|
215
|
+
export function dataBoundary(config, used) {
|
|
216
|
+
const parts = ["Nothing is sent to Clipy. The video and screenshots stay on this machine."];
|
|
217
|
+
const audioTo = used.audioSent ? transcriptionWhere(config.transcription) : null;
|
|
218
|
+
parts.push(audioTo ? `The audio was sent to ${audioTo} for transcription.` : "The audio stays on this machine.");
|
|
219
|
+
const provider = config.model;
|
|
220
|
+
const modelRemote = used.modelCalled && provider !== null && !isLocalHost(provider.baseUrl);
|
|
221
|
+
if (modelRemote && provider) {
|
|
222
|
+
parts.push(`The transcript text, click labels, a bounded sample of pointer and key events, and the capture area were sent to ${hostOf(provider.baseUrl)} to select spoken references.`);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
if (used.modelCalled && provider) {
|
|
226
|
+
parts.push("The transcript and a sample of the interactions were read by a model running on this machine to select spoken references.");
|
|
227
|
+
}
|
|
228
|
+
parts.push("The interaction evidence stays on this machine.");
|
|
229
|
+
}
|
|
230
|
+
if (!audioTo && !modelRemote)
|
|
231
|
+
parts.push("Nothing left this machine.");
|
|
232
|
+
return parts.join(" ");
|
|
233
|
+
}
|
|
234
|
+
function engineLabel(result) {
|
|
235
|
+
return result.engine;
|
|
236
|
+
}
|
|
237
|
+
async function writeJson(path, value) {
|
|
238
|
+
const temp = `${path}.tmp`;
|
|
239
|
+
await writeFile(temp, `${JSON.stringify(value, null, 2)}\n`);
|
|
240
|
+
await rename(temp, path);
|
|
241
|
+
}
|
|
242
|
+
function abortError(signal) {
|
|
243
|
+
const reason = signal.reason;
|
|
244
|
+
return reason instanceof Error ? reason : new Error("Processing cancelled");
|
|
245
|
+
}
|
|
246
|
+
export async function processRecording(options) {
|
|
247
|
+
const dir = resolve(options.dir);
|
|
248
|
+
const ffmpeg = options.ffmpegPath ?? process.env.CLIPY_FFMPEG ?? "ffmpeg";
|
|
249
|
+
const signal = options.signal;
|
|
250
|
+
const now = options.now ?? (() => new Date());
|
|
251
|
+
const created = now();
|
|
252
|
+
const recordingId = basename(dir);
|
|
253
|
+
const video = join(dir, "recording.mp4");
|
|
254
|
+
const arecPath = join(dir, "recording.arec");
|
|
255
|
+
const warnings = [];
|
|
256
|
+
const checkAbort = () => {
|
|
257
|
+
if (signal?.aborted)
|
|
258
|
+
throw abortError(signal);
|
|
259
|
+
};
|
|
260
|
+
await mkdir(join(dir, "frames"), { recursive: true });
|
|
261
|
+
const interactions = options.interactions ? options.interactions.map(cleanEvent).filter((e) => e !== null) : null;
|
|
262
|
+
const clickElements = (options.clickElements ?? []).slice(0, 2000).map((e) => ({
|
|
263
|
+
time_ms: e.time_ms,
|
|
264
|
+
role: e.role == null ? null : String(e.role).slice(0, 256),
|
|
265
|
+
label: e.label == null ? null : String(e.label).slice(0, 4000),
|
|
266
|
+
}));
|
|
267
|
+
// Shared, growing state the document is rendered from at every checkpoint.
|
|
268
|
+
const doc = {
|
|
269
|
+
title: options.title ?? "Local recording",
|
|
270
|
+
...(options.description ? { description: options.description } : {}),
|
|
271
|
+
...(options.expectedResult ? { expectedResult: options.expectedResult } : {}),
|
|
272
|
+
...(options.recordingKind ? { recordingKind: options.recordingKind } : {}),
|
|
273
|
+
...(options.compiler ? { compiler: options.compiler } : {}),
|
|
274
|
+
recordingId,
|
|
275
|
+
state: "preparing",
|
|
276
|
+
statusText: "Preparing: video saved; transcript and frames are being added. Re-read this same file for updates.",
|
|
277
|
+
createdAt: created.toISOString(),
|
|
278
|
+
durationMs: options.durationMs ?? 0,
|
|
279
|
+
origin: options.origin ?? { kind: "capture" },
|
|
280
|
+
transcript: { ready: false, segments: [], engineLabel: "pending", gaps: [] },
|
|
281
|
+
interactions: interactions
|
|
282
|
+
? { captured: true, eventCount: interactions.length, clicks: interactions.filter((e) => e.kind === "down").map(({ time_ms, x, y, button }) => ({ time_ms, x, y, button })) }
|
|
283
|
+
: { captured: false },
|
|
284
|
+
planner: null,
|
|
285
|
+
frames: [],
|
|
286
|
+
dataBoundary: dataBoundary(options.config, { audioSent: false, modelCalled: false }),
|
|
287
|
+
captureGaps: [...(options.captureWarnings ?? [])],
|
|
288
|
+
};
|
|
289
|
+
const metadata = {
|
|
290
|
+
recording_id: recordingId,
|
|
291
|
+
edition: "clipy-cli",
|
|
292
|
+
mode: "local",
|
|
293
|
+
compiler: options.compiler ?? "clipy --local",
|
|
294
|
+
...(options.recordingKind ? { recordingKind: options.recordingKind } : {}),
|
|
295
|
+
...(options.description ? { description: options.description } : {}),
|
|
296
|
+
...(options.expectedResult ? { expectedResult: options.expectedResult } : {}),
|
|
297
|
+
mediaType: "video/mp4",
|
|
298
|
+
title: doc.title,
|
|
299
|
+
createdAt: doc.createdAt,
|
|
300
|
+
createdAtMs: created.getTime(),
|
|
301
|
+
origin: doc.origin,
|
|
302
|
+
capture: options.geometry ?? null,
|
|
303
|
+
};
|
|
304
|
+
const writeArec = async () => {
|
|
305
|
+
const temp = `${arecPath}.tmp`;
|
|
306
|
+
await writeFile(temp, renderLocalArec(doc));
|
|
307
|
+
await rename(temp, arecPath);
|
|
308
|
+
};
|
|
309
|
+
const setStatus = async (stage, message) => {
|
|
310
|
+
await writeJson(join(dir, "status.json"), { stage, message, updatedAtMs: now().getTime(), arecPath });
|
|
311
|
+
options.onStatus?.({ stage, message });
|
|
312
|
+
};
|
|
313
|
+
const finishDoc = (state, statusText) => {
|
|
314
|
+
doc.state = state;
|
|
315
|
+
doc.statusText = statusText;
|
|
316
|
+
};
|
|
317
|
+
try {
|
|
318
|
+
// 1. The video, the sidecar, and a preparing document an agent can already open.
|
|
319
|
+
await placeVideo(ffmpeg, resolve(options.videoPath), video, { move: options.moveVideo, signal });
|
|
320
|
+
checkAbort();
|
|
321
|
+
const durationMs = Math.max(1, Math.round(options.durationMs && options.durationMs > 0 ? options.durationMs : await probeDurationMs(ffmpeg, video, signal)));
|
|
322
|
+
doc.durationMs = durationMs;
|
|
323
|
+
metadata.duration_ms = durationMs;
|
|
324
|
+
if (interactions) {
|
|
325
|
+
await writeJson(join(dir, "recording.cursor.json"), {
|
|
326
|
+
version: 1,
|
|
327
|
+
capture: options.geometry ?? null,
|
|
328
|
+
events: interactions,
|
|
329
|
+
click_elements: clickElements,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
await writeJson(join(dir, "metadata.json"), metadata);
|
|
333
|
+
await writeArec();
|
|
334
|
+
// 2. Transcript.
|
|
335
|
+
const narration = options.narration ?? [];
|
|
336
|
+
await setStatus("transcribing", options.transcript ? "Reading the supplied transcript" : "Transcribing");
|
|
337
|
+
let transcribed = false;
|
|
338
|
+
// Set before the request goes out: a provider that received the audio and
|
|
339
|
+
// then failed, or a take cancelled mid-request, must still disclose it.
|
|
340
|
+
let audioSent = false;
|
|
341
|
+
let transcriptOut;
|
|
342
|
+
let transcriptionFailed = null;
|
|
343
|
+
if (options.transcript) {
|
|
344
|
+
transcriptOut = { engine: options.transcript.label, segments: options.transcript.segments, gaps: [] };
|
|
345
|
+
}
|
|
346
|
+
else if (options.config.transcription.kind === "none") {
|
|
347
|
+
transcriptOut = { engine: "none (transcription is turned off)", segments: [], gaps: [] };
|
|
348
|
+
doc.transcript.gaps.push("Transcription is turned off, so there is no spoken transcript. Run clipy local setup --transcribe to turn it on.");
|
|
349
|
+
}
|
|
350
|
+
else if (!(await hasAudioStream(ffmpeg, video, signal))) {
|
|
351
|
+
transcriptOut = { engine: "none (the recording has no audio track)", segments: [], gaps: [] };
|
|
352
|
+
doc.transcript.noAudio = true;
|
|
353
|
+
doc.transcript.gaps.push("The recording has no audio track, so there is no spoken transcript.");
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
if (transcriptionWhere(options.config.transcription)) {
|
|
357
|
+
audioSent = true;
|
|
358
|
+
doc.dataBoundary = dataBoundary(options.config, { audioSent, modelCalled: false });
|
|
359
|
+
await writeArec();
|
|
360
|
+
}
|
|
361
|
+
try {
|
|
362
|
+
const result = await options.deps.transcribe(video, options.config.transcription, { signal });
|
|
363
|
+
transcribed = true;
|
|
364
|
+
transcriptOut = { engine: engineLabel(result), language: result.language, segments: result.segments, gaps: result.gaps };
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
checkAbort();
|
|
368
|
+
transcriptionFailed = error instanceof Error ? error.message : String(error);
|
|
369
|
+
transcriptOut = { engine: "failed", segments: [], gaps: [] };
|
|
370
|
+
doc.transcript.gaps.push(`Transcription failed (${transcriptionFailed}). There is no transcript; this is not evidence that nothing was said.`);
|
|
371
|
+
warnings.push(`Transcription failed: ${transcriptionFailed}`);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
checkAbort();
|
|
375
|
+
const spoken = boundedSegments(transcriptOut.segments, durationMs);
|
|
376
|
+
const noteSegments = boundedSegments(narrationSegments(narration, durationMs), durationMs);
|
|
377
|
+
const segments = [...spoken, ...noteSegments].sort((a, b) => a.startMs - b.startMs || a.endMs - b.endMs);
|
|
378
|
+
if (noteSegments.length > 0) {
|
|
379
|
+
transcriptOut.engine = spoken.length > 0 || transcribed ? `${transcriptOut.engine} plus narration notes` : "narration notes";
|
|
380
|
+
}
|
|
381
|
+
for (const gap of transcriptOut.gaps) {
|
|
382
|
+
doc.transcript.gaps.push(`Transcript gap from ${gap.startMs} to ${gap.endMs} ms: ${gap.reason}`);
|
|
383
|
+
}
|
|
384
|
+
doc.transcript = { ...doc.transcript, ready: true, segments, engineLabel: transcriptOut.engine, language: transcriptOut.language };
|
|
385
|
+
doc.dataBoundary = dataBoundary(options.config, { audioSent, modelCalled: false });
|
|
386
|
+
await writeJson(join(dir, "transcript.json"), {
|
|
387
|
+
through_ms: durationMs,
|
|
388
|
+
engine: transcriptOut.engine,
|
|
389
|
+
...(transcriptOut.language ? { language: transcriptOut.language } : {}),
|
|
390
|
+
segments,
|
|
391
|
+
gaps: transcriptOut.gaps,
|
|
392
|
+
});
|
|
393
|
+
metadata.transcription = { engine: transcriptOut.engine, language: transcriptOut.language ?? null, failed: transcriptionFailed };
|
|
394
|
+
doc.statusText = "Preparing: transcript saved; moments and frames are being added. Re-read this same file for updates.";
|
|
395
|
+
await writeArec();
|
|
396
|
+
// 3. Moments: rules always, spoken references with the user's provider.
|
|
397
|
+
await setStatus("selecting", "Selecting moments");
|
|
398
|
+
const input = {
|
|
399
|
+
schemaVersion: 2,
|
|
400
|
+
durationMs,
|
|
401
|
+
transcript: segments,
|
|
402
|
+
interactions: momentInteractions(interactions ?? []),
|
|
403
|
+
clickElements,
|
|
404
|
+
interactionSelection: INTERACTION_SELECTION,
|
|
405
|
+
...(captureForPlanner(options.geometry) ? { capture: captureForPlanner(options.geometry) } : {}),
|
|
406
|
+
};
|
|
407
|
+
const provider = options.config.model ?? null;
|
|
408
|
+
let planner = "rules";
|
|
409
|
+
let modelFallback = null;
|
|
410
|
+
let planned;
|
|
411
|
+
// The planner only calls the model when there is a transcript. Disclose it
|
|
412
|
+
// before the request, so a failure or cancel mid-request still says so.
|
|
413
|
+
const modelCalled = provider !== null && segments.length > 0;
|
|
414
|
+
if (modelCalled) {
|
|
415
|
+
doc.dataBoundary = dataBoundary(options.config, { audioSent, modelCalled });
|
|
416
|
+
await writeArec();
|
|
417
|
+
}
|
|
418
|
+
try {
|
|
419
|
+
const { plan, planner: used } = await planMoments(input, { provider, signal });
|
|
420
|
+
planner = used;
|
|
421
|
+
planned = "schemaVersion" in plan ? plan.moments : [];
|
|
422
|
+
}
|
|
423
|
+
catch (error) {
|
|
424
|
+
checkAbort();
|
|
425
|
+
if (!provider)
|
|
426
|
+
throw error;
|
|
427
|
+
// A provider outage never costs the take: rules alone still frame every gesture.
|
|
428
|
+
const kind = String(describePlannerError(error).kind ?? "error");
|
|
429
|
+
modelFallback = kind;
|
|
430
|
+
warnings.push(`The model provider failed (${kind}); moments come from rules only.`);
|
|
431
|
+
const { plan } = await planMoments(input, { provider: null, signal });
|
|
432
|
+
planned = "schemaVersion" in plan ? plan.moments : [];
|
|
433
|
+
}
|
|
434
|
+
checkAbort();
|
|
435
|
+
doc.dataBoundary = dataBoundary(options.config, { audioSent, modelCalled });
|
|
436
|
+
const modelName = provider ? `${provider.model} at ${hostOf(provider.baseUrl)}` : null;
|
|
437
|
+
doc.planner =
|
|
438
|
+
planner === "rules+model"
|
|
439
|
+
? { label: `rules on this machine plus ${modelName} for spoken references` }
|
|
440
|
+
: modelFallback
|
|
441
|
+
? {
|
|
442
|
+
label: "rules on this machine (Flare highlights, pen drawings, clicks, pointer rests)",
|
|
443
|
+
note: `The model provider (${modelName}) failed (${modelFallback}), so moments come from rules only and spoken references were not selected. Run the import again to retry.`,
|
|
444
|
+
}
|
|
445
|
+
: {
|
|
446
|
+
label: "rules on this machine (Flare highlights, pen drawings, clicks, pointer rests)",
|
|
447
|
+
note: "Spoken references were not selected: no model provider is configured. Run clipy local setup --model-provider to add one.",
|
|
448
|
+
};
|
|
449
|
+
metadata.planner = { used: planner, model: modelName, fallback: modelFallback };
|
|
450
|
+
const fromNotes = narrationMoments(narration, planned, durationMs);
|
|
451
|
+
const selected = [...planned, ...fromNotes].sort((a, b) => a.timestamp_ms - b.timestamp_ms);
|
|
452
|
+
const moments = selected.length > 0 ? selected : overviewMoments(durationMs);
|
|
453
|
+
await writeJson(join(dir, "moments.json"), moments);
|
|
454
|
+
// 4. Frames.
|
|
455
|
+
await setStatus("extracting", "Extracting frames");
|
|
456
|
+
const frames = [];
|
|
457
|
+
const frameGaps = [];
|
|
458
|
+
let firstError = null;
|
|
459
|
+
for (const m of moments) {
|
|
460
|
+
checkAbort();
|
|
461
|
+
const file = frameFileName(m.timestamp_ms);
|
|
462
|
+
const result = await extractFrame(ffmpeg, video, Math.min(m.frame_at_ms, durationMs - 1), join(dir, file), signal);
|
|
463
|
+
if (!result.ok) {
|
|
464
|
+
firstError ??= result.error;
|
|
465
|
+
frameGaps.push(`No screenshot for the moment at ${m.timestamp_ms} ms (frame ${m.frame_at_ms} ms): ${result.error}. Inspect recording.mp4 there.`);
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
frames.push({
|
|
469
|
+
timestampMs: m.timestamp_ms,
|
|
470
|
+
frameAtMs: m.frame_at_ms,
|
|
471
|
+
frameTimeMs: result.frameTimeMs,
|
|
472
|
+
file,
|
|
473
|
+
reason: m.reason,
|
|
474
|
+
...(m.source ? { source: m.source } : {}),
|
|
475
|
+
...(m.overview ? { overview: true } : {}),
|
|
476
|
+
...(m.x != null && m.y != null ? { x: m.x, y: m.y } : {}),
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
if (moments.length > 0 && frames.length === 0) {
|
|
480
|
+
throw new Error(`No frame could be extracted from recording.mp4: ${firstError}`);
|
|
481
|
+
}
|
|
482
|
+
doc.frames = frames;
|
|
483
|
+
doc.captureGaps.push(...frameGaps);
|
|
484
|
+
await writeJson(join(dir, "frames.json"), frames.map((f) => ({ timestampMs: f.timestampMs, frameAtMs: f.frameAtMs, frameTimeMs: f.frameTimeMs, reason: f.reason, frame: f.file, source: f.source, overview: f.overview, x: f.x, y: f.y })));
|
|
485
|
+
// 5. Done.
|
|
486
|
+
if (transcriptionFailed) {
|
|
487
|
+
finishDoc("partial", "Partial: transcription failed; moments and frames are complete. See Capture gaps.");
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
finishDoc("complete", "Complete");
|
|
491
|
+
}
|
|
492
|
+
await writeJson(join(dir, "metadata.json"), metadata);
|
|
493
|
+
await writeArec();
|
|
494
|
+
await setStatus("ready", "Ready");
|
|
495
|
+
return {
|
|
496
|
+
dir,
|
|
497
|
+
arecPath,
|
|
498
|
+
durationMs,
|
|
499
|
+
planner,
|
|
500
|
+
modelFallback,
|
|
501
|
+
moments: moments.length,
|
|
502
|
+
frames: frames.length,
|
|
503
|
+
transcriptSegments: segments.length,
|
|
504
|
+
transcriptEngine: transcriptOut.engine,
|
|
505
|
+
warnings: [...warnings, ...frameGaps],
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
catch (error) {
|
|
509
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
510
|
+
finishDoc("failed", `Failed: ${message}`);
|
|
511
|
+
try {
|
|
512
|
+
await writeArec();
|
|
513
|
+
await setStatus("failed", message);
|
|
514
|
+
}
|
|
515
|
+
catch {
|
|
516
|
+
// The original error is what matters.
|
|
517
|
+
}
|
|
518
|
+
throw error;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/** For `open`: the AREC path of a finished folder, or null. */
|
|
522
|
+
export async function arecPathOf(dir) {
|
|
523
|
+
const path = join(resolve(dir), "recording.arec");
|
|
524
|
+
try {
|
|
525
|
+
return (await stat(path)).isFile() ? path : null;
|
|
526
|
+
}
|
|
527
|
+
catch {
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|