CodeInterpreter.svelte 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, getContext } from 'svelte';
  4. import { getCodeInterpreterConfig, setCodeInterpreterConfig } 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 setCodeInterpreterConfig(localStorage.token, config);
  15. };
  16. onMount(async () => {
  17. const res = await getCodeInterpreterConfig(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-1 text-sm font-medium">
  34. {$i18n.t('Code Interpreter')}
  35. </div>
  36. <div>
  37. <div class=" py-0.5 flex w-full justify-between">
  38. <div class=" self-center text-xs font-medium">
  39. {$i18n.t('Enable Code Interpreter')}
  40. </div>
  41. <Switch bind:state={config.ENABLE_CODE_INTERPRETER} />
  42. </div>
  43. </div>
  44. <div class=" py-0.5 flex w-full justify-between">
  45. <div class=" self-center text-xs font-medium">{$i18n.t('Code Interpreter Engine')}</div>
  46. <div class="flex items-center relative">
  47. <select
  48. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-right"
  49. bind:value={config.CODE_INTERPRETER_ENGINE}
  50. placeholder={$i18n.t('Select a engine')}
  51. required
  52. >
  53. <option disabled selected value="">{$i18n.t('Select a engine')}</option>
  54. {#each engines as engine}
  55. <option value={engine}>{engine}</option>
  56. {/each}
  57. </select>
  58. </div>
  59. </div>
  60. {#if config.CODE_INTERPRETER_ENGINE === 'jupyter'}
  61. <div class="mt-1 flex flex-col gap-1.5 mb-1 w-full">
  62. <div class="text-xs font-medium">
  63. {$i18n.t('Jupyter URL')}
  64. </div>
  65. <div class="flex w-full">
  66. <div class="flex-1">
  67. <input
  68. class="w-full text-sm py-0.5 placeholder:text-gray-300 dark:placeholder:text-gray-700 bg-transparent outline-none"
  69. type="text"
  70. placeholder={$i18n.t('Enter Jupyter URL')}
  71. bind:value={config.CODE_INTERPRETER_JUPYTER_URL}
  72. autocomplete="off"
  73. />
  74. </div>
  75. </div>
  76. </div>
  77. <div class="mt-1 flex gap-2 mb-1 w-full items-center justify-between">
  78. <div class="text-xs font-medium">
  79. {$i18n.t('Jupyter Auth')}
  80. </div>
  81. <div>
  82. <select
  83. class="dark:bg-gray-900 w-fit pr-8 rounded px-2 p-1 text-xs bg-transparent outline-none text-left"
  84. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH}
  85. placeholder={$i18n.t('Select an auth method')}
  86. >
  87. <option selected value="">{$i18n.t('None')}</option>
  88. <option value="token">{$i18n.t('Token')}</option>
  89. <option value="password">{$i18n.t('Password')}</option>
  90. </select>
  91. </div>
  92. </div>
  93. {#if config.CODE_INTERPRETER_JUPYTER_AUTH}
  94. <div class="flex w-full gap-2">
  95. <div class="flex-1">
  96. {#if config.CODE_INTERPRETER_JUPYTER_AUTH === 'password'}
  97. <SensitiveInput
  98. type="text"
  99. placeholder={$i18n.t('Enter Jupyter Password')}
  100. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH_PASSWORD}
  101. autocomplete="off"
  102. />
  103. {:else}
  104. <SensitiveInput
  105. type="text"
  106. placeholder={$i18n.t('Enter Jupyter Token')}
  107. bind:value={config.CODE_INTERPRETER_JUPYTER_AUTH_TOKEN}
  108. autocomplete="off"
  109. />
  110. {/if}
  111. </div>
  112. </div>
  113. {/if}
  114. {/if}
  115. </div>
  116. <hr class=" dark:border-gray-850 my-2" />
  117. <div>
  118. <div class="py-0.5 w-full">
  119. <div class=" mb-2.5 text-xs font-medium">
  120. {$i18n.t('Code Interpreter Prompt Template')}
  121. </div>
  122. <Tooltip
  123. content={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
  124. placement="top-start"
  125. >
  126. <Textarea
  127. bind:value={config.CODE_INTERPRETER_PROMPT_TEMPLATE}
  128. placeholder={$i18n.t(
  129. 'Leave empty to use the default prompt, or enter a custom prompt'
  130. )}
  131. />
  132. </Tooltip>
  133. </div>
  134. </div>
  135. {/if}
  136. </div>
  137. <div class="flex justify-end pt-3 text-sm font-medium">
  138. <button
  139. 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"
  140. type="submit"
  141. >
  142. {$i18n.t('Save')}
  143. </button>
  144. </div>
  145. </form>