Files
Enterprise-Knowledge-Base/internal/agent/tools/wiki_delete_page.go
T
wizardchen bd096454e1 feat: Introduce Wiki Fixer agent and related functionalities
- Added a new built-in agent, "Wiki Fixer," designed to repair and optimize Wiki pages based on linter issues, with multilingual support for enhanced accessibility.
- Implemented a corresponding system prompt detailing the agent's role, mission, and workflow for effective issue resolution.
- Introduced new API functions for listing and updating wiki issues, allowing for better management of content conflicts and errors.
- Enhanced the frontend with new UI elements to display pending issues and facilitate auto-fixing, improving user interaction with the Wiki content.

These changes significantly enhance the agent's capabilities for maintaining the accuracy and quality of Wiki pages, fostering a more reliable knowledge base.
2026-04-22 21:18:08 +08:00

116 lines
3.4 KiB
Go

package tools
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
type wikiDeletePageTool struct {
BaseTool
wikiPageService interfaces.WikiPageService
kbIDs []string
}
// NewWikiDeletePageTool creates a new wiki_delete_page tool
func NewWikiDeletePageTool(wikiPageService interfaces.WikiPageService, kbIDs []string) types.Tool {
return &wikiDeletePageTool{
BaseTool: NewBaseTool(
ToolWikiDeletePage,
"Delete a Wiki page. Automatically cleans up incoming links on other pages to prevent dead links.",
json.RawMessage(`{
"type": "object",
"properties": {
"slug": {
"type": "string",
"description": "The slug of the Wiki page to delete"
}
},
"required": ["slug"]
}`),
),
wikiPageService: wikiPageService,
kbIDs: kbIDs,
}
}
func (t *wikiDeletePageTool) Execute(ctx context.Context, args json.RawMessage) (*types.ToolResult, error) {
var params struct {
Slug string `json:"slug"`
}
if err := json.Unmarshal(args, &params); err != nil {
return &types.ToolResult{Success: false, Error: "Failed to parse arguments: " + err.Error()}, nil
}
if len(t.kbIDs) == 0 {
return &types.ToolResult{Success: false, Error: "No knowledge bases available for editing"}, nil
}
kbID := t.kbIDs[0]
if params.Slug == "" {
return &types.ToolResult{Success: false, Error: "slug is required"}, nil
}
// Fetch page to get its incoming links before deleting
existingPage, err := t.wikiPageService.GetPageBySlug(ctx, kbID, params.Slug)
if err != nil {
return &types.ToolResult{Success: false, Error: "Failed to fetch page to delete: " + err.Error()}, nil
}
inLinks := make([]string, len(existingPage.InLinks))
copy(inLinks, existingPage.InLinks)
err = t.wikiPageService.DeletePage(ctx, kbID, params.Slug)
if err != nil {
return &types.ToolResult{Success: false, Error: "Failed to delete page: " + err.Error()}, nil
}
// Clean up incoming links to prevent dead links
updatedCount := 0
var updatedSlugs []string
for _, sourceSlug := range inLinks {
sourcePage, err := t.wikiPageService.GetPageBySlug(ctx, kbID, sourceSlug)
if err == nil {
changed := false
// Replace [[deleted-slug]] with readable name
parts := strings.Split(params.Slug, "/")
readableName := parts[len(parts)-1]
readableName = strings.ReplaceAll(readableName, "-", " ")
link1 := "[[" + params.Slug + "]]"
if strings.Contains(sourcePage.Content, link1) {
sourcePage.Content = strings.ReplaceAll(sourcePage.Content, link1, readableName)
changed = true
}
// Replace [[deleted-slug|Text]] with just Text
re := regexp.MustCompile(`\[\[` + regexp.QuoteMeta(params.Slug) + `\|([^\]]+)\]\]`)
if re.MatchString(sourcePage.Content) {
sourcePage.Content = re.ReplaceAllString(sourcePage.Content, "$1")
changed = true
}
if changed {
_, updateErr := t.wikiPageService.UpdatePage(ctx, sourcePage)
if updateErr == nil {
updatedCount++
updatedSlugs = append(updatedSlugs, sourceSlug)
}
}
}
}
outputMsg := fmt.Sprintf("Successfully deleted page %s and cleaned up %d incoming links.", params.Slug, updatedCount)
if updatedCount > 0 {
outputMsg += fmt.Sprintf(" Affected pages: %s", strings.Join(updatedSlugs, ", "))
}
return &types.ToolResult{Success: true, Output: outputMsg}, nil
}