Files
cobble/src/functions/backend/db/bot-hooks/record-hook-call-response.ts
T
tbenandClaude Opus 5.5 0e59559ea9 Keep every hook call's response, searchable by the user and the bot
A hook call used to record only how it was taken; its run's answer went
to the chat alone and was lost to the call once the job was pruned. Now
`bot_hook_events` keeps `response`, `response_message_id` and
`responded_at`, written on every call a run took in when that run ends
(completed, failed, lost, stopped or cancelled) by
`recordHookCallResponse` in the job helpers, and calls turn done or
failed. The Calls view searches what calls sent and what runs answered,
filters by status and pages; Show all loads one call whole. Bots get
`search_hook_calls` and `read_hook_call` for their own hooks (never in a
hook call's own run), with payloads always marked as outside data.
Calls are kept for the app setting `hook_events_keep_days` (Settings >
General, default now 30 days). The caller still only gets 202 Queued.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-25 17:47:28 +01:00

48 lines
1.6 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS_QUEUE } from "@/db/types/db";
import parseJobJson from "@/src/bots-queue/parse-job-json";
import { AppData } from "@/src/data/app-data";
import recordHookCallResponseSql from "./record-hook-call-response-sql";
type Params = {
/**
* The job that ended. Only a hook call's run (`params.hook_id`) has
* calls to answer; any other job is left alone.
*/
job: BUN_SQLITE_COBBLE_BOTS_QUEUE;
status: "done" | "failed";
/**
* What the run answered, or why it failed. Cut to
* `AppData.BotHookMaxPayloadBytes`.
*/
response: string;
/**
* The chat message the run's end posted, if it posted one.
*/
message_id?: number | string | null;
now?: number;
};
/**
* Keeps a hook run's end on the calls it took in, so they can be found
* later by what was sent or answered, after the job itself is pruned.
* Called wherever a job ends (completed, failed for good, lost, stopped,
* cancelled). Never throws: the job has ended either way.
*/
export default async function recordHookCallResponse({ job, status, response, message_id, now = Date.now() }: Params) {
const hook_id = Number(parseJobJson({ text: job.params })?.hook_id) || 0;
if (!hook_id || !job.id) return;
await BunSQLite.sql(
recordHookCallResponseSql({
hook_id,
job_id: Number(job.id),
status,
response: response.slice(0, AppData.BotHookMaxPayloadBytes),
message_id: Number(message_id) || null,
now,
}),
).catch(() => {});
}