@umituz/react-native-ai-generation-content 1.17.229 → 1.17.231
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/LICENSE +21 -0
- package/README.md +346 -0
- package/package.json +1 -3
- package/src/domain/README.md +503 -0
- package/src/domains/content-moderation/README.md +363 -0
- package/src/domains/creations/README.md +394 -0
- package/src/domains/face-detection/README.md +395 -0
- package/src/domains/prompts/README.md +387 -0
- package/src/features/ai-hug/README.md +276 -0
- package/src/features/ai-kiss/README.md +276 -0
- package/src/features/anime-selfie/README.md +325 -0
- package/src/features/audio-generation/README.md +370 -0
- package/src/features/colorization/README.md +289 -0
- package/src/features/couple-future/README.md +270 -0
- package/src/features/face-swap/README.md +431 -0
- package/src/features/future-prediction/README.md +281 -0
- package/src/features/hd-touch-up/README.md +309 -0
- package/src/features/image-captioning/README.md +361 -0
- package/src/features/image-to-image/README.md +418 -0
- package/src/features/image-to-video/README.md +369 -0
- package/src/features/inpainting/README.md +302 -0
- package/src/features/meme-generator/README.md +327 -0
- package/src/features/photo-restoration/README.md +286 -0
- package/src/features/remove-background/README.md +292 -0
- package/src/features/remove-object/README.md +352 -0
- package/src/features/replace-background/README.md +288 -0
- package/src/features/script-generator/README.md +362 -0
- package/src/features/shared/README.md +280 -0
- package/src/features/sketch-to-image/README.md +296 -0
- package/src/features/style-transfer/README.md +301 -0
- package/src/features/text-to-image/README.md +394 -0
- package/src/features/text-to-video/README.md +245 -0
- package/src/features/text-to-voice/README.md +335 -0
- package/src/features/upscaling/README.md +247 -0
- package/src/infrastructure/config/README.md +310 -0
- package/src/infrastructure/middleware/README.md +378 -0
- package/src/infrastructure/orchestration/README.md +362 -0
- package/src/infrastructure/services/README.md +382 -0
- package/src/infrastructure/utils/README.md +523 -0
- package/src/infrastructure/wrappers/README.md +336 -0
- package/src/presentation/components/README.md +535 -0
- package/src/presentation/components/result/ResultStoryCard.tsx +1 -6
- package/src/presentation/hooks/README.md +380 -0
- package/src/presentation/layouts/README.md +374 -0
- package/src/presentation/screens/README.md +430 -0
- package/src/presentation/types/result-config.types.ts +3 -3
- package/src/presentation/layouts/types/.npmignore.tmp +0 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
# Infrastructure Middleware
|
|
2
|
+
|
|
3
|
+
Request/response middleware for AI generation operations.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The middleware module provides a flexible middleware system for intercepting and modifying AI generation requests and responses. Common use cases include credit checks, history tracking, content moderation, and logging.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Request interception and modification
|
|
12
|
+
- Response processing
|
|
13
|
+
- Async middleware support
|
|
14
|
+
- Error handling
|
|
15
|
+
- Chainable middleware
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Creating Middleware
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import type { GenerationMiddleware } from '@umituz/react-native-ai-generation-content';
|
|
23
|
+
|
|
24
|
+
const loggingMiddleware: GenerationMiddleware = {
|
|
25
|
+
name: 'logging',
|
|
26
|
+
|
|
27
|
+
before: async (context) => {
|
|
28
|
+
console.log('[Before]', {
|
|
29
|
+
featureType: context.featureType,
|
|
30
|
+
inputData: context.inputData,
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
after: async (context) => {
|
|
35
|
+
console.log('[After]', {
|
|
36
|
+
featureType: context.featureType,
|
|
37
|
+
result: context.result,
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
onError: async (context) => {
|
|
42
|
+
console.error('[Error]', {
|
|
43
|
+
featureType: context.featureType,
|
|
44
|
+
error: context.error,
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Credit Check Middleware
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { createCreditCheckMiddleware } from '@umituz/react-native-ai-generation-content';
|
|
54
|
+
|
|
55
|
+
const creditMiddleware = createCreditCheckMiddleware({
|
|
56
|
+
creditCost: 1, // Cost per generation
|
|
57
|
+
paywallThreshold: 5, // Show paywall after 5 insufficient credit attempts
|
|
58
|
+
onInsufficientCredits: async (userId, cost) => {
|
|
59
|
+
Alert.alert(
|
|
60
|
+
'Insufficient Credits',
|
|
61
|
+
`You need ${cost} credits to generate. Upgrade now?`,
|
|
62
|
+
[
|
|
63
|
+
{ text: 'Cancel', style: 'cancel' },
|
|
64
|
+
{ text: 'Upgrade', onPress: () => navigateToUpgrade() },
|
|
65
|
+
]
|
|
66
|
+
);
|
|
67
|
+
},
|
|
68
|
+
onPaywallTrigger: async () => {
|
|
69
|
+
await navigateToPaywall();
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### History Tracking Middleware
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
import { createHistoryTrackingMiddleware } from '@umituz/react-native-ai-generation-content';
|
|
78
|
+
|
|
79
|
+
const historyMiddleware = createHistoryTrackingMiddleware({
|
|
80
|
+
maxHistorySize: 100, // Keep last 100 generations
|
|
81
|
+
storage: AsyncStorage, // Storage implementation
|
|
82
|
+
onHistoryUpdate: async (history) => {
|
|
83
|
+
console.log('History updated:', history.length);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Content Moderation Middleware
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
import { ModerationWrapper } from '@umituz/react-native-ai-generation-content';
|
|
92
|
+
|
|
93
|
+
const moderationMiddleware = {
|
|
94
|
+
name: 'moderation',
|
|
95
|
+
|
|
96
|
+
before: async (context) => {
|
|
97
|
+
// Check prompt for inappropriate content
|
|
98
|
+
if (context.inputData.prompt) {
|
|
99
|
+
const moderationResult = await moderateText(context.inputData.prompt);
|
|
100
|
+
if (!moderationResult.isSafe) {
|
|
101
|
+
throw new Error('Content flagged as inappropriate');
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Custom Middleware
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
const customMiddleware: GenerationMiddleware = {
|
|
112
|
+
name: 'custom',
|
|
113
|
+
|
|
114
|
+
before: async (context) => {
|
|
115
|
+
// Modify request before processing
|
|
116
|
+
if (context.featureType === 'text-to-image') {
|
|
117
|
+
context.inputData.prompt = enhancePrompt(context.inputData.prompt);
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
after: async (context) => {
|
|
122
|
+
// Process response after generation
|
|
123
|
+
if (context.result.success) {
|
|
124
|
+
await cacheResult(context.result);
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
onError: async (context) => {
|
|
129
|
+
// Handle errors
|
|
130
|
+
await logError(context.error);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Middleware Chain
|
|
136
|
+
|
|
137
|
+
### Creating a Chain
|
|
138
|
+
|
|
139
|
+
```tsx
|
|
140
|
+
import { MiddlewareChain } from '@umituz/react-native-ai-generation-content';
|
|
141
|
+
|
|
142
|
+
const chain = new MiddlewareChain([
|
|
143
|
+
creditMiddleware,
|
|
144
|
+
historyMiddleware,
|
|
145
|
+
moderationMiddleware,
|
|
146
|
+
loggingMiddleware,
|
|
147
|
+
]);
|
|
148
|
+
|
|
149
|
+
await chain.execute({
|
|
150
|
+
featureType: 'text-to-image',
|
|
151
|
+
inputData: { prompt: 'A sunset' },
|
|
152
|
+
execute: async (input) => {
|
|
153
|
+
// Actual generation logic
|
|
154
|
+
return await generateImage(input);
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Execution Order
|
|
160
|
+
|
|
161
|
+
Middlewares execute in order:
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
Request → [Middleware 1] → [Middleware 2] → [Middleware 3] → Generation
|
|
165
|
+
↓ before ↓ before ↓ before
|
|
166
|
+
|
|
167
|
+
Response ← [Middleware 1] ← [Middleware 2] ← [Middleware 3] ← Result
|
|
168
|
+
↓ after ↓ after ↓ after
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Error Handling
|
|
172
|
+
|
|
173
|
+
If a middleware throws an error:
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
Request → [Middleware 1] → [Middleware 2] ✗ Error
|
|
177
|
+
↓ before ↓ onError
|
|
178
|
+
|
|
179
|
+
Response ← [Middleware 1] ← Error Propagated
|
|
180
|
+
↓ onError
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Context Types
|
|
184
|
+
|
|
185
|
+
### MiddlewareContext (Before)
|
|
186
|
+
|
|
187
|
+
```tsx
|
|
188
|
+
interface MiddlewareContext {
|
|
189
|
+
featureType: string;
|
|
190
|
+
inputData: any;
|
|
191
|
+
userId?: string;
|
|
192
|
+
options?: Record<string, any>;
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### MiddlewareResultContext (After)
|
|
197
|
+
|
|
198
|
+
```tsx
|
|
199
|
+
interface MiddlewareResultContext extends MiddlewareContext {
|
|
200
|
+
result: GenerationResult;
|
|
201
|
+
duration: number; // Time taken in ms
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### MiddlewareErrorContext (Error)
|
|
206
|
+
|
|
207
|
+
```tsx
|
|
208
|
+
interface MiddlewareErrorContext extends MiddlewareContext {
|
|
209
|
+
error: Error;
|
|
210
|
+
errorType: AIErrorType;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Advanced Usage
|
|
215
|
+
|
|
216
|
+
### Conditional Middleware
|
|
217
|
+
|
|
218
|
+
```tsx
|
|
219
|
+
const conditionalMiddleware: GenerationMiddleware = {
|
|
220
|
+
name: 'conditional',
|
|
221
|
+
|
|
222
|
+
before: async (context) => {
|
|
223
|
+
// Only apply to specific features
|
|
224
|
+
if (context.featureType === 'text-to-image') {
|
|
225
|
+
// Do something
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Async Middleware
|
|
232
|
+
|
|
233
|
+
```tsx
|
|
234
|
+
const asyncMiddleware: GenerationMiddleware = {
|
|
235
|
+
name: 'async',
|
|
236
|
+
|
|
237
|
+
before: async (context) => {
|
|
238
|
+
// Fetch data from API
|
|
239
|
+
const userData = await fetchUserData(context.userId);
|
|
240
|
+
context.inputData.userData = userData;
|
|
241
|
+
},
|
|
242
|
+
};
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Modifying Input
|
|
246
|
+
|
|
247
|
+
```tsx
|
|
248
|
+
const inputModifier: GenerationMiddleware = {
|
|
249
|
+
name: 'input-modifier',
|
|
250
|
+
|
|
251
|
+
before: async (context) => {
|
|
252
|
+
// Add default options
|
|
253
|
+
if (!context.inputData.options) {
|
|
254
|
+
context.inputData.options = {};
|
|
255
|
+
}
|
|
256
|
+
context.inputData.options.quality = 'high';
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Processing Result
|
|
262
|
+
|
|
263
|
+
```tsx
|
|
264
|
+
const resultProcessor: GenerationMiddleware = {
|
|
265
|
+
name: 'result-processor',
|
|
266
|
+
|
|
267
|
+
after: async (context) => {
|
|
268
|
+
// Add metadata to result
|
|
269
|
+
context.result.metadata = {
|
|
270
|
+
...context.result.metadata,
|
|
271
|
+
processedAt: new Date().toISOString(),
|
|
272
|
+
};
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Best Practices
|
|
278
|
+
|
|
279
|
+
1. **Order Matters**: Put credit checks before generation
|
|
280
|
+
2. **Error Handling**: Always handle errors gracefully
|
|
281
|
+
3. **Performance**: Keep middleware lightweight
|
|
282
|
+
4. **Async Safety**: Handle async operations properly
|
|
283
|
+
5. **Side Effects**: Be careful with state mutations
|
|
284
|
+
|
|
285
|
+
## Example: Complete Setup
|
|
286
|
+
|
|
287
|
+
```tsx
|
|
288
|
+
import {
|
|
289
|
+
createCreditCheckMiddleware,
|
|
290
|
+
createHistoryTrackingMiddleware,
|
|
291
|
+
MiddlewareChain,
|
|
292
|
+
} from '@umituz/react-native-ai-generation-content';
|
|
293
|
+
|
|
294
|
+
// Create middlewares
|
|
295
|
+
const creditMiddleware = createCreditCheckMiddleware({
|
|
296
|
+
creditCost: 1,
|
|
297
|
+
onInsufficientCredits: async (userId) => {
|
|
298
|
+
Alert.alert('Low Credits', 'Please upgrade');
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const historyMiddleware = createHistoryTrackingMiddleware({
|
|
303
|
+
maxHistorySize: 100,
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
const analyticsMiddleware: GenerationMiddleware = {
|
|
307
|
+
name: 'analytics',
|
|
308
|
+
|
|
309
|
+
before: async (context) => {
|
|
310
|
+
await Analytics.track('generation_started', {
|
|
311
|
+
feature: context.featureType,
|
|
312
|
+
});
|
|
313
|
+
},
|
|
314
|
+
|
|
315
|
+
after: async (context) => {
|
|
316
|
+
await Analytics.track('generation_completed', {
|
|
317
|
+
feature: context.featureType,
|
|
318
|
+
success: context.result.success,
|
|
319
|
+
});
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
// Create chain
|
|
324
|
+
const middlewareChain = new MiddlewareChain([
|
|
325
|
+
creditMiddleware,
|
|
326
|
+
historyMiddleware,
|
|
327
|
+
analyticsMiddleware,
|
|
328
|
+
]);
|
|
329
|
+
|
|
330
|
+
// Use chain
|
|
331
|
+
const result = await middlewareChain.execute({
|
|
332
|
+
featureType: 'text-to-image',
|
|
333
|
+
inputData: { prompt: 'A sunset' },
|
|
334
|
+
execute: async (input) => {
|
|
335
|
+
return await generateImage(input);
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
## Testing
|
|
341
|
+
|
|
342
|
+
```tsx
|
|
343
|
+
import { MiddlewareChain } from '@umituz/react-native-ai-generation-content';
|
|
344
|
+
|
|
345
|
+
test('middleware executes in order', async () => {
|
|
346
|
+
const executionOrder: string[] = [];
|
|
347
|
+
|
|
348
|
+
const middleware1: GenerationMiddleware = {
|
|
349
|
+
name: 'middleware1',
|
|
350
|
+
before: async () => {
|
|
351
|
+
executionOrder.push('m1-before');
|
|
352
|
+
},
|
|
353
|
+
after: async () => {
|
|
354
|
+
executionOrder.push('m1-after');
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const chain = new MiddlewareChain([middleware1]);
|
|
359
|
+
|
|
360
|
+
await chain.execute({
|
|
361
|
+
featureType: 'test',
|
|
362
|
+
inputData: {},
|
|
363
|
+
execute: async () => ({ success: true }),
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
expect(executionOrder).toEqual(['m1-before', 'm1-after']);
|
|
367
|
+
});
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
## Related
|
|
371
|
+
|
|
372
|
+
- [Config](../config/) - Service configuration
|
|
373
|
+
- [Services](../services/) - AI generation services
|
|
374
|
+
- [Orchestration](../orchestration/) - Generation orchestration
|
|
375
|
+
|
|
376
|
+
## License
|
|
377
|
+
|
|
378
|
+
MIT
|