From d9cf4b7aa945c587f0d5ca7a05c8badba0f130e2 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 8 Apr 2026 22:15:38 +0800 Subject: [PATCH] feat: implement metadata-based alias management for wiki pages Made-with: Cursor --- frontend/src/api/wiki/index.ts | 1 + frontend/src/i18n/locales/zh-CN.ts | 1 + .../src/views/knowledge/wiki/WikiBrowser.vue | 6 ++++ internal/agent/prompts_wiki.go | 4 +++ internal/application/repository/wiki_page.go | 5 +-- internal/application/service/wiki_ingest.go | 35 +++++++++++++++---- internal/types/wiki_page.go | 2 ++ 7 files changed, 46 insertions(+), 8 deletions(-) diff --git a/frontend/src/api/wiki/index.ts b/frontend/src/api/wiki/index.ts index f06c906b..1d32743a 100644 --- a/frontend/src/api/wiki/index.ts +++ b/frontend/src/api/wiki/index.ts @@ -11,6 +11,7 @@ export interface WikiPage { status: string; content: string; summary: string; + aliases: string[]; source_refs: string[]; in_links: string[]; out_links: string[]; diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index 80c818df..ee83338b 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -2132,6 +2132,7 @@ export default { emptyDesc: "上传文档并启用 Wiki 后将自动生成知识页面", selectPageHint: "从左侧选择一个页面查看内容", version: "v{ver}", + aliases: "别名", linksTo: "链接到", linkedFrom: "被链接", sources: "来源文档", diff --git a/frontend/src/views/knowledge/wiki/WikiBrowser.vue b/frontend/src/views/knowledge/wiki/WikiBrowser.vue index 53accacb..4f4223f8 100644 --- a/frontend/src/views/knowledge/wiki/WikiBrowser.vue +++ b/frontend/src/views/knowledge/wiki/WikiBrowser.vue @@ -182,6 +182,12 @@

{{ selectedPage.title }}

+
+ {{ $t('knowledgeEditor.wikiBrowser.aliases') }}: + + {{ alias }} + +
{{ getTypeLabel(selectedPage.page_type) }} diff --git a/internal/agent/prompts_wiki.go b/internal/agent/prompts_wiki.go index fcad15e6..ded0897d 100644 --- a/internal/agent/prompts_wiki.go +++ b/internal/agent/prompts_wiki.go @@ -65,6 +65,7 @@ If previous slugs are provided above, you MUST follow these rules: Each entity should have: - "name": The entity name in {{.Language}} (human-readable) - "slug": URL-friendly slug, format "entity/" (use romanized/pinyin form for non-Latin names). **Reuse previous slug if the entity was extracted before.** +- "aliases": An array of strings representing alternative names, abbreviations, acronyms or translations of the entity found in the document. Provide [] if none. - "description": **Index listing summary** — one sentence, 15-40 words, in {{.Language}}. Describes WHAT this entity IS and its role in the document. Must be self-contained (understandable without reading the full page). This will be displayed in the wiki index. - "details": A 2-5 sentence summary in {{.Language}} of key facts from the document. **Image rule**: If the document contains relevant elements in an tag, include them in the details using Markdown syntax: ![caption](url). @@ -74,6 +75,7 @@ Only include entities that are substantively discussed (mentioned at least twice Each concept should have: - "name": The concept name in {{.Language}} (human-readable) - "slug": URL-friendly slug, format "concept/" (use romanized/pinyin form for non-Latin names). **Reuse previous slug if the concept was extracted before.** +- "aliases": An array of strings representing alternative names, abbreviations, acronyms or translations of the concept found in the document. Provide [] if none. - "description": **Index listing summary** — one sentence, 15-40 words, in {{.Language}}. Defines WHAT this concept IS. Must be self-contained (understandable without reading the full page). This will be displayed in the wiki index. - "details": A 2-5 sentence explanation in {{.Language}} as discussed in the document. **Image rule**: If the document contains relevant elements in an tag, include them in the details using Markdown syntax: ![caption](url). @@ -91,6 +93,7 @@ Output ONLY valid JSON. Example: { "name": "Acme Corp", "slug": "entity/acme-corp", + "aliases": ["Acme", "Acme Corporation"], "description": "A technology company specializing in AI solutions.", "details": "Acme Corp was founded in 2020 and has grown to 500 employees. They focus on enterprise AI products and recently launched their flagship RAG platform." } @@ -99,6 +102,7 @@ Output ONLY valid JSON. Example: { "name": "Retrieval-Augmented Generation", "slug": "concept/retrieval-augmented-generation", + "aliases": ["RAG"], "description": "A technique that combines information retrieval with language model generation.", "details": "RAG works by first retrieving relevant documents from a knowledge base using vector similarity search, then feeding those documents as context to an LLM for answer generation." } diff --git a/internal/application/repository/wiki_page.go b/internal/application/repository/wiki_page.go index c43b01ee..f26d3cdf 100644 --- a/internal/application/repository/wiki_page.go +++ b/internal/application/repository/wiki_page.go @@ -118,10 +118,11 @@ func (r *wikiPageRepository) List(ctx context.Context, req *types.WikiPageListRe query = query.Where("status = ?", req.Status) } if req.Query != "" { - // Use PostgreSQL full-text search + // Use PostgreSQL full-text search + ILIKE for aliases query = query.Where( - "to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(content, '')) @@ plainto_tsquery('simple', ?)", + "(to_tsvector('simple', coalesce(title, '') || ' ' || coalesce(content, '')) @@ plainto_tsquery('simple', ?) OR aliases::text ILIKE ?)", req.Query, + "%"+req.Query+"%", ) } diff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.go index 9b14d00b..987724bd 100644 --- a/internal/application/service/wiki_ingest.go +++ b/internal/application/service/wiki_ingest.go @@ -916,10 +916,11 @@ func (s *wikiIngestService) retractStalePages( // extractedItem represents a single extracted entity or concept type extractedItem struct { - Name string `json:"name"` - Slug string `json:"slug"` - Description string `json:"description"` - Details string `json:"details"` + Name string `json:"name"` + Slug string `json:"slug"` + Aliases []string `json:"aliases"` + Description string `json:"description"` + Details string `json:"details"` } // combinedExtraction represents the parsed result of the combined entity+concept extraction @@ -1057,6 +1058,19 @@ func (s *wikiIngestService) upsertExtractedPages( if updatedSummary != "" { existing.Summary = updatedSummary } + if len(item.Aliases) > 0 { + // Merge new aliases with existing ones, deduplicating + aliasMap := make(map[string]bool) + for _, alias := range existing.Aliases { + aliasMap[alias] = true + } + for _, newAlias := range item.Aliases { + if !aliasMap[newAlias] { + existing.Aliases = append(existing.Aliases, newAlias) + aliasMap[newAlias] = true + } + } + } existing.SourceRefs = appendUnique(existing.SourceRefs, sourceRef) if _, err := s.wikiService.UpdatePage(ctx, existing); err != nil { @@ -1075,6 +1089,7 @@ func (s *wikiIngestService) upsertExtractedPages( KnowledgeBaseID: payload.KnowledgeBaseID, Slug: item.Slug, Title: item.Name, + Aliases: item.Aliases, PageType: pageType, Status: types.WikiPageStatusDraft, Content: pageContent, @@ -1310,13 +1325,21 @@ func (s *wikiIngestService) deduplicateItems( // Build existing pages listing var existingBuf strings.Builder for _, p := range typedPages { - fmt.Fprintf(&existingBuf, "- slug: %s | title: %s\n", p.Slug, p.Title) + aliases := "" + if len(p.Aliases) > 0 { + aliases = fmt.Sprintf(" | aliases: %s", strings.Join(p.Aliases, ", ")) + } + fmt.Fprintf(&existingBuf, "- slug: %s | title: %s%s\n", p.Slug, p.Title, aliases) } // Build new items listing var newBuf strings.Builder for _, item := range items { - fmt.Fprintf(&newBuf, "- slug: %s | name: %s\n", item.Slug, item.Name) + aliases := "" + if len(item.Aliases) > 0 { + aliases = fmt.Sprintf(" | aliases: %s", strings.Join(item.Aliases, ", ")) + } + fmt.Fprintf(&newBuf, "- slug: %s | name: %s%s\n", item.Slug, item.Name, aliases) } // Call LLM for deduplication diff --git a/internal/types/wiki_page.go b/internal/types/wiki_page.go index c238886d..3b1f68ab 100644 --- a/internal/types/wiki_page.go +++ b/internal/types/wiki_page.go @@ -63,6 +63,8 @@ type WikiPage struct { Content string `json:"content" gorm:"type:text"` // One-line summary for index listing Summary string `json:"summary" gorm:"type:text"` + // Alternate names, abbreviations, acronyms or translated names + Aliases StringArray `json:"aliases" gorm:"type:json"` // References to source knowledge IDs that contributed to this page SourceRefs StringArray `json:"source_refs" gorm:"type:json"` // Slugs of pages that link TO this page (backlinks)