@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.
Files changed (55) hide show
  1. package/package.json +6 -1
  2. package/src/domains/content-moderation/domain/entities/moderation.types.ts +84 -0
  3. package/src/domains/content-moderation/domain/interfaces/content-filter.interface.ts +24 -0
  4. package/src/domains/content-moderation/index.ts +67 -0
  5. package/src/domains/content-moderation/infrastructure/rules/default-rules.data.ts +144 -0
  6. package/src/domains/content-moderation/infrastructure/rules/rules-registry.ts +75 -0
  7. package/src/domains/content-moderation/infrastructure/services/content-moderation.service.ts +150 -0
  8. package/src/domains/content-moderation/infrastructure/services/index.ts +8 -0
  9. package/src/domains/content-moderation/infrastructure/services/moderators/base.moderator.ts +62 -0
  10. package/src/domains/content-moderation/infrastructure/services/moderators/image.moderator.ts +64 -0
  11. package/src/domains/content-moderation/infrastructure/services/moderators/index.ts +10 -0
  12. package/src/domains/content-moderation/infrastructure/services/moderators/text.moderator.ts +144 -0
  13. package/src/domains/content-moderation/infrastructure/services/moderators/video.moderator.ts +64 -0
  14. package/src/domains/content-moderation/infrastructure/services/moderators/voice.moderator.ts +74 -0
  15. package/src/domains/content-moderation/infrastructure/services/pattern-matcher.service.ts +51 -0
  16. package/src/domains/content-moderation/presentation/exceptions/content-policy-violation.exception.ts +48 -0
  17. package/src/domains/prompts/domain/entities/AIPromptTemplate.ts +48 -0
  18. package/src/domains/prompts/domain/entities/BackgroundRemovalConfig.ts +86 -0
  19. package/src/domains/prompts/domain/entities/ColorizationConfig.ts +101 -0
  20. package/src/domains/prompts/domain/entities/FaceSwapConfig.ts +54 -0
  21. package/src/domains/prompts/domain/entities/FuturePredictionConfig.ts +93 -0
  22. package/src/domains/prompts/domain/entities/GeneratedPrompt.ts +32 -0
  23. package/src/domains/prompts/domain/entities/ImageEnhancementConfig.ts +93 -0
  24. package/src/domains/prompts/domain/entities/PhotoRestorationConfig.ts +64 -0
  25. package/src/domains/prompts/domain/entities/StyleTransferConfig.ts +80 -0
  26. package/src/domains/prompts/domain/entities/TextGenerationConfig.ts +100 -0
  27. package/src/domains/prompts/domain/entities/types.ts +27 -0
  28. package/src/domains/prompts/domain/entities/value-objects.ts +33 -0
  29. package/src/domains/prompts/domain/repositories/IAIPromptServices.ts +106 -0
  30. package/src/domains/prompts/domain/repositories/IPromptHistoryRepository.ts +10 -0
  31. package/src/domains/prompts/domain/repositories/ITemplateRepository.ts +11 -0
  32. package/src/domains/prompts/index.ts +318 -0
  33. package/src/domains/prompts/infrastructure/repositories/PromptHistoryRepository.ts +85 -0
  34. package/src/domains/prompts/infrastructure/repositories/TemplateRepository.ts +77 -0
  35. package/src/domains/prompts/infrastructure/services/BackgroundRemovalService.ts +209 -0
  36. package/src/domains/prompts/infrastructure/services/ColorizationService.ts +232 -0
  37. package/src/domains/prompts/infrastructure/services/FaceSwapService.ts +198 -0
  38. package/src/domains/prompts/infrastructure/services/FuturePredictionService.ts +176 -0
  39. package/src/domains/prompts/infrastructure/services/ImageEnhancementService.ts +181 -0
  40. package/src/domains/prompts/infrastructure/services/PhotoRestorationService.ts +160 -0
  41. package/src/domains/prompts/infrastructure/services/PromptGenerationService.ts +59 -0
  42. package/src/domains/prompts/infrastructure/services/StyleTransferService.ts +194 -0
  43. package/src/domains/prompts/infrastructure/services/TextGenerationService.ts +241 -0
  44. package/src/domains/prompts/presentation/hooks/useAIServices.ts +213 -0
  45. package/src/domains/prompts/presentation/hooks/useAsyncState.ts +56 -0
  46. package/src/domains/prompts/presentation/hooks/useFaceSwap.ts +100 -0
  47. package/src/domains/prompts/presentation/hooks/useImageEnhancement.ts +100 -0
  48. package/src/domains/prompts/presentation/hooks/usePhotoRestoration.ts +100 -0
  49. package/src/domains/prompts/presentation/hooks/usePromptGeneration.ts +144 -0
  50. package/src/domains/prompts/presentation/hooks/useStyleTransfer.ts +125 -0
  51. package/src/domains/prompts/presentation/hooks/useTemplateRepository.ts +113 -0
  52. package/src/domains/prompts/presentation/theme/theme.ts +16 -0
  53. package/src/domains/prompts/presentation/theme/types.ts +82 -0
  54. package/src/domains/prompts/presentation/theme/utils.ts +24 -0
  55. package/src/index.ts +12 -0
