|
| 1 | +import * as z from 'zod/v4'; |
| 2 | + |
| 3 | +import { LLM_IF_OAI_Chat, LLM_IF_OAI_Fn, LLM_IF_OAI_Json, LLM_IF_OAI_PromptCaching, LLM_IF_OAI_Reasoning, LLM_IF_OAI_Vision } from '~/common/stores/llms/llms.types'; |
| 4 | + |
| 5 | +import { serverCapitalizeFirstLetter } from '~/server/wire'; |
| 6 | + |
| 7 | +import type { ModelDescriptionSchema } from '../../llm.server.types'; |
| 8 | + |
| 9 | +import { fromManualMapping, llmsDefineManualMappings } from '../../models.mappings'; |
| 10 | + |
| 11 | +// --- Modular Model ID inference (auto-derived from _modularKnownModels) --- |
| 12 | +export type LlmsModularModelId = typeof _modularKnownModels[number]['idPrefix']; |
| 13 | + |
| 14 | + |
| 15 | +// [Modular] Models List API schema - observed at https://api.modular.com/v1/models (2026-08-13). |
| 16 | +// The list carries only id/object/created/owned_by - no capabilities, no pricing - so all caps and |
| 17 | +// prices come from the manual mappings below. Ids mostly mirror HuggingFace repo names, are |
| 18 | +// CASE-SENSITIVE, and churn without notice (MiniMaxAI/MiniMax-M3 -> minimax/minimax-m3 mid-day |
| 19 | +// 2026-08-13, the old id now 404s) - keep the table in sync with the live list. |
| 20 | +const _wireModularModelItemSchema = z.object({ |
| 21 | + id: z.string(), // only strictly required field |
| 22 | + object: z.string().nullish(), |
| 23 | + created: z.number().nullish(), |
| 24 | + owned_by: z.string().nullish(), |
| 25 | +}); |
| 26 | + |
| 27 | + |
| 28 | +// [Modular Cloud] Editorial table for the shared endpoints (array order = display order), measured |
| 29 | +// live 2026-08-13. Output caps are unverified where noted: the server silently clamps oversized |
| 30 | +// max_tokens instead of erroring, so an over-large value is never observable as a failure. |
| 31 | +const _modularKnownModels = llmsDefineManualMappings([ |
| 32 | + { |
| 33 | + idPrefix: 'minimax/minimax-m3', |
| 34 | + label: 'MiniMax M3', |
| 35 | + description: '1M-context multimodal MoE with default-on reasoning. Served as NVIDIA NVFP4 (4-bit) quantization on Modular Cloud shared endpoints.', |
| 36 | + contextWindow: 1048576, |
| 37 | + maxCompletionTokens: 131072, // unverified |
| 38 | + interfaces: [LLM_IF_OAI_Chat, LLM_IF_OAI_Fn, LLM_IF_OAI_Vision, LLM_IF_OAI_Reasoning, LLM_IF_OAI_PromptCaching], |
| 39 | + chatPrice: { input: 0.30, output: 1.20, cache: { cType: 'oai-ac', read: 0.06 } }, |
| 40 | + }, |
| 41 | + { |
| 42 | + idPrefix: 'google/gemma-4-31b-it', |
| 43 | + label: 'Gemma 4 31B', |
| 44 | + description: 'Google Gemma 4 31B instruction-tuned, text+image input. Served as NVIDIA NVFP4 (4-bit) quantization.', |
| 45 | + contextWindow: 262144, |
| 46 | + maxCompletionTokens: 32768, // unverified |
| 47 | + // no Reasoning (the catalog claims it, but this deployment exposes no reasoning surface) and no Json |
| 48 | + // (json_object emits type-corrupted output here) |
| 49 | + interfaces: [LLM_IF_OAI_Chat, LLM_IF_OAI_Fn, LLM_IF_OAI_Vision], |
| 50 | + chatPrice: { input: 0.25, output: 0.65 }, |
| 51 | + }, |
| 52 | + { |
| 53 | + idPrefix: 'google/gemma-4-26b-a4b-it', |
| 54 | + label: 'Gemma 4 26B A4B', |
| 55 | + description: 'Google Gemma 4 26B MoE (4B active), text+image input. Served as NVIDIA NVFP4 (4-bit) quantization.', |
| 56 | + contextWindow: 262144, |
| 57 | + maxCompletionTokens: 32768, // unverified |
| 58 | + interfaces: [LLM_IF_OAI_Chat, LLM_IF_OAI_Fn, LLM_IF_OAI_Vision, LLM_IF_OAI_Json], |
| 59 | + chatPrice: { input: 0.15, output: 0.60 }, |
| 60 | + }, |
| 61 | + { |
| 62 | + idPrefix: 'moonshotai/kimi-k2.7-code', |
| 63 | + label: 'Kimi K2.7 Code', |
| 64 | + description: 'Moonshot Kimi K2.7 Code, agentic coding model with always-on reasoning. Served as NVIDIA NVFP4 (4-bit) quantization.', |
| 65 | + contextWindow: 262144, |
| 66 | + maxCompletionTokens: 131072, // unverified |
| 67 | + interfaces: [LLM_IF_OAI_Chat, LLM_IF_OAI_Fn, LLM_IF_OAI_Vision, LLM_IF_OAI_Reasoning, LLM_IF_OAI_Json, LLM_IF_OAI_PromptCaching], |
| 68 | + chatPrice: { input: 0.60, output: 3.00, cache: { cType: 'oai-ac', read: 0.12 } }, |
| 69 | + }, |
| 70 | +]); |
| 71 | + |
| 72 | + |
| 73 | +function _prettyModelId(id: string): string { |
| 74 | + // fallback labeler for unknown models, e.g. "deepseek-ai/DeepSeek-V3" => "DeepSeek V3" |
| 75 | + return (id.split('/').pop() || id) |
| 76 | + .replaceAll(/[_-]/g, ' ') |
| 77 | + .split(' ') |
| 78 | + .map(serverCapitalizeFirstLetter) |
| 79 | + .join(' ') |
| 80 | + .trim(); |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +export function modularModelsToModelDescriptions(wireModels: unknown): ModelDescriptionSchema[] { |
| 85 | + |
| 86 | + // tolerant top-level unwrap: accept a plain array or `{ data: [...] }`, else fall back to [] |
| 87 | + let rawItems: unknown[] = []; |
| 88 | + if (Array.isArray(wireModels)) |
| 89 | + rawItems = wireModels; |
| 90 | + else if (wireModels && typeof wireModels === 'object' && Array.isArray((wireModels as { data?: unknown[] }).data)) |
| 91 | + rawItems = (wireModels as { data: unknown[] }).data; |
| 92 | + |
| 93 | + const descriptions: ModelDescriptionSchema[] = []; |
| 94 | + |
| 95 | + for (const rawItem of rawItems) { |
| 96 | + // per-item safeParse: one bad entry never crashes the rest |
| 97 | + const { data: model, error } = _wireModularModelItemSchema.safeParse(rawItem); |
| 98 | + if (error || !model?.id) { |
| 99 | + if (error) console.warn('[DEV] modular: skipping invalid model entry', z.prettifyError(error)); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + // known models get full caps/pricing; unknown ids (day-zero cloud additions, or any model on a |
| 104 | + // self-hosted MAX host) stay visible with a conservative chat-only shape and no context window |
| 105 | + descriptions.push(fromManualMapping(_modularKnownModels, model.id, model.created ?? undefined, undefined, { |
| 106 | + idPrefix: model.id, |
| 107 | + label: _prettyModelId(model.id), |
| 108 | + description: 'Model served via Modular.', |
| 109 | + contextWindow: null, |
| 110 | + interfaces: [LLM_IF_OAI_Chat, LLM_IF_OAI_Fn], |
| 111 | + hidden: false, |
| 112 | + })); |
| 113 | + } |
| 114 | + |
| 115 | + // sort into editorial display order (= _modularKnownModels array order; unknown models sort at their |
| 116 | + // family slot via prefix else last, ties by id) |
| 117 | + const _rank = (id: string) => { |
| 118 | + const exact = _modularKnownModels.findIndex(known => id === known.idPrefix); |
| 119 | + if (exact !== -1) return exact; |
| 120 | + const prefix = _modularKnownModels.findIndex(known => id.startsWith(known.idPrefix)); |
| 121 | + return prefix === -1 ? _modularKnownModels.length : prefix; |
| 122 | + }; |
| 123 | + return descriptions.sort((a, b) => _rank(a.id) - _rank(b.id) || a.id.localeCompare(b.id)); |
| 124 | +} |
0 commit comments