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.
148 lines
5.6 KiB
TypeScript
148 lines
5.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
AI_OUTPUT_RULES,
|
|
buildComposeSystem,
|
|
extractBlockPlaceholderInstruction,
|
|
plainTextToLexicalJson,
|
|
resolveProseShape,
|
|
wrapProse,
|
|
} from "@/lib/ai/composeSystem";
|
|
|
|
// The exact system `@ai-stack/payloadcms@3.2.30` builds for a richText field
|
|
// (dist/endpoints/index.js -> buildRichTextSystem). Captured verbatim so that a
|
|
// plugin upgrade which changes the wording fails this spec loudly instead of
|
|
// silently letting the JSON/layout instructions back into the model's prompt.
|
|
const PLUGIN_RICH_TEXT_SYSTEM = `INSTRUCTIONS:
|
|
You are a highly skilled and professional blog writer,
|
|
renowned for crafting engaging and well-organized articles.
|
|
|
|
RULES:
|
|
- Generate original and unique content based on the given topic.
|
|
- Ensure the output follows the provided rich text JSON schema exactly.
|
|
- Produce valid JSON with no undefined or null values.
|
|
|
|
- The text may contain one or more standalone placeholder tokens of the exact form
|
|
"[[[BLOCK_<number>]]]" (e.g. "[[[BLOCK_0]]]").
|
|
These represent embedded content you cannot see. Reproduce every such token EXACTLY as given,
|
|
completely unchanged (do not translate, reword, remove, or merge it with other text), as its
|
|
own paragraph, in the same relative position within the surrounding text.
|
|
---
|
|
LAYOUT INSTRUCTIONS:
|
|
[paragraph] - Write a concise introduction (2-3 sentences) that outlines the main topic.
|
|
[horizontalrule] - Insert a horizontal rule to separate the introduction from the main content.
|
|
[list] - Create a list with 3-5 items. Each list item should contain:
|
|
a. [heading] - A brief, descriptive heading (up to 5 words)
|
|
b. [paragraph] - A short explanation or elaboration (1-2 sentences)
|
|
---
|
|
ADDITIONAL GUIDELINES:
|
|
- Ensure coherence and logical flow between all sections.
|
|
`;
|
|
|
|
describe("buildComposeSystem", () => {
|
|
it("strips the plugin's JSON rules and layout markers from the model's system prompt", () => {
|
|
const system = buildComposeSystem({
|
|
pluginSystem: PLUGIN_RICH_TEXT_SYSTEM,
|
|
fieldType: "richText",
|
|
});
|
|
|
|
expect(system).not.toContain("rich text JSON schema");
|
|
expect(system).not.toContain("Produce valid JSON");
|
|
expect(system).not.toMatch(/LAYOUT INSTRUCTIONS/);
|
|
expect(system).not.toMatch(/\[paragraph\]|\[horizontalrule\]|\[list\]|\[heading\]|\[quote\]/);
|
|
});
|
|
|
|
it("keeps the block-placeholder paragraph and nothing else from the plugin system", () => {
|
|
const system = buildComposeSystem({
|
|
pluginSystem: PLUGIN_RICH_TEXT_SYSTEM,
|
|
fieldType: "richText",
|
|
});
|
|
|
|
expect(system).toContain("- The text may contain one or more standalone placeholder tokens");
|
|
expect(system).toContain("[[[BLOCK_<number>]]]");
|
|
expect(system).not.toContain("professional blog writer");
|
|
});
|
|
|
|
it("asks for prose plus the em-dash rule exactly once for every field type", () => {
|
|
for (const fieldType of ["richText", "text", "textarea"]) {
|
|
const system = buildComposeSystem({
|
|
pluginSystem: PLUGIN_RICH_TEXT_SYSTEM,
|
|
fieldType,
|
|
});
|
|
|
|
expect(system.match(/Never use em-dashes/g)).toHaveLength(1);
|
|
expect(system).toContain("Respond with plain prose only.");
|
|
expect(system).not.toContain("rich text JSON schema");
|
|
expect(system).not.toContain("Produce valid JSON");
|
|
}
|
|
});
|
|
|
|
it("sends short fields only the shared rules, with no plugin material", () => {
|
|
const system = buildComposeSystem({
|
|
pluginSystem: PLUGIN_RICH_TEXT_SYSTEM,
|
|
fieldType: "text",
|
|
});
|
|
|
|
expect(system).toBe(AI_OUTPUT_RULES);
|
|
});
|
|
|
|
it("sends the real em-dash character to the model", () => {
|
|
expect(AI_OUTPUT_RULES).toContain("em-dashes (\u2014)");
|
|
});
|
|
});
|
|
|
|
describe("extractBlockPlaceholderInstruction", () => {
|
|
it("returns the placeholder paragraph without the surrounding rules", () => {
|
|
const block = extractBlockPlaceholderInstruction(PLUGIN_RICH_TEXT_SYSTEM);
|
|
|
|
expect(block.startsWith("- The text may contain")).toBe(true);
|
|
expect(block.endsWith("surrounding text.")).toBe(true);
|
|
expect(block).not.toMatch(/JSON/);
|
|
});
|
|
|
|
it("returns an empty string when there is nothing to keep", () => {
|
|
expect(extractBlockPlaceholderInstruction(undefined)).toBe("");
|
|
expect(extractBlockPlaceholderInstruction("just some prose")).toBe("");
|
|
});
|
|
});
|
|
|
|
describe("resolveProseShape / wrapProse", () => {
|
|
const lexicalSchema = { definitions: { root: {} }, type: "object" };
|
|
const textSchema = {
|
|
type: "object",
|
|
properties: { name: { type: "string" } },
|
|
required: ["name"],
|
|
};
|
|
|
|
it("wraps prose into lexical JSON for a richText schema", () => {
|
|
const shape = resolveProseShape(lexicalSchema);
|
|
expect(shape).toEqual({ kind: "lexical" });
|
|
|
|
const json = wrapProse(shape!, "First paragraph.\n\nSecond paragraph.");
|
|
expect(json).toEqual(plainTextToLexicalJson("First paragraph.\n\nSecond paragraph."));
|
|
|
|
const root = (json as { root: { children: Array<{ children: Array<{ text: string }> }> } }).root;
|
|
expect(root.children).toHaveLength(2);
|
|
expect(root.children[0].children[0].text).toBe("First paragraph.");
|
|
});
|
|
|
|
it("wraps prose into the field's own key for a single string property", () => {
|
|
const shape = resolveProseShape(textSchema);
|
|
expect(shape).toEqual({ kind: "field", key: "name" });
|
|
|
|
expect(wrapProse(shape!, "Barracks")).toEqual({ name: "Barracks" });
|
|
});
|
|
|
|
it("declines shapes it cannot wrap", () => {
|
|
expect(
|
|
resolveProseShape({ type: "object", properties: { tags: { type: "array" } } }),
|
|
).toBeNull();
|
|
expect(
|
|
resolveProseShape({
|
|
type: "object",
|
|
properties: { a: { type: "string" }, b: { type: "string" } },
|
|
}),
|
|
).toBeNull();
|
|
expect(resolveProseShape(undefined)).toBeNull();
|
|
});
|
|
});
|