7814
Ta
tiny-c-agent / files
tiny-c-agent / src / catalog.c
2.2 KB
1#include "catalog.h" 2 3#include <string.h> 4 5extern const tiny_model_info tiny_catalog_entries[]; 6extern const size_t tiny_catalog_entries_len; 7 8const tiny_model_info *tiny_catalog_lookup(const char *provider, const char *model) { 9 if (!provider || !model) return NULL; 10 for (size_t i = 0; i < tiny_catalog_entries_len; i++) { 11 const tiny_model_info *e = &tiny_catalog_entries[i]; 12 if (strcmp(e->provider, provider) == 0 && strcmp(e->id, model) == 0) return e; 13 } 14 return NULL; 15} 16 17size_t tiny_catalog_count(void) { return tiny_catalog_entries_len; } 18 19const tiny_model_info *tiny_catalog_get(size_t index) { 20 if (index >= tiny_catalog_entries_len) return NULL; 21 return &tiny_catalog_entries[index]; 22} 23 24// Rank of an effort value on the off..max ladder. Both "off" and the catalog's 25// "none" sit at 0. Returns -1 for an unknown value. 26static int effort_rank(const char *s) { 27 static const char *const ladder[] = { 28 "off", "none", "minimal", "low", "medium", "high", "xhigh", "max", 29 }; 30 for (int i = 0; i < (int)(sizeof ladder / sizeof ladder[0]); i++) 31 if (strcmp(ladder[i], s) == 0) return i <= 1 ? 0 : i - 1; 32 return -1; 33} 34 35// The catalog uses models.dev's "none"; the config and wire use "off". 36static const char *norm_effort(const char *v) { 37 return strcmp(v, "none") == 0 ? "off" : v; 38} 39 40const char *tiny_catalog_clamp_effort(const tiny_model_info *info, const char *desired) { 41 if (!desired) return NULL; 42 if (!info) return desired; 43 for (size_t i = 0; i < info->effort_len; i++) 44 if (strcmp(info->effort[i], desired) == 0) return norm_effort(info->effort[i]); 45 46 int want = effort_rank(desired); 47 if (want < 0) return info->effort_len ? norm_effort(info->effort[0]) : NULL; 48 // Prefer the next higher accepted level, then the next lower. 49 for (int rank = want; rank <= 6; rank++) 50 for (size_t i = 0; i < info->effort_len; i++) 51 if (effort_rank(info->effort[i]) == rank) return norm_effort(info->effort[i]); 52 for (int rank = want - 1; rank >= 0; rank--) 53 for (size_t i = 0; i < info->effort_len; i++) 54 if (effort_rank(info->effort[i]) == rank) return norm_effort(info->effort[i]); 55 return NULL; 56}