Files
Enterprise-Knowledge-Base/internal/agent/tools/wiki_write_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

146 lines
4.5 KiB
Go

package tools
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
type wikiWritePageTool struct {
BaseTool
wikiPageService interfaces.WikiPageService
kbIDs []string
}
// NewWikiWritePageTool creates a new wiki_write_page tool
func NewWikiWritePageTool(wikiPageService interfaces.WikiPageService, kbIDs []string) types.Tool {
return &wikiWritePageTool{
BaseTool: NewBaseTool(
ToolWikiWritePage,
"Create a new Wiki page or completely overwrite an existing one. Automatically handles outbound links.",
json.RawMessage(`{
"type": "object",
"properties": {
"slug": {
"type": "string",
"description": "The slug of the Wiki page (e.g. 'entity/hunyuan-damoxing')"
},
"title": {
"type": "string",
"description": "The title of the page"
},
"summary": {
"type": "string",
"description": "A one-sentence summary for the index listing"
},
"content": {
"type": "string",
"description": "The FULL, complete Markdown content of the page. Do NOT use placeholders."
},
"page_type": {
"type": "string",
"description": "The page type, e.g., 'summary', 'entity', 'concept', 'synthesis', 'comparison'"
},
"aliases": {
"type": "array",
"items": {"type": "string"},
"description": "A list of aliases for the page (optional)"
},
"source_refs": {
"type": "array",
"items": {"type": "string"},
"description": "A list of source knowledge IDs that contributed to this page. If provided, these will COMPLETELY REPLACE the existing source_refs of the page."
}
},
"required": ["slug", "title", "summary", "content", "page_type"]
}`),
),
wikiPageService: wikiPageService,
kbIDs: kbIDs,
}
}
func (t *wikiWritePageTool) Execute(ctx context.Context, args json.RawMessage) (*types.ToolResult, error) {
var params struct {
Slug string `json:"slug"`
Title string `json:"title"`
Summary string `json:"summary"`
Content string `json:"content"`
PageType string `json:"page_type"`
Aliases []string `json:"aliases"`
SourceRefs []string `json:"source_refs"`
}
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.Title == "" || params.PageType == "" || params.Content == "" || params.Summary == "" {
return &types.ToolResult{Success: false, Error: "title, summary, content, and page_type are required for write action"}, nil
}
// Try to get the existing page
existingPage, err := t.wikiPageService.GetPageBySlug(ctx, kbID, params.Slug)
if err != nil && !errors.Is(err, repository.ErrWikiPageNotFound) {
return &types.ToolResult{Success: false, Error: "Failed to check existing page: " + err.Error()}, nil
}
var action string
if existingPage != nil {
// Update
existingPage.Title = params.Title
existingPage.Summary = params.Summary
existingPage.Content = params.Content
existingPage.PageType = params.PageType
existingPage.Aliases = params.Aliases
if len(params.SourceRefs) > 0 {
existingPage.SourceRefs = params.SourceRefs
}
_, err = t.wikiPageService.UpdatePage(ctx, existingPage)
if err != nil {
return &types.ToolResult{Success: false, Error: "Failed to update page: " + err.Error()}, nil
}
action = "updated"
} else {
// Create
newPage := &types.WikiPage{
KnowledgeBaseID: kbID,
Slug: params.Slug,
Title: params.Title,
Summary: params.Summary,
Content: params.Content,
PageType: params.PageType,
Aliases: params.Aliases,
SourceRefs: params.SourceRefs,
}
_, err = t.wikiPageService.CreatePage(ctx, newPage)
if err != nil {
return &types.ToolResult{Success: false, Error: "Failed to create page: " + err.Error()}, nil
}
action = "created"
}
// Inject cross-links so other pages know about this new/updated entity
t.wikiPageService.InjectCrossLinks(ctx, kbID, []string{params.Slug})
// Rebuild the index page to reflect the new/updated summary
_ = t.wikiPageService.RebuildIndexPage(ctx, kbID)
return &types.ToolResult{
Success: true,
Output: fmt.Sprintf("Successfully %s page %s.", action, params.Slug),
}, nil
}