Skip to content

Commit 0136c33

Browse files
committed
Merge branch 'develop' into update/client-ability-execution
2 parents 2ff2139 + 588d786 commit 0136c33

6 files changed

Lines changed: 144 additions & 301 deletions

File tree

includes/Abilities/Title_Generation/Title_Generation.php

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@
2323
*/
2424
class Title_Generation extends Abstract_Ability {
2525

26-
/**
27-
* The default number of candidates to generate.
28-
*
29-
* @since 0.1.0
30-
*
31-
* @var int
32-
*/
33-
protected const CANDIDATES_DEFAULT = 3;
34-
3526
/**
3627
* {@inheritDoc}
3728
*
@@ -41,21 +32,14 @@ protected function input_schema(): array {
4132
return array(
4233
'type' => 'object',
4334
'properties' => array(
44-
'content' => array(
35+
'content' => array(
4536
'type' => 'string',
4637
'description' => esc_html__( 'Content to generate title suggestions for.', 'ai' ),
4738
),
48-
'context' => array(
39+
'context' => array(
4940
'type' => 'string',
5041
'description' => esc_html__( 'Additional context to use when generating title suggestions. This can either be a string of additional context or can be a post ID that will then be used to get context from that post (if it exists). If no content is provided but a valid post ID is used here, the content from that post will be used.', 'ai' ),
5142
),
52-
'candidates' => array(
53-
'type' => 'integer',
54-
'minimum' => 1,
55-
'maximum' => 10,
56-
'default' => self::CANDIDATES_DEFAULT,
57-
'description' => esc_html__( 'Number of titles to generate', 'ai' ),
58-
),
5943
),
6044
);
6145
}
@@ -69,12 +53,9 @@ protected function output_schema(): array {
6953
return array(
7054
'type' => 'object',
7155
'properties' => array(
72-
'titles' => array(
73-
'type' => 'array',
74-
'description' => esc_html__( 'Generated title suggestions.', 'ai' ),
75-
'items' => array(
76-
'type' => 'string',
77-
),
56+
'title' => array(
57+
'type' => 'string',
58+
'description' => esc_html__( 'Generated title suggestion.', 'ai' ),
7859
),
7960
),
8061
);
@@ -90,9 +71,8 @@ protected function execute_callback( $input ) {
9071
$args = wp_parse_args(
9172
$input,
9273
array(
93-
'content' => null,
94-
'context' => null,
95-
'candidates' => self::CANDIDATES_DEFAULT,
74+
'content' => null,
75+
'context' => null,
9676
),
9777
);
9878

@@ -130,30 +110,25 @@ protected function execute_callback( $input ) {
130110
);
131111
}
132112

133-
// Generate the titles.
134-
$result = $this->generate_titles( $content, $context, $args['candidates'] );
113+
// Generate the title.
114+
$result = $this->generate_title( $content, $context );
135115

136116
// If we have an error, return it.
137117
if ( is_wp_error( $result ) ) {
138118
return $result;
139119
}
140120

141-
// If we have no results, return an error.
121+
// If we have no result, return an error.
142122
if ( empty( $result ) ) {
143123
return new WP_Error(
144124
'no_results',
145-
esc_html__( 'No title suggestions were generated.', 'ai' )
125+
esc_html__( 'No title suggestion was generated.', 'ai' )
146126
);
147127
}
148128

149-
// Return the titles in the format the Ability expects.
129+
// Return the title in the format the Ability expects.
150130
return array(
151-
'titles' => array_map(
152-
static function ( $title ) {
153-
return sanitize_text_field( trim( $title, ' "\'' ) );
154-
},
155-
$result
156-
),
131+
'title' => sanitize_text_field( trim( $result, ' "\'' ) ),
157132
);
158133
}
159134

@@ -220,16 +195,15 @@ protected function meta(): array {
220195
}
221196

222197
/**
223-
* Generates title suggestions from the given content.
198+
* Generates a title suggestion from the given content.
224199
*
225200
* @since 0.1.0
226201
*
227-
* @param string $content The content to generate title suggestions for.
202+
* @param string $content The content to generate a title suggestion for.
228203
* @param string|array<string, string> $context Additional context to use.
229-
* @param int $candidates The number of titles to generate.
230-
* @return array<string>|\WP_Error The generated titles, or a WP_Error if there was an error.
204+
* @return string|\WP_Error The generated title, or a WP_Error if there was an error.
231205
*/
232-
protected function generate_titles( string $content, $context, int $candidates = 1 ) {
206+
protected function generate_title( string $content, $context ) {
233207
// Convert the context to a string if it's an array.
234208
if ( is_array( $context ) ) {
235209
$context = implode(
@@ -255,12 +229,17 @@ static function ( $key, $value ) {
255229
$content .= "\n\n<additional-context>" . $context . '</additional-context>';
256230
}
257231

258-
// Generate the titles using the AI client.
259-
return wp_ai_client_prompt( $content )
232+
// Generate the title using the AI client.
233+
$result = wp_ai_client_prompt( $content )
260234
->using_system_instruction( $this->get_system_instruction() )
261235
->using_temperature( 0.7 )
262-
->using_candidate_count( absint( $candidates ) )
263236
->using_model_preference( ...get_preferred_models_for_text_generation() )
264-
->generate_texts();
237+
->generate_text();
238+
239+
if ( is_wp_error( $result ) ) {
240+
return $result;
241+
}
242+
243+
return $result;
265244
}
266245
}

0 commit comments

Comments
 (0)