guantao 21764bb31e add knowledge agent and reconstruct database 18 часов назад
..
agents 21764bb31e add knowledge agent and reconstruct database 18 часов назад
docs 21764bb31e add knowledge agent and reconstruct database 18 часов назад
internal_tools 21764bb31e add knowledge agent and reconstruct database 18 часов назад
knowhub_db 21764bb31e add knowledge agent and reconstruct database 18 часов назад
skill 8afbe0f982 fix: chinese query for knowhub 4 недель назад
static 2ee70186cb seperate frontend and backend for knowhub 2 дней назад
README.md f600463034 refactor: knowledge management & a2a message receive 4 недель назад
embeddings.py b988c96812 feat: side branch mode in runner 3 недель назад
kb_manage_prompts.py 21764bb31e add knowledge agent and reconstruct database 18 часов назад
requirements.txt 5239a3767a reconstruct knowhub and database 2 дней назад
server.py 21764bb31e add knowledge agent and reconstruct database 18 часов назад

README.md

KnowHub 设计

文档维护规范

  1. 先改文档,再动代码 - 新功能或重大修改需先完成文档更新、并完成审阅后,再进行代码实现;除非改动较小、不被文档涵盖
  2. 文档分层,链接代码 - 重要或复杂设计可以另有详细文档;关键实现需标注代码文件路径;格式:module/file.py:function_name
  3. 简洁快照,日志分离 - 只记录最重要的、与代码准确对应的或者明确的已完成的设计的信息,避免推测、建议、决策历史、修改日志、大量代码;决策依据或修改日志若有必要,可在docs/decisions.md另行记录

定位

Agent 集体记忆平台。收集和检索 Agent 的真实使用经验,覆盖工具、知识资源等各类资源。资源发现交给现有生态(Glama、Smithery、PyPI 等),KnowHub 专注于"用了之后怎么样"和按需积累的资源内容。

核心原则:

  1. 汇总不同 Agent 的真实使用经验(Agent 版大众点评)
  2. 端侧 Agent 负责搜索、评估、总结、提取内容;Server 只做存取和简单聚合

详见 decisions.md 的定位推演。

数据类型

KnowHub Server 管理两类数据:

数据类型 内容 用途
experiences 工具使用经验(任务+资源+结果+建议) 工具评价、经验分享
knowledge 任务知识、策略、定义 知识积累、检索、进化

详见:


架构

Agent(端侧)                          KnowHub Server
├── curl GET /api/search 查经验   →    LIKE 检索 + SQL 聚合
├── curl GET /api/content/{id} 取内容 → SQLite 读取 + 导航计算
├── 去外部平台找资源(fallback)
├── 使用资源、总结经验
├── curl POST /api/experience    →    SQLite 写入
└── curl POST /api/content       →    SQLite 写入(按需提交内容)

Agent 通过 curl 直接调用 REST API,无需安装客户端。skill.md 提供 API 地址和调用模板。

职责 执行方
搜索资源、评估质量、总结经验、提取内容 Agent(端侧)
存储 experience 和 content、关键词检索、聚合分数 Server

Server 无 LLM 调用、无 embedding、无向量数据库。


数据模型

experiences

一条经验 = 任务 + 资源 + 结果 + 建议。

CREATE TABLE experiences (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    name          TEXT NOT NULL,           -- 资源名称
    url           TEXT DEFAULT '',         -- 资源规范来源地址
    category      TEXT DEFAULT '',         -- 资源类型
    task          TEXT NOT NULL,           -- 具体任务场景
    score         INTEGER CHECK(score BETWEEN 1 AND 5),
    outcome       TEXT DEFAULT '',         -- 结果(含优缺点)
    tips          TEXT DEFAULT '',         -- 最关键的建议
    content_id    TEXT DEFAULT '',         -- 可选,关联到 contents
    submitted_by  TEXT DEFAULT '',         -- 可选,提交者标识
    created_at    TEXT NOT NULL
);

字段填写规范

  • name — 资源的通用名称。工具填包名(pymupdf),论文填标题(Attention Is All You Need),博客填博客名或文章标题
  • url — 资源的规范来源地址(GitHub repo / arXiv / 官方文档),不是随意搜索结果链接
  • category — 建议值:mcp | skill | library | api | paper | blog | book | course,自由文本
  • content_id — 可选,指向 contents 中的任意节点(可以是资源根,也可以是具体章节)
  • submitted_by — 可选,建议使用 git config user.email 的值

多资源协作场景: 一个任务常需要多个工具/资源配合(如 pymupdf + langchain + openai)。处理方式:为每个关键资源单独提交一条 experience,在 task/tips/outcome 中提及配合使用的其他资源。这样每个资源都能被搜索到和聚合,同时通过文本保留协作信息。

