Files
plugins/packages/mclaw/fake.mjs
T
Joey Shao 367e879cab mclaw 0.5.2: China Mobile Cloud's MClaw agent (ai.yun.139.com), with a browser sign-in (#746)
New package @magpie-community/opencode-mclaw-auth 0.5.2 (joey-41): China
Mobile Cloud's MClaw agent (ai.yun.139.com) as a provider. A browser
sign-in in a dedicated Chrome profile per sign-in harvests the yun.139.com
ticket and fetches the llm key and the model multipliers by itself; the
models come from the account's own lists; the usage card reads the
aitokens pool and, through the vip SSO chain (toSourceId 001005), the
records' own endTime as the reset; the ticket is renewed headless from the
account's own profile, and a ticket for another phone number is refused.
Every host is China Mobile's own. registry.json and the README table list
it.

Reviewed at 905e2a6b, 7ed7d0aa and 7304ac15 (0.5.2, edcd9549 plus a
merge of main). The last review's two must-fixes are in the code:
magpie's cancel (callback(undefined, { signal }) and authorize's signal
both call harvesting.abort(); startBrowserHarvest records an abort while
Chrome is still starting and closes it once open), and a failed,
cancelled or timed-out sign-in deletes its signin-* profile dir after
releasing the lock. Re-signing an account leaving its old dir behind is
in the package README as a known limit.

Run at 7304ac15 (origin/main 10a61c2e already in it), sandbox HOME:
- mote, Bun 1.4.2: bun test in packages/mclaw 30 pass, 0 fail;
  bun scripts/check.mjs mclaw and bun scripts/check.mjs (all) ok.
- WSL Ubuntu 24.04, Bun 1.4.2: whole-repo bun test (see the PR).
- The real startBrowserHarvest with Chrome for Testing on mote, aborted
  before Chrome was up and 4s after: both reject "登录已取消", no Chrome
  left on the profile, no signin-* dir left.
- Review test: authorize and callback given separate signals, as
  magpie's host does, only the callback's aborted: closed and released.
  With callback's listener taken out it goes red (5s timeout).
Not tried: a real MClaw sign-in (no account here). The package's own
magpie-style test hands both calls one signal, so it stays green
without callback's listener; that is the only gap seen.
npm: the name is new, so trusted publishing can't create it; magpie
serves it from the dist branch until an owner publishes its first
version.
2026-11-06 10:35:49 +08:00

86 lines
4.3 KiB
JavaScript
Raw 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.
// 上游的本地 fake:一个 Bun.serve 站着 ai.yun.139.com(openclaw)、
// ypqy.mcloud.139.com(benefit)和 vip.yun.139.com(vip)——fakeUpstream()
// 把 _internal.HOSTS 三个 origin 都指到本地,按路径路由(三台上游的路径
// 互不重叠)。route("POST /api/llm/apikey/get", handler) 追加/覆盖处理器;
// seen 记录每个请求(方法、路径、头、body),供断言。close() 还原 HOSTS。
import { _internal } from './index.mjs'
export function fakeUpstream() {
const routes = new Map()
const seen = []
const server = Bun.serve({
port: 0,
hostname: '127.0.0.1',
async fetch(req) {
const url = new URL(req.url)
const text = await req.text()
const r = { method: req.method, path: url.pathname, headers: req.headers, text }
if (text) try { r.json = JSON.parse(text) } catch {}
seen.push(r)
const handler = routes.get(`${req.method} ${url.pathname}`)
if (!handler) return new Response('no route ' + req.method + ' ' + url.pathname, { status: 404 })
return handler(r)
},
})
const origin = `http://127.0.0.1:${server.port}`
const was = { ..._internal.HOSTS }
_internal.HOSTS.openclaw = origin
_internal.HOSTS.benefit = origin
_internal.HOSTS.vip = origin
_internal.HOSTS.userNjs = origin
const f = {
origin,
seen,
route: (key, handler) => routes.set(key, handler),
close() {
server.stop(true)
Object.assign(_internal.HOSTS, was)
},
}
// 上游三条数据面的默认应答:agent/list(实例 + 可用模型)、apikey/get v1
// (key + 倍率)、aitokens 权益池。用例里 route() 同名覆盖。
f.route('POST /api/adapter/assistant/agent/list', () => json({
success: true,
data: { agentList: [{ agentType: 'mclaw', agentId: 'oclaw-1', modelList: ['blian_qwen38_flash', 'huoshan_glm_5_3'].map((code) => ({ modelCode: code, description: code })) }] },
}))
f.route('POST /api/llm/apikey/get', () => json({
success: true,
data: { apikey: 'na-test-key', modelUrl: 'x', modelList: [
{ modelCode: 'blian_qwen38_flash', modelName: 'Qwen3.8-Flash', tokenRatio: 0.18 },
{ modelCode: 'huoshan_glm_5_3', modelName: 'GLM-5.3', tokenRatio: 3.5 },
] },
}))
f.route('POST /isbo2/openApi/queryAvailableBenefitV2', () => json({
result: { resultCode: '0', resultDesc: 'SUCCESS' },
userAvailableBenefitList: [{ benefitNo: 'RHR135', benefitName: 'aitokens', benefitValue: '30500000', benefitUsed: '11375000' }],
}))
// SSO 换票链默认应答:出票 + ssoLogin 下发 6 个 mc_* cookie(.139.com,24h,
// mc_at/mc_pat 是 httpOnly——和真实抓包一致;插件从 getSetCookie() 整段收)。
f.route('POST /user/querySpecTokenV2', () => json({
success: true, code: '0000', message: '请求成功', data: { token: 'YZsid-fake-ticket', resultCode: '104000' },
}))
f.route('POST /m4c/login/ssoLogin', () => new Response(JSON.stringify({ resultCode: '0', resultDesc: 'SUCCESS' }), {
headers: {
'content-type': 'application/json',
'set-cookie': [
'mc_at=fake-at; Domain=.139.com; Path=/; HttpOnly; Expires=Wed, 06 Nov 2026 00:00:00 GMT',
'mc_bt=fake-bt; Domain=.139.com; Path=/; Expires=Wed, 06 Nov 2026 00:00:00 GMT',
'mc_pat=fake-pat; Domain=.139.com; Path=/; HttpOnly; Expires=Wed, 06 Nov 2026 00:00:00 GMT',
'mc_pbt=fake-pbt; Domain=.139.com; Path=/; Expires=Wed, 06 Nov 2026 00:00:00 GMT',
'mc_ac=fake-ac; Domain=.139.com; Path=/; Expires=Wed, 06 Nov 2026 00:00:00 GMT',
'mc_pac=fake-pac; Domain=.139.com; Path=/; Expires=Wed, 06 Nov 2026 00:00:00 GMT',
],
},
}))
return f
}
export const json = (value, status = 200) => new Response(JSON.stringify(value), { status, headers: { 'content-type': 'application/json' } })
// 两张 pc: Basic 票(浏览器登录收割的形态):掩码手机号不同,票文本内嵌
// 13 位到期 ms(basicExpiryMs 读的就是它)。值即 authorization Cookie/头。
export const TICKET_A = 'Basic ' + Buffer.from(`pc:13112349239:x|1|RCS|${Date.now() + 30 * 86400e3}|sig`, 'utf8').toString('base64')
export const TICKET_B = 'Basic ' + Buffer.from(`pc:18687654321:y|1|RCS|${Date.now() + 30 * 86400e3}|sig`, 'utf8').toString('base64')
export const PHONE_A = '131****9239'
export const PHONE_B = '186****4321'