Skip to content

Commit bd12aa9

Browse files
Algorithm5838github-actions[bot]
authored andcommitted
feat: show and auto-refresh Files panel for pyodide code executions
1 parent ae94f79 commit bd12aa9

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/lib/components/chat/ChatControls.svelte

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
settings,
2121
showFileNavPath,
2222
selectedTerminalId,
23-
user
23+
user,
24+
pyodideWorker
2425
} from '$lib/stores';
2526
2627
import { uploadFile } from '$lib/apis/files';
@@ -73,7 +74,8 @@
7374
$: showControlsTab = $user?.role === 'admin' || ($user?.permissions?.chat?.controls ?? true);
7475
$: showFilesTab =
7576
!!$selectedTerminalId ||
76-
(codeInterpreterEnabled && $config?.code?.interpreter_engine !== 'jupyter');
77+
(codeInterpreterEnabled && $config?.code?.interpreter_engine !== 'jupyter') ||
78+
!!$pyodideWorker;
7779
$: showOverviewTab = hasMessages;
7880
7981
// Tab fallback: if active tab becomes hidden, switch to next available
@@ -361,7 +363,7 @@
361363
/>
362364
{:else if activeTab === 'files' && $selectedTerminalId}
363365
<FileNav onAttach={handleTerminalAttach} />
364-
{:else if activeTab === 'files' && codeInterpreterEnabled}
366+
{:else if activeTab === 'files' && (codeInterpreterEnabled || !!$pyodideWorker)}
365367
<PyodideFileNav />
366368
{:else}
367369
<Controls embed={true} {models} bind:chatFiles bind:params />
@@ -512,7 +514,7 @@
512514
/>
513515
{:else if activeTab === 'files' && $selectedTerminalId}
514516
<FileNav onAttach={handleTerminalAttach} overlay={dragged} />
515-
{:else if activeTab === 'files' && codeInterpreterEnabled}
517+
{:else if activeTab === 'files' && (codeInterpreterEnabled || !!$pyodideWorker)}
516518
<PyodideFileNav overlay={dragged} />
517519
{:else}
518520
<Controls embed={true} {models} bind:chatFiles bind:params />

src/lib/components/chat/PyodideFileNav.svelte

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,17 @@
283283
// ── Lifecycle ─────────────────────────────────────────────────────────
284284
285285
onMount(() => {
286-
ensureWorker();
286+
const worker = ensureWorker();
287287
loadDir(currentPath);
288+
289+
// Auto-refresh the file listing when the worker signals the FS changed
290+
const onWorkerMessage = (e: MessageEvent) => {
291+
if (e.data?.type === 'fs:changed' && !selectedFile && !loading) {
292+
loadDir(currentPath);
293+
}
294+
};
295+
worker.addEventListener('message', onWorkerMessage);
296+
return () => worker.removeEventListener('message', onWorkerMessage);
288297
});
289298
</script>
290299

src/lib/workers/pyodide.worker.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ async function executeCode(
189189
persistFS();
190190
}
191191

192+
// Ensure working directory is /mnt/uploads so relative file writes appear in the panel
193+
try {
194+
self.pyodide.FS.chdir('/mnt/uploads');
195+
} catch (e) {
196+
console.warn('[pyodide] chdir to /mnt/uploads failed:', e);
197+
}
198+
192199
try {
193200
// check if matplotlib is imported in the code
194201
if (code.includes('matplotlib')) {
@@ -233,6 +240,8 @@ matplotlib.pyplot.show = show`);
233240
}
234241

235242
self.postMessage({ id, result: self.result, stdout: self.stdout, stderr: self.stderr });
243+
// Notify listeners (e.g. PyodideFileNav) that the FS may have changed
244+
self.postMessage({ type: 'fs:changed' });
236245
}
237246

238247
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)