KB multi-article support: CRUD articles, per-article indexing, sidebar KB mode

- Replace singleton kb_content table with kb_articles (id, title, content)
- Add article_id to kb_chunks for per-article chunk tracking
- Auto-migrate old kb_content data on startup
- KbManager: index/delete per article, search across all with article_title
- API: full CRUD on /kb/articles, keep GET /kb for agent tool
- Agent: kb_search shows article labels, kb_read concatenates all articles
- Frontend: Sidebar KB mode with article list, KbEditor for single article

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-01 08:56:08 +00:00
parent 3d1c910c4a
commit 40f200db4f
9 changed files with 513 additions and 97 deletions

View File

@@ -983,7 +983,12 @@ async fn run_agent_loop(
Ok(results) if results.is_empty() => "知识库为空或没有匹配结果。".to_string(),
Ok(results) => {
results.iter().enumerate().map(|(i, r)| {
format!("--- 片段 {} (相似度: {:.2}) ---\n{}", i + 1, r.score, r.content)
let article_label = if r.article_title.is_empty() {
String::new()
} else {
format!(" [文章: {}]", r.article_title)
};
format!("--- 片段 {} (相似度: {:.2}){} ---\n{}", i + 1, r.score, article_label, r.content)
}).collect::<Vec<_>>().join("\n\n")
}
Err(e) => format!("Error: {}", e),
@@ -995,14 +1000,14 @@ async fn run_agent_loop(
}
"kb_read" => {
let result: String = match sqlx::query_scalar::<_, String>("SELECT content FROM kb_content WHERE id = 1")
.fetch_one(pool)
.await
{
Ok(content) => {
if content.is_empty() { "知识库为空。".to_string() } else { content }
let result = if let Some(kb) = &mgr.kb {
match kb.read_all().await {
Ok(content) if content.is_empty() => "知识库为空。".to_string(),
Ok(content) => content,
Err(e) => format!("Error: {}", e),
}
Err(e) => format!("Error: {}", e),
} else {
"知识库未初始化。".to_string()
};
state.step_messages.push(ChatMessage::tool_result(&tc.id, &result));
}