@umituz/react-native-ai-gemini-provider 1.14.27 → 1.14.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/domain/README.md +232 -0
- package/src/domain/constants/README.md +191 -0
- package/src/domain/entities/README.md +238 -0
- package/src/infrastructure/README.md +252 -0
- package/src/infrastructure/cache/CACHE_SYSTEM.md +213 -0
- package/src/infrastructure/cache/README.md +213 -0
- package/src/infrastructure/content/CONTENT_BUILDER.md +175 -0
- package/src/infrastructure/content/README.md +175 -0
- package/src/infrastructure/interceptors/README.md +226 -0
- package/src/infrastructure/interceptors/REQUEST_INTERCEPTORS.md +171 -0
- package/src/infrastructure/job/JOB_MANAGER.md +174 -0
- package/src/infrastructure/job/README.md +194 -0
- package/src/infrastructure/response/README.md +187 -0
- package/src/infrastructure/response/RESPONSE_FORMATTER.md +185 -0
- package/src/infrastructure/services/CORE_CLIENT_SERVICE.md +202 -0
- package/src/infrastructure/services/FEATURE_MODEL_SELECTOR_SERVICE.md +206 -0
- package/src/infrastructure/services/GENERATION_EXECUTOR_SERVICE.md +176 -0
- package/src/infrastructure/services/IMAGE_EDIT_SERVICE.md +169 -0
- package/src/infrastructure/services/IMAGE_GENERATION_SERVICE.md +166 -0
- package/src/infrastructure/services/JOB_PROCESSOR_SERVICE.md +174 -0
- package/src/infrastructure/services/PROVIDER_INITIALIZER_SERVICE.md +176 -0
- package/src/infrastructure/services/README.md +233 -0
- package/src/infrastructure/services/RETRY_SERVICE.md +178 -0
- package/src/infrastructure/services/STREAMING_SERVICE.md +166 -0
- package/src/infrastructure/services/STRUCTURED_TEXT_SERVICE.md +175 -0
- package/src/infrastructure/services/TEXT_GENERATION_SERVICE.md +160 -0
- package/src/infrastructure/services/VEO_HTTP_CLIENT_SERVICE.md +179 -0
- package/src/infrastructure/services/VEO_POLLING_SERVICE.md +173 -0
- package/src/infrastructure/services/VIDEO_DOWNLOADER_SERVICE.md +166 -0
- package/src/infrastructure/services/VIDEO_ERROR_HANDLER_SERVICE.md +185 -0
- package/src/infrastructure/services/VIDEO_GENERATION_SERVICE.md +176 -0
- package/src/infrastructure/services/VIDEO_URL_EXTRACTOR_SERVICE.md +186 -0
- package/src/infrastructure/telemetry/README.md +203 -0
- package/src/infrastructure/telemetry/TELEMETRY_SYSTEM.md +200 -0
- package/src/infrastructure/utils/DATA_TRANSFORMER_UTILS.md +175 -0
- package/src/infrastructure/utils/ERROR_MAPPER.md +170 -0
- package/src/infrastructure/utils/ERROR_UTILITIES.md +208 -0
- package/src/infrastructure/utils/IMAGE_PREPARER_UTILS.md +185 -0
- package/src/infrastructure/utils/INPUT_BUILDERS.md +214 -0
- package/src/infrastructure/utils/MODEL_VALIDATION_UTILS.md +189 -0
- package/src/infrastructure/utils/PERFORMANCE_UTILITIES.md +477 -0
- package/src/infrastructure/utils/PERFORMANCE_UTILS.md +219 -0
- package/src/infrastructure/utils/README.md +289 -0
- package/src/presentation/README.md +187 -0
- package/src/presentation/hooks/README.md +188 -0
- package/src/presentation/hooks/USE_GEMINI_HOOK.md +226 -0
- package/src/providers/README.md +247 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# Video Generation Service
|
|
2
|
+
|
|
3
|
+
Generates videos from text prompts and images using Google Veo API.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { geminiVideoGenerationService } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use this service to generate AI-powered videos from text descriptions or images. Supports text-to-video and image-to-video generation with progress tracking using Veo models.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Generate videos from text descriptions
|
|
17
|
+
- Animate existing images
|
|
18
|
+
- Create video content for applications
|
|
19
|
+
- Product visualization and motion graphics
|
|
20
|
+
|
|
21
|
+
## 📌 Strategy
|
|
22
|
+
|
|
23
|
+
Video generation is an async, long-running operation that requires special handling. This service:
|
|
24
|
+
- Implements automatic polling for operation status
|
|
25
|
+
- Provides progress callbacks for UI feedback
|
|
26
|
+
- Supports multiple aspect ratios for different platforms
|
|
27
|
+
- Handles video download and URL generation
|
|
28
|
+
- Manages queuing and processing states
|
|
29
|
+
|
|
30
|
+
**Key Decision**: Video generation uses polling mechanism since processing takes time. The service automatically handles status checks and provides progress updates through callbacks.
|
|
31
|
+
|
|
32
|
+
## ⚠️ Rules
|
|
33
|
+
|
|
34
|
+
### Usage Rules
|
|
35
|
+
- **MUST** initialize provider with API key before use
|
|
36
|
+
- **MUST** handle async operation completion
|
|
37
|
+
- **SHOULD** implement progress callbacks for UX
|
|
38
|
+
- **MUST NOT** assume immediate results
|
|
39
|
+
- **MUST** handle video generation errors appropriately
|
|
40
|
+
|
|
41
|
+
### Prompt Rules
|
|
42
|
+
- **SHOULD** describe motion and action clearly
|
|
43
|
+
- **SHOULD** specify camera movements
|
|
44
|
+
- **MUST NOT** exceed 2000 character limit
|
|
45
|
+
- **SHOULD** include atmosphere and mood details
|
|
46
|
+
|
|
47
|
+
### Configuration Rules
|
|
48
|
+
- **MUST** use valid model IDs (veo-3.1-fast-generate-preview)
|
|
49
|
+
- **SHOULD** select appropriate aspect ratio for platform
|
|
50
|
+
- **MUST** respect content safety guidelines
|
|
51
|
+
- **SHOULD** set reasonable timeout expectations
|
|
52
|
+
|
|
53
|
+
### Error Handling Rules
|
|
54
|
+
- **MUST** catch and handle `GeminiError`
|
|
55
|
+
- **MUST** check `result.status` for completion
|
|
56
|
+
- **MUST** handle `result.error` for failures
|
|
57
|
+
- **SHOULD** provide user-friendly error messages
|
|
58
|
+
- **MUST NOT** expose API keys in errors
|
|
59
|
+
|
|
60
|
+
## 🤖 AI Agent Guidelines
|
|
61
|
+
|
|
62
|
+
### When Modifying This Service
|
|
63
|
+
1. **READ** the implementation file first
|
|
64
|
+
2. **UNDERSTAND** polling mechanism
|
|
65
|
+
3. **MAINTAIN** backward compatibility
|
|
66
|
+
4. **ADD** tests for new functionality
|
|
67
|
+
5. **UPDATE** type definitions
|
|
68
|
+
|
|
69
|
+
### When Adding New Features
|
|
70
|
+
1. **CHECK** if similar feature exists in video services
|
|
71
|
+
2. **FOLLOW** existing patterns
|
|
72
|
+
3. **USE** established error handling
|
|
73
|
+
4. **DOCUMENT** in type definitions
|
|
74
|
+
5. **ADD** examples to tests (not docs)
|
|
75
|
+
|
|
76
|
+
### When Fixing Bugs
|
|
77
|
+
1. **REPRODUCE** bug locally first
|
|
78
|
+
2. **IDENTIFY** root cause
|
|
79
|
+
3. **FIX** with minimal changes
|
|
80
|
+
4. **ADD** regression test
|
|
81
|
+
5. **VERIFY** all tests pass
|
|
82
|
+
|
|
83
|
+
### Code Style Rules
|
|
84
|
+
- **USE** async/await (no callbacks except onProgress)
|
|
85
|
+
- **VALIDATE** inputs at function entry
|
|
86
|
+
- **THROW** typed errors (`GeminiError`)
|
|
87
|
+
- **LOG** important operations
|
|
88
|
+
- **COMMENT** complex logic only
|
|
89
|
+
|
|
90
|
+
## 📦 Available Methods
|
|
91
|
+
|
|
92
|
+
### `generateTextToVideo(input, onProgress?)`
|
|
93
|
+
|
|
94
|
+
Generate video from text prompt.
|
|
95
|
+
|
|
96
|
+
**Refer to**: [`gemini-video-generation.service.ts`](./gemini-video-generation.service.ts)
|
|
97
|
+
|
|
98
|
+
### `generateVideo(input, onProgress?)`
|
|
99
|
+
|
|
100
|
+
Generate video from text and/or image.
|
|
101
|
+
|
|
102
|
+
**Refer to**: [`gemini-video-generation.service.ts`](./gemini-video-generation.service.ts)
|
|
103
|
+
|
|
104
|
+
## 🔗 Related Modules
|
|
105
|
+
|
|
106
|
+
- **Domain Types**: [`domain/entities/README.md`](../domain/entities/README.md)
|
|
107
|
+
- **Veo HTTP Client**: [`VEO_HTTP_CLIENT_SERVICE.md`](./VEO_HTTP_CLIENT_SERVICE.md)
|
|
108
|
+
- **Veo Polling**: [`VEO_POLLING_SERVICE.md`](./VEO_POLLING_SERVICE.md)
|
|
109
|
+
- **Video Downloader**: [`VIDEO_DOWNLOADER_SERVICE.md`](./VIDEO_DOWNLOADER_SERVICE.md)
|
|
110
|
+
- **Video Error Handler**: [`VIDEO_ERROR_HANDLER_SERVICE.md`](./VIDEO_ERROR_HANDLER_SERVICE.md)
|
|
111
|
+
|
|
112
|
+
## 📋 Configuration Reference
|
|
113
|
+
|
|
114
|
+
### Generation Config
|
|
115
|
+
See: [`domain/entities/README.md`](../domain/entities/README.md)
|
|
116
|
+
|
|
117
|
+
### Model Selection
|
|
118
|
+
- Current model: `veo-3.1-fast-generate-preview`
|
|
119
|
+
- Aspect ratios: `16:9` | `9:16` | `1:1` | `4:3`
|
|
120
|
+
- Resolution: Up to 4K
|
|
121
|
+
- Duration: Auto-determined by model
|
|
122
|
+
|
|
123
|
+
### Error Types
|
|
124
|
+
See: [`ERROR_UTILITIES.md`](../infrastructure/utils/ERROR_UTILITIES.md)
|
|
125
|
+
|
|
126
|
+
## 🎓 Usage Patterns
|
|
127
|
+
|
|
128
|
+
### Basic Text-to-Video
|
|
129
|
+
1. Import service
|
|
130
|
+
2. Call `generateTextToVideo()` with prompt and options
|
|
131
|
+
3. Provide `onProgress` callback for UI updates
|
|
132
|
+
4. Handle response (contains `videoUrl`, `status`, `progress`)
|
|
133
|
+
5. Display video or handle errors
|
|
134
|
+
|
|
135
|
+
### Image-to-Video
|
|
136
|
+
1. Import service
|
|
137
|
+
2. Prepare base64-encoded image
|
|
138
|
+
3. Call `generateVideo()` with prompt and image
|
|
139
|
+
4. Monitor progress through callback
|
|
140
|
+
5. Download or display result video
|
|
141
|
+
|
|
142
|
+
### With Progress Tracking
|
|
143
|
+
1. Implement progress callback function
|
|
144
|
+
2. Update UI with progress percentage and status
|
|
145
|
+
3. Handle status changes: queued → processing → completed/failed
|
|
146
|
+
4. Show appropriate loading states
|
|
147
|
+
5. Display final video or error message
|
|
148
|
+
|
|
149
|
+
### With Negative Prompts
|
|
150
|
+
1. Prepare main prompt describing desired content
|
|
151
|
+
2. Add negative prompt for unwanted elements
|
|
152
|
+
3. Call generation method with both prompts
|
|
153
|
+
4. Handle result as normal
|
|
154
|
+
|
|
155
|
+
## 🚨 Common Pitfalls
|
|
156
|
+
|
|
157
|
+
### Don't
|
|
158
|
+
- Assume immediate video generation (takes minutes)
|
|
159
|
+
- Ignore progress callbacks (bad UX)
|
|
160
|
+
- Forget to handle failed status
|
|
161
|
+
- Use extremely long prompts (>2000 chars)
|
|
162
|
+
- Expect all videos to have same duration
|
|
163
|
+
|
|
164
|
+
### Do
|
|
165
|
+
- Implement progress tracking
|
|
166
|
+
- Handle all status states (queued, processing, completed, failed)
|
|
167
|
+
- Provide loading feedback to users
|
|
168
|
+
- Use descriptive prompts with motion details
|
|
169
|
+
- Specify camera movements and atmosphere
|
|
170
|
+
- Handle timeouts appropriately
|
|
171
|
+
- Consider aspect ratio for target platform
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
**Last Updated**: 2025-01-08
|
|
176
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# Video URL Extractor Service
|
|
2
|
+
|
|
3
|
+
Utility for extracting video URLs from Veo API operation responses. Handles multiple response formats from different Veo API versions.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { extractVideoUrl } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use video URL extractor to get video URLs from Veo operation responses. Handles multiple API response formats transparently.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Extract video URLs from Veo responses
|
|
17
|
+
- Handle different API versions
|
|
18
|
+
- Parse operation results
|
|
19
|
+
- Get download URLs
|
|
20
|
+
- Process video generation responses
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
Veo API has multiple response formats. This utility:
|
|
25
|
+
- Handles all response format variations
|
|
26
|
+
- Extracts URLs transparently
|
|
27
|
+
- Provides null safety
|
|
28
|
+
- Supports API version changes
|
|
29
|
+
- Simplifies response parsing
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Always use extractor for Veo responses. Handles format variations automatically.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Extraction Rules
|
|
36
|
+
- **MUST** handle all response formats
|
|
37
|
+
- **SHOULD** validate operation object
|
|
38
|
+
- **MUST** return null if URL not found
|
|
39
|
+
- **SHOULD** log unexpected formats
|
|
40
|
+
- **MUST NOT** throw on missing URL
|
|
41
|
+
|
|
42
|
+
### Validation Rules
|
|
43
|
+
- **SHOULD** check operation structure
|
|
44
|
+
- **MUST** validate URL format
|
|
45
|
+
- **SHOULD** handle null responses
|
|
46
|
+
- **MUST** verify URL accessibility
|
|
47
|
+
- **SHOULD NOT** assume format
|
|
48
|
+
|
|
49
|
+
### Error Handling Rules
|
|
50
|
+
- **MUST** return null on failure
|
|
51
|
+
- **SHOULD** log parsing errors
|
|
52
|
+
- **MUST NOT** throw exceptions
|
|
53
|
+
- **SHOULD** handle malformed responses
|
|
54
|
+
- **MUST** provide graceful degradation
|
|
55
|
+
|
|
56
|
+
### URL Handling Rules
|
|
57
|
+
- **MUST** validate URL strings
|
|
58
|
+
- **SHOULD** check URL scheme
|
|
59
|
+
- **MUST** handle authentication requirements
|
|
60
|
+
- **SHOULD NOT** expose sensitive data
|
|
61
|
+
- **MUST** preserve URL parameters
|
|
62
|
+
|
|
63
|
+
## 🤖 AI Agent Guidelines
|
|
64
|
+
|
|
65
|
+
### When Extracting URLs
|
|
66
|
+
1. **PROVIDE** operation object
|
|
67
|
+
2. **CALL** extractVideoUrl()
|
|
68
|
+
3. **CHECK** return value
|
|
69
|
+
4. **HANDLE** null result
|
|
70
|
+
5. **USE** URL or report error
|
|
71
|
+
|
|
72
|
+
### When Handling Missing URLs
|
|
73
|
+
1. **CHECK** if result is null
|
|
74
|
+
2. **LOG** response structure
|
|
75
|
+
3. **VERIFY** operation completed
|
|
76
|
+
4. **REPORT** error to user
|
|
77
|
+
5. **PROVIDE** recovery options
|
|
78
|
+
|
|
79
|
+
### When Validating Responses
|
|
80
|
+
1. **CHECK** operation object structure
|
|
81
|
+
2. **LOOK** for expected fields
|
|
82
|
+
3. **HANDLE** format variations
|
|
83
|
+
4. **LOG** unexpected formats
|
|
84
|
+
5. **UPDATE** for new API versions
|
|
85
|
+
|
|
86
|
+
### Code Style Rules
|
|
87
|
+
- **VALIDATE** input structure
|
|
88
|
+
- **HANDLE** all format variations
|
|
89
|
+
- **RETURN** null for missing URLs
|
|
90
|
+
- **LOG** unexpected structures
|
|
91
|
+
- **TEST** with various formats
|
|
92
|
+
|
|
93
|
+
## 📦 Available Function
|
|
94
|
+
|
|
95
|
+
### extractVideoUrl
|
|
96
|
+
|
|
97
|
+
**Refer to**: [`gemini-video-url-extractor.ts`](./gemini-video-url-extractor.ts)
|
|
98
|
+
|
|
99
|
+
**Parameters:**
|
|
100
|
+
- `operation`: VeoOperation - Veo operation response object
|
|
101
|
+
|
|
102
|
+
**Returns:** string | null - Video URL if found, otherwise null
|
|
103
|
+
|
|
104
|
+
## 🔗 Related Modules
|
|
105
|
+
|
|
106
|
+
- **Video Generation**: [`VIDEO_GENERATION_SERVICE.md`](./VIDEO_GENERATION_SERVICE.md)
|
|
107
|
+
- **Video Downloader**: [`VIDEO_DOWNLOADER_SERVICE.md`](./VIDEO_DOWNLOADER_SERVICE.md)
|
|
108
|
+
- **Veo Polling**: [`VEO_POLLING_SERVICE.md`](./VEO_POLLING_SERVICE.md)
|
|
109
|
+
|
|
110
|
+
## 📋 Supported Response Formats
|
|
111
|
+
|
|
112
|
+
### Format 1: New SDK Format
|
|
113
|
+
```typescript
|
|
114
|
+
{
|
|
115
|
+
response: {
|
|
116
|
+
generatedVideos: [{
|
|
117
|
+
video: { uri: "https://..." }
|
|
118
|
+
}]
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Format 2: Alternative SDK Format
|
|
124
|
+
```typescript
|
|
125
|
+
{
|
|
126
|
+
response: {
|
|
127
|
+
generatedVideos: [{
|
|
128
|
+
uri: "https://..."
|
|
129
|
+
}]
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Format 3: Legacy Format
|
|
135
|
+
```typescript
|
|
136
|
+
{
|
|
137
|
+
done: true,
|
|
138
|
+
response: {
|
|
139
|
+
videoUri: "https://..."
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## 🎓 Usage Patterns
|
|
145
|
+
|
|
146
|
+
### URL Extraction
|
|
147
|
+
1. Get operation response
|
|
148
|
+
2. Call extractVideoUrl()
|
|
149
|
+
3. Check if URL exists
|
|
150
|
+
4. Use URL for download
|
|
151
|
+
5. Handle missing URL
|
|
152
|
+
|
|
153
|
+
### Format Handling
|
|
154
|
+
1. Provide operation object
|
|
155
|
+
2. Extract URL transparently
|
|
156
|
+
3. Handle any format variation
|
|
157
|
+
4. Get URL or null
|
|
158
|
+
5. Process result accordingly
|
|
159
|
+
|
|
160
|
+
### Error Handling
|
|
161
|
+
1. Call extractor
|
|
162
|
+
2. Check for null result
|
|
163
|
+
3. Log response structure if missing
|
|
164
|
+
4. Report error to user
|
|
165
|
+
5. Provide recovery options
|
|
166
|
+
|
|
167
|
+
## 🚨 Common Pitfalls
|
|
168
|
+
|
|
169
|
+
### Don't
|
|
170
|
+
- Assume specific response format
|
|
171
|
+
- Throw on missing URL
|
|
172
|
+
- Skip null checking
|
|
173
|
+
- Hardcode response paths
|
|
174
|
+
- Ignore format variations
|
|
175
|
+
|
|
176
|
+
### Do
|
|
177
|
+
- Always check for null
|
|
178
|
+
- Handle all formats
|
|
179
|
+
- Log unexpected structures
|
|
180
|
+
- Return null gracefully
|
|
181
|
+
- Test with various responses
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
**Last Updated**: 2025-01-08
|
|
186
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,203 @@
|
|
|
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)
|