diff --git a/packages/core/src/message-details.ts b/packages/core/src/message-details.ts index c5617a89..7d48cd93 100644 --- a/packages/core/src/message-details.ts +++ b/packages/core/src/message-details.ts @@ -3,7 +3,9 @@ // message.refusals:|t> refusals of the format in a row (core/agent-message.ts) // task.result: the agent's own words about the finished story («готово») // Details of an event (progress, blocker) live in its payload. -import { settings, type Db } from "@peppa/db"; +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"; @@ -16,6 +18,19 @@ export async function setMessageDetails(db: Db, questionId: number, details?: st 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> { const uniq = [...new Set(ids)]; if (!uniq.length) return new Map(); diff --git a/packages/core/src/sessions.ts b/packages/core/src/sessions.ts index f6ec095c..275d0070 100644 --- a/packages/core/src/sessions.ts +++ b/packages/core/src/sessions.ts @@ -7,7 +7,7 @@ import { actorColumns } from "./actor.ts"; import type { Question, QuestionKind } from "./tasks.ts"; import { defaultMessageType, type MessageType } from "./questions.ts"; import { userProjectIds } from "./users.ts"; -import { setMessageDetails } from "./message-details.ts"; +import { insertQuestion, setMessageDetails } from "./message-details.ts"; import { questionStates, raiseOnce, type MessageTopic } from "./message-state.ts"; import { keepQuestionDetails, messageText } from "./user-text.ts"; @@ -205,11 +205,7 @@ export async function raiseSessionQuestion( const original = text; text = messageText(kind, text); const row = await raiseOnce(db, { projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, ...o }, async () => { - const [created] = await db - .insert(questions) - .values({ projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, msgType: msgType ?? defaultMessageType({ kind, options }) }) - .returning(); - return created!; + return insertQuestion(db, { projectId: s.projectId, taskId: s.taskId, sessionId: s.id, kind, text, options, awaitingHook, msgType: msgType ?? defaultMessageType({ kind, options }) }, details); }); if (details !== undefined) await setMessageDetails(db, row.id, details); await keepQuestionDetails(db, row.id, original, text).catch(() => {}); diff --git a/packages/core/src/tasks.ts b/packages/core/src/tasks.ts index 1a920c24..3387aceb 100644 --- a/packages/core/src/tasks.ts +++ b/packages/core/src/tasks.ts @@ -19,7 +19,7 @@ import { raiseOnce, type MessageTopic } from "./message-state.ts"; import { keepQuestionDetails, messageText, stripServiceBlocks } from "./user-text.ts"; import { defaultMessageType, type MessageType } from "./questions.ts"; import { renderMain, type AgentMessage } from "./agent-message.ts"; -import { setMessageDetails } from "./message-details.ts"; +import { insertQuestion, setMessageDetails } from "./message-details.ts"; import { touchVersion } from "./task-origin.ts"; export type Task = typeof tasks.$inferSelect; @@ -348,11 +348,7 @@ export async function askPm(db: Db, ref: TaskRef, { topic, head, details, ...q } q = { ...q, text: messageText(q.kind, q.text) }; // PEP-151: the same state of the story again refreshes the earlier message instead of adding one. const row = await raiseOnce(db, { projectId: ref.project.id, taskId: ref.task.id, sessionId: q.sessionId ?? null, kind: q.kind, text: q.text, options: q.options, topic, head }, async () => { - const [created] = await db - .insert(questions) - .values({ projectId: ref.project.id, taskId: ref.task.id, ...q, msgType: q.msgType ?? defaultMessageType(q) }) - .returning(); - return created!; + return insertQuestion(db, { projectId: ref.project.id, taskId: ref.task.id, ...q, msgType: q.msgType ?? defaultMessageType(q) }, details); }); // The status goes right after the question: an answer that comes at once (it moves the story // on) must not be overwritten by a late «awaiting approval».