Spaces:
Sleeping
Sleeping
| /** | |
| * User Session Store | |
| * Simple in-memory storage for user settings (active preset, etc). | |
| * In a real app, this should be a database (Postgres, MongoDB). | |
| */ | |
| const userSessions = {}; | |
| const { stylePresets } = require('../../prompts/stylePresets'); | |
| // Default preset (usually the first one) | |
| const DEFAULT_PRESET_NAME = stylePresets[0].preset_name; | |
| /** | |
| * Returns the user session or initializes it with default values. | |
| * @param {number|string} userId | |
| */ | |
| function getUserSession(userId) { | |
| if (!userSessions[userId]) { | |
| userSessions[userId] = { | |
| currentPreset: DEFAULT_PRESET_NAME, | |
| creativity: 0.7, | |
| tags: "#fblifestyle #kindnessmatters #kindness", | |
| lastPrompt: null, | |
| lastStory: null, | |
| lastImage: null, | |
| awaitingEdit: false, | |
| awaitingImageEdit: false | |
| }; | |
| } | |
| return userSessions[userId]; | |
| } | |
| /** | |
| * Updates various user settings in the session. | |
| */ | |
| function updateUserSession(userId, data) { | |
| const session = getUserSession(userId); | |
| // Update Preset if provided | |
| if (data.presetName) { | |
| const preset = stylePresets.find(p => p.preset_name === data.presetName); | |
| if (preset) { | |
| session.currentPreset = data.presetName; | |
| } | |
| } | |
| // Update Creativity if provided | |
| if (data.creativity !== undefined) { | |
| session.creativity = parseFloat(data.creativity); | |
| } | |
| // Update Tags if provided | |
| if (data.tags !== undefined) { | |
| session.tags = data.tags; | |
| } | |
| return true; | |
| } | |
| /** | |
| * Saves the last generation context for editing. | |
| */ | |
| function updateLastGeneration(userId, prompt, story) { | |
| const session = getUserSession(userId); | |
| session.lastPrompt = prompt; | |
| session.lastStory = story; | |
| return true; | |
| } | |
| /** | |
| * Saves the last generated image. | |
| */ | |
| function updateLastImage(userId, imageBase64) { | |
| const session = getUserSession(userId); | |
| session.lastImage = imageBase64; | |
| return true; | |
| } | |
| /** | |
| * Sets the "awaiting edit message" state. | |
| */ | |
| function setAwaitingEdit(userId, value) { | |
| const session = getUserSession(userId); | |
| session.awaitingEdit = !!value; | |
| } | |
| function setAwaitingImageEdit(userId, value) { | |
| const session = getUserSession(userId); | |
| session.awaitingImageEdit = !!value; | |
| } | |
| /** | |
| * Returns the full preset object for the user's current choice. | |
| */ | |
| function getCurrentPreset(userId) { | |
| const session = getUserSession(userId); | |
| return stylePresets.find(p => p.preset_name === session.currentPreset) || stylePresets[0]; | |
| } | |
| module.exports = { | |
| getUserSession, | |
| updateUserSession, | |
| getCurrentPreset, | |
| updateLastGeneration, | |
| updateLastImage, | |
| setAwaitingEdit, | |
| setAwaitingImageEdit | |
| }; | |