| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utilities |
| 4 |
* |
| 5 |
* @package AutomatorWP\AI_Assistant\Utilities |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get the list of available models |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
function automatorwp_ai_assistant_get_models() { |
| 20 |
|
| 21 |
$models = array(); |
| 22 |
|
| 23 |
if( class_exists( 'WordPress\AiClient\AiClient' ) ) { |
| 24 |
$registry = WordPress\AiClient\AiClient::defaultRegistry(); |
| 25 |
|
| 26 |
$requirements = new WordPress\AiClient\Providers\Models\DTO\ModelRequirements( |
| 27 |
array( WordPress\AiClient\Providers\Models\Enums\CapabilityEnum::textGeneration() ), |
| 28 |
array() |
| 29 |
); |
| 30 |
|
| 31 |
foreach( $registry->findModelsMetadataForSupport( $requirements ) as $result ) { |
| 32 |
$provider = $result->getProvider(); |
| 33 |
$provider_id = $provider->getId(); |
| 34 |
|
| 35 |
$models[$provider_id] = array( |
| 36 |
'name' => $provider->getName(), |
| 37 |
'models' => array(), |
| 38 |
); |
| 39 |
|
| 40 |
foreach( $result->getModels() as $model ) { |
| 41 |
$models[$provider_id]['models'][$model->getId()] = $model->getName(); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return $models; |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get models IDs |
| 52 |
* |
| 53 |
* @since 1.0.0 |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
function automatorwp_ai_assistant_get_models_ids() { |
| 58 |
|
| 59 |
$providers = automatorwp_ai_assistant_get_models(); |
| 60 |
$models_ids = array(); |
| 61 |
|
| 62 |
foreach( $providers as $provider ) { |
| 63 |
|
| 64 |
|
| 65 |
foreach( $provider['models'] as $id => $name ) { |
| 66 |
$models_ids[] = $id; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
return $models_ids; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get a function response from a message |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* |
| 80 |
* @param \WordPress\AiClient\Messages\DTO\Message $message |
| 81 |
* |
| 82 |
* @return mixed|false |
| 83 |
*/ |
| 84 |
function automatorwp_ai_assistant_get_function_response( $message ) { |
| 85 |
$array = $message->toArray(); |
| 86 |
$response = false; |
| 87 |
|
| 88 |
if( |
| 89 |
isset( $array['parts'] ) |
| 90 |
&& isset( $array['parts'][0] ) |
| 91 |
&& isset( $array['parts'][0]['functionResponse'] ) |
| 92 |
&& isset( $array['parts'][0]['functionResponse']['response'] ) |
| 93 |
) { |
| 94 |
$response = $array['parts'][0]['functionResponse']['response']; |
| 95 |
} |
| 96 |
|
| 97 |
return $response; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get all function responses from a message |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @param \WordPress\AiClient\Messages\DTO\Message $message |
| 107 |
* |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
function automatorwp_ai_assistant_get_all_function_responses( $message ) { |
| 111 |
$array = $message->toArray(); |
| 112 |
$responses = array(); |
| 113 |
|
| 114 |
if( isset( $array['parts'] ) && is_array( $array['parts'] ) ) { |
| 115 |
foreach( $array['parts'] as $part ) { |
| 116 |
if( isset( $part['functionResponse'] ) |
| 117 |
&& isset( $part['functionResponse']['response'] ) ) { |
| 118 |
$responses[] = $part['functionResponse']['response']; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $responses; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
function automatorwp_ai_assistant_resolve_automation_ids( $search = '', $args = array() ) { |
| 128 |
|
| 129 |
global $wpdb; |
| 130 |
|
| 131 |
if ( empty( $search ) ) return array(); |
| 132 |
|
| 133 |
$args = wp_parse_args( $args, array( |
| 134 |
'limit' => 3, |
| 135 |
) ); |
| 136 |
|
| 137 |
$ct_table = ct_setup_table( 'automatorwp_automations' ); |
| 138 |
|
| 139 |
// Pull back the search string |
| 140 |
$search = esc_sql( $wpdb->esc_like( $search ) ); |
| 141 |
$from = "FROM {$ct_table->db->table_name} as a "; |
| 142 |
|
| 143 |
$where = "WHERE ( "; |
| 144 |
|
| 145 |
// Search for title or slug |
| 146 |
$where .= "a.title LIKE '%{$search}%' "; |
| 147 |
|
| 148 |
// Search by id |
| 149 |
if( is_numeric( $search ) ) |
| 150 |
$where .= "OR a.id = '{$search}' "; |
| 151 |
|
| 152 |
$where .= " ) "; |
| 153 |
|
| 154 |
$limit = isset( $args['limit'] ) ? absint( $args['limit'] ) : 3; |
| 155 |
|
| 156 |
$ids = $wpdb->get_col( |
| 157 |
"SELECT a.id |
| 158 |
{$from} |
| 159 |
{$where} |
| 160 |
LIMIT {$limit}", |
| 161 |
); |
| 162 |
|
| 163 |
ct_reset_setup_table(); |
| 164 |
|
| 165 |
return $ids; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
function automatorwp_ai_assistant_get_automation( $search = '', $args = array() ) { |
| 170 |
|
| 171 |
$automation_ids = automatorwp_ai_assistant_resolve_automation_ids( $search, $args ); |
| 172 |
$ability = new AutomatorWP_AI_Assistant_Ability(); |
| 173 |
|
| 174 |
$singular = __( 'automation', 'automatorwp' ); |
| 175 |
$plural = __( 'automations', 'automatorwp' ); |
| 176 |
|
| 177 |
// Bail if can not find the post |
| 178 |
if( count( $automation_ids ) === 0 ) { |
| 179 |
return $ability->response_error( sprintf( |
| 180 |
// translators: %1$s: Post type singular %2$s: Search term |
| 181 |
__( 'I couldn\'t find the %1$s "%2$s".', 'automatorwp' ) |
| 182 |
. ' ' . __( 'Try providing a different title or the ID number.', 'automatorwp' ), |
| 183 |
$singular, |
| 184 |
$search |
| 185 |
) ); |
| 186 |
} else if( count( $automation_ids ) > 1 ) { |
| 187 |
// Bail if more than 1 post found |
| 188 |
return $ability->response_error( sprintf( |
| 189 |
// translators: %1$s: Post type plural %2$s: Search term |
| 190 |
__( 'I found several %1$s matching with "%2$s".', 'automatorwp' ) |
| 191 |
. ' ' . __( 'Try providing a different title or the ID number.', 'automatorwp' ), |
| 192 |
$plural, |
| 193 |
$search |
| 194 |
) ); |
| 195 |
} |
| 196 |
|
| 197 |
ct_setup_table( 'automatorwp_automations' ); |
| 198 |
$automation = ct_get_object( absint( $automation_ids[0] ) ); |
| 199 |
ct_reset_setup_table(); |
| 200 |
|
| 201 |
if( ! $automation ) { |
| 202 |
return $ability->response_error( sprintf( |
| 203 |
// translators: %1$s: Post type singular %2$s: Search term |
| 204 |
__( 'I couldn\'t find the %1$s "%2$s".', 'automatorwp' ) |
| 205 |
. ' ' . __( 'Try providing a different title or the ID number.', 'automatorwp' ), |
| 206 |
$singular, |
| 207 |
$search |
| 208 |
) ); |
| 209 |
} |
| 210 |
|
| 211 |
return $automation; |
| 212 |
|
| 213 |
} |