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
@@ -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'),
|
||||
)
|
||||
})
|
||||
Reference in new issue
Block a user