Spaces:
Runtime error
Runtime error
Delete src/lib/components/chat/Messages/CodeBlock.svelte
Browse files
src/lib/components/chat/Messages/CodeBlock.svelte
DELETED
|
@@ -1,273 +0,0 @@
|
|
| 1 |
-
<script lang="ts">
|
| 2 |
-
import Spinner from '$lib/components/common/Spinner.svelte';
|
| 3 |
-
import { copyToClipboard } from '$lib/utils';
|
| 4 |
-
import hljs from 'highlight.js';
|
| 5 |
-
import 'highlight.js/styles/github-dark.min.css';
|
| 6 |
-
import { loadPyodide } from 'pyodide';
|
| 7 |
-
import { onMount, tick } from 'svelte';
|
| 8 |
-
import PyodideWorker from '$lib/workers/pyodide.worker?worker';
|
| 9 |
-
|
| 10 |
-
export let id = '';
|
| 11 |
-
|
| 12 |
-
export let lang = '';
|
| 13 |
-
export let code = '';
|
| 14 |
-
|
| 15 |
-
let highlightedCode = null;
|
| 16 |
-
let executing = false;
|
| 17 |
-
|
| 18 |
-
let stdout = null;
|
| 19 |
-
let stderr = null;
|
| 20 |
-
let result = null;
|
| 21 |
-
|
| 22 |
-
let copied = false;
|
| 23 |
-
|
| 24 |
-
const copyCode = async () => {
|
| 25 |
-
copied = true;
|
| 26 |
-
await copyToClipboard(code);
|
| 27 |
-
|
| 28 |
-
setTimeout(() => {
|
| 29 |
-
copied = false;
|
| 30 |
-
}, 1000);
|
| 31 |
-
};
|
| 32 |
-
|
| 33 |
-
const checkPythonCode = (str) => {
|
| 34 |
-
// Check if the string contains typical Python syntax characters
|
| 35 |
-
const pythonSyntax = [
|
| 36 |
-
'def ',
|
| 37 |
-
'else:',
|
| 38 |
-
'elif ',
|
| 39 |
-
'try:',
|
| 40 |
-
'except:',
|
| 41 |
-
'finally:',
|
| 42 |
-
'yield ',
|
| 43 |
-
'lambda ',
|
| 44 |
-
'assert ',
|
| 45 |
-
'nonlocal ',
|
| 46 |
-
'del ',
|
| 47 |
-
'True',
|
| 48 |
-
'False',
|
| 49 |
-
'None',
|
| 50 |
-
' and ',
|
| 51 |
-
' or ',
|
| 52 |
-
' not ',
|
| 53 |
-
' in ',
|
| 54 |
-
' is ',
|
| 55 |
-
' with '
|
| 56 |
-
];
|
| 57 |
-
|
| 58 |
-
for (let syntax of pythonSyntax) {
|
| 59 |
-
if (str.includes(syntax)) {
|
| 60 |
-
return true;
|
| 61 |
-
}
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
// If none of the above conditions met, it's probably not Python code
|
| 65 |
-
return false;
|
| 66 |
-
};
|
| 67 |
-
|
| 68 |
-
const executePython = async (code) => {
|
| 69 |
-
if (!code.includes('input') && !code.includes('matplotlib')) {
|
| 70 |
-
executePythonAsWorker(code);
|
| 71 |
-
} else {
|
| 72 |
-
result = null;
|
| 73 |
-
stdout = null;
|
| 74 |
-
stderr = null;
|
| 75 |
-
|
| 76 |
-
executing = true;
|
| 77 |
-
|
| 78 |
-
document.pyodideMplTarget = document.getElementById(`plt-canvas-${id}`);
|
| 79 |
-
|
| 80 |
-
let pyodide = await loadPyodide({
|
| 81 |
-
indexURL: '/pyodide/',
|
| 82 |
-
stdout: (text) => {
|
| 83 |
-
console.log('Python output:', text);
|
| 84 |
-
|
| 85 |
-
if (stdout) {
|
| 86 |
-
stdout += `${text}\n`;
|
| 87 |
-
} else {
|
| 88 |
-
stdout = `${text}\n`;
|
| 89 |
-
}
|
| 90 |
-
},
|
| 91 |
-
stderr: (text) => {
|
| 92 |
-
console.log('An error occured:', text);
|
| 93 |
-
if (stderr) {
|
| 94 |
-
stderr += `${text}\n`;
|
| 95 |
-
} else {
|
| 96 |
-
stderr = `${text}\n`;
|
| 97 |
-
}
|
| 98 |
-
},
|
| 99 |
-
packages: ['micropip']
|
| 100 |
-
});
|
| 101 |
-
|
| 102 |
-
try {
|
| 103 |
-
const micropip = pyodide.pyimport('micropip');
|
| 104 |
-
|
| 105 |
-
// await micropip.set_index_urls('https://pypi.org/pypi/{package_name}/json');
|
| 106 |
-
|
| 107 |
-
let packages = [
|
| 108 |
-
code.includes('requests') ? 'requests' : null,
|
| 109 |
-
code.includes('bs4') ? 'beautifulsoup4' : null,
|
| 110 |
-
code.includes('numpy') ? 'numpy' : null,
|
| 111 |
-
code.includes('pandas') ? 'pandas' : null,
|
| 112 |
-
code.includes('matplotlib') ? 'matplotlib' : null,
|
| 113 |
-
code.includes('sklearn') ? 'scikit-learn' : null,
|
| 114 |
-
code.includes('scipy') ? 'scipy' : null,
|
| 115 |
-
code.includes('re') ? 'regex' : null,
|
| 116 |
-
code.includes('seaborn') ? 'seaborn' : null
|
| 117 |
-
].filter(Boolean);
|
| 118 |
-
|
| 119 |
-
console.log(packages);
|
| 120 |
-
await micropip.install(packages);
|
| 121 |
-
|
| 122 |
-
result = await pyodide.runPythonAsync(`from js import prompt
|
| 123 |
-
def input(p):
|
| 124 |
-
return prompt(p)
|
| 125 |
-
__builtins__.input = input`);
|
| 126 |
-
|
| 127 |
-
result = await pyodide.runPython(code);
|
| 128 |
-
|
| 129 |
-
if (!result) {
|
| 130 |
-
result = '[NO OUTPUT]';
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
console.log(result);
|
| 134 |
-
console.log(stdout);
|
| 135 |
-
console.log(stderr);
|
| 136 |
-
|
| 137 |
-
const pltCanvasElement = document.getElementById(`plt-canvas-${id}`);
|
| 138 |
-
|
| 139 |
-
if (pltCanvasElement?.innerHTML !== '') {
|
| 140 |
-
pltCanvasElement.classList.add('pt-4');
|
| 141 |
-
}
|
| 142 |
-
} catch (error) {
|
| 143 |
-
console.error('Error:', error);
|
| 144 |
-
stderr = error;
|
| 145 |
-
}
|
| 146 |
-
|
| 147 |
-
executing = false;
|
| 148 |
-
}
|
| 149 |
-
};
|
| 150 |
-
|
| 151 |
-
const executePythonAsWorker = async (code) => {
|
| 152 |
-
result = null;
|
| 153 |
-
stdout = null;
|
| 154 |
-
stderr = null;
|
| 155 |
-
|
| 156 |
-
executing = true;
|
| 157 |
-
|
| 158 |
-
let packages = [
|
| 159 |
-
code.includes('requests') ? 'requests' : null,
|
| 160 |
-
code.includes('bs4') ? 'beautifulsoup4' : null,
|
| 161 |
-
code.includes('numpy') ? 'numpy' : null,
|
| 162 |
-
code.includes('pandas') ? 'pandas' : null,
|
| 163 |
-
code.includes('sklearn') ? 'scikit-learn' : null,
|
| 164 |
-
code.includes('scipy') ? 'scipy' : null,
|
| 165 |
-
code.includes('re') ? 'regex' : null,
|
| 166 |
-
code.includes('seaborn') ? 'seaborn' : null
|
| 167 |
-
].filter(Boolean);
|
| 168 |
-
|
| 169 |
-
console.log(packages);
|
| 170 |
-
|
| 171 |
-
const pyodideWorker = new PyodideWorker();
|
| 172 |
-
|
| 173 |
-
pyodideWorker.postMessage({
|
| 174 |
-
id: id,
|
| 175 |
-
code: code,
|
| 176 |
-
packages: packages
|
| 177 |
-
});
|
| 178 |
-
|
| 179 |
-
setTimeout(() => {
|
| 180 |
-
if (executing) {
|
| 181 |
-
executing = false;
|
| 182 |
-
stderr = 'Execution Time Limit Exceeded';
|
| 183 |
-
pyodideWorker.terminate();
|
| 184 |
-
}
|
| 185 |
-
}, 60000);
|
| 186 |
-
|
| 187 |
-
pyodideWorker.onmessage = (event) => {
|
| 188 |
-
console.log('pyodideWorker.onmessage', event);
|
| 189 |
-
const { id, ...data } = event.data;
|
| 190 |
-
|
| 191 |
-
console.log(id, data);
|
| 192 |
-
|
| 193 |
-
data['stdout'] && (stdout = data['stdout']);
|
| 194 |
-
data['stderr'] && (stderr = data['stderr']);
|
| 195 |
-
data['result'] && (result = data['result']);
|
| 196 |
-
|
| 197 |
-
executing = false;
|
| 198 |
-
};
|
| 199 |
-
|
| 200 |
-
pyodideWorker.onerror = (event) => {
|
| 201 |
-
console.log('pyodideWorker.onerror', event);
|
| 202 |
-
executing = false;
|
| 203 |
-
};
|
| 204 |
-
};
|
| 205 |
-
|
| 206 |
-
let debounceTimeout;
|
| 207 |
-
$: if (code) {
|
| 208 |
-
// Function to perform the code highlighting
|
| 209 |
-
const highlightCode = () => {
|
| 210 |
-
highlightedCode = hljs.highlightAuto(code, hljs.getLanguage(lang)?.aliases).value || code;
|
| 211 |
-
};
|
| 212 |
-
|
| 213 |
-
// Clear the previous timeout if it exists
|
| 214 |
-
clearTimeout(debounceTimeout);
|
| 215 |
-
|
| 216 |
-
// Set a new timeout to debounce the code highlighting
|
| 217 |
-
debounceTimeout = setTimeout(highlightCode, 10);
|
| 218 |
-
}
|
| 219 |
-
</script>
|
| 220 |
-
|
| 221 |
-
<div class="mb-4" dir="ltr">
|
| 222 |
-
<div
|
| 223 |
-
class="flex justify-between bg-[#202123] text-white text-xs px-4 pt-1 pb-0.5 rounded-t-lg overflow-x-auto"
|
| 224 |
-
>
|
| 225 |
-
<div class="p-1">{@html lang}</div>
|
| 226 |
-
|
| 227 |
-
<div class="flex items-center">
|
| 228 |
-
{#if lang.toLowerCase() === 'python' || lang.toLowerCase() === 'py' || (lang === '' && checkPythonCode(code))}
|
| 229 |
-
{#if executing}
|
| 230 |
-
<div class="copy-code-button bg-none border-none p-1 cursor-not-allowed">Running</div>
|
| 231 |
-
{:else}
|
| 232 |
-
<button
|
| 233 |
-
class="copy-code-button bg-none border-none p-1"
|
| 234 |
-
on:click={() => {
|
| 235 |
-
executePython(code);
|
| 236 |
-
}}>Run</button
|
| 237 |
-
>
|
| 238 |
-
{/if}
|
| 239 |
-
{/if}
|
| 240 |
-
<button class="copy-code-button bg-none border-none p-1" on:click={copyCode}
|
| 241 |
-
>{copied ? 'Copied' : 'Copy Code'}</button
|
| 242 |
-
>
|
| 243 |
-
</div>
|
| 244 |
-
</div>
|
| 245 |
-
|
| 246 |
-
<pre
|
| 247 |
-
class=" hljs p-4 px-5 overflow-x-auto"
|
| 248 |
-
style="border-top-left-radius: 0px; border-top-right-radius: 0px; {(executing ||
|
| 249 |
-
stdout ||
|
| 250 |
-
stderr ||
|
| 251 |
-
result) &&
|
| 252 |
-
'border-bottom-left-radius: 0px; border-bottom-right-radius: 0px;'}"><code
|
| 253 |
-
class="language-{lang} rounded-t-none whitespace-pre"
|
| 254 |
-
>{#if highlightedCode}{@html highlightedCode}{:else}{code}{/if}</code
|
| 255 |
-
></pre>
|
| 256 |
-
|
| 257 |
-
<div
|
| 258 |
-
id="plt-canvas-{id}"
|
| 259 |
-
class="bg-[#202123] text-white max-w-full overflow-x-auto scrollbar-hidden"
|
| 260 |
-
/>
|
| 261 |
-
|
| 262 |
-
{#if executing}
|
| 263 |
-
<div class="bg-[#202123] text-white px-4 py-4 rounded-b-lg">
|
| 264 |
-
<div class=" text-gray-500 text-xs mb-1">STDOUT/STDERR</div>
|
| 265 |
-
<div class="text-sm">Running...</div>
|
| 266 |
-
</div>
|
| 267 |
-
{:else if stdout || stderr || result}
|
| 268 |
-
<div class="bg-[#202123] text-white px-4 py-4 rounded-b-lg">
|
| 269 |
-
<div class=" text-gray-500 text-xs mb-1">STDOUT/STDERR</div>
|
| 270 |
-
<div class="text-sm">{stdout || stderr || result}</div>
|
| 271 |
-
</div>
|
| 272 |
-
{/if}
|
| 273 |
-
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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