UNPKG

nexus-react-core

Version:

A comprehensive React toolkit with services, hooks, and Redux store management

86 lines (83 loc) 3.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setOpenAIClient = void 0; exports.generateImage = generateImage; exports.getRandomPrompt = getRandomPrompt; let openaiClient = null; // Function to set the OpenAI client const setOpenAIClient = (client) => { openaiClient = client; }; exports.setOpenAIClient = setOpenAIClient; function buildPromptWithOptionalTarget(input, difficulty = "medium", includeTarget = true, isUserInterest = false) { // If this is a user interest, use a different prompt format if (isUserInterest) { // Simple cleanup const cleanInput = input.trim(); // Ultra simple prompt that works for all cases return ` Generate a mission image based on the user's interest: ${cleanInput}. The mission is to identify this subject, whether it's a brand, character, object, or category. Create a realistic 3D-rendered image of the subject in a natural, authentic setting. No text, labels, or overlays — just the clear, recognizable 3D subject. `; } // Otherwise, use the mission-based prompt return ` You are generating a high-quality 3D rendered image for a visual mission. Mission: "${input}" Difficulty: "${difficulty}" Instructions: - Ensure the character, brand, or object mentioned in the mission looks **exactly like its real or official appearance**. - The image must **not contain any text**, titles, or labels unless the mission specifically asks for it. - Follow these difficulty guidelines: • easy: The target is clearly visible with minimal background distractions. • medium: The target blends naturally into a moderately detailed environment. • hard: The target is subtly hidden in a complex, busy, and highly detailed scene. Visual Style: - Use modern 3D rendering. - Apply cinematic lighting and shadows. - Create an immersive, polished, and realistic composition. `; } async function generateImage(prompt, isUserInterest = false) { try { if (!openaiClient) { throw new Error("OpenAI client not initialized. Please call setOpenAIClient() first."); } const includeTarget = Math.random() < 0.7; const formattedPrompt = buildPromptWithOptionalTarget(prompt, "medium", includeTarget, isUserInterest); console.log("Sending request to DALL-E API with formatted prompt:", formattedPrompt); const response = await openaiClient.images.generate({ model: "dall-e-3", // Use DALL-E 3 model prompt: formattedPrompt, n: 1, // Generate 1 image size: "1792x1024", // Keep original size quality: "standard", response_format: "url", }); // Return the URL of the generated image const imageUrl = response?.data?.[0]?.url || ""; console.log("Received image URL:", imageUrl.substring(0, 30) + "..."); return imageUrl; } catch (error) { console.error("Error generating image:", error); // Instead of throwing, return fallback image return "/error.svg"; } } // Function to generate random prompts for variety function getRandomPrompt() { // some sophisticated mission prompts some brands and anime characters const mission = [ "Find Sonic the Hedgehog", "Find Goku", "Find Mario", "Find the Mario movie characters", "Find a Pokéball", "Find Pikachu", "Find Spider-Man", "Find Kratos", ]; const randomIndex = Math.floor(Math.random() * mission.length); return mission[randomIndex]; } //# sourceMappingURL=dalleService.js.map