Skip to content

Commit 6512e08

Browse files
committed
refac
1 parent 0ad397c commit 6512e08

2 files changed

Lines changed: 64 additions & 46 deletions

File tree

src/lib/apis/terminal/index.ts

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ export const getCwd = async (baseUrl: string, apiKey: string, sessionId?: string
5858
export 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 (
7980
export 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 (
106108
export 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 (
123126
export 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 (
172181
export 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 (
197209
export 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) => {

src/lib/components/chat/FileNav.svelte

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@
331331
savedPath = path;
332332
pushNavHistory(path);
333333
334-
const result = await listFiles(terminal.url, terminal.key, path);
334+
const result = await listFiles(terminal.url, terminal.key, path, chatId ?? undefined);
335335
loading = false;
336336
337337
// Set working directory on the terminal server (fire-and-forget)
@@ -366,22 +366,22 @@
366366
clearFilePreview();
367367
368368
if (isImage(filePath)) {
369-
const result = await downloadFileBlob(terminal.url, terminal.key, filePath);
369+
const result = await downloadFileBlob(terminal.url, terminal.key, filePath, chatId ?? undefined);
370370
if (result) fileImageUrl = URL.createObjectURL(result.blob);
371371
} else if (isVideo(filePath)) {
372-
const result = await downloadFileBlob(terminal.url, terminal.key, filePath);
372+
const result = await downloadFileBlob(terminal.url, terminal.key, filePath, chatId ?? undefined);
373373
if (result) fileVideoUrl = URL.createObjectURL(result.blob);
374374
} else if (isAudio(filePath)) {
375-
const result = await downloadFileBlob(terminal.url, terminal.key, filePath);
375+
const result = await downloadFileBlob(terminal.url, terminal.key, filePath, chatId ?? undefined);
376376
if (result) fileAudioUrl = URL.createObjectURL(result.blob);
377377
} else if (isPdf(filePath)) {
378-
const result = await downloadFileBlob(terminal.url, terminal.key, filePath);
378+
const result = await downloadFileBlob(terminal.url, terminal.key, filePath, chatId ?? undefined);
379379
if (result) filePdfData = await result.blob.arrayBuffer();
380380
} else if (isSqlite(filePath)) {
381-
const result = await downloadFileBlob(terminal.url, terminal.key, filePath);
381+
const result = await downloadFileBlob(terminal.url, terminal.key, filePath, chatId ?? undefined);
382382
if (result) fileSqliteData = await result.blob.arrayBuffer();
383383
} else if (isOffice(filePath)) {
384-
const result = await downloadFileBlob(terminal.url, terminal.key, filePath);
384+
const result = await downloadFileBlob(terminal.url, terminal.key, filePath, chatId ?? undefined);
385385
if (result) {
386386
const ext = getFileExt(filePath);
387387
const arrayBuffer = await result.blob.arrayBuffer();
@@ -414,7 +414,7 @@
414414
}
415415
}
416416
} else {
417-
fileContent = await readFile(terminal.url, terminal.key, filePath);
417+
fileContent = await readFile(terminal.url, terminal.key, filePath, chatId ?? undefined);
418418
}
419419
fileLoading = false;
420420
};
@@ -427,7 +427,7 @@
427427
const isDir = path.endsWith('/');
428428
const result = isDir
429429
? await archiveFromTerminal(terminal.url, terminal.key, [path.replace(/\/$/, '')])
430-
: await downloadFileBlob(terminal.url, terminal.key, path);
430+
: await downloadFileBlob(terminal.url, terminal.key, path, chatId ?? undefined);
431431
if (!result) return;
432432
const url = URL.createObjectURL(result.blob);
433433
const a = document.createElement('a');
@@ -459,7 +459,7 @@
459459
460460
uploading = true;
461461
for (const file of droppedFiles) {
462-
await uploadToTerminal(terminal.url, terminal.key, currentPath, file);
462+
await uploadToTerminal(terminal.url, terminal.key, currentPath, file, chatId ?? undefined);
463463
}
464464
uploading = false;
465465
await loadDir(currentPath);
@@ -471,7 +471,7 @@
471471
472472
uploading = true;
473473
for (const file of files) {
474-
await uploadToTerminal(terminal.url, terminal.key, currentPath, file);
474+
await uploadToTerminal(terminal.url, terminal.key, currentPath, file, chatId ?? undefined);
475475
}
476476
uploading = false;
477477
await loadDir(currentPath);
@@ -494,7 +494,7 @@
494494
const terminal = selectedTerminal;
495495
if (!terminal) return;
496496
497-
const result = await createDirectory(terminal.url, terminal.key, `${currentPath}${name}`);
497+
const result = await createDirectory(terminal.url, terminal.key, `${currentPath}${name}`, chatId ?? undefined);
498498
toast[result ? 'success' : 'error'](
499499
$i18n.t(result ? 'Folder created' : 'Failed to create folder')
500500
);
@@ -529,7 +529,7 @@
529529
const terminal = selectedTerminal;
530530
if (!terminal) return;
531531
532-
const result = await deleteEntry(terminal.url, terminal.key, path);
532+
const result = await deleteEntry(terminal.url, terminal.key, path, chatId ?? undefined);
533533
toast[result ? 'success' : 'error'](
534534
$i18n.t(result ? '{{name}} deleted' : 'Failed to delete {{name}}', { name })
535535
);
@@ -555,7 +555,7 @@
555555
const sourceDir = source.endsWith('/') ? source : source + '/';
556556
if (destFolder.startsWith(sourceDir)) return;
557557
558-
const result = await moveEntry(terminal.url, terminal.key, source, destination);
558+
const result = await moveEntry(terminal.url, terminal.key, source, destination, chatId ?? undefined);
559559
if ('error' in result) {
560560
toast.error(result.error);
561561
} else {
@@ -574,7 +574,7 @@
574574
575575
if (oldPath === destination) return;
576576
577-
const result = await moveEntry(terminal.url, terminal.key, oldPath, destination);
577+
const result = await moveEntry(terminal.url, terminal.key, oldPath, destination, chatId ?? undefined);
578578
if ('error' in result) {
579579
toast.error(result.error);
580580
} else {

0 commit comments

Comments
 (0)