From c5494f612b6f68572418ec9249e702d43c2248bd Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 23 Apr 2026 11:16:47 +0800 Subject: [PATCH] feat: Implement auto-linked content updates in wiki page management - Introduced UpdateAutoLinkedContent method to persist changes from machine-only link decorators without incrementing the version number, ensuring user-facing revisions remain consistent. - Updated existing methods to utilize the new auto-linked content update logic, enhancing the handling of cross-link injection and dead-link cleanup. - Enhanced the KnowledgeBase and WikiBrowser components to improve user experience during wiki operations, including status tracking and indexing indicators. - Added scheduling for wiki status probes to provide timely feedback on indexing processes after user-triggered actions. --- .../src/views/knowledge/KnowledgeBase.vue | 28 +++++++++++++++++- .../src/views/knowledge/wiki/WikiBrowser.vue | 21 ++++++++++++++ internal/application/repository/wiki_page.go | 24 +++++++++++++++ internal/application/service/wiki_ingest.go | 4 +-- internal/application/service/wiki_lint.go | 2 +- internal/application/service/wiki_page.go | 29 ++++++++++++++++++- internal/types/interfaces/wiki_page.go | 16 ++++++++++ 7 files changed, 119 insertions(+), 5 deletions(-) diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue index 33f84386..104cf5e0 100644 --- a/frontend/src/views/knowledge/KnowledgeBase.vue +++ b/frontend/src/views/knowledge/KnowledgeBase.vue @@ -69,12 +69,17 @@ const onWikiStatusChange = (payload: { pendingTasks: number; isActive: boolean; } let wikiStatusTimer: ReturnType | null = null +let wikiStatusProbeTimers: Array> = [] const stopWikiStatusPolling = () => { if (wikiStatusTimer) { clearInterval(wikiStatusTimer) wikiStatusTimer = null } } +const clearWikiStatusProbes = () => { + wikiStatusProbeTimers.forEach(t => clearTimeout(t)) + wikiStatusProbeTimers = [] +} const fetchWikiStatusOnce = async () => { if (!kbId.value || !isWiki.value) return try { @@ -96,14 +101,31 @@ const fetchWikiStatusOnce = async () => { } } catch (_) { /* ignore */ } } +// 用户刚触发了一个上传 / reparse / URL 导入之类的动作后,后台通常要过 +// 一小段时间才会把 wiki 任务真正塞进队列;如果这时空闲轮询刚好停了, +// 面包屑的"索引中"会延迟很久才亮起。所以这里安排几次退避重试, +// 主动把面包屑的 loading 尽快点亮,一旦探测到任务就会走正常的 5s 轮询。 +const scheduleWikiStatusProbes = () => { + if (!kbId.value || !isWiki.value) return + clearWikiStatusProbes() + const delays = [500, 2000, 5000, 10000] + delays.forEach(delay => { + const timer = setTimeout(() => { fetchWikiStatusOnce() }, delay) + wikiStatusProbeTimers.push(timer) + }) +} watch([kbId, isWiki], ([newKbId, newIsWiki]) => { stopWikiStatusPolling() + clearWikiStatusProbes() wikiStatus.value = { pendingTasks: 0, isActive: false, pendingIssues: 0 } if (newKbId && newIsWiki) { fetchWikiStatusOnce() } }, { immediate: true }) -onUnmounted(stopWikiStatusPolling) +onUnmounted(() => { + stopWikiStatusPolling() + clearWikiStatusProbes() +}) const missingStorageEngine = computed(() => { if (!kbInfo.value || isFAQ.value) return false const spc = kbInfo.value.storage_provider_config @@ -737,6 +759,8 @@ const handleFileUploaded = (event: CustomEvent) => { page = 1; // Reset page counter when reloading files after upload loadKnowledgeFiles(uploadedKbId); loadTags(uploadedKbId); + // 启动几次探测,尽快让面包屑的"索引中"亮起。 + scheduleWikiStatusProbes(); } }; @@ -1541,6 +1565,8 @@ const rebuildConfirm = async () => { MessagePlugin.success(t('knowledgeBase.rebuildSubmitted')); page = 1; // Reset page counter when reloading files after reparse loadKnowledgeFiles(kbId.value); + // reparse 同样会触发 wiki 重入队,探测一下让面包屑尽快亮起。 + scheduleWikiStatusProbes(); } catch (error: any) { MessagePlugin.error(error?.message || t('knowledgeBase.rebuildFailed')); } diff --git a/frontend/src/views/knowledge/wiki/WikiBrowser.vue b/frontend/src/views/knowledge/wiki/WikiBrowser.vue index 19f539e8..ef540913 100644 --- a/frontend/src/views/knowledge/wiki/WikiBrowser.vue +++ b/frontend/src/views/knowledge/wiki/WikiBrowser.vue @@ -2140,6 +2140,27 @@ onUnmounted(() => { color: var(--td-text-color-primary); } +.wiki-reader-aliases { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: 6px 8px; + margin: 0 0 10px; + font-size: 13px; + line-height: 1.4; +} + +.wiki-alias-label { + color: var(--td-text-color-placeholder); + font-size: 13px; + line-height: 1.4; +} + +.wiki-alias-tag { + // Slight vertical nudge so the tag baseline lines up with the label. + vertical-align: middle; +} + .wiki-reader-meta { display: flex; align-items: center; diff --git a/internal/application/repository/wiki_page.go b/internal/application/repository/wiki_page.go index c5eef574..f1686b8a 100644 --- a/internal/application/repository/wiki_page.go +++ b/internal/application/repository/wiki_page.go @@ -59,6 +59,30 @@ func (r *wikiPageRepository) Update(ctx context.Context, page *types.WikiPage) e return nil } +// UpdateAutoLinkedContent persists content changes produced by the automatic +// link decorators (cross-link injection, dead-link cleanup) without bumping +// `version`. These passes rewrite the same revision with wiki-link markup +// added or removed; treating them as real edits would make newly-ingested +// pages appear as v2 on first view and confuse users who expect `version` to +// correspond to the number of intentional revisions. +func (r *wikiPageRepository) UpdateAutoLinkedContent(ctx context.Context, page *types.WikiPage) error { + result := r.db.WithContext(ctx). + Model(page). + Where("id = ?", page.ID). + Updates(map[string]interface{}{ + "content": page.Content, + "out_links": page.OutLinks, + "updated_at": page.UpdatedAt, + }) + if result.Error != nil { + return result.Error + } + if result.RowsAffected == 0 { + return ErrWikiPageNotFound + } + return nil +} + // UpdateMeta updates bookkeeping / provenance fields WITHOUT incrementing the // version number. "Content" for versioning purposes is the user-visible page // body (title/content/summary/page_type/status); everything else — links, diff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.go index 53b18034..f0d9027e 100644 --- a/internal/application/service/wiki_ingest.go +++ b/internal/application/service/wiki_ingest.go @@ -460,7 +460,7 @@ func (s *wikiIngestService) cleanDeadLinks(ctx context.Context, kbID string) { if changed { p.Content = content - if _, err := s.wikiService.UpdatePage(ctx, p); err != nil { + if err := s.wikiService.UpdateAutoLinkedContent(ctx, p); err != nil { logger.Warnf(ctx, "wiki: failed to clean dead links in page %s: %v", p.Slug, err) } else { cleaned++ @@ -509,7 +509,7 @@ func (s *wikiIngestService) injectCrossLinks(ctx context.Context, kbID string, a continue } p.Content = newContent - if _, err := s.wikiService.UpdatePage(ctx, p); err != nil { + if err := s.wikiService.UpdateAutoLinkedContent(ctx, p); err != nil { logger.Warnf(ctx, "wiki ingest: cross-link injection failed for %s: %v", p.Slug, err) continue } diff --git a/internal/application/service/wiki_lint.go b/internal/application/service/wiki_lint.go index 273f309b..b06a38b7 100644 --- a/internal/application/service/wiki_lint.go +++ b/internal/application/service/wiki_lint.go @@ -339,7 +339,7 @@ func (s *WikiLintService) AutoFix(ctx context.Context, kbID string) (int, error) } target := issue.TargetSlug page.Content = strings.ReplaceAll(page.Content, "[["+target+"]]", target) - if _, err := s.wikiService.UpdatePage(ctx, page); err == nil { + if err := s.wikiService.UpdateAutoLinkedContent(ctx, page); err == nil { fixed++ } diff --git a/internal/application/service/wiki_page.go b/internal/application/service/wiki_page.go index 4f1e2618..20a4cd08 100644 --- a/internal/application/service/wiki_page.go +++ b/internal/application/service/wiki_page.go @@ -144,6 +144,33 @@ func (s *wikiPageService) UpdatePageMeta(ctx context.Context, page *types.WikiPa return s.repo.UpdateMeta(ctx, page) } +// UpdateAutoLinkedContent persists content produced by machine-only link +// decorators (cross-link injection / dead-link cleanup) without bumping +// `version`. Out-links are re-parsed from the new body and bidirectional +// in-link references on target pages are refreshed so link navigation stays +// consistent — only the user-facing revision counter is preserved. +func (s *wikiPageService) UpdateAutoLinkedContent(ctx context.Context, page *types.WikiPage) error { + existing, err := s.repo.GetBySlug(ctx, page.KnowledgeBaseID, page.Slug) + if err != nil { + return fmt.Errorf("get existing page: %w", err) + } + + oldOutLinks := existing.OutLinks + + existing.Content = page.Content + existing.OutLinks = s.parseOutLinks(existing.Content) + existing.UpdatedAt = time.Now() + + if err := s.repo.UpdateAutoLinkedContent(ctx, existing); err != nil { + return fmt.Errorf("update auto-linked content: %w", err) + } + + s.removeInLinks(ctx, existing.KnowledgeBaseID, existing.Slug, oldOutLinks) + s.updateInLinks(ctx, existing.KnowledgeBaseID, existing.Slug, existing.OutLinks) + + return nil +} + // GetPageBySlug retrieves a wiki page by its slug func (s *wikiPageService) GetPageBySlug(ctx context.Context, kbID string, slug string) (*types.WikiPage, error) { return s.repo.GetBySlug(ctx, kbID, slug) @@ -573,7 +600,7 @@ func (s *wikiPageService) InjectCrossLinks(ctx context.Context, kbID string, aff continue } p.Content = newContent - if _, err := s.UpdatePage(ctx, p); err != nil { + if err := s.UpdateAutoLinkedContent(ctx, p); err != nil { logger.Warnf(ctx, "wiki: cross-link injection failed for %s: %v", p.Slug, err) continue } diff --git a/internal/types/interfaces/wiki_page.go b/internal/types/interfaces/wiki_page.go index f2d3ad7c..ca10dd68 100644 --- a/internal/types/interfaces/wiki_page.go +++ b/internal/types/interfaces/wiki_page.go @@ -27,6 +27,15 @@ type WikiPageService interface { // ref changes driven by internal reconciliation. UpdatePageMeta(ctx context.Context, page *types.WikiPage) error + // UpdateAutoLinkedContent persists content changes that come from + // machine-only link decoration — cross-link injection and dead-link + // cleanup — without bumping `version`. Out-links are re-parsed from the + // new body and bidirectional in-link references on target pages are + // refreshed. Conceptually the body still represents the same revision + // the user saw before, just with wiki-link markup added or stripped, so + // surfacing a version bump to end users is misleading. + UpdateAutoLinkedContent(ctx context.Context, page *types.WikiPage) error + // GetPageBySlug retrieves a wiki page by its slug within a knowledge base. GetPageBySlug(ctx context.Context, kbID string, slug string) (*types.WikiPage, error) @@ -103,6 +112,13 @@ type WikiPageRepository interface { // unchanged body, and status-only transitions. UpdateMeta(ctx context.Context, page *types.WikiPage) error + // UpdateAutoLinkedContent rewrites `content`, `out_links` and + // `updated_at` in place while leaving `version` untouched. Intended for + // machine-only link markup changes (cross-link injection / dead-link + // cleanup) so the first-ingest page doesn't jump to v2 just because the + // post-processor added a `[[...]]` wrapper. + UpdateAutoLinkedContent(ctx context.Context, page *types.WikiPage) error + // GetByID retrieves a wiki page by its unique ID. GetByID(ctx context.Context, id string) (*types.WikiPage, error)