mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
b88ca96064
* refactor: move client_type to provider, replace model fields with config JSONB - Move `client_type` from `models` to `llm_providers` table - Add `icon` field to `llm_providers` - Replace `dimensions`, `input_modalities`, `supports_reasoning` on `models` with a single `config` JSONB column containing `dimensions`, `compatibilities` (vision, tool-call, image-output, reasoning), and `context_window` - Auto-imported models default to vision + tool-call + reasoning - Update all backend consumers (agent, flow resolver, handlers, memory) - Regenerate sqlc, swagger, and TypeScript SDK - Update frontend forms, display, and i18n for new schema * ui: show provider icon avatar in sidebar and detail header, remove icon input * feat: add built-in provider registry with YAML definitions and enable toggle - Add `enable` column to llm_providers (default true, backward-compatible) - Create internal/registry package to load YAML provider/model definitions on startup and upsert into database (new providers disabled by default) - Add conf/providers/ with OpenAI, Anthropic, Google YAML definitions - Add RegistryConfig to TOML config (providers_dir, default conf/providers) - Model listing APIs and conversation flow now filter by enabled providers - Frontend: enable switch in provider form, green status dot in sidebar, enabled providers sorted to top * fix: make 0041 migration idempotent for fresh databases Guard data migration steps with column-existence checks so the migration succeeds on databases created from the updated init schema.
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package registry
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"github.com/memohai/memoh/internal/db/sqlc"
|
|
)
|
|
|
|
// Load reads all .yaml / .yml files from dir and returns parsed provider
|
|
// definitions. It returns nil (no error) when the directory does not exist.
|
|
func Load(dir string) ([]ProviderDefinition, error) {
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("read providers dir %s: %w", dir, err)
|
|
}
|
|
|
|
var defs []ProviderDefinition
|
|
for _, e := range entries {
|
|
if e.IsDir() {
|
|
continue
|
|
}
|
|
ext := strings.ToLower(filepath.Ext(e.Name()))
|
|
if ext != ".yaml" && ext != ".yml" {
|
|
continue
|
|
}
|
|
path := filepath.Join(dir, e.Name())
|
|
data, err := os.ReadFile(path) //nolint:gosec // operator-managed config directory
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read %s: %w", path, err)
|
|
}
|
|
var def ProviderDefinition
|
|
if err := yaml.Unmarshal(data, &def); err != nil {
|
|
return nil, fmt.Errorf("parse %s: %w", path, err)
|
|
}
|
|
if def.Name == "" {
|
|
continue
|
|
}
|
|
defs = append(defs, def)
|
|
}
|
|
return defs, nil
|
|
}
|
|
|
|
// Sync upserts the given provider definitions into the database. New providers
|
|
// are created with enable=false and an empty API key. Existing providers get
|
|
// their icon and client_type refreshed. Models are upserted by (provider_id,
|
|
// model_id), overwriting name/type/config.
|
|
func Sync(ctx context.Context, logger *slog.Logger, queries *sqlc.Queries, defs []ProviderDefinition) error {
|
|
for _, def := range defs {
|
|
var icon pgtype.Text
|
|
if def.Icon != "" {
|
|
icon = pgtype.Text{String: def.Icon, Valid: true}
|
|
}
|
|
|
|
provider, err := queries.UpsertRegistryProvider(ctx, sqlc.UpsertRegistryProviderParams{
|
|
Name: def.Name,
|
|
BaseUrl: def.BaseURL,
|
|
ClientType: def.ClientType,
|
|
Icon: icon,
|
|
})
|
|
if err != nil {
|
|
logger.Warn("registry: failed to upsert provider", slog.String("name", def.Name), slog.Any("error", err))
|
|
continue
|
|
}
|
|
|
|
for _, m := range def.Models {
|
|
configJSON, err := json.Marshal(m.Config)
|
|
if err != nil {
|
|
logger.Warn("registry: failed to marshal model config",
|
|
slog.String("provider", def.Name), slog.String("model", m.ModelID), slog.Any("error", err))
|
|
continue
|
|
}
|
|
|
|
var name pgtype.Text
|
|
if m.Name != "" {
|
|
name = pgtype.Text{String: m.Name, Valid: true}
|
|
}
|
|
|
|
typ := m.Type
|
|
if typ == "" {
|
|
typ = "chat"
|
|
}
|
|
|
|
_, err = queries.UpsertRegistryModel(ctx, sqlc.UpsertRegistryModelParams{
|
|
ModelID: m.ModelID,
|
|
Name: name,
|
|
LlmProviderID: provider.ID,
|
|
Type: typ,
|
|
Config: configJSON,
|
|
})
|
|
if err != nil {
|
|
logger.Warn("registry: failed to upsert model",
|
|
slog.String("provider", def.Name), slog.String("model", m.ModelID), slog.Any("error", err))
|
|
continue
|
|
}
|
|
}
|
|
|
|
logger.Info("registry: synced provider", slog.String("name", def.Name), slog.Int("models", len(def.Models)))
|
|
}
|
|
return nil
|
|
}
|