fix: changing base URL for fetching models

This commit is contained in:
John Costa 2025-08-15 10:49:45 +01:00
parent 44086e4a86
commit 5e812cfb4c
7 changed files with 50 additions and 14 deletions

View file

@ -59,7 +59,7 @@ export const getModels = async (options: GetModelsOptions): Promise<ModelRecord>
break
case "requesty":
// Requesty models endpoint requires an API key for per-user custom policies
models = await getRequestyModels(options.apiKey)
models = await getRequestyModels(options.baseUrl, options.apiKey)
break
case "glama":
models = await getGlamaModels()

View file

@ -3,8 +3,9 @@ import axios from "axios"
import type { ModelInfo } from "@roo-code/types"
import { parseApiPrice } from "../../../shared/cost"
import { toRequestyServiceUrl } from "../../../shared/utils/requesty"
export async function getRequestyModels(apiKey?: string): Promise<Record<string, ModelInfo>> {
export async function getRequestyModels(baseUrl?: string, apiKey?: string): Promise<Record<string, ModelInfo>> {
const models: Record<string, ModelInfo> = {}
try {
@ -14,8 +15,10 @@ export async function getRequestyModels(apiKey?: string): Promise<Record<string,
headers["Authorization"] = `Bearer ${apiKey}`
}
const url = "https://router.requesty.ai/v1/models"
const response = await axios.get(url, { headers })
const resolvedBaseUrl = toRequestyServiceUrl(baseUrl)
const modelsUrl = new URL("models", resolvedBaseUrl)
const response = await axios.get(modelsUrl.toString(), { headers })
const rawModels = response.data.data
for (const rawModel of rawModels) {

View file

@ -15,6 +15,7 @@ import { DEFAULT_HEADERS } from "./constants"
import { getModels } from "./fetchers/modelCache"
import { BaseProvider } from "./base-provider"
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata } from "../index"
import { toRequestyServiceUrl } from "../../shared/utils/requesty"
// Requesty usage includes an extra field for Anthropic use cases.
// Safely cast the prompt token details section to the appropriate structure.
@ -40,21 +41,23 @@ export class RequestyHandler extends BaseProvider implements SingleCompletionHan
protected options: ApiHandlerOptions
protected models: ModelRecord = {}
private client: OpenAI
private baseURL: string
constructor(options: ApiHandlerOptions) {
super()
this.options = options
this.baseURL = toRequestyServiceUrl(options.requestyBaseUrl)
this.client = new OpenAI({
baseURL: options.requestyBaseUrl || "https://router.requesty.ai/v1",
baseURL: this.baseURL,
apiKey: this.options.requestyApiKey ?? "not-provided",
defaultHeaders: DEFAULT_HEADERS,
})
}
public async fetchModel() {
this.models = await getModels({ provider: "requesty" })
this.models = await getModels({ provider: "requesty", baseUrl: this.baseURL })
return this.getModel()
}

View file

@ -135,7 +135,7 @@ export const getModelMaxOutputTokens = ({
export type GetModelsOptions =
| { provider: "openrouter" }
| { provider: "glama" }
| { provider: "requesty"; apiKey?: string }
| { provider: "requesty"; apiKey?: string; baseUrl?: string }
| { provider: "unbound"; apiKey?: string }
| { provider: "litellm"; apiKey: string; baseUrl: string }
| { provider: "ollama"; baseUrl?: string }

View file

@ -0,0 +1,17 @@
const REQUESTY_BASE_URL = "https://router.requesty.ai/v1"
type URLType = "router" | "app" | "api"
const replaceCname = (baseUrl: string, type: URLType): string => {
if (type === "router") {
return baseUrl
} else {
return baseUrl.replace("router", type).replace("v1", "")
}
}
export const toRequestyServiceUrl = (baseUrl?: string, service: URLType = "router"): string => {
let url = replaceCname(baseUrl ?? REQUESTY_BASE_URL, service)
return new URL(url).toString()
}

View file

@ -1,9 +1,15 @@
import { VSCodeLink } from "@vscode/webview-ui-toolkit/react"
import { useRequestyKeyInfo } from "@/components/ui/hooks/useRequestyKeyInfo"
import { toRequestyServiceUrl } from "@roo/utils/requesty"
export const RequestyBalanceDisplay = ({ apiKey }: { apiKey: string }) => {
const { data: keyInfo } = useRequestyKeyInfo(apiKey)
type RequestyBalanceDisplayProps = {
apiKey: string
baseUrl?: string
}
export const RequestyBalanceDisplay = ({ baseUrl, apiKey }: RequestyBalanceDisplayProps) => {
const { data: keyInfo } = useRequestyKeyInfo(baseUrl, apiKey)
if (!keyInfo) {
return null
@ -13,8 +19,11 @@ export const RequestyBalanceDisplay = ({ apiKey }: { apiKey: string }) => {
const balance = parseFloat(keyInfo.org_balance)
const formattedBalance = balance.toFixed(2)
const resolvedBaseUrl = toRequestyServiceUrl(baseUrl, "app")
const settingsUrl = new URL("settings", resolvedBaseUrl)
return (
<VSCodeLink href="https://app.requesty.ai/settings" className="text-vscode-foreground hover:underline">
<VSCodeLink href={settingsUrl.toString()} className="text-vscode-foreground hover:underline">
${formattedBalance}
</VSCodeLink>
)

View file

@ -1,6 +1,7 @@
import axios from "axios"
import { z } from "zod"
import { useQuery, UseQueryOptions } from "@tanstack/react-query"
import { toRequestyServiceUrl } from "@roo/utils/requesty"
const requestyKeyInfoSchema = z.object({
name: z.string(),
@ -14,11 +15,14 @@ const requestyKeyInfoSchema = z.object({
export type RequestyKeyInfo = z.infer<typeof requestyKeyInfoSchema>
async function getRequestyKeyInfo(apiKey?: string) {
async function getRequestyKeyInfo(baseUrl?: string, apiKey?: string) {
if (!apiKey) return null
const url = toRequestyServiceUrl(baseUrl, "api")
const apiKeyUrl = new URL("x/apikey", url)
try {
const response = await axios.get("https://api.requesty.ai/x/apikey", {
const response = await axios.get(apiKeyUrl.toString(), {
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
@ -39,10 +43,10 @@ async function getRequestyKeyInfo(apiKey?: string) {
}
type UseRequestyKeyInfoOptions = Omit<UseQueryOptions<RequestyKeyInfo | null>, "queryKey" | "queryFn">
export const useRequestyKeyInfo = (apiKey?: string, options?: UseRequestyKeyInfoOptions) => {
export const useRequestyKeyInfo = (baseUrl?: string, apiKey?: string, options?: UseRequestyKeyInfoOptions) => {
return useQuery<RequestyKeyInfo | null>({
queryKey: ["requesty-key-info", apiKey],
queryFn: () => getRequestyKeyInfo(apiKey),
queryFn: () => getRequestyKeyInfo(baseUrl, apiKey),
staleTime: 30 * 1000, // 30 seconds
enabled: !!apiKey,
...options,