RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
feat: locate Excel citation sources in document preview (#18853)
天海蒼灆61298397f691
diff --git a/deepdoc/parser/excel_parser.py b/deepdoc/parser/excel_parser.pyindex bc9edf57c2..eef73fb97e 100644--- a/deepdoc/parser/excel_parser.py+++ b/deepdoc/parser/excel_parser.py@@ -213,7 +213,7 @@ class RAGFlowExcelParser:return ""return str(v).strip()- for sheetname in wb.sheetnames:+ for sheet_idx, sheetname in enumerate(wb.sheetnames):ws = wb[sheetname]try:rows = RAGFlowExcelParser._get_rows_limited(ws)@@ -228,6 +228,7 @@ class RAGFlowExcelParser:for t in list(rows[0]):tb_rows_0 += f"<th>{escape(_fmt(t.value))}</th>"tb_rows_0 += "</tr>"+ col_max = max((len(r) for r in rows), default=1)# rows[0] is the header; split the remaining data rows into# ceil(n_data / chunk_rows) chunks. Using +1 here over-counts by one@@ -235,10 +236,12 @@ class RAGFlowExcelParser:# a spurious header-only chunk.n_data_rows = len(rows) - 1for chunk_i in range((n_data_rows + chunk_rows - 1) // chunk_rows):+ row_start = 2 + chunk_i * chunk_rows+ row_end = min(1 + (chunk_i + 1) * chunk_rows, len(rows))tb = ""tb += f"<table><caption>{sheetname}</caption>"tb += tb_rows_0- for r in list(rows[1 + chunk_i * chunk_rows : min(1 + (chunk_i + 1) * chunk_rows, len(rows))]):+ for r in list(rows[1 + chunk_i * chunk_rows : row_end]):tb += "<tr>"for i, c in enumerate(r):if c.value is None:@@ -247,7 +250,9 @@ class RAGFlowExcelParser:tb += f"<td>{escape(_fmt(c.value))}</td>"tb += "</tr>"tb += "</table>\n"- tb_chunks.append(tb)+ # position: (sheet_idx 0-based, row_start, row_end, col_start, col_end)+ # for add_positions which increments the first component to 1-based.+ tb_chunks.append((tb, (sheet_idx, row_start, row_end, 1, col_max)))return tb_chunks@@ -270,7 +275,7 @@ class RAGFlowExcelParser:wb = RAGFlowExcelParser._load_excel_to_workbook(file_like_object)res = []- for sheetname in wb.sheetnames:+ for sheet_idx, sheetname in enumerate(wb.sheetnames):ws = wb[sheetname]try:rows = RAGFlowExcelParser._get_rows_limited(ws)@@ -280,11 +285,15 @@ class RAGFlowExcelParser:if not rows:continueti = list(rows[0])- for r in list(rows[1:]):+ for row_idx, r in enumerate(list(rows[1:]), start=2):fields = []+ col_min, col_max = None, Nonefor i, c in enumerate(r):if c.value is None or str(c.value).strip() == "":continue+ col = i + 1+ col_min = col if col_min is None else min(col_min, col)+ col_max = col if col_max is None else max(col_max, col)t = str(ti[i].value) if i < len(ti) else ""t += (":" if t else "") + str(c.value)fields.append(t)@@ -293,7 +302,7 @@ class RAGFlowExcelParser:line = "; ".join(fields)if sheetname.lower().find("sheet") < 0:line += " ——" + sheetname- res.append(line)+ res.append((line, (sheet_idx, row_idx, row_idx, col_min or 1, col_max or 1)))return res@staticmethoddiff --git a/internal/parser/parser/office_table_render.go b/internal/parser/parser/office_table_render.goindex bc8153c05d..3e63539362 100644--- a/internal/parser/parser/office_table_render.go+++ b/internal/parser/parser/office_table_render.go@@ -151,6 +151,16 @@ func buildHeaderRow(row []string) string {return b.String()}+// htmlTableChunk is one self-contained <table> chunk with 1-based sheet/row/col+// coordinates for source location (mirrors Excel position_int semantics).+type htmlTableChunk struct {+ HTML string+ RowStart int // inclusive, 1-based Excel row of first data row (header-only chunks use headerRowAbs)+ RowEnd int // inclusive, 1-based Excel row of last data row+ ColStart int+ ColEnd int+}+// recordsToHTMLTableChunks renders records as one or more self-contained HTML// <table> chunks. The first row is always the header (<th>). Data rows are// split into chunks of chunkRows, each chunk being a complete <table> with@@ -161,22 +171,48 @@ func buildHeaderRow(row []string) string {// <thead>/<tbody>, so every <table> is one atomic chunk that downstream// chunkers can consume independently.func recordsToHTMLTableChunks(records [][]string, chunkRows int, caption string) string {- return strings.Join(recordsToHTMLTableChunkList(records, chunkRows, caption), "")+ chunks := recordsToHTMLTableChunkList(records, chunkRows, caption, 1)+ parts := make([]string, len(chunks))+ for i, ch := range chunks {+ parts[i] = ch.HTML+ }+ return strings.Join(parts, "")}-func recordsToHTMLTableChunkList(records [][]string, chunkRows int, caption string) []string {+// recordsToHTMLTableChunkList is the structured form of recordsToHTMLTableChunks.+// headerRowAbs is the 1-based workbook row number of records[0] (normally 1).+func recordsToHTMLTableChunkList(records [][]string, chunkRows int, caption string, headerRowAbs int) []htmlTableChunk {+ if headerRowAbs <= 0 {+ headerRowAbs = 1+ }+ colEnd := 1+ for _, row := range records {+ if n := len(row); n > colEnd {+ colEnd = n+ }+ }if len(records) == 0 {- return []string{"<table><caption>" + html.EscapeString(caption) + "</caption></table>"}+ return []htmlTableChunk{{+ HTML: "<table><caption>" + html.EscapeString(caption) + "</caption></table>",+ RowStart: headerRowAbs,+ RowEnd: headerRowAbs,+ ColStart: 1,+ ColEnd: colEnd,+ }}}- // Build the header row once — repeated in every chunk.headerHTML := buildHeaderRow(records[0])dataRows := records[1:]nData := len(dataRows)if nData == 0 {- // Only a header row exists.- return []string{"<table><caption>" + html.EscapeString(caption) + "</caption>\n" + headerHTML + "</table>"}+ return []htmlTableChunk{{+ HTML: "<table><caption>" + html.EscapeString(caption) + "</caption>\n" + headerHTML + "</table>",+ RowStart: headerRowAbs,+ RowEnd: headerRowAbs,+ ColStart: 1,+ ColEnd: colEnd,+ }}}if chunkRows <= 0 {@@ -184,7 +220,7 @@ func recordsToHTMLTableChunkList(records [][]string, chunkRows int, caption stri}nChunks := (nData + chunkRows - 1) / chunkRows- chunks := make([]string, 0, nChunks)+ out := make([]htmlTableChunk, 0, nChunks)for ci := 0; ci < nChunks; ci++ {start := ci * chunkRowsend := start + chunkRows@@ -208,9 +244,16 @@ func recordsToHTMLTableChunkList(records [][]string, chunkRows int, caption strib.WriteString("</tr>\n")}b.WriteString("</table>\n")- chunks = append(chunks, b.String())++ out = append(out, htmlTableChunk{+ HTML: b.String(),+ RowStart: headerRowAbs + start + 1,+ RowEnd: headerRowAbs + end,+ ColStart: 1,+ ColEnd: colEnd,+ })}- return chunks+ return out}// ──────────────────────────────────────────────────────────── axis helpers@@ -597,10 +640,15 @@ func decodeChunkRows(setup map[string]any) int {// data into chunkRows-sized atomic tables each repeating the header. An empty// or unreadable sheet yields an empty string.func renderSheetTables(f *excelize.File, sheet string, chunkRows int) string {- return strings.Join(renderSheetTableChunks(f, sheet, chunkRows), "")+ chunks := renderSheetTableChunks(f, sheet, chunkRows)+ parts := make([]string, len(chunks))+ for i, ch := range chunks {+ parts[i] = ch.HTML+ }+ return strings.Join(parts, "")}-func renderSheetTableChunks(f *excelize.File, sheet string, chunkRows int) []string {+func renderSheetTableChunks(f *excelize.File, sheet string, chunkRows int) []htmlTableChunk {rows, err := f.GetRows(sheet)if err != nil || len(rows) == 0 {return nil@@ -627,11 +675,32 @@ func renderSheetTableChunks(f *excelize.File, sheet string, chunkRows int) []str// data. For the common case (header on row 1) this is a no-op.records := make([][]string, 0, len(rows))records = append(records, rows[headerRow-1])+ absDataRows := make([]int, 0, len(rows)-1)for i, r := range rows {if i == headerRow-1 {continue}records = append(records, r)+ absDataRows = append(absDataRows, i+1)}- return recordsToHTMLTableChunkList(records, chunkRows, sheet)++ chunks := recordsToHTMLTableChunkList(records, chunkRows, sheet, headerRow)+ if len(chunks) == 0 || len(absDataRows) == 0 {+ return chunks+ }+ if chunkRows <= 0 {+ chunkRows = defaultTableChunkRows+ }+ for ci := range chunks {+ start := ci * chunkRows+ end := start + chunkRows+ if end > len(absDataRows) {+ end = len(absDataRows)+ }+ if start < end {+ chunks[ci].RowStart = absDataRows[start]+ chunks[ci].RowEnd = absDataRows[end-1]+ }+ }+ return chunks}diff --git a/internal/parser/parser/office_table_render_test.go b/internal/parser/parser/office_table_render_test.goindex a9b0c8c3b8..956afb5e62 100644--- a/internal/parser/parser/office_table_render_test.go+++ b/internal/parser/parser/office_table_render_test.go@@ -147,6 +147,38 @@ func TestRecordsToHTMLTableChunks_Chunking(t *testing.T) {}}+func TestRecordsToHTMLTableChunkList_RowRange(t *testing.T) {+ records := make([][]string, 0, 25)+ records = append(records, []string{"H"})+ for i := 0; i < 24; i++ {+ records = append(records, []string{"x"})+ }+ chunks := recordsToHTMLTableChunkList(records, 12, "S", 1)+ if len(chunks) != 2 {+ t.Fatalf("want 2 chunks, got %d", len(chunks))+ }+ if chunks[0].RowStart != 2 || chunks[0].RowEnd != 13 {+ t.Fatalf("chunk0 rows = %d-%d, want 2-13", chunks[0].RowStart, chunks[0].RowEnd)+ }+ if chunks[1].RowStart != 14 || chunks[1].RowEnd != 25 {+ t.Fatalf("chunk1 rows = %d-%d, want 14-25", chunks[1].RowStart, chunks[1].RowEnd)+ }+}++func TestRecordsToHTMLTableChunkList_ColEndUsesWidestRow(t *testing.T) {+ records := [][]string{+ {"H"},+ {"a", "b", "c"},+ }+ chunks := recordsToHTMLTableChunkList(records, 12, "S", 1)+ if len(chunks) != 1 {+ t.Fatalf("want 1 chunk, got %d", len(chunks))+ }+ if chunks[0].ColEnd != 3 {+ t.Fatalf("ColEnd = %d, want 3", chunks[0].ColEnd)+ }+}+// TestXLSXParser_HeaderAndCaption asserts the XLSX parser emits a <caption> and// renders the first row as <th>, and that the header text appears only in <th>.func TestXLSXParser_HeaderAndCaption(t *testing.T) {diff --git a/internal/parser/parser/xlsx_parser.go b/internal/parser/parser/xlsx_parser.goindex 08c29351a4..fa16b6c398 100644--- a/internal/parser/parser/xlsx_parser.go+++ b/internal/parser/parser/xlsx_parser.go@@ -127,13 +127,20 @@ func (p *XLSXParser) ParseWithResult(ctx context.Context, filename string, dataitems := make([]map[string]any, 0)warnings := make([]string, 0)- for _, sheet := range sheets {+ for sheetIdx, sheet := range sheets {for _, table := range renderSheetTableChunks(f, sheet, chunkRows) {items = append(items, map[string]any{- "text": table,+ "text": table.HTML,"doc_type_kwd": "table","ck_type": "table","sheet": sheet,+ "positions": [][]float64{{+ float64(sheetIdx + 1),+ float64(table.RowStart),+ float64(table.RowEnd),+ float64(table.ColStart),+ float64(table.ColEnd),+ }},})}images, imageWarnings := extractXLSXImages(f, sheet)diff --git a/rag/app/naive.py b/rag/app/naive.pyindex e59f2ba168..bce88e08e5 100644--- a/rag/app/naive.py+++ b/rag/app/naive.py@@ -58,6 +58,7 @@ from rag.nlp import (naive_merge_docx,rag_tokenizer,tokenize_chunks,+ tokenize_chunks_with_positions,doc_tokenize_chunks_with_images,tokenize_table,append_context2table_image4pdf,@@ -114,6 +115,53 @@ def _normalize_section_text_for_rtl_presentation_forms(sections):return normalized_sections+def _merge_excel_items(items, chunk_token_num=128):+ """Merge consecutive Excel rows within the same sheet by token budget.++ Each item is (text, (sheet_idx, row_start, row_end, col_start, col_end)).+ When chunk_token_num <= 0, items are returned unchanged (html4excel).+ """+ if not items:+ return []+ if chunk_token_num <= 0:+ return items++ merged = []+ cur_text = ""+ cur_pos = None+ cur_tokens = 0++ for text, pos in items:+ sheet_idx, r1, r2, c1, c2 = pos+ tok = num_tokens_from_string(text)+ same_sheet = cur_pos is not None and cur_pos[0] == sheet_idx+ if cur_text and (not same_sheet or cur_tokens + tok > chunk_token_num):+ merged.append((cur_text, cur_pos))+ cur_text = ""+ cur_pos = None+ cur_tokens = 0++ if not cur_text:+ cur_text = text+ cur_pos = (sheet_idx, r1, r2, c1, c2)+ cur_tokens = tok+ continue++ cur_text = cur_text + "\n" + text+ cur_pos = (+ sheet_idx,+ min(cur_pos[1], r1),+ max(cur_pos[2], r2),+ min(cur_pos[3], c1),+ max(cur_pos[4], c2),+ )+ cur_tokens += tok++ if cur_text:+ merged.append((cur_text, cur_pos))+ return merged++def by_deepdoc(filename, binary=None, from_page=0, to_page=MAXIMUM_PAGE_NUMBER, lang="Chinese", callback=None, pdf_cls=None, **kwargs):pdf_parser = pdf_cls() if pdf_cls else Pdf()sections, tables = pdf_parser(filename if binary is None else binary, from_page=from_page, to_page=to_page, callback=callback)@@ -1187,16 +1235,28 @@ def chunk(filename, binary=None, from_page=0, to_page=MAXIMUM_PAGE_NUMBER, lang=sections = _normalize_section_text_for_rtl_presentation_forms(sections)parser_config["chunk_token_num"] = 0res = tokenize_table(tables, doc, is_english, language=lang)+ sections = []callback(0.8, "Finish parsing.")else:# Default DeepDOC parserexcel_parser = ExcelParser()if parser_config.get("html4excel"):- sections = [(_, "") for _ in excel_parser.html(binary, 12) if _]+ excel_items = [item for item in excel_parser.html(binary, 12) if item and item[0]]parser_config["chunk_token_num"] = 0else:- sections = [(_, "") for _ in excel_parser(binary) if _]- sections = _normalize_section_text_for_rtl_presentation_forms(sections)+ excel_items = [item for item in excel_parser(binary) if item and item[0]]+ excel_items = [(normalize_arabic_presentation_forms(text), pos) for text, pos in excel_items]+ res.extend(+ tokenize_chunks_with_positions(+ _merge_excel_items(excel_items, int(parser_config.get("chunk_token_num", 128))),+ doc,+ is_english,+ child_delimiters_pattern=child_deli,+ language=lang,+ )+ )+ sections = []+ callback(0.8, "Finish parsing.")elif re.search(r"\.(txt|py|js|java|c|cpp|h|php|go|ts|sh|cs|kt|sql)$", filename, re.IGNORECASE):callback(0.1, "Start to parse.")diff --git a/rag/app/one.py b/rag/app/one.pyindex 1a08d2c9cb..7cde2485e8 100644--- a/rag/app/one.py+++ b/rag/app/one.py@@ -145,6 +145,9 @@ def chunk(filename, binary=None, from_page=0, to_page=MAXIMUM_PAGE_NUMBER, lang=callback(0.1, "Start to parse.")excel_parser = ExcelParser()sections = excel_parser.html(binary, MAXIMUM_TASK_PAGE_NUMBER)+ # One parser tokenizes the whole file as a single chunk, so per-cell+ # coordinates cannot be cited. Keep the HTML text only.+ sections = [s[0] if isinstance(s, tuple) else s for s in sections]elif re.search(r"\.(txt|md|markdown|mdx)$", filename, re.IGNORECASE):callback(0.1, "Start to parse.")diff --git a/rag/flow/parser/parser.py b/rag/flow/parser/parser.pyindex eda668727d..d8742083ed 100644--- a/rag/flow/parser/parser.py+++ b/rag/flow/parser/parser.py@@ -880,9 +880,22 @@ class Parser(ProcessBase):spreadsheet_parser = ExcelParser()if conf.get("output_format") == "html":htmls = spreadsheet_parser.html(blob, 1000000000)- self.set_output("html", htmls[0])+ self.set_output("html", htmls[0][0] if htmls else "")elif conf.get("output_format") == "json":- self.set_output("json", [{"text": txt, "doc_type_kwd": "text"} for txt in spreadsheet_parser(blob) if txt])+ self.set_output(+ "json",+ [+ {+ "text": txt,+ "doc_type_kwd": "text",+ # 0-based sheet. TaskExecutor and dataflow_service+ # call add_positions, which stores pn+1 (1-based).+ "positions": [[sheet, r1, r2, c1, c2]],+ }+ for txt, (sheet, r1, r2, c1, c2) in spreadsheet_parser(blob)+ if txt+ ],+ )elif conf.get("output_format") == "markdown":self.set_output("markdown", spreadsheet_parser.markdown(blob))diff --git a/rag/nlp/__init__.py b/rag/nlp/__init__.pyindex fe625b01fa..e6e48d7ce2 100644--- a/rag/nlp/__init__.py+++ b/rag/nlp/__init__.py@@ -451,6 +451,27 @@ def tokenize_chunks(chunks, doc, eng, pdf_parser=None, child_delimiters_pattern=return res+def tokenize_chunks_with_positions(chunks_with_pos, doc, eng, child_delimiters_pattern=None, language="English"):+ """Tokenize chunks that already carry real positions (Excel sheet/row).++ chunks_with_pos: iterable of (text, position) where position is a 5-tuple+ suitable for add_positions (first component 0-based).+ """+ res = []+ for ck, pos in chunks_with_pos:+ if not ck or not str(ck).strip():+ continue+ d = copy.deepcopy(doc)+ add_positions(d, [pos])+ if child_delimiters_pattern:+ d["mom_with_weight"] = ck.removeprefix("\n")+ res.extend(split_with_pattern(d, child_delimiters_pattern, ck, eng, language=language))+ continue+ tokenize(d, ck, eng, language=language)+ res.append(d)+ return res++def doc_tokenize_chunks_with_images(chunks, doc, eng, child_delimiters_pattern=None, batch_size=10, language="English"):res = []for ii, ck in enumerate(chunks):diff --git a/test/unit_test/deepdoc/parser/test_excel_parser.py b/test/unit_test/deepdoc/parser/test_excel_parser.pyindex 9e3a2aa281..b137779ca4 100644--- a/test/unit_test/deepdoc/parser/test_excel_parser.py+++ b/test/unit_test/deepdoc/parser/test_excel_parser.py@@ -73,7 +73,8 @@ def test_exact_multiple_does_not_emit_header_only_chunk():# 12 data rows with chunk_rows=12 (the value rag/app/naive.py uses).chunks = RAGFlowExcelParser().html(_make_xlsx(12), chunk_rows=12)assert len(chunks) == 1- assert all(not _chunk_has_no_data_cells(c) for c in chunks)+ assert all(not _chunk_has_no_data_cells(c[0]) for c in chunks)+ assert chunks[0][1][0] == 0 and chunks[0][1][1] == 2 and chunks[0][1][2] == 13@pytest.mark.p2@@ -81,7 +82,24 @@ def test_multiple_of_chunk_rows_splits_without_spurious_chunk():# 24 data rows with chunk_rows=12 -> exactly 2 data chunks, no trailing header-only chunk.chunks = RAGFlowExcelParser().html(_make_xlsx(24), chunk_rows=12)assert len(chunks) == 2- assert all(not _chunk_has_no_data_cells(c) for c in chunks)+ assert all(not _chunk_has_no_data_cells(c[0]) for c in chunks)+ assert chunks[0][1][1] == 2 and chunks[0][1][2] == 13+ assert chunks[1][1][1] == 14 and chunks[1][1][2] == 25+++def test_html_col_max_uses_widest_row():+ from openpyxl import Workbook++ wb = Workbook()+ ws = wb.active+ ws.append(["H1"])+ ws.append(["a", "b", "c"])+ buf = BytesIO()+ wb.save(buf)+ buf.seek(0)+ chunks = RAGFlowExcelParser().html(buf.read(), chunk_rows=12)+ assert chunks[0][1][3] == 1 and chunks[0][1][4] == 3@pytest.mark.p2@@ -89,7 +107,7 @@ def test_non_multiple_unchanged():# 13 data rows with chunk_rows=12 -> 2 chunks (12 + 1).chunks = RAGFlowExcelParser().html(_make_xlsx(13), chunk_rows=12)assert len(chunks) == 2- assert all(not _chunk_has_no_data_cells(c) for c in chunks)+ assert all(not _chunk_has_no_data_cells(c[0]) for c in chunks)def _make_xlsx_with_values(header, row):@@ -111,13 +129,42 @@ def test_call_keeps_zero_valued_cells():# is real data, not an empty cell, so it must survive. The header is only# emitted alongside a kept value, so a dropped 0 also loses its "stock" label.lines = RAGFlowExcelParser()(_make_xlsx_with_values(["name", "stock"], ["widget", 0]))- joined = " ".join(lines)+ joined = " ".join(text for text, _ in lines)assert "stock" in joined and "0" in joined, lines+ assert lines[0][1][1] == 2@pytest.mark.p2def test_call_skips_truly_empty_cells():# None / empty-string cells carry no value and should still be skipped.lines = RAGFlowExcelParser()(_make_xlsx_with_values(["name", "note"], ["widget", None]))- joined = " ".join(lines)+ joined = " ".join(text for text, _ in lines)assert "note" not in joined, lines+++def _make_two_sheet_xlsx():+ from openpyxl import Workbook++ wb = Workbook()+ ws1 = wb.active+ ws1.title = "s1"+ ws1.append(["H"])+ ws1.append(["a"])+ ws2 = wb.create_sheet("s2")+ ws2.append(["H"])+ ws2.append(["b"])+ buf = BytesIO()+ wb.save(buf)+ buf.seek(0)+ return buf.read()+++def test_call_emits_zero_based_sheet_index():+ # Flow JSON stores this index unchanged; add_positions later does pn+1 so+ # sheet 2 is stored as 2 and the preview selects xs.datas[1].+ lines = RAGFlowExcelParser()(_make_two_sheet_xlsx())+ sheets = {pos[0] for _, pos in lines}+ assert sheets == {0, 1}+ second = next(pos for _, pos in lines if pos[0] == 1)+ assert second == (1, 2, 2, 1, 1)diff --git a/test/unit_test/rag/nlp/test_tokenize_chunks.py b/test/unit_test/rag/nlp/test_tokenize_chunks.pyindex 594305d708..09b0fd4ab4 100644--- a/test/unit_test/rag/nlp/test_tokenize_chunks.py+++ b/test/unit_test/rag/nlp/test_tokenize_chunks.py@@ -57,3 +57,44 @@ def test_tokenize_chunks_with_images_removes_leading_newline_from_parent():assert [doc["mom_with_weight"] for doc in docs] == ["first\nsecond", "first\nsecond"]assert [doc["content_with_weight"] for doc in docs] == ["first\n", "second"]+++def test_tokenize_chunks_with_positions_increments_zero_based_sheet():+ # Flow JSON / naive Excel emit 0-based sheet; add_positions stores pn+1.+ docs = nlp.tokenize_chunks_with_positions(+ [("row", (1, 2, 4, 1, 3))],+ {"docnm_kwd": "a.xlsx"},+ True,+ )+ assert docs[0]["position_int"] == [(2, 2, 4, 1, 3)]+ assert docs[0]["page_num_int"] == [2]+ assert docs[0]["content_with_weight"] == "row"+++def test_tokenize_chunks_with_positions_skips_blank_and_splits_children():+ docs = nlp.tokenize_chunks_with_positions(+ [(" ", (0, 2, 2, 1, 1)), ("first\nsecond", (0, 3, 4, 1, 2))],+ {},+ True,+ child_delimiters_pattern="\n",+ )+ assert [doc["content_with_weight"] for doc in docs] == ["first\n", "second"]+ assert all(doc["position_int"][0] == (1, 3, 4, 1, 2) for doc in docs)+++def test_flow_json_sheet_is_one_based_after_add_positions():+ # rag/flow/parser/parser.py output_format=json stores 0-based sheet.+ # task_executor / dataflow_service then call add_positions (pn+1).+ json_out = [+ {"text": "s1", "positions": [[0, 2, 2, 1, 1]]},+ {"text": "s2", "positions": [[1, 2, 2, 1, 1]]},+ ]+ sheets = []+ for ck in json_out:+ nlp.add_positions(ck, ck["positions"])+ del ck["positions"]+ sheets.append(ck["position_int"][0][0])+ assert sheets == [1, 2]diff --git a/test/unit_test/rag/test_naive_dispatch.py b/test/unit_test/rag/test_naive_dispatch.pyindex dd4649d367..f7e0cac2f7 100644--- a/test/unit_test/rag/test_naive_dispatch.py+++ b/test/unit_test/rag/test_naive_dispatch.py@@ -221,6 +221,7 @@ def naive_module():naive_merge_with_images=lambda *a, **k: [],naive_merge_docx=lambda *a, **k: [],tokenize_chunks=lambda *a, **k: [],+ tokenize_chunks_with_positions=lambda *a, **k: [],doc_tokenize_chunks_with_images=lambda *a, **k: [],tokenize_chunks_with_images=lambda *a, **k: [],tokenize_table=lambda *a, **k: [],@@ -374,3 +375,29 @@ def test_dispatch_uses_resolved_layout_recognize_via_override_for_opendataloaderassert name == "opendataloader"assert parser is _ByOpenDataLoaderassert op_name == resolved+++def test_merge_excel_items_keeps_sheets_separate(naive_module):+ items = [+ ("a b", (0, 2, 2, 1, 2)),+ ("c d", (0, 3, 3, 1, 2)),+ ("e f", (1, 2, 2, 1, 3)),+ ]+ merged = naive_module._merge_excel_items(items, chunk_token_num=10)+ assert [pos for _, pos in merged] == [(0, 2, 3, 1, 2), (1, 2, 2, 1, 3)]+ assert merged[0][0] == "a b\nc d"+++def test_merge_excel_items_splits_on_token_budget(naive_module):+ items = [+ ("one two", (0, 2, 2, 1, 1)),+ ("three four", (0, 3, 3, 1, 1)),+ ]+ merged = naive_module._merge_excel_items(items, chunk_token_num=2)+ assert [pos for _, pos in merged] == [(0, 2, 2, 1, 1), (0, 3, 3, 1, 1)]+++def test_merge_excel_items_passthrough_when_budget_disabled(naive_module):+ items = [("a", (0, 2, 2, 1, 1)), ("b", (0, 3, 3, 1, 1))]+ assert naive_module._merge_excel_items(items, chunk_token_num=0) == items+ assert naive_module._merge_excel_items([], chunk_token_num=128) == []diff --git a/web/src/components/document-preview/excel-preview.tsx b/web/src/components/document-preview/excel-preview.tsxindex f336cf3403..038022fa9e 100644--- a/web/src/components/document-preview/excel-preview.tsx+++ b/web/src/components/document-preview/excel-preview.tsx@@ -14,21 +14,22 @@* limitations under the License.*/-// import { useFetchExcel } from '@/pages/document-viewer/hooks';import classNames from 'classnames';import { useFetchExcel } from './hooks';interface ExcelCsvPreviewerProps {className?: string;url: string;+ /** [sheetIndex(1-based), rowStart, rowEnd, colStart, colEnd] */+ positions?: number[][];}export const ExcelCsvPreviewer: React.FC<ExcelCsvPreviewerProps> = ({className,url,+ positions,}) => {- // const url = useGetDocumentUrl();- const { containerRef } = useFetchExcel(url);+ const { containerRef } = useFetchExcel(url, positions);return (<divdiff --git a/web/src/components/document-preview/hooks.test.ts b/web/src/components/document-preview/hooks.test.tsnew file mode 100644index 0000000000..a1c029d052--- /dev/null+++ b/web/src/components/document-preview/hooks.test.ts@@ -0,0 +1,132 @@+import { applyExcelSourceLocate } from './hooks';++jest.mock('@js-preview/excel', () => ({+ __esModule: true,+ default: { init: jest.fn() },+}));+jest.mock('ahooks', () => ({+ useDebounceFn: () => ({ run: jest.fn() }),+ useSize: () => ({ width: 800, height: 600 }),+}));+jest.mock('axios', () => ({ get: jest.fn() }));+jest.mock('jszip');+jest.mock('xlsx', () => ({ read: jest.fn(), write: jest.fn() }));+jest.mock('@/hooks/route-hook', () => ({+ useGetKnowledgeSearchParams: () => ({}),+}));+jest.mock('@/pages/dataflow-result/hooks', () => ({+ useGetPipelineResultSearchParams: () => ({}),+}));+jest.mock('@/utils/api', () => ({+ default: {},+ restAPIv1: '',+}));+jest.mock('@/utils/authorization-util', () => ({+ getAuthorization: () => '',+}));+jest.mock('@/constants/authorization', () => ({+ Authorization: 'Authorization',+}));++function mockPreviewer(sheetCount = 2) {+ const makeData = () => ({+ addStyle: jest.fn(() => 7),+ rows: {+ setCell: jest.fn(),+ getCellOrNew: jest.fn(),+ getHeight: jest.fn(() => 20),+ },+ scroll: { x: 0, y: 0 },+ });+ const datas = Array.from({ length: sheetCount }, makeData);+ const items = datas.map(() => document.createElement('div'));+ return {+ xs: {+ datas,+ reRender: jest.fn(),+ sheet: {+ resetData: jest.fn(),+ data: datas[0],+ reload: jest.fn(),+ table: { render: jest.fn() },+ },+ bottombar: {+ items,+ clickSwap2: jest.fn(),+ },+ },+ };+}++describe('applyExcelSourceLocate', () => {+ beforeEach(() => {+ jest.spyOn(window, 'getComputedStyle').mockReturnValue({+ getPropertyValue: (name: string) => {+ if (name === '--background-highlight') {+ return 'rgba(76, 164, 231, 0.1)';+ }+ if (name === '--text-title') {+ return 'rgba(22, 22, 24, 1)';+ }+ return '';+ },+ } as CSSStyleDeclaration);+ });++ afterEach(() => {+ jest.restoreAllMocks();+ });++ it('selects the 1-based sheet and paints the cell range with theme tokens', () => {+ const previewer = mockPreviewer();+ applyExcelSourceLocate(previewer as never, [2, 3, 4, 1, 2]);++ expect(previewer.xs.bottombar.clickSwap2).toHaveBeenCalledWith(+ previewer.xs.bottombar.items[1],+ );+ expect(previewer.xs.datas[1].addStyle).toHaveBeenCalledWith({+ bgcolor: 'rgba(76, 164, 231, 0.1)',+ color: 'rgba(22, 22, 24, 1)',+ });+ expect(previewer.xs.datas[1].rows.setCell).toHaveBeenCalledTimes(4);+ expect(previewer.xs.datas[1].rows.setCell).toHaveBeenCalledWith(+ 2,+ 0,+ { style: 7 },+ 'format',+ );+ expect(previewer.xs.datas[1].rows.setCell).toHaveBeenCalledWith(+ 3,+ 1,+ { style: 7 },+ 'format',+ );+ });++ it('is a no-op when the position tuple is short', () => {+ const previewer = mockPreviewer();+ applyExcelSourceLocate(previewer as never, [1, 2]);+ expect(previewer.xs.datas[0].addStyle).not.toHaveBeenCalled();+ });++ it('falls back to resetData when clickSwap2 is missing', () => {+ const previewer = mockPreviewer();+ delete previewer.xs.bottombar.clickSwap2;+ applyExcelSourceLocate(previewer as never, [1, 2, 2, 1, 1]);+ expect(previewer.xs.sheet.resetData).toHaveBeenCalledWith(+ previewer.xs.datas[0],+ );+ });++ it('uses hex fallbacks when theme tokens are empty', () => {+ jest.spyOn(window, 'getComputedStyle').mockReturnValue({+ getPropertyValue: () => ' ',+ } as CSSStyleDeclaration);+ const previewer = mockPreviewer();+ applyExcelSourceLocate(previewer as never, [1, 2, 2, 1, 1]);+ expect(previewer.xs.datas[0].addStyle).toHaveBeenCalledWith({+ bgcolor: '#ffe58f',+ color: '#1a1a1a',+ });+ });+});diff --git a/web/src/components/document-preview/hooks.ts b/web/src/components/document-preview/hooks.tsindex 3caa7b2d16..5a981f313c 100644--- a/web/src/components/document-preview/hooks.ts+++ b/web/src/components/document-preview/hooks.ts@@ -209,7 +209,117 @@ async function normalizeXlsxForExcelJS(data: ArrayBuffer): Promise<ArrayBuffer>}}-export const useFetchExcel = (filePath: string) => {+type ExcelLocatePos = number[];++/** x-spreadsheet Data instance used by @js-preview/excel (not a plain object). */+type XsData = {+ addStyle: (style: Record<string, unknown>) => number;+ rows: {+ setCell: (+ ri: number,+ ci: number,+ cell: { style?: number; text?: string },+ type?: 'all' | 'text' | 'format',+ ) => void;+ getCellOrNew: (+ ri: number,+ ci: number,+ ) => { style?: number; text?: string };+ getHeight?: (ri: number) => number;+ len?: number;+ };+ cols?: { len?: number };+ scroll?: { x: number; y: number };+};++type XsInstance = {+ datas: XsData[];+ reRender?: () => void;+ sheet: {+ resetData: (data: XsData) => void;+ data?: XsData & { row?: { height?: number } };+ reload?: () => void;+ table?: { render?: () => void };+ };+ bottombar?: {+ items: HTMLElement[];+ clickSwap2?: (el: HTMLElement) => void;+ };+};++/** Locate inside @js-preview/excel: switch sheet, paint range via Data API, scroll. */+export function applyExcelSourceLocate(+ previewer: ReturnType<typeof jsPreviewExcel.init>,+ pos: ExcelLocatePos,+) {+ if (!pos || pos.length < 5) return;+ const xs = (previewer as { xs?: XsInstance }).xs;+ if (!xs?.datas?.length) return;++ const sheetIdx = Math.min(+ xs.datas.length - 1,+ Math.max(0, (pos[0] || 1) - 1),+ );+ let r1 = Math.max(0, (pos[1] || 1) - 1);+ let r2 = Math.max(r1, (pos[2] || pos[1] || 1) - 1);+ let c1 = (pos[3] || 1) - 1;+ let c2 = (pos[4] || pos[3] || 1) - 1;+ if (c1 < 0) c1 = 0;+ if (c2 < c1) c2 = c1;++ const data = xs.datas[sheetIdx];+ if (!data?.addStyle || !data.rows?.setCell) return;++ if (typeof data.rows.len === 'number') {+ const lastRow = Math.max(0, data.rows.len - 1);+ r1 = Math.min(r1, lastRow);+ r2 = Math.min(r2, lastRow);+ }+ if (typeof data.cols?.len === 'number') {+ const lastCol = Math.max(0, data.cols.len - 1);+ c1 = Math.min(c1, lastCol);+ c2 = Math.min(c2, lastCol);+ }++ const theme = getComputedStyle(document.documentElement);+ if (r2 >= r1 && c2 >= c1) {+ const styleIdx = data.addStyle({+ bgcolor:+ theme.getPropertyValue('--background-highlight').trim() || '#ffe58f',+ color: theme.getPropertyValue('--text-title').trim() || '#1a1a1a',+ });+ for (let r = r1; r <= r2; r++) {+ for (let c = c1; c <= c2; c++) {+ data.rows.setCell(r, c, { style: styleIdx }, 'format');+ }+ }+ }++ const tab = xs.bottombar?.items?.[sheetIdx];+ if (tab && typeof xs.bottombar?.clickSwap2 === 'function') {+ xs.bottombar.clickSwap2(tab);+ } else {+ xs.sheet.resetData(data);+ }++ const rowHeight = data.rows.getHeight?.(0) || xs.sheet.data?.row?.height || 24;+ let y = 0;+ for (let r = 0; r < r1; r++) {+ y += data.rows.getHeight?.(r) ?? rowHeight;+ }+ data.scroll = { ...(data.scroll || { x: 0, y: 0 }), x: 0, y };+ if (xs.sheet.data && xs.sheet.data !== data) {+ xs.sheet.data.scroll = { ...(xs.sheet.data.scroll || { x: 0, y: 0 }), x: 0, y };+ }+ xs.reRender?.();+ xs.sheet.table?.render?.();+ xs.sheet.reload?.();+}++export const useFetchExcel = (+ filePath: string,+ positions?: number[][],+) => {const [status, setStatus] = useState(true);const { fetchDocument } = useFetchDocument();const [containerEl, setContainerEl] = useState<HTMLDivElement | null>(null);@@ -222,6 +332,10 @@ export const useFetchExcel = (filePath: string) => {// Cache the fetched ArrayBuffer so we don't re-fetch on every resize.const dataRef = useRef<ArrayBuffer | null>(null);const [dataReady, setDataReady] = useState(false);+ const positionsRef = useRef(positions);+ positionsRef.current = positions;+ const locateKey = positions?.[0]?.join(',') ?? '';+ const locateTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);// @js-preview/excel reads the container's width/height at init time and// exposes no public resize method, so the only way to make the spreadsheet@@ -229,6 +343,10 @@ export const useFetchExcel = (filePath: string) => {const renderPreview = useCallback(async () => {if (!containerEl || !dataRef.current) return;+ if (locateTimerRef.current != null) {+ clearTimeout(locateTimerRef.current);+ locateTimerRef.current = null;+ }if (previewerRef.current) {previewerRef.current.destroy();previewerRef.current = null;@@ -239,6 +357,18 @@ export const useFetchExcel = (filePath: string) => {try {await previewer.preview(dataRef.current);+ const pos = positionsRef.current?.[0];+ if (pos) {+ // bottombar tabs are created during loadData; apply after paint.+ const run = () => {+ if (previewerRef.current !== previewer) return;+ applyExcelSourceLocate(previewer, pos);+ };+ requestAnimationFrame(() => {+ run();+ locateTimerRef.current = setTimeout(run, 50);+ });+ }setStatus(true);} catch (e) {// oxlint-disable-next-line no-console@@ -305,15 +435,26 @@ export const useFetchExcel = (filePath: string) => {};}, [filePath, fetchDocument]);- // Initial render + debounced re-render on container resize.+ // Initial render + debounced re-render on container resize / locate target change.useEffect(() => {if (!dataReady || !containerEl) return;debouncedRender();- }, [dataReady, containerEl, size?.width, size?.height, debouncedRender]);+ }, [+ dataReady,+ containerEl,+ size?.width,+ size?.height,+ locateKey,+ debouncedRender,+ ]);// Tear down the previewer on unmount.useEffect(() => {return () => {+ if (locateTimerRef.current != null) {+ clearTimeout(locateTimerRef.current);+ locateTimerRef.current = null;+ }if (previewerRef.current) {previewerRef.current.destroy();previewerRef.current = null;diff --git a/web/src/components/document-preview/index.tsx b/web/src/components/document-preview/index.tsxindex de3361fc62..2d9920bcf9 100644--- a/web/src/components/document-preview/index.tsx+++ b/web/src/components/document-preview/index.tsx@@ -31,6 +31,7 @@ type PreviewProps = {fileType: string;className?: string;url: string;+ positions?: number[][];};const DocumentPreview = function ({fileType,@@ -38,6 +39,7 @@ const DocumentPreview = function ({highlights,setWidthAndHeight,url,+ positions,}: PreviewProps & Partial<IProps>) {const isPdf = fileType === 'pdf';@@ -90,9 +92,13 @@ const DocumentPreview = function ({<PptPreviewer className={className} url={url} /></section>)}- {['xlsx'].indexOf(fileType) > -1 && (- <section>- <ExcelCsvPreviewer className={className} url={url} />+ {['xlsx', 'xls'].indexOf(fileType) > -1 && (+ <section className="h-full">+ <ExcelCsvPreviewer+ className={className}+ url={url}+ positions={positions}+ /></section>)}{['csv'].indexOf(fileType) > -1 && (diff --git a/web/src/components/floating-chat-widget-markdown.tsx b/web/src/components/floating-chat-widget-markdown.tsxindex 7e1afc3957..6f20c1d590 100644--- a/web/src/components/floating-chat-widget-markdown.tsx+++ b/web/src/components/floating-chat-widget-markdown.tsx@@ -34,6 +34,7 @@ import {} from '@/utils/chat';import { citationMarkerReg } from '@/utils/citation-utils';import { getExtension } from '@/utils/document-util';+import { supportsSourceLocate } from '@/utils/source-locate';import { getDirAttribute } from '@/utils/text-direction';import { InfoCircleOutlined } from '@ant-design/icons';import * as HoverCardPrimitive from '@radix-ui/react-hover-card';@@ -108,12 +109,15 @@ const FloatingChatWidgetMarkdown = ({) =>() => {if (!documentId) return;- if (fileExtension !== 'pdf' && documentUrl) {- const nextLink = `/document/${documentId}?ext=${fileExtension}&resource=${'document'}`;- window.open(nextLink, '_blank');- } else if (clickDocumentButton) {+ if (supportsSourceLocate(fileExtension) && clickDocumentButton) {clickDocumentButton(documentId, chunk);+ return;}+ if (!documentUrl) return;+ window.open(+ `/document/${documentId}?ext=${fileExtension}&resource=${'document'}`,+ '_blank',+ );},[clickDocumentButton],);@@ -236,7 +240,9 @@ const FloatingChatWidgetMarkdown = ({fileExtension,documentUrl,)}- disabled={!documentUrl && fileExtension !== 'pdf'}+ disabled={+ !documentUrl && !supportsSourceLocate(fileExtension)+ }style={{ whiteSpace: 'normal' }}><span className="truncate">@@ -245,7 +251,7 @@ const FloatingChatWidgetMarkdown = ({</Button></TooltipTrigger><TooltipContent>- {!documentUrl && fileExtension !== 'pdf'+ {!documentUrl && !supportsSourceLocate(fileExtension)? 'Document link unavailable': document.doc_name}</TooltipContent>diff --git a/web/src/components/markdown-content/index.tsx b/web/src/components/markdown-content/index.tsxindex 408e6abc44..ae5d3bde0e 100644--- a/web/src/components/markdown-content/index.tsx+++ b/web/src/components/markdown-content/index.tsx@@ -20,6 +20,7 @@ import { MarkdownRemarkPlugins } from '@/constants/markdown-remark-plugins';import { IReference, IReferenceChunk } from '@/interfaces/database/chat';import { citationMarkerReg } from '@/utils/citation-utils';import { getExtension } from '@/utils/document-util';+import { supportsSourceLocate } from '@/utils/source-locate';import { getDirAttribute } from '@/utils/text-direction';import DOMPurify from 'dompurify';import { memo, useCallback, useEffect, useMemo } from 'react';@@ -185,15 +186,15 @@ const MarkdownContent = ({documentUrl?: string,) =>() => {- if (fileExtension !== 'pdf') {- if (!documentUrl) {- return;- }- const nextLink = `/document/${documentId}?ext=${fileExtension}&resource=${'document'}`;- window.open(nextLink, '_blank');- } else {- clickDocumentButton?.(documentId, chunk);+ if (supportsSourceLocate(fileExtension) && clickDocumentButton) {+ clickDocumentButton(documentId, chunk);+ return;}+ if (!documentUrl) return;+ window.open(+ `/document/${documentId}?ext=${fileExtension}&resource=${'document'}`,+ '_blank',+ );},[clickDocumentButton],);diff --git a/web/src/components/next-markdown-content/index.tsx b/web/src/components/next-markdown-content/index.tsxindex 68a5b5304f..acc9a6283c 100644--- a/web/src/components/next-markdown-content/index.tsx+++ b/web/src/components/next-markdown-content/index.tsx@@ -20,6 +20,7 @@ import { SafeImg } from '@/components/safe-img';import { MarkdownRemarkPlugins } from '@/constants/markdown-remark-plugins';import { IReferenceChunk, IReferenceObject } from '@/interfaces/database/chat';import { getExtension } from '@/utils/document-util';+import { supportsSourceLocate } from '@/utils/source-locate';import { downloadFileFromBlob } from '@/utils/file-util';import request from '@/utils/request';import DOMPurify from 'dompurify';@@ -230,15 +231,15 @@ function MarkdownContent({documentUrl?: string,) =>() => {- if (fileExtension !== 'pdf') {- if (!documentUrl) {- return;- }- const nextLink = `/document/${documentId}?ext=${fileExtension}&resource=${'document'}`;- window.open(nextLink, '_blank');- } else {- clickDocumentButton?.(documentId, chunk);+ if (supportsSourceLocate(fileExtension) && clickDocumentButton) {+ clickDocumentButton(documentId, chunk);+ return;}+ if (!documentUrl) return;+ window.open(+ `/document/${documentId}?ext=${fileExtension}&resource=${'document'}`,+ '_blank',+ );},[clickDocumentButton],);diff --git a/web/src/components/pdf-drawer/index.tsx b/web/src/components/pdf-drawer/index.tsxindex a5df43f042..f6b35247d8 100644--- a/web/src/components/pdf-drawer/index.tsx+++ b/web/src/components/pdf-drawer/index.tsx@@ -22,7 +22,9 @@ import { IModalProps } from '@/interfaces/common';import { IReferenceChunk } from '@/interfaces/database/chat';import { IChunk } from '@/interfaces/database/dataset';import { cn } from '@/lib/utils';-import PdfPreview from '../document-preview/pdf-preview';+import { getExtension } from '@/utils/document-util';+import { get } from 'lodash';+import DocumentPreview from '../document-preview';import { Sheet, SheetContent, SheetHeader, SheetTitle } from '../ui/sheet';interface IProps extends IModalProps<any> {@@ -42,6 +44,11 @@ export const PdfSheet = ({const getDocumentUrl = useGetDocumentUrl(documentId);const url = getDocumentUrl(documentId);const { highlights, setWidthAndHeight } = useGetChunkHighlights(chunk);+ const fileType = getExtension(+ get(chunk, 'document_name', '') || get(chunk, 'docnm_kwd', '') || 'pdf',+ );+ const positions = Array.isArray(chunk?.positions) ? chunk.positions : [];+return (<Sheet open onOpenChange={hideModal}><SheetContent@@ -55,12 +62,14 @@ export const PdfSheet = ({<SheetTitle>Document Previewer</SheetTitle></SheetHeader>{url && documentId && (- <PdfPreview+ <DocumentPreviewclassName={'p-0 !h-[calc(100vh-80px)] w-full'}+ fileType={fileType || 'pdf'}highlights={highlights}setWidthAndHeight={setWidthAndHeight}url={url}- ></PdfPreview>+ positions={positions}+ />)}</SheetContent></Sheet>diff --git a/web/src/constants/common.ts b/web/src/constants/common.tsindex 0bce29efef..9e3674b63a 100644--- a/web/src/constants/common.ts+++ b/web/src/constants/common.ts@@ -218,6 +218,8 @@ export const ExceptiveType = ['txt','csv','pptx',+ 'html',+ 'htm',...Images,];diff --git a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/document-view-switch/index.tsx b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/document-view-switch/index.tsxindex 44118464b0..c19d66374e 100644--- a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/document-view-switch/index.tsx+++ b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/components/document-view-switch/index.tsx@@ -24,6 +24,7 @@ interface DocumentViewSwitchProps {highlights: IHighlight[];setWidthAndHeight: (width: number, height: number) => void;url: string;+ positions?: number[][];onChunkIdsChange?: (chunkIds: string[]) => void;}@@ -33,6 +34,7 @@ export default function DocumentViewSwitch({highlights,setWidthAndHeight,url,+ positions,onChunkIdsChange,}: DocumentViewSwitchProps) {const { t } = useTranslation();@@ -100,6 +102,7 @@ export default function DocumentViewSwitch({highlights={highlights}setWidthAndHeight={setWidthAndHeight}url={url}+ positions={positions}/>) : (<Representation onNodeClick={handleNodeClick} />diff --git a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/index.tsx b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/index.tsxindex 046bb504e0..2f1278de8e 100644--- a/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/index.tsx+++ b/web/src/pages/chunk/parsed-result/add-knowledge/components/knowledge-chunk/index.tsx@@ -10,6 +10,7 @@ import {useChangeChunkTextMode,useDeleteChunkByIds,useGetChunkHighlights,+ useGetSelectedChunk,useHandleChunkCardClick,useUpdateChunk,} from './hooks';@@ -38,6 +39,7 @@ import {QueryStringMap,useNavigatePage,} from '@/hooks/logic-hooks/navigate-hooks';+import { getExtension } from '@/utils/document-util';import { LucideArrowBigLeft } from 'lucide-react';function Chunk() {@@ -166,19 +168,26 @@ function Chunk() {const { highlights, setWidthAndHeight } =useGetChunkHighlights(selectedChunkId);+ const selectedChunk = useGetSelectedChunk(selectedChunkId);+ const positions = Array.isArray(selectedChunk?.positions)+ ? selectedChunk.positions+ : [];const fileType = useMemo(() => {+ const name = documentInfo?.name || '';+ if (name.includes('.')) {+ return getExtension(name);+ }switch (documentInfo?.type) {case 'doc':- return documentInfo?.name.split('.').pop() || 'doc';case 'visual':- return documentInfo?.name.split('.').pop() || 'visual';+ return documentInfo?.name?.split('.').pop() || documentInfo.type;case 'docx':case 'txt':case 'md':case 'mdx':case 'pdf':- return documentInfo?.type;+ return documentInfo.type;}return 'unknown';}, [documentInfo]);@@ -208,6 +217,7 @@ function Chunk() {highlights={highlights}setWidthAndHeight={setWidthAndHeight}url={fileUrl}+ positions={positions}onChunkIdsChange={handleChunkIdsChange}/></article>diff --git a/web/src/pages/next-search/document-preview-modal/index.tsx b/web/src/pages/next-search/document-preview-modal/index.tsxindex be25e822a9..2f7800abd3 100644--- a/web/src/pages/next-search/document-preview-modal/index.tsx+++ b/web/src/pages/next-search/document-preview-modal/index.tsx@@ -57,6 +57,7 @@ const PdfDrawer = ({const fileType = documentName ? getFileExtensionRegex(documentName) : '';const isWebPage = !fileType && !!chunk.document_url;const url = isWebPage ? (chunk.document_url as string) : getDocumentUrl();+ const positions = Array.isArray(chunk.positions) ? chunk.positions : [];return (<Modaltitle={@@ -88,6 +89,7 @@ const PdfDrawer = ({highlights={highlights}setWidthAndHeight={setWidthAndHeight}url={url}+ positions={positions}></DocumentPreview></Modal>);diff --git a/web/src/utils/__tests__/source-locate.test.ts b/web/src/utils/__tests__/source-locate.test.tsnew file mode 100644index 0000000000..a3c1088a1a--- /dev/null+++ b/web/src/utils/__tests__/source-locate.test.ts@@ -0,0 +1,15 @@+import { supportsSourceLocate } from '../source-locate';++describe('supportsSourceLocate', () => {+ it('accepts pdf and excel extensions case-insensitively', () => {+ expect(supportsSourceLocate('pdf')).toBe(true);+ expect(supportsSourceLocate('XLSX')).toBe(true);+ expect(supportsSourceLocate('xls')).toBe(true);+ });++ it('rejects unsupported or missing extensions', () => {+ expect(supportsSourceLocate('html')).toBe(false);+ expect(supportsSourceLocate('docx')).toBe(false);+ expect(supportsSourceLocate(undefined)).toBe(false);+ });+});diff --git a/web/src/utils/source-locate.ts b/web/src/utils/source-locate.tsnew file mode 100644index 0000000000..1518bfe5f7--- /dev/null+++ b/web/src/utils/source-locate.ts@@ -0,0 +1,23 @@+/*+ * Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++/** File types that support in-app citation source location. */+export const SourceLocateExtensions = ['pdf', 'xlsx', 'xls'] as const;++/** True when the citation should open the in-app preview locate drawer. */+export const supportsSourceLocate = (ext?: string) =>+ !!ext &&+ (SourceLocateExtensions as readonly string[]).includes(ext.toLowerCase());