Compare commits
3 commits
2713a786e4
...
53387c957f
| Author | SHA1 | Date | |
|---|---|---|---|
| 53387c957f | |||
| 67c0e4e94a | |||
| f5166c80b6 |
5 changed files with 161 additions and 60 deletions
|
|
@ -1,5 +1,13 @@
|
|||
# Changelog
|
||||
|
||||
# v0.12.11
|
||||
|
||||
### Fixes
|
||||
|
||||
- Various bug fixes and improvements.
|
||||
|
||||
---
|
||||
|
||||
# v0.12.10
|
||||
|
||||
### Improvements
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "polaris-task-force",
|
||||
"version": "0.12.10",
|
||||
"version": "0.12.11",
|
||||
"description": "Web app for the Arma 3 unit Polaris Task Force. Uses Next.js for client- and server-side rendering, with a Postgres database and some other neat libraries.",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
|
|
|
|||
|
|
@ -1,17 +1,18 @@
|
|||
/**
|
||||
* 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).
|
||||
* The plugin sends a `system` prompt that carries the real task: the field's own
|
||||
* system prompt for Compose, or the named action's instruction (Expand,
|
||||
* Simplify, Rephrase, Proofread, Summarize, Tone, Translate) for an action.
|
||||
* For richText Compose it wraps that task in `buildRichTextSystem`, which adds
|
||||
* rules demanding lexical JSON and a blog layout of `[paragraph]` / `[heading]`
|
||||
* markers, contradicting the prose instruction and making the model argue about
|
||||
* the conflict instead of writing.
|
||||
*
|
||||
* 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.
|
||||
* So the caller's system prompt is kept and only the contradicting part is
|
||||
* removed: every field follows one rule (plain prose out; the application
|
||||
* assembles the JSON the Compose client parses), while the action instructions
|
||||
* reach the model unchanged. `AI_OUTPUT_RULES` is appended to every generation.
|
||||
*/
|
||||
|
||||
export const AI_OUTPUT_RULES = `OUTPUT RULES:
|
||||
|
|
@ -25,7 +26,7 @@ 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.
|
||||
* only part of the plugin's richText RULES block worth keeping.
|
||||
*/
|
||||
const BLOCK_PLACEHOLDER_INSTRUCTION =
|
||||
/- The text may contain one or more standalone placeholder tokens[\s\S]*?surrounding text\./;
|
||||
|
|
@ -36,22 +37,53 @@ export const extractBlockPlaceholderInstruction = (pluginSystem?: string): strin
|
|||
return match ? match[0].trim() : "";
|
||||
};
|
||||
|
||||
export const buildComposeSystem = ({
|
||||
pluginSystem,
|
||||
fieldType,
|
||||
}: {
|
||||
pluginSystem?: string;
|
||||
fieldType?: string;
|
||||
}): string => {
|
||||
const parts: string[] = [];
|
||||
/**
|
||||
* `buildRichTextSystem` appends its RULES / LAYOUT INSTRUCTIONS / ADDITIONAL
|
||||
* GUIDELINES block after the caller's own prompt, so cutting at this marker
|
||||
* leaves the field's system prompt (or an action instruction) intact.
|
||||
*/
|
||||
const PLUGIN_RULES_MARKER = "\n\nRULES:";
|
||||
|
||||
if (fieldType === "richText") {
|
||||
const blockInstruction = extractBlockPlaceholderInstruction(pluginSystem);
|
||||
if (blockInstruction) parts.push(blockInstruction);
|
||||
}
|
||||
/**
|
||||
* Backstop for the same text if that marker wording ever changes: the lines that
|
||||
* demand JSON or a layout are dropped wherever they appear. Each pattern matches
|
||||
* a whole line, so an action instruction that merely mentions JSON-ish words in
|
||||
* passing is never touched.
|
||||
*/
|
||||
const CONTRADICTORY_LINE_PATTERNS = [
|
||||
/^-\s*Ensure the output follows the provided rich text JSON schema exactly\.?$/,
|
||||
/^-\s*Produce valid JSON with no undefined or null values\.?$/,
|
||||
/^LAYOUT INSTRUCTIONS:?$/,
|
||||
/^ADDITIONAL GUIDELINES:?$/,
|
||||
/^\[[a-z][a-z]*\]\s*(-|:)/,
|
||||
];
|
||||
|
||||
parts.push(AI_OUTPUT_RULES);
|
||||
return parts.join("\n\n");
|
||||
const stripContradictoryLines = (text: string): string =>
|
||||
text
|
||||
.split("\n")
|
||||
.filter((line) => !CONTRADICTORY_LINE_PATTERNS.some((pattern) => pattern.test(line.trim())))
|
||||
.join("\n")
|
||||
.replace(/\n{3,}/g, "\n\n")
|
||||
.trim();
|
||||
|
||||
const stripPluginOutputDemands = (pluginSystem: string): string => {
|
||||
const markerIndex = pluginSystem.indexOf(PLUGIN_RULES_MARKER);
|
||||
|
||||
if (markerIndex === -1) return stripContradictoryLines(pluginSystem);
|
||||
|
||||
const base = pluginSystem.slice(0, markerIndex);
|
||||
const blockInstruction = extractBlockPlaceholderInstruction(pluginSystem);
|
||||
return stripContradictoryLines([base, blockInstruction].filter(Boolean).join("\n\n"));
|
||||
};
|
||||
|
||||
/**
|
||||
* The system prompt for one generation: whatever task the plugin sent (the
|
||||
* field's system prompt, or a named action's instruction such as Expand or
|
||||
* Simplify) plus the shared output rules, minus the plugin's JSON/layout demands.
|
||||
*/
|
||||
export const buildComposeSystem = ({ pluginSystem }: { pluginSystem?: string } = {}): string => {
|
||||
const base = stripPluginOutputDemands(pluginSystem ?? "");
|
||||
return base ? `${base}\n\n${AI_OUTPUT_RULES}` : AI_OUTPUT_RULES;
|
||||
};
|
||||
|
||||
type JsonSchemaLike = {
|
||||
|
|
|
|||
|
|
@ -784,17 +784,15 @@ export default buildConfig({
|
|||
const shape = resolveProseShape(options.schema);
|
||||
|
||||
// One contract for every supported field: the model writes prose and
|
||||
// the JSON the Compose client parses is assembled here. The plugin's
|
||||
// richText system asks for lexical JSON and a blog layout, which is
|
||||
// exactly what contradicted the prose instruction, so only its
|
||||
// block-placeholder paragraph survives (see composeSystem.ts).
|
||||
// the JSON the Compose client parses is assembled here. The system
|
||||
// prompt keeps the task the plugin sent (the field's own prompt, or a
|
||||
// named action's instruction such as Expand or Simplify) and drops
|
||||
// only the JSON/layout demands that contradicted it, so the action
|
||||
// instructions still reach the model (see composeSystem.ts).
|
||||
if (shape) {
|
||||
const { text, truncated } = await generatePlainText(
|
||||
prompt,
|
||||
buildComposeSystem({
|
||||
pluginSystem: options.system,
|
||||
fieldType: shape.kind === "lexical" ? "richText" : "text",
|
||||
}),
|
||||
buildComposeSystem({ pluginSystem: options.system }),
|
||||
model,
|
||||
{ maxTokens: options.maxTokens, temperature: options.temperature },
|
||||
);
|
||||
|
|
|
|||
|
|
@ -39,51 +39,113 @@ 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",
|
||||
});
|
||||
// Verbatim from the plugin's `defaultPrompts` (dist/ai/prompts.js). These named
|
||||
// actions carry their whole instruction in the system prompt, so anything that
|
||||
// drops it (as this module once did) turns them into a generic prose request.
|
||||
const EXPAND_SYSTEM = `You are a creative writer and subject matter expert. Expand the given text by adding depth, detail, and relevant information while maintaining the original tone and style.
|
||||
|
||||
-------------
|
||||
INSTRUCTIONS:
|
||||
- Understand the main ideas and tone of the text.
|
||||
- Add more details, examples, explanations, or context.
|
||||
- Maintain the original tone, style, and intent.
|
||||
- Ensure the expanded version flows naturally and coherently.
|
||||
- Do not contradict or alter the original meaning.
|
||||
-------------`;
|
||||
|
||||
const SIMPLIFY_SYSTEM = `You are a skilled communicator specializing in clear and concise writing. Simplify the given text to make it easier to understand while retaining its core message.
|
||||
|
||||
-------------
|
||||
INSTRUCTIONS:
|
||||
- Simplify the language, using more common words and shorter sentences.
|
||||
- Remove unnecessary details or jargon while keeping essential information.
|
||||
- Maintain the original meaning and key points.
|
||||
-------------`;
|
||||
|
||||
const REPHRASE_SYSTEM = `You are a skilled language expert. Rephrase the given text while maintaining its original meaning, tone, and emotional content.
|
||||
|
||||
-------------
|
||||
INSTRUCTIONS:
|
||||
- Rephrase the text according to the guidelines of the ORIGINAL SYSTEM PROMPT, if provided.
|
||||
- Retain the original meaning, tone, and emotional content.
|
||||
-------------
|
||||
|
||||
Previous prompt:
|
||||
Summarise the field in two sentences.`;
|
||||
|
||||
describe("buildComposeSystem", () => {
|
||||
it("keeps the field's own system prompt and drops the plugin's JSON/layout rules", () => {
|
||||
const system = buildComposeSystem({ pluginSystem: PLUGIN_RICH_TEXT_SYSTEM });
|
||||
|
||||
expect(system).toContain("professional blog writer");
|
||||
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\]/);
|
||||
expect(system).not.toContain("ADDITIONAL GUIDELINES");
|
||||
});
|
||||
|
||||
it("keeps the block-placeholder paragraph and nothing else from the plugin system", () => {
|
||||
const system = buildComposeSystem({
|
||||
pluginSystem: PLUGIN_RICH_TEXT_SYSTEM,
|
||||
fieldType: "richText",
|
||||
});
|
||||
it("keeps the block-placeholder paragraph so embedded blocks survive", () => {
|
||||
const system = buildComposeSystem({ pluginSystem: PLUGIN_RICH_TEXT_SYSTEM });
|
||||
|
||||
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");
|
||||
expect(system).toContain("the same relative position within the surrounding text.");
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
it("passes every named action's instruction through unchanged", () => {
|
||||
const cases = [
|
||||
[EXPAND_SYSTEM, "Expand the given text by adding depth, detail"],
|
||||
[SIMPLIFY_SYSTEM, "Simplify the given text to make it easier to understand"],
|
||||
[REPHRASE_SYSTEM, "Rephrase the given text while maintaining its original meaning"],
|
||||
] as const;
|
||||
|
||||
for (const [actionSystem, instruction] of cases) {
|
||||
const system = buildComposeSystem({ pluginSystem: actionSystem });
|
||||
|
||||
expect(system).toContain(instruction);
|
||||
expect(system).toContain("INSTRUCTIONS:");
|
||||
// The output contract is still enforced on top of the action.
|
||||
expect(system).toContain("Respond with plain prose only.");
|
||||
expect(system.endsWith(AI_OUTPUT_RULES)).toBe(true);
|
||||
}
|
||||
|
||||
// The Rephrase action also appends the field's previous prompt.
|
||||
expect(buildComposeSystem({ pluginSystem: REPHRASE_SYSTEM })).toContain(
|
||||
"Summarise the field in two sentences.",
|
||||
);
|
||||
});
|
||||
|
||||
it("adds the em-dash rule exactly once for every action and field shape", () => {
|
||||
const systems = [
|
||||
buildComposeSystem({ pluginSystem: PLUGIN_RICH_TEXT_SYSTEM }),
|
||||
buildComposeSystem({ pluginSystem: EXPAND_SYSTEM }),
|
||||
buildComposeSystem({ pluginSystem: SIMPLIFY_SYSTEM }),
|
||||
buildComposeSystem({}),
|
||||
buildComposeSystem({ pluginSystem: undefined }),
|
||||
];
|
||||
|
||||
for (const system of systems) {
|
||||
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",
|
||||
});
|
||||
it("sends only the shared rules when the plugin provides no system prompt", () => {
|
||||
expect(buildComposeSystem({})).toBe(AI_OUTPUT_RULES);
|
||||
expect(buildComposeSystem()).toBe(AI_OUTPUT_RULES);
|
||||
expect(buildComposeSystem({ pluginSystem: "" })).toBe(AI_OUTPUT_RULES);
|
||||
});
|
||||
|
||||
expect(system).toBe(AI_OUTPUT_RULES);
|
||||
it("still drops JSON and layout demands if the plugin's marker wording changes", () => {
|
||||
const reworded = PLUGIN_RICH_TEXT_SYSTEM.replace("\n\nRULES:", "\n\nGUIDELINES:");
|
||||
const system = buildComposeSystem({ pluginSystem: reworded });
|
||||
|
||||
expect(system).not.toContain("rich text JSON schema");
|
||||
expect(system).not.toContain("Produce valid JSON");
|
||||
expect(system).not.toMatch(/LAYOUT INSTRUCTIONS/);
|
||||
expect(system).not.toContain("[paragraph] - Write a concise introduction");
|
||||
expect(system).toContain("professional blog writer");
|
||||
});
|
||||
|
||||
it("sends the real em-dash character to the model", () => {
|
||||
|
|
@ -103,6 +165,7 @@ describe("extractBlockPlaceholderInstruction", () => {
|
|||
it("returns an empty string when there is nothing to keep", () => {
|
||||
expect(extractBlockPlaceholderInstruction(undefined)).toBe("");
|
||||
expect(extractBlockPlaceholderInstruction("just some prose")).toBe("");
|
||||
expect(extractBlockPlaceholderInstruction(EXPAND_SYSTEM)).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue