/** * In-memory history queries — only used when a richer userbot-backed * implementation isn't available. Frontends may override these. */ import { getRecentFormatted, getFormattedBefore, getFormattedBeforeTime, searchHistory, getMessagesByUser, getKnownUsers, } from "../../../storage/history.js"; import { formatMediaIndex } from "../../../storage/media-index.js"; import type { SharedActionHandlers } from "./types.js"; export const historyHandlers: SharedActionHandlers = { read_history: (body, chatId) => { const limit = Math.min(100, Number(body.limit ?? 30)); const cid = String(chatId); // The tool schema advertises `offset_id` and `before` for paging back; // this fallback used to ignore both and hand back the same newest // window no matter what the model asked, so "go further back" was a // silent no-op on frontends without a platform history API (WhatsApp, // native). const offsetId = Number(body.offset_id); if (Number.isFinite(offsetId) && offsetId > 0) { return { ok: true, text: getFormattedBefore(cid, offsetId, limit) }; } if (body.before !== undefined) { const ts = Date.parse(String(body.before)); if (Number.isFinite(ts)) { return { ok: true, text: getFormattedBeforeTime(cid, ts, limit) }; } } return { ok: true, text: getRecentFormatted(cid, limit) }; }, search_history: (body, chatId) => { const limit = Math.min(100, Number(body.limit ?? 20)); return { ok: true, text: searchHistory(String(chatId), String(body.query ?? ""), limit), }; }, get_user_messages: (body, chatId) => { const limit = Math.min(50, Number(body.limit ?? 20)); return { ok: true, text: getMessagesByUser( String(chatId), String(body.user_name ?? ""), limit, ), }; }, list_known_users: (body, chatId) => ({ ok: true, text: getKnownUsers(String(chatId)), }), list_media: (body, chatId) => ({ ok: true, text: formatMediaIndex( String(chatId), Math.min(20, Number(body.limit ?? 10)), ), }), };