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.
99 lines
3.9 KiB
JavaScript
99 lines
3.9 KiB
JavaScript
// A fake Chromium for tests: serves the DevTools HTTP + WebSocket endpoints
|
|
// and writes the DevToolsActivePort file, so the plugin's real openSession /
|
|
// waitForToken / CDP cookie read run against it. Modes come from argv:
|
|
// --headless=new → serve CDP (the renewal / post-sign-in read)
|
|
// (no headless) → the login window: wait for the plugin's close
|
|
import { writeFileSync, mkdirSync } from "node:fs"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
|
|
const args = process.argv.slice(2)
|
|
const profile = args.find((a) => a.startsWith("--user-data-dir="))?.slice(17) ?? "."
|
|
// bun build --compile's Windows launcher mangles argument values: the drive
|
|
// letter and part of the path are eaten. The test always makes the profile
|
|
// under tmpdir(), so the safe repair is: take the last path segment and
|
|
// re-seat it under tmpdir().
|
|
const profilePath = /^[A-Za-z]:/.test(profile)
|
|
? profile
|
|
: join(tmpdir(), profile.split(/[\\/]/).filter(Boolean).pop() ?? "fake-profile")
|
|
const headless = args.includes("--headless=new")
|
|
const url = args.find((a) => a.startsWith("http")) ?? ""
|
|
let pageTargetUrl = ""
|
|
|
|
try {
|
|
if (!headless) {
|
|
// login window. On Unix it waits for the plugin's SIGTERM and records
|
|
// the signal (the test asserts no SIGKILL was needed). On Windows
|
|
// TerminateProcess is uncatchable and the plugin's WM_CLOSE cannot reach
|
|
// a windowless exe, so exit on our own — otherwise the close path waits
|
|
// its full 15s grace period per sign-in.
|
|
const exit = () => {
|
|
try {
|
|
writeFileSync(join(profilePath, "close-signal.txt"), "SIGTERM")
|
|
} catch {}
|
|
process.exit(0)
|
|
}
|
|
if (process.platform === "win32") {
|
|
setTimeout(() => process.exit(0), 500)
|
|
} else {
|
|
process.on("SIGTERM", exit)
|
|
process.on("SIGINT", exit)
|
|
setInterval(() => {}, 10_000)
|
|
}
|
|
} else {
|
|
mkdirSync(profilePath, { recursive: true })
|
|
const server = Bun.serve({
|
|
port: 0,
|
|
fetch(req, server) {
|
|
const u = new URL(req.url)
|
|
if (u.pathname === "/json/version") return Response.json({ Browser: "FakeChrome/1.0" })
|
|
if (u.pathname === "/json/list") {
|
|
// the plugin's Cdp.connect picks a type:"page" target whose URL
|
|
// matches the console host and whose webSocketDebuggerUrl it can
|
|
// open — hand it exactly that, then serve the socket on upgrade
|
|
pageTargetUrl = `ws://127.0.0.1:${server.port}/devtools/page/1`
|
|
return Response.json([
|
|
{
|
|
type: "page",
|
|
url: url || "https://discovery.intern-ai.org.cn/token-plan/home",
|
|
webSocketDebuggerUrl: pageTargetUrl,
|
|
},
|
|
])
|
|
}
|
|
// any /devtools/* request is the WebSocket the plugin opened
|
|
const up = server.upgrade(req, { data: { page: u.pathname } })
|
|
if (up) return undefined
|
|
return new Response("not found", { status: 404 })
|
|
},
|
|
websocket: {
|
|
open() {},
|
|
message(ws, raw) {
|
|
const msg = JSON.parse(String(raw))
|
|
if (msg.method === "Network.getAllCookies") {
|
|
ws.send(
|
|
JSON.stringify({
|
|
id: msg.id,
|
|
result: {
|
|
cookies: [{ name: "uaa-token", value: "fake-uaa-token-for-tests", domain: ".intern-ai.org.cn" }],
|
|
},
|
|
}),
|
|
)
|
|
} else {
|
|
ws.send(JSON.stringify({ id: msg.id, result: {} }))
|
|
}
|
|
},
|
|
},
|
|
})
|
|
// the browser-level target: /devtools/browser/<id> on the same port
|
|
writeFileSync(join(profilePath, "DevToolsActivePort"), `${server.port}\n/devtools/browser/fake\n`)
|
|
console.error("[fake-chrome] serving on", server.port)
|
|
const exit = () => process.exit(0)
|
|
process.on("SIGTERM", exit)
|
|
process.on("SIGINT", exit)
|
|
// keep alive; the plugin closes us when it has the cookie
|
|
}
|
|
} catch (e) {
|
|
console.error("[fake-chrome] FAILED:", e?.message ?? e, e?.stack?.slice(0, 400))
|
|
process.exit(1)
|
|
}
|