ontology - 类型化知识图谱用于结构化代理记忆与可组合技能
v1.0.4提供本地文件化的类型化知识图谱,实现实体的增删改查、关系链接、约束校验以及图遍历,支持多步骤计划和跨技能状态共享。
详细分析 ▾
运行时依赖
版本
首次发布 ontology 技能,实现类型化、约束校验的知识图谱。支持实体和关系的增删改查、属性和关系的校验,以及针对 Person、Project、Task、Event、Document 等常用类型的图遍历。提供基于 schema 的约束,包括必需属性、枚举值、禁用字段、基数约束以及无环检查。支持多步骤计划和跨技能共享记忆。附带通过 JSONL 存储的 CLI 工具用于创建、查询、关联和验证图数据。
安装命令
点击复制本土化适配说明
本技能为纯本地实现,仅需 Python 环境,无需额外依赖或网络下载。首次使用前请确保工作区具备写入权限。
技能文档
一个 类型化词汇表 + 约束系统,用于将知识表示为可验证的图结构。
核心概念
一切都是 实体,拥有 类型、属性 与指向其他实体的 关系。每一次变更在提交前都会依据类型约束进行校验。
Entity: { id, type, properties, relations, created, updated }
Relation: { from_id, relation_type, to_id, properties }
何时使用
| 触发词 | 操作 |
|---|---|
| "记住..." | 创建/更新实体 |
| "我对 X 知道什么?" | 查询图 |
| "链接 X 到 Y" | 创建关系 |
| "显示项目 Z 的所有任务" | 图遍历 |
| "什么依赖于 X?" | 依赖查询 |
| 多步骤工作计划 | 视为图转换 |
| 技能需要共享状态 | 读写本体对象 |
核心类型
# Agents & People Person: { name, email?, phone?, notes? } Organization: { name, type?, members[] }# Work Project: { name, status, goals[], owner? } Task: { title, status, due?, priority?, assignee?, blockers[] } Goal: { description, target_date?, metrics[] }
# Time & Place Event: { title, start, end?, location?, attendees[], recurrence? } Location: { name, address?, coordinates? }
# Information Document: { title, path?, url?, summary? } Message: { content, sender, recipients[], thread? } Thread: { subject, participants[], messages[] } Note: { content, tags[], refs[] }
# Resources Account: { service, username, credential_ref? } Device: { name, type, identifiers[] } Credential: { service, secret_ref } # Never store secrets directly
# Meta Action: { type, target, timestamp, outcome? } Policy: { scope, rule, enforcement }
存储
默认路径: memory/ontology/graph.jsonl
{"op":"create","entity":{"id":"p_001","type":"Person","properties":{"name":"Alice"}}}
{"op":"create","entity":{"id":"proj_001","type":"Project","properties":{"name":"Website Redesign","status":"active"}}}
{"op":"relate","from":"proj_001","rel":"has_owner","to":"p_001"}
通过脚本或直接文件操作进行查询。对于复杂图,可迁移至 SQLite。
追加写入规则
在处理已有本体数据或 schema 时,请 追加/合并 更改而非覆写文件,以保留历史记录并避免冲突。
工作流
创建实体
python3 scripts/ontology.py create --type Person --props '{"name":"Alice","email":"alice@example.com"}'
查询
python3 scripts/ontology.py query --type Task --where '{"status":"open"}'
python3 scripts/ontology.py get --id task_001
python3 scripts/ontology.py related --id proj_001 --rel has_task
关联实体
python3 scripts/ontology.py relate --from proj_001 --rel has_task --to task_001
校验
python3 scripts/ontology.py validate # 检查所有约束
约束
在 memory/ontology/schema.yaml 中定义:
types: Task: required: [title, status] status_enum: [open, in_progress, blocked, done] Event: required: [title, start] validate: "end >= start if end exists"Credential: required: [service, secret_ref] forbidden_properties: [password, secret, token] # 强制使用间接引用
relations: has_owner: from_types: [Project, Task] to_types: [Person] cardinality: many_to_one blocks: from_types: [Task] to_types: [Task] acyclic: true # 禁止循环依赖
技能约定
使用本体的技能需在 SKILL.md 前置或头部声明:
ontology:
reads: [Task, Project, Person]
writes: [Task, Action]
preconditions:
- "Task.assignee must exist"
postconditions:
- "Created Task has status=open"
将计划建模为图转换
把多步骤计划视为一系列图操作:
Plan: "Schedule team meeting and create follow-up tasks"- CREATE Event { title: "Team Sync", attendees: [p_001, p_002] }
- RELATE Event -> has_project -> proj_001
- CREATE Task { title: "Prepare agenda", assignee: p_001 }
- RELATE Task -> for_event -> event_001
- CREATE Task { title: "Send summary", assignee: p_001, blockers: [task_001] }
每一步在执行前都会校验,若出现约束冲突则回滚。
集成模式
与 Causal Inference 结合
将本体变更记录为因果动作:
# 创建/更新实体时,同时写入因果动作日志
action = {
"action": "create_entity",
"domain": "ontology",
"context": {"type": "Task", "project": "proj_001"},
"outcome": "created"
}
跨技能通信
# Email skill 创建承诺对象 commitment = ontology.create("Commitment", { "source_message": msg_id, "description": "Send report by Friday", "due": "2026-01-31" })
# Task skill 获取并转化为任务 tasks = ontology.query("Commitment", {"status": "pending"}) for c in tasks: ontology.create("Task", { "title": c.description, "due": c.due, "source": c.id })
快速入门
# 初始化本体存储 mkdir -p memory/ontology touch memory/ontology/graph.jsonl# 可选创建 schema(推荐) python3 scripts/ontology.py schema-append --data '{ "types": { "Task": { "required": ["title", "status"] }, "Project": { "required": ["name"] }, "Person": { "required": ["name"] } } }'
# 开始使用 python3 scripts/ontology.py create --type Person --props '{"name":"Alice"}' python3 scripts/ontology.py list --type Person
参考资料
references/schema.md— 完整类型定义与约束模式references/queries.md— 查询语言与遍历示例
指令范围
运行时指令仅操作本地文件(memory/ontology/graph.jsonl 与 memory/ontology/schema.yaml),并提供 CLI 用于创建、查询、关联、校验;属于预期范围。技能会读取/写入工作区文件,并在首次使用时自动创建 memory/ontology 目录。校验包括属性/枚举/禁用字段检查、关系类型/基数校验、标记为 acyclic: true 的关系环路检测,以及 Event 的 end >= start 检查;其它更高层约束可能仅在文档中说明,除非在代码中实现。