Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(docs): keep inspect_pdf going when pdf.js cannot extract one page
Zhao Dengfengebb99c6426d2
diff --git a/apps/desktop/src/main/reviewer/__tests__/pdfInspect.test.ts b/apps/desktop/src/main/reviewer/__tests__/pdfInspect.test.tsindex a58ebbc821..23a3877b59 100644--- a/apps/desktop/src/main/reviewer/__tests__/pdfInspect.test.ts+++ b/apps/desktop/src/main/reviewer/__tests__/pdfInspect.test.ts@@ -148,6 +148,24 @@ describe('inspectPdfPages', () => {expect(Math.round(result.pages[0]!.height)).toBe(792);});+ it('单页文字提取失败只把该页标为未验证,其余页照常检查', async () => {+ // pdf.js 5.7/6.4 在内容流里第一次显示文字前若尚无字体、且先设字号 0(Aspose 等工具写入的+ // 隐形水印即此形态),getTextContent 会抛 "Cannot read properties of null (reading 'name')"。+ // 文件本身完好(MuPDF 正常渲染),不能让这一页拖垮整份检查,也不能把它判成空白。+ const pdf = buildPdf(['before', { raw: 'BT /F1 0 Tf (mark) Tj ET' }, 'after']);+ const result = await inspectPdfPages(pdf, [], 10, 100);+ expect(result.pages.map((p) => p.page)).toEqual([1, 2, 3]);+ expect(result.pages[0]).toMatchObject({ textPreview: 'before', visibilityUnverified: false });+ expect(result.pages[1]).toMatchObject({+ textChars: 0,+ blank: false,+ visibilityUnverified: true,+ });+ expect(result.pages[1]!.textError).toContain("reading 'name'");+ expect(result.pages[1]!.drawOps).toBeGreaterThan(0);+ expect(result.pages[2]).toMatchObject({ textPreview: 'after', visibilityUnverified: false });+ });+it('损坏的 PDF 抛错而不是返回一份"看起来正常"的空结果', async () => {await expect(inspectPdfPages(new Uint8Array(Buffer.from('not a pdf at all')), [], 10, 100),diff --git a/apps/desktop/src/main/reviewer/__tests__/reviewPdfProcess.test.ts b/apps/desktop/src/main/reviewer/__tests__/reviewPdfProcess.test.tsindex 3b1a975e29..e2073a2d7b 100644--- a/apps/desktop/src/main/reviewer/__tests__/reviewPdfProcess.test.ts+++ b/apps/desktop/src/main/reviewer/__tests__/reviewPdfProcess.test.ts@@ -91,8 +91,10 @@ describe('isolated PDF structure inspection', () => {textPreview: '',drawOps: 0,imageOps: 0,- blank: true,- visibilityUnverified: false,+ blank: false,+ visibilityUnverified: true,+ // 单页文字提取失败的原因要穿过进程边界交给 inspect_pdf,否则无法区分空文字与解析失败。+ textError: "Cannot read properties of null (reading 'name')",},],},@@ -109,7 +111,11 @@ describe('isolated PDF structure inspection', () => {pages: [2],fork: () => child,}),- ).resolves.toMatchObject({ numPages: 3, pagesInspected: 1 });+ ).resolves.toMatchObject({+ numPages: 3,+ pagesInspected: 1,+ pages: [{ textError: "Cannot read properties of null (reading 'name')" }],+ });expect(sent).toMatchObject({ kind: 'inspect', pages: [2], maxPages: 5, previewChars: 100 });expect(child.kill).toHaveBeenCalledTimes(1);diff --git a/apps/desktop/src/main/reviewer/reviewPdfProcess.ts b/apps/desktop/src/main/reviewer/reviewPdfProcess.tsindex 11d278f04e..a7e0ca6918 100644--- a/apps/desktop/src/main/reviewer/reviewPdfProcess.ts+++ b/apps/desktop/src/main/reviewer/reviewPdfProcess.ts@@ -218,6 +218,7 @@ function isReviewPdfPageInspection(value: unknown, maxPages: number): boolean {nullableCount(page.imageOps) &&typeof page.blank === 'boolean' &&typeof page.visibilityUnverified === 'boolean' &&+ (page.textError === undefined || typeof page.textError === 'string') &&maxPages > 0);}diff --git a/apps/desktop/src/main/reviewer/reviewPdfProcessProtocol.ts b/apps/desktop/src/main/reviewer/reviewPdfProcessProtocol.tsindex 80331fb7fd..6ff5f4b1f7 100644--- a/apps/desktop/src/main/reviewer/reviewPdfProcessProtocol.ts+++ b/apps/desktop/src/main/reviewer/reviewPdfProcessProtocol.ts@@ -32,8 +32,10 @@ export interface ReviewPdfPageInspection {imageOps: number | null;/** 无文本且无任何绘图/图像算子。算子读不出来时恒为 false(不猜)。 */blank: boolean;- /** true = 算子读取不完整,结构检查未完成;false = 算子已成功读取。 */+ /** true = 算子或文字读取不完整,结构检查未完成;false = 两者都已成功读取。 */visibilityUnverified: boolean;+ /** 该页文字提取失败时的解析器错误(此时 textChars=0 不代表没有文字)。 */+ textError?: string;}export interface ReviewPdfInspectProcessResult {diff --git a/apps/desktop/src/main/reviewer/reviewPdfUtilityProcess.ts b/apps/desktop/src/main/reviewer/reviewPdfUtilityProcess.tsindex 7656af8c75..debff29787 100644--- a/apps/desktop/src/main/reviewer/reviewPdfUtilityProcess.ts+++ b/apps/desktop/src/main/reviewer/reviewPdfUtilityProcess.ts@@ -175,12 +175,13 @@ export function pageVisibilityUnverified(textChars: number,drawOps: number | null,imageOps: number | null,+ textFailed = false,): boolean {// This flag denotes an incomplete structural read, not every page that// legitimately contains content. A successful operator parse must still// allow the normal structural verdict to be `ok`.void textChars;- return drawOps === null || imageOps === null;+ return textFailed || drawOps === null || imageOps === null;}export function selectPdfPageNumbers(@@ -212,6 +213,12 @@ export function selectPdfPageNumbers(* 算子表单独 try/catch:损坏页或超预算时降级成 null 而不是让整次检查失败 ——* 半份结构信息也比"检查工具自己挂了"有用。null 时 blank 恒为 false,并标记* visibilityUnverified,不把未完成的结构检查当成通过。+ *+ * 文字提取同样逐页 try/catch。pdf.js 对部分合法文件会在 getTextContent 里抛错+ * (例如内容流开头先以字号 0 显示文字、此前未设字体时的+ * "Cannot read properties of null (reading 'name')",常见于隐形水印),此时+ * 文件本身可正常渲染。该页记为 textError + visibilityUnverified,blank 恒为 false,+ * 其余页照常检查;整份文件打不开(InvalidPDFException 等)仍然整体抛错。*/export async function inspectPdfPages(data: Uint8Array,@@ -248,14 +255,20 @@ export async function inspectPdfPages(const page = await document.getPage(pageNumber);const viewport = page.getViewport({ scale: 1 });- const textContent = await page.getTextContent();- const parts: string[] = [];- for (const item of textContent.items) {- if (!item || typeof item !== 'object' || !('str' in item)) continue;- if (typeof item.str !== 'string') continue;- parts.push(item.str, item.hasEOL ? '\n' : ' ');+ let text = '';+ let textError: string | undefined;+ try {+ const textContent = await page.getTextContent();+ const parts: string[] = [];+ for (const item of textContent.items) {+ if (!item || typeof item !== 'object' || !('str' in item)) continue;+ if (typeof item.str !== 'string') continue;+ parts.push(item.str, item.hasEOL ? '\n' : ' ');+ }+ text = parts.join('').replace(/\s+/g, ' ').trim();+ } catch (error) {+ textError = (error instanceof Error ? error.message : String(error)).slice(0, 300);}- const text = parts.join('').replace(/\s+/g, ' ').trim();let drawOps: number | null = null;let imageOps: number | null = null;@@ -284,8 +297,14 @@ export async function inspectPdfPages(textPreview: previewChars > 0 ? text.slice(0, previewChars) : '',drawOps,imageOps,- blank: text.length === 0 && drawOps === 0 && imageOps === 0,- visibilityUnverified: pageVisibilityUnverified(text.length, drawOps, imageOps),+ blank: textError === undefined && text.length === 0 && drawOps === 0 && imageOps === 0,+ visibilityUnverified: pageVisibilityUnverified(+ text.length,+ drawOps,+ imageOps,+ textError !== undefined,+ ),+ ...(textError !== undefined ? { textError } : {}),});}return { numPages, pagesInspected: pages.length, pages };diff --git a/packages/lizi-mcps/src/__tests__/cindyDocsMcpServer.test.ts b/packages/lizi-mcps/src/__tests__/cindyDocsMcpServer.test.tsindex ca641b9ad7..bbc95fdbf0 100644--- a/packages/lizi-mcps/src/__tests__/cindyDocsMcpServer.test.ts+++ b/packages/lizi-mcps/src/__tests__/cindyDocsMcpServer.test.ts@@ -2710,6 +2710,29 @@ describe('inspect_pdf', () => {expect(result.verdict).not.toBe('ok');});+ it('单页文字提取失败时给 warning 并指明页码,不当成空白或生成失败', async () => {+ const client = await withPdf({+ numPages: 2,+ pagesInspected: 2,+ pages: [+ page(),+ page({+ page: 2,+ textChars: 0,+ textPreview: '',+ blank: false,+ visibilityUnverified: true,+ textError: "Cannot read properties of null (reading 'name')",+ }),+ ],+ });+ const result = await callTool(client, 'inspect_pdf', { path: 'out.pdf' });+ expect(result.verdict).toBe('warning');+ expect(result.blankPages).toEqual([]);+ expect(result.visibilityUnverifiedPages).toEqual([2]);+ expect(result.warning).toContain('第 2 页是解析器读不出文字');+ });+it('横向与非标准纸张都翻译成人能对照的说法', async () => {const client = await withPdf({numPages: 2,@@ -2858,7 +2881,9 @@ describe('inspect_pdf', () => {});const failed = await callTool(broken, 'inspect_pdf', { path: 'out.pdf' });expect(failed.errorCode).toBe('INSPECT_FAILED');+ // 解析器缺陷也会走到这里:提示仍要求坏文件重做,但不能断言生成环节一定出错。expect((failed.data as Record<string, string>).hint).toContain('重做');+ expect((failed.data as Record<string, string>).hint).toContain('兼容性');});});diff --git a/packages/lizi-mcps/src/cindy-docs/inspect_pdf.ts b/packages/lizi-mcps/src/cindy-docs/inspect_pdf.tsindex bc9f202f73..b807a5ad5e 100644--- a/packages/lizi-mcps/src/cindy-docs/inspect_pdf.ts+++ b/packages/lizi-mcps/src/cindy-docs/inspect_pdf.ts@@ -71,7 +71,8 @@ const DESCRIPTION = ['','【怎么判读】','- blankPages 非空 → 那几页是结构上确定为空的,大概率 CSS 把内容藏了或外部资源没加载上,重做;',- '- visibilityUnverified=true → 某页算子解析未完成,未做位图级可见性确认,结构检查证据不完整;请重试或人工打开确认,不要把 verdict 当作视觉验收;',+ '- visibilityUnverified=true → 某页算子或文字解析未完成,未做位图级可见性确认,结构检查证据不完整;请重试或人工打开确认,不要把 verdict 当作视觉验收;',+ '- textError 非空 → 解析器读不出该页文字,textChars=0 不代表没有文字;常见于引用了原文件的页,用其他阅读器打开该页确认,不要仅凭此判定生成失败;','- numPages 远超预期 → 分页样式没生效(检查 page-break / break-inside);','- paper 不是你要的尺寸 → render_pdf 的 pageSize 传错了;','- textPreview 和你写的内容对不上 → 装错了内容或页序错乱。',@@ -220,6 +221,9 @@ export function registerInspectPdfTool(const visibilityUnverifiedPages = decorated.filter((p) => p.visibilityUnverified).map((p) => p.page);+ const textErrorPages = decorated+ .filter((p) => p.textError !== undefined)+ .map((p) => p.page);const inspectedPageNumbers = new Set(decorated.map((page) => page.page));let accumulatedThrough = Math.min(inspectedThrough, inspection.numPages);while (inspectedPageNumbers.has(accumulatedThrough + 1)) accumulatedThrough += 1;@@ -263,7 +267,7 @@ export function registerInspectPdfTool(: accumulatedVisibilityUnverified? {verdict: 'warning',- warning: `${visibilityUnverifiedPages.length > 0 ? `第 ${visibilityUnverifiedPages.join('、')} 页的结构算子解析未完成` : '此前批次存在结构算子解析未完成的页面'},未做位图级可见性确认,检查证据不完整。请重试并在必要时打开 PDF 确认。${coverageWarning ? ` ${coverageWarning}` : ''}`,+ warning: `${visibilityUnverifiedPages.length > 0 ? `第 ${visibilityUnverifiedPages.join('、')} 页的结构算子解析未完成` : '此前批次存在结构算子解析未完成的页面'},未做位图级可见性确认,检查证据不完整。${textErrorPages.length > 0 ? `其中第 ${textErrorPages.join('、')} 页是解析器读不出文字(见 textError),可能是解析器兼容性问题而非文件损坏。` : ''}请重试并在必要时打开 PDF 确认。${coverageWarning ? ` ${coverageWarning}` : ''}`,}: partial? { verdict: 'incomplete', warning: coverageWarning! }@@ -292,7 +296,7 @@ export function registerInspectPdfTool(timedOut ? 'INSPECT_TIMEOUT' : 'INSPECT_FAILED',timedOut? `解析超过 ${INSPECT_PDF_TIMEOUT_MS / 1000} 秒被中止,文件可能损坏或过于复杂。`- : `读取 PDF 失败:${message}。如果这是刚生成的文件,说明生成环节就出了问题,请重做而不是交付。`,+ : `读取 PDF 失败:${message}。可能是文件损坏,也可能是解析器兼容性问题;先用其他阅读器打开确认。打不开或内容缺失就重做,不要交付。`,{ message },);}diff --git a/packages/lizi-mcps/src/cindy-docs/types.ts b/packages/lizi-mcps/src/cindy-docs/types.tsindex 8238867e53..51de2aad87 100644--- a/packages/lizi-mcps/src/cindy-docs/types.ts+++ b/packages/lizi-mcps/src/cindy-docs/types.ts@@ -94,6 +94,8 @@ export interface DocsPdfPageInspection {blank: boolean;/** 结构存在但未做位图级可见性验证。 */visibilityUnverified: boolean;+ /** 该页文字提取失败时的解析器错误(此时 textChars=0 不代表没有文字)。 */+ textError?: string;}export interface DocsPdfInspection {