Compare commits

..
33 changed files with 132 additions and 1415 deletions

No files matched your search

+1 -3
View File
@@ -8,7 +8,6 @@ Docs: https://docs.clawd.bot
- Highlight: Lobster optional plugin tool for typed workflows + approval gates. https://docs.clawd.bot/tools/lobster - Highlight: Lobster optional plugin tool for typed workflows + approval gates. https://docs.clawd.bot/tools/lobster
- Lobster: allow workflow file args via `argsJson` in the plugin tool. https://docs.clawd.bot/tools/lobster - Lobster: allow workflow file args via `argsJson` in the plugin tool. https://docs.clawd.bot/tools/lobster
- Agents: add identity avatar config support and Control UI avatar rendering. (#1329, #1424) Thanks @dlauer. - Agents: add identity avatar config support and Control UI avatar rendering. (#1329, #1424) Thanks @dlauer.
- Slack: add chat-type reply threading overrides via `replyToModeByChatType`. (#1442) Thanks @stefangalescu.
- Memory: prevent CLI hangs by deferring vector probes, adding sqlite-vec/embedding timeouts, and showing sync progress early. - Memory: prevent CLI hangs by deferring vector probes, adding sqlite-vec/embedding timeouts, and showing sync progress early.
- BlueBubbles: add `asVoice` support for MP3/CAF voice memos in sendAttachment. (#1477, #1482) Thanks @Nicell. - BlueBubbles: add `asVoice` support for MP3/CAF voice memos in sendAttachment. (#1477, #1482) Thanks @Nicell.
- Docs: add troubleshooting entry for gateway.mode blocking gateway start. https://docs.clawd.bot/gateway/troubleshooting - Docs: add troubleshooting entry for gateway.mode blocking gateway start. https://docs.clawd.bot/gateway/troubleshooting
@@ -28,7 +27,6 @@ Docs: https://docs.clawd.bot
- BlueBubbles: stop typing indicator on idle/no-reply. (#1439) Thanks @Nicell. - BlueBubbles: stop typing indicator on idle/no-reply. (#1439) Thanks @Nicell.
- Message tool: keep path/filePath as-is for send; hydrate buffers only for sendAttachment. (#1444) Thanks @hopyky. - Message tool: keep path/filePath as-is for send; hydrate buffers only for sendAttachment. (#1444) Thanks @hopyky.
- Auto-reply: only report a model switch when session state is available. (#1465) Thanks @robbyczgw-cla. - Auto-reply: only report a model switch when session state is available. (#1465) Thanks @robbyczgw-cla.
- Compaction: harden oversized summarization handling and gate UI compaction status by verbose level. (#1466) Thanks @dlauer.
- Control UI: resolve local avatar URLs with basePath across injection + identity RPC. (#1457) Thanks @dlauer. - Control UI: resolve local avatar URLs with basePath across injection + identity RPC. (#1457) Thanks @dlauer.
- Agents: surface concrete API error details instead of generic AI service errors. - Agents: surface concrete API error details instead of generic AI service errors.
- Exec approvals: allow per-segment allowlists for chained shell commands on gateway + node hosts. (#1458) Thanks @czekaj. - Exec approvals: allow per-segment allowlists for chained shell commands on gateway + node hosts. (#1458) Thanks @czekaj.
@@ -36,7 +34,7 @@ Docs: https://docs.clawd.bot
- Doctor: honor CLAWDBOT_GATEWAY_TOKEN for auth checks and security audit token reuse. (#1448) Thanks @azade-c. - Doctor: honor CLAWDBOT_GATEWAY_TOKEN for auth checks and security audit token reuse. (#1448) Thanks @azade-c.
- Agents: make tool summaries more readable and only show optional params when set. - Agents: make tool summaries more readable and only show optional params when set.
- Agents: honor SOUL.md guidance even when the file is nested or path-qualified. (#1434) Thanks @neooriginal. - Agents: honor SOUL.md guidance even when the file is nested or path-qualified. (#1434) Thanks @neooriginal.
- Matrix (plugin): persist m.direct for resolved DMs and harden room fallback. (#1436, #1486) Thanks @sibbl. - Matrix (plugin): persist m.direct for resolved DMs and harden room fallback. (#1436) Thanks @sibbl.
- CLI: prefer `~` for home paths in output. - CLI: prefer `~` for home paths in output.
- Mattermost (plugin): enforce pairing/allowlist gating, keep @username targets, and clarify plugin-only docs. (#1428) Thanks @damoahdominic. - Mattermost (plugin): enforce pairing/allowlist gating, keep @username targets, and clarify plugin-only docs. (#1428) Thanks @damoahdominic.
- Agents: centralize transcript sanitization in the runner; keep <final> tags and error turns intact. - Agents: centralize transcript sanitization in the runner; keep <final> tags and error turns intact.
+6 -55
View File
@@ -362,72 +362,23 @@ By default, Clawdbot replies in the main channel. Use `channels.slack.replyToMod
The mode applies to both auto-replies and agent tool calls (`slack sendMessage`). The mode applies to both auto-replies and agent tool calls (`slack sendMessage`).
### Per-chat-type threading ### DM-specific threading
You can configure different threading behavior per chat type by setting `channels.slack.replyToModeByChatType`: You can configure different threading behavior for DMs vs channels by setting `channels.slack.dm.replyToMode`:
```json5 ```json5
{ {
channels: { channels: {
slack: { slack: {
replyToMode: "off", // default for channels replyToMode: "off", // default for channels
replyToModeByChatType: { dm: {
direct: "all", // DMs always thread replyToMode: "all" // DMs always thread
group: "first" // group DMs/MPIM thread first reply }
},
} }
} }
} }
``` ```
Supported chat types: When `dm.replyToMode` is set, DMs use that mode; channels use the top-level `replyToMode`. If `dm.replyToMode` is not set, both DMs and channels use the top-level setting.
- `direct`: 1:1 DMs (Slack `im`)
- `group`: group DMs / MPIMs (Slack `mpim`)
- `channel`: standard channels (public/private)
Precedence:
1) `replyToModeByChatType.<chatType>`
2) `replyToMode`
3) Provider default (`off`)
Legacy `channels.slack.dm.replyToMode` is still accepted as a fallback for `direct` when no chat-type override is set.
Examples:
Thread DMs only:
```json5
{
channels: {
slack: {
replyToMode: "off",
replyToModeByChatType: { direct: "all" }
}
}
}
```
Thread group DMs but keep channels in the root:
```json5
{
channels: {
slack: {
replyToMode: "off",
replyToModeByChatType: { group: "first" }
}
}
}
```
Make channels thread, keep DMs in the root:
```json5
{
channels: {
slack: {
replyToMode: "first",
replyToModeByChatType: { direct: "off", group: "off" }
}
}
}
```
### Manual threading tags ### Manual threading tags
For fine-grained control, use these tags in agent responses: For fine-grained control, use these tags in agent responses:
+1 -1
View File
@@ -129,7 +129,7 @@ Save to `~/.clawdbot/clawdbot.json` and you can DM the bot from that number.
enabled: true, enabled: true,
maxBytes: 20971520, maxBytes: 20971520,
models: [ models: [
{ provider: "openai", model: "gpt-4o-mini-transcribe" }, { provider: "openai", model: "whisper-1" },
// Optional CLI fallback (Whisper binary): // Optional CLI fallback (Whisper binary):
// { type: "cli", command: "whisper", args: ["--model", "base", "{{MediaPath}}"] } // { type: "cli", command: "whisper", args: ["--model", "base", "{{MediaPath}}"] }
], ],
+2 -2
View File
@@ -1865,7 +1865,7 @@ Note: `applyPatch` is only under `tools.exec`.
- Each `models[]` entry: - Each `models[]` entry:
- Provider entry (`type: "provider"` or omitted): - Provider entry (`type: "provider"` or omitted):
- `provider`: API provider id (`openai`, `anthropic`, `google`/`gemini`, `groq`, etc). - `provider`: API provider id (`openai`, `anthropic`, `google`/`gemini`, `groq`, etc).
- `model`: model id override (required for image; defaults to `gpt-4o-mini-transcribe`/`whisper-large-v3-turbo` for audio providers, and `gemini-3-flash-preview` for video). - `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).
- `profile` / `preferredProfile`: auth profile selection. - `profile` / `preferredProfile`: auth profile selection.
- CLI entry (`type: "cli"`): - CLI entry (`type: "cli"`):
- `command`: executable to run. - `command`: executable to run.
@@ -1890,7 +1890,7 @@ Example:
rules: [{ action: "allow", match: { chatType: "direct" } }] rules: [{ action: "allow", match: { chatType: "direct" } }]
}, },
models: [ models: [
{ provider: "openai", model: "gpt-4o-mini-transcribe" }, { provider: "openai", model: "whisper-1" },
{ type: "cli", command: "whisper", args: ["--model", "base", "{{MediaPath}}"] } { type: "cli", command: "whisper", args: ["--model", "base", "{{MediaPath}}"] }
] ]
}, },
+3 -19
View File
@@ -6,7 +6,7 @@ read_when:
# Audio / Voice Notes — 2026-01-17 # Audio / Voice Notes — 2026-01-17
## What works ## What works
- **Media understanding (audio)**: If audio understanding is enabled (or auto‑detected), Clawdbot: - **Media understanding (audio)**: If `tools.media.audio` is enabled (or a shared `tools.media.models` entry supports audio), Clawdbot:
1) Locates the first audio attachment (local path or URL) and downloads it if needed. 1) Locates the first audio attachment (local path or URL) and downloads it if needed.
2) Enforces `maxBytes` before sending to each model entry. 2) Enforces `maxBytes` before sending to each model entry.
3) Runs the first eligible model entry in order (provider or CLI). 3) Runs the first eligible model entry in order (provider or CLI).
@@ -15,21 +15,6 @@ read_when:
- **Command parsing**: When transcription succeeds, `CommandBody`/`RawBody` are set to the transcript so slash commands still work. - **Command parsing**: When transcription succeeds, `CommandBody`/`RawBody` are set to the transcript so slash commands still work.
- **Verbose logging**: In `--verbose`, we log when transcription runs and when it replaces the body. - **Verbose logging**: In `--verbose`, we log when transcription runs and when it replaces the body.
## Auto-detection (default)
If you **don’t configure models** and `tools.media.audio.enabled` is **not** set to `false`,
Clawdbot auto-detects in this order and stops at the first working option:
1) **Local CLIs** (if installed)
- `sherpa-onnx-offline` (requires `SHERPA_ONNX_MODEL_DIR` with encoder/decoder/joiner/tokens)
- `whisper-cli` (from `whisper-cpp`; uses `WHISPER_CPP_MODEL` or the bundled tiny model)
- `whisper` (Python CLI; downloads models automatically)
2) **Gemini CLI** (`gemini`) using `read_many_files`
3) **Provider keys** (OpenAI → Groq → Deepgram → Google)
To disable auto-detection, set `tools.media.audio.enabled: false`.
To customize, set `tools.media.audio.models`.
Note: Binary detection is best-effort across macOS/Linux/Windows; ensure the CLI is on `PATH` (we expand `~`), or set an explicit CLI model with a full command path.
## Config examples ## Config examples
### Provider + CLI fallback (OpenAI + Whisper CLI) ### Provider + CLI fallback (OpenAI + Whisper CLI)
@@ -41,7 +26,7 @@ Note: Binary detection is best-effort across macOS/Linux/Windows; ensure the CLI
enabled: true, enabled: true,
maxBytes: 20971520, maxBytes: 20971520,
models: [ models: [
{ provider: "openai", model: "gpt-4o-mini-transcribe" }, { provider: "openai", model: "whisper-1" },
{ {
type: "cli", type: "cli",
command: "whisper", command: "whisper",
@@ -69,7 +54,7 @@ Note: Binary detection is best-effort across macOS/Linux/Windows; ensure the CLI
] ]
}, },
models: [ models: [
{ provider: "openai", model: "gpt-4o-mini-transcribe" } { provider: "openai", model: "whisper-1" }
] ]
} }
} }
@@ -98,7 +83,6 @@ Note: Binary detection is best-effort across macOS/Linux/Windows; ensure the CLI
- Audio providers can override `baseUrl`, `headers`, and `providerOptions` via `tools.media.audio`. - Audio providers can override `baseUrl`, `headers`, and `providerOptions` via `tools.media.audio`.
- Default size cap is 20MB (`tools.media.audio.maxBytes`). Oversize audio is skipped for that model and the next entry is tried. - Default size cap is 20MB (`tools.media.audio.maxBytes`). Oversize audio is skipped for that model and the next entry is tried.
- Default `maxChars` for audio is **unset** (full transcript). Set `tools.media.audio.maxChars` or per-entry `maxChars` to trim output. - Default `maxChars` for audio is **unset** (full transcript). Set `tools.media.audio.maxChars` or per-entry `maxChars` to trim output.
- OpenAI auto default is `gpt-4o-mini-transcribe`; set `model: "gpt-4o-transcribe"` for higher accuracy.
- Use `tools.media.audio.attachments` to process multiple voice notes (`mode: "all"` + `maxAttachments`). - Use `tools.media.audio.attachments` to process multiple voice notes (`mode: "all"` + `maxAttachments`).
- Transcript is available to templates as `{{Transcript}}`. - Transcript is available to templates as `{{Transcript}}`.
- CLI stdout is capped (5MB); keep CLI output concise. - CLI stdout is capped (5MB); keep CLI output concise.
+14 -25
View File
@@ -6,7 +6,7 @@ read_when:
--- ---
# Media Understanding (Inbound) — 2026-01-17 # Media Understanding (Inbound) — 2026-01-17
Clawdbot can **summarize inbound media** (image/audio/video) before the reply pipeline runs. It auto‑detects when local tools or provider keys are available, and can be disabled or customized. If understanding is off, models still receive the original files/URLs as usual. Clawdbot can optionally **summarize inbound media** (image/audio/video) before the reply pipeline runs. This is **opt-in** and separate from the base attachment flow—if understanding is off, models still receive the original files/URLs as usual.
## Goals ## Goals
- Optional: pre‑digest inbound media into short text for faster routing + better command parsing. - Optional: pre‑digest inbound media into short text for faster routing + better command parsing.
@@ -88,11 +88,6 @@ Each `models[]` entry can be **provider** or **CLI**:
} }
``` ```
CLI templates can also use:
- `{{MediaDir}}` (directory containing the media file)
- `{{OutputDir}}` (scratch dir created for this run)
- `{{OutputBase}}` (scratch file base path, no extension)
## Defaults and limits ## Defaults and limits
Recommended defaults: Recommended defaults:
- `maxChars`: **500** for image/video (short, command‑friendly) - `maxChars`: **500** for image/video (short, command‑friendly)
@@ -109,22 +104,17 @@ Rules:
- If `<capability>.enabled: true` but no models are configured, Clawdbot tries the - If `<capability>.enabled: true` but no models are configured, Clawdbot tries the
**active reply model** when its provider supports the capability. **active reply model** when its provider supports the capability.
### Auto-detect media understanding (default) ### Auto-enable audio (when keys exist)
If `tools.media.<capability>.enabled` is **not** set to `false` and you haven’t If `tools.media.audio.enabled` is **not** set to `false` and you have any supported
configured models, Clawdbot auto-detects in this order and **stops at the first audio provider keys configured, Clawdbot will **auto-enable audio transcription**
working option**: even when you haven’t listed models explicitly.
1) **Local CLIs** (audio only; if installed) Providers checked (in order):
- `sherpa-onnx-offline` (requires `SHERPA_ONNX_MODEL_DIR` with encoder/decoder/joiner/tokens) 1) OpenAI
- `whisper-cli` (`whisper-cpp`; uses `WHISPER_CPP_MODEL` or the bundled tiny model) 2) Groq
- `whisper` (Python CLI; downloads models automatically) 3) Deepgram
2) **Gemini CLI** (`gemini`) using `read_many_files`
3) **Provider keys**
- Audio: OpenAI → Groq → Deepgram → Google
- Image: OpenAI → Anthropic → Google → MiniMax
- Video: Google
To disable auto-detection, set: To disable this behavior, set:
```json5 ```json5
{ {
tools: { tools: {
@@ -136,7 +126,6 @@ To disable auto-detection, set:
} }
} }
``` ```
Note: Binary detection is best-effort across macOS/Linux/Windows; ensure the CLI is on `PATH` (we expand `~`), or set an explicit CLI model with a full command path.
## Capabilities (optional) ## Capabilities (optional)
If you set `capabilities`, the entry only runs for those media types. For shared If you set `capabilities`, the entry only runs for those media types. For shared
@@ -153,7 +142,7 @@ If you omit `capabilities`, the entry is eligible for the list it appears in.
| Capability | Provider integration | Notes | | Capability | Provider integration | Notes |
|------------|----------------------|-------| |------------|----------------------|-------|
| Image | OpenAI / Anthropic / Google / others via `pi-ai` | Any image-capable model in the registry works. | | Image | OpenAI / Anthropic / Google / others via `pi-ai` | Any image-capable model in the registry works. |
| Audio | OpenAI, Groq, Deepgram, Google | Provider transcription (Whisper/Deepgram/Gemini). | | Audio | OpenAI, Groq, Deepgram | Provider transcription (Whisper/Deepgram). |
| Video | Google (Gemini API) | Provider video understanding. | | Video | Google (Gemini API) | Provider video understanding. |
## Recommended providers ## Recommended providers
@@ -162,8 +151,8 @@ If you omit `capabilities`, the entry is eligible for the list it appears in.
- Good defaults: `openai/gpt-5.2`, `anthropic/claude-opus-4-5`, `google/gemini-3-pro-preview`. - Good defaults: `openai/gpt-5.2`, `anthropic/claude-opus-4-5`, `google/gemini-3-pro-preview`.
**Audio** **Audio**
- `openai/gpt-4o-mini-transcribe`, `groq/whisper-large-v3-turbo`, or `deepgram/nova-3`. - `openai/whisper-1`, `groq/whisper-large-v3-turbo`, or `deepgram/nova-3`.
- CLI fallback: `whisper-cli` (whisper-cpp) or `whisper`. - CLI fallback: `whisper` binary.
- Deepgram setup: [Deepgram (audio transcription)](/providers/deepgram). - Deepgram setup: [Deepgram (audio transcription)](/providers/deepgram).
**Video** **Video**
@@ -220,7 +209,7 @@ When `mode: "all"`, outputs are labeled `[Image 1/2]`, `[Audio 2/2]`, etc.
audio: { audio: {
enabled: true, enabled: true,
models: [ models: [
{ provider: "openai", model: "gpt-4o-mini-transcribe" }, { provider: "openai", model: "whisper-1" },
{ {
type: "cli", type: "cli",
command: "whisper", command: "whisper",
-20
View File
@@ -1,5 +1,3 @@
import { EventEmitter } from "node:events";
import type { AgentMessage, AgentTool } from "@mariozechner/pi-agent-core"; import type { AgentMessage, AgentTool } from "@mariozechner/pi-agent-core";
import type { TSchema } from "@sinclair/typebox"; import type { TSchema } from "@sinclair/typebox";
import type { SessionManager } from "@mariozechner/pi-coding-agent"; import type { SessionManager } from "@mariozechner/pi-coding-agent";
@@ -186,28 +184,10 @@ export function logToolSchemasForGoogle(params: { tools: AgentTool[]; provider:
} }
} }
// Event emitter for unhandled compaction failures that escape try-catch blocks.
// Listeners can use this to trigger session recovery with retry.
const compactionFailureEmitter = new EventEmitter();
export type CompactionFailureListener = (reason: string) => void;
/**
* Register a listener for unhandled compaction failures.
* Called when auto-compaction fails in a way that escapes the normal try-catch,
* e.g., when the summarization request itself exceeds the model's token limit.
* Returns an unsubscribe function.
*/
export function onUnhandledCompactionFailure(cb: CompactionFailureListener): () => void {
compactionFailureEmitter.on("failure", cb);
return () => compactionFailureEmitter.off("failure", cb);
}
registerUnhandledRejectionHandler((reason) => { registerUnhandledRejectionHandler((reason) => {
const message = describeUnknownError(reason); const message = describeUnknownError(reason);
if (!isCompactionFailureError(message)) return false; if (!isCompactionFailureError(message)) return false;
log.error(`Auto-compaction failed (unhandled): ${message}`); log.error(`Auto-compaction failed (unhandled): ${message}`);
compactionFailureEmitter.emit("failure", message);
return true; return true;
}); });
@@ -3,15 +3,7 @@ import { describe, expect, it } from "vitest";
import { __testing } from "./compaction-safeguard.js"; import { __testing } from "./compaction-safeguard.js";
const { const { collectToolFailures, formatToolFailuresSection } = __testing;
collectToolFailures,
formatToolFailuresSection,
computeAdaptiveChunkRatio,
isOversizedForSummary,
BASE_CHUNK_RATIO,
MIN_CHUNK_RATIO,
SAFETY_MARGIN,
} = __testing;
describe("compaction-safeguard tool failures", () => { describe("compaction-safeguard tool failures", () => {
it("formats tool failures with meta and summary", () => { it("formats tool failures with meta and summary", () => {
@@ -104,107 +96,3 @@ describe("compaction-safeguard tool failures", () => {
expect(section).toBe(""); expect(section).toBe("");
}); });
}); });
describe("computeAdaptiveChunkRatio", () => {
const CONTEXT_WINDOW = 200_000;
it("returns BASE_CHUNK_RATIO for normal messages", () => {
// Small messages: 1000 tokens each, well under 10% of context
const messages: AgentMessage[] = [
{ role: "user", content: "x".repeat(1000), timestamp: Date.now() },
{
role: "assistant",
content: [{ type: "text", text: "y".repeat(1000) }],
timestamp: Date.now(),
},
];
const ratio = computeAdaptiveChunkRatio(messages, CONTEXT_WINDOW);
expect(ratio).toBe(BASE_CHUNK_RATIO);
});
it("reduces ratio when average message > 10% of context", () => {
// Large messages: ~50K tokens each (25% of context)
const messages: AgentMessage[] = [
{ role: "user", content: "x".repeat(50_000 * 4), timestamp: Date.now() },
{
role: "assistant",
content: [{ type: "text", text: "y".repeat(50_000 * 4) }],
timestamp: Date.now(),
},
];
const ratio = computeAdaptiveChunkRatio(messages, CONTEXT_WINDOW);
expect(ratio).toBeLessThan(BASE_CHUNK_RATIO);
expect(ratio).toBeGreaterThanOrEqual(MIN_CHUNK_RATIO);
});
it("respects MIN_CHUNK_RATIO floor", () => {
// Very large messages that would push ratio below minimum
const messages: AgentMessage[] = [
{ role: "user", content: "x".repeat(150_000 * 4), timestamp: Date.now() },
];
const ratio = computeAdaptiveChunkRatio(messages, CONTEXT_WINDOW);
expect(ratio).toBeGreaterThanOrEqual(MIN_CHUNK_RATIO);
});
it("handles empty message array", () => {
const ratio = computeAdaptiveChunkRatio([], CONTEXT_WINDOW);
expect(ratio).toBe(BASE_CHUNK_RATIO);
});
it("handles single huge message", () => {
// Single massive message
const messages: AgentMessage[] = [
{ role: "user", content: "x".repeat(180_000 * 4), timestamp: Date.now() },
];
const ratio = computeAdaptiveChunkRatio(messages, CONTEXT_WINDOW);
expect(ratio).toBeGreaterThanOrEqual(MIN_CHUNK_RATIO);
expect(ratio).toBeLessThanOrEqual(BASE_CHUNK_RATIO);
});
});
describe("isOversizedForSummary", () => {
const CONTEXT_WINDOW = 200_000;
it("returns false for small messages", () => {
const msg: AgentMessage = {
role: "user",
content: "Hello, world!",
timestamp: Date.now(),
};
expect(isOversizedForSummary(msg, CONTEXT_WINDOW)).toBe(false);
});
it("returns true for messages > 50% of context", () => {
// Message with ~120K tokens (60% of 200K context)
// After safety margin (1.2x), effective is 144K which is > 100K (50%)
const msg: AgentMessage = {
role: "user",
content: "x".repeat(120_000 * 4),
timestamp: Date.now(),
};
expect(isOversizedForSummary(msg, CONTEXT_WINDOW)).toBe(true);
});
it("applies safety margin", () => {
// Message at exactly 50% of context before margin
// After SAFETY_MARGIN (1.2), it becomes 60% which is > 50%
const halfContextChars = (CONTEXT_WINDOW * 0.5) / SAFETY_MARGIN;
const msg: AgentMessage = {
role: "user",
content: "x".repeat(Math.floor(halfContextChars * 4)),
timestamp: Date.now(),
};
// With safety margin applied, this should be at the boundary
// The function checks if tokens * SAFETY_MARGIN > contextWindow * 0.5
const isOversized = isOversizedForSummary(msg, CONTEXT_WINDOW);
// Due to token estimation, this could be either true or false at the boundary
expect(typeof isOversized).toBe("boolean");
});
});
@@ -4,9 +4,7 @@ import { estimateTokens, generateSummary } from "@mariozechner/pi-coding-agent";
import { DEFAULT_CONTEXT_TOKENS } from "../defaults.js"; import { DEFAULT_CONTEXT_TOKENS } from "../defaults.js";
const BASE_CHUNK_RATIO = 0.4; const MAX_CHUNK_RATIO = 0.4;
const MIN_CHUNK_RATIO = 0.15;
const SAFETY_MARGIN = 1.2; // 20% buffer for estimateTokens() inaccuracy
const FALLBACK_SUMMARY = const FALLBACK_SUMMARY =
"Summary unavailable due to context limits. Older messages were truncated."; "Summary unavailable due to context limits. Older messages were truncated.";
const TURN_PREFIX_INSTRUCTIONS = const TURN_PREFIX_INSTRUCTIONS =
@@ -15,15 +13,6 @@ const TURN_PREFIX_INSTRUCTIONS =
const MAX_TOOL_FAILURES = 8; const MAX_TOOL_FAILURES = 8;
const MAX_TOOL_FAILURE_CHARS = 240; const MAX_TOOL_FAILURE_CHARS = 240;
function isAbortError(err: unknown): boolean {
if (!err || typeof err !== "object") return false;
const name = "name" in err ? String(err.name) : "";
if (name === "AbortError") return true;
const message =
"message" in err && typeof err.message === "string" ? err.message.toLowerCase() : "";
return message.includes("aborted");
}
type ToolFailure = { type ToolFailure = {
toolCallId: string; toolCallId: string;
toolName: string; toolName: string;
@@ -171,38 +160,6 @@ function chunkMessages(messages: AgentMessage[], maxTokens: number): AgentMessag
return chunks; return chunks;
} }
/**
* Compute adaptive chunk ratio based on average message size.
* When messages are large, we use smaller chunks to avoid exceeding model limits.
*/
function computeAdaptiveChunkRatio(messages: AgentMessage[], contextWindow: number): number {
if (messages.length === 0) return BASE_CHUNK_RATIO;
const totalTokens = messages.reduce((sum, m) => sum + estimateTokens(m), 0);
const avgTokens = totalTokens / messages.length;
// Apply safety margin to account for estimation inaccuracy
const safeAvgTokens = avgTokens * SAFETY_MARGIN;
const avgRatio = safeAvgTokens / contextWindow;
// If average message is > 10% of context, reduce chunk ratio
if (avgRatio > 0.1) {
const reduction = Math.min(avgRatio * 2, BASE_CHUNK_RATIO - MIN_CHUNK_RATIO);
return Math.max(MIN_CHUNK_RATIO, BASE_CHUNK_RATIO - reduction);
}
return BASE_CHUNK_RATIO;
}
/**
* Check if a single message is too large to summarize.
* If single message > 50% of context, it can't be summarized safely.
*/
function isOversizedForSummary(msg: AgentMessage, contextWindow: number): boolean {
const tokens = estimateTokens(msg) * SAFETY_MARGIN;
return tokens > contextWindow * 0.5;
}
async function summarizeChunks(params: { async function summarizeChunks(params: {
messages: AgentMessage[]; messages: AgentMessage[];
model: NonNullable<ExtensionContext["model"]>; model: NonNullable<ExtensionContext["model"]>;
@@ -235,84 +192,6 @@ async function summarizeChunks(params: {
return summary ?? "No prior history."; return summary ?? "No prior history.";
} }
/**
* Summarize with progressive fallback for handling oversized messages.
* If full summarization fails, tries partial summarization excluding oversized messages.
*/
async function summarizeWithFallback(params: {
messages: AgentMessage[];
model: NonNullable<ExtensionContext["model"]>;
apiKey: string;
signal: AbortSignal;
reserveTokens: number;
maxChunkTokens: number;
contextWindow: number;
customInstructions?: string;
previousSummary?: string;
}): Promise<string> {
const { messages, contextWindow } = params;
if (messages.length === 0) {
return params.previousSummary ?? "No prior history.";
}
// Try full summarization first
try {
return await summarizeChunks(params);
} catch (fullError) {
if (params.signal.aborted || isAbortError(fullError)) {
throw fullError;
}
console.warn(
`Full summarization failed, trying partial: ${
fullError instanceof Error ? fullError.message : String(fullError)
}`,
);
}
// Fallback 1: Summarize only small messages, note oversized ones
const smallMessages: AgentMessage[] = [];
const oversizedNotes: string[] = [];
for (const msg of messages) {
if (isOversizedForSummary(msg, contextWindow)) {
const role = (msg as { role?: string }).role ?? "message";
const tokens = estimateTokens(msg);
oversizedNotes.push(
`[Large ${role} (~${Math.round(tokens / 1000)}K tokens) omitted from summary]`,
);
} else {
smallMessages.push(msg);
}
}
if (smallMessages.length > 0) {
try {
const partialSummary = await summarizeChunks({
...params,
messages: smallMessages,
});
const notes = oversizedNotes.length > 0 ? `\n\n${oversizedNotes.join("\n")}` : "";
return partialSummary + notes;
} catch (partialError) {
if (params.signal.aborted || isAbortError(partialError)) {
throw partialError;
}
console.warn(
`Partial summarization also failed: ${
partialError instanceof Error ? partialError.message : String(partialError)
}`,
);
}
}
// Final fallback: Just note what was there
return (
`Context contained ${messages.length} messages (${oversizedNotes.length} oversized). ` +
`Summary unavailable due to size limits.`
);
}
export default function compactionSafeguardExtension(api: ExtensionAPI): void { export default function compactionSafeguardExtension(api: ExtensionAPI): void {
api.on("session_before_compact", async (event, ctx) => { api.on("session_before_compact", async (event, ctx) => {
const { preparation, customInstructions, signal } = event; const { preparation, customInstructions, signal } = event;
@@ -354,35 +233,29 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
1, 1,
Math.floor(model.contextWindow ?? DEFAULT_CONTEXT_TOKENS), Math.floor(model.contextWindow ?? DEFAULT_CONTEXT_TOKENS),
); );
const maxChunkTokens = Math.max(1, Math.floor(contextWindowTokens * MAX_CHUNK_RATIO));
// Use adaptive chunk ratio based on message sizes
const allMessages = [...preparation.messagesToSummarize, ...preparation.turnPrefixMessages];
const adaptiveRatio = computeAdaptiveChunkRatio(allMessages, contextWindowTokens);
const maxChunkTokens = Math.max(1, Math.floor(contextWindowTokens * adaptiveRatio));
const reserveTokens = Math.max(1, Math.floor(preparation.settings.reserveTokens)); const reserveTokens = Math.max(1, Math.floor(preparation.settings.reserveTokens));
const historySummary = await summarizeWithFallback({ const historySummary = await summarizeChunks({
messages: preparation.messagesToSummarize, messages: preparation.messagesToSummarize,
model, model,
apiKey, apiKey,
signal, signal,
reserveTokens, reserveTokens,
maxChunkTokens, maxChunkTokens,
contextWindow: contextWindowTokens,
customInstructions, customInstructions,
previousSummary: preparation.previousSummary, previousSummary: preparation.previousSummary,
}); });
let summary = historySummary; let summary = historySummary;
if (preparation.isSplitTurn && preparation.turnPrefixMessages.length > 0) { if (preparation.isSplitTurn && preparation.turnPrefixMessages.length > 0) {
const prefixSummary = await summarizeWithFallback({ const prefixSummary = await summarizeChunks({
messages: preparation.turnPrefixMessages, messages: preparation.turnPrefixMessages,
model, model,
apiKey, apiKey,
signal, signal,
reserveTokens, reserveTokens,
maxChunkTokens, maxChunkTokens,
contextWindow: contextWindowTokens,
customInstructions: TURN_PREFIX_INSTRUCTIONS, customInstructions: TURN_PREFIX_INSTRUCTIONS,
}); });
summary = `${historySummary}\n\n---\n\n**Turn Context (split turn):**\n\n${prefixSummary}`; summary = `${historySummary}\n\n---\n\n**Turn Context (split turn):**\n\n${prefixSummary}`;
@@ -420,9 +293,4 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void {
export const __testing = { export const __testing = {
collectToolFailures, collectToolFailures,
formatToolFailuresSection, formatToolFailuresSection,
computeAdaptiveChunkRatio,
isOversizedForSummary,
BASE_CHUNK_RATIO,
MIN_CHUNK_RATIO,
SAFETY_MARGIN,
} as const; } as const;
+15 -28
View File
@@ -32,34 +32,7 @@ describe("resolveReplyToMode", () => {
expect(resolveReplyToMode(cfg, "slack")).toBe("all"); expect(resolveReplyToMode(cfg, "slack")).toBe("all");
}); });
it("uses chat-type replyToMode overrides for Slack when configured", () => { it("uses dm-specific replyToMode for Slack DMs when configured", () => {
const cfg = {
channels: {
slack: {
replyToMode: "off",
replyToModeByChatType: { direct: "all", group: "first" },
},
},
} as ClawdbotConfig;
expect(resolveReplyToMode(cfg, "slack", null, "direct")).toBe("all");
expect(resolveReplyToMode(cfg, "slack", null, "group")).toBe("first");
expect(resolveReplyToMode(cfg, "slack", null, "channel")).toBe("off");
expect(resolveReplyToMode(cfg, "slack", null, undefined)).toBe("off");
});
it("falls back to top-level replyToMode when no chat-type override is set", () => {
const cfg = {
channels: {
slack: {
replyToMode: "first",
},
},
} as ClawdbotConfig;
expect(resolveReplyToMode(cfg, "slack", null, "direct")).toBe("first");
expect(resolveReplyToMode(cfg, "slack", null, "channel")).toBe("first");
});
it("uses legacy dm.replyToMode for direct messages when no chat-type override exists", () => {
const cfg = { const cfg = {
channels: { channels: {
slack: { slack: {
@@ -70,6 +43,20 @@ describe("resolveReplyToMode", () => {
} as ClawdbotConfig; } as ClawdbotConfig;
expect(resolveReplyToMode(cfg, "slack", null, "direct")).toBe("all"); expect(resolveReplyToMode(cfg, "slack", null, "direct")).toBe("all");
expect(resolveReplyToMode(cfg, "slack", null, "channel")).toBe("off"); expect(resolveReplyToMode(cfg, "slack", null, "channel")).toBe("off");
expect(resolveReplyToMode(cfg, "slack", null, undefined)).toBe("off");
});
it("falls back to top-level replyToMode when dm.replyToMode is not configured", () => {
const cfg = {
channels: {
slack: {
replyToMode: "first",
dm: { enabled: true },
},
},
} as ClawdbotConfig;
expect(resolveReplyToMode(cfg, "slack", null, "direct")).toBe("first");
expect(resolveReplyToMode(cfg, "slack", null, "channel")).toBe("first");
}); });
}); });
-3
View File
@@ -60,12 +60,9 @@ export type MsgContext = {
MediaPath?: string; MediaPath?: string;
MediaUrl?: string; MediaUrl?: string;
MediaType?: string; MediaType?: string;
MediaDir?: string;
MediaPaths?: string[]; MediaPaths?: string[];
MediaUrls?: string[]; MediaUrls?: string[];
MediaTypes?: string[]; MediaTypes?: string[];
OutputDir?: string;
OutputBase?: string;
/** Remote host for SCP when media lives on a different machine (e.g., clawdbot@192.168.64.3). */ /** Remote host for SCP when media lives on a different machine (e.g., clawdbot@192.168.64.3). */
MediaRemoteHost?: string; MediaRemoteHost?: string;
Transcript?: string; Transcript?: string;
+1 -6
View File
@@ -17,7 +17,7 @@ export type SlackDmConfig = {
groupEnabled?: boolean; groupEnabled?: boolean;
/** Optional allowlist for group DM channels (ids or slugs). */ /** Optional allowlist for group DM channels (ids or slugs). */
groupChannels?: Array<string | number>; groupChannels?: Array<string | number>;
/** @deprecated Prefer channels.slack.replyToModeByChatType.direct. */ /** Control reply threading for DMs (off|first|all). Overrides top-level replyToMode for DMs. */
replyToMode?: ReplyToMode; replyToMode?: ReplyToMode;
}; };
@@ -119,11 +119,6 @@ export type SlackAccountConfig = {
reactionAllowlist?: Array<string | number>; reactionAllowlist?: Array<string | number>;
/** Control reply threading when reply tags are present (off|first|all). */ /** Control reply threading when reply tags are present (off|first|all). */
replyToMode?: ReplyToMode; replyToMode?: ReplyToMode;
/**
* Optional per-chat-type reply threading overrides.
* Example: { direct: "all", group: "first", channel: "off" }.
*/
replyToModeByChatType?: Partial<Record<"direct" | "group" | "channel", ReplyToMode>>;
/** Thread session behavior. */ /** Thread session behavior. */
thread?: SlackThreadConfig; thread?: SlackThreadConfig;
actions?: SlackActionConfig; actions?: SlackActionConfig;
-10
View File
@@ -248,7 +248,6 @@ export const SlackDmSchema = z
allowFrom: z.array(z.union([z.string(), z.number()])).optional(), allowFrom: z.array(z.union([z.string(), z.number()])).optional(),
groupEnabled: z.boolean().optional(), groupEnabled: z.boolean().optional(),
groupChannels: z.array(z.union([z.string(), z.number()])).optional(), groupChannels: z.array(z.union([z.string(), z.number()])).optional(),
replyToMode: ReplyToModeSchema.optional(),
}) })
.strict() .strict()
.superRefine((value, ctx) => { .superRefine((value, ctx) => {
@@ -281,14 +280,6 @@ export const SlackThreadSchema = z
}) })
.strict(); .strict();
const SlackReplyToModeByChatTypeSchema = z
.object({
direct: ReplyToModeSchema.optional(),
group: ReplyToModeSchema.optional(),
channel: ReplyToModeSchema.optional(),
})
.strict();
export const SlackAccountSchema = z export const SlackAccountSchema = z
.object({ .object({
name: z.string().optional(), name: z.string().optional(),
@@ -316,7 +307,6 @@ export const SlackAccountSchema = z
reactionNotifications: z.enum(["off", "own", "all", "allowlist"]).optional(), reactionNotifications: z.enum(["off", "own", "all", "allowlist"]).optional(),
reactionAllowlist: z.array(z.union([z.string(), z.number()])).optional(), reactionAllowlist: z.array(z.union([z.string(), z.number()])).optional(),
replyToMode: ReplyToModeSchema.optional(), replyToMode: ReplyToModeSchema.optional(),
replyToModeByChatType: SlackReplyToModeByChatTypeSchema.optional(),
thread: SlackThreadSchema.optional(), thread: SlackThreadSchema.optional(),
actions: z actions: z
.object({ .object({
+1 -2
View File
@@ -7,8 +7,7 @@ import { normalizeAgentId } from "../routing/session-key.js";
const MAX_ASSISTANT_NAME = 50; const MAX_ASSISTANT_NAME = 50;
const MAX_ASSISTANT_AVATAR = 200; const MAX_ASSISTANT_AVATAR = 200;
export const DEFAULT_ASSISTANT_IDENTITY: AssistantIdentity = { export const DEFAULT_ASSISTANT_IDENTITY = {
agentId: "main",
name: "Assistant", name: "Assistant",
avatar: "A", avatar: "A",
}; };
+2 -5
View File
@@ -173,7 +173,7 @@ export function createAgentEventHandler({
nodeSendToSession(sessionKey, "chat", payload); nodeSendToSession(sessionKey, "chat", payload);
}; };
const shouldEmitVerboseEvents = (runId: string, sessionKey?: string) => { const shouldEmitToolEvents = (runId: string, sessionKey?: string) => {
const runContext = getAgentRunContext(runId); const runContext = getAgentRunContext(runId);
const runVerbose = normalizeVerboseLevel(runContext?.verboseLevel); const runVerbose = normalizeVerboseLevel(runContext?.verboseLevel);
if (runVerbose) return runVerbose === "on"; if (runVerbose) return runVerbose === "on";
@@ -198,10 +198,7 @@ export function createAgentEventHandler({
// Include sessionKey so Control UI can filter tool streams per session. // Include sessionKey so Control UI can filter tool streams per session.
const agentPayload = sessionKey ? { ...evt, sessionKey } : evt; const agentPayload = sessionKey ? { ...evt, sessionKey } : evt;
const last = agentRunSeq.get(evt.runId) ?? 0; const last = agentRunSeq.get(evt.runId) ?? 0;
if ( if (evt.stream === "tool" && !shouldEmitToolEvents(evt.runId, sessionKey)) {
(evt.stream === "tool" || evt.stream === "compaction") &&
!shouldEmitVerboseEvents(evt.runId, sessionKey)
) {
agentRunSeq.set(evt.runId, evt.seq); agentRunSeq.set(evt.runId, evt.seq);
return; return;
} }
@@ -77,22 +77,6 @@ describe("gateway server agent", () => {
{ {
registerAgentRunContext("run-tool-off", { sessionKey: "agent:main:main" }); registerAgentRunContext("run-tool-off", { sessionKey: "agent:main:main" });
const compactionEvtP = onceMessage(
ws,
(o) =>
o.type === "event" &&
o.event === "agent" &&
o.payload?.runId === "run-tool-off" &&
o.payload?.stream === "compaction",
1000,
);
emitAgentEvent({
runId: "run-tool-off",
stream: "compaction",
data: { phase: "start" },
});
emitAgentEvent({ emitAgentEvent({
runId: "run-tool-off", runId: "run-tool-off",
stream: "tool", stream: "tool",
@@ -114,8 +98,6 @@ describe("gateway server agent", () => {
? (evt.payload as Record<string, unknown>) ? (evt.payload as Record<string, unknown>)
: {}; : {};
expect(payload.stream).toBe("assistant"); expect(payload.stream).toBe("assistant");
await expect(compactionEvtP).rejects.toThrow("timeout");
} }
{ {
+1 -1
View File
@@ -29,7 +29,7 @@ export const DEFAULT_PROMPT: Record<MediaUnderstandingCapability, string> = {
export const DEFAULT_VIDEO_MAX_BASE64_BYTES = 70 * MB; export const DEFAULT_VIDEO_MAX_BASE64_BYTES = 70 * MB;
export const DEFAULT_AUDIO_MODELS: Record<string, string> = { export const DEFAULT_AUDIO_MODELS: Record<string, string> = {
groq: "whisper-large-v3-turbo", groq: "whisper-large-v3-turbo",
openai: "gpt-4o-mini-transcribe", openai: "whisper-1",
deepgram: "nova-3", deepgram: "nova-3",
}; };
export const CLI_OUTPUT_MAX_BUFFER = 5 * MB; export const CLI_OUTPUT_MAX_BUFFER = 5 * MB;
@@ -1,84 +0,0 @@
import type { AudioTranscriptionRequest, AudioTranscriptionResult } from "../../types.js";
import { normalizeGoogleModelId } from "../../../agents/models-config.providers.js";
import { fetchWithTimeout, normalizeBaseUrl, readErrorResponse } from "../shared.js";
export const DEFAULT_GOOGLE_AUDIO_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
const DEFAULT_GOOGLE_AUDIO_MODEL = "gemini-3-flash-preview";
const DEFAULT_GOOGLE_AUDIO_PROMPT = "Transcribe the audio.";
function resolveModel(model?: string): string {
const trimmed = model?.trim();
if (!trimmed) return DEFAULT_GOOGLE_AUDIO_MODEL;
return normalizeGoogleModelId(trimmed);
}
function resolvePrompt(prompt?: string): string {
const trimmed = prompt?.trim();
return trimmed || DEFAULT_GOOGLE_AUDIO_PROMPT;
}
export async function transcribeGeminiAudio(
params: AudioTranscriptionRequest,
): Promise<AudioTranscriptionResult> {
const fetchFn = params.fetchFn ?? fetch;
const baseUrl = normalizeBaseUrl(params.baseUrl, DEFAULT_GOOGLE_AUDIO_BASE_URL);
const model = resolveModel(params.model);
const url = `${baseUrl}/models/${model}:generateContent`;
const headers = new Headers(params.headers);
if (!headers.has("content-type")) {
headers.set("content-type", "application/json");
}
if (!headers.has("x-goog-api-key")) {
headers.set("x-goog-api-key", params.apiKey);
}
const body = {
contents: [
{
role: "user",
parts: [
{ text: resolvePrompt(params.prompt) },
{
inline_data: {
mime_type: params.mime ?? "audio/wav",
data: params.buffer.toString("base64"),
},
},
],
},
],
};
const res = await fetchWithTimeout(
url,
{
method: "POST",
headers,
body: JSON.stringify(body),
},
params.timeoutMs,
fetchFn,
);
if (!res.ok) {
const detail = await readErrorResponse(res);
const suffix = detail ? `: ${detail}` : "";
throw new Error(`Audio transcription failed (HTTP ${res.status})${suffix}`);
}
const payload = (await res.json()) as {
candidates?: Array<{
content?: { parts?: Array<{ text?: string }> };
}>;
};
const parts = payload.candidates?.[0]?.content?.parts ?? [];
const text = parts
.map((part) => part?.text?.trim())
.filter(Boolean)
.join("\n");
if (!text) {
throw new Error("Audio transcription response missing text");
}
return { text, model };
}
@@ -1,12 +1,10 @@
import type { MediaUnderstandingProvider } from "../../types.js"; import type { MediaUnderstandingProvider } from "../../types.js";
import { describeImageWithModel } from "../image.js"; import { describeImageWithModel } from "../image.js";
import { transcribeGeminiAudio } from "./audio.js";
import { describeGeminiVideo } from "./video.js"; import { describeGeminiVideo } from "./video.js";
export const googleProvider: MediaUnderstandingProvider = { export const googleProvider: MediaUnderstandingProvider = {
id: "google", id: "google",
capabilities: ["image", "audio", "video"], capabilities: ["image", "audio", "video"],
describeImage: describeImageWithModel, describeImage: describeImageWithModel,
transcribeAudio: transcribeGeminiAudio,
describeVideo: describeGeminiVideo, describeVideo: describeGeminiVideo,
}; };
@@ -59,7 +59,7 @@ describe("transcribeOpenAiCompatibleAudio", () => {
fetchFn, fetchFn,
}); });
expect(result.model).toBe("gpt-4o-mini-transcribe"); expect(result.model).toBe("whisper-1");
expect(result.text).toBe("hello"); expect(result.text).toBe("hello");
expect(seenUrl).toBe("https://api.example.com/v1/audio/transcriptions"); expect(seenUrl).toBe("https://api.example.com/v1/audio/transcriptions");
expect(seenInit?.method).toBe("POST"); expect(seenInit?.method).toBe("POST");
@@ -71,7 +71,7 @@ describe("transcribeOpenAiCompatibleAudio", () => {
const form = seenInit?.body as FormData; const form = seenInit?.body as FormData;
expect(form).toBeInstanceOf(FormData); expect(form).toBeInstanceOf(FormData);
expect(form.get("model")).toBe("gpt-4o-mini-transcribe"); expect(form.get("model")).toBe("whisper-1");
expect(form.get("language")).toBe("en"); expect(form.get("language")).toBe("en");
expect(form.get("prompt")).toBe("hello"); expect(form.get("prompt")).toBe("hello");
const file = form.get("file") as Blob | { type?: string; name?: string } | null; const file = form.get("file") as Blob | { type?: string; name?: string } | null;
@@ -4,7 +4,7 @@ import type { AudioTranscriptionRequest, AudioTranscriptionResult } from "../../
import { fetchWithTimeout, normalizeBaseUrl, readErrorResponse } from "../shared.js"; import { fetchWithTimeout, normalizeBaseUrl, readErrorResponse } from "../shared.js";
export const DEFAULT_OPENAI_AUDIO_BASE_URL = "https://api.openai.com/v1"; export const DEFAULT_OPENAI_AUDIO_BASE_URL = "https://api.openai.com/v1";
const DEFAULT_OPENAI_AUDIO_MODEL = "gpt-4o-mini-transcribe"; const DEFAULT_OPENAI_AUDIO_MODEL = "whisper-1";
function resolveModel(model?: string): string { function resolveModel(model?: string): string {
const trimmed = model?.trim(); const trimmed = model?.trim();
@@ -15,8 +15,6 @@ import {
describe("runCapability auto audio entries", () => { describe("runCapability auto audio entries", () => {
it("uses provider keys to auto-enable audio transcription", async () => { it("uses provider keys to auto-enable audio transcription", async () => {
const originalPath = process.env.PATH;
process.env.PATH = "/usr/bin:/bin";
const tmpPath = path.join(os.tmpdir(), `clawdbot-auto-audio-${Date.now()}.wav`); const tmpPath = path.join(os.tmpdir(), `clawdbot-auto-audio-${Date.now()}.wav`);
await fs.writeFile(tmpPath, Buffer.from("RIFF")); await fs.writeFile(tmpPath, Buffer.from("RIFF"));
const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" }; const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" };
@@ -56,18 +54,15 @@ describe("runCapability auto audio entries", () => {
providerRegistry, providerRegistry,
}); });
expect(result.outputs[0]?.text).toBe("ok"); expect(result.outputs[0]?.text).toBe("ok");
expect(seenModel).toBe("gpt-4o-mini-transcribe"); expect(seenModel).toBe("whisper-1");
expect(result.decision.outcome).toBe("success"); expect(result.decision.outcome).toBe("success");
} finally { } finally {
process.env.PATH = originalPath;
await cache.cleanup(); await cache.cleanup();
await fs.unlink(tmpPath).catch(() => {}); await fs.unlink(tmpPath).catch(() => {});
} }
}); });
it("skips auto audio when disabled", async () => { it("skips auto audio when disabled", async () => {
const originalPath = process.env.PATH;
process.env.PATH = "/usr/bin:/bin";
const tmpPath = path.join(os.tmpdir(), `clawdbot-auto-audio-${Date.now()}.wav`); const tmpPath = path.join(os.tmpdir(), `clawdbot-auto-audio-${Date.now()}.wav`);
await fs.writeFile(tmpPath, Buffer.from("RIFF")); await fs.writeFile(tmpPath, Buffer.from("RIFF"));
const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" }; const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" };
@@ -112,7 +107,6 @@ describe("runCapability auto audio entries", () => {
expect(result.outputs).toHaveLength(0); expect(result.outputs).toHaveLength(0);
expect(result.decision.outcome).toBe("disabled"); expect(result.decision.outcome).toBe("disabled");
} finally { } finally {
process.env.PATH = originalPath;
await cache.cleanup(); await cache.cleanup();
await fs.unlink(tmpPath).catch(() => {}); await fs.unlink(tmpPath).catch(() => {});
} }
+38 -472
View File
@@ -1,8 +1,3 @@
import { constants as fsConstants } from "node:fs";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import type { ClawdbotConfig } from "../config/config.js"; import type { ClawdbotConfig } from "../config/config.js";
import type { MsgContext } from "../auto-reply/templating.js"; import type { MsgContext } from "../auto-reply/templating.js";
import { applyTemplate } from "../auto-reply/templating.js"; import { applyTemplate } from "../auto-reply/templating.js";
@@ -21,9 +16,9 @@ import {
} from "./defaults.js"; } from "./defaults.js";
import { isMediaUnderstandingSkipError, MediaUnderstandingSkipError } from "./errors.js"; import { isMediaUnderstandingSkipError, MediaUnderstandingSkipError } from "./errors.js";
import { import {
resolveEntriesWithActiveFallback,
resolveMaxBytes, resolveMaxBytes,
resolveMaxChars, resolveMaxChars,
resolveModelEntries,
resolvePrompt, resolvePrompt,
resolveScopeDecision, resolveScopeDecision,
resolveTimeoutMs, resolveTimeoutMs,
@@ -44,15 +39,7 @@ import {
import { describeImageWithModel } from "./providers/image.js"; import { describeImageWithModel } from "./providers/image.js";
import { estimateBase64Size, resolveVideoMaxBase64Bytes } from "./video.js"; import { estimateBase64Size, resolveVideoMaxBase64Bytes } from "./video.js";
const AUTO_AUDIO_KEY_PROVIDERS = ["openai", "groq", "deepgram", "google"] as const; const AUTO_AUDIO_PROVIDERS = ["openai", "groq", "deepgram"] as const;
const AUTO_IMAGE_KEY_PROVIDERS = ["openai", "anthropic", "google", "minimax"] as const;
const AUTO_VIDEO_KEY_PROVIDERS = ["google"] as const;
const DEFAULT_IMAGE_MODELS: Record<string, string> = {
openai: "gpt-5-mini",
anthropic: "claude-opus-4-5",
google: "gemini-3-flash-preview",
minimax: "MiniMax-VL-01",
};
export type ActiveMediaModel = { export type ActiveMediaModel = {
provider: string; provider: string;
@@ -80,363 +67,27 @@ export function createMediaAttachmentCache(attachments: MediaAttachment[]): Medi
return new MediaAttachmentCache(attachments); return new MediaAttachmentCache(attachments);
} }
const binaryCache = new Map<string, Promise<string | null>>(); async function resolveAutoAudioEntries(params: {
const geminiProbeCache = new Map<string, Promise<boolean>>();
function expandHomeDir(value: string): string {
if (!value.startsWith("~")) return value;
const home = os.homedir();
if (value === "~") return home;
if (value.startsWith("~/")) return path.join(home, value.slice(2));
return value;
}
function hasPathSeparator(value: string): boolean {
return value.includes("/") || value.includes("\\");
}
function candidateBinaryNames(name: string): string[] {
if (process.platform !== "win32") return [name];
const ext = path.extname(name);
if (ext) return [name];
const pathext = (process.env.PATHEXT ?? ".EXE;.CMD;.BAT;.COM")
.split(";")
.map((item) => item.trim())
.filter(Boolean)
.map((item) => (item.startsWith(".") ? item : `.${item}`));
const unique = Array.from(new Set(pathext));
return [name, ...unique.map((item) => `${name}${item}`)];
}
async function isExecutable(filePath: string): Promise<boolean> {
try {
const stat = await fs.stat(filePath);
if (!stat.isFile()) return false;
if (process.platform === "win32") return true;
await fs.access(filePath, fsConstants.X_OK);
return true;
} catch {
return false;
}
}
async function findBinary(name: string): Promise<string | null> {
const cached = binaryCache.get(name);
if (cached) return cached;
const resolved = (async () => {
const direct = expandHomeDir(name.trim());
if (direct && hasPathSeparator(direct)) {
for (const candidate of candidateBinaryNames(direct)) {
if (await isExecutable(candidate)) return candidate;
}
}
const searchName = name.trim();
if (!searchName) return null;
const pathEntries = (process.env.PATH ?? "").split(path.delimiter);
const candidates = candidateBinaryNames(searchName);
for (const entryRaw of pathEntries) {
const entry = expandHomeDir(entryRaw.trim().replace(/^"(.*)"$/, "$1"));
if (!entry) continue;
for (const candidate of candidates) {
const fullPath = path.join(entry, candidate);
if (await isExecutable(fullPath)) return fullPath;
}
}
return null;
})();
binaryCache.set(name, resolved);
return resolved;
}
async function hasBinary(name: string): Promise<boolean> {
return Boolean(await findBinary(name));
}
async function fileExists(filePath?: string | null): Promise<boolean> {
if (!filePath) return false;
try {
await fs.stat(filePath);
return true;
} catch {
return false;
}
}
function extractLastJsonObject(raw: string): unknown {
const trimmed = raw.trim();
const start = trimmed.lastIndexOf("{");
if (start === -1) return null;
const slice = trimmed.slice(start);
try {
return JSON.parse(slice);
} catch {
return null;
}
}
function extractGeminiResponse(raw: string): string | null {
const payload = extractLastJsonObject(raw);
if (!payload || typeof payload !== "object") return null;
const response = (payload as { response?: unknown }).response;
if (typeof response !== "string") return null;
const trimmed = response.trim();
return trimmed || null;
}
function extractSherpaOnnxText(raw: string): string | null {
const tryParse = (value: string): string | null => {
const trimmed = value.trim();
if (!trimmed) return null;
const head = trimmed[0];
if (head !== "{" && head !== '"') return null;
try {
const parsed = JSON.parse(trimmed) as unknown;
if (typeof parsed === "string") {
return tryParse(parsed);
}
if (parsed && typeof parsed === "object") {
const text = (parsed as { text?: unknown }).text;
if (typeof text === "string" && text.trim()) {
return text.trim();
}
}
} catch {}
return null;
};
const direct = tryParse(raw);
if (direct) return direct;
const lines = raw
.split("\n")
.map((line) => line.trim())
.filter(Boolean);
for (let i = lines.length - 1; i >= 0; i -= 1) {
const parsed = tryParse(lines[i] ?? "");
if (parsed) return parsed;
}
return null;
}
async function probeGeminiCli(): Promise<boolean> {
const cached = geminiProbeCache.get("gemini");
if (cached) return cached;
const resolved = (async () => {
if (!(await hasBinary("gemini"))) return false;
try {
const { stdout } = await runExec("gemini", ["--output-format", "json", "ok"], {
timeoutMs: 8000,
});
return Boolean(extractGeminiResponse(stdout) ?? stdout.toLowerCase().includes("ok"));
} catch {
return false;
}
})();
geminiProbeCache.set("gemini", resolved);
return resolved;
}
async function resolveLocalWhisperCppEntry(): Promise<MediaUnderstandingModelConfig | null> {
if (!(await hasBinary("whisper-cli"))) return null;
const envModel = process.env.WHISPER_CPP_MODEL?.trim();
const defaultModel = "/opt/homebrew/share/whisper-cpp/for-tests-ggml-tiny.bin";
const modelPath = envModel && (await fileExists(envModel)) ? envModel : defaultModel;
if (!(await fileExists(modelPath))) return null;
return {
type: "cli",
command: "whisper-cli",
args: ["-m", modelPath, "-otxt", "-of", "{{OutputBase}}", "-np", "-nt", "{{MediaPath}}"],
};
}
async function resolveLocalWhisperEntry(): Promise<MediaUnderstandingModelConfig | null> {
if (!(await hasBinary("whisper"))) return null;
return {
type: "cli",
command: "whisper",
args: [
"--model",
"turbo",
"--output_format",
"txt",
"--output_dir",
"{{OutputDir}}",
"--verbose",
"False",
"{{MediaPath}}",
],
};
}
async function resolveSherpaOnnxEntry(): Promise<MediaUnderstandingModelConfig | null> {
if (!(await hasBinary("sherpa-onnx-offline"))) return null;
const modelDir = process.env.SHERPA_ONNX_MODEL_DIR?.trim();
if (!modelDir) return null;
const tokens = path.join(modelDir, "tokens.txt");
const encoder = path.join(modelDir, "encoder.onnx");
const decoder = path.join(modelDir, "decoder.onnx");
const joiner = path.join(modelDir, "joiner.onnx");
if (!(await fileExists(tokens))) return null;
if (!(await fileExists(encoder))) return null;
if (!(await fileExists(decoder))) return null;
if (!(await fileExists(joiner))) return null;
return {
type: "cli",
command: "sherpa-onnx-offline",
args: [
`--tokens=${tokens}`,
`--encoder=${encoder}`,
`--decoder=${decoder}`,
`--joiner=${joiner}`,
"{{MediaPath}}",
],
};
}
async function resolveLocalAudioEntry(): Promise<MediaUnderstandingModelConfig | null> {
const sherpa = await resolveSherpaOnnxEntry();
if (sherpa) return sherpa;
const whisperCpp = await resolveLocalWhisperCppEntry();
if (whisperCpp) return whisperCpp;
return await resolveLocalWhisperEntry();
}
async function resolveGeminiCliEntry(
_capability: MediaUnderstandingCapability,
): Promise<MediaUnderstandingModelConfig | null> {
if (!(await probeGeminiCli())) return null;
return {
type: "cli",
command: "gemini",
args: [
"--output-format",
"json",
"--allowed-tools",
"read_many_files",
"--include-directories",
"{{MediaDir}}",
"{{Prompt}}",
"Use read_many_files to read {{MediaPath}} and respond with only the text output.",
],
};
}
async function resolveKeyEntry(params: {
cfg: ClawdbotConfig; cfg: ClawdbotConfig;
agentDir?: string; agentDir?: string;
providerRegistry: ProviderRegistry; providerRegistry: ProviderRegistry;
capability: MediaUnderstandingCapability;
activeModel?: ActiveMediaModel;
}): Promise<MediaUnderstandingModelConfig | null> {
const { cfg, agentDir, providerRegistry, capability } = params;
const checkProvider = async (
providerId: string,
model?: string,
): Promise<MediaUnderstandingModelConfig | null> => {
const provider = getMediaUnderstandingProvider(providerId, providerRegistry);
if (!provider) return null;
if (capability === "audio" && !provider.transcribeAudio) return null;
if (capability === "image" && !provider.describeImage) return null;
if (capability === "video" && !provider.describeVideo) return null;
try {
await resolveApiKeyForProvider({ provider: providerId, cfg, agentDir });
return { type: "provider" as const, provider: providerId, model };
} catch {
return null;
}
};
if (capability === "image") {
const activeProvider = params.activeModel?.provider?.trim();
if (activeProvider) {
const activeEntry = await checkProvider(activeProvider, params.activeModel?.model);
if (activeEntry) return activeEntry;
}
for (const providerId of AUTO_IMAGE_KEY_PROVIDERS) {
const model = DEFAULT_IMAGE_MODELS[providerId];
const entry = await checkProvider(providerId, model);
if (entry) return entry;
}
return null;
}
if (capability === "video") {
const activeProvider = params.activeModel?.provider?.trim();
if (activeProvider) {
const activeEntry = await checkProvider(activeProvider, params.activeModel?.model);
if (activeEntry) return activeEntry;
}
for (const providerId of AUTO_VIDEO_KEY_PROVIDERS) {
const entry = await checkProvider(providerId, undefined);
if (entry) return entry;
}
return null;
}
const activeProvider = params.activeModel?.provider?.trim();
if (activeProvider) {
const activeEntry = await checkProvider(activeProvider, params.activeModel?.model);
if (activeEntry) return activeEntry;
}
for (const providerId of AUTO_AUDIO_KEY_PROVIDERS) {
const entry = await checkProvider(providerId, undefined);
if (entry) return entry;
}
return null;
}
async function resolveAutoEntries(params: {
cfg: ClawdbotConfig;
agentDir?: string;
providerRegistry: ProviderRegistry;
capability: MediaUnderstandingCapability;
activeModel?: ActiveMediaModel;
}): Promise<MediaUnderstandingModelConfig[]> { }): Promise<MediaUnderstandingModelConfig[]> {
const activeEntry = await resolveActiveModelEntry(params); const entries: MediaUnderstandingModelConfig[] = [];
if (activeEntry) return [activeEntry]; for (const providerId of AUTO_AUDIO_PROVIDERS) {
if (params.capability === "audio") { const provider = getMediaUnderstandingProvider(providerId, params.providerRegistry);
const localAudio = await resolveLocalAudioEntry(); if (!provider?.transcribeAudio) continue;
if (localAudio) return [localAudio]; try {
await resolveApiKeyForProvider({
provider: providerId,
cfg: params.cfg,
agentDir: params.agentDir,
});
entries.push({ type: "provider", provider: providerId });
} catch {
continue;
}
} }
const gemini = await resolveGeminiCliEntry(params.capability); return entries;
if (gemini) return [gemini];
const keys = await resolveKeyEntry(params);
if (keys) return [keys];
return [];
}
async function resolveActiveModelEntry(params: {
cfg: ClawdbotConfig;
agentDir?: string;
providerRegistry: ProviderRegistry;
capability: MediaUnderstandingCapability;
activeModel?: ActiveMediaModel;
}): Promise<MediaUnderstandingModelConfig | null> {
const activeProviderRaw = params.activeModel?.provider?.trim();
if (!activeProviderRaw) return null;
const providerId = normalizeMediaProviderId(activeProviderRaw);
if (!providerId) return null;
const provider = getMediaUnderstandingProvider(providerId, params.providerRegistry);
if (!provider) return null;
if (params.capability === "audio" && !provider.transcribeAudio) return null;
if (params.capability === "image" && !provider.describeImage) return null;
if (params.capability === "video" && !provider.describeVideo) return null;
try {
await resolveApiKeyForProvider({
provider: providerId,
cfg: params.cfg,
agentDir: params.agentDir,
});
} catch {
return null;
}
return {
type: "provider",
provider: providerId,
model: params.activeModel?.model,
};
} }
function trimOutput(text: string, maxChars?: number): string { function trimOutput(text: string, maxChars?: number): string {
@@ -445,74 +96,6 @@ function trimOutput(text: string, maxChars?: number): string {
return trimmed.slice(0, maxChars).trim(); return trimmed.slice(0, maxChars).trim();
} }
function commandBase(command: string): string {
return path.parse(command).name;
}
function findArgValue(args: string[], keys: string[]): string | undefined {
for (let i = 0; i < args.length; i += 1) {
if (keys.includes(args[i] ?? "")) {
const value = args[i + 1];
if (value) return value;
}
}
return undefined;
}
function hasArg(args: string[], keys: string[]): boolean {
return args.some((arg) => keys.includes(arg));
}
function resolveWhisperOutputPath(args: string[], mediaPath: string): string | null {
const outputDir = findArgValue(args, ["--output_dir", "-o"]);
const outputFormat = findArgValue(args, ["--output_format"]);
if (!outputDir || !outputFormat) return null;
const formats = outputFormat.split(",").map((value) => value.trim());
if (!formats.includes("txt")) return null;
const base = path.parse(mediaPath).name;
return path.join(outputDir, `${base}.txt`);
}
function resolveWhisperCppOutputPath(args: string[]): string | null {
if (!hasArg(args, ["-otxt", "--output-txt"])) return null;
const outputBase = findArgValue(args, ["-of", "--output-file"]);
if (!outputBase) return null;
return `${outputBase}.txt`;
}
async function resolveCliOutput(params: {
command: string;
args: string[];
stdout: string;
mediaPath: string;
}): Promise<string> {
const commandId = commandBase(params.command);
const fileOutput =
commandId === "whisper-cli"
? resolveWhisperCppOutputPath(params.args)
: commandId === "whisper"
? resolveWhisperOutputPath(params.args, params.mediaPath)
: null;
if (fileOutput && (await fileExists(fileOutput))) {
try {
const content = await fs.readFile(fileOutput, "utf8");
if (content.trim()) return content.trim();
} catch {}
}
if (commandId === "gemini") {
const response = extractGeminiResponse(params.stdout);
if (response) return response;
}
if (commandId === "sherpa-onnx-offline") {
const response = extractSherpaOnnxText(params.stdout);
if (response) return response;
}
return params.stdout.trim();
}
type ProviderQuery = Record<string, string | number | boolean>; type ProviderQuery = Record<string, string | number | boolean>;
function normalizeProviderQuery( function normalizeProviderQuery(
@@ -839,48 +422,32 @@ async function runCliEntry(params: {
maxBytes, maxBytes,
timeoutMs, timeoutMs,
}); });
const outputDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-media-cli-"));
const mediaPath = pathResult.path;
const outputBase = path.join(outputDir, path.parse(mediaPath).name);
const templCtx: MsgContext = { const templCtx: MsgContext = {
...ctx, ...ctx,
MediaPath: mediaPath, MediaPath: pathResult.path,
MediaDir: path.dirname(mediaPath),
OutputDir: outputDir,
OutputBase: outputBase,
Prompt: prompt, Prompt: prompt,
MaxChars: maxChars, MaxChars: maxChars,
}; };
const argv = [command, ...args].map((part, index) => const argv = [command, ...args].map((part, index) =>
index === 0 ? part : applyTemplate(part, templCtx), index === 0 ? part : applyTemplate(part, templCtx),
); );
try { if (shouldLogVerbose()) {
if (shouldLogVerbose()) { logVerbose(`Media understanding via CLI: ${argv.join(" ")}`);
logVerbose(`Media understanding via CLI: ${argv.join(" ")}`);
}
const { stdout } = await runExec(argv[0], argv.slice(1), {
timeoutMs,
maxBuffer: CLI_OUTPUT_MAX_BUFFER,
});
const resolved = await resolveCliOutput({
command,
args: argv.slice(1),
stdout,
mediaPath,
});
const text = trimOutput(resolved, maxChars);
if (!text) return null;
return {
kind: capability === "audio" ? "audio.transcription" : `${capability}.description`,
attachmentIndex: params.attachmentIndex,
text,
provider: "cli",
model: command,
};
} finally {
await fs.rm(outputDir, { recursive: true, force: true }).catch(() => {});
} }
const { stdout } = await runExec(argv[0], argv.slice(1), {
timeoutMs,
maxBuffer: CLI_OUTPUT_MAX_BUFFER,
});
const text = trimOutput(stdout, maxChars);
if (!text) return null;
return {
kind: capability === "audio" ? "audio.transcription" : `${capability}.description`,
attachmentIndex: params.attachmentIndex,
text,
provider: "cli",
model: command,
};
} }
async function runAttachmentEntries(params: { async function runAttachmentEntries(params: {
@@ -1014,20 +581,19 @@ export async function runCapability(params: {
}; };
} }
const entries = resolveModelEntries({ const entries = resolveEntriesWithActiveFallback({
cfg, cfg,
capability, capability,
config, config,
providerRegistry: params.providerRegistry, providerRegistry: params.providerRegistry,
activeModel: params.activeModel,
}); });
let resolvedEntries = entries; let resolvedEntries = entries;
if (resolvedEntries.length === 0) { if (resolvedEntries.length === 0 && capability === "audio") {
resolvedEntries = await resolveAutoEntries({ resolvedEntries = await resolveAutoAudioEntries({
cfg, cfg,
agentDir: params.agentDir, agentDir: params.agentDir,
providerRegistry: params.providerRegistry, providerRegistry: params.providerRegistry,
capability,
activeModel: params.activeModel,
}); });
} }
if (resolvedEntries.length === 0) { if (resolvedEntries.length === 0) {
+1 -8
View File
@@ -1,6 +1,5 @@
import type { ClawdbotConfig } from "../config/config.js"; import type { ClawdbotConfig } from "../config/config.js";
import type { SlackAccountConfig } from "../config/types.js"; import type { SlackAccountConfig } from "../config/types.js";
import { normalizeChatType } from "../channels/chat-type.js";
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js"; import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js";
import { resolveSlackAppToken, resolveSlackBotToken } from "./token.js"; import { resolveSlackAppToken, resolveSlackBotToken } from "./token.js";
@@ -21,7 +20,6 @@ export type ResolvedSlackAccount = {
reactionNotifications?: SlackAccountConfig["reactionNotifications"]; reactionNotifications?: SlackAccountConfig["reactionNotifications"];
reactionAllowlist?: SlackAccountConfig["reactionAllowlist"]; reactionAllowlist?: SlackAccountConfig["reactionAllowlist"];
replyToMode?: SlackAccountConfig["replyToMode"]; replyToMode?: SlackAccountConfig["replyToMode"];
replyToModeByChatType?: SlackAccountConfig["replyToModeByChatType"];
actions?: SlackAccountConfig["actions"]; actions?: SlackAccountConfig["actions"];
slashCommand?: SlackAccountConfig["slashCommand"]; slashCommand?: SlackAccountConfig["slashCommand"];
dm?: SlackAccountConfig["dm"]; dm?: SlackAccountConfig["dm"];
@@ -97,7 +95,6 @@ export function resolveSlackAccount(params: {
reactionNotifications: merged.reactionNotifications, reactionNotifications: merged.reactionNotifications,
reactionAllowlist: merged.reactionAllowlist, reactionAllowlist: merged.reactionAllowlist,
replyToMode: merged.replyToMode, replyToMode: merged.replyToMode,
replyToModeByChatType: merged.replyToModeByChatType,
actions: merged.actions, actions: merged.actions,
slashCommand: merged.slashCommand, slashCommand: merged.slashCommand,
dm: merged.dm, dm: merged.dm,
@@ -115,11 +112,7 @@ export function resolveSlackReplyToMode(
account: ResolvedSlackAccount, account: ResolvedSlackAccount,
chatType?: string | null, chatType?: string | null,
): "off" | "first" | "all" { ): "off" | "first" | "all" {
const normalized = normalizeChatType(chatType ?? undefined); if (chatType === "direct" && account.dm?.replyToMode !== undefined) {
if (normalized && account.replyToModeByChatType?.[normalized] !== undefined) {
return account.replyToModeByChatType[normalized] ?? "off";
}
if (normalized === "direct" && account.dm?.replyToMode !== undefined) {
return account.dm.replyToMode; return account.dm.replyToMode;
} }
return account.replyToMode ?? "off"; return account.replyToMode ?? "off";
+35 -51
View File
@@ -20,57 +20,7 @@ describe("buildSlackThreadingToolContext", () => {
expect(result.replyToMode).toBe("first"); expect(result.replyToMode).toBe("first");
}); });
it("uses chat-type replyToMode overrides for direct messages when configured", () => { it("uses dm.replyToMode for direct messages when configured", () => {
const cfg = {
channels: {
slack: {
replyToMode: "off",
replyToModeByChatType: { direct: "all" },
},
},
} as ClawdbotConfig;
const result = buildSlackThreadingToolContext({
cfg,
accountId: null,
context: { ChatType: "direct" },
});
expect(result.replyToMode).toBe("all");
});
it("uses top-level replyToMode for channels when no channel override is set", () => {
const cfg = {
channels: {
slack: {
replyToMode: "off",
replyToModeByChatType: { direct: "all" },
},
},
} as ClawdbotConfig;
const result = buildSlackThreadingToolContext({
cfg,
accountId: null,
context: { ChatType: "channel" },
});
expect(result.replyToMode).toBe("off");
});
it("falls back to top-level when no chat-type override is set", () => {
const cfg = {
channels: {
slack: {
replyToMode: "first",
},
},
} as ClawdbotConfig;
const result = buildSlackThreadingToolContext({
cfg,
accountId: null,
context: { ChatType: "direct" },
});
expect(result.replyToMode).toBe("first");
});
it("uses legacy dm.replyToMode for direct messages when no chat-type override exists", () => {
const cfg = { const cfg = {
channels: { channels: {
slack: { slack: {
@@ -87,6 +37,40 @@ describe("buildSlackThreadingToolContext", () => {
expect(result.replyToMode).toBe("all"); expect(result.replyToMode).toBe("all");
}); });
it("uses top-level replyToMode for channels even when dm.replyToMode is set", () => {
const cfg = {
channels: {
slack: {
replyToMode: "off",
dm: { replyToMode: "all" },
},
},
} as ClawdbotConfig;
const result = buildSlackThreadingToolContext({
cfg,
accountId: null,
context: { ChatType: "channel" },
});
expect(result.replyToMode).toBe("off");
});
it("falls back to top-level when dm.replyToMode is not set", () => {
const cfg = {
channels: {
slack: {
replyToMode: "first",
dm: { enabled: true },
},
},
} as ClawdbotConfig;
const result = buildSlackThreadingToolContext({
cfg,
accountId: null,
context: { ChatType: "direct" },
});
expect(result.replyToMode).toBe("first");
});
it("uses all mode when ThreadLabel is present", () => { it("uses all mode when ThreadLabel is present", () => {
const cfg = { const cfg = {
channels: { channels: {
-169
View File
@@ -1,169 +0,0 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { ClawdbotConfig } from "../src/config/config.js";
import type { MsgContext } from "../src/auto-reply/templating.js";
const makeTempDir = async (prefix: string) => await fs.mkdtemp(path.join(os.tmpdir(), prefix));
const writeExecutable = async (dir: string, name: string, content: string) => {
const filePath = path.join(dir, name);
await fs.writeFile(filePath, content, { mode: 0o755 });
return filePath;
};
const makeTempMedia = async (ext: string) => {
const dir = await makeTempDir("clawdbot-media-e2e-");
const filePath = path.join(dir, `sample${ext}`);
await fs.writeFile(filePath, "audio");
return { dir, filePath };
};
const loadApply = async () => {
vi.resetModules();
return await import("../src/media-understanding/apply.js");
};
const envSnapshot = () => ({
PATH: process.env.PATH,
SHERPA_ONNX_MODEL_DIR: process.env.SHERPA_ONNX_MODEL_DIR,
WHISPER_CPP_MODEL: process.env.WHISPER_CPP_MODEL,
});
const restoreEnv = (snapshot: ReturnType<typeof envSnapshot>) => {
process.env.PATH = snapshot.PATH;
process.env.SHERPA_ONNX_MODEL_DIR = snapshot.SHERPA_ONNX_MODEL_DIR;
process.env.WHISPER_CPP_MODEL = snapshot.WHISPER_CPP_MODEL;
};
describe("media understanding auto-detect (e2e)", () => {
let tempPaths: string[] = [];
afterEach(async () => {
for (const p of tempPaths) {
await fs.rm(p, { recursive: true, force: true }).catch(() => {});
}
tempPaths = [];
});
it("uses sherpa-onnx-offline when available", async () => {
const snapshot = envSnapshot();
try {
const binDir = await makeTempDir("clawdbot-bin-sherpa-");
const modelDir = await makeTempDir("clawdbot-sherpa-model-");
tempPaths.push(binDir, modelDir);
await fs.writeFile(path.join(modelDir, "tokens.txt"), "a");
await fs.writeFile(path.join(modelDir, "encoder.onnx"), "a");
await fs.writeFile(path.join(modelDir, "decoder.onnx"), "a");
await fs.writeFile(path.join(modelDir, "joiner.onnx"), "a");
await writeExecutable(
binDir,
"sherpa-onnx-offline",
"#!/usr/bin/env bash\n" + 'echo "{\\"text\\":\\"sherpa ok\\"}"\n',
);
process.env.PATH = `${binDir}:/usr/bin:/bin`;
process.env.SHERPA_ONNX_MODEL_DIR = modelDir;
const { filePath } = await makeTempMedia(".wav");
tempPaths.push(path.dirname(filePath));
const { applyMediaUnderstanding } = await loadApply();
const ctx: MsgContext = {
Body: "<media:audio>",
MediaPath: filePath,
MediaType: "audio/wav",
};
const cfg: ClawdbotConfig = { tools: { media: { audio: {} } } };
await applyMediaUnderstanding({ ctx, cfg });
expect(ctx.Transcript).toBe("sherpa ok");
} finally {
restoreEnv(snapshot);
}
});
it("uses whisper-cli when sherpa is missing", async () => {
const snapshot = envSnapshot();
try {
const binDir = await makeTempDir("clawdbot-bin-whispercpp-");
const modelDir = await makeTempDir("clawdbot-whispercpp-model-");
tempPaths.push(binDir, modelDir);
const modelPath = path.join(modelDir, "tiny.bin");
await fs.writeFile(modelPath, "model");
await writeExecutable(
binDir,
"whisper-cli",
"#!/usr/bin/env bash\n" +
'out=""\n' +
'prev=""\n' +
'for arg in "$@"; do\n' +
' if [ "$prev" = "-of" ]; then out="$arg"; break; fi\n' +
' prev="$arg"\n' +
"done\n" +
'if [ -n "$out" ]; then echo \'whisper cpp ok\' > "${out}.txt"; fi\n',
);
process.env.PATH = `${binDir}:/usr/bin:/bin`;
process.env.WHISPER_CPP_MODEL = modelPath;
const { filePath } = await makeTempMedia(".wav");
tempPaths.push(path.dirname(filePath));
const { applyMediaUnderstanding } = await loadApply();
const ctx: MsgContext = {
Body: "<media:audio>",
MediaPath: filePath,
MediaType: "audio/wav",
};
const cfg: ClawdbotConfig = { tools: { media: { audio: {} } } };
await applyMediaUnderstanding({ ctx, cfg });
expect(ctx.Transcript).toBe("whisper cpp ok");
} finally {
restoreEnv(snapshot);
}
});
it("uses gemini CLI for images when available", async () => {
const snapshot = envSnapshot();
try {
const binDir = await makeTempDir("clawdbot-bin-gemini-");
tempPaths.push(binDir);
await writeExecutable(
binDir,
"gemini",
"#!/usr/bin/env bash\necho '{" + '\\"response\\":\\"gemini ok\\"' + "}'\n",
);
process.env.PATH = `${binDir}:/usr/bin:/bin`;
const { filePath } = await makeTempMedia(".png");
tempPaths.push(path.dirname(filePath));
const { applyMediaUnderstanding } = await loadApply();
const ctx: MsgContext = {
Body: "<media:image>",
MediaPath: filePath,
MediaType: "image/png",
};
const cfg: ClawdbotConfig = { tools: { media: { image: {} } } };
await applyMediaUnderstanding({ ctx, cfg });
expect(ctx.Body).toContain("gemini ok");
} finally {
restoreEnv(snapshot);
}
});
});
-45
View File
@@ -415,51 +415,6 @@
color: var(--danger); color: var(--danger);
} }
.callout.info {
border-color: rgba(92, 156, 255, 0.4);
color: var(--accent);
}
.callout.success {
border-color: rgba(92, 255, 128, 0.4);
color: var(--positive, #5cff80);
}
.compaction-indicator {
font-size: 13px;
padding: 8px 12px;
margin-bottom: 8px;
animation: compaction-fade-in 0.2s ease-out;
}
.compaction-indicator--active {
animation: compaction-pulse 1.5s ease-in-out infinite;
}
.compaction-indicator--complete {
animation: compaction-fade-in 0.2s ease-out;
}
@keyframes compaction-fade-in {
from {
opacity: 0;
transform: translateY(-4px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes compaction-pulse {
0%, 100% {
opacity: 0.7;
}
50% {
opacity: 1;
}
}
.code-block { .code-block {
font-family: var(--mono); font-family: var(--mono);
font-size: 12px; font-size: 12px;
-8
View File
@@ -145,14 +145,6 @@ export function connectGateway(host: GatewayHost) {
} }
export function handleGatewayEvent(host: GatewayHost, evt: GatewayEventFrame) { export function handleGatewayEvent(host: GatewayHost, evt: GatewayEventFrame) {
try {
handleGatewayEventUnsafe(host, evt);
} catch (err) {
console.error("[gateway] handleGatewayEvent error:", evt.event, err);
}
}
function handleGatewayEventUnsafe(host: GatewayHost, evt: GatewayEventFrame) {
host.eventLogBuffer = [ host.eventLogBuffer = [
{ ts: Date.now(), event: evt.event, payload: evt.payload }, { ts: Date.now(), event: evt.event, payload: evt.payload },
...host.eventLogBuffer, ...host.eventLogBuffer,
-1
View File
@@ -444,7 +444,6 @@ export function renderApp(state: AppViewState) {
showThinking, showThinking,
loading: state.chatLoading, loading: state.chatLoading,
sending: state.chatSending, sending: state.chatSending,
compactionStatus: state.compactionStatus,
assistantAvatarUrl: chatAvatarUrl, assistantAvatarUrl: chatAvatarUrl,
messages: state.chatMessages, messages: state.chatMessages,
toolMessages: state.chatToolMessages, toolMessages: state.chatToolMessages,
+1 -62
View File
@@ -138,69 +138,8 @@ export function resetToolStream(host: ToolStreamHost) {
flushToolStreamSync(host); flushToolStreamSync(host);
} }
export type CompactionStatus = {
active: boolean;
startedAt: number | null;
completedAt: number | null;
retryingAt: number | null;
};
type CompactionHost = ToolStreamHost & {
compactionStatus?: CompactionStatus | null;
compactionClearTimer?: number | null;
};
const COMPACTION_TOAST_DURATION_MS = 5000;
export function handleCompactionEvent(host: CompactionHost, payload: AgentEventPayload) {
const data = payload.data ?? {};
const phase = typeof data.phase === "string" ? data.phase : "";
const willRetry = Boolean(data.willRetry);
// Clear any existing timer
if (host.compactionClearTimer != null) {
window.clearTimeout(host.compactionClearTimer);
host.compactionClearTimer = null;
}
if (phase === "start") {
host.compactionStatus = {
active: true,
startedAt: Date.now(),
completedAt: null,
retryingAt: null,
};
} else if (phase === "end") {
host.compactionStatus = {
active: willRetry,
startedAt: host.compactionStatus?.startedAt ?? null,
completedAt: willRetry ? null : Date.now(),
retryingAt: willRetry ? Date.now() : null,
};
// Auto-clear the toast after duration
host.compactionClearTimer = window.setTimeout(() => {
host.compactionStatus = null;
host.compactionClearTimer = null;
}, COMPACTION_TOAST_DURATION_MS);
}
}
export function handleAgentEvent(host: ToolStreamHost, payload?: AgentEventPayload) { export function handleAgentEvent(host: ToolStreamHost, payload?: AgentEventPayload) {
if (!payload) return; if (!payload || payload.stream !== "tool") return;
// Handle compaction events
if (payload.stream === "compaction") {
const sessionKey =
typeof payload.sessionKey === "string" ? payload.sessionKey : undefined;
if (sessionKey && sessionKey !== host.sessionKey) return;
if (!sessionKey && host.chatRunId && payload.runId !== host.chatRunId) return;
if (host.chatRunId && payload.runId !== host.chatRunId) return;
if (!host.chatRunId) return;
handleCompactionEvent(host as CompactionHost, payload);
return;
}
if (payload.stream !== "tool") return;
const sessionKey = const sessionKey =
typeof payload.sessionKey === "string" ? payload.sessionKey : undefined; typeof payload.sessionKey === "string" ? payload.sessionKey : undefined;
if (sessionKey && sessionKey !== host.sessionKey) return; if (sessionKey && sessionKey !== host.sessionKey) return;
-1
View File
@@ -125,7 +125,6 @@ export class ClawdbotApp extends LitElement {
@state() chatStream: string | null = null; @state() chatStream: string | null = null;
@state() chatStreamStartedAt: number | null = null; @state() chatStreamStartedAt: number | null = null;
@state() chatRunId: string | null = null; @state() chatRunId: string | null = null;
@state() compactionStatus: import("./app-tool-stream").CompactionStatus | null = null;
@state() chatAvatarUrl: string | null = null; @state() chatAvatarUrl: string | null = null;
@state() chatThinkingLevel: string | null = null; @state() chatThinkingLevel: string | null = null;
@state() chatQueue: ChatQueueItem[] = []; @state() chatQueue: ChatQueueItem[] = [];
+1 -5
View File
@@ -254,11 +254,7 @@ export class GatewayBrowserClient {
} }
this.lastSeq = seq; this.lastSeq = seq;
} }
try { this.opts.onEvent?.(evt);
this.opts.onEvent?.(evt);
} catch (err) {
console.error("[gateway] event handler error:", err);
}
return; return;
} }
-50
View File
@@ -16,13 +16,6 @@ import {
import { renderMarkdownSidebar } from "./markdown-sidebar"; import { renderMarkdownSidebar } from "./markdown-sidebar";
import "../components/resizable-divider"; import "../components/resizable-divider";
export type CompactionIndicatorStatus = {
active: boolean;
startedAt: number | null;
completedAt: number | null;
retryingAt: number | null;
};
export type ChatProps = { export type ChatProps = {
sessionKey: string; sessionKey: string;
onSessionKeyChange: (next: string) => void; onSessionKeyChange: (next: string) => void;
@@ -31,7 +24,6 @@ export type ChatProps = {
loading: boolean; loading: boolean;
sending: boolean; sending: boolean;
canAbort?: boolean; canAbort?: boolean;
compactionStatus?: CompactionIndicatorStatus | null;
messages: unknown[]; messages: unknown[];
toolMessages: unknown[]; toolMessages: unknown[];
stream: string | null; stream: string | null;
@@ -67,46 +59,6 @@ export type ChatProps = {
onChatScroll?: (event: Event) => void; onChatScroll?: (event: Event) => void;
}; };
const COMPACTION_TOAST_DURATION_MS = 5000;
function renderCompactionIndicator(status: CompactionIndicatorStatus | null | undefined) {
if (!status) return nothing;
// Show "compacting..." while active
if (status.active) {
return html`
<div class="callout info compaction-indicator compaction-indicator--active">
🧹 Compacting context...
</div>
`;
}
if (status.retryingAt) {
const elapsed = Date.now() - status.retryingAt;
if (elapsed < COMPACTION_TOAST_DURATION_MS) {
return html`
<div class="callout info compaction-indicator compaction-indicator--active">
🧹 Retrying compaction...
</div>
`;
}
}
// Show "compaction complete" briefly after completion
if (status.completedAt) {
const elapsed = Date.now() - status.completedAt;
if (elapsed < COMPACTION_TOAST_DURATION_MS) {
return html`
<div class="callout success compaction-indicator compaction-indicator--complete">
🧹 Context compacted
</div>
`;
}
}
return nothing;
}
export function renderChat(props: ChatProps) { export function renderChat(props: ChatProps) {
const canCompose = props.connected; const canCompose = props.connected;
const isBusy = props.sending || props.stream !== null; const isBusy = props.sending || props.stream !== null;
@@ -137,8 +89,6 @@ export function renderChat(props: ChatProps) {
? html`<div class="callout danger">${props.error}</div>` ? html`<div class="callout danger">${props.error}</div>`
: nothing} : nothing}
${renderCompactionIndicator(props.compactionStatus)}
${props.focusMode ${props.focusMode
? html` ? html`
<button <button