@@ -0,0 +1,318 @@
1
+ /**
2
+ * @umituz/react-native-ai-prompts - Public API
3
+ *
4
+ * AI prompt templates and utilities for React Native applications
5
+ * Following SOLID, DRY, KISS principles with maximum maintainability
6
+ *
7
+ * Usage:
8
+ * import {
9
+ * useFaceSwap,
10
+ * useTemplateRepository,
11
+ * FaceSwapService,
12
+ * TemplateRepository
13
+ * } from '@umituz/react-native-ai-prompts';
14
+ */
15
+
16
+ // =============================================================================
17
+ // DOMAIN LAYER - Types and Value Objects
18
+ // =============================================================================
19
+
20
+ export type {
21
+ AIPromptCategory,
22
+ AIPromptVariableType,
23
+ AIPromptError,
24
+ AIPromptResult,
25
+ } from './domain/entities/types';
26
+
27
+ export type {
28
+ AIPromptVariable,
29
+ AIPromptSafety,
30
+ AIPromptVersion,
31
+ } from './domain/entities/value-objects';
32
+
33
+ export {
34
+ createPromptVersion,
35
+ formatVersion,
36
+ } from './domain/entities/value-objects';
37
+
38
+ // =============================================================================
39
+ // DOMAIN LAYER - Entities
40
+ // =============================================================================
41
+
42
+ export type {
43
+ AIPromptTemplate,
44
+ CreateAIPromptTemplateParams,
45
+ } from './domain/entities/AIPromptTemplate';
46
+
47
+ export {
48
+ createAIPromptTemplate,
49
+ updateTemplateVersion,
50
+ getTemplateString,
51
+ } from './domain/entities/AIPromptTemplate';
52
+
53
+ export type {
54
+ GeneratedPrompt,
55
+ CreateGeneratedPromptParams,
56
+ } from './domain/entities/GeneratedPrompt';
57
+
58
+ export {
59
+ createGeneratedPrompt,
60
+ isPromptRecent,
61
+ } from './domain/entities/GeneratedPrompt';
62
+
63
+ export type {
64
+ FaceSwapConfig,
65
+ FaceSwapTemplate,
66
+ FaceSwapTemplateVariable,
67
+ FaceSwapSafety,
68
+ FaceSwapGenerationResult,
69
+ } from './domain/entities/FaceSwapConfig';
70
+
71
+ export {
72
+ validateFaceSwapConfig,
73
+ createFaceSwapVariable,
74
+ } from './domain/entities/FaceSwapConfig';
75
+
76
+ export type {
77
+ PhotoRestorationConfig,
78
+ PhotoRestorationTemplate,
79
+ PhotoRestorationVariable,
80
+ PhotoRestorationQuality,
81
+ PhotoRestorationResult,
82
+ } from './domain/entities/PhotoRestorationConfig';
83
+
84
+ export {
85
+ validatePhotoRestorationConfig,
86
+ createPhotoRestorationVariable,
87
+ getQualityLevel,
88
+ } from './domain/entities/PhotoRestorationConfig';
89
+
90
+ export type {
91
+ ImageEnhancementConfig,
92
+ ImageEnhancementTemplate,
93
+ ImageEnhancementVariable,
94
+ EnhancementSettings,
95
+ ImageEnhancementResult,
96
+ EnhancementAdjustments,
97
+ } from './domain/entities/ImageEnhancementConfig';
98
+
99
+ export {
100
+ validateImageEnhancementConfig,
101
+ createImageEnhancementVariable,
102
+ calculateAdjustments,
103
+ } from './domain/entities/ImageEnhancementConfig';
104
+
105
+ export type {
106
+ StyleTransferConfig,
107
+ StyleTransferTemplate,
108
+ StyleTransferVariable,
109
+ StyleTransferSettings,
110
+ StyleTransferResult,
111
+ } from './domain/entities/StyleTransferConfig';
112
+
113
+ export {
114
+ validateStyleTransferConfig,
115
+ createStyleTransferVariable,
116
+ getStyleStrengthValue,
117
+ getArtisticModeDescription,
118
+ } from './domain/entities/StyleTransferConfig';
119
+
120
+ export type {
121
+ BackgroundRemovalConfig,
122
+ BackgroundRemovalTemplate,
123
+ BackgroundRemovalVariable,
124
+ BackgroundRemovalSettings,
125
+ BackgroundRemovalResult,
126
+ DetectedObject,
127
+ } from './domain/entities/BackgroundRemovalConfig';
128
+
129
+ export {
130
+ validateBackgroundRemovalConfig,
131
+ createBackgroundRemovalVariable,
132
+ getProcessingTime,
133
+ getQualityScore,
134
+ } from './domain/entities/BackgroundRemovalConfig';
135
+
136
+ export type {
137
+ TextGenerationConfig,
138
+ TextGenerationTemplate,
139
+ TextGenerationVariable,
140
+ TextGenerationSettings,
141
+ TextGenerationResult,
142
+ } from './domain/entities/TextGenerationConfig';
143
+
144
+ export {
145
+ validateTextGenerationConfig,
146
+ createTextGenerationVariable,
147
+ getTokenCount,
148
+ getTemperature,
149
+ getTopP,
150
+ } from './domain/entities/TextGenerationConfig';
151
+
152
+ export type {
153
+ ColorizationConfig,
154
+ ColorizationTemplate,
155
+ ColorizationVariable,
156
+ ColorizationSettings,
157
+ ColorizationResult,
158
+ } from './domain/entities/ColorizationConfig';
159
+
160
+ export {
161
+ validateColorizationConfig,
162
+ createColorizationVariable,
163
+ getColorizationQuality,
164
+ getEraDescription,
165
+ getSuggestedColorPalette,
166
+ } from './domain/entities/ColorizationConfig';
167
+
168
+ export type {
169
+ FuturePredictionConfig,
170
+ FuturePredictionTemplate,
171
+ FuturePredictionVariable,
172
+ FuturePredictionSettings,
173
+ FuturePredictionResult,
174
+ FuturePredictionMetadata,
175
+ FuturePredictionOutputType,
176
+ } from './domain/entities/FuturePredictionConfig';
177
+
178
+ export {
179
+ validateFuturePredictionConfig,
180
+ createFuturePredictionVariable,
181
+ getFutureYear,
182
+ } from './domain/entities/FuturePredictionConfig';
183
+
184
+ // =============================================================================
185
+ // DOMAIN LAYER - Repository Interfaces
186
+ // =============================================================================
187
+
188
+ export type {
189
+ ITemplateRepository,
190
+ } from './domain/repositories/ITemplateRepository';
191
+
192
+ export type {
193
+ IPromptHistoryRepository,
194
+ } from './domain/repositories/IPromptHistoryRepository';
195
+
196
+ export type {
197
+ IFaceSwapService,
198
+ IPhotoRestorationService,
199
+ IImageEnhancementService,
200
+ IStyleTransferService,
201
+ IBackgroundRemovalService,
202
+ ITextGenerationService,
203
+ IColorizationService,
204
+ IPromptGenerationService,
205
+ IFuturePredictionService,
206
+ } from './domain/repositories/IAIPromptServices';
207
+
208
+ // =============================================================================
209
+ // INFRASTRUCTURE LAYER - Repositories
210
+ // =============================================================================
211
+
212
+ export { TemplateRepository } from './infrastructure/repositories/TemplateRepository';
213
+ export { PromptHistoryRepository } from './infrastructure/repositories/PromptHistoryRepository';
214
+
215
+ // =============================================================================
216
+ // INFRASTRUCTURE LAYER - Services
217
+ // =============================================================================
218
+
219
+ export { PromptGenerationService } from './infrastructure/services/PromptGenerationService';
220
+ export { FaceSwapService } from './infrastructure/services/FaceSwapService';
221
+ export { PhotoRestorationService } from './infrastructure/services/PhotoRestorationService';
222
+ export { ImageEnhancementService } from './infrastructure/services/ImageEnhancementService';
223
+ export { StyleTransferService } from './infrastructure/services/StyleTransferService';
224
+ export { BackgroundRemovalService } from './infrastructure/services/BackgroundRemovalService';
225
+ export { TextGenerationService } from './infrastructure/services/TextGenerationService';
226
+ export { ColorizationService } from './infrastructure/services/ColorizationService';
227
+ export { FuturePredictionService } from './infrastructure/services/FuturePredictionService';
228
+
229
+ // =============================================================================
230
+ // PRESENTATION LAYER - Theme
231
+ // =============================================================================
232
+
233
+ export type {
234
+ ThemeColors,
235
+ ThemeSpacing,
236
+ ThemeTypography,
237
+ Theme,
238
+ } from './presentation/theme/types';
239
+
240
+ export {
241
+ createTheme,
242
+ defaultTheme,
243
+ } from './presentation/theme/types';
244
+
245
+ export {
246
+ useTheme,
247
+ setTheme,
248
+ getTheme,
249
+ resetTheme,
250
+ } from './presentation/theme/theme';
251
+
252
+ export {
253
+ createStyleSheet,
254
+ spacing,
255
+ color,
256
+ typography,
257
+ } from './presentation/theme/utils';
258
+
259
+ // =============================================================================
260
+ // PRESENTATION LAYER - Hooks
261
+ // =============================================================================
262
+
263
+ export type {
264
+ AsyncState,
265
+ AsyncActions,
266
+ } from './presentation/hooks/useAsyncState';
267
+
268
+ export { useAsyncState } from './presentation/hooks/useAsyncState';
269
+
270
+ export type {
271
+ UseTemplateState,
272
+ UseTemplateActions,
273
+ } from './presentation/hooks/useTemplateRepository';
274
+
275
+ export { useTemplateRepository } from './presentation/hooks/useTemplateRepository';
276
+
277
+ export type {
278
+ UseFaceSwapState,
279
+ UseFaceSwapActions,
280
+ } from './presentation/hooks/useFaceSwap';
281
+
282
+ export { useFaceSwap } from './presentation/hooks/useFaceSwap';
283
+
284
+ export type {
285
+ UsePhotoRestorationState,
286
+ UsePhotoRestorationActions,
287
+ } from './presentation/hooks/usePhotoRestoration';
288
+
289
+ export { usePhotoRestoration } from './presentation/hooks/usePhotoRestoration';
290
+
291
+ export type {
292
+ UseImageEnhancementState,
293
+ UseImageEnhancementActions,
294
+ } from './presentation/hooks/useImageEnhancement';
295
+
296
+ export { useImageEnhancement } from './presentation/hooks/useImageEnhancement';
297
+
298
+ export type {
299
+ UseStyleTransferState,
300
+ UseStyleTransferActions,
301
+ } from './presentation/hooks/useStyleTransfer';
302
+
303
+ export { useStyleTransfer } from './presentation/hooks/useStyleTransfer';
304
+
305
+ export type {
306
+ AIConfig,
307
+ UseAIServicesState,
308
+ UseAIServicesActions,
309
+ } from './presentation/hooks/useAIServices';
310
+
311
+ export { useAIServices } from './presentation/hooks/useAIServices';
312
+
313
+ export type {
314
+ UsePromptGenerationState,
315
+ UsePromptGenerationActions,
316
+ } from './presentation/hooks/usePromptGeneration';
317
+
318
+ export { usePromptGeneration } from './presentation/hooks/usePromptGeneration';
@@ -0,0 +1,85 @@
1
+ import type { IPromptHistoryRepository } from '../../domain/repositories/IPromptHistoryRepository';
2
+ import type { GeneratedPrompt } from '../../domain/entities/GeneratedPrompt';
3
+ import type { AIPromptResult, AIPromptError } from '../../domain/entities/types';
4
+
5
+ export class PromptHistoryRepository implements IPromptHistoryRepository {
6
+ private storage: GeneratedPrompt[] = [];
7
+ private readonly maxStorageSize = 100;
8
+
9
+ async save(prompt: GeneratedPrompt): Promise<AIPromptResult<void>> {
10
+ try {
11
+ this.storage.push(prompt);
12
+ this.trimStorage();
13
+ return { success: true, data: undefined };
14
+ } catch (error) {
15
+ return {
16
+ success: false,
17
+ error: 'STORAGE_ERROR',
18
+ message: 'Failed to save prompt to history'
19
+ };
20
+ }
21
+ }
22
+
23
+ async findRecent(limit: number = 50): Promise<AIPromptResult<GeneratedPrompt[]>> {
24
+ try {
25
+ const prompts = this.storage.slice(-limit);
26
+ return { success: true, data: prompts };
27
+ } catch (error) {
28
+ return {
29
+ success: false,
30
+ error: 'STORAGE_ERROR',
31
+ message: 'Failed to retrieve recent prompts'
32
+ };
33
+ }
34
+ }
35
+
36
+ async findByTemplateId(
37
+ templateId: string,
38
+ limit: number = 20
39
+ ): Promise<AIPromptResult<GeneratedPrompt[]>> {
40
+ try {
41
+ const prompts = this.storage
42
+ .filter(prompt => prompt.templateId === templateId)
43
+ .slice(-limit);
44
+ return { success: true, data: prompts };
45
+ } catch (error) {
46
+ return {
47
+ success: false,
48
+ error: 'STORAGE_ERROR',
49
+ message: 'Failed to retrieve prompts by template ID'
50
+ };
51
+ }
52
+ }
53
+
54
+ async delete(id: string): Promise<AIPromptResult<void>> {
55
+ try {
56
+ this.storage = this.storage.filter(prompt => prompt.id !== id);
57
+ return { success: true, data: undefined };
58
+ } catch (error) {
59
+ return {
60
+ success: false,
61
+ error: 'STORAGE_ERROR',
62
+ message: 'Failed to delete prompt'
63
+ };
64
+ }
65
+ }
66
+
67
+ async clear(): Promise<AIPromptResult<void>> {
68
+ try {
69
+ this.storage = [];
70
+ return { success: true, data: undefined };
71
+ } catch (error) {
72
+ return {
73
+ success: false,
74
+ error: 'STORAGE_ERROR',
75
+ message: 'Failed to clear prompt history'
76
+ };
77
+ }
78
+ }
79
+
80
+ private trimStorage(): void {
81
+ if (this.storage.length > this.maxStorageSize) {
82
+ this.storage = this.storage.slice(-this.maxStorageSize);
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,77 @@
1
+ import type { ITemplateRepository } from '../../domain/repositories/ITemplateRepository';
2
+ import type { AIPromptTemplate } from '../../domain/entities/AIPromptTemplate';
3
+ import type { AIPromptCategory, AIPromptResult } from '../../domain/entities/types';
4
+
5
+ export class TemplateRepository implements ITemplateRepository {
6
+ private storage = new Map<string, AIPromptTemplate>();
7
+
8
+ async findById(id: string): Promise<AIPromptResult<AIPromptTemplate | null>> {
9
+ try {
10
+ const template = this.storage.get(id) || null;
11
+ return { success: true, data: template };
12
+ } catch (error) {
13
+ return {
14
+ success: false,
15
+ error: 'STORAGE_ERROR',
16
+ message: 'Failed to retrieve template'
17
+ };
18
+ }
19
+ }
20
+
21
+ async findByCategory(category: AIPromptCategory): Promise<AIPromptResult<AIPromptTemplate[]>> {
22
+ try {
23
+ const templates = Array.from(this.storage.values())
24
+ .filter(template => template.category === category);
25
+ return { success: true, data: templates };
26
+ } catch (error) {
27
+ return {
28
+ success: false,
29
+ error: 'STORAGE_ERROR',
30
+ message: 'Failed to retrieve templates by category'
31
+ };
32
+ }
33
+ }
34
+
35
+ async findAll(): Promise<AIPromptResult<AIPromptTemplate[]>> {
36
+ try {
37
+ const templates = Array.from(this.storage.values());
38
+ return { success: true, data: templates };
39
+ } catch (error) {
40
+ return {
41
+ success: false,
42
+ error: 'STORAGE_ERROR',
43
+ message: 'Failed to retrieve all templates'
44
+ };
45
+ }
46
+ }
47
+
48
+ async save(template: AIPromptTemplate): Promise<AIPromptResult<void>> {
49
+ try {
50
+ this.storage.set(template.id, template);
51
+ return { success: true, data: undefined };
52
+ } catch (error) {
53
+ return {
54
+ success: false,
55
+ error: 'STORAGE_ERROR',
56
+ message: 'Failed to save template'
57
+ };
58
+ }
59
+ }
60
+
61
+ async delete(id: string): Promise<AIPromptResult<void>> {
62
+ try {
63
+ this.storage.delete(id);
64
+ return { success: true, data: undefined };
65
+ } catch (error) {
66
+ return {
67
+ success: false,
68
+ error: 'STORAGE_ERROR',
69
+ message: 'Failed to delete template'
70
+ };
71
+ }
72
+ }
73
+
74
+ async exists(id: string): Promise<boolean> {
75
+ return this.storage.has(id);
76
+ }
77
+ }
@@ -0,0 +1,209 @@
1
+ import type { IBackgroundRemovalService } from '../../domain/repositories/IAIPromptServices';
2
+ import type {
3
+ BackgroundRemovalConfig,
4
+ } from '../../domain/entities/BackgroundRemovalConfig';
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
+ validateBackgroundRemovalConfig,
10
+ getProcessingTime,
11
+ getQualityScore
12
+ } from '../../domain/entities/BackgroundRemovalConfig';
13
+ import { PromptGenerationService } from '../services/PromptGenerationService';
14
+
15
+ const createBackgroundRemovalBaseTemplate = (config?: {
16
+ precision?: string;
17
+ edgeRefinement?: boolean;
18
+ }): string => {
19
+ const {
20
+ precision = 'accurate',
21
+ edgeRefinement = true
22
+ } = config || {};
23
+
24
+ return `
25
+ You are an expert AI background removal specialist.
26
+ This is a BACKGROUND REMOVAL task, not image generation.
27
+
28
+ BACKGROUND REMOVAL OBJECTIVES:
29
+ - Remove background with ${precision} precision
30
+ - ${edgeRefinement ? 'Refine edges for clean cutout' : 'Focus on speed over precision'}
31
+ - Preserve subject integrity and details
32
+ - Create professional-quality transparency
33
+
34
+ DETECTION PRINCIPLES:
35
+ - Accurately identify foreground subject
36
+ - Distinguish between subject and background
37
+ - Handle complex edges (hair, fur, transparent objects)
38
+ - Preserve fine details and textures
39
+
40
+ TECHNICAL REQUIREMENTS:
41
+ ${precision === 'ultra-accurate' ?
42
+ `- Maximum precision processing
43
+ - Advanced edge detection algorithms
44
+ - Multiple refinement passes
45
+ - Subpixel accuracy for edges` :
46
+ precision === 'accurate' ?
47
+ `- Standard precision processing
48
+ - Reliable edge detection
49
+ - Single refinement pass
50
+ - Good balance of speed/quality` :
51
+ `- Fast processing optimization
52
+ - Basic edge detection
53
+ - Quick subject isolation
54
+ - Suitable for simple backgrounds`}
55
+
56
+ EDGE HANDLING:
57
+ ${edgeRefinement ?
58
+ `- Feather edges naturally
59
+ - Preserve hair and fine details
60
+ - Remove halos and artifacts
61
+ - Smooth transitions` :
62
+ `- Focus on speed
63
+ - Basic edge processing
64
+ - Minimal refinement
65
+ - Quick turnaround time`}
66
+
67
+ SAFETY CONSTRAINTS:
68
+ - Preserve subject completely
69
+ - Do not alter foreground content
70
+ - Maintain important details
71
+ - Avoid over-removal of subject elements
72
+
73
+ OUTPUT:
74
+ Subject with transparent background,
75
+ ready for compositing or new background application.
76
+ `.trim();
77
+ };
78
+
79
+ export class BackgroundRemovalService implements IBackgroundRemovalService {
80
+ private promptService: PromptGenerationService;
81
+
82
+ constructor() {
83
+ this.promptService = new PromptGenerationService();
84
+ }
85
+
86
+ async generateTemplate(config: BackgroundRemovalConfig): Promise<AIPromptResult<AIPromptTemplate>> {
87
+ try {
88
+ if (!this.validateConfig(config)) {
89
+ return {
90
+ success: false,
91
+ error: 'VALIDATION_ERROR',
92
+ message: 'Invalid background removal configuration'
93
+ };
94
+ }
95
+
96
+ const template = this.createBackgroundRemovalTemplate(config);
97
+ return { success: true, data: template };
98
+ } catch (error) {
99
+ return {
100
+ success: false,
101
+ error: 'GENERATION_FAILED',
102
+ message: 'Failed to generate background removal template'
103
+ };
104
+ }
105
+ }
106
+
107
+ async generatePrompt(
108
+ template: AIPromptTemplate,
109
+ config: BackgroundRemovalConfig
110
+ ): Promise<AIPromptResult<string>> {
111
+ const variables = {
112
+ precision: config.precision,
113
+ edgeRefinement: config.edgeRefinement,
114
+ preserveHair: config.preserveHair,
115
+ outputFormat: config.outputFormat,
116
+ addNewBackground: config.addNewBackground,
117
+ processingTime: getProcessingTime(config.precision),
118
+ qualityScore: getQualityScore(config),
119
+ };
120
+
121
+ return this.promptService.generateFromTemplate(template, variables);
122
+ }
123
+
124
+ validateConfig(config: BackgroundRemovalConfig): boolean {
125
+ return validateBackgroundRemovalConfig(config);
126
+ }
127
+
128
+ estimateProcessingTime(config: BackgroundRemovalConfig): number {
129
+ return getProcessingTime(config.precision);
130
+ }
131
+
132
+ private createBackgroundRemovalTemplate(config: BackgroundRemovalConfig): AIPromptTemplate {
133
+ const templateId = `background-removal-${config.precision}`;
134
+
135
+ const baseTemplate = createBackgroundRemovalBaseTemplate({
136
+ precision: config.precision,
137
+ edgeRefinement: config.edgeRefinement,
138
+ });
139
+
140
+ return createAIPromptTemplate({
141
+ id: templateId,
142
+ name: `Background Removal: ${config.precision}`,
143
+ description: `Remove background with ${config.precision} precision`,
144
+ category: 'background-removal',
145
+ template: `${baseTemplate}
146
+
147
+ REMOVAL CONFIGURATION:
148
+ - Precision Level: ${config.precision}
149
+ - Edge Refinement: ${config.edgeRefinement}
150
+ - Preserve Hair: ${config.preserveHair}
151
+ - Output Format: ${config.outputFormat}
152
+ ${config.addNewBackground ? `- New Background: ${config.addNewBackground}` : ''}
153
+
154
+ SPECIFIC REQUIREMENTS:
155
+ ${this.getSpecificRequirements(config)}
156
+
157
+ PROCESSING EXPECTATIONS:
158
+ - Estimated Time: ${getProcessingTime(config.precision)} seconds
159
+ - Quality Score: ${Math.round(getQualityScore(config) * 100)}%
160
+ - Edge Detail: ${config.edgeRefinement ? 'High precision' : 'Standard precision'}
161
+ - Hair Handling: ${config.preserveHair ? 'Preserved with detail' : 'Standard processing'}
162
+
163
+ OUTPUT FORMAT:
164
+ ${config.outputFormat === 'transparent' ? 'Transparent PNG with alpha channel' :
165
+ config.outputFormat === 'png' ? 'PNG format with transparency' :
166
+ 'WebP format with transparency support'}
167
+
168
+ ${config.addNewBackground ? `NEW BACKGROUND:
169
+ Replace removed background with: ${config.addNewBackground}
170
+ Ensure proper blending and integration with subject.` : ''}
171
+
172
+ EXPECTED RESULT:
173
+ Clean subject with background removed,
174
+ ${config.edgeRefinement ? 'with refined edges and preserved details' : 'with standard edge quality'},
175
+ ready for ${config.outputFormat} output.
176
+ `.trim(),
177
+ variables: [],
178
+ safety: {
179
+ contentFilter: true,
180
+ adultContentFilter: true,
181
+ violenceFilter: true,
182
+ hateSpeechFilter: true,
183
+ copyrightFilter: true,
184
+ },
185
+ version: '1.0.0',
186
+ });
187
+ }
188
+
189
+ private getSpecificRequirements(config: BackgroundRemovalConfig): string {
190
+ const requirements: string[] = [];
191
+
192
+ if (config.preserveHair) {
193
+ requirements.push('- Advanced hair detection and preservation');
194
+ requirements.push('- Fine strand handling and transparency');
195
+ }
196
+
197
+ if (config.edgeRefinement) {
198
+ requirements.push('- Subpixel edge accuracy');
199
+ requirements.push('- Natural feathering and anti-aliasing');
200
+ }
201
+
202
+ if (config.precision === 'ultra-accurate') {
203
+ requirements.push('- Multi-pass refinement processing');
204
+ requirements.push('- Advanced artifact removal');
205
+ }
206
+
207
+ return requirements.join('\n') || '- Standard background removal processing';
208
+ }
209
+ }