1
1
import { AI21 , FileResponse , UploadFileResponse } from 'ai21' ;
2
2
import path from 'path' ;
3
+ import fs from 'fs' ;
3
4
4
5
function sleep ( ms ) {
5
6
return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
@@ -72,6 +73,23 @@ async function listFiles() {
72
73
73
74
const isBrowser = typeof window !== 'undefined' && typeof window . document !== 'undefined' ;
74
75
76
+ const createNodeFile = ( content : Buffer , filename : string , type : string ) => {
77
+ if ( process . platform === 'linux' ) {
78
+ console . log ( 'Running on Linux (GitHub Actions)' ) ;
79
+ // Special handling for Linux (GitHub Actions)
80
+ return {
81
+ name : filename ,
82
+ type : type ,
83
+ buffer : content ,
84
+ [ Symbol . toStringTag ] : 'File'
85
+ } ;
86
+ } else {
87
+ console . log ( 'Running on other platforms' ) ;
88
+ // Regular handling for other platforms
89
+ return new File ( [ content ] , filename , { type } ) ;
90
+ }
91
+ } ;
92
+
75
93
if ( isBrowser ) {
76
94
console . log ( 'Cannot run upload examples in Browser environment' ) ;
77
95
} else {
@@ -89,7 +107,10 @@ if (isBrowser) {
89
107
try {
90
108
console . log ( '=== Starting first operation ===' ) ;
91
109
// First operation - upload file from path
92
- const filePath = path . join ( process . cwd ( ) , 'examples/studio/conversational-rag/files' , 'meerkat.txt' ) ;
110
+ const filePath = path . resolve ( process . cwd ( ) , 'examples/studio/conversational-rag/files' , 'meerkat.txt' ) ;
111
+ if ( ! fs . existsSync ( filePath ) ) {
112
+ throw new Error ( `File not found: ${ filePath } ` ) ;
113
+ }
93
114
await uploadGetUpdateDelete ( filePath , Date . now ( ) . toString ( ) ) ;
94
115
console . log ( '=== First operation completed ===\n' ) ;
95
116
await sleep ( 2000 ) ;
@@ -99,7 +120,7 @@ if (isBrowser) {
99
120
const fileContent = Buffer . from (
100
121
'Opossums are members of the marsupial order Didelphimorphia endemic to the Americas.' ,
101
122
) ;
102
- const dummyFile = new File ( [ fileContent ] , 'example.txt' , { type : 'text/plain' } ) ;
123
+ const dummyFile = createNodeFile ( fileContent , 'example.txt' , 'text/plain' ) ;
103
124
await uploadGetUpdateDelete ( dummyFile , Date . now ( ) . toString ( ) ) ;
104
125
console . log ( '=== Second operation completed ===\n' ) ;
105
126
await sleep ( 2000 ) ;
0 commit comments