Add AI settings API with key management
GET/PATCH /api/v1/settings/ai return and change provider, base_url, model, temperature and system_prompt, with read-only api_key_last4 and key_verified_at. PATCH requires a preset provider, temperature 0-1 in 0.1 steps and a system_prompt of at most 8000 characters. PUT /settings/ai/api-key seals the key with secretbox and records its last four characters; POST /settings/ai/verify calls Provider.Verify and records key_verified_at or returns the provider's typed error; GET /settings/ai/models lists the live models; POST /settings/ai/reset-prompt restores household.DefaultPersonaPrompt. The new internal/ai/aisettings service also builds the harness.Config for each skill run from the saved settings. presets.ForAccount builds a provider's ListModels and Verify without a model, so a key can be checked and a model picked before one is chosen. Tests scan every response body from these routes for the full key, and a harness run with the Fake provider uses the PATCHed model and temperature. Maestro task: 176b2a30-6e90-4339-b430-5b8967b2fd32 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,8 +13,8 @@ import (
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/household"
|
||||
)
|
||||
|
||||
// maxSettingsBody bounds a PATCH /settings/household or
|
||||
// /settings/recipe-search request body.
|
||||
// maxSettingsBody bounds a PATCH /settings/household, /settings/ai or
|
||||
// /settings/recipe-search, or PUT /settings/ai/api-key, request body.
|
||||
const maxSettingsBody = 16 << 10
|
||||
|
||||
// settingsPatch is the PATCH /settings/household body. Every field is
|
||||
@@ -124,13 +124,18 @@ func writeJSON(w http.ResponseWriter, r *http.Request, logger *slog.Logger, stat
|
||||
|
||||
// writeError responds with status and a non-retryable API error.
|
||||
func writeError(w http.ResponseWriter, r *http.Request, logger *slog.Logger, status int, code, message string) {
|
||||
body, err := json.Marshal(errorBody{Error: apiError{Code: code, Message: message}})
|
||||
writeAPIError(w, r, logger, status, apiError{Code: code, Message: message})
|
||||
}
|
||||
|
||||
// writeAPIError responds with status and e.
|
||||
func writeAPIError(w http.ResponseWriter, r *http.Request, logger *slog.Logger, status int, e apiError) {
|
||||
body, err := json.Marshal(errorBody{Error: e})
|
||||
if err != nil {
|
||||
panic(err) // string and bool fields cannot fail to marshal
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if _, err := w.Write(body); err != nil {
|
||||
logger.WarnContext(r.Context(), "write error response", "code", code, "error", err)
|
||||
logger.WarnContext(r.Context(), "write error response", "code", e.Code, "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user