deictic 0.3.0 → 0.4.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/API.md +31 -19
- package/README.md +6 -26
- package/dist/annotations/client.d.ts +1 -1
- package/dist/annotations/client.js +1 -1
- package/dist/annotations/index.d.ts +2 -101
- package/dist/annotations/index.js +1 -22
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +37 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.js +1 -10
- package/dist/logs/client.d.ts +1 -1
- package/dist/logs/client.js +1 -1
- package/dist/logs/index.d.ts +2 -18
- package/dist/logs/index.js +1 -21
- package/dist/network/client.d.ts +1 -1
- package/dist/network/client.js +1 -1
- package/dist/network/index.d.ts +1 -1
- package/dist/network/index.js +1 -37
- package/dist/recommended/index.d.ts +7 -7
- package/dist/recommended/index.js +1 -26
- package/dist/recordings/client.d.ts +1 -1
- package/dist/recordings/client.js +1 -1
- package/dist/recordings/index.d.ts +1 -1
- package/dist/screenshots/client.d.ts +1 -1
- package/dist/screenshots/client.js +1 -108
- package/dist/screenshots/dom.d.ts +1 -1
- package/dist/screenshots/dom.js +1 -1
- package/dist/screenshots/index.d.ts +1 -1
- package/dist/screenshots/index.js +1 -98
- package/dist/screenshots/rrweb.d.ts +2 -1
- package/dist/screenshots/rrweb.d.ts.map +1 -1
- package/dist/screenshots/rrweb.js +6 -2
- package/dist/screenshots/rrweb.js.map +1 -1
- package/dist/shared/annotations.js +22 -0
- package/dist/shared/annotations.js.map +1 -0
- package/dist/shared/board.js +190 -0
- package/dist/shared/board.js.map +1 -0
- package/dist/shared/client.js +108 -0
- package/dist/{screenshots → shared}/client.js.map +1 -1
- package/dist/shared/endpoints.js +4 -1
- package/dist/shared/endpoints.js.map +1 -1
- package/dist/shared/find.js +156 -0
- package/dist/shared/find.js.map +1 -0
- package/dist/shared/frame.js +75 -0
- package/dist/shared/frame.js.map +1 -0
- package/dist/shared/index.d.ts +5 -0
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index2.d.ts +85 -84
- package/dist/shared/index2.d.ts.map +1 -1
- package/dist/shared/index3.d.ts +83 -89
- package/dist/shared/index3.d.ts.map +1 -1
- package/dist/shared/index4.d.ts +98 -50
- package/dist/shared/index4.d.ts.map +1 -1
- package/dist/shared/index5.d.ts +13 -215
- package/dist/shared/index5.d.ts.map +1 -1
- package/dist/shared/index6.d.ts +55 -61
- package/dist/shared/index6.d.ts.map +1 -1
- package/dist/shared/index7.d.ts +224 -0
- package/dist/shared/index7.d.ts.map +1 -0
- package/dist/shared/index8.d.ts +52 -0
- package/dist/shared/index8.d.ts.map +1 -0
- package/dist/shared/index9.d.ts +70 -0
- package/dist/shared/index9.d.ts.map +1 -0
- package/dist/shared/init.js +472 -0
- package/dist/shared/init.js.map +1 -0
- package/dist/shared/local.js +165 -0
- package/dist/shared/local.js.map +1 -0
- package/dist/shared/logs.js +22 -0
- package/dist/shared/logs.js.map +1 -0
- package/dist/shared/network.js +39 -0
- package/dist/shared/network.js.map +1 -0
- package/dist/shared/packages.js +125 -0
- package/dist/shared/packages.js.map +1 -0
- package/dist/shared/playwright.js +36 -9
- package/dist/shared/playwright.js.map +1 -1
- package/dist/shared/recommended.js +26 -0
- package/dist/shared/recommended.js.map +1 -0
- package/dist/shared/recording.js +316 -0
- package/dist/shared/recording.js.map +1 -0
- package/dist/shared/recordings.js +2 -1
- package/dist/shared/recordings.js.map +1 -1
- package/dist/shared/screenshots.js +108 -0
- package/dist/shared/screenshots.js.map +1 -0
- package/dist/shared/src.js +10 -0
- package/dist/shared/src.js.map +1 -0
- package/dist/shared/task.js +108 -0
- package/dist/shared/task.js.map +1 -0
- package/dist/shared/tasks.js +33 -191
- package/dist/shared/tasks.js.map +1 -1
- package/dist/shared/transcribe.js +128 -0
- package/dist/shared/transcribe.js.map +1 -0
- package/dist/shared/vb.js +13 -0
- package/dist/shared/vb.js.map +1 -0
- package/dist/shared/vite.d.ts +16 -0
- package/dist/shared/vite.d.ts.map +1 -0
- package/dist/shared/vite.js +61 -0
- package/dist/shared/vite.js.map +1 -0
- package/dist/shared/voice.js +84 -0
- package/dist/{voice/index.js.map → shared/voice.js.map} +1 -1
- package/dist/shared/voice2.js +90 -0
- package/dist/shared/voice2.js.map +1 -0
- package/dist/shared/watch.js +209 -0
- package/dist/shared/watch.js.map +1 -0
- package/dist/shared/watchers.js +62 -0
- package/dist/shared/watchers.js.map +1 -0
- package/dist/tailwind/client.d.ts +1 -1
- package/dist/tailwind/client.js +1 -1
- package/dist/tailwind/index.d.ts +1 -1
- package/dist/tailwind/index.js +1 -1
- package/dist/tasks/client.d.ts +1 -1
- package/dist/tasks/client.d.ts.map +1 -1
- package/dist/tasks/client.js +45 -8
- package/dist/tasks/client.js.map +1 -1
- package/dist/tasks/index.d.ts +1 -1
- package/dist/tasks/index.js +3 -1
- package/dist/vite.d.ts +2 -15
- package/dist/vite.js +2 -60
- package/dist/voice/client.d.ts +1 -1
- package/dist/voice/client.js +1 -1
- package/dist/voice/index.d.ts +2 -52
- package/dist/voice/index.js +1 -84
- package/package.json +6 -9
- package/skills/deictic/SKILL.md +27 -42
- package/skills/deictic/references/recordings.md +9 -108
- package/skills/deictic/references/tasks.md +21 -107
- package/skills/deictic/references/watch.md +13 -66
- package/src/cli/board.ts +204 -0
- package/src/cli/bun.ts +133 -0
- package/src/cli/edit.ts +56 -0
- package/src/cli/find.ts +147 -0
- package/src/cli/frame.ts +90 -0
- package/src/cli/init.ts +388 -0
- package/src/cli/recording.ts +383 -0
- package/src/cli/task.ts +126 -0
- package/src/cli/transcribe.ts +129 -0
- package/src/cli/vb.ts +11 -0
- package/src/cli/voice.ts +59 -0
- package/src/cli/watch.ts +221 -0
- package/src/cli.ts +37 -0
- package/src/index.ts +5 -0
- package/src/plugins/logs/index.ts +1 -0
- package/src/plugins/network/index.ts +1 -0
- package/src/plugins/recordings/index.ts +1 -0
- package/src/plugins/screenshots/client.ts +3 -2
- package/src/plugins/screenshots/index.ts +10 -3
- package/src/plugins/screenshots/playwright.ts +34 -7
- package/src/plugins/screenshots/rrweb.ts +5 -1
- package/src/plugins/tasks/client.ts +62 -7
- package/src/plugins/tasks/index.ts +41 -20
- package/src/plugins/tasks/local.ts +19 -6
- package/src/plugins/tasks/watchers.ts +41 -6
- package/src/tools/endpoints.ts +3 -0
- package/src/tools/packages.ts +107 -0
- package/dist/annotations/index.d.ts.map +0 -1
- package/dist/annotations/index.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/logs/index.d.ts.map +0 -1
- package/dist/logs/index.js.map +0 -1
- package/dist/network/index.js.map +0 -1
- package/dist/recommended/index.js.map +0 -1
- package/dist/screenshots/index.js.map +0 -1
- package/dist/vite.d.ts.map +0 -1
- package/dist/vite.js.map +0 -1
- package/dist/voice/index.d.ts.map +0 -1
- package/skills/deictic/references/install.md +0 -103
- package/skills/deictic/scripts/frame.mjs +0 -62
- package/skills/deictic/scripts/lib.mjs +0 -277
- package/skills/deictic/scripts/recording.mjs +0 -266
- package/skills/deictic/scripts/task.mjs +0 -113
- package/skills/deictic/scripts/transcribe.mjs +0 -93
- package/skills/deictic/scripts/vb.mjs +0 -12
- package/skills/deictic/scripts/voice.mjs +0 -50
- package/skills/deictic/scripts/watch.mjs +0 -115
package/src/cli/bun.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `init` for Bun's own dev server: `Bun.serve` with HTML imports, which bundles the page with the plugins bunfig.toml
|
|
3
|
+
* names. Only that: a Bun project that runs Vite, Next.js or another bundler is that bundler's, and one with no HTML
|
|
4
|
+
* imports has no page of Bun's for deictic to be in. Three steps, as the API has them: the plugin's module, bunfig's
|
|
5
|
+
* `[serve.static] plugins`, and its routes spread into `Bun.serve`'s.
|
|
6
|
+
*/
|
|
7
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
8
|
+
import { dirname, join, relative } from 'node:path';
|
|
9
|
+
|
|
10
|
+
import MagicString from 'magic-string';
|
|
11
|
+
import { type CallExpression, parseSync, Visitor } from 'oxc-parser';
|
|
12
|
+
|
|
13
|
+
import { addFirst, addImports, eolOf } from './edit.ts';
|
|
14
|
+
|
|
15
|
+
/** The plugin's module, from the root, which bunfig names and the server imports. */
|
|
16
|
+
export const MODULE = 'deictic.plugin.ts';
|
|
17
|
+
|
|
18
|
+
export const PLUGIN = `import { deictic } from 'deictic/bun';
|
|
19
|
+
import { recommended } from 'deictic/recommended';
|
|
20
|
+
|
|
21
|
+
// One module for both: bunfig.toml names it as the dev server's bundler plugin, and the server spreads its routes,
|
|
22
|
+
// deictic's endpoints, into its own.
|
|
23
|
+
export default deictic({ plugins: recommended() });
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const SOURCE = /\.[cm]?[jt]sx?$/;
|
|
27
|
+
const SKIP = new Set(['node_modules', 'dist', 'build', 'out']);
|
|
28
|
+
const SERVE = /\bBun\.serve\s*\(/;
|
|
29
|
+
const HTML = /\bfrom\s*['"][^'"]+\.html['"]/;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The module that serves the page with Bun's bundler: it calls `Bun.serve` and imports an HTML file. Looked for under
|
|
33
|
+
* the root, not in dependencies, builds or dot folders, a few folders deep.
|
|
34
|
+
*/
|
|
35
|
+
export function serverOf(root: string): string | undefined {
|
|
36
|
+
const walk = (dir: string, depth: number): string | undefined => {
|
|
37
|
+
let entries;
|
|
38
|
+
try {
|
|
39
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
for (const e of entries) {
|
|
44
|
+
if (!e.isFile() || !SOURCE.test(e.name)) continue;
|
|
45
|
+
const path = join(dir, e.name);
|
|
46
|
+
const code = readFileSync(path, 'utf8');
|
|
47
|
+
if (SERVE.test(code) && HTML.test(code)) return path;
|
|
48
|
+
}
|
|
49
|
+
if (depth === 0) return undefined;
|
|
50
|
+
for (const e of entries) {
|
|
51
|
+
if (!e.isDirectory() || e.name.startsWith('.') || SKIP.has(e.name)) continue;
|
|
52
|
+
const found = walk(join(dir, e.name), depth - 1);
|
|
53
|
+
if (found !== undefined) return found;
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
};
|
|
57
|
+
return walk(root, 3);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* bunfig.toml with the plugin's module first in `[serve.static] plugins` (first, so it sees the source as written),
|
|
62
|
+
* made where there's none; unchanged where it's there already, and nothing where bunfig says it another way (a dotted
|
|
63
|
+
* key, an inline table) and it can't tell where.
|
|
64
|
+
*/
|
|
65
|
+
export function addToBunfig(text: string | undefined, entry = `./${MODULE}`): string | undefined {
|
|
66
|
+
const quoted = JSON.stringify(entry);
|
|
67
|
+
if (text === undefined) return `[serve.static]\nplugins = [${quoted}]\n`;
|
|
68
|
+
if (text.includes(entry)) return text;
|
|
69
|
+
if (/serve\.static\.|static\s*=\s*\{/.test(text)) return undefined;
|
|
70
|
+
const eol = eolOf(text);
|
|
71
|
+
const header = /^[ \t]*\[serve\.static\][ \t]*(#.*)?$/m.exec(text);
|
|
72
|
+
if (header === null)
|
|
73
|
+
return `${text}${text === '' || text.endsWith('\n') ? '' : eol}${eol}[serve.static]${eol}plugins = [${quoted}]${eol}`;
|
|
74
|
+
const bodyStart = header.index + header[0].length;
|
|
75
|
+
const next = /^[ \t]*\[/m.exec(text.slice(bodyStart));
|
|
76
|
+
const body = text.slice(bodyStart, next === null ? text.length : bodyStart + next.index);
|
|
77
|
+
const list = /^[ \t]*plugins[ \t]*=[ \t]*\[/m.exec(body);
|
|
78
|
+
if (list === null) return `${text.slice(0, bodyStart)}${eol}plugins = [${quoted}]${text.slice(bodyStart)}`;
|
|
79
|
+
const open = bodyStart + list.index + list[0].length;
|
|
80
|
+
const rest = text.slice(open);
|
|
81
|
+
const empty = /^\s*\]/.test(rest);
|
|
82
|
+
const lineBreak = /^[ \t]*\r?\n([ \t]*)/.exec(rest);
|
|
83
|
+
if (empty) return `${text.slice(0, open)}${quoted}${rest.trimStart()}`;
|
|
84
|
+
if (lineBreak !== null) return `${text.slice(0, open)}${eol}${lineBreak[1]}${quoted},${rest}`;
|
|
85
|
+
return `${text.slice(0, open)}${quoted}, ${rest.trimStart()}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The server with the plugin's routes first in `Bun.serve`'s `routes`, or `routes` with them where it has none, and
|
|
90
|
+
* the module imported: nothing where `Bun.serve` isn't given an object it can see, or its routes aren't one.
|
|
91
|
+
*/
|
|
92
|
+
export function addToServer(file: string, code: string, root: string): string | undefined {
|
|
93
|
+
const { program, errors } = parseSync(file, code, { preserveParens: false });
|
|
94
|
+
if (errors.length > 0) return undefined;
|
|
95
|
+
const calls: CallExpression[] = [];
|
|
96
|
+
new Visitor({
|
|
97
|
+
CallExpression(node) {
|
|
98
|
+
const { callee } = node;
|
|
99
|
+
if (
|
|
100
|
+
callee.type === 'MemberExpression' &&
|
|
101
|
+
callee.object.type === 'Identifier' &&
|
|
102
|
+
callee.object.name === 'Bun' &&
|
|
103
|
+
callee.property.type === 'Identifier' &&
|
|
104
|
+
callee.property.name === 'serve'
|
|
105
|
+
)
|
|
106
|
+
calls.push(node);
|
|
107
|
+
},
|
|
108
|
+
}).visit(program);
|
|
109
|
+
const options = calls.length === 1 ? calls[0]!.arguments[0] : undefined;
|
|
110
|
+
if (options?.type !== 'ObjectExpression') return undefined;
|
|
111
|
+
|
|
112
|
+
// `tools`, as the API names it, unless the module has a `tools` of its own.
|
|
113
|
+
const name = /\btools\b/.test(code) ? 'deicticTools' : 'tools';
|
|
114
|
+
const out = new MagicString(code);
|
|
115
|
+
const routes = options.properties.find(
|
|
116
|
+
(p) => p.type === 'Property' && !p.computed && p.key.type === 'Identifier' && p.key.name === 'routes',
|
|
117
|
+
);
|
|
118
|
+
if (routes === undefined) {
|
|
119
|
+
addFirst(out, code, options.start, options.properties[0], `routes: { ...${name}.routes }`);
|
|
120
|
+
} else {
|
|
121
|
+
if (routes.type !== 'Property' || routes.value.type !== 'ObjectExpression') return undefined;
|
|
122
|
+
addFirst(out, code, routes.value.start, routes.value.properties[0], `...${name}.routes`);
|
|
123
|
+
}
|
|
124
|
+
const from = relative(dirname(file), join(root, MODULE)).replaceAll('\\', '/');
|
|
125
|
+
addImports(out, program, code, [[name, from.startsWith('.') ? from : `./${from}`]]);
|
|
126
|
+
return out.toString();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** What to add by hand, for a server or a bunfig it can't edit. */
|
|
130
|
+
export const BY_HAND = {
|
|
131
|
+
bunfig: `[serve.static]\nplugins = ["./${MODULE}"]`,
|
|
132
|
+
server: `import tools from './${MODULE}';\nBun.serve({ routes: { ...tools.routes, '/*': index } });`,
|
|
133
|
+
};
|
package/src/cli/edit.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editing a project's own source the way it's written: its quotes, semicolons, line endings and indentation. What
|
|
3
|
+
* `init` adds to a bundler's config and to a Bun server goes through here.
|
|
4
|
+
*/
|
|
5
|
+
import type MagicString from 'magic-string';
|
|
6
|
+
import type { Program } from 'oxc-parser';
|
|
7
|
+
|
|
8
|
+
/** The file's line ending. */
|
|
9
|
+
export const eolOf = (code: string) => (code.includes('\r\n') ? '\r\n' : '\n');
|
|
10
|
+
|
|
11
|
+
/** The whitespace a line starts with, for the line `offset` is on. */
|
|
12
|
+
export function indentAt(code: string, offset: number): string {
|
|
13
|
+
const start = code.lastIndexOf('\n', offset - 1) + 1;
|
|
14
|
+
return /^[ \t]*/.exec(code.slice(start))![0];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Adds import lines after the module's last import (or `require`, in CommonJS), or at its top: written in the quotes
|
|
19
|
+
* and semicolons of its first import. Each line is `[what, from]`: `import ${what} from '${from}'`, or as a `require`.
|
|
20
|
+
*/
|
|
21
|
+
export function addImports(out: MagicString, program: Program, code: string, lines: [string, string][], commonjs = false): void {
|
|
22
|
+
const eol = eolOf(code);
|
|
23
|
+
const imports = program.body.filter((s) => s.type === 'ImportDeclaration');
|
|
24
|
+
const first = imports[0];
|
|
25
|
+
const quote = first === undefined ? "'" : code[first.source.start]!;
|
|
26
|
+
const semi = first === undefined || code.slice(first.start, first.end).trimEnd().endsWith(';') ? ';' : '';
|
|
27
|
+
const q = (s: string) => `${quote}${s}${quote}`;
|
|
28
|
+
const text = lines
|
|
29
|
+
.map(([what, from]) => (commonjs ? `const ${what} = require(${q(from)})${semi}` : `import ${what} from ${q(from)}${semi}`))
|
|
30
|
+
.join(eol);
|
|
31
|
+
const after = imports.at(-1) ?? requiresOf(program).at(-1);
|
|
32
|
+
if (after === undefined) out.prepend(`${text}${eol}${eol}`);
|
|
33
|
+
else out.appendLeft(after.end, `${eol}${text}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The top-level `const … = require(…)` statements of a CommonJS module, to put new ones after. */
|
|
37
|
+
function requiresOf(program: Program) {
|
|
38
|
+
return program.body.filter(
|
|
39
|
+
(s) =>
|
|
40
|
+
s.type === 'VariableDeclaration' &&
|
|
41
|
+
s.declarations.some(
|
|
42
|
+
(d) => d.init?.type === 'CallExpression' && d.init.callee.type === 'Identifier' && d.init.callee.name === 'require',
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Inserts `text` as the first property of the object literal that starts at `open` (its `{`), as the object is laid out. */
|
|
48
|
+
export function addFirst(out: MagicString, code: string, open: number, first: { start: number } | undefined, text: string): void {
|
|
49
|
+
const eol = eolOf(code);
|
|
50
|
+
if (first === undefined) {
|
|
51
|
+
out.appendLeft(open + 1, ` ${text} `);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const multiline = code.slice(open, first.start).includes('\n');
|
|
55
|
+
out.appendLeft(first.start, multiline ? `${text},${eol}${indentAt(code, first.start)}` : `${text}, `);
|
|
56
|
+
}
|
package/src/cli/find.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding what a task carries: a recording or a voice clip, by a task's ID, a recording's ID, a replay URL or a path,
|
|
3
|
+
* kept under a `.deictic/` near here or downloaded from the board. And the tools outside deictic some commands run
|
|
4
|
+
* (ffmpeg, the project's Playwright).
|
|
5
|
+
*/
|
|
6
|
+
import { spawnSync } from 'node:child_process';
|
|
7
|
+
import { existsSync, mkdtempSync, readdirSync, readFileSync } from 'node:fs';
|
|
8
|
+
import { createRequire } from 'node:module';
|
|
9
|
+
import { tmpdir } from 'node:os';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
import { pathToFileURL } from 'node:url';
|
|
12
|
+
|
|
13
|
+
import type { Kept } from '#src/plugins/recordings/index.ts';
|
|
14
|
+
|
|
15
|
+
import { deicticDirs, download, fail, type Message, task } from './board.ts';
|
|
16
|
+
|
|
17
|
+
const TASK = /^#?\d+$/;
|
|
18
|
+
|
|
19
|
+
export interface Found {
|
|
20
|
+
kept: Kept;
|
|
21
|
+
id: string;
|
|
22
|
+
/** Where it replays, where that's known. */
|
|
23
|
+
replay?: string;
|
|
24
|
+
/** The task it's attached to, where it was found by one. */
|
|
25
|
+
task?: Message;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const parsed = (path: string) => JSON.parse(readFileSync(path, 'utf8')) as Kept;
|
|
29
|
+
|
|
30
|
+
/** An attachment of a task, written to a temporary folder: its path. */
|
|
31
|
+
function fetched(root: Message, name: string): string {
|
|
32
|
+
const file = root.attachments.find((a) => a.name === name);
|
|
33
|
+
if (file === undefined) fail(`Task #${root.id} has no ${name}.`);
|
|
34
|
+
const path = join(mkdtempSync(join(tmpdir(), 'deictic-')), file.name);
|
|
35
|
+
download(file, path);
|
|
36
|
+
return path;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A recording, with where it came from. `ref` is a task's ID (its first recording, or the `nth`), a replay or
|
|
41
|
+
* recording URL, a recording's ID kept under a `.deictic/`, or a path.
|
|
42
|
+
*/
|
|
43
|
+
export async function loadRecording(ref: string | undefined, nth = 1): Promise<Found> {
|
|
44
|
+
if (ref === undefined) fail('Name a recording: a task (68), a replay URL, a recording ID or a JSON file.');
|
|
45
|
+
if (TASK.test(ref)) {
|
|
46
|
+
const root = task(ref);
|
|
47
|
+
const files = root.attachments.filter((a) => /^recording-.*\.json$/.test(a.name));
|
|
48
|
+
const file = files[nth - 1];
|
|
49
|
+
if (file === undefined) fail(`Task #${root.id} has ${files.length} recording(s), not a ${nth}th.`);
|
|
50
|
+
const id = file.name.slice('recording-'.length, -'.json'.length);
|
|
51
|
+
const replay = new RegExp(`https?://\\S+/replay/${id}`).exec(root.text)?.[0];
|
|
52
|
+
return { kept: parsed(fetched(root, file.name)), id, replay, task: root };
|
|
53
|
+
}
|
|
54
|
+
if (/^https?:/.test(ref)) {
|
|
55
|
+
const url = new URL(ref);
|
|
56
|
+
const id = url.pathname.split('/').at(-1) ?? '';
|
|
57
|
+
const json = new URL(url.pathname.replace(/\/(replay|recording)\/[^/]+$/, `/recording/${id}`), url);
|
|
58
|
+
const res = await fetch(json);
|
|
59
|
+
if (!res.ok) fail(`${json.href}: ${res.status} ${await res.text()}`);
|
|
60
|
+
return { kept: (await res.json()) as Kept, id, replay: new URL(json.pathname.replace(/\/recording\//, '/replay/'), url).href };
|
|
61
|
+
}
|
|
62
|
+
if (existsSync(ref)) return { kept: parsed(ref), id: ref.replace(/^.*[\\/]|\.json$/g, '') };
|
|
63
|
+
for (const dir of deicticDirs()) {
|
|
64
|
+
const path = join(dir, 'recordings', `${ref}.json`);
|
|
65
|
+
if (existsSync(path)) return { kept: parsed(path), id: ref };
|
|
66
|
+
}
|
|
67
|
+
return fail(`No recording ${ref} in ${deicticDirs().join(', ') || 'any .deictic/ near here'}: name its task or replay URL instead.`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A voice clip kept under a `.deictic/` near here, by its ID. */
|
|
71
|
+
function keptClip(id: string): string | undefined {
|
|
72
|
+
for (const dir of deicticDirs()) {
|
|
73
|
+
const voice = join(dir, 'voice');
|
|
74
|
+
const name = existsSync(voice) ? readdirSync(voice).find((f) => f.startsWith(`${id}.`)) : undefined;
|
|
75
|
+
if (name !== undefined) return join(voice, name);
|
|
76
|
+
}
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A voice clip's audio as a file: kept under a `.deictic/`, or downloaded from `from`. */
|
|
81
|
+
function voiceFile(id: string, from?: Message): string {
|
|
82
|
+
const kept = keptClip(id);
|
|
83
|
+
if (kept !== undefined) return kept;
|
|
84
|
+
const file = from?.attachments.find((a) => a.name.startsWith(`voice-${id}.`));
|
|
85
|
+
if (from === undefined || file === undefined) fail(`No voice clip ${id} here${from ? ` or on task #${from.id}` : ''}.`);
|
|
86
|
+
return fetched(from, file.name);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface Clip {
|
|
90
|
+
id: string;
|
|
91
|
+
file: string;
|
|
92
|
+
/** Where it starts in its recording, or 0 for a voice note on its own. */
|
|
93
|
+
at: number;
|
|
94
|
+
recording?: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The voice clips `ref` names: a task (every recording's clips and its voice notes), a recording as `loadRecording`
|
|
99
|
+
* takes one, or a clip's ID.
|
|
100
|
+
*/
|
|
101
|
+
export async function voiceClips(ref: string | undefined): Promise<Clip[]> {
|
|
102
|
+
if (ref === undefined) fail('Name a task, a recording or a voice clip.');
|
|
103
|
+
const clips: Clip[] = [];
|
|
104
|
+
if (TASK.test(ref)) {
|
|
105
|
+
const root = task(ref);
|
|
106
|
+
const recordings = root.attachments.filter((a) => /^recording-.*\.json$/.test(a.name));
|
|
107
|
+
const inRecordings = new Set<string>();
|
|
108
|
+
for (let n = 1; n <= recordings.length; n++) {
|
|
109
|
+
const { kept, id } = await loadRecording(ref, n);
|
|
110
|
+
for (const c of kept.voice ?? []) {
|
|
111
|
+
inRecordings.add(c.id);
|
|
112
|
+
clips.push({ id: c.id, file: voiceFile(c.id, root), at: c.at, recording: id });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
for (const a of root.attachments) {
|
|
116
|
+
const id = /^voice-(.+)\.\w+$/.exec(a.name)?.[1];
|
|
117
|
+
if (id !== undefined && !inRecordings.has(id)) clips.push({ id, file: voiceFile(id, root), at: 0 });
|
|
118
|
+
}
|
|
119
|
+
} else if (!/^https?:/.test(ref) && !existsSync(ref) && keptClip(ref) !== undefined) {
|
|
120
|
+
clips.push({ id: ref, file: voiceFile(ref), at: 0 });
|
|
121
|
+
} else {
|
|
122
|
+
const { kept, id } = await loadRecording(ref);
|
|
123
|
+
for (const c of kept.voice ?? []) clips.push({ id: c.id, file: voiceFile(c.id), at: c.at, recording: id });
|
|
124
|
+
}
|
|
125
|
+
if (clips.length === 0) fail(`No voice on ${ref}.`);
|
|
126
|
+
return clips;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Runs ffmpeg, which reads what MediaRecorder makes, and answers its log (stderr) and what it wrote to stdout. */
|
|
130
|
+
export function ffmpeg(args: string[]): { stdout: Buffer; log: string } {
|
|
131
|
+
const run = spawnSync('ffmpeg', ['-hide_banner', '-nostats', ...args], { maxBuffer: 1 << 30 });
|
|
132
|
+
if (run.error) fail('Needs ffmpeg on the PATH (https://ffmpeg.org/download.html).');
|
|
133
|
+
return { stdout: run.stdout, log: run.stderr.toString('utf8') };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The project's own copy of a package deictic doesn't bring (Playwright, either of its names), from here. */
|
|
137
|
+
export async function fromProject<T>(...names: string[]): Promise<T> {
|
|
138
|
+
const project = createRequire(join(process.cwd(), 'package.json'));
|
|
139
|
+
for (const name of names) {
|
|
140
|
+
try {
|
|
141
|
+
return (await import(pathToFileURL(project.resolve(name)).href)) as T;
|
|
142
|
+
} catch {
|
|
143
|
+
// Not this one: the next name.
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return fail(`Needs ${names.join(' or ')} in the project: npm install -D ${names[0]}`);
|
|
147
|
+
}
|
package/src/cli/frame.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deictic frame`: screenshots of a recording at moments of it, as its replay page shows them (marks and pointer
|
|
3
|
+
* included), at the recorded viewport's size. By default: just after each change to the marks, and the end. Needs
|
|
4
|
+
* the dev server running (it serves the replay) and Playwright in the project.
|
|
5
|
+
*/
|
|
6
|
+
import { mkdirSync } from 'node:fs';
|
|
7
|
+
import { tmpdir } from 'node:os';
|
|
8
|
+
import { join, resolve } from 'node:path';
|
|
9
|
+
|
|
10
|
+
import type { Browser, chromium } from 'playwright-core';
|
|
11
|
+
|
|
12
|
+
import { clock, fail, parseArgs } from './board.ts';
|
|
13
|
+
import { fromProject, loadRecording } from './find.ts';
|
|
14
|
+
|
|
15
|
+
export const usage =
|
|
16
|
+
'frame <task | replay URL | recording ID | file> [--at 4.2 ...] [--every 2] [--out dir] [--base url] PNGs of a recording';
|
|
17
|
+
|
|
18
|
+
interface Marks {
|
|
19
|
+
type: number;
|
|
20
|
+
timestamp: number;
|
|
21
|
+
data: { tag?: string; payload?: { svg?: string } };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function run(args: string[]): Promise<void> {
|
|
25
|
+
const { values, positionals } = parseArgs(args, {
|
|
26
|
+
nth: { type: 'string', default: '1' },
|
|
27
|
+
at: { type: 'string', multiple: true },
|
|
28
|
+
every: { type: 'string' },
|
|
29
|
+
out: { type: 'string' },
|
|
30
|
+
base: { type: 'string' },
|
|
31
|
+
});
|
|
32
|
+
const { kept, id, replay } = await loadRecording(positionals[0], Number(values.nth));
|
|
33
|
+
// The replay is served by the dev server that recorded it, under deictic's routes.
|
|
34
|
+
const page =
|
|
35
|
+
values.base === undefined
|
|
36
|
+
? (replay ?? `${new URL(kept.page).origin}/__deictic/plugins/recordings/replay/${id}`)
|
|
37
|
+
: `${values.base.replace(/\/$/, '')}/__deictic/plugins/recordings/replay/${id}`;
|
|
38
|
+
|
|
39
|
+
const events = kept.events as Marks[];
|
|
40
|
+
const t0 = events[0]?.timestamp ?? 0;
|
|
41
|
+
const end = kept.duration / 1000;
|
|
42
|
+
let times = (values.at ?? []).map(Number);
|
|
43
|
+
if (values.every !== undefined) for (let t = 0; t <= end; t += Number(values.every)) times.push(t);
|
|
44
|
+
if (times.length === 0) {
|
|
45
|
+
// Just after each change to the marks, so it's drawn, then the end.
|
|
46
|
+
const marks = events.filter((e) => e.type === 5 && e.data.tag === 'deictic.annotations' && (e.data.payload?.svg ?? '') !== '');
|
|
47
|
+
times = [...marks.map((e) => (e.timestamp - t0 + 50) / 1000), end];
|
|
48
|
+
}
|
|
49
|
+
times = [...new Set(times.map((t) => Math.min(Math.max(0, t), end)))].sort((a, b) => a - b);
|
|
50
|
+
if (times.some((t) => !Number.isFinite(t))) fail('--at and --every take seconds: --at 4.2');
|
|
51
|
+
|
|
52
|
+
const out = resolve(values.out ?? join(tmpdir(), 'deictic-frames', id));
|
|
53
|
+
mkdirSync(out, { recursive: true });
|
|
54
|
+
|
|
55
|
+
const playwright = await fromProject<{ chromium: typeof chromium }>('playwright-core', 'playwright');
|
|
56
|
+
let browser: Browser | undefined;
|
|
57
|
+
for (const channel of [undefined, 'chrome', 'msedge']) {
|
|
58
|
+
try {
|
|
59
|
+
browser = await playwright.chromium.launch(channel === undefined ? {} : { channel });
|
|
60
|
+
break;
|
|
61
|
+
} catch {
|
|
62
|
+
// Not this one: the next.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (browser === undefined) fail('No Chromium to launch: npx playwright install chromium, or install Chrome or Edge.');
|
|
66
|
+
try {
|
|
67
|
+
// `bare`: the replay alone, at the recorded size, so a window that size shows it 1:1.
|
|
68
|
+
const view = await browser.newPage({ viewport: kept.viewport });
|
|
69
|
+
for (const t of times) {
|
|
70
|
+
const url = new URL(page);
|
|
71
|
+
url.searchParams.set('t', t.toFixed(2));
|
|
72
|
+
url.searchParams.set('bare', '');
|
|
73
|
+
const res = await view.goto(url.href).catch((e: unknown) => (e instanceof Error ? e.message.split('\n')[0] : String(e)));
|
|
74
|
+
if (typeof res === 'string' || !res?.ok()) {
|
|
75
|
+
const why = typeof res === 'string' ? res : String(res?.status());
|
|
76
|
+
await browser.close();
|
|
77
|
+
fail(`${url.href}: ${why}. Is the dev server running? Start it, or pass --base with its URL.`);
|
|
78
|
+
}
|
|
79
|
+
await view.waitForSelector('body[data-ready]', { timeout: 15_000 });
|
|
80
|
+
// The replayed page's images and fonts.
|
|
81
|
+
await view.waitForLoadState('networkidle').catch(() => {});
|
|
82
|
+
await view.waitForTimeout(200);
|
|
83
|
+
const path = join(out, `${String(Math.round(t * 1000)).padStart(6, '0')}.png`);
|
|
84
|
+
await view.screenshot({ path });
|
|
85
|
+
console.log(`${clock(t * 1000).padStart(6)} ${path}`);
|
|
86
|
+
}
|
|
87
|
+
} finally {
|
|
88
|
+
await browser.close();
|
|
89
|
+
}
|
|
90
|
+
}
|