feat(wiki): add page hierarchy/category metadata and retrieval APIs

Persist category and hierarchy metadata on wiki pages (new migration), expose
category-path retrieval and related repository/service/handler/router APIs, and
update wiki ingestion (batch, cite, taxonomy) plus agent wiki prompts to build
and consume the taxonomy. Regenerate swagger docs for the new endpoints.
This commit is contained in:
wizardchen
2026-06-16 16:35:06 +08:00
committed by lyingbug
parent ec66a3dac7
commit fe33db9493
20 changed files with 1893 additions and 25 deletions
@@ -357,6 +357,28 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
ExtractionGranularity: granularity,
}
var taxonomyOnce sync.Once
var taxonomyText string
batchCtx.ExistingTaxonomy = func(ctx context.Context) string {
taxonomyOnce.Do(func() {
if s.wikiService == nil {
return
}
paths, err := s.wikiService.ListDistinctCategoryPaths(ctx, payload.KnowledgeBaseID, wikiTaxonomyPromptMaxPaths)
if err != nil {
logger.Warnf(ctx, "wiki ingest: ListDistinctCategoryPaths failed: %v", err)
return
}
taxonomyText = formatExistingTaxonomyForPrompt(paths)
})
return taxonomyText
}
// Snapshot the KB's category folders before this batch runs so we can tell
// afterwards whether new (possibly synonymous) folders were introduced and
// a taxonomy reconcile pass is worth running.
startCategoryKeys := s.snapshotCategoryKeys(ctx, payload.KnowledgeBaseID)
// 1. MAP PHASE (Parallel extraction and generation of updates)
var mapMu sync.Mutex
var failedOps []WikiPendingOp
@@ -628,6 +650,14 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
}
}
// Collapse synonymous category folders introduced by this batch (cold
// start, intra-batch races, long-term drift). Runs only when a genuinely
// new folder appeared vs the pre-batch snapshot; re-points pages' category
// without touching slugs, then refreshes the index.
if len(ingestPagesAffected) > 0 {
s.maybeReconcileTaxonomy(ctx, chatModel, payload.KnowledgeBaseID, lang, startCategoryKeys)
}
// Clean dead [[slug]] references whenever ANY page was touched this
// batch (not just retracts). Reduce-phase failures can leave stale
// references in pages we just rewrote (e.g. summary pages cite
@@ -960,7 +990,7 @@ func (s *wikiIngestService) mapOneDocument(
return
}
candidatesXML := renderCandidateSlugsXML(extractedEntities, extractedConcepts)
citations, newSlugs, batchCount = s.classifyChunkCitations(ctx, chatModel, candidatesXML, chunks, lang)
citations, newSlugs, batchCount = s.classifyChunkCitations(ctx, chatModel, candidatesXML, chunks, lang, batchCtx)
s.tracker().EndSpan(ctx, classifySpan, types.JSONMap{
"cited_slugs": len(citations),
"new_slugs": len(newSlugs),
@@ -1585,6 +1615,11 @@ func (s *wikiIngestService) reduceSlugUpdates(
}
pageAliases := strings.Join(page.Aliases, ", ")
currentCategory := strings.Join(page.CategoryPath, " / ")
if strings.TrimSpace(currentCategory) == "" {
currentCategory = "(none yet)"
}
var updatedContent string
updatedContent, err = s.generateWithTemplate(ctx, chatModel, agent.WikiPageModifyPrompt, map[string]string{
"HasAdditions": hasAdditionsStr,
@@ -1598,11 +1633,22 @@ func (s *wikiIngestService) reduceSlugUpdates(
"DeletedContent": deletedContent.String(),
"RemainingSourcesContent": remainingSourcesContent.String(),
"AvailableSlugs": relatedSlugs.String(),
"CurrentCategory": currentCategory,
"ExistingTaxonomy": resolveExistingTaxonomyForPrompt(ctx, batchCtx),
"Language": language,
})
if err == nil && updatedContent != "" {
updatedSummary, updatedBody := splitSummaryLine(updatedContent)
// CATEGORY is the page-level intrinsic classification produced by
// the editor model. Only overwrite when the model actually emitted
// a non-empty path so a lazy/empty line never wipes a good category.
category, bodyAfterCategory := splitCategoryLine(updatedBody)
updatedBody = bodyAfterCategory
if len(category) > 0 {
page.CategoryPath = types.StringArray(category)
page.WikiPath = ""
}
if updatedBody != "" {
page.Content = updatedBody
} else {