2026-02-28 05:22:07 +00:00
|
|
|
package docparser
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-04-14 09:13:16 +00:00
|
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
2026-02-28 05:22:07 +00:00
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
2026-08-10 05:24:14 +00:00
|
|
|
secutils "github.com/Tencent/WeKnora/internal/utils"
|
2026-02-28 05:22:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
PathRead = "/read"
|
|
|
|
|
PathListEngines = "/list-engines"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// --- JSON DTOs ---
|
|
|
|
|
|
|
|
|
|
type httpReadConfig struct {
|
|
|
|
|
ParserEngine string `json:"parser_engine,omitempty"`
|
|
|
|
|
ParserEngineOverrides map[string]string `json:"parser_engine_overrides,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type httpReadRequest struct {
|
|
|
|
|
FileContent string `json:"file_content,omitempty"` // base64
|
|
|
|
|
FileName string `json:"file_name,omitempty"`
|
|
|
|
|
FileType string `json:"file_type,omitempty"`
|
|
|
|
|
URL string `json:"url,omitempty"`
|
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
|
Config *httpReadConfig `json:"config,omitempty"`
|
|
|
|
|
RequestID string `json:"request_id,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type httpImageRef struct {
|
|
|
|
|
Filename string `json:"filename"`
|
|
|
|
|
OriginalRef string `json:"original_ref"`
|
|
|
|
|
MimeType string `json:"mime_type"`
|
|
|
|
|
StorageKey string `json:"storage_key,omitempty"`
|
|
|
|
|
ImageData []byte `json:"image_data,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type httpReadResponse struct {
|
|
|
|
|
MarkdownContent string `json:"markdown_content"`
|
|
|
|
|
ImageRefs []httpImageRef `json:"image_refs,omitempty"`
|
|
|
|
|
ImageDirPath string `json:"image_dir_path,omitempty"`
|
|
|
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
|
|
|
Error string `json:"error,omitempty"`
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
SourceBlocks []wireSourceBlock `json:"source_blocks,omitempty"`
|
2026-02-28 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HTTPDocumentReader implements DocumentReader over HTTP/JSON.
|
|
|
|
|
type HTTPDocumentReader struct {
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
baseURL string
|
|
|
|
|
client *http.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewHTTPDocumentReader(baseURL string) (*HTTPDocumentReader, error) {
|
2026-08-10 05:24:14 +00:00
|
|
|
baseURL = strings.TrimSuffix(strings.TrimSpace(baseURL), "/")
|
|
|
|
|
if baseURL != "" {
|
|
|
|
|
if err := secutils.ValidateURLForSSRF(baseURL); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("docreader address failed SSRF validation: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
clientCfg := secutils.DefaultSSRFSafeHTTPClientConfig()
|
|
|
|
|
clientCfg.Timeout = 5 * time.Minute
|
2026-02-28 05:22:07 +00:00
|
|
|
p := &HTTPDocumentReader{
|
2026-08-10 05:24:14 +00:00
|
|
|
baseURL: baseURL,
|
|
|
|
|
client: secutils.NewSSRFSafeHTTPClient(clientCfg),
|
2026-02-28 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
if p.baseURL != "" {
|
2026-04-14 09:13:16 +00:00
|
|
|
logger.Infof(context.Background(), "INFO: HTTP docreader base URL: %s", p.baseURL)
|
2026-02-28 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *HTTPDocumentReader) base() string {
|
|
|
|
|
p.mu.RLock()
|
|
|
|
|
defer p.mu.RUnlock()
|
|
|
|
|
return p.baseURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *HTTPDocumentReader) Reconnect(addr string) error {
|
2026-08-10 05:24:14 +00:00
|
|
|
addr = strings.TrimSuffix(strings.TrimSpace(addr), "/")
|
|
|
|
|
if addr != "" {
|
|
|
|
|
if err := secutils.ValidateURLForSSRF(addr); err != nil {
|
|
|
|
|
return fmt.Errorf("docreader address failed SSRF validation: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-28 05:22:07 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
|
defer p.mu.Unlock()
|
2026-08-10 05:24:14 +00:00
|
|
|
p.baseURL = addr
|
2026-04-14 09:13:16 +00:00
|
|
|
logger.Infof(context.Background(), "INFO: HTTP docreader base URL set to %s", p.baseURL)
|
2026-02-28 05:22:07 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *HTTPDocumentReader) IsConnected() bool {
|
|
|
|
|
p.mu.RLock()
|
|
|
|
|
defer p.mu.RUnlock()
|
|
|
|
|
return p.baseURL != ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *HTTPDocumentReader) Close() error { return nil }
|
|
|
|
|
|
|
|
|
|
type httpListEnginesRequest struct {
|
|
|
|
|
ConfigOverrides map[string]string `json:"config_overrides,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type httpParserEngineInfo struct {
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Description string `json:"description"`
|
|
|
|
|
FileTypes []string `json:"file_types"`
|
|
|
|
|
Available bool `json:"available"`
|
|
|
|
|
UnavailableReason string `json:"unavailable_reason,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type httpListEnginesResponse struct {
|
|
|
|
|
Engines []httpParserEngineInfo `json:"engines"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *HTTPDocumentReader) ListEngines(ctx context.Context, overrides map[string]string) ([]types.ParserEngineInfo, error) {
|
|
|
|
|
base := p.base()
|
|
|
|
|
if base == "" {
|
|
|
|
|
return nil, errNotConnected
|
|
|
|
|
}
|
2026-08-10 05:24:14 +00:00
|
|
|
if err := secutils.ValidateURLForSSRF(base); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("docreader address failed SSRF validation: %w", err)
|
|
|
|
|
}
|
2026-02-28 05:22:07 +00:00
|
|
|
|
|
|
|
|
body := httpListEnginesRequest{ConfigOverrides: overrides}
|
|
|
|
|
jsonBody, err := json.Marshal(body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("http marshal list-engines request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, base+PathListEngines, bytes.NewReader(jsonBody))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("http new request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
|
|
resp, err := p.client.Do(httpReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("http list-engines failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
respBytes, _ := io.ReadAll(resp.Body)
|
|
|
|
|
return nil, fmt.Errorf("http list-engines status %d: %s", resp.StatusCode, string(respBytes))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var out httpListEnginesResponse
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("http decode list-engines response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := make([]types.ParserEngineInfo, 0, len(out.Engines))
|
|
|
|
|
for _, e := range out.Engines {
|
|
|
|
|
result = append(result, types.ParserEngineInfo{
|
|
|
|
|
Name: e.Name,
|
|
|
|
|
Description: e.Description,
|
|
|
|
|
FileTypes: e.FileTypes,
|
|
|
|
|
Available: e.Available,
|
|
|
|
|
UnavailableReason: e.UnavailableReason,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fromHTTPReadResponse(resp *httpReadResponse) *types.ReadResult {
|
|
|
|
|
result := &types.ReadResult{
|
|
|
|
|
MarkdownContent: resp.MarkdownContent,
|
|
|
|
|
ImageDirPath: resp.ImageDirPath,
|
|
|
|
|
Metadata: resp.Metadata,
|
|
|
|
|
Error: resp.Error,
|
feat(chat): open cited documents at the cited passage (#3700)
* feat(chat): open cited documents at the cited passage
Clicking a citation used to show only the parsed chunk. It now opens the
original file in the references panel, jumps to the cited place and
highlights it, narrowed to the sentence the citation supports.
Parsing records where each chunk sits in the original file
(chunks.source_locators): PDF page and paragraph boxes from the docreader,
MinerU and PaddleOCR-VL layouts; Word paragraphs, slides, sheet rows and
EPUB chapters aligned in Go against any parser engine's Markdown; text
offsets for Markdown/TXT; segment times for audio. Pipeline text rewrites
are followed with a line-diff remapper. Scanned PDF pages get their OCR
text as quotes at search time so a sentence can pick its page.
The frontend renders PDFs with pdf.js and every other previewable type
with the existing preview, then highlights with the CSS Custom Highlight
API in a highlighter yellow that stays visible against the brand green.
* fix(docreader): serialize source blocks outside main so tests import it
test_docx_tables stubs docreader.parser in sys.modules, so importing
docreader.main from a later test failed in the full unittest discover run.
The wire conversion now lives in docreader.source_wire, which needs only
the proto.
* fix(chat): tighten source locators from review
- Remap rewritten-line-for-line stretches by line so scanned PDF pages
no longer drift onto the wrong page.
- Keep the source preview mounted for citations in the file already open.
- Wait for in-flight PDF page renders before looking for the text layer.
- Count image rune offsets in one pass; stream xlsx rows up to maxUnits.
- Start text locators after a UTF-8 BOM, as viewers decode without it.
- Only re-query image children for scanned-page results.
- Seek audio citations without autoplay and drop stale seek listeners.
- Fix the SQLite migration's cross-reference to versioned/000113.
* fix(migrations): renumber chunk source locators after main's chunk_images
main took versioned 000113 and sqlite 000032 for chunk_images, so the
source_locators column moves to versioned 000114 and sqlite 000033.
2026-09-25 05:26:16 +00:00
|
|
|
SourceBlocks: sourceBlocksFromWire(resp.SourceBlocks),
|
2026-02-28 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
for _, ref := range resp.ImageRefs {
|
|
|
|
|
result.ImageRefs = append(result.ImageRefs, types.ImageRef{
|
|
|
|
|
Filename: ref.Filename,
|
|
|
|
|
OriginalRef: ref.OriginalRef,
|
|
|
|
|
MimeType: ref.MimeType,
|
|
|
|
|
StorageKey: ref.StorageKey,
|
|
|
|
|
ImageData: ref.ImageData,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *HTTPDocumentReader) Read(ctx context.Context, req *types.ReadRequest) (*types.ReadResult, error) {
|
|
|
|
|
base := p.base()
|
|
|
|
|
if base == "" {
|
|
|
|
|
return nil, errNotConnected
|
|
|
|
|
}
|
2026-08-10 05:24:14 +00:00
|
|
|
if err := secutils.ValidateURLForSSRF(base); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("docreader address failed SSRF validation: %w", err)
|
|
|
|
|
}
|
2026-02-28 05:22:07 +00:00
|
|
|
|
|
|
|
|
body := httpReadRequest{
|
|
|
|
|
FileName: req.FileName,
|
|
|
|
|
FileType: req.FileType,
|
|
|
|
|
URL: req.URL,
|
|
|
|
|
Title: req.Title,
|
|
|
|
|
RequestID: req.RequestID,
|
|
|
|
|
Config: &httpReadConfig{
|
|
|
|
|
ParserEngine: req.ParserEngine,
|
|
|
|
|
ParserEngineOverrides: req.ParserEngineOverrides,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
if len(req.FileContent) > 0 {
|
|
|
|
|
body.FileContent = base64.StdEncoding.EncodeToString(req.FileContent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonBody, err := json.Marshal(body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("http marshal read request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, base+PathRead, bytes.NewReader(jsonBody))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("http new request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
|
httpReq.ContentLength = int64(len(jsonBody))
|
|
|
|
|
|
|
|
|
|
resp, err := p.client.Do(httpReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("http read failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
|
|
|
|
return nil, fmt.Errorf("http read status %d: %s", resp.StatusCode, string(bodyBytes))
|
|
|
|
|
}
|
|
|
|
|
var out httpReadResponse
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("http decode read response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return fromHTTPReadResponse(&out), nil
|
|
|
|
|
}
|