Files
Mañjūṣaka 006e96ef03 output-strategy 0.2.1: finish #814's review follow-ups (#815)
Follow-up to #814 (081c67ac). The core request injection is unchanged.

- Page: static text goes back to English when magpie.theme.lang changes
  from zh to en. Unsaved style picks survive a language change, and the
  title follows the language. zh / zh-CN / zh-Hans use the Simplified
  strings. zh-TW and other languages fall back to English on purpose,
  with <html lang> kept as zh-TW. With no host theme, the page loads in
  English without throwing.
- apply.js: an empty system message gets the style block with no
  leading blank lines. The unused media detector and addedBytes are
  removed.
- style.css: obsolete stats/lang/workspace rules are removed, and the
  720px media block is closed again (52 { / 52 }).
- Tests: lowercase "delete" in the cross-turn cache case;
  empty/null/object system; zh -> en restore; unsaved picks kept;
  zh-TW; a missing theme; style.css brace balance and the version.
- PROVENANCE names ANTISLOP_BOUNDARIES (OmniRoute v3.8.52), checked
  byte for byte against the tag. The page/stats.js ignore is gone.
- Version 0.2.1. dist had only 0.2.0, and npm has no version yet.

Run at 2f971ab9 (mote, Bun 1.4.2, fresh HOME):
- package bun test: 55 pass, 0 fail;
- repo bun test: 6561 pass, 20 skip, 0 fail;
- scripts/check.mjs output-strategy: ok;
- CI run 42320776930 at this head, attempt 1: test and on-npm green;
- page render, main vs head, in Chromium and WebKit at 400/700/1000px:
  identical.
Broken by hand, each turning exactly one test red: the dropped }, the
version at 0.2.0, an unguarded theme.lang, the old conditional
applyTheme, a case-sensitive delete skip, the old appendInstruction,
one-way translate, unkept checkbox state, the old zh* normalize, and
addedBytes put back.
Not tried: a live theme switch in the real magpie window, and real
Claude Code/Codex sessions.
2026-11-07 09:10:31 +08:00

145 lines
6.2 KiB
JavaScript