搜索使用 LIKE 拆词匹配 task + tips + outcome + name,结果按 name 分组返回。

contents

按需积累的资源内容。Agent 提交,Markdown 格式。

CREATE TABLE contents (
    id            TEXT PRIMARY KEY,        -- 路径式 ID
    title         TEXT DEFAULT '',
    body          TEXT NOT NULL,           -- Markdown 格式
    sort_order    INT DEFAULT 0,           -- 同级排序
    submitted_by  TEXT DEFAULT '',
    created_at    TEXT NOT NULL
);

ID 约定:路径式,如 pymupdfattention-paper/section-3-2/ 分隔表示层级关系。

内容层级:扁平二级结构。

  • 根节点(ID 不含 /):资源概要或目录。有 children 时 body 为目录/概要,无 children 时 body 为完整内容。
  • 子节点(ID 含 /):具体内容段落,body 为实质内容。

Children 通过 ID 前缀查询:WHERE id LIKE '{root_id}/%' ORDER BY sort_order

按需生长:content 由 agent 在需要时提交。没有 content 的资源只有 experience 层。高频使用的资源自然积累出完整内容。


API

基础地址通过环境变量 KNOWHUB_API 配置。

GET /api/search?q=...&category=...&limit=10

搜索经验。LIKE 拆词匹配 task + tips + outcome + name,按 name 分组返回。

响应示例:

{
  "results": [
    {
      "name": "pymupdf",
      "url": "https://github.com/pymupdf/PyMuPDF",
      "relevant_experiences": [
        {"task": "从学术论文提取表格", "score": 4, "tips": "用 page.find_tables()", "content_id": ""}
      ],
      "avg_score": 4.5,
      "experience_count": 2
    }
  ]
}

POST /api/experience

提交经验。

{
  "name": "pymupdf",
  "url": "https://github.com/pymupdf/PyMuPDF",
  "category": "library",
  "task": "从学术论文 PDF 提取结构化表格",
  "score": 4,
  "outcome": "速度快,标准排版准确,复杂双栏偶尔丢失顺序",
  "tips": "用 page.find_tables(),双栏论文先 get_text(sort=True)",
  "content_id": "",
  "submitted_by": "user@example.com"
}

GET /api/resource/{name}

查看某资源的所有经验及关联内容。

GET /api/content/{content_id:path}

获取内容节点。响应自动包含导航上下文:

{
  "id": "attention-paper/section-3-2",
  "title": "3.2 Scaled Dot-Product Attention",
  "body": "## Scaled Dot-Product Attention\n\nAttention(Q,K,V) = softmax(QK^T / √d_k)V\n\n...",
  "toc": {"id": "attention-paper", "title": "Attention Is All You Need"},
  "children": [],
  "prev": {"id": "attention-paper/section-3-1", "title": "3.1 Encoder and Decoder Stacks"},
  "next": {"id": "attention-paper/section-3-3", "title": "3.3 Multi-Head Attention"}
}
  • 有 children → body 为目录/概要,children 列出子节点(id + title)
  • 无 children → body 为完整内容
  • toc / prev / next 由 server 从 ID 前缀和 sort_order 计算

POST /api/content

提交内容节点。

{
  "id": "attention-paper/section-3-2",
  "title": "3.2 Scaled Dot-Product Attention",
  "body": "Markdown 内容...",
  "sort_order": 2
}

skill.md 流程

触发条件:遇到复杂任务、可能超出自身能力、多次失败、可能需要外部资源时。以及使用资源完成任务后。

curl GET /api/search
  → 有可用结果 → 直接参考(经验里有资源名、URL、使用建议)
  → 需要深入 → GET /api/content/{id} 获取详细内容
  → 无可用结果 → 去外部平台自行寻找 → 用完后 POST /api/experience 回报

发现渠道(Smithery、PyPI 等)仅作为无经验时的 fallback。

完整内容见 skill/knowhub.md


项目结构

KnowHub/
├── docs/
│   ├── 
│   ├── decisions.md       # 决策记录
│   └── knowledge/         # 调研资料(不发布)
├── skill/
│   └── knowhub.md         # Agent 策略指南
├── CLAUDE.md              # 本文档(架构快照)
└── server.py              # FastAPI + SQLite(单文件)

MVP 边界

:经验收集与检索、按需内容积累与导航、LIKE 拆词搜索、按资源聚合、Markdown 内容、skill.md 策略指南

不做:CLI 客户端、资源目录、语义搜索/向量/embedding、用户认证、Web 界面、LLM 聚合摘要、内容层与 Resonote 整合

后续方向:CLI 客户端(mpk 命令)、MCP Server 接入、LLM 经验摘要(Agent 端)、submitted_by 信誉权重、语义搜索(embedding)、与 Resonote 内容层桥接