From 911a1490ead204ace609a98e4df247543177cdc4 Mon Sep 17 00:00:00 2001 From: tayadwx <42323260+tayadwx@users.noreply.github.com> Date: Mon, 21 Sep 2026 13:13:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(wiki):=20enrich=20cited=20chunk=20content?= =?UTF-8?q?=20with=20image=20captions=20in=20Reduce=20phase=20=E5=AE=9E?= =?UTF-8?q?=E4=BD=93/=E6=91=98=E8=A6=81=E9=A1=B5=E9=9D=A2=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E5=AD=90=E5=9D=97=E6=97=B6=E5=90=8C=E6=A0=B7=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=AF=8C=E6=96=87=E6=9C=AC=E5=86=85=E5=AE=B9=20(#3292?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(wiki): enrich cited chunk content with image captions in Reduce phase The citation-pass comment claims image/ocr chunks are "already merged into the text content via reconstructEnrichedContent", but that function has exactly one call site — the per-document summary page. The Reduce / page-edit phase resolves cited chunks straight from the repository and feeds the LLM bare Markdown image links with no caption or OCR context. This mismatch is observable: on concept/entity pages generated from an image-heavy knowledge base, the page-editing model receives ![filename](wkimg:NNN) with no way to judge the image, and empirically either copies the image links verbatim or drops them wholesale — including real content images. A controlled experiment where the same image reached two pages byte-identically produced opposite keep/drop decisions, and appeared in 0 of 9 outgoing page-edit requests. resolveCitedChunks now collects image_info for the cited text chunks (CollectImageInfoByChunkIDs, one query per knowledge batch, outside the per-page loop) and inlines it with EnrichContentWithImageInfo — the same enrichment the summary path uses. Image info collection skips disabled child chunks, so downstream decorative-image cleanup composes with this path automatically. * test(wiki): cover cited-chunk image enrichment in resolveCitedChunks Add unit tests asserting that cited text chunks reach the Reduce phase enriched with caption/OCR info from their enabled image child chunks, that disabled children are skipped (decorative-image cleanup composes), and that unknown chunk IDs still fall through gracefully. --------- Co-authored-by: tayadwx --- .../application/service/wiki_ingest_cite.go | 16 ++- .../service/wiki_ingest_cite_resolve_test.go | 131 ++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 internal/application/service/wiki_ingest_cite_resolve_test.go diff --git a/internal/application/service/wiki_ingest_cite.go b/internal/application/service/wiki_ingest_cite.go index 40282494..97c18ead 100644 --- a/internal/application/service/wiki_ingest_cite.go +++ b/internal/application/service/wiki_ingest_cite.go @@ -12,6 +12,7 @@ import ( "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/modelcontext" "github.com/Tencent/WeKnora/internal/models/chat" + "github.com/Tencent/WeKnora/internal/searchutil" "github.com/Tencent/WeKnora/internal/types" "golang.org/x/sync/errgroup" ) @@ -408,11 +409,24 @@ func (s *wikiIngestService) resolveCitedChunks( logger.Warnf(ctx, "wiki ingest: failed to resolve cited chunks: %v", err) continue } + + // Pull image captions / OCR from the image child chunks of the cited + // text chunks and inline them into the content, the same way the wiki + // summary path does via reconstructEnrichedContent. Without this the + // Reduce phase sees bare Markdown image links with no caption/OCR + // context, so image-heavy documents degrade to "copy verbatim" or + // "drop entirely" behavior on concept/entity pages. + imageInfoByID := searchutil.CollectImageInfoByChunkIDs(ctx, s.chunkRepo, tenantID, ids) + for _, c := range chunks { if c == nil || c.Content == "" { continue } - out[c.ID] = c.Content + content := c.Content + if info := imageInfoByID[c.ID]; info != "" { + content = searchutil.EnrichContentWithImageInfo(content, info) + } + out[c.ID] = content } } return out diff --git a/internal/application/service/wiki_ingest_cite_resolve_test.go b/internal/application/service/wiki_ingest_cite_resolve_test.go new file mode 100644 index 00000000..78122172 --- /dev/null +++ b/internal/application/service/wiki_ingest_cite_resolve_test.go @@ -0,0 +1,131 @@ +package service + +import ( + "context" + "strings" + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/Tencent/WeKnora/internal/types/interfaces" +) + +// citedChunkRepo answers the two chunk-repository queries resolveCitedChunks +// and CollectImageInfoByChunkIDs exercise, keyed by the requested ID / parent +// ID lists. Unimplemented interface methods stay nil: they must never be +// reached by this code path, and a call would panic loudly in tests. +type citedChunkRepo struct { + interfaces.ChunkRepository + byID map[string][]*types.Chunk + byParent map[string][]*types.Chunk +} + +func (r *citedChunkRepo) ListChunksByID( + _ context.Context, _ uint64, ids []string, +) ([]*types.Chunk, error) { + var out []*types.Chunk + for _, id := range ids { + out = append(out, r.byID[id]...) + } + return out, nil +} + +func (r *citedChunkRepo) ListChunksByParentIDs( + _ context.Context, _ uint64, parentIDs []string, +) ([]*types.Chunk, error) { + var out []*types.Chunk + for _, id := range parentIDs { + out = append(out, r.byParent[id]...) + } + return out, nil +} + +func citationChunk(id, content string) *types.Chunk { + return &types.Chunk{ + ID: id, + TenantID: 1, + KnowledgeID: "kn-1", + Content: content, + ChunkType: types.ChunkTypeText, + IsEnabled: true, + } +} + +func imageChildChunk(id, parentID, chunkType types.ChunkType, imageInfoJSON string, enabled bool) *types.Chunk { + return &types.Chunk{ + ID: id, + TenantID: 1, + KnowledgeID: "kn-1", + ChunkType: chunkType, + IsEnabled: enabled, + ParentChunkID: parentID, + ImageInfo: imageInfoJSON, + } +} + +// TestResolveCitedChunksEnrichesWithImageInfo verifies that the Reduce phase +// receives cited text chunks enriched with the caption/OCR info of their +// (enabled) image child chunks — the same treatment the summary page gets via +// reconstructEnrichedContent. Disabled image children must be skipped so the +// decorative-image cleanup composes with this path. +func TestResolveCitedChunksEnrichesWithImageInfo(t *testing.T) { + const handle = "resource://aaaaaaaaaaaaaaaaaaaaaa" + withImage := citationChunk("chunk-with-image", "Revenue chart\n\n![chart]("+handle+")") + plain := citationChunk("chunk-plain", "Plain text with no images at all.") + + captionInfo := `[{"url":"` + handle + `","caption":"Quarterly revenue by region"}]` + disabledInfo := `[{"url":"` + handle + `","caption":"DECORATIVE_CAPTION_MUST_NOT_APPEAR"}]` + + repo := &citedChunkRepo{ + byID: map[string][]*types.Chunk{ + "chunk-with-image": {withImage}, + "chunk-plain": {plain}, + }, + byParent: map[string][]*types.Chunk{ + "chunk-with-image": { + imageChildChunk("img-cap", "chunk-with-image", types.ChunkTypeImageCaption, captionInfo, true), + imageChildChunk("img-off", "chunk-with-image", types.ChunkTypeImageCaption, disabledInfo, false), + }, + }, + } + + svc := &wikiIngestService{chunkRepo: repo} + out := svc.resolveCitedChunks(context.Background(), 1, []SlugUpdate{ + {KnowledgeID: "kn-1", SourceChunks: []string{"chunk-with-image", "chunk-plain"}}, + }) + + enriched, ok := out["chunk-with-image"] + if !ok { + t.Fatalf("cited chunk missing from result: %v", out) + } + if !strings.Contains(enriched, "") || + !strings.Contains(enriched, "Quarterly revenue by region") { + t.Fatalf("cited content not enriched with caption:\n%s", enriched) + } + if strings.Contains(enriched, "DECORATIVE_CAPTION_MUST_NOT_APPEAR") { + t.Fatalf("disabled image child leaked into enrichment:\n%s", enriched) + } + // The original Markdown must survive the enrichment, not be replaced. + if !strings.Contains(enriched, "![chart]("+handle+")") { + t.Fatalf("original markdown image lost during enrichment:\n%s", enriched) + } + + if got := out["chunk-plain"]; got != "Plain text with no images at all." { + t.Fatalf("image-free cited chunk must pass through unchanged, got:\n%s", got) + } +} + +// TestResolveCitedChunksStillFallsBackWithoutRepoRows keeps the documented +// missing/out-of-tenant behaviour: IDs the repo does not know are silently +// skipped so the Reduce phase can fall back to the Details paraphrase. +func TestResolveCitedChunksStillFallsBackWithoutRepoRows(t *testing.T) { + repo := &citedChunkRepo{byID: map[string][]*types.Chunk{}, byParent: map[string][]*types.Chunk{}} + + svc := &wikiIngestService{chunkRepo: repo} + out := svc.resolveCitedChunks(context.Background(), 1, []SlugUpdate{ + {KnowledgeID: "kn-1", SourceChunks: []string{"ghost-chunk"}}, + }) + + if len(out) != 0 { + t.Fatalf("unknown cited chunk must be skipped, got %v", out) + } +}