@umituz/react-native-ai-gemini-provider 1.14.39 → 1.14.40
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/constants/feature-models.constants.ts +2 -2
- package/src/infrastructure/services/feature-input-builder.ts +6 -2
- package/src/infrastructure/services/gemini-provider.ts +1 -1
- package/src/infrastructure/utils/video-feature-builders.util.ts +7 -21
- package/src/domain/constants/README.md +0 -191
- package/src/infrastructure/utils/INPUT_BUILDERS.md +0 -214
- package/src/infrastructure/utils/README.md +0 -289
package/package.json
CHANGED
|
@@ -34,8 +34,8 @@ export const GEMINI_IMAGE_FEATURE_MODELS: Record<ImageFeatureType, string> = {
|
|
|
34
34
|
* Using gemini-2.0-flash-exp for video generation capabilities
|
|
35
35
|
*/
|
|
36
36
|
export const GEMINI_VIDEO_FEATURE_MODELS: Record<VideoFeatureType, string> = {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
37
|
+
"image-to-video": "gemini-2.0-flash-exp",
|
|
38
|
+
"text-to-video": "gemini-2.0-flash-exp",
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
/**
|
|
@@ -69,13 +69,17 @@ class FeatureInputBuilder {
|
|
|
69
69
|
const { sourceImageBase64, targetImageBase64, prompt, options } = data;
|
|
70
70
|
|
|
71
71
|
switch (feature) {
|
|
72
|
-
case "
|
|
73
|
-
case "ai-kiss":
|
|
72
|
+
case "image-to-video":
|
|
74
73
|
return buildVideoFromDualImagesInput(sourceImageBase64, {
|
|
75
74
|
target_image: targetImageBase64,
|
|
76
75
|
motion_prompt: prompt,
|
|
77
76
|
...options,
|
|
78
77
|
});
|
|
78
|
+
case "text-to-video":
|
|
79
|
+
return buildVideoFromDualImagesInput(sourceImageBase64 || "", {
|
|
80
|
+
motion_prompt: prompt,
|
|
81
|
+
...options,
|
|
82
|
+
});
|
|
79
83
|
default:
|
|
80
84
|
throw new Error(`Unknown video feature: ${String(feature)}`);
|
|
81
85
|
}
|
|
@@ -48,7 +48,7 @@ const GEMINI_CAPABILITIES: ProviderCapabilities = {
|
|
|
48
48
|
"hd-touch-up",
|
|
49
49
|
"replace-background",
|
|
50
50
|
] as const,
|
|
51
|
-
videoFeatures: ["
|
|
51
|
+
videoFeatures: ["image-to-video", "text-to-video"] as const,
|
|
52
52
|
textToImage: true,
|
|
53
53
|
textToVideo: true,
|
|
54
54
|
imageToVideo: true,
|
|
@@ -7,36 +7,22 @@ import { buildSingleImageInput, buildDualImageInput } from "./base-input-builder
|
|
|
7
7
|
import type { VideoFromImageOptions, VideoFromDualImageOptions } from "./input-builder.types";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
* Build
|
|
10
|
+
* Build video from single image input for Gemini
|
|
11
11
|
*/
|
|
12
|
-
export function
|
|
12
|
+
export function buildVideoFromImageInput(
|
|
13
13
|
base64: string,
|
|
14
14
|
options?: VideoFromImageOptions,
|
|
15
15
|
): Record<string, unknown> {
|
|
16
|
-
const motionPrompt = options?.motion_prompt || "
|
|
16
|
+
const motionPrompt = options?.motion_prompt || "Animate this image with natural, smooth motion";
|
|
17
17
|
|
|
18
|
-
const prompt = `Transform this image into a video. ${motionPrompt}. Make it natural and
|
|
19
|
-
|
|
20
|
-
return buildSingleImageInput(base64, prompt);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Build AI kiss video input for Gemini
|
|
25
|
-
*/
|
|
26
|
-
export function buildAIKissInput(
|
|
27
|
-
base64: string,
|
|
28
|
-
options?: VideoFromImageOptions,
|
|
29
|
-
): Record<string, unknown> {
|
|
30
|
-
const motionPrompt = options?.motion_prompt || "Create a kissing animation";
|
|
31
|
-
|
|
32
|
-
const prompt = `Transform this image into a video. ${motionPrompt}. Make it romantic and natural.`;
|
|
18
|
+
const prompt = `Transform this image into a video. ${motionPrompt}. Make it natural and cinematic.`;
|
|
33
19
|
|
|
34
20
|
return buildSingleImageInput(base64, prompt);
|
|
35
21
|
}
|
|
36
22
|
|
|
37
23
|
/**
|
|
38
24
|
* Build video from dual images input for Gemini
|
|
39
|
-
* Used for
|
|
25
|
+
* Used for features that need source and target images
|
|
40
26
|
*/
|
|
41
27
|
export function buildVideoFromDualImagesInput(
|
|
42
28
|
sourceBase64: string,
|
|
@@ -47,11 +33,11 @@ export function buildVideoFromDualImagesInput(
|
|
|
47
33
|
|
|
48
34
|
if (!targetBase64) {
|
|
49
35
|
// Single image case (fallback)
|
|
50
|
-
const prompt = `Transform this image into a video. ${motionPrompt}. Make it natural and
|
|
36
|
+
const prompt = `Transform this image into a video. ${motionPrompt}. Make it natural and cinematic.`;
|
|
51
37
|
return buildSingleImageInput(sourceBase64, prompt);
|
|
52
38
|
}
|
|
53
39
|
|
|
54
|
-
const prompt = `Transform these two images into a video. ${motionPrompt}. Make it natural and
|
|
40
|
+
const prompt = `Transform these two images into a video. ${motionPrompt}. Make it natural and cinematic. The first image is the source person, the second is the target person.`;
|
|
55
41
|
|
|
56
42
|
return buildDualImageInput(sourceBase64, targetBase64, prompt);
|
|
57
43
|
}
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
# Domain Constants
|
|
2
|
-
|
|
3
|
-
Constant values and model catalogs for Gemini provider. Contains feature-based model mappings for image and video operations.
|
|
4
|
-
|
|
5
|
-
## 📍 Import Path
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
import {
|
|
9
|
-
GEMINI_IMAGE_FEATURE_MODELS,
|
|
10
|
-
GEMINI_VIDEO_FEATURE_MODELS,
|
|
11
|
-
getGeminiImageFeatureModel,
|
|
12
|
-
getGeminiVideoFeatureModel,
|
|
13
|
-
getAllFeatureModels
|
|
14
|
-
} from '@umituz/react-native-ai-gemini-provider';
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## 🎯 Purpose
|
|
18
|
-
|
|
19
|
-
Use constants to map features to appropriate models. Provides centralized model selection for image and video processing features.
|
|
20
|
-
|
|
21
|
-
**When to use:**
|
|
22
|
-
- Get model ID for specific feature
|
|
23
|
-
- Display available features in UI
|
|
24
|
-
- Validate feature requests
|
|
25
|
-
- Map user features to models
|
|
26
|
-
- List supported operations
|
|
27
|
-
|
|
28
|
-
## 📌 Strategy
|
|
29
|
-
|
|
30
|
-
Features map to specific models. This system:
|
|
31
|
-
- Provides constant feature-to-model mappings
|
|
32
|
-
- Centralizes model configuration
|
|
33
|
-
- Enables feature validation
|
|
34
|
-
- Simplifies model selection
|
|
35
|
-
- Maintains consistency across codebase
|
|
36
|
-
|
|
37
|
-
**Key Decision**: Use feature constants instead of hardcoded model IDs. This enables centralized model management and updates.
|
|
38
|
-
|
|
39
|
-
## ⚠️ Rules
|
|
40
|
-
|
|
41
|
-
### Usage Rules
|
|
42
|
-
- **MUST** use constants for model selection
|
|
43
|
-
- **SHOULD** validate feature before use
|
|
44
|
-
- **MUST** handle unknown features gracefully
|
|
45
|
-
- **SHOULD** check model compatibility
|
|
46
|
-
- **MUST NOT** hardcode model IDs
|
|
47
|
-
|
|
48
|
-
### Feature Selection Rules
|
|
49
|
-
- **MUST** select appropriate model for feature
|
|
50
|
-
- **SHOULD** use getter functions
|
|
51
|
-
- **MUST** validate feature type
|
|
52
|
-
- **SHOULD** handle missing features
|
|
53
|
-
- **MUST** return valid model IDs
|
|
54
|
-
|
|
55
|
-
### Validation Rules
|
|
56
|
-
- **SHOULD** validate feature strings
|
|
57
|
-
- **MUST** check feature exists
|
|
58
|
-
- **SHOULD** provide fallback models
|
|
59
|
-
- **MUST** throw on invalid features
|
|
60
|
-
- **SHOULD** log validation failures
|
|
61
|
-
|
|
62
|
-
### Maintenance Rules
|
|
63
|
-
- **MUST** update constants when models change
|
|
64
|
-
- **SHOULD** document model changes
|
|
65
|
-
- **MUST** maintain backward compatibility
|
|
66
|
-
- **SHOULD** test feature mappings
|
|
67
|
-
- **MUST** update documentation
|
|
68
|
-
|
|
69
|
-
## 🤖 AI Agent Guidelines
|
|
70
|
-
|
|
71
|
-
### When Getting Model for Feature
|
|
72
|
-
1. **CALL** getGeminiImageFeatureModel() or getGeminiVideoFeatureModel()
|
|
73
|
-
2. **VALIDATE** feature exists
|
|
74
|
-
3. **RETURN** model ID
|
|
75
|
-
4. **HANDLE** unknown features
|
|
76
|
-
5. **LOG** model selection
|
|
77
|
-
|
|
78
|
-
### When Listing Features
|
|
79
|
-
1. **CALL** getAllFeatureModels()
|
|
80
|
-
2. **FILTER** by feature type
|
|
81
|
-
3. **DISPLAY** to user
|
|
82
|
-
4. **GROUP** by category
|
|
83
|
-
5. **HANDLE** empty lists
|
|
84
|
-
|
|
85
|
-
### When Validating Features
|
|
86
|
-
1. **CHECK** feature in constants
|
|
87
|
-
2. **VERIFY** model exists
|
|
88
|
-
3. **RETURN** validation result
|
|
89
|
-
4. **PROVIDE** error messages
|
|
90
|
-
5. **SUGGEST** alternatives
|
|
91
|
-
|
|
92
|
-
### Code Style Rules
|
|
93
|
-
- **USE** constants instead of hardcoded values
|
|
94
|
-
- **IMPORT** specific constants needed
|
|
95
|
-
- **VALIDATE** feature inputs
|
|
96
|
-
- **HANDLE** edge cases
|
|
97
|
-
- **DOCUMENT** custom features
|
|
98
|
-
|
|
99
|
-
## 📦 Available Constants
|
|
100
|
-
|
|
101
|
-
### Image Feature Models
|
|
102
|
-
|
|
103
|
-
**Refer to**: [`feature-models.constants.ts`](./feature-models.constants.ts)
|
|
104
|
-
|
|
105
|
-
**Features:**
|
|
106
|
-
- `upscale` - Image upscaling
|
|
107
|
-
- `photo-restore` - Photo restoration
|
|
108
|
-
- `face-swap` - Face swapping
|
|
109
|
-
- `anime-selfie` - Anime style transformation
|
|
110
|
-
- `remove-background` - Background removal
|
|
111
|
-
- `remove-object` - Object removal
|
|
112
|
-
- `hd-touch-up` - HD enhancement
|
|
113
|
-
- `replace-background` - Background replacement
|
|
114
|
-
|
|
115
|
-
**Model**: All use `gemini-2.0-flash-exp`
|
|
116
|
-
|
|
117
|
-
### Video Feature Models
|
|
118
|
-
|
|
119
|
-
**Refer to**: [`feature-models.constants.ts`](./feature-models.constants.ts)
|
|
120
|
-
|
|
121
|
-
**Features:**
|
|
122
|
-
- `ai-hug` - AI hugging effect
|
|
123
|
-
- `ai-kiss` - AI kissing effect
|
|
124
|
-
|
|
125
|
-
**Model**: All use `gemini-2.0-flash-exp`
|
|
126
|
-
|
|
127
|
-
## 🔗 Related Modules
|
|
128
|
-
|
|
129
|
-
- **Domain Entities**: [`../entities/README.md`](../entities/README.md)
|
|
130
|
-
- **Domain README**: [`../README.md`](../README.md)
|
|
131
|
-
- **Model Selector**: [`../../infrastructure/services/FEATURE_MODEL_SELECTOR_SERVICE.md`](../../infrastructure/services/FEATURE_MODEL_SELECTOR_SERVICE.md)
|
|
132
|
-
|
|
133
|
-
## 📋 Feature Reference
|
|
134
|
-
|
|
135
|
-
### Image Editing Features
|
|
136
|
-
- **upscale**: Enlarge image while maintaining quality
|
|
137
|
-
- **photo-restore**: Restore old or damaged photos
|
|
138
|
-
- **face-swap**: Swap faces between images
|
|
139
|
-
- **anime-selfie**: Transform selfie to anime style
|
|
140
|
-
- **remove-background**: Remove image background
|
|
141
|
-
- **remove-object**: Remove specific objects from image
|
|
142
|
-
- **hd-touch-up**: Enhance image quality to HD
|
|
143
|
-
- **replace-background**: Replace image background
|
|
144
|
-
|
|
145
|
-
### Video Features
|
|
146
|
-
- **ai-hug**: Generate video of subjects hugging
|
|
147
|
-
- **ai-kiss**: Generate video of subjects kissing
|
|
148
|
-
|
|
149
|
-
## 🎓 Usage Patterns
|
|
150
|
-
|
|
151
|
-
### Feature-Based Model Selection
|
|
152
|
-
1. Determine feature type (image or video)
|
|
153
|
-
2. Call appropriate getter function
|
|
154
|
-
3. Get model ID for feature
|
|
155
|
-
4. Use model in service call
|
|
156
|
-
5. Handle unknown features
|
|
157
|
-
|
|
158
|
-
### Feature Listing
|
|
159
|
-
1. Call getAllFeatureModels()
|
|
160
|
-
2. Iterate through feature list
|
|
161
|
-
3. Group by category
|
|
162
|
-
4. Display to user
|
|
163
|
-
5. Handle feature selection
|
|
164
|
-
|
|
165
|
-
### Feature Validation
|
|
166
|
-
1. Check feature in constants
|
|
167
|
-
2. Verify model mapping exists
|
|
168
|
-
3. Validate feature type
|
|
169
|
-
4. Return validation result
|
|
170
|
-
5. Provide feedback
|
|
171
|
-
|
|
172
|
-
## 🚨 Common Pitfalls
|
|
173
|
-
|
|
174
|
-
### Don't
|
|
175
|
-
- Hardcode model IDs in application code
|
|
176
|
-
- Use features without validation
|
|
177
|
-
- Assume all features use same model
|
|
178
|
-
- Skip feature existence checks
|
|
179
|
-
- Use invalid feature strings
|
|
180
|
-
|
|
181
|
-
### Do
|
|
182
|
-
- Use constants for model selection
|
|
183
|
-
- Validate features before use
|
|
184
|
-
- Check model mappings
|
|
185
|
-
- Handle unknown features gracefully
|
|
186
|
-
- Update constants when models change
|
|
187
|
-
|
|
188
|
-
---
|
|
189
|
-
|
|
190
|
-
**Last Updated**: 2025-01-08
|
|
191
|
-
**See Also**: [AI_GUIDELINES.md](../../../AI_GUIDELINES.md)
|
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
# Input Builders
|
|
2
|
-
|
|
3
|
-
Helper functions for building Gemini API inputs for image and video processing features. Creates properly formatted prompts and image arrays.
|
|
4
|
-
|
|
5
|
-
## 📍 Import Path
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
import {
|
|
9
|
-
buildUpscaleInput,
|
|
10
|
-
buildPhotoRestoreInput,
|
|
11
|
-
buildRemoveBackgroundInput,
|
|
12
|
-
buildReplaceBackgroundInput,
|
|
13
|
-
buildFaceSwapInput,
|
|
14
|
-
buildSingleImageInput,
|
|
15
|
-
buildDualImageInput
|
|
16
|
-
} from '@umituz/react-native-ai-gemini-provider';
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## 🎯 Purpose
|
|
20
|
-
|
|
21
|
-
Use input builders to create properly formatted inputs for image and video features. Simplifies prompt engineering and image data preparation.
|
|
22
|
-
|
|
23
|
-
**When to use:**
|
|
24
|
-
- Build inputs for image editing features
|
|
25
|
-
- Create prompts for video generation
|
|
26
|
-
- Format image data correctly
|
|
27
|
-
- Combine multiple images
|
|
28
|
-
- Simplify feature integration
|
|
29
|
-
|
|
30
|
-
## 📌 Strategy
|
|
31
|
-
|
|
32
|
-
Input builders encapsulate prompt complexity. This system:
|
|
33
|
-
- Creates optimized prompts for features
|
|
34
|
-
- Formats image data correctly
|
|
35
|
-
- Handles single and multiple images
|
|
36
|
-
- Provides consistent interface
|
|
37
|
-
- Simplifies feature usage
|
|
38
|
-
|
|
39
|
-
**Key Decision**: Always use input builders for image/video features. Ensures correct prompt engineering and data formatting.
|
|
40
|
-
|
|
41
|
-
## ⚠️ Rules
|
|
42
|
-
|
|
43
|
-
### Usage Rules
|
|
44
|
-
- **MUST** use builders for image/video features
|
|
45
|
-
- **SHOULD** provide valid base64 data
|
|
46
|
-
- **MUST** check required parameters
|
|
47
|
-
- **SHOULD** handle missing options
|
|
48
|
-
- **MUST NOT** create inputs manually
|
|
49
|
-
|
|
50
|
-
### Data Rules
|
|
51
|
-
- **MUST** provide valid base64 strings
|
|
52
|
-
- **SHOULD** validate image data
|
|
53
|
-
- **MUST** include correct MIME types
|
|
54
|
-
- **SHOULD** handle encoding errors
|
|
55
|
-
- **MUST NOT** pass invalid data
|
|
56
|
-
|
|
57
|
-
### Prompt Rules
|
|
58
|
-
- **MUST** use builder-generated prompts
|
|
59
|
-
- **SHOULD NOT** modify builder prompts
|
|
60
|
-
- **MUST** follow prompt patterns
|
|
61
|
-
- **SHOULD** test prompt effectiveness
|
|
62
|
-
- **MUST NOT** hardcode prompts
|
|
63
|
-
|
|
64
|
-
### Options Rules
|
|
65
|
-
- **SHOULD** provide appropriate options
|
|
66
|
-
- **MUST** check required parameters
|
|
67
|
-
- **SHOULD** use default values wisely
|
|
68
|
-
- **MUST** validate option values
|
|
69
|
-
- **SHOULD NOT** ignore option validation
|
|
70
|
-
|
|
71
|
-
## 🤖 AI Agent Guidelines
|
|
72
|
-
|
|
73
|
-
### When Building Inputs
|
|
74
|
-
1. **SELECT** appropriate builder function
|
|
75
|
-
2. **PROVIDE** valid base64 data
|
|
76
|
-
3. **CONFIGURE** options if needed
|
|
77
|
-
4. **USE** returned prompt and images
|
|
78
|
-
5. **HANDLE** builder errors
|
|
79
|
-
|
|
80
|
-
### When Using Image Features
|
|
81
|
-
1. **PREPARE** image base64 data
|
|
82
|
-
2. **CALL** appropriate builder
|
|
83
|
-
3. **USE** prompt from builder
|
|
84
|
-
4. **PASS** images array to service
|
|
85
|
-
5. **HANDLE** service response
|
|
86
|
-
|
|
87
|
-
### When Creating Custom Builders
|
|
88
|
-
1. **ANALYZE** existing builders
|
|
89
|
-
2. **FOLLOW** builder pattern
|
|
90
|
-
3. **CREATE** optimized prompt
|
|
91
|
-
4. **RETURN** consistent structure
|
|
92
|
-
5. **TEST** builder thoroughly
|
|
93
|
-
|
|
94
|
-
### Code Style Rules
|
|
95
|
-
- **USE** builders instead of manual input
|
|
96
|
-
- **VALIDATE** input data
|
|
97
|
-
- **HANDLE** missing options
|
|
98
|
-
- **FOLLOW** builder patterns
|
|
99
|
-
- **TEST** builder output
|
|
100
|
-
|
|
101
|
-
## 📦 Available Builders
|
|
102
|
-
|
|
103
|
-
### Single Image Builders
|
|
104
|
-
|
|
105
|
-
**Refer to**: [`image-feature-builders.util.ts`](./image-feature-builders.util.ts)
|
|
106
|
-
|
|
107
|
-
- `buildUpscaleInput(base64, options?)` - Image upscaling
|
|
108
|
-
- `buildPhotoRestoreInput(base64, options?)` - Photo restoration
|
|
109
|
-
- `buildAnimeSelfieInput(base64, options?)` - Anime style transformation
|
|
110
|
-
- `buildRemoveBackgroundInput(base64, options?)` - Background removal
|
|
111
|
-
- `buildRemoveObjectInput(base64, options?)` - Object removal
|
|
112
|
-
- `buildReplaceBackgroundInput(base64, options)` - Background replacement
|
|
113
|
-
- `buildHDTouchUpInput(base64, options?)` - HD enhancement
|
|
114
|
-
|
|
115
|
-
### Dual Image Builders
|
|
116
|
-
|
|
117
|
-
**Refer to**: [`image-feature-builders.util.ts`](./image-feature-builders.util.ts)
|
|
118
|
-
|
|
119
|
-
- `buildFaceSwapInput(sourceBase64, targetBase64, options?)` - Face swapping
|
|
120
|
-
|
|
121
|
-
### Video Builders
|
|
122
|
-
|
|
123
|
-
**Refer to**: [`video-feature-builders.util.ts`](./video-feature-builders.util.ts)
|
|
124
|
-
|
|
125
|
-
- `buildAIHugInput(base64, options?)` - AI hugging effect
|
|
126
|
-
- `buildAIKissInput(base64, options?)` - AI kissing effect
|
|
127
|
-
- `buildVideoFromDualImagesInput(base64a, base64b, options?)` - Dual image to video
|
|
128
|
-
|
|
129
|
-
### Base Builders
|
|
130
|
-
|
|
131
|
-
**Refer to**: [`base-input-builders.util.ts`](./base-input-builders.util.ts)
|
|
132
|
-
|
|
133
|
-
- `buildSingleImageInput(base64, prompt)` - Single image input
|
|
134
|
-
- `buildDualImageInput(base64a, base64b, prompt)` - Dual image input
|
|
135
|
-
|
|
136
|
-
## 🔗 Related Modules
|
|
137
|
-
|
|
138
|
-
- **Image Edit Service**: [`../services/IMAGE_EDIT_SERVICE.md`](../services/IMAGE_EDIT_SERVICE.md)
|
|
139
|
-
- **Image Preparer Utils**: [`../utils/IMAGE_PREPARER_UTILS.md`](../utils/IMAGE_PREPARER_UTILS.md)
|
|
140
|
-
- **Content Builder**: [`../content/README.md`](../content/README.md)
|
|
141
|
-
|
|
142
|
-
## 📋 Builder Return Type
|
|
143
|
-
|
|
144
|
-
All builders return:
|
|
145
|
-
|
|
146
|
-
```typescript
|
|
147
|
-
{
|
|
148
|
-
prompt: string;
|
|
149
|
-
images: Array<{
|
|
150
|
-
base64: string;
|
|
151
|
-
mimeType: string;
|
|
152
|
-
}>;
|
|
153
|
-
}
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
This structure is compatible with `geminiImageEditService.editImage()`.
|
|
157
|
-
|
|
158
|
-
## 🎓 Usage Patterns
|
|
159
|
-
|
|
160
|
-
### Basic Feature Usage
|
|
161
|
-
1. Prepare image base64 data
|
|
162
|
-
2. Call appropriate builder function
|
|
163
|
-
3. Configure options if needed
|
|
164
|
-
4. Use returned prompt and images
|
|
165
|
-
5. Call image edit service
|
|
166
|
-
|
|
167
|
-
### Single Image Processing
|
|
168
|
-
1. Get image base64 string
|
|
169
|
-
2. Call single-image builder
|
|
170
|
-
3. Pass prompt and images to service
|
|
171
|
-
4. Handle service response
|
|
172
|
-
5. Display result
|
|
173
|
-
|
|
174
|
-
### Dual Image Processing
|
|
175
|
-
1. Get both image base64 strings
|
|
176
|
-
2. Call dual-image builder
|
|
177
|
-
3. Use returned input structure
|
|
178
|
-
4. Call appropriate service
|
|
179
|
-
5. Handle response
|
|
180
|
-
|
|
181
|
-
### Batch Processing
|
|
182
|
-
1. Prepare all images
|
|
183
|
-
2. Call builder for each image
|
|
184
|
-
3. Process inputs in sequence
|
|
185
|
-
4. Collect all results
|
|
186
|
-
5. Return batch results
|
|
187
|
-
|
|
188
|
-
### Custom Features
|
|
189
|
-
1. Analyze existing builders
|
|
190
|
-
2. Create custom builder function
|
|
191
|
-
3. Follow builder pattern
|
|
192
|
-
4. Return consistent structure
|
|
193
|
-
5. Test thoroughly
|
|
194
|
-
|
|
195
|
-
## 🚨 Common Pitfalls
|
|
196
|
-
|
|
197
|
-
### Don't
|
|
198
|
-
- Create inputs manually
|
|
199
|
-
- Modify builder prompts
|
|
200
|
-
- Skip builder functions
|
|
201
|
-
- Hardcode prompts
|
|
202
|
-
- Use invalid base64 data
|
|
203
|
-
|
|
204
|
-
### Do
|
|
205
|
-
- Always use input builders
|
|
206
|
-
- Follow builder patterns
|
|
207
|
-
- Validate input data
|
|
208
|
-
- Use builder-generated prompts
|
|
209
|
-
- Test builder output
|
|
210
|
-
|
|
211
|
-
---
|
|
212
|
-
|
|
213
|
-
**Last Updated**: 2025-01-08
|
|
214
|
-
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
# Utils
|
|
2
|
-
|
|
3
|
-
Utility functions and tools for Gemini provider. Handles error management, performance monitoring, data transformation, model validation, image preparation, and input building.
|
|
4
|
-
|
|
5
|
-
## 📍 Import Path
|
|
6
|
-
|
|
7
|
-
```
|
|
8
|
-
import {
|
|
9
|
-
// Error handling
|
|
10
|
-
mapGeminiError,
|
|
11
|
-
isGeminiErrorRetryable,
|
|
12
|
-
categorizeGeminiError,
|
|
13
|
-
createGeminiError,
|
|
14
|
-
|
|
15
|
-
// Model validation
|
|
16
|
-
isValidModel,
|
|
17
|
-
validateModel,
|
|
18
|
-
getSafeModel,
|
|
19
|
-
getModelCategory,
|
|
20
|
-
getAllValidModels,
|
|
21
|
-
|
|
22
|
-
// Performance
|
|
23
|
-
measureAsync,
|
|
24
|
-
measureSync,
|
|
25
|
-
debounce,
|
|
26
|
-
throttle,
|
|
27
|
-
performanceTracker,
|
|
28
|
-
|
|
29
|
-
// Data transformation
|
|
30
|
-
extractBase64Data,
|
|
31
|
-
extractTextFromResponse,
|
|
32
|
-
|
|
33
|
-
// Image preparation
|
|
34
|
-
prepareImageFromUri,
|
|
35
|
-
prepareImage,
|
|
36
|
-
isValidBase64,
|
|
37
|
-
|
|
38
|
-
// Input builders
|
|
39
|
-
buildUpscaleInput,
|
|
40
|
-
buildPhotoRestoreInput,
|
|
41
|
-
buildRemoveBackgroundInput,
|
|
42
|
-
buildReplaceBackgroundInput
|
|
43
|
-
} from '@umituz/react-native-ai-gemini-provider';
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## 🎯 Purpose
|
|
47
|
-
|
|
48
|
-
Use utility functions for common operations across the provider. Provides error handling, validation, performance monitoring, and data transformation capabilities.
|
|
49
|
-
|
|
50
|
-
**When to use:**
|
|
51
|
-
- Validate model IDs
|
|
52
|
-
- Handle API errors
|
|
53
|
-
- Monitor performance
|
|
54
|
-
- Transform data formats
|
|
55
|
-
- Prepare images for processing
|
|
56
|
-
- Build complex inputs
|
|
57
|
-
- Measure operation duration
|
|
58
|
-
- Debounce/throttle functions
|
|
59
|
-
|
|
60
|
-
## 📌 Strategy
|
|
61
|
-
|
|
62
|
-
Utilities provide reusable, composable functions. This system:
|
|
63
|
-
- Separates concerns from business logic
|
|
64
|
-
- Provides consistent error handling
|
|
65
|
-
- Enables performance monitoring
|
|
66
|
-
- Simplifies data transformations
|
|
67
|
-
- Validates inputs centrally
|
|
68
|
-
- Builds complex inputs from simple data
|
|
69
|
-
|
|
70
|
-
**Key Decision**: Use utility functions for cross-cutting concerns. Keep services focused on business logic, use utils for operational tasks.
|
|
71
|
-
|
|
72
|
-
## ⚠️ Rules
|
|
73
|
-
|
|
74
|
-
### Error Handling Rules
|
|
75
|
-
- **MUST** catch and categorize all API errors
|
|
76
|
-
- **SHOULD** use typed GeminiError instances
|
|
77
|
-
- **MUST** check retryable status before retrying
|
|
78
|
-
- **SHOULD** log errors appropriately
|
|
79
|
-
- **MUST NOT** expose sensitive data in errors
|
|
80
|
-
|
|
81
|
-
### Model Validation Rules
|
|
82
|
-
- **MUST** validate model IDs before use
|
|
83
|
-
- **SHOULD** use safe fallbacks for invalid models
|
|
84
|
-
- **MUST** check model category for features
|
|
85
|
-
- **SHOULD** validate early in request flow
|
|
86
|
-
- **MUST NOT** allow arbitrary model IDs
|
|
87
|
-
|
|
88
|
-
### Performance Rules
|
|
89
|
-
- **SHOULD** measure critical operations
|
|
90
|
-
- **MUST** track performance metrics
|
|
91
|
-
- **SHOULD** use debounce/throttle for frequent calls
|
|
92
|
-
- **MUST NOT** impact performance with monitoring
|
|
93
|
-
- **SHOULD** aggregate metrics over time
|
|
94
|
-
|
|
95
|
-
### Data Transformation Rules
|
|
96
|
-
- **MUST** validate input data format
|
|
97
|
-
- **SHOULD** handle edge cases (null, undefined)
|
|
98
|
-
- **MUST** sanitize sensitive information
|
|
99
|
-
- **SHOULD** preserve data integrity
|
|
100
|
-
- **MUST** handle transformation errors
|
|
101
|
-
|
|
102
|
-
### Image Preparation Rules
|
|
103
|
-
- **MUST** validate base64 format
|
|
104
|
-
- **SHOULD** handle different image formats
|
|
105
|
-
- **MUST** extract MIME type correctly
|
|
106
|
-
- **SHOULD** handle large images efficiently
|
|
107
|
-
- **MUST** validate image data
|
|
108
|
-
|
|
109
|
-
## 🤖 AI Agent Guidelines
|
|
110
|
-
|
|
111
|
-
### When Handling Errors
|
|
112
|
-
1. **CATCH** errors from API calls
|
|
113
|
-
2. **MAP** to GeminiError using mapGeminiError()
|
|
114
|
-
3. **CHECK** if retryable using isGeminiErrorRetryable()
|
|
115
|
-
4. **CATEGORIZE** error type
|
|
116
|
-
5. **HANDLE** appropriately (retry or fail)
|
|
117
|
-
|
|
118
|
-
### When Validating Models
|
|
119
|
-
1. **CHECK** model validity with isValidModel()
|
|
120
|
-
2. **GET** safe model with getSafeModel()
|
|
121
|
-
3. **DETERMINE** model category
|
|
122
|
-
4. **VALIDATE** category supports feature
|
|
123
|
-
5. **USE** validated model in requests
|
|
124
|
-
|
|
125
|
-
### When Measuring Performance
|
|
126
|
-
1. **WRAP** operation with measureAsync()
|
|
127
|
-
2. **RECORD** duration with performanceTracker
|
|
128
|
-
3. **ANALYZE** metrics periodically
|
|
129
|
-
4. **IDENTIFY** slow operations
|
|
130
|
-
5. **OPTIMIZE** based on data
|
|
131
|
-
|
|
132
|
-
### When Preparing Images
|
|
133
|
-
1. **VALIDATE** image source (URI or base64)
|
|
134
|
-
2. **EXTRACT** base64 data if needed
|
|
135
|
-
3. **DETERMINE** MIME type
|
|
136
|
-
4. **PREPARE** image object
|
|
137
|
-
5. **HANDLE** preparation errors
|
|
138
|
-
|
|
139
|
-
### Code Style Rules
|
|
140
|
-
- **USE** appropriate utility for each task
|
|
141
|
-
- **VALIDATE** inputs early
|
|
142
|
-
- **HANDLE** all error cases
|
|
143
|
-
- **LOG** important operations
|
|
144
|
-
- **COMMENT** complex transformations
|
|
145
|
-
|
|
146
|
-
## 📦 Available Utilities
|
|
147
|
-
|
|
148
|
-
### Error Management
|
|
149
|
-
|
|
150
|
-
**Refer to**: [`ERROR_UTILITIES.md`](./ERROR_UTILITIES.md)
|
|
151
|
-
|
|
152
|
-
**Functions:**
|
|
153
|
-
- `mapGeminiError(error)` - Map error to GeminiError
|
|
154
|
-
- `isGeminiErrorRetryable(error)` - Check if error is retryable
|
|
155
|
-
- `categorizeGeminiError(error)` - Categorize error type
|
|
156
|
-
- `createGeminiError(error)` - Create GeminiError instance
|
|
157
|
-
|
|
158
|
-
### Model Validation
|
|
159
|
-
|
|
160
|
-
**Refer to**: [`MODEL_VALIDATION_UTILS.md`](./MODEL_VALIDATION_UTILS.md)
|
|
161
|
-
|
|
162
|
-
**Functions:**
|
|
163
|
-
- `isValidModel(model)` - Check if model ID is valid
|
|
164
|
-
- `validateModel(model)` - Validate or throw error
|
|
165
|
-
- `getSafeModel(model, defaultType)` - Get safe model with fallback
|
|
166
|
-
- `getModelCategory(model)` - Get model category
|
|
167
|
-
- `getAllValidModels()` - Get all valid model IDs
|
|
168
|
-
|
|
169
|
-
### Performance Tools
|
|
170
|
-
|
|
171
|
-
**Refer to**: [`PERFORMANCE_UTILS.md`](./PERFORMANCE_UTILS.md)
|
|
172
|
-
|
|
173
|
-
**Functions:**
|
|
174
|
-
- `measureAsync(operation, metadata?)` - Measure async operation
|
|
175
|
-
- `measureSync(operation, metadata?)` - Measure sync operation
|
|
176
|
-
- `debounce(func, wait)` - Create debounced function
|
|
177
|
-
- `throttle(func, limit)` - Create throttled function
|
|
178
|
-
|
|
179
|
-
**Classes:**
|
|
180
|
-
- `PerformanceTimer` - Timer for operations
|
|
181
|
-
- `performanceTracker` - Global performance tracker
|
|
182
|
-
|
|
183
|
-
### Data Transformation
|
|
184
|
-
|
|
185
|
-
**Refer to**: [`DATA_TRANSFORMER_UTILS.md`](./DATA_TRANSFORMER_UTILS.md)
|
|
186
|
-
|
|
187
|
-
**Functions:**
|
|
188
|
-
- `extractBase64Data(base64)` - Extract base64 from data URL
|
|
189
|
-
- `extractTextFromResponse(response)` - Extract text from Gemini response
|
|
190
|
-
|
|
191
|
-
### Image Preparation
|
|
192
|
-
|
|
193
|
-
**Refer to**: [`IMAGE_PREPARER_UTILS.md`](./IMAGE_PREPARER_UTILS.md)
|
|
194
|
-
|
|
195
|
-
**Functions:**
|
|
196
|
-
- `prepareImageFromUri(uri)` - Prepare image from URI
|
|
197
|
-
- `prepareImage(base64, mimeType)` - Prepare image from base64
|
|
198
|
-
- `isValidBase64(base64)` - Validate base64 format
|
|
199
|
-
|
|
200
|
-
### Input Builders
|
|
201
|
-
|
|
202
|
-
**Refer to**: [`INPUT_BUILDERS.md`](./INPUT_BUILDERS.md)
|
|
203
|
-
|
|
204
|
-
**Single Image Features:**
|
|
205
|
-
- `buildUpscaleInput(base64, options?)` - Build upscale input
|
|
206
|
-
- `buildPhotoRestoreInput(base64, options?)` - Build photo restore input
|
|
207
|
-
- `buildRemoveBackgroundInput(base64, options?)` - Build remove background input
|
|
208
|
-
- `buildReplaceBackgroundInput(base64, options)` - Build replace background input
|
|
209
|
-
|
|
210
|
-
**Dual Image Features:**
|
|
211
|
-
- `buildFaceSwapInput(sourceBase64, targetBase64, options?)` - Build face swap input
|
|
212
|
-
|
|
213
|
-
**Video Features:**
|
|
214
|
-
- `buildAIHugInput(base64, options?)` - Build AI hug input
|
|
215
|
-
- `buildAIKissInput(base64, options?)` - Build AI kiss input
|
|
216
|
-
- `buildVideoFromDualImagesInput(base64a, base64b, options?)` - Build dual image video input
|
|
217
|
-
|
|
218
|
-
## 🔗 Related Modules
|
|
219
|
-
|
|
220
|
-
- **Infrastructure README**: [`../infrastructure/README.md`](../infrastructure/README.md)
|
|
221
|
-
- **Services**: [`../services/README.md`](../services/README.md)
|
|
222
|
-
- **Domain Types**: [`../../domain/README.md`](../../domain/README.md)
|
|
223
|
-
|
|
224
|
-
## 📋 Error Categories
|
|
225
|
-
|
|
226
|
-
### Retryable Errors
|
|
227
|
-
- `QUOTA_EXCEEDED` - API quota exceeded
|
|
228
|
-
- `RATE_LIMIT` - Rate limit hit
|
|
229
|
-
- `NETWORK` - Network error
|
|
230
|
-
- `TIMEOUT` - Request timeout
|
|
231
|
-
- `SERVER` - Server error (5xx)
|
|
232
|
-
|
|
233
|
-
### Non-Retryable Errors
|
|
234
|
-
- `AUTHENTICATION` - Invalid credentials
|
|
235
|
-
- `SAFETY` - Safety filter triggered
|
|
236
|
-
- `MODEL_NOT_FOUND` - Invalid model
|
|
237
|
-
- `VALIDATION` - Invalid request
|
|
238
|
-
- `UNKNOWN` - Unknown error
|
|
239
|
-
|
|
240
|
-
## 🎓 Usage Patterns
|
|
241
|
-
|
|
242
|
-
### Error Handling Pattern
|
|
243
|
-
1. Catch API error
|
|
244
|
-
2. Map to GeminiError
|
|
245
|
-
3. Check if retryable
|
|
246
|
-
4. Retry or fail appropriately
|
|
247
|
-
5. Log error for debugging
|
|
248
|
-
|
|
249
|
-
### Model Validation Pattern
|
|
250
|
-
1. Get user input for model
|
|
251
|
-
2. Validate with isValidModel()
|
|
252
|
-
3. Get safe model with fallback
|
|
253
|
-
4. Check model category
|
|
254
|
-
5. Use validated model
|
|
255
|
-
|
|
256
|
-
### Performance Measurement Pattern
|
|
257
|
-
1. Wrap operation with measureAsync()
|
|
258
|
-
2. Record duration to performanceTracker
|
|
259
|
-
3. Analyze metrics periodically
|
|
260
|
-
4. Identify optimization opportunities
|
|
261
|
-
5. Iterate on improvements
|
|
262
|
-
|
|
263
|
-
### Image Processing Pattern
|
|
264
|
-
1. Validate image source
|
|
265
|
-
2. Prepare image with utilities
|
|
266
|
-
3. Build input for feature
|
|
267
|
-
4. Send to image service
|
|
268
|
-
5. Handle result/errors
|
|
269
|
-
|
|
270
|
-
## 🚨 Common Pitfalls
|
|
271
|
-
|
|
272
|
-
### Don't
|
|
273
|
-
- Skip model validation
|
|
274
|
-
- Ignore error types
|
|
275
|
-
- Measure too frequently (performance impact)
|
|
276
|
-
- Mutate input data
|
|
277
|
-
- Assume all models support all features
|
|
278
|
-
|
|
279
|
-
### Do
|
|
280
|
-
- Always validate model IDs
|
|
281
|
-
- Handle all error categories
|
|
282
|
-
- Use debounce/throttle for frequent operations
|
|
283
|
-
- Keep transformations pure
|
|
284
|
-
- Check model capabilities
|
|
285
|
-
|
|
286
|
-
---
|
|
287
|
-
|
|
288
|
-
**Last Updated**: 2025-01-08
|
|
289
|
-
**See Also**: [AI_GUIDELINES.md](../../../../AI_GUIDELINES.md)
|