// PEP-156: who speaks. What the factory or an agent did is signed «FORGE»; an agent's // own words — with its nickname; the AI roles — «Аналитик» and «Продакт-агент»; a person's login // stands only under that person's own words. The journal keeps the real author of an event. import { agents, sessions, type Db } from "@peppa/db"; import { and, desc, eq, isNotNull, notInArray } from "drizzle-orm"; import { questionState } from "./message-state.ts"; import type { Question } from "./tasks.ts"; /** The analyst's name on its messages (an AI agent, shown with the robot badge). */ export const ANALYST_NAME = "Аналитик"; /** Names an AI role signs with (the last one — messages written before PEP-156). */ const ROLE_NAMES = new Set([ANALYST_NAME, "Продакт-агент", "Агент приёмки", "Продакт"]); /** A role, not a nickname: shown without the word «агент» in front, and translated. */ export const isRoleName = (name: string | null | undefined): boolean => !!name && ROLE_NAMES.has(name); /** How an agent signs a message where there is no badge (Telegram, push): «🤖 Шустрик». */ export const agentSignature = (name: string | null | undefined): string => (name ? `🤖 ${name}` : "🤖"); /** Nickname of the agent of a runner; null — the runner has none (yet). */ export async function sessionAgentName(db: Db, sessionId: number | null | undefined): Promise { if (!sessionId) return null; const [r] = await db.select({ name: agents.name }).from(sessions).innerJoin(agents, eq(sessions.agentId, agents.id)).where(eq(sessions.id, sessionId)); return r?.name ?? null; } /** The agent working on the story right now (its runner and nickname); null — nobody has started yet. */ export async function taskLiveAgent(db: Db, taskId: number): Promise<{ sessionId: number; name: string } | null> { const [r] = await db .select({ sessionId: sessions.id, name: agents.name }) .from(sessions) .innerJoin(agents, eq(sessions.agentId, agents.id)) .where(and(eq(sessions.taskId, taskId), isNotNull(sessions.agentId), notInArray(sessions.status, ["done", "failed"]))) .orderBy(desc(sessions.id)) .limit(1); return r ?? null; } export const taskLiveAgentName = async (db: Db, taskId: number): Promise => (await taskLiveAgent(db, taskId))?.name ?? null; export type MessageAuthor = { kind: "agent"; name: string | null } | { kind: "system" }; /** * Who a message to the user is from. An agent's question, permission or plan are the agent's * words (and an `attention` raised as its own words, `topic: "turn"` — since PEP-149 the end of a * turn is a `progress` event instead); «готово», failures, deploys and the rest of * `attention` are the factory's — even when a runner is attached to them. */ export async function questionAuthor(db: Db, q: Pick): Promise { if (!q.sessionId) return { kind: "system" }; if (q.kind === "attention" && (await questionState(db, q.id).catch(() => null))?.topic !== "turn") return { kind: "system" }; return { kind: "agent", name: await sessionAgentName(db, q.sessionId) }; } // ── «вы» ───────────────────────────────────────────────────────────────────────────── /** The line every AI role that talks to the user gets in its prompt. */ export const ADDRESS_RULE = "К человеку обращайся только на «вы» («посмотрите», «вам», «ваш»), никогда на «ты»; без панибратства и уменьшительных."; const INFORMAL = /(?