← 返回 Skills 市场
relunctance

Context Hawk

作者 Gao.QiLin · GitHub ↗ · v1.0.1 · MIT-0
cross-platform ⚠ suspicious
206
总下载
0
收藏
1
当前安装
8
版本数
在 OpenClaw 中安装
/install context-hawk
功能描述
Pure Python memory manager for preserving and retrieving multi-layered AI memories across sessions, topics, and time without external dependencies.
使用说明 (SKILL.md)

🦅 Context-Hawk — AI 记忆守护者

纯 Python 记忆包,零外部依赖,零 API Key,直接 import 就能用

让任何 Python 项目或 AI Agent 拥有记忆能力——跨会话、跨话题、跨时间。


定位

纯 Python 包,可独立使用

  • 不需要 OpenClaw,不需要 API Key
  • pip install 后直接 from hawk.memory import MemoryManager
  • 可嵌入任何 Python 项目(AI Agent、脚本、爬虫等)

配合 hawk-bridge 使用时,hawk-bridge 负责 autoCapture/autoRecall Hook,context-hawk 提供底层 Python 核心。


核心模块

模块 功能 API Key
MemoryManager 四层记忆衰减(Working/Short/Long/Archive)
ContextCompressor 上下文压缩,按 token 限制
Config 配置管理
SelfImproving 自我反思,错误模式学习
VectorRetriever 向量语义检索(LanceDB) ❌*
MarkdownImporter 导入 .md 记忆文件 ❌*
Extractor 记忆提取(6类分类) ⚠️ 可选
Governance 系统巡检指标

VectorRetriever 和 MarkdownImporter 需要 LanceDB(免费本地)


安装

pip install lancedb

不需要任何 API Key,纯本地运行。


快速开始

from hawk.memory import MemoryManager

# 创建记忆管理器
mm = MemoryManager()

# 存入记忆
mm.store("用户喜欢简洁的回复风格", category="preference")
mm.store("公司叫趣近团队", category="entity")
mm.store("老板决定用Go语言重构", category="decision")

# 检索记忆
results = mm.recall("老板的风格偏好")
for r in results:
    print(f"{r.layer}: {r.text}")

# 查看统计
print(mm.get_stats())
# {'working': 2, 'short': 1, 'long': 0, 'archive': 0, 'total': 3, 'avg_importance': 0.65}

MemoryManager — 四层记忆

Working → Short → Long → Archive
  ↑                    ↓
  ←──── Weibull 衰减 ←←←
from hawk.memory import MemoryManager

mm = MemoryManager()

# 存入,指定重要程度(0.0-1.0)
mm.store("这是重要决策", category="decision", importance=0.9)
mm.store("普通信息", category="fact", importance=0.4)

# 触发衰减(清理低价值记忆)
mm.decay()

# 访问记忆(更新计数器,可能触发层级升级)
item = mm.access("memory_id")
print(f"访问次数: {item.access_count}")

VectorRetriever — 向量语义检索

需要先设置 OPENAI_API_KEY(可选,用于 embedding)

from hawk.vector_retriever import VectorRetriever

retriever = VectorRetriever(top_k=5, min_score=0.6)

# 检索相关记忆
chunks = retriever.recall("老板之前部署过哪些服务")
print(retriever.format_for_context(chunks))

输出:

🦅 [记忆检索结果]
[entity] (85%相关): 趣近团队的服务器部署在阿里云
[fact] (72%相关): 生产环境用 Docker Compose 管理

MarkdownImporter — 导入 .md 记忆文件

一键把已有的 .md 记忆文件导入向量库:

from hawk.markdown_importer import MarkdownImporter

importer = MarkdownImporter(memory_dir="~/.openclaw/memory")

# 扫描预览
chunks = importer.scan()
print(f"发现 {len(chunks)} 个记忆块")

# 一键导入(增量,已导入的打标签跳过)
result = importer.import_all()
print(f"导入完成: {result['files']} 个文件, {result['chunks']} 个块")

CLI:

python3.12 -m hawk.markdown_importer --dry-run   # 预览
python3.12 -m hawk.markdown_importer             # 实际导入

ContextCompressor — 上下文压缩

按 token 限制压缩对话历史:

from hawk.compressor import ContextCompressor

compressor = ContextCompressor(max_tokens=4000)

conversation = [
    {"role": "user", "content": "我们要做XX项目"},
    {"role": "assistant", "content": "好的,我来规划"},
    # ... 100 轮对话
]

result = compressor.compress(conversation)
print(f"压缩率: {result['compression_ratio']}")
print(result['compressed'])

Extractor — 记忆提取

零 API 模式(关键词提取,完全离线):

from hawk.extractor import extract_memories

text = "用户说他喜欢用Go语言,公司叫趣近团队,决定用Docker部署"
memories = extract_memories(text, provider="keyword")
# 不需要任何 API Key

有 API Key 时(LLM 6类分类,更精准):

# 设置环境变量
import os
os.environ["OPENAI_API_KEY"] = "sk-xxx"
# 或
os.environ["MINIMAX_API_KEY"] = "sk-cp-xxx"

