mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-27 04:43:11 +00:00
feat(kbtree): 知识库分类树的独立只读服务 + agent 技能 + WebUI 树浏览
让**外部 agent** 也能按分类树用这套知识库。HomeAgent 自己的 agent 仍
直接调内部方法(knowledge_search/create 等),走进程内直调,不经此服务。
一、内核树视图(internal/knowledge/tree.go)
为什么不复用 TreeIndex:那个是**内部导出物**,面向 .index.json 落盘,
每个条目带 top-20 的 TF-IDF 特征向量。直接序列化给外部有三个问题:
体积(200 条时 .index.json 已 246KB 且冗余存了 preview,而正本在
content.md)、泄漏(稀疏特征表 = 分词/IDF 内部表示)、语义错位
(外部要的是"有哪些分类、每类下有什么")。
新增 TreeView/Subtree/Categories/CategoryCounts:不含向量,带条目数
与可读摘要,支持 MaxDepth 懒加载、IncludeItems 只看结构。
节点 Name 是**本级段名**("go")、Path 是完整路径("tech/go")——
最初把全路径写进 Name,前端拼层级会得到 "tech/tech/go",已修。
二、kbtree 插件:独立 HTTP 服务(默认 127.0.0.1:9892)
为何不挂在 WebUI 的 /api/v1/knowledge* 下:
1. 不共享鉴权与端口。WebUI 的 api_key 是给人操作界面用的,把它分发给
外部 agent 等于把管理面凭据扩散出去。本服务用**独立 token** +
独立端口,可单独关闭(token 未配置则启动时随机生成)。
2. 只读。写入要决定分类归属与媒体处理,外部自行拼装容易造出越界/重名
条目 —— 写入留给内核工具。
3. 形状按树组织,而不是平铺搜索接口。
端点:/tree(可指定 category/depth/items)、/categories、/counts、
/search、/ (自述)。全部需 token(X-API-Key / Bearer / ?token=),
非 GET 一律 405。无知识库时 Start 直接失败,不占端口。
鉴权与 Slowloris/超时设置照 remotedevice 范式。
三、agent 技能(assets/skills/knowledge-base/SKILL.md)
指令文档型 skill:教模型"先看树 → 定位分类 → 分类内检索",并列出
易错点(name 已含分类别再拼、只看第一条、404 附现有分类)。
加载与校验由 internal/plugin/skill_bundled_test.go 守住 —— 这条断言
的由来:非白名单的二级标题会被 extractToolDefs 当成工具定义,报错
"invalid tool name",而提示与真正原因(标题层级)毫无关联。
kbtree 的测试还会校验文档提到的端点与代码一致,防漂移。
四、WebUI 树浏览(前端真正用起来,而非留一个没人调的端点)
面板加可折叠的分类树:逐级点选即把搜索范围切到该子树(原先是让人
手打分类名)。当前范围有可见标签与「全库」复位。
This commit is contained in:
370
internal/plugins/kbtree/plugin.go
Normal file
370
internal/plugins/kbtree/plugin.go
Normal file
@ -0,0 +1,370 @@
|
||||
// Package kbtree 把知识库的**分类树**作为独立 HTTP 服务暴露给外部 agent。
|
||||
//
|
||||
// 定位(与 HomeAgent 内部知识工具的分工):
|
||||
// - HomeAgent 自己的 agent **直接调内部方法**(knowledge_search /
|
||||
// knowledge_create 等内核工具),走的是进程内直调,最快、也最少攻击面。
|
||||
// - 其它 agent(别的进程、别的机器、别的语言写的)走本插件的 HTTP 接口。
|
||||
//
|
||||
// 为何独立成服务而不是复用 WebUI 的 /api/v1/knowledge*:
|
||||
// 1. **不共享 WebUI 的鉴权与端口**。WebUI 的 api_key 是给人操作界面用的,
|
||||
// 把它分发给外部 agent 等于把管理面凭据扩散出去。本服务用**独立 token**
|
||||
// 且**独立端口**,可单独关闭。
|
||||
// 2. **只读**。外部 agent 读知识库就够了;写入要决定分类归属与媒体处理,
|
||||
// 让外部自行拼装反而容易造出越界/重名的条目 —— 写入留给内核工具。
|
||||
// 3. 形状按树组织(分类导航、分类内检索、懒加载),而不是平铺的搜索接口。
|
||||
//
|
||||
// 配套的 assets/skills/knowledge-base/SKILL.md 是给 agent 读的指令文档,
|
||||
// 两者配合即可"别的 agent 也能查这套知识库"。
|
||||
package kbtree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/plugin"
|
||||
sdk "gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||||
)
|
||||
|
||||
// defaultAddr 默认只监听本机。外部 agent 若在别的机器,改配置显式对外,
|
||||
// 并自行确认 token 的分发方式(见 RegisterDef 里的说明)。
|
||||
const defaultAddr = "127.0.0.1:9892"
|
||||
|
||||
func init() {
|
||||
plugin.RegisterPluginMeta("kbtree", "知识库树服务", "Knowledge Tree Service")
|
||||
plugin.RegisterFactory("kbtree", func(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
return New(name), nil
|
||||
})
|
||||
}
|
||||
|
||||
type Plugin struct {
|
||||
name string
|
||||
sdkRef *sdk.PluginSDK
|
||||
addr string
|
||||
token string
|
||||
server *http.Server
|
||||
mux *http.ServeMux
|
||||
// 启动时未显式配置 token 则自动生成(与 remotedevice 同策略)
|
||||
generatedToken bool
|
||||
}
|
||||
|
||||
func New(name string) *Plugin {
|
||||
return &Plugin{
|
||||
name: name,
|
||||
addr: defaultAddr,
|
||||
mux: http.NewServeMux(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) Name() string { return p.name }
|
||||
|
||||
func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
if s == nil {
|
||||
return fmt.Errorf("kbtree: SDK 不可用")
|
||||
}
|
||||
p.sdkRef = s
|
||||
|
||||
// Settings 缺失时不能直接 RegisterDef —— 那是空接口上的调用,会 panic。
|
||||
// 缺 Settings 意味着宿主装配不完整(测试环境或裁剪版内核),
|
||||
// 此时用默认值把服务跑起来,而不是让整个插件加载失败。
|
||||
if set := s.Settings(); set != nil {
|
||||
set.RegisterDef(sdk.ConfigDef{
|
||||
Key: "listen_addr", Default: defaultAddr, Type: "string",
|
||||
DisplayName: "监听地址", Category: "kbtree",
|
||||
Description: "知识库树服务 HTTP 监听地址(默认 127.0.0.1:9892,仅本机)。改为对外地址前请确认 token 分发方式",
|
||||
})
|
||||
set.RegisterDef(sdk.ConfigDef{
|
||||
Key: "token", Default: "", Type: "password",
|
||||
DisplayName: "访问令牌", Category: "kbtree",
|
||||
Description: "外部 agent 访问本服务所需的令牌;留空则启动时随机生成(仅本次运行有效)",
|
||||
})
|
||||
if v, _ := set.Get("listen_addr"); v != nil {
|
||||
if a, ok := v.(string); ok && strings.TrimSpace(a) != "" {
|
||||
p.addr = strings.TrimSpace(a)
|
||||
}
|
||||
}
|
||||
if v, _ := set.Get("token"); v != nil {
|
||||
if tk, ok := v.(string); ok && strings.TrimSpace(tk) != "" {
|
||||
p.token = strings.TrimSpace(tk)
|
||||
}
|
||||
}
|
||||
}
|
||||
if p.token == "" {
|
||||
p.token = genToken()
|
||||
p.generatedToken = true
|
||||
}
|
||||
|
||||
// 启动前确认知识库可用:不可用就别占着端口
|
||||
kn := s.Knowledge()
|
||||
if kn == nil {
|
||||
return fmt.Errorf("kbtree: 知识库不可用,未启动服务(避免占端口后只回 503)")
|
||||
}
|
||||
|
||||
p.registerRoutes()
|
||||
p.server = &http.Server{
|
||||
Addr: p.addr,
|
||||
Handler: p.mux,
|
||||
ReadHeaderTimeout: 5 * time.Second, // Slowloris 防护
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
go func() {
|
||||
if err := p.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Printf("[kbtree] 服务错误: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if p.generatedToken {
|
||||
log.Printf("[kbtree] 已启动 http://%s(token 未配置,本次随机生成;重启后失效)", p.addr)
|
||||
} else {
|
||||
log.Printf("[kbtree] 已启动 http://%s", p.addr)
|
||||
}
|
||||
log.Printf("[kbtree] 外部 agent 可用:GET /tree、/categories、/counts、/search?q=&category=")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugin) Stop() error {
|
||||
if p.server != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
return p.server.Shutdown(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// registerRoutes 挂载只读路由。全部需 token。
|
||||
func (p *Plugin) registerRoutes() {
|
||||
p.mux.HandleFunc("/tree", p.requireToken(p.handleTree))
|
||||
p.mux.HandleFunc("/categories", p.requireToken(p.handleCategories))
|
||||
p.mux.HandleFunc("/counts", p.requireToken(p.handleCounts))
|
||||
p.mux.HandleFunc("/search", p.requireToken(p.handleSearch))
|
||||
// 根路径给个自述,便于外部 agent 摸索
|
||||
p.mux.HandleFunc("/", p.requireToken(p.handleRoot))
|
||||
}
|
||||
|
||||
func (p *Plugin) requireToken(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
k := r.Header.Get("X-API-Key")
|
||||
if k == "" {
|
||||
k = r.Header.Get("Authorization")
|
||||
k = strings.TrimPrefix(k, "Bearer ")
|
||||
k = strings.TrimSpace(k)
|
||||
}
|
||||
if k == "" {
|
||||
k = r.URL.Query().Get("token")
|
||||
}
|
||||
if k == "" || k != p.token {
|
||||
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet {
|
||||
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "只支持 GET(本服务只读)"})
|
||||
return
|
||||
}
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// handleRoot 自述端点:告诉调用方有哪些接口可用。
|
||||
func (p *Plugin) handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"service": "kbtree",
|
||||
"purpose": "HomeAgent 知识库分类树的只读访问(供外部 agent 使用)",
|
||||
"read_only": true,
|
||||
"endpoints": []string{
|
||||
"GET /tree?category=&depth=&items= 分类树(可指定子树/层数)",
|
||||
"GET /categories 全部分类路径(含中间层)",
|
||||
"GET /counts 各分类条目数(按数量倒序)",
|
||||
"GET /search?q=&category=&limit= 检索(category 前缀匹配子树)",
|
||||
},
|
||||
"auth": "X-API-Key 头 或 Authorization: Bearer <token> 或 ?token=",
|
||||
"notes": "结果按相关度排序,只采用第一条;节点 name 是本级段名,path 是完整路径",
|
||||
})
|
||||
}
|
||||
|
||||
// handleTree 返回分类树。
|
||||
func (p *Plugin) handleTree(w http.ResponseWriter, r *http.Request) {
|
||||
kn := p.knowledge(w)
|
||||
if kn == nil {
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
opt := sdk.KnowledgeTreeOptions{
|
||||
MaxDepth: intQ(q.Get("depth"), 0),
|
||||
IncludeItems: boolQ(q.Get("items"), true),
|
||||
PreviewLimit: intQ(q.Get("preview"), 0),
|
||||
}
|
||||
cat := strings.Trim(strings.TrimSpace(q.Get("category")), "/")
|
||||
view, err := kn.Subtree(cat, opt)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if view == nil {
|
||||
writeJSON(w, http.StatusNotFound, map[string]interface{}{
|
||||
"error": "分类不存在: " + cat,
|
||||
"categories": p.safeCategories(kn),
|
||||
})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"tree": view})
|
||||
}
|
||||
|
||||
// handleCategories 返回平铺分类列表。
|
||||
func (p *Plugin) handleCategories(w http.ResponseWriter, r *http.Request) {
|
||||
kn := p.knowledge(w)
|
||||
if kn == nil {
|
||||
return
|
||||
}
|
||||
names := p.safeCategories(kn)
|
||||
if names == nil {
|
||||
names = []string{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"categories": names})
|
||||
}
|
||||
|
||||
// handleCounts 返回各分类条目数(倒序)。
|
||||
func (p *Plugin) handleCounts(w http.ResponseWriter, r *http.Request) {
|
||||
kn := p.knowledge(w)
|
||||
if kn == nil {
|
||||
return
|
||||
}
|
||||
counts, err := kn.CategoryCounts()
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if counts == nil {
|
||||
counts = []sdk.KnowledgeCategoryCount{}
|
||||
}
|
||||
// 附总量:只数叶子分类,避免中间层重复计数
|
||||
total := 0
|
||||
byCat := make(map[string]int, len(counts))
|
||||
for _, c := range counts {
|
||||
byCat[c.Category] = c.Count
|
||||
}
|
||||
for c := range byCat {
|
||||
if !isParentCategory(counts, c) {
|
||||
total += byCat[c]
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"counts": counts, "total": total})
|
||||
}
|
||||
|
||||
// handleSearch 按分类 + 关键词检索。
|
||||
func (p *Plugin) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
kn := p.knowledge(w)
|
||||
if kn == nil {
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
query := strings.TrimSpace(q.Get("q"))
|
||||
if query == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "q 必填"})
|
||||
return
|
||||
}
|
||||
limit := intQ(q.Get("limit"), 10)
|
||||
if limit <= 0 || limit > 100 {
|
||||
limit = 10
|
||||
}
|
||||
cat := strings.Trim(strings.TrimSpace(q.Get("category")), "/")
|
||||
results, err := kn.SearchIn(query, cat, limit)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
items := make([]map[string]interface{}, 0, len(results))
|
||||
for _, k := range results {
|
||||
items = append(items, map[string]interface{}{
|
||||
"name": k.Name,
|
||||
"content": k.Content,
|
||||
})
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"query": query,
|
||||
"category": cat,
|
||||
"results": items,
|
||||
"hint": "结果按相关度排序;只采用第一条,第一条不相关请换分类或关键词",
|
||||
})
|
||||
}
|
||||
|
||||
// knowledge 取知识库;不可用时写 503 并返回 nil。
|
||||
func (p *Plugin) knowledge(w http.ResponseWriter) sdk.KnowledgeAPI {
|
||||
if p.sdkRef == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "plugin not started"})
|
||||
return nil
|
||||
}
|
||||
kn := p.sdkRef.Knowledge()
|
||||
if kn == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "knowledge not available"})
|
||||
return nil
|
||||
}
|
||||
return kn
|
||||
}
|
||||
|
||||
func (p *Plugin) safeCategories(kn sdk.KnowledgeAPI) []string {
|
||||
names, err := kn.Categories()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// isParentCategory 判断 c 是否是别的分类的前缀(即中间层)。
|
||||
func isParentCategory(counts []sdk.KnowledgeCategoryCount, c string) bool {
|
||||
for _, o := range counts {
|
||||
if o.Category != c && strings.HasPrefix(o.Category, c+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func genToken() string {
|
||||
buf := make([]byte, 16)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return fmt.Sprintf("tok-%d", time.Now().UnixNano())
|
||||
}
|
||||
return hex.EncodeToString(buf)
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func intQ(s string, def int) int {
|
||||
if s == "" {
|
||||
return def
|
||||
}
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return def
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func boolQ(s string, def bool) bool {
|
||||
if s == "" {
|
||||
return def
|
||||
}
|
||||
switch strings.ToLower(s) {
|
||||
case "1", "true", "yes", "on":
|
||||
return true
|
||||
case "0", "false", "no", "off":
|
||||
return false
|
||||
}
|
||||
return def
|
||||
}
|
||||
352
internal/plugins/kbtree/plugin_test.go
Normal file
352
internal/plugins/kbtree/plugin_test.go
Normal file
@ -0,0 +1,352 @@
|
||||
package kbtree
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/knowledge"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/supervisor"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/pkg/types"
|
||||
)
|
||||
|
||||
type rig struct {
|
||||
p *Plugin
|
||||
ks *knowledge.Store
|
||||
sup *supervisor.Daemon
|
||||
srv *httptest.Server
|
||||
tok string
|
||||
}
|
||||
|
||||
func newRig(t *testing.T) *rig {
|
||||
t.Helper()
|
||||
ks := knowledge.NewStore(t.TempDir())
|
||||
if err := ks.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(ks.Stop)
|
||||
for _, e := range []struct{ name, body string }{
|
||||
{"tech/go/并发", "goroutine 调度 GMP 抢占 通道"},
|
||||
{"tech/go/context", "context 取消 超时 传播"},
|
||||
{"tech/rust/所有权", "borrow checker move 语义"},
|
||||
{"life/sleep", "作息 褪黑素 深睡"},
|
||||
{"cook/coffee", "手冲 烘焙 水温"},
|
||||
} {
|
||||
if err := ks.Add(e.name, e.body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
cfg := &types.Config{Daemon: types.DaemonConfig{
|
||||
CheckInterval: time.Minute, HeartbeatInterval: 30 * time.Second,
|
||||
}}
|
||||
sup := supervisor.New(cfg)
|
||||
sup.Start()
|
||||
t.Cleanup(sup.Shutdown)
|
||||
|
||||
s := sdk.New("test", sdk.SDKConfig{
|
||||
Supervisor: supervisor.NewSDKAdapter(sup),
|
||||
Knowledge: sdk.NewKnowledge(ks),
|
||||
Config: sdk.NewConfig(cfg),
|
||||
})
|
||||
// 让 Start 监听 :0(随机空闲端口),这样既走真实启动路径又不撞固定端口
|
||||
p := New("kbtree")
|
||||
p.addr = "127.0.0.1:0"
|
||||
if err := p.Start(s); err != nil {
|
||||
t.Fatalf("Start 失败: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = p.Stop() })
|
||||
|
||||
// 测试直接打插件的 mux(真实端口已由 :0 分配,无需再开 server)
|
||||
srv := httptest.NewServer(p.mux)
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
return &rig{p: p, ks: ks, sup: sup, srv: srv, tok: p.token}
|
||||
}
|
||||
|
||||
// get 带 token 请求。
|
||||
func (r *rig) get(t *testing.T, path string) (int, []byte) {
|
||||
t.Helper()
|
||||
req, err := http.NewRequest(http.MethodGet, r.srv.URL+path, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("X-API-Key", r.tok)
|
||||
resp, err := r.srv.Client().Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return resp.StatusCode, b
|
||||
}
|
||||
|
||||
func (r *rig) getNoAuth(t *testing.T, path string) int {
|
||||
t.Helper()
|
||||
resp, err := r.srv.Client().Get(r.srv.URL + path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return resp.StatusCode
|
||||
}
|
||||
|
||||
// 无 token / 错 token 一律 401。
|
||||
func TestRequiresToken(t *testing.T) {
|
||||
r := newRig(t)
|
||||
if code := r.getNoAuth(t, "/tree"); code != http.StatusUnauthorized {
|
||||
t.Errorf("无 token 应 401,实为 %d", code)
|
||||
}
|
||||
// 错 token
|
||||
resp, err := r.srv.Client().Get(r.srv.URL + "/tree?token=wrong")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Errorf("错 token 应 401,实为 %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// token 也可用 ?token= 传(外部脚本友好)。
|
||||
func TestTokenViaQuery(t *testing.T) {
|
||||
r := newRig(t)
|
||||
resp, err := r.srv.Client().Get(r.srv.URL + "/tree?token=" + r.tok)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("?token= 应放行,实为 %d", resp.StatusCode)
|
||||
}
|
||||
// Bearer 也行
|
||||
req, _ := http.NewRequest(http.MethodGet, r.srv.URL+"/tree", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+r.tok)
|
||||
resp2, err := r.srv.Client().Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
if resp2.StatusCode != http.StatusOK {
|
||||
t.Errorf("Bearer token 应放行,实为 %d", resp2.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// 根路径自述端点。
|
||||
func TestRootSelfDescription(t *testing.T) {
|
||||
r := newRig(t)
|
||||
code, body := r.get(t, "/")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("应 200,实为 %d", code)
|
||||
}
|
||||
if !strings.Contains(string(body), "read_only") {
|
||||
t.Errorf("自述里应标明只读: %s", body)
|
||||
}
|
||||
if !strings.Contains(string(body), "/tree") {
|
||||
t.Errorf("自述里应列出端点: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// 树端点:结构、计数、懒加载。
|
||||
func TestTreeEndpoint(t *testing.T) {
|
||||
r := newRig(t)
|
||||
code, body := r.get(t, "/tree?items=0")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("应 200,实为 %d: %s", code, body)
|
||||
}
|
||||
if !strings.Contains(string(body), `"total_count":5`) {
|
||||
t.Errorf("总条目应为 5: %s", body)
|
||||
}
|
||||
if !strings.Contains(string(body), `"tech"`) {
|
||||
t.Errorf("应含 tech 分类: %s", body)
|
||||
}
|
||||
// items=0 时无 preview
|
||||
if strings.Contains(string(body), `"preview"`) {
|
||||
t.Error("items=0 不应返回 preview")
|
||||
}
|
||||
|
||||
// 懒加载:depth=1 仍给出准确 total
|
||||
code, body = r.get(t, "/tree?depth=1&items=0")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("depth=1 应 200,实为 %d", code)
|
||||
}
|
||||
if !strings.Contains(string(body), `"total_count":5`) {
|
||||
t.Errorf("剪枝不应改变总数: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// 子树端点。
|
||||
func TestTreeSubtreeEndpoint(t *testing.T) {
|
||||
r := newRig(t)
|
||||
code, body := r.get(t, "/tree?category=tech/go&items=1")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("应 200,实为 %d: %s", code, body)
|
||||
}
|
||||
var resp struct {
|
||||
Tree struct {
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
ItemCount int `json:"item_count"`
|
||||
} `json:"tree"`
|
||||
}
|
||||
json.Unmarshal(body, &resp)
|
||||
if resp.Tree.Path != "tech/go" || resp.Tree.ItemCount != 2 {
|
||||
t.Errorf("tech/go 应 2 条,实为 %q/%d", resp.Tree.Path, resp.Tree.ItemCount)
|
||||
}
|
||||
if resp.Tree.Name != "go" {
|
||||
t.Errorf("name 应为本级段名 go,实为 %q", resp.Tree.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// 不存在的分类:404 + 附现有分类。
|
||||
func TestTreeNotFound(t *testing.T) {
|
||||
r := newRig(t)
|
||||
code, body := r.get(t, "/tree?category=nope/here")
|
||||
if code != http.StatusNotFound {
|
||||
t.Fatalf("应 404,实为 %d", code)
|
||||
}
|
||||
if !strings.Contains(string(body), "categories") {
|
||||
t.Errorf("404 应附现有分类便于自查: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// 检索端点:首位最相关、范围外不混入、缺 q 报 400。
|
||||
func TestSearchEndpoint(t *testing.T) {
|
||||
r := newRig(t)
|
||||
code, body := r.get(t, "/search?q=borrow&category=tech")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("应 200,实为 %d: %s", code, body)
|
||||
}
|
||||
var resp struct {
|
||||
Results []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"results"`
|
||||
Hint string `json:"hint"`
|
||||
}
|
||||
json.Unmarshal(body, &resp)
|
||||
if len(resp.Results) == 0 {
|
||||
t.Fatal("应有命中")
|
||||
}
|
||||
if resp.Results[0].Name != "tech/rust/所有权" {
|
||||
t.Errorf("首位应最相关,实为 %q", resp.Results[0].Name)
|
||||
}
|
||||
for _, it := range resp.Results {
|
||||
if !strings.HasPrefix(it.Name, "tech/") {
|
||||
t.Errorf("范围外 %q 混入", it.Name)
|
||||
}
|
||||
}
|
||||
if resp.Hint == "" {
|
||||
t.Error("应带 hint")
|
||||
}
|
||||
// 缺 q
|
||||
if code, _ := r.get(t, "/search"); code != http.StatusBadRequest {
|
||||
t.Errorf("缺 q 应 400,实为 %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
// 分类与计数端点。
|
||||
func TestCategoriesAndCountsEndpoint(t *testing.T) {
|
||||
r := newRig(t)
|
||||
_, body := r.get(t, "/categories")
|
||||
for _, want := range []string{"tech", "tech/go", "tech/rust", "life", "cook"} {
|
||||
if !strings.Contains(string(body), `"`+want+`"`) {
|
||||
t.Errorf("缺少分类 %s: %s", want, body)
|
||||
}
|
||||
}
|
||||
|
||||
_, body = r.get(t, "/counts")
|
||||
var resp struct {
|
||||
Counts []struct {
|
||||
Category string `json:"category"`
|
||||
Count int `json:"count"`
|
||||
} `json:"counts"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
json.Unmarshal(body, &resp)
|
||||
byCat := map[string]int{}
|
||||
for _, c := range resp.Counts {
|
||||
byCat[c.Category] = c.Count
|
||||
}
|
||||
if byCat["tech"] != 3 {
|
||||
t.Errorf("tech 应聚合 3 条,实为 %d", byCat["tech"])
|
||||
}
|
||||
if resp.Total != 5 {
|
||||
t.Errorf("total 只数叶子应为 5,实为 %d", resp.Total)
|
||||
}
|
||||
}
|
||||
|
||||
// 只读:写方法一律 405,且不改变知识库。
|
||||
func TestReadOnly(t *testing.T) {
|
||||
r := newRig(t)
|
||||
before := len(r.ks.List())
|
||||
for _, m := range []string{http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch} {
|
||||
req, _ := http.NewRequest(m, r.srv.URL+"/tree", nil)
|
||||
req.Header.Set("X-API-Key", r.tok)
|
||||
resp, err := r.srv.Client().Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusMethodNotAllowed {
|
||||
t.Errorf("%s 应 405,实为 %d", m, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
if len(r.ks.List()) != before {
|
||||
t.Error("只读服务不应改变知识库")
|
||||
}
|
||||
}
|
||||
|
||||
// 知识库不可用时 Start 应失败(不占端口)。
|
||||
func TestStartFailsWithoutKnowledge(t *testing.T) {
|
||||
cfg := &types.Config{Daemon: types.DaemonConfig{CheckInterval: time.Minute, HeartbeatInterval: 30 * time.Second}}
|
||||
sup := supervisor.New(cfg)
|
||||
sup.Start()
|
||||
defer sup.Shutdown()
|
||||
s := sdk.New("test", sdk.SDKConfig{
|
||||
Supervisor: supervisor.NewSDKAdapter(sup),
|
||||
Config: sdk.NewConfig(cfg),
|
||||
})
|
||||
if err := New("kbtree").Start(s); err == nil {
|
||||
t.Error("无知识库时 Start 应失败")
|
||||
}
|
||||
}
|
||||
|
||||
// 技能文档里的端点路径必须与代码一致(防止文档漂移)。
|
||||
func TestSkillDocEndpointsMatchCode(t *testing.T) {
|
||||
doc, err := readSkillDoc()
|
||||
if err != nil {
|
||||
t.Skipf("读不到 skill 文档: %v", err)
|
||||
}
|
||||
for _, ep := range []string{"/tree", "/categories", "/counts", "/search"} {
|
||||
if !strings.Contains(doc, ep) {
|
||||
t.Errorf("SKILL.md 未提及端点 %s(文档与代码漂移)", ep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readSkillDoc() (string, error) {
|
||||
// 测试在 internal/plugins/kbtree,skill 在 ../../assets/skills
|
||||
paths := []string{
|
||||
filepath.Join("..", "..", "assets", "skills", "knowledge-base", "SKILL.md"),
|
||||
filepath.Join("..", "..", "..", "assets", "skills", "knowledge-base", "SKILL.md"),
|
||||
}
|
||||
var lastErr error
|
||||
for _, p := range paths {
|
||||
if b, err := readFile(p); err == nil {
|
||||
return string(b), nil
|
||||
} else {
|
||||
lastErr = err
|
||||
}
|
||||
}
|
||||
return "", lastErr
|
||||
}
|
||||
|
||||
func readFile(p string) ([]byte, error) {
|
||||
return os.ReadFile(p)
|
||||
}
|
||||
@ -1647,19 +1647,23 @@
|
||||
'</span><span class="val" id="know-count">' +
|
||||
(k?.knowledge?.item_count ?? "-") +
|
||||
"</span></div>" +
|
||||
'<div style="margin-top:8px;display:flex;gap:4px;flex-wrap:wrap">' +
|
||||
'<details id="know-tree-box" style="margin-top:8px">' +
|
||||
'<summary style="cursor:pointer;font-size:12px;opacity:.8">' +
|
||||
__("按分类浏览", "Browse by category") +
|
||||
'</summary>' +
|
||||
'<div id="know-tree" style="margin-top:6px;max-height:200px;overflow:auto;font-size:12px"></div>' +
|
||||
'</details>' +
|
||||
'<div style="margin-top:8px;display:flex;gap:4px;flex-wrap:wrap;align-items:center">' +
|
||||
'<input id="know-query" placeholder="' +
|
||||
__("搜索知识", "Search knowledge") +
|
||||
'" style="flex:1;min-width:120px">' +
|
||||
'<input id="know-category" placeholder="' +
|
||||
__("分类(可选)", "Category (optional)") +
|
||||
'" style="width:110px" title="' +
|
||||
__("限定在该分类子树内,如 tech 会搜 tech/go、tech/rust。留空则搜全库",
|
||||
"Limit search to a category subtree, e.g. tech covers tech/go, tech/rust. Empty searches all") +
|
||||
'">' +
|
||||
'<span id="know-cat-scope" style="font-size:11px;opacity:.7"></span>' +
|
||||
'<button class="btn btn-primary btn-sm" onclick="searchKnowledgeChat()">' +
|
||||
__("搜索", "Search") +
|
||||
"</button>" +
|
||||
'<button class="btn btn-sm" onclick="clearKnowledgeCategory()">' +
|
||||
__("全库", "All") +
|
||||
"</button>" +
|
||||
'</div><div id="know-result-chat" style="margin-top:8px;max-height:220px;overflow:auto"></div>' +
|
||||
'<div style="margin-top:12px;border-top:1px solid var(--border-color);padding-top:8px">' +
|
||||
'<input id="know-name" placeholder="' +
|
||||
@ -2990,9 +2994,92 @@
|
||||
return html;
|
||||
}
|
||||
|
||||
// 知识库分类树浏览。
|
||||
//
|
||||
// 数据来自 /knowledge/tree(只读树接口)。当前分类存 state,
|
||||
// 搜索时自动带上 —— 这样"先定位分类再检索"这个更准的用法在 UI 上
|
||||
// 是一步的事,而不是要求用户手打分类名。
|
||||
var _knowCat = ""; // 当前分类的完整 path;"" = 全库
|
||||
|
||||
function renderKnowTree(node, depth) {
|
||||
var html = "";
|
||||
// 本节点的条目
|
||||
(node.items || []).forEach(function (it) {
|
||||
html +=
|
||||
'<div style="padding-left:' +
|
||||
(depth * 12 + 4) +
|
||||
'px;padding-top:1px;padding-bottom:1px">' +
|
||||
'<span style="opacity:.7">▸</span> ' +
|
||||
'<span style="word-break:break-all">' +
|
||||
knowEsc(it.name) +
|
||||
"</span>" +
|
||||
(it.size ? ' <span style="opacity:.5;font-size:10px">' + it.size + "B</span>" : "") +
|
||||
(it.media && it.media.length
|
||||
? ' <span style="opacity:.6;font-size:10px">[' + it.media.length + __(" 媒体", " media") + "]</span>"
|
||||
: "") +
|
||||
"</div>";
|
||||
});
|
||||
// 子分类
|
||||
(node.children || []).forEach(function (c) {
|
||||
var isCur = _knowCat === c.path;
|
||||
var mark = isCur ? "● " : "";
|
||||
html +=
|
||||
'<div style="padding-left:' +
|
||||
(depth * 12) +
|
||||
'px"><span onclick="selectKnowledgeCategory(\'' +
|
||||
knowEsc(c.path).replace(/'/g, "'") +
|
||||
'\')" style="cursor:pointer;user-select:none">' +
|
||||
mark +
|
||||
knowEsc(c.name) +
|
||||
' <span style="opacity:.55;font-size:10px">' +
|
||||
c.item_count +
|
||||
"/" +
|
||||
c.total_count +
|
||||
"</span></span></div>";
|
||||
html += renderKnowTree(c, depth + 1);
|
||||
});
|
||||
return html;
|
||||
}
|
||||
|
||||
async function loadKnowledgeTree() {
|
||||
var box = document.getElementById("know-tree");
|
||||
if (!box) return;
|
||||
box.innerHTML = '<div class="loading"></div>';
|
||||
try {
|
||||
var d = await api("/knowledge/tree?items=1");
|
||||
if (!d || !d.tree) {
|
||||
box.innerHTML =
|
||||
'<p style="opacity:.6;font-size:11px">' + __("暂无分类", "No categories yet") + "</p>";
|
||||
return;
|
||||
}
|
||||
box.innerHTML = renderKnowTree(d.tree, 0);
|
||||
} catch (e) {
|
||||
box.innerHTML =
|
||||
'<p style="color:#fca5a5;font-size:11px">' + __("加载分类失败: ", "Load categories failed: ") + knowEsc(e.message) + "</p>";
|
||||
}
|
||||
}
|
||||
|
||||
function selectKnowledgeCategory(path) {
|
||||
_knowCat = path || "";
|
||||
var lbl = document.getElementById("know-cat-scope");
|
||||
if (lbl) {
|
||||
lbl.textContent = _knowCat
|
||||
? __("范围:", "scope: ") + _knowCat
|
||||
: __("范围:全库", "scope: all");
|
||||
}
|
||||
loadKnowledgeTree();
|
||||
// 立刻按新范围搜一次,省一次点击
|
||||
var q = document.getElementById("know-query")?.value;
|
||||
if (q) searchKnowledgeChat();
|
||||
}
|
||||
|
||||
function clearKnowledgeCategory() {
|
||||
selectKnowledgeCategory("");
|
||||
}
|
||||
|
||||
async function searchKnowledgeChat() {
|
||||
var q = document.getElementById("know-query")?.value;
|
||||
var cat = document.getElementById("know-category")?.value || "";
|
||||
var cat = _knowCat || "";
|
||||
var r = document.getElementById("know-result-chat");
|
||||
if (!r) return;
|
||||
if (!q) {
|
||||
@ -3082,7 +3169,14 @@
|
||||
function switchChatPanel(tab, el) {
|
||||
// 切到知识面板时拉实时计数:面板里的数字来自 state.kernel 快照,
|
||||
// 而知识条目会经工具/上传增删,快照不会自己变(实测创建后仍显示 "-")。
|
||||
if (tab === "knowledge") refreshKnowledgeCount();
|
||||
if (tab === "knowledge") {
|
||||
refreshKnowledgeCount();
|
||||
var lbl = document.getElementById("know-cat-scope");
|
||||
if (lbl && !lbl.textContent) {
|
||||
lbl.textContent = _knowCat ? __("范围:", "scope: ") + _knowCat : __("范围:全库", "scope: all");
|
||||
}
|
||||
loadKnowledgeTree();
|
||||
}
|
||||
var panels = {
|
||||
chat: document.getElementById("chat-panel-chat"),
|
||||
starmap: document.getElementById("chat-panel-starmap"),
|
||||
|
||||
@ -467,6 +467,11 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/api/v1/settings/", h.requireAPI(h.handleSettings))
|
||||
mux.HandleFunc("/api/v1/knowledge", h.requireAPI(h.handleKnowledge))
|
||||
mux.HandleFunc("/api/v1/knowledge/", h.requireAPI(h.handleKnowledge))
|
||||
// 知识库的树形只读面(供外部 agent / skill 按分类导航)。
|
||||
// 路由顺序无关:Go 1.22+ 的 ServeMux 取**最长前缀匹配**
|
||||
// (/knowledge/ 只是子树通配,不会吃掉 /knowledge/tree)。go.mod 要求 1.25。
|
||||
mux.HandleFunc("/api/v1/knowledge/tree", h.requireAPI(h.handleKnowledgeTree))
|
||||
mux.HandleFunc("/api/v1/knowledge/tree/", h.requireAPI(h.handleKnowledgeTree))
|
||||
mux.HandleFunc("/api/v1/adapters", h.requireAPI(h.handleAdapters))
|
||||
mux.HandleFunc("/api/v1/adapters/", h.requireAPI(h.handleAdapterByID))
|
||||
mux.HandleFunc("/api/v1/tracker", h.requireAPI(h.handleTracker))
|
||||
|
||||
150
internal/plugins/webui/handler_knowledge_tree.go
Normal file
150
internal/plugins/webui/handler_knowledge_tree.go
Normal file
@ -0,0 +1,150 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
sdk "gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||||
)
|
||||
|
||||
// 知识库的**树形只读 API**。供外部 agent、skill、脚本按分类导航知识库。
|
||||
//
|
||||
// 为何只读:写入面已有 POST /api/v1/knowledge(可带媒体)。而写操作
|
||||
// 需要决定"挂到哪个分类、是否带媒体、是否重算稠密向量",那是内核的事;
|
||||
// 让外部 agent 自己拼分类路径反而容易写出越界/重名的条目。读多写少,
|
||||
// 且读才是"让别的 agent 用起来"的关键。
|
||||
//
|
||||
// 鉴权沿用 requireAPI(api_key 或 cookie session),不新增鉴权面。
|
||||
//
|
||||
// 路由:
|
||||
//
|
||||
// GET /api/v1/knowledge/tree 整棵树
|
||||
// GET /api/v1/knowledge/tree/{category} 某棵子树
|
||||
// ?depth=N 限制层数(0/省略 = 不限)——分类多时做懒加载
|
||||
// ?items=0|1 是否返回条目详情,默认 1
|
||||
// ?preview=N 预览字数上限,默认 120
|
||||
// ?q=关键词 在**该子树内**检索(分类 + 关键词组合)
|
||||
// ?limit=N q 时的返回条数,默认 10
|
||||
// GET /api/v1/knowledge/tree/categories 平铺分类列表
|
||||
// GET /api/v1/knowledge/tree/counts 各分类条目数(倒序)
|
||||
//
|
||||
// 为什么把 categories/counts 也挂在 /tree/ 下:它们是**导航辅助**,
|
||||
// 回答"有哪些分类、哪里的内容最多",与树形视图同源,挂在别处会割裂。
|
||||
|
||||
// handleKnowledgeTree 处理树形只读请求。
|
||||
func (h *Handler) handleKnowledgeTree(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "只支持 GET"})
|
||||
return
|
||||
}
|
||||
if h.knowledge == nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "knowledge not available"})
|
||||
return
|
||||
}
|
||||
|
||||
// path 形如 /api/v1/knowledge/tree[/sub/category]
|
||||
sub := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/v1/knowledge/tree"), "/")
|
||||
|
||||
// 平铺分类列表
|
||||
if sub == "categories" {
|
||||
names, err := h.knowledge.Categories()
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if names == nil {
|
||||
names = []string{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"categories": names})
|
||||
return
|
||||
}
|
||||
// 各分类条目数
|
||||
if sub == "counts" {
|
||||
counts, err := h.knowledge.CategoryCounts()
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if counts == nil {
|
||||
counts = []sdk.KnowledgeCategoryCount{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"counts": counts})
|
||||
return
|
||||
}
|
||||
|
||||
q := r.URL.Query()
|
||||
opt := sdk.KnowledgeTreeOptions{
|
||||
MaxDepth: intQuery(q.Get("depth"), 0),
|
||||
IncludeItems: boolQuery(q.Get("items"), true),
|
||||
PreviewLimit: intQuery(q.Get("preview"), 0),
|
||||
}
|
||||
|
||||
// 分类 + 关键词:在该子树内检索
|
||||
if keyword := strings.TrimSpace(q.Get("q")); keyword != "" {
|
||||
limit := intQuery(q.Get("limit"), 10)
|
||||
if limit <= 0 || limit > 100 {
|
||||
limit = 10
|
||||
}
|
||||
results, err := h.knowledge.SearchIn(keyword, sub, limit)
|
||||
if err != nil {
|
||||
code, msg := knowledgeStatusFor(err)
|
||||
writeJSON(w, code, map[string]string{"error": msg})
|
||||
return
|
||||
}
|
||||
views := make([]knowledgeView, 0, len(results))
|
||||
for _, k := range results {
|
||||
views = append(views, toKnowledgeView(k, k.Name))
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"results": views,
|
||||
"category": sub,
|
||||
"query": keyword,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 树视图(category 为空 = 整棵树)
|
||||
view, err := h.knowledge.Subtree(sub, opt)
|
||||
if err != nil {
|
||||
code, msg := knowledgeStatusFor(err)
|
||||
writeJSON(w, code, map[string]string{"error": msg})
|
||||
return
|
||||
}
|
||||
if view == nil {
|
||||
// 分类不存在:这是调用方能自己纠正的错误,给 404 + 现有分类便于自查
|
||||
known, _ := h.knowledge.Categories()
|
||||
writeJSON(w, http.StatusNotFound, map[string]interface{}{
|
||||
"error": "分类不存在: " + sub,
|
||||
"categories": known,
|
||||
})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"tree": view})
|
||||
}
|
||||
|
||||
// intQuery 解析整数查询参数;空/非法时用 def。
|
||||
func intQuery(s string, def int) int {
|
||||
if s == "" {
|
||||
return def
|
||||
}
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return def
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// boolQuery 解析布尔查询参数;空时用 def。接受 1/0/true/false。
|
||||
func boolQuery(s string, def bool) bool {
|
||||
if s == "" {
|
||||
return def
|
||||
}
|
||||
switch strings.ToLower(s) {
|
||||
case "1", "true", "yes", "on":
|
||||
return true
|
||||
case "0", "false", "no", "off":
|
||||
return false
|
||||
}
|
||||
return def
|
||||
}
|
||||
314
internal/plugins/webui/handler_knowledge_tree_test.go
Normal file
314
internal/plugins/webui/handler_knowledge_tree_test.go
Normal file
@ -0,0 +1,314 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/knowledge"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/supervisor"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/pkg/types"
|
||||
)
|
||||
|
||||
func newTreeHandler(t *testing.T) (*Handler, *knowledge.Store) {
|
||||
t.Helper()
|
||||
ks := knowledge.NewStore(t.TempDir())
|
||||
if err := ks.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(ks.Stop)
|
||||
for _, e := range []struct{ name, body string }{
|
||||
{"tech/go/并发", "goroutine 调度 GMP 抢占"},
|
||||
{"tech/go/context", "context 取消 超时"},
|
||||
{"tech/rust/所有权", "borrow checker move 语义"},
|
||||
{"life/sleep", "作息 褪黑素"},
|
||||
} {
|
||||
if err := ks.Add(e.name, e.body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
cfg := &types.Config{Daemon: types.DaemonConfig{
|
||||
CheckInterval: time.Minute, HeartbeatInterval: 30 * time.Second,
|
||||
}}
|
||||
sup := supervisor.New(cfg)
|
||||
sup.Start()
|
||||
t.Cleanup(sup.Shutdown)
|
||||
return NewHandler(testSDK(sdk.SDKConfig{
|
||||
Supervisor: supervisor.NewSDKAdapter(sup),
|
||||
Knowledge: sdk.NewKnowledge(ks),
|
||||
Config: sdk.NewConfig(cfg),
|
||||
})), ks
|
||||
}
|
||||
|
||||
// 路由不被 /api/v1/knowledge/ 通配吃掉(Go 1.22+ 最长前缀匹配)。
|
||||
func TestKnowledgeTreeRouteNotSwallowed(t *testing.T) {
|
||||
h, _ := newTreeHandler(t)
|
||||
mux := http.NewServeMux()
|
||||
h.RegisterRoutes(mux)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
mux.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api/v1/knowledge/tree", nil))
|
||||
if rec.Code == http.StatusNotFound && rec.Body.Len() == 0 {
|
||||
t.Fatal("路由未命中:/api/v1/knowledge/tree 返回了 mux 的 404")
|
||||
}
|
||||
// 鉴权未配置时是 503(说明命中了我们的 handler,而不是 mux 404)
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Errorf("未配 api_key 时应为 503,实为 %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func treeGet(t *testing.T, h *Handler, path string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
// 绕过鉴权直接打 handler:这里测的是路由与业务,鉴权另有测试覆盖
|
||||
h.handleKnowledgeTree(rec, req)
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestKnowledgeTreeAPI(t *testing.T) {
|
||||
h, _ := newTreeHandler(t)
|
||||
|
||||
rec := treeGet(t, h, "/api/v1/knowledge/tree")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("应 200,实为 %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
Tree struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Children []struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Children []struct {
|
||||
ItemCount int `json:"item_count"`
|
||||
} `json:"children"`
|
||||
} `json:"children"`
|
||||
} `json:"tree"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.Tree.Name != "root" || resp.Tree.Path != "" {
|
||||
t.Errorf("根应为 root/空,实为 %q/%q", resp.Tree.Name, resp.Tree.Path)
|
||||
}
|
||||
if resp.Tree.TotalCount != 4 {
|
||||
t.Errorf("总条目应为 4,实为 %d", resp.Tree.TotalCount)
|
||||
}
|
||||
// 子节点 Name 是本级段名、Path 是全路径
|
||||
var tech *struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Children []struct {
|
||||
ItemCount int `json:"item_count"`
|
||||
} `json:"children"`
|
||||
}
|
||||
for i := range resp.Tree.Children {
|
||||
if resp.Tree.Children[i].Name == "tech" {
|
||||
tech = &resp.Tree.Children[i]
|
||||
}
|
||||
}
|
||||
if tech == nil {
|
||||
t.Fatal("缺少 tech 节点")
|
||||
}
|
||||
if tech.Path != "tech" || tech.TotalCount != 3 {
|
||||
t.Errorf("tech 应为 path=tech total=3,实为 %q/%d", tech.Path, tech.TotalCount)
|
||||
}
|
||||
if len(tech.Children) != 2 {
|
||||
t.Errorf("tech 下应有 2 个子分类,实为 %d", len(tech.Children))
|
||||
}
|
||||
|
||||
// 响应里不得出现向量
|
||||
if containsStr(rec.Body.String(), `"vector"`) {
|
||||
t.Error("树 API 不应返回向量")
|
||||
}
|
||||
}
|
||||
|
||||
// 子树路由:/tree/tech/go
|
||||
func TestKnowledgeTreeSubtreeAPI(t *testing.T) {
|
||||
h, _ := newTreeHandler(t)
|
||||
rec := treeGet(t, h, "/api/v1/knowledge/tree/tech/go")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("应 200,实为 %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
Tree struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
ItemCount int `json:"item_count"`
|
||||
} `json:"tree"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
if resp.Tree.Path != "tech/go" || resp.Tree.ItemCount != 2 {
|
||||
t.Errorf("tech/go 应有 2 条,实为 %q/%d", resp.Tree.Path, resp.Tree.ItemCount)
|
||||
}
|
||||
if resp.Tree.Name != "go" {
|
||||
t.Errorf("Name 应为本级段名 go,实为 %q", resp.Tree.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// 不存在的分类:404 + 附上现有分类便于自查。
|
||||
func TestKnowledgeTreeNotFound(t *testing.T) {
|
||||
h, _ := newTreeHandler(t)
|
||||
rec := treeGet(t, h, "/api/v1/knowledge/tree/nope/here")
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("应 404,实为 %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
Error string `json:"error"`
|
||||
Categories []string `json:"categories"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
if len(resp.Categories) == 0 {
|
||||
t.Error("404 应附带现有分类列表,便于调用方自查")
|
||||
}
|
||||
}
|
||||
|
||||
// 懒加载:depth=1 只给一层但计数准确;items=0 不返回条目。
|
||||
func TestKnowledgeTreeLazyLoad(t *testing.T) {
|
||||
h, _ := newTreeHandler(t)
|
||||
rec := treeGet(t, h, "/api/v1/knowledge/tree?depth=1&items=0")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("应 200,实为 %d", rec.Code)
|
||||
}
|
||||
var resp struct {
|
||||
Tree struct {
|
||||
TotalCount int `json:"total_count"`
|
||||
Children []struct {
|
||||
Name string `json:"name"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Children []any `json:"children"`
|
||||
} `json:"children"`
|
||||
} `json:"tree"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
if resp.Tree.TotalCount != 4 {
|
||||
t.Errorf("剪枝不应改变总数,实为 %d", resp.Tree.TotalCount)
|
||||
}
|
||||
for _, c := range resp.Tree.Children {
|
||||
if len(c.Children) != 0 {
|
||||
t.Errorf("depth=1 时 %s 不应展开子节点", c.Name)
|
||||
}
|
||||
}
|
||||
if containsStr(rec.Body.String(), `"preview"`) {
|
||||
t.Error("items=0 时不应返回 preview")
|
||||
}
|
||||
}
|
||||
|
||||
// 分类内检索:/tree/tech?q=...
|
||||
func TestKnowledgeTreeSearchInCategory(t *testing.T) {
|
||||
h, _ := newTreeHandler(t)
|
||||
rec := treeGet(t, h, "/api/v1/knowledge/tree/tech?q=borrow")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("应 200,实为 %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
Results []knowledgeView `json:"results"`
|
||||
Category string `json:"category"`
|
||||
Query string `json:"query"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
// 首位必须是真正相关的 rust/所有权。
|
||||
//
|
||||
// 不断言"只命中 1 条":词法路对任何查询都会给全库打个低分(这是它
|
||||
// 融合设计的已知性质——靠排序把强命中顶到前面,而不是靠过滤把弱命中
|
||||
// 删掉),所以 tech 子树内会有 3 条候选。这是有意行为,不是缺陷。
|
||||
if len(resp.Results) == 0 {
|
||||
t.Fatal("tech 子树内检索 borrow 应有命中")
|
||||
}
|
||||
if resp.Results[0].Name != "tech/rust/所有权" {
|
||||
t.Errorf("首位应是最相关的 rust/所有权,实为 %q", resp.Results[0].Name)
|
||||
}
|
||||
// 但范围外的 life/sleep 绝不能出现
|
||||
for _, r := range resp.Results {
|
||||
if !strings.HasPrefix(r.Name, "tech/") {
|
||||
t.Errorf("范围外条目 %q 混入 tech 子树检索结果", r.Name)
|
||||
}
|
||||
}
|
||||
if resp.Category != "tech" {
|
||||
t.Errorf("应回显 category=tech,实为 %q", resp.Category)
|
||||
}
|
||||
}
|
||||
|
||||
// 平铺分类与计数端点。
|
||||
func TestKnowledgeTreeCategoriesAndCounts(t *testing.T) {
|
||||
h, _ := newTreeHandler(t)
|
||||
|
||||
rec := treeGet(t, h, "/api/v1/knowledge/tree/categories")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("categories 应 200,实为 %d", rec.Code)
|
||||
}
|
||||
var c1 struct {
|
||||
Categories []string `json:"categories"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &c1)
|
||||
want := map[string]bool{"tech": true, "tech/go": true, "tech/rust": true, "life": true}
|
||||
for _, c := range c1.Categories {
|
||||
delete(want, c)
|
||||
}
|
||||
if len(want) != 0 {
|
||||
t.Errorf("缺少 %v,实为 %v", want, c1.Categories)
|
||||
}
|
||||
|
||||
rec = treeGet(t, h, "/api/v1/knowledge/tree/counts")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("counts 应 200,实为 %d", rec.Code)
|
||||
}
|
||||
var c2 struct {
|
||||
Counts []struct {
|
||||
Category string `json:"category"`
|
||||
Count int `json:"count"`
|
||||
} `json:"counts"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &c2)
|
||||
byName := map[string]int{}
|
||||
for _, x := range c2.Counts {
|
||||
byName[x.Category] = x.Count
|
||||
}
|
||||
if byName["tech"] != 3 || byName["life"] != 1 {
|
||||
t.Errorf("计数错误: %v", byName)
|
||||
}
|
||||
}
|
||||
|
||||
// 只读面:非 GET 一律拒绝(防止有人以为能通过它写入)。
|
||||
func TestKnowledgeTreeIsReadOnly(t *testing.T) {
|
||||
h, ks := newTreeHandler(t)
|
||||
before := len(ks.List())
|
||||
for _, m := range []string{http.MethodPost, http.MethodDelete, http.MethodPut, http.MethodPatch} {
|
||||
rec := httptest.NewRecorder()
|
||||
h.handleKnowledgeTree(rec, httptest.NewRequest(m, "/api/v1/knowledge/tree", nil))
|
||||
if rec.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("%s 应 405,实为 %d", m, rec.Code)
|
||||
}
|
||||
}
|
||||
if len(ks.List()) != before {
|
||||
t.Error("只读端点不应改变条目数")
|
||||
}
|
||||
}
|
||||
|
||||
// 知识库不可用时给 503 而不是 panic。
|
||||
func TestKnowledgeTreeUnavailable(t *testing.T) {
|
||||
h, _ := newTestHandler(t)
|
||||
rec := treeGet(t, h, "/api/v1/knowledge/tree")
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Errorf("无知识库应 503,实为 %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func containsStr(hay, needle string) bool {
|
||||
return len(hay) >= len(needle) && (func() bool {
|
||||
for i := 0; i+len(needle) <= len(hay); i++ {
|
||||
if hay[i:i+len(needle)] == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})()
|
||||
}
|
||||
Reference in New Issue
Block a user