CodeExecution.svelte 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, getContext } from 'svelte';
  4. import { getCodeExecutionConfig, setCodeExecutionConfig } from '$lib/apis/configs';
  5. import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
  6. import Tooltip from '$lib/components/common/Tooltip.svelte';
  7. import Textarea from '$lib/components/common/Textarea.svelte';
  8. import Switch from '$lib/components/common/Switch.svelte';
  9. const i18n = getContext('i18n');
  10. export let saveHandler: Function;
  11. let config = null;
  12. let engines = ['pyodide', 'jupyter'];
  13. const submitHandler = async () => {
  14. const res = await setCodeExecutionConfig(localStorage.token, config);
  15. };
  16. onMount(async () => {
  17. const res = await getCodeExecutionConfig(localStorage.token);
  18. if (res) {
  19. config = res;
  20. }
  21. });
  22. </script>
  23. <form
  24. class="flex flex-col h-full justify-between space-y-3 text-sm"
  25. on:submit|preventDefault={async () => {
  26. await submitHandler();
  27. saveHandler();
  28. }}
  29. >
  30. <div class=" space-y-3 overflow-y-scroll scrollbar-hidden h-full">
  31. {#if config}
  32. <div>
  33. <div class="mb-3.5">
  34. <div class=" mb-2.5 text-base font-medium">{$i18n.t('General')}</div>
  35. <hr class=" border-gray-100 dark:border-gray-850 my-2" />
  36. <div class="mb-2.5">
  37. <div class=" flex w-full justify-between">
  38. <div class=" self-center text-xs font-medium">
  39. {$i18n.t('Enable Code Execution')}
  40. </div>
  41. <Switch bind:state={config.ENABLE_CODE_EXECUTION} />
  42. </div>
  43. </div>
  44. <div class="mb-2.5">
  45. <div class="flex w-full justify-between">
  46. <div class=" self-center text-xs font-medium">{$i18n.t('Code Execution Engine')}</div>
  47. <div class="flex items-center relative">
  48. <select
  49. class="dark:bg-gray-900 w-fit pr-8 rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-right"
  50. bind:value={config.CODE_EXECUTION_ENGINE}
  51. placeholder={$i18n.t('Select a engine')}
  52. required
  53. >
  54. <option disabled selected value="">{$i18n.t('Select a engine')}</option>
  55. {#each engines as engine}
  56. <option value={engine}>{engine}</option>
  57. {/each}
  58. </select>
  59. </div>
  60. </div>
  61. {#if config.CODE_EXECUTION_ENGINE === 'jupyter'}
  62. <div class="text-gray-500 text-xs">
  63. {$i18n.t(
  64. 'Warning: Jupyter execution enables arbitrary code execution, posing severe security risks—proceed with extreme caution.'
  65. )}
  66. </div>
  67. {/if}
  68. </div>
  69. {#if config.CODE_EXECUTION_ENGINE === 'jupyter'}
  70. <div class="mb-2.5 flex flex-col gap-1.5 w-full">
  71. <div class="text-xs font-medium">
  72. {$i18n.t('Jupyter URL')}
  73. </div>
  74. <div class="flex w-full">
  75. <div class="flex-1">
  76. <input
  77. class="w-full text-sm py-0.5 placeholder:text-gray-300 dark:placeholder:text-gray-700 bg-transparent outline-hidden"
  78. type="text"
  79. placeholder={$i18n.t('Enter Jupyter URL')}
  80. bind:value={config.CODE_EXECUTION_JUPYTER_URL}
  81. autocomplete="off"
  82. />
  83. </div>
  84. </div>
  85. </div>
  86. <div class="mb-2.5 flex flex-col gap-1.5 w-full">
  87. <div class=" flex gap-2 w-full items-center justify-between">
  88. <div class="text-xs font-medium">
  89. {$i18n.t('Jupyter Auth')}
  90. </div>
  91. <div>
  92. <select
  93. class="dark:bg-gray-900 w-fit pr-8 rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-left"
  94. bind:value={config.CODE_EXECUTION_JUPYTER_AUTH}
  95. placeholder={$i18n.t('Select an auth method')}
  96. >
  97. <option selected value="">{$i18n.t('None')}</option>
  98. <option value="token">{$i18n.t('Token')}</option>
  99. <option value="password">{$i18n.t('Password')}</option>
  100. </select>
  101. </div>
  102. </div>
  103. {#if config.CODE_EXECUTION_JUPYTER_AUTH}
  104. <div class="flex w-full gap-2">
  105. <div class="flex-1">
  106. {#if config.CODE_EXECUTION_JUPYTER_AUTH === 'password'}
  107. <SensitiveInput
  108. type="text"
  109. placeholder={$i18n.t('Enter Jupyter Password')}
  110. bind:value={config.CODE_EXECUTION_JUPYTER_AUTH_PASSWORD}
  111. autocomplete="off"
  112. />
  113. {:else}
  114. <SensitiveInput
  115. type="text"
  116. placeholder={$i18n.t('Enter Jupyter Token')}
  117. bind:value={config.CODE_EXECUTION_JUPYTER_AUTH_TOKEN}
  118. autocomplete="off"
  119. />
  120. {/if}
  121. </div>
  122. </div>
  123. {/if}
  124. </div>
  125. <div class="flex gap-2 w-full items-center justify-between">
  126. <div class="text-xs font-medium">
  127. {$i18n.t('Code Execution Timeout')}
  128. </div>
  129. <div class="">
  130. <Tooltip content={$i18n.t('Enter timeout in seconds')}>
  131. <input
  132. class="dark:bg-gray-900 w-fit rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-right"
  133. type="number"
  134. bind:value={config.CODE_EXECUTION_JUPYTER_TIMEOUT}
  135. placeholder={$i18n.t('e.g. 60')}
  136. autocomplete="off"
  137. />
  138. </Tooltip>
  139. </div>
  140. </div>
  141. {/if}
  142. </div>
  143. <div class="mb-3.5">
  144. <div class=" mb-2.5 text-base font-medium">{$i18n.t('Code Interpreter')}</div>
  145. <hr class=" border-gray-100 dark:border-gray-850 my-2" />
  146. <div class="mb-2.5">
  147. <div class=" flex w-full justify-between">
  148. <div class=" self-center text-xs font-medium">
  149. {$i18n.t('Enable Code Interpreter')}
  150. </div>
  151. <Switch bind:state={config.ENABLE_CODE_INTERPRETER} />
  152. </div>
  153. </div>
  154. {#if config.ENABLE_CODE_INTERPRETER}
  155. <div class="mb-2.5">
  156. <div class=" flex w-full justify-between">
  157. <div class=" self-center text-xs font-medium">
  158. {$i18n.t('Code Interpreter Engine')}
  159. </div>
  160. <div class="flex items-center relative">
  161. <select
  162. class="dark:bg-gray-900 w-fit pr-8 rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-right"
  163. bind:value={config.CODE_INTERPRETER_ENGINE}
  164. placeholder={$i18n.t('Select a engine')}
  165. required
  166. >
  167. <option disabled selected value="">{$i18n.t('Select a engine')}</option>
  168. {#each engines as engine}
  169. <option value={engine}>{engine}</option>
  170. {/each}
  171. </select>
  172. </div>
  173. </div>
  174. {#if config.CODE_INTERPRETER_ENGINE === 'jupyter'}
  175. <div class="text-gray-500 text-xs">
  176. {$i18n.t(
  177. 'Warning: Jupyter execution enables arbitrary code execution, posing severe security risks—proceed with extreme caution.'
  178. )}
  179. </div>
  180. {/if}
  181. </div>
  182. {#if config.CODE_INTERPRETER_ENGINE === 'jupyter'}
  183. <div class="mb-2.5 flex flex-col gap-1.5 w-full">
  184. <div class="text-xs font-medium">
  185. {$i18n.t('Jupyter URL')}
  186. </div>
  187. <div class="flex w-full">
  188. <div class="flex-1">
  189. <input
  190. class="w-full text-sm py-0.5 placeholder:text-gray-300 dark:placeholder:text-gray-700 bg-transparent outline-hidden"
  191. type="text"
  192. placeholder={$i18n.t('Enter Jupyter URL')}
  193. bind:value={config.CODE_INTERPRETER_JUPYTER_URL}
  194. autocomplete="off"
  195. />
  196. </div>
  197. </div>
  198. </div>
  199. <div class="mb-2.5 flex flex-col gap-1.5 w-full">
  200. <div class="flex gap-2 w-full items-center justify-between">
  201. <div class="text-xs font-medium">
  202. {$i18n.t('Jupyter Auth')}
  203. </div>
  204. <div>
  205. <select
  206. class="dark:bg-gray-900 w-fit pr-8 rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-left"
  207. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH}
  208. placeholder={$i18n.t('Select an auth method')}
  209. >
  210. <option selected value="">{$i18n.t('None')}</option>
  211. <option value="token">{$i18n.t('Token')}</option>
  212. <option value="password">{$i18n.t('Password')}</option>
  213. </select>
  214. </div>
  215. </div>
  216. {#if config.CODE_INTERPRETER_JUPYTER_AUTH}
  217. <div class="flex w-full gap-2">
  218. <div class="flex-1">
  219. {#if config.CODE_INTERPRETER_JUPYTER_AUTH === 'password'}
  220. <SensitiveInput
  221. type="text"
  222. placeholder={$i18n.t('Enter Jupyter Password')}
  223. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH_PASSWORD}
  224. autocomplete="off"
  225. />
  226. {:else}
  227. <SensitiveInput
  228. type="text"
  229. placeholder={$i18n.t('Enter Jupyter Token')}
  230. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH_TOKEN}
  231. autocomplete="off"
  232. />
  233. {/if}
  234. </div>
  235. </div>
  236. {/if}
  237. </div>
  238. <div class="flex gap-2 w-full items-center justify-between">
  239. <div class="text-xs font-medium">
  240. {$i18n.t('Code Execution Timeout')}
  241. </div>
  242. <div class="">
  243. <Tooltip content={$i18n.t('Enter timeout in seconds')}>
  244. <input
  245. class="dark:bg-gray-900 w-fit rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-right"
  246. type="number"
  247. bind:value={config.CODE_INTERPRETER_JUPYTER_TIMEOUT}
  248. placeholder={$i18n.t('e.g. 60')}
  249. autocomplete="off"
  250. />
  251. </Tooltip>
  252. </div>
  253. </div>
  254. {/if}
  255. <hr class="border-gray-100 dark:border-gray-850 my-2" />
  256. <div>
  257. <div class="py-0.5 w-full">
  258. <div class=" mb-2.5 text-xs font-medium">
  259. {$i18n.t('Code Interpreter Prompt Template')}
  260. </div>
  261. <Tooltip
  262. content={$i18n.t(
  263. 'Leave empty to use the default prompt, or enter a custom prompt'
  264. )}
  265. placement="top-start"
  266. >
  267. <Textarea
  268. bind:value={config.CODE_INTERPRETER_PROMPT_TEMPLATE}
  269. placeholder={$i18n.t(
  270. 'Leave empty to use the default prompt, or enter a custom prompt'
  271. )}
  272. />
  273. </Tooltip>
  274. </div>
  275. </div>
  276. {/if}
  277. </div>
  278. </div>
  279. {/if}
  280. </div>
  281. <div class="flex justify-end pt-3 text-sm font-medium">
  282. <button
  283. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
  284. type="submit"
  285. >
  286. {$i18n.t('Save')}
  287. </button>
  288. </div>
  289. </form>