prompt-optimizer-magpie 0.2.3 → 0.2.4

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.
@@ -9,9 +9,36 @@
9
9
  import { readFileSync, writeFileSync, renameSync, unlinkSync } from "node:fs";
10
10
  import { join, dirname } from "node:path";
11
11
  import { randomBytes } from "node:crypto";
12
+ import { createRequire } from "node:module";
12
13
 
13
14
  export const SPEC = process.env.POM_PLUGIN_SPEC ?? "/root/prompt-optimizer-magpie";
14
15
 
16
+ // This plugin's npm name — the match target for fuzzy spec lookup.
17
+ const NAME = (() => {
18
+ try { return createRequire(import.meta.url)("../package.json").name; }
19
+ catch { return "prompt-optimizer-magpie"; }
20
+ })();
21
+
22
+ // Three-tier spec match (.omp-spec-brief.md): an explicit POM_PLUGIN_SPEC
23
+ // keeps literal equality (70 drop-in compat); otherwise entries match by
24
+ // npm install form, preferring npm: > bare/bare@ver > path entries.
25
+ export function matchEntry(doc) {
26
+ const plugins = Array.isArray(doc?.plugins) ? doc.plugins : [];
27
+ if (process.env.POM_PLUGIN_SPEC) {
28
+ return plugins.find((p) => p && p.spec === SPEC) ?? null;
29
+ }
30
+ const tiers = [
31
+ (s) => s === `npm:${NAME}` || s.startsWith(`npm:${NAME}@`),
32
+ (s) => s === NAME || (s.startsWith(`${NAME}@`) && s.length > NAME.length + 1),
33
+ (s) => s.startsWith("/") && s.replace(/\/+$/, "").endsWith(`/${NAME}`),
34
+ ];
35
+ for (const hit of tiers) {
36
+ const e = plugins.find((p) => p && typeof p.spec === "string" && hit(p.spec));
37
+ if (e) return e;
38
+ }
39
+ return null;
40
+ }
41
+
15
42
  // Whitelist + validation (brief 需求 1 表格). `type` drives form rendering;
16
43
  // `check` returns an error string (keyed for T lookup) or null.
17
44
  export const CONFIG_FIELDS = [
@@ -84,7 +111,7 @@ export function readPluginsJson(file) {
84
111
  const doc = JSON.parse(raw);
85
112
  if (!doc || typeof doc !== "object" || !Array.isArray(doc.plugins))
86
113
  return { error: "cfgErrBadFile" };
87
- const entry = doc.plugins.find((p) => p && p.spec === SPEC);
114
+ const entry = matchEntry(doc);
88
115
  if (!entry || typeof entry.options !== "object" || entry.options === null)
89
116
  return { error: "cfgErrNoSpec" };
90
117
  return { doc, entry, options: entry.options };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prompt-optimizer-magpie",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
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",
package/page/app.js CHANGED
@@ -19,25 +19,28 @@
19
19
  s.onerror = res; // a missing file is fine: fixed-port installs have no port.js
20
20
  document.head.appendChild(s);
21
21
  });
22
- (async () => {
23
- let port = FALLBACK_PORT;
22
+ (async () => {
23
+ // server() writes port.js beside this file once bound (port 0
24
+ // auto-select): same-dir script loads pass the sandbox CSP, and it is
25
+ // the plugin's own source of truth. The host's hello.service falls
26
+ // back to the manifest port when the option is 0, so the bridge
27
+ // service URL and options.port only apply when port.js gave nothing.
28
+ await load("./port.js");
29
+ const v = Number(window.__POM_PORT__);
30
+ const runtime = Number.isInteger(v) && v > 0 && v < 65536 ? v : 0;
31
+ let port = runtime || FALLBACK_PORT;
24
32
  let service = "";
25
33
  try {
26
34
  await load("./_magpie/bridge.js");
27
35
  await window.magpie?.ready; // rejects outside a magpie frame — degrade
28
- const opts = await window.magpie?.options?.get?.();
29
- const v = Number(opts?.port);
30
- if (Number.isInteger(v) && v > 0 && v < 65536) port = v;
31
- const s = window.magpie?.service;
32
- if (typeof s === "string" && /^https?:\/\//.test(s)) service = s;
36
+ if (!runtime) {
37
+ const s = window.magpie?.service;
38
+ if (typeof s === "string" && /^https?:\/\//.test(s)) service = s;
39
+ const opts = await window.magpie?.options?.get?.();
40
+ const p = Number(opts?.port);
41
+ if (!service && Number.isInteger(p) && p > 0 && p < 65536) port = p;
42
+ }
33
43
  } catch { /* stay on defaults */ }
34
- // server() writes port.js beside this file once bound (port 0
35
- // auto-select): same-dir script loads pass the sandbox CSP, and it works
36
- // on desktop too, where magpie.service stays "" and the host's
37
- // hello.service falls back to the manifest port when the option is 0.
38
- await load("./port.js");
39
- const v = Number(window.__POM_PORT__);
40
- if (Number.isInteger(v) && v > 0 && v < 65536) port = v;
41
44
  const host = location.hostname || "127.0.0.1";
42
45
  location.replace(service || `http://${host}:${port}/`);
43
46
  clearTimeout(timer);