v1.3.0
This commit is contained in:
parent
44e2366e16
commit
8716024cd2
1 changed files with 322 additions and 121 deletions
|
|
@ -134,6 +134,45 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the AI providers available for translation on this site.
|
||||
*
|
||||
* Only providers registered with the WordPress AI client (core since
|
||||
* WordPress 7.0) that also have their credentials configured are
|
||||
* listed, since those are the only ones capable of serving requests.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @return array<int,array{id:string,name:string}> List of provider IDs and names.
|
||||
*/
|
||||
public function get_available_providers() {
|
||||
$registry = \WordPress\AiClient\AiClient::defaultRegistry();
|
||||
$providers = array();
|
||||
|
||||
foreach ( $registry->getRegisteredProviderIds() as $provider_id ) {
|
||||
$provider_id = (string) $provider_id;
|
||||
|
||||
try {
|
||||
if ( ! $registry->isProviderConfigured( $provider_id ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $registry->getProviderClassName( $provider_id )::metadata()->getName();
|
||||
} catch ( Throwable $e ) {
|
||||
// A misbehaving provider must not break the settings page:
|
||||
// skip it and keep enumerating the rest.
|
||||
continue;
|
||||
}
|
||||
|
||||
$providers[] = array(
|
||||
'id' => $provider_id,
|
||||
'name' => '' !== $name ? $name : $provider_id,
|
||||
);
|
||||
}
|
||||
|
||||
return $providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a single piece of text to the target locale.
|
||||
*
|
||||
|
|
@ -195,7 +234,15 @@ if ( ! class_exists( 'AI_Translator_Translator' ) ) {
|
|||
add_filter( 'wp_ai_client_default_request_timeout', array( $this, 'filter_ai_default_timeout' ), PHP_INT_MAX );
|
||||
|
||||
try {
|
||||
$result = wp_ai_client_prompt( $text )
|
||||
$prompt = wp_ai_client_prompt( $text );
|
||||
|
||||
// Pin the configured provider when one is selected. An empty
|
||||
// value keeps the WordPress AI client default resolution.
|
||||
if ( isset( $site_settings['ai_provider'] ) && is_string( $site_settings['ai_provider'] ) && '' !== $site_settings['ai_provider'] ) {
|
||||
$prompt = $prompt->using_provider( $site_settings['ai_provider'] );
|
||||
}
|
||||
|
||||
$result = $prompt
|
||||
->using_system_instruction( $system_instruction )
|
||||
->generate_text();
|
||||
} catch ( Exception $e ) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue