package model import ( "encoding/json" "fmt" "os" "strings" "sync" ) type Repository struct { models []Model mu sync.RWMutex } func NewRepository(dataPath string) *Repository { r := &Repository{} data, err := os.ReadFile(dataPath) if err != nil { fmt.Printf("Warning: could not load models from %s: %v\n", dataPath, err) return r } if err := json.Unmarshal(data, &r.models); err != nil { fmt.Printf("Warning: could not parse models JSON: %v\n", err) } return r } func (r *Repository) List(filter ModelFilter) ([]ModelSummary, int) { r.mu.RLock() defer r.mu.RUnlock() var results []ModelSummary for _, m := range r.models { if filter.TaskType != "" && m.TaskType != filter.TaskType { continue } if filter.Hardware != "" { found := false for _, hw := range m.SupportedHardware { if hw == filter.Hardware { found = true break } } if !found { continue } } if filter.Query != "" { q := strings.ToLower(filter.Query) if !strings.Contains(strings.ToLower(m.Name), q) && !strings.Contains(strings.ToLower(m.Description), q) { continue } } results = append(results, m.ToSummary()) } return results, len(results) } func (r *Repository) GetByID(id string) (*Model, error) { r.mu.RLock() defer r.mu.RUnlock() for i := range r.models { if r.models[i].ID == id { return &r.models[i], nil } } return nil, fmt.Errorf("model not found: %s", id) } func (r *Repository) Count() int { r.mu.RLock() defer r.mu.RUnlock() return len(r.models) } func (r *Repository) Add(m Model) { r.mu.Lock() defer r.mu.Unlock() r.models = append(r.models, m) } func (r *Repository) Remove(id string) error { r.mu.Lock() defer r.mu.Unlock() for i := range r.models { if r.models[i].ID == id { if !r.models[i].IsCustom { return fmt.Errorf("cannot delete built-in model: %s", id) } r.models = append(r.models[:i], r.models[i+1:]...) return nil } } return fmt.Errorf("model not found: %s", id) }