Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

minimax.ts

108 lines3.4 KB

1/**
2 * MiniMax — model ids from the public models overview (platform.minimax.io).
3 * Chat rows take their facts from the docs pages in `minimax-docs.ts`. The
4 * spec is the two chat documents the docs site publishes: OpenAI-compatible
5 * chat completions and Anthropic-compatible messages.
6 */
7import type { Activity } from '#/db/schema.ts'
8​
9import {
10 MINIMAX_CHAT_SPEC_URL,
11 MINIMAX_MESSAGES_SPEC_URL,
12 minimaxModelFacts,
13} from '../minimax-docs.ts'
14import { fetchOpenApi, fetchText } from '../types.ts'
15import type {
16 ListModelsResult,
17 ModelInfo,
18 ProviderConfig,
19 ProviderSecrets,
20 SpecFetchResult,
21} from '../types.ts'
22​
23export const MINIMAX_MODELS_URL =
24 'https://platform.minimax.io/docs/guides/models-intro.md'
25​
26const SPEC_URLS = [MINIMAX_CHAT_SPEC_URL, MINIMAX_MESSAGES_SPEC_URL]
27​
28const SECTION_ACTIVITY: Array<[RegExp, Activity | null]> = [
29 [/^Language|^语言模型/, 'chat'],
30 [/^Video|^视频/, 'video'],
31 [/^Speech|^Audio|^语音/, 'audio'],
32 [/^Image|^图片/, 'image'],
33 [/^Music|^音乐/, null],
35​
36const WIRE_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/
37​
38/** First-column link whose label is a wire id. Spaced display names stay out. */
39function modelColumnId(line: string): string | null {
40 const trimmed = line.trim()
41 if (!trimmed.startsWith('|') || /^\|\s*:?-+/.test(trimmed)) return null
42 const first = trimmed.split('|')[1]?.trim() ?? ''
43 const markdown = first.match(/^\[([^\]]+)\]\([^)]+\)$/)
44 const anchor = first.match(/^<a\b[^>]*>\s*([^<]+?)\s*<\/a>$/)
45 const label = (markdown?.[1] ?? anchor?.[1] ?? '').trim()
46 return WIRE_ID.test(label) ? label : null
48​
49export function parseMinimaxModels(markdown: string): Array<ModelInfo> {
50 const models: Array<ModelInfo> = []
51 const seen = new Set<string>()
52 for (const chunk of markdown.split(/^### /m).slice(1)) {
53 const newline = chunk.indexOf('\n')
54 const title = newline === -1 ? chunk : chunk.slice(0, newline)
55 const known = SECTION_ACTIVITY.find(([pattern]) => pattern.test(title))
56 const activity = known ? known[1] : null
57 for (const line of chunk.split('\n')) {
58 const rawId = modelColumnId(line)
59 if (!rawId || seen.has(rawId)) continue
60 seen.add(rawId)
61 models.push({ rawId, activity, pricing: null })
62 }
63 }
64 if (models.length === 0) {
65 throw new Error('minimax: models page listed no ids')
66 }
67 return models
69​
70async function listModels(
71 _env: ProviderSecrets,
72 kv?: KVNamespace,
73): Promise<ListModelsResult> {
74 const markdown = await fetchText(MINIMAX_MODELS_URL)
75 const facts = await minimaxModelFacts(kv)
76 return {
77 models: parseMinimaxModels(markdown).map((model) =>
78 model.activity === 'chat' ? { ...model, ...facts(model.rawId) } : model,
79 ),
80 }
82​
83async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
84 const docs = await Promise.all(
85 SPEC_URLS.map(async (url) => ({ url, ...(await fetchOpenApi(url)) })),
86 )
87 return {
88 specs: docs.map((doc) => doc.spec),
89 sources: docs.map(({ url, hash }) => ({ url, hash })),
90 outputStrategy: 'post-200',
91 }
93​
94export const provider: ProviderConfig = {
95 id: 'minimax',
96 displayName: 'MiniMax',
97 specSourceUrl: MINIMAX_CHAT_SPEC_URL,
98 modelsEndpoint: MINIMAX_MODELS_URL,
99 defaultDerivation: 'upstream-spec',
100 fetchSpec,
101 listModels,
102 classify: (path) =>
103 path === '/v1/chat/completions' || path === '/anthropic/v1/messages'
104 ? 'chat'
105 : null,
106 generationEndpointId: ({ activity }) =>
107 activity === 'chat' ? 'v1/chat/completions' : null,