@umituz/react-native-ai-gemini-provider 1.14.28 → 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,178 @@
|
|
|
1
|
+
# Retry Service
|
|
2
|
+
|
|
3
|
+
Automatic retry mechanism with exponential backoff strategy. Handles transient errors gracefully.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { geminiRetryService } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use this service to automatically retry failed operations with intelligent backoff. Handles network issues, rate limits, and transient server errors.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Wrap API calls that may fail transiently
|
|
17
|
+
- Handle rate limiting automatically
|
|
18
|
+
- Improve reliability of network operations
|
|
19
|
+
- Add resilience to critical operations
|
|
20
|
+
- Track retry attempts for monitoring
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
Not all errors should be retried. This service:
|
|
25
|
+
- Uses exponential backoff for delays
|
|
26
|
+
- Retries only transient errors (network, rate limit, server errors)
|
|
27
|
+
- Fails fast for permanent errors (auth, validation, safety)
|
|
28
|
+
- Provides retry callbacks for monitoring
|
|
29
|
+
- Configurable retry limits and delays
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Only retry errors that might succeed on retry (429 rate limits, 5xx server errors, network issues). Never retry validation or authentication errors.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Usage Rules
|
|
36
|
+
- **MUST** wrap only retryable operations
|
|
37
|
+
- **SHOULD** configure appropriate max retries
|
|
38
|
+
- **MUST** handle final failure after all retries
|
|
39
|
+
- **SHOULD** use retry callbacks for monitoring
|
|
40
|
+
- **MUST NOT** retry non-idempotent operations without care
|
|
41
|
+
|
|
42
|
+
### Configuration Rules
|
|
43
|
+
- **MUST** set maxRetries appropriately (default: 3)
|
|
44
|
+
- **SHOULD** configure baseDelay for use case
|
|
45
|
+
- **MUST** set maxDelay to prevent excessive waits
|
|
46
|
+
- **SHOULD** use onRetry callback for logging
|
|
47
|
+
|
|
48
|
+
### Retryable Errors
|
|
49
|
+
- **WILL RETRY**: QUOTA_EXCEEDED, RATE_LIMIT, NETWORK, TIMEOUT, SERVER (5xx)
|
|
50
|
+
- **WILL NOT RETRY**: AUTHENTICATION, SAFETY, VALIDATION, MODEL_NOT_FOUND
|
|
51
|
+
|
|
52
|
+
### Error Handling Rules
|
|
53
|
+
- **MUST** catch and handle final errors
|
|
54
|
+
- **SHOULD** log retry attempts
|
|
55
|
+
- **MUST** differentiate retryable vs non-retryable errors
|
|
56
|
+
- **SHOULD** provide user feedback during retries
|
|
57
|
+
|
|
58
|
+
## 🤖 AI Agent Guidelines
|
|
59
|
+
|
|
60
|
+
### When Modifying This Service
|
|
61
|
+
1. **READ** the implementation file first
|
|
62
|
+
2. **UNDERSTAND** exponential backoff logic
|
|
63
|
+
3. **MAINTAIN** backward compatibility
|
|
64
|
+
4. **ADD** tests for new functionality
|
|
65
|
+
5. **UPDATE** type definitions
|
|
66
|
+
|
|
67
|
+
### When Adding New Features
|
|
68
|
+
1. **CHECK** if similar feature exists
|
|
69
|
+
2. **FOLLOW** existing patterns
|
|
70
|
+
3. **USE** established error handling
|
|
71
|
+
4. **DOCUMENT** in type definitions
|
|
72
|
+
5. **ADD** examples to tests (not docs)
|
|
73
|
+
|
|
74
|
+
### When Fixing Bugs
|
|
75
|
+
1. **REPRODUCE** bug locally first
|
|
76
|
+
2. **IDENTIFY** root cause
|
|
77
|
+
3. **FIX** with minimal changes
|
|
78
|
+
4. **ADD** regression test
|
|
79
|
+
5. **VERIFY** all tests pass
|
|
80
|
+
|
|
81
|
+
### Code Style Rules
|
|
82
|
+
- **USE** async/await (no callbacks)
|
|
83
|
+
- **VALIDATE** inputs at function entry
|
|
84
|
+
- **THROW** typed errors (`GeminiError`)
|
|
85
|
+
- **LOG** retry attempts
|
|
86
|
+
- **COMMENT** complex logic only
|
|
87
|
+
|
|
88
|
+
## 📦 Available Methods
|
|
89
|
+
|
|
90
|
+
### `executeWithRetry(fn, options?)`
|
|
91
|
+
|
|
92
|
+
Execute function with automatic retry on transient errors.
|
|
93
|
+
|
|
94
|
+
**Refer to**: [`gemini-retry.service.ts`](./gemini-retry.service.ts)
|
|
95
|
+
|
|
96
|
+
## 🔗 Related Modules
|
|
97
|
+
|
|
98
|
+
- **Error Utilities**: [`ERROR_UTILITIES.md`](../infrastructure/utils/ERROR_UTILITIES.md)
|
|
99
|
+
- **Error Mapper**: [`ERROR_MAPPER.md`](../infrastructure/utils/ERROR_MAPPER.md)
|
|
100
|
+
- **Text Generation**: [`TEXT_GENERATION_SERVICE.md`](./TEXT_GENERATION_SERVICE.md)
|
|
101
|
+
|
|
102
|
+
## 📋 Configuration Reference
|
|
103
|
+
|
|
104
|
+
### RetryOptions
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
interface RetryOptions {
|
|
108
|
+
maxRetries?: number; // Maximum retry attempts (default: 3)
|
|
109
|
+
baseDelay?: number; // Initial delay in ms (default: 1000)
|
|
110
|
+
maxDelay?: number; // Maximum delay in ms (default: 10000)
|
|
111
|
+
onRetry?: (attempt: number, error: unknown) => void;
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Retryable Error Types
|
|
116
|
+
- `QUOTA_EXCEEDED`: Temporary quota exceeded
|
|
117
|
+
- `RATE_LIMIT`: Too many requests
|
|
118
|
+
- `NETWORK`: Network connectivity issues
|
|
119
|
+
- `TIMEOUT`: Request timeout
|
|
120
|
+
- `SERVER`: Server errors (5xx)
|
|
121
|
+
|
|
122
|
+
### Non-Retryable Error Types
|
|
123
|
+
- `AUTHENTICATION`: Invalid API key or credentials
|
|
124
|
+
- `SAFETY`: Content safety violation
|
|
125
|
+
- `VALIDATION`: Invalid request parameters
|
|
126
|
+
- `MODEL_NOT_FOUND`: Invalid model ID
|
|
127
|
+
|
|
128
|
+
## 🎓 Usage Patterns
|
|
129
|
+
|
|
130
|
+
### Basic Retry
|
|
131
|
+
1. Import service
|
|
132
|
+
2. Wrap async function in `executeWithRetry()`
|
|
133
|
+
3. Configure max retries and delays
|
|
134
|
+
4. Handle final success or failure
|
|
135
|
+
5. Provide user feedback
|
|
136
|
+
|
|
137
|
+
### With Callbacks
|
|
138
|
+
1. Import service
|
|
139
|
+
2. Configure `onRetry` callback
|
|
140
|
+
3. Log retry attempts for monitoring
|
|
141
|
+
4. Update UI with retry status
|
|
142
|
+
5. Track metrics for analytics
|
|
143
|
+
|
|
144
|
+
### Custom Configuration
|
|
145
|
+
1. Assess operation criticality
|
|
146
|
+
2. Set appropriate maxRetries (1-5)
|
|
147
|
+
3. Configure baseDelay for operation type
|
|
148
|
+
4. Set maxDelay to prevent excessive waits
|
|
149
|
+
5. Test retry behavior
|
|
150
|
+
|
|
151
|
+
### For Critical Operations
|
|
152
|
+
1. Use higher maxRetries (5+)
|
|
153
|
+
2. Use longer baseDelay (2000ms)
|
|
154
|
+
3. Set longer maxDelay (60000ms)
|
|
155
|
+
4. Implement detailed callbacks
|
|
156
|
+
5. Add timeout wrapper
|
|
157
|
+
|
|
158
|
+
## 🚨 Common Pitfalls
|
|
159
|
+
|
|
160
|
+
### Don't
|
|
161
|
+
- Retry authentication failures (won't succeed)
|
|
162
|
+
- Retry validation errors (need input fix)
|
|
163
|
+
- Retry without maxDelay (can wait forever)
|
|
164
|
+
- Retry non-idempotent operations carelessly
|
|
165
|
+
- Ignore retry callbacks (no visibility)
|
|
166
|
+
|
|
167
|
+
### Do
|
|
168
|
+
- Configure retries based on operation type
|
|
169
|
+
- Use callbacks for monitoring and analytics
|
|
170
|
+
- Set maxDelay to prevent excessive waits
|
|
171
|
+
- Handle non-retryable errors appropriately
|
|
172
|
+
- Differentiate transient vs permanent errors
|
|
173
|
+
- Consider timeouts for total operation time
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
**Last Updated**: 2025-01-08
|
|
178
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# Streaming Service
|
|
2
|
+
|
|
3
|
+
Provides real-time streaming for AI text generation. Delivers responses chunk-by-chunk for immediate display.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { geminiStreamingService } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use this service to generate AI text responses with streaming. Provides immediate feedback as content is generated, improving user experience for long responses.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Long text generation (stories, articles)
|
|
17
|
+
- Chat interfaces requiring real-time responses
|
|
18
|
+
- Applications needing immediate user feedback
|
|
19
|
+
- Progressive content display
|
|
20
|
+
- Cancelable operations
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
Streaming dramatically improves UX for long responses. This service:
|
|
25
|
+
- Returns AsyncGenerator for chunk-by-chunk delivery
|
|
26
|
+
- Enables progressive UI updates
|
|
27
|
+
- Supports cancellation mid-generation
|
|
28
|
+
- Handles stream errors gracefully
|
|
29
|
+
- Provides finish reason for completion status
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Use streaming for better UX when generating long responses. Each chunk can be displayed immediately, reducing perceived latency.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Usage Rules
|
|
36
|
+
- **MUST** initialize provider with API key before use
|
|
37
|
+
- **MUST** iterate through async generator properly
|
|
38
|
+
- **SHOULD** update UI with each chunk
|
|
39
|
+
- **MUST** handle stream errors appropriately
|
|
40
|
+
- **SHOULD** implement cancellation support
|
|
41
|
+
|
|
42
|
+
### Stream Handling Rules
|
|
43
|
+
- **MUST** use `for await` to consume stream
|
|
44
|
+
- **SHOULD** buffer very small chunks
|
|
45
|
+
- **MUST** check `finishReason` in final chunk
|
|
46
|
+
- **SHOULD** implement memory limits for buffers
|
|
47
|
+
- **MUST NOT** ignore stream errors
|
|
48
|
+
|
|
49
|
+
### Configuration Rules
|
|
50
|
+
- **MUST** use valid model IDs
|
|
51
|
+
- **SHOULD** configure appropriate generation parameters
|
|
52
|
+
- **MUST** handle safety filters in stream
|
|
53
|
+
- **SHOULD** implement retry logic for failures
|
|
54
|
+
|
|
55
|
+
### Error Handling Rules
|
|
56
|
+
- **MUST** catch and handle `GeminiError`
|
|
57
|
+
- **MUST** handle stream interruption errors
|
|
58
|
+
- **SHOULD** check finish reasons (SAFETY, MAX_TOKENS)
|
|
59
|
+
- **MUST NOT** expose API keys in errors
|
|
60
|
+
|
|
61
|
+
## 🤖 AI Agent Guidelines
|
|
62
|
+
|
|
63
|
+
### When Modifying This Service
|
|
64
|
+
1. **READ** the implementation file first
|
|
65
|
+
2. **UNDERSTAND** async generator pattern
|
|
66
|
+
3. **MAINTAIN** backward compatibility
|
|
67
|
+
4. **ADD** tests for new functionality
|
|
68
|
+
5. **UPDATE** type definitions
|
|
69
|
+
|
|
70
|
+
### When Adding New Features
|
|
71
|
+
1. **CHECK** if similar feature exists in streaming/text services
|
|
72
|
+
2. **FOLLOW** existing patterns
|
|
73
|
+
3. **USE** established error handling
|
|
74
|
+
4. **DOCUMENT** in type definitions
|
|
75
|
+
5. **ADD** examples to tests (not docs)
|
|
76
|
+
|
|
77
|
+
### When Fixing Bugs
|
|
78
|
+
1. **REPRODUCE** bug locally first
|
|
79
|
+
2. **IDENTIFY** root cause
|
|
80
|
+
3. **FIX** with minimal changes
|
|
81
|
+
4. **ADD** regression test
|
|
82
|
+
5. **VERIFY** all tests pass
|
|
83
|
+
|
|
84
|
+
### Code Style Rules
|
|
85
|
+
- **USE** async/await and `for await`
|
|
86
|
+
- **VALIDATE** inputs at function entry
|
|
87
|
+
- **THROW** typed errors (`GeminiError`)
|
|
88
|
+
- **LOG** important operations
|
|
89
|
+
- **COMMENT** complex logic only
|
|
90
|
+
|
|
91
|
+
## 📦 Available Methods
|
|
92
|
+
|
|
93
|
+
### `streamText(model, prompt, config?)`
|
|
94
|
+
|
|
95
|
+
Stream text generation chunk-by-chunk.
|
|
96
|
+
|
|
97
|
+
**Refer to**: [`gemini-streaming.service.ts`](./gemini-streaming.service.ts)
|
|
98
|
+
|
|
99
|
+
## 🔗 Related Modules
|
|
100
|
+
|
|
101
|
+
- **Domain Types**: [`domain/entities/README.md`](../domain/entities/README.md)
|
|
102
|
+
- **Text Generation**: [`TEXT_GENERATION_SERVICE.md`](./TEXT_GENERATION_SERVICE.md)
|
|
103
|
+
- **Core Client**: [`CORE_CLIENT_SERVICE.md`](./CORE_CLIENT_SERVICE.md)
|
|
104
|
+
|
|
105
|
+
## 📋 Configuration Reference
|
|
106
|
+
|
|
107
|
+
### Generation Config
|
|
108
|
+
See: [`domain/entities/README.md`](../domain/entities/README.md)
|
|
109
|
+
|
|
110
|
+
### Model Selection
|
|
111
|
+
See: [`FEATURE_MODEL_SELECTOR_SERVICE.md`](./FEATURE_MODEL_SELECTOR_SERVICE.md)
|
|
112
|
+
|
|
113
|
+
### Error Types
|
|
114
|
+
See: [`ERROR_UTILITIES.md`](../infrastructure/utils/ERROR_UTILITIES.md)
|
|
115
|
+
|
|
116
|
+
## 🎓 Usage Patterns
|
|
117
|
+
|
|
118
|
+
### Basic Streaming
|
|
119
|
+
1. Import service
|
|
120
|
+
2. Call `streamText()` with model and prompt
|
|
121
|
+
3. Use `for await` to iterate through chunks
|
|
122
|
+
4. Append each chunk to displayed text
|
|
123
|
+
5. Handle completion or errors
|
|
124
|
+
|
|
125
|
+
### With UI Updates
|
|
126
|
+
1. Create state for streamed text
|
|
127
|
+
2. Start streaming with `streamText()`
|
|
128
|
+
3. Update state with each chunk
|
|
129
|
+
4. Display progress to user
|
|
130
|
+
5. Handle final chunk and finish reason
|
|
131
|
+
|
|
132
|
+
### With Cancellation
|
|
133
|
+
1. Start streaming operation
|
|
134
|
+
2. Track cancellation flag
|
|
135
|
+
3. Check flag in stream loop
|
|
136
|
+
4. Break loop if cancelled
|
|
137
|
+
5. Handle cleanup appropriately
|
|
138
|
+
|
|
139
|
+
### With Error Handling
|
|
140
|
+
1. Wrap stream in try-catch
|
|
141
|
+
2. Check `finishReason` in chunks
|
|
142
|
+
3. Handle SAFETY, MAX_TOKENS reasons
|
|
143
|
+
4. Catch network and timeout errors
|
|
144
|
+
5. Provide user feedback
|
|
145
|
+
|
|
146
|
+
## 🚨 Common Pitfalls
|
|
147
|
+
|
|
148
|
+
### Don't
|
|
149
|
+
- Buffer all chunks before displaying (defeats streaming purpose)
|
|
150
|
+
- Ignore finish reasons in final chunk
|
|
151
|
+
- Forget error handling in stream loop
|
|
152
|
+
- Create memory leaks with unbounded buffers
|
|
153
|
+
- Assume streaming is always faster
|
|
154
|
+
|
|
155
|
+
### Do
|
|
156
|
+
- Update UI with each chunk
|
|
157
|
+
- Check finishReason for completion status
|
|
158
|
+
- Handle stream interruption gracefully
|
|
159
|
+
- Implement buffer size limits
|
|
160
|
+
- Use cancellation for long operations
|
|
161
|
+
- Provide user feedback during streaming
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
**Last Updated**: 2025-01-08
|
|
166
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Structured Text Generation Service
|
|
2
|
+
|
|
3
|
+
Generates structured JSON output with schema validation. Produces type-safe content with defined structure.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { geminiStructuredTextService } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use this service to generate AI responses in structured JSON format with type safety and schema validation.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Generate JSON with specific structure
|
|
17
|
+
- Create type-safe AI responses
|
|
18
|
+
- Extract structured data from text
|
|
19
|
+
- Build forms and data entry with AI
|
|
20
|
+
- Generate configuration objects
|
|
21
|
+
|
|
22
|
+
## 📌 Strategy
|
|
23
|
+
|
|
24
|
+
Structured generation ensures type safety and validates output. This service:
|
|
25
|
+
- Accepts JSON schema for output validation
|
|
26
|
+
- Returns typed responses matching schema
|
|
27
|
+
- Validates AI-generated JSON structure
|
|
28
|
+
- Provides type-safe interfaces
|
|
29
|
+
- Handles parsing and validation errors
|
|
30
|
+
|
|
31
|
+
**Key Decision**: Use JSON Schema for validation. The service validates AI output against the schema and returns typed results, ensuring data integrity.
|
|
32
|
+
|
|
33
|
+
## ⚠️ Rules
|
|
34
|
+
|
|
35
|
+
### Usage Rules
|
|
36
|
+
- **MUST** initialize provider with API key before use
|
|
37
|
+
- **MUST** provide valid JSON schema
|
|
38
|
+
- **SHOULD** define TypeScript interfaces for type safety
|
|
39
|
+
- **MUST** handle schema validation errors
|
|
40
|
+
- **SHOULD** specify required vs optional fields
|
|
41
|
+
|
|
42
|
+
### Schema Rules
|
|
43
|
+
- **MUST** use valid JSON Schema format
|
|
44
|
+
- **SHOULD** include field descriptions
|
|
45
|
+
- **MUST** specify required fields explicitly
|
|
46
|
+
- **SHOULD** use appropriate types (string, number, boolean, array, object)
|
|
47
|
+
- **MUST NOT** create overly complex nested schemas
|
|
48
|
+
|
|
49
|
+
### Error Handling Rules
|
|
50
|
+
- **MUST** catch and handle `GeminiError`
|
|
51
|
+
- **MUST** handle PARSING_ERROR for invalid JSON
|
|
52
|
+
- **MUST** handle VALIDATION errors for schema mismatches
|
|
53
|
+
- **SHOULD** provide fallback values when appropriate
|
|
54
|
+
- **MUST NOT** expose API keys in errors
|
|
55
|
+
|
|
56
|
+
## 🤖 AI Agent Guidelines
|
|
57
|
+
|
|
58
|
+
### When Modifying This Service
|
|
59
|
+
1. **READ** the implementation file first
|
|
60
|
+
2. **UNDERSTAND** schema validation logic
|
|
61
|
+
3. **MAINTAIN** backward compatibility
|
|
62
|
+
4. **ADD** tests for new functionality
|
|
63
|
+
5. **UPDATE** type definitions
|
|
64
|
+
|
|
65
|
+
### When Adding New Features
|
|
66
|
+
1. **CHECK** if similar feature exists
|
|
67
|
+
2. **FOLLOW** existing patterns
|
|
68
|
+
3. **USE** established error handling
|
|
69
|
+
4. **DOCUMENT** in type definitions
|
|
70
|
+
5. **ADD** examples to tests (not docs)
|
|
71
|
+
|
|
72
|
+
### When Fixing Bugs
|
|
73
|
+
1. **REPRODUCE** bug locally first
|
|
74
|
+
2. **IDENTIFY** root cause
|
|
75
|
+
3. **FIX** with minimal changes
|
|
76
|
+
4. **ADD** regression test
|
|
77
|
+
5. **VERIFY** all tests pass
|
|
78
|
+
|
|
79
|
+
### Code Style Rules
|
|
80
|
+
- **USE** async/await (no callbacks)
|
|
81
|
+
- **VALIDATE** inputs at function entry
|
|
82
|
+
- **THROW** typed errors (`GeminiError`)
|
|
83
|
+
- **LOG** important operations
|
|
84
|
+
- **COMMENT** complex logic only
|
|
85
|
+
|
|
86
|
+
## 📦 Available Methods
|
|
87
|
+
|
|
88
|
+
### `generateStructuredText<T>(model, prompt, schema, config?)`
|
|
89
|
+
|
|
90
|
+
Generate structured JSON output based on provided schema.
|
|
91
|
+
|
|
92
|
+
**Refer to**: [`gemini-structured-text.service.ts`](./gemini-structured-text.service.ts)
|
|
93
|
+
|
|
94
|
+
## 🔗 Related Modules
|
|
95
|
+
|
|
96
|
+
- **Domain Types**: [`domain/entities/README.md`](../domain/entities/README.md)
|
|
97
|
+
- **Text Generation**: [`TEXT_GENERATION_SERVICE.md`](./TEXT_GENERATION_SERVICE.md)
|
|
98
|
+
- **Error Utilities**: [`ERROR_UTILITIES.md`](../infrastructure/utils/ERROR_UTILITIES.md)
|
|
99
|
+
|
|
100
|
+
## 📋 Configuration Reference
|
|
101
|
+
|
|
102
|
+
### JSON Schema Format
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
{
|
|
106
|
+
type: 'object',
|
|
107
|
+
properties: {
|
|
108
|
+
fieldName: {
|
|
109
|
+
type: 'string' | 'number' | 'boolean' | 'array' | 'object',
|
|
110
|
+
description?: string,
|
|
111
|
+
enum?: string[]
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
required: string[]
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Schema Best Practices
|
|
119
|
+
- Use detailed field descriptions
|
|
120
|
+
- Specify required fields explicitly
|
|
121
|
+
- Use appropriate data types
|
|
122
|
+
- Define nested objects clearly
|
|
123
|
+
- Add enum constraints for fixed values
|
|
124
|
+
|
|
125
|
+
## 🎓 Usage Patterns
|
|
126
|
+
|
|
127
|
+
### Simple JSON Generation
|
|
128
|
+
1. Define TypeScript interface for result
|
|
129
|
+
2. Create JSON schema matching interface
|
|
130
|
+
3. Call `generateStructuredText()` with prompt and schema
|
|
131
|
+
4. Receive typed result matching schema
|
|
132
|
+
5. Handle validation errors if schema mismatch
|
|
133
|
+
|
|
134
|
+
### Nested Objects
|
|
135
|
+
1. Define nested interfaces
|
|
136
|
+
2. Create nested schema structure
|
|
137
|
+
3. Specify required fields at each level
|
|
138
|
+
4. Generate and validate nested structure
|
|
139
|
+
5. Handle result as typed object
|
|
140
|
+
|
|
141
|
+
### Array Output Schema
|
|
142
|
+
1. Define interface with array properties
|
|
143
|
+
2. Create schema with array item types
|
|
144
|
+
3. Specify array item structure
|
|
145
|
+
4. Generate and validate array results
|
|
146
|
+
5. Use typed results in application
|
|
147
|
+
|
|
148
|
+
### Enum Validation
|
|
149
|
+
1. Define interface with enum-like fields
|
|
150
|
+
2. Create schema with enum constraints
|
|
151
|
+
3. Specify allowed values
|
|
152
|
+
4. Generate and validate against enum
|
|
153
|
+
5. Handle type-safe results
|
|
154
|
+
|
|
155
|
+
## 🚨 Common Pitfalls
|
|
156
|
+
|
|
157
|
+
### Don't
|
|
158
|
+
- Use minimal schemas without descriptions
|
|
159
|
+
- Make all fields required (too strict)
|
|
160
|
+
- Use `any` type instead of proper interfaces
|
|
161
|
+
- Create overly complex nested schemas
|
|
162
|
+
- Ignore validation errors
|
|
163
|
+
|
|
164
|
+
### Do
|
|
165
|
+
- Provide detailed field descriptions
|
|
166
|
+
- Explicitly mark required vs optional fields
|
|
167
|
+
- Use TypeScript interfaces for type safety
|
|
168
|
+
- Test schemas with sample data
|
|
169
|
+
- Handle parsing and validation errors
|
|
170
|
+
- Use fallback values for non-critical fields
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
**Last Updated**: 2025-01-08
|
|
175
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Text Generation Service
|
|
2
|
+
|
|
3
|
+
Generates text content using Google Gemini API models.
|
|
4
|
+
|
|
5
|
+
## 📍 Import Path
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
import { geminiTextGenerationService } from '@umituz/react-native-ai-gemini-provider';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 🎯 Purpose
|
|
12
|
+
|
|
13
|
+
Use this service to generate AI-powered text content. Supports single prompts, conversational interfaces, and multimodal input with images.
|
|
14
|
+
|
|
15
|
+
**When to use:**
|
|
16
|
+
- Text generation (stories, summaries, translations)
|
|
17
|
+
- Chat interfaces and conversational AI
|
|
18
|
+
- Content analysis with images
|
|
19
|
+
- Any text-based AI generation task
|
|
20
|
+
|
|
21
|
+
## 📌 Strategy
|
|
22
|
+
|
|
23
|
+
Text generation is the foundation of most AI interactions. This service:
|
|
24
|
+
- Abstracts Gemini API complexity
|
|
25
|
+
- Provides type-safe interfaces
|
|
26
|
+
- Handles errors consistently
|
|
27
|
+
- Supports streaming for real-time responses
|
|
28
|
+
- Integrates with retry logic
|
|
29
|
+
|
|
30
|
+
**Key Decision**: We use streaming by default for better UX, but fallback to non-streaming for compatibility.
|
|
31
|
+
|
|
32
|
+
## ⚠️ Rules
|
|
33
|
+
|
|
34
|
+
### Usage Rules
|
|
35
|
+
- **MUST** initialize provider with API key before use
|
|
36
|
+
- **MUST** handle errors appropriately
|
|
37
|
+
- **MUST** validate model IDs before use
|
|
38
|
+
- **SHOULD** use streaming for long responses
|
|
39
|
+
- **MUST NOT** exceed rate limits
|
|
40
|
+
|
|
41
|
+
### Configuration Rules
|
|
42
|
+
- **MUST** set appropriate `maxOutputTokens`
|
|
43
|
+
- **SHOULD** adjust `temperature` based on use case
|
|
44
|
+
- **MUST** use valid model IDs
|
|
45
|
+
- **SHOULD** implement retry logic for failures
|
|
46
|
+
|
|
47
|
+
### Error Handling Rules
|
|
48
|
+
- **MUST** catch and handle `GeminiError`
|
|
49
|
+
- **MUST** provide user-friendly error messages
|
|
50
|
+
- **SHOULD** log errors for debugging
|
|
51
|
+
- **MUST NOT** expose API keys in errors
|
|
52
|
+
|
|
53
|
+
## 🤖 AI Agent Guidelines
|
|
54
|
+
|
|
55
|
+
### When Modifying This Service
|
|
56
|
+
1. **READ** the implementation file first
|
|
57
|
+
2. **UNDERSTAND** current error handling
|
|
58
|
+
3. **MAINTAIN** backward compatibility
|
|
59
|
+
4. **ADD** tests for new functionality
|
|
60
|
+
5. **UPDATE** type definitions
|
|
61
|
+
|
|
62
|
+
### When Adding New Features
|
|
63
|
+
1. **CHECK** if similar feature exists
|
|
64
|
+
2. **FOLLOW** existing patterns
|
|
65
|
+
3. **USE** established error handling
|
|
66
|
+
4. **DOCUMENT** in type definitions
|
|
67
|
+
5. **ADD** examples to tests (not docs)
|
|
68
|
+
|
|
69
|
+
### When Fixing Bugs
|
|
70
|
+
1. **REPRODUCE** bug locally first
|
|
71
|
+
2. **IDENTIFY** root cause
|
|
72
|
+
3. **FIX** with minimal changes
|
|
73
|
+
4. **ADD** regression test
|
|
74
|
+
5. **VERIFY** all tests pass
|
|
75
|
+
|
|
76
|
+
### Code Style Rules
|
|
77
|
+
- **USE** async/await (no callbacks)
|
|
78
|
+
- **VALIDATE** inputs at function entry
|
|
79
|
+
- **THROW** typed errors (`GeminiError`)
|
|
80
|
+
- **LOG** important operations
|
|
81
|
+
- **COMMENT** complex logic only
|
|
82
|
+
|
|
83
|
+
## 📦 Available Methods
|
|
84
|
+
|
|
85
|
+
### `generateText(model, prompt, config?)`
|
|
86
|
+
Generate text from a prompt.
|
|
87
|
+
|
|
88
|
+
**Refer to**: [`gemini-text-generation.service.ts`](./gemini-text-generation.service.ts)
|
|
89
|
+
|
|
90
|
+
### `generateContent(model, contents, config?)`
|
|
91
|
+
Generate content with structured messages.
|
|
92
|
+
|
|
93
|
+
**Refer to**: [`gemini-text-generation.service.ts`](./gemini-text-generation.service.ts)
|
|
94
|
+
|
|
95
|
+
### `generateWithImages(model, prompt, images, config?)`
|
|
96
|
+
Generate text with image context.
|
|
97
|
+
|
|
98
|
+
**Refer to**: [`gemini-text-generation.service.ts`](./gemini-text-generation.service.ts)
|
|
99
|
+
|
|
100
|
+
### `generateTextStream(model, prompt, config?)`
|
|
101
|
+
Generate text with streaming response.
|
|
102
|
+
|
|
103
|
+
**Refer to**: [`gemini-text-generation.service.ts`](./gemini-text-generation.service.ts)
|
|
104
|
+
|
|
105
|
+
## 🔗 Related Modules
|
|
106
|
+
|
|
107
|
+
- **Domain Types**: [`domain/entities/README.md`](../domain/entities/README.md)
|
|
108
|
+
- **Image Generation**: [`IMAGE_GENERATION_SERVICE.md`](./IMAGE_GENERATION_SERVICE.md)
|
|
109
|
+
- **Retry Service**: [`RETRY_SERVICE.md`](./RETRY_SERVICE.md)
|
|
110
|
+
- **Streaming Service**: [`STREAMING_SERVICE.md`](./STREAMING_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
|
+
See: [`FEATURE_MODEL_SELECTOR_SERVICE.md`](./FEATURE_MODEL_SELECTOR_SERVICE.md)
|
|
119
|
+
|
|
120
|
+
### Error Types
|
|
121
|
+
See: [`ERROR_UTILITIES.md`](../infrastructure/utils/ERROR_UTILITIES.md)
|
|
122
|
+
|
|
123
|
+
## 🎓 Usage Patterns
|
|
124
|
+
|
|
125
|
+
### Basic Generation
|
|
126
|
+
1. Import service
|
|
127
|
+
2. Call `generateText()` with model and prompt
|
|
128
|
+
3. Handle response or error
|
|
129
|
+
4. Display result to user
|
|
130
|
+
|
|
131
|
+
### With Retry Logic
|
|
132
|
+
1. Use retry service wrapper
|
|
133
|
+
2. Configure max retries and delays
|
|
134
|
+
3. Handle retryable errors
|
|
135
|
+
4. Provide feedback to user
|
|
136
|
+
|
|
137
|
+
### Streaming
|
|
138
|
+
1. Use `generateTextStream()`
|
|
139
|
+
2. Handle chunks in callback
|
|
140
|
+
3. Update UI progressively
|
|
141
|
+
4. Handle stream completion
|
|
142
|
+
|
|
143
|
+
## 🚨 Common Pitfalls
|
|
144
|
+
|
|
145
|
+
### Don't
|
|
146
|
+
- Use hardcoded model IDs
|
|
147
|
+
- Ignore error handling
|
|
148
|
+
- Exceed rate limits
|
|
149
|
+
- Skip initialization
|
|
150
|
+
|
|
151
|
+
### Do
|
|
152
|
+
- Use feature model selector
|
|
153
|
+
- Handle all errors
|
|
154
|
+
- Implement backoff
|
|
155
|
+
- Initialize provider
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
**Last Updated**: 2025-01-08
|
|
160
|
+
**See Also**: [AI_GUIDELINES.md](../../AI_GUIDELINES.md)
|