63 lines
3.0 KiB
TypeScript
63 lines
3.0 KiB
TypeScript
// PEP-152: what belongs to a message but is not its main text — kept in `settings`, no columns:
|
|
// message.details:q:<questionId> technical details of a message that is a `questions` row
|
|
// message.refusals:<s<id>|t<id>> refusals of the format in a row (core/agent-message.ts)
|
|
// task.result:<taskId> the agent's own words about the finished story («готово»)
|
|
// Details of an event (progress, blocker) live in its payload.
|
|
import { questions, settings, type Db } from "@peppa/db";
|
|
|
|
type Question = typeof questions.$inferSelect;
|
|
import { inArray } from "drizzle-orm";
|
|
import { deleteSetting, getSetting, setSetting } from "./settings.ts";
|
|
|
|
const DETAILS_MAX = 20_000;
|
|
const qKey = (id: number) => `message.details:q:${id}`;
|
|
|
|
export async function setMessageDetails(db: Db, questionId: number, details?: string | null): Promise<void> {
|
|
const d = (details ?? "").trim().slice(0, DETAILS_MAX);
|
|
if (d) await setSetting(db, qKey(questionId), d);
|
|
else await deleteSetting(db, qKey(questionId));
|
|
}
|
|
|
|
/**
|
|
* Inserts a `questions` row together with its details in one transaction: a reader (the bot,
|
|
* a page, a test) never sees the new message without its «Подробнее».
|
|
*/
|
|
export function insertQuestion(db: Db, values: typeof questions.$inferInsert, details?: string | null): Question {
|
|
const d = (details ?? "").trim().slice(0, DETAILS_MAX);
|
|
return db.transaction((tx) => {
|
|
const row = tx.insert(questions).values(values).returning().get() as Question;
|
|
if (d) tx.insert(settings).values({ key: qKey(row.id), value: d }).onConflictDoUpdate({ target: settings.key, set: { value: d, updatedAt: new Date() } }).run();
|
|
return row;
|
|
});
|
|
}
|
|
|
|
export async function messageDetails(db: Db, ids: number[]): Promise<Map<number, string>> {
|
|
const uniq = [...new Set(ids)];
|
|
if (!uniq.length) return new Map();
|
|
const rows = await db.select().from(settings).where(inArray(settings.key, uniq.map(qKey)));
|
|
return new Map(rows.map((r) => [Number(r.key.split(":").at(-1)), String(r.value)]));
|
|
}
|
|
|
|
const refusalKey = (who: string) => `message.refusals:${who}`;
|
|
|
|
/** Counts one more refusal in a row and returns the count. */
|
|
export async function countRefusal(db: Db, who: string): Promise<number> {
|
|
const n = ((await getSetting<number>(db, refusalKey(who))) ?? 0) + 1;
|
|
await setSetting(db, refusalKey(who), n);
|
|
return n;
|
|
}
|
|
|
|
export async function clearRefusals(db: Db, who: string): Promise<void> {
|
|
if ((await getSetting<number>(db, refusalKey(who))) != null) await deleteSetting(db, refusalKey(who));
|
|
}
|
|
|
|
export interface TaskResult {
|
|
/** What the person got, in the agent's plain words (checked like any main text). */
|
|
summary: string;
|
|
details?: string | null;
|
|
}
|
|
|
|
const resultKey = (taskId: number) => `task.result:${taskId}`;
|
|
export const getTaskResult = (db: Db, taskId: number) => getSetting<TaskResult>(db, resultKey(taskId));
|
|
export const setTaskResult = (db: Db, taskId: number, r: TaskResult) => setSetting(db, resultKey(taskId), { summary: r.summary.trim(), details: (r.details ?? "").trim().slice(0, DETAILS_MAX) || null });
|