Files
cobble/src/utils/hook-payload-block.ts
T
tbenandClaude Opus 5.5 bd9744043e Let outside services and computers' browser notifications give bots work
A bot hook (bot_hooks) is a secret URL, POST /api/hooks/<token>, whose
calls queue the hook's brief plus the raw body as a task, per user and
bot like a routine. Only the token's hash is kept (shown once, rotatable);
an optional HMAC-SHA256 signing secret is stored encrypted. Calls are
rate limited per address and per hook, recorded in bot_hook_events
(pruned daily), deduplicated by delivery id, and a call while the last
run still waits joins its brief instead of queueing another. The payload
is fenced data and HookRunNote tells the bot never to take instructions
from it; failed runs retry like routines.

A hook can instead watch a computer's browser notifications: the app
installs a small watcher that owns org.freedesktop.Notifications on the
sandbox's session bus, so Chrome hands it every notification (it only
looks for that service at start-up, so Chrome is restarted once, never
mid-task), and it posts each one to its hooks, optionally filtered.
Computers reach the app at COMPUTER_APP_URL or the app container's name.

Users make hooks in the conversation's Hooks modal and manage them all
on /admin/routines; bots never create or change hooks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-25 14:06:59 +01:00

38 lines
1.3 KiB
TypeScript

type Params = {
payload: string;
content_type?: string | null;
received_at: number;
/**
* Longest payload kept, in characters; the rest is cut and says so.
*/
max_chars: number;
};
/**
* One hook call as a brief shows it: when it came and what it says,
* fenced so nothing in it can pass for the brief. JSON is
* pretty-printed; anything else is shown as sent. The fence is longer
* than any run of backticks in the payload, so the payload can't close it.
*/
export default function hookPayloadBlock({ payload, content_type, received_at, max_chars }: Params) {
const text = (() => {
try {
return JSON.stringify(JSON.parse(payload), null, 2);
} catch {
return payload;
}
})();
const cut = text.length > max_chars;
const body = cut ? text.slice(0, max_chars) : text;
const longest_run = Math.max(0, ...(body.match(/`+/g) || []).map((run) => run.length));
const fence = "`".repeat(Math.max(3, longest_run + 1));
const kind = (content_type || "").split(";")[0]?.trim() || "unknown type";
const lines = [`Call received ${new Date(received_at).toISOString()} (${kind}):`, "", fence, body || "(empty body)", fence];
if (cut) lines.push(`(Cut at ${max_chars} characters.)`);
return lines.join("\n");
}