@umituz/react-native-ai-pruna-provider 1.0.20 → 1.0.22
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-pruna-provider",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.22",
|
|
4
4
|
"description": "Pruna AI provider for React Native - implements IAIProvider interface for unified AI generation",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"@umituz/react-native-ai-generation-content": ">=1.70.0",
|
|
36
|
+
"@umituz/react-native-design-system": ">=1.0.0",
|
|
36
37
|
"expo": ">=54.0.0",
|
|
37
38
|
"react": ">=19.0.0",
|
|
38
39
|
"react-native": ">=0.81.4"
|
|
@@ -16,12 +16,16 @@ import { PRUNA_BASE_URL, PRUNA_PREDICTIONS_URL, PRUNA_FILES_URL, UPLOAD_CONFIG }
|
|
|
16
16
|
import { generationLogCollector } from "../utils/log-collector";
|
|
17
17
|
import { detectMimeType } from "../utils/mime-detection.util";
|
|
18
18
|
import { getExtensionForMime } from "../utils/constants/mime.constants";
|
|
19
|
+
import {
|
|
20
|
+
base64ToTempFile,
|
|
21
|
+
deleteTempFile,
|
|
22
|
+
} from "@umituz/react-native-design-system/filesystem";
|
|
19
23
|
|
|
20
24
|
const TAG = 'pruna-api';
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* Upload a base64 file (image or audio) to Pruna's file storage.
|
|
24
|
-
*
|
|
28
|
+
* Uses design system filesystem for React Native compatibility.
|
|
25
29
|
* Returns the HTTPS file URL to use in predictions.
|
|
26
30
|
*/
|
|
27
31
|
export async function uploadFileToStorage(
|
|
@@ -42,70 +46,67 @@ export async function uploadFileToStorage(
|
|
|
42
46
|
|
|
43
47
|
generationLogCollector.log(sessionId, TAG, 'Uploading file to Pruna storage...');
|
|
44
48
|
|
|
45
|
-
//
|
|
46
|
-
const
|
|
49
|
+
// Use design system filesystem to create temp file (React Native compatible)
|
|
50
|
+
const tempUri = await base64ToTempFile(base64Data);
|
|
47
51
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
binaryStr = atob(raw);
|
|
52
|
-
} catch {
|
|
53
|
-
throw new Error("Invalid file format. Please provide base64 or a valid URL.");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Convert to byte array for MIME detection
|
|
57
|
-
const bytes = new Uint8Array(binaryStr.length);
|
|
58
|
-
for (let i = 0; i < binaryStr.length; i++) {
|
|
59
|
-
bytes[i] = binaryStr.charCodeAt(i);
|
|
52
|
+
if (!tempUri) {
|
|
53
|
+
throw new Error("Failed to create temporary file from base64 data");
|
|
60
54
|
}
|
|
61
55
|
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
try {
|
|
57
|
+
// Fetch the temp file to get a Blob
|
|
58
|
+
const response = await fetch(tempUri);
|
|
59
|
+
const blob = await response.blob();
|
|
60
|
+
generationLogCollector.log(sessionId, TAG, `Temp file created (${blob.size} bytes), uploading to Pruna...`);
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
uri: base64Data.startsWith('data:') ? base64Data : `data:${mime};base64,${raw}`,
|
|
69
|
-
type: mime,
|
|
70
|
-
name: `upload.${ext}`,
|
|
71
|
-
} as any);
|
|
62
|
+
// Create FormData with the blob
|
|
63
|
+
const formData = new FormData();
|
|
64
|
+
formData.append('content', blob, 'upload');
|
|
72
65
|
|
|
73
|
-
|
|
66
|
+
const startTime = Date.now();
|
|
74
67
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
68
|
+
// Apply timeout to prevent indefinite hangs
|
|
69
|
+
const uploadController = new AbortController();
|
|
70
|
+
const timeoutId = setTimeout(() => uploadController.abort(), UPLOAD_CONFIG.timeoutMs);
|
|
78
71
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
72
|
+
try {
|
|
73
|
+
const uploadResponse = await fetch(PRUNA_FILES_URL, {
|
|
74
|
+
method: 'POST',
|
|
75
|
+
headers: { 'apikey': apiKey },
|
|
76
|
+
body: formData,
|
|
77
|
+
signal: uploadController.signal,
|
|
78
|
+
});
|
|
86
79
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
80
|
+
if (!uploadResponse.ok) {
|
|
81
|
+
const err = await uploadResponse.json().catch(() => ({ message: uploadResponse.statusText }));
|
|
82
|
+
const errorMessage = (err as { message?: string }).message || `File upload error: ${uploadResponse.status}`;
|
|
83
|
+
generationLogCollector.error(sessionId, TAG, `File upload failed: ${errorMessage}`);
|
|
84
|
+
throw new Error(errorMessage);
|
|
85
|
+
}
|
|
93
86
|
|
|
94
|
-
|
|
95
|
-
|
|
87
|
+
const data: PrunaFileUploadResponse = await uploadResponse.json();
|
|
88
|
+
const fileUrl = data.urls?.get || `${PRUNA_FILES_URL}/${data.id}`;
|
|
96
89
|
|
|
97
|
-
|
|
98
|
-
|
|
90
|
+
const elapsed = Date.now() - startTime;
|
|
91
|
+
generationLogCollector.log(sessionId, TAG, `File upload completed in ${elapsed}ms → ${fileUrl}`);
|
|
99
92
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
93
|
+
return fileUrl;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
96
|
+
generationLogCollector.error(sessionId, TAG, `File upload timed out after ${UPLOAD_CONFIG.timeoutMs}ms`);
|
|
97
|
+
throw new Error(`File upload timed out after ${UPLOAD_CONFIG.timeoutMs}ms`);
|
|
98
|
+
}
|
|
99
|
+
throw error;
|
|
100
|
+
} finally {
|
|
101
|
+
clearTimeout(timeoutId);
|
|
105
102
|
}
|
|
106
|
-
throw error;
|
|
107
103
|
} finally {
|
|
108
|
-
|
|
104
|
+
// Cleanup temp file
|
|
105
|
+
try {
|
|
106
|
+
await deleteTempFile(tempUri);
|
|
107
|
+
} catch (cleanupError) {
|
|
108
|
+
generationLogCollector.warn(sessionId, TAG, `Failed to delete temp file: ${cleanupError}`);
|
|
109
|
+
}
|
|
109
110
|
}
|
|
110
111
|
}
|
|
111
112
|
|