diff --git a/packages/core/src/message-details.ts b/packages/core/src/message-details.ts index c5617a89..7ed3ea89 100644 --- a/packages/core/src/message-details.ts +++ b/packages/core/src/message-details.ts @@ -3,7 +3,7 @@ // 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"; import { inArray } from "drizzle-orm"; import { deleteSetting, getSetting, setSetting } from "./settings.ts"; @@ -16,6 +16,19 @@ export async function setMessageDetails(db: Db, questionId: number, details?: st else await deleteSetting(db, qKey(questionId)); } +/** + * A new message together with its details, in one transaction: a reader never sees the + * message without its «Подробнее» (the web, the bot and tests read from other processes). + */ +export function insertQuestion(db: Db, values: typeof questions.$inferInsert, details?: string | null): typeof questions.$inferSelect { + const d = (details ?? "").trim().slice(0, DETAILS_MAX); + return db.transaction((tx) => { + const row = tx.insert(questions).values(values).returning().get()!; + 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 8b009730..01c0fc26 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"; @@ -197,14 +197,13 @@ export async function raiseSessionQuestion( // PEP-154: no service text (tags, command output, paths) in what the person reads. const original = text; text = messageText(kind, text); + let created = false; 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!; + created = true; + 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); + // A refreshed message (PEP-151) gets the fresh details. + if (!created && details !== undefined) await setMessageDetails(db, row.id, details); await keepQuestionDetails(db, row.id, original, text).catch(() => {}); await notify(db, { type: "question", questionId: row.id }); return row; diff --git a/packages/core/src/tasks.ts b/packages/core/src/tasks.ts index 456f9068..942a6f8b 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"; export type Task = typeof tasks.$inferSelect; export type TaskStatus = (typeof taskStatus.enumValues)[number]; @@ -327,18 +327,16 @@ export async function askPm(db: Db, ref: TaskRef, { topic, head, details, ...q } const original = q.text; 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. + let created = false; 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!; + created = true; + 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». if (q.kind === "approval") await updateTask(db, ref, { status: "awaiting_approval" }, q.sessionId); // PEP-152: the details follow the message (a refreshed one gets the fresh details). - if (details !== undefined) await setMessageDetails(db, row.id, details); + if (!created && details !== undefined) await setMessageDetails(db, row.id, details); await keepQuestionDetails(db, row.id, original, q.text).catch(() => {}); await notify(db, { type: "question", questionId: row.id }); return row;