feat: Enhance wiki functionality with recent activity log
This commit introduces a new "Recent Activity" section in the WikiBrowser component, allowing users to view the latest changes made to the wiki. The implementation includes: - Addition of a toggleable recent activity log displaying the last five entries. - Backend support for fetching the activity log from the knowledge base. - Updates to the wiki ingest process to ensure draft pages are published post-ingest. - Improvements to the deduplication process for entities and concepts during wiki page creation. These enhancements aim to improve user engagement and provide better insights into wiki updates.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user