The desktop sign-in of astudio and agnes-code is now an oauth method, so magpie shows no empty key box. When the desktop session is on disk it finishes at once; when it isn't, it links the official site and polls every 2s for up to 5 minutes, ending on cancel (the host's signal). Missing, unreadable (with the reason, read errors other than ENOENT kept), expired and banned are told apart; banned and non-Windows agnes-code throw so the host shows the reason. agnes-code decrypts the DPAPI master key once per sign-in (re-decrypted if the session frame no longer opens with it) and kills a PowerShell stuck past 15s. agnes-code's README and registry summary say how it differs from the `agnes` entry. By jinyue806. Reviewed at 3a6bb97 (base 17e5ef7; main since then only changed packages/workbuddy). Run at 3a6bb97: - mote, bun 1.4.2, HOME=$(mktemp -d) bun test: 4921 pass, 0 fail (374 files); bun scripts/check.mjs ok; node --test on both packages 62 pass. - xiaomi-gpu (Windows 11, node 22): node --test on both packages 62 pass. - Raw bytes of astudio/index.test.mjs: 0 single-backslash "D:\a", 22 "D:\\astudio". - magpie's plugin host (host.js from magpie origin/main e41ef6288, run by bun on mote, sandbox HOME): astudio authorize with nothing on disk gave the official URL and method auto; writing a session file 3s into the callback finished it in 4.0s and saved key + metadata to auth.json; with the file present the url was empty; banned came back as the error message; an aborted callback was dropped; agnes-code on macOS came back with "仅支持 Windows" and no poll. - Broken by hand: dropping the master-key cache's set reds "decrypted once per sign-in"; dropping the banned throw reds "refuses banned accounts". Not tried: a real DPAPI decrypt (Protect over ssh is "Access is denied", no interactive logon there), real AStudio or Agnes Code desktops. Left for a follow-up the author offered: a DPAPI failure itself is not remembered, so each 2s poll can spawn PowerShell again (bounded by the 5-minute wait).
642 lines
28 KiB
JavaScript
642 lines
28 KiB
JavaScript
// AStudio(科大讯飞 Astron Studio 桌面端)for OpenCode and magpie.
|
||
//
|
||
// AStudio 桌面端登录后,把账号的模型凭据与会话写进它的数据目录(Windows 下
|
||
// 注册表 HKCU\Software\AStudioStorage 登记 DataRoot):
|
||
// <DataRoot>/userdata/astron-session.json — modelBearerToken / token / uid
|
||
// <DataRoot>/runtime/acode-home/config.toml — [model_providers.astron-spark]
|
||
// experimental_bearer_token = "…"(与 session 的 modelBearerToken 同源)
|
||
// 两套云端接口:
|
||
// 模型调用 POST https://astronstudio-api-volces-prod.xf-yun.com/model-gateway/v1/responses
|
||
// (OpenAI Responses 协议,Authorization: Bearer <modelBearerToken>,
|
||
// 与桌面端写进 acode 内核的 base_url/wire_api 完全一致)
|
||
// 模型目录 GET https://astronstudio-api-volces-prod.xf-yun.com/api/v1/model-manager/models
|
||
// 账号面 https://agent.xfyun.cn/xingchen-studio/…(Cookie 会话鉴权):
|
||
// points/balance、membership/me、membership/plan-config、points/summary
|
||
//
|
||
// 本插件复用这份登录态:第一种登录方式直接从磁盘读取桌面端会话(Raccoon
|
||
// 「复用桌面端登录态」同款;magpie 的 oauth 方式,读不到时给出官网链接并轮询,
|
||
// 等用户装好桌面端登录),第二种让用户手动粘贴 modelBearerToken。目录与
|
||
// 思考档位按账号实时拉取(目录读取不计费)。AStudio 有「每日登录积分」
|
||
// (服务端在当天账号会话首次活跃时发放,客户端弹窗只做通知,无领取接口),
|
||
// auth.checkin 用「发放前后余额差」观测:涨了记 claimed,没涨不谎称已签——
|
||
// 记 inactive 并说明未观测到(宿主把 done 渲染成「今日已签到」,是没发生的断言)。
|
||
|
||
import { spawnSync } from "node:child_process"
|
||
import { readFileSync } from "node:fs"
|
||
import { homedir as osHomedir } from "node:os"
|
||
|
||
const ID = "astudio"
|
||
|
||
// 云端地址(来自桌面端 resolveAstronRuntimeProfile 的 production 分支)。
|
||
const MODEL_GATEWAY_BASE = "https://astronstudio-api-volces-prod.xf-yun.com/model-gateway/v1"
|
||
const MODEL_MANAGER_URL = "https://astronstudio-api-volces-prod.xf-yun.com/api/v1/model-manager/models"
|
||
const STUDIO_API_BASE = "https://agent.xfyun.cn/xingchen-studio/"
|
||
|
||
// 读取桌面端登录态失败时展示的跳转页:AStudio 官网(含桌面端下载)。
|
||
const OFFICIAL_SITE = "https://agent.xfyun.cn/"
|
||
// 等待「用户去装桌面端并登录」的窗口与轮询间隔。
|
||
const DESKTOP_POLL_MS = 2000
|
||
const DESKTOP_WAIT_MS = 5 * 60 * 1000
|
||
|
||
const REGISTRY_KEY = "HKCU\\Software\\AStudioStorage"
|
||
const REGISTRY_PREFIXES = ["Production", "Canary", "Development"]
|
||
|
||
// 未登录/拿不到桌面端数据时的兜底目录(桌面端未在注册表登记时也可能落在这些位置)。
|
||
// 函数吃 homedir 参数(readDesktopCredential 注入,缺省回落真实家目录),测试可重定向。
|
||
const SESSION_FALLBACKS = [
|
||
(home = osHomedir()) => join(home, ".acode", "acode", "userdata", "astron-session.json"),
|
||
]
|
||
|
||
const ACODE_CONFIG_FALLBACKS = [
|
||
(home = osHomedir()) => join(home, ".acode", "acode", "config.toml"),
|
||
]
|
||
|
||
// 模型目录读不到时的启动表(2026-11-01 实测目录快照;models 钩子会用实时目录覆盖)。
|
||
const BOOTSTRAP = [
|
||
{ id: "astronclaw-auto", name: "Auto", context: 262144, output: 32768 },
|
||
{ id: "spark-x2.6", name: "Spark-X2.6", context: 262144, output: 32768, badge: "Preview" },
|
||
{ id: "spark-x2.6-4b", name: "Spark-X2.6-Lite", context: 1048576, output: 110000, free: true },
|
||
{ id: "spark-x2.5", name: "Spark-X2.5", context: 262144, output: 32768 },
|
||
{ id: "xopglm52", name: "GLM-5.2", context: 1048576, output: 32768 },
|
||
{ id: "xopdeepseekv4pro0813", name: "DeepSeek-V4-Pro", context: 1048576, output: 32768 },
|
||
{ id: "xopdsv4flash0731in", name: "DeepSeek-V4-Flash", context: 1048576, output: 32768 },
|
||
]
|
||
|
||
function join(...parts) {
|
||
return parts.join("/")
|
||
}
|
||
|
||
// rowFromCatalogEntry 把 model-manager 的一个模型变成 OpenCode 的 Model。
|
||
// 套餐不按 token 计费,无 cost;倍率上游未给出,不标。
|
||
function rowFromCatalogEntry(m) {
|
||
const image = Array.isArray(m?.input_modalities) && m.input_modalities.includes("image")
|
||
const efforts = Array.isArray(m?.supported_reasoning_levels)
|
||
? m.supported_reasoning_levels.filter((l) => typeof l?.effort === "string" && l.effort)
|
||
: []
|
||
const reasoning = efforts.some((l) => l.effort !== "none")
|
||
const variants = reasoning
|
||
? Object.fromEntries(efforts.map((l) => [l.effort, { reasoningEffort: l.effort }]))
|
||
: undefined
|
||
const free = m?.badge?.text?.en === "Free"
|
||
return {
|
||
id: m.slug,
|
||
name: m.display_name ?? m.slug,
|
||
limit: {
|
||
context: typeof m?.context_window === "number" && m.context_window > 0 ? m.context_window : 0,
|
||
output: typeof m?.max_output_tokens === "number" && m.max_output_tokens > 0 ? m.max_output_tokens : 32768,
|
||
},
|
||
reasoning,
|
||
tool_call: true,
|
||
...(variants ? { variants } : {}),
|
||
...(free ? { free: true } : {}),
|
||
modalities: { input: image ? ["text", "image"] : ["text"], output: ["text"] },
|
||
capabilities: {
|
||
reasoning,
|
||
toolcall: true,
|
||
attachment: image,
|
||
input: { text: true, image },
|
||
output: { text: true },
|
||
},
|
||
}
|
||
}
|
||
|
||
function bootstrapRows() {
|
||
return Object.fromEntries(
|
||
BOOTSTRAP.map((m) => [
|
||
m.id,
|
||
{
|
||
id: m.id,
|
||
name: m.name,
|
||
limit: { context: m.context, output: m.output },
|
||
reasoning: true,
|
||
tool_call: true,
|
||
...(m.free ? { free: true } : {}),
|
||
modalities: { input: ["text"], output: ["text"] },
|
||
capabilities: {
|
||
reasoning: true,
|
||
toolcall: true,
|
||
attachment: false,
|
||
input: { text: true, image: false },
|
||
output: { text: true },
|
||
},
|
||
},
|
||
]),
|
||
)
|
||
}
|
||
|
||
// registryDataRoots 读注册表里登记的各 profile 数据根目录(Windows)。
|
||
function registryDataRoots(registryQuery = defaultRegistryQuery) {
|
||
const roots = []
|
||
for (const prefix of REGISTRY_PREFIXES) {
|
||
const value = registryQuery(REGISTRY_KEY, `${prefix}DataRoot`)
|
||
if (value) roots.push(value)
|
||
}
|
||
return roots
|
||
}
|
||
|
||
function defaultRegistryQuery(key, valueName) {
|
||
if (process.platform !== "win32") return null
|
||
try {
|
||
const r = spawnSync("reg", ["query", key, "/v", valueName, "/reg:64"], { timeout: 5000, windowsHide: true })
|
||
const out = `${r.stdout ?? ""}`
|
||
const m = out.match(/REG_SZ\s+(\S[^\r\n]*)/)
|
||
return m ? m[1].trim() : null
|
||
} catch {
|
||
return null
|
||
}
|
||
}
|
||
|
||
// readAcodeConfigToken 兜底:从 acode 内核的 config.toml 取凭据。
|
||
function readAcodeConfigToken(content) {
|
||
const section = content.match(/^\s*\[\s*model_providers\s*\.\s*astron-spark\s*\]\s*$/m)
|
||
if (!section) return null
|
||
const after = content.slice(section.index + section[0].length)
|
||
const nextSection = after.search(/^\s*\[/m)
|
||
const body = nextSection >= 0 ? after.slice(0, nextSection) : after
|
||
const token = body.match(/^\s*experimental_bearer_token\s*=\s*"([^"]+)"\s*$/m)?.[1]
|
||
const uid = body.match(/^\s*uid\s*=\s*"([^"]+)"\s*$/m)?.[1]
|
||
return token ? { key: token, uid: uid ?? undefined } : null
|
||
}
|
||
|
||
// readDesktopCredential 找到桌面端登录态并返回 { key, uid, session, source }。
|
||
// session 是账号面的 Cookie 材料(token/ssoSessionId/accountId),积分与签到用。
|
||
// 找不到抛错,error message 面向用户。
|
||
function readDesktopCredential({ env = process.env, homedir = osHomedir(), readFileImpl, registryQuery } = {}) {
|
||
const readFile = readFileImpl ?? ((path) => readFileSync(path, "utf8"))
|
||
const sessionPaths = []
|
||
const configPaths = []
|
||
const configured = env.ASTUDIO_HOME?.trim() || env.SYNARA_HOME?.trim()
|
||
if (configured) {
|
||
sessionPaths.push(join(configured, "userdata", "astron-session.json"))
|
||
configPaths.push(join(configured, "runtime", "acode-home", "config.toml"))
|
||
}
|
||
for (const root of registryDataRoots(registryQuery)) {
|
||
sessionPaths.push(join(root, "userdata", "astron-session.json"))
|
||
configPaths.push(join(root, "runtime", "acode-home", "config.toml"))
|
||
}
|
||
for (const p of SESSION_FALLBACKS) sessionPaths.push(p(homedir))
|
||
for (const p of ACODE_CONFIG_FALLBACKS) configPaths.push(p(homedir))
|
||
|
||
const problems = []
|
||
for (const path of sessionPaths) {
|
||
const raw = readFileOrNull(path, readFile, problems)
|
||
if (raw === null) continue
|
||
let session
|
||
try {
|
||
session = JSON.parse(raw)
|
||
} catch {
|
||
problems.push(`${path} 不是有效 JSON`)
|
||
continue
|
||
}
|
||
if (session.banned === true) throw Object.assign(new Error(`AStudio 账号已被封禁(${path})`), { code: "banned" })
|
||
const key = typeof session.modelBearerToken === "string" && session.modelBearerToken.trim()
|
||
? session.modelBearerToken.trim()
|
||
: typeof session.token === "string" && session.token.trim()
|
||
? session.token.trim()
|
||
: null
|
||
if (!key) {
|
||
problems.push(`${path} 里没有 modelBearerToken/token`)
|
||
continue
|
||
}
|
||
if (key.length > 4096) {
|
||
problems.push(`${path} 的凭据异常地长`)
|
||
continue
|
||
}
|
||
const uid = typeof session.uid === "string" && session.uid.trim() ? session.uid.trim() : undefined
|
||
const cookieSession = typeof session.token === "string" && session.token.trim() && typeof session.accountId === "string" && session.accountId.trim()
|
||
? { token: session.token.trim(), ssoSessionId: typeof session.ssoSessionId === "string" ? session.ssoSessionId.trim() : undefined, accountId: session.accountId.trim() }
|
||
: undefined
|
||
return { key, uid, session: cookieSession, source: path }
|
||
}
|
||
|
||
for (const path of configPaths) {
|
||
const raw = readFileOrNull(path, readFile, problems)
|
||
if (raw === null) continue
|
||
const parsed = readAcodeConfigToken(raw)
|
||
if (parsed) return { ...parsed, session: undefined, source: path }
|
||
problems.push(`${path} 里没有 astron-spark 凭据`)
|
||
}
|
||
|
||
const detail = problems.length ? `:${problems.join(";")}` : ""
|
||
// 「什么都没找到」与「有数据但读不出来」分开说:后者的用户多半已经装好了。
|
||
const message = problems.length
|
||
? `AStudio 桌面端登录态读不出来${detail}。请先启动 AStudio 并登录,或改用「粘贴 modelBearerToken」方式。`
|
||
: "未找到 AStudio 桌面端登录态。请先启动 AStudio 并登录,或改用「粘贴 modelBearerToken」方式。"
|
||
throw Object.assign(
|
||
new Error(message),
|
||
// problems 为空 = 这些路径上什么都没有(新机器);非空 = 有 AStudio 的数据但用不了。
|
||
{ code: problems.length ? "unreadable" : "missing" },
|
||
)
|
||
}
|
||
|
||
// readFileOrNull 读一个候选文件:不存在(ENOENT)算「这台机器上没有」,别的读盘
|
||
// 错误(权限、I/O)要留进 problems,否则用户只会看到「没装」而看不出是读不了。
|
||
function readFileOrNull(path, readFile, problems) {
|
||
try {
|
||
return readFile(path)
|
||
} catch (error) {
|
||
if (error?.code !== "ENOENT") problems.push(`${path} 读取失败(${error?.message ?? "未知错误"})`)
|
||
return null
|
||
}
|
||
}
|
||
|
||
// 桌面端 buildAstronCookieHeader 的等价实现(agent.xfyun.cn 的 Cookie 会话)。
|
||
function buildCookieHeader(session) {
|
||
const parts = []
|
||
if (session?.ssoSessionId) parts.push(`ssoSessionId=${session.ssoSessionId}`, `sso_sessionid=${session.ssoSessionId}`)
|
||
if (session?.accountId) parts.push(`account_id=${session.accountId}`)
|
||
if (session?.token) parts.push(`token=${session.token}`)
|
||
return parts.join("; ")
|
||
}
|
||
|
||
// studioJson 调账号面接口;信封 flag:true&&code:0 视为成功并返回 data,
|
||
// 401/403(含 code 80000 登录异常)抛带 signIn:"expired" 的错。
|
||
async function studioJson(path, { session, fetchImpl, method = "GET", body, operation = path }) {
|
||
let res
|
||
try {
|
||
res = await fetchImpl(new URL(path, STUDIO_API_BASE), {
|
||
method,
|
||
headers: {
|
||
accept: "application/json",
|
||
...(body !== undefined ? { "content-type": "application/json" } : {}),
|
||
cookie: buildCookieHeader(session),
|
||
},
|
||
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
||
signal: AbortSignal.timeout(15000),
|
||
redirect: "error",
|
||
})
|
||
} catch (error) {
|
||
throw new Error(`${operation} 网络失败:${error?.cause?.code || error?.message || "未知错误"}`)
|
||
}
|
||
if (res.status === 401 || res.status === 403) {
|
||
throw Object.assign(new Error(`AStudio 会话已失效(HTTP ${res.status}),请重新登录 AStudio`), { signIn: "expired" })
|
||
}
|
||
let payload
|
||
try {
|
||
payload = await res.json()
|
||
} catch {
|
||
throw new Error(`${operation} 返回的不是 JSON(HTTP ${res.status})`)
|
||
}
|
||
const envelope = payload && typeof payload === "object" && !Array.isArray(payload) ? payload : {}
|
||
if (!res.ok || (envelope.flag === false || (envelope.code !== undefined && envelope.code !== 0))) {
|
||
if (envelope.code === 80000 || /重新登录/.test(String(envelope.desc ?? ""))) {
|
||
throw Object.assign(new Error(`AStudio 会话已失效,请重新登录 AStudio`), { signIn: "expired" })
|
||
}
|
||
throw new Error(`${operation} 失败(HTTP ${res.status}${envelope.desc ? `:${envelope.desc}` : ""})`)
|
||
}
|
||
return envelope.data
|
||
}
|
||
|
||
async function fetchCatalog(key, fetchImpl) {
|
||
return await fetchImpl(MODEL_MANAGER_URL, {
|
||
headers: { authorization: `Bearer ${key}`, accept: "application/json" },
|
||
signal: AbortSignal.timeout(15000),
|
||
redirect: "error",
|
||
})
|
||
}
|
||
|
||
// authResult 把读到的桌面端凭据变成 magpie 的登录结果:带 key 会被存成 API key,
|
||
// metadata 整份留给 loader / usage / checkin(session 是账号面的 Cookie 材料)。
|
||
function authResult(cred) {
|
||
return {
|
||
type: "success",
|
||
key: cred.key,
|
||
accountId: cred.uid,
|
||
metadata: {
|
||
uid: cred.uid,
|
||
source: cred.source,
|
||
method: "desktop",
|
||
session: cred.session,
|
||
},
|
||
}
|
||
}
|
||
|
||
// probeDesktopCredential 读一次桌面端登录态,把「没找到」变成返回值而不是异常。
|
||
function probeDesktopCredential(deps = {}) {
|
||
try {
|
||
return { cred: readDesktopCredential(deps) }
|
||
} catch (error) {
|
||
return { error }
|
||
}
|
||
}
|
||
|
||
// waitForDesktopCredential 轮询磁盘,等用户在本机装好并登录桌面端。返回
|
||
// {status:"ready", result} / {status:"timeout", error} / {status:"aborted"}。
|
||
async function waitForDesktopCredential({ read, sleep, now, signal, waitMs = DESKTOP_WAIT_MS, stepMs = DESKTOP_POLL_MS }) {
|
||
const deadline = now() + waitMs
|
||
for (;;) {
|
||
// magpie 取消登录时会 abort 这个 signal,别再空转剩下的几分钟。
|
||
if (signal?.aborted) return { status: "aborted" }
|
||
const trial = read()
|
||
if (trial.cred) return { status: "ready", result: authResult(trial.cred) }
|
||
if (now() >= deadline) return { status: "timeout", error: trial.error }
|
||
await sleep(stepMs)
|
||
}
|
||
}
|
||
|
||
// reasonOf 把读盘失败压成一句给用户看的话(超时失败时带上最后一次的原因)。
|
||
function reasonOf(error) {
|
||
return error?.code === "unreadable" || error?.code === "banned"
|
||
? error.message
|
||
: "本机没有 AStudio 桌面端登录态"
|
||
}
|
||
|
||
function createPlugin({ fetchImpl = globalThis.fetch, env = process.env, homedir = osHomedir(), registryQuery, readFileImpl, sleepImpl, nowMsImpl = () => Date.now() } = {}) {
|
||
const sleep = sleepImpl ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)))
|
||
|
||
async function readBalance(session) {
|
||
return await studioJson("points/balance", { session, fetchImpl, operation: "积分余额" })
|
||
}
|
||
|
||
return {
|
||
config: async (cfg) => {
|
||
cfg.provider ??= {}
|
||
// ??= 保留用户在 plugins.json 里自己配置的同名供应商。
|
||
cfg.provider[ID] ??= {
|
||
name: "AStudio",
|
||
npm: "@ai-sdk/openai", // OpenAI Responses;网关在 baseURL 后拼 /responses
|
||
api: MODEL_GATEWAY_BASE,
|
||
models: bootstrapRows(),
|
||
}
|
||
},
|
||
|
||
auth: {
|
||
provider: ID,
|
||
methods: [
|
||
{
|
||
// oauth 方式:magpie 不显示 key 输入框,authorize 必须立刻返回
|
||
// { url, instructions, method, callback },结果由 callback 交回。
|
||
// 读得到就立刻完成(url 留空);读不到就给出官网链接,边等边让用户去装。
|
||
type: "oauth",
|
||
label: "读取 AStudio 桌面端登录态",
|
||
async authorize(_inputs, { signal } = {}) {
|
||
const read = () => probeDesktopCredential({ env, homedir, registryQuery, readFileImpl })
|
||
const probe = read()
|
||
if (probe.cred) {
|
||
return {
|
||
url: "",
|
||
instructions: `已读取本机 AStudio 登录态(${probe.cred.uid}),正在完成登录…`,
|
||
method: "auto",
|
||
callback: async () => authResult(probe.cred),
|
||
}
|
||
}
|
||
// 硬失败:抛错让宿主把原因原样当登录失败显示(0.1.2810 也认 {type:"failed"},
|
||
// 但抛错在任何宿主版本上都成立),不能落到下面那条「装上吧」的说明里。
|
||
if (probe.error?.code === "banned") throw new Error(probe.error.message)
|
||
return {
|
||
url: OFFICIAL_SITE,
|
||
instructions:
|
||
probe.error?.code === "unreadable"
|
||
? `${probe.error.message}(上方链接是官网;在 AStudio 里登录或修好后,这一步会自动完成。)`
|
||
: "本机没有找到 AStudio 桌面端登录态。装上并登录 AStudio 桌面端就好——上方链接是官网,可下载;登录后这一步会自动完成。已有 token 的话,也可以改用「粘贴 modelBearerToken」。",
|
||
method: "auto",
|
||
callback: async (_code, { signal: callbackSignal } = {}) => {
|
||
const waited = await waitForDesktopCredential({ read, sleep, now: nowMsImpl, signal: callbackSignal ?? signal })
|
||
if (waited.status === "ready") return waited.result
|
||
if (waited.status === "aborted") return { type: "failed", error: "登录已取消" }
|
||
return {
|
||
type: "failed",
|
||
error: `仍未检测到 AStudio 桌面端登录态(最后一次:${reasonOf(waited.error)})。装上或登录 AStudio 后,重新点一次「读取 AStudio 桌面端登录态」即可。`,
|
||
}
|
||
},
|
||
}
|
||
},
|
||
},
|
||
{
|
||
type: "api",
|
||
label: "粘贴 modelBearerToken",
|
||
prompts: [
|
||
{
|
||
type: "password",
|
||
key: "modelBearerToken",
|
||
message: "modelBearerToken(astron-session.json 或 acode config.toml 里的值)",
|
||
validate: (v) => (v && v.trim() ? undefined : "必填"),
|
||
},
|
||
{
|
||
type: "text",
|
||
key: "uid",
|
||
message: "账号 uid(可选,用于显示,如 18800000000)",
|
||
},
|
||
{
|
||
type: "password",
|
||
key: "sessionToken",
|
||
message: "网页会话 token(astron-session.json 的 token 字段;用于积分余额与每日签到)",
|
||
},
|
||
{
|
||
type: "text",
|
||
key: "accountId",
|
||
message: "账号面 accountId(与 sessionToken 一起填,积分余额与签到才可用)",
|
||
},
|
||
{
|
||
type: "password",
|
||
key: "ssoSessionId",
|
||
message: "ssoSessionId(可选,Cookie 会话补充材料)",
|
||
},
|
||
],
|
||
// 粘贴路径此前只问不存(没有 authorize,输入全部丢弃)。现在按桌面端
|
||
// 成功返回的同一形状落库;token+accountId 齐才组得出 Cookie 会话。
|
||
async authorize(inputs) {
|
||
const key = String(inputs?.modelBearerToken ?? "").trim()
|
||
if (!key) return { type: "failed", error: "modelBearerToken 必填" }
|
||
if (key.length > 4096) return { type: "failed", error: "凭据异常地长" }
|
||
const token = String(inputs?.sessionToken ?? "").trim()
|
||
const accountId = String(inputs?.accountId ?? "").trim()
|
||
const ssoSessionId = String(inputs?.ssoSessionId ?? "").trim()
|
||
const uid = String(inputs?.uid ?? "").trim() || undefined
|
||
const session = token && accountId
|
||
? { token, accountId, ...(ssoSessionId ? { ssoSessionId } : {}) }
|
||
: undefined
|
||
return {
|
||
type: "success",
|
||
key,
|
||
accountId: accountId || uid,
|
||
metadata: {
|
||
uid,
|
||
source: "paste",
|
||
method: "paste",
|
||
...(session ? { session } : {}),
|
||
},
|
||
}
|
||
},
|
||
},
|
||
],
|
||
// @ai-sdk/openai 把 apiKey 发成 Authorization: Bearer <key>,与桌面端内核
|
||
// 对 model-gateway 的调用完全一致,无需自定义头。
|
||
async loader(getAuth) {
|
||
const auth = await getAuth()
|
||
if (auth?.type !== "api" || !auth.key) return {}
|
||
return { baseURL: MODEL_GATEWAY_BASE, apiKey: auth.key }
|
||
},
|
||
// 有 Cookie 会话时显示真实积分余额与会员档;只有模型凭据时退回目录
|
||
// 连通性检查。读取均不计费。
|
||
async usage(getAuth) {
|
||
const auth = await getAuth()
|
||
if (auth?.type !== "api" || !auth.key) return {}
|
||
const session = auth.metadata?.session
|
||
if (!session?.token) {
|
||
let res
|
||
try {
|
||
res = await fetchCatalog(auth.key, fetchImpl)
|
||
} catch (error) {
|
||
return { error: `AStudio 不可达:${error?.cause?.code || error?.message || "网络错误"}`, windows: [] }
|
||
}
|
||
if (res.status === 401 || res.status === 403) {
|
||
return { error: `AStudio 拒绝了这份凭据(HTTP ${res.status}),请重新登录`, signIn: "expired", windows: [] }
|
||
}
|
||
if (!res.ok) return { error: `AStudio 返回 HTTP ${res.status}`, windows: [] }
|
||
return { plan: "AStudio", windows: [] }
|
||
}
|
||
// 单项失败不塌方(一边挂了另一边还能显示),但登录态过期必须透传,
|
||
// 不能折叠成「账号信息读取失败」。
|
||
const tolerate = (promise) =>
|
||
promise.catch((error) => {
|
||
if (error?.signIn === "expired") throw error
|
||
return null
|
||
})
|
||
let balance, membership
|
||
try {
|
||
;[balance, membership] = await Promise.all([
|
||
tolerate(readBalance(session)),
|
||
tolerate(studioJson("membership/me", { session, fetchImpl, operation: "会员信息" })),
|
||
])
|
||
} catch (error) {
|
||
return { error: error?.message ?? "AStudio 账号信息读取失败", signIn: "expired", windows: [] }
|
||
}
|
||
if (!balance && !membership) {
|
||
return { error: "AStudio 账号信息读取失败", windows: [] }
|
||
}
|
||
const windows = []
|
||
if (balance && typeof balance.totalBalance === "number") {
|
||
windows.push({
|
||
name: "通用积分",
|
||
balance: balance.totalBalance,
|
||
aside: false,
|
||
...(balance.activityNextExpireTime && balance.activityBalance > 0
|
||
? { runsOutAt: balance.activityNextExpireTime, runsOut: balance.activityBalance }
|
||
: {}),
|
||
})
|
||
}
|
||
if (balance && typeof balance.sparkTotalBalance === "number") {
|
||
windows.push({ name: "星火积分", balance: balance.sparkTotalBalance, aside: false })
|
||
}
|
||
return {
|
||
...(membership?.planName ? { plan: membership.planName } : { plan: "AStudio" }),
|
||
...(membership?.uid != null ? { user: String(membership.uid) } : {}),
|
||
...(membership?.endTime ? { until: membership.endTime } : {}),
|
||
windows,
|
||
}
|
||
},
|
||
// 「每日登录积分」:服务端在当天账号会话首次活跃时发放,客户端没有领取
|
||
// 接口(弹窗只做通知)。这里以一组账号面请求代表当天的会话活跃,并用
|
||
// 发放前后的余额差观测结果:涨了是 claimed,没涨是 done。
|
||
async checkin(getAuth) {
|
||
const auth = await getAuth()
|
||
if (auth?.type !== "api" || !auth.key) return {}
|
||
const session = auth.metadata?.session
|
||
if (!session?.token) {
|
||
return { outcome: "ineligible", message: "该账号只有模型凭据(无网页会话),无法执行每日登录签到" }
|
||
}
|
||
let config
|
||
try {
|
||
config = await studioJson("membership/plan-config", { session, fetchImpl, operation: "签到配置" })
|
||
} catch (error) {
|
||
if (error?.signIn === "expired") throw error
|
||
return { outcome: "failed", message: error?.message ?? "签到配置读取失败" }
|
||
}
|
||
if (config?.dailyLoginEnabled === false) {
|
||
return { outcome: "inactive", message: "当前没有每日登录积分活动" }
|
||
}
|
||
let before
|
||
try {
|
||
before = await readBalance(session)
|
||
} catch (error) {
|
||
if (error?.signIn === "expired") throw error
|
||
return { outcome: "failed", message: error?.message ?? "积分余额读取失败" }
|
||
}
|
||
// 会话活跃 touch:与桌面端启动时同类只读请求。
|
||
await studioJson("membership/me", { session, fetchImpl, operation: "会员信息" }).catch(() => null)
|
||
await studioJson("points/summary", { session, fetchImpl, operation: "积分汇总" }).catch(() => null)
|
||
await sleep(2500)
|
||
let after
|
||
try {
|
||
after = await readBalance(session)
|
||
} catch (error) {
|
||
if (error?.signIn === "expired") throw error
|
||
return { outcome: "failed", message: error?.message ?? "积分余额读取失败" }
|
||
}
|
||
if (typeof before?.totalBalance !== "number" || typeof after?.totalBalance !== "number") {
|
||
return { outcome: "failed", message: "积分余额读数不完整" }
|
||
}
|
||
const delta = after.totalBalance - before.totalBalance
|
||
if (delta > 0) return { outcome: "claimed", credit: delta, message: `每日登录积分 +${delta}` }
|
||
// 余额没涨 ≠ 已发放:无领取接口,发放可能挂靠桌面端登录,本次未观测到。
|
||
// 不能报 done(宿主渲染成「今日已签到」,是没发生的断言);inactive 是
|
||
// 静默结算态,message 仍会把实情带给用户。
|
||
return { outcome: "inactive", message: "未观测到新发放:余额无变化(发放可能挂靠桌面端登录)" }
|
||
},
|
||
},
|
||
|
||
provider: {
|
||
id: ID,
|
||
// 按账号实时拉目录(模型、思考档位、限免标记都可能随账号/时间变)。
|
||
async models(provider, { auth } = {}) {
|
||
if (auth?.type !== "api" || !auth.key) return provider.models
|
||
let res
|
||
try {
|
||
res = await fetchCatalog(auth.key, fetchImpl)
|
||
} catch {
|
||
// 网络失败保留原列表,与目录接口非 2xx 同待遇。
|
||
return provider.models
|
||
}
|
||
if (res.status === 401 || res.status === 403) {
|
||
throw Object.assign(new Error(`AStudio 拒绝了这份凭据(HTTP ${res.status}),请重新登录`), { signIn: "expired" })
|
||
}
|
||
if (!res.ok) return provider.models
|
||
let body
|
||
try {
|
||
body = await res.json()
|
||
} catch {
|
||
throw new Error("AStudio 模型目录不是有效 JSON")
|
||
}
|
||
const list = body?.data?.models
|
||
if (!Array.isArray(list) || list.some((m) => typeof m?.slug !== "string" || !m.slug)) {
|
||
throw new Error("AStudio 模型目录格式异常")
|
||
}
|
||
const rows = Object.fromEntries(
|
||
list
|
||
.filter((m) => m.visibility !== "hidden" && m.supported_in_api !== false)
|
||
.map((m) => [m.slug, rowFromCatalogEntry(m)]),
|
||
)
|
||
if (!Object.keys(rows).length) throw new Error("AStudio 模型目录为空")
|
||
return rows
|
||
},
|
||
},
|
||
}
|
||
}
|
||
|
||
// 插件是唯一的导出函数;magpie 会把入口导出的每个函数都当插件调用,测试需要的
|
||
// 内部函数按社区约定放在 _internal 对象上。
|
||
export const AStudioPlugin = async () => createPlugin()
|
||
export const _internal = {
|
||
createPlugin,
|
||
rowFromCatalogEntry,
|
||
bootstrapRows,
|
||
readDesktopCredential,
|
||
probeDesktopCredential,
|
||
waitForDesktopCredential,
|
||
readFileOrNull,
|
||
reasonOf,
|
||
authResult,
|
||
readAcodeConfigToken,
|
||
registryDataRoots,
|
||
buildCookieHeader,
|
||
studioJson,
|
||
MODEL_GATEWAY_BASE,
|
||
MODEL_MANAGER_URL,
|
||
STUDIO_API_BASE,
|
||
OFFICIAL_SITE,
|
||
DESKTOP_WAIT_MS,
|
||
BOOTSTRAP,
|
||
ID,
|
||
}
|