memories = extract_memories(text, provider="openai")
# 或
memories = extract_memories(text, provider="minimax",
                            api_key="sk-cp-xxx",
                            model="MiniMax-M2.7",
                            base_url="https://api.minimaxi.com/anthropic")

输出示例:

[
  {"text": "喜欢用Go语言", "category": "preference", "importance": 0.8, "abstract": "用户偏好Go", "overview": "[preference] 喜欢用Go语言"},
  {"text": "公司叫趣近团队", "category": "entity", "importance": 0.9, "abstract": "公司名称", "overview": "[entity] 公司叫趣近团队"},
  {"text": "决定用Docker部署", "category": "decision", "importance": 0.95, "abstract": "部署决策", "overview": "[decision] 决定用Docker部署"}
]

SelfImproving — 自我反思

from hawk.self_improving import SelfImproving

si = SelfImproving()

# 记录错误
si.learn_from_error(
    error_type="recall_miss",
    context={"query": "老板上次说的项目", "results": []},
    correction="改进了检索关键词策略"
)

# 获取统计
print(si.get_stats())
# {'total': 1, 'resolved': 0, 'unresolved': 1, 'by_type': {'recall_miss': 1}}

# 获取未解决的问题
unresolved = si.get_unresolved()
for u in unresolved:
    print(f"待解决: {u.error_type} - {u.context}")

Governance — 系统巡检

from hawk.governance import Governance

gov = Governance()

# 记录指标
gov.log_extraction(total=10, stored=3, skipped=7)
gov.log_noise_filter(filtered_count=2, total=10)
gov.log_recall(hits=2, total=5)

# 查看24小时统计
stats = gov.get_stats(hours=24)
print(stats)
# {'extractions': 10, 'noise_filtered': 2, 'recalls': 5, 'recall_hits': 2, ...}

HawkContext — autoCapture + autoRecall 包装器

HawkContext 是一个 Python 上下文管理器,包装 LLM 调用,自动实现:

  • autoRecall:对话开始时自动检索相关记忆并注入上下文
  • autoCapture:对话结束时自动提取记忆存入 LanceDB
from hawk import HawkContext

# 初始化(自动从环境变量读取配置)
hawk = HawkContext(
    provider="minimax",      # minimax | openai | groq | ollama | keyword
    api_key="sk-cp-xxx",
    model="MiniMax-M2.7"
)

# 对话开始时自动 recall
# 对话结束时(with 块退出时)自动 capture
with hawk:
    response = hawk.chat(
        "老板之前用什么技术栈?",
        system_prompt="你是一个助手"
    )
    print(response)

# 也可以手动控制
hawk.recall("用户的项目偏好")
hawk.capture()

自动 recall 机制:

  • hawk.recall()hawk.chat() 内部被调用
  • 检索当前用户消息相关的记忆
  • 结果自动插入 LLM 消息中

支持 provider:

provider API Key 说明
minimax 需要 MiniMax-M2.7(推荐)
openai 需要 GPT-4o 等
groq 不需要 免费 Llama-3
ollama 不需要 本地模型
keyword 不需要 纯规则提取

配置优先级:

  1. 显式传参 > 环境变量 > 默认值

与 hawk-bridge 的关系

context-hawk hawk-bridge
定位 纯 Python 包 OpenClaw Hook 插件
触发方式 手动调用 / 脚本 自动 Hook
API Key 可选(keyword模式不需要) 需要
用途 任何 Python 项目 OpenClaw 用户

推荐组合:

  • 独立 Python 项目 → 只装 context-hawk
  • OpenClaw 用户 → 装 hawk-bridge(内置 context-hawk 核心)

目录结构

context-hawk/
├── SKILL.md               ← 本文档
├── hawk/
│   ├── __init__.py
│   ├── memory.py           ← 四层记忆 + 衰减
│   ├── compressor.py       ← 上下文压缩
│   ├── config.py           ← 配置管理
│   ├── self_improving.py   ← 自我反思
│   ├── vector_retriever.py ← 向量检索
│   ├── markdown_importer.py ← .md 导入
│   ├── extractor.py        ← 记忆提取(keyword / LLM)
│   └── governance.py        ← 巡检指标
└── scripts/
    └── install.sh          ← 一键安装脚本
