Rich text fields received the plugin's richText system prompt, which demanded lexical JSON and a blog layout of [paragraph]/[heading] markers, next to an instruction asking for plain prose. The model answered by arguing about the conflict, and that argument was saved into the field. Text fields had the mirror problem: a prose instruction sent over a transport that forced JSON. Every field now asks for plain prose and nothing else, and the handler assembles the JSON the Compose client already parses: - src/lib/ai/composeSystem.ts owns the shared rules, keeps only the plugin's [[[BLOCK_<n>]]] echo instruction for richText, and wraps prose into lexical JSON or a single field value. - payload.config.ts routes every wrappable schema through generatePlainText; streamObject stays only as the fallback for shapes prose cannot fill, such as a hasMany array. - Truncated output reaches the client again (finish_reason length was swallowed on the prose path). Adds tests/int/ai-compose-system.int.spec.ts (10 cases) and picks up the generated payload-types.ts drift for the model field description.
124 lines
4 KiB
TypeScript
124 lines
4 KiB
TypeScript
/**
|
|
* 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_<n>]]]` 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<string, { type?: string } | undefined>;
|
|
};
|
|
|
|
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<string, unknown> =>
|
|
shape.kind === "lexical" ? plainTextToLexicalJson(text) : { [shape.key]: text };
|
|
|
|
export function plainTextToLexicalJson(text: string): Record<string, unknown> {
|
|
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,
|
|
},
|
|
};
|
|
}
|