mercury-agent 0.4.5
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/LICENSE +22 -0
- package/README.md +438 -0
- package/container/Dockerfile +127 -0
- package/container/Dockerfile.base +109 -0
- package/container/Dockerfile.power +17 -0
- package/container/agent-package.json +8 -0
- package/container/build.sh +54 -0
- package/docs/TODOS.md +147 -0
- package/docs/auth/dashboard.md +28 -0
- package/docs/auth/overview.md +109 -0
- package/docs/auth/whatsapp.md +173 -0
- package/docs/configuration.md +54 -0
- package/docs/container-lifecycle.md +349 -0
- package/docs/context-architecture.md +87 -0
- package/docs/deployment.md +199 -0
- package/docs/extensions.md +375 -0
- package/docs/graceful-shutdown.md +62 -0
- package/docs/kb-distillation.md +77 -0
- package/docs/media/overview.md +140 -0
- package/docs/media/whatsapp.md +171 -0
- package/docs/memory.md +137 -0
- package/docs/permissions.md +217 -0
- package/docs/pipeline.md +228 -0
- package/docs/prd-chat-memory.md +76 -0
- package/docs/prd-config-load.md +82 -0
- package/docs/rate-limiting.md +166 -0
- package/docs/scheduler.md +288 -0
- package/docs/setup-discord.md +100 -0
- package/docs/setup-slack.md +119 -0
- package/docs/setup-whatsapp.md +94 -0
- package/docs/subagents.md +166 -0
- package/docs/web-search.md +62 -0
- package/examples/extensions/README.md +12 -0
- package/examples/extensions/charts/index.ts +13 -0
- package/examples/extensions/charts/skill/SKILL.md +98 -0
- package/examples/extensions/gws/README.md +52 -0
- package/examples/extensions/gws/index.ts +106 -0
- package/examples/extensions/gws/skill/SKILL.md +57 -0
- package/examples/extensions/gws/skill/references/calendar.md +101 -0
- package/examples/extensions/gws/skill/references/docs.md +65 -0
- package/examples/extensions/gws/skill/references/drive.md +79 -0
- package/examples/extensions/gws/skill/references/gmail.md +85 -0
- package/examples/extensions/gws/skill/references/sheets.md +60 -0
- package/examples/extensions/napkin/index.ts +821 -0
- package/examples/extensions/napkin/prompts/consolidation-monthly.md +73 -0
- package/examples/extensions/napkin/prompts/consolidation-weekly.md +67 -0
- package/examples/extensions/napkin/prompts/kb-distillation.md +176 -0
- package/examples/extensions/napkin/skill/SKILL.md +728 -0
- package/examples/extensions/pdf/index.ts +23 -0
- package/examples/extensions/pdf/skill/LICENSE.txt +30 -0
- package/examples/extensions/pdf/skill/SKILL.md +314 -0
- package/examples/extensions/pdf/skill/forms.md +294 -0
- package/examples/extensions/pdf/skill/reference.md +612 -0
- package/examples/extensions/pdf/skill/scripts/check_bounding_boxes.py +65 -0
- package/examples/extensions/pdf/skill/scripts/check_fillable_fields.py +11 -0
- package/examples/extensions/pdf/skill/scripts/convert_pdf_to_images.py +33 -0
- package/examples/extensions/pdf/skill/scripts/create_validation_image.py +37 -0
- package/examples/extensions/pdf/skill/scripts/extract_form_field_info.py +122 -0
- package/examples/extensions/pdf/skill/scripts/extract_form_structure.py +115 -0
- package/examples/extensions/pdf/skill/scripts/fill_fillable_fields.py +98 -0
- package/examples/extensions/pdf/skill/scripts/fill_pdf_form_with_annotations.py +107 -0
- package/examples/extensions/permission-guard/index.ts +65 -0
- package/examples/extensions/pinchtab/index.ts +199 -0
- package/examples/extensions/pinchtab/lib/session-injector.ts +144 -0
- package/examples/extensions/pinchtab/skill/SKILL.md +224 -0
- package/examples/extensions/pinchtab/skill/TRUST.md +69 -0
- package/examples/extensions/pinchtab/skill/references/api.md +297 -0
- package/examples/extensions/pinchtab/skill/references/env.md +45 -0
- package/examples/extensions/pinchtab/skill/references/profiles.md +107 -0
- package/examples/extensions/tradestation/host/refresh.ts +102 -0
- package/examples/extensions/tradestation/index.ts +153 -0
- package/examples/extensions/tradestation/skill/SKILL.md +67 -0
- package/examples/extensions/tradestation/skill/scripts/ts-cli.ts +111 -0
- package/examples/extensions/voice-synth/index.ts +94 -0
- package/examples/extensions/voice-synth/skill/SKILL.md +38 -0
- package/examples/extensions/voice-transcribe/index.ts +381 -0
- package/examples/extensions/voice-transcribe/requirements.txt +8 -0
- package/examples/extensions/voice-transcribe/scripts/transcribe.py +179 -0
- package/examples/extensions/voice-transcribe/skill/SKILL.md +53 -0
- package/examples/extensions/web-search/index.ts +22 -0
- package/examples/extensions/web-search/skill/SKILL.md +114 -0
- package/examples/extensions/web-search/skill/references/apartments.md +178 -0
- package/examples/extensions/web-search/skill/references/car-purchase.md +132 -0
- package/examples/extensions/web-search/skill/references/car-rental.md +113 -0
- package/examples/extensions/web-search/skill/references/flights.md +133 -0
- package/examples/extensions/web-search/skill/references/hotels.md +148 -0
- package/examples/extensions/yahoo-mail/cli/bun.lock +66 -0
- package/examples/extensions/yahoo-mail/cli/package.json +13 -0
- package/examples/extensions/yahoo-mail/cli/ymail.mjs +353 -0
- package/examples/extensions/yahoo-mail/index.ts +57 -0
- package/examples/extensions/yahoo-mail/skill/SKILL.md +78 -0
- package/package.json +106 -0
- package/resources/agents/explore.md +50 -0
- package/resources/agents/worker.md +24 -0
- package/resources/builtin-extensions.txt +3 -0
- package/resources/connection-env-vars.json +25 -0
- package/resources/extensions/.gitkeep +0 -0
- package/resources/pi-extensions/subagent/agents.ts +126 -0
- package/resources/pi-extensions/subagent/index.ts +964 -0
- package/resources/profiles/coding/AGENTS.md +43 -0
- package/resources/profiles/coding/mercury-profile.yaml +15 -0
- package/resources/profiles/general/AGENTS.md +31 -0
- package/resources/profiles/general/mercury-profile.yaml +15 -0
- package/resources/profiles/research/AGENTS.md +40 -0
- package/resources/profiles/research/mercury-profile.yaml +15 -0
- package/resources/skills/config/SKILL.md +25 -0
- package/resources/skills/context/SKILL.md +33 -0
- package/resources/skills/conversation-recap/SKILL.md +19 -0
- package/resources/skills/media/SKILL.md +27 -0
- package/resources/skills/mutes/SKILL.md +31 -0
- package/resources/skills/permissions/SKILL.md +19 -0
- package/resources/skills/preferences/SKILL.md +31 -0
- package/resources/skills/recall/SKILL.md +24 -0
- package/resources/skills/roles/SKILL.md +18 -0
- package/resources/skills/spaces/SKILL.md +18 -0
- package/resources/skills/tasks/SKILL.md +45 -0
- package/resources/templates/AGENTS.md +157 -0
- package/resources/templates/env.template +34 -0
- package/resources/templates/mercury.example.yaml +75 -0
- package/src/adapters/discord-native.ts +534 -0
- package/src/adapters/discord.ts +38 -0
- package/src/adapters/setup.ts +89 -0
- package/src/adapters/slack.ts +9 -0
- package/src/adapters/whatsapp-media.ts +337 -0
- package/src/adapters/whatsapp.ts +629 -0
- package/src/agent/api-socket.ts +127 -0
- package/src/agent/container-entry.ts +967 -0
- package/src/agent/container-error.ts +49 -0
- package/src/agent/container-runner.ts +1272 -0
- package/src/agent/model-capabilities-core.ts +23 -0
- package/src/agent/model-capabilities.ts +231 -0
- package/src/agent/pi-failure-class.ts +83 -0
- package/src/agent/pi-jsonl-parser.ts +306 -0
- package/src/agent/preferences-prompt.ts +20 -0
- package/src/agent/user-error-messages.ts +78 -0
- package/src/bridges/discord.ts +171 -0
- package/src/bridges/slack.ts +177 -0
- package/src/bridges/teams.ts +160 -0
- package/src/bridges/telegram.ts +571 -0
- package/src/bridges/whatsapp.ts +290 -0
- package/src/chat-shim.ts +259 -0
- package/src/cli/mercury.ts +2508 -0
- package/src/cli/mrctl-http.ts +27 -0
- package/src/cli/mrctl.ts +611 -0
- package/src/cli/whatsapp-auth.ts +260 -0
- package/src/config-file.ts +397 -0
- package/src/config-model-chain.ts +30 -0
- package/src/config.ts +316 -0
- package/src/core/api-types.ts +58 -0
- package/src/core/api.ts +105 -0
- package/src/core/commands.ts +76 -0
- package/src/core/conversation.ts +47 -0
- package/src/core/handler.ts +206 -0
- package/src/core/media.ts +200 -0
- package/src/core/mute-duration.ts +22 -0
- package/src/core/outbox.ts +76 -0
- package/src/core/permissions.ts +192 -0
- package/src/core/profiles.ts +245 -0
- package/src/core/rate-limiter.ts +127 -0
- package/src/core/router.ts +191 -0
- package/src/core/routes/chat.ts +172 -0
- package/src/core/routes/config-builtin.ts +107 -0
- package/src/core/routes/config.ts +81 -0
- package/src/core/routes/connections.ts +190 -0
- package/src/core/routes/console.ts +668 -0
- package/src/core/routes/control.ts +46 -0
- package/src/core/routes/conversations.ts +66 -0
- package/src/core/routes/dashboard.ts +2491 -0
- package/src/core/routes/extensions.ts +37 -0
- package/src/core/routes/index.ts +14 -0
- package/src/core/routes/media.ts +72 -0
- package/src/core/routes/messages.ts +37 -0
- package/src/core/routes/mutes.ts +89 -0
- package/src/core/routes/prefs.ts +95 -0
- package/src/core/routes/roles.ts +125 -0
- package/src/core/routes/spaces.ts +60 -0
- package/src/core/routes/storage.ts +126 -0
- package/src/core/routes/tasks.ts +189 -0
- package/src/core/routes/tradestation.ts +268 -0
- package/src/core/routes/tts.ts +51 -0
- package/src/core/runtime.ts +1140 -0
- package/src/core/space-queue.ts +103 -0
- package/src/core/storage-cleanup.ts +140 -0
- package/src/core/storage-guard.ts +24 -0
- package/src/core/task-scheduler.ts +132 -0
- package/src/core/telegram-format.ts +178 -0
- package/src/core/trigger.ts +142 -0
- package/src/dashboard/index.html +729 -0
- package/src/dashboard/tokens.css +53 -0
- package/src/extensions/api.ts +252 -0
- package/src/extensions/catalog.ts +117 -0
- package/src/extensions/config-registry.ts +83 -0
- package/src/extensions/context.ts +36 -0
- package/src/extensions/hooks.ts +156 -0
- package/src/extensions/image-builder.ts +617 -0
- package/src/extensions/installer.ts +306 -0
- package/src/extensions/jobs.ts +122 -0
- package/src/extensions/loader.ts +271 -0
- package/src/extensions/permission-guard.ts +52 -0
- package/src/extensions/reserved.ts +28 -0
- package/src/extensions/skills.ts +123 -0
- package/src/extensions/types.ts +462 -0
- package/src/logger.ts +174 -0
- package/src/main.ts +586 -0
- package/src/server.ts +391 -0
- package/src/storage/db.ts +1624 -0
- package/src/storage/memory.ts +45 -0
- package/src/storage/pi-auth.ts +95 -0
- package/src/text/markdown.ts +117 -0
- package/src/text/rtl.ts +38 -0
- package/src/tradestation/host-api.ts +77 -0
- package/src/tradestation/pending-orders.ts +69 -0
- package/src/tts/azure.ts +52 -0
- package/src/tts/google.ts +128 -0
- package/src/tts/index.ts +8 -0
- package/src/tts/language.ts +20 -0
- package/src/tts/synthesize.ts +133 -0
- package/src/types.ts +295 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
# Extensions
|
|
2
|
+
|
|
3
|
+
Mercury's extension system lets you add CLIs, skills, background jobs, lifecycle hooks, config keys, and dashboard widgets — all in TypeScript.
|
|
4
|
+
|
|
5
|
+
Extensions **cannot register HTTP routes** on the host API today. Features that need authenticated host endpoints (for example TradeStation **order placement** with a two-step confirm flow) are implemented as routes under `src/core/routes/` and gated with the same permission name as the extension (`tradestation`). A future `mercury.route()` API could move such handlers next to the extension source.
|
|
6
|
+
|
|
7
|
+
## Structure
|
|
8
|
+
|
|
9
|
+
Extensions live in `.mercury/extensions/*/`. Each directory is an extension:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
.mercury/extensions/
|
|
13
|
+
├── napkin/
|
|
14
|
+
│ ├── index.ts # Required — setup function
|
|
15
|
+
│ ├── skill/SKILL.md # Optional — agent skill
|
|
16
|
+
│ └── package.json # Optional — dependencies
|
|
17
|
+
└── gws/
|
|
18
|
+
└── index.ts
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The extension **name** is the directory name.
|
|
22
|
+
|
|
23
|
+
## Setup Function
|
|
24
|
+
|
|
25
|
+
Every extension exports a default function that receives the `MercuryExtensionAPI`:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import type { MercuryExtensionAPI } from "mercury-agent";
|
|
29
|
+
|
|
30
|
+
export default function(mercury: MercuryExtensionAPI) {
|
|
31
|
+
// Declare what this extension provides
|
|
32
|
+
mercury.cli({ name: "napkin", install: "bun add -g napkin-ai" });
|
|
33
|
+
mercury.permission({ defaultRoles: ["admin", "member"] });
|
|
34
|
+
mercury.skill("./skill");
|
|
35
|
+
|
|
36
|
+
mercury.on("workspace_init", async ({ workspace, containerWorkspace }) => {
|
|
37
|
+
mkdirSync(join(workspace, "entities"), { recursive: true });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
mercury.job("cleanup", {
|
|
41
|
+
interval: 3600_000,
|
|
42
|
+
run: async (ctx) => { /* ... */ },
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
mercury.config("enabled", {
|
|
46
|
+
description: "Enable napkin for this space",
|
|
47
|
+
default: "true",
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
mercury.widget({
|
|
51
|
+
label: "Napkin Status",
|
|
52
|
+
render: (ctx) => `<p>Last run: ${mercury.store.get("last-run") ?? "never"}</p>`,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
All declarations are optional — use only what you need.
|
|
58
|
+
|
|
59
|
+
## API Reference
|
|
60
|
+
|
|
61
|
+
### `mercury.cli(opts)`
|
|
62
|
+
|
|
63
|
+
Declare a CLI tool to install in the container image.
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
mercury.cli({ name: "napkin", install: "bun add -g napkin-ai" });
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
- `name` — binary name (should match extension name)
|
|
70
|
+
- `install` — shell command run as a Dockerfile `RUN` step
|
|
71
|
+
|
|
72
|
+
Mercury auto-generates a derived Docker image with all extension CLIs installed. The agent calls them directly in bash. Permission enforcement is handled by a built-in pi extension that blocks denied CLIs based on the caller's role.
|
|
73
|
+
|
|
74
|
+
Can be called multiple times for extensions that need several tools (e.g., media needs ffmpeg, imagemagick, and yt-dlp).
|
|
75
|
+
|
|
76
|
+
### `mercury.permission(opts)`
|
|
77
|
+
|
|
78
|
+
Register this extension's permission and set default roles.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
mercury.permission({ defaultRoles: ["admin", "member"] });
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- Permission name = extension name (e.g., `napkin`)
|
|
85
|
+
- `defaultRoles` — roles that get this permission by default
|
|
86
|
+
- `admin` always gets all permissions automatically
|
|
87
|
+
- Per-space overrides in `space_config` take precedence
|
|
88
|
+
|
|
89
|
+
Can only be called once per extension.
|
|
90
|
+
|
|
91
|
+
### `mercury.env(def)`
|
|
92
|
+
|
|
93
|
+
Declare an environment variable this extension needs. Only injected into containers when the caller has permission for this extension. Claimed vars are excluded from the blind `MERCURY_*` passthrough, preventing credential leakage to unprivileged callers.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
mercury.env({ from: "MERCURY_GH_TOKEN" }); // injected as GH_TOKEN
|
|
97
|
+
mercury.env({ from: "MERCURY_GH_TOKEN", as: "GITHUB_TOKEN" }); // custom container name
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
- `from` — env var name as set in `.env` (e.g. `MERCURY_GH_TOKEN`)
|
|
101
|
+
- `as` — (optional) name inside the container. Defaults to `from` with `MERCURY_` prefix stripped
|
|
102
|
+
|
|
103
|
+
Can be called multiple times for multiple env vars.
|
|
104
|
+
|
|
105
|
+
### `mercury.skill(relativePath)`
|
|
106
|
+
|
|
107
|
+
Register a skill directory for agent discovery.
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
mercury.skill("./skill");
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The directory must contain a `SKILL.md` file in pi's [skill format](https://agentskills.io/specification). Mercury copies the entire skill directory into `.mercury/global/skills/<name>/`, which is mounted into containers at `/home/node/.pi/agent/skills/<name>/`. Pi discovers it automatically.
|
|
114
|
+
|
|
115
|
+
Skills can contain multiple files — scripts, references, assets — not just SKILL.md. The agent uses relative paths from SKILL.md to access them.
|
|
116
|
+
|
|
117
|
+
### `mercury.requires(capabilities)`
|
|
118
|
+
|
|
119
|
+
Declare that this extension's skill (or CLI workflows) needs certain model capabilities (`tools`, `vision`, etc.). If **no** leg in `MERCURY_MODEL_CHAIN` satisfies **all** listed flags, the extension skill is not copied into the global skills directory and Mercury logs a startup warning.
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
mercury.requires(["tools"]); // e.g. PDF / scripting extensions
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Capability keys: `tools`, `vision`, `audio_input`, `audio_output`, `extended_thinking`.
|
|
126
|
+
|
|
127
|
+
Host configuration:
|
|
128
|
+
|
|
129
|
+
- Built-in prefix map for common model IDs
|
|
130
|
+
- Optional `.mercury/model-capabilities.yaml` for per-model overrides
|
|
131
|
+
- Optional `MERCURY_MODEL_CAPABILITIES` JSON env var (applies to every leg)
|
|
132
|
+
|
|
133
|
+
### `mercury.on(event, handler)`
|
|
134
|
+
|
|
135
|
+
Subscribe to lifecycle events.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
mercury.on("workspace_init", async (event, ctx) => {
|
|
139
|
+
// event.workspace — absolute host path
|
|
140
|
+
// event.containerWorkspace — container-relative path (e.g. /spaces/main)
|
|
141
|
+
mkdirSync(join(event.workspace, "my-dir"), { recursive: true });
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### Events
|
|
146
|
+
|
|
147
|
+
| Event | When | Can mutate? |
|
|
148
|
+
|-------|------|-------------|
|
|
149
|
+
| `startup` | After extensions loaded, runtime ready | No |
|
|
150
|
+
| `shutdown` | Mercury shutting down | No |
|
|
151
|
+
| `workspace_init` | Space workspace created/ensured | No |
|
|
152
|
+
| `before_container` | About to spawn container | Yes |
|
|
153
|
+
| `after_container` | Container finished | Yes |
|
|
154
|
+
|
|
155
|
+
Both `workspace_init` and `before_container` events include:
|
|
156
|
+
- `workspace` — absolute host path (for file operations on the host)
|
|
157
|
+
- `containerWorkspace` — container-relative path, e.g. `/spaces/main` (for env vars passed into the container)
|
|
158
|
+
|
|
159
|
+
`before_container` also includes:
|
|
160
|
+
- `attachments` — incoming `MessageAttachment[]` (e.g. voice, images), if any
|
|
161
|
+
|
|
162
|
+
#### `before_container` mutations
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
mercury.on("before_container", async (event, ctx) => {
|
|
166
|
+
return {
|
|
167
|
+
systemPrompt: "Extra instructions...", // appended to system prompt
|
|
168
|
+
promptAppend: "Transcript or extra user text...", // appended to user prompt (newline-joined across handlers)
|
|
169
|
+
env: { MY_VAR: event.containerWorkspace + "/data" }, // container-relative paths
|
|
170
|
+
block: { reason: "Rate limited" }, // prevent container from running
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The user message stored in the DB and passed to the container uses the original prompt plus any `promptAppend` from hooks (after `before_container` runs).
|
|
176
|
+
|
|
177
|
+
#### Extension context helpers
|
|
178
|
+
|
|
179
|
+
`ctx.hasCallerPermission(spaceId, callerId, permission)` returns whether the caller has a built-in or extension-registered permission in that space. Use it in hooks to mirror container RBAC (e.g. only transcribe voice for members who have your extension permission).
|
|
180
|
+
|
|
181
|
+
#### `after_container` mutations
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
mercury.on("after_container", async (event, ctx) => {
|
|
185
|
+
return {
|
|
186
|
+
reply: event.reply + "\n\n_Powered by Mercury_", // transform reply
|
|
187
|
+
suppress: true, // don't send reply
|
|
188
|
+
};
|
|
189
|
+
});
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `mercury.job(name, def)`
|
|
193
|
+
|
|
194
|
+
Register a background job that runs on the host.
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
// Interval-based
|
|
198
|
+
mercury.job("cleanup", {
|
|
199
|
+
interval: 3600_000, // every hour
|
|
200
|
+
run: async (ctx) => { /* ... */ },
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// Cron-based
|
|
204
|
+
mercury.job("daily-report", {
|
|
205
|
+
cron: "0 9 * * *", // 9am daily
|
|
206
|
+
run: async (ctx) => { /* ... */ },
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Must specify either `interval` or `cron`, not both. Jobs are started after extensions load and stopped on shutdown. Errors are caught and logged — a failing job never crashes Mercury.
|
|
211
|
+
|
|
212
|
+
### `mercury.config(key, def)`
|
|
213
|
+
|
|
214
|
+
Register a per-space config key.
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
mercury.config("enabled", {
|
|
218
|
+
description: "Enable napkin for this space",
|
|
219
|
+
default: "true",
|
|
220
|
+
validate: (v) => v === "true" || v === "false",
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Keys are namespaced to the extension: the above registers `napkin.enabled`. Users set it via `mrctl config set napkin.enabled false`.
|
|
225
|
+
|
|
226
|
+
### `mercury.widget(def)`
|
|
227
|
+
|
|
228
|
+
Register a dashboard widget.
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
mercury.widget({
|
|
232
|
+
label: "KB Distillation",
|
|
233
|
+
render: (ctx) => {
|
|
234
|
+
const lastRun = mercury.store.get("last-run") ?? "never";
|
|
235
|
+
return `<div>Last run: ${lastRun}</div>`;
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Widgets render HTML fragments in the dashboard overview. Errors show a placeholder — never crash the dashboard.
|
|
241
|
+
|
|
242
|
+
### `mercury.store`
|
|
243
|
+
|
|
244
|
+
Scoped key-value store for persistent state.
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
mercury.store.set("last-run", Date.now().toString());
|
|
248
|
+
mercury.store.get("last-run"); // "1709654400000"
|
|
249
|
+
mercury.store.delete("last-run"); // true
|
|
250
|
+
mercury.store.list(); // [{ key: "last-run", value: "..." }]
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Each extension sees only its own keys. Backed by the `extension_state` SQLite table.
|
|
254
|
+
|
|
255
|
+
## Extension Context
|
|
256
|
+
|
|
257
|
+
Event handlers and job runners receive a `MercuryExtensionContext`:
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
interface MercuryExtensionContext {
|
|
261
|
+
readonly db: Db; // Database access
|
|
262
|
+
readonly config: AppConfig; // Mercury configuration
|
|
263
|
+
readonly log: Logger; // Logger scoped to the extension
|
|
264
|
+
hasCallerPermission(spaceId: string, callerId: string, permission: string): boolean;
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Container Integration
|
|
269
|
+
|
|
270
|
+
### Skill Files
|
|
271
|
+
|
|
272
|
+
Skills can include anything the agent needs:
|
|
273
|
+
|
|
274
|
+
```
|
|
275
|
+
napkin/skill/
|
|
276
|
+
├── SKILL.md # Required: frontmatter + instructions
|
|
277
|
+
├── scripts/ # Helper scripts
|
|
278
|
+
│ └── search.js
|
|
279
|
+
├── references/ # Detailed docs loaded on-demand
|
|
280
|
+
│ └── api-reference.md
|
|
281
|
+
└── assets/
|
|
282
|
+
└── template.json
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
All files are copied into the container mount. Relative paths from SKILL.md work.
|
|
286
|
+
|
|
287
|
+
### Derived Image
|
|
288
|
+
|
|
289
|
+
Extensions that declare `mercury.cli()` get their tools installed in a derived Docker image:
|
|
290
|
+
|
|
291
|
+
```
|
|
292
|
+
FROM mercury-agent:latest
|
|
293
|
+
RUN bun add -g napkin-ai # from napkin extension
|
|
294
|
+
RUN pip install some-tool # from another extension
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Mercury builds this image on startup (cached by content hash). If no extensions declare CLIs, the base image is used unchanged.
|
|
298
|
+
|
|
299
|
+
### Agent Discovery
|
|
300
|
+
|
|
301
|
+
The agent discovers extension CLIs via skills (SKILL.md files) and invokes them through `mrctl`:
|
|
302
|
+
|
|
303
|
+
```bash
|
|
304
|
+
napkin search "query" # called directly, RBAC enforced at bash level
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## Built-in Commands vs Extensions
|
|
308
|
+
|
|
309
|
+
`mrctl` has two types of commands:
|
|
310
|
+
|
|
311
|
+
| Type | Examples | How it works |
|
|
312
|
+
|------|----------|--------------|
|
|
313
|
+
| **Built-in** | `tasks`, `roles`, `permissions`, `config`, `spaces`, `conversations`, `stop`, `compact` | HTTP calls to host API via `mrctl` |
|
|
314
|
+
| **Extension** | `napkin`, `pinchtab`, any custom | Called directly in bash, RBAC enforced by pi extension |
|
|
315
|
+
|
|
316
|
+
Built-in names are reserved — extension registration fails on collision.
|
|
317
|
+
|
|
318
|
+
## Permissions
|
|
319
|
+
|
|
320
|
+
One permission per extension, named after the extension. Extensions declare which roles get it by default:
|
|
321
|
+
|
|
322
|
+
```typescript
|
|
323
|
+
mercury.permission({ defaultRoles: ["admin", "member"] });
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
- `admin` always gets all permissions
|
|
327
|
+
- Per-space overrides via `mrctl permissions set <role> prompt,napkin,...`
|
|
328
|
+
- See [permissions.md](permissions.md) for the full RBAC system
|
|
329
|
+
|
|
330
|
+
## Installation
|
|
331
|
+
|
|
332
|
+
### `mercury add`
|
|
333
|
+
|
|
334
|
+
Install extensions from local paths, npm, or git:
|
|
335
|
+
|
|
336
|
+
```bash
|
|
337
|
+
mercury add ./path/to/extension # local directory
|
|
338
|
+
mercury add npm:mercury-ext-napkin # npm package
|
|
339
|
+
mercury add git:github.com/user/ext # git repo
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
Mercury copies the extension to `.mercury/extensions/<name>/`, installs dependencies if `package.json` is present, copies skills to the global dir, and validates the extension loads correctly.
|
|
343
|
+
|
|
344
|
+
### `mercury remove`
|
|
345
|
+
|
|
346
|
+
```bash
|
|
347
|
+
mercury remove napkin
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
Removes the extension directory and its installed skill. Restart Mercury to apply.
|
|
351
|
+
|
|
352
|
+
### `mercury extensions list`
|
|
353
|
+
|
|
354
|
+
```bash
|
|
355
|
+
mercury extensions list # or: mercury ext list
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Shows all installed extensions (user + built-in) with features and descriptions.
|
|
359
|
+
|
|
360
|
+
## Examples
|
|
361
|
+
|
|
362
|
+
See [`examples/extensions/`](../examples/extensions/) for complete, working extensions ranging from minimal (charts — CLI + skill) to full-featured (napkin — hooks, jobs, config, widgets, KB distillation).
|
|
363
|
+
|
|
364
|
+
## Types
|
|
365
|
+
|
|
366
|
+
All types are in `src/extensions/types.ts`:
|
|
367
|
+
|
|
368
|
+
- `MercuryExtensionAPI` — setup API surface
|
|
369
|
+
- `MercuryExtensionContext` — runtime context for hooks/jobs
|
|
370
|
+
- `MercuryEvents` — all lifecycle events
|
|
371
|
+
- `EventHandler<E>` / `EventResult<E>` — typed handlers with mutation support
|
|
372
|
+
- `ExtensionMeta` — collected metadata after setup
|
|
373
|
+
- `ExtensionStore` — scoped key-value store
|
|
374
|
+
- `JobDef`, `ConfigDef`, `WidgetDef`, `CliDef`, `PermissionDef` — definitions
|
|
375
|
+
- `ExtensionSetupFn` — the default export signature
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Graceful Shutdown
|
|
2
|
+
|
|
3
|
+
On `SIGTERM` or `SIGINT`, mercury tears down all components in order instead of exiting abruptly.
|
|
4
|
+
|
|
5
|
+
## Sequence
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
SIGTERM/SIGINT received
|
|
9
|
+
│
|
|
10
|
+
├─1─► Stop scheduler (clear poll timer)
|
|
11
|
+
├─2─► Cancel all pending queue entries
|
|
12
|
+
├─3─► Kill running containers (docker kill)
|
|
13
|
+
├─4─► Wait for active work to drain (up to 8s)
|
|
14
|
+
├─5─► Disconnect adapters (WhatsApp socket, etc.)
|
|
15
|
+
├─6─► Stop HTTP server
|
|
16
|
+
├─7─► Close SQLite database
|
|
17
|
+
└─8─► exit(0)
|
|
18
|
+
|
|
19
|
+
Second signal → force exit(1)
|
|
20
|
+
10s timeout → force exit(1)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Why this order
|
|
24
|
+
|
|
25
|
+
1. **Scheduler first** — prevents new work from being created while we're shutting down.
|
|
26
|
+
2. **Cancel pending queue entries** — no point starting queued work we'll just kill.
|
|
27
|
+
3. **Kill containers** — uses `docker kill` for reliable termination. Falls back to `SIGKILL` if the docker command fails. Containers are labeled with `mercury.managed=true` for identification (see [container-lifecycle.md](./container-lifecycle.md)).
|
|
28
|
+
4. **Wait for drain** — gives active container runs a chance to finish cleanly (up to 8s).
|
|
29
|
+
5. **Disconnect adapters** — closes the WhatsApp socket, Slack/Discord connections. Done after containers so in-flight replies can still be posted.
|
|
30
|
+
6. **Stop HTTP server** — stops accepting new webhook/API requests.
|
|
31
|
+
7. **Close DB last** — everything above may still write to the database (message storage, task updates), so the DB connection stays open until the very end.
|
|
32
|
+
|
|
33
|
+
## Safety mechanisms
|
|
34
|
+
|
|
35
|
+
| Mechanism | Behavior |
|
|
36
|
+
|-----------|----------|
|
|
37
|
+
| **Double-signal** | Second SIGINT/SIGTERM forces immediate `exit(1)` |
|
|
38
|
+
| **Global timeout** | If cleanup takes longer than 10s, forced `exit(1)` |
|
|
39
|
+
| **Idempotent** | `shutdown()` is guarded by a `shuttingDown` flag — calling it twice is a no-op |
|
|
40
|
+
| **Hook errors** | Individual shutdown hook failures are logged and swallowed — remaining cleanup continues |
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
### `MercuryCoreRuntime`
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
core.installSignalHandlers() // trap SIGTERM + SIGINT
|
|
48
|
+
core.onShutdown(async () => {}) // register cleanup callback
|
|
49
|
+
await core.shutdown(timeoutMs?) // trigger shutdown manually (default 10s)
|
|
50
|
+
core.isShuttingDown // boolean
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Component methods used during shutdown
|
|
54
|
+
|
|
55
|
+
| Component | Method | What it does |
|
|
56
|
+
|-----------|--------|-------------|
|
|
57
|
+
| `TaskScheduler` | `stop()` | Clears the poll timer |
|
|
58
|
+
| `SpaceQueue` | `cancelAll()` | Drops all pending entries, returns count |
|
|
59
|
+
| `SpaceQueue` | `waitForActive(ms)` | Resolves when active count hits 0 or timeout |
|
|
60
|
+
| `AgentContainerRunner` | `killAll()` | Kill all running containers via `docker kill` |
|
|
61
|
+
| `Db` | `close()` | Closes SQLite connection |
|
|
62
|
+
| `WhatsAppBaileysAdapter` | `shutdown()` | Ends the Baileys socket |
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# KB Distillation
|
|
2
|
+
|
|
3
|
+
KB distillation is **extension-based** in Mercury (not built-in).
|
|
4
|
+
|
|
5
|
+
As of v0.3.x, Mercury no longer ships a built-in `kb-distill` extension. The recommended approach is to use a user-installed extension (for example, `napkin`) that runs a background job and writes distilled knowledge into each space vault.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Recommended Setup
|
|
10
|
+
|
|
11
|
+
Use the real example extension at:
|
|
12
|
+
|
|
13
|
+
- `examples/extensions/napkin/`
|
|
14
|
+
|
|
15
|
+
It demonstrates:
|
|
16
|
+
|
|
17
|
+
- `workspace_init` hook to scaffold vault structure
|
|
18
|
+
- `before_container` hook to set `NAPKIN_VAULT`
|
|
19
|
+
- background job (`distill`) for periodic extraction
|
|
20
|
+
- dashboard widget + extension store state
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## How Distillation Works (Extension Pattern)
|
|
25
|
+
|
|
26
|
+
Typical flow:
|
|
27
|
+
|
|
28
|
+
1. Read messages from `state.db` per `space_id`
|
|
29
|
+
2. Export messages to `.messages/YYYY-MM-DD.jsonl`
|
|
30
|
+
3. Detect changed files (hash compare)
|
|
31
|
+
4. Run `pi` with a distillation prompt against changed files
|
|
32
|
+
5. Update vault files incrementally (append/update, not destructive rewrite)
|
|
33
|
+
|
|
34
|
+
This keeps runs idempotent and avoids re-processing unchanged days.
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Data Layout (napkin example)
|
|
39
|
+
|
|
40
|
+
```text
|
|
41
|
+
.mercury/spaces/<space-id>/
|
|
42
|
+
├── .messages/
|
|
43
|
+
│ └── YYYY-MM-DD.jsonl
|
|
44
|
+
└── knowledge/
|
|
45
|
+
├── people/
|
|
46
|
+
├── projects/
|
|
47
|
+
├── references/
|
|
48
|
+
├── daily/
|
|
49
|
+
└── templates/
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Message export format:
|
|
53
|
+
|
|
54
|
+
```json
|
|
55
|
+
{"ts":1709123456,"role":"ambient","content":"Alice: Great idea!"}
|
|
56
|
+
{"ts":1709123457,"role":"user","content":"What do you think about X?"}
|
|
57
|
+
{"ts":1709123458,"role":"assistant","content":"I think..."}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Configuration
|
|
63
|
+
|
|
64
|
+
With the napkin example extension:
|
|
65
|
+
|
|
66
|
+
- `MERCURY_KB_DISTILL_INTERVAL_MS=0` → disabled (default)
|
|
67
|
+
- `MERCURY_KB_DISTILL_INTERVAL_MS=3600000` → check every hour
|
|
68
|
+
|
|
69
|
+
You can also expose interval as extension config keys (see `examples/extensions/napkin/index.ts`).
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Notes
|
|
74
|
+
|
|
75
|
+
- Distillation behavior depends on the installed extension implementation
|
|
76
|
+
- Mercury core provides hooks, jobs, DB access, and workspace isolation
|
|
77
|
+
- Distillation logic/prompt lives in the extension, not in core Mercury
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Media Handling
|
|
2
|
+
|
|
3
|
+
Mercury downloads and processes media attachments from chat platforms, saving them to space workspaces and passing them to pi for processing. Models can also produce files via the `outbox/` directory.
|
|
4
|
+
|
|
5
|
+
## Supported Platforms
|
|
6
|
+
|
|
7
|
+
| Platform | Ingress | Egress | Details |
|
|
8
|
+
|----------|---------|--------|---------|
|
|
9
|
+
| WhatsApp | ✅ Baileys socket | ✅ image/video/audio/document | [whatsapp.md](./whatsapp.md) |
|
|
10
|
+
| Discord | ✅ CDN URL download | ✅ channel.send() with files | Via `DiscordBridge` |
|
|
11
|
+
| Slack | ✅ URL download (auth'd) | ✅ files.uploadV2 | Via `SlackBridge` |
|
|
12
|
+
|
|
13
|
+
## Architecture
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌─────────┐
|
|
17
|
+
│ Platform │────▶│ Bridge │────▶│ Runtime │────▶│ pi │
|
|
18
|
+
│ │ │ (normalize) │ │ (store/pass) │ │ (view) │
|
|
19
|
+
└──────────────┘ └──────────────┘ └──────────────┘ └─────────┘
|
|
20
|
+
│ │
|
|
21
|
+
▼ ▼
|
|
22
|
+
┌──────────────┐ ┌──────────────┐
|
|
23
|
+
│ Workspace │ │ Workspace │
|
|
24
|
+
│ /inbox/ │ │ /outbox/ │
|
|
25
|
+
└──────────────┘ └──────────────┘
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Media Types
|
|
29
|
+
|
|
30
|
+
All platforms map to these generic types defined in `src/types.ts`:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
type MediaType = "image" | "video" | "audio" | "voice" | "document";
|
|
34
|
+
|
|
35
|
+
interface MessageAttachment {
|
|
36
|
+
path: string; // Local file path
|
|
37
|
+
type: MediaType; // Generic type
|
|
38
|
+
mimeType: string; // MIME type (e.g., "image/jpeg")
|
|
39
|
+
filename?: string; // Original filename if available
|
|
40
|
+
sizeBytes?: number; // File size in bytes
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Configuration
|
|
45
|
+
|
|
46
|
+
| Env Variable | Default | Description |
|
|
47
|
+
|--------------|---------|-------------|
|
|
48
|
+
| `MERCURY_MEDIA_ENABLED` | `true` | Enable/disable media downloads |
|
|
49
|
+
| `MERCURY_MEDIA_MAX_SIZE_MB` | `10` | Max file size to download (MB) |
|
|
50
|
+
|
|
51
|
+
## Storage
|
|
52
|
+
|
|
53
|
+
### Ingress (inbox/)
|
|
54
|
+
|
|
55
|
+
Incoming media files are saved to the space workspace:
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
.mercury/spaces/<space_id>/inbox/<timestamp>-<type>.<ext>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Example:
|
|
62
|
+
```
|
|
63
|
+
.mercury/spaces/whatsapp_123456_g_us/inbox/
|
|
64
|
+
├── 1709012345-image.jpg
|
|
65
|
+
├── 1709012400-voice.ogg
|
|
66
|
+
└── 1709012500-document.pdf
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Egress (outbox/)
|
|
70
|
+
|
|
71
|
+
The model writes files to `outbox/` during a container run. After exit, the runtime scans for files with `mtime >= startTime` and attaches them to the reply:
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
.mercury/spaces/<space_id>/outbox/
|
|
75
|
+
├── chart.png
|
|
76
|
+
└── summary.pdf
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Previous outbox files are not deleted — only new or modified files are sent. See [pipeline.md](../pipeline.md) for details.
|
|
80
|
+
|
|
81
|
+
## Database Schema
|
|
82
|
+
|
|
83
|
+
Attachments are stored as JSON in the `messages.attachments` column:
|
|
84
|
+
|
|
85
|
+
```sql
|
|
86
|
+
ALTER TABLE messages ADD COLUMN attachments TEXT;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```json
|
|
90
|
+
[
|
|
91
|
+
{
|
|
92
|
+
"path": "/Users/.../media/1709012345-image.jpg",
|
|
93
|
+
"type": "image",
|
|
94
|
+
"mimeType": "image/jpeg",
|
|
95
|
+
"sizeBytes": 12345
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Prompt Format
|
|
101
|
+
|
|
102
|
+
Attachments are passed to pi as XML:
|
|
103
|
+
|
|
104
|
+
```xml
|
|
105
|
+
<attachments>
|
|
106
|
+
<attachment type="image" path="/spaces/xxx/media/123-image.jpg" mime="image/jpeg" size="12345" />
|
|
107
|
+
</attachments>
|
|
108
|
+
|
|
109
|
+
@mercury what's in this image?
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Reply context includes media info:
|
|
113
|
+
|
|
114
|
+
```xml
|
|
115
|
+
<reply_to name="John" jid="123@wa" message_id="ABC" media_type="image" media_mime="image/jpeg">
|
|
116
|
+
Check out this sunset!
|
|
117
|
+
</reply_to>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## pi Capabilities
|
|
121
|
+
|
|
122
|
+
| Media Type | pi Support |
|
|
123
|
+
|------------|------------|
|
|
124
|
+
| Images (jpg, png, gif, webp) | ✅ Can view via `read` tool |
|
|
125
|
+
| Voice/Audio | ❌ Cannot play — needs transcription |
|
|
126
|
+
| Video | ❌ Cannot play — could extract frames |
|
|
127
|
+
| Documents (txt, code) | ✅ Can read text-based files |
|
|
128
|
+
| Documents (pdf, docx) | ❌ Cannot read binary formats |
|
|
129
|
+
|
|
130
|
+
## Voice transcription
|
|
131
|
+
|
|
132
|
+
Voice and audio attachments are not playable inside pi. Install the **`voice-transcribe`** extension (see dashboard catalog or `examples/extensions/voice-transcribe/`) to append a text transcript before the agent runs.
|
|
133
|
+
|
|
134
|
+
- **Default (`voice-transcribe.provider=local`)**: runs Python on the Mercury host; install deps from the extension’s `requirements.txt`. Set `voice-transcribe.local_engine` to `transformers` (default, e.g. [mike249/whisper-tiny-he-2](https://huggingface.co/mike249/whisper-tiny-he-2)) or `faster_whisper` for [CTranslate2](https://github.com/OpenNMT/CTranslate2) models on the Hub (e.g. [ivrit-ai](https://huggingface.co/ivrit-ai) `*-ct2` repos). See the extension skill for `MERCURY_VOICE_FW_COMPUTE_TYPE` and `MERCURY_VOICE_LANGUAGE`.
|
|
135
|
+
- **API (`voice-transcribe.provider=api`)**: uses the [Hugging Face Inference API](https://huggingface.co/docs/api-inference) with `MERCURY_HF_TOKEN` — choose a model that has a Hub Inference Provider.
|
|
136
|
+
|
|
137
|
+
## Future Enhancements
|
|
138
|
+
|
|
139
|
+
- [ ] Video frame extraction
|
|
140
|
+
- [ ] PDF text extraction
|