diff --git a/frontend/src/views/knowledge/wiki/WikiBrowser.vue b/frontend/src/views/knowledge/wiki/WikiBrowser.vue index daf9eb05..d3c8e964 100644 --- a/frontend/src/views/knowledge/wiki/WikiBrowser.vue +++ b/frontend/src/views/knowledge/wiki/WikiBrowser.vue @@ -51,6 +51,15 @@
+ +
+
+ Recent Activity + {{ showLog ? '▼' : '▶' }} +
+
+
+
('pages') const searchQuery = ref('') const filterType = ref('') const graphRef = ref(null) +const logContent = ref('') +const showLog = ref(false) + +// Render log content as simple HTML (last 5 entries) +const renderedLog = computed(() => { + if (!logContent.value) return '' + // Take last ~5 entries (split by ## headings) + const entries = logContent.value.split(/(?=^## )/m).filter(e => e.trim()) + const recent = entries.slice(-5).reverse() + let html = recent.join('\n') + html = html.replace(/^## (.+)$/gm, '

$1

') + html = html.replace(/\*\*(.+?)\*\*/g, '$1') + html = html.replace(/^- (.+)$/gm, '
  • $1
  • ') + html = html.replace(/\n/g, '
    ') + return html +}) // Simple markdown-to-HTML rendering (replace wiki links with clickable ones) const renderedContent = computed(() => { @@ -188,6 +214,17 @@ async function loadStats() { } } +async function loadLog() { + try { + const res = await getWikiLog(props.knowledgeBaseId) + const page = (res as any).data || res as any + logContent.value = page?.content || '' + } catch (e) { + // Log page may not exist yet + logContent.value = '' + } +} + async function loadGraph() { try { const res = await getWikiGraph(props.knowledgeBaseId) @@ -244,6 +281,7 @@ function formatDate(dateStr: string) { onMounted(() => { loadPages() loadStats() + loadLog() }) @@ -374,6 +412,45 @@ onMounted(() => { padding: 40px; color: #999; } +.wiki-recent-activity { + margin-bottom: 16px; + border: 1px solid #e8e8e8; + border-radius: 8px; + overflow: hidden; +} +.wiki-recent-activity-header { + display: flex; + justify-content: space-between; + align-items: center; + padding: 10px 16px; + background: #fafafa; + cursor: pointer; + font-size: 14px; +} +.wiki-recent-activity-header:hover { + background: #f0f0f0; +} +.wiki-toggle { + font-size: 10px; + color: #999; +} +.wiki-recent-activity-content { + padding: 12px 16px; + font-size: 13px; + color: #555; + max-height: 300px; + overflow-y: auto; + line-height: 1.6; +} +.wiki-recent-activity-content :deep(h4) { + font-size: 14px; + margin: 8px 0 4px; + color: #333; +} +.wiki-recent-activity-content :deep(li) { + margin-left: 16px; + list-style: disc; +} .wiki-page-viewer { max-width: 800px; } diff --git a/internal/agent/prompts_wiki.go b/internal/agent/prompts_wiki.go index a575d1f6..f62d7e02 100644 --- a/internal/agent/prompts_wiki.go +++ b/internal/agent/prompts_wiki.go @@ -125,3 +125,65 @@ const WikiLogEntryTemplate = `## [{{.Date}}] {{.Operation}} | {{.Title}} - **Pages affected**: {{.PagesAffected}} - **Summary**: {{.Summary}} ` + +// WikiAgentSystemPromptAddendum is appended to the Agent system prompt when +// wiki knowledge bases are detected among the search targets. +// It tells the LLM how and when to use wiki tools. +const WikiAgentSystemPromptAddendum = ` +### Wiki Knowledge Base Guidelines + +You have access to a **Wiki Knowledge Base** — a persistent, interlinked collection of LLM-generated Markdown pages. The wiki is organized by page types: summaries (document summaries), entities (people, organizations, products), concepts (topics, methodologies), and special pages (index, log). + +#### Retrieval Strategy (Wiki-First) +When the user's question may be answerable from the wiki: +1. **Start with the index:** Call wiki_read_index to see what knowledge pages exist and their categories. +2. **Search if needed:** Call wiki_search with keywords to find relevant pages. +3. **Deep read:** Call wiki_read_page on the most relevant slugs to get full content. +4. **Follow links:** Wiki pages contain [[slug]] cross-references. Follow them to gather related context (1-2 hops). +5. **Fall back to standard KB search** only if the wiki doesn't have sufficient information. + +#### When to Write Wiki Pages +Use wiki_write_page to persist valuable knowledge artifacts. Write a page when: +- You produce a **cross-document synthesis** that combines insights from multiple sources (use page_type "synthesis") +- You generate a **comparison or evaluation** of entities, approaches, or concepts (use page_type "comparison") +- The user explicitly asks you to save analysis to the wiki + +**Do NOT** write wiki pages for: +- Simple factual answers that don't add new insight +- Conversational responses (greetings, clarifications) +- Content that already exists in an existing wiki page (update it instead) + +#### Page Content Guidelines +- Write in Markdown with proper heading hierarchy +- Use [[entity/slug]] and [[concept/slug]] syntax to link to other wiki pages +- Include a one-line summary in the first paragraph (used for index listings) +- Cite source documents when possible +- Keep pages focused: one topic/entity/concept per page + +#### Log Page +The wiki has a log page (slug: "log") that records all ingest and update activity. Read it when the user asks about recent changes, update history, or what's new in the knowledge base. +` + +// WikiDeduplicationPrompt asks the LLM to identify duplicate entities/concepts +// between newly extracted items and existing wiki pages. +const WikiDeduplicationPrompt = `You are a deduplication system. Given a list of newly extracted items and a list of existing wiki pages, determine which new items refer to the same entity or concept as an existing page. + +## Newly Extracted Items +{{.NewItems}} + +## Existing Wiki Pages +{{.ExistingPages}} + +## Instructions +For each newly extracted item, check if it refers to the same real-world entity or concept as any existing page. Consider: +- Name variations (e.g. "Acme Corp" vs "Acme Corporation", "RAG" vs "Retrieval-Augmented Generation") +- Abbreviations and full names +- Translations (e.g. "苹果公司" vs "Apple Inc.") +- Minor spelling/formatting differences + +Return a JSON object with a "merges" map. The key is the NEW item's slug, the value is the EXISTING page's slug that it should merge into. Only include items that have a match. + +If no items match any existing pages, return: {"merges": {}} + +Output ONLY valid JSON. Example: +{"merges": {"entity/acme-corporation": "entity/acme-corp", "concept/rag": "concept/retrieval-augmented-generation"}}` diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go index d22b6762..570cb80b 100644 --- a/internal/application/service/agent_service.go +++ b/internal/application/service/agent_service.go @@ -117,6 +117,15 @@ func (s *agentService) CreateAgentEngine( systemPromptTemplate = config.ResolveSystemPrompt(config.WebSearchEnabled) } + // 4.5 Append wiki guidelines if any search target is a wiki KB + for _, target := range config.SearchTargets { + kb, err := s.knowledgeBaseService.GetKnowledgeBaseByIDOnly(ctx, target.KnowledgeBaseID) + if err == nil && kb != nil && kb.Type == types.KnowledgeBaseTypeWiki { + systemPromptTemplate += "\n" + agent.WikiAgentSystemPromptAddendum + break + } + } + // 5. Create engine engine := agent.NewAgentEngine( config, chatModel, toolRegistry, eventBus, diff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.go index 73b9caee..2adf914f 100644 --- a/internal/application/service/wiki_ingest.go +++ b/internal/application/service/wiki_ingest.go @@ -132,7 +132,6 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task content = string([]rune(content)[:maxContentForWiki]) } - // Get human-readable language name for LLM prompts // Reuses language mapping from middleware infrastructure (supports 9+ languages) // Maps locale codes like "zh", "en" to names like "Chinese (Simplified)", "English" @@ -175,7 +174,7 @@ func (s *wikiIngestService) ProcessWikiIngest(ctx context.Context, t *asynq.Task Slug: summarySlug, Title: docTitle + " - Summary", PageType: types.WikiPageTypeSummary, - Status: types.WikiPageStatusPublished, + Status: types.WikiPageStatusDraft, Content: summaryContent, Summary: truncateString(summaryContent, 200), SourceRefs: types.StringArray{payload.KnowledgeID}, @@ -206,6 +205,9 @@ 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 + s.publishDraftPages(ctx, payload.KnowledgeBaseID, pagesAffected) + logger.Infof(ctx, "wiki ingest: completed for knowledge %s, %d pages affected", payload.KnowledgeID, len(pagesAffected)) @@ -258,6 +260,12 @@ func (s *wikiIngestService) extractEntitiesAndConcepts( var affected []string + // Deduplicate entities against existing wiki pages (LLM-based) + result.Entities = s.deduplicateItems(ctx, chatModel, result.Entities, types.WikiPageTypeEntity, payload.KnowledgeBaseID) + + // Deduplicate concepts against existing wiki pages (LLM-based) + result.Concepts = s.deduplicateItems(ctx, chatModel, result.Concepts, types.WikiPageTypeConcept, payload.KnowledgeBaseID) + // Upsert entity pages entitySlugs, err := s.upsertExtractedPages(ctx, chatModel, result.Entities, types.WikiPageTypeEntity, docTitle, lang, payload) if err != nil { @@ -328,7 +336,7 @@ func (s *wikiIngestService) upsertExtractedPages( Slug: item.Slug, Title: item.Name, PageType: pageType, - Status: types.WikiPageStatusPublished, + Status: types.WikiPageStatusDraft, Content: pageContent, Summary: item.Description, SourceRefs: types.StringArray{payload.KnowledgeID}, @@ -457,6 +465,98 @@ func (s *wikiIngestService) appendLogEntry(ctx context.Context, payload WikiInge } } +// publishDraftPages transitions draft pages to published status after ingest completes. +// This ensures users don't see half-built pages during the ingest process. +func (s *wikiIngestService) publishDraftPages(ctx context.Context, kbID string, slugs []string) { + for _, slug := range slugs { + page, err := s.wikiService.GetPageBySlug(ctx, kbID, slug) + if err != nil || page == nil { + continue + } + if page.Status == types.WikiPageStatusDraft { + page.Status = types.WikiPageStatusPublished + if _, err := s.wikiService.UpdatePage(ctx, page); err != nil { + logger.Warnf(ctx, "wiki ingest: failed to publish page %s: %v", slug, err) + } + } + } +} + +// deduplicateItems uses LLM to identify new items that refer to the same entity/concept +// as existing wiki pages, and remaps their slugs so upsertExtractedPages merges them. +func (s *wikiIngestService) deduplicateItems( + ctx context.Context, + chatModel chat.Chat, + items []extractedItem, + pageType string, + kbID string, +) []extractedItem { + if len(items) == 0 { + return items + } + + // Get existing pages of the same type + resp, err := s.wikiService.ListPages(ctx, &types.WikiPageListRequest{ + KnowledgeBaseID: kbID, + PageType: pageType, + PageSize: 2000, + }) + if err != nil || resp == nil || len(resp.Pages) == 0 { + return items // No existing pages → nothing to deduplicate against + } + + // Build existing pages listing + var existingBuf strings.Builder + for _, p := range resp.Pages { + fmt.Fprintf(&existingBuf, "- slug: %s | title: %s\n", p.Slug, p.Title) + } + + // Build new items listing + var newBuf strings.Builder + for _, item := range items { + fmt.Fprintf(&newBuf, "- slug: %s | name: %s\n", item.Slug, item.Name) + } + + // Call LLM for deduplication + dedupeJSON, err := s.generateWithTemplate(ctx, chatModel, agent.WikiDeduplicationPrompt, map[string]string{ + "NewItems": newBuf.String(), + "ExistingPages": existingBuf.String(), + }) + if err != nil { + logger.Warnf(ctx, "wiki ingest: deduplication LLM call failed: %v", err) + return items + } + + // Parse response + dedupeJSON = strings.TrimSpace(dedupeJSON) + dedupeJSON = strings.TrimPrefix(dedupeJSON, "```json") + dedupeJSON = strings.TrimPrefix(dedupeJSON, "```") + dedupeJSON = strings.TrimSuffix(dedupeJSON, "```") + dedupeJSON = strings.TrimSpace(dedupeJSON) + + var dedupeResult struct { + Merges map[string]string `json:"merges"` + } + if err := json.Unmarshal([]byte(dedupeJSON), &dedupeResult); err != nil { + logger.Warnf(ctx, "wiki ingest: failed to parse dedup JSON: %v", err) + return items + } + + if len(dedupeResult.Merges) == 0 { + return items + } + + // Remap slugs for matched items + for i, item := range items { + if existingSlug, ok := dedupeResult.Merges[item.Slug]; ok { + logger.Infof(ctx, "wiki ingest: dedup merge %s → %s", item.Slug, existingSlug) + items[i].Slug = existingSlug + } + } + + return items +} + // generateWithTemplate executes a prompt template and calls the LLM func (s *wikiIngestService) generateWithTemplate(ctx context.Context, chatModel chat.Chat, promptTpl string, data map[string]string) (string, error) { tmpl, err := template.New("wiki").Parse(promptTpl) diff --git a/internal/application/service/wiki_lint.go b/internal/application/service/wiki_lint.go index c24127de..072bf401 100644 --- a/internal/application/service/wiki_lint.go +++ b/internal/application/service/wiki_lint.go @@ -147,7 +147,7 @@ func (s *WikiLintService) RunLint(ctx context.Context, kbID string) (*WikiLintRe Severity: SeverityWarning, PageSlug: page.Slug, Description: fmt.Sprintf("Page '%s' has very little content (%d chars)", page.Title, len(content)), - AutoFixable: false, + AutoFixable: true, }) } } @@ -291,6 +291,21 @@ func (s *WikiLintService) AutoFix(ctx context.Context, kbID string) (int, error) if _, err := s.wikiService.UpdatePage(ctx, page); err == nil { fixed++ } + + case LintIssueEmptyContent: + // Archive pages with very little content instead of deleting + page, err := s.wikiService.GetPageBySlug(ctx, kbID, issue.PageSlug) + if err != nil { + continue + } + // Don't archive index or log pages + if page.PageType == types.WikiPageTypeIndex || page.PageType == types.WikiPageTypeLog { + continue + } + page.Status = types.WikiPageStatusArchived + if _, err := s.wikiService.UpdatePage(ctx, page); err == nil { + fixed++ + } } }