@open-document/core 0.14.0 → 0.14.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/dist/{build-CyBlmrjS.js → build-I0cgoERP.js} +1 -1
- package/dist/{check-DUKDEem-.js → check-Dys96QSL.js} +3 -3
- package/dist/cli/bin.js +9 -9
- package/dist/{context-BPApOW9n.js → context-C-osJW5f.js} +1 -1
- package/dist/{dev-DrnOvsX3.js → dev-CC4PrIOC.js} +1 -1
- package/dist/{diff-BPMD2Knh.js → diff-DhutD4TJ.js} +2 -2
- package/dist/{export-DeVtL8aX.js → export-KrajVJO7.js} +3 -3
- package/dist/{import-WAgWsuOv.js → import-Yv34za3L.js} +3 -3
- package/dist/ops/index.js +2 -2
- package/dist/{ops-raK6424A.js → ops-Dux5HlkH.js} +2 -422
- package/dist/{preview-B8Vw9QCh.js → preview-7eqn9Wzf.js} +1 -1
- package/dist/{config-cNpYUpF6.js → session-BSbrC5gK.js} +510 -4
- package/dist/{templates-D9jAFJ8R.js → templates-s-VLTSRc.js} +3 -3
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/app/components/agent/agent-panel.tsx +6 -1
- package/src/app/components/agent/transcript.tsx +111 -3
- package/src/app/lib/i18n-ja/agent-panel.ts +8 -0
- package/src/app/lib/i18n-ko/agent-panel.ts +8 -0
- package/src/app/lib/i18n-zh-cn/agent-panel.ts +8 -0
- package/src/app/lib/i18n-zh-tw/agent-panel.ts +8 -0
- package/src/app/lib/use-locale.ts +7 -0
|
@@ -9,11 +9,12 @@ import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
9
9
|
import fs from "node:fs/promises";
|
|
10
10
|
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
11
11
|
import fg from "fast-glob";
|
|
12
|
-
import { normalizePath } from "vite";
|
|
12
|
+
import { createServer, mergeConfig, normalizePath } from "vite";
|
|
13
13
|
import { execFile, spawn } from "node:child_process";
|
|
14
14
|
import { promisify } from "node:util";
|
|
15
15
|
import { parse } from "@babel/parser";
|
|
16
16
|
import { createHash, randomBytes, randomUUID } from "node:crypto";
|
|
17
|
+
import { Buffer as Buffer$1 } from "node:buffer";
|
|
17
18
|
import tailwindcss from "@tailwindcss/vite";
|
|
18
19
|
import react from "@vitejs/plugin-react";
|
|
19
20
|
import os from "node:os";
|
|
@@ -990,6 +991,41 @@ async function revertChange(ctx, docId, opts) {
|
|
|
990
991
|
return { ok: true };
|
|
991
992
|
});
|
|
992
993
|
}
|
|
994
|
+
const CLOCK_SLACK_MS = 1e3;
|
|
995
|
+
/**
|
|
996
|
+
* Whether the dev server should run `open-doc check` for the turn that just
|
|
997
|
+
* ended: only for an agent that cannot start a browser itself, only when the
|
|
998
|
+
* turn ran to its end, and only when it touched the document's source — it
|
|
999
|
+
* used a tool, and the file changed after the message. The mtime alone is not
|
|
1000
|
+
* enough: a document copied or made from a template a moment earlier reads as
|
|
1001
|
+
* fresh to a turn whose agent never even started.
|
|
1002
|
+
*/
|
|
1003
|
+
function shouldCheckAfterTurn(opts) {
|
|
1004
|
+
const { session, userItemId, canOpenBrowser, entryMtimeMs } = opts;
|
|
1005
|
+
if (canOpenBrowser || !session.docId || entryMtimeMs === null) return false;
|
|
1006
|
+
const start = session.items.findIndex((i) => i.id === userItemId);
|
|
1007
|
+
if (start === -1) return false;
|
|
1008
|
+
const turn = session.items.slice(start + 1);
|
|
1009
|
+
const result = [...turn].reverse().find((i) => i.kind === "result");
|
|
1010
|
+
if (result?.kind === "result" && result.aborted) return false;
|
|
1011
|
+
if (!turn.some((i) => i.kind === "tool")) return false;
|
|
1012
|
+
const startedAt = Date.parse(session.items[start].ts);
|
|
1013
|
+
return Number.isFinite(startedAt) && entryMtimeMs >= startedAt - CLOCK_SLACK_MS;
|
|
1014
|
+
}
|
|
1015
|
+
/** A check report as the transcript keeps it: errors first, the rest counted. */
|
|
1016
|
+
function checkItemResult(report) {
|
|
1017
|
+
const findings = [...report.findings].sort((a, b) => Number(b.severity === "error") - Number(a.severity === "error") || a.page - b.page).slice(0, 20).map((f) => ({
|
|
1018
|
+
page: f.page,
|
|
1019
|
+
severity: f.severity,
|
|
1020
|
+
message: f.message,
|
|
1021
|
+
...f.loc ? { loc: f.loc } : {}
|
|
1022
|
+
}));
|
|
1023
|
+
return {
|
|
1024
|
+
pageCount: report.pageCount,
|
|
1025
|
+
findings,
|
|
1026
|
+
total: report.findings.length
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
993
1029
|
//#endregion
|
|
994
1030
|
//#region src/agent/install.ts
|
|
995
1031
|
const LOCKFILES = [
|
|
@@ -1117,7 +1153,7 @@ function systemPromptAppend(askInstruction, opts) {
|
|
|
1117
1153
|
`The workspace is an open-doc project: documents live under ${opts.docsDir}/<id>/index.tsx and the dev server hot-reloads every edit, so the user sees changes immediately.`,
|
|
1118
1154
|
"Use the project skills: create-doc to draft a new document, apply-comments to resolve @doc-comment markers left by the inspector, create-theme for themes, and doc-authoring as the technical reference before writing a page.",
|
|
1119
1155
|
SECTION_BY_SECTION,
|
|
1120
|
-
opts.canOpenBrowser === false ? "Your sandbox cannot start a browser, so `open-doc check` and the page preview fail here: do not run them.
|
|
1156
|
+
opts.canOpenBrowser === false ? "Your sandbox cannot start a browser, so `open-doc check` and the page preview fail here: do not run them. open-doc runs the check itself once your turn ends and shows the user what it finds, so keep the file compiling after every write." : `After editing a document, run \`pnpm exec open-doc check <id>\` and fix what it reports. To look at one page as it prints, open ${preview} and wait for [data-od-preview-ready="true"].`,
|
|
1121
1157
|
"Keep replies short and concrete: the panel is narrow. Prefer doing the work over describing it, and finish with a one-line summary of what changed.",
|
|
1122
1158
|
askInstruction
|
|
1123
1159
|
].join(" ");
|
|
@@ -1619,6 +1655,11 @@ function closePending(item) {
|
|
|
1619
1655
|
...item,
|
|
1620
1656
|
status: "denied"
|
|
1621
1657
|
};
|
|
1658
|
+
if (item.kind === "check" && item.status === "running") return {
|
|
1659
|
+
...item,
|
|
1660
|
+
status: "failed",
|
|
1661
|
+
error: "interrupted"
|
|
1662
|
+
};
|
|
1622
1663
|
return item;
|
|
1623
1664
|
}
|
|
1624
1665
|
function createSessionStore(opts) {
|
|
@@ -3199,6 +3240,174 @@ async function importMarkdown(ctx, opts) {
|
|
|
3199
3240
|
};
|
|
3200
3241
|
}
|
|
3201
3242
|
//#endregion
|
|
3243
|
+
//#region src/ops/layout.ts
|
|
3244
|
+
/**
|
|
3245
|
+
* Chromium and a Vite server are expensive to start, and an agent checking its
|
|
3246
|
+
* work calls these back to back. One session is kept warm and closed once
|
|
3247
|
+
* nobody has asked for a while.
|
|
3248
|
+
*/
|
|
3249
|
+
const IDLE_MS = 12e4;
|
|
3250
|
+
let shared = null;
|
|
3251
|
+
let pending = null;
|
|
3252
|
+
let idleTimer = null;
|
|
3253
|
+
let leases = 0;
|
|
3254
|
+
function armIdleClose() {
|
|
3255
|
+
if (idleTimer) clearTimeout(idleTimer);
|
|
3256
|
+
idleTimer = setTimeout(() => {
|
|
3257
|
+
if (leases > 0) return;
|
|
3258
|
+
const current = shared;
|
|
3259
|
+
shared = null;
|
|
3260
|
+
idleTimer = null;
|
|
3261
|
+
current?.session.close().catch(() => {});
|
|
3262
|
+
}, IDLE_MS);
|
|
3263
|
+
idleTimer.unref?.();
|
|
3264
|
+
}
|
|
3265
|
+
async function acquire(ctx) {
|
|
3266
|
+
if (shared && shared.origin === ctx.serverOrigin) return shared.session;
|
|
3267
|
+
if (!pending) {
|
|
3268
|
+
const origin = ctx.serverOrigin;
|
|
3269
|
+
pending = createRenderSession({
|
|
3270
|
+
userCwd: ctx.userCwd,
|
|
3271
|
+
origin,
|
|
3272
|
+
deviceScaleFactor: 2
|
|
3273
|
+
}).then((session) => {
|
|
3274
|
+
shared = {
|
|
3275
|
+
session,
|
|
3276
|
+
origin
|
|
3277
|
+
};
|
|
3278
|
+
return session;
|
|
3279
|
+
}).finally(() => {
|
|
3280
|
+
pending = null;
|
|
3281
|
+
});
|
|
3282
|
+
}
|
|
3283
|
+
return pending;
|
|
3284
|
+
}
|
|
3285
|
+
async function withDoc(ctx, docId, fn) {
|
|
3286
|
+
if (!resolveEntry(ctx, docId)) throw new OpsError(404, `document not found: ${docId}`);
|
|
3287
|
+
let session;
|
|
3288
|
+
try {
|
|
3289
|
+
session = await acquire(ctx);
|
|
3290
|
+
} catch (err) {
|
|
3291
|
+
if (err instanceof RenderUnavailableError) throw new OpsError(501, err.message);
|
|
3292
|
+
throw new OpsError(500, err.message);
|
|
3293
|
+
}
|
|
3294
|
+
leases++;
|
|
3295
|
+
let renderer = null;
|
|
3296
|
+
try {
|
|
3297
|
+
renderer = await session.open(docId);
|
|
3298
|
+
return await fn(renderer);
|
|
3299
|
+
} catch (err) {
|
|
3300
|
+
if (err instanceof OpsError) throw err;
|
|
3301
|
+
throw new OpsError(422, err.message);
|
|
3302
|
+
} finally {
|
|
3303
|
+
await renderer?.close().catch(() => {});
|
|
3304
|
+
leases--;
|
|
3305
|
+
armIdleClose();
|
|
3306
|
+
}
|
|
3307
|
+
}
|
|
3308
|
+
/** Closes the warm browser. Call before the process exits so nothing lingers. */
|
|
3309
|
+
async function closeRenderSession() {
|
|
3310
|
+
if (idleTimer) clearTimeout(idleTimer);
|
|
3311
|
+
idleTimer = null;
|
|
3312
|
+
const current = shared;
|
|
3313
|
+
shared = null;
|
|
3314
|
+
await current?.session.close().catch(() => {});
|
|
3315
|
+
}
|
|
3316
|
+
/**
|
|
3317
|
+
* Renders the document headlessly at true page size and reports what is wrong
|
|
3318
|
+
* with the layout — the part of a document an agent writing React cannot see.
|
|
3319
|
+
*/
|
|
3320
|
+
async function checkLayout(ctx, docId) {
|
|
3321
|
+
return withDoc(ctx, docId, async (renderer) => {
|
|
3322
|
+
const report = await renderer.diagnose();
|
|
3323
|
+
return {
|
|
3324
|
+
docId: report.docId || docId,
|
|
3325
|
+
title: report.title,
|
|
3326
|
+
pageCount: report.pageCount,
|
|
3327
|
+
errors: report.findings.filter((f) => f.severity === "error").length,
|
|
3328
|
+
warnings: report.findings.filter((f) => f.severity === "warn").length,
|
|
3329
|
+
findings: report.findings
|
|
3330
|
+
};
|
|
3331
|
+
});
|
|
3332
|
+
}
|
|
3333
|
+
async function renderDocPage(ctx, docId, page) {
|
|
3334
|
+
return withDoc(ctx, docId, async (renderer) => {
|
|
3335
|
+
const bytes = await renderer.screenshot(page);
|
|
3336
|
+
return {
|
|
3337
|
+
docId,
|
|
3338
|
+
page,
|
|
3339
|
+
pageCount: renderer.status.pageCount,
|
|
3340
|
+
mimeType: "image/png",
|
|
3341
|
+
base64: Buffer$1.from(bytes).toString("base64")
|
|
3342
|
+
};
|
|
3343
|
+
});
|
|
3344
|
+
}
|
|
3345
|
+
function resolveOutDir(ctx, outDir) {
|
|
3346
|
+
const resolved = path.resolve(ctx.userCwd, outDir);
|
|
3347
|
+
if (resolved !== ctx.userCwd && !resolved.startsWith(ctx.userCwd + path.sep)) throw new OpsError(400, `outDir must stay inside the workspace: ${outDir}`);
|
|
3348
|
+
return resolved;
|
|
3349
|
+
}
|
|
3350
|
+
/**
|
|
3351
|
+
* Writes the document's files. With `each`, a document that exports
|
|
3352
|
+
* `records` is laid out and written once per row — `name` (or
|
|
3353
|
+
* `meta.recordName`) names each row's files.
|
|
3354
|
+
*/
|
|
3355
|
+
async function exportDocument(ctx, docId, opts = {}) {
|
|
3356
|
+
const format = opts.format ?? "pdf";
|
|
3357
|
+
const dir = resolveOutDir(ctx, opts.outDir ?? "out");
|
|
3358
|
+
return withDoc(ctx, docId, async (renderer) => {
|
|
3359
|
+
await fs.mkdir(dir, { recursive: true });
|
|
3360
|
+
const written = [];
|
|
3361
|
+
const taken = /* @__PURE__ */ new Set();
|
|
3362
|
+
const write = async (name, bytes) => {
|
|
3363
|
+
const file = path.join(dir, name);
|
|
3364
|
+
await fs.writeFile(file, bytes);
|
|
3365
|
+
written.push(path.relative(ctx.userCwd, file));
|
|
3366
|
+
};
|
|
3367
|
+
const free = (base) => {
|
|
3368
|
+
let name = base;
|
|
3369
|
+
for (let n = 2; taken.has(name); n++) name = `${base}-${n}`;
|
|
3370
|
+
taken.add(name);
|
|
3371
|
+
return name;
|
|
3372
|
+
};
|
|
3373
|
+
const writeCopy = async (base) => {
|
|
3374
|
+
if (format === "pdf") await write(`${base}.pdf`, await renderer.pdf());
|
|
3375
|
+
else if (format === "html" || format === "docx") {
|
|
3376
|
+
const bundle = format === "html" ? await renderer.html() : await renderer.docx();
|
|
3377
|
+
if (!bundle) throw new OpsError(422, `document has no pages: ${docId}`);
|
|
3378
|
+
await write(`${base}${path.extname(bundle.filename)}`, Buffer$1.from(bundle.base64, "base64"));
|
|
3379
|
+
} else {
|
|
3380
|
+
const width = String(renderer.status.pageCount).length;
|
|
3381
|
+
for (let page = 1; page <= renderer.status.pageCount; page++) await write(`${base}-${String(page).padStart(width, "0")}.png`, await renderer.screenshot(page));
|
|
3382
|
+
}
|
|
3383
|
+
};
|
|
3384
|
+
if (!opts.each) {
|
|
3385
|
+
await writeCopy(docId);
|
|
3386
|
+
return {
|
|
3387
|
+
docId,
|
|
3388
|
+
format,
|
|
3389
|
+
pageCount: renderer.status.pageCount,
|
|
3390
|
+
files: written
|
|
3391
|
+
};
|
|
3392
|
+
}
|
|
3393
|
+
const records = renderer.status.records;
|
|
3394
|
+
if (records === 0) throw new OpsError(422, `${docId} has no records — export \`records\` (rows from a .csv) to write one copy per row`);
|
|
3395
|
+
let pageCount = 0;
|
|
3396
|
+
for (let index = 0; index < records; index++) {
|
|
3397
|
+
const name = await renderer.setRecord(index, opts.name);
|
|
3398
|
+
pageCount += renderer.status.pageCount;
|
|
3399
|
+
await writeCopy(free(name));
|
|
3400
|
+
}
|
|
3401
|
+
return {
|
|
3402
|
+
docId,
|
|
3403
|
+
format,
|
|
3404
|
+
pageCount,
|
|
3405
|
+
files: written,
|
|
3406
|
+
records
|
|
3407
|
+
};
|
|
3408
|
+
});
|
|
3409
|
+
}
|
|
3410
|
+
//#endregion
|
|
3202
3411
|
//#region src/vite/themes-plugin.ts
|
|
3203
3412
|
const THEMES_VMOD = "virtual:open-doc/themes";
|
|
3204
3413
|
function resolved(id) {
|
|
@@ -3698,6 +3907,49 @@ async function renamePlaceholderDoc(server, ctx, store, broadcast, turn) {
|
|
|
3698
3907
|
to: renamed.id
|
|
3699
3908
|
});
|
|
3700
3909
|
}
|
|
3910
|
+
async function checkAfterTurn(server, ctx, store, providers, turn) {
|
|
3911
|
+
const session = store.get(turn.sessionId);
|
|
3912
|
+
const docId = session?.docId;
|
|
3913
|
+
if (!session || !docId) return;
|
|
3914
|
+
if (store.list().some((s) => s.docId === docId && s.status === "running")) return;
|
|
3915
|
+
const entry = resolveEntry(ctx, docId);
|
|
3916
|
+
const entryMtimeMs = entry ? (await fs.stat(entry).catch(() => null))?.mtimeMs ?? null : null;
|
|
3917
|
+
if (!shouldCheckAfterTurn({
|
|
3918
|
+
session,
|
|
3919
|
+
userItemId: turn.userItemId,
|
|
3920
|
+
canOpenBrowser: providers[session.provider]?.canOpenBrowser ?? true,
|
|
3921
|
+
entryMtimeMs
|
|
3922
|
+
})) return;
|
|
3923
|
+
const item = store.appendItem(session.id, {
|
|
3924
|
+
kind: "check",
|
|
3925
|
+
id: newId("c"),
|
|
3926
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3927
|
+
docId,
|
|
3928
|
+
status: "running",
|
|
3929
|
+
pageCount: null,
|
|
3930
|
+
findings: [],
|
|
3931
|
+
total: 0,
|
|
3932
|
+
error: null
|
|
3933
|
+
});
|
|
3934
|
+
const origin = server.resolvedUrls?.local?.[0]?.replace(/\/$/, "");
|
|
3935
|
+
try {
|
|
3936
|
+
const report = await checkLayout({
|
|
3937
|
+
...ctx,
|
|
3938
|
+
...origin ? { serverOrigin: origin } : {}
|
|
3939
|
+
}, docId);
|
|
3940
|
+
if (!store.get(session.id)) return;
|
|
3941
|
+
store.updateItem(session.id, item.id, "check", {
|
|
3942
|
+
status: "done",
|
|
3943
|
+
...checkItemResult(report)
|
|
3944
|
+
});
|
|
3945
|
+
} catch (err) {
|
|
3946
|
+
if (!store.get(session.id)) return;
|
|
3947
|
+
store.updateItem(session.id, item.id, "check", {
|
|
3948
|
+
status: "failed",
|
|
3949
|
+
error: err instanceof Error ? err.message : String(err)
|
|
3950
|
+
});
|
|
3951
|
+
}
|
|
3952
|
+
}
|
|
3701
3953
|
function parseRespond(raw) {
|
|
3702
3954
|
if (!raw || typeof raw !== "object") return null;
|
|
3703
3955
|
const r = raw;
|
|
@@ -3744,7 +3996,10 @@ function registerAgentRoutes(server, ctx) {
|
|
|
3744
3996
|
},
|
|
3745
3997
|
store,
|
|
3746
3998
|
providers,
|
|
3747
|
-
onTurnEnd: (turn) =>
|
|
3999
|
+
onTurnEnd: async (turn) => {
|
|
4000
|
+
await renamePlaceholderDoc(server, ctx, store, broadcast, turn);
|
|
4001
|
+
await checkAfterTurn(server, ctx, store, providers, turn);
|
|
4002
|
+
}
|
|
3748
4003
|
});
|
|
3749
4004
|
store.subscribe(broadcast);
|
|
3750
4005
|
server.httpServer?.on("close", () => {
|
|
@@ -6782,4 +7037,255 @@ async function createViteConfig(opts) {
|
|
|
6782
7037
|
};
|
|
6783
7038
|
}
|
|
6784
7039
|
//#endregion
|
|
6785
|
-
|
|
7040
|
+
//#region src/render/session.ts
|
|
7041
|
+
var RenderUnavailableError = class extends Error {
|
|
7042
|
+
constructor(message) {
|
|
7043
|
+
super(message);
|
|
7044
|
+
this.name = "RenderUnavailableError";
|
|
7045
|
+
}
|
|
7046
|
+
};
|
|
7047
|
+
const INSTALL_HINT = "Headless rendering needs Playwright. Install it in this workspace:\n pnpm add -D playwright && pnpm exec playwright install chromium";
|
|
7048
|
+
async function loadPlaywright() {
|
|
7049
|
+
try {
|
|
7050
|
+
return await import("playwright");
|
|
7051
|
+
} catch {
|
|
7052
|
+
throw new RenderUnavailableError(INSTALL_HINT);
|
|
7053
|
+
}
|
|
7054
|
+
}
|
|
7055
|
+
const DEFAULT_TIMEOUT = 6e4;
|
|
7056
|
+
const PRINT_PAGE_SELECTOR = "#od-print-root .od-print-page";
|
|
7057
|
+
async function bootServer(userCwd) {
|
|
7058
|
+
const base = await createViteConfig({
|
|
7059
|
+
userCwd,
|
|
7060
|
+
headless: true
|
|
7061
|
+
});
|
|
7062
|
+
const config = mergeConfig(base, {
|
|
7063
|
+
logLevel: "silent",
|
|
7064
|
+
server: {
|
|
7065
|
+
port: 0,
|
|
7066
|
+
strictPort: false,
|
|
7067
|
+
open: false
|
|
7068
|
+
}
|
|
7069
|
+
});
|
|
7070
|
+
const server = await createServer(config);
|
|
7071
|
+
await server.listen();
|
|
7072
|
+
const origin = server.resolvedUrls?.local?.[0]?.replace(/\/$/, "");
|
|
7073
|
+
if (!origin) {
|
|
7074
|
+
await server.close();
|
|
7075
|
+
throw new Error("Could not determine the dev server URL");
|
|
7076
|
+
}
|
|
7077
|
+
return {
|
|
7078
|
+
server,
|
|
7079
|
+
origin
|
|
7080
|
+
};
|
|
7081
|
+
}
|
|
7082
|
+
async function createRenderSession(opts) {
|
|
7083
|
+
const { chromium } = await loadPlaywright();
|
|
7084
|
+
const timeout = opts.timeoutMs ?? DEFAULT_TIMEOUT;
|
|
7085
|
+
let server = null;
|
|
7086
|
+
let origin = opts.origin?.replace(/\/$/, "") ?? "";
|
|
7087
|
+
if (!origin) {
|
|
7088
|
+
const booted = await bootServer(opts.userCwd);
|
|
7089
|
+
server = booted.server;
|
|
7090
|
+
origin = booted.origin;
|
|
7091
|
+
}
|
|
7092
|
+
let browser;
|
|
7093
|
+
try {
|
|
7094
|
+
browser = await chromium.launch({ headless: true });
|
|
7095
|
+
} catch (err) {
|
|
7096
|
+
await server?.close();
|
|
7097
|
+
throw new RenderUnavailableError(`${INSTALL_HINT}\n\nChromium failed to launch: ${err.message}`);
|
|
7098
|
+
}
|
|
7099
|
+
return {
|
|
7100
|
+
origin,
|
|
7101
|
+
async open(docId) {
|
|
7102
|
+
const page = await browser.newPage({
|
|
7103
|
+
viewport: {
|
|
7104
|
+
width: 1280,
|
|
7105
|
+
height: 1024
|
|
7106
|
+
},
|
|
7107
|
+
...opts.deviceScaleFactor !== void 0 ? { deviceScaleFactor: opts.deviceScaleFactor } : {}
|
|
7108
|
+
});
|
|
7109
|
+
const errors = [];
|
|
7110
|
+
page.on("pageerror", (err) => errors.push(String(err?.message ?? err)));
|
|
7111
|
+
await page.goto(`${origin}/d/${encodeURIComponent(docId)}`, {
|
|
7112
|
+
waitUntil: "load",
|
|
7113
|
+
timeout
|
|
7114
|
+
});
|
|
7115
|
+
try {
|
|
7116
|
+
await page.waitForFunction("globalThis.__openDoc ? globalThis.__openDoc.status().ready : false", void 0, { timeout });
|
|
7117
|
+
} catch {
|
|
7118
|
+
await page.close();
|
|
7119
|
+
const detail = errors.length ? `\n${errors.join("\n")}` : "";
|
|
7120
|
+
throw new Error(`Document "${docId}" never finished rendering.${detail}`);
|
|
7121
|
+
}
|
|
7122
|
+
const status = await page.evaluate("globalThis.__openDoc.status()");
|
|
7123
|
+
const renderer = {
|
|
7124
|
+
status,
|
|
7125
|
+
diagnose: () => page.evaluate("globalThis.__openDoc.diagnose()"),
|
|
7126
|
+
async pdf() {
|
|
7127
|
+
await page.evaluate("globalThis.__openDoc.preparePrint()");
|
|
7128
|
+
try {
|
|
7129
|
+
return await page.pdf({
|
|
7130
|
+
printBackground: true,
|
|
7131
|
+
preferCSSPageSize: true,
|
|
7132
|
+
outline: true,
|
|
7133
|
+
tagged: true
|
|
7134
|
+
});
|
|
7135
|
+
} finally {
|
|
7136
|
+
await page.evaluate("globalThis.__openDoc.releasePrint()");
|
|
7137
|
+
}
|
|
7138
|
+
},
|
|
7139
|
+
async screenshot(pageNumber) {
|
|
7140
|
+
if (pageNumber < 1 || pageNumber > status.pageCount) throw new Error(`page ${pageNumber} is out of range — "${docId}" has ${status.pageCount}`);
|
|
7141
|
+
await page.evaluate("globalThis.__openDoc.preparePrint()");
|
|
7142
|
+
await page.emulateMedia({ media: "print" });
|
|
7143
|
+
try {
|
|
7144
|
+
const sheet = await page.$(`${PRINT_PAGE_SELECTOR}:nth-child(${pageNumber})`);
|
|
7145
|
+
if (!sheet) throw new Error(`page ${pageNumber} did not render`);
|
|
7146
|
+
return await sheet.screenshot({ type: "png" });
|
|
7147
|
+
} finally {
|
|
7148
|
+
await page.emulateMedia({ media: null });
|
|
7149
|
+
await page.evaluate("globalThis.__openDoc.releasePrint()");
|
|
7150
|
+
}
|
|
7151
|
+
},
|
|
7152
|
+
async sheets() {
|
|
7153
|
+
await page.evaluate("globalThis.__openDoc.preparePrint()");
|
|
7154
|
+
await page.emulateMedia({ media: "print" });
|
|
7155
|
+
try {
|
|
7156
|
+
const out = [];
|
|
7157
|
+
for (let n = 1; n <= status.pageCount; n++) {
|
|
7158
|
+
const selector = `${PRINT_PAGE_SELECTOR}:nth-child(${n})`;
|
|
7159
|
+
const sheet = await page.$(selector);
|
|
7160
|
+
if (!sheet) throw new Error(`page ${n} did not render`);
|
|
7161
|
+
const png = await sheet.screenshot({ type: "png" });
|
|
7162
|
+
const text = await page.evaluate(`document.querySelector(${JSON.stringify(selector)})?.innerText ?? ''`);
|
|
7163
|
+
out.push({
|
|
7164
|
+
png,
|
|
7165
|
+
text
|
|
7166
|
+
});
|
|
7167
|
+
}
|
|
7168
|
+
return out;
|
|
7169
|
+
} finally {
|
|
7170
|
+
await page.emulateMedia({ media: null });
|
|
7171
|
+
await page.evaluate("globalThis.__openDoc.releasePrint()");
|
|
7172
|
+
}
|
|
7173
|
+
},
|
|
7174
|
+
html: () => page.evaluate("globalThis.__openDoc.htmlBundle()"),
|
|
7175
|
+
docx: () => page.evaluate("globalThis.__openDoc.docxBundle()"),
|
|
7176
|
+
async setRecord(index, pattern) {
|
|
7177
|
+
const { name } = await page.evaluate(`globalThis.__openDoc.setRecord(${JSON.stringify(index)}, ${pattern === void 0 ? "undefined" : JSON.stringify(pattern)})`);
|
|
7178
|
+
await page.waitForFunction(`(() => { const s = globalThis.__openDoc.status(); return s.ready && s.record === ${JSON.stringify(index)}; })()`, void 0, { timeout });
|
|
7179
|
+
renderer.status = await page.evaluate("globalThis.__openDoc.status()");
|
|
7180
|
+
return name;
|
|
7181
|
+
},
|
|
7182
|
+
close: () => page.close()
|
|
7183
|
+
};
|
|
7184
|
+
return renderer;
|
|
7185
|
+
},
|
|
7186
|
+
async compareImages(before, after) {
|
|
7187
|
+
const page = await browser.newPage();
|
|
7188
|
+
try {
|
|
7189
|
+
const toUrl = (png) => `data:image/png;base64,${Buffer$1.from(png).toString("base64")}`;
|
|
7190
|
+
return await page.evaluate(compareInBrowser, {
|
|
7191
|
+
a: toUrl(before),
|
|
7192
|
+
b: toUrl(after),
|
|
7193
|
+
cell: DIFF_CELL
|
|
7194
|
+
});
|
|
7195
|
+
} finally {
|
|
7196
|
+
await page.close();
|
|
7197
|
+
}
|
|
7198
|
+
},
|
|
7199
|
+
async close() {
|
|
7200
|
+
await browser.close();
|
|
7201
|
+
await server?.close();
|
|
7202
|
+
}
|
|
7203
|
+
};
|
|
7204
|
+
}
|
|
7205
|
+
/** Changed pixels are gathered into cells this many px square before boxing. */
|
|
7206
|
+
const DIFF_CELL = 12;
|
|
7207
|
+
/**
|
|
7208
|
+
* Runs in the page. The browser decodes both PNGs, so nothing on the Node side
|
|
7209
|
+
* has to: draw each onto white, mark every cell where a pixel moved by more
|
|
7210
|
+
* than antialiasing does, and box the clusters of marked cells. Clusters
|
|
7211
|
+
* within two cells of each other join, so one edited paragraph is one box.
|
|
7212
|
+
*/
|
|
7213
|
+
async function compareInBrowser({ a, b, cell }) {
|
|
7214
|
+
const load = async (src) => {
|
|
7215
|
+
const image = new Image();
|
|
7216
|
+
image.src = src;
|
|
7217
|
+
await image.decode();
|
|
7218
|
+
return image;
|
|
7219
|
+
};
|
|
7220
|
+
const [x, y] = await Promise.all([load(a), load(b)]);
|
|
7221
|
+
const width = Math.max(x.naturalWidth, y.naturalWidth);
|
|
7222
|
+
const height = Math.max(x.naturalHeight, y.naturalHeight);
|
|
7223
|
+
const pixels = (image) => {
|
|
7224
|
+
const canvas = document.createElement("canvas");
|
|
7225
|
+
canvas.width = width;
|
|
7226
|
+
canvas.height = height;
|
|
7227
|
+
const g = canvas.getContext("2d");
|
|
7228
|
+
if (!g) throw new Error("no 2D canvas");
|
|
7229
|
+
g.fillStyle = "#ffffff";
|
|
7230
|
+
g.fillRect(0, 0, width, height);
|
|
7231
|
+
g.drawImage(image, 0, 0);
|
|
7232
|
+
return g.getImageData(0, 0, width, height).data;
|
|
7233
|
+
};
|
|
7234
|
+
const p = pixels(x);
|
|
7235
|
+
const q = pixels(y);
|
|
7236
|
+
const cols = Math.ceil(width / cell);
|
|
7237
|
+
const rows = Math.ceil(height / cell);
|
|
7238
|
+
const hot = new Uint8Array(cols * rows);
|
|
7239
|
+
let changed = 0;
|
|
7240
|
+
for (let i = 0; i < p.length; i += 4) {
|
|
7241
|
+
if (Math.abs((p[i] ?? 0) - (q[i] ?? 0)) + Math.abs((p[i + 1] ?? 0) - (q[i + 1] ?? 0)) + Math.abs((p[i + 2] ?? 0) - (q[i + 2] ?? 0)) <= 48) continue;
|
|
7242
|
+
changed++;
|
|
7243
|
+
const px = i / 4 % width;
|
|
7244
|
+
const py = Math.floor(i / 4 / width);
|
|
7245
|
+
hot[Math.floor(py / cell) * cols + Math.floor(px / cell)] = 1;
|
|
7246
|
+
}
|
|
7247
|
+
const seen = new Uint8Array(cols * rows);
|
|
7248
|
+
const boxes = [];
|
|
7249
|
+
for (let start = 0; start < hot.length; start++) {
|
|
7250
|
+
if (!hot[start] || seen[start]) continue;
|
|
7251
|
+
let minC = cols;
|
|
7252
|
+
let maxC = 0;
|
|
7253
|
+
let minR = rows;
|
|
7254
|
+
let maxR = 0;
|
|
7255
|
+
const stack = [start];
|
|
7256
|
+
seen[start] = 1;
|
|
7257
|
+
while (stack.length > 0) {
|
|
7258
|
+
const at = stack.pop();
|
|
7259
|
+
const c = at % cols;
|
|
7260
|
+
const r = Math.floor(at / cols);
|
|
7261
|
+
minC = Math.min(minC, c);
|
|
7262
|
+
maxC = Math.max(maxC, c);
|
|
7263
|
+
minR = Math.min(minR, r);
|
|
7264
|
+
maxR = Math.max(maxR, r);
|
|
7265
|
+
for (let dr = -2; dr <= 2; dr++) for (let dc = -2; dc <= 2; dc++) {
|
|
7266
|
+
const nr = r + dr;
|
|
7267
|
+
const nc = c + dc;
|
|
7268
|
+
if (nr < 0 || nc < 0 || nr >= rows || nc >= cols) continue;
|
|
7269
|
+
const next = nr * cols + nc;
|
|
7270
|
+
if (hot[next] && !seen[next]) {
|
|
7271
|
+
seen[next] = 1;
|
|
7272
|
+
stack.push(next);
|
|
7273
|
+
}
|
|
7274
|
+
}
|
|
7275
|
+
}
|
|
7276
|
+
boxes.push({
|
|
7277
|
+
x: minC * cell,
|
|
7278
|
+
y: minR * cell,
|
|
7279
|
+
width: Math.min(width, (maxC + 1) * cell) - minC * cell,
|
|
7280
|
+
height: Math.min(height, (maxR + 1) * cell) - minR * cell
|
|
7281
|
+
});
|
|
7282
|
+
}
|
|
7283
|
+
return {
|
|
7284
|
+
width,
|
|
7285
|
+
height,
|
|
7286
|
+
changed: changed / (width * height),
|
|
7287
|
+
boxes
|
|
7288
|
+
};
|
|
7289
|
+
}
|
|
7290
|
+
//#endregion
|
|
7291
|
+
export { assertExpectedSource as A, readDocument as B, exportDocument as C, listRevisions as D, slugify as E, deleteDocument as F, resolveEntry as H, docDir as I, duplicateDocument as L, withFileLock as M, OpsError as N, readChanges as O, createDocument as P, listDocIds$1 as R, closeRenderSession as S, importMarkdown as T, writeDocument as U, renameDocument as V, makeContext as W, listFolders as _, readText as a, writeAsset as b, writeTexts as c, suggestDocId as d, createFolder as f, listAssets as g, findAssetUsages as h, addComment as i, hashSource as j, revertChange as k, createFromTemplate as l, fileDocument as m, createRenderSession as n, writeEdits as o, deleteAsset as p, createViteConfig as r, writeText as s, RenderUnavailableError as t, listTemplates as u, listThemes as v, renderDocPage as w, checkLayout as x, readTheme as y, listDocuments as z };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import "./ops-
|
|
3
|
-
import { t as cliContext } from "./context-
|
|
1
|
+
import { l as createFromTemplate, u as listTemplates } from "./session-BSbrC5gK.js";
|
|
2
|
+
import "./ops-Dux5HlkH.js";
|
|
3
|
+
import { t as cliContext } from "./context-C-osJW5f.js";
|
|
4
4
|
import chalk from "chalk";
|
|
5
5
|
//#region src/cli/templates.ts
|
|
6
6
|
/**
|
package/dist/vite/index.js
CHANGED
package/package.json
CHANGED
|
@@ -313,7 +313,12 @@ export function AgentPanel({
|
|
|
313
313
|
) : (
|
|
314
314
|
<>
|
|
315
315
|
{session && session.items.length > 0 ? (
|
|
316
|
-
<Transcript
|
|
316
|
+
<Transcript
|
|
317
|
+
session={session}
|
|
318
|
+
onRespond={onRespond}
|
|
319
|
+
onEdit={onEdit}
|
|
320
|
+
onFix={(text) => send(text)}
|
|
321
|
+
/>
|
|
317
322
|
) : isNewDoc ? (
|
|
318
323
|
<NewDocState providerName={providerName} />
|
|
319
324
|
) : (
|