@umituz/react-native-ai-gemini-provider 2.0.13 → 2.0.15
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/entities/error.types.ts +0 -17
- package/src/domain/entities/gemini.types.ts +1 -37
- package/src/domain/entities/models.ts +0 -31
- package/src/index.ts +8 -24
- package/src/infrastructure/interceptors/RequestInterceptors.ts +20 -6
- package/src/infrastructure/interceptors/ResponseInterceptors.ts +20 -6
- package/src/infrastructure/services/gemini-client-core.service.ts +25 -32
- package/src/infrastructure/services/gemini-provider.ts +31 -15
- package/src/infrastructure/services/gemini-streaming.service.ts +0 -4
- package/src/infrastructure/services/gemini-structured-text.service.ts +8 -5
- package/src/infrastructure/services/gemini-text-generation.service.ts +20 -51
- package/src/infrastructure/services/index.ts +2 -16
- package/src/infrastructure/telemetry/TelemetryHooks.ts +23 -10
- package/src/infrastructure/utils/async-state.util.ts +0 -7
- package/src/infrastructure/utils/error-mapper.util.ts +13 -8
- package/src/infrastructure/utils/gemini-data-transformer.util.ts +12 -9
- package/src/infrastructure/utils/performance.util.ts +4 -54
- package/src/infrastructure/utils/rate-limiter.util.ts +12 -8
- package/src/presentation/hooks/use-gemini.ts +91 -24
- package/src/providers/ProviderConfig.ts +0 -32
- package/src/providers/ProviderFactory.ts +24 -37
- package/src/providers/index.ts +2 -7
- package/src/domain/README.md +0 -232
- package/src/domain/constants/index.ts +0 -5
- package/src/domain/entities/README.md +0 -238
- package/src/infrastructure/README.md +0 -252
- package/src/infrastructure/cache/CACHE_SYSTEM.md +0 -213
- package/src/infrastructure/cache/README.md +0 -213
- package/src/infrastructure/cache/SimpleCache.ts +0 -173
- package/src/infrastructure/cache/index.ts +0 -7
- package/src/infrastructure/content/ContentBuilder.ts +0 -24
- package/src/infrastructure/content/README.md +0 -175
- package/src/infrastructure/interceptors/README.md +0 -226
- package/src/infrastructure/interceptors/REQUEST_INTERCEPTORS.md +0 -171
- package/src/infrastructure/job/JOB_MANAGER.md +0 -174
- package/src/infrastructure/job/JobManager.ts +0 -114
- package/src/infrastructure/job/README.md +0 -194
- package/src/infrastructure/response/README.md +0 -187
- package/src/infrastructure/response/RESPONSE_FORMATTER.md +0 -185
- package/src/infrastructure/response/ResponseFormatter.ts +0 -58
- package/src/infrastructure/services/generation-executor.ts +0 -71
- package/src/infrastructure/services/job-processor.ts +0 -58
- package/src/infrastructure/services/provider-initializer.ts +0 -31
- package/src/infrastructure/telemetry/README.md +0 -203
- package/src/infrastructure/telemetry/TELEMETRY_SYSTEM.md +0 -200
- package/src/presentation/README.md +0 -187
- package/src/presentation/hooks/README.md +0 -188
- package/src/presentation/hooks/USE_GEMINI_HOOK.md +0 -226
- package/src/providers/README.md +0 -247
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
# Telemetry Module
|
|
2
|
-
|
|
3
|
-
Monitoring and logging system for AI operations. Tracks request, response, error, and retry events for observability and debugging.
|
|
4
|
-
|
|
5
|
-
## 📍 Import Path
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
import { telemetryHooks } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## 🎯 Purpose
|
|
12
|
-
|
|
13
|
-
Use telemetry to monitor AI operations in real-time. Provides event-based system for tracking performance, errors, and usage patterns.
|
|
14
|
-
|
|
15
|
-
**When to use:**
|
|
16
|
-
- Monitor API call performance
|
|
17
|
-
- Track error rates and types
|
|
18
|
-
- Analyze usage patterns
|
|
19
|
-
- Debug AI operations
|
|
20
|
-
- Send events to analytics services
|
|
21
|
-
- Implement custom monitoring
|
|
22
|
-
- Track rate limiting
|
|
23
|
-
- Generate metrics
|
|
24
|
-
|
|
25
|
-
## 📌 Strategy
|
|
26
|
-
|
|
27
|
-
Telemetry provides visibility into AI operations. This system:
|
|
28
|
-
- Uses publish-subscribe pattern for events
|
|
29
|
-
- Supports multiple listeners simultaneously
|
|
30
|
-
- Emits events for all operations
|
|
31
|
-
- Provides timing and metadata
|
|
32
|
-
- Enables custom monitoring solutions
|
|
33
|
-
- Integrates with analytics platforms
|
|
34
|
-
|
|
35
|
-
**Key Decision**: Subscribe to telemetry events early in application lifecycle. Use events for monitoring, analytics, and debugging without modifying core services.
|
|
36
|
-
|
|
37
|
-
## ⚠️ Rules
|
|
38
|
-
|
|
39
|
-
### Usage Rules
|
|
40
|
-
- **MUST** subscribe to events before operations
|
|
41
|
-
- **SHOULD** unsubscribe when done
|
|
42
|
-
- **MUST NOT** emit sensitive data in events
|
|
43
|
-
- **SHOULD** aggregate metrics for performance
|
|
44
|
-
- **MUST** handle listener errors gracefully
|
|
45
|
-
|
|
46
|
-
### Event Subscription Rules
|
|
47
|
-
- **SHOULD** subscribe at application startup
|
|
48
|
-
- **MUST** store unsubscribe function
|
|
49
|
-
- **SHOULD** handle all event types
|
|
50
|
-
- **MUST NOT** block event emission
|
|
51
|
-
- **SHOULD** filter events as needed
|
|
52
|
-
|
|
53
|
-
### Data Privacy Rules
|
|
54
|
-
- **MUST NOT** log API keys in events
|
|
55
|
-
- **SHOULD NOT** include user data in metadata
|
|
56
|
-
- **MUST** sanitize sensitive information
|
|
57
|
-
- **SHOULD** use anonymized IDs
|
|
58
|
-
- **MUST** comply with privacy policies
|
|
59
|
-
|
|
60
|
-
### Performance Rules
|
|
61
|
-
- **SHOULD** keep listeners lightweight
|
|
62
|
-
- **MUST** avoid expensive operations in listeners
|
|
63
|
-
- **SHOULD** batch event processing
|
|
64
|
-
- **MUST NOT** block main thread
|
|
65
|
-
- **SHOULD** use async operations for heavy processing
|
|
66
|
-
|
|
67
|
-
## 🤖 AI Agent Guidelines
|
|
68
|
-
|
|
69
|
-
### When Adding Telemetry
|
|
70
|
-
1. **SUBSCRIBE** to telemetry events early
|
|
71
|
-
2. **HANDLE** all event types appropriately
|
|
72
|
-
3. **AGGREGATE** metrics for analysis
|
|
73
|
-
4. **SEND** to monitoring/analytics service
|
|
74
|
-
5. **UNSUBSCRIBE** on cleanup
|
|
75
|
-
|
|
76
|
-
### When Creating Custom Monitoring
|
|
77
|
-
1. **DEFINE** metrics to track
|
|
78
|
-
2. **CREATE** data structures for aggregation
|
|
79
|
-
3. **IMPLEMENT** event filtering
|
|
80
|
-
4. **CALCULATE** statistics periodically
|
|
81
|
-
5. **EXPORT** metrics for visualization
|
|
82
|
-
|
|
83
|
-
### When Debugging with Telemetry
|
|
84
|
-
1. **ADD** verbose logging for events
|
|
85
|
-
2. **TRACK** request/response correlation
|
|
86
|
-
3. **MONITOR** error patterns
|
|
87
|
-
4. **ANALYZE** performance metrics
|
|
88
|
-
5. **IDENTIFY** bottlenecks
|
|
89
|
-
|
|
90
|
-
### Code Style Rules
|
|
91
|
-
- **USE** unsubscribe pattern for cleanup
|
|
92
|
-
- **FILTER** events by type or model
|
|
93
|
-
- **AGGREGATE** metrics efficiently
|
|
94
|
-
- **HANDLE** errors in listeners
|
|
95
|
-
- **LOG** important state changes
|
|
96
|
-
|
|
97
|
-
## 📦 Available Telemetry
|
|
98
|
-
|
|
99
|
-
### TelemetryHooks
|
|
100
|
-
|
|
101
|
-
**Refer to**: [`TelemetryHooks.ts`](./TelemetryHooks.ts)
|
|
102
|
-
|
|
103
|
-
**Singleton instance**: `telemetryHooks`
|
|
104
|
-
|
|
105
|
-
**Methods:**
|
|
106
|
-
- `subscribe(listener)` - Add event listener
|
|
107
|
-
- `emit(event)` - Emit custom event
|
|
108
|
-
- `logRequest(model, feature?)` - Log request start
|
|
109
|
-
- `logResponse(model, startTime, feature?, metadata?)` - Log response
|
|
110
|
-
- `logError(model, error, feature?)` - Log error
|
|
111
|
-
- `logRetry(model, attempt, feature?)` - Log retry
|
|
112
|
-
- `clear()` - Clear all listeners
|
|
113
|
-
- `getListenerCount()` - Get active listener count
|
|
114
|
-
|
|
115
|
-
## 🔗 Related Modules
|
|
116
|
-
|
|
117
|
-
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
118
|
-
- **Services**: [`../services/README.md`](../services/README.md)
|
|
119
|
-
- **Interceptors**: [`../interceptors/README.md`](../interceptors/README.md)
|
|
120
|
-
|
|
121
|
-
## 📋 Event Types
|
|
122
|
-
|
|
123
|
-
### Request Event
|
|
124
|
-
Emitted when operation starts.
|
|
125
|
-
- Contains model and feature
|
|
126
|
-
- Includes timestamp
|
|
127
|
-
- No duration yet
|
|
128
|
-
|
|
129
|
-
### Response Event
|
|
130
|
-
Emitted when operation completes.
|
|
131
|
-
- Contains model and feature
|
|
132
|
-
- Includes duration in milliseconds
|
|
133
|
-
- May include metadata (token count, finish reason)
|
|
134
|
-
|
|
135
|
-
### Error Event
|
|
136
|
-
Emitted when operation fails.
|
|
137
|
-
- Contains model and feature
|
|
138
|
-
- Includes error information
|
|
139
|
-
- May include error type
|
|
140
|
-
|
|
141
|
-
### Retry Event
|
|
142
|
-
Emitted when retry is attempted.
|
|
143
|
-
- Contains model and feature
|
|
144
|
-
- Includes attempt number
|
|
145
|
-
- Tracks retry behavior
|
|
146
|
-
|
|
147
|
-
## 🎓 Usage Patterns
|
|
148
|
-
|
|
149
|
-
### Basic Monitoring
|
|
150
|
-
1. Import `telemetryHooks`
|
|
151
|
-
2. Subscribe to events with listener function
|
|
152
|
-
3. Handle different event types
|
|
153
|
-
4. Store unsubscribe function
|
|
154
|
-
5. Unsubscribe on cleanup
|
|
155
|
-
|
|
156
|
-
### Analytics Integration
|
|
157
|
-
1. Subscribe to telemetry events
|
|
158
|
-
2. Transform events to analytics format
|
|
159
|
-
3. Send to analytics service
|
|
160
|
-
4. Filter sensitive information
|
|
161
|
-
5. Handle errors gracefully
|
|
162
|
-
|
|
163
|
-
### Performance Tracking
|
|
164
|
-
1. Track response times by model
|
|
165
|
-
2. Calculate statistics (avg, min, max)
|
|
166
|
-
3. Monitor trends over time
|
|
167
|
-
4. Alert on slow operations
|
|
168
|
-
5. Generate performance reports
|
|
169
|
-
|
|
170
|
-
### Error Tracking
|
|
171
|
-
1. Subscribe to error events
|
|
172
|
-
2. Categorize errors by type
|
|
173
|
-
3. Track error rates
|
|
174
|
-
4. Send to error tracking service
|
|
175
|
-
5. Monitor for anomalies
|
|
176
|
-
|
|
177
|
-
### Custom Dashboard
|
|
178
|
-
1. Aggregate event data
|
|
179
|
-
2. Calculate statistics
|
|
180
|
-
3. Track model usage
|
|
181
|
-
4. Monitor success rates
|
|
182
|
-
5. Generate dashboard metrics
|
|
183
|
-
|
|
184
|
-
## 🚨 Common Pitfalls
|
|
185
|
-
|
|
186
|
-
### Don't
|
|
187
|
-
- Forget to unsubscribe from events
|
|
188
|
-
- Log sensitive data in events
|
|
189
|
-
- Block event emission with heavy processing
|
|
190
|
-
- Subscribe multiple times without cleanup
|
|
191
|
-
- Emit custom events without proper format
|
|
192
|
-
|
|
193
|
-
### Do
|
|
194
|
-
- Subscribe early in application lifecycle
|
|
195
|
-
- Clean up listeners when done
|
|
196
|
-
- Keep listeners lightweight
|
|
197
|
-
- Aggregate metrics efficiently
|
|
198
|
-
- Handle all event types appropriately
|
|
199
|
-
|
|
200
|
-
---
|
|
201
|
-
|
|
202
|
-
**Last Updated**: 2025-01-08
|
|
203
|
-
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
# Telemetry System
|
|
2
|
-
|
|
3
|
-
Monitoring and logging system for AI operations. Allows applications to track requests, responses, errors, and retries in real-time.
|
|
4
|
-
|
|
5
|
-
## 📍 Import Path
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
import { telemetryHooks } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## 🎯 Purpose
|
|
12
|
-
|
|
13
|
-
Use telemetry to monitor AI operations in real-time. Provides event-based system for tracking performance, errors, and usage patterns.
|
|
14
|
-
|
|
15
|
-
**When to use:**
|
|
16
|
-
- Monitor API call performance
|
|
17
|
-
- Track error rates and types
|
|
18
|
-
- Analyze usage patterns
|
|
19
|
-
- Debug AI operations
|
|
20
|
-
- Send events to analytics services
|
|
21
|
-
- Track rate limiting
|
|
22
|
-
|
|
23
|
-
## 📌 Strategy
|
|
24
|
-
|
|
25
|
-
Telemetry provides visibility into AI operations. This system:
|
|
26
|
-
- Uses publish-subscribe pattern for events
|
|
27
|
-
- Supports multiple listeners simultaneously
|
|
28
|
-
- Emits events for all operations
|
|
29
|
-
- Provides timing and metadata
|
|
30
|
-
- Enables custom monitoring solutions
|
|
31
|
-
|
|
32
|
-
**Key Decision**: Subscribe to telemetry events early in application lifecycle. Use events for monitoring, analytics, and debugging.
|
|
33
|
-
|
|
34
|
-
## ⚠️ Rules
|
|
35
|
-
|
|
36
|
-
### Usage Rules
|
|
37
|
-
- **MUST** subscribe to events before operations
|
|
38
|
-
- **SHOULD** unsubscribe when done
|
|
39
|
-
- **MUST NOT** emit sensitive data in events
|
|
40
|
-
- **SHOULD** aggregate metrics for performance
|
|
41
|
-
- **MUST** handle listener errors gracefully
|
|
42
|
-
|
|
43
|
-
### Event Subscription Rules
|
|
44
|
-
- **SHOULD** subscribe at application startup
|
|
45
|
-
- **MUST** store unsubscribe function
|
|
46
|
-
- **SHOULD** handle all event types
|
|
47
|
-
- **MUST NOT** block event emission
|
|
48
|
-
- **SHOULD** filter events as needed
|
|
49
|
-
|
|
50
|
-
### Data Privacy Rules
|
|
51
|
-
- **MUST NOT** log API keys in events
|
|
52
|
-
- **SHOULD NOT** include user data in metadata
|
|
53
|
-
- **MUST** sanitize sensitive information
|
|
54
|
-
- **SHOULD** use anonymized IDs
|
|
55
|
-
- **MUST** comply with privacy policies
|
|
56
|
-
|
|
57
|
-
### Performance Rules
|
|
58
|
-
- **SHOULD** keep listeners lightweight
|
|
59
|
-
- **MUST** avoid expensive operations in listeners
|
|
60
|
-
- **SHOULD** batch event processing
|
|
61
|
-
- **MUST NOT** block main thread
|
|
62
|
-
- **SHOULD** use async operations for heavy processing
|
|
63
|
-
|
|
64
|
-
## 🤖 AI Agent Guidelines
|
|
65
|
-
|
|
66
|
-
### When Adding Telemetry
|
|
67
|
-
1. **SUBSCRIBE** to telemetry events early
|
|
68
|
-
2. **HANDLE** all event types appropriately
|
|
69
|
-
3. **AGGREGATE** metrics for analysis
|
|
70
|
-
4. **SEND** to monitoring/analytics service
|
|
71
|
-
5. **UNSUBSCRIBE** on cleanup
|
|
72
|
-
|
|
73
|
-
### When Creating Custom Monitoring
|
|
74
|
-
1. **DEFINE** metrics to track
|
|
75
|
-
2. **CREATE** data structures for aggregation
|
|
76
|
-
3. **IMPLEMENT** event filtering
|
|
77
|
-
4. **CALCULATE** statistics periodically
|
|
78
|
-
5. **EXPORT** metrics for visualization
|
|
79
|
-
|
|
80
|
-
### When Debugging with Telemetry
|
|
81
|
-
1. **ADD** verbose logging for events
|
|
82
|
-
2. **TRACK** request/response correlation
|
|
83
|
-
3. **MONITOR** error patterns
|
|
84
|
-
4. **ANALYZE** performance metrics
|
|
85
|
-
5. **IDENTIFY** bottlenecks
|
|
86
|
-
|
|
87
|
-
### Code Style Rules
|
|
88
|
-
- **USE** unsubscribe pattern for cleanup
|
|
89
|
-
- **FILTER** events by type or model
|
|
90
|
-
- **AGGREGATE** metrics efficiently
|
|
91
|
-
- **HANDLE** errors in listeners
|
|
92
|
-
- **LOG** important state changes
|
|
93
|
-
|
|
94
|
-
## 📦 Available System
|
|
95
|
-
|
|
96
|
-
### telemetryHooks
|
|
97
|
-
|
|
98
|
-
**Refer to**: [`TelemetryHooks.ts`](./TelemetryHooks.ts)
|
|
99
|
-
|
|
100
|
-
**Singleton instance**
|
|
101
|
-
|
|
102
|
-
**Methods:**
|
|
103
|
-
- `subscribe(listener)` - Add event listener
|
|
104
|
-
- `emit(event)` - Emit custom event
|
|
105
|
-
- `logRequest(model, feature?)` - Log request start
|
|
106
|
-
- `logResponse(model, startTime, feature?, metadata?)` - Log response
|
|
107
|
-
- `logError(model, error, feature?)` - Log error
|
|
108
|
-
- `logRetry(model, attempt, feature?)` - Log retry
|
|
109
|
-
- `clear()` - Clear all listeners
|
|
110
|
-
- `getListenerCount()` - Get active listener count
|
|
111
|
-
|
|
112
|
-
## 🔗 Related Modules
|
|
113
|
-
|
|
114
|
-
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
115
|
-
- **Services**: [`../services/README.md`](../services/README.md)
|
|
116
|
-
- **Interceptors**: [`../interceptors/README.md`](../interceptors/README.md)
|
|
117
|
-
|
|
118
|
-
## 📋 Event Types
|
|
119
|
-
|
|
120
|
-
### Request Event
|
|
121
|
-
Emitted when operation starts.
|
|
122
|
-
- Contains model and feature
|
|
123
|
-
- Includes timestamp
|
|
124
|
-
- No duration yet
|
|
125
|
-
|
|
126
|
-
### Response Event
|
|
127
|
-
Emitted when operation completes.
|
|
128
|
-
- Contains model and feature
|
|
129
|
-
- Includes duration in milliseconds
|
|
130
|
-
- May include metadata (token count, finish reason)
|
|
131
|
-
|
|
132
|
-
### Error Event
|
|
133
|
-
Emitted when operation fails.
|
|
134
|
-
- Contains model and feature
|
|
135
|
-
- Includes error information
|
|
136
|
-
- May include error type
|
|
137
|
-
|
|
138
|
-
### Retry Event
|
|
139
|
-
Emitted when retry is attempted.
|
|
140
|
-
- Contains model and feature
|
|
141
|
-
- Includes attempt number
|
|
142
|
-
- Tracks retry behavior
|
|
143
|
-
|
|
144
|
-
## 🎓 Usage Patterns
|
|
145
|
-
|
|
146
|
-
### Basic Monitoring
|
|
147
|
-
1. Subscribe to telemetry events
|
|
148
|
-
2. Track metrics in event handler
|
|
149
|
-
3. Send to analytics/monitoring service
|
|
150
|
-
4. Aggregate and analyze metrics
|
|
151
|
-
5. Unsubscribe when done
|
|
152
|
-
|
|
153
|
-
### Analytics Integration
|
|
154
|
-
1. Subscribe to telemetry events
|
|
155
|
-
2. Transform events to analytics format
|
|
156
|
-
3. Send to analytics service
|
|
157
|
-
4. Filter sensitive information
|
|
158
|
-
5. Handle errors gracefully
|
|
159
|
-
|
|
160
|
-
### Performance Tracking
|
|
161
|
-
1. Track response times by model
|
|
162
|
-
2. Calculate statistics (avg, min, max)
|
|
163
|
-
3. Monitor trends over time
|
|
164
|
-
4. Alert on slow operations
|
|
165
|
-
5. Generate performance reports
|
|
166
|
-
|
|
167
|
-
### Error Tracking
|
|
168
|
-
1. Subscribe to error events
|
|
169
|
-
2. Categorize errors by type
|
|
170
|
-
3. Track error rates
|
|
171
|
-
4. Send to error tracking service
|
|
172
|
-
5. Monitor for anomalies
|
|
173
|
-
|
|
174
|
-
### Custom Dashboard
|
|
175
|
-
1. Aggregate event data
|
|
176
|
-
2. Calculate statistics
|
|
177
|
-
3. Track model usage
|
|
178
|
-
4. Monitor success rates
|
|
179
|
-
5. Generate dashboard metrics
|
|
180
|
-
|
|
181
|
-
## 🚨 Common Pitfalls
|
|
182
|
-
|
|
183
|
-
### Don't
|
|
184
|
-
- Forget to unsubscribe from events
|
|
185
|
-
- Log sensitive data in events
|
|
186
|
-
- Block event emission with heavy processing
|
|
187
|
-
- Subscribe multiple times without cleanup
|
|
188
|
-
- Emit custom events without proper format
|
|
189
|
-
|
|
190
|
-
### Do
|
|
191
|
-
- Subscribe early in application lifecycle
|
|
192
|
-
- Clean up listeners when done
|
|
193
|
-
- Keep listeners lightweight
|
|
194
|
-
- Aggregate telemetry metrics
|
|
195
|
-
- Handle all event types appropriately
|
|
196
|
-
|
|
197
|
-
---
|
|
198
|
-
|
|
199
|
-
**Last Updated**: 2025-01-08
|
|
200
|
-
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -1,187 +0,0 @@
|
|
|
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)
|