@umituz/react-native-ai-gemini-provider 2.0.13 → 2.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/domain/entities/error.types.ts +0 -17
- package/src/domain/entities/gemini.types.ts +1 -37
- package/src/domain/entities/models.ts +0 -31
- package/src/index.ts +8 -24
- package/src/infrastructure/interceptors/RequestInterceptors.ts +20 -6
- package/src/infrastructure/interceptors/ResponseInterceptors.ts +20 -6
- package/src/infrastructure/services/gemini-client-core.service.ts +25 -32
- package/src/infrastructure/services/gemini-provider.ts +31 -15
- package/src/infrastructure/services/gemini-streaming.service.ts +0 -4
- package/src/infrastructure/services/gemini-structured-text.service.ts +8 -5
- package/src/infrastructure/services/gemini-text-generation.service.ts +20 -51
- package/src/infrastructure/services/index.ts +2 -16
- package/src/infrastructure/telemetry/TelemetryHooks.ts +23 -10
- package/src/infrastructure/utils/async-state.util.ts +0 -7
- package/src/infrastructure/utils/error-mapper.util.ts +13 -8
- package/src/infrastructure/utils/gemini-data-transformer.util.ts +12 -9
- package/src/infrastructure/utils/performance.util.ts +4 -54
- package/src/infrastructure/utils/rate-limiter.util.ts +12 -8
- package/src/presentation/hooks/use-gemini.ts +91 -24
- package/src/providers/ProviderConfig.ts +0 -32
- package/src/providers/ProviderFactory.ts +24 -37
- package/src/providers/index.ts +2 -7
- package/src/domain/README.md +0 -232
- package/src/domain/constants/index.ts +0 -5
- package/src/domain/entities/README.md +0 -238
- package/src/infrastructure/README.md +0 -252
- package/src/infrastructure/cache/CACHE_SYSTEM.md +0 -213
- package/src/infrastructure/cache/README.md +0 -213
- package/src/infrastructure/cache/SimpleCache.ts +0 -173
- package/src/infrastructure/cache/index.ts +0 -7
- package/src/infrastructure/content/ContentBuilder.ts +0 -24
- package/src/infrastructure/content/README.md +0 -175
- package/src/infrastructure/interceptors/README.md +0 -226
- package/src/infrastructure/interceptors/REQUEST_INTERCEPTORS.md +0 -171
- package/src/infrastructure/job/JOB_MANAGER.md +0 -174
- package/src/infrastructure/job/JobManager.ts +0 -114
- package/src/infrastructure/job/README.md +0 -194
- package/src/infrastructure/response/README.md +0 -187
- package/src/infrastructure/response/RESPONSE_FORMATTER.md +0 -185
- package/src/infrastructure/response/ResponseFormatter.ts +0 -58
- package/src/infrastructure/services/generation-executor.ts +0 -71
- package/src/infrastructure/services/job-processor.ts +0 -58
- package/src/infrastructure/services/provider-initializer.ts +0 -31
- package/src/infrastructure/telemetry/README.md +0 -203
- package/src/infrastructure/telemetry/TELEMETRY_SYSTEM.md +0 -200
- package/src/presentation/README.md +0 -187
- package/src/presentation/hooks/README.md +0 -188
- package/src/presentation/hooks/USE_GEMINI_HOOK.md +0 -226
- package/src/providers/README.md +0 -247
|
@@ -1,187 +0,0 @@
|
|
|
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)
|
|
@@ -1,185 +0,0 @@
|
|
|
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)
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Response Formatter
|
|
3
|
-
* Formats Gemini API responses into consistent output structure
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
declare const __DEV__: boolean;
|
|
7
|
-
|
|
8
|
-
export class ResponseFormatter {
|
|
9
|
-
formatResponse<T>(
|
|
10
|
-
response: unknown,
|
|
11
|
-
input: Record<string, unknown>,
|
|
12
|
-
): T {
|
|
13
|
-
const resp = response as {
|
|
14
|
-
candidates?: Array<{
|
|
15
|
-
content: {
|
|
16
|
-
parts: Array<{
|
|
17
|
-
text?: string;
|
|
18
|
-
inlineData?: { mimeType: string; data: string };
|
|
19
|
-
}>;
|
|
20
|
-
};
|
|
21
|
-
}>;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const candidate = resp.candidates?.[0];
|
|
25
|
-
const parts = candidate?.content.parts || [];
|
|
26
|
-
|
|
27
|
-
// Extract text
|
|
28
|
-
const text = parts.find((p) => p.text)?.text;
|
|
29
|
-
|
|
30
|
-
// Extract image if present
|
|
31
|
-
const imagePart = parts.find((p) => p.inlineData);
|
|
32
|
-
const imageData = imagePart?.inlineData;
|
|
33
|
-
|
|
34
|
-
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
35
|
-
// eslint-disable-next-line no-console
|
|
36
|
-
console.log("[ResponseFormatter] Formatting response:", {
|
|
37
|
-
hasText: !!text,
|
|
38
|
-
textLength: text?.length ?? 0,
|
|
39
|
-
hasImage: !!imageData,
|
|
40
|
-
outputFormat: input.outputFormat,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Build result object - always return { text } for consistency
|
|
45
|
-
const result: Record<string, unknown> = {
|
|
46
|
-
text,
|
|
47
|
-
response,
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
if (imageData) {
|
|
51
|
-
result.imageUrl = `data:${imageData.mimeType};base64,${imageData.data}`;
|
|
52
|
-
result.imageBase64 = imageData.data;
|
|
53
|
-
result.mimeType = imageData.mimeType;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return result as T;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generation Executor
|
|
3
|
-
* Handles execution of text generation
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { geminiTextGenerationService } from "./gemini-text-generation.service";
|
|
7
|
-
import { geminiStructuredTextService } from "./gemini-structured-text.service";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export interface ExecutionOptions {
|
|
11
|
-
onProgress?: (progress: number) => void;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface GenerationInput {
|
|
15
|
-
prompt?: string;
|
|
16
|
-
generationConfig?: unknown;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export type GenerationResult = string;
|
|
20
|
-
|
|
21
|
-
export class GenerationExecutor {
|
|
22
|
-
/**
|
|
23
|
-
* Execute text generation
|
|
24
|
-
*/
|
|
25
|
-
async executeTextGeneration(prompt: string, model: string): Promise<string> {
|
|
26
|
-
|
|
27
|
-
const response = await geminiTextGenerationService.generateContent(
|
|
28
|
-
model,
|
|
29
|
-
[{ parts: [{ text: prompt }], role: "user" }],
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
return this.extractTextFromResponse(response);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Execute structured text generation (JSON output)
|
|
37
|
-
*/
|
|
38
|
-
async executeStructuredGeneration<T>(
|
|
39
|
-
prompt: string,
|
|
40
|
-
schema: Record<string, unknown>,
|
|
41
|
-
model: string,
|
|
42
|
-
): Promise<T> {
|
|
43
|
-
|
|
44
|
-
return geminiStructuredTextService.generateStructuredText<T>(
|
|
45
|
-
model,
|
|
46
|
-
prompt,
|
|
47
|
-
schema,
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Extract text from Gemini response
|
|
54
|
-
*/
|
|
55
|
-
private extractTextFromResponse(response: unknown): string {
|
|
56
|
-
const resp = response as {
|
|
57
|
-
candidates?: Array<{
|
|
58
|
-
content: {
|
|
59
|
-
parts: Array<{ text?: string }>;
|
|
60
|
-
};
|
|
61
|
-
}>;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
return resp.candidates?.[0]?.content.parts
|
|
65
|
-
.filter((p): p is { text: string } => "text" in p && typeof p.text === "string")
|
|
66
|
-
.map((p) => p.text)
|
|
67
|
-
.join("") || "";
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export const generationExecutor = new GenerationExecutor();
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Job Processor
|
|
3
|
-
* Handles async job processing for text generation
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { JobManager } from "../job/JobManager";
|
|
7
|
-
import type { JobSubmission, JobStatus } from "../job/JobManager";
|
|
8
|
-
import { generationExecutor } from "./generation-executor";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export class JobProcessor {
|
|
12
|
-
private jobManager = new JobManager();
|
|
13
|
-
|
|
14
|
-
submitJob(model: string, input: Record<string, unknown>): Promise<JobSubmission> {
|
|
15
|
-
const submission = this.jobManager.submitJob(model, input);
|
|
16
|
-
|
|
17
|
-
this.processJobAsync(submission.requestId).catch(() => {
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
return Promise.resolve(submission);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
getJobStatus(_model: string, requestId: string): Promise<JobStatus> {
|
|
24
|
-
const status = this.jobManager.getJobStatus(requestId);
|
|
25
|
-
return Promise.resolve(status);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
getJobResult<T = unknown>(_model: string, requestId: string): Promise<T> {
|
|
29
|
-
try {
|
|
30
|
-
const result = this.jobManager.getJobResult<T>(requestId);
|
|
31
|
-
return Promise.resolve(result);
|
|
32
|
-
} catch (error) {
|
|
33
|
-
return Promise.reject(error);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
clear(): void {
|
|
38
|
-
this.jobManager.clear();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
private async processJobAsync(requestId: string): Promise<void> {
|
|
42
|
-
const job = this.jobManager.getJob(requestId);
|
|
43
|
-
if (!job) return;
|
|
44
|
-
|
|
45
|
-
this.jobManager.updateJobStatus(requestId, "IN_PROGRESS");
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
const prompt = String(job.input.prompt ?? "");
|
|
49
|
-
const result = await generationExecutor.executeTextGeneration(prompt, job.model);
|
|
50
|
-
this.jobManager.setJobResult(requestId, result);
|
|
51
|
-
} catch (error) {
|
|
52
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
53
|
-
this.jobManager.setJobError(requestId, errorMessage);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export const jobProcessor = new JobProcessor();
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Provider Initializer
|
|
3
|
-
* Handles initialization logic for Gemini Provider
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { GeminiConfig } from "../../domain/entities";
|
|
7
|
-
import { geminiClientCoreService } from "./gemini-client-core.service";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export type GeminiProviderConfig = GeminiConfig;
|
|
11
|
-
|
|
12
|
-
export class ProviderInitializer {
|
|
13
|
-
initialize(config: GeminiProviderConfig): void {
|
|
14
|
-
if (geminiClientCoreService.isInitialized()) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
geminiClientCoreService.initialize(config);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
isInitialized(): boolean {
|
|
23
|
-
return geminiClientCoreService.isInitialized();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
reset(): void {
|
|
27
|
-
geminiClientCoreService.reset();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const providerInitializer = new ProviderInitializer();
|