Files
plugins/packages/joycode/models.test.mjs
T
liangmouse f653369a77 joycode 0.2.0: route models by adapterType to Chat, Responses and Messages (#464)
joycode 0.2.0 (liangmouse). Each JoyCode model is routed by the `extJson.adapterType` in its catalogue entry:
- missing/other → Chat Completions (`@ai-sdk/openai-compatible`, `chat_completions`)
- `openai-response` → Responses (`@ai-sdk/openai`, `responses_completions`)
- `anthropic` → Anthropic Messages (`@ai-sdk/anthropic`, `anthropic_completions`)

The routing matches JoyCoder.joycoder-fe 3.8.71's `buildApiHandler`. Other changes:
- Anthropic `/messages/count_tokens` gets a local 404 and nothing goes upstream, so magpie's `unsupportedCount` uses its local estimate.
- All three protocols send `language: UNKNOWN`, as JoyCode's own handlers do.
- Non-streaming JSON for Anthropic and Responses counts as success.
- `flatten` applies to chat only.
- JoyCode's extra SSE layer (`data: event:` / `data: data:`) is removed line by line as the stream arrives. Standard SSE passes through unchanged.

Reviewed and run at f1ccb2ab59b0570295264317f270b266c7a585da (on mote, macOS, Bun 1.4.2, HOME a temp dir):
- `bun test packages/joycode`: 42 pass. `node --test packages/joycode/*.test.mjs`: 42 pass. `bun scripts/check.mjs joycode`: ok.
- Broken by hand, one at a time, each went red:
  - count_tokens guard off → the count_tokens test;
  - Anthropic normalizer off → the Anthropic double-wrapped test;
  - Responses normalizer off → the Responses double-wrapped test;
  - `language` dropped → 2 tests.
- Official SDKs (`@anthropic-ai/sdk` 1.3.0, `openai` 7.45.1) read JoyCode-framed streams through the plugin's fetch, served locally and cut into 7-byte chunks:
  - `messages.stream` got the full event sequence, text "OK", `end_turn`;
  - `responses.create({stream})` got 9 events ending in `response.completed`, text "OK".
  - The same bytes without the plugin: "request ended without sending any chunks" from Anthropic and "malformed server-sent event JSON" from OpenAI.
- Upstream functionIds seen were `anthropic_completions` and `responses_completions`.

Real account: the author's run is in the PR thread and the CHANGELOG (`provider test` GPT-6 Astra 200 in 3.0s, Claude-Opus-5-hq 200 in 7.3s). Not tried here: no JoyCode account.
2026-10-29 01:12:42 +08:00

71 lines
3.5 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import plugin from './index.mjs'
const catalogue = [
{ chatApiModel: 'JoyAI-Code-1.6', label: 'JoyAI-Code-1.6', maxTotalTokens: 200000, respMaxTokens: 64000, features: ['function_call'] },
{ chatApiModel: 'GLM-5.3-agent', label: 'GLM-5.3', maxTotalTokens: 200000, respMaxTokens: 64000, features: ['function_call'] },
{ chatApiModel: 'GPT-5.6 Sol', label: 'GPT-5.6 Sol', extJson: { adapterType: 'openai-response' } },
{ chatApiModel: 'Claude-Opus-5-hq', label: 'Claude-Opus-5', extJson: { adapterType: 'anthropic' } },
]
const billingRate = { excludeTenants: ['JD'], billingRates: [
{ chatApiModel: 'JoyAI-Code-1.6', billingRate: '0x' },
{ chatApiModel: 'GLM-5.3-agent', billingRate: '0.6x' },
{ chatApiModel: 'unavailable-model', billingRate: '0.2x' },
] }
async function load(t, { tenant = 'personal', failRates = false, failList = false, list = catalogue } = {}) {
t.mock.method(globalThis, 'fetch', async (url, init) => {
const fn = new URL(url).searchParams.get('functionId')
assert.equal(init.headers.tenant, tenant)
if (fn === 'joycode_modelList') {
if (failList) throw new Error('offline')
assert.equal(init.body, '{}')
return Response.json({ code: 0, data: list })
}
assert.equal(fn, 'plugin_config')
assert.deepEqual(JSON.parse(init.body), { sceneType: 'billingRate' })
if (failRates) throw new Error('offline')
return Response.json({ code: 0, data: { billingRate } })
})
const hooks = await plugin.server({})
const cfg = {}
await hooks.config(cfg)
const provider = cfg.provider.joycode
assert.equal(typeof hooks.provider?.models, 'function')
return { provider, models: await hooks.provider.models(provider, { auth: { type: 'api', key: 'fake', metadata: { tenant } } }) }
}
test('sync current model multipliers including zero without adding pricing-only models', async t => {
const { models } = await load(t)
assert.deepEqual(Object.keys(models), ['JoyAI-Code-1.6', 'GLM-5.3-agent', 'GPT-5.6 Sol', 'Claude-Opus-5-hq'])
assert.equal(models['JoyAI-Code-1.6'].rate, 0)
assert.equal(models['JoyAI-Code-1.6'].free, true)
assert.equal(models['GLM-5.3-agent'].rate, 0.6)
assert.equal(models['GLM-5.3-agent'].capabilities.input.image, false)
})
test('model adapter chooses the SDK that speaks its upstream protocol', async t => {
const { models } = await load(t)
assert.equal(models['JoyAI-Code-1.6'].api.npm, '@ai-sdk/openai-compatible')
assert.equal(models['GPT-5.6 Sol'].api.npm, '@ai-sdk/openai')
assert.equal(models['Claude-Opus-5-hq'].api.npm, '@ai-sdk/anthropic')
for (const model of Object.values(models)) assert.equal(model.api.url, 'https://api-ai.jd.com/v1')
})
test('excluded tenants do not inherit public pricing', async t => {
const { models } = await load(t, { tenant: 'JD' })
assert.equal(models['GLM-5.3-agent'].rate, undefined)
assert.notEqual(models['JoyAI-Code-1.6'].free, true)
})
test('model-specific billing rate takes priority', async t => {
const { models } = await load(t, { list: [{ ...catalogue[1], billingRate: '0.3x' }] })
assert.equal(models['GLM-5.3-agent'].rate, 0.3)
})
test('pricing fetch failure retains models without invented rates', async t => {
const { models } = await load(t, { failRates: true })
assert.equal(Object.keys(models).length, 4)
assert.equal(models['GLM-5.3-agent'].rate, undefined)
})
test('catalogue fetch failure returns original fallback model list', async t => {
const { models, provider } = await load(t, { failList: true })
assert.equal(models, provider.models)
})