44 *
55 * Triggers A/B variant image generation from comic script
66 * Pipeline: Script → SVG Description → Image Rendering
7+ *
8+ * Integrates ledgrrr workflows for:
9+ * - Image rendering governance and audit tracking
10+ * - Forecasting and logging metrics
711 */
812
913import { isValidDay } from '../lib/comic-response.ts' ;
@@ -14,6 +18,7 @@ import {
1418 getAvailableProviders ,
1519} from '../lib/image-providers.ts' ;
1620import { generateVariantPrompts } from '../lib/svg-prompt-generator.ts' ;
21+ import { invokeWorkflow } from '../lib/ledgrrr-mcp-client.ts' ;
1722
1823export async function onRequestPost ( context : any ) {
1924 const { request, env } = context ;
@@ -135,6 +140,24 @@ export async function onRequestPost(context: any) {
135140 ) ;
136141 }
137142
143+ // === Image Rendering Governance (Ledgrrr) ===
144+ let auditImageResult : any = null ;
145+ try {
146+ auditImageResult = await invokeWorkflow ( 'image_rendering' , {
147+ script_a : script . variant_a || script . a || '' ,
148+ script_b : script . variant_b || script . b || '' ,
149+ image_a_path : `pending/${ day } /variant-a.jpg` ,
150+ image_b_path : `pending/${ day } /variant-b.jpg` ,
151+ } ) ;
152+
153+ if ( ! auditImageResult . success && auditImageResult . error ) {
154+ console . warn ( '[image-generate] Image rendering audit failed (non-blocking):' , auditImageResult . error ) ;
155+ }
156+ } catch ( err : any ) {
157+ console . warn ( '[image-generate] Image rendering governance error (non-blocking):' , err . message ) ;
158+ // Graceful degradation: continue without ledgrrr if unavailable
159+ }
160+
138161 // === Store in R2 ===
139162 const r2Keys = {
140163 imageA : null as string | null ,
@@ -157,16 +180,49 @@ export async function onRequestPost(context: any) {
157180 r2Keys . imageB = keyB ;
158181 }
159182
183+ // === Forecasting & Logging (Ledgrrr) ===
184+ let auditForecastResult : any = null ;
185+ try {
186+ // Calculate image quality scores (placeholder metrics)
187+ const imageQualityA = images . imageA ? 0.85 : 0 ;
188+ const imageQualityB = images . imageB ? 0.82 : 0 ;
189+ const imageASize = images . imageA ?. imageBuffer ?. byteLength || 0 ;
190+ const imageBSize = images . imageB ?. imageBuffer ?. byteLength || 0 ;
191+
192+ auditForecastResult = await invokeWorkflow ( 'forecasting_and_log' , {
193+ image_a_path : r2Keys . imageA || '' ,
194+ image_b_path : r2Keys . imageB || '' ,
195+ variant_a_score : imageQualityA ,
196+ variant_b_score : imageQualityB ,
197+ day : day ,
198+ metrics : {
199+ script_length_a : ( script . variant_a || script . a || '' ) . length ,
200+ script_length_b : ( script . variant_b || script . b || '' ) . length ,
201+ image_size_a : imageASize ,
202+ image_size_b : imageBSize ,
203+ } ,
204+ } ) ;
205+
206+ if ( ! auditForecastResult . success && auditForecastResult . error ) {
207+ console . warn ( '[image-generate] Forecasting audit failed (non-blocking):' , auditForecastResult . error ) ;
208+ }
209+ } catch ( err : any ) {
210+ console . warn ( '[image-generate] Forecasting governance error (non-blocking):' , err . message ) ;
211+ // Graceful degradation: continue without ledgrrr if unavailable
212+ }
213+
160214 // === Save to database ===
161215 await env . DB . prepare (
162216 `
163- INSERT INTO comics (day, r2_key_a, r2_key_b, model_provider_a, model_provider_b)
164- VALUES (?, ?, ?, ?, ?)
217+ INSERT INTO comics (day, r2_key_a, r2_key_b, model_provider_a, model_provider_b, audit_log_render, audit_log_forecast )
218+ VALUES (?, ?, ?, ?, ?, ?, ? )
165219 ON CONFLICT(day) DO UPDATE SET
166220 r2_key_a = COALESCE(?, r2_key_a),
167221 r2_key_b = COALESCE(?, r2_key_b),
168222 model_provider_a = ?,
169- model_provider_b = ?
223+ model_provider_b = ?,
224+ audit_log_render = ?,
225+ audit_log_forecast = ?
170226 `
171227 )
172228 . bind (
@@ -175,10 +231,14 @@ export async function onRequestPost(context: any) {
175231 r2Keys . imageB ,
176232 env . IMAGE_PROVIDER_A || 'unknown' ,
177233 env . IMAGE_PROVIDER_B || 'unknown' ,
234+ auditImageResult ? JSON . stringify ( auditImageResult . audit_entry ) : null ,
235+ auditForecastResult ? JSON . stringify ( auditForecastResult . audit_entry ) : null ,
178236 r2Keys . imageA ,
179237 r2Keys . imageB ,
180238 env . IMAGE_PROVIDER_A || 'unknown' ,
181- env . IMAGE_PROVIDER_B || 'unknown'
239+ env . IMAGE_PROVIDER_B || 'unknown' ,
240+ auditImageResult ? JSON . stringify ( auditImageResult . audit_entry ) : null ,
241+ auditForecastResult ? JSON . stringify ( auditForecastResult . audit_entry ) : null
182242 )
183243 . run ( ) ;
184244
@@ -195,6 +255,20 @@ export async function onRequestPost(context: any) {
195255 renderer : renderer . constructor . name ,
196256 promptsGenerated : true ,
197257 } ,
258+ audit_trail : {
259+ image_rendering : auditImageResult ? {
260+ success : auditImageResult . success ,
261+ entry_id : auditImageResult . audit_entry ?. entry_id ,
262+ timestamp : auditImageResult . audit_entry ?. timestamp ,
263+ status : auditImageResult . audit_entry ?. status ,
264+ } : null ,
265+ forecasting_and_log : auditForecastResult ? {
266+ success : auditForecastResult . success ,
267+ entry_id : auditForecastResult . audit_entry ?. entry_id ,
268+ timestamp : auditForecastResult . audit_entry ?. timestamp ,
269+ status : auditForecastResult . audit_entry ?. status ,
270+ } : null ,
271+ } ,
198272 } ) ;
199273 } catch ( err : any ) {
200274 console . error ( 'Image generation error:' , err ) ;
0 commit comments