New package @magpie-community/opencode-inkstone-auth 0.1.0 for Intern InkStone (书生·端砚, Shanghai AI Lab) Token Plan, provider id `intern-inkstone`. Community package by yunwang2442-creator. What it does: - Browser sign-in (Edge/Chrome, its own profile under $XDG_CONFIG_HOME or ~/.config/magpie/inkstone-browser, or %LOCALAPPDATA%\magpie\inkstone-browser). The login window has no debugging port. A headless CDP read takes the uaa-token cookie, and the same read renews it silently later. - Usage card: ink-credit balance, 5-hour and 7-day windows, credit packs. - Chat: a pasted ink-* API key, or one the sign-in creates, named ink-<host4>-<acct4>-<rand4> (18 of the console's 20 characters). An idempotency clash (-15109) retries with a fresh name, up to three times. Pruning is on by default. It only touches this machine's and account's prefix, keeps the newest two and the key this sign-in created, and never deletes manual keys or keys of unknown age. - Closing on Unix: SIGTERM, then SIGKILL only after a 15s grace. Leftover browsers are matched by --user-data-dir=<escaped profile>, and Windows uses .Contains(). - The login wait comes from an injected waitForLogin in tests. No env hook is left in production code. - Hosts: discovery.intern-ai.org.cn, discovery-api.intern-ai.org.cn, 127.0.0.1 (CDP). Outside the package: scripts/opencode2.mjs lets a prompt with `required: false` stay optional (`p.required !== false`). Every package's copy is synced byte for byte, so all 39 copies in the merged tree are blobafe074cb. No other package sets `required: false` on a prompt, so this changes nothing for them. Also a root README row and a registry entry. Review at 36412a15, on mote, HOME=$(mktemp -d), Bun 1.4.2: - bun test packages/inkstone: 68 pass, 0 fail in Asia/Shanghai, UTC, America/Los_Angeles and Pacific/Kiritimati. - bun scripts/check.mjs inkstone and bun scripts/check.mjs: ok. - bun test (repo), on 36412a15 merged with origin/maine7cb9dc5: 6743 pass, 20 skip, 0 fail. main has since moved by cline 0.1.14 and zcode 0.1.55 only, and none of the adapter copies changed. - Broken by hand, each red: no protectName protection; prune_keys default off; no post-sign-in prune; no SIGTERM on close; the -15109 retry keeping its name. Not tried: a real InkStone account sign-in on macOS, Linux or Windows (no account here); a live chat request; magpie end to end with the plugin.
178 lines
7.7 KiB
JavaScript
178 lines
7.7 KiB
JavaScript
// The API-key side: the catalogue it reads, the models magpie lists for the
|
|
// account, the loader, and the sign-in methods. Everything runs against the
|
|
// local fake.
|
|
import "./nonet.mjs"
|
|
import { readFileSync } from "node:fs"
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { InkStoneAuthPlugin } from "./index.mjs"
|
|
import { fakeInkStone, json } from "./fake.mjs"
|
|
|
|
let f
|
|
afterEach(() => f?.close())
|
|
|
|
const META = JSON.parse(readFileSync(new URL("./testdata/public-models.json", import.meta.url), "utf8"))
|
|
const auth = { type: "oauth", access: "sk-test", apiKey: "sk-test", uaaToken: "uaa-token" }
|
|
|
|
async function plugin() {
|
|
return InkStoneAuthPlugin({ client: { auth: { set: async () => {} } } })
|
|
}
|
|
|
|
function serve(live = [{ id: "glm-5.3" }, { id: "Agents-A1" }, { id: "intern-decision-4b" }]) {
|
|
f.route("GET /v1/models", (r) => {
|
|
expect(r.headers.get("authorization")).toBe("Bearer sk-test")
|
|
return json({ object: "list", data: live })
|
|
})
|
|
f.route("GET /api/tokenplan/v1/public/models", () => json(META))
|
|
}
|
|
|
|
test("config declares the provider with a working npm, base URL and bootstrap models", async () => {
|
|
f = fakeInkStone()
|
|
const p = await plugin()
|
|
const cfg = {}
|
|
await p.config(cfg)
|
|
const provider = cfg.provider["intern-inkstone"]
|
|
expect(provider.name).toBe("书生·端砚")
|
|
expect(provider.npm).toBe("@ai-sdk/openai-compatible")
|
|
expect(provider.api).toBe(f.origin + "/v1")
|
|
expect(Object.keys(provider.models).length).toBeGreaterThan(0)
|
|
expect(provider.models["glm-5.3"].limit.context).toBe(1048576)
|
|
})
|
|
|
|
test("the listing is the key's live set intersected with the public metadata", async () => {
|
|
f = fakeInkStone()
|
|
serve()
|
|
const p = await plugin()
|
|
const models = await p.provider.models({ id: "intern-inkstone", models: {} }, { auth })
|
|
expect(Object.keys(models).sort()).toEqual(["Agents-A1", "glm-5.3"])
|
|
expect(models["glm-5.3"].limit.context).toBe(1048576)
|
|
expect(models["glm-5.3"].reasoning).toBe(true)
|
|
expect(models["Agents-A1"].capabilities.input.image).toBe(true)
|
|
expect(models["glm-5.3"].variants).toBeUndefined()
|
|
// the magpie-only price markers reach provider.models too
|
|
expect(models["glm-5.3"].rate).toBe(8)
|
|
expect(models["glm-5.3"].free).toBeUndefined()
|
|
expect(models["Agents-A1"].free).toBe(true)
|
|
expect(models["Agents-A1"].rate).toBeUndefined()
|
|
})
|
|
|
|
test("config's bootstrap models carry the free/rate markers too", async () => {
|
|
f = fakeInkStone()
|
|
const p = await plugin()
|
|
const cfg = {}
|
|
await p.config(cfg)
|
|
const models = cfg.provider["intern-inkstone"].models
|
|
expect(models["Sage-A1.5"].free).toBe(true)
|
|
expect(models["Sage-A1.5"].rate).toBeUndefined()
|
|
expect(models["glm-5.3"].rate).toBe(8)
|
|
expect(models["glm-5.3"].free).toBeUndefined()
|
|
})
|
|
|
|
test("an account with no API key advertises no models", async () => {
|
|
f = fakeInkStone()
|
|
const p = await plugin()
|
|
const noKey = { type: "oauth", access: "", uaaToken: "uaa-token" }
|
|
expect(await p.provider.models({ id: "intern-inkstone", models: { "glm-5.3": {} } }, { auth: noKey })).toEqual({})
|
|
expect(await p.auth.loader(async () => noKey)).toEqual({})
|
|
})
|
|
|
|
test("a key the platform refused lists nothing, not the bootstrap set", async () => {
|
|
f = fakeInkStone()
|
|
f.route("GET /v1/models", () => json({ error: "unauthorized" }, 401))
|
|
f.route("GET /api/tokenplan/v1/public/models", () => json(META))
|
|
const p = await plugin()
|
|
expect(await p.provider.models({ id: "intern-inkstone", models: { "glm-5.3": {} } }, { auth })).toEqual({})
|
|
})
|
|
|
|
test("a catalogue read that fails keeps the declared models", async () => {
|
|
f = fakeInkStone()
|
|
f.route("GET /v1/models", () => json({ error: "boom" }, 500))
|
|
f.route("GET /api/tokenplan/v1/public/models", () => json(META))
|
|
const p = await plugin()
|
|
const given = { "glm-5.3": {} }
|
|
const got = await p.provider.models({ id: "intern-inkstone", models: given }, { auth })
|
|
expect(got).toEqual(given)
|
|
})
|
|
|
|
test("an account with no auth yet still sees the bootstrap list", async () => {
|
|
f = fakeInkStone()
|
|
const p = await plugin()
|
|
const cfg = {}
|
|
await p.config(cfg)
|
|
const declared = cfg.provider["intern-inkstone"].models
|
|
expect(await p.provider.models({ id: "intern-inkstone", models: {} }, {})).toEqual(declared)
|
|
})
|
|
|
|
test("the loader serves the models with the key at the platform's base URL", async () => {
|
|
f = fakeInkStone()
|
|
const p = await plugin()
|
|
const loader = await p.auth.loader(async () => auth)
|
|
expect(loader.apiKey).toBe("sk-test")
|
|
expect(loader.baseURL).toBe(f.origin + "/v1")
|
|
})
|
|
|
|
test("the sign-in methods cover browser, API key, and API key + console token", async () => {
|
|
const p = await plugin()
|
|
const labels = p.auth.methods.map((m) => m.label)
|
|
expect(labels[0]).toContain("Browser sign-in")
|
|
expect(labels[1]).toContain("API key only")
|
|
expect(labels[2]).toContain("console token")
|
|
// the browser method must not block the host's authorize call: the browser
|
|
// work runs in its callback (the host's authorize deadline is short)
|
|
const browser = p.auth.methods[0]
|
|
const result = browser.authorize({})
|
|
if (process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY) {
|
|
// a displayless CI runner is refused up front, not left with a window it cannot open
|
|
expect(result.type).toBe("failed")
|
|
expect(result.error).toContain("no display")
|
|
} else {
|
|
expect(result.method).toBe("auto")
|
|
expect(typeof result.callback).toBe("function")
|
|
expect(result.instructions).toContain("Sign in there")
|
|
expect(result.url).toBe("")
|
|
}
|
|
// the browser method's key prompt is optional; a non-sk- one is refused
|
|
expect(browser.prompts[0].validate("")).toBeUndefined()
|
|
expect(browser.prompts[0].validate("sk-abc")).toBeUndefined()
|
|
expect(browser.prompts[0].validate("nope")).toContain("sk-")
|
|
})
|
|
|
|
test("the API-key + console-token method reads the token out of a pasted cookie", async () => {
|
|
f = fakeInkStone()
|
|
serve()
|
|
f.route("GET /api/tokenplan/v1/users/validate", () => json({ code: 0, data: { name: "张三", phone: "12345678901" } }))
|
|
const p = await plugin()
|
|
const method = p.auth.methods[2]
|
|
// a full cookie string must be reduced to just the uaa-token value
|
|
const okResult = await method.authorize({
|
|
apiKey: "sk-test",
|
|
uaaToken: "sid=SESSIONSECRET; uaa-token=abc.def.ghi; other=1",
|
|
})
|
|
expect(okResult.type).toBe("success")
|
|
expect(okResult.uaaToken).toBe("abc.def.ghi")
|
|
expect(JSON.stringify(okResult)).not.toContain("SESSIONSECRET")
|
|
// a bare token value is accepted as-is
|
|
const bare = await method.authorize({ apiKey: "sk-test", uaaToken: "abc.def.ghi" })
|
|
expect(bare.type).toBe("success")
|
|
expect(bare.uaaToken).toBe("abc.def.ghi")
|
|
// base64 padding at the end is still a bare token
|
|
const padded = await method.authorize({ apiKey: "sk-test", uaaToken: "abc.def.gh==" })
|
|
expect(padded.type).toBe("success")
|
|
// a string without a uaa-token is refused, never stored verbatim
|
|
for (const junk of ["sid=SESSIONSECRET", "Cookie: uaa-token=abc", "abc=def", "uaa-token", "abc def", "short"]) {
|
|
const bad = await method.authorize({ apiKey: "sk-test", uaaToken: junk })
|
|
expect(bad.type).toBe("failed")
|
|
expect(bad.error).toContain("uaa-token")
|
|
}
|
|
})
|
|
|
|
test("cleanDetail masks real uaa-token, sk-, Bearer and JWT runs", async () => {
|
|
const { cleanDetail } = await import("./lib/protocol.mjs")
|
|
expect(cleanDetail("failed: uaa-token=secretValue123456")).toBe("failed: ***")
|
|
expect(cleanDetail("cookie: uaa_token: secretValue123456; other")).toBe("cookie: ***; other")
|
|
expect(cleanDetail("auth header: Bearer opaqueToken123456")).toBe("auth header: ***")
|
|
expect(cleanDetail("key is sk-12345678abcdef")).toBe("key is ***")
|
|
expect(cleanDetail("jwt: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0In0.sig")).toBe("jwt: ***")
|
|
// diagnostic words must stay
|
|
expect(cleanDetail("error: token expired")).toBe("error: token expired")
|
|
})
|