mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 05:28:20 +00:00
initial commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { sortProviders, PRIORITY_PROVIDERS } from './generate-builtin-providers'
|
||||
|
||||
describe('generate-builtin-providers sorting', () => {
|
||||
it('should sort priority providers in correct order', () => {
|
||||
const entries: [string, unknown][] = [
|
||||
['anthropic', {}],
|
||||
['openai', {}],
|
||||
['google', {}],
|
||||
]
|
||||
const sorted = sortProviders([...entries])
|
||||
expect(sorted[0][0]).toBe('openai')
|
||||
expect(sorted[1][0]).toBe('anthropic')
|
||||
expect(sorted[2][0]).toBe('google')
|
||||
})
|
||||
|
||||
it('should place priority providers before non-priority ones', () => {
|
||||
const entries: [string, unknown][] = [
|
||||
['unknown-provider', {}],
|
||||
['openai', {}],
|
||||
['anthropic', {}],
|
||||
]
|
||||
const sorted = sortProviders([...entries])
|
||||
expect(sorted[0][0]).toBe('openai')
|
||||
expect(sorted[1][0]).toBe('anthropic')
|
||||
expect(sorted[2][0]).toBe('unknown-provider')
|
||||
})
|
||||
|
||||
it('should sort non-priority providers alphabetically', () => {
|
||||
const entries: [string, unknown][] = [
|
||||
['zebra', {}],
|
||||
['alpha', {}],
|
||||
['betty', {}],
|
||||
]
|
||||
const sorted = sortProviders([...entries])
|
||||
expect(sorted[0][0]).toBe('alpha')
|
||||
expect(sorted[1][0]).toBe('betty')
|
||||
expect(sorted[2][0]).toBe('zebra')
|
||||
})
|
||||
|
||||
it('should handle complex mixed lists correctly', () => {
|
||||
const entries: [string, unknown][] = [
|
||||
['zai', {}],
|
||||
['mistral', {}],
|
||||
['openai', {}],
|
||||
['alpha', {}],
|
||||
['google', {}],
|
||||
]
|
||||
const sorted = sortProviders([...entries])
|
||||
// Expected order: openai (0), google (2), mistral (6), alpha (alpha < zai), zai
|
||||
expect(sorted[0][0]).toBe('openai')
|
||||
expect(sorted[1][0]).toBe('google')
|
||||
expect(sorted[2][0]).toBe('mistral')
|
||||
expect(sorted[3][0]).toBe('alpha')
|
||||
expect(sorted[4][0]).toBe('zai')
|
||||
})
|
||||
|
||||
it('should cover all priority providers in the correct order', () => {
|
||||
const entries: [string, unknown][] = PRIORITY_PROVIDERS.map((p) => [p, {}])
|
||||
// Shuffle the list
|
||||
const shuffled = [...entries].sort(() => Math.random() - 0.5)
|
||||
const sorted = sortProviders(shuffled)
|
||||
|
||||
for (let i = 0; i < PRIORITY_PROVIDERS.length; i++) {
|
||||
expect(sorted[i][0]).toBe(PRIORITY_PROVIDERS[i])
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,142 @@
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
|
||||
export const PRIORITY_PROVIDERS = [
|
||||
'openai',
|
||||
'anthropic',
|
||||
'google',
|
||||
'deepseek',
|
||||
'groq',
|
||||
'openrouter',
|
||||
'mistral',
|
||||
'amazon-bedrock',
|
||||
]
|
||||
|
||||
const envMap: 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 function sortProviders(entries: [string, unknown][]): [string, unknown][] {
|
||||
return entries.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)
|
||||
})
|
||||
}
|
||||
|
||||
async function sync() {
|
||||
console.log('Fetching pi models from GitHub...')
|
||||
const res = await fetch(
|
||||
'https://raw.githubusercontent.com/earendil-works/pi/refs/heads/main/packages/ai/src/models.generated.ts',
|
||||
)
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch pi models: ${res.statusText}`)
|
||||
}
|
||||
let text = await res.text()
|
||||
|
||||
console.log('Parsing models...')
|
||||
// Remove imports
|
||||
text = text.replace(/import\s+.*?from\s+['"].*?['"];?/g, '')
|
||||
|
||||
// Remove 'satisfies Model<...>'
|
||||
text = text.replace(/satisfies\s+Model<[^>]+>/g, '')
|
||||
|
||||
// Write to a temporary file to import it
|
||||
const tmpPath = path.join(process.cwd(), 'scripts', 'tmp-models.ts')
|
||||
fs.writeFileSync(tmpPath, text)
|
||||
|
||||
try {
|
||||
const { MODELS } = await import(tmpPath)
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const providers: Record<string, any> = {}
|
||||
|
||||
const sortedEntries = sortProviders(Object.entries(MODELS))
|
||||
|
||||
for (const [providerName, models] of sortedEntries) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const modelList = Object.values(models as Record<string, any>)
|
||||
if (modelList.length === 0) continue
|
||||
|
||||
const firstModel = modelList[0]
|
||||
providers[providerName] = {
|
||||
name: providerName,
|
||||
config: {
|
||||
api: firstModel.api,
|
||||
baseUrl: firstModel.baseUrl,
|
||||
apiKey: envMap[providerName] || '',
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
models: modelList.map((m: any) => ({
|
||||
modelId: m.id,
|
||||
name: m.name,
|
||||
config: {
|
||||
api: m.api,
|
||||
reasoning: m.reasoning,
|
||||
input: m.input,
|
||||
contextWindow: m.contextWindow,
|
||||
maxTokens: m.maxTokens,
|
||||
cost: m.cost,
|
||||
},
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
const generatedDir = path.join(process.cwd(), 'src', 'generated')
|
||||
if (!fs.existsSync(generatedDir)) {
|
||||
fs.mkdirSync(generatedDir, { recursive: true })
|
||||
}
|
||||
|
||||
const outPath = path.join(generatedDir, 'providers.generated.ts')
|
||||
const outContent = `// This file is auto-generated by scripts/generate-builtin-providers.ts
|
||||
// Do not edit manually - run 'bun run generate-providers' to update
|
||||
import type { ProviderConfig } from '@mariozechner/pi-coding-agent'
|
||||
|
||||
export const builtinProviders: Record<string, ProviderConfig> = ${JSON.stringify(providers, null, 2)} as unknown as Record<string, ProviderConfig>
|
||||
`
|
||||
fs.writeFileSync(outPath, outContent)
|
||||
console.log(`Successfully generated builtin providers: ${outPath}`)
|
||||
} finally {
|
||||
if (fs.existsSync(tmpPath)) {
|
||||
fs.unlinkSync(tmpPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sync().catch((err) => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in New Issue
Block a user