Add binary download support for compiled code (#35)

- Request compiled binaries via return_artifact parameter
- Add "Download Binary" button when artifact is available
- Support base64 decoding and browser download
- Handle artifact errors gracefully
- Works with C, C++, Rust, Go, Java, and other compiled languages

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Russell 2025-11-11 13:38:41 -05:00 committed by GitHub
parent ccedf063a0
commit 108b6e270f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1427,7 +1427,8 @@ async function executeCodeBlock(code, blockElement, playButton) {
},
body: JSON.stringify({
language: language,
code: code
code: code,
return_artifact: true // Request compiled binary for compiled languages
})
});
@ -1564,6 +1565,57 @@ function displayExecutionResults(result, resultsContainer, language) {
}
resultsContainer.innerHTML = outputHtml;
// Check for compiled binary artifact
if (result.artifact && result.artifact.type === 'base64' && result.artifact.data) {
// Create download binary button
const downloadButton = document.createElement('button');
downloadButton.textContent = '⬇ Download Binary';
downloadButton.style.marginTop = '10px';
downloadButton.style.padding = '6px 12px';
downloadButton.style.backgroundColor = 'var(--button-primary)';
downloadButton.style.color = 'white';
downloadButton.style.border = 'none';
downloadButton.style.borderRadius = '4px';
downloadButton.style.cursor = 'pointer';
downloadButton.style.fontFamily = 'inherit';
downloadButton.style.fontSize = '14px';
downloadButton.onclick = () => {
try {
// Convert base64 to binary
const binaryString = atob(result.artifact.data);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
// Create blob and download
const blob = new Blob([bytes], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = result.artifact.filename || 'compiled_binary';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch (error) {
console.error('Error downloading binary:', error);
alert('Failed to download binary: ' + error.message);
}
};
resultsContainer.appendChild(downloadButton);
} else if (result.artifact && result.artifact.type === 'error') {
// Show artifact error if present
const artifactError = document.createElement('div');
artifactError.style.color = 'var(--text-warning)';
artifactError.style.marginTop = '8px';
artifactError.style.fontSize = '12px';
artifactError.textContent = `Artifact error: ${result.artifact.error}`;
resultsContainer.appendChild(artifactError);
}
}
// Helper function to escape HTML