import "server-only"; import { z } from "zod"; const stringArraySchema = z.array(z.string().trim()).optional(); const nullableStringArraySchema = stringArraySchema.nullable().optional(); export const conversationStateEventSchemas = { prompt_only_ready: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), optimizedPrompt: z.string().min(1), imageSummary: z.string().optional(), sourceImageUrls: nullableStringArraySchema, }), grid_confirmation_requested: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), pendingGridConfirmation: z.object({ gridType: z.enum(["grid_2x2", "grid_3x2", "grid_3x3"]), shotCount: z.union([z.literal(4), z.literal(6), z.literal(9)]), label: z.string().min(1), originalPrompt: z.string().min(1), }), }), clarification_requested: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), clarificationMessage: z.string().min(1), currentIntent: z.string().nullable().optional(), }), storyboard_confirmed: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), taskId: z.string().min(1).optional(), status: z.string().optional(), }), storyboard_execution_started: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), taskId: z.string().min(1), status: z.string().optional(), }), image_workflow_updated: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), currentIntent: z.enum(["image_gen", "image_edit", "vision"]), currentSubIntent: z.string().nullable().optional(), currentFocusAssetIds: nullableStringArraySchema, currentFocusImageUrls: nullableStringArraySchema, lastGeneratedAssetIds: nullableStringArraySchema, lastGeneratedImageUrls: nullableStringArraySchema, lastResolvedTargetAssetIds: nullableStringArraySchema, lastResolvedTargetImageUrls: nullableStringArraySchema, lastMessageId: z.string().uuid(), lastUserPrompt: z.string(), lastOptimizedPrompt: z.string().optional(), lastRequestedImageCount: z.number().int().min(1).max(12).optional(), lastAspectRatio: z.string().optional(), lastImageSize: z.string().optional(), lastSourceImageUrls: nullableStringArraySchema, lastResultImageUrls: nullableStringArraySchema, }), storyboard_revision_requested: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), taskId: z.string().min(1), status: z.string().optional(), }), storyboard_execution_completed: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), taskId: z.string().min(1), status: z.string().optional(), successCount: z.number().int().min(0).optional(), failedCount: z.number().int().min(0).optional(), firstVideoUrl: z.string().optional(), }), video_generation_started: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), source: z.string().min(1), providerType: z.string().optional(), modelRef: z.string().optional(), mode: z.string().optional(), taskId: z.string().optional(), status: z.string().optional(), }), video_generation_completed: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), source: z.string().min(1), providerType: z.string().optional(), modelRef: z.string().optional(), mode: z.string().optional(), taskId: z.string().optional(), status: z.string().optional(), temporaryVideoUrl: z.string().optional(), }), video_generation_failed: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), source: z.string().min(1), providerType: z.string().optional(), modelRef: z.string().optional(), mode: z.string().optional(), taskId: z.string().optional(), status: z.string().optional(), error: z.string().optional(), }), target_resolved: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), currentIntent: z.string().nullable().optional(), currentFocusAssetIds: nullableStringArraySchema, currentFocusImageUrls: nullableStringArraySchema, selectedAssetIds: nullableStringArraySchema, selectedImageUrls: nullableStringArraySchema, lastUploadedAssetIds: nullableStringArraySchema, lastUploadedImageUrls: nullableStringArraySchema, lastReferencedAssetIds: nullableStringArraySchema, lastReferencedImageUrls: nullableStringArraySchema, lastResolvedTargetAssetIds: nullableStringArraySchema, lastResolvedTargetImageUrls: nullableStringArraySchema, activeCanvasNodeIds: nullableStringArraySchema, lastCanvasSnapshot: z.unknown().optional(), lastMessageId: z.string().uuid(), text: z.string(), resolutionMode: z.string(), confidence: z.number(), }), video_workflow_updated: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), lastMessageId: z.string().uuid(), text: z.string().optional(), status: z.string().optional(), }), workflow_cancelled: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), messageId: z.string().uuid(), text: z.string(), workflowType: z.string().min(1), workflowId: z.string().min(1).optional(), status: z.string().optional(), }), agent_preset_completed: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), currentIntent: z.enum(["image_gen", "video_gen"]), lastMessageId: z.string().uuid(), text: z.string(), agentPresetId: z.string().uuid(), agentPresetName: z.string().min(1), slotCount: z.number().int().min(0), successCount: z.number().int().min(0), }), storyboard_assistant_updated: z.object({ userId: z.string().uuid(), chatId: z.string().uuid(), projectId: z.string().uuid().nullable().optional(), lastMessageId: z.string().uuid(), text: z.string(), storyboardGridType: z.enum(["grid_2x2", "grid_3x2", "grid_3x3"]).optional(), storyboardShotCount: z.union([z.literal(4), z.literal(6), z.literal(9)]).optional(), }), } as const; export type ConversationStateEventType = keyof typeof conversationStateEventSchemas; export type ConversationStateEventPayload = z.infer< (typeof conversationStateEventSchemas)[T] >; export type ConversationStateEvent = { [K in ConversationStateEventType]: { type: K; payload: ConversationStateEventPayload; }; }[ConversationStateEventType];