@cosmicstack/mercury-agent 1.0.6 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  <p align="center">
2
- <img src="docs/card.png" alt="Mercury — Soul-Driven AI Agent" width="600">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="docs/card-dark.png">
4
+ <source media="(prefers-color-scheme: light)" srcset="docs/card-light.png">
5
+ <img alt="Mercury — Soul-Driven AI Agent" src="docs/card-light.png" width="600">
6
+ </picture>
3
7
  </p>
4
8
 
5
9
  <p align="center">
@@ -16,6 +20,10 @@
16
20
  <a href="https://nodejs.org/"><img src="https://img.shields.io/node/v/@cosmicstack/mercury-agent" alt="node"></a>
17
21
  </p>
18
22
 
23
+ <p align="center">
24
+ English | <a href="README.zh-CN.md">简体中文</a>
25
+ </p>
26
+
19
27
  ---
20
28
 
21
29
  ## Quick Start
@@ -258,6 +266,73 @@ MIT © [Cosmic Stack](https://github.com/cosmicstack-labs)
258
266
 
259
267
  ---
260
268
 
261
- ## Suggestions and Contributions
269
+ ## Contributing
270
+
271
+ We're open to contributions! Mercury is built to evolve, and we welcome help from the community. Whether it's fixing a bug, adding a tool, improving memory, or refining the soul — all quality contributions are appreciated.
272
+
273
+ ### 🎯 Agentic Expertise — Must-Have for Contributors
274
+
275
+ Mercury isn't just another open-source project — it's a **soul-driven agent** that runs 24/7, manages permissions, remembers context, and interacts across channels. If you're contributing, you must think like an agent builder, not just a library contributor. These are non-negotiable principles every contributor should internalize:
276
+
277
+ | Principle | What It Means |
278
+ |-----------|---------------|
279
+ | 🧠 **Think in loops** | Mercury operates in a 10-step agentic loop. Your tool or feature will be called multiple times per conversation. Make it idempotent where possible. |
280
+ | 🔐 **Permission-first** | Every action that touches the outside world (files, shell, network, git) must go through the permission system. Never assume approval. |
281
+ | 💾 **Memory-aware** | If your feature generates facts about the user, consider hooking into the Second Brain. If it reads user data, check memory first. |
282
+ | 📏 **Token-conscious** | Mercury has a daily token budget. Logging, verbose outputs, and large context dumps burn tokens fast. Keep it lean. |
283
+ | 🔌 **Channel-agnostic** | Tools should work identically on CLI and Telegram. Don't assume a terminal, a keyboard, or even a human on the other end. |
284
+ | 🔁 **Graceful degradation** | If a provider fails, a tool errors, or a file doesn't exist — Mercury should recover, not crash. Always handle edge cases. |
285
+ | 📋 **Self-documenting** | Your tool's name and description are what Mercury reads to decide when to use it. Make them clear, specific, and action-oriented. |
286
+ | 🧪 **Test the loop, not just the function** | A tool that works in isolation may fail in the agentic loop (e.g., returns too much data, blocks the next step). Test end-to-end. |
287
+
288
+ ### Code Quality — Dos
289
+
290
+ | Do | Why |
291
+ |----|-----|
292
+ | ✅ Write clean, readable TypeScript with explicit types | Mercury's codebase is type-safe — keep it that way |
293
+ | ✅ Add JSDoc comments on public functions and tools | Helps other contributors and the agent understand intent |
294
+ | ✅ Keep functions small and single-purpose | Easier to test, review, and reason about |
295
+ | ✅ Use async/await over raw promises | Consistent error handling and readability |
296
+ | ✅ Write tests for new tools and memory features | Reliability matters for a 24/7 agent |
297
+ | ✅ Follow the existing project structure (`src/tools/`, `src/memory/`, `src/channels/`) | Keeps the codebase navigable |
298
+ | ✅ Use the Agent Skills spec for new skill-based features | Ensures compatibility with the skills ecosystem |
299
+ | ✅ Document breaking changes in PR descriptions | Helps maintainers version properly |
300
+
301
+ ### Code Quality — Don'ts
302
+
303
+ | Don't | Why |
304
+ |-------|-----|
305
+ | ❌ Don't add dependencies without discussion | Mercury is lean — every dep adds surface area |
306
+ | ❌ Don't hardcode API keys, tokens, or paths | Use config/env vars like the rest of the codebase |
307
+ | ❌ Don't bypass the permission system | Tools must ask before acting — that's Mercury's core promise |
308
+ | ❌ Don't introduce sync/blocking I/O in hot paths | Mercury is async-first for a reason |
309
+ | ❌ Don't commit large binary files or secrets | Use `.gitignore` and env files |
310
+ | ❌ Don't change the soul/persona system without discussion | It's the heart of Mercury — changes need care |
311
+ | ❌ Don't submit untested Telegram or daemon changes | These are hard to debug post-merge |
312
+ | ❌ Don't ignore the token budget system | Every tool should be mindful of token consumption |
313
+
314
+ ### Getting Started
315
+
316
+ 1. Fork the repo
317
+ 2. Run `npm install`
318
+ 3. Make your changes
319
+ 4. Run `npm run build` to verify it compiles
320
+ 5. Test with `mercury` locally
321
+ 6. Open a PR with a clear description of what you changed and why
322
+
323
+ ### PR Guidelines
324
+
325
+ - Keep PRs focused — one feature/fix per PR
326
+ - Include before/after behavior in the description
327
+ - Tag related issues if applicable
328
+ - Be responsive to review feedback
329
+
330
+ ### Need Help?
331
+
332
+ Open an issue or reach out at [mercury@cosmicstack.org](mailto:mercury@cosmicstack.org). We're friendly.
333
+
334
+ ---
335
+
336
+ ## Community
262
337
 
263
- For suggestions, contributions, or any inquiries, please reach out to us at [mercury@cosmicstack.org](mailto:mercury@cosmicstack.org).
338
+ 1. **Discord** [Join the Mercury Agent Discord](https://discord.gg/5emMpMJy5J) for real-time chat, support, and community discussions.
@@ -0,0 +1,240 @@
1
+ # Mercury Agent 中文文档
2
+
3
+ > 一个以“灵魂”为中心的 AI Agent,内置权限加固工具、Token 预算、多渠道访问和 SQLite 支持的 Second Brain 记忆。
4
+
5
+ [English](README.md) | 简体中文
6
+
7
+ Mercury 会记住重要信息,在执行有风险的操作前先请求确认,并且可以通过 CLI 或 Telegram 以 24/7 后台进程运行。它适合需要本地文件操作、命令执行、长期记忆、定时任务和多模型兜底能力的个人 AI 助手场景。
8
+
9
+ ## 快速开始
10
+
11
+ 直接运行:
12
+
13
+ ```bash
14
+ npx @cosmicstack/mercury-agent
15
+ ```
16
+
17
+ 或全局安装:
18
+
19
+ ```bash
20
+ npm i -g @cosmicstack/mercury-agent
21
+ mercury
22
+ ```
23
+
24
+ 首次运行会启动配置向导。你需要输入姓名、模型 API Key,并可选择配置 Telegram Bot Token。之后如需重新配置:
25
+
26
+ ```bash
27
+ mercury doctor
28
+ ```
29
+
30
+ ## 为什么选择 Mercury
31
+
32
+ - **权限优先**:Shell 命令有阻止列表,文件读写受目录作用域限制,危险操作会进入待审批流程。
33
+ - **Second Brain 记忆**:基于 SQLite 和 FTS5 的结构化持久记忆,支持自动提取、相关召回、冲突处理和自动整理。
34
+ - **灵魂驱动**:人格由你拥有的 Markdown 文件定义,包括 `soul.md`、`persona.md`、`taste.md` 和 `heartbeat.md`。
35
+ - **Token 感知**:内置每日 Token 预算,超过阈值后自动简洁回复,并支持 `/budget` 查看、重置或临时覆盖。
36
+ - **实时流式输出**:CLI 支持实时 Token 流和 Markdown 重渲染,Telegram 支持可编辑状态消息。
37
+ - **持续运行**:可作为后台守护进程运行,崩溃后自动重启,并支持开机自启、定时任务和主动通知。
38
+ - **可扩展**:支持安装社区 Skill、调度 Skill 定时运行,并兼容 [Agent Skills](https://agentskills.io) 规范。
39
+
40
+ ## 守护进程模式
41
+
42
+ 推荐使用:
43
+
44
+ ```bash
45
+ mercury up
46
+ ```
47
+
48
+ 该命令会安装系统服务、启动后台守护进程,并确保 Mercury 正在运行。如果 Mercury 已经运行,它只会确认状态并显示 PID。
49
+
50
+ 常用命令:
51
+
52
+ ```bash
53
+ mercury restart # 重启后台进程
54
+ mercury stop # 停止后台进程
55
+ mercury start -d # 后台启动,不安装系统服务
56
+ mercury logs # 查看近期守护进程日志
57
+ mercury status # 查看运行状态
58
+ ```
59
+
60
+ 系统服务支持:
61
+
62
+ | 平台 | 方式 | 是否需要管理员权限 |
63
+ |------|------|--------------------|
64
+ | macOS | LaunchAgent (`~/Library/LaunchAgents/`) | 否 |
65
+ | Linux | systemd user unit (`~/.config/systemd/user/`) | 否,开机启动可能需要 linger |
66
+ | Windows | Task Scheduler (`schtasks`) | 否 |
67
+
68
+ ## CLI 命令
69
+
70
+ | 命令 | 说明 |
71
+ |------|------|
72
+ | `mercury up` | 推荐命令:安装服务、启动守护进程并确保运行 |
73
+ | `mercury` | 启动 Agent,等同于 `mercury start` |
74
+ | `mercury start` | 前台启动 |
75
+ | `mercury start -d` | 后台启动 |
76
+ | `mercury restart` | 重启后台进程 |
77
+ | `mercury stop` | 停止后台进程 |
78
+ | `mercury logs` | 查看近期日志 |
79
+ | `mercury doctor` | 重新配置,回车保留现有值 |
80
+ | `mercury setup` | 重新运行配置向导 |
81
+ | `mercury status` | 查看配置和守护进程状态 |
82
+ | `mercury help` | 查看完整手册 |
83
+ | `mercury upgrade` | 升级到最新版本 |
84
+ | `mercury telegram list` | 查看已批准和待处理的 Telegram 用户 |
85
+ | `mercury telegram approve <code\|id>` | 批准配对码或待处理请求 |
86
+ | `mercury telegram reject <id>` | 拒绝 Telegram 访问请求 |
87
+ | `mercury telegram remove <id>` | 移除已批准用户 |
88
+ | `mercury telegram promote <id>` | 将 Telegram 成员提升为管理员 |
89
+ | `mercury telegram demote <id>` | 将 Telegram 管理员降级为成员 |
90
+ | `mercury telegram reset` | 清空 Telegram 访问状态并重新开始 |
91
+ | `mercury service install` | 安装开机自启系统服务 |
92
+ | `mercury service uninstall` | 卸载系统服务 |
93
+ | `mercury service status` | 查看系统服务状态 |
94
+ | `mercury --verbose` | 使用调试日志启动 |
95
+
96
+ ## 对话内命令
97
+
98
+ 这些命令可在 CLI 或 Telegram 对话中输入,不消耗 API Token。
99
+
100
+ | 命令 | 说明 |
101
+ |------|------|
102
+ | `/help` | 查看完整手册 |
103
+ | `/status` | 查看 Agent 配置、预算和用量 |
104
+ | `/tools` | 列出已加载工具 |
105
+ | `/skills` | 列出已安装 Skill |
106
+ | `/stream` | 切换 Telegram 文本流式输出 |
107
+ | `/stream off` | 关闭流式输出,改为单条消息 |
108
+ | `/budget` | 查看 Token 预算状态 |
109
+ | `/budget override` | 为单次请求临时覆盖预算 |
110
+ | `/budget reset` | 将用量重置为零 |
111
+ | `/budget set <n>` | 修改每日 Token 预算 |
112
+ | `/permissions` | 修改权限模式 |
113
+ | `/tasks` | 列出定时任务 |
114
+ | `/memory` | 查看和管理 Second Brain 记忆 |
115
+ | `/unpair` | Telegram:重置所有访问 |
116
+
117
+ ## 内置工具
118
+
119
+ | 分类 | 工具 |
120
+ |------|------|
121
+ | 文件系统 | `read_file`, `write_file`, `create_file`, `edit_file`, `list_dir`, `delete_file`, `send_file`, `approve_scope` |
122
+ | Shell | `run_command`, `cd`, `approve_command` |
123
+ | 消息 | `send_message` |
124
+ | Git | `git_status`, `git_diff`, `git_log`, `git_add`, `git_commit`, `git_push` |
125
+ | Web | `fetch_url` |
126
+ | Skills | `install_skill`, `list_skills`, `use_skill` |
127
+ | 调度 | `schedule_task`, `list_scheduled_tasks`, `cancel_scheduled_task` |
128
+ | 系统 | `budget_status` |
129
+
130
+ ## 渠道
131
+
132
+ | 渠道 | 能力 |
133
+ |------|------|
134
+ | CLI | Readline 提示符、方向键命令菜单、实时文本流、Markdown 重渲染、权限模式选择 |
135
+ | Telegram | HTML 格式化、可编辑流式消息、文件上传、输入状态、多用户访问和管理员/成员角色 |
136
+
137
+ ### Telegram 访问模型
138
+
139
+ Mercury 使用组织式访问模型,包含管理员和成员。
140
+
141
+ - 首次设置:向你的 Bot 发送 `/start`,获取配对码,然后在 CLI 中执行 `mercury telegram approve <code>`。你会成为首位管理员。
142
+ - 新用户:发送 `/start` 请求访问,由管理员在 CLI 中批准或拒绝。
143
+ - 角色:管理员可以批准、拒绝、提升、降级和重置访问;成员可以与 Mercury 对话。
144
+ - 重置:管理员可在 Telegram 发送 `/unpair`,或在 CLI 中执行 `mercury telegram reset`。
145
+ - 仅支持私聊,群聊消息会被忽略。
146
+
147
+ ## 调度器
148
+
149
+ - **周期任务**:使用 cron 表达式,例如 `0 9 * * *` 表示每天 9 点。
150
+ - **一次性任务**:使用 `delay_seconds`,例如 15 秒后执行。
151
+ - 任务会持久化到 `~/.mercury/schedules.yaml`,重启后自动恢复。
152
+ - 执行结果会返回到创建任务时所在的渠道。
153
+
154
+ ## Second Brain
155
+
156
+ Mercury 默认启用结构化持久记忆,并会在对话后自动提取、存储和召回与你有关的重要事实。
157
+
158
+ - 10 种记忆类型:identity、preference、goal、project、habit、decision、constraint、relationship、episode、reflection。
159
+ - 自动提取:每轮对话后提取 0 到 3 条事实,并记录置信度、重要性和持久性。
160
+ - 相关召回:每次消息前注入最相关的前 5 条记忆,默认预算 900 字符。
161
+ - 自动整理:每 60 分钟生成个人资料摘要、活跃状态摘要和反思。
162
+ - 冲突处理:按置信度和时间新旧处理相互冲突的记忆。
163
+ - 自动修剪:活跃作用域记忆 21 天后过期,推断记忆会衰减,低置信持久记忆 120 天后撤销。
164
+ - 用户控制:通过 `/memory` 查看、搜索、暂停、恢复和清空。
165
+ - 禁用方式:设置 `SECOND_BRAIN_ENABLED=false`,或在配置中设置 `memory.secondBrain.enabled: false`。
166
+
167
+ 所有数据都保存在本机 `~/.mercury/memory/second-brain/second-brain.db`,不会上传到云端。
168
+
169
+ ## 配置位置
170
+
171
+ 运行时数据保存在 `~/.mercury/`,不会写入你的项目目录。
172
+
173
+ | 路径 | 用途 |
174
+ |------|------|
175
+ | `~/.mercury/mercury.yaml` | 主配置,包括提供商、渠道和预算 |
176
+ | `~/.mercury/.env` | API Key 和 Token |
177
+ | `~/.mercury/soul/*.md` | Agent 人格文件 |
178
+ | `~/.mercury/permissions.yaml` | 能力和审批规则 |
179
+ | `~/.mercury/skills/` | 已安装 Skill |
180
+ | `~/.mercury/schedules.yaml` | 定时任务 |
181
+ | `~/.mercury/token-usage.json` | 每日 Token 用量 |
182
+ | `~/.mercury/memory/short-term/` | 每段对话的短期记忆 JSON 文件 |
183
+ | `~/.mercury/memory/long-term/` | 自动提取事实,JSONL 格式 |
184
+ | `~/.mercury/memory/episodic/` | 带时间戳的事件日志,JSONL 格式 |
185
+ | `~/.mercury/memory/second-brain/` | 结构化记忆数据库 |
186
+ | `~/.mercury/daemon.pid` | 后台进程 PID |
187
+ | `~/.mercury/daemon.log` | 守护进程日志 |
188
+
189
+ ## 模型提供商兜底
190
+
191
+ Mercury 可以配置多个 LLM 提供商,并按顺序自动尝试。如果某个提供商失败,会切换到下一个。
192
+
193
+ | 提供商 | 默认模型 | API Key | 说明 |
194
+ |--------|----------|---------|------|
195
+ | DeepSeek | `deepseek-chat` | `DEEPSEEK_API_KEY` | 默认、成本较低 |
196
+ | OpenAI | `gpt-4o-mini` | `OPENAI_API_KEY` | 支持 GPT-4o、o3 等 |
197
+ | Anthropic | `claude-sonnet-4` | `ANTHROPIC_API_KEY` | Claude Sonnet、Haiku、Opus |
198
+ | Grok (xAI) | `grok-4` | `GROK_API_KEY` | OpenAI 兼容接口 |
199
+ | Ollama Cloud | `gpt-oss:120b` | `OLLAMA_CLOUD_API_KEY` | 远程 Ollama API |
200
+ | Ollama Local | `gpt-oss:20b` | 无需 Key | 本地 Ollama 实例 |
201
+
202
+ ## 架构
203
+
204
+ - TypeScript + Node.js 20+
205
+ - Vercel AI SDK v4,支持 `generateText`、`streamText` 和多步 Agent 循环
206
+ - grammY Telegram Bot
207
+ - SQLite + FTS5 Second Brain
208
+ - JSONL 短期、长期和情景记忆
209
+ - 后台守护进程、PID 文件和崩溃恢复
210
+ - macOS、Linux、Windows 系统服务
211
+
212
+ ## 参与贡献
213
+
214
+ 欢迎贡献修复、工具、记忆能力、渠道能力或文档改进。请保持 PR 聚焦,并在提交前运行:
215
+
216
+ ```bash
217
+ npm install
218
+ npm run build
219
+ ```
220
+
221
+ 贡献 Mercury 时请特别注意:
222
+
223
+ - 工具必须走权限系统,不能绕过审批。
224
+ - 面向 Agent 循环设计,尽量保持幂等。
225
+ - 避免冗长输出和过度日志,Token 预算是核心约束。
226
+ - CLI 和 Telegram 行为应尽量一致。
227
+ - 新增依赖、破坏性变更和 soul/persona 系统调整应先讨论。
228
+
229
+ ## 许可证
230
+
231
+ MIT © [Cosmic Stack](https://github.com/cosmicstack-labs)
232
+
233
+ ## 社区
234
+
235
+ - Discord:[加入 Mercury Agent Discord](https://discord.gg/5emMpMJy5J)
236
+ - 邮箱:[mercury@cosmicstack.org](mailto:mercury@cosmicstack.org)
237
+
238
+ ## 免责声明
239
+
240
+ 这是 AI 软件,可能出现错误。请自行评估风险后使用。