New package @magpie-community/opencode-openai-decisions-auth (provider
openai-decisions, model gpt-6-luna) by genglintong: answers magpie's
System One (base /systemone through its fetch) with OpenAI's
POST /v1/decisions, converting choice/score/noul to choice/score/predicate
and back with probabilities, confidence and usage. Sign-in is a plain
OpenAI API key from the host's getAuth, read again per request.
The six points of the 11-05 review, checked at 613fc413a:
1. credentials.mjs, the saved-key reader and the keyless method are gone;
no read of providers.json, APPDATA or ~/.config/magpie anywhere.
2. A per-question {"type":"refusal"} is left out of answers; the others
and usage are kept (all refused gives answers {}). magpie's own
Decisions translation reads a missing answer as a refusal too.
3. Omitted, null or blank instructions get a non-empty default.
4. Registry entry on one line; merges cleanly with origin/main 9204c079.
5. Renamed; package.json magpie.icon and registry icon are an SVG data URI.
6. No test writes to disk; comments in English; the only repo link left is
this one (NOTICE, for the opencode2.mjs bridge, byte-identical to
scripts/opencode2.mjs at main).
Run at 613fc413a and at its merge with origin/main 9204c079, off the Mac:
- WSL magpie-desktop, fresh HOME: bun test packages/openai-decisions 23
pass 0 fail; bun scripts/check.mjs 56/56 (merged tree 57/57);
node 22.23.4 --test test/*.test.mjs 23/23 on the merged tree; full
bun test of the merged tree: 8111 pass, 19 skip, 0 fail.
- Windows xiaomi-gpu, fresh HOME and USERPROFILE, Bun 1.4.2: 23 pass 0
fail, check.mjs 56/56.
- Broken by hand, each red then restored: refusal thrown again (3 red),
blank instructions passed through (1 red), key captured at load instead
of per request (1 red), redirect:"error" dropped (1 red), URL check
dropped (1 red).
- Tests reach no network (fetch injected or globalThis.fetch stubbed).
npm pack --dry-run lists the 10 files; every runtime import is in files.
Not tried: a real OpenAI Decisions call (no credentials used), Windows or
Linux in a real magpie.
229 lines
13 KiB
JavaScript
229 lines
13 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { createDecisionFetch, fromOpenAIResponse, toOpenAIRequest } from "../adapter.mjs";
|
|
|
|
const body = () => ({
|
|
model: "gpt-6-luna",
|
|
state: { message: "What does HTTP 404 mean?", previous_message_kind: "question" },
|
|
questions: {
|
|
intent: { type: "choice", instructions: "Which task?", criteria: { question: "Explain a fact", edit: null } },
|
|
effort: { type: "score", instructions: "How hard?", criteria: ["Little work", "Some work", "Much work", "Most work"] },
|
|
levels: { type: "noul", instructions: "Are these kinds levels of one scale?" },
|
|
},
|
|
});
|
|
|
|
const reply = () => ({
|
|
id: "decision-test", model: "gpt-6-luna", usage: { input_tokens: 42, output_tokens: 0 },
|
|
answers: [
|
|
{ name: "levels", type: "predicate", probability: 0.9 },
|
|
{ name: "intent", type: "choice", choice: "question", confidence: 0.8, probabilities: [{ value: "question", probability: 0.9 }, { value: "edit", probability: 0.1 }] },
|
|
{ name: "effort", type: "score", score: 0.6, confidence: 0.3, probabilities: [0.6, 0.2, 0.2, 0].map((probability, value) => ({ value, label: String(value), probability })) },
|
|
],
|
|
});
|
|
|
|
test("converts JSON evidence and all three native classifier questions", () => {
|
|
const source = body();
|
|
const out = toOpenAIRequest(source);
|
|
assert.equal(out.model, "gpt-6-luna");
|
|
assert.deepEqual(out.input, [{ role: "user", content: [{ type: "input_json", data: source.state }] }]);
|
|
assert.deepEqual(out.questions[0], { name: "intent", type: "choice", instructions: "Which task?", choices: [{ value: "question", description: "Explain a fact" }, { value: "edit" }] });
|
|
assert.deepEqual(out.questions[1].levels, source.questions.effort.criteria.map((description, index) => ({ label: String(index), description })));
|
|
assert.deepEqual(out.questions[2], { name: "levels", type: "predicate", instructions: source.questions.levels.instructions });
|
|
assert.equal(source.questions.levels.type, "noul");
|
|
});
|
|
|
|
test("accepts text evidence and Magpie's documented choices shorthand", () => {
|
|
const source = { model: "gpt-6-luna", state: "hello", questions: { intent: { type: "choice", instructions: "Kind?", choices: ["question", "edit"] } } };
|
|
assert.equal(toOpenAIRequest(source).input, "hello");
|
|
assert.deepEqual(toOpenAIRequest(source).questions[0].choices, [{ value: "question" }, { value: "edit" }]);
|
|
});
|
|
|
|
test("numeric and boolean System One state use text instead of an invalid input_json scalar", () => {
|
|
for (const state of [42, true, false]) {
|
|
assert.equal(toOpenAIRequest({ ...body(), state }).input, JSON.stringify(state));
|
|
}
|
|
});
|
|
|
|
test("retains structured instructions and criteria, including predicate definitions", () => {
|
|
const source = { model: "gpt-6-luna", state: [1, 2], questions: {
|
|
kind: { type: "choice", instructions: { question: "Which?", record: { id: 1 } }, criteria: { first: { examples: [1] }, other: null } },
|
|
yes: { type: "noul", instructions: "Does this match?", criteria: { true: { rule: "positive" }, false: "negative" } },
|
|
rating: { type: "score", criteria: [1, { label: "higher" }, null] },
|
|
} };
|
|
const out = toOpenAIRequest(source);
|
|
assert.deepEqual(JSON.parse(out.questions[0].instructions), source.questions.kind.instructions);
|
|
assert.deepEqual(JSON.parse(out.questions[0].choices[0].description), { examples: [1] });
|
|
assert.deepEqual(JSON.parse(out.questions[1].instructions), { instructions: "Does this match?", criteria: source.questions.yes.criteria });
|
|
assert.deepEqual(out.questions[2].levels, [{ label: "0", description: "1" }, { label: "1", description: '{"label":"higher"}' }, { label: "2" }]);
|
|
});
|
|
|
|
test("supplies non-empty instructions for omitted, null and blank System One instructions", () => {
|
|
for (const instructions of [undefined, null, "", " \n\t"]) {
|
|
const source = body();
|
|
for (const question of Object.values(source.questions)) question.instructions = instructions;
|
|
for (const question of toOpenAIRequest(source).questions) {
|
|
assert.equal(typeof question.instructions, "string");
|
|
assert.ok(question.instructions.trim().length > 0);
|
|
}
|
|
assert.equal(source.questions.intent.instructions, instructions);
|
|
source.questions.levels.criteria = { true: "Matches", false: "Does not match" };
|
|
const predicate = toOpenAIRequest(source).questions[2];
|
|
const combined = JSON.parse(predicate.instructions);
|
|
assert.ok(combined.instructions.trim().length > 0);
|
|
assert.deepEqual(combined.criteria, source.questions.levels.criteria);
|
|
}
|
|
});
|
|
|
|
test("restores named answers independent of order and preserves probabilities, confidence and usage", () => {
|
|
const out = fromOpenAIResponse(reply(), body());
|
|
assert.equal(out.answers.intent.choice, "question");
|
|
assert.deepEqual(out.answers.intent.probabilities, { question: 0.9, edit: 0.1 });
|
|
assert.equal(out.answers.intent.confidence, 0.8);
|
|
assert.equal(out.answers.effort.score, 0.6);
|
|
assert.deepEqual(out.answers.effort.probabilities, { 0: 0.6, 1: 0.2, 2: 0.2, 3: 0 });
|
|
assert.deepEqual(out.answers.levels, { type: "noul", noul: 0.9 });
|
|
assert.deepEqual(out.usage, { input_tokens: 42, output_tokens: 0 });
|
|
assert.equal(out.id, "decision-test");
|
|
});
|
|
|
|
test("omits a per-question refusal while preserving the other answers and usage", () => {
|
|
for (const name of ["intent", "effort", "levels"]) {
|
|
const upstream = reply();
|
|
upstream.answers = upstream.answers.map((answer) => answer.name === name ? { name, type: "refusal" } : answer);
|
|
const out = fromOpenAIResponse(upstream, body());
|
|
assert.deepEqual(Object.keys(out.answers), Object.keys(body().questions).filter((key) => key !== name));
|
|
assert.equal(Object.hasOwn(out.answers, name), false);
|
|
assert.deepEqual(out.usage, upstream.usage);
|
|
for (const [key, answer] of Object.entries(fromOpenAIResponse(reply(), body()).answers)) {
|
|
if (key !== name) assert.deepEqual(out.answers[key], answer);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("an all-refusal response keeps empty answers instead of inventing probabilities", () => {
|
|
const upstream = reply();
|
|
upstream.answers = upstream.answers.map(({ name }) => ({ name, type: "refusal" }));
|
|
const out = fromOpenAIResponse(upstream, body());
|
|
assert.deepEqual(out.answers, {});
|
|
assert.deepEqual(out.usage, upstream.usage);
|
|
});
|
|
|
|
test("refusals still reject duplicate, unknown and missing question answers", () => {
|
|
for (const answers of [
|
|
[{ name: "intent", type: "refusal" }, { name: "intent", type: "refusal" }, reply().answers[2]],
|
|
[{ name: "unknown", type: "refusal" }, reply().answers[1], reply().answers[2]],
|
|
[{ name: "intent", type: "refusal" }],
|
|
]) assert.throws(() => fromOpenAIResponse({ ...reply(), answers }, body()));
|
|
});
|
|
|
|
test("does not treat prototype-named choices or question IDs as object properties", () => {
|
|
const source = JSON.parse('{"model":"gpt-6-luna","state":"hello","questions":{"__proto__":{"type":"choice","instructions":"Kind?","criteria":{"__proto__":null,"constructor":null}}}}');
|
|
const out = fromOpenAIResponse({ model: "gpt-6-luna", answers: [{ name: "__proto__", type: "choice", choice: "__proto__", confidence: 1, probabilities: [{ value: "__proto__", probability: 1 }, { value: "constructor", probability: 0 }] }] }, source);
|
|
assert.equal(Object.hasOwn(out.answers, "__proto__"), true);
|
|
assert.equal(Object.hasOwn(out.answers.__proto__.probabilities, "__proto__"), true);
|
|
});
|
|
|
|
test("rejects unsupported fields and question types before requesting the upstream", () => {
|
|
for (const source of [
|
|
{ ...body(), stream: true },
|
|
{ ...body(), model: "another-model" },
|
|
{ ...body(), questions: {} },
|
|
{ ...body(), questions: { x: { type: "text", instructions: "Explain" } } },
|
|
{ ...body(), questions: { x: { type: "choice", instructions: "Kind?", criteria: { only: null } } } },
|
|
{ ...body(), questions: { x: { ...body().questions.intent, choices: ["question", "edit"] } } },
|
|
{ ...body(), questions: { x: { ...body().questions.intent, temperature: 0 } } },
|
|
{ ...body(), questions: { x: { type: "noul", criteria: { other: "not a boolean definition" } } } },
|
|
]) assert.throws(() => toOpenAIRequest(source));
|
|
});
|
|
|
|
test("rejects missing, duplicate, wrong-type and unrequested answers", () => {
|
|
for (const mutate of [
|
|
(r) => r.answers.pop(),
|
|
(r) => r.answers.push(r.answers[0]),
|
|
(r) => { r.answers[0].name = "unexpected"; },
|
|
(r) => { r.answers[0].type = "choice"; },
|
|
]) {
|
|
const upstream = reply();
|
|
mutate(upstream);
|
|
assert.throws(() => fromOpenAIResponse(upstream, body()));
|
|
}
|
|
});
|
|
|
|
test("rejects invalid or incomplete probability distributions and invalid scores", () => {
|
|
for (const mutate of [
|
|
(r) => { r.answers[1].probabilities[0].probability = NaN; },
|
|
(r) => { r.answers[1].probabilities[0].probability = 1.1; },
|
|
(r) => r.answers[1].probabilities.pop(),
|
|
(r) => { r.answers[1].probabilities[1].value = "question"; },
|
|
(r) => { r.answers[1].probabilities[0].probability = 0.1; },
|
|
(r) => { r.answers[1].choice = "unknown"; },
|
|
(r) => { r.answers[1].confidence = -1; },
|
|
(r) => { r.answers[2].score = 4; },
|
|
(r) => { r.answers[0].probability = -0.1; },
|
|
]) {
|
|
const upstream = reply();
|
|
mutate(upstream);
|
|
assert.throws(() => fromOpenAIResponse(upstream, body()));
|
|
}
|
|
});
|
|
|
|
test("fetch uses the fixed Decisions URL, current key and a fresh header set", async () => {
|
|
let call;
|
|
const fetcher = createDecisionFetch({ getKey: () => "fixture-upstream-key", fetchImpl: async (url, init) => { call = { url, init }; return Response.json(reply(), { headers: { "x-request-id": "req-test", "set-cookie": "not-forwarded" } }); } });
|
|
const response = await fetcher("https://api.openai.com/v1/systemone", { method: "POST", headers: { authorization: "Bearer local-gateway-key", "x-magpie-private": "not-forwarded" }, body: JSON.stringify(body()) });
|
|
assert.equal(response.status, 200);
|
|
assert.equal(call.url, "https://api.openai.com/v1/decisions");
|
|
assert.equal(call.init.redirect, "error");
|
|
assert.equal(call.init.headers.get("authorization"), "Bearer fixture-upstream-key");
|
|
assert.equal(call.init.headers.has("x-magpie-private"), false);
|
|
assert.equal(response.headers.has("set-cookie"), false);
|
|
assert.equal(response.headers.get("x-request-id"), "req-test");
|
|
assert.equal((await response.json()).answers.intent.choice, "question");
|
|
});
|
|
|
|
test("does not send malformed requests or arbitrary endpoints upstream", async () => {
|
|
let sent = 0;
|
|
const fetcher = createDecisionFetch({ getKey: () => "fixture", fetchImpl: async () => { sent++; throw Error("should not send"); } });
|
|
for (const [url, init] of [
|
|
["https://elsewhere.example/v1/systemone", { method: "POST", body: JSON.stringify(body()) }],
|
|
["https://api.openai.com/v1/chat/completions", { method: "POST", body: JSON.stringify(body()) }],
|
|
["https://api.openai.com/v1/systemone?upstream=elsewhere", { method: "POST", body: JSON.stringify(body()) }],
|
|
["https://api.openai.com/v1/systemone", { method: "GET" }],
|
|
["https://api.openai.com/v1/systemone", { method: "POST", body: "invalid-json" }],
|
|
]) assert.equal((await fetcher(url, init)).status, 400);
|
|
assert.equal(sent, 0);
|
|
});
|
|
|
|
test("preserves upstream 401/429/503 status, error body and retry headers", async () => {
|
|
for (const status of [401, 429, 503]) {
|
|
const raw = JSON.stringify({ error: { message: "Upstream refused this request" } });
|
|
const fetcher = createDecisionFetch({ getKey: () => "fixture", fetchImpl: async () => new Response(raw, { status, headers: { "retry-after": "5", "content-type": "application/json" } }) });
|
|
const response = await fetcher("https://api.openai.com/v1/systemone", { method: "POST", body: JSON.stringify(body()) });
|
|
assert.equal(response.status, status);
|
|
assert.equal(await response.text(), raw);
|
|
assert.equal(response.headers.get("retry-after"), "5");
|
|
}
|
|
});
|
|
|
|
test("an incomplete successful upstream response becomes 502", async () => {
|
|
const fetcher = createDecisionFetch({ getKey: () => "fixture", fetchImpl: async () => Response.json({ model: "gpt-6-luna", answers: [] }) });
|
|
assert.equal((await fetcher("https://api.openai.com/v1/systemone", { method: "POST", body: JSON.stringify(body()) })).status, 502);
|
|
});
|
|
|
|
test("a successful response with a per-question refusal remains HTTP 200", async () => {
|
|
const upstream = reply();
|
|
upstream.answers[0] = { name: "levels", type: "refusal" };
|
|
const fetcher = createDecisionFetch({ getKey: () => "fixture", fetchImpl: async () => Response.json(upstream) });
|
|
const response = await fetcher("https://api.openai.com/v1/systemone", { method: "POST", body: JSON.stringify(body()) });
|
|
assert.equal(response.status, 200);
|
|
const out = await response.json();
|
|
assert.deepEqual(Object.keys(out.answers), ["intent", "effort"]);
|
|
assert.equal(out.answers.intent.choice, "question");
|
|
});
|
|
|
|
test("cancellation reaches the upstream without retries or a manufactured answer", async () => {
|
|
const controller = new AbortController();
|
|
const fetcher = createDecisionFetch({ getKey: () => "fixture", fetchImpl: async (_url, init) => { assert.equal(init.signal.aborted, false); controller.abort(); init.signal.throwIfAborted(); } });
|
|
await assert.rejects(fetcher("https://api.openai.com/v1/systemone", { method: "POST", signal: controller.signal, body: JSON.stringify(body()) }), { name: "AbortError" });
|
|
});
|