@umituz/react-native-ai-gemini-provider 1.14.27 → 1.14.29
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 +1 -1
- package/src/domain/README.md +232 -0
- package/src/domain/constants/README.md +191 -0
- package/src/domain/entities/README.md +238 -0
- package/src/infrastructure/README.md +252 -0
- package/src/infrastructure/cache/CACHE_SYSTEM.md +213 -0
- package/src/infrastructure/cache/README.md +213 -0
- package/src/infrastructure/content/CONTENT_BUILDER.md +175 -0
- package/src/infrastructure/content/README.md +175 -0
- package/src/infrastructure/interceptors/README.md +226 -0
- package/src/infrastructure/interceptors/REQUEST_INTERCEPTORS.md +171 -0
- package/src/infrastructure/job/JOB_MANAGER.md +174 -0
- package/src/infrastructure/job/README.md +194 -0
- package/src/infrastructure/response/README.md +187 -0
- package/src/infrastructure/response/RESPONSE_FORMATTER.md +185 -0
- package/src/infrastructure/services/CORE_CLIENT_SERVICE.md +202 -0
- package/src/infrastructure/services/FEATURE_MODEL_SELECTOR_SERVICE.md +206 -0
- package/src/infrastructure/services/GENERATION_EXECUTOR_SERVICE.md +176 -0
- package/src/infrastructure/services/IMAGE_EDIT_SERVICE.md +169 -0
- package/src/infrastructure/services/IMAGE_GENERATION_SERVICE.md +166 -0
- package/src/infrastructure/services/JOB_PROCESSOR_SERVICE.md +174 -0
- package/src/infrastructure/services/PROVIDER_INITIALIZER_SERVICE.md +176 -0
- package/src/infrastructure/services/README.md +233 -0
- package/src/infrastructure/services/RETRY_SERVICE.md +178 -0
- package/src/infrastructure/services/STREAMING_SERVICE.md +166 -0
- package/src/infrastructure/services/STRUCTURED_TEXT_SERVICE.md +175 -0
- package/src/infrastructure/services/TEXT_GENERATION_SERVICE.md +160 -0
- package/src/infrastructure/services/VEO_HTTP_CLIENT_SERVICE.md +179 -0
- package/src/infrastructure/services/VEO_POLLING_SERVICE.md +173 -0
- package/src/infrastructure/services/VIDEO_DOWNLOADER_SERVICE.md +166 -0
- package/src/infrastructure/services/VIDEO_ERROR_HANDLER_SERVICE.md +185 -0
- package/src/infrastructure/services/VIDEO_GENERATION_SERVICE.md +176 -0
- package/src/infrastructure/services/VIDEO_URL_EXTRACTOR_SERVICE.md +186 -0
- package/src/infrastructure/telemetry/README.md +203 -0
- package/src/infrastructure/telemetry/TELEMETRY_SYSTEM.md +200 -0
- package/src/infrastructure/utils/DATA_TRANSFORMER_UTILS.md +175 -0
- package/src/infrastructure/utils/ERROR_MAPPER.md +170 -0
- package/src/infrastructure/utils/ERROR_UTILITIES.md +208 -0
- package/src/infrastructure/utils/IMAGE_PREPARER_UTILS.md +185 -0
- package/src/infrastructure/utils/INPUT_BUILDERS.md +214 -0
- package/src/infrastructure/utils/MODEL_VALIDATION_UTILS.md +189 -0
- package/src/infrastructure/utils/PERFORMANCE_UTILITIES.md +477 -0
- package/src/infrastructure/utils/PERFORMANCE_UTILS.md +219 -0
- package/src/infrastructure/utils/README.md +289 -0
- package/src/presentation/README.md +187 -0
- package/src/presentation/hooks/README.md +188 -0
- package/src/presentation/hooks/USE_GEMINI_HOOK.md +226 -0
- package/src/providers/README.md +247 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# Performance Utilities
|
|
2
|
+
|
|
3
|
+
Tools for measuring, tracking, and optimizing AI operation performance.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
measureAsync,
|
|
10
|
+
measureSync,
|
|
11
|
+
debounce,
|
|
12
|
+
throttle,
|
|
13
|
+
PerformanceTimer,
|
|
14
|
+
performanceTracker
|
|
15
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 🎯 Purpose
|
|
19
|
+
|
|
20
|
+
Use these utilities to monitor and optimize AI operation performance. Provides timing, debouncing, throttling, and performance tracking.
|
|
21
|
+
|
|
22
|
+
**When to use:**
|
|
23
|
+
- Measure API call duration
|
|
24
|
+
- Track performance metrics over time
|
|
25
|
+
- Implement debouncing for user input
|
|
26
|
+
- Throttle expensive operations
|
|
27
|
+
- Monitor slow operations
|
|
28
|
+
- Optimize application performance
|
|
29
|
+
|
|
30
|
+
## 📌 Strategy
|
|
31
|
+
|
|
32
|
+
Performance monitoring is crucial for UX. These utilities:
|
|
33
|
+
- Measure operation duration accurately
|
|
34
|
+
- Track metrics over time
|
|
35
|
+
- Provide performance statistics
|
|
36
|
+
- Enable optimization through debouncing/throttling
|
|
37
|
+
- Support performance debugging
|
|
38
|
+
|
|
39
|
+
**Key Decision**: Use `measureAsync()` for all critical AI operations to identify performance bottlenecks early.
|
|
40
|
+
|
|
41
|
+
## ⚠️ Rules
|
|
42
|
+
|
|
43
|
+
### Usage Rules
|
|
44
|
+
- **MUST** always stop PerformanceTimers
|
|
45
|
+
- **SHOULD** include metadata for context
|
|
46
|
+
- **MUST** track slow operations (>5s)
|
|
47
|
+
- **SHOULD** clear statistics periodically
|
|
48
|
+
- **MUST NOT** leak timers (always cleanup)
|
|
49
|
+
|
|
50
|
+
### Measurement Rules
|
|
51
|
+
- **SHOULD** measure async operations
|
|
52
|
+
- **MUST** use descriptive operation names
|
|
53
|
+
- **SHOULD** include relevant metadata
|
|
54
|
+
- **MUST** handle errors in measured operations
|
|
55
|
+
- **SHOULD NOT** measure trivial operations
|
|
56
|
+
|
|
57
|
+
### Debounce/Throttle Rules
|
|
58
|
+
- **SHOULD** debounce user input
|
|
59
|
+
- **MUST** throttle expensive operations
|
|
60
|
+
- **SHOULD** set appropriate time intervals
|
|
61
|
+
- **MUST NOT** debounce/throttle everything
|
|
62
|
+
- **SHOULD** consider UX impact
|
|
63
|
+
|
|
64
|
+
## 🤖 AI Agent Guidelines
|
|
65
|
+
|
|
66
|
+
### When Modifying These Utilities
|
|
67
|
+
1. **READ** the implementation file first
|
|
68
|
+
2. **UNDERSTAND** performance implications
|
|
69
|
+
3. **MAINTAIN** backward compatibility
|
|
70
|
+
4. **ADD** tests for new functionality
|
|
71
|
+
5. **UPDATE** type definitions
|
|
72
|
+
|
|
73
|
+
### When Adding New Features
|
|
74
|
+
1. **CHECK** if similar utility exists
|
|
75
|
+
2. **FOLLOW** existing patterns
|
|
76
|
+
3. **USE** established error handling
|
|
77
|
+
4. **DOCUMENT** in type definitions
|
|
78
|
+
5. **ADD** examples to tests (not docs)
|
|
79
|
+
|
|
80
|
+
### When Fixing Bugs
|
|
81
|
+
1. **REPRODUCE** bug locally first
|
|
82
|
+
2. **IDENTIFY** root cause
|
|
83
|
+
3. **FIX** with minimal changes
|
|
84
|
+
4. **ADD** regression test
|
|
85
|
+
5. **VERIFY** all tests pass
|
|
86
|
+
|
|
87
|
+
### Code Style Rules
|
|
88
|
+
- **USE** precise timing measurements
|
|
89
|
+
- **VALIDATE** inputs
|
|
90
|
+
- **HANDLE** errors gracefully
|
|
91
|
+
- **LOG** performance in development
|
|
92
|
+
- **COMMENT** complex logic only
|
|
93
|
+
|
|
94
|
+
## 📦 Available Utilities
|
|
95
|
+
|
|
96
|
+
### `measureAsync(operation, metadata?)`
|
|
97
|
+
|
|
98
|
+
Measure async operation duration.
|
|
99
|
+
|
|
100
|
+
**Refer to**: [`performance.util.ts`](./performance.util.ts)
|
|
101
|
+
|
|
102
|
+
### `measureSync(operation, metadata?)`
|
|
103
|
+
|
|
104
|
+
Measure sync operation duration.
|
|
105
|
+
|
|
106
|
+
**Refer to**: [`performance.util.ts`](./performance.util.ts)
|
|
107
|
+
|
|
108
|
+
### `debounce(func, wait)`
|
|
109
|
+
|
|
110
|
+
Debounce function calls.
|
|
111
|
+
|
|
112
|
+
**Refer to**: [`performance.util.ts`](./performance.util.ts)
|
|
113
|
+
|
|
114
|
+
### `throttle(func, limit)`
|
|
115
|
+
|
|
116
|
+
Throttle function calls.
|
|
117
|
+
|
|
118
|
+
**Refer to**: [`performance.util.ts`](./performance.util.ts)
|
|
119
|
+
|
|
120
|
+
### `PerformanceTimer`
|
|
121
|
+
|
|
122
|
+
Timer class for measuring operations.
|
|
123
|
+
|
|
124
|
+
**Refer to**: [`performance.util.ts`](./performance.util.ts)
|
|
125
|
+
|
|
126
|
+
### `performanceTracker`
|
|
127
|
+
|
|
128
|
+
Global performance statistics tracker.
|
|
129
|
+
|
|
130
|
+
**Refer to**: [`performance.util.ts`](./performance.util.ts)
|
|
131
|
+
|
|
132
|
+
## 🔗 Related Modules
|
|
133
|
+
|
|
134
|
+
- **Telemetry Service**: [`../services/TELEMETRY_SERVICE.md`](../services/TELEMETRY_SERVICE.md)
|
|
135
|
+
- **Retry Service**: [`../services/RETRY_SERVICE.md`](../services/RETRY_SERVICE.md)
|
|
136
|
+
- **Domain Types**: [`../../domain/entities/README.md`](../../domain/entities/README.md)
|
|
137
|
+
|
|
138
|
+
## 📋 Configuration Reference
|
|
139
|
+
|
|
140
|
+
### Performance Metrics
|
|
141
|
+
|
|
142
|
+
Tracked metrics include:
|
|
143
|
+
- `count`: Number of operations
|
|
144
|
+
- `avg`: Average duration (ms)
|
|
145
|
+
- `min`: Minimum duration (ms)
|
|
146
|
+
- `max`: Maximum duration (ms)
|
|
147
|
+
|
|
148
|
+
### Debounce/Throttle Timing
|
|
149
|
+
|
|
150
|
+
**Typical values:**
|
|
151
|
+
- User input debounce: 300-500ms
|
|
152
|
+
- API call throttle: 1000-2000ms
|
|
153
|
+
- Expensive operations: 2000-5000ms
|
|
154
|
+
|
|
155
|
+
## 🎓 Usage Patterns
|
|
156
|
+
|
|
157
|
+
### Measuring API Calls
|
|
158
|
+
1. Import `measureAsync`
|
|
159
|
+
2. Wrap API call in measurement
|
|
160
|
+
3. Include descriptive metadata
|
|
161
|
+
4. Check duration for slow operations
|
|
162
|
+
5. Log or report performance issues
|
|
163
|
+
|
|
164
|
+
### Performance Monitoring
|
|
165
|
+
1. Create `PerformanceTimer` instance
|
|
166
|
+
2. Start timer before operation
|
|
167
|
+
3. Stop timer after operation (in finally block)
|
|
168
|
+
4. Extract metrics from timer
|
|
169
|
+
5. Handle results appropriately
|
|
170
|
+
|
|
171
|
+
### Performance Statistics
|
|
172
|
+
1. Record operation with `performanceTracker.record()`
|
|
173
|
+
2. Get statistics with `getStats()`
|
|
174
|
+
3. Analyze average/min/max durations
|
|
175
|
+
4. Identify slow operations
|
|
176
|
+
5. Clear stats periodically
|
|
177
|
+
|
|
178
|
+
### Debouncing User Input
|
|
179
|
+
1. Import `debounce` utility
|
|
180
|
+
2. Wrap user input handler
|
|
181
|
+
3. Set appropriate delay (300-500ms)
|
|
182
|
+
4. Handle debounced calls
|
|
183
|
+
5. Update UI appropriately
|
|
184
|
+
|
|
185
|
+
### Throttling API Calls
|
|
186
|
+
1. Import `throttle` utility
|
|
187
|
+
2. Wrap expensive operation
|
|
188
|
+
3. Set minimum time between calls
|
|
189
|
+
4. Handle throttled execution
|
|
190
|
+
5. Provide user feedback
|
|
191
|
+
|
|
192
|
+
### Performance Comparison
|
|
193
|
+
1. Use `measureAsync` for each operation
|
|
194
|
+
2. Compare durations
|
|
195
|
+
3. Identify faster/slower methods
|
|
196
|
+
4. Optimize based on results
|
|
197
|
+
5. Document findings
|
|
198
|
+
|
|
199
|
+
## 🚨 Common Pitfalls
|
|
200
|
+
|
|
201
|
+
### Don't
|
|
202
|
+
- Forget to stop timers (memory leaks)
|
|
203
|
+
- Skip metadata (hard to analyze)
|
|
204
|
+
- Ignore slow operations
|
|
205
|
+
- Debounce/throttle everything
|
|
206
|
+
- Clear statistics too frequently
|
|
207
|
+
|
|
208
|
+
### Do
|
|
209
|
+
- Always stop timers in finally blocks
|
|
210
|
+
- Include descriptive metadata
|
|
211
|
+
- Monitor for slow operations (>5s)
|
|
212
|
+
- Set appropriate debounce/throttle intervals
|
|
213
|
+
- Clear stats periodically (daily)
|
|
214
|
+
- Consider UX when timing operations
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
**Last Updated**: 2025-01-08
|
|
219
|
+
**See Also**: [AI_GUIDELINES.md](../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# Utils
|
|
2
|
+
|
|
3
|
+
Utility functions and tools for Gemini provider. Handles error management, performance monitoring, data transformation, model validation, image preparation, and input building.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
// Error handling
|
|
10
|
+
mapGeminiError,
|
|
11
|
+
isGeminiErrorRetryable,
|
|
12
|
+
categorizeGeminiError,
|
|
13
|
+
createGeminiError,
|
|
14
|
+
|
|
15
|
+
// Model validation
|
|
16
|
+
isValidModel,
|
|
17
|
+
validateModel,
|
|
18
|
+
getSafeModel,
|
|
19
|
+
getModelCategory,
|
|
20
|
+
getAllValidModels,
|
|
21
|
+
|
|
22
|
+
// Performance
|
|
23
|
+
measureAsync,
|
|
24
|
+
measureSync,
|
|
25
|
+
debounce,
|
|
26
|
+
throttle,
|
|
27
|
+
performanceTracker,
|
|
28
|
+
|
|
29
|
+
// Data transformation
|
|
30
|
+
extractBase64Data,
|
|
31
|
+
extractTextFromResponse,
|
|
32
|
+
|
|
33
|
+
// Image preparation
|
|
34
|
+
prepareImageFromUri,
|
|
35
|
+
prepareImage,
|
|
36
|
+
isValidBase64,
|
|
37
|
+
|
|
38
|
+
// Input builders
|
|
39
|
+
buildUpscaleInput,
|
|
40
|
+
buildPhotoRestoreInput,
|
|
41
|
+
buildRemoveBackgroundInput,
|
|
42
|
+
buildReplaceBackgroundInput
|
|
43
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## 🎯 Purpose
|
|
47
|
+
|
|
48
|
+
Use utility functions for common operations across the provider. Provides error handling, validation, performance monitoring, and data transformation capabilities.
|
|
49
|
+
|
|
50
|
+
**When to use:**
|
|
51
|
+
- Validate model IDs
|
|
52
|
+
- Handle API errors
|
|
53
|
+
- Monitor performance
|
|
54
|
+
- Transform data formats
|
|
55
|
+
- Prepare images for processing
|
|
56
|
+
- Build complex inputs
|
|
57
|
+
- Measure operation duration
|
|
58
|
+
- Debounce/throttle functions
|
|
59
|
+
|
|
60
|
+
## 📌 Strategy
|
|
61
|
+
|
|
62
|
+
Utilities provide reusable, composable functions. This system:
|
|
63
|
+
- Separates concerns from business logic
|
|
64
|
+
- Provides consistent error handling
|
|
65
|
+
- Enables performance monitoring
|
|
66
|
+
- Simplifies data transformations
|
|
67
|
+
- Validates inputs centrally
|
|
68
|
+
- Builds complex inputs from simple data
|
|
69
|
+
|
|
70
|
+
**Key Decision**: Use utility functions for cross-cutting concerns. Keep services focused on business logic, use utils for operational tasks.
|
|
71
|
+
|
|
72
|
+
## ⚠️ Rules
|
|
73
|
+
|
|
74
|
+
### Error Handling Rules
|
|
75
|
+
- **MUST** catch and categorize all API errors
|
|
76
|
+
- **SHOULD** use typed GeminiError instances
|
|
77
|
+
- **MUST** check retryable status before retrying
|
|
78
|
+
- **SHOULD** log errors appropriately
|
|
79
|
+
- **MUST NOT** expose sensitive data in errors
|
|
80
|
+
|
|
81
|
+
### Model Validation Rules
|
|
82
|
+
- **MUST** validate model IDs before use
|
|
83
|
+
- **SHOULD** use safe fallbacks for invalid models
|
|
84
|
+
- **MUST** check model category for features
|
|
85
|
+
- **SHOULD** validate early in request flow
|
|
86
|
+
- **MUST NOT** allow arbitrary model IDs
|
|
87
|
+
|
|
88
|
+
### Performance Rules
|
|
89
|
+
- **SHOULD** measure critical operations
|
|
90
|
+
- **MUST** track performance metrics
|
|
91
|
+
- **SHOULD** use debounce/throttle for frequent calls
|
|
92
|
+
- **MUST NOT** impact performance with monitoring
|
|
93
|
+
- **SHOULD** aggregate metrics over time
|
|
94
|
+
|
|
95
|
+
### Data Transformation Rules
|
|
96
|
+
- **MUST** validate input data format
|
|
97
|
+
- **SHOULD** handle edge cases (null, undefined)
|
|
98
|
+
- **MUST** sanitize sensitive information
|
|
99
|
+
- **SHOULD** preserve data integrity
|
|
100
|
+
- **MUST** handle transformation errors
|
|
101
|
+
|
|
102
|
+
### Image Preparation Rules
|
|
103
|
+
- **MUST** validate base64 format
|
|
104
|
+
- **SHOULD** handle different image formats
|
|
105
|
+
- **MUST** extract MIME type correctly
|
|
106
|
+
- **SHOULD** handle large images efficiently
|
|
107
|
+
- **MUST** validate image data
|
|
108
|
+
|
|
109
|
+
## 🤖 AI Agent Guidelines
|
|
110
|
+
|
|
111
|
+
### When Handling Errors
|
|
112
|
+
1. **CATCH** errors from API calls
|
|
113
|
+
2. **MAP** to GeminiError using mapGeminiError()
|
|
114
|
+
3. **CHECK** if retryable using isGeminiErrorRetryable()
|
|
115
|
+
4. **CATEGORIZE** error type
|
|
116
|
+
5. **HANDLE** appropriately (retry or fail)
|
|
117
|
+
|
|
118
|
+
### When Validating Models
|
|
119
|
+
1. **CHECK** model validity with isValidModel()
|
|
120
|
+
2. **GET** safe model with getSafeModel()
|
|
121
|
+
3. **DETERMINE** model category
|
|
122
|
+
4. **VALIDATE** category supports feature
|
|
123
|
+
5. **USE** validated model in requests
|
|
124
|
+
|
|
125
|
+
### When Measuring Performance
|
|
126
|
+
1. **WRAP** operation with measureAsync()
|
|
127
|
+
2. **RECORD** duration with performanceTracker
|
|
128
|
+
3. **ANALYZE** metrics periodically
|
|
129
|
+
4. **IDENTIFY** slow operations
|
|
130
|
+
5. **OPTIMIZE** based on data
|
|
131
|
+
|
|
132
|
+
### When Preparing Images
|
|
133
|
+
1. **VALIDATE** image source (URI or base64)
|
|
134
|
+
2. **EXTRACT** base64 data if needed
|
|
135
|
+
3. **DETERMINE** MIME type
|
|
136
|
+
4. **PREPARE** image object
|
|
137
|
+
5. **HANDLE** preparation errors
|
|
138
|
+
|
|
139
|
+
### Code Style Rules
|
|
140
|
+
- **USE** appropriate utility for each task
|
|
141
|
+
- **VALIDATE** inputs early
|
|
142
|
+
- **HANDLE** all error cases
|
|
143
|
+
- **LOG** important operations
|
|
144
|
+
- **COMMENT** complex transformations
|
|
145
|
+
|
|
146
|
+
## 📦 Available Utilities
|
|
147
|
+
|
|
148
|
+
### Error Management
|
|
149
|
+
|
|
150
|
+
**Refer to**: [`ERROR_UTILITIES.md`](./ERROR_UTILITIES.md)
|
|
151
|
+
|
|
152
|
+
**Functions:**
|
|
153
|
+
- `mapGeminiError(error)` - Map error to GeminiError
|
|
154
|
+
- `isGeminiErrorRetryable(error)` - Check if error is retryable
|
|
155
|
+
- `categorizeGeminiError(error)` - Categorize error type
|
|
156
|
+
- `createGeminiError(error)` - Create GeminiError instance
|
|
157
|
+
|
|
158
|
+
### Model Validation
|
|
159
|
+
|
|
160
|
+
**Refer to**: [`MODEL_VALIDATION_UTILS.md`](./MODEL_VALIDATION_UTILS.md)
|
|
161
|
+
|
|
162
|
+
**Functions:**
|
|
163
|
+
- `isValidModel(model)` - Check if model ID is valid
|
|
164
|
+
- `validateModel(model)` - Validate or throw error
|
|
165
|
+
- `getSafeModel(model, defaultType)` - Get safe model with fallback
|
|
166
|
+
- `getModelCategory(model)` - Get model category
|
|
167
|
+
- `getAllValidModels()` - Get all valid model IDs
|
|
168
|
+
|
|
169
|
+
### Performance Tools
|
|
170
|
+
|
|
171
|
+
**Refer to**: [`PERFORMANCE_UTILS.md`](./PERFORMANCE_UTILS.md)
|
|
172
|
+
|
|
173
|
+
**Functions:**
|
|
174
|
+
- `measureAsync(operation, metadata?)` - Measure async operation
|
|
175
|
+
- `measureSync(operation, metadata?)` - Measure sync operation
|
|
176
|
+
- `debounce(func, wait)` - Create debounced function
|
|
177
|
+
- `throttle(func, limit)` - Create throttled function
|
|
178
|
+
|
|
179
|
+
**Classes:**
|
|
180
|
+
- `PerformanceTimer` - Timer for operations
|
|
181
|
+
- `performanceTracker` - Global performance tracker
|
|
182
|
+
|
|
183
|
+
### Data Transformation
|
|
184
|
+
|
|
185
|
+
**Refer to**: [`DATA_TRANSFORMER_UTILS.md`](./DATA_TRANSFORMER_UTILS.md)
|
|
186
|
+
|
|
187
|
+
**Functions:**
|
|
188
|
+
- `extractBase64Data(base64)` - Extract base64 from data URL
|
|
189
|
+
- `extractTextFromResponse(response)` - Extract text from Gemini response
|
|
190
|
+
|
|
191
|
+
### Image Preparation
|
|
192
|
+
|
|
193
|
+
**Refer to**: [`IMAGE_PREPARER_UTILS.md`](./IMAGE_PREPARER_UTILS.md)
|
|
194
|
+
|
|
195
|
+
**Functions:**
|
|
196
|
+
- `prepareImageFromUri(uri)` - Prepare image from URI
|
|
197
|
+
- `prepareImage(base64, mimeType)` - Prepare image from base64
|
|
198
|
+
- `isValidBase64(base64)` - Validate base64 format
|
|
199
|
+
|
|
200
|
+
### Input Builders
|
|
201
|
+
|
|
202
|
+
**Refer to**: [`INPUT_BUILDERS.md`](./INPUT_BUILDERS.md)
|
|
203
|
+
|
|
204
|
+
**Single Image Features:**
|
|
205
|
+
- `buildUpscaleInput(base64, options?)` - Build upscale input
|
|
206
|
+
- `buildPhotoRestoreInput(base64, options?)` - Build photo restore input
|
|
207
|
+
- `buildRemoveBackgroundInput(base64, options?)` - Build remove background input
|
|
208
|
+
- `buildReplaceBackgroundInput(base64, options)` - Build replace background input
|
|
209
|
+
|
|
210
|
+
**Dual Image Features:**
|
|
211
|
+
- `buildFaceSwapInput(sourceBase64, targetBase64, options?)` - Build face swap input
|
|
212
|
+
|
|
213
|
+
**Video Features:**
|
|
214
|
+
- `buildAIHugInput(base64, options?)` - Build AI hug input
|
|
215
|
+
- `buildAIKissInput(base64, options?)` - Build AI kiss input
|
|
216
|
+
- `buildVideoFromDualImagesInput(base64a, base64b, options?)` - Build dual image video input
|
|
217
|
+
|
|
218
|
+
## 🔗 Related Modules
|
|
219
|
+
|
|
220
|
+
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
221
|
+
- **Services**: [`../services/README.md`](../services/README.md)
|
|
222
|
+
- **Domain Types**: [`../../domain/README.md`](../../domain/README.md)
|
|
223
|
+
|
|
224
|
+
## 📋 Error Categories
|
|
225
|
+
|
|
226
|
+
### Retryable Errors
|
|
227
|
+
- `QUOTA_EXCEEDED` - API quota exceeded
|
|
228
|
+
- `RATE_LIMIT` - Rate limit hit
|
|
229
|
+
- `NETWORK` - Network error
|
|
230
|
+
- `TIMEOUT` - Request timeout
|
|
231
|
+
- `SERVER` - Server error (5xx)
|
|
232
|
+
|
|
233
|
+
### Non-Retryable Errors
|
|
234
|
+
- `AUTHENTICATION` - Invalid credentials
|
|
235
|
+
- `SAFETY` - Safety filter triggered
|
|
236
|
+
- `MODEL_NOT_FOUND` - Invalid model
|
|
237
|
+
- `VALIDATION` - Invalid request
|
|
238
|
+
- `UNKNOWN` - Unknown error
|
|
239
|
+
|
|
240
|
+
## 🎓 Usage Patterns
|
|
241
|
+
|
|
242
|
+
### Error Handling Pattern
|
|
243
|
+
1. Catch API error
|
|
244
|
+
2. Map to GeminiError
|
|
245
|
+
3. Check if retryable
|
|
246
|
+
4. Retry or fail appropriately
|
|
247
|
+
5. Log error for debugging
|
|
248
|
+
|
|
249
|
+
### Model Validation Pattern
|
|
250
|
+
1. Get user input for model
|
|
251
|
+
2. Validate with isValidModel()
|
|
252
|
+
3. Get safe model with fallback
|
|
253
|
+
4. Check model category
|
|
254
|
+
5. Use validated model
|
|
255
|
+
|
|
256
|
+
### Performance Measurement Pattern
|
|
257
|
+
1. Wrap operation with measureAsync()
|
|
258
|
+
2. Record duration to performanceTracker
|
|
259
|
+
3. Analyze metrics periodically
|
|
260
|
+
4. Identify optimization opportunities
|
|
261
|
+
5. Iterate on improvements
|
|
262
|
+
|
|
263
|
+
### Image Processing Pattern
|
|
264
|
+
1. Validate image source
|
|
265
|
+
2. Prepare image with utilities
|
|
266
|
+
3. Build input for feature
|
|
267
|
+
4. Send to image service
|
|
268
|
+
5. Handle result/errors
|
|
269
|
+
|
|
270
|
+
## 🚨 Common Pitfalls
|
|
271
|
+
|
|
272
|
+
### Don't
|
|
273
|
+
- Skip model validation
|
|
274
|
+
- Ignore error types
|
|
275
|
+
- Measure too frequently (performance impact)
|
|
276
|
+
- Mutate input data
|
|
277
|
+
- Assume all models support all features
|
|
278
|
+
|
|
279
|
+
### Do
|
|
280
|
+
- Always validate model IDs
|
|
281
|
+
- Handle all error categories
|
|
282
|
+
- Use debounce/throttle for frequent operations
|
|
283
|
+
- Keep transformations pure
|
|
284
|
+
- Check model capabilities
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
**Last Updated**: 2025-01-08
|
|
289
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# Presentation Layer
|
|
2
|
+
|
|
3
|
+
Bridge between UI layer and service layer. Contains React components and hooks for UI integration.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { useGemini } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use presentation layer to integrate AI functionality into React Native UI. Provides React hooks and state management.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Add AI features to React Native components
|
|
17
|
+
- Manage AI operation state in UI
|
|
18
|
+
- Handle loading and error states
|
|
19
|
+
- Create AI-powered user interfaces
|
|
20
|
+
- Build chat interfaces and forms
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
Presentation layer abstracts service complexity. This layer:
|
|
25
|
+
- Provides React hooks for AI operations
|
|
26
|
+
- Manages loading, error, and result states
|
|
27
|
+
- Handles component lifecycle
|
|
28
|
+
- Simplifies UI integration
|
|
29
|
+
- Enables reactive AI interactions
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Use hooks for React Native UI components. They provide clean, declarative integration with AI services.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Usage Rules
|
|
36
|
+
- **MUST** initialize provider before using hooks
|
|
37
|
+
- **SHOULD** use hooks only in React components
|
|
38
|
+
- **MUST** handle loading and error states
|
|
39
|
+
- **SHOULD** implement cleanup on unmount
|
|
40
|
+
- **MUST NOT** use hooks outside React functions
|
|
41
|
+
|
|
42
|
+
### State Management Rules
|
|
43
|
+
- **SHOULD** display loading indicators
|
|
44
|
+
- **MUST** handle and display errors
|
|
45
|
+
- **SHOULD** implement success callbacks
|
|
46
|
+
- **MUST** reset state when appropriate
|
|
47
|
+
- **SHOULD NOT** ignore state changes
|
|
48
|
+
|
|
49
|
+
### UI Integration Rules
|
|
50
|
+
- **MUST** provide user feedback during operations
|
|
51
|
+
- **SHOULD** disable controls during loading
|
|
52
|
+
- **MUST** handle all error states gracefully
|
|
53
|
+
- **SHOULD** update UI progressively
|
|
54
|
+
- **MUST NOT** block UI thread
|
|
55
|
+
|
|
56
|
+
### Performance Rules
|
|
57
|
+
- **SHOULD** implement cleanup on unmount
|
|
58
|
+
- **MUST** cancel operations on unmount
|
|
59
|
+
- **SHOULD** use memoization where appropriate
|
|
60
|
+
- **MUST NOT** create memory leaks
|
|
61
|
+
- **SHOULD** optimize re-renders
|
|
62
|
+
|
|
63
|
+
## 🤖 AI Agent Guidelines
|
|
64
|
+
|
|
65
|
+
### When Modifying Presentation Layer
|
|
66
|
+
1. **READ** existing hooks first
|
|
67
|
+
2. **UNDERSTAND** React patterns
|
|
68
|
+
3. **MAINTAIN** backward compatibility
|
|
69
|
+
4. **ADD** tests for new functionality
|
|
70
|
+
5. **UPDATE** documentation
|
|
71
|
+
|
|
72
|
+
### When Adding New Hooks
|
|
73
|
+
1. **CHECK** if similar hook exists
|
|
74
|
+
2. **FOLLOW** existing hook patterns
|
|
75
|
+
3. **USE** established state management
|
|
76
|
+
4. **DOCUMENT** in hook documentation
|
|
77
|
+
5. **ADD** examples to tests (not docs)
|
|
78
|
+
|
|
79
|
+
### When Creating Components
|
|
80
|
+
1. **FOLLOW** React best practices
|
|
81
|
+
2. **USE** hooks for state management
|
|
82
|
+
3. **HANDLE** all edge cases
|
|
83
|
+
4. **PROVIDE** TypeScript types
|
|
84
|
+
5. **TEST** component behavior
|
|
85
|
+
|
|
86
|
+
### Code Style Rules
|
|
87
|
+
- **FOLLOW** React hooks rules
|
|
88
|
+
- **USE** useMemo/useCallback for optimization
|
|
89
|
+
- **VALIDATE** props
|
|
90
|
+
- **HANDLE** errors gracefully
|
|
91
|
+
- **COMMENT** complex logic only
|
|
92
|
+
|
|
93
|
+
## 📦 Available Modules
|
|
94
|
+
|
|
95
|
+
### React Hooks
|
|
96
|
+
|
|
97
|
+
**useGemini**: Main hook for AI text generation
|
|
98
|
+
|
|
99
|
+
**Refer to**: [`hooks/README.md`](./hooks/README.md)
|
|
100
|
+
|
|
101
|
+
**Files:**
|
|
102
|
+
- [`hooks/use-gemini.ts`](./hooks/use-gemini.ts) - Hook implementation
|
|
103
|
+
- [`hooks/USE_GEMINI_HOOK.md`](./hooks/USE_GEMINI_HOOK.md) - Detailed documentation
|
|
104
|
+
|
|
105
|
+
## 🔗 Related Modules
|
|
106
|
+
|
|
107
|
+
- **Services**: [`../infrastructure/services/README.md`](../infrastructure/services/README.md)
|
|
108
|
+
- **Domain Types**: [`../domain/README.md`](../domain/README.md)
|
|
109
|
+
- **Providers**: [`../providers/README.md`](../providers/README.md)
|
|
110
|
+
|
|
111
|
+
## 📋 Hook Features
|
|
112
|
+
|
|
113
|
+
### State Management
|
|
114
|
+
- `result`: Generated content
|
|
115
|
+
- `isGenerating`: Loading state
|
|
116
|
+
- `error`: Error message
|
|
117
|
+
- `reset`: Clear all state
|
|
118
|
+
|
|
119
|
+
### Methods
|
|
120
|
+
- `generate(prompt)`: Generate text from prompt
|
|
121
|
+
- `generateWithImage(prompt, image, mimeType)`: Generate with image context
|
|
122
|
+
- `reset()`: Clear all state
|
|
123
|
+
|
|
124
|
+
### Configuration
|
|
125
|
+
- `model`: Model ID to use
|
|
126
|
+
- `generationConfig`: Generation parameters
|
|
127
|
+
- `onSuccess`: Success callback
|
|
128
|
+
- `onError`: Error callback
|
|
129
|
+
|
|
130
|
+
## 🎓 Usage Patterns
|
|
131
|
+
|
|
132
|
+
### Basic Integration
|
|
133
|
+
1. Import hook in component
|
|
134
|
+
2. Call `useGemini()` hook
|
|
135
|
+
3. Use returned methods and state
|
|
136
|
+
4. Display loading, error, and result states
|
|
137
|
+
5. Handle user interactions
|
|
138
|
+
|
|
139
|
+
### Chat Interface
|
|
140
|
+
1. Use hook for AI responses
|
|
141
|
+
2. Maintain messages array in state
|
|
142
|
+
3. Call `generate()` on user input
|
|
143
|
+
4. Update messages with result
|
|
144
|
+
5. Display conversation
|
|
145
|
+
|
|
146
|
+
### Form Integration
|
|
147
|
+
1. Integrate hook with form
|
|
148
|
+
2. Call generation on form submit
|
|
149
|
+
3. Disable form during generation
|
|
150
|
+
4. Display errors appropriately
|
|
151
|
+
5. Handle success/failure
|
|
152
|
+
|
|
153
|
+
### Image Analysis
|
|
154
|
+
1. Use `generateWithImage()` method
|
|
155
|
+
2. Provide base64 image data
|
|
156
|
+
3. Handle loading and result states
|
|
157
|
+
4. Display analysis result
|
|
158
|
+
5. Handle errors
|
|
159
|
+
|
|
160
|
+
### State Resetting
|
|
161
|
+
1. Use `reset()` to clear state
|
|
162
|
+
2. Implement cleanup on unmount
|
|
163
|
+
3. Reset before new operations
|
|
164
|
+
4. Clear previous results
|
|
165
|
+
5. Handle user actions
|
|
166
|
+
|
|
167
|
+
## 🚨 Common Pitfalls
|
|
168
|
+
|
|
169
|
+
### Don't
|
|
170
|
+
- Use hooks outside React functions
|
|
171
|
+
- Forget to handle loading states
|
|
172
|
+
- Ignore error states
|
|
173
|
+
- Skip cleanup on unmount
|
|
174
|
+
- Block UI during operations
|
|
175
|
+
|
|
176
|
+
### Do
|
|
177
|
+
- Always handle loading and error states
|
|
178
|
+
- Provide user feedback
|
|
179
|
+
- Implement cleanup
|
|
180
|
+
- Disable controls during loading
|
|
181
|
+
- Reset state appropriately
|
|
182
|
+
- Handle all edge cases
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
**Last Updated**: 2025-01-08
|
|
187
|
+
**See Also**: [AI_GUIDELINES.md](../../../AI_GUIDELINES.md)
|