All files / shared/src/types models-dev.ts

73.33% Statements 44/60
100% Branches 0/0
0% Functions 0/2
73.33% Lines 44/60

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1191x         1x 1x 1x 1x             1x 1x 1x 1x 1x             1x 1x 1x 1x                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x             1x                                 1x                                  
import { z } from 'zod';
 
/**
 * Models.dev API model modalities (input/output types)
 */
export const ModelsDevModalitiesSchema = z.object({
  input: z.array(z.string()),
  output: z.array(z.string()),
});
 
export type ModelsDevModalities = z.infer<typeof ModelsDevModalitiesSchema>;
 
/**
 * Models.dev API model cost information
 */
export const ModelsDevCostSchema = z.object({
  input: z.number().optional(),
  output: z.number().optional(),
  cache_read: z.number().optional(),
});
 
export type ModelsDevCost = z.infer<typeof ModelsDevCostSchema>;
 
/**
 * Models.dev API model limits
 */
export const ModelsDevLimitSchema = z.object({
  context: z.number().optional(),
  output: z.number().optional(),
});
 
export type ModelsDevLimit = z.infer<typeof ModelsDevLimitSchema>;
 
/**
 * Models.dev API individual model schema
 * Note: The id field may not always be present in the API response,
 * so we make it optional and use the key from the parent object instead
 */
export const ModelsDevModelSchema = z.object({
  id: z.string().optional(),
  name: z.string().optional(),
  attachment: z.boolean().optional(),
  reasoning: z.boolean().optional(),
  tool_call: z.boolean().optional(),
  temperature: z.boolean().optional(),
  knowledge: z.string().optional(),
  release_date: z.string().optional(),
  last_updated: z.string().optional(),
  modalities: ModelsDevModalitiesSchema.optional(),
  open_weights: z.boolean().optional(),
  cost: ModelsDevCostSchema.optional(),
  limit: ModelsDevLimitSchema.optional(),
});
 
export type ModelsDevModel = z.infer<typeof ModelsDevModelSchema>;
 
/**
 * Models.dev API provider schema
 */
export const ModelsDevProviderSchema = z.object({
  id: z.string(),
  env: z.array(z.string()).optional(),
  npm: z.string().optional(),
  api: z.string().optional(),
  name: z.string().optional(),
  doc: z.string().optional(),
  models: z.record(z.string(), ModelsDevModelSchema),
});
 
export type ModelsDevProvider = z.infer<typeof ModelsDevProviderSchema>;
 
/**
 * Models.dev API response schema (top-level object with provider IDs as keys)
 */
export const ModelsDevResponseSchema = z.record(
  z.string(),
  ModelsDevProviderSchema,
);
 
export type ModelsDevResponse = z.infer<typeof ModelsDevResponseSchema>;
 
/**
 * Parse and validate models.dev API response
 */
export const parseModelsDevResponse = (data: unknown): ModelsDevResponse => {
  return ModelsDevResponseSchema.parse(data);
};
 
/**
 * Flatten all models from all providers into a single array with provider info
 */
export interface FlattenedModel {
  model: ModelsDevModel;
  providerId: string;
  providerName: string | undefined;
}
 
/**
 * Flatten models.dev response into an array of models with provider information
 * Ensures model.id is always set from the key
 */
export const flattenModelsDevResponse = (
  response: ModelsDevResponse,
): FlattenedModel[] => {
  const flattened: FlattenedModel[] = [];
 
  for (const [providerId, provider] of Object.entries(response)) {
    for (const [modelId, model] of Object.entries(provider.models)) {
      flattened.push({
        model: { ...model, id: modelId },
        providerId,
        providerName: provider.name,
      });
    }
  }
 
  return flattened;
};