docs/design/openclaw-integration.md
本方案讨论在 OpenViking 中集成 OpenClaw Context Engine 的扩展机制,以及围绕新引擎的记忆管理、查询、注入等完整设计。
This proposal discusses the extension mechanism for integrating OpenClaw Context Engine into OpenViking, along with the complete design for memory management, retrieval, and injection around the new engine.
当 compact 时一次性把对话上传到 ov。
Upload conversation to OpenViking in one batch when compact is triggered.
可选项:compact 时,可以把一些工作记忆(比如 TODO,摘要),留在压缩后的上下文里面(如果有的话),避免断档。
Optional: During compact, keep some working memories (e.g., TODOs, summaries) in the compressed context (if available) to avoid discontinuity.
由于涉及 agent 记忆的提取,建议把 system prompt 和工具调用也一起上传。
Since agent memory extraction is involved, it is recommended to also upload the system prompt and tool calls together.
相比于之前模式(每条消息都写入),一次性的写入可以减少记忆提取阶段的 token 消耗,带来的缺点是跨 session 的记忆同步会变慢(不敏感)。
Compared to the previous mode (writing every message), one-time writing reduces token consumption during memory extraction. The downside is that cross-session memory synchronization will be slower (not sensitive).
compact 一般在 /new 或消息达到一定长度时触发,消息未达长度的部分不会触发记忆提取。这部分未来可以加入 timeout 触发提取的机制(当前可以不要)。
Compact is typically triggered by /new or when messages reach a certain length. Messages that don't reach the length threshold won't trigger memory extraction. A timeout-based extraction mechanism can be added in the future (not needed currently).
为了平衡记忆同步的实时性和用户体验,采用双阈值的非阻塞上报机制:
Threshold 1: Early Upload (e.g., 50% of context window) / 阈值 1:提前上报(如上下文窗口的 50%)
Threshold 2: Force Cleanup (e.g., 70% of context window) / 阈值 2:强制清理(如上下文窗口的 70%)
Cleanup After Upload Completion (Before Threshold 2) / 上报完成后的清理(阈值 2 前)
Compact Hook Integration / Compact Hook 集成
Benefits / 优势:
这允许 agent 通过 commit_memory 工具(或ov cli)主动记录记忆,支持用户请求如:
This allows the agent to actively record memories via a commit_memory tool (or ov cli), supporting user requests like:
commit_memory Tool / 工具:
memory_content, memory_type, priority, category记忆查询分为三部分:
Memory retrieval has three parts:
在会话开始时一次性注入到 system prompt。
Injected once at session start into the system prompt.
Profile Sources / 画像来源:
profile.md - User's main profile file (always included) / 用户主画像文件(总是包含)为每条用户消息召回,仅用于该次 LLM 调用。
Retrieved for each user message, only used for that single LLM call.
Query Construction / 查询构建: Use last N user messages (default: 5) concatenated as the search query / 使用最近 N 条用户消息(默认:5条)拼接作为搜索查询
Lightweight Intent Detection / 轻量级意图检测(TODO 可选模块,可基于轻量模型实现 / TODO optional module, can be implemented with lightweight models):
将自动召回的记忆作为模拟的 function call 结果注入,而不是直接注入到 prompt 中
Inject auto-retrieved memories as simulated function call results instead of directly injecting into the prompt. This is a continuation of Part 2.
Benefits / 好处:
Example Flow / 示例流程:
User: How do I optimize the database?
Assistant: [Function Call] search_memories({"query": "database optimization...", "max_results": 5})
System: [Function Result] {"success": true, "memories": [...]}
Assistant: Based on...
Retrieval Flow / 召回流程:
除了每轮自动注入之外,还提供 Agent 发起的查询机制,以满足更复杂的检索需求。这是一种"测试时计算"的方法,用于解决单轮回召可能遗漏的多跳和多背景检索问题。
In addition to per-turn auto-injection, provide an agent-initiated query mechanism to meet more complex retrieval needs. This is a "test-time compute" approach to solve multi-hop and multi-context retrieval problems that single-round retrieval may miss.
为了让主动记忆的路径尽可能短,可以默认把 ov ls viking:// 的结果预先注入到 system prompt 里面,让 agent 预先知道 ov 里有哪些数据可以用。如果能模拟成是 agent 主动调用的,效果可能更好。
To make the path to active memory as short as possible, pre-inject the results of ov ls viking:// into the system prompt by default, so the agent knows in advance what data is available in OpenViking. Effect may be better if simulated as an agent-initiated call.
Design / 设计:
ov ls viking:// (or equivalent) / 运行 ov ls viking://(或等效操作)Example / 示例:
Assistant: [Function Call] ov_ls({"path": "viking://"})
System: [Function Result] {
"directories": [
"viking://docs/",
"viking://user/memories/",
"viking://agent/skills/",
"viking://assets/"
]
}
| Scenario / 场景 | Example / 示例 |
|---|---|
| Multi-hop reasoning / 多跳推理 | "What did I say about project X last week, and how does that relate to the Y file we discussed?" |
| Need for comprehensive context / 需要全面上下文 | "Tell me everything I've said about database optimization" |
| Temporal queries / 时间查询 | "What decisions did we make in the last month about authentication?" |
| Cross-session retrieval / 跨会话检索 | "Find my previous conversation about API design patterns" |
| Directory semantic exploration / 目录语义探索 | "What's in the /docs folder that's relevant to my current task?" |
Skill 记忆是Agent记忆的一种,区别点在于他锚定一个确定性的Skill
因此可以通过拦截工具调用中的 read skills/xxx/SKILL.md 文件调用,在返回结果中添加 skill 记忆的方式注入。
Skill memories are a type of agent memory, with the key distinction that they are anchored to a specific skill.
Therefore, they can be injected by intercepting read skills/xxx/SKILL.md file calls in tool calls, and adding skill memories to the returned results.
Design / 设计:
Intercept skill file reads / 拦截 skill 文件读取
skills/<skill_name>/SKILL.md / 当 agent 读取 skills/<skill_name>/SKILL.md 时Augment skill content / 增强 skill 内容
Skill memory structure / Skill 记忆结构
Example / 示例:
Original SKILL.md:
## Create Presentation
Create a PowerPoint presentation...
Augmented with skill memory:
## Create Presentation (Used 15 times, 93% success)
Create a PowerPoint presentation...
---
## Past Examples
- Successfully created Q3 financial report (2024-03-01)
- Created project kickoff deck (2024-02-15)
## Tips
- User prefers dark theme templates
- Always include executive summary slide
- Use company logo from /assets/logo.png
## Known Issues
- Large images (>10MB) sometimes fail - compress first
工具记忆可以通过 system prompt 方式注入。
Tool memories can be injected via system prompt.
Design / 设计:
Inject into system prompt / 注入到 system prompt
Tool memory content / 工具记忆内容
Format / 格式
Example / 示例:
## Tool Usage Memories
### run_shell
- Called 42 times, 88% success rate
- Average time: 2.3s
- Common issues:
- Forgetting to use `cd` before relative paths
- Long-running commands need `--async` flag
- Best practice: Always use `&&` for chained commands
### edit_file
- Called 156 times, 95% success rate
- Best practice: Use `search_replace` instead of full rewrite when possible
本节讨论如何将 OpenViking 能力注入到 Agent 中。提出了两种方案,都避免了基于 skill 的注入模式。
This section discusses how to inject OpenViking capabilities into the agent. Two options are proposed, both avoiding the skill-based injection pattern.
直接将 OpenViking CLI 用法说明注入到 system prompt 中。Agent 使用内置的 bash 工具调用 ov 命令。
Inject OpenViking CLI usage instructions directly into the system prompt. The agent uses the built-in bash tool to call ov commands.
Advantages / 优势:
Requirements / 要求:
Example Commands / 示例命令:
ov search --query "your query" [--category <category>] [--limit N]
ov ls memories [--category <category>]
ov search-docs --query "your query" [--path <directory>]
ov history [--limit N]
ov remember --content "what to remember" [--type <type>] [--priority N]
将 OpenViking 能力定义为显式的工具定义(function calling)。
Define OpenViking capabilities as explicit tool definitions (function calling).
Advantages / 优势:
Disadvantages / 劣势:
| Aspect / 方面 | Option 1: System Prompt + Bash / 方案 1:System Prompt + Bash | Option 2: Tool Definition / 方案 2:工具定义 |
|---|---|---|
| Trigger Stability / 触发稳定性 | Depends on bash tool reliability / 取决于 bash 工具可靠性 | More predictable / 更可预测 |
| Flexibility / 灵活性 | High - can compose commands / 高 - 可以组合命令 | Lower - fixed schema / 较低 - 固定 schema |
| Maintenance / 维护成本 | Maintain CLI help text / 维护 CLI 帮助文本 | Maintain tool definitions / 维护工具定义 |
| LLM Friendliness / LLM 友好度 | Requires good CLI design / 需要好的 CLI 设计 | Explicit schema helps / 显式 schema 有帮助 |
| Bash Required / 需要 Bash | Yes / 是 | No / 否 |
Primary Recommendation: Option 1 (CLI + Bash) if CLI can be made LLM-friendly
主要建议:如果 CLI 能做到对 LLM 友好,选择方案 1(CLI + Bash)
Why / 为什么:
Fallback: Option 2 (Tool Definition) if CLI can't be simplified enough
备选方案:如果 CLI 无法足够简化,选择方案 2(工具定义)