(ctx context.Context, name string, endpoint providers.Endpoint)
| 248 | } |
| 249 | |
| 250 | func (s Store) upsertProvider(ctx context.Context, name string, endpoint providers.Endpoint) error { |
| 251 | if err := s.Init(ctx); err != nil { |
| 252 | return err |
| 253 | } |
| 254 | models, err := json.Marshal(endpoint.Models) |
| 255 | if err != nil { |
| 256 | return fmt.Errorf("appstate: marshal models: %w", err) |
| 257 | } |
| 258 | var enabled any |
| 259 | if endpoint.Enabled != nil { |
| 260 | if *endpoint.Enabled { |
| 261 | enabled = 1 |
| 262 | } else { |
| 263 | enabled = 0 |
| 264 | } |
| 265 | } |
| 266 | db, err := s.open() |
| 267 | if err != nil { |
| 268 | return err |
| 269 | } |
| 270 | defer db.Close() |
| 271 | _, err = db.ExecContext(ctx, `INSERT INTO providers(name, endpoint, api_key, api_key_env, supported_client, list_models_cmd, models_json, keep_proxy_config, use_proxy, enabled, description, updated_at) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(name) DO UPDATE SET endpoint = excluded.endpoint, api_key = excluded.api_key, api_key_env = excluded.api_key_env, supported_client = excluded.supported_client, list_models_cmd = excluded.list_models_cmd, models_json = excluded.models_json, keep_proxy_config = excluded.keep_proxy_config, use_proxy = excluded.use_proxy, enabled = excluded.enabled, description = excluded.description, updated_at = excluded.updated_at`, name, endpoint.Endpoint, endpoint.APIKey, endpoint.APIKeyEnv, endpoint.SupportedClient, endpoint.ListModelsCmd, string(models), endpoint.KeepProxyConfig, endpoint.UseProxy, enabled, endpoint.Description, time.Now().UTC().Format(time.RFC3339Nano)) |
| 272 | if err != nil { |
| 273 | return fmt.Errorf("appstate: upsert provider %s: %w", name, err) |
| 274 | } |
| 275 | return nil |
| 276 | } |
| 277 | |
| 278 | func (s Store) open() (*sql.DB, error) { |
| 279 | if s.path == "" { |
no test coverage detected