Skip to content

Commit 1b973f7

Browse files
feat: add inbound media understanding
Co-authored-by: Tristan Manchester <[email protected]>
1 parent 4b749f1 commit 1b973f7

42 files changed

Lines changed: 2547 additions & 101 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
- Docs: add Date & Time guide and update prompt/timezone configuration docs.
9090
- Messages: debounce rapid inbound messages across channels with per-connector overrides. (#971) — thanks @juanpablodlc.
9191
- Messages: allow media-only sends (CLI/tool) and show Telegram voice recording status for voice notes. (#957) — thanks @rdev.
92+
- Media: add optional inbound media understanding for image/audio/video with provider + CLI fallbacks. (#1005) — thanks @tristanmanchester.
9293
- Auth/Status: keep auth profiles sticky per session (rotate on compaction/new), surface provider usage headers in `/status` and `clawdbot models status`, and update docs.
9394
- CLI: add `--json` output for `clawdbot daemon` lifecycle/install commands.
9495
- Memory: make `node-llama-cpp` an optional dependency (avoid Node 25 install failures) and improve local-embeddings fallback/errors.

‎docs/gateway/configuration-examples.md‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,21 @@ Save to `~/.clawdbot/clawdbot.json` and you can DM the bot from that number.
124124

125125
// Tooling
126126
tools: {
127-
audio: {
128-
transcription: {
129-
args: ["--model", "base", "{{MediaPath}}"],
127+
media: {
128+
audio: {
129+
enabled: true,
130+
maxBytes: 20971520,
131+
models: [
132+
{ provider: "openai", model: "whisper-1" },
133+
// Optional CLI fallback (Whisper binary):
134+
// { type: "cli", command: "whisper", args: ["--model", "base", "{{MediaPath}}"] }
135+
],
130136
timeoutSeconds: 120
137+
},
138+
video: {
139+
enabled: true,
140+
maxBytes: 52428800,
141+
models: [{ provider: "google", model: "gemini-3-flash-preview" }]
131142
}
132143
}
133144
},

‎docs/gateway/configuration.md‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,58 @@ Legacy: `tools.bash` is still accepted as an alias.
17691769
- `tools.web.fetch.firecrawl.maxAgeMs` (optional)
17701770
- `tools.web.fetch.firecrawl.timeoutSeconds` (optional)
17711771

1772+
`tools.media` configures inbound media understanding (image/audio/video):
1773+
- `tools.media.image` / `tools.media.audio` / `tools.media.video`:
1774+
- `enabled`: opt-out switch (default true).
1775+
- `prompt`: optional prompt override (image/video append a `maxChars` hint automatically).
1776+
- `maxChars`: max output characters (default 500 for image/video; unset for audio).
1777+
- `maxBytes`: max media size to send (defaults: image 10MB, audio 20MB, video 50MB).
1778+
- `timeoutSeconds`: request timeout (defaults: image 60s, audio 60s, video 120s).
1779+
- `language`: optional audio hint.
1780+
- `scope`: optional gating (first match wins) with `match.channel`, `match.chatType`, or `match.keyPrefix`.
1781+
- `models`: ordered list of model entries; failures or oversize media fall back to the next entry.
1782+
- Each `models[]` entry:
1783+
- Provider entry (`type: "provider"` or omitted):
1784+
- `provider`: API provider id (`openai`, `anthropic`, `google`/`gemini`, `groq`, etc).
1785+
- `model`: model id override (required for image; defaults to `whisper-1`/`whisper-large-v3-turbo` for audio providers, and `gemini-3-flash-preview` for video).
1786+
- `profile` / `preferredProfile`: auth profile selection.
1787+
- CLI entry (`type: "cli"`):
1788+
- `command`: executable to run.
1789+
- `args`: templated args (supports `{{MediaPath}}`, `{{Prompt}}`, `{{MaxChars}}`, etc).
1790+
- `capabilities`: optional list (`image`, `audio`, `video`) to gate a shared entry.
1791+
- `prompt`, `maxChars`, `maxBytes`, `timeoutSeconds`, `language` can be overridden per entry.
1792+
1793+
If no models are configured (or `enabled: false`), understanding is skipped; the model still receives the original attachments.
1794+
1795+
Provider auth follows the standard model auth order (auth profiles, env vars like `OPENAI_API_KEY`/`GROQ_API_KEY`/`GEMINI_API_KEY`, or `models.providers.*.apiKey`).
1796+
1797+
Example:
1798+
```json5
1799+
{
1800+
tools: {
1801+
media: {
1802+
audio: {
1803+
enabled: true,
1804+
maxBytes: 20971520,
1805+
scope: {
1806+
default: "deny",
1807+
rules: [{ action: "allow", match: { chatType: "direct" } }]
1808+
},
1809+
models: [
1810+
{ provider: "openai", model: "whisper-1" },
1811+
{ type: "cli", command: "whisper", args: ["--model", "base", "{{MediaPath}}"] }
1812+
]
1813+
},
1814+
video: {
1815+
enabled: true,
1816+
maxBytes: 52428800,
1817+
models: [{ provider: "google", model: "gemini-3-flash-preview" }]
1818+
}
1819+
}
1820+
}
1821+
}
1822+
```
1823+
17721824
`agents.defaults.subagents` configures sub-agent defaults:
17731825
- `model`: default model for spawned sub-agents (string or `{ primary, fallbacks }`). If omitted, sub-agents inherit the caller’s model unless overridden per agent or per call.
17741826
- `maxConcurrent`: max concurrent sub-agent runs (default 1)
@@ -2848,7 +2900,7 @@ clawdbot dns setup --apply
28482900

28492901
## Template variables
28502902

2851-
Template placeholders are expanded in `tools.audio.transcription.args` (and any future templated argument fields).
2903+
Template placeholders are expanded in `tools.media.*.models[].args` (and any future templated argument fields).
28522904

28532905
| Variable | Description |
28542906
|----------|-------------|
@@ -2864,6 +2916,8 @@ Template placeholders are expanded in `tools.audio.transcription.args` (and any
28642916
| `{{MediaPath}}` | Local media path (if downloaded) |
28652917
| `{{MediaType}}` | Media type (image/audio/document/…) |
28662918
| `{{Transcript}}` | Audio transcript (when enabled) |
2919+
| `{{Prompt}}` | Resolved media prompt for CLI entries |
2920+
| `{{MaxChars}}` | Resolved max output chars for CLI entries |
28672921
| `{{ChatType}}` | `"direct"` or `"group"` |
28682922
| `{{GroupSubject}}` | Group subject (best effort) |
28692923
| `{{GroupMembers}}` | Group members preview (best effort) |

‎docs/gateway/doctor.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Current migrations:
111111
- `routing.bindings` → top-level `bindings`
112112
- `routing.agents`/`routing.defaultAgentId` → `agents.list` + `agents.list[].default`
113113
- `routing.agentToAgent` → `tools.agentToAgent`
114-
- `routing.transcribeAudio` → `tools.audio.transcription`
114+
- `routing.transcribeAudio` → `tools.media.audio.models`
115115
- `bindings[].match.accountID` → `bindings[].match.accountId`
116116
- `identity` → `agents.list[].identity`
117117
- `agent.*` → `agents.defaults` + `tools.*` (tools/elevated/exec/sandbox/subagents)

‎docs/nodes/audio.md‎

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,73 @@ summary: "How inbound audio/voice notes are downloaded, transcribed, and injecte
33
read_when:
44
- Changing audio transcription or media handling
55
---
6-
# Audio / Voice Notes — 2025-12-05
6+
# Audio / Voice Notes — 2026-01-17
77

88
## What works
9-
- **Optional transcription**: If `tools.audio.transcription` is set in `~/.clawdbot/clawdbot.json`, Clawdbot will:
10-
1) Download inbound audio to a temp path when WhatsApp only provides a URL.
11-
2) Run the configured CLI args (templated with `{{MediaPath}}`), expecting transcript on stdout.
12-
3) Replace `Body` with the transcript, set `{{Transcript}}`, and prepend the original media path plus a `Transcript:` section in the command prompt so models see both.
13-
4) Continue through the normal auto-reply pipeline (templating, sessions, Pi command).
14-
- **Verbose logging**: In `--verbose`, we log when transcription runs and when the transcript replaces the body.
9+
- **Media understanding (audio)**: If `tools.media.audio` is enabled and has `models`, Clawdbot:
10+
1) Locates the first audio attachment (local path or URL) and downloads it if needed.
11+
2) Enforces `maxBytes` before sending to each model entry.
12+
3) Runs the first eligible model entry in order (provider or CLI).
13+
4) If it fails or skips (size/timeout), it tries the next entry.
14+
5) On success, it replaces `Body` with an `[Audio]` block and sets `{{Transcript}}`.
15+
- **Command parsing**: When transcription succeeds, `CommandBody`/`RawBody` are set to the transcript so slash commands still work.
16+
- **Verbose logging**: In `--verbose`, we log when transcription runs and when it replaces the body.
1517

