100milionseek 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +118 -2
- package/README.zh-CN.md +108 -0
- package/index.mjs +4 -0
- package/opencode.mjs +5 -0
- package/opencode2.mjs +620 -0
- package/package.json +53 -5
- package/src/index.mjs +380 -0
- package/src/load.mjs +28 -0
- package/src/models.mjs +179 -0
- package/src/quota.mjs +73 -0
- package/src/systemone.mjs +70 -0
- package/test/all.mjs +13 -0
- package/test/load-report.test.mjs +92 -0
- package/test/load.test.mjs +54 -0
- package/test/models.test.mjs +153 -0
- package/test/plugin.test.mjs +278 -0
- package/test/quota.test.mjs +86 -0
- package/test/systemone.test.mjs +69 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Magpie's System One answers, in the shape a classifier has to be read in.
|
|
2
|
+
//
|
|
3
|
+
// magpie POSTs {model, questions, state} to {baseURL}/systemone and reads back
|
|
4
|
+
// {model, answers: {"<question>": {choice, score, confidence}}}. The service
|
|
5
|
+
// documents its judge as answering "one probability per question" in the
|
|
6
|
+
// vendor's own wording, so this renames numbers into `score` and leaves
|
|
7
|
+
// anything already shaped like an answer alone. An unrecognised body passes
|
|
8
|
+
// through untouched: a wrong guess about the format is not worth losing the
|
|
9
|
+
// vendor's own error.
|
|
10
|
+
|
|
11
|
+
function scoreOf(value) {
|
|
12
|
+
const n = typeof value === "number" ? value : typeof value === "string" && value.trim() ? Number(value) : NaN;
|
|
13
|
+
return Number.isFinite(n) ? n : null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function answerObject(value) {
|
|
17
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
18
|
+
// Already what magpie wants: at least one of choice / score / confidence.
|
|
19
|
+
if ("score" in value || "confidence" in value || "choice" in value) return value;
|
|
20
|
+
const probability = scoreOf(value.probability ?? value.p ?? value.value);
|
|
21
|
+
return probability === null ? null : { score: probability };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function normalizeSystemOne(body, questions) {
|
|
25
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) return body;
|
|
26
|
+
|
|
27
|
+
const source = body.answers ?? body.probabilities ?? body.scores ?? body.results;
|
|
28
|
+
if (source === undefined || source === null) return body;
|
|
29
|
+
|
|
30
|
+
const answers = {};
|
|
31
|
+
let renamed = false;
|
|
32
|
+
|
|
33
|
+
if (Array.isArray(source)) {
|
|
34
|
+
// Either bare numbers, or rows that name their own question.
|
|
35
|
+
for (const [index, row] of source.entries()) {
|
|
36
|
+
if (!row || typeof row !== "object" || Array.isArray(row)) {
|
|
37
|
+
const probability = scoreOf(row);
|
|
38
|
+
const question = Array.isArray(questions) ? questions[index] : undefined;
|
|
39
|
+
if (probability !== null && question !== undefined) {
|
|
40
|
+
answers[String(question)] = { score: probability };
|
|
41
|
+
renamed = true;
|
|
42
|
+
}
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const question = row.question ?? row.id ?? row.name;
|
|
46
|
+
const probability = scoreOf(row.probability ?? row.p ?? row.score ?? row.value);
|
|
47
|
+
if (question !== undefined && probability !== null) {
|
|
48
|
+
answers[String(question)] = { score: probability };
|
|
49
|
+
renamed = true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
} else if (typeof source === "object") {
|
|
53
|
+
for (const [question, value] of Object.entries(source)) {
|
|
54
|
+
const asAnswer = answerObject(value);
|
|
55
|
+
if (asAnswer) {
|
|
56
|
+
answers[question] = asAnswer;
|
|
57
|
+
if (asAnswer !== value) renamed = true;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const probability = scoreOf(value);
|
|
61
|
+
if (probability !== null) {
|
|
62
|
+
answers[question] = { score: probability };
|
|
63
|
+
renamed = true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!renamed) return body;
|
|
69
|
+
return { ...body, answers };
|
|
70
|
+
}
|
package/test/all.mjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Runs every test file in one process.
|
|
2
|
+
//
|
|
3
|
+
// `node --test` would spawn a child per file, which some sandboxes refuse
|
|
4
|
+
// (spawn EPERM) and which is slower than it needs to be for tests that share
|
|
5
|
+
// nothing but a fetch double. Importing the files runs them in this process and
|
|
6
|
+
// still exits non-zero when any test fails.
|
|
7
|
+
|
|
8
|
+
import "./quota.test.mjs";
|
|
9
|
+
import "./models.test.mjs";
|
|
10
|
+
import "./systemone.test.mjs";
|
|
11
|
+
import "./load.test.mjs";
|
|
12
|
+
import "./load-report.test.mjs";
|
|
13
|
+
import "./plugin.test.mjs";
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import { HundredMilionSeekPlugin, PROVIDER } from "../src/index.mjs";
|
|
5
|
+
import { CHAT_MODEL, JUDGE_MODEL } from "../src/models.mjs";
|
|
6
|
+
|
|
7
|
+
const json = (body, status = 200) =>
|
|
8
|
+
new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
|
|
9
|
+
|
|
10
|
+
const status = (over = {}) => ({
|
|
11
|
+
state: "ok",
|
|
12
|
+
upstream: { inflight: 500, capacity: 1000 },
|
|
13
|
+
...over,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// The host hands `input.magpie.load` over and records what was reported.
|
|
17
|
+
function host(reply) {
|
|
18
|
+
const reports = [];
|
|
19
|
+
return {
|
|
20
|
+
reports,
|
|
21
|
+
input: {
|
|
22
|
+
magpie: {
|
|
23
|
+
load: (provider, loads) => {
|
|
24
|
+
reports.push({ provider, loads });
|
|
25
|
+
return Object.keys(loads).length;
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
fetchImpl: async (url, init) => (typeof reply === "function" ? reply(url, init) : reply),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const settle = () => new Promise((r) => setTimeout(r, 10));
|
|
34
|
+
|
|
35
|
+
test("every model of the provider carries the upstream's load", async () => {
|
|
36
|
+
const h = host(json(status()));
|
|
37
|
+
await HundredMilionSeekPlugin(h.input, { fetchImpl: h.fetchImpl });
|
|
38
|
+
await settle();
|
|
39
|
+
|
|
40
|
+
assert.equal(h.reports.length, 1);
|
|
41
|
+
const [report] = h.reports;
|
|
42
|
+
assert.equal(report.provider, PROVIDER);
|
|
43
|
+
assert.deepEqual(Object.keys(report.loads).sort(), [CHAT_MODEL, JUDGE_MODEL].sort());
|
|
44
|
+
assert.equal(report.loads[CHAT_MODEL], 50);
|
|
45
|
+
assert.equal(report.loads[JUDGE_MODEL], 50);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("a load the service did not give is not reported at all", async () => {
|
|
49
|
+
// Reporting 0 would tell magpie the relay is idle, which it does not know.
|
|
50
|
+
const h = host(json({}));
|
|
51
|
+
await HundredMilionSeekPlugin(h.input, { fetchImpl: h.fetchImpl });
|
|
52
|
+
await settle();
|
|
53
|
+
assert.equal(h.reports.length, 0);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("a failing status read is silent, not fatal", async () => {
|
|
57
|
+
const h = host(async () => {
|
|
58
|
+
throw new TypeError("fetch failed");
|
|
59
|
+
});
|
|
60
|
+
const plugin = await HundredMilionSeekPlugin(h.input, { fetchImpl: h.fetchImpl });
|
|
61
|
+
await settle();
|
|
62
|
+
assert.equal(h.reports.length, 0);
|
|
63
|
+
// And the rest of the plugin still works.
|
|
64
|
+
assert.equal(plugin.auth.provider, PROVIDER);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("an HTTP error from the status endpoint is not a load of zero", async () => {
|
|
68
|
+
const h = host(json({ error: "nope" }, 503));
|
|
69
|
+
await HundredMilionSeekPlugin(h.input, { fetchImpl: h.fetchImpl });
|
|
70
|
+
await settle();
|
|
71
|
+
assert.equal(h.reports.length, 0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("no magpie on the host means no polling", async () => {
|
|
75
|
+
// A plain OpenCode host passes no `magpie`, and the plugin must not fetch.
|
|
76
|
+
let asked = 0;
|
|
77
|
+
await HundredMilionSeekPlugin({}, {
|
|
78
|
+
fetchImpl: async () => {
|
|
79
|
+
asked += 1;
|
|
80
|
+
return json(status());
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
await settle();
|
|
84
|
+
assert.equal(asked, 0);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("the report can be turned off", async () => {
|
|
88
|
+
const h = host(json(status()));
|
|
89
|
+
await HundredMilionSeekPlugin(h.input, { fetchImpl: h.fetchImpl, load: false });
|
|
90
|
+
await settle();
|
|
91
|
+
assert.equal(h.reports.length, 0);
|
|
92
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import { loadOf } from "../src/load.mjs";
|
|
5
|
+
|
|
6
|
+
// The shapes /api/status really returns, trimmed to the fields the load reads.
|
|
7
|
+
const status = (over = {}) => ({
|
|
8
|
+
state: "ok",
|
|
9
|
+
sessions_in_flight: 0,
|
|
10
|
+
allowance: { session_limit: 5 },
|
|
11
|
+
upstream: { inflight: 100, capacity: 1000 },
|
|
12
|
+
...over,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("load is how much of the upstream's capacity is already in use", () => {
|
|
16
|
+
assert.equal(loadOf(status()), 10);
|
|
17
|
+
assert.equal(loadOf(status({ upstream: { inflight: 500, capacity: 1000 } })), 50);
|
|
18
|
+
assert.equal(loadOf(status({ upstream: { inflight: 999, capacity: 1000 } })), 100);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("an idle service is a real zero", () => {
|
|
22
|
+
assert.equal(loadOf(status({ upstream: { inflight: 0, capacity: 1000 } })), 0);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("a load above one hundred is passed on, not clipped to it", () => {
|
|
26
|
+
// Over-subscribed is worth showing; magpie clamps at 10000 itself.
|
|
27
|
+
assert.equal(loadOf(status({ upstream: { inflight: 1200, capacity: 1000 } })), 120);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("the service-wide session count is not this account's queue", () => {
|
|
31
|
+
// `sessions_in_flight` counts every user while `session_limit` is per
|
|
32
|
+
// account; reading one against the other would report a permanent 100.
|
|
33
|
+
const out = loadOf(status({ sessions_in_flight: 977, allowance: { session_limit: 5 } }));
|
|
34
|
+
assert.equal(out, 10);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("a degraded upstream is not treated as full", () => {
|
|
38
|
+
// It reads "degraded" whenever it is retrying anything, which is most of the
|
|
39
|
+
// day; calling that full would route every request away for ever.
|
|
40
|
+
assert.equal(loadOf(status({ state: "degraded" })), 10);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("counts that cannot be read say nothing rather than zero", () => {
|
|
44
|
+
assert.equal(loadOf(status({ upstream: { inflight: "many", capacity: 1000 } })), null);
|
|
45
|
+
assert.equal(loadOf(status({ upstream: { inflight: 5, capacity: 0 } })), null);
|
|
46
|
+
assert.equal(loadOf(status({ upstream: {} })), null);
|
|
47
|
+
assert.equal(loadOf(null), null);
|
|
48
|
+
assert.equal(loadOf("nonsense"), null);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("the figure is always a whole number magpie can use", () => {
|
|
52
|
+
const out = loadOf(status({ upstream: { inflight: 1, capacity: 3 } }));
|
|
53
|
+
assert.equal(out, 33);
|
|
54
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
CHAT_MODEL,
|
|
6
|
+
JUDGE_MODEL,
|
|
7
|
+
isJudge,
|
|
8
|
+
flagMap,
|
|
9
|
+
configModels,
|
|
10
|
+
fullModel,
|
|
11
|
+
fullModels,
|
|
12
|
+
modelIDs,
|
|
13
|
+
accountModels,
|
|
14
|
+
smallOf,
|
|
15
|
+
} from "../src/models.mjs";
|
|
16
|
+
|
|
17
|
+
const BASE = "https://100milionseek.top/v1";
|
|
18
|
+
const ID = "hundred-milion-seek";
|
|
19
|
+
|
|
20
|
+
test("isJudge recognises the judge family and nothing else", () => {
|
|
21
|
+
assert.equal(isJudge(JUDGE_MODEL), true);
|
|
22
|
+
assert.equal(isJudge("jev-2.0"), true);
|
|
23
|
+
assert.equal(isJudge("JEV-anything"), true);
|
|
24
|
+
assert.equal(isJudge(CHAT_MODEL), false);
|
|
25
|
+
assert.equal(isJudge(""), false);
|
|
26
|
+
assert.equal(isJudge(undefined), false);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("configModels describes the chat model and the judge", () => {
|
|
30
|
+
const models = configModels();
|
|
31
|
+
const chat = models[CHAT_MODEL];
|
|
32
|
+
assert.equal(chat.limit.context, 198_000);
|
|
33
|
+
assert.equal(chat.limit.output, 65_536);
|
|
34
|
+
assert.equal(chat.tool_call, true);
|
|
35
|
+
assert.equal(chat.reasoning, false);
|
|
36
|
+
assert.deepEqual(chat.cost, { input: 0, output: 0 });
|
|
37
|
+
// Never `free: true`: every token spends the day's allowance.
|
|
38
|
+
assert.equal("free" in chat, false);
|
|
39
|
+
|
|
40
|
+
const judge = models[JUDGE_MODEL];
|
|
41
|
+
assert.equal(judge.decides, true);
|
|
42
|
+
assert.equal(judge.tool_call, false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("configModels turns reasoning on only when asked", () => {
|
|
46
|
+
assert.equal(configModels({ reasoning: true })[CHAT_MODEL].reasoning, true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("flagMap fills a capability map and never returns an empty one", () => {
|
|
50
|
+
assert.deepEqual(flagMap(["text", "image"]), { text: true, image: true });
|
|
51
|
+
assert.deepEqual(flagMap([]), { text: true });
|
|
52
|
+
assert.deepEqual(flagMap(undefined), { text: true });
|
|
53
|
+
assert.deepEqual(flagMap(["audio"], ["text"]), { audio: true });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("fullModel expands a config entry into a whole Model", () => {
|
|
57
|
+
const small = configModels({ reasoning: true })[CHAT_MODEL];
|
|
58
|
+
const model = fullModel(ID, CHAT_MODEL, small, BASE);
|
|
59
|
+
assert.equal(model.id, CHAT_MODEL);
|
|
60
|
+
assert.equal(model.providerID, ID);
|
|
61
|
+
assert.equal(model.api.id, CHAT_MODEL);
|
|
62
|
+
assert.equal(model.api.url, BASE);
|
|
63
|
+
assert.equal(model.api.npm, "@ai-sdk/openai-compatible");
|
|
64
|
+
assert.equal(model.limit.context, 198_000);
|
|
65
|
+
assert.equal(model.capabilities.reasoning, true);
|
|
66
|
+
assert.equal(model.capabilities.toolcall, true);
|
|
67
|
+
assert.equal(model.capabilities.attachment, false);
|
|
68
|
+
assert.deepEqual(model.capabilities.input, { text: true });
|
|
69
|
+
assert.deepEqual(model.capabilities.output, { text: true });
|
|
70
|
+
assert.deepEqual(model.cost, { input: 0, output: 0, cache: { read: 0, write: 0 } });
|
|
71
|
+
assert.equal(model.status, "active");
|
|
72
|
+
assert.equal("decides" in model, false);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("fullModel keeps decides on the judge", () => {
|
|
76
|
+
const model = fullModel(ID, JUDGE_MODEL, configModels()[JUDGE_MODEL], BASE);
|
|
77
|
+
assert.equal(model.decides, true);
|
|
78
|
+
assert.equal(model.limit.output, 4_096);
|
|
79
|
+
assert.equal(model.capabilities.toolcall, false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("fullModel survives a missing entry by falling back to the chat limits", () => {
|
|
83
|
+
const model = fullModel(ID, "brand-new-model", undefined, BASE);
|
|
84
|
+
assert.equal(model.limit.context, 198_000);
|
|
85
|
+
assert.equal(model.limit.output, 65_536);
|
|
86
|
+
assert.equal(model.name, "brand-new-model");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("fullModels expands the whole table", () => {
|
|
90
|
+
const models = fullModels(configModels(), { baseURL: BASE, providerID: ID });
|
|
91
|
+
assert.deepEqual(Object.keys(models).sort(), [CHAT_MODEL, JUDGE_MODEL].sort());
|
|
92
|
+
assert.equal(models[JUDGE_MODEL].decides, true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("modelIDs reads every listing shape the API might use", () => {
|
|
96
|
+
assert.deepEqual(modelIDs({ object: "list", data: [{ id: "a" }, { id: "b" }] }), ["a", "b"]);
|
|
97
|
+
assert.deepEqual(modelIDs({ models: ["a", "b"] }), ["a", "b"]);
|
|
98
|
+
assert.deepEqual(modelIDs(["a", { id: "b" }]), ["a", "b"]);
|
|
99
|
+
// A model served twice is offered once.
|
|
100
|
+
assert.deepEqual(modelIDs({ data: [{ id: "a" }, { id: "a" }] }), ["a"]);
|
|
101
|
+
assert.deepEqual(modelIDs({ data: [{ id: "" }, { object: "model" }] }), []);
|
|
102
|
+
assert.deepEqual(modelIDs(null), []);
|
|
103
|
+
assert.deepEqual(modelIDs("nonsense"), []);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("accountModels describes the listed ids and always offers the judge", () => {
|
|
107
|
+
const template = configModels();
|
|
108
|
+
const models = accountModels([CHAT_MODEL], template, { baseURL: BASE, providerID: ID });
|
|
109
|
+
assert.deepEqual(Object.keys(models).sort(), [CHAT_MODEL, JUDGE_MODEL].sort());
|
|
110
|
+
assert.equal(models[CHAT_MODEL].api.id, CHAT_MODEL);
|
|
111
|
+
assert.equal(models[JUDGE_MODEL].decides, true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("accountModels gives an unknown listed id the chat model's shape", () => {
|
|
115
|
+
const models = accountModels(["some-new-model"], configModels(), {
|
|
116
|
+
baseURL: BASE,
|
|
117
|
+
providerID: ID,
|
|
118
|
+
fallback: configModels()[CHAT_MODEL],
|
|
119
|
+
});
|
|
120
|
+
assert.equal(models["some-new-model"].id, "some-new-model");
|
|
121
|
+
assert.equal(models["some-new-model"].api.id, "some-new-model");
|
|
122
|
+
assert.equal(models["some-new-model"].limit.context, 198_000);
|
|
123
|
+
assert.equal(models[JUDGE_MODEL].decides, true);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("accountModels never invents a judge shape for an unknown jev- id", () => {
|
|
127
|
+
const models = accountModels(["jev-9.9"], configModels(), {
|
|
128
|
+
baseURL: BASE,
|
|
129
|
+
providerID: ID,
|
|
130
|
+
fallback: configModels()[CHAT_MODEL],
|
|
131
|
+
});
|
|
132
|
+
// Unknown judge ids are not offered as chat models; the known judge still is.
|
|
133
|
+
assert.equal(models["jev-9.9"], undefined);
|
|
134
|
+
assert.ok(models[JUDGE_MODEL]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("accountModels falls back to the whole table when nothing was listed", () => {
|
|
138
|
+
const models = accountModels([], configModels(), { baseURL: BASE, providerID: ID });
|
|
139
|
+
assert.deepEqual(Object.keys(models).sort(), [CHAT_MODEL, JUDGE_MODEL].sort());
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("smallOf turns either model shape back into the config entry shape", () => {
|
|
143
|
+
const full = fullModel(ID, CHAT_MODEL, configModels({ reasoning: true })[CHAT_MODEL], BASE);
|
|
144
|
+
const small = smallOf(full, CHAT_MODEL);
|
|
145
|
+
assert.equal(small.name, full.name);
|
|
146
|
+
assert.equal(small.id, CHAT_MODEL);
|
|
147
|
+
assert.deepEqual(small.limit, { context: 198_000, output: 65_536 });
|
|
148
|
+
assert.equal(small.reasoning, true);
|
|
149
|
+
assert.equal(small.tool_call, true);
|
|
150
|
+
assert.equal(small.attachment, false);
|
|
151
|
+
assert.deepEqual(small.modalities, { input: ["text"], output: ["text"] });
|
|
152
|
+
assert.equal(smallOf(undefined, "x"), undefined);
|
|
153
|
+
});
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import { HundredMilionSeekPlugin, PROVIDER, BASE, ORIGIN, keyOf } from "../src/index.mjs";
|
|
5
|
+
import { CHAT_MODEL, JUDGE_MODEL } from "../src/models.mjs";
|
|
6
|
+
|
|
7
|
+
const QUOTA = {
|
|
8
|
+
"x-quota-limit": "100000000",
|
|
9
|
+
"x-quota-used": "2500000",
|
|
10
|
+
"x-quota-remaining": "97500000",
|
|
11
|
+
"x-quota-reset": "2026-11-26T00:00:00Z",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// A fetch double that records what was sent and answers with a scripted reply.
|
|
15
|
+
function recorder(reply) {
|
|
16
|
+
const calls = [];
|
|
17
|
+
const fetchImpl = async (url, init) => {
|
|
18
|
+
const call = { url: String(url), init };
|
|
19
|
+
calls.push(call);
|
|
20
|
+
return typeof reply === "function" ? reply(call) : reply;
|
|
21
|
+
};
|
|
22
|
+
return { calls, fetchImpl };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const json = (body, status = 200, headers = {}) =>
|
|
26
|
+
new Response(JSON.stringify(body), {
|
|
27
|
+
status,
|
|
28
|
+
headers: { "content-type": "application/json", ...headers },
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const account = { type: "api", key: "hms-test-key" };
|
|
32
|
+
|
|
33
|
+
test("keyOf reads every shape magpie stores a key in", () => {
|
|
34
|
+
assert.equal(keyOf({ type: "api", key: "hms-a" }), "hms-a");
|
|
35
|
+
assert.equal(keyOf({ type: "api", key: " hms-a " }), "hms-a");
|
|
36
|
+
assert.equal(keyOf({ apiKey: "hms-b" }), "hms-b");
|
|
37
|
+
assert.equal(keyOf({ access: "hms-c" }), "hms-c");
|
|
38
|
+
assert.equal(keyOf({ methods: [{ key: "hms-d" }] }), "hms-d");
|
|
39
|
+
assert.equal(keyOf({ type: "api", key: "hms-e" }), "hms-e");
|
|
40
|
+
assert.equal(keyOf(null), "");
|
|
41
|
+
assert.equal(keyOf({}), "");
|
|
42
|
+
assert.equal(keyOf({ key: 42 }), "");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("config declares the provider once and never overwrites the user's own", async () => {
|
|
46
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async () => json({}) });
|
|
47
|
+
|
|
48
|
+
const fresh = {};
|
|
49
|
+
await plugin.config(fresh);
|
|
50
|
+
assert.equal(fresh.provider[PROVIDER].npm, "@ai-sdk/openai-compatible");
|
|
51
|
+
assert.equal(fresh.provider[PROVIDER].api, BASE);
|
|
52
|
+
assert.equal(fresh.provider[PROVIDER].decide, true);
|
|
53
|
+
assert.ok(fresh.provider[PROVIDER].models[CHAT_MODEL]);
|
|
54
|
+
|
|
55
|
+
const mine = { provider: { [PROVIDER]: { name: "mine", models: {} } } };
|
|
56
|
+
await plugin.config(mine);
|
|
57
|
+
assert.equal(mine.provider[PROVIDER].name, "mine");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("auth names the provider, its five slots and its icon", async () => {
|
|
61
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async () => json({}) });
|
|
62
|
+
assert.equal(plugin.auth.provider, PROVIDER);
|
|
63
|
+
assert.equal(plugin.auth.maxConcurrency, 5);
|
|
64
|
+
assert.equal(plugin.auth.icon, `${ORIGIN}/favicon.svg`);
|
|
65
|
+
assert.equal(plugin.auth.methods[0].type, "api");
|
|
66
|
+
assert.equal(plugin.auth.checkin, undefined);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("the sign-in check accepts a working key and remembers the allowance it saw", async () => {
|
|
70
|
+
const { calls, fetchImpl } = recorder(json({ object: "list", data: [] }, 200, QUOTA));
|
|
71
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl });
|
|
72
|
+
const out = await plugin.auth.methods[0].authorize({ key: " hms-good " });
|
|
73
|
+
assert.equal(out.type, "success");
|
|
74
|
+
assert.equal(out.key, "hms-good");
|
|
75
|
+
assert.equal(calls[0].url, `${BASE}/models`);
|
|
76
|
+
assert.equal(calls[0].init.headers.authorization, "Bearer hms-good");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("the sign-in check refuses a key the service rejected", async () => {
|
|
80
|
+
const plugin = await HundredMilionSeekPlugin({}, {
|
|
81
|
+
fetchImpl: async () => json({ error: { message: "invalid_api_key" } }, 401),
|
|
82
|
+
});
|
|
83
|
+
const out = await plugin.auth.methods[0].authorize({ key: "hms-bad" });
|
|
84
|
+
assert.equal(out.type, "failed");
|
|
85
|
+
assert.match(out.error, /invalid_api_key/);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("the sign-in check refuses an empty key without asking the service", async () => {
|
|
89
|
+
const { calls, fetchImpl } = recorder(json({}));
|
|
90
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl });
|
|
91
|
+
const out = await plugin.auth.methods[0].authorize({ key: " " });
|
|
92
|
+
assert.equal(out.type, "failed");
|
|
93
|
+
assert.equal(calls.length, 0);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("the sign-in check keeps the key when the service cannot be reached", async () => {
|
|
97
|
+
const plugin = await HundredMilionSeekPlugin({}, {
|
|
98
|
+
fetchImpl: async () => {
|
|
99
|
+
throw new TypeError("fetch failed");
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
const out = await plugin.auth.methods[0].authorize({ key: "hms-unreachable" });
|
|
103
|
+
assert.equal(out.type, "success");
|
|
104
|
+
assert.equal(out.key, "hms-unreachable");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("the loader hands out the base URL, the account's key and a guarded fetch", async () => {
|
|
108
|
+
const { calls, fetchImpl } = recorder(json({ choices: [] }, 200, QUOTA));
|
|
109
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl });
|
|
110
|
+
const loader = await plugin.auth.loader(async () => account);
|
|
111
|
+
assert.equal(loader.baseURL, BASE);
|
|
112
|
+
assert.equal(loader.apiKey, "hms-test-key");
|
|
113
|
+
|
|
114
|
+
await loader.fetch(`${BASE}/chat/completions`, {
|
|
115
|
+
method: "POST",
|
|
116
|
+
headers: { "content-type": "application/json", authorization: "Bearer someone-else" },
|
|
117
|
+
body: JSON.stringify({ model: CHAT_MODEL, messages: [] }),
|
|
118
|
+
});
|
|
119
|
+
const sent = calls[0];
|
|
120
|
+
assert.equal(sent.url, `${BASE}/chat/completions`);
|
|
121
|
+
// The account's key wins over whatever was passed in.
|
|
122
|
+
assert.equal(sent.init.headers.get("authorization"), "Bearer hms-test-key");
|
|
123
|
+
assert.equal(sent.init.headers.get("content-type"), "application/json");
|
|
124
|
+
assert.equal(sent.init.method, "POST");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("the loader sends nothing when no account is signed in", async () => {
|
|
128
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async () => json({}) });
|
|
129
|
+
assert.deepEqual(await plugin.auth.loader(async () => undefined), {});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("a request is rebuilt on the plugin's own origin", async () => {
|
|
133
|
+
const { calls, fetchImpl } = recorder(json({ ok: true }, 200, QUOTA));
|
|
134
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl });
|
|
135
|
+
const loader = await plugin.auth.loader(async () => account);
|
|
136
|
+
await loader.fetch("https://elsewhere.example/v1/chat/completions?trace=1", { method: "POST", body: "{}" });
|
|
137
|
+
assert.equal(calls[0].url, `${BASE}/chat/completions?trace=1`);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("the judge's answer is rewritten into what magpie reads", async () => {
|
|
141
|
+
const { fetchImpl } = recorder(json({ model: JUDGE_MODEL, answers: { q1: 0.8 } }));
|
|
142
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl });
|
|
143
|
+
const loader = await plugin.auth.loader(async () => account);
|
|
144
|
+
const res = await loader.fetch(`${BASE}/systemone`, {
|
|
145
|
+
method: "POST",
|
|
146
|
+
headers: { "content-type": "application/json" },
|
|
147
|
+
body: JSON.stringify({ model: JUDGE_MODEL, questions: ["q1"], state: {} }),
|
|
148
|
+
});
|
|
149
|
+
assert.equal(res.status, 200);
|
|
150
|
+
assert.deepEqual(await res.json(), { model: JUDGE_MODEL, answers: { q1: { score: 0.8 } } });
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("the judge's own request is forwarded as magpie sent it", async () => {
|
|
154
|
+
// The vendor's /systemone already takes {model, questions, state}; rewriting
|
|
155
|
+
// the request would be shaping an answer that does not exist yet.
|
|
156
|
+
const { calls, fetchImpl } = recorder(json({ model: JUDGE_MODEL, answers: { q1: 0.8 } }));
|
|
157
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl });
|
|
158
|
+
const loader = await plugin.auth.loader(async () => account);
|
|
159
|
+
const sent = JSON.stringify({ model: JUDGE_MODEL, questions: ["q1"], state: { turns: 3 } });
|
|
160
|
+
await loader.fetch(`${BASE}/systemone`, {
|
|
161
|
+
method: "POST",
|
|
162
|
+
headers: { "content-type": "application/json" },
|
|
163
|
+
body: sent,
|
|
164
|
+
});
|
|
165
|
+
assert.equal(calls[0].url, `${BASE}/systemone`);
|
|
166
|
+
assert.equal(calls[0].init.body, sent);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("a spent allowance gets a Retry-After even when the service sent none", async () => {
|
|
170
|
+
const reset = new Date(Date.now() + 3_600_000).toISOString();
|
|
171
|
+
const body = {
|
|
172
|
+
error: { message: "You have used today's allowance", code: "daily_quota_exceeded", reset_at: reset },
|
|
173
|
+
};
|
|
174
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async () => json(body, 429) });
|
|
175
|
+
const loader = await plugin.auth.loader(async () => account);
|
|
176
|
+
const res = await loader.fetch(`${BASE}/chat/completions`, { method: "POST", body: "{}" });
|
|
177
|
+
assert.equal(res.status, 429);
|
|
178
|
+
const after = Number(res.headers.get("retry-after"));
|
|
179
|
+
assert.ok(after > 3500 && after <= 3600, `retry-after was ${after}`);
|
|
180
|
+
assert.equal((await res.json()).error.code, "daily_quota_exceeded");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test("a concurrency 429 keeps the service's own short Retry-After", async () => {
|
|
184
|
+
const plugin = await HundredMilionSeekPlugin({}, {
|
|
185
|
+
fetchImpl: async () =>
|
|
186
|
+
json({ error: { code: "session_limit_reached" } }, 429, { "retry-after": "2" }),
|
|
187
|
+
});
|
|
188
|
+
const loader = await plugin.auth.loader(async () => account);
|
|
189
|
+
const res = await loader.fetch(`${BASE}/chat/completions`, { method: "POST", body: "{}" });
|
|
190
|
+
assert.equal(res.headers.get("retry-after"), "2");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("other failures pass through untouched", async () => {
|
|
194
|
+
const plugin = await HundredMilionSeekPlugin({}, {
|
|
195
|
+
fetchImpl: async () => json({ error: { code: "model_not_found" } }, 404),
|
|
196
|
+
});
|
|
197
|
+
const loader = await plugin.auth.loader(async () => account);
|
|
198
|
+
const res = await loader.fetch(`${BASE}/chat/completions`, { method: "POST", body: "{}" });
|
|
199
|
+
assert.equal(res.status, 404);
|
|
200
|
+
assert.equal(res.headers.get("retry-after"), null);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("usage reads the allowance from the account's own response headers", async () => {
|
|
204
|
+
const plugin = await HundredMilionSeekPlugin({}, {
|
|
205
|
+
fetchImpl: async () => json({ object: "list", data: [] }, 200, QUOTA),
|
|
206
|
+
});
|
|
207
|
+
const usage = await plugin.auth.usage(async () => account);
|
|
208
|
+
assert.equal(usage.plan, "Free");
|
|
209
|
+
assert.equal(usage.windows.length, 1);
|
|
210
|
+
const [w] = usage.windows;
|
|
211
|
+
assert.equal(w.limit, 100_000_000);
|
|
212
|
+
assert.equal(w.amount, 2_500_000);
|
|
213
|
+
assert.equal(w.used, 2.5);
|
|
214
|
+
assert.equal(w.aside, undefined);
|
|
215
|
+
assert.equal(usage.error, undefined);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("usage marks the account when its key stopped working", async () => {
|
|
219
|
+
const plugin = await HundredMilionSeekPlugin({}, {
|
|
220
|
+
fetchImpl: async () => json({ error: { code: "invalid_api_key" } }, 401),
|
|
221
|
+
});
|
|
222
|
+
const usage = await plugin.auth.usage(async () => account);
|
|
223
|
+
assert.equal(usage.signIn, "expired");
|
|
224
|
+
assert.ok(usage.error);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("usage shows an unknown allowance rather than a made-up number", async () => {
|
|
228
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async () => json({ object: "list" }) });
|
|
229
|
+
const usage = await plugin.auth.usage(async () => account);
|
|
230
|
+
assert.equal(usage.windows.length, 1);
|
|
231
|
+
assert.equal("used" in usage.windows[0], false);
|
|
232
|
+
assert.equal(usage.windows[0].limit, undefined);
|
|
233
|
+
assert.ok(usage.windows[0].display);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("usage falls back to the allowance a request already reported", async () => {
|
|
237
|
+
let reply = () => json({ choices: [] }, 200, QUOTA);
|
|
238
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async (url, init) => reply(url, init) });
|
|
239
|
+
const loader = await plugin.auth.loader(async () => account);
|
|
240
|
+
await loader.fetch(`${BASE}/chat/completions`, { method: "POST", body: "{}" });
|
|
241
|
+
reply = () => {
|
|
242
|
+
throw new TypeError("fetch failed");
|
|
243
|
+
};
|
|
244
|
+
const usage = await plugin.auth.usage(async () => account);
|
|
245
|
+
assert.equal(usage.windows[0].limit, 100_000_000);
|
|
246
|
+
assert.ok(usage.error);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test("usage says nothing when no account is signed in", async () => {
|
|
250
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async () => json({}) });
|
|
251
|
+
assert.deepEqual(await plugin.auth.usage(async () => undefined), {});
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test("the models hook offers what the account can actually use", async () => {
|
|
255
|
+
const listing = { object: "list", data: [{ id: CHAT_MODEL }, { id: JUDGE_MODEL }] };
|
|
256
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async () => json(listing, 200, QUOTA) });
|
|
257
|
+
const before = { [CHAT_MODEL]: { name: "already here" } };
|
|
258
|
+
const models = await plugin.provider.models({ models: before }, { auth: account });
|
|
259
|
+
assert.deepEqual(Object.keys(models).sort(), [CHAT_MODEL, JUDGE_MODEL].sort());
|
|
260
|
+
assert.equal(models[CHAT_MODEL].api.id, CHAT_MODEL);
|
|
261
|
+
assert.equal(models[JUDGE_MODEL].decides, true);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("the models hook keeps the list it has when the read fails", async () => {
|
|
265
|
+
const plugin = await HundredMilionSeekPlugin({}, {
|
|
266
|
+
fetchImpl: async () => {
|
|
267
|
+
throw new TypeError("fetch failed");
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
const before = { [CHAT_MODEL]: { name: "already here" } };
|
|
271
|
+
assert.equal(await plugin.provider.models({ models: before }, { auth: account }), before);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("the models hook keeps the list it has when there is no account", async () => {
|
|
275
|
+
const plugin = await HundredMilionSeekPlugin({}, { fetchImpl: async () => json({}) });
|
|
276
|
+
const before = { [CHAT_MODEL]: { name: "already here" } };
|
|
277
|
+
assert.equal(await plugin.provider.models({ models: before }), before);
|
|
278
|
+
});
|