auto-image-generation
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Avatar Generator Module
|
||||
* Handles automatic avatar generation for characters without images
|
||||
*/
|
||||
|
||||
import { executeSlashCommandsOnChatInput } from '../../../../../../../scripts/slash-commands.js';
|
||||
import { extensionSettings } from '../../core/state.js';
|
||||
import { saveSettings } from '../../core/persistence.js';
|
||||
|
||||
// Track pending avatar generations to avoid duplicate requests
|
||||
const pendingGenerations = new Set();
|
||||
|
||||
/**
|
||||
* Style presets for avatar generation prompts
|
||||
*/
|
||||
const STYLE_PRESETS = {
|
||||
'auto': 'portrait, fantasy character, RPG style',
|
||||
'fantasy': 'portrait, fantasy character, medieval RPG style, detailed face',
|
||||
'scifi': 'portrait, sci-fi character, futuristic, cyberpunk style, detailed face',
|
||||
'anime': 'portrait, anime character, manga style, detailed face',
|
||||
'realistic': 'portrait, realistic character, detailed face, photorealistic'
|
||||
};
|
||||
|
||||
/**
|
||||
* Builds the generation prompt for a character
|
||||
* @param {string} characterName - Name of the character
|
||||
* @returns {string} Full prompt for /sd command
|
||||
*/
|
||||
function buildGenerationPrompt(characterName) {
|
||||
const style = STYLE_PRESETS[extensionSettings.avatarGenerationStyle] || STYLE_PRESETS.auto;
|
||||
const custom = extensionSettings.avatarGenerationPrompt || '';
|
||||
return `${style}, ${characterName}, ${custom}`.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an avatar for a character using /sd command
|
||||
* @param {string} characterName - Name of the character to generate avatar for
|
||||
* @returns {Promise<string|null>} Avatar URL or null if failed
|
||||
*/
|
||||
export async function generateAvatar(characterName) {
|
||||
// Skip if already generating
|
||||
if (pendingGenerations.has(characterName)) {
|
||||
console.log(`[RPG Avatar] Already generating avatar for: ${characterName}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Skip if disabled
|
||||
if (!extensionSettings.autoGenerateAvatars) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Skip if custom avatar already exists
|
||||
if (extensionSettings.npcAvatars && extensionSettings.npcAvatars[characterName]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
pendingGenerations.add(characterName);
|
||||
console.log(`[RPG Avatar] Starting generation for: ${characterName}`);
|
||||
|
||||
try {
|
||||
const prompt = buildGenerationPrompt(characterName);
|
||||
|
||||
// Execute /sd command with quiet=true
|
||||
// This saves to gallery without posting to chat
|
||||
const result = await executeSlashCommandsOnChatInput(
|
||||
`/sd ${prompt} quiet=true`,
|
||||
{ clearChatInput: false }
|
||||
);
|
||||
|
||||
// The result might be an object with various properties
|
||||
// We need to extract the actual image URL if available
|
||||
let imageUrl = null;
|
||||
|
||||
if (result) {
|
||||
// Handle different result formats
|
||||
if (typeof result === 'string') {
|
||||
imageUrl = result;
|
||||
} else if (result.pipe) {
|
||||
imageUrl = result.pipe;
|
||||
} else if (result.output || result.image || result.url) {
|
||||
imageUrl = result.output || result.image || result.url;
|
||||
}
|
||||
|
||||
// Only store if we got a valid string URL
|
||||
if (imageUrl && typeof imageUrl === 'string') {
|
||||
if (!extensionSettings.npcAvatars) {
|
||||
extensionSettings.npcAvatars = {};
|
||||
}
|
||||
extensionSettings.npcAvatars[characterName] = imageUrl;
|
||||
saveSettings();
|
||||
|
||||
console.log(`[RPG Avatar] Generation complete for: ${characterName}`);
|
||||
return imageUrl;
|
||||
} else {
|
||||
console.warn(`[RPG Avatar] Generation result for ${characterName} was not a valid URL:`, result);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error(`[RPG Avatar] Generation failed for ${characterName}:`, error);
|
||||
return null;
|
||||
} finally {
|
||||
pendingGenerations.delete(characterName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a character needs an avatar and triggers generation
|
||||
* @param {string} characterName - Name of the character to check
|
||||
* @param {boolean} hasAvatar - Whether the character already has an avatar
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export function checkAndGenerateAvatar(characterName, hasAvatar) {
|
||||
// Only generate if no avatar exists and feature is enabled
|
||||
if (hasAvatar || !extensionSettings.autoGenerateAvatars) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if we already have a custom NPC avatar
|
||||
if (extensionSettings.npcAvatars && extensionSettings.npcAvatars[characterName]) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Trigger generation (non-blocking)
|
||||
generateAvatar(characterName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an avatar is currently being generated for a character
|
||||
* @param {string} characterName - Name of the character to check
|
||||
* @returns {boolean} True if generation is in progress
|
||||
*/
|
||||
export function isGenerating(characterName) {
|
||||
return pendingGenerations.has(characterName);
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
import { saveChatData } from '../../core/persistence.js';
|
||||
import { getSafeThumbnailUrl } from '../../utils/avatars.js';
|
||||
import { saveSettings } from '../../core/persistence.js';
|
||||
import { checkAndGenerateAvatar, isGenerating } from '../features/avatarGenerator.js';
|
||||
|
||||
/**
|
||||
* Helper to log to both console and debug logs array
|
||||
@@ -110,12 +111,21 @@ function namesMatch(cardName, aiName) {
|
||||
function getCharacterAvatar(characterName) {
|
||||
// First, check if there's a custom NPC avatar
|
||||
if (extensionSettings.npcAvatars && extensionSettings.npcAvatars[characterName]) {
|
||||
debugLog(`[RPG Thoughts] Found custom NPC avatar for: ${characterName}`);
|
||||
return extensionSettings.npcAvatars[characterName];
|
||||
const avatar = extensionSettings.npcAvatars[characterName];
|
||||
// Skip if not a valid string (e.g., if it's an object from a previous bug)
|
||||
if (typeof avatar === 'string' && avatar) {
|
||||
debugLog(`[RPG Thoughts] Found custom NPC avatar for: ${characterName}`);
|
||||
return avatar;
|
||||
} else {
|
||||
// Clear invalid avatar data
|
||||
console.warn(`[RPG Thoughts] Invalid avatar data for ${characterName}, clearing...`);
|
||||
delete extensionSettings.npcAvatars[characterName];
|
||||
}
|
||||
}
|
||||
|
||||
// Use the existing avatar lookup logic
|
||||
let characterPortrait = FALLBACK_AVATAR_DATA_URI;
|
||||
let hasAvatar = false;
|
||||
|
||||
// For group chats, search through group members first
|
||||
if (selected_group) {
|
||||
@@ -129,6 +139,7 @@ function getCharacterAvatar(characterName) {
|
||||
if (matchingMember && matchingMember.avatar && matchingMember.avatar !== 'none') {
|
||||
const thumbnailUrl = getSafeThumbnailUrl('avatar', matchingMember.avatar);
|
||||
if (thumbnailUrl) {
|
||||
hasAvatar = true;
|
||||
return thumbnailUrl;
|
||||
}
|
||||
}
|
||||
@@ -147,6 +158,7 @@ function getCharacterAvatar(characterName) {
|
||||
if (matchingCharacter && matchingCharacter.avatar && matchingCharacter.avatar !== 'none') {
|
||||
const thumbnailUrl = getSafeThumbnailUrl('avatar', matchingCharacter.avatar);
|
||||
if (thumbnailUrl) {
|
||||
hasAvatar = true;
|
||||
return thumbnailUrl;
|
||||
}
|
||||
}
|
||||
@@ -157,10 +169,16 @@ function getCharacterAvatar(characterName) {
|
||||
characters[this_chid].name && namesMatch(characters[this_chid].name, characterName)) {
|
||||
const thumbnailUrl = getSafeThumbnailUrl('avatar', characters[this_chid].avatar);
|
||||
if (thumbnailUrl) {
|
||||
hasAvatar = true;
|
||||
return thumbnailUrl;
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger auto-generation if no avatar was found
|
||||
if (!hasAvatar) {
|
||||
checkAndGenerateAvatar(characterName, false);
|
||||
}
|
||||
|
||||
return characterPortrait;
|
||||
}
|
||||
|
||||
@@ -500,7 +518,7 @@ export function renderThoughts() {
|
||||
// Find character portrait using the new helper function
|
||||
const characterPortrait = getCharacterAvatar(char.name);
|
||||
|
||||
debugLog(`[RPG Thoughts] Final avatar for ${char.name}:`, characterPortrait.substring(0, 50) + '...');
|
||||
debugLog(`[RPG Thoughts] Final avatar for ${char.name}:`, typeof characterPortrait === 'string' ? characterPortrait.substring(0, 50) + '...' : characterPortrait);
|
||||
|
||||
// Get relationship badge - only if relationships are enabled in config
|
||||
let relationshipBadge = '⚖️'; // Default
|
||||
@@ -519,10 +537,14 @@ export function renderThoughts() {
|
||||
// Escape character name for use in HTML attributes
|
||||
const escapedName = escapeHtmlAttr(char.name);
|
||||
|
||||
// Check if avatar is being generated
|
||||
const isCurrentlyGenerating = isGenerating(char.name);
|
||||
|
||||
html += `
|
||||
<div class="rpg-character-card" data-character-name="${escapedName}">
|
||||
<div class="rpg-character-avatar rpg-avatar-upload" data-character="${escapedName}" title="Click to upload custom avatar Right-click to remove custom avatar">
|
||||
<div class="rpg-character-avatar rpg-avatar-upload ${isCurrentlyGenerating ? 'rpg-avatar-generating' : ''}" data-character="${escapedName}" title="Click to upload custom avatar Right-click to remove custom avatar">
|
||||
<img src="${characterPortrait}" alt="${escapedName}" onerror="this.style.opacity='0.5';this.onerror=null;" />
|
||||
${isCurrentlyGenerating ? '<div class="rpg-generating-overlay"><i class="fa-solid fa-spinner fa-spin"></i></div>' : ''}
|
||||
${hasRelationshipEnabled ? `<div class="rpg-relationship-badge rpg-editable" contenteditable="true" data-character="${escapedName}" data-field="${relationshipFieldName}" title="Click to edit (use emoji: ⚔️ ⚖️ ⭐ ❤️)">${relationshipBadge}</div>` : ''}
|
||||
</div>
|
||||
<div class="rpg-character-content">
|
||||
|
||||
Reference in New Issue
Block a user