feat(ai): support both responses and chat completions APIs

Drive both OpenAI-compatible flavours from one code path. Requests
default to /v1/responses and fall back once to /v1/chat/completions when
the provider reports the endpoint missing (404/405/501) or rejects the
payload shape (400/422); the working flavour is cached for the rest of
the run as a first-attempt hint, with the other flavour kept as a
fallback so a backend change self-corrects in-request. A per-provider
override is available via
  /ai set api-type <provider> responses|chat-completions|auto
and persisted as api_type= in the provider section.

One payload builder serves both flavours (messages/stream vs
input/stream/store); history and custom settings are serialized once per
request and the per-flavour envelope is assembled per attempt under a
single lock acquisition, so the fallback retry cannot double-count the
prompt. ai_parse_response_typed() dispatches on the request flavour and
splits extraction per envelope: chat completions anchored on
choices[].message.content, responses on the output_text part with a
string-aware backward scan bounded to that part's own object so a
reasoning summary or a truncated body is never returned as the reply.

Endpoint detection classifies wrong-model errors from the structured
error code/type/message when present, so route-missing 404s from
gateways no longer suppress the fallback; it preserves and surfaces the
first payload-rejection error when the fallback also fails, and re-probes
on an unparseable 2xx. resolved_api_type is written only through a locked
helper guarded by a URL/api-type epoch, and api_url is snapshotted under
settings_lock in the request and models-fetch threads, closing
use-after-free and stale-cache races against /ai set provider. The
models-fetch provider ref is taken on the main thread and released on
every worker exit path. Reserved custom-setting keys are extended with
input (store stays writable as a legitimate chat-completions parameter).
This commit is contained in:
2026-07-06 16:28:36 +03:00
parent 5d7b7bb23d
commit 14f76ab1db
10 changed files with 858 additions and 123 deletions

View File

