CodeExecution.svelte 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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">{$i18n.t('Code Execution Engine')}</div>
  39. <div class="flex items-center relative">
  40. <select
  41. class="dark:bg-gray-900 w-fit pr-8 rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-right"
  42. bind:value={config.CODE_EXECUTION_ENGINE}
  43. placeholder={$i18n.t('Select a engine')}
  44. required
  45. >
  46. <option disabled selected value="">{$i18n.t('Select a engine')}</option>
  47. {#each engines as engine}
  48. <option value={engine}>{engine}</option>
  49. {/each}
  50. </select>
  51. </div>
  52. </div>
  53. {#if config.CODE_EXECUTION_ENGINE === 'jupyter'}
  54. <div class="text-gray-500 text-xs">
  55. {$i18n.t(
  56. 'Warning: Jupyter execution enables arbitrary code execution, posing severe security risks—proceed with extreme caution.'
  57. )}
  58. </div>
  59. {/if}
  60. </div>
  61. {#if config.CODE_EXECUTION_ENGINE === 'jupyter'}
  62. <div class="mb-2.5 flex flex-col gap-1.5 w-full">
  63. <div class="text-xs font-medium">
  64. {$i18n.t('Jupyter URL')}
  65. </div>
  66. <div class="flex w-full">
  67. <div class="flex-1">
  68. <input
  69. class="w-full text-sm py-0.5 placeholder:text-gray-300 dark:placeholder:text-gray-700 bg-transparent outline-hidden"
  70. type="text"
  71. placeholder={$i18n.t('Enter Jupyter URL')}
  72. bind:value={config.CODE_EXECUTION_JUPYTER_URL}
  73. autocomplete="off"
  74. />
  75. </div>
  76. </div>
  77. </div>
  78. <div class=" flex gap-2 w-full items-center justify-between">
  79. <div class="text-xs font-medium">
  80. {$i18n.t('Jupyter Auth')}
  81. </div>
  82. <div>
  83. <select
  84. class="dark:bg-gray-900 w-fit pr-8 rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-left"
  85. bind:value={config.CODE_EXECUTION_JUPYTER_AUTH}
  86. placeholder={$i18n.t('Select an auth method')}
  87. >
  88. <option selected value="">{$i18n.t('None')}</option>
  89. <option value="token">{$i18n.t('Token')}</option>
  90. <option value="password">{$i18n.t('Password')}</option>
  91. </select>
  92. </div>
  93. </div>
  94. {#if config.CODE_EXECUTION_JUPYTER_AUTH}
  95. <div class="flex w-full gap-2">
  96. <div class="flex-1">
  97. {#if config.CODE_EXECUTION_JUPYTER_AUTH === 'password'}
  98. <SensitiveInput
  99. type="text"
  100. placeholder={$i18n.t('Enter Jupyter Password')}
  101. bind:value={config.CODE_EXECUTION_JUPYTER_AUTH_PASSWORD}
  102. autocomplete="off"
  103. />
  104. {:else}
  105. <SensitiveInput
  106. type="text"
  107. placeholder={$i18n.t('Enter Jupyter Token')}
  108. bind:value={config.CODE_EXECUTION_JUPYTER_AUTH_TOKEN}
  109. autocomplete="off"
  110. />
  111. {/if}
  112. </div>
  113. </div>
  114. {/if}
  115. {/if}
  116. </div>
  117. <div class="mb-3.5">
  118. <div class=" mb-2.5 text-base font-medium">{$i18n.t('Code Interpreter')}</div>
  119. <hr class=" border-gray-100 dark:border-gray-850 my-2" />
  120. <div class="mb-2.5">
  121. <div class=" flex w-full justify-between">
  122. <div class=" self-center text-xs font-medium">
  123. {$i18n.t('Enable Code Interpreter')}
  124. </div>
  125. <Switch bind:state={config.ENABLE_CODE_INTERPRETER} />
  126. </div>
  127. </div>
  128. {#if config.ENABLE_CODE_INTERPRETER}
  129. <div class="mb-2.5">
  130. <div class=" flex w-full justify-between">
  131. <div class=" self-center text-xs font-medium">
  132. {$i18n.t('Code Interpreter Engine')}
  133. </div>
  134. <div class="flex items-center relative">
  135. <select
  136. class="dark:bg-gray-900 w-fit pr-8 rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-right"
  137. bind:value={config.CODE_INTERPRETER_ENGINE}
  138. placeholder={$i18n.t('Select a engine')}
  139. required
  140. >
  141. <option disabled selected value="">{$i18n.t('Select a engine')}</option>
  142. {#each engines as engine}
  143. <option value={engine}>{engine}</option>
  144. {/each}
  145. </select>
  146. </div>
  147. </div>
  148. {#if config.CODE_EXECUTION_ENGINE === 'jupyter'}
  149. <div class="text-gray-500 text-xs">
  150. {$i18n.t(
  151. 'Warning: Jupyter execution enables arbitrary code execution, posing severe security risks—proceed with extreme caution.'
  152. )}
  153. </div>
  154. {/if}
  155. </div>
  156. {#if config.CODE_INTERPRETER_ENGINE === 'jupyter'}
  157. <div class="mb-2.5 flex flex-col gap-1.5 w-full">
  158. <div class="text-xs font-medium">
  159. {$i18n.t('Jupyter URL')}
  160. </div>
  161. <div class="flex w-full">
  162. <div class="flex-1">
  163. <input
  164. class="w-full text-sm py-0.5 placeholder:text-gray-300 dark:placeholder:text-gray-700 bg-transparent outline-hidden"
  165. type="text"
  166. placeholder={$i18n.t('Enter Jupyter URL')}
  167. bind:value={config.CODE_INTERPRETER_JUPYTER_URL}
  168. autocomplete="off"
  169. />
  170. </div>
  171. </div>
  172. </div>
  173. <div class="flex gap-2 w-full items-center justify-between">
  174. <div class="text-xs font-medium">
  175. {$i18n.t('Jupyter Auth')}
  176. </div>
  177. <div>
  178. <select
  179. class="dark:bg-gray-900 w-fit pr-8 rounded-sm px-2 p-1 text-xs bg-transparent outline-hidden text-left"
  180. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH}
  181. placeholder={$i18n.t('Select an auth method')}
  182. >
  183. <option selected value="">{$i18n.t('None')}</option>
  184. <option value="token">{$i18n.t('Token')}</option>
  185. <option value="password">{$i18n.t('Password')}</option>
  186. </select>
  187. </div>
  188. </div>
  189. {#if config.CODE_INTERPRETER_JUPYTER_AUTH}
  190. <div class="flex w-full gap-2">
  191. <div class="flex-1">
  192. {#if config.CODE_INTERPRETER_JUPYTER_AUTH === 'password'}
  193. <SensitiveInput
  194. type="text"
  195. placeholder={$i18n.t('Enter Jupyter Password')}
  196. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH_PASSWORD}
  197. autocomplete="off"
  198. />
  199. {:else}
  200. <SensitiveInput
  201. type="text"
  202. placeholder={$i18n.t('Enter Jupyter Token')}
  203. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH_TOKEN}
  204. autocomplete="off"
  205. />
  206. {/if}
  207. </div>
  208. </div>
  209. {/if}
  210. {/if}
  211. <hr class="border-gray-100 dark:border-gray-850 my-2" />
  212. <div>
  213. <div class="py-0.5 w-full">
  214. <div class=" mb-2.5 text-xs font-medium">
  215. {$i18n.t('Code Interpreter Prompt Template')}
  216. </div>
  217. <Tooltip
  218. content={$i18n.t(
  219. 'Leave empty to use the default prompt, or enter a custom prompt'
  220. )}
  221. placement="top-start"
  222. >
  223. <Textarea
  224. bind:value={config.CODE_INTERPRETER_PROMPT_TEMPLATE}
  225. placeholder={$i18n.t(
  226. 'Leave empty to use the default prompt, or enter a custom prompt'
  227. )}
  228. />
  229. </Tooltip>
  230. </div>
  231. </div>
  232. {/if}
  233. </div>
  234. </div>
  235. {/if}
  236. </div>
  237. <div class="flex justify-end pt-3 text-sm font-medium">
  238. <button
  239. 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"
  240. type="submit"
  241. >
  242. {$i18n.t('Save')}
  243. </button>
  244. </div>
  245. </form>