@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,233 @@
|
|
|
1
|
+
# Services Layer
|
|
2
|
+
|
|
3
|
+
Communication layer with Gemini API. Contains all business logic for text, image, and video generation.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
geminiClientCoreService,
|
|
10
|
+
geminiTextGenerationService,
|
|
11
|
+
geminiImageGenerationService,
|
|
12
|
+
geminiImageEditService,
|
|
13
|
+
geminiVideoGenerationService,
|
|
14
|
+
geminiStreamingService,
|
|
15
|
+
geminiRetryService,
|
|
16
|
+
providerInitializer,
|
|
17
|
+
featureModelSelector
|
|
18
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 🎯 Purpose
|
|
22
|
+
|
|
23
|
+
Use services to execute AI operations. Each service handles specific AI capabilities with proper error handling and retry logic.
|
|
24
|
+
|
|
25
|
+
**When to use:**
|
|
26
|
+
- Generate text content
|
|
27
|
+
- Create images from text
|
|
28
|
+
- Edit and transform images
|
|
29
|
+
- Generate videos
|
|
30
|
+
- Stream real-time responses
|
|
31
|
+
- Handle AI errors and retries
|
|
32
|
+
|
|
33
|
+
## 📌 Strategy
|
|
34
|
+
|
|
35
|
+
Services encapsulate API communication logic. This layer:
|
|
36
|
+
- Provides high-level interfaces for AI operations
|
|
37
|
+
- Handles error detection and retry logic
|
|
38
|
+
- Manages API communication complexity
|
|
39
|
+
- Implements feature-based model selection
|
|
40
|
+
- Supports both simple and advanced use cases
|
|
41
|
+
|
|
42
|
+
**Key Decision**: Use services for all AI operations. They handle complexity, errors, and optimization automatically.
|
|
43
|
+
|
|
44
|
+
## ⚠️ Rules
|
|
45
|
+
|
|
46
|
+
### Usage Rules
|
|
47
|
+
- **MUST** initialize provider before using services
|
|
48
|
+
- **SHOULD** use appropriate service for operation type
|
|
49
|
+
- **MUST** handle service errors appropriately
|
|
50
|
+
- **SHOULD** check model validation
|
|
51
|
+
- **MUST NOT** bypass error handling
|
|
52
|
+
|
|
53
|
+
### Error Handling Rules
|
|
54
|
+
- **MUST** catch and handle `GeminiError`
|
|
55
|
+
- **SHOULD** implement retry logic for transient errors
|
|
56
|
+
- **MUST** provide user-friendly error messages
|
|
57
|
+
- **SHOULD** log errors for debugging
|
|
58
|
+
- **MUST NOT** expose API keys in errors
|
|
59
|
+
|
|
60
|
+
### Configuration Rules
|
|
61
|
+
- **MUST** use valid model IDs
|
|
62
|
+
- **SHOULD** configure appropriate generation parameters
|
|
63
|
+
- **MUST** respect API rate limits
|
|
64
|
+
- **SHOULD** implement timeouts for long operations
|
|
65
|
+
- **MUST NOT** use deprecated models
|
|
66
|
+
|
|
67
|
+
### Model Selection Rules
|
|
68
|
+
- **SHOULD** use feature-based model selector
|
|
69
|
+
- **MUST** validate model before use
|
|
70
|
+
- **SHOULD** consider user tier for model selection
|
|
71
|
+
- **MUST NOT** hardcode model IDs in application code
|
|
72
|
+
|
|
73
|
+
## 🤖 AI Agent Guidelines
|
|
74
|
+
|
|
75
|
+
### When Modifying Services
|
|
76
|
+
1. **READ** the implementation file first
|
|
77
|
+
2. **UNDERSTAND** service dependencies
|
|
78
|
+
3. **MAINTAIN** backward compatibility
|
|
79
|
+
4. **ADD** tests for new functionality
|
|
80
|
+
5. **UPDATE** all documentation
|
|
81
|
+
|
|
82
|
+
### When Adding New Services
|
|
83
|
+
1. **CHECK** if similar service exists
|
|
84
|
+
2. **FOLLOW** existing service patterns
|
|
85
|
+
3. **USE** established error handling
|
|
86
|
+
4. **DOCUMENT** in service documentation
|
|
87
|
+
5. **ADD** examples to tests (not docs)
|
|
88
|
+
|
|
89
|
+
### When Fixing Service Bugs
|
|
90
|
+
1. **REPRODUCE** bug locally first
|
|
91
|
+
2. **IDENTIFY** root cause
|
|
92
|
+
3. **FIX** with minimal changes
|
|
93
|
+
4. **ADD** regression test
|
|
94
|
+
5. **VERIFY** all tests pass
|
|
95
|
+
|
|
96
|
+
### Code Style Rules
|
|
97
|
+
- **USE** async/await (no callbacks)
|
|
98
|
+
- **VALIDATE** inputs at service entry
|
|
99
|
+
- **THROW** typed errors (`GeminiError`)
|
|
100
|
+
- **LOG** important operations
|
|
101
|
+
- **COMMENT** complex logic only
|
|
102
|
+
|
|
103
|
+
## 📦 Available Services
|
|
104
|
+
|
|
105
|
+
### Core Services
|
|
106
|
+
|
|
107
|
+
**Text Generation**: Generate AI text content
|
|
108
|
+
|
|
109
|
+
**Refer to**: [`TEXT_GENERATION_SERVICE.md`](./TEXT_GENERATION_SERVICE.md)
|
|
110
|
+
|
|
111
|
+
**Image Generation**: Create images from text prompts
|
|
112
|
+
|
|
113
|
+
**Refer to**: [`IMAGE_GENERATION_SERVICE.md`](./IMAGE_GENERATION_SERVICE.md)
|
|
114
|
+
|
|
115
|
+
**Image Edit**: Edit and transform images
|
|
116
|
+
|
|
117
|
+
**Refer to**: [`IMAGE_EDIT_SERVICE.md`](./IMAGE_EDIT_SERVICE.md)
|
|
118
|
+
|
|
119
|
+
**Video Generation**: Generate videos from text/images
|
|
120
|
+
|
|
121
|
+
**Refer to**: [`VIDEO_GENERATION_SERVICE.md`](./VIDEO_GENERATION_SERVICE.md)
|
|
122
|
+
|
|
123
|
+
**Streaming**: Real-time text streaming
|
|
124
|
+
|
|
125
|
+
**Refer to**: [`STREAMING_SERVICE.md`](./STREAMING_SERVICE.md)
|
|
126
|
+
|
|
127
|
+
**Retry**: Automatic retry with backoff
|
|
128
|
+
|
|
129
|
+
**Refer to**: [`RETRY_SERVICE.md`](./RETRY_SERVICE.md)
|
|
130
|
+
|
|
131
|
+
### Support Services
|
|
132
|
+
|
|
133
|
+
**Core Client**: Low-level SDK communication
|
|
134
|
+
|
|
135
|
+
**Refer to**: [`CORE_CLIENT_SERVICE.md`](./CORE_CLIENT_SERVICE.md)
|
|
136
|
+
|
|
137
|
+
**Feature Model Selector**: Feature-based model selection
|
|
138
|
+
|
|
139
|
+
**Refer to**: [`FEATURE_MODEL_SELECTOR_SERVICE.md`](./FEATURE_MODEL_SELECTOR_SERVICE.md)
|
|
140
|
+
|
|
141
|
+
**Provider Initializer**: Provider initialization
|
|
142
|
+
|
|
143
|
+
**Refer to**: [`PROVIDER_INITIALIZER_SERVICE.md`](./PROVIDER_INITIALIZER_SERVICE.md)
|
|
144
|
+
|
|
145
|
+
## 🔗 Related Modules
|
|
146
|
+
|
|
147
|
+
- **Domain Types**: [`../../domain/README.md`](../../domain/README.md)
|
|
148
|
+
- **Infrastructure**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
149
|
+
- **Presentation**: [`../presentation/README.md`](../presentation/README.md)
|
|
150
|
+
|
|
151
|
+
## 📋 Service Categories
|
|
152
|
+
|
|
153
|
+
### Text Services
|
|
154
|
+
- Text generation (simple and structured)
|
|
155
|
+
- Multimodal content (text + images)
|
|
156
|
+
- Streaming responses
|
|
157
|
+
- Conversational AI
|
|
158
|
+
|
|
159
|
+
### Image Services
|
|
160
|
+
- Text-to-image generation
|
|
161
|
+
- Image editing and transformation
|
|
162
|
+
- Background removal
|
|
163
|
+
- Image enhancement
|
|
164
|
+
- Style transfer
|
|
165
|
+
|
|
166
|
+
### Video Services
|
|
167
|
+
- Text-to-video generation
|
|
168
|
+
- Image-to-video generation
|
|
169
|
+
- Progress tracking
|
|
170
|
+
- Polling mechanism
|
|
171
|
+
|
|
172
|
+
### Infrastructure Services
|
|
173
|
+
- SDK client management
|
|
174
|
+
- Error handling and retry
|
|
175
|
+
- Model selection and validation
|
|
176
|
+
- Provider initialization
|
|
177
|
+
|
|
178
|
+
## 🎓 Usage Patterns
|
|
179
|
+
|
|
180
|
+
### Basic Text Generation
|
|
181
|
+
1. Import `geminiTextGenerationService`
|
|
182
|
+
2. Call `generateText()` with model and prompt
|
|
183
|
+
3. Handle response or error
|
|
184
|
+
4. Display result to user
|
|
185
|
+
|
|
186
|
+
### Image Generation
|
|
187
|
+
1. Import `geminiImageGenerationService`
|
|
188
|
+
2. Call `generateImage()` with descriptive prompt
|
|
189
|
+
3. Handle image data (URL or base64)
|
|
190
|
+
4. Display image in UI
|
|
191
|
+
|
|
192
|
+
### Video Generation
|
|
193
|
+
1. Import `geminiVideoGenerationService`
|
|
194
|
+
2. Call `generateTextToVideo()` with prompt
|
|
195
|
+
3. Provide progress callback
|
|
196
|
+
4. Monitor progress
|
|
197
|
+
5. Handle completion
|
|
198
|
+
|
|
199
|
+
### Error Handling
|
|
200
|
+
1. Wrap service call in try-catch
|
|
201
|
+
2. Check if error is `GeminiError`
|
|
202
|
+
3. Switch on error type
|
|
203
|
+
4. Handle each error type appropriately
|
|
204
|
+
5. Provide user feedback
|
|
205
|
+
|
|
206
|
+
### Streaming
|
|
207
|
+
1. Import `geminiStreamingService`
|
|
208
|
+
2. Call `streamText()` with model and prompt
|
|
209
|
+
3. Use `for await` to consume stream
|
|
210
|
+
4. Display chunks progressively
|
|
211
|
+
5. Handle completion
|
|
212
|
+
|
|
213
|
+
## 🚨 Common Pitfalls
|
|
214
|
+
|
|
215
|
+
### Don't
|
|
216
|
+
- Use services without initializing provider
|
|
217
|
+
- Ignore error handling
|
|
218
|
+
- Hardcode model IDs
|
|
219
|
+
- Exceed API rate limits
|
|
220
|
+
- Skip progress callbacks for long operations
|
|
221
|
+
|
|
222
|
+
### Do
|
|
223
|
+
- Initialize provider before using services
|
|
224
|
+
- Handle all error types
|
|
225
|
+
- Use feature model selector
|
|
226
|
+
- Implement retry logic
|
|
227
|
+
- Provide user feedback
|
|
228
|
+
- Monitor long operations
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
**Last Updated**: 2025-01-08
|
|
233
|
+
**See Also**: [AI_GUIDELINES.md](../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# Retry Service
|
|
2
|
+
|
|
3
|
+
Automatic retry mechanism with exponential backoff strategy. Handles transient errors gracefully.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { geminiRetryService } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use this service to automatically retry failed operations with intelligent backoff. Handles network issues, rate limits, and transient server errors.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Wrap API calls that may fail transiently
|
|
17
|
+
- Handle rate limiting automatically
|
|
18
|
+
- Improve reliability of network operations
|
|
19
|
+
- Add resilience to critical operations
|
|
20
|
+
- Track retry attempts for monitoring
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
Not all errors should be retried. This service:
|
|
25
|
+
- Uses exponential backoff for delays
|
|
26
|
+
- Retries only transient errors (network, rate limit, server errors)
|
|
27
|
+
- Fails fast for permanent errors (auth, validation, safety)
|
|
28
|
+
- Provides retry callbacks for monitoring
|
|
29
|
+
- Configurable retry limits and delays
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Only retry errors that might succeed on retry (429 rate limits, 5xx server errors, network issues). Never retry validation or authentication errors.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Usage Rules
|
|
36
|
+
- **MUST** wrap only retryable operations
|
|
37
|
+
- **SHOULD** configure appropriate max retries
|
|
38
|
+
- **MUST** handle final failure after all retries
|
|
39
|
+
- **SHOULD** use retry callbacks for monitoring
|
|
40
|
+
- **MUST NOT** retry non-idempotent operations without care
|
|
41
|
+
|
|
42
|
+
### Configuration Rules
|
|
43
|
+
- **MUST** set maxRetries appropriately (default: 3)
|
|
44
|
+
- **SHOULD** configure baseDelay for use case
|
|
45
|
+
- **MUST** set maxDelay to prevent excessive waits
|
|
46
|
+
- **SHOULD** use onRetry callback for logging
|
|
47
|
+
|
|
48
|
+
### Retryable Errors
|
|
49
|
+
- **WILL RETRY**: QUOTA_EXCEEDED, RATE_LIMIT, NETWORK, TIMEOUT, SERVER (5xx)
|
|
50
|
+
- **WILL NOT RETRY**: AUTHENTICATION, SAFETY, VALIDATION, MODEL_NOT_FOUND
|
|
51
|
+
|
|
52
|
+
### Error Handling Rules
|
|
53
|
+
- **MUST** catch and handle final errors
|
|
54
|
+
- **SHOULD** log retry attempts
|
|
55
|
+
- **MUST** differentiate retryable vs non-retryable errors
|
|
56
|
+
- **SHOULD** provide user feedback during retries
|
|
57
|
+
|
|
58
|
+
## 🤖 AI Agent Guidelines
|
|
59
|
+
|
|
60
|
+
### When Modifying This Service
|
|
61
|
+
1. **READ** the implementation file first
|
|
62
|
+
2. **UNDERSTAND** exponential backoff logic
|
|
63
|
+
3. **MAINTAIN** backward compatibility
|
|
64
|
+
4. **ADD** tests for new functionality
|
|
65
|
+
5. **UPDATE** type definitions
|
|
66
|
+
|
|
67
|
+
### When Adding New Features
|
|
68
|
+
1. **CHECK** if similar feature exists
|
|
69
|
+
2. **FOLLOW** existing patterns
|
|
70
|
+
3. **USE** established error handling
|
|
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
|
+
- **USE** async/await (no callbacks)
|
|
83
|
+
- **VALIDATE** inputs at function entry
|
|
84
|
+
- **THROW** typed errors (`GeminiError`)
|
|
85
|
+
- **LOG** retry attempts
|
|
86
|
+
- **COMMENT** complex logic only
|
|
87
|
+
|
|
88
|
+
## 📦 Available Methods
|
|
89
|
+
|
|
90
|
+
### `executeWithRetry(fn, options?)`
|
|
91
|
+
|
|
92
|
+
Execute function with automatic retry on transient errors.
|
|
93
|
+
|
|
94
|
+
**Refer to**: [`gemini-retry.service.ts`](./gemini-retry.service.ts)
|
|
95
|
+
|
|
96
|
+
## 🔗 Related Modules
|
|
97
|
+
|
|
98
|
+
- **Error Utilities**: [`ERROR_UTILITIES.md`](../infrastructure/utils/ERROR_UTILITIES.md)
|
|
99
|
+
- **Error Mapper**: [`ERROR_MAPPER.md`](../infrastructure/utils/ERROR_MAPPER.md)
|
|
100
|
+
- **Text Generation**: [`TEXT_GENERATION_SERVICE.md`](./TEXT_GENERATION_SERVICE.md)
|
|
101
|
+
|
|
102
|
+
## 📋 Configuration Reference
|
|
103
|
+
|
|
104
|
+
### RetryOptions
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
interface RetryOptions {
|
|
108
|
+
maxRetries?: number; // Maximum retry attempts (default: 3)
|
|
109
|
+
baseDelay?: number; // Initial delay in ms (default: 1000)
|
|
110
|
+
maxDelay?: number; // Maximum delay in ms (default: 10000)
|
|
111
|
+
onRetry?: (attempt: number, error: unknown) => void;
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Retryable Error Types
|
|
116
|
+
- `QUOTA_EXCEEDED`: Temporary quota exceeded
|
|
117
|
+
- `RATE_LIMIT`: Too many requests
|
|
118
|
+
- `NETWORK`: Network connectivity issues
|
|
119
|
+
- `TIMEOUT`: Request timeout
|
|
120
|
+
- `SERVER`: Server errors (5xx)
|
|
121
|
+
|
|
122
|
+
### Non-Retryable Error Types
|
|
123
|
+
- `AUTHENTICATION`: Invalid API key or credentials
|
|
124
|
+
- `SAFETY`: Content safety violation
|
|
125
|
+
- `VALIDATION`: Invalid request parameters
|
|
126
|
+
- `MODEL_NOT_FOUND`: Invalid model ID
|
|
127
|
+
|
|
128
|
+
## 🎓 Usage Patterns
|
|
129
|
+
|
|
130
|
+
### Basic Retry
|
|
131
|
+
1. Import service
|
|
132
|
+
2. Wrap async function in `executeWithRetry()`
|
|
133
|
+
3. Configure max retries and delays
|
|
134
|
+
4. Handle final success or failure
|
|
135
|
+
5. Provide user feedback
|
|
136
|
+
|
|
137
|
+
### With Callbacks
|
|
138
|
+
1. Import service
|
|
139
|
+
2. Configure `onRetry` callback
|
|
140
|
+
3. Log retry attempts for monitoring
|
|
141
|
+
4. Update UI with retry status
|
|
142
|
+
5. Track metrics for analytics
|
|
143
|
+
|
|
144
|
+
### Custom Configuration
|
|
145
|
+
1. Assess operation criticality
|
|
146
|
+
2. Set appropriate maxRetries (1-5)
|
|
147
|
+
3. Configure baseDelay for operation type
|
|
148
|
+
4. Set maxDelay to prevent excessive waits
|
|
149
|
+
5. Test retry behavior
|
|
150
|
+
|
|
151
|
+
### For Critical Operations
|
|
152
|
+
1. Use higher maxRetries (5+)
|
|
153
|
+
2. Use longer baseDelay (2000ms)
|
|
154
|
+
3. Set longer maxDelay (60000ms)
|
|
155
|
+
4. Implement detailed callbacks
|
|
156
|
+
5. Add timeout wrapper
|
|
157
|
+
|
|
158
|
+
## 🚨 Common Pitfalls
|
|
159
|
+
|
|
160
|
+
### Don't
|
|
161
|
+
- Retry authentication failures (won't succeed)
|
|
162
|
+
- Retry validation errors (need input fix)
|
|
163
|
+
- Retry without maxDelay (can wait forever)
|
|
164
|
+
- Retry non-idempotent operations carelessly
|
|
165
|
+
- Ignore retry callbacks (no visibility)
|
|
166
|
+
|
|
167
|
+
### Do
|
|
168
|
+
- Configure retries based on operation type
|
|
169
|
+
- Use callbacks for monitoring and analytics
|
|
170
|
+
- Set maxDelay to prevent excessive waits
|
|
171
|
+
- Handle non-retryable errors appropriately
|
|
172
|
+
- Differentiate transient vs permanent errors
|
|
173
|
+
- Consider timeouts for total operation time
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
**Last Updated**: 2025-01-08
|
|
178
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Streaming Service
|
|
2
|
+
|
|
3
|
+
Provides real-time streaming for AI text generation. Delivers responses chunk-by-chunk for immediate display.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { geminiStreamingService } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use this service to generate AI text responses with streaming. Provides immediate feedback as content is generated, improving user experience for long responses.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Long text generation (stories, articles)
|
|
17
|
+
- Chat interfaces requiring real-time responses
|
|
18
|
+
- Applications needing immediate user feedback
|
|
19
|
+
- Progressive content display
|
|
20
|
+
- Cancelable operations
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
Streaming dramatically improves UX for long responses. This service:
|
|
25
|
+
- Returns AsyncGenerator for chunk-by-chunk delivery
|
|
26
|
+
- Enables progressive UI updates
|
|
27
|
+
- Supports cancellation mid-generation
|
|
28
|
+
- Handles stream errors gracefully
|
|
29
|
+
- Provides finish reason for completion status
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Use streaming for better UX when generating long responses. Each chunk can be displayed immediately, reducing perceived latency.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Usage Rules
|
|
36
|
+
- **MUST** initialize provider with API key before use
|
|
37
|
+
- **MUST** iterate through async generator properly
|
|
38
|
+
- **SHOULD** update UI with each chunk
|
|
39
|
+
- **MUST** handle stream errors appropriately
|
|
40
|
+
- **SHOULD** implement cancellation support
|
|
41
|
+
|
|
42
|
+
### Stream Handling Rules
|
|
43
|
+
- **MUST** use `for await` to consume stream
|
|
44
|
+
- **SHOULD** buffer very small chunks
|
|
45
|
+
- **MUST** check `finishReason` in final chunk
|
|
46
|
+
- **SHOULD** implement memory limits for buffers
|
|
47
|
+
- **MUST NOT** ignore stream errors
|
|
48
|
+
|
|
49
|
+
### Configuration Rules
|
|
50
|
+
- **MUST** use valid model IDs
|
|
51
|
+
- **SHOULD** configure appropriate generation parameters
|
|
52
|
+
- **MUST** handle safety filters in stream
|
|
53
|
+
- **SHOULD** implement retry logic for failures
|
|
54
|
+
|
|
55
|
+
### Error Handling Rules
|
|
56
|
+
- **MUST** catch and handle `GeminiError`
|
|
57
|
+
- **MUST** handle stream interruption errors
|
|
58
|
+
- **SHOULD** check finish reasons (SAFETY, MAX_TOKENS)
|
|
59
|
+
- **MUST NOT** expose API keys in errors
|
|
60
|
+
|
|
61
|
+
## 🤖 AI Agent Guidelines
|
|
62
|
+
|
|
63
|
+
### When Modifying This Service
|
|
64
|
+
1. **READ** the implementation file first
|
|
65
|
+
2. **UNDERSTAND** async generator pattern
|
|
66
|
+
3. **MAINTAIN** backward compatibility
|
|
67
|
+
4. **ADD** tests for new functionality
|
|
68
|
+
5. **UPDATE** type definitions
|
|
69
|
+
|
|
70
|
+
### When Adding New Features
|
|
71
|
+
1. **CHECK** if similar feature exists in streaming/text services
|
|
72
|
+
2. **FOLLOW** existing patterns
|
|
73
|
+
3. **USE** established error handling
|
|
74
|
+
4. **DOCUMENT** in type definitions
|
|
75
|
+
5. **ADD** examples to tests (not docs)
|
|
76
|
+
|
|
77
|
+
### When Fixing Bugs
|
|
78
|
+
1. **REPRODUCE** bug locally first
|
|
79
|
+
2. **IDENTIFY** root cause
|
|
80
|
+
3. **FIX** with minimal changes
|
|
81
|
+
4. **ADD** regression test
|
|
82
|
+
5. **VERIFY** all tests pass
|
|
83
|
+
|
|
84
|
+
### Code Style Rules
|
|
85
|
+
- **USE** async/await and `for await`
|
|
86
|
+
- **VALIDATE** inputs at function entry
|
|
87
|
+
- **THROW** typed errors (`GeminiError`)
|
|
88
|
+
- **LOG** important operations
|
|
89
|
+
- **COMMENT** complex logic only
|
|
90
|
+
|
|
91
|
+
## 📦 Available Methods
|
|
92
|
+
|
|
93
|
+
### `streamText(model, prompt, config?)`
|
|
94
|
+
|
|
95
|
+
Stream text generation chunk-by-chunk.
|
|
96
|
+
|
|
97
|
+
**Refer to**: [`gemini-streaming.service.ts`](./gemini-streaming.service.ts)
|
|
98
|
+
|
|
99
|
+
## 🔗 Related Modules
|
|
100
|
+
|
|
101
|
+
- **Domain Types**: [`domain/entities/README.md`](../domain/entities/README.md)
|
|
102
|
+
- **Text Generation**: [`TEXT_GENERATION_SERVICE.md`](./TEXT_GENERATION_SERVICE.md)
|
|
103
|
+
- **Core Client**: [`CORE_CLIENT_SERVICE.md`](./CORE_CLIENT_SERVICE.md)
|
|
104
|
+
|
|
105
|
+
## 📋 Configuration Reference
|
|
106
|
+
|
|
107
|
+
### Generation Config
|
|
108
|
+
See: [`domain/entities/README.md`](../domain/entities/README.md)
|
|
109
|
+
|
|
110
|
+
### Model Selection
|
|
111
|
+
See: [`FEATURE_MODEL_SELECTOR_SERVICE.md`](./FEATURE_MODEL_SELECTOR_SERVICE.md)
|
|
112
|
+
|
|
113
|
+
### Error Types
|
|
114
|
+
See: [`ERROR_UTILITIES.md`](../infrastructure/utils/ERROR_UTILITIES.md)
|
|
115
|
+
|
|
116
|
+
## 🎓 Usage Patterns
|
|
117
|
+
|
|
118
|
+
### Basic Streaming
|
|
119
|
+
1. Import service
|
|
120
|
+
2. Call `streamText()` with model and prompt
|
|
121
|
+
3. Use `for await` to iterate through chunks
|
|
122
|
+
4. Append each chunk to displayed text
|
|
123
|
+
5. Handle completion or errors
|
|
124
|
+
|
|
125
|
+
### With UI Updates
|
|
126
|
+
1. Create state for streamed text
|
|
127
|
+
2. Start streaming with `streamText()`
|
|
128
|
+
3. Update state with each chunk
|
|
129
|
+
4. Display progress to user
|
|
130
|
+
5. Handle final chunk and finish reason
|
|
131
|
+
|
|
132
|
+
### With Cancellation
|
|
133
|
+
1. Start streaming operation
|
|
134
|
+
2. Track cancellation flag
|
|
135
|
+
3. Check flag in stream loop
|
|
136
|
+
4. Break loop if cancelled
|
|
137
|
+
5. Handle cleanup appropriately
|
|
138
|
+
|
|
139
|
+
### With Error Handling
|
|
140
|
+
1. Wrap stream in try-catch
|
|
141
|
+
2. Check `finishReason` in chunks
|
|
142
|
+
3. Handle SAFETY, MAX_TOKENS reasons
|
|
143
|
+
4. Catch network and timeout errors
|
|
144
|
+
5. Provide user feedback
|
|
145
|
+
|
|
146
|
+
## 🚨 Common Pitfalls
|
|
147
|
+
|
|
148
|
+
### Don't
|
|
149
|
+
- Buffer all chunks before displaying (defeats streaming purpose)
|
|
150
|
+
- Ignore finish reasons in final chunk
|
|
151
|
+
- Forget error handling in stream loop
|
|
152
|
+
- Create memory leaks with unbounded buffers
|
|
153
|
+
- Assume streaming is always faster
|
|
154
|
+
|
|
155
|
+
### Do
|
|
156
|
+
- Update UI with each chunk
|
|
157
|
+
- Check finishReason for completion status
|
|
158
|
+
- Handle stream interruption gracefully
|
|
159
|
+
- Implement buffer size limits
|
|
160
|
+
- Use cancellation for long operations
|
|
161
|
+
- Provide user feedback during streaming
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
**Last Updated**: 2025-01-08
|
|
166
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|