@umituz/react-native-ai-gemini-provider 1.14.18 → 1.14.19

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/react-native-ai-gemini-provider",
3
- "version": "1.14.18",
3
+ "version": "1.14.19",
4
4
  "description": "Google Gemini AI provider for React Native applications",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -28,6 +28,10 @@ export interface GeminiGenerationConfig {
28
28
  stopSequences?: string[];
29
29
  /** Response modalities for multimodal output (TEXT, IMAGE) */
30
30
  responseModalities?: Array<"TEXT" | "IMAGE">;
31
+ /** Response MIME type for structured output (e.g., "application/json") */
32
+ responseMimeType?: string;
33
+ /** Response schema for structured JSON output */
34
+ responseSchema?: Record<string, unknown>;
31
35
  }
32
36
 
33
37
  export interface GeminiSafetySettings {
@@ -159,6 +159,94 @@ class GeminiTextGenerationService {
159
159
 
160
160
  return this.generateContent(model, contents, config);
161
161
  }
162
+
163
+ /**
164
+ * Generate structured JSON response with schema
165
+ */
166
+ async generateStructuredText<T>(
167
+ model: string,
168
+ prompt: string,
169
+ schema: Record<string, unknown>,
170
+ config?: Omit<GeminiGenerationConfig, "responseMimeType" | "responseSchema">,
171
+ ): Promise<T> {
172
+ const generationConfig: GeminiGenerationConfig = {
173
+ ...config,
174
+ responseMimeType: "application/json",
175
+ responseSchema: schema as unknown as undefined,
176
+ };
177
+
178
+ const contents: GeminiContent[] = [
179
+ { parts: [{ text: prompt }], role: "user" },
180
+ ];
181
+
182
+ const response = await this.generateContent(model, contents, generationConfig);
183
+ const text = extractTextFromResponse(response);
184
+
185
+ // Clean and parse JSON (remove markdown code blocks if present)
186
+ const cleanedText = text.replace(/```json\n?/g, "").replace(/```\n?/g, "").trim();
187
+
188
+ try {
189
+ return JSON.parse(cleanedText) as T;
190
+ } catch (error) {
191
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
192
+ // eslint-disable-next-line no-console
193
+ console.error("[Gemini] Failed to parse structured response:", {
194
+ text: cleanedText.substring(0, 200),
195
+ error: error instanceof Error ? error.message : String(error),
196
+ });
197
+ }
198
+ throw new Error(`Failed to parse structured response: ${error instanceof Error ? error.message : String(error)}`);
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Generate structured JSON response with images and schema
204
+ */
205
+ async generateStructuredTextWithImages<T>(
206
+ model: string,
207
+ prompt: string,
208
+ images: Array<{ base64: string; mimeType: string }>,
209
+ schema: Record<string, unknown>,
210
+ config?: Omit<GeminiGenerationConfig, "responseMimeType" | "responseSchema">,
211
+ ): Promise<T> {
212
+ const generationConfig: GeminiGenerationConfig = {
213
+ ...config,
214
+ responseMimeType: "application/json",
215
+ responseSchema: schema as unknown as undefined,
216
+ };
217
+
218
+ const parts: GeminiContent["parts"] = [{ text: prompt }];
219
+
220
+ for (const image of images) {
221
+ parts.push({
222
+ inlineData: {
223
+ mimeType: image.mimeType,
224
+ data: extractBase64Data(image.base64),
225
+ },
226
+ });
227
+ }
228
+
229
+ const contents: GeminiContent[] = [{ parts, role: "user" }];
230
+
231
+ const response = await this.generateContent(model, contents, generationConfig);
232
+ const text = extractTextFromResponse(response);
233
+
234
+ // Clean and parse JSON
235
+ const cleanedText = text.replace(/```json\n?/g, "").replace(/```\n?/g, "").trim();
236
+
237
+ try {
238
+ return JSON.parse(cleanedText) as T;
239
+ } catch (error) {
240
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
241
+ // eslint-disable-next-line no-console
242
+ console.error("[Gemini] Failed to parse structured response:", {
243
+ text: cleanedText.substring(0, 200),
244
+ error: error instanceof Error ? error.message : String(error),
245
+ });
246
+ }
247
+ throw new Error(`Failed to parse structured response: ${error instanceof Error ? error.message : String(error)}`);
248
+ }
249
+ }
162
250
  }
163
251
 
164
252
  export const geminiTextGenerationService = new GeminiTextGenerationService();