Files
我是一条没有名字的区 e7004d9354 inkstone 0.1.0: Intern InkStone (书生·端砚) Token Plan, browser sign-in, credit card and ink-* key chat (#775)
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 blob
afe074cb. 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/main e7cb9dc5:
  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.
2026-11-07 15:07:06 +08:00

261 lines
10 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 模型目录:TokenPlan 的聊天模型清单。
//
// 事实来源分两层,且**两层都是活的**:
// 1. `GET /v1/models`(API Key):这个 Key 真能调用的模型——权威的"有谁";
// 2. `GET /api/tokenplan/v1/public/models`(无需登录):上下文、输入类型、
// 能力、协议与墨点单价——权威的"长什么样"。
//
// 官网文档的模型表与 live 不一致(文档多出已下线的 id),所以这里**不硬编码
// 模型表**:boot 清单只用于冷启动兜底,真实清单永远来自接口。决策模型
// (SystemOne)不是聊天接口,永不进入聊天清单。思考档位不做:平台明确
// 不支持通过请求参数开关/调整思考强度。
export const DEFAULT_CONTEXT = 262_144
// 文档与公共接口都没给最大输出;P0 用真实账号实测后更新。给一个保守值,
// 让 agent 能请求的输出不被人为放大。
export const DEFAULT_OUTPUT = 32_768
// 冷启动兜底清单(登录前 / 两次读取都失败时)。只列官方文档与 live 列表
// 都确认过的 id;有一个 id 在本仓库的抓包里始终被脱敏,宁缺毋滥,不硬编码。
// `free` 是文档口径的限时免费(价格 0),运行时以接口价格为准。
// `rate` 是输入价归一化(1000 墨点/1M = x1),展示用,不参与计费。
export const BOOT_MODELS = {
"Agents-A1": { context: 262_144, reasoning: false, tool: true, image: true, free: true },
"Atria-Dawn": { context: 524_288, reasoning: false, tool: false, image: false, free: true },
"Sage-A1.5": { context: 524_288, reasoning: false, tool: true, image: true, free: true },
"intern-s2": { context: 262_144, reasoning: false, tool: true, image: true, free: true },
"deepseek-v4.1-flash-1017": { context: 1_048_576, reasoning: true, tool: true, image: true, rate: 1 },
"glm-5.3": { context: 1_048_576, reasoning: true, tool: true, image: false, rate: 8 },
"minimax-m3": { context: 1_048_576, reasoning: true, tool: true, image: false, rate: 2.1 },
"qwen3.8-27b": { context: 262_144, reasoning: true, tool: true, image: true, rate: 0.8 },
}
const text = (v) => (typeof v === "string" && v.trim() ? v.trim() : undefined)
const num = (v) => {
const n = typeof v === "number" ? v : Number(String(v ?? "").trim())
return Number.isFinite(n) && n > 0 ? n : undefined
}
const list = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === "string" && x) : undefined)
/** 归一化一条公共元数据记录(兼容中英文能力字段)。 */
export function normalizePublic(row) {
if (!row || typeof row !== "object") return null
const id = text(row.id)
if (!id) return null
const caps = list(row.capabilities) ?? []
const capsEn = list(row.capabilities_en) ?? []
const has = (zh, en) => caps.includes(zh) || capsEn.includes(en)
// 价格字段缺失或非法 ≠ 价格为 0:只有两个字段都是有限的非负数、且都为 0,
// 才敢标免费,否则一条缺价格的记录会被误标 free,用户以为不扣墨点。
const priceOf = (v) => {
if (v === null || v === undefined) return undefined
if (typeof v === "string" && !v.trim()) return undefined
const n = typeof v === "number" ? v : Number(String(v).trim())
return Number.isFinite(n) && n >= 0 ? n : undefined
}
const inputPrice = priceOf(row.input_uncached_price)
const outputPrice = priceOf(row.output_price)
const hasPrice = inputPrice !== undefined && outputPrice !== undefined
return {
id,
name: text(row.display_name) ?? text(row.display_name_en) ?? id,
type: text(row.model_type) ?? "chat",
inputs: list(row.input_types) ?? ["text"],
context: num(row.context_length) ?? DEFAULT_CONTEXT,
reasoning: has("深度思考", "Reasoning"),
tool: has("工具调用", "Tool calling"),
...(hasPrice ? { free: inputPrice === 0 && outputPrice === 0 } : {}),
...(inputPrice > 0 ? { rate: Math.round((inputPrice / 1000) * 10) / 10 } : {}),
protocols: new Set(list(row.supported_protocols) ?? []),
marketing: text(row.marketing_label) ?? "",
}
}
/** 从各式信封里取公共模型数组({data:{models:[…]}} / {data:[…]} / 裸数组)。 */
export function publicRowsOf(payload) {
const rows =
payload?.data?.models ??
(Array.isArray(payload?.data) ? payload.data : undefined) ??
payload?.models ??
[]
return (Array.isArray(rows) ? rows : []).map(normalizePublic).filter(Boolean)
}
/** 一条 live 目录行(/v1/models 的 data[]),兼容文档示例之外的字段。 */
export function normalizeLive(row) {
if (!row || typeof row !== "object") return null
const id = text(row.id) ?? text(row.model_id)
if (!id) return null
return {
id,
context: num(row.context_length ?? row.contextLength),
output: num(row.max_output_length ?? row.maxOutputLength ?? row.max_output_tokens),
inputs: list(row.input_modalities ?? row.inputModalities),
features: list(row.supported_features ?? row.supportedFeatures) ?? [],
}
}
export function liveRowsOf(payload) {
const rows = Array.isArray(payload?.data) ? payload.data : []
return rows.map(normalizeLive).filter(Boolean)
}
const MODALITIES = ["text", "image", "audio", "video", "pdf"]
/**
* 一条模型,magpie 的 provider.models / config 两处用的形状。没有 variants:
* 平台不支持思考档位,给了也是无效选择器。
* `free`/`rate` 是 magpie 专有展示字段(模型选择器徽标),OpenCode 忽略。
*/
export function modelOf({ id, name, context, output, inputs, reasoning, tool, image, free, rate }) {
const input = inputs?.length ? inputs : ["text"]
const inputFlags = Object.fromEntries(MODALITIES.map((k) => [k, input.includes(k)]))
const attach = input.some((k) => k !== "text")
return {
name,
reasoning: reasoning === true,
tool_call: tool === true,
attachment: attach,
modalities: { input, output: ["text"] },
limit: { context: context || DEFAULT_CONTEXT, output: output || DEFAULT_OUTPUT },
capabilities: {
reasoning: reasoning === true,
toolcall: tool === true,
attachment: attach,
input: inputFlags,
output: { text: true },
},
...(free ? { free: true } : {}),
...(rate ? { rate } : {}),
}
}
/** boot 清单的一条(config 的冷启动兜底)。 */
export function bootModelOf(id, boot = BOOT_MODELS[id]) {
if (!boot) return null
return modelOf({
id,
name: id,
context: boot.context,
output: DEFAULT_OUTPUT,
inputs: boot.image ? ["text", "image"] : ["text"],
reasoning: boot.reasoning,
tool: boot.tool,
image: boot.image,
free: boot.free,
rate: boot.rate,
})
}
export function bootModels() {
const out = {}
for (const id of Object.keys(BOOT_MODELS)) {
const m = bootModelOf(id)
if (m) out[id] = m
}
return out
}
/** 是否值得列成聊天模型:live 有它,且(有元数据时)它真支持 openai_chat。 */
export function isChatCandidate(live, meta) {
if (!live?.id) return false
if (meta && meta.type === "decision") return false
if (meta && meta.protocols.size && !meta.protocols.has("openai_chat")) return false
return true
}
/** 一条目录合并结果 → provider.models 返回的一条。 */
export function mergeModel(live, meta) {
const id = live?.id ?? meta?.id
if (!id) return null
const inputs = meta?.inputs ?? live?.inputs
const reasoning = meta ? meta.reasoning : (live?.features ?? []).includes("reasoning")
const tool = meta ? meta.tool : (live?.features ?? []).includes("tools")
const image = inputs ? inputs.includes("image") : false
return modelOf({
id,
name: meta?.name ?? id,
context: meta?.context ?? live?.context,
output: live?.output ?? DEFAULT_OUTPUT,
inputs,
reasoning,
tool,
image,
free: meta?.free,
rate: meta?.rate,
})
}
/**
* 拉一次、按 Key 分桶缓存的模型目录。
*
* 缓存必须按 API Key 分桶:provider.models 是按账号调用的,A 账号的清单
* 不能给 B 账号用;公共元数据没有账号,全局缓存一段即可。并发请求不 join
* 别的账号的 in-flight。失败时保留该 Key 上次成功的清单;空清单做短负缓存,
* 避免新 Key 生效前的风暴。
*/
export function createCatalog({ fetchLive, fetchMeta, refreshMs = 6 * 3600_000, metaTtlMs = 10 * 60_000, negativeMs = 30_000 } = {}) {
let metaRows = null
let metaAt = 0
const buckets = new Map()
async function meta() {
const now = Date.now()
if (metaRows && now - metaAt < metaTtlMs) return metaRows
const rows = await fetchMeta()
if (rows.length) {
metaRows = rows
metaAt = now
}
return metaRows ?? []
}
function bucketFor(apiKey) {
let b = buckets.get(apiKey)
if (!b) buckets.set(apiKey, (b = { rows: null, rawAt: 0, inflight: null, emptyAt: 0 }))
return b
}
return {
/** 最近一次成功的公共元数据(没有则 null);boot 兜底用。 */
async meta() {
return meta()
},
/** 这个 Key 的可聊模型:[{ id, meta, live }],失败抛错。 */
async forKey(apiKey) {
if (!apiKey) throw new Error("没有 API Key")
const b = bucketFor(apiKey)
const now = Date.now()
if (b.rows && now - b.rawAt < refreshMs) return b.rows
if (b.inflight) return b.inflight
if (b.emptyAt && now - b.emptyAt < negativeMs) return b.rows ?? []
b.inflight = (async () => {
try {
const live = await fetchLive(apiKey)
const metas = await meta().catch(() => [])
const byId = new Map(metas.map((m) => [m.id, m]))
const rows = live
.map((l) => ({ live: l, meta: byId.get(l.id) }))
.filter(({ live, meta }) => isChatCandidate(live, meta))
.map(({ live, meta }) => ({ id: live.id, live, meta, model: mergeModel(live, meta) }))
.filter((r) => r.model)
if (rows.length) {
b.rows = rows
b.rawAt = Date.now()
b.emptyAt = 0
} else {
b.emptyAt = Date.now()
}
return rows.length ? rows : (b.rows ?? [])
} catch (e) {
if (b.rows) return b.rows
throw e
} finally {
b.inflight = null
}
})()
return b.inflight
},
}
}