feat(ai): implement robust JSON model parsing

Rewrote internal JSON parsing to correctly handle whitespace, escaped
quotes, and strict field context extraction. This prevents incorrect
field names or provider names from being added to the model list.

Added `ai_parse_models_from_json` public API to facilitate testing.

Changed `/ai models` default behavior to always fetch fresh models.
Replaced `--refresh` flag with `--cached` to display local cache.

Added comprehensive unit tests covering OpenAI and Perplexity formats,
array format, empty/null JSON, escaped quotes, and whitespace handling.
This commit is contained in:
2026-05-09 13:56:59 +00:00
parent 07d267b5bc
commit a898cb212d
6 changed files with 396 additions and 39 deletions

View File

@@ -168,6 +168,18 @@ gboolean ai_fetch_models(const gchar* provider_name, gpointer user_data);
*/
gboolean ai_models_are_fresh(const gchar* provider_name);
/* ========================================================================
* Model Parsing (for testing)
* ======================================================================== */
/**
* Parse model IDs from an OpenAI-compatible API response.
* Expected format: {"object":"list","data":[{"id":"model1",...},...]}
* @param provider The provider to add models to
* @param json The JSON response from the API
*/
void ai_parse_models_from_json(AIProvider* provider, const gchar* json);
/* ========================================================================
* Session Management
* ======================================================================== */