关于 ontology
Ontology 是一种技能,可将知识建模为由实体和关系构成的类型化、可验证图。它适用于需要共享结构化状态的 Agent 和技能,并在应用变更前按声明的约束进行验证。
主要功能
- 01创建和查询类型化实体
- 02通过关系连接实体
- 03验证属性与关系约束
- 04遍历图并查询依赖项
- 05将图数据存储为 JSONL 文件
适用场景
- 01跟踪任务、项目和人员
- 02查询关系及项目依赖
- 03在技能间共享结构化状态
- 04将多步骤计划表示为图操作
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ hiveminderbot/ ontology --skill ontology安装后在 Claude Code 中输入/ontology确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
PATTERN_A.md_meta.jsonREADME.mdpyproject.toml.clawhub/origin.jsondocs/README.mddocs/examples/project-management.mddocs/reference/cli.mdreferences/queries.mdschema.mdscripts/__init__.pyontology.pysrc/__init__.pycli.pysrc/services/entity_service.pyrelation_service.pyvalidation_service.pyschema_service.pysrc/utils/id_utils.pygraph_loader.pypath_utils.pytests/integration/test_workflows.pytests/unit/test_id_utils.pytest_entity_service.pytest_graph_loader.pytest_path_utils.pytest_validation_service.pytest_schema_service.pytest_relation_service.py详细介绍
本体
用于将知识表示为可验证图的类型化词汇与约束系统。
架构
此技能遵循 模式 A:完全模块化 架构:
ontology/
├── src/ # Modular source code
│ ├── __init__.py # Package exports
│ ├── cli.py # CLI entry point
│ ├── services/ # Business logic
│ │ ├── entity_service.py
│ │ ├── relation_service.py
│ │ ├── validation_service.py
│ │ └── schema_service.py
│ └── utils/ # Utilities
│ ├── path_utils.py
│ ├── id_utils.py
│ └── graph_loader.py
├── tests/ # Comprehensive tests (60+)
│ ├── unit/
│ └── integration/
├── docs/ # Documentation
├── scripts/ # Legacy CLI (deprecated)
└── SKILL.md # This file
核心概念
一切都是一个实体,具有类型、属性以及与其他实体的关系。每次变更在提交前都会根据类型约束进行验证。
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。
仅追加规则
处理现有本体数据或架构时,应追加/合并变更,而不是覆盖文件。这样可以保留历史记录,避免覆盖先前的定义。
工作流
使用模块化 API
from src.services.entity_service import create_entity, query_entities
from src.services.relation_service import create_relation
from src.services.validation_service import validate_graph
# Create entities
alice = create_entity("Person", {"name": "Alice"}, "memory/ontology/graph.jsonl")
project = create_entity("Project", {"name": "Website"}, "memory/ontology/graph.jsonl")
# Create relation
create_relation(alice["id"], "owns", project["id"], {}, "memory/ontology/graph.jsonl")
# Validate
errors = validate_graph("memory/ontology/graph.jsonl", "memory/ontology/schema.yaml")
使用 CLI
# Create entity
python3 -m src.cli create --type Person --props '{"name":"Alice"}'
# Query
python3 -m src.cli query --type Task --where '{"status":"open"}'
python3 -m src.cli get --id task_001
python3 -m src.cli related --id proj_001 --rel has_task
# Link entities
python3 -m src.cli relate --from proj_001 --rel has_task --to task_001
# Validate
python3 -m src.cli validate
旧版 CLI(已弃用)
python3 scripts/ontology.py create --type Person --props '{"name":"Alice"}'
约束
在 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] # Force indirection
relations:
has_owner:
from_types: [Project, Task]
to_types: [Person]
cardinality: many_to_one
blocks:
from_types: [Task]
to_types: [Task]
acyclic: true # No circular dependencies
技能契约
使用本体的技能应声明:
# In SKILL.md frontmatter or header
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"
1. CREATE Event { title: "Team Sync", attendees: [p_001, p_002] }
2. RELATE Event -> has_project -> proj_001
3. CREATE Task { title: "Prepare agenda", assignee: p_001 }
4. RELATE Task -> for_event -> event_001
5. CREATE Task { title: "Send summary", assignee: p_001, blockers: [task_001] }
每个步骤都会在执行前进行验证。若违反约束,则回滚。
集成模式
与因果推理结合
将本体变更记录为因果动作:
# When creating/updating entities, also log to causal action log
action = {
"action": "create_entity",
"domain": "ontology",
"context": {"type": "Task", "project": "proj_001"},
"outcome": "created"
}
跨技能通信
# Email skill creates commitment
commitment = ontology.create("Commitment", {
"source_message": msg_id,
"description": "Send report by Friday",
"due": "2026-01-31"
})
# Task skill picks it up
tasks = ontology.query("Commitment", {"status": "pending"})
for c in tasks:
ontology.create("Task", {
"title": c.description,
"due": c.due,
"source": c.id
})
快速入门
# Initialize ontology storage
mkdir -p memory/ontology
touch memory/ontology/graph.jsonl
# Run tests
cd skills/ontology
python3 -m pytest tests/ -v
# Create schema (optional but recommended)
python3 -m src.cli schema-append --data '{
"types": {
"Task": { "required": ["title", "status"] },
"Project": { "required": ["name"] },
"Person": { "required": ["name"] }
}
}'
# Start using
python3 -m src.cli create --type Person --props '{"name":"Alice"}'
python3 -m src.cli 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 检查;除非在代码中实现,其他更高层级的约束可能仍仅作为文档说明。
评论
支持 Markdown