ToolkitEditor.svelte 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <script>
  2. import { getContext, createEventDispatcher, onMount } from 'svelte';
  3. const i18n = getContext('i18n');
  4. import CodeEditor from './CodeEditor.svelte';
  5. import { goto } from '$app/navigation';
  6. import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  7. const dispatch = createEventDispatcher();
  8. let formElement = null;
  9. let loading = false;
  10. let showConfirm = false;
  11. export let edit = false;
  12. export let clone = false;
  13. export let id = '';
  14. export let name = '';
  15. export let meta = {
  16. description: ''
  17. };
  18. export let content = '';
  19. $: if (name && !edit && !clone) {
  20. id = name.replace(/\s+/g, '_').toLowerCase();
  21. }
  22. let codeEditor;
  23. const saveHandler = async () => {
  24. loading = true;
  25. dispatch('save', {
  26. id,
  27. name,
  28. meta,
  29. content
  30. });
  31. };
  32. const submitHandler = async () => {
  33. if (codeEditor) {
  34. const res = await codeEditor.formatHandler();
  35. if (res) {
  36. console.log('Code formatted successfully');
  37. saveHandler();
  38. }
  39. }
  40. };
  41. </script>
  42. <div class=" flex flex-col justify-between w-full overflow-y-auto h-full">
  43. <div class="mx-auto w-full md:px-0 h-full">
  44. <form
  45. bind:this={formElement}
  46. class=" flex flex-col max-h-[100dvh] h-full"
  47. on:submit|preventDefault={() => {
  48. if (edit) {
  49. submitHandler();
  50. } else {
  51. showConfirm = true;
  52. }
  53. }}
  54. >
  55. <div class="mb-2.5">
  56. <button
  57. class="flex space-x-1"
  58. on:click={() => {
  59. goto('/workspace/tools');
  60. }}
  61. type="button"
  62. >
  63. <div class=" self-center">
  64. <svg
  65. xmlns="http://www.w3.org/2000/svg"
  66. viewBox="0 0 20 20"
  67. fill="currentColor"
  68. class="w-4 h-4"
  69. >
  70. <path
  71. fill-rule="evenodd"
  72. d="M17 10a.75.75 0 01-.75.75H5.612l4.158 3.96a.75.75 0 11-1.04 1.08l-5.5-5.25a.75.75 0 010-1.08l5.5-5.25a.75.75 0 111.04 1.08L5.612 9.25H16.25A.75.75 0 0117 10z"
  73. clip-rule="evenodd"
  74. />
  75. </svg>
  76. </div>
  77. <div class=" self-center font-medium text-sm">{$i18n.t('Back')}</div>
  78. </button>
  79. </div>
  80. <div class="flex flex-col flex-1 overflow-auto h-0 rounded-lg">
  81. <div class="w-full mb-2 flex flex-col gap-1.5">
  82. <div class="flex gap-2 w-full">
  83. <input
  84. class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  85. type="text"
  86. placeholder="Toolkit Name (e.g. My ToolKit)"
  87. bind:value={name}
  88. required
  89. />
  90. <input
  91. class="w-full px-3 py-2 text-sm font-medium disabled:text-gray-300 dark:disabled:text-gray-700 bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  92. type="text"
  93. placeholder="Toolkit ID (e.g. my_toolkit)"
  94. bind:value={id}
  95. required
  96. disabled={edit}
  97. />
  98. </div>
  99. <input
  100. class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  101. type="text"
  102. placeholder="Toolkit Description (e.g. A toolkit for performing various operations)"
  103. bind:value={meta.description}
  104. required
  105. />
  106. </div>
  107. <div class="mb-2 flex-1 overflow-auto h-0 rounded-lg">
  108. <CodeEditor
  109. bind:value={content}
  110. bind:this={codeEditor}
  111. on:save={() => {
  112. if (formElement) {
  113. formElement.requestSubmit();
  114. }
  115. }}
  116. />
  117. </div>
  118. <div class="pb-3 flex justify-between">
  119. <div class="flex-1 pr-3">
  120. <div class="text-xs text-gray-500 line-clamp-2">
  121. <span class=" font-semibold dark:text-gray-200">Warning:</span> Tools are a function
  122. calling system with arbitrary code execution <br />—
  123. <span class=" font-medium dark:text-gray-400"
  124. >don't install random tools from sources you don't trust.</span
  125. >
  126. </div>
  127. </div>
  128. <button
  129. class="px-3 py-1.5 text-sm font-medium bg-emerald-600 hover:bg-emerald-700 text-gray-50 transition rounded-lg"
  130. type="submit"
  131. >
  132. {$i18n.t('Save')}
  133. </button>
  134. </div>
  135. </div>
  136. </form>
  137. </div>
  138. </div>
  139. <ConfirmDialog
  140. bind:show={showConfirm}
  141. on:confirm={() => {
  142. submitHandler();
  143. }}
  144. >
  145. <div class="text-sm text-gray-500">
  146. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  147. <div>Please carefully review the following warnings:</div>
  148. <ul class=" mt-1 list-disc pl-4 text-xs">
  149. <li>Tools have a function calling system that allows arbitrary code execution.</li>
  150. <li>Do not install tools from sources you do not fully trust.</li>
  151. </ul>
  152. </div>
  153. <div class="my-3">
  154. I acknowledge that I have read and I understand the implications of my action. I am aware of
  155. the risks associated with executing arbitrary code and I have verified the trustworthiness of
  156. the source.
  157. </div>
  158. </div>
  159. </ConfirmDialog>