PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.6
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.6
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / classes / core / response.php

response.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.6, at classes/core/response.php

103 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Tool Response Helper
4 *
5 * IMPORTANT: All tool handlers MUST use this class for responses.
6 * This ensures consistent response format for the Laravel backend and AI.
7 *
8 * Standard Response Format:
9 * - Success: ['success' => true, 'message' => '...', 'data' => [...]]
10 * - Error: ['success' => false, 'error' => '...']
11 *
12 * Usage:
13 * use ZipAI\MCP\Classes\Core\Response;
14 *
15 * // Success response
16 * return Response::success( 'Page created successfully.' );
17 * return Response::success( 'Page created successfully.', ['id' => 123, 'url' => '...'] );
18 *
19 * // Error response
20 * return Response::error( 'Page title is required.' );
21 *
22 * // From WP_Error
23 * if ( is_wp_error( $result ) ) {
24 * return Response::from_wp_error( $result );
25 * }
26 *
27 * @package zip-ai
28 */
29
30 namespace ZipAI\MCP\Classes\Core;
31
32 // Exit if accessed directly.
33 if ( ! defined( 'ABSPATH' ) ) {
34 exit;
35 }
36
37 /**
38 * Response Class - Enforces consistent response format for all tools.
39 */
40 class Response {
41
42 /**
43 * Create a success response.
44 *
45 * @param string $message Success message for the AI to communicate to user.
46 * @param array $data Optional additional data (e.g., created resource details).
47 * @return array Standardized success response.
48 */
49 public static function success( $message, $data = array() ) {
50 $response = array(
51 'success' => true,
52 'message' => $message,
53 );
54
55 if ( ! empty( $data ) ) {
56 $response['data'] = $data;
57 }
58
59 return $response;
60 }
61
62 /**
63 * Create an error response.
64 *
65 * @param string $message Error message for the AI to communicate to user.
66 * @param string $suggestion Optional suggestion for the AI on how to resolve the issue.
67 * @param array $data Optional structured detail (e.g. per-item results of a
68 * partially-failed batch) so the caller can see exactly
69 * what failed without parsing the message string.
70 * @return array Standardized error response.
71 */
72 public static function error( $message, $suggestion = '', $data = array() ) {
73 $response = array(
74 'success' => false,
75 'error' => $message,
76 );
77
78 if ( ! empty( $suggestion ) ) {
79 $response['suggestion'] = $suggestion;
80 }
81
82 if ( ! empty( $data ) ) {
83 $response['data'] = $data;
84 }
85
86 return $response;
87 }
88
89 /**
90 * Create an error response from WP_Error.
91 *
92 * @param \WP_Error $wp_error WordPress error object.
93 * @return array Standardized error response.
94 */
95 public static function from_wp_error( $wp_error ) {
96 if ( ! is_wp_error( $wp_error ) ) {
97 return self::error( 'An unknown error occurred.' );
98 }
99
100 return self::error( $wp_error->get_error_message() );
101 }
102 }
103