|
| 1 | +import axios from "axios" |
| 2 | +import { z } from "zod" |
| 3 | +import type { ModelInfo } from "@roo-code/types" |
| 4 | + |
| 5 | +// Helicone Public Model Registry schemas |
| 6 | +const heliconePricingSchema = z.object({ |
| 7 | + prompt: z.number().optional(), |
| 8 | + completion: z.number().optional(), |
| 9 | + cacheRead: z.number().optional(), |
| 10 | + cacheWrite: z.number().optional(), |
| 11 | +}) |
| 12 | + |
| 13 | +const heliconeEndpointSchema = z.object({ |
| 14 | + provider: z.string().optional(), |
| 15 | + providerSlug: z.string().optional(), |
| 16 | + supportsPtb: z.boolean().optional(), |
| 17 | + pricing: heliconePricingSchema.optional(), |
| 18 | +}) |
| 19 | + |
| 20 | +const heliconeModelSchema = z.object({ |
| 21 | + id: z.string(), |
| 22 | + name: z.string().optional(), |
| 23 | + author: z.string().optional(), |
| 24 | + contextLength: z.number().optional(), |
| 25 | + maxOutput: z.number().optional(), |
| 26 | + description: z.string().optional(), |
| 27 | + inputModalities: z.array(z.string().nullable()).nullable().optional(), |
| 28 | + outputModalities: z.array(z.string().nullable()).nullable().optional(), |
| 29 | + supportedParameters: z.array(z.string().nullable()).nullable().optional(), |
| 30 | + endpoints: z.array(heliconeEndpointSchema).default([]), |
| 31 | +}) |
| 32 | + |
| 33 | +const heliconeModelsResponseSchema = z.object({ |
| 34 | + models: z.array(heliconeModelSchema), |
| 35 | + total: z.number().optional(), |
| 36 | +}) |
| 37 | + |
| 38 | +export async function getHeliconeModels(): Promise<Record<string, ModelInfo>> { |
| 39 | + const models: Record<string, ModelInfo> = {} |
| 40 | + |
| 41 | + try { |
| 42 | + // Public model registry (no auth required) |
| 43 | + const url = "https://api.helicone.ai/v1/public/model-registry/models" |
| 44 | + const resp = await axios.get(url) |
| 45 | + const parsed = heliconeModelsResponseSchema.safeParse(resp.data.data) |
| 46 | + const data = parsed.success ? parsed.data?.models : resp.data?.data?.models || [] |
| 47 | + |
| 48 | + if (!parsed.success) { |
| 49 | + console.error("Helicone models response is invalid", parsed.error.format()) |
| 50 | + } |
| 51 | + |
| 52 | + for (const m of data) { |
| 53 | + const model = heliconeModelSchema.parse(m) |
| 54 | + |
| 55 | + const contextLength = model.contextLength ?? 128_000 |
| 56 | + const maxTokens = model.maxOutput ?? Math.ceil(contextLength * 0.2) |
| 57 | + const inputMods = (model.inputModalities || []).filter((mod): mod is string => typeof mod === "string") |
| 58 | + |
| 59 | + // Find the first PTB-enabled endpoint for pricing |
| 60 | + const ptbEndpoint = model.endpoints.find((ep) => ep.supportsPtb === true) |
| 61 | + if (!ptbEndpoint) continue |
| 62 | + |
| 63 | + const info: ModelInfo = { |
| 64 | + maxTokens, |
| 65 | + contextWindow: contextLength, |
| 66 | + supportsImages: inputMods.includes("image"), |
| 67 | + supportsPromptCache: true, |
| 68 | + inputPrice: ptbEndpoint.pricing?.prompt, |
| 69 | + outputPrice: ptbEndpoint.pricing?.completion, |
| 70 | + cacheWritesPrice: ptbEndpoint.pricing?.cacheWrite, |
| 71 | + cacheReadsPrice: ptbEndpoint.pricing?.cacheRead, |
| 72 | + description: model.description, |
| 73 | + } |
| 74 | + |
| 75 | + models[model.id] = info |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + console.error( |
| 79 | + `Error fetching Helicone models: ${JSON.stringify(error, Object.getOwnPropertyNames(error as any), 2)}`, |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + return models |
| 84 | +} |
0 commit comments