import {
OUTPUT_STYLE_CATALOG,
OUTPUT_STYLE_IDS,
SHARED_BOUNDARIES,
} from "./catalog.js"
export const OUTPUT_STYLE_MARKER = "[Output Strategy]"
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value)
}
function resolveStyles(selection) {
if (isRecord(selection) && Object.prototype.hasOwnProperty.call(selection, "styles")) selection = selection.styles
if (isRecord(selection)) return OUTPUT_STYLE_IDS.filter((id) => selection[id] === true)
if (!Array.isArray(selection)) return []
const selected = new Set()
for (const entry of selection) {
const id = typeof entry === "string" ? entry : isRecord(entry) ? entry.id : null
if (typeof id === "string") selected.add(id)
}
return OUTPUT_STYLE_IDS.filter((id) => selected.has(id))
}
export function buildOutputStylesInstruction(selection) {
const resolved = resolveStyles(selection)
if (resolved.length === 0) return ""
const instructions = resolved.map((id) => OUTPUT_STYLE_CATALOG[id].instruction.trim())
const boundaries = [SHARED_BOUNDARIES]
for (const id of resolved) {
const boundary = OUTPUT_STYLE_CATALOG[id].boundaries
if (boundary && !boundaries.includes(boundary)) boundaries.push(boundary)
}
return `${OUTPUT_STYLE_MARKER}\n${instructions.join("\n\n")}\n\n${boundaries.join("\n")}`
}
function contentIncludesMarker(content) {
if (typeof content === "string") return content.includes(OUTPUT_STYLE_MARKER)
if (!Array.isArray(content)) return false
return content.some((block) => isRecord(block) && contentIncludesMarker(typeof block.text === "string" ? block.text : block.content))
}
// Scan every place injection can land, regardless of which request shape the
// body currently takes, so a body re-shaped between pipeline stages is never
// injected twice. User-authored text is deliberately not scanned: a user
// quoting the marker must not disable injection.
function bodyIncludesMarker(body) {
if (contentIncludesMarker(body.instructions)) return true
if (contentIncludesMarker(body.system)) return true
if (Array.isArray(body.messages)) {
if (body.messages.some((message) => isRecord(message) && (message.role === "system" || message.role === "developer") && contentIncludesMarker(message.content))) return true
}
if (Array.isArray(body.input)) {
if (body.input.some((item) => isRecord(item) && (item.role === "system" || item.role === "developer") && contentIncludesMarker(item.content))) return true
}
return false
}
function appendInstruction(existing, instruction) {
const trimmed = existing.trimEnd()
return trimmed ? `${trimmed}\n\n${instruction}` : instruction
}
function placeSystemInstruction(messages, system, instruction, protocol) {
const first = messages[0]
if (first?.role === "system" && typeof first.content === "string") {
const nextMessages = [...messages]
nextMessages[0] = { ...first, content: appendInstruction(first.content, instruction) }
return { messages: nextMessages }
}
if (typeof system === "string") {
return { system: system ? appendInstruction(system, instruction) : instruction }
}
if (Array.isArray(system)) {
return { system: [...system, { type: "text", text: instruction }] }
}
if (system !== undefined && system !== null) {
// A non-string, non-array system field is someone else's contract
// (some proxies send {type:"text",...}); replacing it is data loss.
return null
}
const mergeIndex = messages.findIndex(
(message) => message?.role === "system" && typeof message.content === "string",
)
if (mergeIndex >= 0) {
const nextMessages = [...messages]
const target = messages[mergeIndex]
nextMessages[mergeIndex] = { ...target, content: appendInstruction(target.content, instruction) }
return { messages: nextMessages }
}
// Anthropic only accepts user/assistant inside messages; system belongs at the top level.
if (protocol === "anthropic") return { system: instruction }
return { messages: [{ role: "system", content: instruction }, ...messages] }
}
function placeOrSkip(body, messages, instruction, protocol, resolved) {
const placed = placeSystemInstruction(messages, body.system, instruction, protocol)
if (!placed) return { body, applied: false, skippedReason: "unsupported_system_shape" }
return appliedResult({ ...body, ...placed }, resolved)
}
function responsesInput(body, instruction) {
const message = { role: "developer", content: [{ type: "input_text", text: instruction }] }
return Array.isArray(body.input) ? [message, ...body.input] : [message, { role: "user", content: body.input }]
}
function appliedResult(body, appliedStyles) {
return { body, applied: true, appliedStyles }
}
export function applyOutputStyles(body, selection) {
try {
const resolved = resolveStyles(selection)
if (resolved.length === 0) return { body, applied: false, skippedReason: "no_styles" }
if (!isRecord(body)) return { body, applied: false, skippedReason: "no_messages" }
if (bodyIncludesMarker(body)) return { body, applied: false, skippedReason: "already_applied" }
const instruction = buildOutputStylesInstruction(resolved)
const messages = Array.isArray(body.messages) ? body.messages : null
const responsesShape =
(!messages || messages.length === 0) &&
(typeof body.instructions === "string" || typeof body.input === "string" || Array.isArray(body.input))
if (responsesShape) {
// Codex owns instructions: its identity and base instructions must stay
// byte-identical. The selected styles live in their own input item.
if (body.instructions !== undefined && typeof body.instructions !== "string") {
return { body, applied: false, skippedReason: "unsupported_instructions_shape" }
}
if (typeof body.input !== "string" && !Array.isArray(body.input)) {
return { body, applied: false, skippedReason: "no_messages" }
}
return appliedResult({ ...body, input: responsesInput(body, instruction) }, resolved)
}
if (!messages) return { body, applied: false, skippedReason: "no_messages" }
return placeOrSkip(body, messages, instruction, isRecord(selection) ? selection.protocol : undefined, resolved)
} catch {
return { body, applied: false, skippedReason: "error" }
}
}