diff --git a/packages/joycode/CHANGELOG.md b/packages/joycode/CHANGELOG.md index 0560968..7b98229 100644 --- a/packages/joycode/CHANGELOG.md +++ b/packages/joycode/CHANGELOG.md @@ -1,5 +1,9 @@ # 更新记录 +## 0.2.1 + +- 修复 Codex 压缩时重放 `store: false` 的 `rs_…` reasoning id 被 JoyCode Responses 后端拒绝并连续失败的问题。转发前移除 reasoning item 的持久化 id,保留其摘要和加密内容;`store: true` 及其他 item 的 id 不变。 + ## 0.2.0 - 按 JoyCode 模型目录中的 `extJson.adapterType` 自动选择 Chat Completions、Responses 或 Anthropic Messages,使 `/models` 返回的 GPT、Claude 及其他模型都能使用正确的 SDK 和网关。 diff --git a/packages/joycode/README.md b/packages/joycode/README.md index 9b9e26f..cfbb8a6 100644 --- a/packages/joycode/README.md +++ b/packages/joycode/README.md @@ -49,6 +49,8 @@ magpie plugin login joycode 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 后端拒绝多轮/工具调用格式的消息,可以开启回退模式: diff --git a/packages/joycode/chat.test.mjs b/packages/joycode/chat.test.mjs index ba19680..3fc9d67 100644 --- a/packages/joycode/chat.test.mjs +++ b/packages/joycode/chat.test.mjs @@ -255,6 +255,48 @@ test('Responses double-wrapped SSE is normalized across chunk boundaries', async ].join('')) }) +test('Responses drops unstored reasoning ids but keeps inline reasoning and other ids', async t => { + let sent + const payload = { + model: 'GPT-6 Astra', + store: false, + stream: true, + input: [ + { type: 'message', role: 'user', content: 'OK' }, + { type: 'reasoning', id: 'rs_18e2e0b7486f2800000002e3', summary: [{ type: 'summary_text', text: 'notes' }] }, + { type: 'function_call', id: 'fc_1', call_id: 'call_1', name: 'ping', arguments: '{}' }, + ], + } + const r = await request(t, new Response('data: [DONE]\n\n', { + headers: { 'content-type': 'text/event-stream' }, + }), { + input: 'https://api-ai.jd.com/v1/responses', + payload, + upstream(_url, init) { sent = JSON.parse(init.body) }, + }) + assert.equal(r.status, 200) + assert.deepEqual(sent.input, [ + { type: 'message', role: 'user', content: 'OK' }, + { type: 'reasoning', summary: [{ type: 'summary_text', text: 'notes' }] }, + { type: 'function_call', id: 'fc_1', call_id: 'call_1', name: 'ping', arguments: '{}' }, + ]) + assert.equal(sent.store, false) +}) + +test('Responses keeps reasoning ids when the upstream stores the items', async t => { + let sent + const reasoning = { type: 'reasoning', id: 'rs_stored', summary: [] } + const r = await request(t, new Response('data: [DONE]\n\n', { + headers: { 'content-type': 'text/event-stream' }, + }), { + input: 'https://api-ai.jd.com/v1/responses', + payload: { model: 'GPT-6 Astra', store: true, stream: true, input: [reasoning] }, + upstream(_url, init) { sent = JSON.parse(init.body) }, + }) + assert.equal(r.status, 200) + assert.deepEqual(sent.input, [reasoning]) +}) + test('Anthropic count_tokens returns 404 without contacting upstream', async t => { let fetchCalls = 0 t.mock.method(globalThis, 'fetch', async () => { diff --git a/packages/joycode/index.mjs b/packages/joycode/index.mjs index da20d0a..a75812b 100644 --- a/packages/joycode/index.mjs +++ b/packages/joycode/index.mjs @@ -171,6 +171,26 @@ function requestProtocol(input, payload) { return 'chat' } +/** + * Reasoning ids belong to items the upstream stored. Codex replays reasoning + * history with `store: false`, so JoyCode cannot resolve those ids and rejects + * the whole Responses request ("Item with id 'rs_…' not found"). The item's + * summary and encrypted content remain valid inline; only the stored-item id + * has to go. + */ +function withoutUnstoredReasoningIds(payload) { + if (payload?.store === true || !Array.isArray(payload?.input)) return payload + let changed = false + const input = payload.input.map((item) => { + if (!item || typeof item !== 'object' || item.type !== 'reasoning' || + !Object.prototype.hasOwnProperty.call(item, 'id')) return item + changed = true + const { id, ...inline } = item + return inline + }) + return changed ? { ...payload, input } : payload +} + /** * OpenAI `messages` -> { prompt, system } — fallback for accounts/backends * that reject multi-turn or tool-call payloads. @@ -754,8 +774,9 @@ export const JoyCodeAuthPlugin = async (_input, options) => { : new TextDecoder().decode(source) const payload = JSON.parse(source) const protocol = requestProtocol(request ?? input, payload) + const forwarded = protocol === 'responses' ? withoutUnstoredReasoningIds(payload) : payload const body = JSON.stringify({ - ...(flatten && protocol === 'chat' ? flattenBody(payload) : payload), + ...(flatten && protocol === 'chat' ? flattenBody(payload) : forwarded), ...account, language: 'UNKNOWN', }) diff --git a/packages/joycode/package.json b/packages/joycode/package.json index 2794e57..91fb5b6 100644 --- a/packages/joycode/package.json +++ b/packages/joycode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-joycode-auth", - "version": "0.2.0", + "version": "0.2.1", "description": "OpenCode provider plugin: JoyCode (JD Cloud JoyCoder IDE credits)", "type": "module", "main": "./index.mjs",