peppa/packages/core/src/questions.ts
2026-11-16 13:05:59 +05:00

57 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-149: four kinds of agent messages for the person — question, done, progress, blocker.
// The kind decides the route: only a `question` lands in «Ждут вашего ответа» and comes to
// Telegram as a question; `done` waits for «Принять»; `progress` and a non-product `blocker`
// are events of the story thread («События агентов») and never wake the person.
import { questions } from "@peppa/db";
import { sql, type SQL } from "drizzle-orm";
import { PeppaError } from "./errors.ts";
export const MESSAGE_TYPES = ["question", "done", "progress", "blocker"] as const;
export type MessageType = (typeof MESSAGE_TYPES)[number];
type Typed = { kind: string; msgType?: MessageType | null; options?: string[] | null; taskId?: number | null };
/** The type a new row gets when the caller names none: anything a person must decide is a question. */
export function defaultMessageType(q: Pick<Typed, "kind" | "options">): MessageType | null {
if (q.kind !== "attention") return "question";
return q.options?.length ? "question" : null;
}
/**
* The type of a message: the stored one, else derived (rows before PEP-149 and messages of the
* factory itself). null — a factory message nobody has to answer (a failure, a limit, a release).
*/
export function questionType(q: Typed, taskStatus?: string | null): MessageType | null {
if (q.msgType) return q.msgType;
const d = defaultMessageType(q);
if (d) return d;
return q.taskId != null && taskStatus === "review" ? "done" : null;
}
/** True for what waits for a person: a question to answer or a finished story to accept. */
export function needsPerson(q: Typed, taskStatus?: string | null): boolean {
const t = questionType(q, taskStatus);
return t === "question" || t === "done";
}
/** SQL twin of `questionType(q) === "question"` (counters of open questions). */
export const IS_QUESTION: SQL = sql`(${questions.msgType} = 'question' OR (${questions.msgType} IS NULL AND (${questions.kind} != 'attention' OR json_array_length(${questions.options}) > 0)))`;
export const ASK_MIN_OPTIONS = 2;
export const ASK_MAX_OPTIONS = 4;
/**
* Checks the answers of an agent's question: 2–4 different options and a default among them.
* Returns the options with the default first. The text of the refusal is for the agent.
*/
export function checkChoices(options: string[] | undefined, def: string | undefined): { options: string[]; defaultOption: string } {
const opts = [...new Set((options ?? []).map((o) => o.trim()).filter(Boolean))];
if (opts.length < ASK_MIN_OPTIONS || opts.length > ASK_MAX_OPTIONS) {
throw new PeppaError(`Not sent: a question to the owner needs ${ASK_MIN_OPTIONS}–${ASK_MAX_OPTIONS} answer options (got ${opts.length}). The owner answers with one tap — give the possible answers as \`options\` and your recommendation as \`default\`. If there is nothing to choose from, it is not a question: decide yourself, or use report_progress / report_blocker.`);
}
const d = def?.trim();
if (!d) throw new PeppaError("Not sent: name the `default` — the option you go on with while the owner has not answered (one of `options`).");
if (!opts.includes(d)) throw new PeppaError(`Not sent: \`default\` must be exactly one of \`options\` (${opts.map((o) => `«${o}»`).join(", ")}).`);
return { options: [d, ...opts.filter((o) => o !== d)], defaultOption: d };
}