@tmhs/screencast-mcp 0.45.0 → 0.46.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 +21 -8
- package/dist/prompts.js +16 -3
- package/dist/server.js +8 -0
- package/dist/tools/bugReport.js +1 -1
- package/dist/tools/cutByTranscript.js +86 -0
- package/dist/tools/indexRecording.js +173 -0
- package/dist/tools/pipeline.js +200 -19
- package/dist/tools/searchRecording.js +73 -0
- package/dist/tools/stepGuide.js +193 -0
- package/dist/tools/transcribe.js +4 -3
- package/dist/tools/zoom.js +1 -1
- package/dist/utils/audio.js +12 -4
- package/dist/utils/bugReport.js +1 -75
- package/dist/utils/captions.js +19 -6
- package/dist/utils/chapters.js +5 -0
- package/dist/utils/idle.js +10 -3
- package/dist/utils/keystrokes.js +2 -17
- package/dist/utils/pipeline.js +131 -14
- package/dist/utils/recipes.js +67 -0
- package/dist/utils/recordingIndex.js +266 -0
- package/dist/utils/stepGuide.js +205 -0
- package/dist/utils/steps.js +88 -0
- package/dist/utils/timeline.js +85 -0
- package/dist/utils/toolsets.js +10 -4
- package/dist/utils/transcriptCut.js +134 -0
- package/package.json +1 -1
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { closeSync, existsSync, openSync, readFileSync, readSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, join, resolve } from "node:path";
|
|
4
|
+
import { ScreencastError } from "./errors.js";
|
|
5
|
+
import { pickSceneFrames } from "./media.js";
|
|
6
|
+
import { normalizeForMatch, regexRanges, MAX_QUERY_LENGTH } from "./ocr.js";
|
|
7
|
+
import { maskSecrets, DEFAULT_SECRET_PATTERN } from "./bugReport.js";
|
|
8
|
+
import { eventPaths, whenFinalized } from "./events.js";
|
|
9
|
+
import { readMarkers } from "./chapters.js";
|
|
10
|
+
import { assertOutputAllowed } from "./paths.js";
|
|
11
|
+
import { getStore } from "../context.js";
|
|
12
|
+
export const INDEX_VERSION = 1;
|
|
13
|
+
export const DEFAULT_INDEX_THRESHOLD = 0.03;
|
|
14
|
+
export const DEFAULT_INDEX_FRAMES = 120;
|
|
15
|
+
export const MAX_INDEX_FRAMES = 600;
|
|
16
|
+
export const MAX_SEGMENT_SEC = 10;
|
|
17
|
+
export const DEFAULT_SEARCH_LIMIT = 10;
|
|
18
|
+
export const MAX_SEARCH_LIMIT = 50;
|
|
19
|
+
export const MAX_SNIPPET = 200;
|
|
20
|
+
export const MAX_MATCHES_PER_HIT = 5;
|
|
21
|
+
const r3 = (v) => Math.round(v * 1000) / 1000;
|
|
22
|
+
export function indexSidecarPath(video) {
|
|
23
|
+
return `${video.replace(/\.[^./\\]+$/, "")}.index.json`;
|
|
24
|
+
}
|
|
25
|
+
export function fallbackIndexPath(video, editsDir) {
|
|
26
|
+
const key = createHash("sha1").update(resolve(video).toLowerCase()).digest("hex").slice(0, 12);
|
|
27
|
+
const name = basename(video).replace(/\.[^.]+$/, "");
|
|
28
|
+
return join(editsDir, "indexes", `${name}-${key}.index.json`);
|
|
29
|
+
}
|
|
30
|
+
export function indexWritePath(video, editsDir) {
|
|
31
|
+
const sidecar = indexSidecarPath(video);
|
|
32
|
+
try {
|
|
33
|
+
assertOutputAllowed(sidecar);
|
|
34
|
+
return sidecar;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return fallbackIndexPath(video, editsDir);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function findIndex(video, editsDir) {
|
|
41
|
+
for (const p of [indexSidecarPath(video), fallbackIndexPath(video, editsDir)])
|
|
42
|
+
if (existsSync(p))
|
|
43
|
+
return p;
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
export function planKeyframes(changes, durationSec, maxFrames, maxGap = MAX_SEGMENT_SEC) {
|
|
47
|
+
const times = pickSceneFrames(changes.filter((c) => c.timeSec < durationSec), maxFrames).map(r3);
|
|
48
|
+
while (times.length < maxFrames) {
|
|
49
|
+
let best = -1;
|
|
50
|
+
let gap = 0;
|
|
51
|
+
for (let i = 0; i < times.length; i++) {
|
|
52
|
+
const g = (times[i + 1] ?? durationSec) - times[i];
|
|
53
|
+
if (g > gap) {
|
|
54
|
+
gap = g;
|
|
55
|
+
best = i;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (best === -1 || gap <= maxGap)
|
|
59
|
+
break;
|
|
60
|
+
times.splice(best + 1, 0, r3(times[best] + gap / 2));
|
|
61
|
+
}
|
|
62
|
+
return [...new Set(times)].sort((a, b) => a - b);
|
|
63
|
+
}
|
|
64
|
+
export function segmentsFrom(starts, durationSec) {
|
|
65
|
+
return starts.map((start, i) => ({ start, end: r3(starts[i + 1] ?? durationSec) }));
|
|
66
|
+
}
|
|
67
|
+
export function keyframeTime(seg) {
|
|
68
|
+
return r3(seg.start + Math.min(0.25, (seg.end - seg.start) / 2));
|
|
69
|
+
}
|
|
70
|
+
export function mergeSegments(segments) {
|
|
71
|
+
const out = [];
|
|
72
|
+
for (const seg of segments) {
|
|
73
|
+
const prev = out.at(-1);
|
|
74
|
+
if (prev && prev.ocr.join("\n") === seg.ocr.join("\n") && !seg.events && !seg.markers && !seg.transcript) {
|
|
75
|
+
prev.end = seg.end;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
out.push({ ...seg, ocr: [...seg.ocr] });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
export function eventTexts(events) {
|
|
84
|
+
const out = [];
|
|
85
|
+
for (const e of events) {
|
|
86
|
+
const at = `at ${Math.round(e.x)},${Math.round(e.y)}`;
|
|
87
|
+
let text = null;
|
|
88
|
+
if (e.type === "down")
|
|
89
|
+
text = `click ${e.button ?? "left"} ${at}`;
|
|
90
|
+
else if (e.type === "key")
|
|
91
|
+
text = `shortcut ${e.keys ?? "key"}`;
|
|
92
|
+
else if (e.type === "typing")
|
|
93
|
+
text = `typing ${r3(e.durationSec ?? 0)} s`;
|
|
94
|
+
else if (e.type === "wheel")
|
|
95
|
+
text = `scroll ${(e.delta ?? 0) > 0 ? "up" : "down"} ${at}`;
|
|
96
|
+
else if (e.type === "gap")
|
|
97
|
+
text = `event gap (${e.reason ?? "unknown"})`;
|
|
98
|
+
if (text)
|
|
99
|
+
out.push({ t: r3(e.t), text });
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
export function markerTexts(markers) {
|
|
104
|
+
return markers.map((m) => ({ t: r3(m.t), text: m.label }));
|
|
105
|
+
}
|
|
106
|
+
export function cueTexts(cues) {
|
|
107
|
+
return cues.map((c) => ({ t: r3(c.start), text: c.text.replace(/\s+/g, " ").trim() })).filter((c) => c.text);
|
|
108
|
+
}
|
|
109
|
+
export function attach(segments, items, field) {
|
|
110
|
+
const out = segments.map((s) => {
|
|
111
|
+
const { [field]: _old, ...rest } = s;
|
|
112
|
+
return rest;
|
|
113
|
+
});
|
|
114
|
+
if (out.length === 0)
|
|
115
|
+
return out;
|
|
116
|
+
for (const item of [...items].sort((a, b) => a.t - b.t)) {
|
|
117
|
+
let i = out.findIndex((s) => item.t >= s.start && item.t < s.end);
|
|
118
|
+
if (i === -1)
|
|
119
|
+
i = item.t < out[0].start ? 0 : out.length - 1;
|
|
120
|
+
const seg = out[i];
|
|
121
|
+
(seg[field] ??= []).push(item);
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
export function fingerprintFile(path) {
|
|
126
|
+
const st = statSync(path);
|
|
127
|
+
const hash = createHash("sha1");
|
|
128
|
+
const chunk = 64 * 1024;
|
|
129
|
+
const fd = openSync(path, "r");
|
|
130
|
+
try {
|
|
131
|
+
const buf = Buffer.alloc(chunk);
|
|
132
|
+
for (const pos of st.size > chunk ? [0, st.size - chunk] : [0]) {
|
|
133
|
+
const n = readSync(fd, buf, 0, chunk, pos);
|
|
134
|
+
hash.update(buf.subarray(0, n));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
closeSync(fd);
|
|
139
|
+
}
|
|
140
|
+
return { size: st.size, mtimeMs: Math.round(st.mtimeMs), sample: hash.digest("hex") };
|
|
141
|
+
}
|
|
142
|
+
export function sameFingerprint(a, b) {
|
|
143
|
+
return a.size === b.size && a.mtimeMs === b.mtimeMs && a.sample === b.sample;
|
|
144
|
+
}
|
|
145
|
+
export function parseIndex(text) {
|
|
146
|
+
try {
|
|
147
|
+
const data = JSON.parse(text);
|
|
148
|
+
if (data?.version !== INDEX_VERSION || !Array.isArray(data.segments) || !data.fingerprint)
|
|
149
|
+
return null;
|
|
150
|
+
return data;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
export function readIndex(path) {
|
|
157
|
+
try {
|
|
158
|
+
return parseIndex(readFileSync(path, "utf8"));
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
export function writeIndex(path, index) {
|
|
165
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
166
|
+
writeFileSync(tmp, JSON.stringify(index));
|
|
167
|
+
renameSync(tmp, path);
|
|
168
|
+
}
|
|
169
|
+
function snippet(text) {
|
|
170
|
+
const s = text.replace(/\s+/g, " ").trim();
|
|
171
|
+
return s.length > MAX_SNIPPET ? `${s.slice(0, MAX_SNIPPET - 1)}…` : s;
|
|
172
|
+
}
|
|
173
|
+
function itemsOf(index) {
|
|
174
|
+
const items = [];
|
|
175
|
+
index.segments.forEach((s, seg) => {
|
|
176
|
+
for (const text of s.ocr)
|
|
177
|
+
items.push({ seg, source: "screen", t: s.start, text });
|
|
178
|
+
for (const [source, list] of [["event", s.events], ["marker", s.markers], ["transcript", s.transcript]]) {
|
|
179
|
+
for (const it of list ?? [])
|
|
180
|
+
items.push({ seg, source, t: it.t, text: it.text });
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
return items;
|
|
184
|
+
}
|
|
185
|
+
export function searchIndex(index, query, opts = {}) {
|
|
186
|
+
if (query.trim().length === 0)
|
|
187
|
+
throw new ScreencastError("query must not be empty.");
|
|
188
|
+
if (query.length > MAX_QUERY_LENGTH) {
|
|
189
|
+
throw new ScreencastError(`The query is ${query.length} characters; at most ${MAX_QUERY_LENGTH} are allowed.`);
|
|
190
|
+
}
|
|
191
|
+
const items = itemsOf(index);
|
|
192
|
+
const matched = new Map();
|
|
193
|
+
const add = (item, score) => {
|
|
194
|
+
const entry = matched.get(item.seg) ?? { score: 0, matches: [] };
|
|
195
|
+
entry.score += score;
|
|
196
|
+
entry.matches.push(item);
|
|
197
|
+
matched.set(item.seg, entry);
|
|
198
|
+
};
|
|
199
|
+
if (opts.regex) {
|
|
200
|
+
const ranges = regexRanges(items.map((i) => i.text), query);
|
|
201
|
+
items.forEach((item, i) => {
|
|
202
|
+
if ((ranges[i] ?? []).length > 0)
|
|
203
|
+
add(item, 2);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
const needle = normalizeForMatch(query.trim());
|
|
208
|
+
const words = needle.split("_").filter(Boolean);
|
|
209
|
+
const norm = items.map((i) => normalizeForMatch(i.text));
|
|
210
|
+
items.forEach((item, i) => {
|
|
211
|
+
if (norm[i].includes(needle))
|
|
212
|
+
add(item, 2);
|
|
213
|
+
});
|
|
214
|
+
if (words.length > 1) {
|
|
215
|
+
const bySeg = new Map();
|
|
216
|
+
items.forEach((item, i) => bySeg.set(item.seg, [...(bySeg.get(item.seg) ?? []), i]));
|
|
217
|
+
for (const [seg, idx] of bySeg) {
|
|
218
|
+
if (matched.has(seg))
|
|
219
|
+
continue;
|
|
220
|
+
if (!words.every((w) => idx.some((i) => norm[i].includes(w))))
|
|
221
|
+
continue;
|
|
222
|
+
for (const i of idx)
|
|
223
|
+
if (words.some((w) => norm[i].includes(w)))
|
|
224
|
+
add(items[i], 0);
|
|
225
|
+
matched.get(seg).score = 1;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const ranked = [...matched.entries()]
|
|
230
|
+
.map(([seg, m]) => ({ seg, ...m }))
|
|
231
|
+
.sort((a, b) => b.score - a.score || index.segments[a.seg].start - index.segments[b.seg].start);
|
|
232
|
+
const chosen = ranked.slice(0, opts.limit ?? DEFAULT_SEARCH_LIMIT);
|
|
233
|
+
const pattern = opts.secretPattern === undefined ? DEFAULT_SECRET_PATTERN : opts.secretPattern;
|
|
234
|
+
const shown = chosen.flatMap((c) => c.matches.slice(0, MAX_MATCHES_PER_HIT));
|
|
235
|
+
const masked = pattern ? maskSecrets(shown.map((m) => m.text), pattern) : shown.map((m) => m.text);
|
|
236
|
+
let k = 0;
|
|
237
|
+
const hits = chosen.map((c) => {
|
|
238
|
+
const seg = index.segments[c.seg];
|
|
239
|
+
const matches = c.matches.slice(0, MAX_MATCHES_PER_HIT).map((m) => ({ source: m.source, t: m.t, text: snippet(masked[k++] ?? "") }));
|
|
240
|
+
return { start: seg.start, end: seg.end, t: Math.min(...c.matches.map((m) => m.t)), score: c.score, matches };
|
|
241
|
+
});
|
|
242
|
+
return { hits, total: matched.size };
|
|
243
|
+
}
|
|
244
|
+
export async function resolveRecording(args) {
|
|
245
|
+
if ((args.input === undefined) === (args.sessionId === undefined)) {
|
|
246
|
+
throw new ScreencastError("Provide exactly one of: input (a recording path) or sessionId.");
|
|
247
|
+
}
|
|
248
|
+
if (args.sessionId !== undefined) {
|
|
249
|
+
const record = getStore().get(args.sessionId);
|
|
250
|
+
if (!record)
|
|
251
|
+
throw new ScreencastError(`No session with id "${args.sessionId}".`);
|
|
252
|
+
if (record.status === "recording")
|
|
253
|
+
throw new ScreencastError("The recording is still running; stop_recording it first.");
|
|
254
|
+
await whenFinalized(record.id);
|
|
255
|
+
if (!existsSync(record.outputPath))
|
|
256
|
+
throw new ScreencastError(`The session's recording is missing: ${record.outputPath}`);
|
|
257
|
+
const events = record.events?.path;
|
|
258
|
+
const markers = record.markers?.length ? record.markers : readMarkers(record.outputPath);
|
|
259
|
+
return { path: record.outputPath, eventsPath: events && existsSync(events) ? events : null, markers };
|
|
260
|
+
}
|
|
261
|
+
const path = args.input;
|
|
262
|
+
if (!existsSync(path))
|
|
263
|
+
throw new ScreencastError(`Input file not found: ${path}`);
|
|
264
|
+
const { jsonl } = eventPaths(path);
|
|
265
|
+
return { path, eventsPath: existsSync(jsonl) ? jsonl : null, markers: readMarkers(path) };
|
|
266
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { maskSecrets, DEFAULT_SECRET_PATTERN } from "./bugReport.js";
|
|
2
|
+
import { formatTimecode } from "./sheet.js";
|
|
3
|
+
import { ringSize, DEFAULT_RING_COLOR } from "./zoom.js";
|
|
4
|
+
export const MAX_GUIDE_STEPS = 100;
|
|
5
|
+
export const SHOT_LEAD_SEC = 0.1;
|
|
6
|
+
export const SHOT_MAX_WIDTH = 1280;
|
|
7
|
+
const LABEL_MAX = 40;
|
|
8
|
+
const LABEL_REACH_PX = 24;
|
|
9
|
+
export const GUIDE_FILES = {
|
|
10
|
+
markdown: "guide.md",
|
|
11
|
+
html: "guide.html",
|
|
12
|
+
images: "images",
|
|
13
|
+
image: (n) => `step-${String(n).padStart(2, "0")}.png`,
|
|
14
|
+
};
|
|
15
|
+
export function caption(s) {
|
|
16
|
+
return s.label ? `${s.text} ${s.label}` : s.text;
|
|
17
|
+
}
|
|
18
|
+
const r3 = (n) => Math.round(n * 1000) / 1000;
|
|
19
|
+
export function shotTime(t, durationSec, fps) {
|
|
20
|
+
const last = Math.max(0, durationSec - 1 / (fps > 0 ? fps : 30));
|
|
21
|
+
return r3(Math.min(Math.max(0, t - SHOT_LEAD_SEC), last));
|
|
22
|
+
}
|
|
23
|
+
function distance(r, x, y) {
|
|
24
|
+
const dx = Math.max(r.x - x, 0, x - (r.x + r.width));
|
|
25
|
+
const dy = Math.max(r.y - y, 0, y - (r.y + r.height));
|
|
26
|
+
return Math.hypot(dx, dy);
|
|
27
|
+
}
|
|
28
|
+
export function labelAt(lines, x, y) {
|
|
29
|
+
const near = lines
|
|
30
|
+
.map((line) => ({ line, d: distance(line, x, y) }))
|
|
31
|
+
.filter((c) => c.d <= LABEL_REACH_PX && c.line.text.trim().length > 0)
|
|
32
|
+
.sort((a, b) => a.d - b.d || a.line.width * a.line.height - b.line.width * b.line.height);
|
|
33
|
+
const best = near[0]?.line;
|
|
34
|
+
if (!best)
|
|
35
|
+
return null;
|
|
36
|
+
let text = best.text.replace(/\s+/g, " ").trim();
|
|
37
|
+
if (text.length > LABEL_MAX) {
|
|
38
|
+
const word = [...best.words].sort((a, b) => distance(a, x, y) - distance(b, x, y))[0];
|
|
39
|
+
text = word ? word.text.trim() : `${text.slice(0, LABEL_MAX - 1).trimEnd()}…`;
|
|
40
|
+
}
|
|
41
|
+
return text.length > 0 ? text : null;
|
|
42
|
+
}
|
|
43
|
+
function clickVerb(s) {
|
|
44
|
+
const verb = s.action === "double_click" ? "double-click" : "click";
|
|
45
|
+
const b = s.button && s.button !== "left" ? `${s.button.charAt(0).toUpperCase()}${s.button.slice(1)}-${verb}` : verb;
|
|
46
|
+
return b.charAt(0).toUpperCase() + b.slice(1);
|
|
47
|
+
}
|
|
48
|
+
export function guideSteps(steps, opts) {
|
|
49
|
+
const pattern = opts.secretPattern ?? DEFAULT_SECRET_PATTERN;
|
|
50
|
+
const mask = (s) => maskSecrets([s], pattern)[0] ?? s;
|
|
51
|
+
let n = 0;
|
|
52
|
+
return steps.map((s, i) => {
|
|
53
|
+
if (s.action === "gap")
|
|
54
|
+
return { n: 0, t: s.t, action: s.action, text: s.text };
|
|
55
|
+
n++;
|
|
56
|
+
const base = { n, t: s.t, action: s.action, text: s.text, shotT: shotTime(s.t, opts.durationSec, opts.fps) };
|
|
57
|
+
const label = opts.labels?.[i];
|
|
58
|
+
if (s.action === "click" || s.action === "double_click") {
|
|
59
|
+
const verb = clickVerb(s);
|
|
60
|
+
if (s.inFrame === false)
|
|
61
|
+
return { ...base, text: `${verb} outside the recorded area` };
|
|
62
|
+
return {
|
|
63
|
+
...base,
|
|
64
|
+
x: s.x,
|
|
65
|
+
y: s.y,
|
|
66
|
+
...(label ? { text: verb, label: mask(label) } : { text: `${verb} the marked spot` }),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (s.action === "scroll") {
|
|
70
|
+
return { ...base, ...(s.inFrame === false ? {} : { x: s.x, y: s.y }), ...(label ? { text: `${s.text} over`, label: mask(label) } : {}) };
|
|
71
|
+
}
|
|
72
|
+
if (s.action === "shortcut")
|
|
73
|
+
return s.keys ? { ...base, text: "Press", label: s.keys } : base;
|
|
74
|
+
return { ...base, text: "Type text" };
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
export function assignSections(steps, markers) {
|
|
78
|
+
const sorted = [...markers].sort((a, b) => a.t - b.t);
|
|
79
|
+
return steps.map((s) => {
|
|
80
|
+
const m = sorted.filter((k) => k.t <= s.t + 1e-6).at(-1);
|
|
81
|
+
return m ? { ...s, section: m.label } : s;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
export function shotFilter(size, opts) {
|
|
85
|
+
const n = (v) => String(Math.round(v));
|
|
86
|
+
const chain = [];
|
|
87
|
+
for (const b of opts.boxes ?? [])
|
|
88
|
+
chain.push(`drawbox=x=${n(b.x)}:y=${n(b.y)}:w=${n(b.width)}:h=${n(b.height)}:color=black:t=fill`);
|
|
89
|
+
const scale = `scale=w='min(iw,${SHOT_MAX_WIDTH})':h=-2:flags=lanczos`;
|
|
90
|
+
const base = `[0:v]${chain.length ? chain.join(",") + "," : ""}format=rgba`;
|
|
91
|
+
if (!opts.click)
|
|
92
|
+
return `${base},${scale},format=rgb24[out]`;
|
|
93
|
+
const D = ringSize(size.width, size.height);
|
|
94
|
+
const c = D / 2;
|
|
95
|
+
const th = Math.max(3, Math.round(D / 10));
|
|
96
|
+
const r = c - th / 2 - 1;
|
|
97
|
+
const color = opts.color ?? DEFAULT_RING_COLOR;
|
|
98
|
+
return (`${base}[bg];` +
|
|
99
|
+
`color=c=${color}:s=${D}x${D}:d=1,format=rgba,` +
|
|
100
|
+
`geq=r='r(X,Y)':g='g(X,Y)':b='b(X,Y)':a='if(lt(abs(hypot(X-${c},Y-${c})-${r}),${th / 2}),255,0)'[ring];` +
|
|
101
|
+
`[bg][ring]overlay=x=${n(opts.click.x - c)}:y=${n(opts.click.y - c)},${scale},format=rgb24[out]`);
|
|
102
|
+
}
|
|
103
|
+
export function buildStepFrameArgs(input, t, output) {
|
|
104
|
+
return ["-y", "-v", "error", "-ss", String(t), "-i", input, "-frames:v", "1", output];
|
|
105
|
+
}
|
|
106
|
+
export function buildStepShotArgs(frame, filter, output) {
|
|
107
|
+
return ["-y", "-v", "error", "-i", frame, "-filter_complex", filter, "-map", "[out]", "-frames:v", "1", output];
|
|
108
|
+
}
|
|
109
|
+
const md = (s) => s.replace(/\s*\n\s*/g, " ").replace(/([\\`*_[\]<>#|])/g, "\\$1");
|
|
110
|
+
function footer(g) {
|
|
111
|
+
const scan = g.ocr === "read"
|
|
112
|
+
? `Screenshots scanned for secrets with OCR (best-effort); ${g.redactions} region${g.redactions === 1 ? "" : "s"} boxed out.`
|
|
113
|
+
: "Screenshots were not scanned for secrets: review them before sharing.";
|
|
114
|
+
return `${scan} Typed text is never logged, so it never appears here.`;
|
|
115
|
+
}
|
|
116
|
+
function intro(g) {
|
|
117
|
+
const count = g.steps.filter((s) => s.n > 0).length;
|
|
118
|
+
return `${count} step${count === 1 ? "" : "s"} from a ${formatTimecode(g.recording.durationSec)} recording.`;
|
|
119
|
+
}
|
|
120
|
+
export function buildGuideMarkdown(g) {
|
|
121
|
+
const out = [`# ${md(g.title)}`, "", intro(g), ""];
|
|
122
|
+
if (g.steps.length === 0)
|
|
123
|
+
out.push(`_${g.stepsNote ?? "No steps in the recording."}_`, "");
|
|
124
|
+
let section;
|
|
125
|
+
for (const s of g.steps) {
|
|
126
|
+
if (s.section !== undefined && s.section !== section) {
|
|
127
|
+
section = s.section;
|
|
128
|
+
out.push(`## ${md(section)}`, "");
|
|
129
|
+
}
|
|
130
|
+
if (s.n === 0) {
|
|
131
|
+
out.push(`> _${md(s.text)}_`, "");
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const line = s.label ? `${md(s.text)} **${md(s.label)}**` : md(s.text);
|
|
135
|
+
out.push(`${s.n}. ${line} <sub>${formatTimecode(s.t)}</sub>`, "");
|
|
136
|
+
if (s.image)
|
|
137
|
+
out.push(` `, "");
|
|
138
|
+
}
|
|
139
|
+
if (g.stepsOmitted)
|
|
140
|
+
out.push(`_${g.stepsOmitted} more steps not shown._`, "");
|
|
141
|
+
out.push("<sub>", footer(g), `Generated by screencast-mcp step_guide, ${g.createdAt}.`, "</sub>", "");
|
|
142
|
+
return out.join("\n");
|
|
143
|
+
}
|
|
144
|
+
const html = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
145
|
+
const STYLE = `:root{--bg:#f6f5f2;--card:#fff;--ink:#16161a;--dim:#6b6b74;--line:#e2e0da;--rec:#e5383b}
|
|
146
|
+
@media (prefers-color-scheme:dark){:root{--bg:#121214;--card:#1b1b1f;--ink:#ececf1;--dim:#9a9aa6;--line:#2c2c33}}
|
|
147
|
+
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:16px/1.55 system-ui,-apple-system,"Segoe UI",sans-serif}
|
|
148
|
+
main{max-width:920px;margin:0 auto;padding:40px 16px 64px}
|
|
149
|
+
header{border-bottom:1px solid var(--line);padding-bottom:20px;margin-bottom:28px}
|
|
150
|
+
.rec{font:600 12px/1 ui-monospace,"Cascadia Mono",Consolas,monospace;letter-spacing:.12em;color:var(--rec);text-transform:uppercase}
|
|
151
|
+
.rec::before{content:"";display:inline-block;width:8px;height:8px;border-radius:50%;background:var(--rec);margin-right:8px;vertical-align:1px}
|
|
152
|
+
h1{font-size:30px;line-height:1.2;margin:10px 0 6px}h2{font-size:20px;margin:40px 0 16px}
|
|
153
|
+
.meta{color:var(--dim);margin:0}ol{list-style:none;margin:0;padding:0}
|
|
154
|
+
li{background:var(--card);border:1px solid var(--line);border-radius:10px;margin:0 0 18px;overflow:hidden}
|
|
155
|
+
.cap{display:flex;gap:14px;align-items:baseline;padding:14px 18px}
|
|
156
|
+
.n{flex:none;font:700 14px/1 ui-monospace,Consolas,monospace;color:var(--rec);min-width:2.2em}
|
|
157
|
+
.t{margin-left:auto;font:12px/1 ui-monospace,Consolas,monospace;color:var(--dim)}
|
|
158
|
+
li img{display:block;width:100%;height:auto;border-top:1px solid var(--line)}
|
|
159
|
+
.note{color:var(--dim);font-style:italic;margin:0 0 18px;padding-left:18px;border-left:3px solid var(--line)}
|
|
160
|
+
footer{color:var(--dim);font-size:13px;margin-top:40px;border-top:1px solid var(--line);padding-top:16px}`;
|
|
161
|
+
export function buildGuideHtml(g, imageSrc) {
|
|
162
|
+
const body = [];
|
|
163
|
+
let section;
|
|
164
|
+
let open = false;
|
|
165
|
+
const close = () => {
|
|
166
|
+
if (open)
|
|
167
|
+
body.push("</ol>");
|
|
168
|
+
open = false;
|
|
169
|
+
};
|
|
170
|
+
for (const s of g.steps) {
|
|
171
|
+
if (s.section !== undefined && s.section !== section) {
|
|
172
|
+
close();
|
|
173
|
+
section = s.section;
|
|
174
|
+
body.push(`<h2>${html(section)}</h2>`);
|
|
175
|
+
}
|
|
176
|
+
if (s.n === 0) {
|
|
177
|
+
close();
|
|
178
|
+
body.push(`<p class="note">${html(s.text)}</p>`);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (!open)
|
|
182
|
+
body.push("<ol>");
|
|
183
|
+
open = true;
|
|
184
|
+
const text = s.label ? `${html(s.text)} <strong>${html(s.label)}</strong>` : html(s.text);
|
|
185
|
+
body.push(`<li><div class="cap"><span class="n">${String(s.n).padStart(2, "0")}</span><span>${text}</span>` +
|
|
186
|
+
`<span class="t">${formatTimecode(s.t)}</span></div>` +
|
|
187
|
+
(s.image ? `<img src="${html(imageSrc(s.image))}" alt="Step ${s.n}: ${html(caption(s))}" loading="lazy">` : "") +
|
|
188
|
+
"</li>");
|
|
189
|
+
}
|
|
190
|
+
close();
|
|
191
|
+
if (g.steps.length === 0)
|
|
192
|
+
body.push(`<p class="note">${html(g.stepsNote ?? "No steps in the recording.")}</p>`);
|
|
193
|
+
if (g.stepsOmitted)
|
|
194
|
+
body.push(`<p class="note">${g.stepsOmitted} more steps not shown.</p>`);
|
|
195
|
+
return [
|
|
196
|
+
"<!doctype html>",
|
|
197
|
+
'<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">',
|
|
198
|
+
`<title>${html(g.title)}</title><style>${STYLE}</style></head><body><main>`,
|
|
199
|
+
`<header><div class="rec">Step guide</div><h1>${html(g.title)}</h1><p class="meta">${html(intro(g))}</p></header>`,
|
|
200
|
+
...body,
|
|
201
|
+
`<footer>${html(footer(g))}<br>Generated by screencast-mcp step_guide, ${html(g.createdAt)}.</footer>`,
|
|
202
|
+
"</main></body></html>",
|
|
203
|
+
"",
|
|
204
|
+
].join("\n");
|
|
205
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const DOUBLE_CLICK_SEC = 0.5;
|
|
2
|
+
const DOUBLE_CLICK_PX = 6;
|
|
3
|
+
const SCROLL_JOIN_SEC = 1;
|
|
4
|
+
const r3 = (n) => Math.round(n * 1000) / 1000;
|
|
5
|
+
const GAP_WHY = {
|
|
6
|
+
input_not_seen: "input went to an elevated window or the lock screen, or the hook was removed",
|
|
7
|
+
helper_stalled: "the event logger paused",
|
|
8
|
+
helper_stopped: "the event logger stopped",
|
|
9
|
+
};
|
|
10
|
+
function clickText(button, double, e) {
|
|
11
|
+
const verb = double ? "double-click" : "click";
|
|
12
|
+
const which = button && button !== "left" ? `${button.charAt(0).toUpperCase()}${button.slice(1)} ${verb}` : `${verb.charAt(0).toUpperCase()}${verb.slice(1)}`;
|
|
13
|
+
return e.inFrame ? `${which} at (${e.x}, ${e.y})` : `${which} outside the recorded area`;
|
|
14
|
+
}
|
|
15
|
+
export function inputSteps(events) {
|
|
16
|
+
const steps = [];
|
|
17
|
+
const sorted = [...events].sort((a, b) => a.t - b.t);
|
|
18
|
+
let lastDown = null;
|
|
19
|
+
let scroll = null;
|
|
20
|
+
const scrollText = (axis, notches) => {
|
|
21
|
+
const dir = axis === "vertical" ? (notches >= 0 ? "up" : "down") : notches >= 0 ? "right" : "left";
|
|
22
|
+
const n = Math.abs(notches);
|
|
23
|
+
return `Scroll ${dir} ${n} notch${n === 1 ? "" : "es"}`;
|
|
24
|
+
};
|
|
25
|
+
for (const e of sorted) {
|
|
26
|
+
if (e.type === "move" || e.type === "up")
|
|
27
|
+
continue;
|
|
28
|
+
if (e.type !== "wheel")
|
|
29
|
+
scroll = null;
|
|
30
|
+
if (e.type === "down") {
|
|
31
|
+
const prev = lastDown;
|
|
32
|
+
if (prev &&
|
|
33
|
+
prev.e.button === e.button &&
|
|
34
|
+
e.t - prev.e.t <= DOUBLE_CLICK_SEC &&
|
|
35
|
+
Math.abs(e.x - prev.e.x) <= DOUBLE_CLICK_PX &&
|
|
36
|
+
Math.abs(e.y - prev.e.y) <= DOUBLE_CLICK_PX &&
|
|
37
|
+
prev.step.action === "click" &&
|
|
38
|
+
steps.at(-1) === prev.step) {
|
|
39
|
+
prev.step.action = "double_click";
|
|
40
|
+
prev.step.text = clickText(e.button, true, prev.e);
|
|
41
|
+
lastDown = null;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const step = {
|
|
45
|
+
t: e.t,
|
|
46
|
+
action: "click",
|
|
47
|
+
text: clickText(e.button, false, e),
|
|
48
|
+
x: e.x,
|
|
49
|
+
y: e.y,
|
|
50
|
+
button: e.button ?? "left",
|
|
51
|
+
inFrame: e.inFrame,
|
|
52
|
+
};
|
|
53
|
+
steps.push(step);
|
|
54
|
+
lastDown = { e, step };
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
lastDown = null;
|
|
58
|
+
if (e.type === "wheel") {
|
|
59
|
+
const axis = e.axis ?? "vertical";
|
|
60
|
+
const delta = e.delta ?? 0;
|
|
61
|
+
if (scroll && scroll.axis === axis && e.t - scroll.last <= SCROLL_JOIN_SEC && Math.sign(delta) === Math.sign(scroll.notches)) {
|
|
62
|
+
scroll.notches += delta;
|
|
63
|
+
scroll.last = e.t;
|
|
64
|
+
scroll.step.text = scrollText(axis, scroll.notches);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const step = { t: e.t, action: "scroll", text: scrollText(axis, delta), x: e.x, y: e.y, inFrame: e.inFrame };
|
|
68
|
+
steps.push(step);
|
|
69
|
+
scroll = { step, axis, notches: delta, last: e.t };
|
|
70
|
+
}
|
|
71
|
+
else if (e.type === "key") {
|
|
72
|
+
steps.push({ t: e.t, action: "shortcut", text: `Press ${e.keys ?? "a shortcut"}`, ...(e.keys ? { keys: e.keys } : {}) });
|
|
73
|
+
}
|
|
74
|
+
else if (e.type === "typing") {
|
|
75
|
+
const d = e.durationSec ?? 0;
|
|
76
|
+
steps.push({ t: e.t, action: "typing", text: `Type${d > 0 ? ` for ${r3(d)} s` : ""} (keys are not logged)`, durationSec: d });
|
|
77
|
+
}
|
|
78
|
+
else if (e.type === "gap") {
|
|
79
|
+
const why = GAP_WHY[e.reason ?? "input_not_seen"];
|
|
80
|
+
const d = r3(e.durationSec ?? 0);
|
|
81
|
+
steps.push({ t: e.t, action: "gap", text: `Input may be missing for ${d} s (${why})`, durationSec: d });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return steps;
|
|
85
|
+
}
|
|
86
|
+
export function reconstructSteps(events) {
|
|
87
|
+
return inputSteps(events).map(({ button: _b, inFrame: _i, keys: _k, durationSec: _d, ...step }) => step);
|
|
88
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const r3 = (v) => Math.round(v * 1000) / 1000;
|
|
2
|
+
export function cutShift(t, spans) {
|
|
3
|
+
let removed = 0;
|
|
4
|
+
for (const s of spans) {
|
|
5
|
+
if (t <= s.start)
|
|
6
|
+
break;
|
|
7
|
+
removed += Math.min(t, s.end) - s.start;
|
|
8
|
+
}
|
|
9
|
+
return removed;
|
|
10
|
+
}
|
|
11
|
+
export function mapTime(t, ops) {
|
|
12
|
+
let v = t;
|
|
13
|
+
for (const op of ops) {
|
|
14
|
+
if (op.op === "trim") {
|
|
15
|
+
if (v < op.start || v >= op.end)
|
|
16
|
+
return null;
|
|
17
|
+
v -= op.start;
|
|
18
|
+
}
|
|
19
|
+
else if (op.op === "speed")
|
|
20
|
+
v /= op.factor;
|
|
21
|
+
else
|
|
22
|
+
v -= cutShift(v, op.spans);
|
|
23
|
+
}
|
|
24
|
+
return v;
|
|
25
|
+
}
|
|
26
|
+
export function mapWindow(from, to, ops) {
|
|
27
|
+
let a = from;
|
|
28
|
+
let b = to;
|
|
29
|
+
for (const op of ops) {
|
|
30
|
+
if (op.op === "trim") {
|
|
31
|
+
a = Math.max(a, op.start) - op.start;
|
|
32
|
+
b = Math.min(b, op.end) - op.start;
|
|
33
|
+
}
|
|
34
|
+
else if (op.op === "speed") {
|
|
35
|
+
a /= op.factor;
|
|
36
|
+
b /= op.factor;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
a -= cutShift(a, op.spans);
|
|
40
|
+
b -= cutShift(b, op.spans);
|
|
41
|
+
}
|
|
42
|
+
if (b <= a)
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return [a, b];
|
|
46
|
+
}
|
|
47
|
+
export function mapSpans(spans, ops) {
|
|
48
|
+
return spans.flatMap((s) => {
|
|
49
|
+
const w = mapWindow(s.start, s.end, ops);
|
|
50
|
+
return w ? [{ start: r3(w[0]), end: r3(w[1]) }] : [];
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
export function retimeCues(cues, ops) {
|
|
54
|
+
return cues.flatMap((c) => {
|
|
55
|
+
const w = mapWindow(c.start, c.end, ops);
|
|
56
|
+
return w && w[1] - w[0] >= 0.05 ? [{ ...c, start: r3(w[0]), end: r3(w[1]) }] : [];
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const ASS_TIME = /^(\d+):(\d{2}):(\d{2})\.(\d{2})$/;
|
|
60
|
+
const assSec = (s) => {
|
|
61
|
+
const m = s.trim().match(ASS_TIME);
|
|
62
|
+
return m ? Number(m[1]) * 3600 + Number(m[2]) * 60 + Number(m[3]) + Number(m[4]) / 100 : null;
|
|
63
|
+
};
|
|
64
|
+
const assTime = (sec) => {
|
|
65
|
+
const cs = Math.round(sec * 100);
|
|
66
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
67
|
+
return `${Math.floor(cs / 360_000)}:${pad(Math.floor((cs % 360_000) / 6000))}:${pad(Math.floor((cs % 6000) / 100))}.${pad(cs % 100)}`;
|
|
68
|
+
};
|
|
69
|
+
export function retimeAss(text, ops) {
|
|
70
|
+
return text
|
|
71
|
+
.split(/\r?\n/)
|
|
72
|
+
.flatMap((line) => {
|
|
73
|
+
const m = line.match(/^(Dialogue:\s*[^,]*,)([^,]*),([^,]*),(.*)$/);
|
|
74
|
+
if (!m)
|
|
75
|
+
return [line];
|
|
76
|
+
const [, head, s, e, rest] = m;
|
|
77
|
+
const a = assSec(s);
|
|
78
|
+
const b = assSec(e);
|
|
79
|
+
if (a === null || b === null)
|
|
80
|
+
return [line];
|
|
81
|
+
const w = mapWindow(a, b, ops);
|
|
82
|
+
return w && w[1] - w[0] >= 0.05 ? [`${head}${assTime(w[0])},${assTime(w[1])},${rest}`] : [];
|
|
83
|
+
})
|
|
84
|
+
.join("\n");
|
|
85
|
+
}
|