ToolkitEditor.svelte 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. showConfirm = true;
  49. }}
  50. >
  51. <div class="mb-2.5">
  52. <button
  53. class="flex space-x-1"
  54. on:click={() => {
  55. goto('/workspace/tools');
  56. }}
  57. type="button"
  58. >
  59. <div class=" self-center">
  60. <svg
  61. xmlns="http://www.w3.org/2000/svg"
  62. viewBox="0 0 20 20"
  63. fill="currentColor"
  64. class="w-4 h-4"
  65. >
  66. <path
  67. fill-rule="evenodd"
  68. 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"
  69. clip-rule="evenodd"
  70. />
  71. </svg>
  72. </div>
  73. <div class=" self-center font-medium text-sm">{$i18n.t('Back')}</div>
  74. </button>
  75. </div>
  76. <div class="flex flex-col flex-1 overflow-auto h-0 rounded-lg">
  77. <div class="w-full mb-2 flex flex-col gap-1.5">
  78. <div class="flex gap-2 w-full">
  79. <input
  80. 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"
  81. type="text"
  82. placeholder="Toolkit Name (e.g. My ToolKit)"
  83. bind:value={name}
  84. required
  85. />
  86. <input
  87. 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"
  88. type="text"
  89. placeholder="Toolkit ID (e.g. my_toolkit)"
  90. bind:value={id}
  91. required
  92. disabled={edit}
  93. />
  94. </div>
  95. <input
  96. 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"
  97. type="text"
  98. placeholder="Toolkit Description (e.g. A toolkit for performing various operations)"
  99. bind:value={meta.description}
  100. required
  101. />
  102. </div>
  103. <div class="mb-2 flex-1 overflow-auto h-0 rounded-lg">
  104. <CodeEditor
  105. bind:value={content}
  106. bind:this={codeEditor}
  107. on:save={() => {
  108. if (formElement) {
  109. formElement.requestSubmit();
  110. }
  111. }}
  112. />
  113. </div>
  114. <div class="pb-3 flex justify-between">
  115. <div class="flex-1 pr-3">
  116. <div class="text-xs text-gray-500 line-clamp-2">
  117. <span class=" font-semibold dark:text-gray-200">Warning:</span> Tools are a function
  118. calling system with arbitrary code execution <br />—
  119. <span class=" font-medium dark:text-gray-400"
  120. >don't install random tools from sources you don't trust.</span
  121. >
  122. </div>
  123. </div>
  124. <button
  125. class="px-3 py-1.5 text-sm font-medium bg-emerald-600 hover:bg-emerald-700 text-gray-50 transition rounded-lg"
  126. type="submit"
  127. >
  128. {$i18n.t('Save')}
  129. </button>
  130. </div>
  131. </div>
  132. </form>
  133. </div>
  134. </div>
  135. <ConfirmDialog
  136. bind:show={showConfirm}
  137. on:confirm={() => {
  138. submitHandler();
  139. }}
  140. >
  141. <div class="text-sm text-gray-500">
  142. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  143. <div>Please carefully review the following warnings:</div>
  144. <ul class=" mt-1 list-disc pl-4 text-xs">
  145. <li>Tools have a function calling system that allows arbitrary code execution.</li>
  146. <li>Do not install tools from sources you do not fully trust.</li>
  147. </ul>
  148. </div>
  149. <div class="my-3">
  150. I acknowledge that I have read and I understand the implications of my action. I am aware of
  151. the risks associated with executing arbitrary code and I have verified the trustworthiness of
  152. the source.
  153. </div>
  154. </div>
  155. </ConfirmDialog>