fix: stabilize local folder AI document workflow

- scope local-folder PageTree revision and document sidebar rendering to fileTreeScope

- preserve projected table/image attrs for local Markdown aggregate fallback

- avoid FileTree restore forced layouts on cold design open

- add API ChatOnly provider runtime and local OCR task handling regressions
This commit is contained in:
lix-2026
2026-06-02 17:17:49 +08:00
parent 610b23d3a5
commit 9f4b5c4d48
27 changed files with 3825 additions and 229 deletions
+191 -106
View File
@@ -80,11 +80,17 @@ async function main() {
const relativePath = "docs/Page.md";
const documentId = localMdDocumentId(relativePath);
const sourceRootRelativePath = "docs/Page.assets/photo.png";
const failedSourceRootRelativePath = "docs/Page.assets/photo-failed.png";
const ocrToken = "TASK526_OCR_TOKEN";
writeWorkspaceManifest(root, actorId);
fs.mkdirSync(path.join(root, "docs", "Page.assets"), { recursive: true });
fs.writeFileSync(path.join(root, relativePath), "# OCR Page\n\n![photo](./Page.assets/photo.png)\n", "utf8");
fs.writeFileSync(path.join(root, sourceRootRelativePath), Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a]));
fs.writeFileSync(path.join(root, relativePath), "# OCR Page\n\n![photo](<./Page.assets/photo.png>)\n", "utf8");
const tinyPng = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAFgwJ/l8FQOQAAAABJRU5ErkJggg==",
"base64",
);
fs.writeFileSync(path.join(root, sourceRootRelativePath), tinyPng);
fs.writeFileSync(path.join(root, failedSourceRootRelativePath), tinyPng);
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext();
@@ -93,6 +99,15 @@ async function main() {
try {
await ensureAuthenticated(page, context.request);
await ensureDocumentVisible(page, root, relativePath);
await page.waitForFunction(() => {
const image = document.querySelector('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror img');
return image instanceof HTMLImageElement
&& image.complete
&& image.naturalWidth > 0
&& image.src.includes("Page.assets%2Fphoto.png")
&& !image.src.includes("%3C")
&& !image.src.includes("%3E");
}, null, { timeout: UI_TIMEOUT_MS });
screenshots.page = path.join(OUTPUT_DIR, "01-page.png");
await page.screenshot({ path: screenshots.page, fullPage: true });
@@ -126,17 +141,23 @@ async function main() {
sourceRootRelativePath,
workspaceId,
});
await page.locator('[data-testid="mnote-local-ocr-toolbar"]').first().waitFor({
state: "visible",
timeout: UI_TIMEOUT_MS,
});
await page.getByTestId("mnote-local-ocr-run").click({ timeout: UI_TIMEOUT_MS });
await page.waitForFunction(() => document.querySelector('[data-mnote-local-ocr-status="done"]'), null, {
await page.locator('[data-mnote-resource-tab-panel]:not([hidden]) [data-testid="mnote-local-ocr-toolbar"]').first().waitFor({
state: "detached",
timeout: UI_TIMEOUT_MS,
});
const watchBatchBeforeOcr = await page.evaluate(() => document.documentElement.getAttribute("data-mnote-local-folder-watch-batch-applied") || "");
await page.getByTestId("mnote-local-ocr-task-toggle").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
await page.getByTestId("mnote-local-ocr-task-toggle").click({ timeout: UI_TIMEOUT_MS });
await page.getByTestId("mnote-local-ocr-task-drawer").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
await page.getByTestId("mnote-local-ocr-task-drawer").waitFor({ state: "hidden", timeout: UI_TIMEOUT_MS });
const topbarOcrButtonInfo = await page.getByTestId("mnote-local-ocr-task-toggle").evaluate((node) => {
const topbar = node.closest(".wolai-topbar-actions");
return {
inTopbar: Boolean(topbar),
text: node.textContent || "",
badge: node.querySelector("[data-mnote-local-ocr-task-count]")?.textContent || "",
};
});
assert.equal(topbarOcrButtonInfo.inTopbar, true, `OCR 任务入口应位于右上角 topbar: ${JSON.stringify(topbarOcrButtonInfo)}`);
await page.waitForFunction(
(sourcePath) => {
const row = document.querySelector(`[data-mnote-local-ocr-task-row="${CSS.escape(sourcePath)}"]`);
@@ -145,111 +166,100 @@ async function main() {
sourceRootRelativePath,
{ timeout: UI_TIMEOUT_MS },
);
const uiOcrPath = await page.locator('[data-mnote-local-ocr-path]').first().getAttribute("data-mnote-local-ocr-path");
await page.getByTestId("mnote-local-ocr-task-toggle").click({ timeout: UI_TIMEOUT_MS });
await page.getByTestId("mnote-local-ocr-task-drawer").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
const uiOcrPath = await page.evaluate(async ({ rootUri, sourceRootRelativePath }) => {
const url = new URL("/api/local-folder/ocr/status", window.location.origin);
url.searchParams.set("rootUri", rootUri);
url.searchParams.set("sourceRootRelativePath", sourceRootRelativePath);
const response = await fetch(url.toString(), { cache: "no-store", headers: { accept: "application/json" } });
const payload = await response.json().catch(() => null);
return payload?.job?.ocrRootRelativePath || "";
}, {
rootUri: fileUrl(root),
sourceRootRelativePath,
});
assert(uiOcrPath && uiOcrPath.endsWith(".ocr.md"), `UI OCR path invalid: ${uiOcrPath}`);
await page.waitForFunction(
({ before, ocrPath }) => {
const root = document.documentElement;
const marker = root.getAttribute("data-mnote-local-ocr-filetree-refresh") || "";
const applied = root.getAttribute("data-mnote-local-folder-watch-batch-applied") || "";
return marker === ocrPath && applied && applied !== before;
},
{ before: watchBatchBeforeOcr, ocrPath: uiOcrPath },
{ timeout: UI_TIMEOUT_MS },
);
screenshots.ocrToolbar = path.join(OUTPUT_DIR, "02-ocr-toolbar.png");
await page.screenshot({ path: screenshots.ocrToolbar, fullPage: true });
await page.getByTestId("mnote-local-ocr-insert").click({ timeout: UI_TIMEOUT_MS });
await page.getByTestId("mnote-local-ocr-status").filter({ hasText: "OCR 链接已插入正文" }).waitFor({
state: "visible",
timeout: UI_TIMEOUT_MS,
});
await page.getByTestId("mnote-local-ocr-open").click({ timeout: UI_TIMEOUT_MS });
await page.locator('[data-mnote-resource-tab-panel][data-resource-kind="markdown"]:not([hidden]) [data-testid="mnote-leptos-tiptap-island-editor-root"]').first().waitFor({
state: "visible",
timeout: UI_TIMEOUT_MS,
});
const result = await page.evaluate(async ({ rootUri, documentId, sourceRootRelativePath, workspaceId, ocrToken }) => {
async function jsonFetch(pathname, init = {}) {
const response = await fetch(pathname, {
...init,
headers: {
"content-type": "application/json",
...(init.headers || {}),
},
});
const payload = await response.json().catch(() => null);
return { status: response.status, payload };
}
const create = await jsonFetch("/api/local-folder/ocr/jobs", {
method: "POST",
body: JSON.stringify({
rootUri,
documentId,
sourceRootRelativePath,
provider: "mock",
mockMarkdown: `# OCR Result\n\n${ocrToken} browser smoke text`,
}),
const failedOcrRoute = async (route) => {
if (route.request().method() !== "POST") return route.fallback();
const body = route.request().postDataJSON();
if (body?.sourceRootRelativePath !== failedSourceRootRelativePath) return route.fallback();
return route.fulfill({
status: 401,
contentType: "application/json",
body: JSON.stringify({ ok: false, error: { message: "local_ocr_job_failed_401" } }),
});
const ocrRootRelativePath = create.payload?.job?.ocrRootRelativePath || "";
const read = await jsonFetch(`/api/local-folder/ocr/read?rootUri=${encodeURIComponent(rootUri)}&ocrRootRelativePath=${encodeURIComponent(ocrRootRelativePath)}`, {
method: "GET",
headers: {},
};
await page.route("**/api/local-folder/ocr/jobs", failedOcrRoute);
await page.evaluate(async ({ rootUri, documentId, sourceRootRelativePath, workspaceId }) => {
window.__MNOTE_LOCAL_OCR_PROVIDER = "mineru";
const title = sourceRootRelativePath.split("/").filter(Boolean).pop() || "photo-failed.png";
const fileUrl = new URL("/api/local-folder/files/open", window.location.origin);
fileUrl.searchParams.set("rootUri", rootUri);
fileUrl.searchParams.set("path", sourceRootRelativePath);
const opened = await window.__mnoteDocumentPaneRuntime.openResourceInActiveTab({
objectIdentity: `local-file:${sourceRootRelativePath}`,
assetId: `local-file:${sourceRootRelativePath}`,
title,
fileName: title,
kind: "image",
rootUri,
path: sourceRootRelativePath,
href: fileUrl.toString(),
documentId,
ownerDocumentId: documentId,
workspaceId,
sourceKind: "local_folder",
});
const status = await jsonFetch(`/api/local-folder/ocr/status?rootUri=${encodeURIComponent(rootUri)}&sourceRootRelativePath=${encodeURIComponent(sourceRootRelativePath)}`, {
method: "GET",
headers: {},
});
const jobs = await jsonFetch(`/api/local-folder/ocr/jobs?rootUri=${encodeURIComponent(rootUri)}`, {
method: "GET",
headers: {},
});
const withoutOcr = await jsonFetch("/api/search/documents", {
method: "POST",
body: JSON.stringify({
workspaceId,
sourceKind: "local_folder",
rootUri,
query: ocrToken,
limit: 5,
filters: { includeOcr: false },
}),
});
const withOcr = await jsonFetch("/api/search/documents", {
method: "POST",
body: JSON.stringify({
workspaceId,
sourceKind: "local_folder",
rootUri,
query: ocrToken,
limit: 5,
filters: { includeOcr: true },
}),
});
const insert = await jsonFetch("/api/local-folder/ocr/insert", {
method: "POST",
body: JSON.stringify({
rootUri,
documentId,
ocrRootRelativePath,
mode: "link",
}),
});
return { create, read, status, jobs, withoutOcr, withOcr, insert, ocrRootRelativePath };
if (!opened) throw new Error("OCR failed source image resource tab did not open");
}, {
rootUri: fileUrl(root),
documentId,
sourceRootRelativePath,
sourceRootRelativePath: failedSourceRootRelativePath,
workspaceId,
ocrToken,
});
assert.equal(result.create.status, 200, `OCR create failed: ${JSON.stringify(result.create)}`);
assert.equal(result.create.payload.job.status, "done", `OCR job should be done: ${JSON.stringify(result.create.payload)}`);
assert(result.ocrRootRelativePath.endsWith(".ocr.md"), `OCR path invalid: ${result.ocrRootRelativePath}`);
assert.equal(result.read.status, 200, `OCR read failed: ${JSON.stringify(result.read)}`);
assert(result.read.payload.markdown.includes(ocrToken), "OCR read should include mock OCR text");
assert.equal(result.status.payload.job.ocrRootRelativePath, result.ocrRootRelativePath, "status should return OCR sidecar path");
assert(result.jobs.payload.jobs.some((job) => job.ocrRootRelativePath === result.ocrRootRelativePath), "jobs list should include OCR job");
assert.equal(result.withoutOcr.payload.results.length, 0, `includeOcr=false should not match OCR text: ${JSON.stringify(result.withoutOcr.payload)}`);
const ocrResult = result.withOcr.payload.results.find((item) => item.hasOcr === true);
assert(ocrResult, `includeOcr=true should return owner page OCR result: ${JSON.stringify(result.withOcr.payload)}`);
assert.equal(ocrResult.documentId, documentId, "OCR search result should point to owner document");
assert.equal(ocrResult.ocrEvidence.ocrRootRelativePath, result.ocrRootRelativePath, "OCR evidence should include sidecar path");
assert.equal(result.insert.status, 200, `OCR insert failed: ${JSON.stringify(result.insert)}`);
const ownerMarkdown = fs.readFileSync(path.join(root, relativePath), "utf8");
assert(ownerMarkdown.includes(`[OCRphoto.png](`), `owner markdown should include explicit OCR link:\n${ownerMarkdown}`);
const failedImageTab = page.locator('.mnote-main-tab[data-mnote-tab-kind="image"]', { hasText: "photo-failed.png" }).first();
await failedImageTab.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
await failedImageTab.click({ timeout: UI_TIMEOUT_MS });
await page.waitForFunction(
() => {
const active = document.querySelector('.mnote-main-tab.is-active[data-mnote-tab-kind="image"] .mnote-main-tab-title');
return active && (active.textContent || "").includes("photo-failed.png");
},
null,
{ timeout: UI_TIMEOUT_MS },
);
await page.locator('[data-mnote-resource-tab-panel]:not([hidden]) [data-testid="mnote-local-ocr-toolbar"]').first().waitFor({
state: "detached",
timeout: UI_TIMEOUT_MS,
});
await page.getByTestId("mnote-local-ocr-task-toggle").click({ timeout: UI_TIMEOUT_MS });
await page.waitForFunction(
(sourcePath) => {
const row = document.querySelector(`[data-mnote-local-ocr-task-row="${CSS.escape(sourcePath)}"]`);
return row
&& row.getAttribute("data-mnote-local-ocr-task-status") === "failed"
&& (row.textContent || "").includes("local_ocr_job_failed_401");
},
failedSourceRootRelativePath,
{ timeout: UI_TIMEOUT_MS },
);
screenshots.ocrFailedTask = path.join(OUTPUT_DIR, "04-ocr-failed-task.png");
await page.screenshot({ path: screenshots.ocrFailedTask, fullPage: true });
await page.unroute("**/api/local-folder/ocr/jobs", failedOcrRoute);
const openResourceResult = await page.evaluate(async ({ rootUri, documentId, ocrRootRelativePath, workspaceId }) => {
const runtime = window.__mnoteDocumentPaneRuntime;
@@ -273,7 +283,7 @@ async function main() {
}, {
rootUri: fileUrl(root),
documentId,
ocrRootRelativePath: result.ocrRootRelativePath,
ocrRootRelativePath: uiOcrPath,
workspaceId,
});
assert.equal(openResourceResult, true, "OCR sidecar resource tab should open");
@@ -281,15 +291,90 @@ async function main() {
state: "visible",
timeout: UI_TIMEOUT_MS,
});
await page.waitForFunction(
() => {
const active = document.querySelector('[data-mnote-resource-tab-panel][data-resource-kind="markdown"]:not([hidden]) .ProseMirror');
return active && (active.textContent || "").includes("OCR UI smoke text");
},
null,
{ timeout: UI_TIMEOUT_MS },
).catch(async (error) => {
const debug = await page.evaluate(() => ({
activeTab: document.querySelector('.mnote-main-tab.is-active')?.outerHTML || '',
activePanels: Array.from(document.querySelectorAll('[data-mnote-resource-tab-panel]:not([hidden])')).map((panel) => ({
kind: panel.getAttribute('data-resource-kind') || '',
objectIdentity: panel.getAttribute('data-mnote-object-identity') || '',
text: (panel.textContent || '').slice(0, 200),
html: panel.innerHTML.slice(0, 500),
})),
marker: document.documentElement.getAttribute('data-mnote-local-ocr-filetree-open') || '',
}));
throw new Error(`${error.message}; debug=${JSON.stringify(debug)}`);
});
const fileTreeOpenResult = await page.evaluate((ocrRootRelativePath) => {
const parentPath = ocrRootRelativePath.split("/").slice(0, -1).join("/");
const parentRow = document.querySelector(`#sidebar-file-tree-root .tree-row[data-shell-mode="filetree"][data-local-relative-path="${CSS.escape(parentPath)}"]`);
if (!(parentRow instanceof HTMLElement)) return { ok: false, reason: "ocr_parent_row_missing", parentPath };
if (parentRow.getAttribute("aria-expanded") !== "true") {
const toggle = parentRow.querySelector('[data-rust-action="toggle"]');
if (toggle instanceof HTMLElement) toggle.click();
}
return { ok: true, parentPath };
}, uiOcrPath);
assert.equal(fileTreeOpenResult.ok, true, `OCR sidecar parent should exist in filetree: ${JSON.stringify(fileTreeOpenResult)}`);
const ocrFileTreeRow = page.locator(`#sidebar-file-tree-root .tree-row[data-shell-mode="filetree"][data-local-relative-path="${uiOcrPath.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"]`).first();
await ocrFileTreeRow.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
await ocrFileTreeRow.click({ timeout: UI_TIMEOUT_MS });
await page.waitForFunction(
() => document.documentElement.getAttribute("data-mnote-local-ocr-filetree-open") === "resource-tab",
null,
{ timeout: UI_TIMEOUT_MS },
);
await page.waitForFunction(
() => {
const active = document.querySelector('[data-mnote-resource-tab-panel][data-resource-kind="markdown"]:not([hidden]) .ProseMirror');
return active && (active.textContent || "").includes("OCR UI smoke text");
},
null,
{ timeout: UI_TIMEOUT_MS },
).catch(async (error) => {
const debug = await page.evaluate(() => ({
activeTab: document.querySelector('.mnote-main-tab.is-active')?.outerHTML || '',
activePanels: Array.from(document.querySelectorAll('[data-mnote-resource-tab-panel]:not([hidden])')).map((panel) => ({
kind: panel.getAttribute('data-resource-kind') || '',
objectIdentity: panel.getAttribute('data-mnote-object-identity') || '',
text: (panel.textContent || '').slice(0, 200),
html: panel.innerHTML.slice(0, 500),
})),
marker: document.documentElement.getAttribute('data-mnote-local-ocr-filetree-open') || '',
}));
throw new Error(`${error.message}; debug=${JSON.stringify(debug)}`);
});
screenshots.ocrResource = path.join(OUTPUT_DIR, "03-ocr-resource-tab.png");
await page.screenshot({ path: screenshots.ocrResource, fullPage: true });
await page.evaluate((sourcePath) => {
const row = document.querySelector(`[data-mnote-local-ocr-task-row="${CSS.escape(sourcePath)}"]`);
const clear = row && row.querySelector("[data-mnote-local-ocr-task-clear]");
if (!(clear instanceof HTMLButtonElement)) throw new Error("missing OCR clear button");
clear.click();
}, failedSourceRootRelativePath);
await page.waitForFunction(
(sourcePath) => !document.querySelector(`[data-mnote-local-ocr-task-row="${CSS.escape(sourcePath)}"]`),
failedSourceRootRelativePath,
{ timeout: UI_TIMEOUT_MS },
);
const deleteButtonVisible = await page.evaluate((sourcePath) => {
const row = document.querySelector(`[data-mnote-local-ocr-task-row="${CSS.escape(sourcePath)}"]`);
return Boolean(row && row.querySelector("[data-mnote-local-ocr-task-delete]"));
}, sourceRootRelativePath);
assert.equal(deleteButtonVisible, true, "已完成 OCR 任务应展示删除 OCR 按钮");
await writeResult({
ok: true,
task: TASK,
root,
documentId,
ocrRootRelativePath: result.ocrRootRelativePath,
ocrRootRelativePath: uiOcrPath,
screenshots,
});
console.log(JSON.stringify({
@@ -297,7 +382,7 @@ async function main() {
task: TASK,
root,
documentId,
ocrRootRelativePath: result.ocrRootRelativePath,
ocrRootRelativePath: uiOcrPath,
screenshots,
}, null, 2));
} catch (error) {