@tmhs/screencast-mcp 0.44.2 → 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 +58 -11
- package/assets/ui/player.html +9 -0
- package/assets/ui/player.js +20 -0
- package/dist/index.js +12 -0
- package/dist/prompts.js +34 -4
- package/dist/server.js +20 -0
- package/dist/tools/addMarker.js +89 -0
- package/dist/tools/backdrop.js +89 -0
- package/dist/tools/bugReport.js +266 -0
- package/dist/tools/chapters.js +156 -0
- package/dist/tools/contactSheet.js +19 -17
- package/dist/tools/cutByTranscript.js +86 -0
- package/dist/tools/findText.js +14 -64
- package/dist/tools/indexRecording.js +173 -0
- package/dist/tools/keystrokeOverlay.js +131 -0
- package/dist/tools/pipeline.js +251 -18
- package/dist/tools/reviewRecording.js +12 -0
- package/dist/tools/saveReplay.js +90 -0
- package/dist/tools/searchRecording.js +73 -0
- package/dist/tools/startRecording.js +51 -10
- package/dist/tools/stepGuide.js +193 -0
- package/dist/tools/stopRecording.js +25 -1
- package/dist/tools/transcribe.js +4 -3
- package/dist/tools/zoom.js +1 -1
- package/dist/utils/audio.js +12 -4
- package/dist/utils/backdrop.js +151 -0
- package/dist/utils/bugReport.js +172 -0
- package/dist/utils/captions.js +19 -6
- package/dist/utils/chapters.js +227 -0
- package/dist/utils/idle.js +10 -3
- package/dist/utils/keystrokes.js +155 -0
- package/dist/utils/lifecycle.js +11 -2
- package/dist/utils/media.js +5 -2
- package/dist/utils/ocrScan.js +77 -0
- package/dist/utils/pipeline.js +169 -15
- package/dist/utils/recipes.js +67 -0
- package/dist/utils/recordingIndex.js +266 -0
- package/dist/utils/replay.js +278 -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 +32 -6
- package/dist/utils/transcriptCut.js +134 -0
- package/package.json +1 -1
package/dist/tools/findText.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { extname, join } from "node:path";
|
|
3
|
-
import { existsSync, mkdtempSync,
|
|
3
|
+
import { existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import { errorResponse, okResponse, ScreencastError } from "../utils/errors.js";
|
|
6
6
|
import { READ_ONLY, looseOutput } from "../utils/toolSpec.js";
|
|
7
|
-
import { requireFfmpeg, runFfmpeg
|
|
8
|
-
import { jobTimeoutMs } from "../utils/jobs.js";
|
|
7
|
+
import { requireFfmpeg, runFfmpeg } from "../utils/ffmpeg.js";
|
|
9
8
|
import { resolveCaptureTarget } from "../utils/resolveTarget.js";
|
|
10
9
|
import { captureStill } from "../utils/still.js";
|
|
11
10
|
import { buildHighlightArgs, targetSize } from "../utils/diff.js";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
11
|
+
import { findMatches, redactionRects } from "../utils/ocr.js";
|
|
12
|
+
import { readFrames, scanVideoFrames, scanMatches } from "../utils/ocrScan.js";
|
|
14
13
|
import { windowMatchReport } from "../utils/windows.js";
|
|
15
14
|
import { confirmCapture, describeTarget } from "../utils/consent.js";
|
|
16
15
|
import { subdir, stamp, rand, stillOutput } from "../utils/paths.js";
|
|
@@ -62,34 +61,6 @@ const inputSchema = {
|
|
|
62
61
|
.optional()
|
|
63
62
|
.describe("Return the image with matches outlined (default: true with a query)."),
|
|
64
63
|
};
|
|
65
|
-
async function readFrames(sources, work, signal) {
|
|
66
|
-
const sized = await Promise.all(sources.map(async (source) => {
|
|
67
|
-
const { width, height } = await probeMedia(source);
|
|
68
|
-
if (!width || !height)
|
|
69
|
-
throw new ScreencastError("Could not read the image size.");
|
|
70
|
-
return { source, width, height };
|
|
71
|
-
}));
|
|
72
|
-
const passes = [];
|
|
73
|
-
const passScaleList = [];
|
|
74
|
-
const owner = [];
|
|
75
|
-
for (const [f, { source, width, height }] of sized.entries()) {
|
|
76
|
-
for (const [i, scale] of passScales(width, height).entries()) {
|
|
77
|
-
const out = join(work, `pass${f}-${i}.png`);
|
|
78
|
-
await runFfmpeg(["-y", "-v", "error", "-i", source, "-vf", `scale=iw*${scale}:ih*${scale}:flags=lanczos`, out], 60_000, {
|
|
79
|
-
signal,
|
|
80
|
-
});
|
|
81
|
-
passes.push(out);
|
|
82
|
-
passScaleList.push(scale);
|
|
83
|
-
owner.push(f);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
const results = await recognize(passes, passScaleList);
|
|
87
|
-
return sized.map(({ width, height }, f) => ({
|
|
88
|
-
lines: mergePasses(results.filter((_r, i) => owner[i] === f)),
|
|
89
|
-
width,
|
|
90
|
-
height,
|
|
91
|
-
}));
|
|
92
|
-
}
|
|
93
64
|
export function register(server) {
|
|
94
65
|
server.registerTool("find_text", {
|
|
95
66
|
title: "Find Text (OCR)",
|
|
@@ -180,43 +151,22 @@ async function scanClip(args, work, signal) {
|
|
|
180
151
|
if (IMAGE_EXTENSIONS.has(extname(args.input).toLowerCase())) {
|
|
181
152
|
throw new ScreencastError("scanEverySec is for a video input; an image has one frame, so drop scanEverySec.");
|
|
182
153
|
}
|
|
183
|
-
const info = await probeMedia(args.input);
|
|
184
154
|
const maxFrames = args.maxFrames ?? DEFAULT_SCAN_FRAMES;
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
if (frames.length === 0)
|
|
189
|
-
throw new ScreencastError("Could not read any frames from the video.");
|
|
190
|
-
const step = 1 / plan.fps;
|
|
191
|
-
const timeAt = (i) => Math.round(i * step * 1000) / 1000;
|
|
192
|
-
const end = info.videoDurationSec ?? info.durationSec ?? timeAt(frames.length - 1) + step;
|
|
193
|
-
const read = await readFrames(frames, work, signal);
|
|
194
|
-
const [size] = read;
|
|
155
|
+
const scan = await scanVideoFrames(args.input, { everySec: args.scanEverySec, maxFrames }, work, signal);
|
|
156
|
+
const { step } = scan;
|
|
157
|
+
const [size] = scan.frames;
|
|
195
158
|
if (!size)
|
|
196
159
|
throw new ScreencastError("Could not read any frames from the video.");
|
|
197
|
-
const matches = [];
|
|
198
|
-
const redactRegions = [];
|
|
199
160
|
const seenLines = new Set();
|
|
200
|
-
|
|
161
|
+
for (const frame of scan.frames)
|
|
201
162
|
for (const l of frame.lines)
|
|
202
163
|
seenLines.add(l.text);
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const t = timeAt(i);
|
|
207
|
-
const start = Math.max(0, Math.round((t - step) * 1000) / 1000);
|
|
208
|
-
const stop = Math.min(end, Math.round((t + step) * 1000) / 1000);
|
|
209
|
-
const rects = redactionRects(hits, frame);
|
|
210
|
-
for (const [k, hit] of hits.entries()) {
|
|
211
|
-
matches.push({ t, ...hit });
|
|
212
|
-
const rect = rects[k];
|
|
213
|
-
if (rect)
|
|
214
|
-
redactRegions.push({ ...rect, start, end: stop });
|
|
215
|
-
}
|
|
216
|
-
});
|
|
164
|
+
const { matches, redactRegions } = args.query
|
|
165
|
+
? scanMatches(scan, args.query, args.regex === true)
|
|
166
|
+
: { matches: [], redactRegions: [] };
|
|
217
167
|
const images = [];
|
|
218
168
|
const first = matches[0];
|
|
219
|
-
const firstFrame = first && frames.find((
|
|
169
|
+
const firstFrame = first && scan.frames.find((f) => f.t === first.t)?.path;
|
|
220
170
|
if ((args.returnImage ?? Boolean(args.query)) && first && firstFrame) {
|
|
221
171
|
const shown = join(work, "shown.png");
|
|
222
172
|
await runFfmpeg(buildHighlightArgs(firstFrame, matches.filter((m) => m.t === first.t), shown), 30_000, { signal });
|
|
@@ -228,8 +178,8 @@ async function scanClip(args, work, signal) {
|
|
|
228
178
|
imageSize: { width: size.width, height: size.height },
|
|
229
179
|
scan: {
|
|
230
180
|
everySec: Math.round(step * 1000) / 1000,
|
|
231
|
-
framesScanned: frames.length,
|
|
232
|
-
...(
|
|
181
|
+
framesScanned: scan.frames.length,
|
|
182
|
+
...(scan.clamped ? { note: `Widened from ${args.scanEverySec} s to fit maxFrames ${maxFrames}.` } : {}),
|
|
233
183
|
},
|
|
234
184
|
...(args.query
|
|
235
185
|
? {
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { errorResponse, okResponse, ScreencastError } from "../utils/errors.js";
|
|
6
|
+
import { WRITES_FILE, looseOutput } from "../utils/toolSpec.js";
|
|
7
|
+
import { requireFfmpeg, runFfmpeg, runCapture, probeMedia, hasFilter } from "../utils/ffmpeg.js";
|
|
8
|
+
import { jobTimeoutMs, progressReporter } from "../utils/jobs.js";
|
|
9
|
+
import { buildSceneScanArgs, parseSceneScan, buildSampleAtTimestampArgs } from "../utils/media.js";
|
|
10
|
+
import { readFrames } from "../utils/ocrScan.js";
|
|
11
|
+
import { ocrStatus } from "../utils/capabilities.js";
|
|
12
|
+
import { parseEventsJsonl } from "../utils/events.js";
|
|
13
|
+
import { resolveWhisperModel, buildTranscribeArgs, parseSrt, normalizeCues } from "../utils/captions.js";
|
|
14
|
+
import { subdir, tempPath } from "../utils/paths.js";
|
|
15
|
+
import { DEFAULT_INDEX_FRAMES, DEFAULT_INDEX_THRESHOLD, INDEX_VERSION, MAX_INDEX_FRAMES, attach, cueTexts, eventTexts, fingerprintFile, findIndex, indexWritePath, keyframeTime, markerTexts, mergeSegments, planKeyframes, readIndex, resolveRecording, sameFingerprint, segmentsFrom, writeIndex, } from "../utils/recordingIndex.js";
|
|
16
|
+
const OCR_BATCH = 8;
|
|
17
|
+
const inputSchema = {
|
|
18
|
+
input: z.string().optional().describe("Recording path."),
|
|
19
|
+
sessionId: z.string().optional().describe("Or a stopped session id."),
|
|
20
|
+
sceneThreshold: z.number().gt(0).lt(1).optional().describe(`Change starting a segment (default ${DEFAULT_INDEX_THRESHOLD}).`),
|
|
21
|
+
maxFrames: z.number().int().min(1).max(MAX_INDEX_FRAMES).optional().describe(`Keyframes to OCR (default ${DEFAULT_INDEX_FRAMES}).`),
|
|
22
|
+
transcript: z.string().optional().describe("An .srt, or 'whisper' to transcribe. Default: <video>.srt if any."),
|
|
23
|
+
force: z.boolean().optional().describe("Re-OCR despite a matching cached index."),
|
|
24
|
+
};
|
|
25
|
+
export function register(server) {
|
|
26
|
+
server.registerTool("index_recording", {
|
|
27
|
+
title: "Index Recording",
|
|
28
|
+
description: "Build a recording's text timeline (<video>.index.json: OCR per scene, events, markers, transcript) " +
|
|
29
|
+
"for search_recording.",
|
|
30
|
+
inputSchema,
|
|
31
|
+
outputSchema: looseOutput({ indexPath: z.string(), segments: z.number() }),
|
|
32
|
+
annotations: WRITES_FILE,
|
|
33
|
+
}, async (args, extra) => {
|
|
34
|
+
const work = mkdtempSync(join(tmpdir(), "screencast-index-"));
|
|
35
|
+
try {
|
|
36
|
+
const { ffmpeg } = requireFfmpeg();
|
|
37
|
+
const source = await resolveRecording(args);
|
|
38
|
+
const info = await probeMedia(source.path);
|
|
39
|
+
const duration = info.videoDurationSec ?? info.durationSec;
|
|
40
|
+
if (!info.width || duration === null || !(duration > 0)) {
|
|
41
|
+
throw new ScreencastError(`Could not read the video size and duration of ${source.path}.`);
|
|
42
|
+
}
|
|
43
|
+
const threshold = args.sceneThreshold ?? DEFAULT_INDEX_THRESHOLD;
|
|
44
|
+
const maxFrames = args.maxFrames ?? DEFAULT_INDEX_FRAMES;
|
|
45
|
+
const edits = subdir("edits");
|
|
46
|
+
const outPath = indexWritePath(source.path, edits);
|
|
47
|
+
const fingerprint = fingerprintFile(source.path);
|
|
48
|
+
const report = progressReporter(extra);
|
|
49
|
+
const notes = [];
|
|
50
|
+
const existingPath = args.force ? null : findIndex(source.path, edits);
|
|
51
|
+
const prior = existingPath ? readIndex(existingPath) : null;
|
|
52
|
+
const cached = prior !== null &&
|
|
53
|
+
sameFingerprint(prior.fingerprint, fingerprint) &&
|
|
54
|
+
prior.sceneThreshold === threshold &&
|
|
55
|
+
prior.maxFrames === maxFrames;
|
|
56
|
+
let segments;
|
|
57
|
+
let framesRead = 0;
|
|
58
|
+
let ocr = false;
|
|
59
|
+
if (cached) {
|
|
60
|
+
segments = prior.segments;
|
|
61
|
+
framesRead = prior.framesRead;
|
|
62
|
+
ocr = prior.ocr;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const scan = await runCapture(ffmpeg, buildSceneScanArgs(source.path, threshold), jobTimeoutMs(duration), {
|
|
66
|
+
signal: extra.signal,
|
|
67
|
+
});
|
|
68
|
+
if (scan.code !== 0)
|
|
69
|
+
throw new ScreencastError(`Scene scan failed (exit ${scan.code}): ${scan.stderr.trim().slice(-400)}`);
|
|
70
|
+
report?.(0.1);
|
|
71
|
+
const spans = segmentsFrom(planKeyframes(parseSceneScan(scan.stdout), duration, maxFrames), duration);
|
|
72
|
+
const texts = spans.map(() => []);
|
|
73
|
+
ocr = process.platform === "win32" && (await ocrStatus()).available;
|
|
74
|
+
if (ocr) {
|
|
75
|
+
for (let b = 0; b < spans.length; b += OCR_BATCH) {
|
|
76
|
+
extra.signal.throwIfAborted();
|
|
77
|
+
const batch = spans.slice(b, b + OCR_BATCH);
|
|
78
|
+
const frames = [];
|
|
79
|
+
for (const [i, span] of batch.entries()) {
|
|
80
|
+
const out = join(work, `key_${b + i}.png`);
|
|
81
|
+
await runFfmpeg(buildSampleAtTimestampArgs(source.path, keyframeTime(span), out), 60_000, { signal: extra.signal });
|
|
82
|
+
if (existsSync(out))
|
|
83
|
+
frames.push({ at: b + i, path: out });
|
|
84
|
+
}
|
|
85
|
+
const read = frames.length > 0 ? await readFrames(frames.map((f) => f.path), work, extra.signal) : [];
|
|
86
|
+
read.forEach((f, i) => (texts[frames[i].at] = f.lines.map((l) => l.text)));
|
|
87
|
+
framesRead += read.length;
|
|
88
|
+
report?.(0.1 + (0.85 * Math.min(spans.length, b + OCR_BATCH)) / spans.length);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
notes.push("Windows OCR is not available, so the index holds no screen text; install an OCR language (doctor).");
|
|
93
|
+
}
|
|
94
|
+
const read = spans.map((s, i) => ({ ...s, ocr: texts[i] ?? [] }));
|
|
95
|
+
segments = ocr ? mergeSegments(read) : read;
|
|
96
|
+
}
|
|
97
|
+
const events = source.eventsPath ? eventTexts(parseEventsJsonl(readFileSync(source.eventsPath, "utf8"))) : [];
|
|
98
|
+
segments = attach(segments, events, "events");
|
|
99
|
+
segments = attach(segments, markerTexts(source.markers), "markers");
|
|
100
|
+
const cues = await transcriptCues(args.transcript, source.path, info.audioCodec !== null, duration, extra.signal, notes);
|
|
101
|
+
if (cues !== null)
|
|
102
|
+
segments = attach(segments, cueTexts(cues), "transcript");
|
|
103
|
+
else if (cached) {
|
|
104
|
+
const kept = prior.segments.flatMap((s) => s.transcript ?? []);
|
|
105
|
+
segments = attach(segments, kept, "transcript");
|
|
106
|
+
}
|
|
107
|
+
const index = {
|
|
108
|
+
version: INDEX_VERSION,
|
|
109
|
+
video: source.path,
|
|
110
|
+
fingerprint,
|
|
111
|
+
durationSec: duration,
|
|
112
|
+
createdAt: new Date().toISOString(),
|
|
113
|
+
sceneThreshold: threshold,
|
|
114
|
+
maxFrames,
|
|
115
|
+
framesRead,
|
|
116
|
+
ocr,
|
|
117
|
+
segments,
|
|
118
|
+
};
|
|
119
|
+
mkdirSync(join(outPath, ".."), { recursive: true });
|
|
120
|
+
writeIndex(outPath, index);
|
|
121
|
+
report?.(1);
|
|
122
|
+
const count = (f) => segments.reduce((n, s) => n + (s[f]?.length ?? 0), 0);
|
|
123
|
+
return okResponse({
|
|
124
|
+
indexPath: outPath,
|
|
125
|
+
segments: segments.length,
|
|
126
|
+
cached,
|
|
127
|
+
framesRead,
|
|
128
|
+
durationSec: duration,
|
|
129
|
+
sources: {
|
|
130
|
+
screenLines: segments.reduce((n, s) => n + s.ocr.length, 0),
|
|
131
|
+
events: count("events"),
|
|
132
|
+
markers: count("markers"),
|
|
133
|
+
transcriptCues: count("transcript"),
|
|
134
|
+
},
|
|
135
|
+
...(notes.length > 0 ? { note: notes.join(" ") } : {}),
|
|
136
|
+
next: "search_recording { query } returns timecoded hits; the index holds raw screen text, so treat it like the video.",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
return errorResponse(error);
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
rmSync(work, { recursive: true, force: true });
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
async function transcriptCues(arg, video, hasAudio, duration, signal, notes) {
|
|
148
|
+
if (arg === "whisper") {
|
|
149
|
+
if (!hasAudio) {
|
|
150
|
+
notes.push("The recording has no audio, so there is no transcript.");
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
if (!hasFilter("whisper")) {
|
|
154
|
+
throw new ScreencastError("This ffmpeg has no whisper filter.", "transcript: 'whisper' needs FFmpeg 8 built with whisper.cpp; or pass an .srt.");
|
|
155
|
+
}
|
|
156
|
+
const model = resolveWhisperModel(undefined);
|
|
157
|
+
const raw = tempPath(".srt");
|
|
158
|
+
try {
|
|
159
|
+
await runFfmpeg(buildTranscribeArgs(video, model, raw), jobTimeoutMs(duration), { signal });
|
|
160
|
+
return existsSync(raw) ? normalizeCues(parseSrt(readFileSync(raw, "utf8"))) : [];
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
rmSync(raw, { force: true });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const path = arg ?? `${video.replace(/\.[^./\\]+$/, "")}.srt`;
|
|
167
|
+
if (!existsSync(path)) {
|
|
168
|
+
if (arg !== undefined)
|
|
169
|
+
throw new ScreencastError(`Transcript not found: ${arg}`);
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
return normalizeCues(parseSrt(readFileSync(path, "utf8")));
|
|
173
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { errorResponse, okResponse, ScreencastError } from "../utils/errors.js";
|
|
6
|
+
import { WRITES_FILE, looseOutput } from "../utils/toolSpec.js";
|
|
7
|
+
import { requireFfmpeg, probeMedia } from "../utils/ffmpeg.js";
|
|
8
|
+
import { runEditJob } from "../utils/jobs.js";
|
|
9
|
+
import { eventPaths, parseEventsJsonl } from "../utils/events.js";
|
|
10
|
+
import { resolveTextFont } from "../utils/fonts.js";
|
|
11
|
+
import { planBadges, groupBadges, buildKeystrokeArgs, BADGE_POSITIONS, DEFAULT_LINGER_SEC, } from "../utils/keystrokes.js";
|
|
12
|
+
import { editOutputExt, assertEditContainer } from "../utils/media.js";
|
|
13
|
+
import { resolveOutput, subdir, stamp, rand } from "../utils/paths.js";
|
|
14
|
+
import { fontFileSchema } from "./callout.js";
|
|
15
|
+
export const keystrokeOptionsSchema = {
|
|
16
|
+
events: z.string().optional(),
|
|
17
|
+
clicks: z.boolean().optional(),
|
|
18
|
+
typing: z.boolean().optional(),
|
|
19
|
+
keys: z.enum(["shortcuts", "masked"]).optional(),
|
|
20
|
+
lingerSec: z.number().positive().max(10).optional(),
|
|
21
|
+
position: z.enum(BADGE_POSITIONS).optional(),
|
|
22
|
+
size: z.number().min(8).max(400).optional(),
|
|
23
|
+
color: z.string().optional(),
|
|
24
|
+
fontFile: z.string().min(1).optional(),
|
|
25
|
+
};
|
|
26
|
+
const o = keystrokeOptionsSchema;
|
|
27
|
+
export function loadBadges(input, events, opts) {
|
|
28
|
+
const path = events ?? eventPaths(input).jsonl;
|
|
29
|
+
if (!existsSync(path)) {
|
|
30
|
+
throw new ScreencastError(`No event log for ${input} (looked for ${path}). Record with start_recording events: true ` +
|
|
31
|
+
'(keys: "shortcuts" to name shortcuts), or pass events: <path to .events.jsonl>.');
|
|
32
|
+
}
|
|
33
|
+
const log = parseEventsJsonl(readFileSync(path, "utf8"));
|
|
34
|
+
return { badges: planBadges(log, opts), shortcutsLogged: log.filter((e) => e.type === "key").length, path };
|
|
35
|
+
}
|
|
36
|
+
export function prepareKeystrokeAssets(labels, workDir, tag = "", fontFile) {
|
|
37
|
+
const font = resolveTextFont(labels.join(" "), { weight: "bold", fontFile });
|
|
38
|
+
const textFiles = {};
|
|
39
|
+
labels.forEach((label, i) => {
|
|
40
|
+
if (workDir === null) {
|
|
41
|
+
textFiles[label] = `<badge ${i}>`;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const f = join(workDir, `${tag}badge_${i}.txt`);
|
|
45
|
+
writeFileSync(f, label);
|
|
46
|
+
textFiles[label] = f;
|
|
47
|
+
});
|
|
48
|
+
return {
|
|
49
|
+
fontFile: font.fontFile,
|
|
50
|
+
textFiles,
|
|
51
|
+
...(font.fallback && { fallbackFont: font.fontFile }),
|
|
52
|
+
warnings: font.warning ? [font.warning] : [],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const inputSchema = {
|
|
56
|
+
input: z.string().min(1).describe("Path to the recording."),
|
|
57
|
+
events: o.events.describe("Event log (default: the input's .events.jsonl)."),
|
|
58
|
+
clicks: o.clicks.describe("Show clicks (default true)."),
|
|
59
|
+
typing: o.typing.describe('Show typing as "Typing…" (default true).'),
|
|
60
|
+
keys: o.keys.describe("shortcuts (default): the chord (Ctrl + S); masked: modifiers only (Ctrl + …)."),
|
|
61
|
+
lingerSec: o.lingerSec.describe(`Seconds a badge stays up (default ${DEFAULT_LINGER_SEC}).`),
|
|
62
|
+
position: o.position.describe("Default bottom (centred)."),
|
|
63
|
+
size: o.size.describe("Font px (default 4.5% of the frame height)."),
|
|
64
|
+
color: o.color.describe("Text color (default white)."),
|
|
65
|
+
fontFile: fontFileSchema,
|
|
66
|
+
dryRun: z.boolean().optional().describe("Return the planned badges; write nothing."),
|
|
67
|
+
output: z.string().optional().describe("Optional output path. Defaults under SCREENCAST_HOME/edits."),
|
|
68
|
+
overwrite: z.boolean().optional().describe("Allow replacing an existing file at the output path (default false)."),
|
|
69
|
+
};
|
|
70
|
+
export function register(server) {
|
|
71
|
+
server.registerTool("keystroke_overlay", {
|
|
72
|
+
title: "Keystroke Overlay",
|
|
73
|
+
description: "Burn keycast-style badges for shortcuts, clicks, and typing from the recording's input event log " +
|
|
74
|
+
"(start_recording events: true). Never shows typed text: the log has none.",
|
|
75
|
+
inputSchema,
|
|
76
|
+
outputSchema: looseOutput({ badges: z.number().int() }),
|
|
77
|
+
annotations: WRITES_FILE,
|
|
78
|
+
}, async (args, extra) => {
|
|
79
|
+
try {
|
|
80
|
+
requireFfmpeg();
|
|
81
|
+
if (!existsSync(args.input))
|
|
82
|
+
throw new ScreencastError(`Input file not found: ${args.input}`);
|
|
83
|
+
const plan = { clicks: args.clicks, typing: args.typing, keys: args.keys, lingerSec: args.lingerSec };
|
|
84
|
+
const { badges, shortcutsLogged } = loadBadges(args.input, args.events, plan);
|
|
85
|
+
const note = shortcutsLogged === 0
|
|
86
|
+
? 'The log names no shortcuts (recorded with keys: "masked"), so only clicks and typing can show.'
|
|
87
|
+
: undefined;
|
|
88
|
+
if (badges.length === 0) {
|
|
89
|
+
return okResponse({ badges: 0, note: "Nothing to show: the log has no clicks, shortcuts, or typing; nothing written." });
|
|
90
|
+
}
|
|
91
|
+
const groups = groupBadges(badges);
|
|
92
|
+
if (args.dryRun) {
|
|
93
|
+
const assets = prepareKeystrokeAssets(groups.map((g) => g.label), null, "", args.fontFile);
|
|
94
|
+
return okResponse({
|
|
95
|
+
dryRun: true,
|
|
96
|
+
badges: badges.length,
|
|
97
|
+
plan: badges,
|
|
98
|
+
...(assets.fallbackFont && { fallbackFont: assets.fallbackFont }),
|
|
99
|
+
...(assets.warnings.length > 0 && { warnings: assets.warnings }),
|
|
100
|
+
...(note && { note }),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
assertEditContainer(args.output);
|
|
104
|
+
const info = await probeMedia(args.input);
|
|
105
|
+
if (!info.height)
|
|
106
|
+
throw new ScreencastError(`Could not read the size of ${args.input}.`);
|
|
107
|
+
const output = resolveOutput(args.output, subdir("edits"), `keys-${stamp()}-${rand()}${editOutputExt(args.input)}`, args.overwrite);
|
|
108
|
+
const work = mkdtempSync(join(tmpdir(), "screencast-keys-"));
|
|
109
|
+
try {
|
|
110
|
+
const assets = prepareKeystrokeAssets(groups.map((g) => g.label), work, "", args.fontFile);
|
|
111
|
+
const style = { position: args.position, size: args.size, color: args.color };
|
|
112
|
+
const ffArgs = buildKeystrokeArgs(args.input, output, groups, assets, style, { height: info.height });
|
|
113
|
+
await runEditJob(ffArgs, { output, durationSec: info.durationSec, extra });
|
|
114
|
+
return okResponse({
|
|
115
|
+
outputPath: output,
|
|
116
|
+
badges: badges.length,
|
|
117
|
+
labels: groups.map((g) => g.label),
|
|
118
|
+
...(assets.fallbackFont && { fallbackFont: assets.fallbackFont }),
|
|
119
|
+
...(assets.warnings.length > 0 && { warnings: assets.warnings }),
|
|
120
|
+
...(note && { note }),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
rmSync(work, { recursive: true, force: true });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
return errorResponse(error);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|