feat: consolidate docs, backend/session infra, and settings updates

This commit is contained in:
Dwindi Ramadhana
2026-05-28 00:58:20 +07:00
parent 2424acf726
commit 44e06eed88
102 changed files with 35423 additions and 11181 deletions

View File

@@ -95,6 +95,7 @@ class WP_Agentic_Writer_Local_Backend_Provider implements WP_Agentic_Writer_AI_P
$code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $code ) {
$body = wp_remote_retrieve_body( $response );
error_log( '[WPAW] Local backend HTTP error: ' . $code . ', body: ' . substr( $body, 0, 500 ) );
return new WP_Error(
'api_error',
sprintf(
@@ -108,7 +109,10 @@ class WP_Agentic_Writer_Local_Backend_Provider implements WP_Agentic_Writer_AI_P
$body = json_decode( wp_remote_retrieve_body( $response ), true );
error_log( '[WPAW] Local backend response keys: ' . implode( ', ', is_array( $body ) ? array_keys( $body ) : array( 'not_array' ) ) );
if ( ! isset( $body['choices'][0]['message']['content'] ) ) {
error_log( '[WPAW] Local backend response: ' . wp_json_encode( $body ) );
return new WP_Error(
'invalid_response',
__( 'Invalid response format from Local Backend', 'wp-agentic-writer' )
@@ -166,6 +170,23 @@ class WP_Agentic_Writer_Local_Backend_Provider implements WP_Agentic_Writer_AI_P
$ch = curl_init( $this->base_url . '/v1/messages' );
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $this->api_key,
);
// Add search headers if web search is enabled
if ( ! empty( $options['web_search_enabled'] ) ) {
$headers[] = 'X-Search-Enabled: true';
// Extract last user message as search query
foreach ( array_reverse( $messages ) as $msg ) {
if ( 'user' === $msg['role'] ) {
$headers[] = 'X-Search-Query: ' . substr( $msg['content'], 0, 500 );
break;
}
}
}
curl_setopt_array( $ch, array(
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => false,
@@ -184,7 +205,7 @@ class WP_Agentic_Writer_Local_Backend_Provider implements WP_Agentic_Writer_AI_P
$buffer = substr( $buffer, $newline_pos + 1 );
$line = trim( $line );
if ( empty( $line ) || ! str_starts_with( $line, 'data: ' ) ) {
if ( empty( $line ) || 0 !== strpos( $line, 'data: ' ) ) {
continue;
}
@@ -213,10 +234,7 @@ class WP_Agentic_Writer_Local_Backend_Provider implements WP_Agentic_Writer_AI_P
return strlen( $data );
},
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer ' . $this->api_key,
),
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => wp_json_encode( $body ),
CURLOPT_TIMEOUT => 300,
) );
@@ -235,7 +253,8 @@ class WP_Agentic_Writer_Local_Backend_Provider implements WP_Agentic_Writer_AI_P
}
if ( $http_code >= 400 ) {
return new WP_Error( 'api_error', sprintf( 'API error (%d): %s', $http_code, $buffer ) );
error_log( 'WPAW Local Backend API error: HTTP=' . $http_code . ', Buffer: ' . substr( $buffer, 0, 1000 ) );
return new WP_Error( 'api_error', sprintf( 'API error (%d): %s', $http_code, substr( $buffer, 0, 500 ) ) );
}
// FALLBACK: If no SSE chunks were parsed, the proxy likely returned a plain JSON response.