|
1 | | -import { Context } from 'hono'; |
2 | | -import models from '../data/models.json'; |
3 | | -import providers from '../data/providers.json'; |
| 1 | +import { Context, Next } from 'hono'; |
| 2 | +import { HEADER_KEYS } from '../globals'; |
| 3 | +import { env } from 'hono/adapter'; |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * Handles the models request. Returns a list of models supported by the Ai gateway. |
7 | 7 | * Allows filters in query params for the provider |
8 | 8 | * @param c - The Hono context |
9 | 9 | * @returns - The response |
10 | 10 | */ |
11 | | -export async function modelsHandler(c: Context): Promise<Response> { |
12 | | - // If the request does not contain a provider query param, return all models. Add a count as well. |
13 | | - const provider = c.req.query('provider'); |
14 | | - if (!provider) { |
15 | | - return c.json({ |
16 | | - ...models, |
17 | | - count: models.data.length, |
18 | | - }); |
19 | | - } else { |
20 | | - // Filter the models by the provider |
21 | | - const filteredModels = models.data.filter( |
22 | | - (model: any) => model.provider.id === provider |
23 | | - ); |
24 | | - return c.json({ |
25 | | - ...models, |
26 | | - data: filteredModels, |
27 | | - count: filteredModels.length, |
28 | | - }); |
| 11 | +export const modelsHandler = async (context: Context, next: Next) => { |
| 12 | + const fetchOptions: Record<string, any> = {}; |
| 13 | + fetchOptions['method'] = context.req.method; |
| 14 | + |
| 15 | + const controlPlaneURL = env(context).ALBUS_BASEPATH; |
| 16 | + |
| 17 | + const headers = Object.fromEntries(context.req.raw.headers); |
| 18 | + |
| 19 | + const authHeader = headers['Authorization'] || headers['authorization']; |
| 20 | + |
| 21 | + const apiKey = |
| 22 | + headers[HEADER_KEYS.API_KEY] || authHeader?.replace('Bearer ', ''); |
| 23 | + let config: any = headers[HEADER_KEYS.CONFIG]; |
| 24 | + if (config && typeof config === 'string') { |
| 25 | + try { |
| 26 | + config = JSON.parse(config); |
| 27 | + } catch { |
| 28 | + config = {}; |
| 29 | + } |
29 | 30 | } |
30 | | -} |
| 31 | + const providerHeader = headers[HEADER_KEYS.PROVIDER]; |
| 32 | + const virtualKey = headers[HEADER_KEYS.VIRTUAL_KEY]; |
| 33 | + |
| 34 | + const containsProvider = |
| 35 | + providerHeader || virtualKey || config?.provider || config?.virtual_key; |
| 36 | + |
| 37 | + if (containsProvider || !controlPlaneURL) { |
| 38 | + return next(); |
| 39 | + } |
| 40 | + |
| 41 | + // Strip gateway endpoint for models endpoint. |
| 42 | + const urlObject = new URL(context.req.url); |
| 43 | + const requestRoute = `${controlPlaneURL}${context.req.path.replace('/v1/', '/v2/')}${urlObject.search}`; |
| 44 | + fetchOptions['headers'] = { |
| 45 | + [HEADER_KEYS.API_KEY]: apiKey, |
| 46 | + }; |
31 | 47 |
|
32 | | -export async function providersHandler(c: Context): Promise<Response> { |
33 | | - return c.json({ |
34 | | - ...providers, |
35 | | - count: providers.data.length, |
| 48 | + const resp = await fetch(requestRoute, fetchOptions); |
| 49 | + return new Response(resp.body, { |
| 50 | + status: resp.status, |
| 51 | + headers: { |
| 52 | + 'content-type': 'application/json', |
| 53 | + }, |
36 | 54 | }); |
37 | | -} |
| 55 | +}; |
0 commit comments