-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptAIHelper.php
More file actions
387 lines (326 loc) · 14.3 KB
/
PromptAIHelper.php
File metadata and controls
387 lines (326 loc) · 14.3 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php namespace ProcessWire;
class PromptAIHelper {
public static array $adminTemplates = ['admin', 'language', 'user', 'permission', 'role'];
public static array $textFieldTypes = [
'ProcessWire\FieldtypePageTitle',
'ProcessWire\FieldtypePageTitleLanguage',
'ProcessWire\FieldtypeText',
'ProcessWire\FieldtypeTextarea',
'ProcessWire\FieldtypeTextLanguage',
'ProcessWire\FieldtypeTextareaLanguage',
];
public static array $fileFieldTypes = [
'ProcessWire\FieldtypeImage',
'ProcessWire\FieldtypeFile',
];
public static function parsePromptMatrix(?string $promptMatrixString = '', $showErrors = false): array {
$promptMatrix = [];
// If empty, return empty array
if (empty($promptMatrixString)) {
return $promptMatrix;
}
$jsonData = json_decode($promptMatrixString, true);
if (json_last_error() !== JSON_ERROR_NONE || !is_array($jsonData)) {
if ($showErrors) {
wire()->error(__('Invalid JSON format in prompt configuration'));
}
return $promptMatrix;
}
$availableTemplates = PromptAIHelper::getTemplateOptions();
$availableFields = PromptAIHelper::getFieldOptions();
foreach ($jsonData as $index => $config) {
$promptMatrixEntity = PromptMatrixEntity::fromArray($config);
// Validation
// Mode is required
if (!$promptMatrixEntity->mode || !in_array($promptMatrixEntity->mode, ['inline', 'page'])) {
if ($showErrors) {
wire()->error(__('Mode is missing or invalid in configuration ').($index + 1));
}
continue;
}
// Fields array is REQUIRED
if (!$promptMatrixEntity->fields || !is_array($promptMatrixEntity->fields) || empty($promptMatrixEntity->fields)) {
if ($showErrors) {
wire()->error(__('Fields are missing in configuration ').($index + 1));
}
continue;
}
// Prompt is required
if (!$promptMatrixEntity->prompt) {
if ($showErrors) {
wire()->error(__('Prompt is missing in configuration ').($index + 1));
}
continue;
}
// Validate template IDs exist (if set)
if ($promptMatrixEntity->templates && is_array($promptMatrixEntity->templates)) {
foreach ($promptMatrixEntity->templates as $templateId) {
if (!array_key_exists($templateId, $availableTemplates)) {
if ($showErrors) {
wire()->error(__('Template ID ').$templateId.__(' does not exist in configuration ').($index + 1));
}
continue 2; // Skip this entire configuration
}
}
}
// Validate all field IDs exist
foreach ($promptMatrixEntity->fields as $fieldId) {
if (!array_key_exists($fieldId, $availableFields)) {
if ($showErrors) {
wire()->error(__('Field ID ').$fieldId.__(' does not exist in configuration ').($index + 1));
}
continue 2; // Skip this entire configuration
}
}
$promptMatrix[] = $promptMatrixEntity;
}
return $promptMatrix;
}
public static function getFieldOptions(): array {
$fieldsOptions = [];
if (wire('fields')) {
/** @var Field $field */
foreach (wire('fields') as $field) {
if ($field->flags && ($field->flags === Field::flagSystem || $field->flags === 24)) {
continue;
}
if (!in_array(get_class($field->type), self::$textFieldTypes) && !in_array(get_class($field->type), self::$fileFieldTypes)) {
continue;
}
$label = $field->label ? $field->label.' ('.$field->name.')' : $field->name;
$fieldsOptions[$field->id] = $label;
}
}
return $fieldsOptions;
}
public static function getTemplateOptions(): array {
$templatesOptions = [];
if (wire('templates')) {
foreach (wire('templates') as $template) {
if (in_array($template->name, self::$adminTemplates)) {
continue;
}
if (str_starts_with($template->name, 'field-')) {
continue;
}
// Exclude RPB system datapage (not a block template)
if ($template->name === 'rockpagebuilder_datapage') {
continue;
}
$label = $template->label ? $template->label.' ('.$template->name.')' : $template->name;
if (str_starts_with($template->name, 'repeater_')) {
$name = str_replace('repeater_', '', $template->name);
$label = 'Repeater: '.$name;
}
// Label RPB block templates clearly
if (str_starts_with($template->name, 'rockpagebuilderblock-')) {
$blockName = str_replace('rockpagebuilderblock-', '', $template->name);
$label = 'RPB Block: ' . ucfirst($blockName);
}
$templatesOptions[$template->id] = $label;
}
}
return $templatesOptions;
}
public static function getRepeaterTemplateIdsForPage(Page $page): array {
$templatesIds = [];
if (wire('templates')) {
foreach (wire('templates') as $template) {
if (!str_starts_with($template->name, 'repeater_')) {
continue;
}
$name = str_replace('repeater_', '', $template->name);
if ($page->$name) {
$templatesIds[] = $template->id;
}
}
}
return $templatesIds;
}
/**
* Check if a template configuration matches a given template ID
* Template configuration is always an array or null (for all templates)
*/
public static function templateMatches($entityTemplates, int $pageTemplateId): bool {
if ($entityTemplates === null || empty($entityTemplates)) {
return true; // null/empty means all templates
}
return is_array($entityTemplates) && in_array($pageTemplateId, $entityTemplates);
}
public static function getRelevantPrompts(Page $page, array $promptMatrix): array {
$template = $page ? $page->template : null;
$relevantPrompts = [];
foreach ($promptMatrix as $index => $promptMatrixEntity) {
if (PromptAIHelper::templateMatches($promptMatrixEntity->templates, $template->id)) {
$relevantPrompts[$index] = $promptMatrixEntity;
continue;
}
// Handle repeater templates and RPB block templates
if (is_array($promptMatrixEntity->templates)) {
foreach ($promptMatrixEntity->templates as $templateId) {
$entityTemplate = wire('templates')->get($templateId);
if (!$entityTemplate) continue;
if (str_starts_with($entityTemplate->name, 'repeater_')) {
$repeaterName = str_replace('repeater_', '', $entityTemplate->name);
if ($page->$repeaterName) {
$relevantPrompts[$index] = $promptMatrixEntity;
break;
}
} elseif (str_starts_with($entityTemplate->name, 'rockpagebuilderblock-')) {
// RPB stores blocks centrally, not as direct children.
// Check the page's RPB fields for any blocks of this template.
$found = false;
foreach ($page->template->fields as $pageField) {
if ($found) break;
if (strpos(get_class($pageField->type), 'RockPageBuilder') === false) continue;
$value = $page->get($pageField->name);
if (!$value || !is_iterable($value)) continue;
foreach ($value as $block) {
if ($block instanceof Page && $block->template->id == $entityTemplate->id) {
$found = true;
break;
}
}
}
if ($found) {
$relevantPrompts[$index] = $promptMatrixEntity;
break;
}
}
}
}
}
return $relevantPrompts;
}
public static function getMediaType($filePath) {
$mimeType = mime_content_type($filePath);
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
// Fallback-Mapping for problematic file types
$extensionMap = [
'csv' => 'text/csv',
'md' => 'text/markdown',
];
if ($mimeType === 'text/plain' && isset($extensionMap[$extension])) {
return $extensionMap[$extension];
}
return $mimeType;
}
/**
* Determine repeater context from a page
*
* @param Page $page The page to check (could be parent or repeater item)
* @return array ['parentPage' => Page, 'repeaterItem' => Page|null]
*/
public static function getRepeaterContext(Page $page): array {
if (strpos($page->template->name, 'repeater_') === 0) {
return [
'parentPage' => $page->getForPage(),
'repeaterItem' => $page,
];
}
if (strpos($page->template->name, 'rockpagebuilderblock-') === 0) {
return [
'parentPage' => $page->parent,
'repeaterItem' => $page,
];
}
return [
'parentPage' => $page,
'repeaterItem' => null,
];
}
/**
* Substitute placeholders in prompt text with actual field values
*
* Supported placeholders:
* - {page.fieldname} - Field from the main/parent page
* - {item.fieldname} - Field from current repeater item (only in repeater context)
*
* @param string $prompt The prompt text with placeholders
* @param Page $page The main page being processed
* @param Page|null $repeaterItem Optional repeater item page for {item.*} placeholders
* @return string Prompt with placeholders replaced
*/
public static function substitutePlaceholders(
string $prompt,
Page $page,
?Page $repeaterItem = null
): string {
/** @var PromptAI $module */
$module = wire('modules')->get('PromptAI');
// Pattern 1: {page.fieldname} - always references parent/main page
preg_match_all('/\{page\.([a-zA-Z0-9_]+)\}/', $prompt, $pageMatches, PREG_SET_ORDER);
foreach ($pageMatches as $match) {
$placeholder = $match[0]; // e.g., "{page.title}"
$fieldName = $match[1]; // e.g., "title"
$field = wire('fields')->get($fieldName);
$value = $page->get($fieldName);
// Handle non-existent or empty fields
if ($value === null || $value === '') {
$module->warning(__('Placeholder field not found or empty: ') . $placeholder);
$value = '';
}
// Convert to string (handle different field types)
$fieldtype = $field ? $field->type : null;
$value = $module->fieldValueToString($value, $fieldtype);
$prompt = str_replace($placeholder, $value, $prompt);
}
// Pattern 2: {item.fieldname} - only available in repeater context
if ($repeaterItem !== null) {
preg_match_all('/\{item\.([a-zA-Z0-9_]+)\}/', $prompt, $itemMatches, PREG_SET_ORDER);
foreach ($itemMatches as $match) {
$placeholder = $match[0]; // e.g., "{item.title}"
$fieldName = $match[1]; // e.g., "title"
$field = wire('fields')->get($fieldName);
$value = $repeaterItem->get($fieldName);
if ($value === null || $value === '') {
$module->warning(__('Placeholder field not found or empty: ') . $placeholder);
$value = '';
}
$fieldtype = $field ? $field->type : null;
/** @var PromptAI $module */
$module = wire('modules')->get('PromptAI');
$value = $module->fieldValueToString($value, $fieldtype);
$prompt = str_replace($placeholder, $value, $prompt);
}
} else {
// Warn if {item.*} used outside repeater context
if (preg_match('/\{item\.[a-zA-Z0-9_]+\}/', $prompt)) {
$module->warning(__('Placeholder {item.*} used outside repeater context'));
}
}
return $prompt;
}
/**
* Substitute placeholders in prompt and prepare for AI chat
*
* This wrapper method:
* - Substitutes placeholders ({page.field} and {item.field})
* - Builds final prompt
* - Returns the prompt ready for AI chat
*
* @param string $prompt The prompt text with placeholders
* @param Page $page The parent/main page being processed
* @param string $content Optional content to append after prompt
* @param Page|null $repeaterItem Optional repeater item for {item.*} placeholders
* @return string Final prompt with placeholders substituted
*/
public static function substituteAndPreparePrompt(
string $prompt,
Page $page,
string $content = '',
?Page $repeaterItem = null
): string {
// Substitute placeholders
$substitutedPrompt = self::substitutePlaceholders(
$prompt,
$page,
$repeaterItem
);
// Combine with content if provided
if ($content) {
return trim($substitutedPrompt . PHP_EOL . $content);
}
return $substitutedPrompt;
}
}