| 1 | #!/usr/bin/env python3 |
| 2 | """Regenerate src/providers_data.rs from Mastra's provider-registry.json. |
| 3 | |
| 4 | Fetches the canonical provider registry (base URLs, env vars, model lists) |
| 5 | from the mastra-ai/mastra GitHub repo and emits a Rust static table. |
| 6 | Curated overrides fix poor auto-picked defaults and fill missing base URLs. |
| 7 | |
| 8 | Usage: python3 scripts/gen_providers.py |
| 9 | """ |
| 10 | |
| 11 | import json |
| 12 | import re |
| 13 | import urllib.request |
| 14 | |
| 15 | REGISTRY_URL = ( |
| 16 | "https://raw.githubusercontent.com/mastra-ai/mastra/main/" |
| 17 | "packages/core/src/llm/model/provider-registry.json" |
| 18 | ) |
| 19 | OUT = "src/llm/providers_data.rs" |
| 20 | |
| 21 | BAD = re.compile( |
| 22 | r"(embedding|realtime |
| 23 | r"translate|^text- |
| 24 | re.I, |
| 25 | ) |
| 26 | |
| 27 | CURATED = { |
| 28 | "openai": (None, |
| 29 | "anthropic": (None, |
| 30 | "google": (None, |
| 31 | "groq": (None, |
| 32 | "xai": ("https://api.x.ai/v1" |
| 33 | "cerebras": ("https://api.cerebras.ai/v1" |
| 34 | "deepinfra": ("https://api.deepinfra.com/v1/openai" |
| 35 | "perplexity": ("https://api.perplexity.ai" |
| 36 | "togetherai": ("https://api.together.xyz/v1" |
| 37 | "moonshotai": (None, |
| 38 | "moonshotai-cn": (None, |
| 39 | "zai": (None, |
| 40 | "alibaba": (None, |
| 41 | "sakana": (None, |
| 42 | "upstage": (None, |
| 43 | "deepseek": (None, |
| 44 | "opencode": (None, |
| 45 | "opencode-go": (None, |
| 46 | "vercel": ("https://ai.gateway.vercel.ai/v1" |
| 47 | } |
| 48 | |
| 49 | |
| 50 | def fetch() -> |
| 51 | with urllib.request.urlopen(REGISTRY_URL |
| 52 | return json.load(r) |
| 53 | |
| 54 | |
| 55 | def pick_model(v: |
| 56 | models = v.get("models", |
| 57 | depr = set(v.get |
| 58 | for m in models: |
| 59 | if m in depr: |
| 60 | continue |
| 61 | if BAD.search(m |
| 62 | continue |
| 63 | return m |
| 64 | return models[0] |
| 65 | |
| 66 | |
| 67 | def pick_models(v: |
| 68 | """All non-deprecated chat-ish model names, sorted, capped for size.""" |
| 69 | depr = set(v.get |
| 70 | out = [] |
| 71 | for m in v.get("models |
| 72 | if m in depr or BAD |
| 73 | continue |
| 74 | if m not in out: |
| 75 | out.append(m) |
| 76 | out.sort() |
| 77 | return out[:cap |
| 78 | |
| 79 | |
| 80 | def fmt_for(npm) |
| 81 | if not npm: |
| 82 | return "Compatible" |
| 83 | if "anthropic" in npm: |
| 84 | return "Anthropic" |
| 85 | if "google" in npm and |
| 86 | return "Gemini" |
| 87 | if "bedrock" in npm or |
| 88 | return "Bedrock" |
| 89 | return "Compatible" |
| 90 | |
| 91 | |
| 92 | def esc(s: |
| 93 | return s.replace("\\", |
| 94 | |
| 95 | |
| 96 | def main() -> |
| 97 | data = fetch() |
| 98 | provs = data["providers |
| 99 | rows = [] |
| 100 | model_lists = {} |
| 101 | for key, v in sorted |
| 102 | env = v.get("apiKeyEnvVar") |
| 103 | if isinstance(env, |
| 104 | env = ",".join(env |
| 105 | base = v.get("url") |
| 106 | model = pick_model(v) |
| 107 | curated = CURATED.get(key |
| 108 | if curated: |
| 109 | if curated[0] |
| 110 | base = curated[0] |
| 111 | if curated[1] |
| 112 | model = curated[1] |
| 113 | rows.append((key, env, |
| 114 | model_lists[key] = |
| 115 | |
| 116 | lines = [ |
| 117 | "// Generated by scripts/gen_providers.py from Mastra's provider-registry.json", |
| 118 | "// (https://github.com/mastra-ai/mastra). Do not edit by hand.", |
| 119 | "", |
| 120 | "use super::ApiFormat;", |
| 121 | "", |
| 122 | "pub struct ProviderRow {", |
| 123 | " pub name: &'static str,", |
| 124 | " pub format: ApiFormat,", |
| 125 | " pub env: &'static str,", |
| 126 | " pub base_url: &'static str,", |
| 127 | " pub default_model: &'static str,", |
| 128 | " pub requires_key: bool,", |
| 129 | "}", |
| 130 | "", |
| 131 | "pub const PROVIDER_ROWS: &[ProviderRow] = &[", |
| 132 | ] |
| 133 | fmt_rs = { |
| 134 | "Compatible": "ApiFormat::Compatible", |
| 135 | "Anthropic": "ApiFormat::Anthropic", |
| 136 | "Gemini": "ApiFormat::Gemini", |
| 137 | "Bedrock": "ApiFormat::Bedrock", |
| 138 | } |
| 139 | for name, env, fmt, base, |
| 140 | rk = "true" if env else |
| 141 | lines.append( |
| 142 | f' ProviderRow {{ name: "{esc( |
| 143 | f'env: "{esc(env) |
| 144 | f'default_model: "{esc(model) |
| 145 | ) |
| 146 | lines.append("];") |
| 147 | lines.append("") |
| 148 | lines.append("pub const MODEL_LISTS: &[(&str, &[&str])] = &[") |
| 149 | for key in sorted(model_lists |
| 150 | models = model_lists[key] |
| 151 | if not models: |
| 152 | continue |
| 153 | joined = ", ".join(f |
| 154 | lines.append(f' ("{esc |
| 155 | lines.append("];") |
| 156 | lines.append("") |
| 157 | with open(OUT, |
| 158 | f.write("\n".join( |
| 159 | print(f"wrote {OUT |
| 160 | |
| 161 | |
| 162 | if __name__ == "__main__": |
| 163 | main() |