@umituz/react-native-ai-gemini-provider 1.9.2 → 1.10.0
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 +2 -2
- package/src/index.ts +21 -0
- package/src/providers/ProviderConfig.ts +137 -0
- package/src/providers/ProviderFactory.ts +118 -0
- package/src/providers/index.ts +25 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-ai-gemini-provider",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Google Gemini AI provider for React Native applications",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -46,4 +46,4 @@
|
|
|
46
46
|
"README.md",
|
|
47
47
|
"LICENSE"
|
|
48
48
|
]
|
|
49
|
-
}
|
|
49
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -93,3 +93,24 @@ export type {
|
|
|
93
93
|
UseGeminiOptions,
|
|
94
94
|
UseGeminiReturn,
|
|
95
95
|
} from "./presentation/hooks";
|
|
96
|
+
|
|
97
|
+
// =============================================================================
|
|
98
|
+
// PROVIDER CONFIGURATION - Tier-based Setup
|
|
99
|
+
// =============================================================================
|
|
100
|
+
|
|
101
|
+
export {
|
|
102
|
+
providerFactory,
|
|
103
|
+
resolveProviderConfig,
|
|
104
|
+
getCostOptimizedConfig,
|
|
105
|
+
getQualityOptimizedConfig,
|
|
106
|
+
} from "./providers";
|
|
107
|
+
|
|
108
|
+
export type {
|
|
109
|
+
SubscriptionTier,
|
|
110
|
+
QualityPreference,
|
|
111
|
+
ProviderPreferences,
|
|
112
|
+
ProviderConfigInput,
|
|
113
|
+
ResolvedProviderConfig,
|
|
114
|
+
OptimizationStrategy,
|
|
115
|
+
ProviderFactoryOptions,
|
|
116
|
+
} from "./providers";
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider Configuration
|
|
3
|
+
* Centralized configuration for AI provider with tier-based settings
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type SubscriptionTier = "free" | "premium";
|
|
7
|
+
|
|
8
|
+
export type QualityPreference = "fast" | "balanced" | "high";
|
|
9
|
+
|
|
10
|
+
export interface ProviderPreferences {
|
|
11
|
+
/** Quality preference (fast = Flash, balanced = Flash, high = Pro) */
|
|
12
|
+
quality?: QualityPreference;
|
|
13
|
+
/** Maximum retry attempts for failed requests */
|
|
14
|
+
maxRetries?: number;
|
|
15
|
+
/** Base delay for retry backoff (ms) */
|
|
16
|
+
baseDelay?: number;
|
|
17
|
+
/** Maximum delay for retry backoff (ms) */
|
|
18
|
+
maxDelay?: number;
|
|
19
|
+
/** Request timeout (ms) */
|
|
20
|
+
timeout?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ProviderConfigInput {
|
|
24
|
+
/** API key for authentication */
|
|
25
|
+
apiKey: string;
|
|
26
|
+
/** Subscription tier (determines default models and limits) */
|
|
27
|
+
subscriptionTier: SubscriptionTier;
|
|
28
|
+
/** Optional user preferences (overrides tier defaults) */
|
|
29
|
+
preferences?: ProviderPreferences;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ResolvedProviderConfig {
|
|
33
|
+
apiKey: string;
|
|
34
|
+
subscriptionTier: SubscriptionTier;
|
|
35
|
+
textModel: string;
|
|
36
|
+
imageGenerationModel: string;
|
|
37
|
+
imageEditModel: string;
|
|
38
|
+
maxRetries: number;
|
|
39
|
+
baseDelay: number;
|
|
40
|
+
maxDelay: number;
|
|
41
|
+
timeout: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Default configurations per subscription tier
|
|
46
|
+
*/
|
|
47
|
+
const TIER_DEFAULTS: Record<
|
|
48
|
+
SubscriptionTier,
|
|
49
|
+
Omit<ResolvedProviderConfig, "apiKey" | "subscriptionTier">
|
|
50
|
+
> = {
|
|
51
|
+
free: {
|
|
52
|
+
textModel: "gemini-2.5-flash",
|
|
53
|
+
imageGenerationModel: "imagen-4.0-generate-001",
|
|
54
|
+
imageEditModel: "gemini-2.5-flash-image", // Fast model for free tier
|
|
55
|
+
maxRetries: 1, // Limited retries for free tier
|
|
56
|
+
baseDelay: 1000,
|
|
57
|
+
maxDelay: 5000,
|
|
58
|
+
timeout: 30000, // 30 seconds
|
|
59
|
+
},
|
|
60
|
+
premium: {
|
|
61
|
+
textModel: "gemini-2.5-flash",
|
|
62
|
+
imageGenerationModel: "imagen-4.0-generate-001",
|
|
63
|
+
imageEditModel: "gemini-3-pro-image-preview", // High quality for premium
|
|
64
|
+
maxRetries: 2, // More retries for premium
|
|
65
|
+
baseDelay: 1000,
|
|
66
|
+
maxDelay: 10000,
|
|
67
|
+
timeout: 60000, // 60 seconds
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Quality preference to model mapping
|
|
73
|
+
*/
|
|
74
|
+
const QUALITY_TO_MODEL: Record<QualityPreference, string> = {
|
|
75
|
+
fast: "gemini-2.5-flash-image",
|
|
76
|
+
balanced: "gemini-2.5-flash-image",
|
|
77
|
+
high: "gemini-3-pro-image-preview",
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Resolve provider configuration based on tier and preferences
|
|
82
|
+
*/
|
|
83
|
+
export function resolveProviderConfig(
|
|
84
|
+
input: ProviderConfigInput,
|
|
85
|
+
): ResolvedProviderConfig {
|
|
86
|
+
const tierDefaults = TIER_DEFAULTS[input.subscriptionTier];
|
|
87
|
+
const preferences = input.preferences || {};
|
|
88
|
+
|
|
89
|
+
// Override image edit model if quality preference is provided
|
|
90
|
+
let imageEditModel = tierDefaults.imageEditModel;
|
|
91
|
+
if (preferences.quality) {
|
|
92
|
+
imageEditModel = QUALITY_TO_MODEL[preferences.quality];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
apiKey: input.apiKey,
|
|
97
|
+
subscriptionTier: input.subscriptionTier,
|
|
98
|
+
textModel: tierDefaults.textModel,
|
|
99
|
+
imageGenerationModel: tierDefaults.imageGenerationModel,
|
|
100
|
+
imageEditModel,
|
|
101
|
+
maxRetries: preferences.maxRetries ?? tierDefaults.maxRetries,
|
|
102
|
+
baseDelay: preferences.baseDelay ?? tierDefaults.baseDelay,
|
|
103
|
+
maxDelay: preferences.maxDelay ?? tierDefaults.maxDelay,
|
|
104
|
+
timeout: preferences.timeout ?? tierDefaults.timeout,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get cost-optimized config (always use fastest/cheapest models)
|
|
110
|
+
* Useful for development or cost-sensitive scenarios
|
|
111
|
+
*/
|
|
112
|
+
export function getCostOptimizedConfig(
|
|
113
|
+
input: ProviderConfigInput,
|
|
114
|
+
): ResolvedProviderConfig {
|
|
115
|
+
const resolved = resolveProviderConfig(input);
|
|
116
|
+
return {
|
|
117
|
+
...resolved,
|
|
118
|
+
imageEditModel: "gemini-2.5-flash-image", // Always use fast model
|
|
119
|
+
maxRetries: 0, // No retries to minimize cost
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Get quality-optimized config (always use best models)
|
|
125
|
+
* Useful for production or quality-critical scenarios
|
|
126
|
+
*/
|
|
127
|
+
export function getQualityOptimizedConfig(
|
|
128
|
+
input: ProviderConfigInput,
|
|
129
|
+
): ResolvedProviderConfig {
|
|
130
|
+
const resolved = resolveProviderConfig(input);
|
|
131
|
+
return {
|
|
132
|
+
...resolved,
|
|
133
|
+
imageEditModel: "gemini-3-pro-image-preview", // Always use pro model
|
|
134
|
+
maxRetries: 2, // More retries for reliability
|
|
135
|
+
timeout: 60000, // Longer timeout
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider Factory
|
|
3
|
+
* Creates and configures AI provider instances with tier-based settings
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { geminiClientCoreService } from "../infrastructure/services/gemini-client-core.service";
|
|
7
|
+
import type { GeminiConfig } from "../domain/entities";
|
|
8
|
+
import type {
|
|
9
|
+
ProviderConfigInput,
|
|
10
|
+
ResolvedProviderConfig,
|
|
11
|
+
} from "./ProviderConfig";
|
|
12
|
+
import {
|
|
13
|
+
resolveProviderConfig,
|
|
14
|
+
getCostOptimizedConfig,
|
|
15
|
+
getQualityOptimizedConfig,
|
|
16
|
+
} from "./ProviderConfig";
|
|
17
|
+
|
|
18
|
+
declare const __DEV__: boolean;
|
|
19
|
+
|
|
20
|
+
export type OptimizationStrategy = "cost" | "quality" | "balanced";
|
|
21
|
+
|
|
22
|
+
export interface ProviderFactoryOptions extends ProviderConfigInput {
|
|
23
|
+
/** Optimization strategy (overrides tier defaults) */
|
|
24
|
+
strategy?: OptimizationStrategy;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class ProviderFactory {
|
|
28
|
+
private currentConfig: ResolvedProviderConfig | null = null;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Initialize provider with tier-based configuration
|
|
32
|
+
*/
|
|
33
|
+
initialize(options: ProviderFactoryOptions): void {
|
|
34
|
+
let config: ResolvedProviderConfig;
|
|
35
|
+
|
|
36
|
+
// Apply optimization strategy
|
|
37
|
+
switch (options.strategy) {
|
|
38
|
+
case "cost":
|
|
39
|
+
config = getCostOptimizedConfig(options);
|
|
40
|
+
break;
|
|
41
|
+
case "quality":
|
|
42
|
+
config = getQualityOptimizedConfig(options);
|
|
43
|
+
break;
|
|
44
|
+
case "balanced":
|
|
45
|
+
default:
|
|
46
|
+
config = resolveProviderConfig(options);
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.currentConfig = config;
|
|
51
|
+
|
|
52
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
53
|
+
console.log("[ProviderFactory] Initializing with config:", {
|
|
54
|
+
tier: config.subscriptionTier,
|
|
55
|
+
strategy: options.strategy || "balanced",
|
|
56
|
+
textModel: config.textModel,
|
|
57
|
+
imageEditModel: config.imageEditModel,
|
|
58
|
+
maxRetries: config.maxRetries,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Initialize Gemini client with resolved config
|
|
63
|
+
const geminiConfig: GeminiConfig = {
|
|
64
|
+
apiKey: config.apiKey,
|
|
65
|
+
imageModel: config.imageEditModel,
|
|
66
|
+
maxRetries: config.maxRetries,
|
|
67
|
+
baseDelay: config.baseDelay,
|
|
68
|
+
maxDelay: config.maxDelay,
|
|
69
|
+
defaultTimeoutMs: config.timeout,
|
|
70
|
+
defaultModel: config.textModel,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
geminiClientCoreService.initialize(geminiConfig);
|
|
74
|
+
|
|
75
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
76
|
+
console.log("[ProviderFactory] Provider initialized successfully");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get current resolved configuration
|
|
82
|
+
*/
|
|
83
|
+
getConfig(): ResolvedProviderConfig | null {
|
|
84
|
+
return this.currentConfig;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Check if provider is initialized
|
|
89
|
+
*/
|
|
90
|
+
isInitialized(): boolean {
|
|
91
|
+
return this.currentConfig !== null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Update configuration without re-initializing
|
|
96
|
+
* Useful for switching models or adjusting settings
|
|
97
|
+
*/
|
|
98
|
+
updateConfig(updates: Partial<ProviderConfigInput>): void {
|
|
99
|
+
if (!this.currentConfig) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
"Provider not initialized. Call initialize() first.",
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const newInput: ProviderConfigInput = {
|
|
106
|
+
apiKey: updates.apiKey || this.currentConfig.apiKey,
|
|
107
|
+
subscriptionTier:
|
|
108
|
+
updates.subscriptionTier || this.currentConfig.subscriptionTier,
|
|
109
|
+
preferences: {
|
|
110
|
+
...updates.preferences,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
this.initialize(newInput);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const providerFactory = new ProviderFactory();
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider Configuration & Factory
|
|
3
|
+
* Centralized configuration system for tier-based AI provider setup
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
resolveProviderConfig,
|
|
8
|
+
getCostOptimizedConfig,
|
|
9
|
+
getQualityOptimizedConfig,
|
|
10
|
+
} from "./ProviderConfig";
|
|
11
|
+
|
|
12
|
+
export type {
|
|
13
|
+
SubscriptionTier,
|
|
14
|
+
QualityPreference,
|
|
15
|
+
ProviderPreferences,
|
|
16
|
+
ProviderConfigInput,
|
|
17
|
+
ResolvedProviderConfig,
|
|
18
|
+
} from "./ProviderConfig";
|
|
19
|
+
|
|
20
|
+
export { providerFactory } from "./ProviderFactory";
|
|
21
|
+
|
|
22
|
+
export type {
|
|
23
|
+
OptimizationStrategy,
|
|
24
|
+
ProviderFactoryOptions,
|
|
25
|
+
} from "./ProviderFactory";
|