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.
75 lines
3.1 KiB
JavaScript
75 lines
3.1 KiB
JavaScript
// normalizeChatResponse:网关以 HTTP 200 包裸 JSON 错误、正常 JSON、SSE 流、
|
|
// 空响应、超过旧 1MB 截断的大回复——全部本地构造,不碰网络。
|
|
import './nonet.mjs'
|
|
import { expect, test } from 'bun:test'
|
|
import { _internal } from './index.mjs'
|
|
|
|
const { normalizeChatResponse } = _internal
|
|
|
|
const sseResponse = (chunks) => new Response(
|
|
new ReadableStream({
|
|
start(controller) {
|
|
for (const chunk of chunks) controller.enqueue(new TextEncoder().encode(chunk))
|
|
controller.close()
|
|
},
|
|
}),
|
|
{ status: 200, headers: { 'content-type': 'text/event-stream' } },
|
|
)
|
|
|
|
test('HTTP 200 + 裸 JSON 错误 01000002 → 401,标记登录失效', async () => {
|
|
const out = await normalizeChatResponse(Response.json({ error: { code: '01000002', message: '无效的ApiKey' } }))
|
|
expect(out.status).toBe(401)
|
|
expect(out.headers.get('x-magpie-sign-in')).toBe('expired')
|
|
const body = await out.json()
|
|
expect(body.error.code).toBe('01000002')
|
|
expect(body.error.message).toBe('无效的ApiKey')
|
|
})
|
|
|
|
test('HTTP 200 + 裸 JSON 错误(其他 code)→ 502,登录保持', async () => {
|
|
const out = await normalizeChatResponse(Response.json({ error: { code: '01000005', message: '资源不存在' } }))
|
|
expect(out.status).toBe(502)
|
|
expect(out.headers.get('x-magpie-sign-in')).toBe('kept')
|
|
})
|
|
|
|
test('HTTP 200 + success:false → 502', async () => {
|
|
const out = await normalizeChatResponse(Response.json({ success: false, code: 'X', message: 'nope' }))
|
|
expect(out.status).toBe(502)
|
|
expect((await out.json()).error.message).toBe('nope')
|
|
})
|
|
|
|
test('正常 JSON 回复原样透出', async () => {
|
|
const payload = { choices: [{ message: { role: 'assistant', content: 'hi' } }], usage: { total_tokens: 1 } }
|
|
const out = await normalizeChatResponse(Response.json(payload))
|
|
expect(out.status).toBe(200)
|
|
expect(await out.json()).toEqual(payload)
|
|
})
|
|
|
|
test('超过 1MB 的合法 JSON 回复不再被截断成 502', async () => {
|
|
const payload = { choices: [{ message: { role: 'assistant', content: 'x'.repeat(2 * 1024 * 1024) } }] }
|
|
const out = await normalizeChatResponse(Response.json(payload))
|
|
expect(out.status).toBe(200)
|
|
const body = await out.json()
|
|
expect(body.choices[0].message.content.length).toBe(2 * 1024 * 1024)
|
|
})
|
|
|
|
test('SSE 流透传:检查过的首字节重放,其余照序', async () => {
|
|
const chunks = ['data: {"id":"1","cho', 'ices":[{"delta":{"content":"你"}}]}\n\n', 'data: [DONE]\n\n']
|
|
const out = await normalizeChatResponse(sseResponse(chunks))
|
|
expect(out.status).toBe(200)
|
|
expect(out.headers.get('content-type')).toBe('text/event-stream')
|
|
const text = await out.text()
|
|
expect(text).toBe(chunks.join(''))
|
|
})
|
|
|
|
test('空响应体 → 502 empty_response', async () => {
|
|
const out = await normalizeChatResponse(new Response('', { status: 200 }))
|
|
expect(out.status).toBe(502)
|
|
expect((await out.json()).error.code).toBe('empty_response')
|
|
})
|
|
|
|
test('JSON 但既非错误也非对话响应 → 502 invalid_response', async () => {
|
|
const out = await normalizeChatResponse(Response.json({ foo: 'bar' }))
|
|
expect(out.status).toBe(502)
|
|
expect((await out.json()).error.code).toBe('invalid_response')
|
|
})
|