Files
plugins/packages/output-strategy/page/app.js
T
Mañjūṣaka 081c67acaa output-strategy 0.2.0: composable output-style middleware (#814)
Adds @magpie-community/middleware-output-strategy 0.2.0. It is middleware only. It appends composable output-style instructions before the model call, and has a small selection page.
- Codex `instructions` stay byte-identical. The styles go into a `developer` input item at input[0].
- An existing marker means the body was already applied, and it passes unchanged, so the prompt cache holds across turns.
- It writes nothing to disk and has no hooks or telemetry.
- PROVENANCE credits OmniRoute v3.8.52.

Run at 9b47678ba (mote, Bun 1.4.2, HOME=$(mktemp -d)):
- package bun test: 49 pass, 0 fail;
- repo bun test: 6555 pass, 0 fail;
- scripts/check.mjs output-strategy: ok;
- magpie's moejs engine at origin/main 8e350155b: the Claude Code system field is identical over four turns, and the Codex input prefix is stable;
- 12 guards broken by hand, each one red.
- PR CI (test, on-npm): green.

Not tried: real accounts, Codex over websocket, Windows, Linux.
2026-11-07 08:07:11 +08:00

170 lines
5.5 KiB
JavaScript

(function () {
"use strict"
var i18n = globalThis.OutputStrategyI18n
var t = function (text) { return i18n ? i18n.t(text) : text }
var byId = function (id) { return document.getElementById(id) }
var form = byId("stylesForm")
var fieldset = byId("stylesFieldset")
var list = byId("stylesList")
var saveButton = byId("saveButton")
var message = byId("formMessage")
var options = {}
var inputs = Object.create(null)
var loaded = false
var saving = false
var dirty = false
var lastMessage = ["Reading settings…", false]
var catalog = readCatalog(globalThis.OUTPUT_STRATEGY_CATALOG)
function readCatalog(value) {
var entries = Array.isArray(value) ? value : value && Array.isArray(value.styles) ? value.styles : []
return entries.filter(function (entry) {
return entry && typeof entry.id === "string" && entry.id.length > 0
}).map(function (entry) {
return {
id: entry.id,
label: typeof entry.label === "string" && entry.label ? entry.label : entry.id,
description: typeof entry.description === "string" ? entry.description : "",
}
})
}
function element(tag, className, text) {
var node = document.createElement(tag)
if (className) node.className = className
if (text != null) node.textContent = text
return node
}
function clear(node) {
while (node.firstChild) node.removeChild(node.firstChild)
}
function setMessage(text, isError) {
lastMessage = [text, isError === true]
message.textContent = t(text)
message.className = "form-message" + (isError ? " error" : "")
}
function selectedTotal() {
return catalog.reduce(function (total, style) {
return total + (inputs[style.id] && inputs[style.id].checked ? 1 : 0)
}, 0)
}
function updateSelection() {
byId("selectedCount").textContent = selectedTotal() + " / " + catalog.length
}
function updateControls() {
var unavailable = !loaded || saving || catalog.length === 0
fieldset.disabled = unavailable
saveButton.disabled = unavailable || !dirty
saveButton.textContent = t(saving ? "Saving…" : "Save changes")
}
function renderCatalogState() {
byId("catalogState").textContent = catalog.length
? t("{n} styles; injected in catalog order.").replace("{n}", String(catalog.length))
: t("Catalog not loaded")
}
function renderCatalog() {
clear(list)
inputs = Object.create(null)
renderCatalogState()
if (!catalog.length) {
list.append(element("p", "empty-state", t("Catalog not loaded; style toggles unavailable.")))
updateSelection()
updateControls()
return
}
catalog.forEach(function (style) {
var input = element("input")
var copy = element("span", "style-copy")
var row = element("label", "style-row")
input.type = "checkbox"
input.id = "style-" + style.id
input.name = style.id
input.setAttribute("data-style-id", style.id)
input.addEventListener("change", function () {
dirty = true
setMessage("Unsaved changes.")
updateSelection()
updateControls()
})
copy.append(element("span", "style-name", t(style.label)), element("span", "style-description", t(style.description)))
row.setAttribute("for", input.id)
row.append(copy, input)
list.append(row)
inputs[style.id] = input
})
updateSelection()
updateControls()
}
function applyOptions(next) {
options = next && typeof next === "object" ? next : {}
var styles = options.styles && typeof options.styles === "object" ? options.styles : {}
catalog.forEach(function (style) {
if (inputs[style.id]) inputs[style.id].checked = styles[style.id] === true
})
dirty = false
updateSelection()
updateControls()
}
function save(event) {
event.preventDefault()
if (!loaded || saving || !catalog.length) return
var styles = Object.assign({}, options.styles && typeof options.styles === "object" ? options.styles : {})
catalog.forEach(function (style) { styles[style.id] = inputs[style.id].checked === true })
var next = Object.assign({}, options, { styles: styles })
saving = true
setMessage("Saving…")
updateControls()
Promise.resolve()
.then(function () { return globalThis.magpie.options.set(next) })
.then(function (saved) {
applyOptions(saved && typeof saved === "object" ? saved : next)
setMessage("Saved.")
})
.catch(function () { setMessage("Could not save. Nothing was written; try again.", true) })
.then(function () {
saving = false
updateControls()
})
}
// The sandboxed frame has CSP form-action 'none'; call save directly.
saveButton.addEventListener("click", save)
form.addEventListener("submit", save)
renderCatalog()
if (i18n) i18n.onChange(function () {
renderCatalog()
setMessage(lastMessage[0], lastMessage[1])
})
var host = globalThis.magpie
if (!host || !host.options || typeof host.options.get !== "function" || typeof host.options.set !== "function") {
setMessage("Configuration bridge unavailable; view only.", true)
updateControls()
return
}
Promise.resolve()
.then(function () { return host.options.get() })
.then(function (current) {
loaded = true
applyOptions(current)
setMessage(catalog.length ? "Pick styles and save." : "Catalog not loaded; styles cannot be changed.", !catalog.length)
})
.catch(function () {
loaded = false
setMessage("Could not read settings; reopen the page.", true)
updateControls()
})
})()