安全使用建议
This package contains real memory-management code but a few things don't add up and deserve manual review before use: - The skill advertises 'zero external dependencies' but optionally uses LanceDB and embedding providers (OpenAI/Minimax). Decide whether you will enable those features and only provide API keys when necessary. - The runtime docs and code read environment variables (OPENAI_API_KEY, MINIMAX_API_KEY, etc.) and use user-home paths (~/.openclaw/memory). The manifest did not declare these — expect the package to create/read files in your home directory. - The README suggests running a remote installer (curl | bash). Do NOT run remote installers you haven't audited; instead inspect install.sh in the package and run commands manually in a sandbox. - Review install.sh, scripts/, and any network-related code for outbound endpoints before installing. If you want to minimize exposure: disable autoCapture/autoRecall, run in offline providers (keyword/groq/ollama when possible), and restrict file permissions on the memory directory. - If you lack the ability to audit code, run the package in an isolated environment (VM/container) and monitor filesystem and network activity before granting it access to production data or API keys.
能力标签
cryptocan-make-purchases
能力评估
Purpose & Capability
The name/description advertise a 'pure Python' memory manager with 'zero external dependencies' and 'zero API Key'. The bundle actually includes code for vector retrieval, Markdown importing, and optional LanceDB/OpenAI/Minimax integrations. That is plausibly coherent if those features are optional, but the messaging is misleading: several features explicitly require external components or API keys (VectorRetriever, embeddings, lanceDB integration). The README also advertises a curl|bash installer which is outside a strict 'pure local Python only' promise.
Instruction Scope
SKILL.md instructs the agent to read and use environment variables (OPENAI_API_KEY, MINIMAX_API_KEY) and to operate on a user-scoped path (~/.openclaw/memory). It describes automatic capture/recall hooks (autoCapture/autoRecall) that persist conversational data and task_state to disk. The declared skill metadata listed no required env vars or config paths, so the runtime instructions access environment and file paths not reflected in the manifest — a scope mismatch and a privacy surface to note.
Install Mechanism
The registry entry lists no install spec, but the package contains install.sh and README(s) that suggest running a remote bootstrap (bash <(curl -fsSL https://raw.githubusercontent.com/relunctance/context-hawk/master/install.sh)). Remote curl|bash installation is high-risk. Even though SKILL.md itself only asks to 'pip install lancedb' for an optional component, the presence of install scripts and the README's one-liner is a red flag and should be treated cautiously.
Credentials
The manifest claims no required credentials, yet SKILL.md and code reference multiple provider API keys (OPENAI_API_KEY, MINIMAX_API_KEY) and provider configuration. That makes the declared environment requirements incomplete. The skill will also create/read files under ~/.openclaw/memory (and memory/.hawk/task_state.jsonl) which are not declared. Requesting or auto-reading general-purpose API keys without declaring them is a proportionality mismatch.
Persistence & Privilege
The skill persistently stores memory and task state (JSONL under memory/.hawk or ~/.openclaw/memory) and includes CLI/installer scripts. Persistence and writing to user home are expected for a memory manager, but users should be aware it will create and keep long-lived local state. The skill is not marked always:true, and autonomous invocation is platform-default; that autonomy combined with persistent storage increases blast radius and is worth user attention but is not, by itself, an indicator of maliciousness.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install context-hawk
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /context-hawk 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.1
- Added new module: hawk/normalize.py for memory normalization. - Introduced pyproject.toml for modern Python packaging/metadata. - Updated 10+ multilingual README files for improved documentation and clarity. - Enhanced core modules: memory, compressor, extractor, config, governance, and others with refinements. - Adjusted install.sh and other internal scripts for improved setup and compatibility.
v1.0.0
Context-Hawk v1.0.0 — Initial Release - Introduces a pure Python memory management package with no external dependencies or API keys required. - Provides four-level memory decay (Working, Short, Long, Archive) through MemoryManager. - Includes context compressor, vector retrieval (LanceDB support), markdown memory import, and memory extraction (offline & LLM-powered). - Adds self-improving error learning and system governance metrics modules. - Features HawkContext context manager for auto memory recall/capture with LLM providers. - Designed for easy integration into any Python project, with or without hawk-bridge.
v2.2.2
- Added new modules: `compression.py` and `similarity.py` in the `hawk` directory. - Introduced a shell install script (`install.sh`), a cron health check script, and a health check test. - Updated core files including `memory.py` and `vector_retriever.py` with improvements or fixes. - Documentation updated in both `README.md` and `README.zh-CN.md`.
v2.2.1
Add Jina API key setup guide; add openclaw.compat.pluginApi
v2.2.0
v2.2.0: add install.sh for auto symlink to ~/bin, update Quick Start docs
v2.1.0
v2.1.0: English-only, no Chinese, no Xiyou references, keep_recent=5, auto_check_rounds=10, hawk task/resume/introspect commands
v2.0.0
v2.0.0: 任务状态记忆(hawk resume)+中文增强版README+task_state.jsonl持久化+十大能力清单
v1.1.0
Initial public release: 4-tier memory, 5 compression strategies, 5 injection policies, self-introspection, LanceDB integration
元数据
Slug context-hawk
版本 1.0.1
许可证 MIT-0
累计安装 1
当前安装数 1
历史版本数 8
常见问题

Context Hawk 是什么?

Pure Python memory manager for preserving and retrieving multi-layered AI memories across sessions, topics, and time without external dependencies. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 206 次。

如何安装 Context Hawk?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install context-hawk」即可一键安装,无需额外配置。

Context Hawk 是免费的吗?

是的,Context Hawk 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Context Hawk 支持哪些平台?

Context Hawk 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Context Hawk?

由 Gao.QiLin(@relunctance)开发并维护,当前版本 v1.0.1。

💬 留言讨论