Improve LightRAG knowledge search locator alignment

This commit is contained in:
lix-2026
2026-06-08 20:35:49 +08:00
parent 9551d4c1dc
commit 0e8b03daf8
28 changed files with 5769 additions and 140 deletions
+145 -7
View File
@@ -37,6 +37,7 @@ function debugLog(message) {
}
const toolContextStorage = new AsyncLocalStorage();
const MNOTE_UI_CITATION_QUEUE = [];
const MNOTE_TOOL_NAMES = [
'mnote.skill.read',
@@ -408,6 +409,8 @@ let nextId = 1;
const pendingReqs = new Map(); // id → { resolve, reject }
const requestHandlers = new Map(); // method → handler
const notificationHandlers = new Map(); // method → handler
let rpcServerReady = false;
const queuedRpcLines = [];
function sendMessage(msg) {
const line = JSON.stringify(msg) + '\n';
@@ -434,9 +437,7 @@ function onNotification(method, handler) {
notificationHandlers.set(method, handler);
}
// Start reading NDJSON from stdin
const rl = createInterface({ input: stdin, terminal: false });
rl.on('line', async (raw) => {
async function handleRpcLine(raw) {
const trimmed = raw.trim();
if (!trimmed) return;
@@ -480,6 +481,16 @@ rl.on('line', async (raw) => {
const handler = notificationHandlers.get(msg.method);
if (handler) handler(msg.params);
}
}
// 先接住 stdio 输入,但等全部 handler 注册完成后再处理,避免 initialize 抢跑。
const rl = createInterface({ input: stdin, terminal: false });
rl.on('line', (raw) => {
if (!rpcServerReady) {
queuedRpcLines.push(raw);
return;
}
void handleRpcLine(raw);
});
// ── Helper: send ACP session/update (camelCase per protocol spec) ──
@@ -566,7 +577,129 @@ async function callMnoteTool(toolName, args) {
const text = await response.text().catch(() => '');
throw new Error(`mnote tool ${toolName} failed: HTTP ${response.status} ${text}`);
}
return response.json();
const result = compactMnoteToolResultForReasonix(toolName, await response.json());
const citations = collectUiCitationMarkdowns(result).slice(0, 8);
if (citations.length) MNOTE_UI_CITATION_QUEUE.push(citations);
return result;
}
function compactMnoteToolResultForReasonix(toolName, payload) {
if (toolName !== 'mnote.knowledge_rag.query') return payload;
const result = payload?.result && typeof payload.result === 'object' ? payload.result : payload;
const citationMarkdowns = collectUiCitationMarkdowns(result).slice(0, 8);
const citations = citationMarkdowns
.map((citationMarkdown) => ({ citationMarkdown }))
.slice(0, 8);
if (!citations.length || !result || typeof result !== 'object') return payload;
return {
ok: payload?.ok !== false,
schema: result.schema || 'mnote.knowledge_rag.agent_query_result.v1',
uiCitations: citations,
citationRendering: 'MNote UI renders uiCitations after the answer as clickable source locators. Do not copy citationMarkdown into the final answer and do not hand-write /documents links.',
answerCitationPolicy: 'Answer the substance in plain text. Mention source titles only if useful; leave clickable citation insertion to MNote UI.',
answerGuidance: result.answerGuidance || '',
references: Array.isArray(result.references) ? result.references.slice(0, 8) : [],
citations: citationMarkdowns,
sourceScope: result.sourceScope || [],
sourceScopeMode: result.sourceScopeMode || '',
rawScopeFiltered: Boolean(result.rawScopeFiltered),
};
}
function collectUiCitationMarkdowns(value) {
const references = Array.isArray(value?.references) ? value.references : [];
const referenceCitations = [];
if (references.length) {
const hasPrecise = references.some((reference) =>
typeof reference?.citationMarkdown === 'string' &&
reference.citationMarkdown.trim() &&
reference.locatorDegraded !== true
);
const seen = new Set();
for (const reference of references) {
const citation = String(reference?.citationMarkdown || '').trim();
if (!citation || seen.has(citation)) continue;
if (hasPrecise && reference?.locatorDegraded === true) continue;
seen.add(citation);
referenceCitations.push(citation);
}
if (referenceCitations.length) return referenceCitations;
}
const citations = collectCitationMarkdowns(value);
const hasPrecise = citations.some((item) => !isDegradedCitationMarkdown(item));
return citations.filter((citation) => {
return !hasPrecise || !isDegradedCitationMarkdown(citation);
});
}
function isDegradedCitationMarkdown(value) {
const text = String(value || '').toLowerCase();
return text.includes('来源定位降级') || text.includes('locator degraded');
}
function collectCitationMarkdowns(value) {
const out = [];
const seen = new Set();
function add(text) {
const value = String(text || '').trim();
if (!value || seen.has(value)) return;
seen.add(value);
out.push(value);
}
function visit(node) {
if (!node) return;
if (typeof node === 'string') {
const trimmed = node.trim();
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
try {
visit(JSON.parse(trimmed));
} catch {}
}
return;
}
if (Array.isArray(node)) {
node.forEach(visit);
return;
}
if (typeof node !== 'object') return;
if (typeof node.citationMarkdown === 'string') add(node.citationMarkdown);
Object.values(node).forEach(visit);
}
visit(value);
return out;
}
function toolResultTextWithUiCitations(rawText) {
const text = String(rawText || '');
let citationMarkdowns = collectUiCitationMarkdownsFromText(text).slice(0, 8);
if (!citationMarkdowns.length && MNOTE_UI_CITATION_QUEUE.length) {
citationMarkdowns = MNOTE_UI_CITATION_QUEUE.shift();
}
const citations = citationMarkdowns.map((citationMarkdown) => ({ citationMarkdown })).slice(0, 8);
if (!citations.length) return text.slice(0, 8000);
const prefix = JSON.stringify({
schema: 'mnote.acp.tool_result_ui_citations.v1',
uiCitations: citations,
citationRendering: 'MNote UI renders these citations after the answer; the model must not hand-write local citation links.',
});
const budget = Math.max(0, 8000 - prefix.length - 2);
return `${prefix}\n${text.slice(0, budget)}`;
}
function collectUiCitationMarkdownsFromText(text) {
const trimmed = String(text || '').trim();
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
try {
return collectUiCitationMarkdowns(JSON.parse(trimmed));
} catch {}
const firstLine = trimmed.split('\n')[0]?.trim();
if (firstLine && firstLine !== trimmed && (firstLine.startsWith('{') || firstLine.startsWith('['))) {
try {
return collectUiCitationMarkdowns(JSON.parse(firstLine));
} catch {}
}
}
return collectCitationMarkdowns(trimmed);
}
// ── Register Tools ───────────────────────────────────
@@ -637,7 +770,7 @@ function fallbackMnoteToolSpecs() {
{
mnoteToolName: 'mnote.knowledge_rag.query',
name: 'mnote_knowledge_rag_query',
description: '向 LightRAG 资料库提问,返回 provider 原始结果和经 MNote registry 映射、sourcePaths 后过滤的 references。回答必须引用返回来源不要引用 raw chunks。',
description: '向 LightRAG 资料库提问,返回 provider 原始结果和经 MNote registry 映射、sourcePaths 后过滤的 references/citations。用户要求链接、来源、引用或证据时优先调用。不要在最终回答中手写 citationMarkdown、/documents、mnote:// 或搜索引擎包装链接;MNote 前端会把 uiCitations/citationMarkdown 自动追加成可点击来源不要引用 raw chunks。',
parameters: {
type: 'object',
properties: {
@@ -775,7 +908,7 @@ onRequest('session/new', async (params) => {
'<available-skills>',
'Use mnote_skill_read to load the full content of any skill listed by the current prompt capabilities.',
'- mnote-current-page — Read the current MNote Markdown page when the task needs page content.',
'- mnote-knowledge-rag — Ask the LightRAG-backed knowledge library across books, papers, PDFs, Office files, images, and attachments. Use mnote_knowledge_rag_query for knowledge-library questions and cite only returned references; sourcePaths filters returned references after provider retrieval, so do not cite raw chunks. If locatorDegraded is true, say the source location is degraded rather than inventing page/bbox.',
'- mnote-knowledge-rag — Ask the LightRAG-backed knowledge library across books, papers, PDFs, Office files, images, and attachments. Use mnote_knowledge_rag_query for knowledge-library questions and answers that require links/sources/citations. Returned uiCitations/citationMarkdown values are MNote clickable source locators and are rendered by the MNote UI after the final answer. Do not copy citationMarkdown into the answer, do not hand-write /documents or mnote:// links, and never wrap local citation URLs with search engines. Mention source titles in plain text only when useful; sourcePaths filters returned references after provider retrieval, so do not cite raw chunks. If locatorDegraded is true, say the source location is degraded rather than inventing page/bbox.',
'- mnote-local-file — Resolve MNote targets and then use native file tools inside allowed roots.',
'- mnote-chat-only — Reply conversationally without MNote file/page tools.',
'</available-skills>',
@@ -841,6 +974,7 @@ onRequest('session/prompt', async (params) => {
const announcedToolKeys = new Set();
const preparingToolCallIds = [];
const inflightToolCallIds = [];
MNOTE_UI_CITATION_QUEUE.length = 0;
function nextToolCallId() {
return `tc_${nextToolCallSeq++}`;
@@ -933,7 +1067,7 @@ onRequest('session/prompt', async (params) => {
}
case 'tool': {
hasToolCall = true;
const resultText = String(ev.content || '').slice(0, 8000);
const resultText = toolResultTextWithUiCitations(ev.content);
emitToolResult(
session.id,
inflightToolCallIds.shift() || ev.callId || nextToolCallId(),
@@ -990,4 +1124,8 @@ onNotification('session/cancel', (params) => {
// ── Start ────────────────────────────────────────────
rpcServerReady = true;
for (const raw of queuedRpcLines.splice(0)) {
await handleRpcLine(raw);
}
process.stderr.write(`[reasonix-acp-mnote] ready (mnote=${MNOTE_WEB_URL})\n`);
@@ -112,6 +112,21 @@ async function query(context, queryText, sourcePaths) {
return result.payload;
}
async function search(context, queryText, sourcePaths) {
const result = await apiJson(context, "POST", `${BASE_URL}/api/knowledge-rag/search`, {
rootUri: ROOT_URI,
workspaceId: WORKSPACE_ID,
query: queryText,
mode: "mix",
topK: 12,
chunkTopK: 12,
includeChunkContent: true,
sourcePaths,
});
assert(result.ok, `knowledge-rag search 失败: ${result.status} ${result.text.slice(0, 1000)}`);
return result.payload;
}
async function deleteSource(context, sourcePath) {
const result = await apiJson(context, "POST", `${BASE_URL}/api/knowledge-rag/delete-source`, {
rootUri: ROOT_URI,
@@ -146,6 +161,23 @@ async function main() {
`sourcePaths scope 不应返回非 alpha 来源: ${JSON.stringify(references, null, 2).slice(0, 3000)}`,
);
assert(scoped.raw, "HTTP API 仍应保留 raw 供调试调用方使用");
const searched = await search(context, marker, [sourceA]);
const searchResults = Array.isArray(searched.results) ? searched.results : [];
assert.equal(searched.schema, "mnote.knowledge_rag.search_results.v1", `search schema 不正确: ${JSON.stringify(searched, null, 2).slice(0, 3000)}`);
assert.equal(searched.sourceScopeMode, "post_filter_mapped_references", `search sourceScopeMode 不正确: ${JSON.stringify(searched, null, 2).slice(0, 3000)}`);
assert(searchResults.length > 0, `资料库 search 应至少返回 alpha: ${JSON.stringify(searched, null, 2).slice(0, 3000)}`);
assert(
searchResults.every((item) => item.provider === "lightrag" && item.matchSource === "lightrag_reference"),
`资料库 search 结果必须来自 LightRAG references: ${JSON.stringify(searchResults, null, 2).slice(0, 3000)}`,
);
assert(
searchResults.every((item) => item.path === sourceA),
`资料库 search sourcePaths scope 不应返回非 alpha 来源: ${JSON.stringify(searchResults, null, 2).slice(0, 3000)}`,
);
assert(
searchResults.some((item) => item.citationUrl || item.locator || item.openAction),
`资料库 search 结果必须可打开来源: ${JSON.stringify(searchResults, null, 2).slice(0, 3000)}`,
);
await deleteSource(context, sourceA);
assert(fs.existsSync(path.join(ROOT_PATH, sourceA)), "delete-source 不应删除用户原始 source 文件");
@@ -164,6 +196,7 @@ async function main() {
sourceScopeMode: scoped.sourceScopeMode,
rawScopeFiltered: scoped.rawScopeFiltered,
scopedReferenceCount: references.length,
searchResultCount: searchResults.length,
alphaDocId: entryFor(indexedStatus, sourceA)?.lightRagDocId || null,
betaDocId: entryFor(indexedStatus, sourceB)?.lightRagDocId || null,
sourceAExistsAfterDelete: fs.existsSync(path.join(ROOT_PATH, sourceA)),
@@ -0,0 +1,179 @@
#!/usr/bin/env node
"use strict";
const assert = require("node:assert");
const fs = require("node:fs");
const path = require("node:path");
const { chromium } = require("playwright");
const BASE_URL = (process.env.MNOTE_WEB_SMOKE_BASE_URL || "http://127.0.0.1:3000").replace(/\/+$/, "");
const ROOT_URI = process.env.MNOTE_KNOWLEDGE_RAG_ROOT_URI || "file:///mnt/Data1T/Mnote_data/users/mnote-e2e/workspaces/my-space";
const WORKSPACE_ID = process.env.MNOTE_KNOWLEDGE_RAG_WORKSPACE_ID || "local-ws:mnote-e2e:my-space";
const QUERY = process.env.MNOTE_KNOWLEDGE_RAG_OFFICE_QUERY || "三乙基硅";
const EXPECTED_RESOURCE = process.env.MNOTE_KNOWLEDGE_RAG_EXPECTED_RESOURCE
|| "有机合成中的保护基/[OCR]_有机合成中的保护基-酚羰基羧基巯基的保护_20250201_1908.layered_删减-2025-02-04 18-59-42.docx";
const EXPECTED_BLOCK_ID = process.env.MNOTE_KNOWLEDGE_RAG_EXPECTED_BLOCK_ID || "0ace5daa070f0911e09d8ab37c64eeea";
const EXPECTED_HIGHLIGHT = process.env.MNOTE_KNOWLEDGE_RAG_EXPECTED_HIGHLIGHT || "三乙基硅酯";
const OUTPUT_DIR = path.join(process.cwd(), "tmp", "task540-knowledge-rag-office-result-open-locator-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
const SEARCH_SCREENSHOT_PATH = path.join(OUTPUT_DIR, "office-search-result.png");
const OPEN_SCREENSHOT_PATH = path.join(OUTPUT_DIR, "office-open-locator.png");
const UI_TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_UI_TIMEOUT_MS || 30_000);
const CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH
|| ["/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/snap/bin/chromium"]
.find((candidate) => fs.existsSync(candidate));
async function main() {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const browser = await chromium.launch({ headless: true, executablePath: CHROMIUM_EXECUTABLE_PATH });
const context = await browser.newContext({ viewport: { width: 1360, height: 900 } });
try {
const auth = await context.request.post(`${BASE_URL}/api/auth`, {
data: {
action: "auth:signIn",
args: {
provider: "password",
params: {
email: "mnote.e2e@example.com",
password: process.env.MNOTE_E2E_PASSWORD || "MnoteE2E123!",
flow: "signIn",
},
},
},
});
assert(auth.ok(), `登录失败: ${auth.status()} ${await auth.text()}`);
const apiSearch = await context.request.post(`${BASE_URL}/api/knowledge-rag/search`, {
data: {
rootUri: ROOT_URI,
workspaceId: WORKSPACE_ID,
query: QUERY,
mode: "mix",
topK: 8,
chunkTopK: 8,
includeChunkContent: true,
sourcePaths: [EXPECTED_RESOURCE],
},
});
assert(apiSearch.ok(), `knowledge-rag search 失败: ${apiSearch.status()} ${await apiSearch.text()}`);
const apiPayload = await apiSearch.json();
const apiFirst = apiPayload.results?.[0];
assert(apiFirst, `API 未返回资料库结果: ${JSON.stringify(apiPayload, null, 2).slice(0, 3000)}`);
assert.equal(apiFirst.locator?.blockId, EXPECTED_BLOCK_ID, `API locator blockId 不匹配: ${JSON.stringify(apiFirst, null, 2)}`);
assert(String(apiFirst.snippet || "").includes(EXPECTED_HIGHLIGHT), `API snippet 未命中 ${EXPECTED_HIGHLIGHT}: ${apiFirst.snippet}`);
const page = await context.newPage();
const pageErrors = [];
const consoleErrors = [];
page.on("pageerror", (error) => pageErrors.push(String(error?.stack || error?.message || error)));
page.on("console", (message) => {
if (["error", "warning"].includes(message.type())) consoleErrors.push(`${message.type()}: ${message.text()}`);
});
await page.goto(`${BASE_URL}/`, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await page.evaluate(() => {
window.localStorage?.setItem("mnote.search.collapseSourcesDefault.v1", "0");
});
await page.waitForSelector('[data-mnote-action="open-search-modal"]', { timeout: UI_TIMEOUT_MS });
await page.click('[data-mnote-action="open-search-modal"]');
await page.waitForSelector('[data-testid="wolai-search-input"]', { timeout: UI_TIMEOUT_MS });
await page.fill('[data-testid="wolai-search-input"]', QUERY);
await page.evaluate(() => {
const button = document.querySelector('[data-search-switch="knowledge"]');
if (button instanceof HTMLElement) {
button.setAttribute("aria-checked", "true");
button.classList.add("is-on");
}
document.querySelector('[data-testid="wolai-search-input"]')
?.dispatchEvent(new Event("input", { bubbles: true }));
});
await page.waitForFunction(
(expected) => {
const first = document.querySelector('[data-testid="wolai-search-result-row"]');
return first && first.textContent.includes(expected);
},
EXPECTED_HIGHLIGHT,
{ timeout: UI_TIMEOUT_MS },
);
await page.screenshot({ path: SEARCH_SCREENSHOT_PATH, fullPage: true });
await page.click('[data-testid="wolai-search-result-row"]');
await page.waitForFunction(
(expectedResource) => document.querySelector(`[data-mnote-resource-tab-panel][data-resource-path="${CSS.escape(expectedResource)}"]`),
EXPECTED_RESOURCE,
{ timeout: UI_TIMEOUT_MS },
);
await page.waitForTimeout(8_000);
const state = await page.evaluate((expectedResource) => {
const panel = document.querySelector(`[data-mnote-resource-tab-panel][data-resource-path="${CSS.escape(expectedResource)}"]`);
const frame = panel?.querySelector("iframe.mnote-resource-tab-frame");
const result = {
url: location.href,
panelVisible: !!panel && !panel.hidden,
panelResourcePath: panel ? panel.getAttribute("data-resource-path") : "",
panelBlockId: panel ? panel.getAttribute("data-mnote-evidence-block-id") : "",
panelEvidenceText: panel ? panel.getAttribute("data-mnote-evidence-text") : "",
frameSrc: frame ? frame.getAttribute("src") : "",
};
if (frame?.contentDocument) {
const doc = frame.contentDocument;
const viewer = doc.querySelector("#mnote-office-viewer");
const highlighted = doc.querySelector('[data-mnote-office-evidence-target="true"]');
result.iframe = {
readyState: doc.readyState,
status: doc.documentElement.getAttribute("data-mnote-office-preview-status") || "",
applied: doc.documentElement.getAttribute("data-mnote-office-evidence-applied") || "",
bodyEvidenceText: doc.body?.dataset?.evidenceText || "",
textHasQuery: (viewer?.textContent || "").includes("三乙基硅"),
scrollY: frame.contentWindow?.scrollY || 0,
highlightedText: highlighted?.textContent?.slice(0, 160) || "",
highlightedTop: highlighted ? Math.round(highlighted.getBoundingClientRect().top) : null,
};
}
return result;
}, EXPECTED_RESOURCE);
await page.screenshot({ path: OPEN_SCREENSHOT_PATH, fullPage: true });
assert.equal(state.panelVisible, true, `搜索结果未打开资源标签: ${JSON.stringify(state, null, 2)}`);
assert.equal(state.panelResourcePath, EXPECTED_RESOURCE, `资源路径不匹配: ${JSON.stringify(state, null, 2)}`);
assert.equal(state.panelBlockId, EXPECTED_BLOCK_ID, `资源标签 blockId 不匹配: ${JSON.stringify(state, null, 2)}`);
assert.equal(state.iframe?.applied, "true", `Office preview 未应用 evidence locator: ${JSON.stringify(state, null, 2)}`);
assert(String(state.iframe?.highlightedText || "").includes(EXPECTED_HIGHLIGHT), `Office preview 未高亮 ${EXPECTED_HIGHLIGHT}: ${JSON.stringify(state, null, 2)}`);
const result = {
ok: true,
baseUrl: BASE_URL,
rootUri: ROOT_URI,
workspaceId: WORKSPACE_ID,
query: QUERY,
expectedResource: EXPECTED_RESOURCE,
expectedBlockId: EXPECTED_BLOCK_ID,
apiFirst: {
snippet: apiFirst.snippet,
blockId: apiFirst.locator?.blockId,
citationUrl: apiFirst.citationUrl,
},
state,
pageErrors,
consoleErrors,
screenshots: {
search: SEARCH_SCREENSHOT_PATH,
open: OPEN_SCREENSHOT_PATH,
},
};
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.log(JSON.stringify(result, null, 2));
} finally {
await browser.close().catch(() => undefined);
}
}
main().catch((error) => {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
fs.writeFileSync(
path.join(OUTPUT_DIR, "failure.json"),
`${JSON.stringify({ ok: false, error: error.stack || error.message || String(error) }, null, 2)}\n`,
"utf8",
);
console.error(error.stack || error.message || String(error));
process.exit(1);
});
@@ -0,0 +1,233 @@
#!/usr/bin/env node
"use strict";
const assert = require("node:assert");
const fs = require("node:fs");
const path = require("node:path");
const { chromium } = require("playwright");
const BASE_URL = (process.env.MNOTE_WEB_SMOKE_BASE_URL || "http://127.0.0.1:3000").replace(/\/+$/, "");
const QUERY = process.env.MNOTE_KNOWLEDGE_RAG_REPOSITION_QUERY || "三甲基硅";
const EXPECTED_RESOURCE = process.env.MNOTE_KNOWLEDGE_RAG_EXPECTED_RESOURCE
|| "有机合成中的保护基/[OCR]_有机合成中的保护基-酚羰基羧基巯基的保护_20250201_1908.layered_删减-2025-02-04 18-59-42.docx";
const OUTPUT_DIR = path.join(process.cwd(), "tmp", "task541-knowledge-rag-search-panel-office-reposition-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
const SEARCH_SCREENSHOT_PATH = path.join(OUTPUT_DIR, "search-restored.png");
const OPEN_SCREENSHOT_PATH = path.join(OUTPUT_DIR, "office-reposition.png");
const UI_TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_UI_TIMEOUT_MS || 30_000);
const CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH
|| ["/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/snap/bin/chromium"]
.find((candidate) => fs.existsSync(candidate));
async function openKnowledgeSearch(page, query) {
await page.click('[data-mnote-action="open-search-modal"]');
await page.waitForSelector('[data-testid="wolai-search-input"]', { timeout: UI_TIMEOUT_MS });
await page.evaluate(() => {
const button = document.querySelector('[data-search-switch="knowledge"]');
if (button instanceof HTMLElement) {
button.setAttribute("aria-checked", "true");
button.classList.add("is-on");
}
});
await page.fill('[data-testid="wolai-search-input"]', query);
await page.evaluate(() => {
document.querySelector('[data-testid="wolai-search-input"]')
?.dispatchEvent(new Event("input", { bubbles: true }));
});
}
async function waitForOfficeRows(page) {
await page.waitForFunction(
({ query, expectedResource }) => {
const rows = Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'));
return rows.some((row) => {
const locator = JSON.parse(row.getAttribute("data-evidence-locator") || "null");
const path = String(locator?.resourcePath || locator?.resource_path || "");
return path === expectedResource && row.textContent.includes(query);
});
},
{ query: QUERY, expectedResource: EXPECTED_RESOURCE },
{ timeout: UI_TIMEOUT_MS },
);
}
async function pickOfficeResult(page, excludeBlockId = "") {
return page.evaluate(({ expectedResource, excludeBlockId }) => {
const rows = Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'));
for (const row of rows) {
const locator = JSON.parse(row.getAttribute("data-evidence-locator") || "null");
const resourcePath = String(locator?.resourcePath || locator?.resource_path || "");
const blockId = String(locator?.blockId || locator?.block_id || "");
if (resourcePath === expectedResource && blockId && blockId !== excludeBlockId) {
return {
index: Number(row.getAttribute("data-search-result-index") || -1),
blockId,
text: row.textContent,
};
}
}
return null;
}, { expectedResource: EXPECTED_RESOURCE, excludeBlockId });
}
async function clickSearchResultByIndex(page, index) {
await page.locator(`[data-testid="wolai-search-result-row"][data-search-result-index="${index}"]`).click();
}
async function waitForOfficeLocator(page, expectedBlockId) {
await page.waitForFunction(
({ expectedResource, expectedBlockId }) => {
const panel = document.querySelector(`[data-mnote-resource-tab-panel][data-resource-path="${CSS.escape(expectedResource)}"]`);
return panel && panel.getAttribute("data-mnote-evidence-block-id") === expectedBlockId;
},
{ expectedResource: EXPECTED_RESOURCE, expectedBlockId },
{ timeout: UI_TIMEOUT_MS },
);
await page.waitForTimeout(1_500);
}
async function readOfficeState(page) {
return page.evaluate((expectedResource) => {
const panel = document.querySelector(`[data-mnote-resource-tab-panel][data-resource-path="${CSS.escape(expectedResource)}"]`);
const frame = panel?.querySelector("iframe.mnote-resource-tab-frame");
const result = {
searchOpen: !document.querySelector('[data-testid="wolai-search-modal"]')?.hidden,
panelBlockId: panel?.getAttribute("data-mnote-evidence-block-id") || "",
frameSrc: frame?.getAttribute("src") || "",
loadCount: window.__mnoteTask541LoadCount || 0,
};
if (frame?.contentDocument) {
const doc = frame.contentDocument;
const target = doc.querySelector('[data-mnote-office-evidence-target="true"]');
const marker = doc.querySelector('[data-mnote-office-evidence-marker="true"]');
result.iframe = {
status: doc.documentElement.getAttribute("data-mnote-office-preview-status") || "",
applied: doc.documentElement.getAttribute("data-mnote-office-evidence-applied") || "",
highlightedText: target?.textContent?.slice(0, 200) || "",
markerMode: marker?.getAttribute("data-mnote-office-evidence-marker-mode") || "",
markerTargetText: marker?.getAttribute("data-mnote-office-evidence-target-text") || "",
markerHeight: marker ? Math.round(marker.getBoundingClientRect().height) : 0,
};
}
return result;
}, EXPECTED_RESOURCE);
}
async function main() {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const browser = await chromium.launch({ headless: true, executablePath: CHROMIUM_EXECUTABLE_PATH });
const context = await browser.newContext({ viewport: { width: 1360, height: 900 } });
try {
const auth = await context.request.post(`${BASE_URL}/api/auth`, {
data: {
action: "auth:signIn",
args: {
provider: "password",
params: {
email: "mnote.e2e@example.com",
password: process.env.MNOTE_E2E_PASSWORD || "MnoteE2E123!",
flow: "signIn",
},
},
},
});
assert(auth.ok(), `登录失败: ${auth.status()} ${await auth.text()}`);
const page = await context.newPage();
const pageErrors = [];
const consoleErrors = [];
page.on("pageerror", (error) => pageErrors.push(String(error?.stack || error?.message || error)));
page.on("console", (message) => {
if (["error", "warning"].includes(message.type())) consoleErrors.push(`${message.type()}: ${message.text()}`);
});
await page.goto(`${BASE_URL}/`, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await page.evaluate(() => {
window.localStorage?.setItem("mnote.search.collapseSourcesDefault.v1", "0");
});
await page.waitForSelector('[data-mnote-action="open-search-modal"]', { timeout: UI_TIMEOUT_MS });
await openKnowledgeSearch(page, QUERY);
await waitForOfficeRows(page);
const beforeClose = await page.evaluate(() => ({
query: document.querySelector('[data-testid="wolai-search-input"]')?.value || "",
count: document.querySelectorAll('[data-testid="wolai-search-result-row"]').length,
meta: document.querySelector('[data-testid="wolai-search-result-meta"]')?.textContent || "",
}));
await page.click('[data-testid="wolai-search-close"]');
await page.click('[data-mnote-action="open-search-modal"]');
await page.waitForSelector('[data-testid="wolai-search-input"]', { timeout: UI_TIMEOUT_MS });
const afterReopen = await page.evaluate(() => ({
query: document.querySelector('[data-testid="wolai-search-input"]')?.value || "",
count: document.querySelectorAll('[data-testid="wolai-search-result-row"]').length,
meta: document.querySelector('[data-testid="wolai-search-result-meta"]')?.textContent || "",
}));
await page.screenshot({ path: SEARCH_SCREENSHOT_PATH, fullPage: true });
assert.equal(afterReopen.query, QUERY, `搜索面板未恢复 query: ${JSON.stringify({ beforeClose, afterReopen })}`);
assert(afterReopen.count >= beforeClose.count, `搜索面板重新打开后结果丢失: ${JSON.stringify({ beforeClose, afterReopen })}`);
const first = await pickOfficeResult(page);
assert(first, `没有找到可打开的 Office 搜索结果`);
await clickSearchResultByIndex(page, first.index);
await waitForOfficeLocator(page, first.blockId);
const firstState = await readOfficeState(page);
assert.equal(firstState.searchOpen, false, `点击搜索结果后搜索面板应关闭但保留状态: ${JSON.stringify(firstState, null, 2)}`);
await page.evaluate((expectedResource) => {
const panel = document.querySelector(`[data-mnote-resource-tab-panel][data-resource-path="${CSS.escape(expectedResource)}"]`);
const frame = panel?.querySelector("iframe.mnote-resource-tab-frame");
window.__mnoteTask541LoadCount = 0;
if (frame) frame.addEventListener("load", () => {
window.__mnoteTask541LoadCount = (window.__mnoteTask541LoadCount || 0) + 1;
});
}, EXPECTED_RESOURCE);
await page.click('[data-mnote-action="open-search-modal"]');
await waitForOfficeRows(page);
const second = await pickOfficeResult(page, first.blockId);
assert(second, `没有找到同一文件的第二个不同 block 搜索结果: ${JSON.stringify(first)}`);
await clickSearchResultByIndex(page, second.index);
await waitForOfficeLocator(page, second.blockId);
const secondState = await readOfficeState(page);
await page.screenshot({ path: OPEN_SCREENSHOT_PATH, fullPage: true });
assert.equal(secondState.loadCount, 0, `同一 Office 文件重定位触发了 iframe reload: ${JSON.stringify({ firstState, secondState }, null, 2)}`);
assert.equal(secondState.iframe?.applied, "true", `Office preview 未应用第二次定位: ${JSON.stringify(secondState, null, 2)}`);
const highlighted = `${secondState.iframe?.highlightedText || ""} ${secondState.iframe?.markerTargetText || ""}`;
assert(highlighted.includes(QUERY), `Office preview 高亮未包含搜索词: ${JSON.stringify(secondState, null, 2)}`);
assert((secondState.iframe?.markerTargetText || "").length <= 160, `Office preview range marker 文本过长: ${JSON.stringify(secondState, null, 2)}`);
assert((secondState.iframe?.markerHeight || 0) <= 140, `Office preview range marker 框选过高: ${JSON.stringify(secondState, null, 2)}`);
const result = {
ok: true,
baseUrl: BASE_URL,
query: QUERY,
expectedResource: EXPECTED_RESOURCE,
beforeClose,
afterReopen,
first,
second,
firstState,
secondState,
pageErrors,
consoleErrors,
screenshots: {
search: SEARCH_SCREENSHOT_PATH,
open: OPEN_SCREENSHOT_PATH,
},
};
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.log(JSON.stringify(result, null, 2));
} finally {
await browser.close().catch(() => undefined);
}
}
main().catch((error) => {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
fs.writeFileSync(
path.join(OUTPUT_DIR, "failure.json"),
`${JSON.stringify({ ok: false, error: error.stack || error.message || String(error) }, null, 2)}\n`,
"utf8",
);
console.error(error.stack || error.message || String(error));
process.exit(1);
});
@@ -0,0 +1,231 @@
#!/usr/bin/env node
"use strict";
const assert = require("node:assert");
const fs = require("node:fs");
const path = require("node:path");
const { chromium } = require("playwright");
const BASE_URL = (process.env.MNOTE_WEB_SMOKE_BASE_URL || "http://127.0.0.1:3000").replace(/\/+$/, "");
const ROOT_URI = process.env.MNOTE_KNOWLEDGE_RAG_ROOT_URI || "file:///mnt/Data1T/Mnote_data/users/mnote-e2e/workspaces/my-space";
const WORKSPACE_ID = process.env.MNOTE_KNOWLEDGE_RAG_WORKSPACE_ID || "local-ws:mnote-e2e:my-space";
const GROUP_QUERY = process.env.MNOTE_KNOWLEDGE_RAG_GROUP_QUERY || "三甲基硅";
const SHORT_QUERY = process.env.MNOTE_KNOWLEDGE_RAG_SHORT_QUERY || "吗啉";
const TOO_SHORT_QUERY = process.env.MNOTE_KNOWLEDGE_RAG_TOO_SHORT_QUERY || "吗";
const OUTPUT_DIR = path.join(process.cwd(), "tmp", "task542-knowledge-rag-search-grouping-and-short-query-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
const GROUP_SCREENSHOT_PATH = path.join(OUTPUT_DIR, "grouped-collapsed.png");
const EXPANDED_SCREENSHOT_PATH = path.join(OUTPUT_DIR, "grouped-expanded.png");
const UI_TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_UI_TIMEOUT_MS || 30_000);
const CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH
|| ["/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/snap/bin/chromium"]
.find((candidate) => fs.existsSync(candidate));
async function main() {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const browser = await chromium.launch({ headless: true, executablePath: CHROMIUM_EXECUTABLE_PATH });
const context = await browser.newContext({ viewport: { width: 1360, height: 900 } });
try {
const auth = await context.request.post(`${BASE_URL}/api/auth`, {
data: {
action: "auth:signIn",
args: {
provider: "password",
params: {
email: "mnote.e2e@example.com",
password: process.env.MNOTE_E2E_PASSWORD || "MnoteE2E123!",
flow: "signIn",
},
},
},
});
assert(auth.ok(), `登录失败: ${auth.status()} ${await auth.text()}`);
const localSearch = await context.request.post(`${BASE_URL}/api/search/documents`, {
data: {
workspaceId: WORKSPACE_ID,
sourceKind: "local_folder",
rootUri: ROOT_URI,
query: SHORT_QUERY,
limit: 30,
filters: {
includeOcr: false,
titleOnly: false,
exact: false,
onlyCurrentPage: false,
timeRange: "any",
timeField: "updated",
},
},
});
assert(localSearch.ok(), `本地搜索失败: ${localSearch.status()} ${await localSearch.text()}`);
const localPayload = await localSearch.json();
assert.equal(localPayload.results?.length || 0, 0, `本地文档搜索不应把两字 CJK query 匹配到无关文档: ${JSON.stringify(localPayload.results?.slice(0, 3), null, 2)}`);
const knowledgeSearch = await context.request.post(`${BASE_URL}/api/knowledge-rag/search`, {
data: {
workspaceId: WORKSPACE_ID,
rootUri: ROOT_URI,
query: SHORT_QUERY,
mode: "mix",
topK: 12,
chunkTopK: 24,
includeChunkContent: true,
},
});
assert(knowledgeSearch.ok(), `资料库短 query 检索失败: ${knowledgeSearch.status()} ${await knowledgeSearch.text()}`);
const knowledgePayload = await knowledgeSearch.json();
assert((knowledgePayload.results?.length || 0) >= 8, `资料库 2 字 query 应返回段落去重后的 LightRAG 检索命中: ${JSON.stringify(knowledgePayload, null, 2).slice(0, 2000)}`);
assert(String(knowledgePayload.results?.[0]?.snippet || "").includes(SHORT_QUERY), `资料库结果未包含 ${SHORT_QUERY}: ${JSON.stringify(knowledgePayload.results?.[0], null, 2)}`);
assert(knowledgePayload.references?.some((reference) => reference.matchSource === "lightrag_search"), `2 字 query 应走 LightRAG search provider: ${JSON.stringify(knowledgePayload.references?.slice(0, 3), null, 2)}`);
const tooShortSearch = await context.request.post(`${BASE_URL}/api/knowledge-rag/search`, {
data: {
workspaceId: WORKSPACE_ID,
rootUri: ROOT_URI,
query: TOO_SHORT_QUERY,
mode: "mix",
topK: 12,
chunkTopK: 24,
includeChunkContent: true,
},
});
assert.equal(tooShortSearch.status(), 400, `资料库 1 字 query 应返回 400: ${tooShortSearch.status()} ${await tooShortSearch.text()}`);
const tooShortPayload = await tooShortSearch.json();
assert.equal(tooShortPayload.code, "knowledge_rag_search_query_too_short", `1 字 query 错误码不符合预期: ${JSON.stringify(tooShortPayload)}`);
assert(String(tooShortPayload.message || "").includes("至少 2 个字"), `1 字 query 提示不符合预期: ${JSON.stringify(tooShortPayload)}`);
const page = await context.newPage();
const pageErrors = [];
const consoleErrors = [];
page.on("pageerror", (error) => pageErrors.push(String(error?.stack || error?.message || error)));
page.on("console", (message) => {
if (["error", "warning"].includes(message.type())) consoleErrors.push(`${message.type()}: ${message.text()}`);
});
await page.goto(`${BASE_URL}/`, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await page.evaluate(() => {
window.localStorage?.setItem("mnote.search.collapseSourcesDefault.v1", "1");
});
await page.waitForSelector('[data-mnote-action="open-search-modal"]', { timeout: UI_TIMEOUT_MS });
await page.click('[data-mnote-action="open-search-modal"]');
await page.waitForSelector('[data-testid="wolai-search-input"]', { timeout: UI_TIMEOUT_MS });
await page.evaluate(() => {
const knowledge = document.querySelector('[data-search-switch="knowledge"]');
if (knowledge instanceof HTMLElement) {
knowledge.setAttribute("aria-checked", "true");
knowledge.classList.add("is-on");
}
const collapse = document.querySelector('[data-search-switch="collapseSource"]');
if (collapse instanceof HTMLElement) {
collapse.setAttribute("aria-checked", "true");
collapse.classList.add("is-on");
}
});
await page.fill('[data-testid="wolai-search-input"]', TOO_SHORT_QUERY);
await page.evaluate(() => {
document.querySelector('[data-testid="wolai-search-input"]')
?.dispatchEvent(new Event("input", { bubbles: true }));
});
await page.waitForSelector('[data-search-empty="true"]', { timeout: UI_TIMEOUT_MS });
const tooShortUi = await page.evaluate(() => ({
meta: document.querySelector('[data-testid="wolai-search-result-meta"]')?.textContent || "",
empty: document.querySelector('[data-search-empty="true"]')?.textContent || "",
rows: document.querySelectorAll('[data-testid="wolai-search-result-row"]').length,
}));
assert(tooShortUi.empty.includes("至少 2 个字"), `1 字 query UI 未提示至少 2 个字: ${JSON.stringify(tooShortUi)}`);
assert.equal(tooShortUi.rows, 0, `1 字 query 不应发起并渲染结果: ${JSON.stringify(tooShortUi)}`);
await page.fill('[data-testid="wolai-search-input"]', GROUP_QUERY);
await page.evaluate(() => {
document.querySelector('[data-testid="wolai-search-input"]')
?.dispatchEvent(new Event("input", { bubbles: true }));
});
await page.waitForSelector(".wolai-search-source-group", { timeout: UI_TIMEOUT_MS });
const collapsed = await page.evaluate(() => ({
switchOn: document.querySelector('[data-search-switch="collapseSource"]')?.getAttribute("aria-checked") === "true",
groups: document.querySelectorAll(".wolai-search-source-group").length,
visibleRows: Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'))
.filter((row) => row.offsetParent !== null).length,
meta: document.querySelector('[data-testid="wolai-search-result-meta"]')?.textContent || "",
firstHeader: document.querySelector(".wolai-search-source-header")?.textContent || "",
}));
await page.screenshot({ path: GROUP_SCREENSHOT_PATH, fullPage: true });
assert.equal(collapsed.switchOn, true, `折叠同来源开关未默认打开: ${JSON.stringify(collapsed)}`);
assert(collapsed.groups >= 1, `未按来源分组: ${JSON.stringify(collapsed)}`);
assert.equal(collapsed.visibleRows, 0, `默认折叠时不应展示重复来源子结果: ${JSON.stringify(collapsed)}`);
assert(
!String(collapsed.firstHeader || "").includes("有机合成中的保护基/[OCR]"),
`来源组标题不应再在标题下重复显示完整来源路径: ${JSON.stringify(collapsed)}`
);
await page.click(".wolai-search-source-header");
await page.waitForFunction(() => {
return Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'))
.some((row) => row.offsetParent !== null);
}, null, { timeout: UI_TIMEOUT_MS });
const expanded = await page.evaluate(() => ({
expanded: document.querySelector(".wolai-search-source-header")?.getAttribute("aria-expanded") === "true",
visibleRows: Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'))
.filter((row) => row.offsetParent !== null).length,
firstRows: Array.from(document.querySelectorAll('[data-testid="wolai-search-result-row"]'))
.slice(0, 3)
.map((row) => row.textContent),
}));
await page.screenshot({ path: EXPANDED_SCREENSHOT_PATH, fullPage: true });
assert.equal(expanded.expanded, true, `来源组未展开: ${JSON.stringify(expanded)}`);
assert(expanded.visibleRows > 0, `展开后未展示来源下结果: ${JSON.stringify(expanded)}`);
assert(
expanded.firstRows.every((text) => !String(text || "").includes("有机合成中的保护基/[OCR]")),
`折叠同来源展开后,子结果不应重复显示完整来源路径: ${JSON.stringify(expanded.firstRows)}`
);
assert(
expanded.firstRows.every((text) => !String(text || "").trim().endsWith("docx")),
`折叠同来源展开后,子结果不应重复显示来源类型: ${JSON.stringify(expanded.firstRows)}`
);
assert(
expanded.firstRows.every((text) => !/(?:<\/?e(?:q(?:uation)?)?\b|<\/?drawing\b|format=["']?latex|\blatex\b)/i.test(String(text || ""))),
`资料库搜索结果仍暴露原始公式/绘图标记: ${JSON.stringify(expanded.firstRows)}`
);
const result = {
ok: true,
baseUrl: BASE_URL,
rootUri: ROOT_URI,
workspaceId: WORKSPACE_ID,
shortQuery: SHORT_QUERY,
tooShortQuery: TOO_SHORT_QUERY,
groupQuery: GROUP_QUERY,
localCount: localPayload.results?.length || 0,
knowledgeCount: knowledgePayload.results?.length || 0,
knowledgeFirst: {
title: knowledgePayload.results?.[0]?.title,
snippet: knowledgePayload.results?.[0]?.snippet,
blockId: knowledgePayload.results?.[0]?.locator?.blockId,
},
tooShortUi,
collapsed,
expanded,
pageErrors,
consoleErrors,
screenshots: {
collapsed: GROUP_SCREENSHOT_PATH,
expanded: EXPANDED_SCREENSHOT_PATH,
},
};
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.log(JSON.stringify(result, null, 2));
} finally {
await browser.close().catch(() => undefined);
}
}
main().catch((error) => {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
fs.writeFileSync(
path.join(OUTPUT_DIR, "failure.json"),
`${JSON.stringify({ ok: false, error: error.stack || error.message || String(error) }, null, 2)}\n`,
"utf8",
);
console.error(error.stack || error.message || String(error));
process.exit(1);
});
@@ -0,0 +1,253 @@
#!/usr/bin/env node
"use strict";
const assert = require("node:assert");
const fs = require("node:fs");
const path = require("node:path");
const { chromium } = require("playwright");
const BASE_URL = (process.env.MNOTE_WEB_SMOKE_BASE_URL || "http://127.0.0.1:3000").replace(/\/+$/, "");
const ROOT_URI = process.env.MNOTE_KNOWLEDGE_RAG_ROOT_URI || "file:///mnt/Data1T/Mnote_data/users/mnote-e2e/workspaces/my-space";
const WORKSPACE_ID = process.env.MNOTE_KNOWLEDGE_RAG_WORKSPACE_ID || "local-ws:mnote-e2e:my-space";
const QUERY = process.env.MNOTE_KNOWLEDGE_RAG_PYRROLIDINE_QUERY || "吡咯烷";
const EXPECTED_RESOURCE = process.env.MNOTE_KNOWLEDGE_RAG_EXPECTED_RESOURCE
|| "有机合成中的保护基/[OCR]_有机合成中的保护基-酚羰基羧基巯基的保护_20250201_1908.layered_删减-2025-02-04 18-59-42.docx";
const TOP_N = Number(process.env.MNOTE_KNOWLEDGE_RAG_TOP_N || 5);
const OUTPUT_DIR = path.join(process.cwd(), "tmp", "task543-knowledge-rag-pyrrolidine-top5-locator-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
const UI_TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_UI_TIMEOUT_MS || 30_000);
const CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH
|| ["/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/snap/bin/chromium"]
.find((candidate) => fs.existsSync(candidate));
function normalizeText(value) {
return String(value || "")
.replace(/<[^>]+>/g, " ")
.replace(/[#*_`~>\[\](){}]+/g, " ")
.replace(/[\u200B-\u200D\uFEFF]/g, "")
.replace(/\s+/g, " ")
.trim();
}
function compactText(value) {
return normalizeText(value).replace(/[0-9-]+/g, "").replace(/[\s\p{P}\p{S}]+/gu, "");
}
function longestCommonSubstringLength(left, right) {
const a = compactText(left);
const b = compactText(right);
if (!a || !b) return 0;
const shorter = a.length <= b.length ? a : b;
const longer = a.length <= b.length ? b : a;
for (let len = Math.min(80, shorter.length); len >= 6; len -= 1) {
for (let start = 0; start + len <= shorter.length; start += 1) {
if (longer.includes(shorter.slice(start, start + len))) return len;
}
}
return 0;
}
async function signIn(context) {
const auth = await context.request.post(`${BASE_URL}/api/auth`, {
data: {
action: "auth:signIn",
args: {
provider: "password",
params: {
email: "mnote.e2e@example.com",
password: process.env.MNOTE_E2E_PASSWORD || "MnoteE2E123!",
flow: "signIn",
},
},
},
});
assert(auth.ok(), `登录失败: ${auth.status()} ${await auth.text()}`);
}
async function apiSearch(context) {
const response = await context.request.post(`${BASE_URL}/api/knowledge-rag/search`, {
data: {
rootUri: ROOT_URI,
workspaceId: WORKSPACE_ID,
query: QUERY,
mode: "mix",
topK: 20,
chunkTopK: 20,
includeChunkContent: true,
sourcePaths: [EXPECTED_RESOURCE],
},
});
assert(response.ok(), `knowledge-rag search 失败: ${response.status()} ${await response.text()}`);
const payload = await response.json();
const results = Array.isArray(payload.results) ? payload.results : [];
assert(results.length >= TOP_N, `资料库结果不足 ${TOP_N} 条: ${JSON.stringify(payload, null, 2).slice(0, 3000)}`);
const blockIds = results.map((item) => item?.locator?.blockId).filter(Boolean);
assert.equal(new Set(blockIds).size, blockIds.length, `搜索结果仍有同段落重复: ${JSON.stringify(blockIds)}`);
return results.slice(0, TOP_N);
}
async function openSearchPanel(page, navigate = false) {
if (navigate) {
await page.goto(`${BASE_URL}/`, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await page.evaluate(() => {
window.localStorage?.setItem("mnote.search.collapseSourcesDefault.v1", "0");
});
}
const inputVisible = await page.locator('[data-testid="wolai-search-input"]').isVisible().catch(() => false);
if (!inputVisible) {
await page.waitForSelector('[data-mnote-action="open-search-modal"]', { timeout: UI_TIMEOUT_MS });
await page.click('[data-mnote-action="open-search-modal"]');
}
await page.waitForSelector('[data-testid="wolai-search-input"]', { timeout: UI_TIMEOUT_MS });
await page.fill('[data-testid="wolai-search-input"]', QUERY);
await page.evaluate(() => {
const button = document.querySelector('[data-search-switch="knowledge"]');
if (button instanceof HTMLElement) {
button.setAttribute("aria-checked", "true");
button.classList.add("is-on");
}
document.querySelector('[data-testid="wolai-search-input"]')
?.dispatchEvent(new Event("input", { bubbles: true }));
});
await page.waitForFunction(
(topN) => document.querySelectorAll('[data-testid="wolai-search-result-row"]').length >= topN,
TOP_N,
{ timeout: UI_TIMEOUT_MS },
);
}
async function clickAndAuditRow(page, index, apiResult) {
const rows = await page.$$('[data-testid="wolai-search-result-row"]');
assert(rows[index], `缺少第 ${index + 1} 条搜索结果`);
const rowText = await rows[index].evaluate((node) => node.textContent || "");
assert(
!/(?:<\/?equation\b|format=["']?latex|<\/?drawing\b|\blatex\b)/i.test(rowText),
`${index + 1} 条搜索结果仍暴露原始公式/绘图标记: ${rowText}`,
);
const locator = await rows[index].evaluate((node) => {
const raw = node.getAttribute("data-evidence-locator") || "";
try {
return raw ? JSON.parse(raw) : null;
} catch (_) {
return null;
}
});
assert(locator?.blockId, `${index + 1} 条缺少 locator blockId: ${rowText}`);
assert.equal(locator.blockId, apiResult?.locator?.blockId, `${index + 1} 条 UI/API blockId 不一致`);
await rows[index].click();
await page.waitForFunction(
({ expectedResource, expectedBlockId }) => {
const panel = document.querySelector(`[data-mnote-resource-tab-panel][data-resource-path="${CSS.escape(expectedResource)}"]`);
return panel && !panel.hidden && panel.getAttribute("data-mnote-evidence-block-id") === expectedBlockId;
},
{ expectedResource: EXPECTED_RESOURCE, expectedBlockId: locator.blockId },
{ timeout: UI_TIMEOUT_MS },
);
await page.waitForTimeout(1200);
await page.waitForFunction(
(expectedResource) => {
const panel = document.querySelector(`[data-mnote-resource-tab-panel][data-resource-path="${CSS.escape(expectedResource)}"]`);
const frame = panel?.querySelector("iframe.mnote-resource-tab-frame");
const doc = frame?.contentDocument;
return doc?.documentElement?.getAttribute("data-mnote-office-evidence-applied") === "true";
},
EXPECTED_RESOURCE,
{ timeout: UI_TIMEOUT_MS },
);
const state = await page.evaluate((expectedResource) => {
const panel = document.querySelector(`[data-mnote-resource-tab-panel][data-resource-path="${CSS.escape(expectedResource)}"]`);
const frame = panel?.querySelector("iframe.mnote-resource-tab-frame");
const doc = frame?.contentDocument;
const highlighted = doc?.querySelector('[data-mnote-office-evidence-target="true"]');
return {
panelBlockId: panel?.getAttribute("data-mnote-evidence-block-id") || "",
panelEvidenceText: panel?.getAttribute("data-mnote-evidence-text") || "",
iframeApplied: doc?.documentElement?.getAttribute("data-mnote-office-evidence-applied") || "",
highlightedText: highlighted?.textContent || "",
highlightedTag: highlighted?.tagName || "",
highlightedTop: highlighted ? Math.round(highlighted.getBoundingClientRect().top) : null,
};
}, EXPECTED_RESOURCE);
const evidenceText = locator?.openAction?.params?.evidenceText || apiResult?.locator?.openAction?.params?.evidenceText || apiResult?.quote || rowText;
const overlap = longestCommonSubstringLength(state.highlightedText, evidenceText);
assert.equal(state.panelBlockId, locator.blockId, `${index + 1} 条 panel blockId 不一致: ${JSON.stringify(state, null, 2)}`);
assert(state.highlightedText.includes(QUERY), `${index + 1} 条定位高亮未包含 query: ${JSON.stringify(state, null, 2)}`);
assert(
normalizeText(state.highlightedText).length >= 24 || state.highlightedTag === "P",
`${index + 1} 条仍是短词高亮,不是段落级高亮: ${JSON.stringify(state, null, 2)}`,
);
assert(
overlap >= 10 || (
state.highlightedTag === "P"
&& compactText(state.highlightedText).includes(compactText(QUERY))
&& compactText(state.highlightedText).length >= 6
),
`${index + 1} 条搜索结果与实际定位上下文不一致: ${JSON.stringify({ rowText, evidenceText, overlap, state }, null, 2)}`,
);
return {
index: index + 1,
rowText: normalizeText(rowText).slice(0, 220),
apiBlockId: apiResult?.locator?.blockId || "",
uiBlockId: locator.blockId,
panelBlockId: state.panelBlockId,
highlightedText: normalizeText(state.highlightedText).slice(0, 220),
highlightedTag: state.highlightedTag,
overlap,
};
}
async function main() {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const browser = await chromium.launch({ headless: true, executablePath: CHROMIUM_EXECUTABLE_PATH });
const context = await browser.newContext({ viewport: { width: 1360, height: 900 } });
try {
await signIn(context);
const topResults = await apiSearch(context);
const page = await context.newPage();
const pageErrors = [];
const consoleErrors = [];
page.on("pageerror", (error) => pageErrors.push(String(error?.stack || error?.message || error)));
page.on("console", (message) => {
if (["error", "warning"].includes(message.type())) consoleErrors.push(`${message.type()}: ${message.text()}`);
});
await openSearchPanel(page, true);
const audits = [];
for (let index = 0; index < TOP_N; index += 1) {
await openSearchPanel(page, false);
audits.push(await clickAndAuditRow(page, index, topResults[index]));
}
await page.screenshot({ path: path.join(OUTPUT_DIR, "top5-after-last-click.png"), fullPage: true });
const result = {
ok: true,
baseUrl: BASE_URL,
rootUri: ROOT_URI,
workspaceId: WORKSPACE_ID,
query: QUERY,
topN: TOP_N,
audits,
pageErrors,
consoleErrors,
};
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.log(JSON.stringify(result, null, 2));
} finally {
await browser.close().catch(() => undefined);
}
}
main().catch((error) => {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
fs.writeFileSync(
path.join(OUTPUT_DIR, "failure.json"),
`${JSON.stringify({ ok: false, error: error.stack || error.message || String(error) }, null, 2)}\n`,
"utf8",
);
console.error(error.stack || error.message || String(error));
process.exit(1);
});