POST /memory
按层写入一条记忆条目。
POST /cells/{cellID}/memory
认证
Authorization: Bearer $TOKEN
请求参数
请求体(JSON)
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
layer | string | ✅ | 目标层。可写层:about_me / agent_memory / consensus |
kind | string | ✅ | 记忆类别:preference / fact / constraint / instruction / correction / convention / lesson |
content | string | ✅ | 记忆内容文本 |
namespace | string | 否 | 命名空间。agent_memory 层必填,其他层可选 |
key | string | 否 | 去重键。相同 (layer, namespace, actor, key) 会覆盖已有条目 |
请求示例
保存一条个人偏好到 about_me 层:
curl -X POST "http://localhost:9091/cells/dept-legal/memory" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"layer": "about_me",
"kind": "preference",
"content": "我喜欢使用 Vim 键位的编辑器"
}'
保存一条角色记忆到 agent_memory 层(须指定 namespace):
curl -X POST "http://localhost:9091/cells/dept-legal/memory" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"layer": "agent_memory",
"kind": "fact",
"content": "项目使用 Go 1.22 + PostgreSQL 16",
"namespace": "agent-coder",
"key": "tech-stack"
}'
响应
成功(201 Created)
{
"id": "mem-abc123",
"layer": "about_me",
"kind": "preference"
}
响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 新建条目的唯一 ID |
layer | string | 写入的目标层 |
kind | string | 记忆类别 |
错误码
| 错误 | HTTP 状态码 | 说明 |
|---|---|---|
ErrLayerNotWritable | 400 | 目标层不可写。environment(宿主感知器产出)、session、working 是运行时态,不接受人工写入 |
ErrLayerShared | 400 | consensus 层是部门/Cell 级共识,不能携带 actor(个人归属) |
ErrLayerNamespaceRequired | 400 | agent_memory 层必须指定 namespace。空 namespace 会导致写入成功但永不被召回 |
ErrUnknownLayer | 400 | 不认识的层名。通常意味着客户端版本过期 |
ErrKindRequired | 400 | kind 字段为空。kind 是记忆系统多张表的索引键,不可省略 |
ErrContentQualityRejected | 400 | 内容未通过质量门控(拦截引擎状态前缀如 PLAN_STATE: 等) |
ErrThreatPatternDetected | 400 | 内容命中威胁模式扫描(凭据泄漏正则 + invisible unicode) |
注意事项
- 可写层仅三个:
about_me、agent_memory、consensus。其余层由引擎或运行时自动管理。 consensus层需要 admin 权限。普通用户写consensus会被拒绝。actor由服务端从 token 中提取,请求体中无需也不应传入。- 写入前引擎会执行威胁模式扫描(INV-MEM-24)和内容质量门控(INV-MEM-31),不通过则拒绝。
相关端点
- GET /memory — 列出记忆
- POST /memory/search — 语义搜索
- GET /memory/counts — 层计数
- DELETE /memory/{id} — 删除单条
- DELETE /memory/layer — 清空指定层