Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205export type ModelProtocol = "workers-ai" | "chat-completions" | "messages";
interface ProviderDefinition { name: string; description: string; credential: boolean; models: Record<string, ModelProtocol>; transport: | { kind: "workers-ai" } | { kind: "direct"; baseURL: string } | { kind: "gateway"; gatewayId: string; provider: string; baseURL: string; };}
// Trusted definitions, never user-supplied URLs. A provider owns its model wire// formats, routing and credential requirements; callers select only an ID/model.export const MODEL_PROVIDERS = { "workers-ai": { name: "Cloudflare Workers AI", description: "Workers AI uses this installation’s Cloudflare connection. No provider key is needed.", credential: false, transport: { kind: "workers-ai" }, models: { "@cf/meta/llama-3.3-70b-instruct-fp8-fast": "workers-ai", "@cf/meta/llama-4-scout-17b-16e-instruct": "workers-ai", "thinkingmachines/inkling-256k": "messages", "@cf/qwen/qwen3-30b-a3b-fp8": "workers-ai", "@cf/qwen/qwen3.8-27b": "workers-ai", "@cf/nvidia/nemotron-3-120b-a12b": "workers-ai", "@cf/zai-org/glm-4.7-flash": "workers-ai", "@cf/zai-org/glm-5.3": "workers-ai", "@cf/deepseek-ai/deepseek-v4-flash-0731": "workers-ai", }, }, anthropic: { name: "Anthropic", description: "Anthropic uses your own API key. Usage is billed to your Anthropic account.", credential: true, transport: { kind: "direct", baseURL: "https://api.anthropic.com/v1" }, models: { "claude-sonnet-5": "messages", "claude-haiku-4-5-20251001": "messages", }, }, openrouter: { name: "OpenRouter", description: "Uses your OpenRouter API key through Cloudflare AI Gateway. Usage is billed to your OpenRouter account.", credential: true, transport: { kind: "gateway", gatewayId: "default", provider: "openrouter", baseURL: "https://openrouter.ai/api/v1", }, // Top ten text models by weekly usage: https://openrouter.ai/rankings // (2026-09-06). IDs and tool support verified against /api/v1/models. // Use GPT-5.6 Luna as the initial selection when switching providers. models: { "openai/gpt-5.6-luna": "chat-completions", "tencent/hy4-preview": "chat-completions", "z-ai/glm-5.3-flash": "chat-completions", "deepseek/deepseek-v4-flash-0731": "chat-completions", "minimax/minimax-m3:free": "chat-completions", "deepseek/deepseek-v4-flash": "chat-completions", "tencent/hy3": "chat-completions", "nvidia/nemotron-3-ultra-550b-a55b:free": "chat-completions", "z-ai/glm-5.3": "chat-completions", "xiaomi/mimo-v2.5": "chat-completions", // Preserve selections stored by dev.6; catalog changes must not break reads. "openai/gpt-5-mini": "chat-completions", }, },} as const satisfies Record<string, ProviderDefinition>;
export type ModelProvider = keyof typeof MODEL_PROVIDERS;export type CredentialProvider = { [P in ModelProvider]: (typeof MODEL_PROVIDERS)[P]["credential"] extends true ? P : never;}[ModelProvider];export type ModelConfiguration = { [P in ModelProvider]: { provider: P; model: keyof (typeof MODEL_PROVIDERS)[P]["models"]; effort?: ModelEffort; };}[ModelProvider];
export const EFFORT_LABELS = { low: "Low", medium: "Medium", high: "High", xhigh: "Extra high", max: "Maximum",} as const;export type ModelEffort = keyof typeof EFFORT_LABELS;
// Verified 2026-09-07. Native effort only: never synthesize token budgets.// https://openrouter.ai/api/v1/models (reasoning.supported_efforts)// https://platform.claude.com/docs/en/build-with-claude/effort// https://tinker-docs.thinkingmachines.ai/tinker/compatible-apis/anthropic/// Empty means no verified effort control on this exact provider route.export const MODEL_EFFORTS = { "workers-ai": { "@cf/meta/llama-3.3-70b-instruct-fp8-fast": [], "@cf/meta/llama-4-scout-17b-16e-instruct": [], "thinkingmachines/inkling-256k": ["low", "medium", "high", "xhigh", "max"], "@cf/qwen/qwen3-30b-a3b-fp8": [], "@cf/qwen/qwen3.8-27b": [], "@cf/nvidia/nemotron-3-120b-a12b": [], "@cf/zai-org/glm-4.7-flash": [], "@cf/zai-org/glm-5.3": [], "@cf/deepseek-ai/deepseek-v4-flash-0731": [], }, anthropic: { "claude-sonnet-5": ["low", "medium", "high", "xhigh", "max"], "claude-haiku-4-5-20251001": [], }, openrouter: { "openai/gpt-5.6-luna": ["low", "medium", "high", "xhigh", "max"], "tencent/hy4-preview": ["low", "high"], "z-ai/glm-5.3-flash": ["low", "high", "max"], "deepseek/deepseek-v4-flash-0731": ["low", "high", "max"], "minimax/minimax-m3:free": [], "deepseek/deepseek-v4-flash": ["high", "xhigh"], "tencent/hy3": ["low", "high"], "nvidia/nemotron-3-ultra-550b-a55b:free": ["medium", "high"], "z-ai/glm-5.3": ["low", "high", "max"], "xiaomi/mimo-v2.5": [], "openai/gpt-5-mini": [], },} as const satisfies { [P in ModelProvider]: Record< keyof (typeof MODEL_PROVIDERS)[P]["models"], readonly ModelEffort[] >;};
export function modelEfforts( configuration: ModelConfiguration,): readonly ModelEffort[] { const models: Record<string, readonly ModelEffort[]> = MODEL_EFFORTS[configuration.provider]; return models[configuration.model];}
export const MODEL_CATALOG = Object.fromEntries( Object.entries(MODEL_PROVIDERS).map(([id, provider]) => [ id, Object.keys(provider.models), ]),) as { [P in ModelProvider]: (keyof (typeof MODEL_PROVIDERS)[P]["models"])[] };
export function isModelProvider(value: unknown): value is ModelProvider { return typeof value === "string" && Object.hasOwn(MODEL_PROVIDERS, value);}
// Verified 2026-09-07: https://developers.cloudflare.com/workers-ai/platform/pricing/// Free allowance is capped, not unlimited free inference. Gateway unified// billing can charge credits instead of using the Workers AI daily allocation.const WORKERS_AI_PRICING = { "@cf/meta/llama-3.3-70b-instruct-fp8-fast": "Free allowance", "@cf/meta/llama-4-scout-17b-16e-instruct": "Free allowance", "thinkingmachines/inkling-256k": "Paid", "@cf/qwen/qwen3-30b-a3b-fp8": "Free allowance", "@cf/qwen/qwen3.8-27b": "Free allowance", "@cf/nvidia/nemotron-3-120b-a12b": "Free allowance", "@cf/zai-org/glm-4.7-flash": "Free allowance", "@cf/zai-org/glm-5.3": "Paid", "@cf/deepseek-ai/deepseek-v4-flash-0731": "Paid",} as const satisfies Record< keyof (typeof MODEL_PROVIDERS)["workers-ai"]["models"], string>;
export function modelLabel(provider: ModelProvider, model: string): string { const pricing = provider === "workers-ai" ? (WORKERS_AI_PRICING as Record<string, string>)[model] : provider === "openrouter" && model.endsWith(":free") ? "Free" : "Paid"; return pricing ? `${model} · ${pricing}` : model;}
export function isCredentialProvider( value: unknown,): value is CredentialProvider { return isModelProvider(value) && MODEL_PROVIDERS[value].credential;}
export const CREDENTIAL_PROVIDERS = Object.keys(MODEL_PROVIDERS).filter(isCredentialProvider);
export function missingProviderKey(provider: ModelProvider): string { return `Add an ${MODEL_PROVIDERS[provider].name} API key in settings before sending a message`;}