feat: add initial model provider adapters
This commit is contained in:
1 parent
7f35960ba6
commit
547f7c0977
16 files changed
+577
-21
No files matched your search
@@ -15,6 +15,20 @@ test('mock catalog contains sync multimodal and async 3D models', async () => {
|
||||
assert.ok(models.some((model) => model.type === '3d' && model.mode === 'async'))
|
||||
})
|
||||
|
||||
test('initial provider catalog includes DeepSeek, Kimi, GLM and Seedance models', async () => {
|
||||
const catalog = createCatalog()
|
||||
const [providers, models] = await Promise.all([catalog.listProviders(), catalog.listModels()])
|
||||
|
||||
assert.ok(providers.some((provider) => provider.code === 'deepseek' && provider.protocol === 'openai-compatible'))
|
||||
assert.ok(providers.some((provider) => provider.code === 'kimi' && provider.protocol === 'openai-compatible'))
|
||||
assert.ok(providers.some((provider) => provider.code === 'zhipu' && provider.protocol === 'openai-compatible'))
|
||||
assert.ok(providers.some((provider) => provider.code === 'bytedance' && provider.protocol === 'http'))
|
||||
assert.ok(models.some((model) => model.providerId === 'provider-deepseek' && model.mode === 'sync'))
|
||||
assert.ok(models.some((model) => model.providerId === 'provider-kimi' && model.type === 'multimodal'))
|
||||
assert.ok(models.some((model) => model.providerId === 'provider-zhipu' && model.type === 'multimodal'))
|
||||
assert.ok(models.some((model) => model.providerId === 'provider-bytedance' && model.type === 'video' && model.mode === 'async'))
|
||||
})
|
||||
|
||||
test('platform key is returned once, hashed at rest, and can be revoked', async () => {
|
||||
const repository = new MemoryCatalogRepository()
|
||||
const catalog = new CatalogService(repository)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { OpenAICompatibleProvider, OpenAICompatibleProviderError } from '../src/providers/openai-compatible.js'
|
||||
|
||||
test('OpenAI-compatible adapter forwards chat requests without exposing provider credentials', async () => {
|
||||
const requests: Array<{ url: string; init: RequestInit | undefined }> = []
|
||||
const provider = new OpenAICompatibleProvider({
|
||||
providerName: 'DeepSeek',
|
||||
apiKey: 'test-deepseek-key',
|
||||
baseUrl: 'https://api.deepseek.com/',
|
||||
fetcher: async (url, init) => {
|
||||
requests.push({ url: String(url), init })
|
||||
return new Response(JSON.stringify({ id: 'chat-1', choices: [{ message: { role: 'assistant', content: 'done' } }] }), { status: 200 })
|
||||
},
|
||||
})
|
||||
|
||||
const response = await provider.chatCompletion({ model: 'deepseek-v4-flash', messages: [{ role: 'user', content: 'hello' }] })
|
||||
|
||||
assert.equal(response.choices?.[0]?.message?.content, 'done')
|
||||
assert.equal(requests[0]?.url, 'https://api.deepseek.com/chat/completions')
|
||||
assert.equal(new Headers(requests[0]?.init?.headers).get('Authorization'), 'Bearer test-deepseek-key')
|
||||
assert.deepEqual(JSON.parse(String(requests[0]?.init?.body)), {
|
||||
model: 'deepseek-v4-flash',
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
stream: false,
|
||||
})
|
||||
})
|
||||
|
||||
test('OpenAI-compatible adapter maps upstream failures without leaking credentials', async () => {
|
||||
const provider = new OpenAICompatibleProvider({
|
||||
providerName: 'Kimi',
|
||||
apiKey: 'test-kimi-secret',
|
||||
baseUrl: 'https://api.moonshot.cn/v1',
|
||||
fetcher: async () => new Response(JSON.stringify({ error: { message: 'invalid token' } }), { status: 401 }),
|
||||
})
|
||||
|
||||
await assert.rejects(
|
||||
provider.chatCompletion({ model: 'kimi-k2.6', messages: [] }),
|
||||
(error: unknown) => error instanceof OpenAICompatibleProviderError && error.statusCode === 401 && !error.message.includes('test-kimi-secret'),
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { SeedanceProvider, SeedanceProviderError, mapSeedanceTask } from '../src/providers/seedance.js'
|
||||
|
||||
test('Seedance adapter creates an official asynchronous video generation task', async () => {
|
||||
const requests: Array<{ url: string; init: RequestInit | undefined }> = []
|
||||
const provider = new SeedanceProvider({
|
||||
apiKey: 'test-volcengine-key',
|
||||
fetcher: async (url, init) => {
|
||||
requests.push({ url: String(url), init })
|
||||
return new Response(JSON.stringify({ id: 'cgt-20260828-demo', status: 'queued' }), { status: 200 })
|
||||
},
|
||||
})
|
||||
|
||||
const taskId = await provider.createTextToVideo({ model: 'doubao-seedance-1-0-pro-250528', prompt: 'a cinematic product video' })
|
||||
|
||||
assert.equal(taskId, 'cgt-20260828-demo')
|
||||
assert.equal(requests[0]?.url, 'https://ark.cn-beijing.volces.com/api/v3/contents/generations/tasks')
|
||||
assert.equal(new Headers(requests[0]?.init?.headers).get('Authorization'), 'Bearer test-volcengine-key')
|
||||
assert.deepEqual(JSON.parse(String(requests[0]?.init?.body)), {
|
||||
model: 'doubao-seedance-1-0-pro-250528',
|
||||
content: [{ type: 'text', text: 'a cinematic product video' }],
|
||||
})
|
||||
})
|
||||
|
||||
test('Seedance task response maps video output and timestamps to MOYI task view', () => {
|
||||
const mapped = mapSeedanceTask({
|
||||
id: 'cgt-20260828-demo',
|
||||
model: 'doubao-seedance-1-0-pro-250528',
|
||||
status: 'succeeded',
|
||||
created_at: 1787880000,
|
||||
updated_at: 1787880060,
|
||||
content: { video_url: 'https://example.test/result.mp4' },
|
||||
})
|
||||
|
||||
assert.equal(mapped.status, 'succeeded')
|
||||
assert.equal(mapped.progress, 100)
|
||||
assert.equal(mapped.output?.videoUrl, 'https://example.test/result.mp4')
|
||||
assert.match(mapped.createdAt || '', /^2026-/)
|
||||
})
|
||||
|
||||
test('Seedance adapter validates task identifiers before making requests', async () => {
|
||||
const provider = new SeedanceProvider({ apiKey: 'test-key', fetcher: async () => new Response('{}') })
|
||||
await assert.rejects(provider.getTask('../invalid'), (error: unknown) => error instanceof SeedanceProviderError)
|
||||
})
|
||||
Reference in new issue
Block a user