( ctx context.Context, tx database.Store, excludedConfigIDs ...uuid.UUID, )
| 7253 | } |
| 7254 | |
| 7255 | func ensureDefaultChatModelConfig( |
| 7256 | ctx context.Context, |
| 7257 | tx database.Store, |
| 7258 | excludedConfigIDs ...uuid.UUID, |
| 7259 | ) error { |
| 7260 | _, err := tx.GetDefaultChatModelConfig(ctx) |
| 7261 | switch { |
| 7262 | case err == nil: |
| 7263 | return nil |
| 7264 | case !xerrors.Is(err, sql.ErrNoRows): |
| 7265 | return xerrors.Errorf("get default model config: %w", err) |
| 7266 | } |
| 7267 | |
| 7268 | modelConfigs, err := tx.GetChatModelConfigs(ctx) |
| 7269 | if err != nil { |
| 7270 | return xerrors.Errorf("list chat model configs: %w", err) |
| 7271 | } |
| 7272 | if len(modelConfigs) == 0 { |
| 7273 | return nil |
| 7274 | } |
| 7275 | |
| 7276 | candidateConfig := modelConfigs[0] |
| 7277 | excluded := make(map[uuid.UUID]struct{}, len(excludedConfigIDs)) |
| 7278 | for _, configID := range excludedConfigIDs { |
| 7279 | if configID == uuid.Nil { |
| 7280 | continue |
| 7281 | } |
| 7282 | excluded[configID] = struct{}{} |
| 7283 | } |
| 7284 | for _, config := range modelConfigs { |
| 7285 | if _, skip := excluded[config.ID]; skip { |
| 7286 | continue |
| 7287 | } |
| 7288 | candidateConfig = config |
| 7289 | break |
| 7290 | } |
| 7291 | |
| 7292 | if err := tx.UnsetDefaultChatModelConfigs(ctx); err != nil { |
| 7293 | return xerrors.Errorf("unset default model configs: %w", err) |
| 7294 | } |
| 7295 | |
| 7296 | params := chatModelConfigToUpdateParams(candidateConfig) |
| 7297 | params.IsDefault = true |
| 7298 | if _, err := tx.UpdateChatModelConfig(ctx, params); err != nil { |
| 7299 | if xerrors.Is(err, sql.ErrNoRows) { |
| 7300 | // Do not wrap with %w. Callers map target misses to 404, but a |
| 7301 | // default-candidate race is an internal retryable failure. |
| 7302 | return xerrors.Errorf("set default model config: %v", err) |
| 7303 | } |
| 7304 | return xerrors.Errorf("set default model config: %w", err) |
| 7305 | } |
| 7306 | return nil |
| 7307 | } |
| 7308 | |
| 7309 | func chatModelConfigToUpdateParams( |
| 7310 | config database.ChatModelConfig, |
no test coverage detected