@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,238 @@
|
|
|
1
|
+
# Domain Entities
|
|
2
|
+
|
|
3
|
+
Core data structures and type definitions for the Gemini provider. Contains all types required for API communication.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import type {
|
|
9
|
+
GeminiConfig,
|
|
10
|
+
GeminiContent,
|
|
11
|
+
GeminiResponse,
|
|
12
|
+
GeminiImageGenerationResult,
|
|
13
|
+
VideoGenerationResult,
|
|
14
|
+
GeminiError,
|
|
15
|
+
GeminiErrorType
|
|
16
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 🎯 Purpose
|
|
20
|
+
|
|
21
|
+
Use these types for type-safe integration with Gemini services. Provides TypeScript interfaces for all API operations.
|
|
22
|
+
|
|
23
|
+
**When to use:**
|
|
24
|
+
- Type-check API requests and responses
|
|
25
|
+
- Define function signatures
|
|
26
|
+
- Validate data structures
|
|
27
|
+
- Understand API response formats
|
|
28
|
+
- Handle typed errors
|
|
29
|
+
|
|
30
|
+
## 📌 Strategy
|
|
31
|
+
|
|
32
|
+
Strong typing prevents runtime errors. These types:
|
|
33
|
+
- Define all API request/response structures
|
|
34
|
+
- Provide type safety for operations
|
|
35
|
+
- Enable autocomplete in IDEs
|
|
36
|
+
- Document API contracts
|
|
37
|
+
- Catch errors at compile time
|
|
38
|
+
|
|
39
|
+
**Key Decision**: All types defined in domain layer have NO external dependencies. This keeps them pure and reusable.
|
|
40
|
+
|
|
41
|
+
## ⚠️ Rules
|
|
42
|
+
|
|
43
|
+
### Usage Rules
|
|
44
|
+
- **MUST** use types from domain layer
|
|
45
|
+
- **SHOULD** prefer type imports over value imports
|
|
46
|
+
- **MUST** handle all error types
|
|
47
|
+
- **SHOULD** use type guards for validation
|
|
48
|
+
- **MUST NOT** use `any` type
|
|
49
|
+
|
|
50
|
+
### Type Safety Rules
|
|
51
|
+
- **MUST** define explicit return types
|
|
52
|
+
- **SHOULD** use strict null checks
|
|
53
|
+
- **MUST** validate runtime data
|
|
54
|
+
- **SHOULD** use discriminated unions for errors
|
|
55
|
+
- **MUST NOT** bypass type checking
|
|
56
|
+
|
|
57
|
+
### Error Handling Rules
|
|
58
|
+
- **MUST** catch and handle `GeminiError`
|
|
59
|
+
- **SHOULD** check error types
|
|
60
|
+
- **MUST** handle all error states
|
|
61
|
+
- **SHOULD** provide type-safe error messages
|
|
62
|
+
- **MUST NOT** ignore error types
|
|
63
|
+
|
|
64
|
+
## 🤖 AI Agent Guidelines
|
|
65
|
+
|
|
66
|
+
### When Modifying Types
|
|
67
|
+
1. **READ** existing type definitions first
|
|
68
|
+
2. **UNDERSTAND** type relationships
|
|
69
|
+
3. **MAINTAIN** backward compatibility
|
|
70
|
+
4. **UPDATE** all usages
|
|
71
|
+
5. **ADD** tests for new types
|
|
72
|
+
|
|
73
|
+
### When Adding New Types
|
|
74
|
+
1. **CHECK** if similar type exists
|
|
75
|
+
2. **FOLLOW** existing naming conventions
|
|
76
|
+
3. **USE** consistent patterns
|
|
77
|
+
4. **DOCUMENT** with JSDoc comments
|
|
78
|
+
5. **EXPORT** from index files
|
|
79
|
+
|
|
80
|
+
### When Fixing Type Bugs
|
|
81
|
+
1. **IDENTIFY** root cause in type definition
|
|
82
|
+
2. **FIX** with minimal changes
|
|
83
|
+
3. **UPDATE** all dependent code
|
|
84
|
+
4. **ADD** type tests
|
|
85
|
+
5. **VERIFY** no breaking changes
|
|
86
|
+
|
|
87
|
+
### Code Style Rules
|
|
88
|
+
- **USE** explicit type annotations
|
|
89
|
+
- **AVOID** `any` type
|
|
90
|
+
- **PREFER** readonly where appropriate
|
|
91
|
+
- **USE** discriminated unions
|
|
92
|
+
- **DOCUMENT** complex types
|
|
93
|
+
|
|
94
|
+
## 📦 Available Types
|
|
95
|
+
|
|
96
|
+
### Configuration Types
|
|
97
|
+
|
|
98
|
+
**GeminiConfig**: Client configuration
|
|
99
|
+
|
|
100
|
+
**Refer to**: [`gemini.types.ts`](./gemini.types.ts)
|
|
101
|
+
|
|
102
|
+
### Request/Response Types
|
|
103
|
+
|
|
104
|
+
**GeminiContent**: API request content structure
|
|
105
|
+
|
|
106
|
+
**GeminiResponse**: API response structure
|
|
107
|
+
|
|
108
|
+
**GeminiCandidate**: Generated candidate results
|
|
109
|
+
|
|
110
|
+
**Refer to**: [`gemini.types.ts`](./gemini.types.ts)
|
|
111
|
+
|
|
112
|
+
### Image Types
|
|
113
|
+
|
|
114
|
+
**GeminiImageGenerationResult**: Image generation result
|
|
115
|
+
|
|
116
|
+
**GeminiImageInput**: Image input structure
|
|
117
|
+
|
|
118
|
+
**Refer to**: [`gemini.types.ts`](./gemini.types.ts)
|
|
119
|
+
|
|
120
|
+
### Video Types
|
|
121
|
+
|
|
122
|
+
**VideoGenerationInput**: Video generation input
|
|
123
|
+
|
|
124
|
+
**VideoGenerationResult**: Video generation result
|
|
125
|
+
|
|
126
|
+
**VideoOperationStatus**: Operation status enum
|
|
127
|
+
|
|
128
|
+
**Refer to**: [`video.types.ts`](./video.types.ts)
|
|
129
|
+
|
|
130
|
+
### Error Types
|
|
131
|
+
|
|
132
|
+
**GeminiError**: Custom error class
|
|
133
|
+
|
|
134
|
+
**GeminiErrorType**: Error type enum
|
|
135
|
+
|
|
136
|
+
**GeminiErrorInfo**: Error information interface
|
|
137
|
+
|
|
138
|
+
**Refer to**: [`error.types.ts`](./error.types.ts)
|
|
139
|
+
|
|
140
|
+
### Model Types
|
|
141
|
+
|
|
142
|
+
**GeminiModel**: Model information
|
|
143
|
+
|
|
144
|
+
**GeminiGenerationConfig**: Generation parameters
|
|
145
|
+
|
|
146
|
+
**Refer to**: [`models.ts`](./models.ts)
|
|
147
|
+
|
|
148
|
+
## 🔗 Related Modules
|
|
149
|
+
|
|
150
|
+
- **Services**: [`../../infrastructure/services/README.md`](../../infrastructure/services/README.md)
|
|
151
|
+
- **Error Utilities**: [`../../infrastructure/utils/ERROR_UTILITIES.md`](../../infrastructure/utils/ERROR_UTILITIES.md)
|
|
152
|
+
- **Constants**: [`../constants/README.md`](../constants/README.md)
|
|
153
|
+
|
|
154
|
+
## 📋 Type Reference
|
|
155
|
+
|
|
156
|
+
### Finish Reason Types
|
|
157
|
+
|
|
158
|
+
Values indicating why generation completed:
|
|
159
|
+
- `STOP` - Normal completion
|
|
160
|
+
- `MAX_TOKENS` - Token limit reached
|
|
161
|
+
- `SAFETY` - Blocked by safety filter
|
|
162
|
+
- `RECITATION` - Copyright detected
|
|
163
|
+
- `OTHER` - Other reasons
|
|
164
|
+
|
|
165
|
+
### Video Operation Status
|
|
166
|
+
|
|
167
|
+
Operation lifecycle states:
|
|
168
|
+
- `queued` - Waiting in queue
|
|
169
|
+
- `processing` - Currently processing
|
|
170
|
+
- `completed` - Successfully finished
|
|
171
|
+
- `failed` - Operation failed
|
|
172
|
+
|
|
173
|
+
### Error Categories
|
|
174
|
+
|
|
175
|
+
Error types for handling:
|
|
176
|
+
- `API_ERROR` - General API errors
|
|
177
|
+
- `VALIDATION_ERROR` - Input validation errors
|
|
178
|
+
- `NETWORK_ERROR` - Network connectivity issues
|
|
179
|
+
- `TIMEOUT_ERROR` - Request timeout
|
|
180
|
+
- `RATE_LIMIT_ERROR` - Too many requests
|
|
181
|
+
- `PARSING_ERROR` - Response parsing failures
|
|
182
|
+
- `QUOTA_EXCEEDED` - API quota exceeded
|
|
183
|
+
- `AUTHENTICATION` - Invalid credentials
|
|
184
|
+
- `SAFETY` - Content safety violations
|
|
185
|
+
- `MODEL_NOT_FOUND` - Invalid model ID
|
|
186
|
+
- `SERVER` - Server errors (5xx)
|
|
187
|
+
|
|
188
|
+
## 🎓 Usage Patterns
|
|
189
|
+
|
|
190
|
+
### Type Imports
|
|
191
|
+
1. Import types from package
|
|
192
|
+
2. Use for function parameters
|
|
193
|
+
3. Define return types
|
|
194
|
+
4. Handle typed errors
|
|
195
|
+
5. Validate runtime data
|
|
196
|
+
|
|
197
|
+
### Error Type Checking
|
|
198
|
+
1. Catch error from operation
|
|
199
|
+
2. Check if `instanceof GeminiError`
|
|
200
|
+
3. Switch on error.type
|
|
201
|
+
4. Handle each error type appropriately
|
|
202
|
+
5. Provide user feedback
|
|
203
|
+
|
|
204
|
+
### Image Type Usage
|
|
205
|
+
1. Use `GeminiImageInput` for image data
|
|
206
|
+
2. Provide base64-encoded images
|
|
207
|
+
3. Specify MIME type
|
|
208
|
+
4. Handle image generation results
|
|
209
|
+
5. Extract image URL or base64
|
|
210
|
+
|
|
211
|
+
### Video Type Usage
|
|
212
|
+
1. Use `VideoGenerationInput` for requests
|
|
213
|
+
2. Monitor `VideoOperationStatus`
|
|
214
|
+
3. Handle progress updates
|
|
215
|
+
4. Extract video URL when complete
|
|
216
|
+
5. Handle errors appropriately
|
|
217
|
+
|
|
218
|
+
## 🚨 Common Pitfalls
|
|
219
|
+
|
|
220
|
+
### Don't
|
|
221
|
+
- Use `any` type to bypass typing
|
|
222
|
+
- Ignore error types
|
|
223
|
+
- Skip type validation
|
|
224
|
+
- Use hardcoded model IDs
|
|
225
|
+
- Forget to handle all error states
|
|
226
|
+
|
|
227
|
+
### Do
|
|
228
|
+
- Use explicit type annotations
|
|
229
|
+
- Check error types
|
|
230
|
+
- Validate runtime data
|
|
231
|
+
- Use type guards
|
|
232
|
+
- Handle all finish reasons
|
|
233
|
+
- Provide type-safe error messages
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
**Last Updated**: 2025-01-08
|
|
238
|
+
**See Also**: [AI_GUIDELINES.md](../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# Infrastructure Layer
|
|
2
|
+
|
|
3
|
+
Performance optimization, monitoring, and extensibility features. Includes cache, telemetry, and interceptor mechanisms.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
SimpleCache,
|
|
10
|
+
modelSelectionCache,
|
|
11
|
+
telemetryHooks,
|
|
12
|
+
requestInterceptors,
|
|
13
|
+
responseInterceptors
|
|
14
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 🎯 Purpose
|
|
18
|
+
|
|
19
|
+
Use infrastructure features to optimize performance, monitor operations, and extend functionality. Provides caching, telemetry, and middleware capabilities.
|
|
20
|
+
|
|
21
|
+
**When to use:**
|
|
22
|
+
- Cache expensive operations
|
|
23
|
+
- Monitor AI operations
|
|
24
|
+
- Intercept requests/responses
|
|
25
|
+
- Optimize performance
|
|
26
|
+
- Track metrics
|
|
27
|
+
- Extend functionality
|
|
28
|
+
|
|
29
|
+
## 📌 Strategy
|
|
30
|
+
|
|
31
|
+
Infrastructure features are cross-cutting concerns. These systems:
|
|
32
|
+
- Improve performance through caching
|
|
33
|
+
- Enable observability through telemetry
|
|
34
|
+
- Provide extensibility through interceptors
|
|
35
|
+
- Support monitoring and debugging
|
|
36
|
+
- Optimize resource usage
|
|
37
|
+
|
|
38
|
+
**Key Decision**: Use infrastructure features to add capabilities without modifying core services. Keep infrastructure separate from business logic.
|
|
39
|
+
|
|
40
|
+
## ⚠️ Rules
|
|
41
|
+
|
|
42
|
+
### Usage Rules
|
|
43
|
+
- **MUST** initialize features before use
|
|
44
|
+
- **SHOULD** clean up resources appropriately
|
|
45
|
+
- **MUST** handle cache misses gracefully
|
|
46
|
+
- **SHOULD** monitor telemetry for insights
|
|
47
|
+
- **MUST NOT** create circular dependencies
|
|
48
|
+
|
|
49
|
+
### Cache Rules
|
|
50
|
+
- **MUST** set appropriate TTL values
|
|
51
|
+
- **SHOULD** monitor cache hit rates
|
|
52
|
+
- **MUST** handle cache size limits
|
|
53
|
+
- **SHOULD** invalidate stale data
|
|
54
|
+
- **MUST NOT** cache everything indiscriminately
|
|
55
|
+
|
|
56
|
+
### Telemetry Rules
|
|
57
|
+
- **SHOULD** subscribe to events early
|
|
58
|
+
- **MUST** unsubscribe when done
|
|
59
|
+
- **SHOULD NOT** emit sensitive data
|
|
60
|
+
- **MUST** handle listener errors
|
|
61
|
+
- **SHOULD** aggregate metrics
|
|
62
|
+
|
|
63
|
+
### Interceptor Rules
|
|
64
|
+
- **MUST** return modified context
|
|
65
|
+
- **SHOULD** handle errors in interceptors
|
|
66
|
+
- **MUST** unsubscribe when done
|
|
67
|
+
- **SHOULD NOT** block execution excessively
|
|
68
|
+
- **MUST** maintain order dependency
|
|
69
|
+
|
|
70
|
+
## 🤖 AI Agent Guidelines
|
|
71
|
+
|
|
72
|
+
### When Modifying Infrastructure
|
|
73
|
+
1. **READ** existing implementations first
|
|
74
|
+
2. **UNDERSTAND** feature dependencies
|
|
75
|
+
3. **MAINTAIN** backward compatibility
|
|
76
|
+
4. **ADD** tests for new functionality
|
|
77
|
+
5. **UPDATE** documentation
|
|
78
|
+
|
|
79
|
+
### When Adding New Features
|
|
80
|
+
1. **CHECK** if similar feature exists
|
|
81
|
+
2. **FOLLOW** existing patterns
|
|
82
|
+
3. **USE** established error handling
|
|
83
|
+
4. **DOCUMENT** in code comments
|
|
84
|
+
5. **ADD** examples to tests (not docs)
|
|
85
|
+
|
|
86
|
+
### When Fixing Bugs
|
|
87
|
+
1. **REPRODUCE** bug locally first
|
|
88
|
+
2. **IDENTIFY** root cause
|
|
89
|
+
3. **FIX** with minimal changes
|
|
90
|
+
4. **ADD** regression test
|
|
91
|
+
5. **VERIFY** all tests pass
|
|
92
|
+
|
|
93
|
+
### Code Style Rules
|
|
94
|
+
- **USE** dependency injection
|
|
95
|
+
- **VALIDATE** inputs
|
|
96
|
+
- **HANDLE** errors gracefully
|
|
97
|
+
- **LOG** important operations
|
|
98
|
+
- **COMMENT** complex logic only
|
|
99
|
+
|
|
100
|
+
## 📦 Available Features
|
|
101
|
+
|
|
102
|
+
### Cache System
|
|
103
|
+
|
|
104
|
+
LRU cache implementation for performance optimization.
|
|
105
|
+
|
|
106
|
+
**Refer to**: [`cache/README.md`](./cache/README.md)
|
|
107
|
+
|
|
108
|
+
**Files:**
|
|
109
|
+
- [`cache/SimpleCache.ts`](./cache/SimpleCache.ts) - Cache implementation
|
|
110
|
+
- [`cache/index.ts`](./cache/index.ts) - Cache exports
|
|
111
|
+
|
|
112
|
+
### Telemetry System
|
|
113
|
+
|
|
114
|
+
Event-based monitoring for AI operations.
|
|
115
|
+
|
|
116
|
+
**Refer to**: [`telemetry/README.md`](./telemetry/README.md)
|
|
117
|
+
|
|
118
|
+
**Files:**
|
|
119
|
+
- [`telemetry/TelemetryHooks.ts`](./telemetry/TelemetryHooks.ts) - Telemetry implementation
|
|
120
|
+
- [`telemetry/index.ts`](./telemetry/index.ts) - Telemetry exports
|
|
121
|
+
|
|
122
|
+
### Interceptors
|
|
123
|
+
|
|
124
|
+
Middleware for request/response transformation.
|
|
125
|
+
|
|
126
|
+
**Refer to**: [`interceptors/README.md`](./interceptors/README.md)
|
|
127
|
+
|
|
128
|
+
**Files:**
|
|
129
|
+
- [`interceptors/RequestInterceptors.ts`](./interceptors/RequestInterceptors.ts) - Request interceptors
|
|
130
|
+
- [`interceptors/ResponseInterceptors.ts`](./interceptors/ResponseInterceptors.ts) - Response interceptors
|
|
131
|
+
- [`interceptors/index.ts`](./interceptors/index.ts) - Interceptor exports
|
|
132
|
+
|
|
133
|
+
## 🔗 Related Modules
|
|
134
|
+
|
|
135
|
+
- **Services**: [`services/README.md`](./services/README.md)
|
|
136
|
+
- **Utils**: [`utils/README.md`](./utils/README.md)
|
|
137
|
+
- **Domain Types**: [`../domain/README.md`](../domain/README.md)
|
|
138
|
+
|
|
139
|
+
## 📋 Feature Reference
|
|
140
|
+
|
|
141
|
+
### SimpleCache
|
|
142
|
+
|
|
143
|
+
LRU cache with TTL support.
|
|
144
|
+
|
|
145
|
+
**Methods:**
|
|
146
|
+
- `set(key, value, ttl?)` - Store value
|
|
147
|
+
- `get(key)` - Retrieve value
|
|
148
|
+
- `has(key)` - Check existence
|
|
149
|
+
- `delete(key)` - Remove entry
|
|
150
|
+
- `clear()` - Clear all entries
|
|
151
|
+
- `getStats()` - Get cache statistics
|
|
152
|
+
|
|
153
|
+
**Configuration:**
|
|
154
|
+
- `maxSize`: Maximum entries (default: 100)
|
|
155
|
+
- `ttl`: Time-to-live in milliseconds (default: 5 minutes)
|
|
156
|
+
|
|
157
|
+
### Telemetry Hooks
|
|
158
|
+
|
|
159
|
+
Event system for monitoring operations.
|
|
160
|
+
|
|
161
|
+
**Events:**
|
|
162
|
+
- `request` - Operation started
|
|
163
|
+
- `response` - Operation completed
|
|
164
|
+
- `error` - Operation failed
|
|
165
|
+
- `retry` - Retry attempted
|
|
166
|
+
|
|
167
|
+
**Methods:**
|
|
168
|
+
- `subscribe(listener)` - Add event listener
|
|
169
|
+
- `emit(event)` - Emit event
|
|
170
|
+
- `logRequest(model, feature)` - Log request start
|
|
171
|
+
- `logResponse(model, startTime, feature)` - Log response
|
|
172
|
+
- `logError(model, error, feature)` - Log error
|
|
173
|
+
- `clear()` - Clear all listeners
|
|
174
|
+
|
|
175
|
+
### Request Interceptors
|
|
176
|
+
|
|
177
|
+
Middleware for request transformation.
|
|
178
|
+
|
|
179
|
+
**Methods:**
|
|
180
|
+
- `use(interceptor)` - Add interceptor
|
|
181
|
+
- Returns unsubscribe function
|
|
182
|
+
|
|
183
|
+
**Execution Order:** First added runs first
|
|
184
|
+
|
|
185
|
+
### Response Interceptors
|
|
186
|
+
|
|
187
|
+
Middleware for response transformation.
|
|
188
|
+
|
|
189
|
+
**Methods:**
|
|
190
|
+
- `use(interceptor)` - Add interceptor
|
|
191
|
+
- Returns unsubscribe function
|
|
192
|
+
|
|
193
|
+
**Execution Order:** Last added runs first (reverse order)
|
|
194
|
+
|
|
195
|
+
## 🎓 Usage Patterns
|
|
196
|
+
|
|
197
|
+
### Caching Strategy
|
|
198
|
+
1. Import `SimpleCache` or use global cache
|
|
199
|
+
2. Configure cache size and TTL
|
|
200
|
+
3. Check cache before operation
|
|
201
|
+
4. Store result in cache after operation
|
|
202
|
+
5. Handle cache hits and misses
|
|
203
|
+
|
|
204
|
+
### Monitoring Setup
|
|
205
|
+
1. Subscribe to telemetry events early
|
|
206
|
+
2. Track metrics in event handler
|
|
207
|
+
3. Send to analytics/monitoring service
|
|
208
|
+
4. Aggregate and analyze metrics
|
|
209
|
+
5. Unsubscribe when done
|
|
210
|
+
|
|
211
|
+
### Request Interception
|
|
212
|
+
1. Add interceptor before operations
|
|
213
|
+
2. Modify request context
|
|
214
|
+
3. Return modified context
|
|
215
|
+
4. Clean up interceptor when done
|
|
216
|
+
5. Handle errors appropriately
|
|
217
|
+
|
|
218
|
+
### Response Interception
|
|
219
|
+
1. Add interceptor before operations
|
|
220
|
+
2. Process response data
|
|
221
|
+
3. Return modified context
|
|
222
|
+
4. Clean up interceptor when done
|
|
223
|
+
5. Handle errors appropriately
|
|
224
|
+
|
|
225
|
+
### Performance Optimization
|
|
226
|
+
1. Cache expensive operations
|
|
227
|
+
2. Monitor cache hit rates
|
|
228
|
+
3. Adjust TTL based on data freshness
|
|
229
|
+
4. Track operation durations
|
|
230
|
+
5. Optimize based on metrics
|
|
231
|
+
|
|
232
|
+
## 🚨 Common Pitfalls
|
|
233
|
+
|
|
234
|
+
### Don't
|
|
235
|
+
- Cache everything indiscriminately
|
|
236
|
+
- Forget to unsubscribe from events
|
|
237
|
+
- Block execution in interceptors
|
|
238
|
+
- Emit sensitive data in telemetry
|
|
239
|
+
- Create circular dependencies
|
|
240
|
+
|
|
241
|
+
### Do
|
|
242
|
+
- Set appropriate cache TTL values
|
|
243
|
+
- Monitor cache hit rates
|
|
244
|
+
- Clean up resources
|
|
245
|
+
- Keep interceptors lightweight
|
|
246
|
+
- Aggregate telemetry metrics
|
|
247
|
+
- Handle errors gracefully
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
**Last Updated**: 2025-01-08
|
|
252
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# Cache System
|
|
2
|
+
|
|
3
|
+
LRU (Least Recently Used) cache implementation for performance optimization. Supports TTL-based expiration and automatic cleanup.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
SimpleCache,
|
|
10
|
+
modelSelectionCache
|
|
11
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 🎯 Purpose
|
|
15
|
+
|
|
16
|
+
Use cache to store expensive operation results and avoid redundant API calls. Improves performance and reduces costs.
|
|
17
|
+
|
|
18
|
+
**When to use:**
|
|
19
|
+
- Cache API responses
|
|
20
|
+
- Store model selection results
|
|
21
|
+
- Optimize expensive operations
|
|
22
|
+
- Reduce API calls
|
|
23
|
+
- Improve response times
|
|
24
|
+
|
|
25
|
+
## 📌 Strategy
|
|
26
|
+
|
|
27
|
+
Caching significantly improves performance. This system:
|
|
28
|
+
- Implements LRU eviction policy
|
|
29
|
+
- Supports TTL (time-to-live) for entries
|
|
30
|
+
- Provides type-safe caching
|
|
31
|
+
- Automatically expires stale entries
|
|
32
|
+
- Offers global and instance caching
|
|
33
|
+
|
|
34
|
+
**Key Decision**: Use `modelSelectionCache` for model selection. Create custom `SimpleCache` instances for specific use cases.
|
|
35
|
+
|
|
36
|
+
## ⚠️ Rules
|
|
37
|
+
|
|
38
|
+
### Usage Rules
|
|
39
|
+
- **MUST** configure appropriate cache size
|
|
40
|
+
- **SHOULD** set reasonable TTL values
|
|
41
|
+
- **MUST** handle cache misses gracefully
|
|
42
|
+
- **SHOULD** monitor cache hit rates
|
|
43
|
+
- **MUST NOT** cache everything indiscriminately
|
|
44
|
+
|
|
45
|
+
### Configuration Rules
|
|
46
|
+
- **MUST** set `maxSize` based on memory constraints
|
|
47
|
+
- **SHOULD** set `ttl` based on data freshness needs
|
|
48
|
+
- **MUST** consider cache entry size
|
|
49
|
+
- **SHOULD** invalidate stale data
|
|
50
|
+
- **MUST NOT** use excessive cache sizes
|
|
51
|
+
|
|
52
|
+
### Cache Key Rules
|
|
53
|
+
- **MUST** use descriptive cache keys
|
|
54
|
+
- **SHOULD** include relevant parameters in key
|
|
55
|
+
- **MUST NOT** use duplicate keys for different data
|
|
56
|
+
- **SHOULD** namespace keys appropriately
|
|
57
|
+
- **MUST** handle key collisions
|
|
58
|
+
|
|
59
|
+
### Error Handling Rules
|
|
60
|
+
- **MUST** handle cache failures gracefully
|
|
61
|
+
- **SHOULD** log cache errors in development
|
|
62
|
+
- **MUST** provide fallback on cache miss
|
|
63
|
+
- **SHOULD NOT** throw errors from cache operations
|
|
64
|
+
- **MUST** return data on cache miss
|
|
65
|
+
|
|
66
|
+
## 🤖 AI Agent Guidelines
|
|
67
|
+
|
|
68
|
+
### When Modifying Cache Module
|
|
69
|
+
1. **READ** existing cache implementation first
|
|
70
|
+
2. **UNDERSTAND** LRU eviction logic
|
|
71
|
+
3. **MAINTAIN** backward compatibility
|
|
72
|
+
4. **ADD** tests for new functionality
|
|
73
|
+
5. **UPDATE** documentation
|
|
74
|
+
|
|
75
|
+
### When Adding New Caches
|
|
76
|
+
1. **CHECK** if similar cache exists
|
|
77
|
+
2. **FOLLOW** existing cache patterns
|
|
78
|
+
3. **USE** appropriate configuration
|
|
79
|
+
4. **DOCUMENT** cache purpose
|
|
80
|
+
5. **ADD** usage examples to tests
|
|
81
|
+
|
|
82
|
+
### When Optimizing Cache
|
|
83
|
+
1. **MEASURE** cache hit rates
|
|
84
|
+
2. **ADJUST** TTL values based on usage
|
|
85
|
+
3. **MONITOR** memory usage
|
|
86
|
+
4. **OPTIMIZE** cache size
|
|
87
|
+
5. **TEST** performance improvements
|
|
88
|
+
|
|
89
|
+
### Code Style Rules
|
|
90
|
+
- **USE** generic type parameters
|
|
91
|
+
- **VALIDATE** cache inputs
|
|
92
|
+
- **HANDLE** edge cases (null, undefined)
|
|
93
|
+
- **LOG** cache operations in development
|
|
94
|
+
- **COMMENT** complex logic only
|
|
95
|
+
|
|
96
|
+
## 📦 Available Caches
|
|
97
|
+
|
|
98
|
+
### SimpleCache
|
|
99
|
+
|
|
100
|
+
**Refer to**: [`SimpleCache.ts`](./SimpleCache.ts)
|
|
101
|
+
|
|
102
|
+
Generic LRU cache implementation.
|
|
103
|
+
|
|
104
|
+
**Methods:**
|
|
105
|
+
- `set(key, value, ttl?)` - Store value
|
|
106
|
+
- `get(key)` - Retrieve value
|
|
107
|
+
- `has(key)` - Check existence
|
|
108
|
+
- `delete(key)` - Remove entry
|
|
109
|
+
- `clear()` - Clear all entries
|
|
110
|
+
- `size()` - Get cache size
|
|
111
|
+
- `keys()` - Get all keys
|
|
112
|
+
- `getStats()` - Get statistics
|
|
113
|
+
|
|
114
|
+
### modelSelectionCache
|
|
115
|
+
|
|
116
|
+
**Refer to**: [`index.ts`](./index.ts)
|
|
117
|
+
|
|
118
|
+
Global cache for model selection results.
|
|
119
|
+
|
|
120
|
+
**Configuration:**
|
|
121
|
+
- `maxSize`: 50 entries
|
|
122
|
+
- `ttl`: 10 minutes
|
|
123
|
+
- Automatic model-feature mapping
|
|
124
|
+
|
|
125
|
+
## 🔗 Related Modules
|
|
126
|
+
|
|
127
|
+
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
128
|
+
- **Services**: [`../services/README.md`](../services/README.md)
|
|
129
|
+
- **Performance Utils**: [`../utils/PERFORMANCE_UTILS.md`](../utils/PERFORMANCE_UTILS.md)
|
|
130
|
+
|
|
131
|
+
## 📋 Configuration Reference
|
|
132
|
+
|
|
133
|
+
### Cache Options
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
interface CacheOptions {
|
|
137
|
+
maxSize?: number; // Maximum entries (default: 100)
|
|
138
|
+
ttl?: number; // Time-to-live in milliseconds (default: 5 minutes)
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Typical TTL Values
|
|
143
|
+
|
|
144
|
+
- **Hot data**: 30 seconds - 1 minute
|
|
145
|
+
- **Warm data**: 5 - 10 minutes
|
|
146
|
+
- **Cold data**: 30 - 60 minutes
|
|
147
|
+
- **Static data**: 1+ hours
|
|
148
|
+
|
|
149
|
+
### Cache Size Guidelines
|
|
150
|
+
|
|
151
|
+
- **Small cache**: 20-50 entries
|
|
152
|
+
- **Medium cache**: 100-200 entries
|
|
153
|
+
- **Large cache**: 500-1000 entries
|
|
154
|
+
- **Consider memory constraints**
|
|
155
|
+
|
|
156
|
+
## 🎓 Usage Patterns
|
|
157
|
+
|
|
158
|
+
### Basic Caching
|
|
159
|
+
1. Create `SimpleCache` instance
|
|
160
|
+
2. Configure size and TTL
|
|
161
|
+
3. Check cache before operation
|
|
162
|
+
4. Store result in cache
|
|
163
|
+
5. Handle cache hits and misses
|
|
164
|
+
|
|
165
|
+
### Model Selection Caching
|
|
166
|
+
1. Import `modelSelectionCache`
|
|
167
|
+
2. Check cache for feature-model mapping
|
|
168
|
+
3. Store selection in cache
|
|
169
|
+
4. Use cached model ID
|
|
170
|
+
5. Handle cache misses
|
|
171
|
+
|
|
172
|
+
### Cache Invalidation
|
|
173
|
+
1. Get all cache keys
|
|
174
|
+
2. Filter by pattern
|
|
175
|
+
3. Delete matching entries
|
|
176
|
+
4. Clear specific data
|
|
177
|
+
5. Update affected entries
|
|
178
|
+
|
|
179
|
+
### Multi-Level Caching
|
|
180
|
+
1. Create multiple cache instances
|
|
181
|
+
2. Use different TTL for each level
|
|
182
|
+
3. Check caches in order (fast to slow)
|
|
183
|
+
4. Promote data between levels
|
|
184
|
+
5. Handle multi-level misses
|
|
185
|
+
|
|
186
|
+
### Cache Monitoring
|
|
187
|
+
1. Call `getStats()` periodically
|
|
188
|
+
2. Calculate cache hit rate
|
|
189
|
+
3. Monitor memory usage
|
|
190
|
+
4. Adjust configuration based on metrics
|
|
191
|
+
5. Log performance data
|
|
192
|
+
|
|
193
|
+
## 🚨 Common Pitfalls
|
|
194
|
+
|
|
195
|
+
### Don't
|
|
196
|
+
- Cache everything indiscriminately
|
|
197
|
+
- Set excessive cache sizes
|
|
198
|
+
- Use very long TTL for dynamic data
|
|
199
|
+
- Forget to handle cache misses
|
|
200
|
+
- Cache sensitive or user-specific data without care
|
|
201
|
+
|
|
202
|
+
### Do
|
|
203
|
+
- Monitor cache hit rates
|
|
204
|
+
- Set appropriate TTL values
|
|
205
|
+
- Handle cache misses gracefully
|
|
206
|
+
- Clear cache periodically
|
|
207
|
+
- Use namespace prefixes for keys
|
|
208
|
+
- Consider memory constraints
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
**Last Updated**: 2025-01-08
|
|
213
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|