@clipy/cli 0.18.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.
Files changed (56) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/README.md +210 -9
  3. package/dist/context/importCmd.js +1 -1
  4. package/dist/context/ytdlp.js +1 -1
  5. package/dist/context-core/arec.js +1 -1
  6. package/dist/index.js +1042 -88
  7. package/dist/local/arec.js +186 -0
  8. package/dist/local/commands.js +1103 -0
  9. package/dist/local/deps.js +42 -0
  10. package/dist/local/media.js +154 -0
  11. package/dist/local/pipeline.js +530 -0
  12. package/dist/local/ports.js +1 -0
  13. package/dist/local/speechEngine.js +260 -0
  14. package/dist/local/transcriptFile.js +54 -0
  15. package/dist/local/vendor/capture/capture.js +442 -0
  16. package/dist/local/vendor/capture/check.js +103 -0
  17. package/dist/local/vendor/capture/children.js +69 -0
  18. package/dist/local/vendor/capture/ffmpegArgs.js +108 -0
  19. package/dist/local/vendor/capture/index.js +36 -0
  20. package/dist/local/vendor/capture/proc.js +71 -0
  21. package/dist/local/vendor/capture/scripts.js +107 -0
  22. package/dist/local/vendor/capture/sidecar.js +199 -0
  23. package/dist/local/vendor/capture/sources.js +272 -0
  24. package/dist/local/vendor/capture/types.js +1 -0
  25. package/dist/local/vendor/capture/virtualDisplay.js +170 -0
  26. package/dist/local/vendor/capture/xauth.js +68 -0
  27. package/dist/local/vendor/config/errors.js +12 -0
  28. package/dist/local/vendor/config/index.js +241 -0
  29. package/dist/local/vendor/config/paths.js +37 -0
  30. package/dist/local/vendor/config/presets.js +61 -0
  31. package/dist/local/vendor/config/schema.js +66 -0
  32. package/dist/local/vendor/moments/generated/drawStrokes.js +127 -0
  33. package/dist/local/vendor/moments/generated/keyMomentsCursorContext.js +568 -0
  34. package/dist/local/vendor/moments/generated/keyMomentsFusion.js +406 -0
  35. package/dist/local/vendor/moments/generated/localMomentPlan.js +178 -0
  36. package/dist/local/vendor/moments/generated/types.js +1 -0
  37. package/dist/local/vendor/moments/index.js +147 -0
  38. package/dist/local/vendor/moments/llm.js +158 -0
  39. package/dist/local/vendor/transcribe/audio.js +251 -0
  40. package/dist/local/vendor/transcribe/chunking.js +250 -0
  41. package/dist/local/vendor/transcribe/index.js +89 -0
  42. package/dist/local/vendor/transcribe/local.js +221 -0
  43. package/dist/local/vendor/transcribe/localWorker.js +79 -0
  44. package/dist/local/vendor/transcribe/models.js +369 -0
  45. package/dist/local/vendor/transcribe/providers/assemblyai.js +61 -0
  46. package/dist/local/vendor/transcribe/providers/common.js +105 -0
  47. package/dist/local/vendor/transcribe/providers/deepgram.js +59 -0
  48. package/dist/local/vendor/transcribe/providers/elevenlabs.js +43 -0
  49. package/dist/local/vendor/transcribe/providers/gemini.js +102 -0
  50. package/dist/local/vendor/transcribe/providers/http.js +214 -0
  51. package/dist/local/vendor/transcribe/providers/openai.js +48 -0
  52. package/dist/local/vendor/transcribe/tar.js +53 -0
  53. package/dist/local/vendor/transcribe/types.js +1 -0
  54. package/dist/macBridge.js +78 -9
  55. package/dist/skill.js +46 -1
  56. package/package.json +19 -9
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The real implementations behind `Deps` (ports.ts), from the vendored local
3
+ * edition packages. Tests inject fakes instead.
4
+ *
5
+ * Loaded lazily by the CLI (`await import("./local/deps.js")`), so commands
6
+ * that never run local mode do not load the capture, transcription or config
7
+ * code at all.
8
+ */
9
+ import * as capture from "./vendor/capture/index.js";
10
+ import * as config from "./vendor/config/index.js";
11
+ import * as transcribe from "./vendor/transcribe/index.js";
12
+ import { speechEngineStatus } from "./speechEngine.js";
13
+ export function realDeps() {
14
+ return {
15
+ // The engine lives in a folder of its own (downloaded on first use), so
16
+ // the vendored package is told where; it cannot resolve it by name.
17
+ transcribe: (input, cfg, options) => transcribe.transcribe(input, cfg, { ...options, speechEngine: options?.speechEngine ?? speechEngineStatus().entry ?? undefined }),
18
+ downloadModel: (model, options) => transcribe.downloadModel(model, options),
19
+ modelStatus: (model) => transcribe.modelStatus(model),
20
+ modelInfo: (model) => {
21
+ // `license` and `attribution` are optional in the model table; older
22
+ // vendored copies lack them and `clipy models` falls back to its own.
23
+ const spec = transcribe.LOCAL_MODELS[model];
24
+ return {
25
+ label: spec.label,
26
+ sizeBytes: spec.sizeBytes,
27
+ ...(typeof spec.license === "string" ? { license: spec.license } : {}),
28
+ ...(typeof spec.attribution === "string" ? { attribution: spec.attribution } : {}),
29
+ };
30
+ },
31
+ checkCapture: () => capture.checkCapture(),
32
+ listSources: () => capture.listSources(),
33
+ startCapture: (options) => capture.startCapture(options),
34
+ startVirtualDisplay: (options) => capture.startVirtualDisplay(options),
35
+ stopAllChildren: () => capture.stopAllChildren(),
36
+ loadConfig: () => config.loadConfig(),
37
+ // saveConfig validates the shape with zod at runtime, so this cast is checked.
38
+ saveConfig: (patch) => config.saveConfig(patch),
39
+ presetProvider: (preset, apiKey, model, baseUrl) => config.presetProvider(preset, apiKey, model, baseUrl),
40
+ configPath: () => config.configPath(),
41
+ };
42
+ }
@@ -0,0 +1,154 @@
1
+ /**
2
+ * The few ffmpeg operations the pipeline needs: duration, one frame at a time,
3
+ * and bringing an imported file into `recording.mp4`. ffmpeg is the only
4
+ * external program the local edition relies on.
5
+ */
6
+ import { spawn } from "node:child_process";
7
+ import { copyFile, rename, rm, stat } from "node:fs/promises";
8
+ import { extname } from "node:path";
9
+ /** Width cap for extracted frames: enough to read UI text, small enough for an agent's context. */
10
+ export const FRAME_MAX_WIDTH = 1280;
11
+ export function runFfmpeg(ffmpeg, args, signal) {
12
+ return new Promise((resolve, reject) => {
13
+ const child = spawn(ffmpeg, args, { stdio: ["ignore", "ignore", "pipe"], signal });
14
+ let stderr = "";
15
+ child.stderr.setEncoding("utf8");
16
+ child.stderr.on("data", (chunk) => {
17
+ // Keep the tail only; ffmpeg can be chatty on long files.
18
+ stderr = (stderr + chunk).slice(-64_000);
19
+ });
20
+ child.on("error", (error) => {
21
+ if (error.code === "ENOENT") {
22
+ reject(new Error(`ffmpeg was not found (${ffmpeg}). Install ffmpeg and try again; clipy doctor shows what is missing.`));
23
+ }
24
+ else {
25
+ reject(error);
26
+ }
27
+ });
28
+ child.on("close", (code) => resolve({ code, stderr }));
29
+ });
30
+ }
31
+ function lastLine(text) {
32
+ return text.trim().split("\n").pop()?.trim() ?? "";
33
+ }
34
+ function parseClock(h, m, s) {
35
+ return Math.round((Number(h) * 3600 + Number(m) * 60 + Number(s)) * 1000);
36
+ }
37
+ /** Duration in ms from ffmpeg's own header read, falling back to a full decode. */
38
+ export async function probeDurationMs(ffmpeg, file, signal) {
39
+ const header = await runFfmpeg(ffmpeg, ["-hide_banner", "-i", file], signal);
40
+ const match = /Duration:\s*(\d+):(\d{2}):(\d{2}(?:\.\d+)?)/.exec(header.stderr);
41
+ if (match)
42
+ return parseClock(match[1], match[2], match[3]);
43
+ if (/Invalid data found|No such file|does not contain any stream/i.test(header.stderr)) {
44
+ throw new Error(`ffmpeg cannot read ${file}: ${lastLine(header.stderr)}`);
45
+ }
46
+ // Fragmented or streamed files can lack a header duration: decode and read the last timestamp.
47
+ const decode = await runFfmpeg(ffmpeg, ["-hide_banner", "-nostats", "-progress", "pipe:2", "-i", file, "-f", "null", "-"], signal);
48
+ const times = [...decode.stderr.matchAll(/out_time_ms=(\d+)/g)];
49
+ if (times.length > 0)
50
+ return Math.round(Number(times[times.length - 1][1]) / 1000);
51
+ throw new Error(`ffmpeg could not tell how long ${file} is: ${lastLine(decode.stderr)}`);
52
+ }
53
+ /**
54
+ * Whether the file has an audio stream, read from ffmpeg's header dump. A
55
+ * headless browser capture or a silent screen capture has none, and there is
56
+ * nothing to transcribe then.
57
+ */
58
+ export async function hasAudioStream(ffmpeg, file, signal) {
59
+ const header = await runFfmpeg(ffmpeg, ["-hide_banner", "-i", file], signal);
60
+ return /Stream #\d+:\d+[^\n]*: Audio:/.test(header.stderr);
61
+ }
62
+ async function nonEmpty(path) {
63
+ try {
64
+ return (await stat(path)).size > 0;
65
+ }
66
+ catch {
67
+ return false;
68
+ }
69
+ }
70
+ /**
71
+ * One JPEG at `atMs`, scaled to at most 1280 wide. The last frame of a file
72
+ * sits a little before its duration, so a request that lands past it steps
73
+ * back. Returns the time actually used, or an error message.
74
+ */
75
+ export async function extractFrame(ffmpeg, video, atMs, output, signal) {
76
+ let lastError = "no frame decoded";
77
+ for (const stepBack of [0, 100, 500, 1000]) {
78
+ const t = Math.max(0, atMs - stepBack);
79
+ await rm(output, { force: true });
80
+ const run = await runFfmpeg(ffmpeg, [
81
+ "-hide_banner", "-loglevel", "error", "-y",
82
+ "-ss", (t / 1000).toFixed(3),
83
+ "-i", video,
84
+ "-frames:v", "1",
85
+ "-vf", `scale='min(${FRAME_MAX_WIDTH},iw)':-2`,
86
+ "-q:v", "3",
87
+ output,
88
+ ], signal);
89
+ if (run.code === 0 && (await nonEmpty(output)))
90
+ return { ok: true, frameTimeMs: t };
91
+ if (run.stderr.trim())
92
+ lastError = lastLine(run.stderr);
93
+ if (t === 0)
94
+ break;
95
+ }
96
+ await rm(output, { force: true });
97
+ return { ok: false, error: lastError };
98
+ }
99
+ /**
100
+ * Puts the video at `target` (always `recording.mp4`). An MP4 is copied, or
101
+ * moved when the caller owns it; anything else is remuxed without re-encoding
102
+ * when the codecs allow it, and re-encoded to H.264/AAC when they do not.
103
+ */
104
+ export async function placeVideo(ffmpeg, source, target, options = {}) {
105
+ if (source === target)
106
+ return;
107
+ // Everything is written to a temporary name and renamed into place only once
108
+ // it is complete, and the source is removed only after that. A failure or a
109
+ // cancel part-way never leaves a partial `target` that looks like the take,
110
+ // and never costs the source.
111
+ const partial = `${target}.tmp`;
112
+ const finish = async () => {
113
+ await rename(partial, target);
114
+ if (options.move)
115
+ await rm(source, { force: true });
116
+ };
117
+ try {
118
+ const ext = extname(source).toLowerCase();
119
+ if (ext === ".mp4" || ext === ".m4v") {
120
+ if (options.move) {
121
+ try {
122
+ await rename(source, target);
123
+ return;
124
+ }
125
+ catch {
126
+ // Different volume: fall through to a copy.
127
+ }
128
+ }
129
+ await copyFile(source, partial);
130
+ await finish();
131
+ return;
132
+ }
133
+ const remux = await runFfmpeg(ffmpeg, ["-hide_banner", "-loglevel", "error", "-y", "-i", source, "-map", "0:v:0", "-map", "0:a?", "-c", "copy", "-movflags", "+faststart", "-f", "mp4", partial], options.signal);
134
+ options.signal?.throwIfAborted();
135
+ if (remux.code === 0 && (await nonEmpty(partial))) {
136
+ await finish();
137
+ return;
138
+ }
139
+ const encode = await runFfmpeg(ffmpeg, [
140
+ "-hide_banner", "-loglevel", "error", "-y", "-i", source,
141
+ "-map", "0:v:0", "-map", "0:a?",
142
+ "-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-pix_fmt", "yuv420p",
143
+ "-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", "-f", "mp4", partial,
144
+ ], options.signal);
145
+ options.signal?.throwIfAborted();
146
+ if (encode.code !== 0 || !(await nonEmpty(partial))) {
147
+ throw new Error(`ffmpeg could not convert ${source} to MP4: ${lastLine(encode.stderr)}`);
148
+ }
149
+ await finish();
150
+ }
151
+ finally {
152
+ await rm(partial, { force: true });
153
+ }
154
+ }