-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-preset.php
More file actions
242 lines (216 loc) · 7.08 KB
/
Copy pathimage-preset.php
File metadata and controls
242 lines (216 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* Example: Image Generation Preset
*
* A preset that generates images instead of text. Demonstrates how to
* override execute() to use generateImage() with ModelConfig for
* orientation control.
*
* Required file structure in your plugin:
*
* your-plugin/
* ├── your-plugin.php
* ├── src/
* │ └── Presets/
* │ └── ImageGeneratePreset.php <-- this class
* └── templates/
* └── presets/
* └── image-generate.php <-- prompt template
*
* @package WordPress\InfomaniakAiToolkit\Examples
*/
declare(strict_types=1);
namespace YourPlugin\Presets;
use WordPress\AiClient\AiClient;
use WordPress\AiClient\Files\Enums\MediaOrientationEnum;
use WordPress\AiClient\Providers\Models\DTO\ModelConfig;
use WordPress\InfomaniakAiToolkit\Presets\BasePreset;
/**
* Generates an image from a text description.
*/
class ImageGeneratePreset extends BasePreset
{
public function name(): string
{
return 'image-generate';
}
public function label(): string
{
return __('Generate Image', 'your-plugin');
}
public function description(): string
{
return __('Generates an image from a text description.', 'your-plugin');
}
public function inputSchema(): array
{
return [
'type' => 'object',
'properties' => [
'prompt' => [
'type' => 'string',
'description' => 'A detailed description of the image to generate.',
],
'orientation' => [
'type' => 'string',
'description' => 'The image orientation: square, landscape, or portrait.',
'default' => 'square',
],
],
'required' => ['prompt'],
];
}
protected function templateName(): string
{
return 'image-generate';
}
protected function templateData(array $input): array
{
return [
'prompt' => $input['prompt'] ?? '',
];
}
/**
* Override modelType() to indicate this preset uses image models.
*
* This is used by the admin UI to filter the model selector,
* showing only image-capable models when this preset is selected.
*/
public function modelType(): string
{
return 'image';
}
protected function category(): string
{
return 'media';
}
protected function outputAbilitySchema(): array
{
return [
'type' => 'object',
'properties' => [
'data_uri' => [
'type' => 'string',
'description' => 'The generated image as a data URI.',
],
'mime_type' => [
'type' => 'string',
'description' => 'The MIME type of the generated image.',
],
],
];
}
protected function annotations(): array
{
return [
'readonly' => true,
'destructive' => false,
'idempotent' => false,
];
}
/**
* Override execute() to use generateImage() instead of generateText().
*
* Image generation requires:
* 1. A ModelConfig to set orientation (square, landscape, portrait)
* 2. Calling generateImage() instead of generateText()
* 3. Extracting the data URI and MIME type from the returned File DTO
*
* The tracking context (setTrackingContext/clearTrackingContext) ensures
* usage is attributed to this preset.
*/
public function execute(array $input)
{
if (!class_exists(AiClient::class)) {
return new \WP_Error(
'ai_unavailable',
__('AI Client is not available.', 'your-plugin')
);
}
$this->setTrackingContext();
try {
$data = $this->templateData($input);
$promptText = $this->render(
$this->templatesPath() . '/' . $this->templateName() . '.php',
$data
);
if (empty($promptText)) {
return new \WP_Error(
'preset_template_error',
__('Failed to render prompt template.', 'your-plugin')
);
}
// Map orientation string to enum.
$orientation = $input['orientation'] ?? 'square';
$orientationEnum = match ($orientation) {
'landscape' => MediaOrientationEnum::landscape(),
'portrait' => MediaOrientationEnum::portrait(),
default => MediaOrientationEnum::square(),
};
$config = new ModelConfig();
$config->setOutputMediaOrientation($orientationEnum);
$builder = AiClient::prompt($promptText)
->usingProvider($this->provider())
->usingModelConfig($config);
$modelPref = $this->modelPreference();
if ($modelPref !== null) {
$builder->usingModelPreference($modelPref);
}
try {
$file = $builder->generateImage();
} catch (\Throwable $e) {
return new \WP_Error(
'ai_generation_error',
$e->getMessage()
);
}
return [
'data_uri' => $file->getDataUri(),
'mime_type' => $file->getMimeType(),
];
} finally {
$this->clearTrackingContext();
}
}
}
/*
* --------------------------------------------------------------------------
* Template: templates/presets/image-generate.php
* --------------------------------------------------------------------------
*
* <?= $prompt ?>
*
* --------------------------------------------------------------------------
* Registration in your-plugin.php:
* --------------------------------------------------------------------------
*
* add_action('wp_abilities_api_init', function() {
* if (!class_exists(\WordPress\InfomaniakAiToolkit\Presets\BasePreset::class)) {
* return;
* }
* $preset = new \YourPlugin\Presets\ImageGeneratePreset();
* $preset->registerAsAbility();
* });
*
* --------------------------------------------------------------------------
* Usage in PHP:
* --------------------------------------------------------------------------
*
* $preset = new \YourPlugin\Presets\ImageGeneratePreset();
* $result = $preset->execute([
* 'prompt' => 'A serene mountain landscape at sunset',
* 'orientation' => 'landscape',
* ]);
*
* if (!is_wp_error($result)) {
* echo '<img src="' . esc_attr($result['data_uri']) . '" />';
* }
*
* --------------------------------------------------------------------------
* Runtime model override:
* --------------------------------------------------------------------------
*
* $preset = new \YourPlugin\Presets\ImageGeneratePreset();
* $preset->setModelPreference('your-preferred-model');
* $result = $preset->execute(['prompt' => 'A red cat']);
*/