@umituz/react-native-ai-gemini-provider 1.14.28 → 1.14.30
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 +41 -3
- 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/services/gemini-provider.ts +9 -2
- 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,170 @@
|
|
|
1
|
+
# Error Mapper Utility
|
|
2
|
+
|
|
3
|
+
Maps Gemini API errors to standardized error types with retry information. Provides consistent error categorization.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
mapGeminiError,
|
|
10
|
+
isGeminiErrorRetryable,
|
|
11
|
+
categorizeGeminiError
|
|
12
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 🎯 Purpose
|
|
16
|
+
|
|
17
|
+
Use error mapper to categorize and handle Gemini API errors consistently. Provides retry information and error types.
|
|
18
|
+
|
|
19
|
+
**When to use:**
|
|
20
|
+
- Categorize API errors
|
|
21
|
+
- Determine retry eligibility
|
|
22
|
+
- Handle error responses
|
|
23
|
+
- Implement retry logic
|
|
24
|
+
- Provide user feedback
|
|
25
|
+
|
|
26
|
+
## 📌 Strategy
|
|
27
|
+
|
|
28
|
+
Consistent error handling improves reliability. This utility:
|
|
29
|
+
- Maps errors to standard types
|
|
30
|
+
- Provides retry information
|
|
31
|
+
- Categorizes errors appropriately
|
|
32
|
+
- Includes status codes
|
|
33
|
+
- Simplifies error handling
|
|
34
|
+
|
|
35
|
+
**Key Decision**: Always map errors before handling. Provides consistent error information across the application.
|
|
36
|
+
|
|
37
|
+
## ⚠️ Rules
|
|
38
|
+
|
|
39
|
+
### Error Mapping Rules
|
|
40
|
+
- **MUST** map all API errors
|
|
41
|
+
- **SHOULD** check retry status
|
|
42
|
+
- **MUST** handle unknown errors
|
|
43
|
+
- **SHOULD** preserve error context
|
|
44
|
+
- **MUST NOT** expose sensitive data
|
|
45
|
+
|
|
46
|
+
### Retry Rules
|
|
47
|
+
- **SHOULD** retry retryable errors
|
|
48
|
+
- **MUST** implement backoff
|
|
49
|
+
- **SHOULD** set retry limits
|
|
50
|
+
- **MUST NOT** retry non-retryable errors
|
|
51
|
+
- **SHOULD** track retry attempts
|
|
52
|
+
|
|
53
|
+
### Categorization Rules
|
|
54
|
+
- **MUST** use correct error type
|
|
55
|
+
- **SHOULD** check error category
|
|
56
|
+
- **MUST** handle all categories
|
|
57
|
+
- **SHOULD** provide context
|
|
58
|
+
- **MUST NOT** miscategorize errors
|
|
59
|
+
|
|
60
|
+
### Error Handling Rules
|
|
61
|
+
- **MUST** catch and map errors
|
|
62
|
+
- **SHOULD** log error details
|
|
63
|
+
- **MUST** inform user appropriately
|
|
64
|
+
- **SHOULD** provide recovery options
|
|
65
|
+
- **MUST NOT** suppress errors
|
|
66
|
+
|
|
67
|
+
## 🤖 AI Agent Guidelines
|
|
68
|
+
|
|
69
|
+
### When Mapping Errors
|
|
70
|
+
1. **CATCH** API error
|
|
71
|
+
2. **CALL** mapGeminiError()
|
|
72
|
+
3. **GET** error information
|
|
73
|
+
4. **CHECK** retry status
|
|
74
|
+
5. **HANDLE** appropriately
|
|
75
|
+
|
|
76
|
+
### When Checking Retry Eligibility
|
|
77
|
+
1. **CALL** isGeminiErrorRetryable()
|
|
78
|
+
2. **CHECK** return value
|
|
79
|
+
3. **RETRY** if true
|
|
80
|
+
4. **FAIL** if false
|
|
81
|
+
5. **IMPLEMENT** backoff
|
|
82
|
+
|
|
83
|
+
### When Categorizing Errors
|
|
84
|
+
1. **CALL** categorizeGeminiError()
|
|
85
|
+
2. **GET** error type
|
|
86
|
+
3. **SWITCH** on category
|
|
87
|
+
4. **HANDLE** each case
|
|
88
|
+
5. **PROVIDE** user feedback
|
|
89
|
+
|
|
90
|
+
### Code Style Rules
|
|
91
|
+
- **MAP** all errors consistently
|
|
92
|
+
- **CHECK** retry before retrying
|
|
93
|
+
- **HANDLE** all error types
|
|
94
|
+
- **PROVIDE** clear messages
|
|
95
|
+
- **LOG** error information
|
|
96
|
+
|
|
97
|
+
## 📦 Available Functions
|
|
98
|
+
|
|
99
|
+
**Refer to**: [`error-mapper.util.ts`](./error-mapper.util.ts)
|
|
100
|
+
|
|
101
|
+
### Error Mapping
|
|
102
|
+
- `mapGeminiError(error)` - Map error to GeminiErrorInfo
|
|
103
|
+
- `isGeminiErrorRetryable(error)` - Check if error is retryable
|
|
104
|
+
- `categorizeGeminiError(error)` - Categorize error type
|
|
105
|
+
|
|
106
|
+
## 🔗 Related Modules
|
|
107
|
+
|
|
108
|
+
- **Error Utilities**: [`./ERROR_UTILITIES.md`](./ERROR_UTILITIES.md)
|
|
109
|
+
- **Retry Service**: [`../services/RETRY_SERVICE.md`](../services/RETRY_SERVICE.md)
|
|
110
|
+
- **Domain Types**: [`../../domain/README.md`](../../domain/README.md)
|
|
111
|
+
|
|
112
|
+
## 📋 Error Categories
|
|
113
|
+
|
|
114
|
+
### Retryable Errors
|
|
115
|
+
- `QUOTA_EXCEEDED` - API quota exceeded
|
|
116
|
+
- `RATE_LIMIT` - Rate limit hit
|
|
117
|
+
- `NETWORK` - Network error
|
|
118
|
+
- `TIMEOUT` - Request timeout
|
|
119
|
+
- `SERVER` - Server error (5xx)
|
|
120
|
+
|
|
121
|
+
### Non-Retryable Errors
|
|
122
|
+
- `AUTHENTICATION` - Invalid credentials
|
|
123
|
+
- `SAFETY` - Safety filter triggered
|
|
124
|
+
- `MODEL_NOT_FOUND` - Invalid model
|
|
125
|
+
- `VALIDATION` - Invalid request
|
|
126
|
+
- `UNKNOWN` - Unknown error
|
|
127
|
+
|
|
128
|
+
## 🎓 Usage Patterns
|
|
129
|
+
|
|
130
|
+
### Error Handling
|
|
131
|
+
1. Catch API error
|
|
132
|
+
2. Map to GeminiError
|
|
133
|
+
3. Check retryable status
|
|
134
|
+
4. Retry or fail appropriately
|
|
135
|
+
5. Provide user feedback
|
|
136
|
+
|
|
137
|
+
### Retry Logic
|
|
138
|
+
1. Check isGeminiErrorRetryable()
|
|
139
|
+
2. If true, implement retry
|
|
140
|
+
3. Use exponential backoff
|
|
141
|
+
4. Set retry limit
|
|
142
|
+
5. Fail after max retries
|
|
143
|
+
|
|
144
|
+
### Error Categorization
|
|
145
|
+
1. Call categorizeGeminiError()
|
|
146
|
+
2. Get error type
|
|
147
|
+
3. Switch on type
|
|
148
|
+
4. Handle each case
|
|
149
|
+
5. Provide specific feedback
|
|
150
|
+
|
|
151
|
+
## 🚨 Common Pitfalls
|
|
152
|
+
|
|
153
|
+
### Don't
|
|
154
|
+
- Skip error mapping
|
|
155
|
+
- Retry without checking
|
|
156
|
+
- Ignore error types
|
|
157
|
+
- Suppress errors
|
|
158
|
+
- Expose sensitive data
|
|
159
|
+
|
|
160
|
+
### Do
|
|
161
|
+
- Always map errors
|
|
162
|
+
- Check retry status
|
|
163
|
+
- Handle all categories
|
|
164
|
+
- Provide clear feedback
|
|
165
|
+
- Log error details
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
**Last Updated**: 2025-01-08
|
|
170
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# Error Utilities
|
|
2
|
+
|
|
3
|
+
Utilities for managing and standardizing Gemini API errors. Provides error categorization, retry detection, and user-friendly messaging.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
mapGeminiError,
|
|
10
|
+
isGeminiErrorRetryable,
|
|
11
|
+
categorizeGeminiError,
|
|
12
|
+
createGeminiError
|
|
13
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## 🎯 Purpose
|
|
17
|
+
|
|
18
|
+
Use these utilities to handle Gemini API errors consistently. Provides error categorization, retry logic, and user-friendly messaging.
|
|
19
|
+
|
|
20
|
+
**When to use:**
|
|
21
|
+
- Categorize API errors by type
|
|
22
|
+
- Determine if errors are retryable
|
|
23
|
+
- Create user-friendly error messages
|
|
24
|
+
- Implement retry logic
|
|
25
|
+
- Log errors for monitoring
|
|
26
|
+
|
|
27
|
+
## 📌 Strategy
|
|
28
|
+
|
|
29
|
+
Consistent error handling improves UX and debugging. These utilities:
|
|
30
|
+
- Categorize errors into standard types
|
|
31
|
+
- Identify retryable vs non-retryable errors
|
|
32
|
+
- Provide structured error information
|
|
33
|
+
- Enable intelligent retry logic
|
|
34
|
+
- Support error analytics and monitoring
|
|
35
|
+
|
|
36
|
+
**Key Decision**: Use error categorization to determine retry logic. Only retry transient errors (network, rate limit, server errors), not permanent errors (auth, validation, safety).
|
|
37
|
+
|
|
38
|
+
## ⚠️ Rules
|
|
39
|
+
|
|
40
|
+
### Usage Rules
|
|
41
|
+
- **MUST** categorize errors before handling
|
|
42
|
+
- **SHOULD** check retryable status before retrying
|
|
43
|
+
- **MUST** provide user-friendly messages
|
|
44
|
+
- **SHOULD** log errors for debugging
|
|
45
|
+
- **MUST NOT** expose API keys in errors
|
|
46
|
+
|
|
47
|
+
### Error Handling Rules
|
|
48
|
+
- **MUST** use `isGeminiErrorRetryable()` before retrying
|
|
49
|
+
- **SHOULD** use `categorizeGeminiError()` for error handling
|
|
50
|
+
- **MUST** handle permanent errors appropriately (no retry)
|
|
51
|
+
- **SHOULD** implement user-friendly error messages
|
|
52
|
+
- **MUST** log errors with full context
|
|
53
|
+
|
|
54
|
+
### Retry Rules
|
|
55
|
+
- **WILL RETRY**: QUOTA_EXCEEDED, RATE_LIMIT, NETWORK, TIMEOUT, SERVER (5xx)
|
|
56
|
+
- **WILL NOT RETRY**: AUTHENTICATION, SAFETY, VALIDATION, MODEL_NOT_FOUND
|
|
57
|
+
|
|
58
|
+
## 🤖 AI Agent Guidelines
|
|
59
|
+
|
|
60
|
+
### When Modifying These Utilities
|
|
61
|
+
1. **READ** the implementation file first
|
|
62
|
+
2. **UNDERSTAND** error patterns
|
|
63
|
+
3. **MAINTAIN** backward compatibility
|
|
64
|
+
4. **ADD** tests for new error patterns
|
|
65
|
+
5. **UPDATE** type definitions
|
|
66
|
+
|
|
67
|
+
### When Adding New Features
|
|
68
|
+
1. **CHECK** if similar error handling exists
|
|
69
|
+
2. **FOLLOW** existing patterns
|
|
70
|
+
3. **ADD** new error patterns to mapper
|
|
71
|
+
4. **DOCUMENT** in type definitions
|
|
72
|
+
5. **ADD** examples to tests (not docs)
|
|
73
|
+
|
|
74
|
+
### When Fixing Bugs
|
|
75
|
+
1. **REPRODUCE** bug locally first
|
|
76
|
+
2. **IDENTIFY** root cause
|
|
77
|
+
3. **FIX** with minimal changes
|
|
78
|
+
4. **ADD** regression test
|
|
79
|
+
5. **VERIFY** all tests pass
|
|
80
|
+
|
|
81
|
+
### Code Style Rules
|
|
82
|
+
- **VALIDATE** error inputs
|
|
83
|
+
- **THROW** typed errors (`GeminiError`)
|
|
84
|
+
- **LOG** error categorization
|
|
85
|
+
- **COMMENT** complex pattern matching
|
|
86
|
+
- **USE** consistent error types
|
|
87
|
+
|
|
88
|
+
## 📦 Available Functions
|
|
89
|
+
|
|
90
|
+
### `mapGeminiError(error)`
|
|
91
|
+
|
|
92
|
+
Convert Gemini API errors to standard format.
|
|
93
|
+
|
|
94
|
+
**Refer to**: [`error-mapper.util.ts`](./error-mapper.util.ts)
|
|
95
|
+
|
|
96
|
+
### `isGeminiErrorRetryable(error)`
|
|
97
|
+
|
|
98
|
+
Check if error is retryable.
|
|
99
|
+
|
|
100
|
+
**Refer to**: [`error-mapper.util.ts`](./error-mapper.util.ts)
|
|
101
|
+
|
|
102
|
+
### `categorizeGeminiError(error)`
|
|
103
|
+
|
|
104
|
+
Determine error category.
|
|
105
|
+
|
|
106
|
+
**Refer to**: [`error-mapper.util.ts`](./error-mapper.util.ts)
|
|
107
|
+
|
|
108
|
+
### `createGeminiError(error)`
|
|
109
|
+
|
|
110
|
+
Create `GeminiError` instance.
|
|
111
|
+
|
|
112
|
+
**Refer to**: [`error-mapper.util.ts`](./error-mapper.util.ts)
|
|
113
|
+
|
|
114
|
+
## 🔗 Related Modules
|
|
115
|
+
|
|
116
|
+
- **Error Types**: [`domain/entities/error.types.ts`](../domain/entities/error.types.ts)
|
|
117
|
+
- **Retry Service**: [`RETRY_SERVICE.md`](../infrastructure/services/RETRY_SERVICE.md)
|
|
118
|
+
- **Error Mapper**: [`ERROR_MAPPER.md`](./ERROR_MAPPER.md)
|
|
119
|
+
|
|
120
|
+
## 📋 Configuration Reference
|
|
121
|
+
|
|
122
|
+
### GeminiErrorType Categories
|
|
123
|
+
|
|
124
|
+
| Error Type | Description | Retry | Example Message |
|
|
125
|
+
|------------|-------------|-------|-----------------|
|
|
126
|
+
| `QUOTA_EXCEEDED` | Quota exceeded | ✅ | "Quota exceeded" |
|
|
127
|
+
| `RATE_LIMIT` | Rate limit | ✅ | "Too many requests" |
|
|
128
|
+
| `AUTHENTICATION` | Authentication failed | ❌ | "Invalid API key" |
|
|
129
|
+
| `SAFETY` | Safety blocked | ❌ | "Content blocked" |
|
|
130
|
+
| `MODEL_NOT_FOUND` | Model not found | ❌ | "Model not found" |
|
|
131
|
+
| `NETWORK` | Network error | ✅ | "Network error" |
|
|
132
|
+
| `TIMEOUT` | Request timeout | ✅ | "Request timeout" |
|
|
133
|
+
| `SERVER` | Server error (5xx) | ✅ | "Internal server error" |
|
|
134
|
+
| `VALIDATION` | Validation error | ❌ | "Invalid request" |
|
|
135
|
+
| `UNKNOWN` | Unknown error | ❌ | "Unknown error" |
|
|
136
|
+
|
|
137
|
+
### Error Patterns
|
|
138
|
+
|
|
139
|
+
Error mapper searches for these patterns in error messages:
|
|
140
|
+
- **Quota**: "quota", "resource exhausted", "429"
|
|
141
|
+
- **Rate Limit**: "rate limit", "too many requests"
|
|
142
|
+
- **Auth**: "unauthorized", "invalid api key", "401", "403"
|
|
143
|
+
- **Safety**: "safety", "blocked", "harmful"
|
|
144
|
+
- **Not Found**: "model not found", "404"
|
|
145
|
+
- **Network**: "network", "fetch failed", "connection"
|
|
146
|
+
- **Timeout**: "timeout", "timed out"
|
|
147
|
+
- **Server**: "500", "502", "503", "504", "internal server"
|
|
148
|
+
- **Validation**: "invalid", "bad request", "400"
|
|
149
|
+
|
|
150
|
+
## 🎓 Usage Patterns
|
|
151
|
+
|
|
152
|
+
### Retry Logic
|
|
153
|
+
1. Catch error from API call
|
|
154
|
+
2. Check `isGeminiErrorRetryable()`
|
|
155
|
+
3. If retryable and max retries not reached, retry with backoff
|
|
156
|
+
4. If non-retryable or max retries reached, handle error
|
|
157
|
+
5. Provide user feedback
|
|
158
|
+
|
|
159
|
+
### Error Categorization
|
|
160
|
+
1. Catch error from operation
|
|
161
|
+
2. Call `categorizeGeminiError()` to get error type
|
|
162
|
+
3. Switch on error type for handling
|
|
163
|
+
4. Show appropriate user message per type
|
|
164
|
+
5. Log error with full context
|
|
165
|
+
|
|
166
|
+
### User-Friendly Messages
|
|
167
|
+
1. Categorize error using `categorizeGeminiError()`
|
|
168
|
+
2. Map error type to user-friendly message
|
|
169
|
+
3. Display message to user
|
|
170
|
+
4. Provide actionable next steps when possible
|
|
171
|
+
5. Log technical details separately
|
|
172
|
+
|
|
173
|
+
### Error Analytics
|
|
174
|
+
1. Categorize each error
|
|
175
|
+
2. Track error counts by type
|
|
176
|
+
3. Calculate error rates
|
|
177
|
+
4. Send to analytics service
|
|
178
|
+
5. Monitor error trends
|
|
179
|
+
|
|
180
|
+
### Conditional Retry
|
|
181
|
+
1. Attempt operation
|
|
182
|
+
2. On error, check if retryable
|
|
183
|
+
3. If retryable, calculate backoff delay
|
|
184
|
+
4. Wait for backoff period
|
|
185
|
+
5. Retry operation or fail after max attempts
|
|
186
|
+
|
|
187
|
+
## 🚨 Common Pitfalls
|
|
188
|
+
|
|
189
|
+
### Don't
|
|
190
|
+
- Retry non-retryable errors (auth, validation, safety)
|
|
191
|
+
- Show technical error messages to users
|
|
192
|
+
- Ignore error categorization
|
|
193
|
+
- Forget to log errors
|
|
194
|
+
- Expose API keys in error messages
|
|
195
|
+
|
|
196
|
+
### Do
|
|
197
|
+
- Always check error type before handling
|
|
198
|
+
- Use retryable status for retry logic
|
|
199
|
+
- Provide user-friendly messages
|
|
200
|
+
- Log errors with full context
|
|
201
|
+
- Track errors for analytics
|
|
202
|
+
- Implement exponential backoff for retries
|
|
203
|
+
- Handle permanent errors appropriately
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
**Last Updated**: 2025-01-08
|
|
208
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# Image Preparer Utility
|
|
2
|
+
|
|
3
|
+
Helper functions for preparing images for Gemini API. Handles URI to base64 conversion and MIME type detection.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
prepareImageFromUri,
|
|
10
|
+
prepareImage,
|
|
11
|
+
isValidBase64
|
|
12
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 🎯 Purpose
|
|
16
|
+
|
|
17
|
+
Use image preparer to convert image URIs to base64 format. Handles various URI types and MIME type detection.
|
|
18
|
+
|
|
19
|
+
**When to use:**
|
|
20
|
+
- Convert image URIs to base64
|
|
21
|
+
- Detect MIME types from URIs
|
|
22
|
+
- Prepare images for API upload
|
|
23
|
+
- Validate base64 data
|
|
24
|
+
- Handle React Native image selection
|
|
25
|
+
|
|
26
|
+
## 📌 Strategy
|
|
27
|
+
|
|
28
|
+
Image preparation ensures compatibility. This utility:
|
|
29
|
+
- Converts various URI formats to base64
|
|
30
|
+
- Detects MIME types automatically
|
|
31
|
+
- Validates base64 data
|
|
32
|
+
- Handles different image sources
|
|
33
|
+
- Simplifies image processing
|
|
34
|
+
|
|
35
|
+
**Key Decision**: Always use preparer for image data. Handles URI conversion and MIME detection automatically.
|
|
36
|
+
|
|
37
|
+
## ⚠️ Rules
|
|
38
|
+
|
|
39
|
+
### Conversion Rules
|
|
40
|
+
- **MUST** validate URI before conversion
|
|
41
|
+
- **SHOULD** handle various URI formats
|
|
42
|
+
- **MUST** detect MIME type correctly
|
|
43
|
+
- **SHOULD** validate output base64
|
|
44
|
+
- **MUST NOT** lose data integrity
|
|
45
|
+
|
|
46
|
+
### Validation Rules
|
|
47
|
+
- **SHOULD** validate base64 format
|
|
48
|
+
- **MUST** check for empty data
|
|
49
|
+
- **SHOULD** validate MIME types
|
|
50
|
+
- **MUST** handle validation errors
|
|
51
|
+
- **SHOULD NOT** accept invalid data
|
|
52
|
+
|
|
53
|
+
### URI Handling Rules
|
|
54
|
+
- **MUST** support various URI types
|
|
55
|
+
- **SHOULD** detect format automatically
|
|
56
|
+
- **MUST** handle network URIs
|
|
57
|
+
- **SHOULD** cache conversions when possible
|
|
58
|
+
- **MUST NOT** fail silently
|
|
59
|
+
|
|
60
|
+
### Performance Rules
|
|
61
|
+
- **SHOULD** cache prepared images
|
|
62
|
+
- **MUST** handle large images efficiently
|
|
63
|
+
- **SHOULD** optimize when needed
|
|
64
|
+
- **MUST NOT** block main thread
|
|
65
|
+
- **SHOULD** handle concurrent requests
|
|
66
|
+
|
|
67
|
+
## 🤖 AI Agent Guidelines
|
|
68
|
+
|
|
69
|
+
### When Preparing Images
|
|
70
|
+
1. **CALL** prepareImageFromUri()
|
|
71
|
+
2. **PROVIDE** image URI
|
|
72
|
+
3. **WAIT** for conversion
|
|
73
|
+
4. **VALIDATE** output
|
|
74
|
+
5. **HANDLE** errors gracefully
|
|
75
|
+
|
|
76
|
+
### When Validating Base64
|
|
77
|
+
1. **CALL** isValidBase64()
|
|
78
|
+
2. **PROVIDE** base64 string
|
|
79
|
+
3. **CHECK** return value
|
|
80
|
+
4. **HANDLE** invalid data
|
|
81
|
+
5. **PROVIDE** clear feedback
|
|
82
|
+
|
|
83
|
+
### When Processing Multiple Images
|
|
84
|
+
1. **PREPARE** images in parallel
|
|
85
|
+
2. **USE** Promise.all()
|
|
86
|
+
3. **VALIDATE** all results
|
|
87
|
+
4. **CACHE** prepared images
|
|
88
|
+
5. **HANDLE** partial failures
|
|
89
|
+
|
|
90
|
+
### Code Style Rules
|
|
91
|
+
- **VALIDATE** input URIs
|
|
92
|
+
- **HANDLE** conversion errors
|
|
93
|
+
- **CACHE** when appropriate
|
|
94
|
+
- **VALIDATE** output data
|
|
95
|
+
- **LOG** preparation issues
|
|
96
|
+
|
|
97
|
+
## 📦 Available Functions
|
|
98
|
+
|
|
99
|
+
**Refer to**: [`image-preparer.util.ts`](./image-preparer.util.ts)
|
|
100
|
+
|
|
101
|
+
### Image Preparation
|
|
102
|
+
- `prepareImageFromUri(uri)` - Convert URI to base64
|
|
103
|
+
- `prepareImage(uri)` - Alias for prepareImageFromUri
|
|
104
|
+
|
|
105
|
+
### Validation
|
|
106
|
+
- `isValidBase64(str)` - Validate base64 format
|
|
107
|
+
|
|
108
|
+
## 🔗 Related Modules
|
|
109
|
+
|
|
110
|
+
- **Data Transformer**: [`./DATA_TRANSFORMER_UTILS.md`](./DATA_TRANSFORMER_UTILS.md)
|
|
111
|
+
- **Input Builders**: [`./INPUT_BUILDERS.md`](./INPUT_BUILDERS.md)
|
|
112
|
+
- **Image Edit Service**: [`../services/IMAGE_EDIT_SERVICE.md`](../services/IMAGE_EDIT_SERVICE.md)
|
|
113
|
+
|
|
114
|
+
## 📋 Supported Formats
|
|
115
|
+
|
|
116
|
+
### URI Types
|
|
117
|
+
- Data URL: `data:image/png;base64,...`
|
|
118
|
+
- HTTP/HTTPS URL: `https://example.com/image.jpg`
|
|
119
|
+
- File URI: `file:///path/to/image.png`
|
|
120
|
+
- React Native Asset: Asset paths
|
|
121
|
+
|
|
122
|
+
### MIME Types
|
|
123
|
+
- JPEG: `.jpg`, `.jpeg` → `image/jpeg`
|
|
124
|
+
- PNG: `.png` → `image/png`
|
|
125
|
+
- GIF: `.gif` → `image/gif`
|
|
126
|
+
- WebP: `.webp` → `image/webp`
|
|
127
|
+
|
|
128
|
+
### Return Type
|
|
129
|
+
```typescript
|
|
130
|
+
{
|
|
131
|
+
base64: string; // Pure base64 (no data URL)
|
|
132
|
+
mimeType: string; // Detected MIME type
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 🎓 Usage Patterns
|
|
137
|
+
|
|
138
|
+
### Basic Preparation
|
|
139
|
+
1. Provide image URI
|
|
140
|
+
2. Call prepareImageFromUri()
|
|
141
|
+
3. Get base64 and MIME type
|
|
142
|
+
4. Use in API request
|
|
143
|
+
5. Handle errors
|
|
144
|
+
|
|
145
|
+
### Image Picker Integration
|
|
146
|
+
1. Get URI from image picker
|
|
147
|
+
2. Call prepareImageFromUri()
|
|
148
|
+
3. Validate prepared image
|
|
149
|
+
4. Use in service call
|
|
150
|
+
5. Display to user
|
|
151
|
+
|
|
152
|
+
### Multiple Images
|
|
153
|
+
1. Collect all image URIs
|
|
154
|
+
2. Prepare in parallel
|
|
155
|
+
3. Validate all results
|
|
156
|
+
4. Use in batch operations
|
|
157
|
+
5. Handle partial failures
|
|
158
|
+
|
|
159
|
+
### Base64 Validation
|
|
160
|
+
1. Get base64 string
|
|
161
|
+
2. Call isValidBase64()
|
|
162
|
+
3. Check validation result
|
|
163
|
+
4. Use or reject data
|
|
164
|
+
5. Provide feedback
|
|
165
|
+
|
|
166
|
+
## 🚨 Common Pitfalls
|
|
167
|
+
|
|
168
|
+
### Don't
|
|
169
|
+
- Skip validation of prepared images
|
|
170
|
+
- Assume URI format
|
|
171
|
+
- Forget to handle errors
|
|
172
|
+
- Use unvalidated base64
|
|
173
|
+
- Block main thread with large images
|
|
174
|
+
|
|
175
|
+
### Do
|
|
176
|
+
- Always validate output
|
|
177
|
+
- Handle various URI formats
|
|
178
|
+
- Cache prepared images
|
|
179
|
+
- Handle errors gracefully
|
|
180
|
+
- Optimize for performance
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
**Last Updated**: 2025-01-08
|
|
185
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|