Package Exports
- opcode-pg-memory
- opcode-pg-memory/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (opcode-pg-memory) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
opcode-pg-memory
OpenCode 长期记忆插件 — PostgreSQL + pgvector 四层记忆架构,话题隔离 · Agent 自主调用 · OmO 多 Agent 协调
安装
# npm 安装(推荐)
bunx opcode-pg-memory install
# 或从 GitHub
git clone https://github.com/Vbs313/opcode-pg-memory.git && cd opcode-pg-memory
.\scripts\setup.ps1bunx install 会自动注册插件到 opencode.jsonc 并创建 /pg-memory-init 命令。
注册
在 ~/.config/opencode/opencode.jsonc 中确认 MCP 配置:
{
"mcp": {
"pg-memory": {
"type": "local",
"command": ["bun", "path/to/opcode-pg-memory/dist/mcp-server.js"],
"enabled": true,
"environment": {
"PG_HOST": "localhost", "PG_PORT": "5432",
"PG_DATABASE": "PGOMO", "PG_USER": "opencode",
"PG_PASSWORD": "你的密码",
"EMBEDDING_PROVIDER": "ollama",
"EMBEDDING_MODEL": "qwen3-embedding:0.6b",
"EMBEDDING_DIMENSIONS": "1024"
}
}
}
}验证:opencode --print-logs --log-level INFO | findstr "PG Memory" → 看到 Plugin initialized successfully
配置
三重优先级:MCP environment > ~/.config/opencode/pg-memory.jsonc > 默认值
// ~/.config/opencode/pg-memory.jsonc(可选)
{
"similarityThreshold": 0.7,
"maxMemories": 15,
"logLevel": "info",
"compactionThreshold": 0.85
}| Provider | 模型 | 维度 |
|---|---|---|
ollama |
qwen3-embedding:0.6b |
1024 |
deepseek |
text-embedding-v2 |
1536 |
openai |
text-embedding-3-small |
1536 |
MCP 工具
recall_memory
// Agent 自主调用
recall_memory({
"query": "数据库连接池配置",
"caller_context": { "type": "omo_agent", "current_goal": "性能调优" },
"scope": "task",
"aggregate_similar": true,
"retrieval_strategies": ["semantic", "graph"],
"filters": { "tier": "project" }
})| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
scope |
session / task / project |
session |
检索范围:当前会话 / 同任务所有会话 / 同项目所有会话 |
aggregate_similar |
boolean |
false |
合并连续同名工具调用(如 read ×47 最近读取了...),仅在查询层聚合,不丢失明细 |
hindsight_reflect
// 跨会话反思
hindsight_reflect({ "omo_task_id": "task_123", "aggregate": true })架构
session_map → topic_segments → entities / observations / relations / reflections
→ semantic_cache (HNSW 动态阈值)
→ token_usage_log项目结构
src/
├── index.ts # 插件入口(chat.message + event hook)
├── config.ts # 集中配置(文件+环境+默认值)
├── cli.ts # CLI(install 命令)
├── types.ts # 类型定义
├── db/init-db.ts # 数据库初始化 + 迁移
├── hooks/ # 生命周期钩子
├── mcp/ # MCP 工具实现
├── topic/ # 话题段管理器
├── services/ # 日志 · 关键词 · 隐私过滤
├── cache/ # 语义缓存
└── omo/ # OmO 适配器
mcp-server.ts # MCP 服务入口运维
同步对账
# 查看同步健康度
node scripts/verify-sync.js
# JSON 输出(用于自动化/监控)
node scripts/verify-sync.js --json健康状态标志:
| 指标 | 健康 | 告警 |
|---|---|---|
| 同步率 | ≥ 99.5% | < 99% |
| tool_status = 'failed' 占比 | < 5% | > 10% |
| 缺失会话数 | 0 | > 0 |
| 含 tool_call_id 比例 | 100% | < 99% |
| embedding 覆盖度 | ≥ 95% | < 95% |
首次部署后执行一次全量同步:
node scripts/verify-sync.js
# 确认同步率 >= 99.5%verify-sync.js 是只读脚本,不修改数据库。同步由 EventSynchronizer 自动通过 OpenCode 事件总线或 SQLite 轮询完成。
批量 embedding 回填
首次部署或新增 embedding 模型后,对历史观察生成向量:
# 查看待回填数量
node scripts/backfill-embeddings.js --dry-run
# 全量回填(按 created_at 顺序,支持断点续传)
node scripts/backfill-embeddings.js
# 限制处理条数(快速验证)
node scripts/backfill-embeddings.js --limit 100文档
| 文档 | 内容 |
|---|---|
| USAGE_GUIDE.md | 完整使用指南 — 配置 · 工具 · 运维 |
| PLUGIN_DOCUMENTATION.md | 架构详解 — 钩子系统 · 数据库设计 |
| AGENTS.template.md | AGENTS.md 模板 — OmO directory-agents-injector 自动通知子代理 |