/** * The single output contract shared by every AI Compose generation. * * The plugin builds a richText system prompt that asks the model for lexical * JSON and injects a blog layout of `[paragraph]` / `[heading]` markers, while * the same field's instruction asks for plain prose with "no markdown, no * lists, no headings". Both reach the model in one request, and it answers by * arguing about the conflict (which the handler then saves as the first * paragraph of the field). * * Every field now follows one rule instead: the model is asked for plain prose * and nothing else, and the application assembles the JSON the Compose client * parses. `AI_OUTPUT_RULES` is appended to every generation; the helpers below * decide what prose is wrapped into for a given field schema. */ export const AI_OUTPUT_RULES = `OUTPUT RULES: Respond with plain prose only. Do not output JSON, code fences, schema definitions, or layout markers. Ordinary sentences and paragraphs are the entire expected output; the application converts them into the editor's format. CRITICAL: Never use em-dashes (\u2014) in your writing. Avoid obvious AI tells.`; /** * The plugin's block-placeholder paragraph. Preserved embed blocks are replaced * with `[[[BLOCK_]]]` tokens before generation and swapped back afterwards, so * the model has to echo the tokens verbatim for the blocks to survive. It is the * only instruction from the plugin's richText system worth keeping. */ const BLOCK_PLACEHOLDER_INSTRUCTION = /- The text may contain one or more standalone placeholder tokens[\s\S]*?surrounding text\./; export const extractBlockPlaceholderInstruction = (pluginSystem?: string): string => { if (!pluginSystem) return ""; const match = pluginSystem.match(BLOCK_PLACEHOLDER_INSTRUCTION); return match ? match[0].trim() : ""; }; export const buildComposeSystem = ({ pluginSystem, fieldType, }: { pluginSystem?: string; fieldType?: string; }): string => { const parts: string[] = []; if (fieldType === "richText") { const blockInstruction = extractBlockPlaceholderInstruction(pluginSystem); if (blockInstruction) parts.push(blockInstruction); } parts.push(AI_OUTPUT_RULES); return parts.join("\n\n"); }; type JsonSchemaLike = { definitions?: unknown; properties?: Record; }; export type ProseShape = { kind: "lexical" } | { kind: "field"; key: string }; /** * Whether a generation schema is one this module knows how to fill with prose: * the lexical editor schema for richText (identified by its `definitions`), or * a single string property for text/textarea. Anything else (e.g. a hasMany * array) is left to the handler's structured fallback. */ export const resolveProseShape = (schema: unknown): ProseShape | null => { if (!schema || typeof schema !== "object") return null; const candidate = schema as JsonSchemaLike; if (candidate.definitions) return { kind: "lexical" }; const keys = Object.keys(candidate.properties ?? {}); if (keys.length !== 1) return null; const property = candidate.properties?.[keys[0]]; if (property?.type !== "string") return null; return { kind: "field", key: keys[0] }; }; export const wrapProse = (shape: ProseShape, text: string): Record => shape.kind === "lexical" ? plainTextToLexicalJson(text) : { [shape.key]: text }; export function plainTextToLexicalJson(text: string): Record { const paragraphs = text.split(/\n\n+/).filter((p) => p.trim()); const children = paragraphs.map((para) => ({ type: "paragraph", children: [ { type: "text", text: para.trim(), format: 0, style: "", mode: "normal", detail: 0, direction: "ltr", indent: 0, version: 1, }, ], direction: "ltr", format: "start", indent: 0, textFormat: 0, textStyle: "", version: 1, })); return { root: { type: "root", children, direction: "ltr", format: "", indent: 0, version: 1, }, }; }