Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /** * Orchestrator task for generating number-line postcards. * * Coordinates subtasks: * 1. Render reference scenes (inline) * 2. Loop: postcard-image-generate → postcard-review (max 3 attempts) * 3. postcard-thumbnail-generate * 4. Update DB + notify user * * Each subtask is a first-class background task with its own events, * progress tracking, and admin visibility. */ import { createTask, awaitTask } from '../task-manager' import { getImageProvider } from '../image-providers' import { storeImage, readPersistentImage } from '../image-storage' import { startPostcardImageGenerate } from './postcard-image-generate' import { startPostcardReview } from './postcard-review' import { startPostcardThumbnailGenerate } from './postcard-thumbnail-generate' import { db } from '@/db' import * as schema from '@/db/schema' import { eq } from 'drizzle-orm' import type { PostcardGenerateEvent } from './events' import type { PostcardReviewOutput } from './postcard-review' const MAX_REVIEW_ATTEMPTS = 3 export interface PostcardGenerateInput { postcardId: string userId: string } export interface PostcardGenerateOutput { postcardId: string imageUrl?: string thumbnailUrl?: string status: 'ready' | 'failed' error?: string } export async function startPostcardGenerate(input: PostcardGenerateInput): Promise<string> { return createTask<PostcardGenerateInput, PostcardGenerateOutput, PostcardGenerateEvent>( 'postcard-generate', input, async (handle, config) => { const { postcardId } = config const parentTaskId = handle.id // 1. Load postcard record const [postcard] = await db .select() .from(schema.numberLinePostcards) .where(eq(schema.numberLinePostcards.id, postcardId)) .limit(1) if (!postcard) { handle.fail(`Postcard ${postcardId} not found`) return } const manifest = postcard.manifest // 2. Update status to generating await db .update(schema.numberLinePostcards) .set({ status: 'generating', updatedAt: new Date() }) .where(eq(schema.numberLinePostcards.id, postcardId)) handle.emit({ type: 'postcard_rendering', postcardId }) handle.setProgress(5, 'Preparing number line scene') // 3. Render reference scenes and store for subtask access let referenceImageKey: string | undefined try { const { createServerCanvas } = await import('../server-canvas') const { renderMomentScene } = await import( '@/components/toys/number-line/renderMomentScene' ) const momentCount = Math.min(manifest.moments.length, 4) let referenceImage: Buffer | undefined if (momentCount === 1) { const width = 800 const height = 600 const canvas = createServerCanvas(width, height) const ctx = canvas.getContext('2d') renderMomentScene( ctx as unknown as CanvasRenderingContext2D, manifest.moments[0].snapshot, width, height ) referenceImage = canvas.toBuffer('image/png') } else if (momentCount > 1) { const tileW = 400 const tileH = 300 const canvas = createServerCanvas(800, 600) const ctx = canvas.getContext('2d') for (let i = 0; i < momentCount; i++) { const tileCanvas = createServerCanvas(tileW, tileH) const tileCtx = tileCanvas.getContext('2d') renderMomentScene( tileCtx as unknown as CanvasRenderingContext2D, manifest.moments[i].snapshot, tileW, tileH ) const col = i % 2 const row = Math.floor(i / 2) ;(ctx as unknown as CanvasRenderingContext2D).drawImage( tileCanvas as unknown as CanvasImageSource, col * tileW, row * tileH ) } referenceImage = canvas.toBuffer('image/png') } if (referenceImage) { // Store reference image so subtasks can read it by key const refFilename = `${postcardId}-reference.png` storeImage( { type: 'persistent', category: 'postcards', filename: refFilename }, referenceImage ) referenceImageKey = `postcards/${refFilename}` handle.setProgress(10, 'Number line scene rendered') } } catch (err) { console.warn('[postcard-generate] Scene render failed, continuing without reference:', err) handle.setProgress(10, 'Skipping scene render, generating image directly') } // 4. Select provider const gemini = getImageProvider('gemini') const openai = getImageProvider('openai') const provider = gemini?.isAvailable() ? gemini : openai?.isAvailable() ? openai : null if (!provider) { await db .update(schema.numberLinePostcards) .set({ status: 'failed', updatedAt: new Date() }) .where(eq(schema.numberLinePostcards.id, postcardId)) handle.fail('No image generation provider available') return } const callerNum = manifest.callerNumber const displayNum = Number.isInteger(callerNum) ? callerNum.toString() : callerNum.toPrecision(6) const momentDescriptions = manifest.moments .slice(0, 4) .map((m, i) => `${i + 1}. "${m.caption}" (${m.category})`) .join('\n') const hasScreenshots = !!referenceImageKey const basePrompt = [ `Create a postcard commemorating a phone call between a child named ${manifest.childName}${manifest.childAge ? ` (age ${manifest.childAge})` : ''} and the number ${displayNum} on the number line.`, ``, `The number's personality: ${manifest.callerPersonality}`, ``, `Key moments from the call:`, momentDescriptions, ``, `Session summary: ${manifest.sessionSummary}`, ``, hasScreenshots ? `IMPORTANT: The attached image contains actual screenshots from the call showing the number line at key moments. These screenshots should be incorporated into the postcard as "photos" — like snapshots pinned to a scrapbook or corkboard. They are the real visual record of the experience. Frame them, tilt them slightly, add decorative tape or pins, but keep the screenshot content clearly visible and recognizable. The postcard should feel like a collage of memories from the call.` : `Style: warm, playful, mathematical. Include the number ${displayNum} prominently with number line elements.`, ``, `The overall postcard should feel warm, playful, and nostalgic — like a keepsake from a fun mathematical adventure. Include the number ${displayNum} as a character or prominent element.`, ].join('\n') const modelId = provider.meta.id === 'gemini' ? 'gemini-3-pro-image-preview' : provider.meta.models[0].id handle.emit({ type: 'postcard_generating_image', postcardId, provider: provider.meta.id, model: modelId, }) // 5. Generate-review loop via subtasks let acceptedImagePath: string | undefined let reviewFeedback: string | undefined try { for (let attempt = 1; attempt <= MAX_REVIEW_ATTEMPTS; attempt++) { if (handle.isCancelled()) return const prompt = reviewFeedback ? `${basePrompt}\n\n${reviewFeedback}` : basePrompt handle.setProgress( 15 + attempt * 15, `Generating image (attempt ${attempt}/${MAX_REVIEW_ATTEMPTS})` ) // Subtask: generate image const genTaskId = await startPostcardImageGenerate( { postcardId, prompt, providerId: provider.meta.id, modelId, attempt, referenceImageKey, _userId: input.userId, }, input.userId, parentTaskId ) const genState = await awaitTask<{ postcardId: string; imagePath: string }>(genTaskId) const imagePath = genState.output!.imagePath handle.emit({ type: 'postcard_reviewing', postcardId, attempt }) handle.setProgress( 20 + attempt * 15, `Reviewing image (attempt ${attempt}/${MAX_REVIEW_ATTEMPTS})` ) // Subtask: review image const reviewTaskId = await startPostcardReview( { postcardId, imagePath, manifest, hasReferenceScreenshots: hasScreenshots, previousFeedback: reviewFeedback, }, input.userId, parentTaskId ) const reviewState = await awaitTask<PostcardReviewOutput>(reviewTaskId) const reviewResult = reviewState.output! handle.emit({ type: 'postcard_review_result', postcardId, attempt, pass: reviewResult.pass, issues: reviewResult.criteriaResults.flatMap((r) => r.issues), }) if (reviewResult.pass) { acceptedImagePath = imagePath break } // Last attempt — accept what we have if (attempt === MAX_REVIEW_ATTEMPTS) { console.warn( `[postcard-generate] Review failed after ${MAX_REVIEW_ATTEMPTS} attempts, accepting best effort` ) acceptedImagePath = imagePath break } reviewFeedback = reviewResult.feedback } // 6. Copy accepted draft to final location const [draftCategory, draftFilename] = acceptedImagePath!.split('/') const draftData = await readPersistentImage(draftCategory, draftFilename) if (!draftData) { throw new Error(`Draft image not found at ${acceptedImagePath}`) } const { publicUrl: imageUrl } = storeImage( { type: 'persistent', category: 'postcards', filename: `${postcardId}.png` }, draftData.buffer ) // 7. Generate thumbnail via subtask handle.setProgress(80, 'Generating thumbnail') const thumbnailPrompt = [ `Create a simple, iconic thumbnail image for a postcard from the number ${displayNum}.`, `The number's personality: ${manifest.callerPersonality}.`, `Style: warm, colorful, mathematical. Show ${displayNum} as a friendly character.`, `Simple composition suitable for a small thumbnail. No text or words.`, ].join(' ') const thumbTaskId = await startPostcardThumbnailGenerate( { postcardId, prompt: thumbnailPrompt, providerId: provider.meta.id, modelId, _userId: input.userId, }, input.userId, parentTaskId ) const thumbState = await awaitTask<{ thumbnailUrl: string }>(thumbTaskId) const thumbnailUrl = thumbState.output!.thumbnailUrl // 8. Update postcard record await db .update(schema.numberLinePostcards) .set({ status: 'ready', imageUrl, thumbnailUrl, updatedAt: new Date(), }) .where(eq(schema.numberLinePostcards.id, postcardId)) handle.emit({ type: 'postcard_complete', postcardId, imageUrl, thumbnailUrl, }) // 9. Notify user try { const { bootstrapChannels } = await import('../notifications/bootstrap') bootstrapChannels() const { notifyUser } = await import('../notifications/dispatcher') await notifyUser(postcard.userId, { type: 'postcard-ready', data: { postcardId, callerNumber: manifest.callerNumber, imageUrl, thumbnailUrl, postcardUrl: `/my-stuff/postcards/${postcardId}`, }, }) } catch (notifyErr) { console.error('[postcard-generate] Failed to send notification:', notifyErr) } handle.complete({ postcardId, imageUrl, thumbnailUrl, status: 'ready', }) } catch (err) { const error = err instanceof Error ? err.message : 'Image generation failed' await db .update(schema.numberLinePostcards) .set({ status: 'failed', updatedAt: new Date() }) .where(eq(schema.numberLinePostcards.id, postcardId)) handle.emit({ type: 'postcard_error', postcardId, error }) handle.fail(error) } }, input.userId ) } |