@@ -27,33 +27,41 @@ async function waitForFileProcessing(
27
27
async function uploadGetUpdateDelete ( fileInput , path ) {
28
28
const client = new AI21 ( { apiKey : process . env . AI21_API_KEY } ) ;
29
29
try {
30
- console . log ( `Uploading file with id ${ fileInput } ` ) ;
30
+ console . log ( `Starting upload for file:` , typeof fileInput ) ;
31
31
const uploadFileResponse : UploadFileResponse = await client . files . create ( {
32
32
file : fileInput ,
33
33
path : path ,
34
34
} ) ;
35
- console . log ( `Uploaded file with id ${ uploadFileResponse } ` ) ;
35
+ console . log ( `✓ Upload completed. File ID: ${ uploadFileResponse . fileId } ` ) ;
36
36
37
+ console . log ( 'Waiting for file processing...' ) ;
37
38
let file : FileResponse = await waitForFileProcessing ( client , uploadFileResponse . fileId ) ;
38
- console . log ( file ) ;
39
+ console . log ( `✓ File processing completed with status: ${ file . status } ` ) ;
39
40
40
41
if ( file . status === 'PROCESSED' ) {
41
- console . log ( 'Now updating the file labels and publicUrl ...' ) ;
42
+ console . log ( 'Starting file update ...' ) ;
42
43
await client . files . update ( {
43
44
fileId : uploadFileResponse . fileId ,
44
45
labels : [ 'test99' ] ,
45
46
publicUrl : 'https://www.miri.com' ,
46
47
} ) ;
47
48
file = await client . files . get ( uploadFileResponse . fileId ) ;
48
- console . log ( file ) ;
49
+ console . log ( '✓ File update completed' ) ;
49
50
} else {
50
- console . log ( `File did not processed well, ended with status ${ file . status } ` ) ;
51
+ console . log ( `⚠ File processing failed with status ${ file . status } ` ) ;
52
+ return ; // Exit early if processing failed
51
53
}
52
54
53
- console . log ( 'Now deleting the file' ) ;
55
+ console . log ( 'Starting file deletion... ' ) ;
54
56
await client . files . delete ( uploadFileResponse . fileId ) ;
57
+ console . log ( '✓ File deletion completed' ) ;
58
+
59
+ // Add buffer time between operations
60
+ await sleep ( 2000 ) ;
61
+
55
62
} catch ( error ) {
56
- console . error ( 'Error:' , error ) ;
63
+ console . error ( '❌ Error in uploadGetUpdateDelete:' , error ) ;
64
+ throw error ;
57
65
}
58
66
}
59
67
@@ -71,22 +79,29 @@ if (isBrowser) {
71
79
/* Run all operations sequentially */
72
80
( async ( ) => {
73
81
try {
82
+ console . log ( '=== Starting first operation ===' ) ;
74
83
// First operation - upload file from path
75
84
const filePath = path . join ( process . cwd ( ) , 'examples/studio/conversational-rag/files' , 'meerkat.txt' ) ;
76
85
await uploadGetUpdateDelete ( filePath , Date . now ( ) . toString ( ) ) ;
86
+ console . log ( '=== First operation completed ===\n' ) ;
87
+ await sleep ( 2000 ) ;
77
88
89
+ console . log ( '=== Starting second operation ===' ) ;
78
90
// Second operation - upload file from File instance
79
91
const fileContent = Buffer . from (
80
92
'Opossums are members of the marsupial order Didelphimorphia endemic to the Americas.' ,
81
93
) ;
82
94
const dummyFile = new File ( [ fileContent ] , 'example.txt' , { type : 'text/plain' } ) ;
83
- console . log ( 'Running file upload in Node environment' ) ;
84
95
await uploadGetUpdateDelete ( dummyFile , Date . now ( ) . toString ( ) ) ;
96
+ console . log ( '=== Second operation completed ===\n' ) ;
97
+ await sleep ( 2000 ) ;
85
98
86
- // Finally, list the files
99
+ console . log ( '=== Starting file listing ===' ) ;
87
100
await listFiles ( ) ;
101
+ console . log ( '=== File listing completed ===' ) ;
88
102
} catch ( error ) {
89
- console . error ( error ) ;
103
+ console . error ( '❌ Main execution error:' , error ) ;
104
+ process . exit ( 1 ) ; // Exit with error code if something fails
90
105
}
91
106
} ) ( ) ;
92
107
}
0 commit comments