@agentdao/core
Version:
Core functionality, skills, and ready-made UI components for AgentDAO - Web3 subscriptions, content generation, social media, help support, live chat, RSS fetching, web search, and agent pricing integration
328 lines (327 loc) • 12.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageGenerationSkill = void 0;
class ImageGenerationSkill {
constructor(config) {
this.config = {
...config,
image: {
defaultSize: '1024x1024',
defaultStyle: 'vivid',
maxImages: 4,
quality: 'standard',
...config.image
},
ai: {
maxTokens: 1000,
temperature: 0.7,
...config.ai
}
};
}
/**
* Generate images using AI
*/
async generateImages(request) {
const { prompt, size = this.config.image.defaultSize, style = this.config.image.defaultStyle, quality = this.config.image.quality, numImages = 1, negativePrompt, seed } = request;
try {
// Validate request
if (!prompt || prompt.trim().length === 0) {
throw new Error('Prompt is required for image generation');
}
if (numImages > this.config.image.maxImages) {
throw new Error(`Maximum ${this.config.image.maxImages} images allowed per request`);
}
if (!this.config.ai.apiKey) {
throw new Error('API key is required for image generation');
}
// Generate images based on provider
const images = [];
for (let i = 0; i < numImages; i++) {
const imageId = `img_${Date.now()}_${i}`;
let imageUrl;
let modelUsed;
if (this.config.ai.provider === 'openai') {
// Use OpenAI DALL-E API
const response = await fetch('https://api.openai.com/v1/images/generations', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.config.ai.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: prompt,
n: 1,
size: size,
quality: quality,
response_format: 'url'
})
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(`OpenAI API error: ${errorData.error?.message || response.statusText}`);
}
const data = await response.json();
imageUrl = data.data[0].url;
modelUsed = 'dall-e-3';
}
else {
throw new Error(`Unsupported provider: ${this.config.ai.provider}. Use 'openai' for DALL-E models, 'midjourney' for Midjourney, or 'stable-diffusion' for Stable Diffusion.`);
}
const image = {
id: imageId,
url: imageUrl,
prompt,
size: size,
style: style,
quality: quality,
createdAt: new Date(),
metadata: {
model: modelUsed,
provider: this.config.ai.provider,
seed: seed,
negativePrompt
}
};
images.push(image);
}
// Track analytics if enabled
if (this.config.analytics?.enabled) {
await this.trackGeneration(prompt, images.length);
}
return images;
}
catch (error) {
throw new Error(`Image generation failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Generate variations of an existing image
*/
async generateVariations(imageUrl, options) {
const { size = this.config.image.defaultSize, numVariations = 4 } = options || {};
if (!this.config.ai.apiKey) {
throw new Error('API key is required for image generation');
}
try {
const variations = [];
for (let i = 0; i < numVariations; i++) {
const imageId = `var_${Date.now()}_${i}`;
let generatedImageUrl;
let modelUsed;
if (this.config.ai.provider === 'openai') {
// Use OpenAI DALL-E API for variations
const response = await fetch('https://api.openai.com/v1/images/variations', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.config.ai.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
image: imageUrl,
n: 1,
size: size,
response_format: 'url'
})
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(`OpenAI API error: ${errorData.error?.message || response.statusText}`);
}
const data = await response.json();
generatedImageUrl = data.data[0].url;
modelUsed = 'dall-e-2';
}
else {
throw new Error(`Unsupported provider: ${this.config.ai.provider}. Use 'openai' for DALL-E models, 'midjourney' for Midjourney, or 'stable-diffusion' for Stable Diffusion.`);
}
const variation = {
id: imageId,
url: generatedImageUrl,
prompt: `Variation of: ${imageUrl}`,
size: size,
style: this.config.image.defaultStyle,
quality: this.config.image.quality,
createdAt: new Date(),
metadata: {
model: modelUsed,
provider: this.config.ai.provider,
seed: Math.floor(Math.random() * 1000000)
}
};
variations.push(variation);
}
return variations;
}
catch (error) {
throw new Error(`Image variation generation failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Edit an existing image using AI
*/
async editImage(imageUrl, prompt, options) {
const { size = this.config.image.defaultSize, mask } = options || {};
if (!this.config.ai.apiKey) {
throw new Error('API key is required for image editing');
}
try {
const imageId = `edit_${Date.now()}`;
let generatedImageUrl;
let modelUsed;
if (this.config.ai.provider === 'openai') {
// Use OpenAI DALL-E API for image editing
const body = {
image: imageUrl,
prompt: prompt,
n: 1,
size: size,
response_format: 'url'
};
if (mask) {
body.mask = mask;
}
const response = await fetch('https://api.openai.com/v1/images/edits', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.config.ai.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(`OpenAI API error: ${errorData.error?.message || response.statusText}`);
}
const data = await response.json();
generatedImageUrl = data.data[0].url;
modelUsed = 'dall-e-2';
}
else {
throw new Error(`Unsupported provider: ${this.config.ai.provider}. Use 'openai' for DALL-E models, 'midjourney' for Midjourney, or 'stable-diffusion' for Stable Diffusion.`);
}
const editedImage = {
id: imageId,
url: generatedImageUrl,
prompt: `Edit: ${prompt}`,
size: size,
style: this.config.image.defaultStyle,
quality: this.config.image.quality,
createdAt: new Date(),
metadata: {
model: modelUsed,
provider: this.config.ai.provider,
seed: Math.floor(Math.random() * 1000000)
}
};
return editedImage;
}
catch (error) {
throw new Error(`Image editing failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Generate images in different styles
*/
async generateStyledImages(prompt, styles, options) {
const { size = this.config.image.defaultSize } = options || {};
const images = [];
try {
for (const style of styles) {
const styledPrompt = `${prompt}, ${style} style`;
const image = await this.generateImages({
prompt: styledPrompt,
size,
numImages: 1
});
images.push(...image);
}
return images;
}
catch (error) {
throw new Error(`Styled image generation failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Get available image generation models
*/
async getAvailableModels() {
return [
{
id: 'dall-e-3',
name: 'DALL-E 3',
provider: 'openai',
capabilities: ['text-to-image', 'image-editing'],
maxSize: '1792x1024'
},
{
id: 'dall-e-2',
name: 'DALL-E 2',
provider: 'openai',
capabilities: ['text-to-image', 'image-variations'],
maxSize: '1024x1024'
},
{
id: 'midjourney-v6',
name: 'Midjourney v6',
provider: 'midjourney',
capabilities: ['text-to-image', 'image-variations'],
maxSize: '1792x1024'
},
{
id: 'stable-diffusion-xl',
name: 'Stable Diffusion XL',
provider: 'stability-ai',
capabilities: ['text-to-image', 'image-editing'],
maxSize: '1024x1024'
}
];
}
/**
* Track image generation analytics
*/
async trackGeneration(prompt, imageCount) {
if (!this.config.analytics?.enabled)
return;
try {
// Track generation metrics
const analyticsData = {
agentId: this.config.agentId,
agentName: this.config.agentName,
prompt,
imageCount,
provider: this.config.ai.provider,
model: this.config.ai.model,
timestamp: new Date().toISOString()
};
// Send to analytics endpoint
if (this.config.integration?.webhooks?.enabled) {
await fetch(this.config.integration.webhooks.url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
type: 'image_generation',
data: analyticsData
})
});
}
}
catch (error) {
console.error('Failed to track image generation analytics:', error);
}
}
/**
* Get usage statistics
*/
async getUsageStats() {
// This would typically fetch from a database
return {
totalGenerations: 0,
totalImages: 0,
averageImagesPerGeneration: 0,
mostUsedSize: this.config.image.defaultSize,
mostUsedStyle: this.config.image.defaultStyle
};
}
}
exports.ImageGenerationSkill = ImageGenerationSkill;