@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,194 @@
|
|
|
1
|
+
# Job Manager
|
|
2
|
+
|
|
3
|
+
Async job submission, tracking, and status management service. Manages long-running operations with polling capability.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
JobManager,
|
|
10
|
+
AIJobStatusType
|
|
11
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 🎯 Purpose
|
|
15
|
+
|
|
16
|
+
Use job manager to handle async operations that take time to complete. Provides job lifecycle management, status tracking, and result retrieval.
|
|
17
|
+
|
|
18
|
+
**When to use:**
|
|
19
|
+
- Submit long-running AI operations
|
|
20
|
+
- Track job status over time
|
|
21
|
+
- Implement polling for completion
|
|
22
|
+
- Manage async job lifecycles
|
|
23
|
+
- Handle batch processing
|
|
24
|
+
- Monitor job progress
|
|
25
|
+
- Retrieve results when ready
|
|
26
|
+
|
|
27
|
+
## 📌 Strategy
|
|
28
|
+
|
|
29
|
+
Job manager abstracts async operation complexity. This system:
|
|
30
|
+
- Generates unique job IDs
|
|
31
|
+
- Tracks job status through lifecycle
|
|
32
|
+
- Stores results for retrieval
|
|
33
|
+
- Handles errors gracefully
|
|
34
|
+
- Supports status polling
|
|
35
|
+
- Manages job completion
|
|
36
|
+
|
|
37
|
+
**Key Decision**: Use job manager for video generation and other long-running operations. Poll for status instead of blocking.
|
|
38
|
+
|
|
39
|
+
## ⚠️ Rules
|
|
40
|
+
|
|
41
|
+
### Usage Rules
|
|
42
|
+
- **MUST** save job ID after submission
|
|
43
|
+
- **SHOULD** check job status before getting result
|
|
44
|
+
- **MUST** handle job completion and failure
|
|
45
|
+
- **SHOULD** implement polling for long operations
|
|
46
|
+
- **MUST NOT** lose job IDs
|
|
47
|
+
|
|
48
|
+
### Job Lifecycle Rules
|
|
49
|
+
- **JOBS** start as IN_QUEUE
|
|
50
|
+
- **MUST** update status through lifecycle
|
|
51
|
+
- **SHOULD** set result or error on completion
|
|
52
|
+
- **MUST** handle all status transitions
|
|
53
|
+
- **SHOULD** clean up completed jobs
|
|
54
|
+
|
|
55
|
+
### Error Handling Rules
|
|
56
|
+
- **MUST** set job error on failure
|
|
57
|
+
- **SHOULD** propagate errors to callers
|
|
58
|
+
- **MUST** handle timeout scenarios
|
|
59
|
+
- **SHOULD** log job failures
|
|
60
|
+
- **MUST NOT** leave jobs in invalid state
|
|
61
|
+
|
|
62
|
+
### Polling Rules
|
|
63
|
+
- **SHOULD** poll with appropriate intervals
|
|
64
|
+
- **MUST** implement timeout for polling
|
|
65
|
+
- **SHOULD** handle polling errors
|
|
66
|
+
- **MUST** stop polling on completion
|
|
67
|
+
- **SHOULD NOT** poll too frequently
|
|
68
|
+
|
|
69
|
+
## 🤖 AI Agent Guidelines
|
|
70
|
+
|
|
71
|
+
### When Submitting Jobs
|
|
72
|
+
1. **CALL** submitJob() with model and input
|
|
73
|
+
2. **SAVE** returned job ID
|
|
74
|
+
3. **TRACK** job status
|
|
75
|
+
4. **HANDLE** completion/error
|
|
76
|
+
5. **RETRIEVE** result when ready
|
|
77
|
+
|
|
78
|
+
### When Polling Jobs
|
|
79
|
+
1. **IMPLEMENT** polling loop with timeout
|
|
80
|
+
2. **CHECK** job status each iteration
|
|
81
|
+
3. **WAIT** between polls (exponential backoff)
|
|
82
|
+
4. **RETURN** result on completion
|
|
83
|
+
5. **THROW** on timeout or failure
|
|
84
|
+
|
|
85
|
+
### When Managing Job Lifecycle
|
|
86
|
+
1. **UPDATE** status appropriately
|
|
87
|
+
2. **SET** result on success
|
|
88
|
+
3. **SET** error on failure
|
|
89
|
+
4. **VERIFY** state transitions
|
|
90
|
+
5. **CLEAN UP** old jobs
|
|
91
|
+
|
|
92
|
+
### Code Style Rules
|
|
93
|
+
- **USE** descriptive job inputs
|
|
94
|
+
- **HANDLE** all job status cases
|
|
95
|
+
- **IMPLEMENT** polling with timeout
|
|
96
|
+
- **LOG** job state changes
|
|
97
|
+
- **VALIDATE** job IDs
|
|
98
|
+
|
|
99
|
+
## 📦 Available Classes
|
|
100
|
+
|
|
101
|
+
### JobManager
|
|
102
|
+
|
|
103
|
+
**Refer to**: [`JobManager.ts`](./JobManager.ts)
|
|
104
|
+
|
|
105
|
+
**Methods:**
|
|
106
|
+
- `submitJob(model, input)` - Submit new job
|
|
107
|
+
- `getJobStatus(requestId)` - Get job status
|
|
108
|
+
- `getJobResult(requestId)` - Get job result
|
|
109
|
+
- `updateJobStatus(requestId, status)` - Update job status
|
|
110
|
+
- `setJobResult(requestId, result)` - Set job result
|
|
111
|
+
- `setJobError(requestId, error)` - Set job error
|
|
112
|
+
- `getJob(requestId)` - Get job details
|
|
113
|
+
- `clear()` - Clear all jobs
|
|
114
|
+
|
|
115
|
+
## 🔗 Related Modules
|
|
116
|
+
|
|
117
|
+
- **Video Generation Service**: [`../services/VIDEO_GENERATION_SERVICE.md`](../services/VIDEO_GENERATION_SERVICE.md)
|
|
118
|
+
- **Services README**: [`../services/README.md`](../services/README.md)
|
|
119
|
+
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
120
|
+
|
|
121
|
+
## 📋 Job Status Types
|
|
122
|
+
|
|
123
|
+
### IN_QUEUE
|
|
124
|
+
Job is queued, waiting to start.
|
|
125
|
+
|
|
126
|
+
### PROCESSING
|
|
127
|
+
Job is currently being processed.
|
|
128
|
+
|
|
129
|
+
### COMPLETED
|
|
130
|
+
Job finished successfully with result.
|
|
131
|
+
|
|
132
|
+
### FAILED
|
|
133
|
+
Job failed with error.
|
|
134
|
+
|
|
135
|
+
### CANCELLED
|
|
136
|
+
Job was cancelled before completion.
|
|
137
|
+
|
|
138
|
+
## 🎓 Usage Patterns
|
|
139
|
+
|
|
140
|
+
### Basic Job Submission
|
|
141
|
+
1. Create JobManager instance
|
|
142
|
+
2. Call submitJob() with model and input
|
|
143
|
+
3. Store returned job ID
|
|
144
|
+
4. Monitor job status
|
|
145
|
+
5. Retrieve result when complete
|
|
146
|
+
|
|
147
|
+
### Status Polling
|
|
148
|
+
1. Submit job and get ID
|
|
149
|
+
2. Poll job status periodically
|
|
150
|
+
3. Check for COMPLETED or FAILED
|
|
151
|
+
4. Implement timeout
|
|
152
|
+
5. Return result or throw error
|
|
153
|
+
|
|
154
|
+
### Batch Processing
|
|
155
|
+
1. Submit multiple jobs
|
|
156
|
+
2. Store all job IDs
|
|
157
|
+
3. Poll all jobs in parallel
|
|
158
|
+
4. Collect all results
|
|
159
|
+
5. Return batch results
|
|
160
|
+
|
|
161
|
+
### Job Lifecycle Management
|
|
162
|
+
1. Submit job to manager
|
|
163
|
+
2. Update status to PROCESSING
|
|
164
|
+
3. Perform operation
|
|
165
|
+
4. Set result or error
|
|
166
|
+
5. Handle completion
|
|
167
|
+
|
|
168
|
+
### Error Recovery
|
|
169
|
+
1. Submit job with retry logic
|
|
170
|
+
2. Monitor job status
|
|
171
|
+
3. On failure, check error type
|
|
172
|
+
4. Retry if appropriate
|
|
173
|
+
5. Handle permanent failures
|
|
174
|
+
|
|
175
|
+
## 🚨 Common Pitfalls
|
|
176
|
+
|
|
177
|
+
### Don't
|
|
178
|
+
- Lose job ID after submission
|
|
179
|
+
- Call getJobResult() without checking status
|
|
180
|
+
- Poll too frequently
|
|
181
|
+
- Forget to implement timeout
|
|
182
|
+
- Leave completed jobs in queue
|
|
183
|
+
|
|
184
|
+
### Do
|
|
185
|
+
- Always save job ID
|
|
186
|
+
- Check status before getting result
|
|
187
|
+
- Use exponential backoff for polling
|
|
188
|
+
- Implement timeout for polling
|
|
189
|
+
- Clean up completed jobs
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
**Last Updated**: 2025-01-08
|
|
194
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# Response Module
|
|
2
|
+
|
|
3
|
+
Response formatting and transformation utilities for Gemini API responses. Provides consistent response structures.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import {
|
|
9
|
+
ResponseFormatter
|
|
10
|
+
} from '@umituz/react-native-ai-gemini-provider';
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 🎯 Purpose
|
|
14
|
+
|
|
15
|
+
Use response module to format and transform Gemini API responses consistently. Extracts text and image data from responses.
|
|
16
|
+
|
|
17
|
+
**When to use:**
|
|
18
|
+
- Format API responses consistently
|
|
19
|
+
- Extract text from responses
|
|
20
|
+
- Extract image data from multimodal responses
|
|
21
|
+
- Normalize response structures
|
|
22
|
+
- Handle response types safely
|
|
23
|
+
|
|
24
|
+
## 📌 Strategy
|
|
25
|
+
|
|
26
|
+
Response formatting ensures consistency. This module:
|
|
27
|
+
- Extracts text content automatically
|
|
28
|
+
- Handles image data in responses
|
|
29
|
+
- Provides typed response structures
|
|
30
|
+
- Preserves original response
|
|
31
|
+
- Simplifies response handling
|
|
32
|
+
|
|
33
|
+
**Key Decision**: Always format responses before using. Provides consistent interface for different response types.
|
|
34
|
+
|
|
35
|
+
## ⚠️ Rules
|
|
36
|
+
|
|
37
|
+
### Formatting Rules
|
|
38
|
+
- **MUST** format all API responses
|
|
39
|
+
- **SHOULD** specify expected response type
|
|
40
|
+
- **MUST** check for optional fields
|
|
41
|
+
- **SHOULD** preserve original response
|
|
42
|
+
- **MUST NOT** assume response structure
|
|
43
|
+
|
|
44
|
+
### Type Safety Rules
|
|
45
|
+
- **SHOULD** use generic type parameter
|
|
46
|
+
- **MUST** check for optional fields
|
|
47
|
+
- **SHOULD** use type guards
|
|
48
|
+
- **MUST** handle undefined values
|
|
49
|
+
- **SHOULD NOT** use non-null assertion
|
|
50
|
+
|
|
51
|
+
### Data Extraction Rules
|
|
52
|
+
- **MUST** handle missing text gracefully
|
|
53
|
+
- **SHOULD** validate image data
|
|
54
|
+
- **MUST** check for image presence
|
|
55
|
+
- **SHOULD** extract MIME type correctly
|
|
56
|
+
- **MUST NOT** fail on partial data
|
|
57
|
+
|
|
58
|
+
### Error Handling Rules
|
|
59
|
+
- **SHOULD** wrap formatting in try-catch
|
|
60
|
+
- **MUST** handle malformed responses
|
|
61
|
+
- **SHOULD** log formatting errors
|
|
62
|
+
- **MUST** provide fallback values
|
|
63
|
+
- **SHOULD NOT** throw formatting errors
|
|
64
|
+
|
|
65
|
+
## 🤖 AI Agent Guidelines
|
|
66
|
+
|
|
67
|
+
### When Formatting Responses
|
|
68
|
+
1. **CREATE** ResponseFormatter instance
|
|
69
|
+
2. **CALL** formatResponse() with type
|
|
70
|
+
3. **CHECK** for optional fields
|
|
71
|
+
4. **EXTRACT** needed data
|
|
72
|
+
5. **HANDLE** missing data gracefully
|
|
73
|
+
|
|
74
|
+
### When Handling Text Responses
|
|
75
|
+
1. **FORMAT** response with text type
|
|
76
|
+
2. **CHECK** text field exists
|
|
77
|
+
3. **VALIDATE** text not empty
|
|
78
|
+
4. **USE** text value
|
|
79
|
+
5. **HANDLE** empty text case
|
|
80
|
+
|
|
81
|
+
### When Handling Image Responses
|
|
82
|
+
1. **FORMAT** response with image type
|
|
83
|
+
2. **CHECK** imageUrl exists
|
|
84
|
+
3. **EXTRACT** imageBase64 if needed
|
|
85
|
+
4. **USE** MIME type correctly
|
|
86
|
+
5. **HANDLE** missing image case
|
|
87
|
+
|
|
88
|
+
### Code Style Rules
|
|
89
|
+
- **USE** generic type parameter
|
|
90
|
+
- **CHECK** optional fields before use
|
|
91
|
+
- **PROVIDE** fallback values
|
|
92
|
+
- **LOG** formatting issues
|
|
93
|
+
- **PRESERVE** original response
|
|
94
|
+
|
|
95
|
+
## 📦 Available Classes
|
|
96
|
+
|
|
97
|
+
### ResponseFormatter
|
|
98
|
+
|
|
99
|
+
**Refer to**: [`ResponseFormatter.ts`](./ResponseFormatter.ts)
|
|
100
|
+
|
|
101
|
+
**Methods:**
|
|
102
|
+
- `formatResponse<T>(response, input)` - Format response to typed structure
|
|
103
|
+
|
|
104
|
+
## 🔗 Related Modules
|
|
105
|
+
|
|
106
|
+
- **Text Generation**: [`../services/TEXT_GENERATION_SERVICE.md`](../services/TEXT_GENERATION_SERVICE.md)
|
|
107
|
+
- **Image Generation**: [`../services/IMAGE_GENERATION_SERVICE.md`](../services/IMAGE_GENERATION_SERVICE.md)
|
|
108
|
+
- **Data Transformer**: [`../utils/DATA_TRANSFORMER_UTILS.md`](../utils/DATA_TRANSFORMER_UTILS.md)
|
|
109
|
+
|
|
110
|
+
## 📋 Response Types
|
|
111
|
+
|
|
112
|
+
### Text Response
|
|
113
|
+
```typescript
|
|
114
|
+
{
|
|
115
|
+
text: string; // Extracted text content
|
|
116
|
+
response: unknown; // Original API response
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Multimodal Response
|
|
121
|
+
```typescript
|
|
122
|
+
{
|
|
123
|
+
text: string; // Extracted text content
|
|
124
|
+
imageUrl: string; // Data URL (data:image/png;base64,...)
|
|
125
|
+
imageBase64: string; // Base64 image data
|
|
126
|
+
mimeType: string; // Image MIME type
|
|
127
|
+
response: unknown; // Original API response
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## 🎓 Usage Patterns
|
|
132
|
+
|
|
133
|
+
### Basic Formatting
|
|
134
|
+
1. Create ResponseFormatter instance
|
|
135
|
+
2. Call formatResponse() with type
|
|
136
|
+
3. Extract needed fields
|
|
137
|
+
4. Check for optional data
|
|
138
|
+
5. Handle response appropriately
|
|
139
|
+
|
|
140
|
+
### Text Response Handling
|
|
141
|
+
1. Format response as text type
|
|
142
|
+
2. Check text field exists
|
|
143
|
+
3. Validate text content
|
|
144
|
+
4. Use text in application
|
|
145
|
+
5. Handle empty text case
|
|
146
|
+
|
|
147
|
+
### Image Response Handling
|
|
148
|
+
1. Format response with image type
|
|
149
|
+
2. Check imageUrl exists
|
|
150
|
+
3. Extract imageBase64 if needed
|
|
151
|
+
4. Use imageUrl for display
|
|
152
|
+
5. Handle missing image case
|
|
153
|
+
|
|
154
|
+
### Multimodal Handling
|
|
155
|
+
1. Format response
|
|
156
|
+
2. Check for both text and image
|
|
157
|
+
3. Handle different response types
|
|
158
|
+
4. Display appropriate content
|
|
159
|
+
5. Fallback to available data
|
|
160
|
+
|
|
161
|
+
### Type-Safe Formatting
|
|
162
|
+
1. Define response interface
|
|
163
|
+
2. Format with generic type
|
|
164
|
+
3. Use type guards
|
|
165
|
+
4. Narrow response type
|
|
166
|
+
5. Use typed data safely
|
|
167
|
+
|
|
168
|
+
## 🚨 Common Pitfalls
|
|
169
|
+
|
|
170
|
+
### Don't
|
|
171
|
+
- Assume text field always exists
|
|
172
|
+
- Use imageUrl without checking
|
|
173
|
+
- Discard original response
|
|
174
|
+
- Use non-null assertions
|
|
175
|
+
- Skip type checking
|
|
176
|
+
|
|
177
|
+
### Do
|
|
178
|
+
- Always check optional fields
|
|
179
|
+
- Validate data before use
|
|
180
|
+
- Preserve original response
|
|
181
|
+
- Use type guards
|
|
182
|
+
- Handle missing data gracefully
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
**Last Updated**: 2025-01-08
|
|
187
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# Response Formatter
|
|
2
|
+
|
|
3
|
+
Utility for formatting Gemini API responses into consistent output structures. Handles text and image data extraction from API responses.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { ResponseFormatter } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use ResponseFormatter to convert raw Gemini API responses into consistent, typed structures. Extracts text and image data automatically.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Format API responses consistently
|
|
17
|
+
- Extract text from responses
|
|
18
|
+
- Extract image data from multimodal responses
|
|
19
|
+
- Normalize response structures
|
|
20
|
+
- Handle response types safely
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
Response formatting ensures consistency. This utility:
|
|
25
|
+
- Extracts text content automatically
|
|
26
|
+
- Handles image data in responses
|
|
27
|
+
- Provides typed response structures
|
|
28
|
+
- Preserves original response
|
|
29
|
+
- Simplifies response handling
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Always format responses before using. Provides consistent interface for different response types.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Formatting Rules
|
|
36
|
+
- **MUST** format all API responses
|
|
37
|
+
- **SHOULD** specify expected response type
|
|
38
|
+
- **MUST** check for optional fields
|
|
39
|
+
- **SHOULD** preserve original response
|
|
40
|
+
- **MUST NOT** assume response structure
|
|
41
|
+
|
|
42
|
+
### Type Safety Rules
|
|
43
|
+
- **SHOULD** use generic type parameter
|
|
44
|
+
- **MUST** check for optional fields
|
|
45
|
+
- **SHOULD** use type guards
|
|
46
|
+
- **MUST** handle undefined values
|
|
47
|
+
- **SHOULD NOT** use non-null assertion
|
|
48
|
+
|
|
49
|
+
### Data Extraction Rules
|
|
50
|
+
- **MUST** handle missing text gracefully
|
|
51
|
+
- **SHOULD** validate image data
|
|
52
|
+
- **MUST** check for image presence
|
|
53
|
+
- **SHOULD** extract MIME type correctly
|
|
54
|
+
- **MUST NOT** fail on partial data
|
|
55
|
+
|
|
56
|
+
### Error Handling Rules
|
|
57
|
+
- **SHOULD** wrap formatting in try-catch
|
|
58
|
+
- **MUST** handle malformed responses
|
|
59
|
+
- **SHOULD** log formatting errors
|
|
60
|
+
- **MUST** provide fallback values
|
|
61
|
+
- **SHOULD NOT** throw formatting errors
|
|
62
|
+
|
|
63
|
+
## 🤖 AI Agent Guidelines
|
|
64
|
+
|
|
65
|
+
### When Formatting Responses
|
|
66
|
+
1. **CREATE** ResponseFormatter instance
|
|
67
|
+
2. **CALL** formatResponse() with type
|
|
68
|
+
3. **CHECK** for optional fields
|
|
69
|
+
4. **EXTRACT** needed data
|
|
70
|
+
5. **HANDLE** missing data gracefully
|
|
71
|
+
|
|
72
|
+
### When Handling Text Responses
|
|
73
|
+
1. **FORMAT** response with text type
|
|
74
|
+
2. **CHECK** text field exists
|
|
75
|
+
3. **VALIDATE** text not empty
|
|
76
|
+
4. **USE** text value
|
|
77
|
+
5. **HANDLE** empty text case
|
|
78
|
+
|
|
79
|
+
### When Handling Image Responses
|
|
80
|
+
1. **FORMAT** response with image type
|
|
81
|
+
2. **CHECK** imageUrl exists
|
|
82
|
+
3. **EXTRACT** imageBase64 data
|
|
83
|
+
4. **USE** MIME type correctly
|
|
84
|
+
5. **HANDLE** missing image case
|
|
85
|
+
|
|
86
|
+
### Code Style Rules
|
|
87
|
+
- **USE** generic type parameter
|
|
88
|
+
- **CHECK** optional fields before use
|
|
89
|
+
- **PROVIDE** fallback values
|
|
90
|
+
- **LOG** formatting issues
|
|
91
|
+
- **PRESERVE** original response
|
|
92
|
+
|
|
93
|
+
## 📦 Available Class
|
|
94
|
+
|
|
95
|
+
### ResponseFormatter
|
|
96
|
+
|
|
97
|
+
**Refer to**: [`ResponseFormatter.ts`](./ResponseFormatter.ts)
|
|
98
|
+
|
|
99
|
+
**Methods:**
|
|
100
|
+
- `formatResponse<T>(response, input)` - Format response to typed structure
|
|
101
|
+
|
|
102
|
+
## 🔗 Related Modules
|
|
103
|
+
|
|
104
|
+
- **Text Generation Service**: [`../services/TEXT_GENERATION_SERVICE.md`](../services/TEXT_GENERATION_SERVICE.md)
|
|
105
|
+
- **Image Generation Service**: [`../services/IMAGE_GENERATION_SERVICE.md`](../services/IMAGE_GENERATION_SERVICE.md)
|
|
106
|
+
- **Generation Executor**: [`../services/GENERATION_EXECUTOR_SERVICE.md`](../services/GENERATION_EXECUTOR_SERVICE.md)
|
|
107
|
+
|
|
108
|
+
## 📋 Response Types
|
|
109
|
+
|
|
110
|
+
### Text Response
|
|
111
|
+
```typescript
|
|
112
|
+
{
|
|
113
|
+
text: string; // Extracted text content
|
|
114
|
+
response: unknown; // Original API response
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Multimodal Response
|
|
119
|
+
```typescript
|
|
120
|
+
{
|
|
121
|
+
text: string; // Extracted text content
|
|
122
|
+
imageUrl: string; // Data URL (data:image/png;base64,...)
|
|
123
|
+
imageBase64: string; // Base64 image data
|
|
124
|
+
mimeType: string; // Image MIME type
|
|
125
|
+
response: unknown; // Original API response
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## 🎓 Usage Patterns
|
|
130
|
+
|
|
131
|
+
### Basic Formatting
|
|
132
|
+
1. Create ResponseFormatter instance
|
|
133
|
+
2. Call formatResponse() with type
|
|
134
|
+
3. Extract needed fields
|
|
135
|
+
4. Check for optional data
|
|
136
|
+
5. Handle response appropriately
|
|
137
|
+
|
|
138
|
+
### Text Response Handling
|
|
139
|
+
1. Format response as text type
|
|
140
|
+
2. Check text field exists
|
|
141
|
+
3. Validate text content
|
|
142
|
+
4. Use text in application
|
|
143
|
+
5. Handle empty text case
|
|
144
|
+
|
|
145
|
+
### Image Response Handling
|
|
146
|
+
1. Format response with image type
|
|
147
|
+
2. Check imageUrl exists
|
|
148
|
+
3. Extract imageBase64 if needed
|
|
149
|
+
4. Use imageUrl for display
|
|
150
|
+
5. Handle missing image case
|
|
151
|
+
|
|
152
|
+
### Multimodal Handling
|
|
153
|
+
1. Format response
|
|
154
|
+
2. Check for both text and image
|
|
155
|
+
3. Handle different response types
|
|
156
|
+
4. Display appropriate content
|
|
157
|
+
5. Fallback to available data
|
|
158
|
+
|
|
159
|
+
### Type-Safe Formatting
|
|
160
|
+
1. Define response interface
|
|
161
|
+
2. Format with generic type
|
|
162
|
+
3. Use type guards
|
|
163
|
+
4. Narrow response type
|
|
164
|
+
5. Use typed data safely
|
|
165
|
+
|
|
166
|
+
## 🚨 Common Pitfalls
|
|
167
|
+
|
|
168
|
+
### Don't
|
|
169
|
+
- Assume text field always exists
|
|
170
|
+
- Use imageUrl without checking
|
|
171
|
+
- Discard original response
|
|
172
|
+
- Use non-null assertions
|
|
173
|
+
- Skip type checking
|
|
174
|
+
|
|
175
|
+
### Do
|
|
176
|
+
- Always check optional fields
|
|
177
|
+
- Validate data before use
|
|
178
|
+
- Preserve original response
|
|
179
|
+
- Use type guards
|
|
180
|
+
- Handle missing data gracefully
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
**Last Updated**: 2025-01-08
|
|
185
|
+
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|