@gnawbit/tabretake 0.0.0-stage → 0.4.1
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/CHANGELOG.md +89 -0
- package/LICENSE +21 -0
- package/README.md +569 -2
- package/README.zh-CN.md +532 -0
- package/SKILL.md +161 -0
- package/agents/openai.yaml +4 -0
- package/bin/demo-reel.mjs +2 -0
- package/bin/tabretake.mjs +12 -0
- package/examples/site/app.js +87 -0
- package/examples/site/index.html +168 -0
- package/examples/site/style.css +705 -0
- package/examples/stories/readme.json +34 -0
- package/examples/stories/release.json +48 -0
- package/examples/story-video.json +32 -0
- package/examples/story.json +46 -0
- package/package.json +62 -4
- package/references/rendering.md +77 -0
- package/references/story.md +80 -0
- package/schemas/story.schema.json +616 -0
- package/scripts/capabilities.mjs +22 -0
- package/scripts/cli.mjs +107 -0
- package/scripts/demo.mjs +39 -0
- package/scripts/doctor.mjs +83 -0
- package/scripts/init.mjs +118 -0
- package/scripts/lib/browser.mjs +208 -0
- package/scripts/lib/camera.mjs +63 -0
- package/scripts/lib/captions.mjs +39 -0
- package/scripts/lib/config.mjs +76 -0
- package/scripts/lib/ffmpeg.mjs +60 -0
- package/scripts/lib/fonts.mjs +36 -0
- package/scripts/lib/metadata.mjs +14 -0
- package/scripts/lib/navigation.mjs +26 -0
- package/scripts/lib/options.mjs +67 -0
- package/scripts/lib/report.mjs +42 -0
- package/scripts/lib/tty.mjs +18 -0
- package/scripts/plan.mjs +235 -0
- package/scripts/record.mjs +352 -0
- package/scripts/render.mjs +588 -0
- package/scripts/serve.mjs +74 -0
- package/scripts/setup.mjs +23 -0
- package/scripts/validate.mjs +18 -0
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { copyFile, mkdir, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parseCommand } from "./lib/options.mjs";
|
|
4
|
+
import {
|
|
5
|
+
clickAndType,
|
|
6
|
+
dragBy,
|
|
7
|
+
hover,
|
|
8
|
+
installCursor,
|
|
9
|
+
launchBrowser,
|
|
10
|
+
moveAndClick,
|
|
11
|
+
newContext,
|
|
12
|
+
resolveLocator,
|
|
13
|
+
} from "./lib/browser.mjs";
|
|
14
|
+
import { waitForEnter } from "./lib/tty.mjs";
|
|
15
|
+
import { assertConfig, readConfig } from "./lib/config.mjs";
|
|
16
|
+
import { commandError, createReporter, requireInteractiveLogin } from "./lib/report.mjs";
|
|
17
|
+
import { navigate } from "./lib/navigation.mjs";
|
|
18
|
+
|
|
19
|
+
function parseViewport(value, fallback = { width: 1440, height: 810 }) {
|
|
20
|
+
if (value === undefined) return fallback;
|
|
21
|
+
if (!/^[1-9]\d*x[1-9]\d*$/.test(value)) throw new Error("--viewport must be WIDTHxHEIGHT with positive integers");
|
|
22
|
+
const [width, height] = value.split("x").map(Number);
|
|
23
|
+
return { width, height };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function record(argv, command = "record") {
|
|
27
|
+
const preview = command === "preview";
|
|
28
|
+
const { values } = parseCommand(command, argv);
|
|
29
|
+
requireInteractiveLogin(values);
|
|
30
|
+
const reporter = createReporter(command, values.json);
|
|
31
|
+
const screenshots = [];
|
|
32
|
+
|
|
33
|
+
if (!values.config && !values.url) {
|
|
34
|
+
throw new Error("Provide --config demo.json (or at least --url)");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const configPath = values.config ? path.resolve(values.config) : null;
|
|
38
|
+
const config = configPath
|
|
39
|
+
? await readConfig(configPath)
|
|
40
|
+
: {};
|
|
41
|
+
|
|
42
|
+
assertConfig({ ...config, ...(values.url ? { url: values.url } : {}) }, { kind: "story" });
|
|
43
|
+
const url = values.url || config.url;
|
|
44
|
+
if (!url) throw new Error("Missing url in config or --url");
|
|
45
|
+
|
|
46
|
+
const viewport = parseViewport(values.viewport, config.viewport || {
|
|
47
|
+
width: 1440,
|
|
48
|
+
height: 810,
|
|
49
|
+
});
|
|
50
|
+
assertConfig({ ...config, url, viewport }, { kind: "story" });
|
|
51
|
+
const scale = 1;
|
|
52
|
+
const steps = config.steps || [];
|
|
53
|
+
if (steps.length === 0) throw new Error("Config has no steps");
|
|
54
|
+
|
|
55
|
+
const outDir = path.resolve(values.out || config.out || "demo");
|
|
56
|
+
const rawDir = path.join(outDir, "raw");
|
|
57
|
+
if (!preview) {
|
|
58
|
+
await mkdir(rawDir, { recursive: true });
|
|
59
|
+
await rm(rawDir, { recursive: true, force: true });
|
|
60
|
+
await mkdir(rawDir, { recursive: true });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let storageState = values["storage-state"];
|
|
64
|
+
if (values.login) {
|
|
65
|
+
storageState = await loginAndSave(url, {
|
|
66
|
+
proxy: values.proxy,
|
|
67
|
+
viewport,
|
|
68
|
+
statePath: path.resolve(values["save-state"] || "auth.json"),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const browser = await launchBrowser({
|
|
73
|
+
headless: preview ? values.headless : !values.headed,
|
|
74
|
+
proxy: values.proxy,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
let videoPath = null;
|
|
78
|
+
let events = [];
|
|
79
|
+
let videoOffset = 0;
|
|
80
|
+
let timelineDuration = 0;
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
const videoStartedAt = Date.now();
|
|
84
|
+
const context = await newContext(browser, {
|
|
85
|
+
viewport,
|
|
86
|
+
videoDir: preview ? undefined : rawDir,
|
|
87
|
+
storageState,
|
|
88
|
+
});
|
|
89
|
+
const page = await context.newPage();
|
|
90
|
+
|
|
91
|
+
await installCursor(page);
|
|
92
|
+
|
|
93
|
+
events = [];
|
|
94
|
+
|
|
95
|
+
await navigate(page, url);
|
|
96
|
+
await page.waitForLoadState("networkidle", { timeout: 5000 }).catch(() => {});
|
|
97
|
+
await page.waitForTimeout(Number(config.initialWait ?? 700));
|
|
98
|
+
|
|
99
|
+
const startedAt = Date.now();
|
|
100
|
+
videoOffset = Math.max(0, (startedAt - videoStartedAt) / 1000);
|
|
101
|
+
|
|
102
|
+
for (const [index, step] of steps.entries()) {
|
|
103
|
+
if (preview) reporter.log(`Step ${index + 1}/${steps.length}: ${step.action}`);
|
|
104
|
+
const t = Date.now() - startedAt;
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
switch (step.action) {
|
|
108
|
+
case "wait": {
|
|
109
|
+
await page.waitForTimeout(Number(step.ms || 500));
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
case "goto": {
|
|
114
|
+
await navigate(page, step.url || url);
|
|
115
|
+
await page.waitForLoadState("networkidle", { timeout: 15000 }).catch(() => {});
|
|
116
|
+
await page.waitForTimeout(Number(step.pause || 500));
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
case "click": {
|
|
121
|
+
const locator = await resolveLocator(page, step);
|
|
122
|
+
const point = await moveAndClick(page, locator, 0, Number(step.timeout ?? 20000));
|
|
123
|
+
events.push({
|
|
124
|
+
t,
|
|
125
|
+
tEnd: Date.now() - startedAt,
|
|
126
|
+
zoom: step.zoom !== false,
|
|
127
|
+
action: "click",
|
|
128
|
+
x: point.x,
|
|
129
|
+
y: point.y,
|
|
130
|
+
caption: step.caption || "",
|
|
131
|
+
label: step.text || step.name || step.selector || "",
|
|
132
|
+
});
|
|
133
|
+
await page.waitForTimeout(Number(step.pause ?? 700));
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
case "type": {
|
|
138
|
+
const locator = await resolveLocator(page, step);
|
|
139
|
+
const value = String(step.value ?? step.text ?? "");
|
|
140
|
+
const point = await clickAndType(page, locator, value, 0);
|
|
141
|
+
events.push({
|
|
142
|
+
t,
|
|
143
|
+
tEnd: Date.now() - startedAt,
|
|
144
|
+
zoom: step.zoom !== false,
|
|
145
|
+
action: "type",
|
|
146
|
+
x: point.x,
|
|
147
|
+
y: point.y,
|
|
148
|
+
caption: step.caption || "",
|
|
149
|
+
label: value,
|
|
150
|
+
});
|
|
151
|
+
await page.waitForTimeout(Number(step.pause ?? 400));
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
case "hover": {
|
|
156
|
+
const locator = await resolveLocator(page, step);
|
|
157
|
+
const point = await hover(page, locator, 0);
|
|
158
|
+
events.push({
|
|
159
|
+
t,
|
|
160
|
+
tEnd: Date.now() - startedAt,
|
|
161
|
+
zoom: step.zoom !== false,
|
|
162
|
+
action: "hover",
|
|
163
|
+
x: point.x,
|
|
164
|
+
y: point.y,
|
|
165
|
+
caption: step.caption || "",
|
|
166
|
+
label: step.text || step.selector || "",
|
|
167
|
+
});
|
|
168
|
+
await page.waitForTimeout(Number(step.pause ?? 500));
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
case "focus": {
|
|
173
|
+
const locator = await resolveLocator(page, step);
|
|
174
|
+
await locator.scrollIntoViewIfNeeded({ timeout: Number(step.timeout ?? 20000) });
|
|
175
|
+
const box = await locator.boundingBox();
|
|
176
|
+
if (!box || box.width <= 0 || box.height <= 0) {
|
|
177
|
+
throw commandError("TARGET_NOT_FOUND", "The focus target has no visible area.");
|
|
178
|
+
}
|
|
179
|
+
const focusedAt = Date.now() - startedAt;
|
|
180
|
+
events.push({
|
|
181
|
+
t: focusedAt, tEnd: focusedAt, action: "focus",
|
|
182
|
+
x: box.x + box.width / 2, y: box.y + box.height / 2,
|
|
183
|
+
zoom: step.zoom !== false, caption: step.caption || "",
|
|
184
|
+
label: step.text || step.name || step.selector || "",
|
|
185
|
+
});
|
|
186
|
+
await page.waitForTimeout(Number(step.pause ?? 700));
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
case "drag": {
|
|
191
|
+
const locator = await resolveLocator(page, step);
|
|
192
|
+
const point = await dragBy(
|
|
193
|
+
page,
|
|
194
|
+
locator,
|
|
195
|
+
Number(step.dx || 0),
|
|
196
|
+
Number(step.dy || 0),
|
|
197
|
+
Number(step.steps || 24),
|
|
198
|
+
0,
|
|
199
|
+
);
|
|
200
|
+
events.push({
|
|
201
|
+
t,
|
|
202
|
+
tEnd: Date.now() - startedAt,
|
|
203
|
+
zoom: step.zoom !== false,
|
|
204
|
+
action: "drag",
|
|
205
|
+
x: point.x,
|
|
206
|
+
y: point.y,
|
|
207
|
+
caption: step.caption || "",
|
|
208
|
+
label: step.selector || "",
|
|
209
|
+
});
|
|
210
|
+
await page.waitForTimeout(Number(step.pause ?? 600));
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
case "scroll": {
|
|
215
|
+
const y = Number(step.y || 600);
|
|
216
|
+
await page.evaluate((dy) => {
|
|
217
|
+
window.scrollBy({ top: dy, behavior: "smooth" });
|
|
218
|
+
}, y);
|
|
219
|
+
events.push({
|
|
220
|
+
t,
|
|
221
|
+
tEnd: Date.now() - startedAt,
|
|
222
|
+
zoom: step.zoom !== false,
|
|
223
|
+
action: "scroll",
|
|
224
|
+
x: viewport.width / 2,
|
|
225
|
+
y: viewport.height / 2,
|
|
226
|
+
caption: step.caption || "",
|
|
227
|
+
});
|
|
228
|
+
await page.waitForTimeout(Number(step.pause ?? 700));
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
case "press": {
|
|
233
|
+
await page.keyboard.press(step.key || "Enter");
|
|
234
|
+
events.push({
|
|
235
|
+
t,
|
|
236
|
+
tEnd: Date.now() - startedAt,
|
|
237
|
+
zoom: step.zoom !== false,
|
|
238
|
+
action: "press",
|
|
239
|
+
x: viewport.width / 2,
|
|
240
|
+
y: viewport.height / 2,
|
|
241
|
+
caption: step.caption || "",
|
|
242
|
+
});
|
|
243
|
+
await page.waitForTimeout(Number(step.pause ?? 500));
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
case "waitFor": {
|
|
248
|
+
if (step.url) {
|
|
249
|
+
await page.waitForURL(step.url, { timeout: Number(step.timeout || 20000) });
|
|
250
|
+
} else if (step.selector) {
|
|
251
|
+
await page.waitForSelector(step.selector, {
|
|
252
|
+
state: "visible",
|
|
253
|
+
timeout: Number(step.timeout || 20000),
|
|
254
|
+
});
|
|
255
|
+
} else if (step.text) {
|
|
256
|
+
await page.getByText(step.text, { exact: false }).first().waitFor({
|
|
257
|
+
state: "visible",
|
|
258
|
+
timeout: Number(step.timeout || 20000),
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
await page.waitForTimeout(Number(step.pause ?? 300));
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
case "screenshot": {
|
|
266
|
+
const file = path.join(outDir, step.file || `shot-${Date.now()}.png`);
|
|
267
|
+
await mkdir(path.dirname(file), { recursive: true });
|
|
268
|
+
await page.screenshot({ path: file });
|
|
269
|
+
screenshots.push({ kind: "screenshot", path: file });
|
|
270
|
+
reporter.log(`Screenshot: ${file}`);
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
default:
|
|
275
|
+
throw new Error(`Unknown step action: ${step.action}`);
|
|
276
|
+
}
|
|
277
|
+
} catch (error) {
|
|
278
|
+
const navigationError = ["PAGE_UNREACHABLE", "PAGE_HTTP_ERROR"].includes(error.code);
|
|
279
|
+
throw commandError(navigationError ? error.code : error.code === "TARGET_NOT_FOUND" ? "TARGET_NOT_FOUND" :
|
|
280
|
+
error.name === "TimeoutError" ? "STEP_TIMEOUT" : "STEP_FAILED",
|
|
281
|
+
navigationError ? error.message : `Could not complete step ${index + 1} (${step.action}).`, {
|
|
282
|
+
step: { index, action: step.action, path: `/steps/${index}` },
|
|
283
|
+
...(navigationError && error.status ? { status: error.status } : {}),
|
|
284
|
+
hint: navigationError ? error.hint : "Re-run plan with the same login state, check this step's target or wait condition, then retry record.",
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
await page.waitForTimeout(Number(config.tail ?? 1200));
|
|
290
|
+
timelineDuration = (Date.now() - startedAt) / 1000;
|
|
291
|
+
|
|
292
|
+
const video = page.video();
|
|
293
|
+
await page.close();
|
|
294
|
+
await context.close();
|
|
295
|
+
|
|
296
|
+
videoPath = video ? await video.path() : null;
|
|
297
|
+
if (!preview && !videoPath) throw new Error("Playwright did not produce a video");
|
|
298
|
+
} finally {
|
|
299
|
+
await browser.close();
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (preview) {
|
|
303
|
+
reporter.log(`Preview completed ${steps.length} step(s).`);
|
|
304
|
+
await reporter.success({ duration: timelineDuration, stepCount: steps.length, artifacts: screenshots });
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const recordingPath = path.join(outDir, "recording.webm");
|
|
309
|
+
await copyFile(videoPath, recordingPath);
|
|
310
|
+
|
|
311
|
+
const recordPath = path.join(outDir, "record.json");
|
|
312
|
+
const record = {
|
|
313
|
+
config,
|
|
314
|
+
url,
|
|
315
|
+
viewport,
|
|
316
|
+
scale,
|
|
317
|
+
video: path.basename(recordingPath),
|
|
318
|
+
videoOffset,
|
|
319
|
+
duration: timelineDuration,
|
|
320
|
+
events,
|
|
321
|
+
recordedAt: new Date().toISOString(),
|
|
322
|
+
};
|
|
323
|
+
await writeFile(recordPath, JSON.stringify(record, null, 2), "utf8");
|
|
324
|
+
|
|
325
|
+
reporter.log(`\nRecorded ${events.length} interaction(s).`);
|
|
326
|
+
reporter.log(`Video: ${recordingPath}`);
|
|
327
|
+
reporter.log(`Record: ${recordPath}`);
|
|
328
|
+
reporter.log(`\nNext: tabretake render --config ${path.relative(process.cwd(), recordPath)}`);
|
|
329
|
+
reporter.log(` (or pass the original --config path)\n`);
|
|
330
|
+
await reporter.success({ duration: timelineDuration, eventCount: events.length, artifacts: [
|
|
331
|
+
{ kind: "video", path: recordingPath }, { kind: "timeline", path: recordPath }, ...screenshots,
|
|
332
|
+
] });
|
|
333
|
+
|
|
334
|
+
return record;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async function loginAndSave(url, { proxy, viewport, statePath }) {
|
|
338
|
+
const browser = await launchBrowser({ headless: false, proxy });
|
|
339
|
+
try {
|
|
340
|
+
const context = await newContext(browser, { viewport });
|
|
341
|
+
const page = await context.newPage();
|
|
342
|
+
await navigate(page, url, { allowAuthErrors: true });
|
|
343
|
+
console.log("\nA browser window is open. Log in, then come back here.");
|
|
344
|
+
await waitForEnter("Press Enter when you are logged in... ");
|
|
345
|
+
await context.storageState({ path: statePath });
|
|
346
|
+
console.log(`Saved login state to ${statePath}\n`);
|
|
347
|
+
await context.close();
|
|
348
|
+
return statePath;
|
|
349
|
+
} finally {
|
|
350
|
+
await browser.close();
|
|
351
|
+
}
|
|
352
|
+
}
|