16-
## Config example (Whisper CLI)
17-
Requires `whisper` CLI installed:
18+
## Config examples
19+
20+
### Provider + CLI fallback (OpenAI + Whisper CLI)
21+
```json5
22+
{
23+
tools: {
24+
media: {
25+
audio: {
26+
enabled: true,
27+
maxBytes: 20971520,
28+
models: [
29+
{ provider: "openai", model: "whisper-1" },
30+
{
31+
type: "cli",
32+
command: "whisper",
33+
args: ["--model", "base", "{{MediaPath}}"],
34+
timeoutSeconds: 45
35+
}
36+
]
37+
}
38+
}
39+
}
40+
}
41+
```
42+
43+
### Provider-only with scope gating
1844
```json5
1945
{
2046
tools: {
21-
audio: {
22-
transcription: {
23-
args: ["--model", "base", "{{MediaPath}}"],
24-
timeoutSeconds: 45
47+
media: {
48+
audio: {
49+
enabled: true,
50+
scope: {
51+
default: "allow",
52+
rules: [
53+
{ action: "deny", match: { chatType: "group" } }
54+
]
55+
},
56+
models: [
57+
{ provider: "openai", model: "whisper-1" }
58+
]
2559
}
2660
}
2761
}
2862
}
2963
```
3064

