@wp-typia/project-tools 0.22.1 → 0.22.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/runtime/built-in-block-code-templates/interactivity.d.ts +1 -1
  2. package/dist/runtime/built-in-block-code-templates/interactivity.js +4 -2
  3. package/dist/runtime/cli-add-shared.d.ts +49 -0
  4. package/dist/runtime/cli-add-shared.js +204 -71
  5. package/dist/runtime/cli-add-workspace-ability-scaffold.d.ts +5 -0
  6. package/dist/runtime/cli-add-workspace-ability-scaffold.js +392 -0
  7. package/dist/runtime/cli-add-workspace-ability-templates.d.ts +34 -0
  8. package/dist/runtime/cli-add-workspace-ability-templates.js +500 -0
  9. package/dist/runtime/cli-add-workspace-ability-types.d.ts +27 -0
  10. package/dist/runtime/cli-add-workspace-ability-types.js +14 -0
  11. package/dist/runtime/cli-add-workspace-ability.js +12 -852
  12. package/dist/runtime/cli-add-workspace-ai-scaffold.d.ts +21 -0
  13. package/dist/runtime/cli-add-workspace-ai-scaffold.js +91 -0
  14. package/dist/runtime/cli-add-workspace-ai-source-emitters.js +119 -1
  15. package/dist/runtime/cli-add-workspace-ai-templates.d.ts +4 -0
  16. package/dist/runtime/cli-add-workspace-ai-templates.js +605 -0
  17. package/dist/runtime/cli-add-workspace-ai.js +15 -465
  18. package/dist/runtime/cli-add-workspace-assets.js +7 -4
  19. package/dist/runtime/cli-add-workspace.js +1 -19
  20. package/dist/runtime/cli-doctor-workspace-bindings.d.ts +11 -0
  21. package/dist/runtime/cli-doctor-workspace-bindings.js +134 -0
  22. package/dist/runtime/cli-doctor-workspace-blocks.d.ts +11 -0
  23. package/dist/runtime/cli-doctor-workspace-blocks.js +504 -0
  24. package/dist/runtime/cli-doctor-workspace-features.d.ts +11 -0
  25. package/dist/runtime/cli-doctor-workspace-features.js +383 -0
  26. package/dist/runtime/cli-doctor-workspace-package.d.ts +18 -0
  27. package/dist/runtime/cli-doctor-workspace-package.js +59 -0
  28. package/dist/runtime/cli-doctor-workspace-shared.d.ts +69 -0
  29. package/dist/runtime/cli-doctor-workspace-shared.js +87 -0
  30. package/dist/runtime/cli-doctor-workspace.js +25 -1062
  31. package/dist/runtime/scaffold-compatibility.d.ts +2 -0
  32. package/dist/runtime/scaffold-compatibility.js +2 -0
  33. package/dist/runtime/typia-llm.d.ts +37 -2
  34. package/dist/runtime/typia-llm.js +240 -3
  35. package/dist/runtime/workspace-inventory.js +24 -0
  36. package/package.json +3 -3
