Files
plugins/packages/joycode
yetone 17e5ef7457 all: load on OpenCode 2.x through a default {id, setup} (#657)
nbczw8750 (#657): trae 0.2.26 on OpenCode 2.2.5 fails with "Plugin
must export a default definition with an id and an effect or setup
function. (cause: SchemaError(Missing key at ["default"]))". 2.x
imports a package's exports["."] (a plugin dir and an npm install
alike) and has no V1 path at all, so every package here that serves
models failed the same way, not only trae.

Every one of the 32 model packages now has:
- opencode2.mjs, exports["."]: a copy of scripts/opencode2.mjs, an
  adapter that runs the package's V1 plugins inside 2.x's promise
  API. Config providers become 2.x providers (package pinned so the
  aisdk sdk hook runs), V1 auth methods become integration methods
  (oauth auto/code with forms; api without authorize as a key method),
  V1 credentials are kept in metadata.v1, the loader's baseURL,
  apiKey, headers and fetch are applied through the sdk hook, an
  auth.set renewal is stored per credential, and credential.switched
  /updated reload the account's own model list.
- opencode.mjs, main: re-exports the V1 plugin functions only.
  OpenCode 1.x and magpie's host.js take main. On 1.x, trae 0.2.26's
  main exported a non-plugin (_internal) and 1.19.5 refused it ("Plugin
  export is not a function"); the new main fixes that too.
- both files in "files"; patch version bumped. devin already had 2.x
  and is unchanged.

scripts/check.mjs fails a model package whose 2.x entry has no default
{id, setup}, whose opencode2.mjs differs from the canonical one, whose
entry is missing from files, or whose main exports a non-plugin.

Run at this head (mote):
- HOME=$(mktemp -d) bun test, as CI runs it: 4900 pass, 0 fail
  (4914 tests, 374 files). A first run set globalThis.fetch for the
  whole process from opencode2.test and failed 528 tests in other
  packages; the block is now set and put back around that file's tests.
- bun scripts/check.mjs: every package passes. With the network
  blocked cloudflare-workers-ai fails on its fetched catalog, the same
  on origin/main.
- Real OpenCode 2.2.5 (npm opencode-ai), sandbox HOME, packed trae
  0.2.27: loads with no error; /api/integration lists trae-cn "Trae CN"
  and trae-global "Trae Global" with their browser sign-ins; `auth
  login` starts the trae.cn authorization and waits (cancelled there);
  with a fake credential POSTed to /api/credential, `opencode models`
  lists trae-cn's six models.
- Real 2.2.5 with a fake V1 key plugin through the same adapter
  against a local fake server: `auth login` with a key, `models`,
  `opencode run` reached /real/v1/chat/completions with the loader's
  "Bearer loader-sk-fake" and header, and printed the fake's reply;
  with @ai-sdk/anthropic it reached /real/v1/messages with x-api-key
  from the loader. This run found the key method missing after 2.x's
  second integration transform (a once-only flag outside the
  transform); fixed, and opencode2.test checks a rebuild.
- Real OpenCode 1.19.5, sandbox HOME, trae 0.2.27: `opencode models`
  lists trae-cn and trae-global models; 0.2.26 failed there.

Broken by hand (scratch copy, bun test scripts/ and check.mjs):
- trae's exports back to "./index.mjs": the trae 2.x test fails
  (default.id undefined), check fails trae with "OpenCode 2.x:
  ./index.mjs has no default {id, setup} (plugins#657)".
- zed's opencode2.mjs changed: the copies test fails, check fails zed
  "differs from scripts/opencode2.mjs".
- the key flag back outside the transform: the 1.x plugin test fails
  on the rebuilt sign-ins (key missing).

Not tried: a real sign-in with a real account on 2.x; Windows (the
reporter's OS); chat.headers, chat.params, usage and tool hooks, which
the adapter doesn't carry to 2.x; the tools/pages packages (calc,
goanyapi, layerfs, search1api, aelios, agent-console, chat,
fingerprint, nowledge-mem, kimi-desktop), which still load only on 1.x.
2026-11-03 13:54:26 +08:00
..

JoyCode

把京东云 JoyCode(JoyCoder IDE 自带的模型额度)作为一个多协议供应商接进 magpie。装好后,所有 agent(Claude Code、Codex 等)都能通过 magpie 用 joycode/<model> 访问 JoyCode 的模型,并享受 magpie 的多账号号池、故障切换和用量统计。

逆向了 JoyCode IDE 的「彩网关」协议(https://api-ai.jd.com/api,HMAC-SHA256 签名 query + ptKey 账号票据头)。请求与 IDE 本身走同一账号、同一额度。

安装

magpie plugin add @magpie-community/opencode-joycode-auth
magpie plugin login joycode

两种登录方式(任选):

  1. 浏览器登录(京东账号)(推荐)— magpie 打开 JoyCode 官方登录页,用京东账号/扫码登录,完成后自动拿到凭证。和 JoyCode IDE 自己的登录走的是同一套流程。
  2. 手动粘贴 ptKey — 从已登录的 JoyCode IDE 登录态里提取(%APPDATA%\JoyCode\User\globalStorage\state.vscdb 的 JoyCoder.IDE → joyCoderUser.ptKey)。这是账号票据,等同密码,勿外传。这种方式还会追问可选的 tenant(企业租户名,个人账号留空)。

凭证由 magpie 存在自己的 plugin-auth.json(权限 600),本插件目录里不落任何凭证。

多账号 = 多次登录:magpie plugin login joycode 再跑一遍(或在应用里再点一次),每个账号是一个号池座位,magpie 会自动轮换。

模型

模型由登录账号的 joycode_modelList 动态同步,不需要在插件里逐个维护。插件读取每个模型的 extJson.adapterType,为它选择对应的协议、AI SDK 和 JoyCode 网关:

extJson.adapterType 上游协议 AI SDK JoyCode functionId
缺失 / 其他 OpenAI Chat Completions @ai-sdk/openai-compatible chat_completions
openai-response OpenAI Responses @ai-sdk/openai responses_completions
anthropic Anthropic Messages @ai-sdk/anthropic anthropic_completions

因此 GPT 模型会走 Responses,Claude 模型会走 Anthropic Messages,GLM、Kimi、DeepSeek、MiniMax、Doubao 等继续走 Chat Completions。2026-10-28 使用真实账号核对时,joycode_modelList 返回 15 个模型:10 个 chat、4 个 openai-response、1 个 anthropic,三类均已覆盖。

同时通过 plugin_config 的 billingRate 场景同步积分消耗倍率。点击「重新获取模型」可更新模型和倍率;按 chatApiModel 匹配,不会把仅出现在计费表中的模型加入列表。模型自身的倍率优先;公共倍率配置遵守 excludeTenants,无租户或被排除的租户不套用公共倍率。明确的 0× 会标记免费;查不到的倍率不当作 0×,也不虚构优惠前倍率。

当前实测:JoyAI-Code-1.6 0×、GLM-5.3 0.6×、Kimi-K2.6 0.5×、MiniMax-M3 0.4×、Doubao-Seed-2.0-pro 0.4×。这些是查询结果,代码没有写死价格。它们是积分倍率,不是美元/token 价格。

视觉(image_url)为实验性:按接口 features 声明能力,实际图片请求尚未验收。

消息转发策略

根据 2026-10-30 的 JoyCode 实测,仅对 Responses 协议做以下兼容:GPT-6 Astra、GPT-6.1 Sol 的显式 none / minimal 转为 low;GPT-6 Sol、GPT-5.6 Sol 仅将 minimal 转为 low,保留上游接受的 none。其他 reasoning 参数、有效或未指定的强度、未知模型和其他协议均不变。启用低档推理可能增加用量。此表基于当前账号目录,不自动覆盖未来新增模型。

默认保留完整多轮对话、工具调用和模型原生请求格式。插件从当前账号的 joycode_userInfo 获取真实 userId、tenant、orgFullName,按 JoyCode 请求格式补入正文;三种协议统一设置 language: UNKNOWN。身份信息按账号 loader 隔离缓存,不接受客户端指定的用户身份。缺少这些元数据时,已观察到网关返回 AI_GRAY_ACCESS_DENIED。

网关可能以 HTTP 200 和 SSE 响应头返回裸 JSON 错误;插件会把这种错误转换为明确的失败响应,避免客户端一直等待。普通 403 权限错误不会再被误标为登录失效。

JoyCode 的 Anthropic 与 Responses 网关还会给 SSE 多加一层 framing(Anthropic 为 data: event: … 和 data: data: …,Responses 为 data: data: …)。插件在流式转发时增量还原标准 SSE,不缓存完整回复;否则官方 Anthropic/OpenAI SDK 会分别表现为空流和 malformed server-sent event JSON。标准 SSE 会按原字节透传,跨上游 chunk 的边界也有回归测试。

Codex 在 store: false 下仍可能重放带 rs_… id 的 reasoning item。JoyCode 的 Responses 后端会把这些 id 当成已持久化条目查找,找不到便以 Item with id 'rs_…' not found 拒绝整个请求,压缩尤其容易触发。插件在转发前移除这类 reasoning item 的 id,保留其 summary 和 encrypted_content;store: true 时保留 id,其他 item 的 id 也不改。

0.1.1 已实测 JoyAI-Code-1.6 完整流式文字回复、工具调用及工具结果往返,并通过 magpie provider test。0.2.0 于 2026-10-28 通过真实账号验证:provider test 的 GPT-6 Astra Responses 请求返回 200(3.0 秒),Claude-Opus-5-hq Anthropic Messages 请求返回 200(7.3 秒);再经 magpie 网关用官方 OpenAI SDK 的 Responses 流和官方 Anthropic SDK 的 Messages 流各发一条短请求,均正确收到文本 OK 和完整事件序列(2.9 秒、7.6 秒)。更早的 Claude 冷启动曾出现 34.7 秒,分段计时显示时间耗在上游响应头而不是插件缓冲。Anthropic 的 /v1/messages/count_tokens 在插件本地返回 404,不访问 JoyCode,避免把 token 计数误发成一次付费生成。

如果实测 JoyCode 后端拒绝多轮/工具调用格式的消息,可以开启回退模式:

magpie plugin options joycode '{"flatten": true}'

flatten 模式会把 Chat Completions 消息压平成 system + 单条 user prompt(工具调用和工具结果会被丢弃),Responses 和 Anthropic Messages 不受影响。开了 flatten 就别用 chat 模型跑需要工具的 agent 会话,只适合纯对话。默认关闭。

已知限制

  • 额度展示:通过 IDE 同款 get_newpoint_ide 接口读取套餐、已用/总积分、剩余积分和有效期文字,可在账号及用量页面刷新。有效期文字不等同于额度重置时间,因此不推算重置倒计时。多个积分池分别显示,不相加;额度窗口仅作展示,由上游判定具体模型能否继续使用。
  • ptKey 失效:表现为账号被标记「需要重新登录」,重新 magpie plugin login joycode 即可。
  • 签名 key 轮换:签名 key 是内嵌在每份 JoyCode IDE 里的公开协议常量,不是密钥。若 JoyCode 轮换它,从新版 extension.js 重新提取后,可通过环境变量 JOYCODE_SIGN_KEY 覆盖,无需改代码。
  • 登录方式默认 loginType: PIN_JD_CLOUD;如果你的账号是京东云企业域登录且请求被拒,改 index.mjs 顶部的 LOGIN_TYPE 常量。

开发

bun test                # 浏览器登录、额度映射、模型倍率回归测试

相关文档:magpie 插件开发。版本变更见 CHANGELOG.md。

这是非官方适配插件,与京东或 magpie 官方无隶属关系。JoyCode 图标及商标归其权利人所有;代码的 MIT 许可不授予这些品牌资产的权利。