@umituz/react-native-ai-generation-content 1.12.2 → 1.12.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -1
- package/src/domains/content-moderation/domain/entities/moderation.types.ts +84 -0
- package/src/domains/content-moderation/domain/interfaces/content-filter.interface.ts +24 -0
- package/src/domains/content-moderation/index.ts +67 -0
- package/src/domains/content-moderation/infrastructure/rules/default-rules.data.ts +144 -0
- package/src/domains/content-moderation/infrastructure/rules/rules-registry.ts +75 -0
- package/src/domains/content-moderation/infrastructure/services/content-moderation.service.ts +150 -0
- package/src/domains/content-moderation/infrastructure/services/index.ts +8 -0
- package/src/domains/content-moderation/infrastructure/services/moderators/base.moderator.ts +62 -0
- package/src/domains/content-moderation/infrastructure/services/moderators/image.moderator.ts +64 -0
- package/src/domains/content-moderation/infrastructure/services/moderators/index.ts +10 -0
- package/src/domains/content-moderation/infrastructure/services/moderators/text.moderator.ts +144 -0
- package/src/domains/content-moderation/infrastructure/services/moderators/video.moderator.ts +64 -0
- package/src/domains/content-moderation/infrastructure/services/moderators/voice.moderator.ts +74 -0
- package/src/domains/content-moderation/infrastructure/services/pattern-matcher.service.ts +51 -0
- package/src/domains/content-moderation/presentation/exceptions/content-policy-violation.exception.ts +48 -0
- package/src/domains/prompts/domain/entities/AIPromptTemplate.ts +48 -0
- package/src/domains/prompts/domain/entities/BackgroundRemovalConfig.ts +86 -0
- package/src/domains/prompts/domain/entities/ColorizationConfig.ts +101 -0
- package/src/domains/prompts/domain/entities/FaceSwapConfig.ts +54 -0
- package/src/domains/prompts/domain/entities/FuturePredictionConfig.ts +93 -0
- package/src/domains/prompts/domain/entities/GeneratedPrompt.ts +32 -0
- package/src/domains/prompts/domain/entities/ImageEnhancementConfig.ts +93 -0
- package/src/domains/prompts/domain/entities/PhotoRestorationConfig.ts +64 -0
- package/src/domains/prompts/domain/entities/StyleTransferConfig.ts +80 -0
- package/src/domains/prompts/domain/entities/TextGenerationConfig.ts +100 -0
- package/src/domains/prompts/domain/entities/types.ts +27 -0
- package/src/domains/prompts/domain/entities/value-objects.ts +33 -0
- package/src/domains/prompts/domain/repositories/IAIPromptServices.ts +106 -0
- package/src/domains/prompts/domain/repositories/IPromptHistoryRepository.ts +10 -0
- package/src/domains/prompts/domain/repositories/ITemplateRepository.ts +11 -0
- package/src/domains/prompts/index.ts +318 -0
- package/src/domains/prompts/infrastructure/repositories/PromptHistoryRepository.ts +85 -0
- package/src/domains/prompts/infrastructure/repositories/TemplateRepository.ts +77 -0
- package/src/domains/prompts/infrastructure/services/BackgroundRemovalService.ts +209 -0
- package/src/domains/prompts/infrastructure/services/ColorizationService.ts +232 -0
- package/src/domains/prompts/infrastructure/services/FaceSwapService.ts +198 -0
- package/src/domains/prompts/infrastructure/services/FuturePredictionService.ts +176 -0
- package/src/domains/prompts/infrastructure/services/ImageEnhancementService.ts +181 -0
- package/src/domains/prompts/infrastructure/services/PhotoRestorationService.ts +160 -0
- package/src/domains/prompts/infrastructure/services/PromptGenerationService.ts +59 -0
- package/src/domains/prompts/infrastructure/services/StyleTransferService.ts +194 -0
- package/src/domains/prompts/infrastructure/services/TextGenerationService.ts +241 -0
- package/src/domains/prompts/presentation/hooks/useAIServices.ts +213 -0
- package/src/domains/prompts/presentation/hooks/useAsyncState.ts +56 -0
- package/src/domains/prompts/presentation/hooks/useFaceSwap.ts +100 -0
- package/src/domains/prompts/presentation/hooks/useImageEnhancement.ts +100 -0
- package/src/domains/prompts/presentation/hooks/usePhotoRestoration.ts +100 -0
- package/src/domains/prompts/presentation/hooks/usePromptGeneration.ts +144 -0
- package/src/domains/prompts/presentation/hooks/useStyleTransfer.ts +125 -0
- package/src/domains/prompts/presentation/hooks/useTemplateRepository.ts +113 -0
- package/src/domains/prompts/presentation/theme/theme.ts +16 -0
- package/src/domains/prompts/presentation/theme/types.ts +82 -0
- package/src/domains/prompts/presentation/theme/utils.ts +24 -0
- package/src/index.ts +12 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import type { ITextGenerationService } from '../../domain/repositories/IAIPromptServices';
|
|
2
|
+
import type {
|
|
3
|
+
TextGenerationConfig,
|
|
4
|
+
} from '../../domain/entities/TextGenerationConfig';
|
|
5
|
+
import type { AIPromptTemplate } from '../../domain/entities/AIPromptTemplate';
|
|
6
|
+
import type { AIPromptResult } from '../../domain/entities/types';
|
|
7
|
+
import { createAIPromptTemplate } from '../../domain/entities/AIPromptTemplate';
|
|
8
|
+
import {
|
|
9
|
+
validateTextGenerationConfig,
|
|
10
|
+
getTokenCount,
|
|
11
|
+
getTemperature,
|
|
12
|
+
getTopP
|
|
13
|
+
} from '../../domain/entities/TextGenerationConfig';
|
|
14
|
+
import { PromptGenerationService } from '../services/PromptGenerationService';
|
|
15
|
+
|
|
16
|
+
const createTextGenerationBaseTemplate = (config?: {
|
|
17
|
+
promptType?: string;
|
|
18
|
+
tone?: string;
|
|
19
|
+
language?: string;
|
|
20
|
+
}): string => {
|
|
21
|
+
const {
|
|
22
|
+
promptType = 'general',
|
|
23
|
+
tone = 'professional',
|
|
24
|
+
language = 'English'
|
|
25
|
+
} = config || {};
|
|
26
|
+
|
|
27
|
+
return `
|
|
28
|
+
You are an expert text generation specialist.
|
|
29
|
+
This is a TEXT GENERATION task with specific requirements.
|
|
30
|
+
|
|
31
|
+
GENERATION OBJECTIVES:
|
|
32
|
+
- Generate ${promptType} content with ${tone} tone
|
|
33
|
+
- Write in ${language} language
|
|
34
|
+
- Follow provided context and constraints
|
|
35
|
+
- Deliver high-quality, appropriate content
|
|
36
|
+
|
|
37
|
+
CONTENT STANDARDS:
|
|
38
|
+
- Clear, coherent, and well-structured text
|
|
39
|
+
- Appropriate for the specified tone and purpose
|
|
40
|
+
- Natural language flow and readability
|
|
41
|
+
- Relevant and informative content
|
|
42
|
+
|
|
43
|
+
STYLE REQUIREMENTS:
|
|
44
|
+
- Maintain ${tone} tone throughout
|
|
45
|
+
- Use appropriate vocabulary and expressions
|
|
46
|
+
- Follow conventions for ${promptType} content
|
|
47
|
+
- Adapt complexity to intended audience
|
|
48
|
+
|
|
49
|
+
LANGUAGE REQUIREMENTS:
|
|
50
|
+
- Write fluently in ${language}
|
|
51
|
+
- Use proper grammar and syntax
|
|
52
|
+
- Consider cultural and regional variations
|
|
53
|
+
- Maintain consistency in language usage
|
|
54
|
+
|
|
55
|
+
SAFETY AND QUALITY:
|
|
56
|
+
- Generate appropriate, non-offensive content
|
|
57
|
+
- Avoid harmful or misleading information
|
|
58
|
+
- Ensure factual accuracy when applicable
|
|
59
|
+
- Respect copyright and intellectual property
|
|
60
|
+
|
|
61
|
+
OUTPUT:
|
|
62
|
+
Professional ${promptType} content in ${language},
|
|
63
|
+
written with ${tone} tone,
|
|
64
|
+
meeting all specified requirements.
|
|
65
|
+
`.trim();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export class TextGenerationService implements ITextGenerationService {
|
|
69
|
+
private promptService: PromptGenerationService;
|
|
70
|
+
|
|
71
|
+
constructor() {
|
|
72
|
+
this.promptService = new PromptGenerationService();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async generateTemplate(config: TextGenerationConfig): Promise<AIPromptResult<AIPromptTemplate>> {
|
|
76
|
+
try {
|
|
77
|
+
if (!this.validateConfig(config)) {
|
|
78
|
+
return {
|
|
79
|
+
success: false,
|
|
80
|
+
error: 'VALIDATION_ERROR',
|
|
81
|
+
message: 'Invalid text generation configuration'
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const template = this.createTextGenerationTemplate(config);
|
|
86
|
+
return { success: true, data: template };
|
|
87
|
+
} catch (error) {
|
|
88
|
+
return {
|
|
89
|
+
success: false,
|
|
90
|
+
error: 'GENERATION_FAILED',
|
|
91
|
+
message: 'Failed to generate text template'
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async generatePrompt(
|
|
97
|
+
template: AIPromptTemplate,
|
|
98
|
+
config: TextGenerationConfig
|
|
99
|
+
): Promise<AIPromptResult<string>> {
|
|
100
|
+
const parameters = this.getGenerationParameters(config);
|
|
101
|
+
const variables = {
|
|
102
|
+
promptType: config.promptType,
|
|
103
|
+
tone: config.tone,
|
|
104
|
+
length: config.length,
|
|
105
|
+
language: config.language,
|
|
106
|
+
context: config.context || '',
|
|
107
|
+
keywords: config.keywords?.join(', ') || '',
|
|
108
|
+
...parameters,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return this.promptService.generateFromTemplate(template, variables);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
validateConfig(config: TextGenerationConfig): boolean {
|
|
115
|
+
return validateTextGenerationConfig(config);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
estimateTokens(config: TextGenerationConfig): number {
|
|
119
|
+
return getTokenCount(config.length);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
getGenerationParameters(config: TextGenerationConfig): Record<string, number> {
|
|
123
|
+
return {
|
|
124
|
+
temperature: getTemperature(config.promptType, config.tone),
|
|
125
|
+
maxTokens: this.estimateTokens(config),
|
|
126
|
+
topP: getTopP(config.promptType),
|
|
127
|
+
frequencyPenalty: config.tone === 'formal' ? 0.3 : 0.1,
|
|
128
|
+
presencePenalty: config.promptType === 'creative' ? 0.2 : 0.1,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private createTextGenerationTemplate(config: TextGenerationConfig): AIPromptTemplate {
|
|
133
|
+
const templateId = `text-generation-${config.promptType}`;
|
|
134
|
+
|
|
135
|
+
const baseTemplate = createTextGenerationBaseTemplate({
|
|
136
|
+
promptType: config.promptType,
|
|
137
|
+
tone: config.tone,
|
|
138
|
+
language: config.language,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return createAIPromptTemplate({
|
|
142
|
+
id: templateId,
|
|
143
|
+
name: `Text Generation: ${config.promptType}`,
|
|
144
|
+
description: `Generate ${config.promptType} content in ${config.language}`,
|
|
145
|
+
category: 'text-generation',
|
|
146
|
+
template: `${baseTemplate}
|
|
147
|
+
|
|
148
|
+
GENERATION PARAMETERS:
|
|
149
|
+
- Prompt Type: ${config.promptType}
|
|
150
|
+
- Tone: ${config.tone}
|
|
151
|
+
- Length: ${config.length}
|
|
152
|
+
- Language: ${config.language}
|
|
153
|
+
- Estimated Tokens: ${this.estimateTokens(config)}
|
|
154
|
+
|
|
155
|
+
${config.context ? `CONTEXT:
|
|
156
|
+
${config.context}
|
|
157
|
+
|
|
158
|
+
` : ''}${config.keywords && config.keywords.length > 0 ? `KEYWORDS:
|
|
159
|
+
${config.keywords.join(', ')}
|
|
160
|
+
|
|
161
|
+
` : ''}CONTENT REQUIREMENTS:
|
|
162
|
+
- Generate ${config.length} content appropriate for ${config.promptType}
|
|
163
|
+
- Use ${config.tone} tone throughout the text
|
|
164
|
+
- Follow conventions for ${config.promptType} writing
|
|
165
|
+
- Write fluently in ${config.language}
|
|
166
|
+
|
|
167
|
+
STYLE GUIDELINES:
|
|
168
|
+
${this.getStyleGuidelines(config)}
|
|
169
|
+
|
|
170
|
+
GENERATION PARAMETERS:
|
|
171
|
+
- Temperature: ${getTemperature(config.promptType, config.tone)} (creativity level)
|
|
172
|
+
- Max Tokens: ${this.estimateTokens(config)} (content length)
|
|
173
|
+
- Top-P: ${getTopP(config.promptType)} (nucleus sampling)
|
|
174
|
+
- Frequency Penalty: ${config.tone === 'formal' ? 0.3 : 0.1} (repetition reduction)
|
|
175
|
+
- Presence Penalty: ${config.promptType === 'creative' ? 0.2 : 0.1} (new topic encouragement)
|
|
176
|
+
|
|
177
|
+
EXPECTED OUTPUT:
|
|
178
|
+
Professional ${config.promptType} content in ${config.language},
|
|
179
|
+
approximately ${this.estimateTokens(config)} tokens long,
|
|
180
|
+
with ${config.tone} tone and appropriate style.
|
|
181
|
+
`.trim(),
|
|
182
|
+
variables: [],
|
|
183
|
+
safety: {
|
|
184
|
+
contentFilter: true,
|
|
185
|
+
adultContentFilter: true,
|
|
186
|
+
violenceFilter: true,
|
|
187
|
+
hateSpeechFilter: true,
|
|
188
|
+
copyrightFilter: true,
|
|
189
|
+
},
|
|
190
|
+
version: '1.0.0',
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private getStyleGuidelines(config: TextGenerationConfig): string {
|
|
195
|
+
const guidelines: string[] = [];
|
|
196
|
+
|
|
197
|
+
switch (config.promptType) {
|
|
198
|
+
case 'creative':
|
|
199
|
+
guidelines.push('- Use imaginative and engaging language');
|
|
200
|
+
guidelines.push('- Incorporate vivid descriptions and storytelling');
|
|
201
|
+
break;
|
|
202
|
+
case 'technical':
|
|
203
|
+
guidelines.push('- Use precise terminology and clear explanations');
|
|
204
|
+
guidelines.push('- Maintain logical structure and accuracy');
|
|
205
|
+
break;
|
|
206
|
+
case 'marketing':
|
|
207
|
+
guidelines.push('- Use persuasive and compelling language');
|
|
208
|
+
guidelines.push('- Include clear calls to action');
|
|
209
|
+
break;
|
|
210
|
+
case 'educational':
|
|
211
|
+
guidelines.push('- Present information clearly and progressively');
|
|
212
|
+
guidelines.push('- Include examples and explanations');
|
|
213
|
+
break;
|
|
214
|
+
case 'conversational':
|
|
215
|
+
guidelines.push('- Use natural, approachable language');
|
|
216
|
+
guidelines.push('- Include questions and engagement elements');
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
switch (config.tone) {
|
|
221
|
+
case 'formal':
|
|
222
|
+
guidelines.push('- Maintain professional and respectful language');
|
|
223
|
+
guidelines.push('- Use proper grammar and complete sentences');
|
|
224
|
+
break;
|
|
225
|
+
case 'casual':
|
|
226
|
+
guidelines.push('- Use relaxed, natural language');
|
|
227
|
+
guidelines.push('- Include some conversational elements');
|
|
228
|
+
break;
|
|
229
|
+
case 'friendly':
|
|
230
|
+
guidelines.push('- Use warm and approachable tone');
|
|
231
|
+
guidelines.push('- Include encouraging and positive elements');
|
|
232
|
+
break;
|
|
233
|
+
case 'humorous':
|
|
234
|
+
guidelines.push('- Incorporate appropriate humor and wit');
|
|
235
|
+
guidelines.push('- Maintain lighthearted and engaging style');
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return guidelines.join('\n') || '- Follow standard writing conventions';
|
|
240
|
+
}
|
|
241
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { useState, useCallback } from 'react';
|
|
2
|
+
import type {
|
|
3
|
+
FaceSwapConfig
|
|
4
|
+
} from '../../domain/entities/FaceSwapConfig';
|
|
5
|
+
import type {
|
|
6
|
+
PhotoRestorationConfig
|
|
7
|
+
} from '../../domain/entities/PhotoRestorationConfig';
|
|
8
|
+
import type {
|
|
9
|
+
ImageEnhancementConfig
|
|
10
|
+
} from '../../domain/entities/ImageEnhancementConfig';
|
|
11
|
+
import type {
|
|
12
|
+
StyleTransferConfig
|
|
13
|
+
} from '../../domain/entities/StyleTransferConfig';
|
|
14
|
+
import type {
|
|
15
|
+
BackgroundRemovalConfig
|
|
16
|
+
} from '../../domain/entities/BackgroundRemovalConfig';
|
|
17
|
+
import type {
|
|
18
|
+
TextGenerationConfig
|
|
19
|
+
} from '../../domain/entities/TextGenerationConfig';
|
|
20
|
+
import type {
|
|
21
|
+
ColorizationConfig
|
|
22
|
+
} from '../../domain/entities/ColorizationConfig';
|
|
23
|
+
import type {
|
|
24
|
+
IFaceSwapService,
|
|
25
|
+
IPhotoRestorationService,
|
|
26
|
+
IImageEnhancementService,
|
|
27
|
+
IStyleTransferService,
|
|
28
|
+
IBackgroundRemovalService,
|
|
29
|
+
ITextGenerationService,
|
|
30
|
+
IColorizationService
|
|
31
|
+
} from '../../domain/repositories/IAIPromptServices';
|
|
32
|
+
import type { ITemplateRepository } from '../../domain/repositories/ITemplateRepository';
|
|
33
|
+
import type { IPromptHistoryRepository } from '../../domain/repositories/IPromptHistoryRepository';
|
|
34
|
+
import type { GeneratedPrompt } from '../../domain/entities/GeneratedPrompt';
|
|
35
|
+
import { createGeneratedPrompt } from '../../domain/entities/GeneratedPrompt';
|
|
36
|
+
import { useAsyncState } from './useAsyncState';
|
|
37
|
+
|
|
38
|
+
export type AIConfig =
|
|
39
|
+
| { type: 'face-swap'; config: FaceSwapConfig }
|
|
40
|
+
| { type: 'photo-restoration'; config: PhotoRestorationConfig }
|
|
41
|
+
| { type: 'image-enhancement'; config: ImageEnhancementConfig }
|
|
42
|
+
| { type: 'style-transfer'; config: StyleTransferConfig }
|
|
43
|
+
| { type: 'background-removal'; config: BackgroundRemovalConfig }
|
|
44
|
+
| { type: 'text-generation'; config: TextGenerationConfig }
|
|
45
|
+
| { type: 'colorization'; config: ColorizationConfig };
|
|
46
|
+
|
|
47
|
+
export interface UseAIServicesState {
|
|
48
|
+
generatedPrompt: GeneratedPrompt | null;
|
|
49
|
+
processing: boolean;
|
|
50
|
+
currentService: string | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface UseAIServicesActions {
|
|
54
|
+
processRequest: (aiConfig: AIConfig) => Promise<void>;
|
|
55
|
+
getAvailableStyles: (serviceType: string) => Promise<string[]>;
|
|
56
|
+
clearPrompt: () => void;
|
|
57
|
+
reset: () => void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const useAIServices = (
|
|
61
|
+
services: {
|
|
62
|
+
faceSwap: IFaceSwapService;
|
|
63
|
+
photoRestoration: IPhotoRestorationService;
|
|
64
|
+
imageEnhancement: IImageEnhancementService;
|
|
65
|
+
styleTransfer: IStyleTransferService;
|
|
66
|
+
backgroundRemoval: IBackgroundRemovalService;
|
|
67
|
+
textGeneration: ITextGenerationService;
|
|
68
|
+
colorization: IColorizationService;
|
|
69
|
+
},
|
|
70
|
+
repositories: {
|
|
71
|
+
template: ITemplateRepository;
|
|
72
|
+
history: IPromptHistoryRepository;
|
|
73
|
+
}
|
|
74
|
+
): UseAIServicesState & UseAIServicesActions => {
|
|
75
|
+
const {
|
|
76
|
+
data: generatedPrompt,
|
|
77
|
+
loading: processing,
|
|
78
|
+
error,
|
|
79
|
+
setError,
|
|
80
|
+
setData: setGeneratedPrompt,
|
|
81
|
+
clearError,
|
|
82
|
+
} = useAsyncState<GeneratedPrompt>(null);
|
|
83
|
+
|
|
84
|
+
const [currentService, setCurrentService] = useState<string | null>(null);
|
|
85
|
+
|
|
86
|
+
const processRequest = useCallback(async (aiConfig: AIConfig): Promise<void> => {
|
|
87
|
+
clearError();
|
|
88
|
+
setCurrentService(aiConfig.type);
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
let templateResult;
|
|
92
|
+
let promptResult;
|
|
93
|
+
|
|
94
|
+
switch (aiConfig.type) {
|
|
95
|
+
case 'face-swap':
|
|
96
|
+
templateResult = await services.faceSwap.generateTemplate(aiConfig.config);
|
|
97
|
+
if (templateResult.success && templateResult.data) {
|
|
98
|
+
promptResult = await services.faceSwap.generatePrompt(templateResult.data, aiConfig.config);
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
|
|
102
|
+
case 'photo-restoration':
|
|
103
|
+
templateResult = await services.photoRestoration.generateTemplate(aiConfig.config);
|
|
104
|
+
if (templateResult.success && templateResult.data) {
|
|
105
|
+
promptResult = await services.photoRestoration.generatePrompt(templateResult.data, aiConfig.config);
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
|
|
109
|
+
case 'image-enhancement':
|
|
110
|
+
templateResult = await services.imageEnhancement.generateTemplate(aiConfig.config);
|
|
111
|
+
if (templateResult.success && templateResult.data) {
|
|
112
|
+
promptResult = await services.imageEnhancement.generatePrompt(templateResult.data, aiConfig.config);
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
|
|
116
|
+
case 'style-transfer':
|
|
117
|
+
templateResult = await services.styleTransfer.generateTemplate(aiConfig.config);
|
|
118
|
+
if (templateResult.success && templateResult.data) {
|
|
119
|
+
promptResult = await services.styleTransfer.generatePrompt(templateResult.data, aiConfig.config);
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
|
|
123
|
+
case 'background-removal':
|
|
124
|
+
templateResult = await services.backgroundRemoval.generateTemplate(aiConfig.config);
|
|
125
|
+
if (templateResult.success && templateResult.data) {
|
|
126
|
+
promptResult = await services.backgroundRemoval.generatePrompt(templateResult.data, aiConfig.config);
|
|
127
|
+
}
|
|
128
|
+
break;
|
|
129
|
+
|
|
130
|
+
case 'text-generation':
|
|
131
|
+
templateResult = await services.textGeneration.generateTemplate(aiConfig.config);
|
|
132
|
+
if (templateResult.success && templateResult.data) {
|
|
133
|
+
promptResult = await services.textGeneration.generatePrompt(templateResult.data, aiConfig.config);
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
|
|
137
|
+
case 'colorization':
|
|
138
|
+
templateResult = await services.colorization.generateTemplate(aiConfig.config);
|
|
139
|
+
if (templateResult.success && templateResult.data) {
|
|
140
|
+
promptResult = await services.colorization.generatePrompt(templateResult.data, aiConfig.config);
|
|
141
|
+
}
|
|
142
|
+
break;
|
|
143
|
+
|
|
144
|
+
default:
|
|
145
|
+
setError('Unknown AI service type');
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!templateResult?.success || !templateResult.data) {
|
|
150
|
+
setError('Failed to generate template');
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!promptResult?.success) {
|
|
155
|
+
setError('Failed to generate prompt');
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const newPrompt = createGeneratedPrompt({
|
|
160
|
+
templateId: templateResult.data.id,
|
|
161
|
+
generatedText: promptResult.data,
|
|
162
|
+
variables: aiConfig.config as unknown as Record<string, unknown>,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
await repositories.history.save(newPrompt);
|
|
166
|
+
setGeneratedPrompt(newPrompt);
|
|
167
|
+
|
|
168
|
+
} catch (error) {
|
|
169
|
+
setError('An unexpected error occurred');
|
|
170
|
+
} finally {
|
|
171
|
+
setCurrentService(null);
|
|
172
|
+
}
|
|
173
|
+
}, [services, repositories, setError, setGeneratedPrompt, clearError]);
|
|
174
|
+
|
|
175
|
+
const getAvailableStyles = useCallback(async (serviceType: string): Promise<string[]> => {
|
|
176
|
+
try {
|
|
177
|
+
switch (serviceType) {
|
|
178
|
+
case 'face-swap':
|
|
179
|
+
return await services.faceSwap.getAvailableStyles();
|
|
180
|
+
case 'style-transfer':
|
|
181
|
+
return await services.styleTransfer.getAvailableStyles();
|
|
182
|
+
default:
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
186
|
+
setError('Failed to load available styles');
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
}, [services, setError]);
|
|
190
|
+
|
|
191
|
+
const clearPrompt = useCallback(() => {
|
|
192
|
+
setGeneratedPrompt(null);
|
|
193
|
+
setCurrentService(null);
|
|
194
|
+
clearError();
|
|
195
|
+
}, [setGeneratedPrompt, clearError]);
|
|
196
|
+
|
|
197
|
+
const reset = useCallback(() => {
|
|
198
|
+
setGeneratedPrompt(null);
|
|
199
|
+
setCurrentService(null);
|
|
200
|
+
clearError();
|
|
201
|
+
}, [setGeneratedPrompt, clearError]);
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
generatedPrompt,
|
|
205
|
+
processing,
|
|
206
|
+
currentService,
|
|
207
|
+
error,
|
|
208
|
+
processRequest,
|
|
209
|
+
getAvailableStyles,
|
|
210
|
+
clearPrompt,
|
|
211
|
+
reset,
|
|
212
|
+
} as UseAIServicesState & UseAIServicesActions;
|
|
213
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useState, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
export interface AsyncState<T> {
|
|
4
|
+
data: T | null;
|
|
5
|
+
loading: boolean;
|
|
6
|
+
error: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface AsyncActions<T> {
|
|
10
|
+
setLoading: (loading: boolean) => void;
|
|
11
|
+
setError: (error: string | null) => void;
|
|
12
|
+
setData: (data: T | null) => void;
|
|
13
|
+
clearError: () => void;
|
|
14
|
+
reset: () => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const useAsyncState = <T>(initialData: T | null = null): AsyncState<T> & AsyncActions<T> => {
|
|
18
|
+
const [state, setState] = useState<AsyncState<T>>({
|
|
19
|
+
data: initialData,
|
|
20
|
+
loading: false,
|
|
21
|
+
error: null,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const setLoading = useCallback((loading: boolean) => {
|
|
25
|
+
setState(prev => ({ ...prev, loading }));
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
const setError = useCallback((error: string | null) => {
|
|
29
|
+
setState(prev => ({ ...prev, error, loading: false }));
|
|
30
|
+
}, []);
|
|
31
|
+
|
|
32
|
+
const setData = useCallback((data: T | null) => {
|
|
33
|
+
setState(prev => ({ ...prev, data, loading: false, error: null }));
|
|
34
|
+
}, []);
|
|
35
|
+
|
|
36
|
+
const clearError = useCallback(() => {
|
|
37
|
+
setState(prev => ({ ...prev, error: null }));
|
|
38
|
+
}, []);
|
|
39
|
+
|
|
40
|
+
const reset = useCallback(() => {
|
|
41
|
+
setState({
|
|
42
|
+
data: initialData,
|
|
43
|
+
loading: false,
|
|
44
|
+
error: null,
|
|
45
|
+
});
|
|
46
|
+
}, [initialData]);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
...state,
|
|
50
|
+
setLoading,
|
|
51
|
+
setError,
|
|
52
|
+
setData,
|
|
53
|
+
clearError,
|
|
54
|
+
reset,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import type { FaceSwapConfig, FaceSwapGenerationResult } from '../../domain/entities/FaceSwapConfig';
|
|
3
|
+
import type { IFaceSwapService } from '../../domain/repositories/IAIPromptServices';
|
|
4
|
+
import type { ITemplateRepository } from '../../domain/repositories/ITemplateRepository';
|
|
5
|
+
import type { IPromptHistoryRepository } from '../../domain/repositories/IPromptHistoryRepository';
|
|
6
|
+
import type { GeneratedPrompt } from '../../domain/entities/GeneratedPrompt';
|
|
7
|
+
import { createGeneratedPrompt } from '../../domain/entities/GeneratedPrompt';
|
|
8
|
+
import { useAsyncState } from './useAsyncState';
|
|
9
|
+
|
|
10
|
+
export interface UseFaceSwapState {
|
|
11
|
+
generatedPrompt: string | null;
|
|
12
|
+
lastResult: FaceSwapGenerationResult | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface UseFaceSwapActions {
|
|
16
|
+
generateFaceSwapPrompt: (config: FaceSwapConfig) => Promise<void>;
|
|
17
|
+
getAvailableStyles: () => Promise<string[]>;
|
|
18
|
+
clearPrompt: () => void;
|
|
19
|
+
reset: () => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const useFaceSwap = (
|
|
23
|
+
faceSwapService: IFaceSwapService,
|
|
24
|
+
templateRepository: ITemplateRepository,
|
|
25
|
+
historyRepository: IPromptHistoryRepository
|
|
26
|
+
): UseFaceSwapState & UseFaceSwapActions => {
|
|
27
|
+
const {
|
|
28
|
+
data: lastResult,
|
|
29
|
+
loading,
|
|
30
|
+
error,
|
|
31
|
+
setError,
|
|
32
|
+
setData: setResult,
|
|
33
|
+
clearError,
|
|
34
|
+
} = useAsyncState<FaceSwapGenerationResult>(null);
|
|
35
|
+
|
|
36
|
+
const generateFaceSwapPrompt = useCallback(async (config: FaceSwapConfig): Promise<void> => {
|
|
37
|
+
clearError();
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const templateResult = await faceSwapService.generateTemplate(config);
|
|
41
|
+
if (!templateResult.success || !templateResult.data) {
|
|
42
|
+
setError(templateResult.success === false && templateResult.error === 'TEMPLATE_NOT_FOUND' ? 'Template not found' : 'Failed to generate template');
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const promptResult = await faceSwapService.generatePrompt(templateResult.data, config);
|
|
47
|
+
if (!promptResult.success) {
|
|
48
|
+
setError(promptResult.success === false && promptResult.error === 'GENERATION_FAILED' ? 'Failed to generate prompt' : 'Generation failed');
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const generatedPrompt = createGeneratedPrompt({
|
|
53
|
+
templateId: templateResult.data.id,
|
|
54
|
+
generatedText: promptResult.data,
|
|
55
|
+
variables: config as unknown as Record<string, unknown>,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
await historyRepository.save(generatedPrompt);
|
|
59
|
+
|
|
60
|
+
const generationResult: FaceSwapGenerationResult = {
|
|
61
|
+
template: templateResult.data,
|
|
62
|
+
prompt: generatedPrompt,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
setResult(generationResult);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
setError('An unexpected error occurred');
|
|
68
|
+
}
|
|
69
|
+
}, [faceSwapService, historyRepository, setError, setResult, clearError]);
|
|
70
|
+
|
|
71
|
+
const getAvailableStyles = useCallback(async (): Promise<string[]> => {
|
|
72
|
+
try {
|
|
73
|
+
return await faceSwapService.getAvailableStyles();
|
|
74
|
+
} catch (error) {
|
|
75
|
+
setError('Failed to load available styles');
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
}, [faceSwapService, setError]);
|
|
79
|
+
|
|
80
|
+
const clearPrompt = useCallback(() => {
|
|
81
|
+
setResult(null);
|
|
82
|
+
clearError();
|
|
83
|
+
}, [setResult, clearError]);
|
|
84
|
+
|
|
85
|
+
const reset = useCallback(() => {
|
|
86
|
+
setResult(null);
|
|
87
|
+
clearError();
|
|
88
|
+
}, [setResult, clearError]);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
generatedPrompt: lastResult?.prompt.generatedText || null,
|
|
92
|
+
lastResult,
|
|
93
|
+
loading,
|
|
94
|
+
error,
|
|
95
|
+
generateFaceSwapPrompt,
|
|
96
|
+
getAvailableStyles,
|
|
97
|
+
clearPrompt,
|
|
98
|
+
reset,
|
|
99
|
+
} as UseFaceSwapState & UseFaceSwapActions;
|
|
100
|
+
};
|