@@ -0,0 +1,605 @@
1
+ import { quotePhpString } from "./php-utils.js";
2
+ import { toTitleCase } from "./string-case.js";
3
+ /**
4
+ * Build the generated PHP controller and hook surface for a workspace AI feature.
5
+ */
6
+ export function buildAiFeaturePhpSource(aiFeatureSlug, namespace, phpPrefix, textDomain) {
7
+ const aiFeatureTitle = toTitleCase(aiFeatureSlug);
8
+ const aiFeaturePhpId = aiFeatureSlug.replace(/-/g, "_");
9
+ const loadSchemaFunctionName = `${phpPrefix}_${aiFeaturePhpId}_load_ai_feature_schema`;
10
+ const loadAiSchemaFunctionName = `${phpPrefix}_${aiFeaturePhpId}_load_ai_response_schema`;
11
+ const normalizeSchemaFunctionName = `${phpPrefix}_${aiFeaturePhpId}_sanitize_ai_feature_schema`;
12
+ const validatePayloadFunctionName = `${phpPrefix}_${aiFeaturePhpId}_validate_ai_feature_payload`;
13
+ const canManageFunctionName = `${phpPrefix}_${aiFeaturePhpId}_can_manage_ai_feature`;
14
+ const normalizePromptPayloadFunctionName = `${phpPrefix}_${aiFeaturePhpId}_normalize_ai_feature_prompt_payload`;
15
+ const buildPromptFunctionName = `${phpPrefix}_${aiFeaturePhpId}_build_ai_feature_prompt`;
16
+ const resolvePromptOptionsFunctionName = `${phpPrefix}_${aiFeaturePhpId}_resolve_ai_feature_prompt_options`;
17
+ const normalizeProviderTypeFunctionName = `${phpPrefix}_${aiFeaturePhpId}_normalize_provider_type`;
18
+ const buildTelemetryFunctionName = `${phpPrefix}_${aiFeaturePhpId}_build_ai_feature_telemetry`;
19
+ const resolveUnavailableMessageFunctionName = `${phpPrefix}_${aiFeaturePhpId}_resolve_ai_feature_unavailable_message`;
20
+ const isSupportedFunctionName = `${phpPrefix}_${aiFeaturePhpId}_is_ai_feature_supported`;
21
+ const adminNoticeFunctionName = `${phpPrefix}_${aiFeaturePhpId}_ai_feature_admin_notice`;
22
+ const handlerFunctionName = `${phpPrefix}_${aiFeaturePhpId}_handle_run_ai_feature`;
23
+ const registerRoutesFunctionName = `${phpPrefix}_${aiFeaturePhpId}_register_ai_feature_routes`;
24
+ const permissionFilterHook = `${phpPrefix}_${aiFeaturePhpId}_ai_feature_permission`;
25
+ const promptPayloadFilterHook = `${phpPrefix}_${aiFeaturePhpId}_ai_feature_prompt_payload`;
26
+ const promptFilterHook = `${phpPrefix}_${aiFeaturePhpId}_ai_feature_prompt`;
27
+ const promptOptionsFilterHook = `${phpPrefix}_${aiFeaturePhpId}_ai_feature_prompt_options`;
28
+ const adminNoticeMessageFilterHook = `${phpPrefix}_${aiFeaturePhpId}_ai_feature_admin_notice_message`;
29
+ const unavailableMessageFilterHook = `${phpPrefix}_${aiFeaturePhpId}_ai_feature_unavailable_message`;
30
+ const telemetryFilterHook = `${phpPrefix}_${aiFeaturePhpId}_ai_feature_telemetry`;
31
+ return `<?php
32
+ if ( ! defined( 'ABSPATH' ) ) {
33
+ \treturn;
34
+ }
35
+
36
+ /*
37
+ * Customization hooks for the ${aiFeatureTitle} AI feature:
38
+ *
39
+ * - ${quotePhpString(permissionFilterHook)} filters the default current_user_can( 'edit_posts' ) capability check.
40
+ * - ${quotePhpString(promptPayloadFilterHook)} filters the validated request payload array before prompt serialization.
41
+ * - ${quotePhpString(promptFilterHook)} filters the final prompt string after payload normalization.
42
+ * - ${quotePhpString(promptOptionsFilterHook)} filters prompt options with \`temperature\` and \`modelPreference\` keys.
43
+ * - ${quotePhpString(adminNoticeMessageFilterHook)} filters the wp-admin notice shown when AI support is unavailable.
44
+ * - ${quotePhpString(unavailableMessageFilterHook)} filters REST-facing unavailable messages by reason code.
45
+ * - ${quotePhpString(telemetryFilterHook)} filters the response telemetry array before schema validation. Return a schema-compatible array.
46
+ */
47
+
48
+ if ( ! function_exists( '${loadSchemaFunctionName}' ) ) {
49
+ \tfunction ${loadSchemaFunctionName}( $schema_name ) {
50
+ \t\t$project_root = dirname( __DIR__, 2 );
51
+ \t\t$schema_path = $project_root . '/src/ai-features/${aiFeatureSlug}/api-schemas/' . $schema_name . '.schema.json';
52
+ \t\tif ( ! file_exists( $schema_path ) ) {
53
+ \t\t\treturn null;
54
+ \t\t}
55
+
56
+ \t\t$decoded = json_decode( file_get_contents( $schema_path ), true );
57
+ \t\treturn is_array( $decoded ) ? $decoded : null;
58
+ \t}
59
+ }
60
+
61
+ if ( ! function_exists( '${loadAiSchemaFunctionName}' ) ) {
62
+ \tfunction ${loadAiSchemaFunctionName}() {
63
+ \t\t$project_root = dirname( __DIR__, 2 );
64
+ \t\t$schema_path = $project_root . '/src/ai-features/${aiFeatureSlug}/ai-schemas/feature-result.ai.schema.json';
65
+ \t\tif ( ! file_exists( $schema_path ) ) {
66
+ \t\t\treturn null;
67
+ \t\t}
68
+
69
+ \t\t$decoded = json_decode( file_get_contents( $schema_path ), true );
70
+ \t\treturn is_array( $decoded ) ? $decoded : null;
71
+ \t}
72
+ }
73
+
74
+ if ( ! function_exists( '${normalizeSchemaFunctionName}' ) ) {
75
+ \tfunction ${normalizeSchemaFunctionName}( $schema ) {
76
+ \t\tif ( ! is_array( $schema ) ) {
77
+ \t\t\treturn $schema;
78
+ \t\t}
79
+
80
+ \t\tunset( $schema['$schema'], $schema['title'] );
81
+
82
+ \t\tif ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {
83
+ \t\t\tforeach ( $schema['properties'] as $key => $property_schema ) {
84
+ \t\t\t\t$schema['properties'][ $key ] = ${normalizeSchemaFunctionName}( $property_schema );
85
+ \t\t\t}
86
+ \t\t}
87
+
88
+ \t\tif ( isset( $schema['items'] ) && is_array( $schema['items'] ) ) {
89
+ \t\t\t$schema['items'] = ${normalizeSchemaFunctionName}( $schema['items'] );
90
+ \t\t}
91
+
92
+ \t\treturn $schema;
93
+ \t}
94
+ }
95
+
96
+ if ( ! function_exists( '${validatePayloadFunctionName}' ) ) {
97
+ \tfunction ${validatePayloadFunctionName}( $value, $schema_name, $param_name ) {
98
+ \t\t$schema = ${loadSchemaFunctionName}( $schema_name );
99
+ \t\tif ( ! is_array( $schema ) ) {
100
+ \t\t\treturn new WP_Error( 'missing_schema', 'Missing AI feature schema.', array( 'status' => 500 ) );
101
+ \t\t}
102
+
103
+ \t\t$rest_schema = ${normalizeSchemaFunctionName}( $schema );
104
+ \t\t$validation = rest_validate_value_from_schema( $value, $rest_schema, $param_name );
105
+ \t\tif ( is_wp_error( $validation ) ) {
106
+ \t\t\treturn $validation;
107
+ \t\t}
108
+
109
+ \t\treturn rest_sanitize_value_from_schema( $value, $rest_schema, $param_name );
110
+ \t}
111
+ }
112
+
113
+ if ( ! function_exists( '${canManageFunctionName}' ) ) {
114
+ \tfunction ${canManageFunctionName}( WP_REST_Request $request = null ) {
115
+ \t\t$permission = apply_filters(
116
+ \t\t\t${quotePhpString(permissionFilterHook)},
117
+ \t\t\tcurrent_user_can( 'edit_posts' ),
118
+ \t\t\t$request
119
+ \t\t);
120
+ \t\tif ( is_wp_error( $permission ) ) {
121
+ \t\t\treturn $permission;
122
+ \t\t}
123
+ \t\treturn (bool) $permission;
124
+ \t}
125
+ }
126
+
127
+ if ( ! function_exists( '${normalizePromptPayloadFunctionName}' ) ) {
128
+ \tfunction ${normalizePromptPayloadFunctionName}( array $payload ) {
129
+ \t\t$normalized_payload = apply_filters(
130
+ \t\t\t${quotePhpString(promptPayloadFilterHook)},
131
+ \t\t\t$payload
132
+ \t\t);
133
+ \t\treturn is_array( $normalized_payload ) ? $normalized_payload : $payload;
134
+ \t}
135
+ }
136
+
137
+ if ( ! function_exists( '${buildPromptFunctionName}' ) ) {
138
+ \tfunction ${buildPromptFunctionName}( array $payload ) {
139
+ \t\t$normalized_payload = ${normalizePromptPayloadFunctionName}( $payload );
140
+ \t\t$prompt = sprintf(
141
+ \t\t\t'You are helping with the %1$s AI workflow. Read the JSON request payload and return JSON that matches the provided schema. Request payload: %2$s',
142
+ \t\t\t${quotePhpString(aiFeatureTitle)},
143
+ \t\t\twp_json_encode( $normalized_payload )
144
+ \t\t);
145
+ \t\t$filtered_prompt = apply_filters(
146
+ \t\t\t${quotePhpString(promptFilterHook)},
147
+ \t\t\t$prompt,
148
+ \t\t\t$normalized_payload,
149
+ \t\t\t$payload
150
+ \t\t);
151
+ \t\treturn is_string( $filtered_prompt ) && '' !== $filtered_prompt ? $filtered_prompt : $prompt;
152
+ \t}
153
+ }
154
+
155
+ if ( ! function_exists( '${resolvePromptOptionsFunctionName}' ) ) {
156
+ \tfunction ${resolvePromptOptionsFunctionName}( array $payload = array() ) {
157
+ \t\t$options = apply_filters(
158
+ \t\t\t${quotePhpString(promptOptionsFilterHook)},
159
+ \t\t\tarray(
160
+ \t\t\t\t'modelPreference' => array(),
161
+ \t\t\t\t'temperature' => 0.2,
162
+ \t\t\t),
163
+ \t\t\t$payload
164
+ \t\t);
165
+ \t\tif ( ! is_array( $options ) ) {
166
+ \t\t\t$options = array();
167
+ \t\t}
168
+
169
+ \t\t$temperature = 0.2;
170
+ \t\tif ( array_key_exists( 'temperature', $options ) ) {
171
+ \t\t\tif ( null === $options['temperature'] ) {
172
+ \t\t\t\t$temperature = null;
173
+ \t\t\t} elseif ( is_numeric( $options['temperature'] ) ) {
174
+ \t\t\t\t$temperature = (float) $options['temperature'];
175
+ \t\t\t}
176
+ \t\t}
177
+
178
+ \t\t$model_preferences = array();
179
+ \t\tif ( isset( $options['modelPreference'] ) ) {
180
+ \t\t\t$raw_model_preferences = $options['modelPreference'];
181
+ \t\t\tif ( is_string( $raw_model_preferences ) && '' !== $raw_model_preferences ) {
182
+ \t\t\t\t$model_preferences = array( $raw_model_preferences );
183
+ \t\t\t} elseif ( is_array( $raw_model_preferences ) ) {
184
+ \t\t\t\t$model_preferences = array_values(
185
+ \t\t\t\t\tarray_filter(
186
+ \t\t\t\t\t\tarray_map(
187
+ \t\t\t\t\t\t\tstatic function ( $candidate ) {
188
+ \t\t\t\t\t\t\t\tif ( is_string( $candidate ) && '' !== $candidate ) {
189
+ \t\t\t\t\t\t\t\t\treturn $candidate;
190
+ \t\t\t\t\t\t\t\t}
191
+ \t\t\t\t\t\t\t\tif ( ! is_array( $candidate ) ) {
192
+ \t\t\t\t\t\t\t\t\treturn null;
193
+ \t\t\t\t\t\t\t\t}
194
+
195
+ \t\t\t\t\t\t\t\t$normalized = array_values(
196
+ \t\t\t\t\t\t\t\t\tarray_filter(
197
+ \t\t\t\t\t\t\t\t\t\t$candidate,
198
+ \t\t\t\t\t\t\t\t\t\tstatic function ( $value ) {
199
+ \t\t\t\t\t\t\t\t\t\t\treturn is_string( $value ) && '' !== $value;
200
+ \t\t\t\t\t\t\t\t\t\t}
201
+ \t\t\t\t\t\t\t\t\t)
202
+ \t\t\t\t\t\t\t\t);
203
+
204
+ \t\t\t\t\t\t\t\treturn count( $normalized ) > 0 ? $normalized : null;
205
+ \t\t\t\t\t\t\t},
206
+ \t\t\t\t\t\t\t$raw_model_preferences
207
+ \t\t\t\t\t\t),
208
+ \t\t\t\t\t\tstatic function ( $candidate ) {
209
+ \t\t\t\t\t\t\treturn null !== $candidate;
210
+ \t\t\t\t\t\t}
211
+ \t\t\t\t\t)
212
+ \t\t\t\t);
213
+ \t\t\t}
214
+ \t\t}
215
+
216
+ \t\treturn array(
217
+ \t\t\t'modelPreference' => $model_preferences,
218
+ \t\t\t'temperature' => $temperature,
219
+ \t\t);
220
+ \t}
221
+ }
222
+
223
+ if ( ! function_exists( '${normalizeProviderTypeFunctionName}' ) ) {
224
+ \tfunction ${normalizeProviderTypeFunctionName}( $provider_type ) {
225
+ \t\tif ( is_object( $provider_type ) && isset( $provider_type->value ) && is_string( $provider_type->value ) ) {
226
+ \t\t\treturn $provider_type->value;
227
+ \t\t}
228
+
229
+ \t\treturn is_string( $provider_type ) && '' !== $provider_type ? $provider_type : 'cloud';
230
+ \t}
231
+ }
232
+
233
+ if ( ! function_exists( '${buildTelemetryFunctionName}' ) ) {
234
+ \tfunction ${buildTelemetryFunctionName}( $result, array $payload = array(), array $normalized_result = array() ) {
235
+ \t\tif (
236
+ \t\t\t! is_object( $result ) ||
237
+ \t\t\t! method_exists( $result, 'getId' ) ||
238
+ \t\t\t! method_exists( $result, 'getModelMetadata' ) ||
239
+ \t\t\t! method_exists( $result, 'getProviderMetadata' ) ||
240
+ \t\t\t! method_exists( $result, 'getTokenUsage' )
241
+ \t\t) {
242
+ \t\t\treturn new WP_Error(
243
+ \t\t\t\t'ai_client_result_shape',
244
+ \t\t\t\t'The current WordPress AI Client result object is missing telemetry helpers.',
245
+ \t\t\t\tarray( 'status' => 502 )
246
+ \t\t\t);
247
+ \t\t}
248
+
249
+ \t\t$model_metadata = $result->getModelMetadata();
250
+ \t\t$provider_metadata = $result->getProviderMetadata();
251
+ \t\t$token_usage = $result->getTokenUsage();
252
+
253
+ \t\tif (
254
+ \t\t\t! is_object( $model_metadata ) ||
255
+ \t\t\t! method_exists( $model_metadata, 'getId' ) ||
256
+ \t\t\t! method_exists( $model_metadata, 'getName' ) ||
257
+ \t\t\t! is_object( $provider_metadata ) ||
258
+ \t\t\t! method_exists( $provider_metadata, 'getId' ) ||
259
+ \t\t\t! method_exists( $provider_metadata, 'getName' ) ||
260
+ \t\t\t! method_exists( $provider_metadata, 'getType' ) ||
261
+ \t\t\t! is_object( $token_usage ) ||
262
+ \t\t\t! method_exists( $token_usage, 'getCompletionTokens' ) ||
263
+ \t\t\t! method_exists( $token_usage, 'getPromptTokens' ) ||
264
+ \t\t\t! method_exists( $token_usage, 'getTotalTokens' )
265
+ \t\t) {
266
+ \t\t\treturn new WP_Error(
267
+ \t\t\t\t'ai_client_result_shape',
268
+ \t\t\t\t'The current WordPress AI Client telemetry objects are missing expected getters.',
269
+ \t\t\t\tarray( 'status' => 502 )
270
+ \t\t\t);
271
+ \t\t}
272
+
273
+ \t\t$telemetry = array(
274
+ \t\t\t'modelId' => (string) $model_metadata->getId(),
275
+ \t\t\t'modelName' => (string) $model_metadata->getName(),
276
+ \t\t\t'providerId' => (string) $provider_metadata->getId(),
277
+ \t\t\t'providerName' => (string) $provider_metadata->getName(),
278
+ \t\t\t'providerType' => ${normalizeProviderTypeFunctionName}( $provider_metadata->getType() ),
279
+ \t\t\t'resultId' => (string) $result->getId(),
280
+ \t\t\t'tokenUsage' => array(
281
+ \t\t\t\t'completionTokens' => (int) $token_usage->getCompletionTokens(),
282
+ \t\t\t\t'promptTokens' => (int) $token_usage->getPromptTokens(),
283
+ \t\t\t\t'totalTokens' => (int) $token_usage->getTotalTokens(),
284
+ \t\t\t),
285
+ \t\t);
286
+
287
+ \t\tif ( method_exists( $token_usage, 'getThoughtTokens' ) ) {
288
+ \t\t\t$thought_tokens = $token_usage->getThoughtTokens();
289
+ \t\t\tif ( null !== $thought_tokens ) {
290
+ \t\t\t\t$telemetry['tokenUsage']['thoughtTokens'] = (int) $thought_tokens;
291
+ \t\t\t}
292
+ \t\t}
293
+
294
+ \t\t$filtered_telemetry = apply_filters(
295
+ \t\t\t${quotePhpString(telemetryFilterHook)},
296
+ \t\t\t$telemetry,
297
+ \t\t\t$result,
298
+ \t\t\t$payload,
299
+ \t\t\t$normalized_result
300
+ \t\t);
301
+ \t\treturn is_array( $filtered_telemetry ) ? $filtered_telemetry : $telemetry;
302
+ \t}
303
+ }
304
+
305
+ if ( ! function_exists( '${resolveUnavailableMessageFunctionName}' ) ) {
306
+ \tfunction ${resolveUnavailableMessageFunctionName}( $message, $reason, array $context = array() ) {
307
+ \t\t$filtered_message = apply_filters(
308
+ \t\t\t${quotePhpString(unavailableMessageFilterHook)},
309
+ \t\t\t$message,
310
+ \t\t\t$reason,
311
+ \t\t\t$context
312
+ \t\t);
313
+ \t\treturn is_string( $filtered_message ) && '' !== $filtered_message ? $filtered_message : $message;
314
+ \t}
315
+ }
316
+
317
+ if ( ! function_exists( '${isSupportedFunctionName}' ) ) {
318
+ \tfunction ${isSupportedFunctionName}( array $payload = array(), $cache_result = true ) {
319
+ \t\tstatic $is_supported = null;
320
+ \t\t$use_cache = $cache_result && count( $payload ) === 0;
321
+ \t\tif ( $use_cache && null !== $is_supported ) {
322
+ \t\t\treturn $is_supported;
323
+ \t\t}
324
+
325
+ \t\tif ( ! function_exists( 'wp_ai_client_prompt' ) ) {
326
+ \t\t\tif ( $use_cache ) {
327
+ \t\t\t\t$is_supported = false;
328
+ \t\t\t}
329
+ \t\t\treturn false;
330
+ \t\t}
331
+
332
+ \t\t$schema = ${loadAiSchemaFunctionName}();
333
+ \t\tif ( ! is_array( $schema ) ) {
334
+ \t\t\tif ( $use_cache ) {
335
+ \t\t\t\t$is_supported = false;
336
+ \t\t\t}
337
+ \t\t\treturn false;
338
+ \t\t}
339
+
340
+ \t\t$prompt = wp_ai_client_prompt( 'AI feature support probe.' );
341
+ \t\tif ( ! is_object( $prompt ) || ! method_exists( $prompt, 'as_json_response' ) ) {
342
+ \t\t\tif ( $use_cache ) {
343
+ \t\t\t\t$is_supported = false;
344
+ \t\t\t}
345
+ \t\t\treturn false;
346
+ \t\t}
347
+ \t\t$prompt_options = ${resolvePromptOptionsFunctionName}( $payload );
348
+ \t\tif (
349
+ \t\t\tarray_key_exists( 'temperature', $prompt_options ) &&
350
+ \t\t\tnull !== $prompt_options['temperature'] &&
351
+ \t\t\tmethod_exists( $prompt, 'using_temperature' )
352
+ \t\t) {
353
+ \t\t\t$prompt = $prompt->using_temperature( $prompt_options['temperature'] );
354
+ \t\t}
355
+ \t\tif (
356
+ \t\t\t! empty( $prompt_options['modelPreference'] ) &&
357
+ \t\t\tmethod_exists( $prompt, 'using_model_preference' )
358
+ \t\t) {
359
+ \t\t\t$prompt = $prompt->using_model_preference( ...$prompt_options['modelPreference'] );
360
+ \t\t}
361
+
362
+ \t\t$structured_prompt = $prompt->as_json_response( $schema );
363
+ \t\tif ( ! is_object( $structured_prompt ) ) {
364
+ \t\t\tif ( $use_cache ) {
365
+ \t\t\t\t$is_supported = false;
366
+ \t\t\t}
367
+ \t\t\treturn false;
368
+ \t\t}
369
+
370
+ \t\tif ( method_exists( $structured_prompt, 'is_supported_for_text_generation' ) ) {
371
+ \t\t\t$supported = (bool) $structured_prompt->is_supported_for_text_generation();
372
+ \t\t\tif ( $use_cache ) {
373
+ \t\t\t\t$is_supported = $supported;
374
+ \t\t\t}
375
+ \t\t\treturn $supported;
376
+ \t\t}
377
+
378
+ \t\t$supported = method_exists( $structured_prompt, 'generate_text_result' );
379
+ \t\tif ( $use_cache ) {
380
+ \t\t\t$is_supported = $supported;
381
+ \t\t}
382
+ \t\treturn $supported;
383
+ \t}
384
+ }
385
+
386
+ if ( ! function_exists( '${adminNoticeFunctionName}' ) ) {
387
+ \tfunction ${adminNoticeFunctionName}() {
388
+ \t\tif ( ! current_user_can( 'manage_options' ) || ${isSupportedFunctionName}() ) {
389
+ \t\t\treturn;
390
+ \t\t}
391
+
392
+ \t\t$message = sprintf(
393
+ \t\t\t/* translators: %s: AI feature name. */
394
+ \t\t\t__( 'The %s AI feature is optional and remains disabled until the WordPress AI Client is available with structured text generation support for the generated schema.', ${quotePhpString(textDomain)} ),
395
+ \t\t\t${quotePhpString(aiFeatureTitle)}
396
+ \t\t);
397
+ \t\t$filtered_message = apply_filters(
398
+ \t\t\t${quotePhpString(adminNoticeMessageFilterHook)},
399
+ \t\t\t$message,
400
+ \t\t\tarray(
401
+ \t\t\t\t'featureSlug' => ${quotePhpString(aiFeatureSlug)},
402
+ \t\t\t\t'featureTitle' => ${quotePhpString(aiFeatureTitle)},
403
+ \t\t\t\t'namespace' => ${quotePhpString(namespace)},
404
+ \t\t\t)
405
+ \t\t);
406
+ \t\tif ( is_string( $filtered_message ) && '' !== $filtered_message ) {
407
+ \t\t\t$message = $filtered_message;
408
+ \t\t}
409
+ \t\tprintf( '<div class="notice notice-warning"><p>%s</p></div>', esc_html( $message ) );
410
+ \t}
411
+ }
412
+
413
+ if ( ! function_exists( '${handlerFunctionName}' ) ) {
414
+ \tfunction ${handlerFunctionName}( WP_REST_Request $request ) {
415
+ \t\t$payload = ${validatePayloadFunctionName}( $request->get_json_params(), 'feature-request', 'body' );
416
+ \t\tif ( is_wp_error( $payload ) ) {
417
+ \t\t\treturn $payload;
418
+ \t\t}
419
+
420
+ \t\tif ( ! ${isSupportedFunctionName}( $payload, false ) ) {
421
+ \t\t\treturn new WP_Error(
422
+ \t\t\t\t'ai_client_unavailable',
423
+ \t\t\t\t${resolveUnavailableMessageFunctionName}(
424
+ \t\t\t\t\t'The WordPress AI Client is unavailable or does not support this feature endpoint.',
425
+ \t\t\t\t\t'support_probe_failed',
426
+ \t\t\t\t\tarray(
427
+ \t\t\t\t\t\t'featureSlug' => ${quotePhpString(aiFeatureSlug)},
428
+ \t\t\t\t\t)
429
+ \t\t\t\t),
430
+ \t\t\t\tarray( 'status' => 501 )
431
+ \t\t\t);
432
+ \t\t}
433
+
434
+ \t\t$ai_schema = ${loadAiSchemaFunctionName}();
435
+ \t\tif ( ! is_array( $ai_schema ) ) {
436
+ \t\t\treturn new WP_Error(
437
+ \t\t\t\t'ai_client_schema_missing',
438
+ \t\t\t\t'The generated AI response schema is missing for this feature endpoint.',
439
+ \t\t\t\tarray( 'status' => 500 )
440
+ \t\t\t);
441
+ \t\t}
442
+
443
+ \t\t$prompt_options = ${resolvePromptOptionsFunctionName}( $payload );
444
+ \t\t$prompt = wp_ai_client_prompt( ${buildPromptFunctionName}( $payload ) );
445
+ \t\tif ( ! is_object( $prompt ) ) {
446
+ \t\t\treturn new WP_Error(
447
+ \t\t\t\t'ai_client_unavailable',
448
+ \t\t\t\t${resolveUnavailableMessageFunctionName}(
449
+ \t\t\t\t\t'The WordPress AI Client prompt builder is unavailable on this site.',
450
+ \t\t\t\t\t'prompt_builder_missing',
451
+ \t\t\t\t\tarray(
452
+ \t\t\t\t\t\t'featureSlug' => ${quotePhpString(aiFeatureSlug)},
453
+ \t\t\t\t\t)
454
+ \t\t\t\t),
455
+ \t\t\t\tarray( 'status' => 501 )
456
+ \t\t\t);
457
+ \t\t}
458
+
459
+ \t\tif (
460
+ \t\t\tarray_key_exists( 'temperature', $prompt_options ) &&
461
+ \t\t\tnull !== $prompt_options['temperature'] &&
462
+ \t\t\tmethod_exists( $prompt, 'using_temperature' )
463
+ \t\t) {
464
+ \t\t\t$prompt = $prompt->using_temperature( $prompt_options['temperature'] );
465
+ \t\t}
466
+ \t\tif (
467
+ \t\t\t! empty( $prompt_options['modelPreference'] ) &&
468
+ \t\t\tmethod_exists( $prompt, 'using_model_preference' )
469
+ \t\t) {
470
+ \t\t\t$prompt = $prompt->using_model_preference( ...$prompt_options['modelPreference'] );
471
+ \t\t}
472
+ \t\tif ( ! method_exists( $prompt, 'as_json_response' ) ) {
473
+ \t\t\treturn new WP_Error(
474
+ \t\t\t\t'ai_client_unavailable',
475
+ \t\t\t\t${resolveUnavailableMessageFunctionName}(
476
+ \t\t\t\t\t'The current WordPress AI Client does not expose as_json_response().',
477
+ \t\t\t\t\t'as_json_response_missing',
478
+ \t\t\t\t\tarray(
479
+ \t\t\t\t\t\t'featureSlug' => ${quotePhpString(aiFeatureSlug)},
480
+ \t\t\t\t\t)
481
+ \t\t\t\t),
482
+ \t\t\t\tarray( 'status' => 501 )
483
+ \t\t\t);
484
+ \t\t}
485
+
486
+ \t\t$structured_prompt = $prompt->as_json_response( $ai_schema );
487
+ \t\tif ( ! is_object( $structured_prompt ) ) {
488
+ \t\t\treturn new WP_Error(
489
+ \t\t\t\t'ai_client_unavailable',
490
+ \t\t\t\t${resolveUnavailableMessageFunctionName}(
491
+ \t\t\t\t\t'The current WordPress AI Client could not prepare a structured-output prompt.',
492
+ \t\t\t\t\t'structured_prompt_missing',
493
+ \t\t\t\t\tarray(
494
+ \t\t\t\t\t\t'featureSlug' => ${quotePhpString(aiFeatureSlug)},
495
+ \t\t\t\t\t)
496
+ \t\t\t\t),
497
+ \t\t\t\tarray( 'status' => 501 )
498
+ \t\t\t);
499
+ \t\t}
500
+
501
+ \t\tif (
502
+ \t\t\tmethod_exists( $structured_prompt, 'is_supported_for_text_generation' ) &&
503
+ \t\t\t! $structured_prompt->is_supported_for_text_generation()
504
+ \t\t) {
505
+ \t\t\treturn new WP_Error(
506
+ \t\t\t\t'ai_client_unavailable',
507
+ \t\t\t\t${resolveUnavailableMessageFunctionName}(
508
+ \t\t\t\t\t'The current WordPress AI Client provider or model does not support this structured-output feature.',
509
+ \t\t\t\t\t'text_generation_unsupported',
510
+ \t\t\t\t\tarray(
511
+ \t\t\t\t\t\t'featureSlug' => ${quotePhpString(aiFeatureSlug)},
512
+ \t\t\t\t\t)
513
+ \t\t\t\t),
514
+ \t\t\t\tarray( 'status' => 501 )
515
+ \t\t\t);
516
+ \t\t}
517
+ \t\tif ( ! method_exists( $structured_prompt, 'generate_text_result' ) ) {
518
+ \t\t\treturn new WP_Error(
519
+ \t\t\t\t'ai_client_unavailable',
520
+ \t\t\t\t${resolveUnavailableMessageFunctionName}(
521
+ \t\t\t\t\t'The current WordPress AI Client does not expose generate_text_result() after as_json_response().',
522
+ \t\t\t\t\t'generate_text_result_missing',
523
+ \t\t\t\t\tarray(
524
+ \t\t\t\t\t\t'featureSlug' => ${quotePhpString(aiFeatureSlug)},
525
+ \t\t\t\t\t)
526
+ \t\t\t\t),
527
+ \t\t\t\tarray( 'status' => 501 )
528
+ \t\t\t);
529
+ \t\t}
530
+
531
+ \t\t$result = $structured_prompt->generate_text_result();
532
+ \t\tif ( is_wp_error( $result ) ) {
533
+ \t\t\treturn $result;
534
+ \t\t}
535
+ \t\tif ( ! is_object( $result ) || ! method_exists( $result, 'toText' ) ) {
536
+ \t\t\treturn new WP_Error(
537
+ \t\t\t\t'ai_client_result_shape',
538
+ \t\t\t\t'The current WordPress AI Client result does not expose toText().',
539
+ \t\t\t\tarray( 'status' => 502 )
540
+ \t\t\t);
541
+ \t\t}
542
+
543
+ \t\t$decoded_result = json_decode( $result->toText(), true );
544
+ \t\tif ( ! is_array( $decoded_result ) ) {
545
+ \t\t\treturn new WP_Error(
546
+ \t\t\t\t'ai_client_invalid_json',
547
+ \t\t\t\t'The AI feature response did not decode to a JSON object.',
548
+ \t\t\t\tarray( 'status' => 502 )
549
+ \t\t\t);
550
+ \t\t}
551
+
552
+ \t\t$normalized_result = ${validatePayloadFunctionName}( $decoded_result, 'feature-result', 'result' );
553
+ \t\tif ( is_wp_error( $normalized_result ) ) {
554
+ \t\t\treturn new WP_Error(
555
+ \t\t\t\t'ai_client_invalid_response',
556
+ \t\t\t\t$normalized_result->get_error_message(),
557
+ \t\t\t\tarray( 'status' => 502 )
558
+ \t\t\t);
559
+ \t\t}
560
+
561
+ \t\t$telemetry = ${buildTelemetryFunctionName}( $result, $payload, $normalized_result );
562
+ \t\tif ( is_wp_error( $telemetry ) ) {
563
+ \t\t\treturn $telemetry;
564
+ \t\t}
565
+
566
+ \t\t$response = ${validatePayloadFunctionName}(
567
+ \t\t\tarray(
568
+ \t\t\t\t'result' => $normalized_result,
569
+ \t\t\t\t'telemetry' => $telemetry,
570
+ \t\t\t),
571
+ \t\t\t'feature-response',
572
+ \t\t\t'response'
573
+ \t\t);
574
+ \t\tif ( is_wp_error( $response ) ) {
575
+ \t\t\treturn new WP_Error(
576
+ \t\t\t\t'ai_client_invalid_response',
577
+ \t\t\t\t$response->get_error_message(),
578
+ \t\t\t\tarray( 'status' => 502 )
579
+ \t\t\t);
580
+ \t\t}
581
+
582
+ \t\treturn rest_ensure_response( $response );
583
+ \t}
584
+ }
585
+
586
+ if ( ! function_exists( '${registerRoutesFunctionName}' ) ) {
587
+ \tfunction ${registerRoutesFunctionName}() {
588
+ \t\tregister_rest_route(
589
+ \t\t\t${quotePhpString(namespace)},
590
+ \t\t\t'/ai/${aiFeatureSlug}',
591
+ \t\t\tarray(
592
+ \t\t\t\tarray(
593
+ \t\t\t\t\t'methods' => WP_REST_Server::CREATABLE,
594
+ \t\t\t\t\t'callback' => '${handlerFunctionName}',
595
+ \t\t\t\t\t'permission_callback' => '${canManageFunctionName}',
596
+ \t\t\t\t)
597
+ \t\t\t)
598
+ \t\t);
599
+ \t}
600
+ }
601
+
602
+ add_action( 'admin_notices', '${adminNoticeFunctionName}' );
603
+ add_action( 'rest_api_init', '${registerRoutesFunctionName}' );
604
+ `;
605
+ }