Rebased on top of upstream commit 95b57550d3 ("Fix curl direct use
is forbidden. Must use getUrlContent()") which replaced the raw
curl_* calls in this method with getURLContent(). The diagnostics
this patch adds are re-implemented on top of the new array shape
returned by getURLContent().
When the LLM API returns a non-JSON body, the previous code surfaced
only the generic message:
"Error: Invalid JSON response from API."
with no clue about what really happened. Common real-world causes
are: HTTP 4xx/5xx with empty body, HTML error page from a proxy,
gateway timeout, model not found, etc. None of these are
distinguishable from each other in the admin Log Viewer, so admins
had to re-run with curl by hand to diagnose.
This patch:
- Reads $result['http_code'] and $result['url'] (both populated by
getURLContent() via curl_getinfo()) and stores an enriched payload
in $this->lastResponse so the admin Log Viewer shows HTTP code +
effective URL + body length + body contents.
- Returns a descriptive error message when the body is not valid
JSON: includes the HTTP code, body length, and a 500-char body
snippet (with the '<empty>' placeholder for zero-length bodies).
- Adds the cURL error number ($result['curl_error_no']) alongside
the cURL error message in the network-level error message, with
the effective URL for context.
- Restores the LLM-specific response timeout that was lost in the
getURLContent() refactor: $this->timeout is now passed as the
10th argument ($timeoutresponse) so the configured value is
honored instead of falling back to MAIN_USE_RESPONSE_TIMEOUT
(default 30s). This also fixes the phpstan warning
"Property UniversalLLMAdapter::$timeout is never read, only
written." that the upstream refactor introduced.
No behavioral change for successful calls. Strictly improves the
error-path diagnostics surfaced to administrators, and restores
the per-LLM configurable timeout.
Tested with: a misconfigured Google Gemini URL that returns HTTP
404 with an empty body now produces:
Error: Invalid JSON response from API (HTTP 404, 0 bytes).
Body snippet: <empty>
instead of the previous opaque message.