mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 20:34:28 +00:00
refactor(core): replace generated provider files with pi-ai dynamic methods
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { getBuiltinProvidersMap, PRIORITY_PROVIDERS, ENV_MAP } from './builtin'
|
||||
|
||||
describe('Builtin Providers', () => {
|
||||
it('should return a non-empty record of providers', () => {
|
||||
const providers = getBuiltinProvidersMap()
|
||||
expect(Object.keys(providers).length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('should place priority providers first in the map', () => {
|
||||
const providers = getBuiltinProvidersMap()
|
||||
const keys = Object.keys(providers)
|
||||
|
||||
expect(keys[0]).toBe(PRIORITY_PROVIDERS[0])
|
||||
expect(keys[1]).toBe(PRIORITY_PROVIDERS[1])
|
||||
})
|
||||
|
||||
it('should format openai provider correctly with env key', () => {
|
||||
const providers = getBuiltinProvidersMap()
|
||||
const openai = providers.openai
|
||||
|
||||
expect(openai).toBeDefined()
|
||||
expect(openai.name).toBe('openai')
|
||||
expect(openai.config.apiKey).toBe(ENV_MAP.openai)
|
||||
expect(openai.models.length).toBeGreaterThan(0)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,100 @@
|
||||
import { getBuiltinProviders, getBuiltinModels } from '@earendil-works/pi-ai/providers/all'
|
||||
import type { ProviderConfigSerializable, providerModelSchema } from '@shumai/dtos'
|
||||
import type { z } from 'zod'
|
||||
|
||||
type ProviderModel = z.infer<typeof providerModelSchema>
|
||||
|
||||
export const PRIORITY_PROVIDERS = [
|
||||
'openai',
|
||||
'anthropic',
|
||||
'google',
|
||||
'deepseek',
|
||||
'groq',
|
||||
'openrouter',
|
||||
'mistral',
|
||||
'amazon-bedrock',
|
||||
]
|
||||
|
||||
export const ENV_MAP: Record<string, string> = {
|
||||
openai: 'OPENAI_API_KEY',
|
||||
anthropic: 'ANTHROPIC_API_KEY',
|
||||
'github-copilot': 'COPILOT_GITHUB_TOKEN',
|
||||
'azure-openai-responses': 'AZURE_OPENAI_API_KEY',
|
||||
deepseek: 'DEEPSEEK_API_KEY',
|
||||
google: 'GEMINI_API_KEY',
|
||||
'google-vertex': 'GOOGLE_CLOUD_API_KEY',
|
||||
groq: 'GROQ_API_KEY',
|
||||
cerebras: 'CEREBRAS_API_KEY',
|
||||
xai: 'XAI_API_KEY',
|
||||
openrouter: 'OPENROUTER_API_KEY',
|
||||
'vercel-ai-gateway': 'AI_GATEWAY_API_KEY',
|
||||
zai: 'ZAI_API_KEY',
|
||||
mistral: 'MISTRAL_API_KEY',
|
||||
minimax: 'MINIMAX_API_KEY',
|
||||
'minimax-cn': 'MINIMAX_CN_API_KEY',
|
||||
moonshotai: 'MOONSHOT_API_KEY',
|
||||
'moonshotai-cn': 'MOONSHOT_API_KEY',
|
||||
huggingface: 'HF_TOKEN',
|
||||
fireworks: 'FIREWORKS_API_KEY',
|
||||
together: 'TOGETHER_API_KEY',
|
||||
opencode: 'OPENCODE_API_KEY',
|
||||
'opencode-go': 'OPENCODE_API_KEY',
|
||||
'kimi-coding': 'KIMI_API_KEY',
|
||||
'cloudflare-workers-ai': 'CLOUDFLARE_API_KEY',
|
||||
'cloudflare-ai-gateway': 'CLOUDFLARE_API_KEY',
|
||||
xiaomi: 'XIAOMI_API_KEY',
|
||||
'xiaomi-token-plan-cn': 'XIAOMI_TOKEN_PLAN_CN_API_KEY',
|
||||
'xiaomi-token-plan-ams': 'XIAOMI_TOKEN_PLAN_AMS_API_KEY',
|
||||
'xiaomi-token-plan-sgp': 'XIAOMI_TOKEN_PLAN_SGP_API_KEY',
|
||||
}
|
||||
|
||||
export interface BuiltinProviderData {
|
||||
name: string
|
||||
config: ProviderConfigSerializable
|
||||
models: ProviderModel[]
|
||||
}
|
||||
|
||||
export function getBuiltinProvidersMap(): Record<string, BuiltinProviderData> {
|
||||
const providerNames = getBuiltinProviders()
|
||||
|
||||
providerNames.sort((a, b) => {
|
||||
const indexA = PRIORITY_PROVIDERS.indexOf(a)
|
||||
const indexB = PRIORITY_PROVIDERS.indexOf(b)
|
||||
|
||||
if (indexA !== -1 && indexB !== -1) return indexA - indexB
|
||||
if (indexA !== -1) return -1
|
||||
if (indexB !== -1) return 1
|
||||
return a.localeCompare(b)
|
||||
})
|
||||
|
||||
const providers: Record<string, BuiltinProviderData> = {}
|
||||
|
||||
for (const providerName of providerNames) {
|
||||
const modelList = getBuiltinModels(providerName)
|
||||
if (!modelList || modelList.length === 0) continue
|
||||
|
||||
const firstModel = modelList[0]
|
||||
providers[providerName] = {
|
||||
name: providerName,
|
||||
config: {
|
||||
api: firstModel.api as ProviderConfigSerializable['api'],
|
||||
baseUrl: firstModel.baseUrl,
|
||||
apiKey: ENV_MAP[providerName] || '',
|
||||
},
|
||||
models: modelList.map((m) => ({
|
||||
modelId: m.id,
|
||||
name: m.name,
|
||||
config: {
|
||||
api: m.api as ProviderConfigSerializable['api'],
|
||||
reasoning: m.reasoning,
|
||||
input: m.input,
|
||||
contextWindow: m.contextWindow,
|
||||
maxTokens: m.maxTokens,
|
||||
cost: m.cost,
|
||||
},
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
return providers
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { providerService } from './provider'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { ProviderConfigSerializable, providerModelSchema } from '@shumai/dtos'
|
||||
import { builtinProviders } from '@shumai/core/src/generated/providers.generated'
|
||||
import { getBuiltinProvidersMap } from '@shumai/core/src/provider/builtin'
|
||||
import { z } from 'zod'
|
||||
|
||||
type ProviderModel = z.infer<typeof providerModelSchema>
|
||||
@@ -35,6 +35,7 @@ describe('ProviderService', () => {
|
||||
|
||||
// The first provider in generated file is 'openai'
|
||||
// Since we use id: 'desc', 'openai' should be first in the list
|
||||
const builtinProviders = getBuiltinProvidersMap()
|
||||
const firstInGenerated = Object.keys(builtinProviders)[0]
|
||||
expect(providers[0].name).toBe(firstInGenerated)
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import { ProviderConfigSerializable, providerModelSchema } from '@shumai/dtos'
|
||||
import { builtinProviders } from '@shumai/core/src/generated/providers.generated'
|
||||
import { getBuiltinProvidersMap } from '@shumai/core/src/provider/builtin'
|
||||
import { z } from 'zod'
|
||||
|
||||
type ProviderModel = z.infer<typeof providerModelSchema>
|
||||
@@ -19,23 +19,20 @@ export class ProviderService {
|
||||
})
|
||||
|
||||
if (team && team._count.providers === 0) {
|
||||
const builtinProviders = getBuiltinProvidersMap()
|
||||
// We insert them one by one in reverse order to ensure that the
|
||||
// popular providers (first in builtinProviders) get the highest ULIDs
|
||||
// and appear first when sorted by id DESC.
|
||||
const entries = Object.entries(builtinProviders).reverse()
|
||||
for (const [name, data] of entries) {
|
||||
const typedData = data as unknown as {
|
||||
config: ProviderConfigSerializable
|
||||
models: ProviderModel[]
|
||||
}
|
||||
await this.prismaClient.provider.create({
|
||||
data: {
|
||||
teamId: team.id,
|
||||
name,
|
||||
config: typedData.config,
|
||||
config: data.config,
|
||||
isBuiltin: true,
|
||||
models: {
|
||||
create: typedData.models.map((m) => ({
|
||||
create: data.models.map((m) => ({
|
||||
modelId: m.modelId,
|
||||
name: m.name,
|
||||
config: m.config,
|
||||
|
||||
Reference in New Issue
Block a user