prompt-optimizer-magpie 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/turn.mjs +14 -1
- package/package.json +1 -1
package/lib/turn.mjs
CHANGED
|
@@ -41,6 +41,15 @@ const REPLYING_TO = /\[Replying to: "[^"]*"\]/g
|
|
|
41
41
|
// shortcut: a multi-paragraph note's later paragraphs leak into case 2 —
|
|
42
42
|
// fail-open (optimizer sees note text), never loses user words.
|
|
43
43
|
const BC_RESUME = /(?:^|\n)\n?\[billion-context\][\s\S]*$/
|
|
44
|
+
// nanobot host appends a [Runtime Context] sandbox-metadata trailer to the
|
|
45
|
+
// user message — host words, never user words, and an optimizer rewrite of
|
|
46
|
+
// the span could mangle it. Shape chosen: paired-strip first (keep real
|
|
47
|
+
// user text after the closing tag), then trailer-strip to end for unclosed
|
|
48
|
+
// or trailing blocks — same fail-open cut as BC_RESUME: keep the text
|
|
49
|
+
// before, and a block-only message becomes "" (caller's `clean ||
|
|
50
|
+
// undefined` drops the turn).
|
|
51
|
+
const RUNTIME_CONTEXT_PAIRED = /(?:^|\n)\n?\[Runtime Context[^\]]*\][\s\S]*?\[\/Runtime Context\][^\S\n]*(?:\n|$)/g
|
|
52
|
+
const RUNTIME_CONTEXT_TRAILER = /(?:^|\n)\n?\[Runtime Context[^\]]*\][\s\S]*$/
|
|
44
53
|
export function stripBC(text) {
|
|
45
54
|
const m = text.match(BC_RESUME);
|
|
46
55
|
if (!m) return text;
|
|
@@ -48,6 +57,10 @@ export function stripBC(text) {
|
|
|
48
57
|
if (before.trim()) return before;
|
|
49
58
|
return text.split(/\n\n+/).slice(1).join("\n\n").trim();
|
|
50
59
|
}
|
|
60
|
+
export function stripRuntimeContext(text) {
|
|
61
|
+
const paired = text.replace(RUNTIME_CONTEXT_PAIRED, "");
|
|
62
|
+
return paired === text ? text.replace(RUNTIME_CONTEXT_TRAILER, "") : paired;
|
|
63
|
+
}
|
|
51
64
|
const STRIP_BLOCKS = [
|
|
52
65
|
/<system-reminder>[\s\S]*?<\/system-reminder>/g,
|
|
53
66
|
tag("task-notification"),
|
|
@@ -148,7 +161,7 @@ function userTextOf(m, protocol) {
|
|
|
148
161
|
if (texts.some((t) => SKIP_NEEDLES.some((n) => t.includes(n)))) return undefined
|
|
149
162
|
let clean = texts.join("")
|
|
150
163
|
for (const re of STRIP_BLOCKS) clean = clean.replace(re, "")
|
|
151
|
-
clean = stripBC(clean.replace(ACP_LINE, "").replace(REPLYING_TO, ""))
|
|
164
|
+
clean = stripRuntimeContext(stripBC(clean.replace(ACP_LINE, "").replace(REPLYING_TO, "")))
|
|
152
165
|
// Grok CLI wraps the whole user turn in <user_query>…</user_query>; unwrap
|
|
153
166
|
// when it covers the message so the optimizer sees the raw prompt.
|
|
154
167
|
const uq = clean.match(/^\s*<user_query>([\s\S]*?)<\/user_query>\s*$/)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prompt-optimizer-magpie",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Self-contained Magpie gateway request hook that rewrites vague first-turn prompts into precise ones before they reach your model. Fail-open by design.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "AGPL-3.0-only",
|