@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,213 @@
|
|
|
1
|
+
# Cache Module
|
|
2
|
+
|
|
3
|
+
LRU (Least Recently Used) cache implementation. Used for performance optimization and cost savings.
|
|
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 module:
|
|
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
|
+
Generic LRU cache implementation.
|
|
101
|
+
|
|
102
|
+
**Refer to**: [`SimpleCache.ts`](./SimpleCache.ts)
|
|
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
|
+
Global cache for model selection results.
|
|
117
|
+
|
|
118
|
+
**Refer to**: [`index.ts`](./index.ts)
|
|
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)
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Content Builder
|
|
2
|
+
|
|
3
|
+
Builder class for creating Gemini API content structures. Provides type-safe methods for building text, image, and file content parts.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { ContentBuilder } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use ContentBuilder to create properly formatted Gemini content objects. Handles text, images, and file data for multimodal inputs.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Build content for text generation
|
|
17
|
+
- Create multimodal content (text + images)
|
|
18
|
+
- Format chat messages
|
|
19
|
+
- Add file data to requests
|
|
20
|
+
- Build conversation history
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
ContentBuilder ensures correct API format. This class:
|
|
25
|
+
- Creates properly structured content objects
|
|
26
|
+
- Handles different content types (text, images, files)
|
|
27
|
+
- Simplifies multimodal content creation
|
|
28
|
+
- Provides consistent formatting
|
|
29
|
+
- Validates content structure
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Use ContentBuilder for all content creation. Ensures compatibility with Gemini API format.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Content Creation Rules
|
|
36
|
+
- **MUST** specify role (user or model)
|
|
37
|
+
- **SHOULD** use builder methods
|
|
38
|
+
- **MUST** provide valid parts array
|
|
39
|
+
- **SHOULD** validate content structure
|
|
40
|
+
- **MUST NOT** create malformed content
|
|
41
|
+
|
|
42
|
+
### Content Part Rules
|
|
43
|
+
- **MUST** use correct MIME types
|
|
44
|
+
- **SHOULD** provide valid base64 data
|
|
45
|
+
- **MUST** handle encoding properly
|
|
46
|
+
- **SHOULD** validate image data
|
|
47
|
+
- **MUST NOT** use invalid formats
|
|
48
|
+
|
|
49
|
+
### Multimodal Rules
|
|
50
|
+
- **SHOULD** include text with images
|
|
51
|
+
- **MUST** order parts appropriately
|
|
52
|
+
- **SHOULD** limit image count
|
|
53
|
+
- **MUST** handle mixed content types
|
|
54
|
+
- **SHOULD NOT** exceed size limits
|
|
55
|
+
|
|
56
|
+
### Best Practices Rules
|
|
57
|
+
- **SHOULD** use builder for consistency
|
|
58
|
+
- **MUST** validate content before sending
|
|
59
|
+
- **SHOULD** handle build errors
|
|
60
|
+
- **MUST** use appropriate MIME types
|
|
61
|
+
- **SHOULD** test content structures
|
|
62
|
+
|
|
63
|
+
## 🤖 AI Agent Guidelines
|
|
64
|
+
|
|
65
|
+
### When Building Text Content
|
|
66
|
+
1. **CREATE** ContentBuilder instance
|
|
67
|
+
2. **BUILD** text part
|
|
68
|
+
3. **CREATE** content with role
|
|
69
|
+
4. **VALIDATE** structure
|
|
70
|
+
5. **RETURN** content object
|
|
71
|
+
|
|
72
|
+
### When Building Multimodal Content
|
|
73
|
+
1. **CREATE** ContentBuilder instance
|
|
74
|
+
2. **BUILD** text part
|
|
75
|
+
3. **BUILD** image data parts
|
|
76
|
+
4. **COMBINE** parts in array
|
|
77
|
+
5. **CREATE** content object
|
|
78
|
+
|
|
79
|
+
### When Building Chat History
|
|
80
|
+
1. **CREATE** ContentBuilder instance
|
|
81
|
+
2. **BUILD** user messages
|
|
82
|
+
3. **BUILD** model responses
|
|
83
|
+
4. **COMBINE** in array
|
|
84
|
+
5. **RETURN** conversation array
|
|
85
|
+
|
|
86
|
+
### Code Style Rules
|
|
87
|
+
- **USE** builder methods
|
|
88
|
+
- **VALIDATE** inputs
|
|
89
|
+
- **HANDLE** errors gracefully
|
|
90
|
+
- **COMMENT** complex content
|
|
91
|
+
- **TEST** content structures
|
|
92
|
+
|
|
93
|
+
## 📦 Available Class
|
|
94
|
+
|
|
95
|
+
### ContentBuilder
|
|
96
|
+
|
|
97
|
+
**Refer to**: [`ContentBuilder.ts`](./ContentBuilder.ts)
|
|
98
|
+
|
|
99
|
+
**Methods:**
|
|
100
|
+
- `buildContent(role, parts)` - Build GeminiContent object
|
|
101
|
+
- `buildTextPart(text)` - Build text part
|
|
102
|
+
- `buildImageDataPart(mimeType, data)` - Build image data part
|
|
103
|
+
- `buildFileDataPart(mimeType, fileUri)` - Build file data part
|
|
104
|
+
|
|
105
|
+
## 🔗 Related Modules
|
|
106
|
+
|
|
107
|
+
- **Domain Entities**: [`../../domain/entities/README.md`](../../domain/entities/README.md)
|
|
108
|
+
- **Text Generation**: [`../services/TEXT_GENERATION_SERVICE.md`](../services/TEXT_GENERATION_SERVICE.md)
|
|
109
|
+
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
110
|
+
|
|
111
|
+
## 📋 Supported MIME Types
|
|
112
|
+
|
|
113
|
+
### Image Formats
|
|
114
|
+
- `image/png` - PNG images
|
|
115
|
+
- `image/jpeg` - JPEG images
|
|
116
|
+
- `image/webp` - WebP images
|
|
117
|
+
- `image/heic` - HEIC images
|
|
118
|
+
- `image/heif` - HEIF images
|
|
119
|
+
|
|
120
|
+
### Document Formats
|
|
121
|
+
- `application/pdf` - PDF documents
|
|
122
|
+
- `text/plain` - Plain text
|
|
123
|
+
- `text/html` - HTML documents
|
|
124
|
+
- `application/json` - JSON data
|
|
125
|
+
|
|
126
|
+
## 🎓 Usage Patterns
|
|
127
|
+
|
|
128
|
+
### Basic Text Content
|
|
129
|
+
1. Create ContentBuilder instance
|
|
130
|
+
2. Build text part
|
|
131
|
+
3. Create content with role
|
|
132
|
+
4. Use in service call
|
|
133
|
+
5. Handle response
|
|
134
|
+
|
|
135
|
+
### Multimodal Content
|
|
136
|
+
1. Create ContentBuilder instance
|
|
137
|
+
2. Build text part
|
|
138
|
+
3. Build image data parts
|
|
139
|
+
4. Combine all parts
|
|
140
|
+
5. Create content with role
|
|
141
|
+
|
|
142
|
+
### Chat Conversation
|
|
143
|
+
1. Create ContentBuilder instance
|
|
144
|
+
2. Build user message content
|
|
145
|
+
3. Build model response content
|
|
146
|
+
4. Repeat for conversation
|
|
147
|
+
5. Return content array
|
|
148
|
+
|
|
149
|
+
### File-Based Content
|
|
150
|
+
1. Create ContentBuilder instance
|
|
151
|
+
2. Build text instruction
|
|
152
|
+
3. Build file data part
|
|
153
|
+
4. Create content with role
|
|
154
|
+
5. Process file
|
|
155
|
+
|
|
156
|
+
## 🚨 Common Pitfalls
|
|
157
|
+
|
|
158
|
+
### Don't
|
|
159
|
+
- Create content objects manually
|
|
160
|
+
- Skip role specification
|
|
161
|
+
- Use invalid MIME types
|
|
162
|
+
- Forget to validate content
|
|
163
|
+
- Mix up part order
|
|
164
|
+
|
|
165
|
+
### Do
|
|
166
|
+
- Use ContentBuilder for all content
|
|
167
|
+
- Always specify role
|
|
168
|
+
- Validate MIME types
|
|
169
|
+
- Test content structure
|
|
170
|
+
- Follow API format
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
**Last Updated**: 2025-01-08
|
|
175
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Content Builder
|
|
2
|
+
|
|
3
|
+
Helper class for building content structures for Gemini API. Creates properly formatted content objects with text, images, and files.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { ContentBuilder } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use ContentBuilder to create properly formatted Gemini content objects. Handles text, images, and file data for multimodal inputs.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Build content for text generation
|
|
17
|
+
- Create multimodal content (text + images)
|
|
18
|
+
- Format chat messages
|
|
19
|
+
- Add file data to requests
|
|
20
|
+
- Build conversation history
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
ContentBuilder ensures correct API format. This class:
|
|
25
|
+
- Creates properly structured content objects
|
|
26
|
+
- Handles different content types (text, images, files)
|
|
27
|
+
- Simplifies multimodal content creation
|
|
28
|
+
- Provides consistent formatting
|
|
29
|
+
- Validates content structure
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Use ContentBuilder for all content creation. Ensures compatibility with Gemini API format.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Content Creation Rules
|
|
36
|
+
- **MUST** specify role (user or model)
|
|
37
|
+
- **SHOULD** use builder methods
|
|
38
|
+
- **MUST** provide valid parts array
|
|
39
|
+
- **SHOULD** validate content structure
|
|
40
|
+
- **MUST NOT** create malformed content
|
|
41
|
+
|
|
42
|
+
### Content Part Rules
|
|
43
|
+
- **MUST** use correct MIME types
|
|
44
|
+
- **SHOULD** provide valid base64 data
|
|
45
|
+
- **MUST** handle encoding properly
|
|
46
|
+
- **SHOULD** validate image data
|
|
47
|
+
- **MUST NOT** use invalid formats
|
|
48
|
+
|
|
49
|
+
### Multimodal Rules
|
|
50
|
+
- **SHOULD** include text with images
|
|
51
|
+
- **MUST** order parts appropriately
|
|
52
|
+
- **SHOULD** limit image count
|
|
53
|
+
- **MUST** handle mixed content types
|
|
54
|
+
- **SHOULD NOT** exceed size limits
|
|
55
|
+
|
|
56
|
+
### Best Practices Rules
|
|
57
|
+
- **SHOULD** use builder for consistency
|
|
58
|
+
- **MUST** validate content before sending
|
|
59
|
+
- **SHOULD** handle build errors
|
|
60
|
+
- **MUST** use appropriate MIME types
|
|
61
|
+
- **SHOULD** test content structures
|
|
62
|
+
|
|
63
|
+
## 🤖 AI Agent Guidelines
|
|
64
|
+
|
|
65
|
+
### When Building Text Content
|
|
66
|
+
1. **CREATE** ContentBuilder instance
|
|
67
|
+
2. **BUILD** text part
|
|
68
|
+
3. **CREATE** content with role
|
|
69
|
+
4. **VALIDATE** structure
|
|
70
|
+
5. **RETURN** content object
|
|
71
|
+
|
|
72
|
+
### When Building Multimodal Content
|
|
73
|
+
1. **CREATE** ContentBuilder instance
|
|
74
|
+
2. **BUILD** text part
|
|
75
|
+
3. **BUILD** image data parts
|
|
76
|
+
4. **COMBINE** parts in array
|
|
77
|
+
5. **CREATE** content object
|
|
78
|
+
|
|
79
|
+
### When Building Chat History
|
|
80
|
+
1. **CREATE** ContentBuilder instance
|
|
81
|
+
2. **BUILD** user messages
|
|
82
|
+
3. **BUILD** model responses
|
|
83
|
+
4. **COMBINE** in array
|
|
84
|
+
5. **RETURN** conversation array
|
|
85
|
+
|
|
86
|
+
### Code Style Rules
|
|
87
|
+
- **USE** builder methods
|
|
88
|
+
- **VALIDATE** inputs
|
|
89
|
+
- **HANDLE** errors gracefully
|
|
90
|
+
- **COMMENT** complex content
|
|
91
|
+
- **TEST** content structures
|
|
92
|
+
|
|
93
|
+
## 📦 Available Class
|
|
94
|
+
|
|
95
|
+
### ContentBuilder
|
|
96
|
+
|
|
97
|
+
**Refer to**: [`ContentBuilder.ts`](./ContentBuilder.ts)
|
|
98
|
+
|
|
99
|
+
**Methods:**
|
|
100
|
+
- `buildContent(role, parts)` - Build GeminiContent object
|
|
101
|
+
- `buildTextPart(text)` - Build text part
|
|
102
|
+
- `buildImageDataPart(mimeType, data)` - Build image data part
|
|
103
|
+
- `buildFileDataPart(mimeType, fileUri)` - Build file data part
|
|
104
|
+
|
|
105
|
+
## 🔗 Related Modules
|
|
106
|
+
|
|
107
|
+
- **Domain Entities**: [`../../domain/entities/README.md`](../../domain/entities/README.md)
|
|
108
|
+
- **Text Generation**: [`../services/TEXT_GENERATION_SERVICE.md`](../services/TEXT_GENERATION_SERVICE.md)
|
|
109
|
+
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
110
|
+
|
|
111
|
+
## 📋 Supported MIME Types
|
|
112
|
+
|
|
113
|
+
### Image Formats
|
|
114
|
+
- `image/png` - PNG images
|
|
115
|
+
- `image/jpeg` - JPEG images
|
|
116
|
+
- `image/webp` - WebP images
|
|
117
|
+
- `image/heic` - HEIC images
|
|
118
|
+
- `image/heif` - HEIF images
|
|
119
|
+
|
|
120
|
+
### Document Formats
|
|
121
|
+
- `application/pdf` - PDF documents
|
|
122
|
+
- `text/plain` - Plain text
|
|
123
|
+
- `text/html` - HTML documents
|
|
124
|
+
- `application/json` - JSON data
|
|
125
|
+
|
|
126
|
+
## 🎓 Usage Patterns
|
|
127
|
+
|
|
128
|
+
### Basic Text Content
|
|
129
|
+
1. Create ContentBuilder instance
|
|
130
|
+
2. Build text part
|
|
131
|
+
3. Create content with role
|
|
132
|
+
4. Use in service call
|
|
133
|
+
5. Handle response
|
|
134
|
+
|
|
135
|
+
### Multimodal Content
|
|
136
|
+
1. Create ContentBuilder instance
|
|
137
|
+
2. Build text part
|
|
138
|
+
3. Build image data parts
|
|
139
|
+
4. Combine all parts
|
|
140
|
+
5. Create content with role
|
|
141
|
+
|
|
142
|
+
### Chat Conversation
|
|
143
|
+
1. Create ContentBuilder instance
|
|
144
|
+
2. Build user message content
|
|
145
|
+
3. Build model response content
|
|
146
|
+
4. Repeat for conversation
|
|
147
|
+
5. Return content array
|
|
148
|
+
|
|
149
|
+
### File-Based Content
|
|
150
|
+
1. Create ContentBuilder instance
|
|
151
|
+
2. Build text instruction
|
|
152
|
+
3. Build file data part
|
|
153
|
+
4. Create content with role
|
|
154
|
+
5. Process file
|
|
155
|
+
|
|
156
|
+
## 🚨 Common Pitfalls
|
|
157
|
+
|
|
158
|
+
### Don't
|
|
159
|
+
- Create content objects manually
|
|
160
|
+
- Skip role specification
|
|
161
|
+
- Use invalid MIME types
|
|
162
|
+
- Forget to validate content
|
|
163
|
+
- Mix up part order
|
|
164
|
+
|
|
165
|
+
### Do
|
|
166
|
+
- Use ContentBuilder for all content
|
|
167
|
+
- Always specify role
|
|
168
|
+
- Validate MIME types
|
|
169
|
+
- Test content structure
|
|
170
|
+
- Follow API format
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
**Last Updated**: 2025-01-08
|
|
175
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|