@@ -186,11 +186,13 @@ ai_json_escape(const gchar* str)
return g_string_free(result, FALSE);
}
/* Keys emitted as fixed payload fields; custom settings must not duplicate them */
/* Keys emitted as fixed payload fields (in either API flavour); custom
* settings must not duplicate them */
static gboolean
_is_reserved_payload_key(const gchar* key)
{
return g_strcmp0(key, "model") == 0 || g_strcmp0(key, "messages") == 0 || g_strcmp0(key, "stream") == 0;
return g_strcmp0(key, "model") == 0 || g_strcmp0(key, "messages") == 0
|| g_strcmp0(key, "input") == 0 || g_strcmp0(key, "stream") == 0;
}
/* RFC 8259 scalars (number/true/false/null) may be emitted bare in a JSON payload */
@@ -199,7 +201,16 @@ _is_json_bare_token(const gchar* value)
{
if (g_strcmp0(value, "true") == 0 || g_strcmp0(value, "false") == 0 || g_strcmp0(value, "null") == 0)
return TRUE;
return g_regex_match_simple("^-?(0|[1-9][0-9]*)(\\.[0-9]+)?([eE][+-]?[0-9]+)?$", value, 0, 0);
/* Compile the number grammar once; this runs per custom setting per request */
static gsize once = 0;
static GRegex* number_re = NULL;
if (g_once_init_enter(&once)) {
number_re = g_regex_new("^-?(0|[1-9][0-9]*)(\\.[0-9]+)?([eE][+-]?[0-9]+)?$",
G_REGEX_OPTIMIZE, 0, NULL);
g_once_init_leave(&once, 1);
}
return number_re && g_regex_match(number_re, value, 0, NULL);
}
/**
@@ -392,6 +403,135 @@ _extract_json_string(const gchar* json, const gchar* field)
return NULL;
}
/* ========================================================================
* API type helpers
* ======================================================================== */
gboolean
ai_api_type_from_string(const gchar* str, AIApiType* type)
{
if (!str || !type)
return FALSE;
if (g_strcmp0(str, "auto") == 0) {
*type = AI_API_TYPE_AUTO;
} else if (g_strcmp0(str, "responses") == 0) {
*type = AI_API_TYPE_RESPONSES;
} else if (g_strcmp0(str, "chat-completions") == 0) {
*type = AI_API_TYPE_CHAT_COMPLETIONS;
} else {
return FALSE;
}
return TRUE;
}
const gchar*
ai_api_type_to_string(AIApiType type)
{
switch (type) {
case AI_API_TYPE_RESPONSES:
return "responses";
case AI_API_TYPE_CHAT_COMPLETIONS:
return "chat-completions";
default:
return "auto";
}
}
static const gchar*
_api_type_endpoint(AIApiType type)
{
return type == AI_API_TYPE_CHAT_COMPLETIONS ? "v1/chat/completions" : "v1/responses";
}
static AIApiType
_other_flavour(AIApiType type)
{
return type == AI_API_TYPE_CHAT_COMPLETIONS ? AI_API_TYPE_RESPONSES : AI_API_TYPE_CHAT_COMPLETIONS;
}
static gboolean
_names_model(const gchar* s)
{
if (!s)
return FALSE;
/* "does not exist" alone also appears in route-missing bodies; require it
* to talk about a model */
return strstr(s, "model_not_found") || strstr(s, "invalid_model")
|| strstr(s, "unknown_model") || strstr(s, "model not found")
|| (strstr(s, "model") && strstr(s, "does not exist"));
}
/* A missing endpoint answers 404/405/501. A wrong model can also 404/405, but
* the error names the model (OpenAI code model_not_found, vLLM message "The
* model `x` does not exist"), so treat the endpoint as present then. Prefer
* the structured error fields; fall back to the raw body for servers that
* send no JSON error envelope. */
static gboolean
_body_names_model_error(const gchar* body)
{
if (!body)
return FALSE;
const gchar* err = strstr(body, "\"error\"");
if (err) {
auto_gchar gchar* code = _extract_json_string(err, "code");
auto_gchar gchar* type = _extract_json_string(err, "type");
auto_gchar gchar* message = _extract_json_string(err, "message");
if (code || type || message)
return _names_model(code) || _names_model(type) || _names_model(message);
}
return _names_model(body);
}
static gboolean
_is_endpoint_missing(long http_code, const gchar* body)
{
if (http_code != 404 && http_code != 405 && http_code != 501)
return FALSE;
return !_body_names_model_error(body);
}
/* During an AUTO probe, move on to the other flavour when this endpoint is
* missing or rejects the payload shape (400/422) — but not on auth/rate-limit
* or model errors, which the other endpoint would fail the same way. */
static gboolean
_should_try_other_flavour(long http_code, const gchar* body)
{
if (_is_endpoint_missing(http_code, body))
return TRUE;
if (http_code != 400 && http_code != 422)
return FALSE;
return !_body_names_model_error(body);
}
/* Copy the provider URL under settings_lock, slash-terminated; @epoch_out
* (optional) captures api_epoch in the same critical section so a flavour
* stamped later can be tied to the URL it was probed against */
static gchar*
_provider_base_url(AIProvider* provider, gint* epoch_out)
{
pthread_mutex_lock(&provider->settings_lock);
gchar* base = g_strdup_printf("%s%s", provider->api_url,
g_str_has_suffix(provider->api_url, "/") ? "" : "/");
if (epoch_out)
*epoch_out = g_atomic_int_get(&provider->api_epoch);
pthread_mutex_unlock(&provider->settings_lock);
return base;
}
/* Single writer for resolved_api_type: takes settings_lock and, when
* @expected_epoch >= 0, drops the write if the epoch moved (the URL or
* api-type changed while the request was in flight) */
static void
_provider_set_resolved(AIProvider* provider, AIApiType type, gint expected_epoch)
{
pthread_mutex_lock(&provider->settings_lock);
if (expected_epoch < 0 || g_atomic_int_get(&provider->api_epoch) == expected_epoch)
g_atomic_int_set(&provider->resolved_api_type, type);
pthread_mutex_unlock(&provider->settings_lock);
}
/* ========================================================================
* Provider Management
* ======================================================================== */
@@ -404,6 +544,9 @@ ai_provider_new(const gchar* name, const gchar* api_url)
provider->api_url = g_strdup(api_url ? api_url : "");
provider->project_id = NULL;
provider->default_model = NULL;
provider->api_type = AI_API_TYPE_AUTO;
provider->resolved_api_type = AI_API_TYPE_AUTO;
provider->api_epoch = 0;
provider->settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
provider->models = NULL;
provider->models_fresh = FALSE;
@@ -519,6 +662,12 @@ ai_client_init(void)
provider->default_model = g_strdup(dm);
}
auto_gchar gchar* at = prefs_ai_get_api_type(provider->name);
AIApiType api_type;
if (at && ai_api_type_from_string(at, &api_type)) {
provider->api_type = api_type;
}
GList* settings = prefs_ai_list_settings(provider->name);
for (GList* s = settings; s; s = g_list_next(s)) {
const gchar* name = (const gchar*)s->data;
@@ -573,9 +722,15 @@ _ai_add_provider_nopersist(const gchar* name, const gchar* api_url)
/* Check if provider already exists */
AIProvider* existing = g_hash_table_lookup(providers, name);
if (existing) {
pthread_mutex_lock(&existing->settings_lock);
g_free(existing->api_url);
existing->api_url = g_strdup(api_url);
return ai_provider_ref(existing);
g_atomic_int_inc(&existing->api_epoch);
/* New URL may serve a different API flavour; reset under the lock so a
* request thread cannot re-stamp the stale flavour for the new URL */
g_atomic_int_set(&existing->resolved_api_type, AI_API_TYPE_AUTO);
pthread_mutex_unlock(&existing->settings_lock);
return existing; /* borrowed, same as the create path below */
}
/* Create new provider (ref_count=1 owned by hash table) */
@@ -598,25 +753,18 @@ ai_add_provider(const gchar* name, const gchar* api_url)
ai_client_init();
}
/* Check if provider already exists */
AIProvider* existing = g_hash_table_lookup(providers, name);
if (existing) {
/* Update existing provider */
g_free(existing->api_url);
existing->api_url = g_strdup(api_url);
/* Persist the updated URL to config */
prefs_ai_set_provider(name, api_url);
log_info("Updated provider: %s", name);
return ai_provider_ref(existing);
}
gboolean existed = g_hash_table_lookup(providers, name) != NULL;
/* Persist the new provider to config */
/* Persist first, then reuse the internal helper for both the update and
* create paths (it handles the locked URL swap or the fresh insert) */
prefs_ai_set_provider(name, api_url);
/* Add to hash table (reuse internal helper) */
AIProvider* provider = _ai_add_provider_nopersist(name, api_url);
log_info("Added provider: %s (URL: %s)", name, api_url);
if (existed) {
log_info("Updated provider: %s", name);
} else {
log_info("Added provider: %s (URL: %s)", name, api_url);
}
return provider; /* Caller gets non-owning pointer; hash table owns ref */
}
@@ -747,6 +895,45 @@ ai_get_provider_default_model(const gchar* provider_name)
return provider->default_model;
}
gboolean
ai_set_provider_api_type(const gchar* provider_name, const gchar* api_type)
{
AIApiType type;
if (!provider_name || !ai_api_type_from_string(api_type, &type))
return FALSE;
AIProvider* provider = ai_get_provider(provider_name);
if (!provider) {
log_warning("Provider '%s' not found for API type setting", provider_name);
return FALSE;
}
/* Same protocol as a URL swap: bump the epoch under the lock so an
* in-flight request cannot re-stamp resolved_api_type after this reset */
pthread_mutex_lock(&provider->settings_lock);
g_atomic_int_set(&provider->api_type, type);
g_atomic_int_inc(&provider->api_epoch);
g_atomic_int_set(&provider->resolved_api_type, AI_API_TYPE_AUTO);
pthread_mutex_unlock(&provider->settings_lock);
if (type == AI_API_TYPE_AUTO) {
prefs_ai_remove_api_type(provider_name);
} else {
prefs_ai_set_api_type(provider_name, api_type);
}
log_info("API type for provider '%s' set to: %s", provider_name, ai_api_type_to_string(type));
return TRUE;
}
AIApiType
ai_get_provider_api_type(const gchar* provider_name)
{
AIProvider* provider = provider_name ? ai_get_provider(provider_name) : NULL;
if (!provider)
return AI_API_TYPE_AUTO;
return g_atomic_int_get(&provider->api_type);
}
gboolean
ai_set_provider_setting(const gchar* provider_name, const gchar* setting, const gchar* value)
{
@@ -812,6 +999,7 @@ typedef void (*ai_response_handler_t)(AIProvider* provider, const gchar* respons
typedef struct
{
gchar* provider_name;
AIProvider* provider; /* owned ref taken on the main thread, or NULL */
gpointer user_data;
gchar* request_url;
ai_response_handler_t handler;
@@ -874,22 +1062,27 @@ static gpointer
_ai_generic_request_thread(gpointer data)
{
ai_request_t* req = (ai_request_t*)data;
AIProvider* provider = ai_get_provider(req->provider_name);
if (!provider) {
log_error("Provider '%s' not found for request", req->provider_name);
g_free(req->provider_name);
g_free(req->request_url);
g_free(req);
return NULL;
}
/* Keep the provider alive for the duration of this request. Without this, /ai remove provider X could free the provider
* struct (via the hash table's destroy-notify) while curl_easy_perform is still using it — classic UAF. */
ai_provider_ref(provider);
/* Keep the provider alive for the duration of this request. Prefer the ref
* taken on the main thread (guaranteed pre-removal); the lookup fallback
* still has a small window against /ai remove provider. */
AIProvider* provider = g_steal_pointer(&req->provider);
if (!provider) {
provider = ai_get_provider(req->provider_name);
if (!provider) {
log_error("Provider '%s' not found for request", req->provider_name);
g_free(req->provider_name);
g_free(req->request_url);
g_free(req);
return NULL;
}
ai_provider_ref(provider);
}
CURL* curl = curl_easy_init();
if (!curl) {
log_error("Failed to initialize curl for %s", req->provider_name);
ai_provider_unref(provider);
g_free(req->provider_name);
g_free(req->request_url);
g_free(req);
@@ -901,6 +1094,7 @@ _ai_generic_request_thread(gpointer data)
auto_gchar gchar* error_msg = g_strdup_printf("No API key set for provider '%s'.", req->provider_name);
_aiwin_display_error(req->user_data, error_msg);
curl_easy_cleanup(curl);
ai_provider_unref(provider);
g_free(req->provider_name);
g_free(req->request_url);
g_free(req);
@@ -1247,19 +1441,11 @@ static gpointer
_models_fetch_thread(gpointer data)
{
ai_request_t* req = (ai_request_t*)data;
AIProvider* provider = ai_get_provider(req->provider_name);
if (!provider) {
g_free(req->provider_name);
g_free(req);
return NULL;
}
/* Build URL for listing models */
auto_gchar gchar* base_url = g_strdup_printf("%s%s", provider->api_url,
g_str_has_suffix(provider->api_url, "/") ? "" : "/");
auto_gchar gchar* request_url = g_strdup_printf("%sv1/models", base_url);
req->request_url = g_strdup(request_url);
/* req->provider carries a ref taken on the main thread, so the provider
* cannot be freed under us; the locked snapshot guards the URL swap */
auto_gchar gchar* base_url = _provider_base_url(req->provider, NULL);
req->request_url = g_strdup_printf("%sv1/models", base_url);
req->handler = _models_response_handler;
return _ai_generic_request_thread(req);
@@ -1277,13 +1463,17 @@ ai_fetch_models(const gchar* provider_name, gpointer user_data)
return FALSE;
}
/* Always fetch fresh models from API */
/* Always fetch fresh models from API. Ref the provider on this thread,
* while it is guaranteed alive, so /ai remove provider cannot free it
* under the worker; the worker's request struct owns the ref. */
ai_request_t* req = g_new0(ai_request_t, 1);
req->provider_name = g_strdup(provider_name);
req->provider = ai_provider_ref(provider);
req->user_data = user_data;
GThread* thread = g_thread_new("ai_models_fetch", _models_fetch_thread, req);
if (!thread) {
ai_provider_unref(req->provider);
g_free(req->provider_name);
g_free(req);
log_error("Failed to create thread for model fetch");
@@ -1464,28 +1654,20 @@ ai_session_switch(AISession* session, const gchar* provider_name,
* ======================================================================== */
/**
* Build JSON payload from a GList of AIMessage nodes and a prompt string.
* This is the thread-safe variant used by _ai_request_thread() which holds
* the session lock while calling it.
* Serialize history plus the new user prompt as a JSON message-object list
* (without the enclosing brackets); the fragment is shared by both API
* flavours. Must be called with the session lock held.
*
* Custom provider settings are merged into the payload as additional JSON
* key-value pairs alongside "model" and "messages": scalar values
* (number/true/false/null) are emitted bare, anything else as a JSON string;
* reserved keys (model/messages/stream) are skipped.
*
* @param provider The AI provider (for custom settings)
* @param model The model identifier
* @param history GList of AIMessage* (must be held under session lock)
* @param prompt The new user prompt to append
* @return Newly allocated JSON string (caller must free)
* @return Newly allocated fragment (caller must free)
*/
static gchar*
_build_json_payload_from_list(AIProvider* provider, const gchar* model, GList* history, const gchar* prompt)
_serialize_message_list(GList* history, const gchar* prompt)
{
GString* messages_json = g_string_new("");
GList* curr = history;
while (curr) {
for (GList* curr = history; curr; curr = g_list_next(curr)) {
AIMessage* msg = curr->data;
auto_gchar gchar* escaped_content = ai_json_escape(msg->content);
auto_gchar gchar* escaped_role = ai_json_escape(msg->role);
@@ -1495,8 +1677,6 @@ _build_json_payload_from_list(AIProvider* provider, const gchar* model, GList* h
}
g_string_append_printf(messages_json, "{\"role\":\"%s\",\"content\":\"%s\"}",
escaped_role, escaped_content);
curr = g_list_next(curr);
}
/* Add the new user message */
@@ -1506,11 +1686,24 @@ _build_json_payload_from_list(AIProvider* provider, const gchar* model, GList* h
}
g_string_append_printf(messages_json, "{\"role\":\"user\",\"content\":\"%s\"}", escaped_prompt);
auto_gchar gchar* escaped_model = ai_json_escape(model);
return g_string_free(messages_json, FALSE);
}
/* Build custom settings JSON fragment; settings is mutated on the main
* thread (/ai set custom), so iterate under settings_lock */
/**
* Serialize custom provider settings as a JSON key-value fragment: scalar
* values (number/true/false/null) are emitted bare, anything else as a JSON
* string; reserved keys (model/messages/input/stream) are skipped.
*
* @param provider The AI provider (may be NULL)
* @param store_set Output: set TRUE when a custom "store" value is present
* @return Newly allocated fragment, empty string when there are no settings
*/
static gchar*
_serialize_custom_settings(AIProvider* provider, gboolean* store_set)
{
GString* custom_json = g_string_new("");
*store_set = FALSE;
if (provider) {
pthread_mutex_lock(&provider->settings_lock);
if (provider->settings) {
@@ -1521,6 +1714,9 @@ _build_json_payload_from_list(AIProvider* provider, const gchar* model, GList* h
if (_is_reserved_payload_key((gchar*)key)) {
continue; /* fixed payload fields stay authoritative */
}
if (g_strcmp0((gchar*)key, "store") == 0) {
*store_set = TRUE; /* suppresses the fixed "store":false */
}
auto_gchar gchar* escaped_key = ai_json_escape((gchar*)key);
if (custom_json->len > 0) {
g_string_append_c(custom_json, ',');
@@ -1537,31 +1733,141 @@ _build_json_payload_from_list(AIProvider* provider, const gchar* model, GList* h
pthread_mutex_unlock(&provider->settings_lock);
}
/* Assemble final payload: model, messages, custom settings, then fixed keys */
gchar* json_payload;
if (custom_json->len > 0) {
json_payload = g_strdup_printf(
"{\"model\":\"%s\",\"messages\":[%s],%s,\"stream\":false}",
escaped_model, messages_json->str, custom_json->str);
} else {
json_payload = g_strdup_printf(
"{\"model\":\"%s\",\"messages\":[%s],\"stream\":false}",
escaped_model, messages_json->str);
return g_string_free(custom_json, FALSE);
}
/**
* Assemble the payload for one API flavour from pre-serialized fragments:
* model, message list, custom settings, then fixed keys. Chat completions
* uses the "messages" list key; responses uses "input" plus "store":false
* unless a custom "store" setting overrides it.
*
* @param model The model identifier
* @param messages_json Fragment from _serialize_message_list()
* @param custom_json Fragment from _serialize_custom_settings()
* @param store_set Whether custom_json already carries a "store" value
* @param api_type The API flavour to build for (must not be AUTO)
* @return Newly allocated JSON string (caller must free)
*/
static gchar*
_assemble_json_payload(const gchar* model, const gchar* messages_json, const gchar* custom_json, gboolean store_set, AIApiType api_type)
{
auto_gchar gchar* escaped_model = ai_json_escape(model);
const gchar* list_key = api_type == AI_API_TYPE_CHAT_COMPLETIONS ? "messages" : "input";
const gchar* fixed_tail = (api_type == AI_API_TYPE_CHAT_COMPLETIONS || store_set)
? "\"stream\":false"
: "\"stream\":false,\"store\":false";
return g_strdup_printf("{\"model\":\"%s\",\"%s\":[%s],%s%s%s}",
escaped_model, list_key, messages_json,
custom_json, *custom_json ? "," : "", fixed_tail);
}
/* Chat completions: {"choices":[{"message":{"content":"..."}}]}. Anchor on
* "message" so a non-string "content" ordered earlier in the choice (e.g.
* "logprobs":{"content":[...]}) is not mistaken for the reply. */
static gchar*
_parse_chat_completions(const gchar* json)
{
const gchar* choices = strstr(json, "\"choices\"");
if (!choices)
return NULL;
const gchar* message = strstr(choices, "\"message\"");
return _extract_json_string(message ? message : choices, "content");
}
/* TRUE when no object boundary ({ or }) lies between @from and @to outside of
* string literals — i.e. both positions belong to the same JSON object */
static gboolean
_same_json_object(const gchar* from, const gchar* to)
{
gboolean in_string = FALSE;
for (const gchar* p = from; p < to && *p; p++) {
if (in_string) {
if (*p == '\\' && *(p + 1) != '\0')
p++;
else if (*p == '"')
in_string = FALSE;
} else if (*p == '"') {
in_string = TRUE;
} else if (*p == '{' || *p == '}') {
return FALSE;
}
}
return TRUE;
}
/* Responses: {"output":[...,{"content":[{"type":"output_text","text":"..."}]}]}.
* "content" is an array here, so extract the inner "text" of the output_text
* part; reasoning items carry their own "text" (summary) ahead of it. */
static gchar*
_parse_responses(const gchar* json)
{
const gchar* output = strstr(json, "\"output\"");
if (!output)
return NULL;
const gchar* part = strstr(output, "\"output_text\"");
if (part) {
gchar* text = _extract_json_string(part, "text");
if (text)
return text;
/* "text" may precede "type":"output_text" in the part: take the
* nearest preceding "text" that shares the part's object, so an
* earlier item's text (a reasoning summary) is never returned; skip
* matches where extraction fails (e.g. a string value equal to "text") */
gsize window = part - output;
while (window > 0) {
const gchar* key = g_strrstr_len(output, window, "\"text\"");
if (!key)
break;
if (_same_json_object(key, part)) {
text = _extract_json_string(key, "text");
if (text)
return text;
}
window = key - output;
}
return NULL;
}
g_string_free(messages_json, TRUE);
g_string_free(custom_json, TRUE);
return json_payload;
/* No output_text tag (truncated reply, or a server tagging the part
* "type":"text"): anchor on the message "content" array so a reasoning
* "summary" text is not returned as the reply */
const gchar* content_arr = strstr(output, "\"content\"");
return _extract_json_string(content_arr ? content_arr : output, "text");
}
/* Extract assistant content, trying the known request flavour's extractor
* first (AUTO means chat-completions first), then the other flavour, then a
* bare top-level "content" string. Each extractor runs at most once. */
gchar*
ai_parse_response_typed(AIApiType api_type, const gchar* response_json)
{
if (!response_json || strlen(response_json) == 0)
return NULL;
gchar* out;
if (api_type == AI_API_TYPE_RESPONSES) {
out = _parse_responses(response_json);
if (!out)
out = _parse_chat_completions(response_json);
} else {
out = _parse_chat_completions(response_json);
if (!out)
out = _parse_responses(response_json);
}
if (out)
return out;
/* No recognizable envelope: accept a bare top-level "content" string */
return _extract_json_string(response_json, "content");
}
gchar*
ai_parse_response(const gchar* response_json)
{
if (!response_json || strlen(response_json) == 0)
return NULL;
/* OpenAI /v1/chat/completions: {"choices":[{"message":{"content":"..."}}]} */
return _extract_json_string(response_json, "content");
return ai_parse_response_typed(AI_API_TYPE_AUTO, response_json);
}
/* Extract the human-readable error.message from a provider error envelope:
@@ -1636,10 +1942,11 @@ _ai_request_thread(gpointer data)
return NULL;
}
/* Build JSON payload from history + prompt (under lock), then add user message to history */
/* Serialize the payload fragments once from a single history snapshot
* (under lock); per-flavour envelopes are assembled cheaply per attempt */
log_debug("[AI-THREAD] Building JSON payload...");
pthread_mutex_lock(&session->lock);
auto_gchar gchar* json_payload = _build_json_payload_from_list(local_provider, local_model, session->history, prompt);
auto_gchar gchar* messages_json = _serialize_message_list(session->history, prompt);
/* Add user message to history now (it's in JSON but not yet in history) */
AIMessage* user_msg = g_new0(AIMessage, 1);
@@ -1648,57 +1955,122 @@ _ai_request_thread(gpointer data)
session->history = g_list_append(session->history, user_msg);
pthread_mutex_unlock(&session->lock);
log_debug("[AI-THREAD] Added user message to history");
log_debug("[AI-THREAD] JSON payload: %s", json_payload);
gboolean store_set = FALSE;
auto_gchar gchar* custom_json = _serialize_custom_settings(local_provider, &store_set);
/* Build headers using shared helper */
struct curl_slist* headers = _build_curl_headers(local_provider, local_api_key);
/* Response buffer */
/* Response buffer (allocated per attempt inside the loop) */
struct curl_response_t response;
response.data = g_new0(gchar, 1);
response.data = NULL;
response.size = 0;
/* Build request URL */
const gchar* api_url = local_provider->api_url;
auto_gchar gchar* request_url = g_strdup_printf("%s%sv1/chat/completions", api_url, g_str_has_suffix(api_url, "/") ? "" : "/");
log_debug("[AI-THREAD] API URL: %s", api_url);
log_debug("[AI-THREAD] API Request URL: %s", request_url);
/* Snapshot the URL together with the epoch, so a flavour stamped after
* this request cannot be attributed to a URL it was not probed against */
gint api_epoch = 0;
auto_gchar gchar* base_url = _provider_base_url(local_provider, &api_epoch);
/* Flavour(s) to try: explicit setting wins; a previously detected flavour
* is a hint that keeps the other flavour as a fallback attempt (so a
* backend change self-corrects in-request); else probe responses first. */
AIApiType configured = g_atomic_int_get(&local_provider->api_type);
AIApiType attempts[2];
gint n_attempts = 0;
if (configured != AI_API_TYPE_AUTO) {
attempts[n_attempts++] = configured;
} else {
AIApiType resolved = g_atomic_int_get(&local_provider->resolved_api_type);
AIApiType first = resolved != AI_API_TYPE_AUTO ? resolved : AI_API_TYPE_RESPONSES;
attempts[n_attempts++] = first;
attempts[n_attempts++] = _other_flavour(first);
}
log_debug("[AI-THREAD] API URL: %s", base_url);
log_debug("[AI-THREAD] Model: %s", local_model);
/* Set URL and execute request */
curl_easy_setopt(curl, CURLOPT_URL, request_url);
/* Per-request options shared by all attempts */
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_payload);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60L);
CURLcode res = curl_easy_perform(curl);
log_debug("[AI-THREAD] curl_easy_perform completed, res: %d", res);
/* Get HTTP response code */
CURLcode res = CURLE_OK;
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
log_debug("[AI-THREAD] HTTP response code: %ld", http_code);
AIApiType used_type = attempts[0];
/* A payload rejection (400/422) that triggered the fallback is the
* actionable error; keep it in case the other flavour also fails */
auto_gchar gchar* first_reject_body = NULL;
long first_reject_code = 0;
for (gint i = 0; i < n_attempts; i++) {
used_type = attempts[i];
auto_gchar gchar* request_url = g_strdup_printf("%s%s", base_url, _api_type_endpoint(used_type));
auto_gchar gchar* json_payload = _assemble_json_payload(local_model, messages_json, custom_json, store_set, used_type);
log_debug("[AI-THREAD] API Request URL: %s", request_url);
log_debug("[AI-THREAD] JSON payload: %s", json_payload);
/* Reset the response buffer for this attempt */
g_free(response.data);
response.data = g_new0(gchar, 1);
response.size = 0;
curl_easy_setopt(curl, CURLOPT_URL, request_url);
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, json_payload);
res = curl_easy_perform(curl);
log_debug("[AI-THREAD] curl_easy_perform completed, res: %d", res);
http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
log_debug("[AI-THREAD] HTTP response code: %ld", http_code);
if (res == CURLE_OK && i + 1 < n_attempts && _should_try_other_flavour(http_code, response.data)) {
log_info("[AI-THREAD] %s endpoint rejected request for '%s' (HTTP %ld), falling back to %s",
ai_api_type_to_string(used_type), local_provider_name, http_code,
ai_api_type_to_string(attempts[i + 1]));
log_debug("[AI-THREAD] Rejected %s response body: %s",
ai_api_type_to_string(used_type), response.data);
if (!first_reject_body && !_is_endpoint_missing(http_code, response.data)) {
first_reject_body = g_strdup(response.data);
first_reject_code = http_code;
}
continue;
}
break;
}
auto_gchar gchar* response_data = g_steal_pointer(&response.data);
if (configured == AI_API_TYPE_AUTO && res == CURLE_OK && _is_endpoint_missing(http_code, response_data)) {
/* The flavour we tried is no longer served; re-probe on the next request */
_provider_set_resolved(local_provider, AI_API_TYPE_AUTO, api_epoch);
}
if (res != CURLE_OK) {
auto_gchar gchar* error_msg = g_strdup(curl_easy_strerror(res));
log_error("AI request failed for %s/%s: %s", local_provider_name, local_model, error_msg);
_aiwin_display_error(user_data, error_msg);
} else if (http_code >= 400) {
/* Handle HTTP errors */
/* Handle HTTP errors; a payload rejection that triggered the fallback
* is more actionable than the other endpoint's follow-up failure */
const gchar* err_body = first_reject_body ? first_reject_body : response_data;
long err_code = first_reject_body ? first_reject_code : http_code;
log_debug("[AI-THREAD] HTTP error response body (%zu bytes): %s",
response.size, response_data);
/* Try to extract the actual error message from the JSON response */
auto_gchar gchar* parsed_error = ai_parse_error_message(response_data);
auto_gchar gchar* error_msg = parsed_error ? g_strdup_printf("HTTP %ld: %s", http_code, parsed_error) : g_strdup_printf("HTTP %ld: %s", http_code, response_data && strlen(response_data) > 0 ? response_data : "Unknown error");
auto_gchar gchar* parsed_error = ai_parse_error_message(err_body);
auto_gchar gchar* error_msg = parsed_error ? g_strdup_printf("HTTP %ld: %s", err_code, parsed_error) : g_strdup_printf("HTTP %ld: %s", err_code, err_body && strlen(err_body) > 0 ? err_body : "Unknown error");
log_error("AI request failed for %s/%s: %s", local_provider_name, local_model, error_msg);
_aiwin_display_error(user_data, error_msg);
} else {
log_debug("[AI-THREAD] Raw API response (%zu bytes): %s", response.size, response_data);
auto_gchar gchar* content = ai_parse_response(response_data);
auto_gchar gchar* content = ai_parse_response_typed(used_type, response_data);
if (content) {
if (configured == AI_API_TYPE_AUTO && http_code >= 200 && http_code < 300) {
/* Remember the flavour that worked so later requests try it
* first; the epoch check discards the stamp if the URL or
* api-type changed while this request was in flight */
_provider_set_resolved(local_provider, used_type, api_epoch);
}
/* Add assistant response to history (under lock) */
pthread_mutex_lock(&session->lock);
AIMessage* assistant_msg = g_new0(AIMessage, 1);
@@ -1708,6 +2080,10 @@ _ai_request_thread(gpointer data)
pthread_mutex_unlock(&session->lock);
_aiwin_display_response(user_data, content);
} else {
/* A 2xx we cannot parse means the cached flavour may be wrong; drop
* it so the next request re-probes instead of wedging on it */
if (configured == AI_API_TYPE_AUTO)
_provider_set_resolved(local_provider, AI_API_TYPE_AUTO, api_epoch);
log_error("AI response parse failed for %s/%s: %.200s...",
local_provider_name, local_model, response_data);
_aiwin_display_error(user_data, "Failed to parse AI response.");