openai_client = $openai_client; } /** * Execute local tool calls and follow up with function_call_output items. * * @param array $response_data Initial response data. * @param string $api_key OpenAI API key. * @param string $model OpenAI model name. * @param array $tools Tool definitions. * @param string $reasoning_effort Reasoning-effort key. * @param int $timeout OpenAI HTTP timeout in seconds. * @param array $tool_context Private context for local tool execution. * @return array|WP_Error */ public function run( $response_data, $api_key, $model, $tools, $reasoning_effort = AIEditor_Model_Config::DEFAULT_REASONING_EFFORT, $timeout = 120, $tool_context = array() ) { $markup = ''; $reply = ''; $history_action = ''; $tool_name = ''; $tool_names = array(); $tool_calls_log = array(); $tool_outputs_log = array(); $current_response = $response_data; for ( $step = 0; $step < 5; $step++ ) { $tool_calls = $this->get_function_calls( $current_response ); if ( empty( $tool_calls ) ) { break; } $tool_outputs = array(); $pending_tool_outputs_log = array(); $stop_after_local_tool = false; foreach ( $tool_calls as $tool_call ) { if ( empty( $tool_call['call_id'] ) || empty( $tool_call['name'] ) ) { continue; } $tool_result = $this->execute_tool_call( $tool_call, $markup, $reply, $history_action, $tool_context ); if ( ! empty( $tool_call['name'] ) ) { $tool_names[] = $tool_call['name']; $tool_calls_log[] = array( 'name' => $tool_call['name'], 'call_id' => isset( $tool_call['call_id'] ) ? (string) $tool_call['call_id'] : '', 'arguments' => $this->decode_tool_call_arguments( $tool_call ), ); } $tool_output = array( 'type' => 'function_call_output', 'call_id' => $tool_call['call_id'], 'output' => wp_json_encode( $tool_result ), ); $tool_outputs[] = $tool_output; if ( class_exists( 'AIEditor_Ability_Tools' ) && AIEditor_Ability_Tools::is_ability_tool( $tool_call['name'] ) ) { $pending_tool_outputs_log[] = array( 'name' => $tool_call['name'], 'call_id' => isset( $tool_call['call_id'] ) ? (string) $tool_call['call_id'] : '', 'output' => $tool_result, ); } if ( $this->should_stop_after_local_tool_call( $tool_call['name'] ) ) { $stop_after_local_tool = true; } } if ( empty( $tool_outputs ) || empty( $current_response['id'] ) ) { break; } if ( $stop_after_local_tool ) { break; } $tool_outputs_log = array_merge( $tool_outputs_log, $pending_tool_outputs_log ); $follow_up_response = $this->openai_client->create_response( array( 'model' => $model, 'previous_response_id' => $current_response['id'], 'input' => $tool_outputs, 'tools' => $tools, 'parallel_tool_calls' => false, 'reasoning' => array( 'effort' => $reasoning_effort, ), 'tool_choice' => 'auto', ), $api_key, $timeout ); if ( is_wp_error( $follow_up_response ) ) { return $follow_up_response; } if ( 200 !== $follow_up_response['meta']['status'] ) { return $this->openai_client->build_error_response( $follow_up_response['data'], $follow_up_response['meta']['status'] ); } $current_response = $follow_up_response['data']; } $tool_name = $this->resolve_primary_tool_name( $markup, $reply, $tool_names ); return array( 'response_data' => $current_response, 'markup' => $markup, 'reply' => $reply, 'history_action' => $history_action, 'tool_name' => $tool_name, 'tool_names' => array_values( array_unique( $tool_names ) ), 'tool_calls' => $tool_calls_log, 'tool_outputs' => $tool_outputs_log, ); } /** * Decode tool call arguments for debug metadata. * * @param array $tool_call Tool call item. * @return array|string|null */ private function decode_tool_call_arguments( $tool_call ) { if ( ! isset( $tool_call['arguments'] ) || '' === $tool_call['arguments'] ) { return null; } $arguments = json_decode( $tool_call['arguments'], true ); if ( JSON_ERROR_NONE === json_last_error() ) { return $arguments; } return $tool_call['arguments']; } /** * Extract display text from a Responses API payload. * * @param array $response_data Response payload. * @return string */ public function extract_response_text( $response_data ) { if ( ! empty( $response_data['output_text'] ) && is_string( $response_data['output_text'] ) ) { return $response_data['output_text']; } $final_answer_parts = array(); $text_parts = array(); if ( empty( $response_data['output'] ) || ! is_array( $response_data['output'] ) ) { return ''; } foreach ( $response_data['output'] as $item ) { if ( empty( $item['type'] ) || 'message' !== $item['type'] || empty( $item['content'] ) || ! is_array( $item['content'] ) ) { continue; } foreach ( $item['content'] as $content_item ) { if ( isset( $content_item['type'] ) && 'output_text' === $content_item['type'] && isset( $content_item['text'] ) ) { if ( isset( $item['phase'] ) && 'final_answer' === $item['phase'] ) { $final_answer_parts[] = $content_item['text']; } $text_parts[] = $content_item['text']; } } } if ( ! empty( $final_answer_parts ) ) { return trim( implode( "\n\n", $final_answer_parts ) ); } return trim( implode( "\n\n", $text_parts ) ); } /** * Pick the primary tool name that actually drove the result returned to the client. * * @param string $markup Markup returned by tool calls. * @param string $reply Reply text returned by tool calls. * @param array $tool_names Tool names called during the loop. * @return string */ private function resolve_primary_tool_name( $markup, $reply, $tool_names ) { if ( ! empty( $markup ) ) { if ( in_array( 'insert_block_markup', $tool_names, true ) ) { return 'insert_block_markup'; } return 'update_selected_block_markup'; } if ( ! empty( $reply ) ) { return 'request_clarification'; } if ( empty( $tool_names ) ) { return ''; } return end( $tool_names ); } /** * Decide whether a local tool call already produced the final client result. * * @param string $tool_name Local tool name. * @return bool */ private function should_stop_after_local_tool_call( $tool_name ) { return in_array( $tool_name, array( 'change_editor_history', 'insert_block_markup', 'request_clarification', 'update_selected_block_markup', ), true ); } /** * Return function_call items from a Responses API payload. * * @param array $response_data Response payload. * @return array */ private function get_function_calls( $response_data ) { $tool_calls = array(); if ( empty( $response_data['output'] ) || ! is_array( $response_data['output'] ) ) { return $tool_calls; } foreach ( $response_data['output'] as $item ) { if ( isset( $item['type'] ) && 'function_call' === $item['type'] ) { $tool_calls[] = $item; } } return $tool_calls; } /** * Execute a single local tool call. * * @param array $tool_call Tool call item. * @param string $markup Accumulator for replacement markup. * @param string $reply Accumulator for reply text. * @param string $history_action Accumulator for undo/redo action. * @param array $tool_context Private context for local tool execution. * @return array */ private function execute_tool_call( $tool_call, &$markup, &$reply, &$history_action, $tool_context = array() ) { if ( 'request_clarification' === $tool_call['name'] ) { $arguments = json_decode( $tool_call['arguments'], true ); if ( JSON_ERROR_NONE !== json_last_error() || ! is_array( $arguments ) || empty( $arguments['message'] ) ) { return array( 'status' => 'error', 'message' => __( 'The model returned invalid user guidance.', 'ai-editor' ), ); } $reply = (string) $arguments['message']; return array( 'status' => 'success', 'message' => $reply, ); } if ( 'change_editor_history' === $tool_call['name'] ) { $arguments = json_decode( $tool_call['arguments'], true ); if ( JSON_ERROR_NONE !== json_last_error() || ! is_array( $arguments ) || empty( $arguments['action'] ) || ! in_array( $arguments['action'], array( 'undo', 'redo' ), true ) ) { return array( 'status' => 'error', 'message' => __( 'The model returned an invalid history change request.', 'ai-editor' ), ); } $history_action = $arguments['action']; return array( 'status' => 'success', 'history_action' => $arguments['action'], 'message' => 'undo' === $arguments['action'] ? __( 'Undo prepared.', 'ai-editor' ) : __( 'Redo prepared.', 'ai-editor' ), ); } if ( 'update_selected_block_markup' === $tool_call['name'] ) { $arguments = json_decode( $tool_call['arguments'], true ); if ( JSON_ERROR_NONE !== json_last_error() || ! is_array( $arguments ) || empty( $arguments['markup'] ) ) { return array( 'status' => 'error', 'message' => __( 'The model returned invalid replacement markup.', 'ai-editor' ), ); } $markup = (string) $arguments['markup']; return array( 'status' => 'success', 'message' => __( 'Replacement markup prepared.', 'ai-editor' ), ); } if ( 'insert_block_markup' === $tool_call['name'] ) { $arguments = json_decode( $tool_call['arguments'], true ); if ( JSON_ERROR_NONE !== json_last_error() || ! is_array( $arguments ) || empty( $arguments['markup'] ) ) { return array( 'status' => 'error', 'message' => __( 'The model returned invalid insert markup.', 'ai-editor' ), ); } $markup = (string) $arguments['markup']; return array( 'status' => 'success', 'message' => __( 'Insert markup prepared.', 'ai-editor' ), ); } if ( class_exists( 'AIEditor_Ability_Tools' ) && AIEditor_Ability_Tools::is_ability_tool( $tool_call['name'] ) ) { return AIEditor_Ability_Tools::execute_tool_call( $tool_call, $tool_context ); } return array( 'status' => 'error', 'message' => __( 'Unknown tool requested.', 'ai-editor' ), ); } }