mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
refactor: provider & models (#277)
* 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.
This commit is contained in:
@@ -40,12 +40,24 @@ func (s *Service) Create(ctx context.Context, req CreateRequest) (GetResponse, e
|
||||
return GetResponse{}, fmt.Errorf("marshal metadata: %w", err)
|
||||
}
|
||||
|
||||
// Create provider
|
||||
clientType := req.ClientType
|
||||
if clientType == "" {
|
||||
clientType = string(models.ClientTypeOpenAICompletions)
|
||||
}
|
||||
|
||||
var icon pgtype.Text
|
||||
if req.Icon != "" {
|
||||
icon = pgtype.Text{String: req.Icon, Valid: true}
|
||||
}
|
||||
|
||||
provider, err := s.queries.CreateLlmProvider(ctx, sqlc.CreateLlmProviderParams{
|
||||
Name: req.Name,
|
||||
BaseUrl: req.BaseURL,
|
||||
ApiKey: req.APIKey,
|
||||
Metadata: metadataJSON,
|
||||
Name: req.Name,
|
||||
BaseUrl: req.BaseURL,
|
||||
ApiKey: req.APIKey,
|
||||
ClientType: clientType,
|
||||
Icon: icon,
|
||||
Enable: true,
|
||||
Metadata: metadataJSON,
|
||||
})
|
||||
if err != nil {
|
||||
return GetResponse{}, fmt.Errorf("create provider: %w", err)
|
||||
@@ -119,6 +131,21 @@ func (s *Service) Update(ctx context.Context, id string, req UpdateRequest) (Get
|
||||
|
||||
apiKey := resolveUpdatedAPIKey(existing.ApiKey, req.APIKey)
|
||||
|
||||
clientType := existing.ClientType
|
||||
if req.ClientType != nil {
|
||||
clientType = *req.ClientType
|
||||
}
|
||||
|
||||
icon := existing.Icon
|
||||
if req.Icon != nil {
|
||||
icon = pgtype.Text{String: *req.Icon, Valid: *req.Icon != ""}
|
||||
}
|
||||
|
||||
enable := existing.Enable
|
||||
if req.Enable != nil {
|
||||
enable = *req.Enable
|
||||
}
|
||||
|
||||
metadata := existing.Metadata
|
||||
if req.Metadata != nil {
|
||||
metadataJSON, err := json.Marshal(req.Metadata)
|
||||
@@ -130,11 +157,14 @@ func (s *Service) Update(ctx context.Context, id string, req UpdateRequest) (Get
|
||||
|
||||
// Update provider
|
||||
updated, err := s.queries.UpdateLlmProvider(ctx, sqlc.UpdateLlmProviderParams{
|
||||
ID: providerID,
|
||||
Name: name,
|
||||
BaseUrl: baseURL,
|
||||
ApiKey: apiKey,
|
||||
Metadata: metadata,
|
||||
ID: providerID,
|
||||
Name: name,
|
||||
BaseUrl: baseURL,
|
||||
ApiKey: apiKey,
|
||||
ClientType: clientType,
|
||||
Icon: icon,
|
||||
Enable: enable,
|
||||
Metadata: metadata,
|
||||
})
|
||||
if err != nil {
|
||||
return GetResponse{}, fmt.Errorf("update provider: %w", err)
|
||||
@@ -182,8 +212,7 @@ func (s *Service) Test(ctx context.Context, id string) (TestResponse, error) {
|
||||
|
||||
baseURL := strings.TrimRight(provider.BaseUrl, "/")
|
||||
|
||||
// Determine client type from the first model using this provider.
|
||||
clientType := resolveProviderClientType(ctx, s.queries, providerID)
|
||||
clientType := models.ClientType(provider.ClientType)
|
||||
|
||||
sdkProvider := models.NewSDKProvider(baseURL, provider.ApiKey, clientType, probeTimeout)
|
||||
|
||||
@@ -198,18 +227,6 @@ func (s *Service) Test(ctx context.Context, id string) (TestResponse, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// resolveProviderClientType looks up models associated with the provider
|
||||
// and returns the first model's client_type. Falls back to openai-completions.
|
||||
func resolveProviderClientType(ctx context.Context, q *sqlc.Queries, providerID pgtype.UUID) models.ClientType {
|
||||
rows, err := q.ListModelsByProviderID(ctx, providerID)
|
||||
if err == nil && len(rows) > 0 {
|
||||
if ct := rows[0].ClientType; ct.Valid && ct.String != "" {
|
||||
return models.ClientType(ct.String)
|
||||
}
|
||||
}
|
||||
return models.ClientTypeOpenAICompletions
|
||||
}
|
||||
|
||||
// FetchRemoteModels fetches models from the provider's /v1/models endpoint.
|
||||
func (s *Service) FetchRemoteModels(ctx context.Context, id string) ([]RemoteModel, error) {
|
||||
providerID, err := db.ParseUUID(id)
|
||||
@@ -270,14 +287,22 @@ func (s *Service) toGetResponse(provider sqlc.LlmProvider) GetResponse {
|
||||
// Mask API key (show only first 8 characters)
|
||||
maskedAPIKey := maskAPIKey(provider.ApiKey)
|
||||
|
||||
var icon string
|
||||
if provider.Icon.Valid {
|
||||
icon = provider.Icon.String
|
||||
}
|
||||
|
||||
return GetResponse{
|
||||
ID: provider.ID.String(),
|
||||
Name: provider.Name,
|
||||
BaseURL: provider.BaseUrl,
|
||||
APIKey: maskedAPIKey,
|
||||
Metadata: metadata,
|
||||
CreatedAt: provider.CreatedAt.Time,
|
||||
UpdatedAt: provider.UpdatedAt.Time,
|
||||
ID: provider.ID.String(),
|
||||
Name: provider.Name,
|
||||
BaseURL: provider.BaseUrl,
|
||||
APIKey: maskedAPIKey,
|
||||
ClientType: provider.ClientType,
|
||||
Icon: icon,
|
||||
Enable: provider.Enable,
|
||||
Metadata: metadata,
|
||||
CreatedAt: provider.CreatedAt.Time,
|
||||
UpdatedAt: provider.UpdatedAt.Time,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+23
-20
@@ -4,29 +4,37 @@ import "time"
|
||||
|
||||
// CreateRequest represents a request to create a new LLM provider.
|
||||
type CreateRequest struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
BaseURL string `json:"base_url" validate:"required,url"`
|
||||
APIKey string `json:"api_key"` //nolint:gosec // intentional: LLM provider API key supplied by operator
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
BaseURL string `json:"base_url" validate:"required,url"`
|
||||
APIKey string `json:"api_key"` //nolint:gosec // intentional: LLM provider API key supplied by operator
|
||||
ClientType string `json:"client_type" validate:"required"`
|
||||
Icon string `json:"icon,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
// UpdateRequest represents a request to update an existing LLM provider.
|
||||
type UpdateRequest struct {
|
||||
Name *string `json:"name,omitempty"`
|
||||
BaseURL *string `json:"base_url,omitempty"`
|
||||
APIKey *string `json:"api_key,omitempty"` //nolint:gosec // intentional: LLM provider API key update field
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
BaseURL *string `json:"base_url,omitempty"`
|
||||
APIKey *string `json:"api_key,omitempty"` //nolint:gosec // intentional: LLM provider API key update field
|
||||
ClientType *string `json:"client_type,omitempty"`
|
||||
Icon *string `json:"icon,omitempty"`
|
||||
Enable *bool `json:"enable,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
// GetResponse represents the response for getting a provider.
|
||||
type GetResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
BaseURL string `json:"base_url"`
|
||||
APIKey string `json:"api_key,omitempty"` //nolint:gosec // intentional: partially masked API key for display
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
BaseURL string `json:"base_url"`
|
||||
APIKey string `json:"api_key,omitempty"` //nolint:gosec // intentional: partially masked API key for display
|
||||
ClientType string `json:"client_type"`
|
||||
Icon string `json:"icon,omitempty"`
|
||||
Enable bool `json:"enable"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// ListResponse represents the response for listing providers.
|
||||
@@ -61,11 +69,6 @@ type FetchModelsResponse struct {
|
||||
Data []RemoteModel `json:"data"`
|
||||
}
|
||||
|
||||
// ImportModelsRequest represents a request to import models from a provider.
|
||||
type ImportModelsRequest struct {
|
||||
ClientType string `json:"client_type"`
|
||||
}
|
||||
|
||||
// ImportModelsResponse represents the response for importing models.
|
||||
type ImportModelsResponse struct {
|
||||
Created int `json:"created"`
|
||||
|
||||
Reference in New Issue
Block a user