All files / api/src/handlers models-handler.ts

0% Statements 0/26
0% Branches 0/1
0% Functions 0/1
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                                                               
import models from '@api/data/models.json';
import providers from '@api/data/providers.json';
import type { AppContext } from '@api/types/hono';
import type { GatewayModel } from '@shared/types/ai-model';
 
/**
 * Handles the models request. Returns a list of models supported by the Ai gateway.
 * Allows filters in query params for the provider
 */
export function modelsHandler(c: AppContext): Response {
  // If the request does not contain a provider query param, return all models. Add a count as well.
  const provider = c.req.query('provider');
  if (!provider) {
    return c.json({
      ...models,
      count: models.data.length,
    });
  } else {
    // Filter the models by the provider
    const filteredModels = models.data.filter(
      (model: GatewayModel) => model.provider.id === provider,
    );
    return c.json({
      ...models,
      data: filteredModels,
      count: filteredModels.length,
    });
  }
}
 
/**
 * Handles the providers request. Returns a list of providers supported by the Ai gateway.
 */
export function providersHandler(c: AppContext): Response {
  return c.json({
    ...providers,
    count: providers.data.length,
  });
}