@@ -58,13 +58,14 @@ export const getCwd = async (baseUrl: string, apiKey: string, sessionId?: string
5858export const listFiles = async (
5959 baseUrl : string ,
6060 apiKey : string ,
61- path : string = '/'
61+ path : string = '/' ,
62+ sessionId ?: string
6263) : Promise < FileEntry [ ] | null > => {
6364 // The endpoint uses `directory` as the query param name
6465 const url = `${ baseUrl . replace ( / \/ $ / , '' ) } /files/list?directory=${ encodeURIComponent ( path ) } ` ;
65- const res = await fetch ( url , {
66- headers : { Authorization : `Bearer ${ apiKey } ` }
67- } )
66+ const headers : Record < string , string > = { Authorization : `Bearer ${ apiKey } ` } ;
67+ if ( sessionId ) headers [ 'X-Session-Id' ] = sessionId ;
68+ const res = await fetch ( url , { headers } )
6869 . then ( async ( res ) => {
6970 if ( ! res . ok ) throw await res . json ( ) ;
7071 return res . json ( ) ;
@@ -79,12 +80,13 @@ export const listFiles = async (
7980export const readFile = async (
8081 baseUrl : string ,
8182 apiKey : string ,
82- path : string
83+ path : string ,
84+ sessionId ?: string
8385) : Promise < string | null > => {
8486 const url = `${ baseUrl . replace ( / \/ $ / , '' ) } /files/read?path=${ encodeURIComponent ( path ) } ` ;
85- const res = await fetch ( url , {
86- headers : { Authorization : `Bearer ${ apiKey } ` }
87- } ) . catch ( ( err ) => {
87+ const headers : Record < string , string > = { Authorization : `Bearer ${ apiKey } ` } ;
88+ if ( sessionId ) headers [ 'X-Session-Id' ] = sessionId ;
89+ const res = await fetch ( url , { headers } ) . catch ( ( err ) => {
8890 console . error ( 'open-terminal readFile error:' , err ) ;
8991 return null ;
9092 } ) ;
@@ -106,12 +108,13 @@ export const readFile = async (
106108export const downloadFileBlob = async (
107109 baseUrl : string ,
108110 apiKey : string ,
109- path : string
111+ path : string ,
112+ sessionId ?: string
110113) : Promise < { blob : Blob ; filename : string } | null > => {
111114 const url = `${ baseUrl . replace ( / \/ $ / , '' ) } /files/view?path=${ encodeURIComponent ( path ) } ` ;
112- const res = await fetch ( url , {
113- headers : { Authorization : `Bearer ${ apiKey } ` }
114- } ) . catch ( ( ) => null ) ;
115+ const headers : Record < string , string > = { Authorization : `Bearer ${ apiKey } ` } ;
116+ if ( sessionId ) headers [ 'X-Session-Id' ] = sessionId ;
117+ const res = await fetch ( url , { headers } ) . catch ( ( ) => null ) ;
115118
116119 if ( ! res || ! res . ok ) return null ;
117120
@@ -123,15 +126,18 @@ export const downloadFileBlob = async (
123126export const archiveFromTerminal = async (
124127 baseUrl : string ,
125128 apiKey : string ,
126- paths : string [ ]
129+ paths : string [ ] ,
130+ sessionId ?: string
127131) : Promise < { blob : Blob ; filename : string } | null > => {
128132 const url = `${ baseUrl . replace ( / \/ $ / , '' ) } /files/archive` ;
133+ const headers : Record < string , string > = {
134+ Authorization : `Bearer ${ apiKey } ` ,
135+ 'Content-Type' : 'application/json'
136+ } ;
137+ if ( sessionId ) headers [ 'X-Session-Id' ] = sessionId ;
129138 const res = await fetch ( url , {
130139 method : 'POST' ,
131- headers : {
132- Authorization : `Bearer ${ apiKey } ` ,
133- 'Content-Type' : 'application/json'
134- } ,
140+ headers,
135141 body : JSON . stringify ( { paths } )
136142 } ) . catch ( ( ) => null ) ;
137143
@@ -148,14 +154,17 @@ export const uploadToTerminal = async (
148154 baseUrl : string ,
149155 apiKey : string ,
150156 directory : string ,
151- file : File
157+ file : File ,
158+ sessionId ?: string
152159) : Promise < { path : string ; size : number } | null > => {
153160 const url = `${ baseUrl . replace ( / \/ $ / , '' ) } /files/upload?directory=${ encodeURIComponent ( directory ) } ` ;
154161 const body = new FormData ( ) ;
155162 body . append ( 'file' , file ) ;
163+ const headers : Record < string , string > = { Authorization : `Bearer ${ apiKey } ` } ;
164+ if ( sessionId ) headers [ 'X-Session-Id' ] = sessionId ;
156165 const res = await fetch ( url , {
157166 method : 'POST' ,
158- headers : { Authorization : `Bearer ${ apiKey } ` } ,
167+ headers,
159168 body
160169 } )
161170 . then ( async ( res ) => {
@@ -172,15 +181,18 @@ export const uploadToTerminal = async (
172181export const createDirectory = async (
173182 baseUrl : string ,
174183 apiKey : string ,
175- path : string
184+ path : string ,
185+ sessionId ?: string
176186) : Promise < { path : string } | null > => {
177187 const url = `${ baseUrl . replace ( / \/ $ / , '' ) } /files/mkdir` ;
188+ const headers : Record < string , string > = {
189+ Authorization : `Bearer ${ apiKey } ` ,
190+ 'Content-Type' : 'application/json'
191+ } ;
192+ if ( sessionId ) headers [ 'X-Session-Id' ] = sessionId ;
178193 const res = await fetch ( url , {
179194 method : 'POST' ,
180- headers : {
181- Authorization : `Bearer ${ apiKey } ` ,
182- 'Content-Type' : 'application/json'
183- } ,
195+ headers,
184196 body : JSON . stringify ( { path } )
185197 } )
186198 . then ( async ( res ) => {
@@ -197,12 +209,15 @@ export const createDirectory = async (
197209export const deleteEntry = async (
198210 baseUrl : string ,
199211 apiKey : string ,
200- path : string
212+ path : string ,
213+ sessionId ?: string
201214) : Promise < { path : string ; type : string } | null > => {
202215 const url = `${ baseUrl . replace ( / \/ $ / , '' ) } /files/delete?path=${ encodeURIComponent ( path ) } ` ;
216+ const headers : Record < string , string > = { Authorization : `Bearer ${ apiKey } ` } ;
217+ if ( sessionId ) headers [ 'X-Session-Id' ] = sessionId ;
203218 const res = await fetch ( url , {
204219 method : 'DELETE' ,
205- headers : { Authorization : `Bearer ${ apiKey } ` }
220+ headers
206221 } )
207222 . then ( async ( res ) => {
208223 if ( ! res . ok ) throw await res . json ( ) ;
@@ -247,15 +262,18 @@ export const moveEntry = async (
247262 baseUrl : string ,
248263 apiKey : string ,
249264 source : string ,
250- destination : string
265+ destination : string ,
266+ sessionId ?: string
251267) : Promise < { source : string ; destination : string } | { error : string } > => {
252268 const url = `${ baseUrl . replace ( / \/ $ / , '' ) } /files/move` ;
269+ const headers : Record < string , string > = {
270+ Authorization : `Bearer ${ apiKey } ` ,
271+ 'Content-Type' : 'application/json'
272+ } ;
273+ if ( sessionId ) headers [ 'X-Session-Id' ] = sessionId ;
253274 const res = await fetch ( url , {
254275 method : 'POST' ,
255- headers : {
256- Authorization : `Bearer ${ apiKey } ` ,
257- 'Content-Type' : 'application/json'
258- } ,
276+ headers,
259277 body : JSON . stringify ( { source, destination } )
260278 } )
261279 . then ( async ( res ) => {
0 commit comments