Repository navigation
Studio: always show the executed Python script in chat with a download option - #7240
Conversation
…d option The script the python tool runs was rendered inside a collapsible that closes when the run ends or the thread is reopened, so the code disappeared from the transcript and there was no way to save it. Render the script outside the collapsible so it stays visible, and add a Download button that saves it as script.py. Other tools and normal chat are unaffected.
There was a problem hiding this comment.
Code Review
This pull request introduces a client-side download feature for Python scripts in the assistant UI and refactors the layout to keep the script source visible when reopened. Feedback was provided regarding the download implementation, pointing out a potential race condition with immediate URL revocation and a memory leak if an error occurs, along with a suggested fix using a finally block and a safer timeout.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| const download = useCallback(() => { | ||
| if (typeof document === "undefined") { | ||
| return; | ||
| } | ||
| try { | ||
| const blob = new Blob([code], { type: "text/x-python" }); | ||
| const url = URL.createObjectURL(blob); | ||
| const anchor = document.createElement("a"); | ||
| anchor.href = url; | ||
| anchor.download = name; | ||
| document.body.appendChild(anchor); | ||
| anchor.click(); | ||
| anchor.remove(); | ||
| // Revoke next tick, after the click consumes the URL. | ||
| setTimeout(() => URL.revokeObjectURL(url), 0); | ||
| } catch { | ||
| // Best-effort: never break the transcript over a download. | ||
| } | ||
| }, [code, name]); |
There was a problem hiding this comment.
There are two potential issues with the current implementation of download:
- Race Condition: Using
setTimeoutwith0ms to revoke the object URL can cause download failures in some browsers (like Safari or Firefox under load) because the URL might be revoked before the browser has fully initiated the download process. - Memory Leak: If an error is thrown during DOM manipulation (e.g., if
document.bodyis null orappendChildfails), thecatchblock is entered, andURL.revokeObjectURL(url)is never called, leaving the blob in memory.
Using a finally block and a slightly longer timeout (e.g., 100ms) resolves both issues safely.
const download = useCallback(() => {
if (typeof document === "undefined") {
return;
}
let url: string | null = null;
try {
const blob = new Blob([code], { type: "text/x-python" });
url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = name;
document.body.appendChild(anchor);
anchor.click();
anchor.remove();
} catch {
// Best-effort: never break the transcript over a download.
} finally {
if (url) {
setTimeout(() => URL.revokeObjectURL(url), 100);
}
}
}, [code, name]);
There was a problem hiding this comment.
Deferring revokeObjectURL to a zero-delay timeout is the standard correct pattern: only a synchronous or microtask revoke cancels the download, a macrotask does not. The createElement/appendChild/click path does not throw on real input and SSR is already guarded, so there is no reachable leak. Keeping as is.
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a1ac054ba2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const blob = new Blob([code], { type: "text/x-python" }); | ||
| const url = URL.createObjectURL(blob); | ||
| const anchor = document.createElement("a"); | ||
| anchor.href = url; | ||
| anchor.download = name; | ||
| document.body.appendChild(anchor); | ||
| anchor.click(); |
There was a problem hiding this comment.
Use the native download path for Tauri
When Studio is running as the Tauri desktop app, this always uses a blob anchor rather than the app's downloadFile helper, so the new action bypasses the registered save_native_file command and its native save chooser. The desktop client explicitly branches downloads through that command; route this button through the helper so Python scripts can be saved in desktop builds as well as in a browser.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
This doesn't apply here. There is no downloadFile helper and no save_native_file command in the repo, and the desktop client does not branch downloads through a native command. The registered Tauri commands are all server/install lifecycle (start_server, check_health, start_install, ...); none save files, and there is no file-save dialog wiring on the webview.
Every existing download affordance in Studio uses the same client-side blob-anchor pattern this button uses: image download (downloadImagePart in image.tsx), audio (audio-player.tsx), attachments (attachment.tsx), and chat-history export (export-chat-history.ts) all build a Blob, URL.createObjectURL, set a.download, and click. Routing this one button through a non-existent helper would make it inconsistent with the rest of the app.
If reliable file saving inside the desktop webview is a concern, that is a pre-existing, app-wide question that affects all of those buttons equally, not something specific to this PR. Keeping this consistent with the established pattern for now.
|
pre-commit.ci run |
…ow-up to #7240) (#7363) * Studio: keep the executed Python script visible in chat, with download + viewport-gated highlight Always show the executed Python script under the tool card (not only inside the collapsible run/output section, which is unmounted from history), with Copy and a client-side .py Download button, so the script stays visible on reopen (#7165). The script is rendered eagerly, but shiki syntax-highlighting only runs once the block scrolls near the viewport (IntersectionObserver, 200px margin); until then a plain monospace placeholder shows the same source with matching padding, so there is no layout jump. This bounds highlighting to the cards actually on screen instead of tokenizing every script up front. Measured shiki cost is ~8 ms per typical 2 KB script, so eager highlighting of a long agentic transcript (20-50+ Python calls) would add ~170-420 ms of main-thread work on load; viewport-gating keeps it to the few visible cards (~15-35 ms) regardless of transcript length. Falls back to immediate highlight when IntersectionObserver is unavailable (SSR / tests). * Use a div for the pre-highlight placeholder so container [&_pre]:!p-0 doesn't strip its p-3 The placeholder shares the highlighted block's p-3 padding to avoid a layout jump, but as a <pre> it was caught by the container's [&_pre]:!p-0 !important rule and rendered with no padding, so the script shifted by p-3 when shiki swapped in. A plain div keeps the padding. * Match placeholder wrapping to the highlighted pre (whitespace-pre, not pre-wrap) The placeholder wrapped long lines while the highlighted Streamdown <pre> keeps them on one line and scrolls in the container's overflow-auto, so a script with a long line changed height when shiki swapped in. Use whitespace-pre so the placeholder scrolls the same way and the height stays stable. --------- Co-authored-by: danielhanchen <[email protected]>
Ungrouping the aggregate tool group was not enough on its own. Each Python
card still mounts with defaultOpen={isRunning}, so on a reopened turn the
script and its Copy/Download controls stayed hidden behind the card's own
chevron and the reported issue persisted.
Render ToolCodeCell outside ToolFallbackContent for Python, restoring the
behaviour from unslothai#7240 that unslothai#7455 folded back inside when it unified the code
cell. Status, output and images still collapse. Terminal keeps its command
inside the collapsible: a one-line command is not the artifact a user reopens
a thread to retrieve, a script is.
Verified against a running Studio: reopening a persisted turn with two
adjacent Python calls now shows both scripts and both Download controls with
no clicks, and Download still saves byte-exact script.py.
…7528) * Studio: keep grouped Python scripts visible and save them natively * Studio: render the executed Python script outside the card collapsible Ungrouping the aggregate tool group was not enough on its own. Each Python card still mounts with defaultOpen={isRunning}, so on a reopened turn the script and its Copy/Download controls stayed hidden behind the card's own chevron and the reported issue persisted. Render ToolCodeCell outside ToolFallbackContent for Python, restoring the behaviour from #7240 that #7455 folded back inside when it unified the code cell. Status, output and images still collapse. Terminal keeps its command inside the collapsible: a one-line command is not the artifact a user reopens a thread to retrieve, a script is. Verified against a running Studio: reopening a persisted turn with two adjacent Python calls now shows both scripts and both Download controls with no clicks, and Download still saves byte-exact script.py. --------- Co-authored-by: Daniel Han <[email protected]>
Problem
When the model uses the Python tool to write and run a script, the script itself often did not appear in the chat. The code was rendered inside a collapsible card that is open only while the tool runs, so once the run finished, or the thread was reopened from history, the card collapsed and the script disappeared. There was also no way to save it, so the user had to prompt again to get the code back.
Fix
Render the executed script outside the collapsible so it stays visible after the run and when the thread is reopened, and add a Download button that saves it as
script.py. Only the run status, stdout and images remain collapsible.Verification
Reproduced live in a running Studio with a local GGUF: before the change, reopening the thread from history showed no code block; after it, the script is visible with Copy and Download. The frontend builds cleanly (tsc and vite) and the change is limited to the Python tool card.
Compatibility
Frontend only, scoped to the Python tool component. The bash, web-search and other tool cards and normal chat are untouched. The download uses a client-side blob, so it needs no backend file serving and survives the sandbox deleting its temp file. Blob and anchor download are supported in all current browsers.
Fixes #7165