3165
## Notes & limits
32-
- We don’t ship a transcriber; you opt in with the Whisper CLI on your PATH.
33-
- Size guard: inbound audio must be ≤5 MB (matches the temp media store and transcript pipeline).
34-
- Outbound caps: web send supports audio/voice up to 16 MB (sent as a voice note with `ptt: true`).
35-
- If transcription fails, we fall back to the original body/media note; replies still go through.
36-
- Transcript is available to templates as `{{Transcript}}`; models get both the media path and a `Transcript:` block in the prompt when using command mode.
66+
- Provider auth follows the standard model auth order (auth profiles, env vars, `models.providers.*.apiKey`).
67+
- Default size cap is 20MB (`tools.media.audio.maxBytes`). Oversize audio is skipped for that model and the next entry is tried.
68+
- Default `maxChars` for audio is **unset** (full transcript). Set `tools.media.audio.maxChars` or per-entry `maxChars` to trim output.
69+
- Transcript is available to templates as `{{Transcript}}`.
70+
- CLI stdout is capped (5MB); keep CLI output concise.
3771

3872
## Gotchas
73+
- Scope rules use first-match wins. `chatType` is normalized to `direct`, `group`, or `room`.
3974
- Ensure your CLI exits 0 and prints plain text; JSON needs to be massaged via `jq -r .text`.
40-
- Keep timeouts reasonable (`timeoutSeconds`, default 45s) to avoid blocking the reply queue.
75+
- Keep timeouts reasonable (`timeoutSeconds`, default 60s) to avoid blocking the reply queue.

‎docs/nodes/images.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,23 @@ The WhatsApp channel runs via **Baileys Web**. This document captures the curren
3838
- `{{MediaUrl}}` pseudo-URL for the inbound media.
3939
- `{{MediaPath}}` local temp path written before running the command.
4040
- When a per-session Docker sandbox is enabled, inbound media is copied into the sandbox workspace and `MediaPath`/`MediaUrl` are rewritten to a relative path like `media/inbound/<filename>`.
41-
- Audio transcription (if configured via `tools.audio.transcription`) runs before templating and can replace `Body` with the transcript.
41+
- Media understanding (if configured via `tools.media.*`) runs before templating and can insert `[Image]`, `[Audio]`, and `[Video]` blocks into `Body`.
42+
- Audio sets `{{Transcript}}` and uses the transcript for command parsing so slash commands still work.
43+
- Video and image descriptions preserve any caption text for command parsing.
44+
- Only the first matching image/audio/video attachment is processed; remaining attachments are left untouched.
4245

4346
## Limits & Errors
47+
**Outbound send caps (WhatsApp web send)**
4448
- Images: ~6 MB cap after recompression.
4549
- Audio/voice/video: 16 MB cap; documents: 100 MB cap.
4650
- Oversize or unreadable media → clear error in logs and the reply is skipped.
4751

52+
**Media understanding caps (transcription/description)**
53+
- Image default: 10 MB (`tools.media.image.maxBytes`).
54+
- Audio default: 20 MB (`tools.media.audio.maxBytes`).
55+
- Video default: 50 MB (`tools.media.video.maxBytes`).
56+
- Oversize media skips understanding, but replies still go through with the original body.
57+
4858
## Notes for Tests
4959
- Cover send + reply flows for image/audio/document cases.
5060
- Validate recompression for images (size bound) and voice-note flag for audio.

0 commit comments

Comments
 (0)