Show how each bot message was generated

Every bot message gets an info button that opens its details: the
provider and model behind it, tokens and cost, the job that ran it, each
model used and every call in order. Each model call now records how it
was made in token_usages.settings (generation settings sent, whose
settings they were, pinned or automatic model, fallbacks), shown per
model in the modal.

Live replies now carry their token usage, from the job's push and the
live relay alike, so it shows without a reload.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-25 04:33:47 +01:00
co-authored by Claude Opus 5.5
parent feec69adca
commit c13b9ea826
48 changed files with 1307 additions and 57 deletions
+20 -13
View File
@@ -1,6 +1,7 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOT_MESSAGES } from "@/db/types/db";
import grabMessagesReplies from "@/src/functions/backend/db/bots/grab-messages-replies";
import withMessageUsage from "@/src/functions/backend/db/usage/with-message-usage";
import { sendToUser } from "@/src/websocket/(functions)/send-to-user";
type Params = {
@@ -14,7 +15,7 @@ type Params = {
* with the message a reply quotes. Returns the newest id read.
*/
export default async function relayNewBotMessages({ after_id, users }: Params) {
const rows =
const read =
(
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOT_MESSAGES & { user_id: number }>({
sql: `SELECT m.*, s.user_id FROM bot_messages m JOIN bot_sessions s ON s.id = m.session_id
@@ -23,25 +24,31 @@ export default async function relayNewBotMessages({ after_id, users }: Params) {
})
).payload || [];
/**
* With what each one spent, like a loaded message, so the thread shows
* its tokens without a reload.
*/
const rows = await withMessageUsage({
messages: read.filter((row) => users.has(String(row.user_id))),
});
const replies = await grabMessagesReplies({
message_ids: rows
.filter((row) => row.reply_to_message_id && users.has(String(row.user_id)))
.filter((row) => row.reply_to_message_id)
.map((row) => Number(row.id)),
});
for (const { user_id, ...message } of rows) {
if (users.has(String(user_id))) {
const reply_to = replies.get(Number(message.id));
const reply_to = replies.get(Number(message.id));
sendToUser({
user_id,
data: {
event: "server:bot-message",
data: reply_to ? { ...message, reply_to } : message,
},
});
}
sendToUser({
user_id,
data: {
event: "server:bot-message",
data: reply_to ? { ...message, reply_to } : message,
},
});
}
return Number(rows[rows.length - 1]?.id) || after_id;
return Number(read[read.length - 1]?.id) || after_id;
}