feat: Add arrow toggle functionality in WikiBrowser component
Implemented new features in the WikiBrowser component to allow users to toggle the visibility of arrows on graph edges. This includes: 1. **Localization Updates**: Added translations for "Show arrows" and "Hide arrows" in English, Korean, Russian, and Chinese. 2. **Graph Rendering Enhancements**: Introduced logic to dynamically update SVG line attributes based on the toggle state, improving visual clarity in graph representations. 3. **State Management**: Added reactive state management for arrow visibility, ensuring seamless user interaction. These changes enhance the user experience by providing more control over graph visualizations.
This commit is contained in:
@@ -13,6 +13,9 @@ import (
|
||||
// ErrWikiPageNotFound is returned when a wiki page is not found
|
||||
var ErrWikiPageNotFound = errors.New("wiki page not found")
|
||||
|
||||
// ErrWikiPageConflict is returned when an optimistic lock conflict is detected
|
||||
var ErrWikiPageConflict = errors.New("wiki page version conflict")
|
||||
|
||||
// wikiPageRepository implements the WikiPageRepository interface
|
||||
type wikiPageRepository struct {
|
||||
db *gorm.DB
|
||||
@@ -28,9 +31,46 @@ func (r *wikiPageRepository) Create(ctx context.Context, page *types.WikiPage) e
|
||||
return r.db.WithContext(ctx).Create(page).Error
|
||||
}
|
||||
|
||||
// Update updates an existing wiki page record
|
||||
// Update updates an existing wiki page record with optimistic locking.
|
||||
// Increments version — use only for content changes visible to the user.
|
||||
// The caller must set page.Version to the expected current version.
|
||||
func (r *wikiPageRepository) Update(ctx context.Context, page *types.WikiPage) error {
|
||||
result := r.db.WithContext(ctx).Save(page)
|
||||
expectedVersion := page.Version
|
||||
page.Version = expectedVersion + 1
|
||||
|
||||
result := r.db.WithContext(ctx).
|
||||
Model(page).
|
||||
Where("id = ? AND version = ?", page.ID, expectedVersion).
|
||||
Updates(page)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
// Could be not found or version conflict — check which
|
||||
var count int64
|
||||
r.db.WithContext(ctx).Model(&types.WikiPage{}).Where("id = ?", page.ID).Count(&count)
|
||||
if count == 0 {
|
||||
return ErrWikiPageNotFound
|
||||
}
|
||||
return ErrWikiPageConflict
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateMeta updates only metadata fields (links, status, source_refs) WITHOUT
|
||||
// incrementing the version number. Used for internal bookkeeping operations
|
||||
// like link maintenance, status changes, and source ref updates.
|
||||
func (r *wikiPageRepository) UpdateMeta(ctx context.Context, page *types.WikiPage) error {
|
||||
result := r.db.WithContext(ctx).
|
||||
Model(page).
|
||||
Where("id = ?", page.ID).
|
||||
Updates(map[string]interface{}{
|
||||
"in_links": page.InLinks,
|
||||
"out_links": page.OutLinks,
|
||||
"status": page.Status,
|
||||
"source_refs": page.SourceRefs,
|
||||
"updated_at": page.UpdatedAt,
|
||||
})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
@@ -1284,7 +1284,7 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbI
|
||||
}
|
||||
}
|
||||
|
||||
var archivedSlugs []string
|
||||
var deletedSlugs []string
|
||||
var retractSlugs []string
|
||||
for _, page := range pages {
|
||||
// Skip index/log system pages
|
||||
@@ -1296,18 +1296,16 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbI
|
||||
remaining := removeSourceRef(page.SourceRefs, knowledgeID)
|
||||
|
||||
if len(remaining) == 0 {
|
||||
// No other sources → archive the page
|
||||
page.Status = types.WikiPageStatusArchived
|
||||
page.SourceRefs = remaining
|
||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||
logger.Warnf(ctx, "wiki cleanup: failed to archive page %s: %v", page.Slug, err)
|
||||
// No other sources → delete the page entirely
|
||||
if err := s.wikiService.DeletePage(ctx, kbID, page.Slug); err != nil {
|
||||
logger.Warnf(ctx, "wiki cleanup: failed to delete page %s: %v", page.Slug, err)
|
||||
} else {
|
||||
archivedSlugs = append(archivedSlugs, page.Slug)
|
||||
deletedSlugs = append(deletedSlugs, page.Slug)
|
||||
}
|
||||
} else {
|
||||
// Other sources remain → remove source ref and queue LLM content retraction
|
||||
page.SourceRefs = remaining
|
||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||
if err := s.wikiService.UpdatePageMeta(ctx, page); err != nil {
|
||||
logger.Warnf(ctx, "wiki cleanup: failed to update source refs for page %s: %v", page.Slug, err)
|
||||
} else {
|
||||
retractSlugs = append(retractSlugs, page.Slug)
|
||||
@@ -1315,9 +1313,9 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbI
|
||||
}
|
||||
}
|
||||
|
||||
if len(archivedSlugs) > 0 {
|
||||
logger.Infof(ctx, "wiki cleanup: archived %d pages after knowledge %s deletion: %v",
|
||||
len(archivedSlugs), knowledgeID, archivedSlugs)
|
||||
if len(deletedSlugs) > 0 {
|
||||
logger.Infof(ctx, "wiki cleanup: deleted %d pages after knowledge %s deletion: %v",
|
||||
len(deletedSlugs), knowledgeID, deletedSlugs)
|
||||
}
|
||||
|
||||
// Enqueue async LLM retraction for multi-source pages
|
||||
@@ -1334,6 +1332,11 @@ func (s *knowledgeService) cleanupWikiOnKnowledgeDelete(ctx context.Context, kbI
|
||||
})
|
||||
logger.Infof(ctx, "wiki cleanup: enqueued retract task for %d pages: %v", len(retractSlugs), retractSlugs)
|
||||
}
|
||||
|
||||
// Rebuild index page to reflect deleted pages
|
||||
if len(deletedSlugs) > 0 {
|
||||
s.rebuildWikiIndexSimple(ctx, kbID, docTitle, deletedSlugs)
|
||||
}
|
||||
}
|
||||
|
||||
// removeSourceRef removes entries from source_refs that match a knowledge ID.
|
||||
@@ -1350,6 +1353,67 @@ func removeSourceRef(refs types.StringArray, knowledgeID string) types.StringArr
|
||||
return result
|
||||
}
|
||||
|
||||
// rebuildWikiIndexSimple regenerates the index page from current page listing
|
||||
// and appends a deletion log entry. Does NOT use LLM — generates a simple
|
||||
// structured listing. Used during document deletion when LLM is not available.
|
||||
func (s *knowledgeService) rebuildWikiIndexSimple(ctx context.Context, kbID, docTitle string, deletedSlugs []string) {
|
||||
// Rebuild index
|
||||
resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{
|
||||
KnowledgeBaseID: kbID,
|
||||
PageSize: 500,
|
||||
SortBy: "page_type",
|
||||
SortOrder: "asc",
|
||||
})
|
||||
if err != nil || resp == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var indexContent strings.Builder
|
||||
indexContent.WriteString("# Wiki Index\n\n")
|
||||
|
||||
currentType := ""
|
||||
for _, p := range resp.Pages {
|
||||
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog {
|
||||
continue
|
||||
}
|
||||
if p.Status == types.WikiPageStatusArchived {
|
||||
continue
|
||||
}
|
||||
if p.PageType != currentType {
|
||||
currentType = p.PageType
|
||||
label := currentType
|
||||
if len(label) > 0 {
|
||||
label = strings.ToUpper(label[:1]) + label[1:]
|
||||
}
|
||||
fmt.Fprintf(&indexContent, "\n## %s\n\n", label)
|
||||
}
|
||||
fmt.Fprintf(&indexContent, "- [[%s]] — %s\n", p.Slug, p.Summary)
|
||||
}
|
||||
|
||||
indexPage, _ := s.wikiService.GetIndex(ctx, kbID)
|
||||
if indexPage != nil {
|
||||
indexPage.Content = indexContent.String()
|
||||
if _, err := s.wikiService.UpdatePage(ctx, indexPage); err != nil {
|
||||
logger.Warnf(ctx, "wiki cleanup: failed to rebuild index: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Append log entry
|
||||
logPage, _ := s.wikiService.GetLog(ctx, kbID)
|
||||
if logPage != nil {
|
||||
entry := fmt.Sprintf("\n## [%s] delete | %s\n- **Pages deleted**: %d (%s)\n",
|
||||
time.Now().UTC().Format("2006-01-02 15:04"),
|
||||
docTitle,
|
||||
len(deletedSlugs),
|
||||
strings.Join(deletedSlugs, ", "),
|
||||
)
|
||||
logPage.Content = logPage.Content + entry
|
||||
if _, err := s.wikiService.UpdatePage(ctx, logPage); err != nil {
|
||||
logger.Warnf(ctx, "wiki cleanup: failed to update log: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteKnowledgeList deletes a knowledge entry and all related resources
|
||||
func (s *knowledgeService) DeleteKnowledgeList(ctx context.Context, ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -22,6 +23,57 @@ const (
|
||||
maxContentForWiki = 32768
|
||||
)
|
||||
|
||||
// wikiKBLock provides distributed per-KB locking for wiki operations using Redis.
|
||||
// Ensures that concurrent ingest/retract tasks for the SAME knowledge base
|
||||
// run sequentially across all instances, preventing race conditions on shared wiki pages.
|
||||
// Different KBs can still run in parallel.
|
||||
const (
|
||||
wikiLockPrefix = "wiki:lock:"
|
||||
wikiLockTTL = 30 * time.Minute // max time a single ingest/retract can hold the lock
|
||||
wikiLockRetry = 500 * time.Millisecond
|
||||
)
|
||||
|
||||
func acquireWikiLock(ctx context.Context, redisClient *redis.Client, kbID string) (string, error) {
|
||||
if redisClient == nil {
|
||||
return "", nil // No Redis (Lite mode) — no distributed lock, rely on single-instance
|
||||
}
|
||||
lockKey := wikiLockPrefix + kbID
|
||||
lockValue := uuid.New().String()
|
||||
|
||||
deadline := time.Now().Add(wikiLockTTL) // don't wait longer than the lock TTL itself
|
||||
for time.Now().Before(deadline) {
|
||||
ok, err := redisClient.SetNX(ctx, lockKey, lockValue, wikiLockTTL).Result()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("wiki lock: redis error: %w", err)
|
||||
}
|
||||
if ok {
|
||||
return lockValue, nil
|
||||
}
|
||||
// Another task holds the lock — wait and retry
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
case <-time.After(wikiLockRetry):
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("wiki lock: timeout acquiring lock for KB %s", kbID)
|
||||
}
|
||||
|
||||
func releaseWikiLock(ctx context.Context, redisClient *redis.Client, kbID, lockValue string) {
|
||||
if redisClient == nil || lockValue == "" {
|
||||
return
|
||||
}
|
||||
lockKey := wikiLockPrefix + kbID
|
||||
// Only release if we still own the lock (atomic check-and-delete via Lua)
|
||||
script := redis.NewScript(`
|
||||
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("DEL", KEYS[1])
|
||||
end
|
||||
return 0
|
||||
`)
|
||||
script.Run(ctx, redisClient, []string{lockKey}, lockValue)
|
||||
}
|
||||
|
||||
// WikiIngestPayload is the asynq task payload for wiki ingest
|
||||
type WikiIngestPayload struct {
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
@@ -47,6 +99,7 @@ type wikiIngestService struct {
|
||||
chunkRepo interfaces.ChunkRepository
|
||||
modelService interfaces.ModelService
|
||||
task interfaces.TaskEnqueuer
|
||||
redisClient *redis.Client // nil in Lite mode (no Redis)
|
||||
}
|
||||
|
||||
// NewWikiIngestService creates a new wiki ingest service
|
||||
@@ -56,6 +109,7 @@ func NewWikiIngestService(
|
||||
chunkRepo interfaces.ChunkRepository,
|
||||
modelService interfaces.ModelService,
|
||||
task interfaces.TaskEnqueuer,
|
||||
redisClient *redis.Client,
|
||||
) interfaces.TaskHandler {
|
||||
return &wikiIngestService{
|
||||
wikiService: wikiService,
|
||||
@@ -63,6 +117,7 @@ func NewWikiIngestService(
|
||||
chunkRepo: chunkRepo,
|
||||
modelService: modelService,
|
||||
task: task,
|
||||
redisClient: redisClient,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,8 +154,33 @@ func EnqueueWikiRetract(ctx context.Context, task interfaces.TaskEnqueuer, paylo
|
||||
}
|
||||
}
|
||||
|
||||
// Handle implements interfaces.TaskHandler for asynq task processing
|
||||
// Handle implements interfaces.TaskHandler for asynq task processing.
|
||||
// Acquires a distributed per-KB lock (Redis) to serialize wiki operations
|
||||
// within the same knowledge base, preventing race conditions.
|
||||
// Different KBs run fully in parallel.
|
||||
func (s *wikiIngestService) Handle(ctx context.Context, t *asynq.Task) error {
|
||||
// Extract KB ID from payload for locking
|
||||
var kbID string
|
||||
switch t.Type() {
|
||||
case types.TypeWikiRetract:
|
||||
var p WikiRetractPayload
|
||||
if err := json.Unmarshal(t.Payload(), &p); err == nil {
|
||||
kbID = p.KnowledgeBaseID
|
||||
}
|
||||
default:
|
||||
var p WikiIngestPayload
|
||||
if err := json.Unmarshal(t.Payload(), &p); err == nil {
|
||||
kbID = p.KnowledgeBaseID
|
||||
}
|
||||
}
|
||||
|
||||
// Acquire distributed lock for this KB (blocks until available or timeout)
|
||||
lockValue, err := acquireWikiLock(ctx, s.redisClient, kbID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("wiki: failed to acquire KB lock: %w", err)
|
||||
}
|
||||
defer releaseWikiLock(ctx, s.redisClient, kbID, lockValue)
|
||||
|
||||
switch t.Type() {
|
||||
case types.TypeWikiRetract:
|
||||
return s.ProcessWikiRetract(ctx, t)
|
||||
@@ -198,6 +278,9 @@ func (s *wikiIngestService) ProcessWikiRetract(ctx context.Context, t *asynq.Tas
|
||||
KnowledgeBaseID: payload.KnowledgeBaseID,
|
||||
}, payload.DocTitle+" [DELETED]", payload.PageSlugs, nil)
|
||||
|
||||
// Clean up dead links pointing to archived/deleted pages
|
||||
s.cleanDeadLinks(ctx, payload.KnowledgeBaseID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -343,7 +426,11 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
||||
// Step 5: Append to log page with synthesis suggestions
|
||||
s.appendLogEntry(ctx, payload, docTitle, pagesAffected, synthesisSuggestions)
|
||||
|
||||
// Step 6: Publish all draft pages created during this ingest
|
||||
// Step 6: Cross-link injection — scan all affected pages and inject [[wiki-links]]
|
||||
// for mentions of other wiki page titles. Pure text matching, no LLM call.
|
||||
s.injectCrossLinks(ctx, payload.KnowledgeBaseID, pagesAffected)
|
||||
|
||||
// Step 7: Publish all draft pages created during this ingest
|
||||
s.publishDraftPages(ctx, payload.KnowledgeBaseID, pagesAffected)
|
||||
|
||||
// Step 7: Handle stale pages — pages that previously referenced this document
|
||||
@@ -357,6 +444,195 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task
|
||||
return nil
|
||||
}
|
||||
|
||||
// cleanDeadLinks removes [[wiki-links]] that point to archived or deleted pages.
|
||||
// Scans all published pages, checks each out_link, and removes references to
|
||||
// pages that no longer exist or are archived. No LLM call — pure text cleanup.
|
||||
func (s *wikiIngestService) cleanDeadLinks(ctx context.Context, kbID string) {
|
||||
resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{
|
||||
KnowledgeBaseID: kbID,
|
||||
PageSize: 500,
|
||||
})
|
||||
if err != nil || resp == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Build set of live (non-archived, non-system) slugs
|
||||
liveSlugs := make(map[string]bool)
|
||||
for _, p := range resp.Pages {
|
||||
if p.Status != types.WikiPageStatusArchived {
|
||||
liveSlugs[p.Slug] = true
|
||||
}
|
||||
}
|
||||
|
||||
var cleaned int
|
||||
for _, p := range resp.Pages {
|
||||
if p.Status == types.WikiPageStatusArchived {
|
||||
continue
|
||||
}
|
||||
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog {
|
||||
continue
|
||||
}
|
||||
|
||||
content := p.Content
|
||||
changed := false
|
||||
|
||||
// Find all [[slug]] references and remove dead ones
|
||||
for _, outSlug := range p.OutLinks {
|
||||
if liveSlugs[outSlug] {
|
||||
continue // link is alive
|
||||
}
|
||||
// Dead link — remove the [[slug]] from content, keep the display text if any
|
||||
linkPattern := "[[" + outSlug + "]]"
|
||||
if strings.Contains(content, linkPattern) {
|
||||
// Replace [[dead-slug]] with just the slug's readable part
|
||||
parts := strings.Split(outSlug, "/")
|
||||
readableName := parts[len(parts)-1]
|
||||
readableName = strings.ReplaceAll(readableName, "-", " ")
|
||||
content = strings.ReplaceAll(content, linkPattern, readableName)
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
|
||||
if changed {
|
||||
p.Content = content
|
||||
if _, err := s.wikiService.UpdatePage(ctx, p); err != nil {
|
||||
logger.Warnf(ctx, "wiki: failed to clean dead links in page %s: %v", p.Slug, err)
|
||||
} else {
|
||||
cleaned++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cleaned > 0 {
|
||||
logger.Infof(ctx, "wiki: cleaned dead links in %d pages", cleaned)
|
||||
}
|
||||
}
|
||||
|
||||
// injectCrossLinks scans affected pages and injects [[wiki-links]] for mentions
|
||||
// of other wiki page titles in the content. Pure text replacement, no LLM call.
|
||||
// Only processes entity/concept/synthesis/comparison pages (not index/log/summary).
|
||||
func (s *wikiIngestService) injectCrossLinks(ctx context.Context, kbID string, affectedSlugs []string) {
|
||||
// Build a title→slug lookup from ALL pages in this KB
|
||||
resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{
|
||||
KnowledgeBaseID: kbID,
|
||||
PageSize: 500,
|
||||
})
|
||||
if err != nil || resp == nil || len(resp.Pages) < 2 {
|
||||
return
|
||||
}
|
||||
|
||||
type pageRef struct {
|
||||
slug string
|
||||
title string
|
||||
}
|
||||
var allRefs []pageRef
|
||||
for _, p := range resp.Pages {
|
||||
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog {
|
||||
continue
|
||||
}
|
||||
if p.Title != "" {
|
||||
allRefs = append(allRefs, pageRef{slug: p.Slug, title: p.Title})
|
||||
}
|
||||
}
|
||||
if len(allRefs) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// Sort by title length descending — match longer names first to avoid
|
||||
// partial matches (e.g. "北京邮电大学" before "北京")
|
||||
for i := 0; i < len(allRefs); i++ {
|
||||
for j := i + 1; j < len(allRefs); j++ {
|
||||
if len([]rune(allRefs[j].title)) > len([]rune(allRefs[i].title)) {
|
||||
allRefs[i], allRefs[j] = allRefs[j], allRefs[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process only the pages we just created/updated
|
||||
affectedSet := make(map[string]bool, len(affectedSlugs))
|
||||
for _, s := range affectedSlugs {
|
||||
affectedSet[s] = true
|
||||
}
|
||||
|
||||
var updated int
|
||||
for _, p := range resp.Pages {
|
||||
if !affectedSet[p.Slug] {
|
||||
continue
|
||||
}
|
||||
// Skip system pages and summary (summary already has links from the prompt)
|
||||
if p.PageType == types.WikiPageTypeIndex || p.PageType == types.WikiPageTypeLog || p.PageType == types.WikiPageTypeSummary {
|
||||
continue
|
||||
}
|
||||
|
||||
content := p.Content
|
||||
changed := false
|
||||
|
||||
for _, ref := range allRefs {
|
||||
// Don't self-link
|
||||
if ref.slug == p.Slug {
|
||||
continue
|
||||
}
|
||||
// Skip if already linked with this slug
|
||||
if strings.Contains(content, "[["+ref.slug+"]]") {
|
||||
continue
|
||||
}
|
||||
// Replace title mentions with [[slug]] link
|
||||
// Only replace if the title appears as a standalone term (not inside an existing link)
|
||||
if strings.Contains(content, ref.title) {
|
||||
// Replace first occurrence only (avoid over-linking)
|
||||
content = replaceFirstOutsideLinks(content, ref.title, ref.title+" [["+ref.slug+"]]")
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
|
||||
if changed {
|
||||
p.Content = content
|
||||
if _, err := s.wikiService.UpdatePage(ctx, p); err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: cross-link injection failed for %s: %v", p.Slug, err)
|
||||
} else {
|
||||
updated++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if updated > 0 {
|
||||
logger.Infof(ctx, "wiki ingest: injected cross-links in %d pages", updated)
|
||||
}
|
||||
}
|
||||
|
||||
// replaceFirstOutsideLinks replaces the first occurrence of `old` with `new` in s,
|
||||
// but only if it's not already inside a [[...]] wiki link.
|
||||
func replaceFirstOutsideLinks(s, old, newStr string) string {
|
||||
idx := 0
|
||||
for {
|
||||
pos := strings.Index(s[idx:], old)
|
||||
if pos < 0 {
|
||||
return s // not found
|
||||
}
|
||||
absPos := idx + pos
|
||||
|
||||
// Check if this occurrence is inside a [[ ... ]] link
|
||||
// Look backwards for [[ without encountering ]]
|
||||
insideLink := false
|
||||
for i := absPos - 1; i >= 0 && i >= absPos-200; i-- {
|
||||
if i > 0 && s[i-1:i+1] == "]]" {
|
||||
break // closed link before us
|
||||
}
|
||||
if i > 0 && s[i-1:i+1] == "[[" {
|
||||
insideLink = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !insideLink {
|
||||
return s[:absPos] + newStr + s[absPos+len(old):]
|
||||
}
|
||||
|
||||
// Skip this match, try next
|
||||
idx = absPos + len(old)
|
||||
}
|
||||
}
|
||||
|
||||
// getExistingPageSlugsForKnowledge returns all page slugs that currently reference
|
||||
// a given knowledge ID in their source_refs. Used to snapshot state before re-ingest.
|
||||
func (s *wikiIngestService) getExistingPageSlugsForKnowledge(ctx context.Context, kbID, knowledgeID string) map[string]bool {
|
||||
@@ -438,16 +714,14 @@ func (s *wikiIngestService) retractStalePages(
|
||||
}
|
||||
|
||||
if len(remaining) == 0 {
|
||||
// No other sources → archive
|
||||
page.Status = types.WikiPageStatusArchived
|
||||
page.SourceRefs = remaining
|
||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to archive stale page %s: %v", slug, err)
|
||||
// No other sources → delete the page
|
||||
if err := s.wikiService.DeletePage(ctx, payload.KnowledgeBaseID, slug); err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to delete stale page %s: %v", slug, err)
|
||||
}
|
||||
} else {
|
||||
// Multi-source → remove ref, queue retract
|
||||
page.SourceRefs = remaining
|
||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||
if err := s.wikiService.UpdatePageMeta(ctx, page); err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to update stale page %s: %v", slug, err)
|
||||
} else {
|
||||
retractSlugs = append(retractSlugs, slug)
|
||||
@@ -773,7 +1047,7 @@ func (s *wikiIngestService) publishDraftPages(ctx context.Context, kbID string,
|
||||
}
|
||||
if page.Status == types.WikiPageStatusDraft {
|
||||
page.Status = types.WikiPageStatusPublished
|
||||
if _, err := s.wikiService.UpdatePage(ctx, page); err != nil {
|
||||
if err := s.wikiService.UpdatePageMeta(ctx, page); err != nil {
|
||||
logger.Warnf(ctx, "wiki ingest: failed to publish page %s: %v", slug, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)
|
||||
|
||||
oldOutLinks := existing.OutLinks
|
||||
|
||||
// Update fields
|
||||
// Update fields (version is incremented by the repository's optimistic lock)
|
||||
existing.Title = page.Title
|
||||
existing.Content = page.Content
|
||||
existing.Summary = page.Summary
|
||||
@@ -95,7 +95,6 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)
|
||||
existing.SourceRefs = page.SourceRefs
|
||||
existing.PageMetadata = page.PageMetadata
|
||||
existing.Status = page.Status
|
||||
existing.Version++
|
||||
existing.UpdatedAt = time.Now()
|
||||
|
||||
// Re-parse outbound links
|
||||
@@ -117,6 +116,12 @@ func (s *wikiPageService) UpdatePage(ctx context.Context, page *types.WikiPage)
|
||||
return existing, nil
|
||||
}
|
||||
|
||||
// UpdatePageMeta updates only metadata (status, source_refs) without version bump or link re-parse.
|
||||
func (s *wikiPageService) UpdatePageMeta(ctx context.Context, page *types.WikiPage) error {
|
||||
page.UpdatedAt = time.Now()
|
||||
return s.repo.UpdateMeta(ctx, page)
|
||||
}
|
||||
|
||||
// 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)
|
||||
@@ -325,10 +330,10 @@ func (s *wikiPageService) RebuildLinks(ctx context.Context, kbID string) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Save all pages
|
||||
// Save all pages (link rebuild is metadata-only, no version bump)
|
||||
for _, p := range pages {
|
||||
p.UpdatedAt = time.Now()
|
||||
if err := s.repo.Update(ctx, p); err != nil {
|
||||
if err := s.repo.UpdateMeta(ctx, p); err != nil {
|
||||
logger.Warnf(ctx, "wiki: failed to update links for page %s: %v", p.Slug, err)
|
||||
}
|
||||
}
|
||||
@@ -383,7 +388,7 @@ func (s *wikiPageService) updateInLinks(ctx context.Context, kbID string, source
|
||||
if !containsString(targetPage.InLinks, sourceSlug) {
|
||||
targetPage.InLinks = append(targetPage.InLinks, sourceSlug)
|
||||
targetPage.UpdatedAt = time.Now()
|
||||
if err := s.repo.Update(ctx, targetPage); err != nil {
|
||||
if err := s.repo.UpdateMeta(ctx, targetPage); err != nil {
|
||||
logger.Warnf(ctx, "wiki: failed to update in_links for %s: %v", targetSlug, err)
|
||||
}
|
||||
}
|
||||
@@ -401,7 +406,7 @@ func (s *wikiPageService) removeInLinks(ctx context.Context, kbID string, source
|
||||
if len(newInLinks) != len(targetPage.InLinks) {
|
||||
targetPage.InLinks = newInLinks
|
||||
targetPage.UpdatedAt = time.Now()
|
||||
if err := s.repo.Update(ctx, targetPage); err != nil {
|
||||
if err := s.repo.UpdateMeta(ctx, targetPage); err != nil {
|
||||
logger.Warnf(ctx, "wiki: failed to update in_links for %s: %v", targetSlug, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user