// SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2026 avtc /** * Messages the extension sends on its own account: the task gate, a phase's * skill after a transition, the reminder after a compaction, the kanban * auto-agent's instructions. * * Each is a custom message of the type `featyard`, so the session shows and * stores it as the extension's, not as something the person typed. The model * reads its text the way it reads a user message. * * What the person asks for with a command (`/fy:design`, `/fy:continue`) is * passed on as a user message: it is the person's. */ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { Box, Text } from "@earendil-works/pi-tui"; /** The custom type of the extension's own messages. */ export const OWN_MESSAGE_TYPE = "featyard"; /** What sends an own message: `pi`, or the context of a replaced session. */ export interface OwnMessageSender { sendMessage: ( message: { customType: string; content: string; display: boolean }, options: { deliverAs: "steer" | "followUp"; triggerTurn: boolean }, ) => unknown; } /** A skill as it stands in a message: a block that names the skill, then what it was run with. */ const SKILL_BLOCK = /^\n[\s\S]*?\n<\/skill>(?:\n\n[\s\S]+)?$/; const SKILL_NAME = 1; const NO_SKILL = null; const NO_TEXT = ""; const SHOWN = true; const STARTS_A_TURN = true; const PAD_Y = 1; const NO_PAD = 0; /** * Send `text` to the agent as the extension's own message. It starts a turn * when none runs; while one runs, `deliverAs` says whether it joins the turn * at the next step or follows it. */ export function sendOwnMessage(sender: OwnMessageSender, text: string, deliverAs: "steer" | "followUp"): void { sender.sendMessage( { customType: OWN_MESSAGE_TYPE, content: text, display: SHOWN }, { deliverAs, triggerTurn: STARTS_A_TURN }, ); } /** The text of a message's content: its text parts, joined. */ function textOf(content: unknown): string { if (typeof content === "string") return content; if (!Array.isArray(content)) return NO_TEXT; return content .map((part: { type?: string; text?: string }) => part.type === "text" && typeof part.text === "string" ? part.text : NO_TEXT, ) .join("\n"); } /** The name of the skill `text` is a block of; null for any other text. */ export function skillNameOf(text: string): string | null { return SKILL_BLOCK.exec(text)?.[SKILL_NAME] ?? NO_SKILL; } /** * Draw the extension's own messages. A skill is one line that names it until * the person expands the message; everything else, and the expanded skill, is * drawn by pi under the `[featyard]` label. */ export function registerOwnMessageRenderer(pi: Pick): void { pi.registerMessageRenderer(OWN_MESSAGE_TYPE, (message, options, theme) => { if (options.expanded) return undefined; const skill = skillNameOf(textOf(message.content)); if (skill === NO_SKILL) return undefined; const line = theme.fg("customMessageLabel", theme.bold(`[${OWN_MESSAGE_TYPE}]`)) + theme.fg("customMessageText", ` skill ${skill}`) + theme.fg("dim", " (Ctrl+O to expand)"); const box = new Box(options.outputPad, PAD_Y, (text: string) => theme.bg("customMessageBg", text)); box.addChild(new Text(line, NO_PAD, NO_PAD)); return box; }); }