Tools.svelte 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import { onMount, getContext } from 'svelte';
  6. import { WEBUI_NAME, prompts, tools } from '$lib/stores';
  7. import { createNewPrompt, deletePromptByCommand, getPrompts } from '$lib/apis/prompts';
  8. import { goto } from '$app/navigation';
  9. const i18n = getContext('i18n');
  10. let toolsImportInputElement: HTMLInputElement;
  11. let importFiles;
  12. let query = '';
  13. </script>
  14. <svelte:head>
  15. <title>
  16. {$i18n.t('Tools')} | {$WEBUI_NAME}
  17. </title>
  18. </svelte:head>
  19. <div class="mb-3 flex justify-between items-center">
  20. <div class=" text-lg font-semibold self-center">{$i18n.t('Tools')}</div>
  21. </div>
  22. <div class=" flex w-full space-x-2">
  23. <div class="flex flex-1">
  24. <div class=" self-center ml-1 mr-3">
  25. <svg
  26. xmlns="http://www.w3.org/2000/svg"
  27. viewBox="0 0 20 20"
  28. fill="currentColor"
  29. class="w-4 h-4"
  30. >
  31. <path
  32. fill-rule="evenodd"
  33. d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z"
  34. clip-rule="evenodd"
  35. />
  36. </svg>
  37. </div>
  38. <input
  39. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  40. bind:value={query}
  41. placeholder={$i18n.t('Search Tools')}
  42. />
  43. </div>
  44. <div>
  45. <a
  46. class=" px-2 py-2 rounded-xl border border-gray-200 dark:border-gray-600 dark:border-0 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 transition font-medium text-sm flex items-center space-x-1"
  47. href="/workspace/tools/create"
  48. >
  49. <svg
  50. xmlns="http://www.w3.org/2000/svg"
  51. viewBox="0 0 16 16"
  52. fill="currentColor"
  53. class="w-4 h-4"
  54. >
  55. <path
  56. d="M8.75 3.75a.75.75 0 0 0-1.5 0v3.5h-3.5a.75.75 0 0 0 0 1.5h3.5v3.5a.75.75 0 0 0 1.5 0v-3.5h3.5a.75.75 0 0 0 0-1.5h-3.5v-3.5Z"
  57. />
  58. </svg>
  59. </a>
  60. </div>
  61. </div>
  62. <hr class=" dark:border-gray-850 my-2.5" />
  63. <div class="my-3 mb-5">
  64. {#each $tools.filter((t) => query === '' || t.name.includes(query)) as tool}
  65. <div
  66. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl"
  67. >
  68. <div class=" flex flex-1 space-x-4 cursor-pointer w-full">
  69. <a href={`/workspace/tools/edit?id=${encodeURIComponent(tool.id)}`}>
  70. <div class=" flex-1 self-center pl-5">
  71. <div class=" font-bold">{tool.name}</div>
  72. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1">
  73. {tool.meta.description}
  74. </div>
  75. </div>
  76. </a>
  77. </div>
  78. <div class="flex flex-row space-x-1 self-center">
  79. <a
  80. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  81. type="button"
  82. href={`/workspace/tools/edit?command=${encodeURIComponent(tool.id)}`}
  83. >
  84. <svg
  85. xmlns="http://www.w3.org/2000/svg"
  86. fill="none"
  87. viewBox="0 0 24 24"
  88. stroke-width="1.5"
  89. stroke="currentColor"
  90. class="w-4 h-4"
  91. >
  92. <path
  93. stroke-linecap="round"
  94. stroke-linejoin="round"
  95. d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"
  96. />
  97. </svg>
  98. </a>
  99. <button
  100. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  101. type="button"
  102. on:click={() => {
  103. sessionStorage.tool = JSON.stringify(tool);
  104. goto('/workspace/tools/create');
  105. }}
  106. >
  107. <svg
  108. xmlns="http://www.w3.org/2000/svg"
  109. fill="none"
  110. viewBox="0 0 24 24"
  111. stroke-width="1.5"
  112. stroke="currentColor"
  113. class="w-4 h-4"
  114. >
  115. <path
  116. stroke-linecap="round"
  117. stroke-linejoin="round"
  118. d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 0 1-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 0 1 1.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 0 0-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 0 1-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 0 0-3.375-3.375h-1.5a1.125 1.125 0 0 1-1.125-1.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H9.75"
  119. />
  120. </svg>
  121. </button>
  122. <button
  123. class="self-center w-fit text-sm px-2 py-2 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  124. type="button"
  125. on:click={() => {
  126. // deletePrompt(prompt.command);
  127. // deleteTool
  128. }}
  129. >
  130. <svg
  131. xmlns="http://www.w3.org/2000/svg"
  132. fill="none"
  133. viewBox="0 0 24 24"
  134. stroke-width="1.5"
  135. stroke="currentColor"
  136. class="w-4 h-4"
  137. >
  138. <path
  139. stroke-linecap="round"
  140. stroke-linejoin="round"
  141. d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"
  142. />
  143. </svg>
  144. </button>
  145. </div>
  146. </div>
  147. {/each}
  148. </div>
  149. <div class=" flex justify-end w-full mb-2">
  150. <div class="flex space-x-2">
  151. <input
  152. id="documents-import-input"
  153. bind:this={toolsImportInputElement}
  154. bind:files={importFiles}
  155. type="file"
  156. accept=".json"
  157. hidden
  158. on:change={() => {
  159. console.log(importFiles);
  160. const reader = new FileReader();
  161. reader.onload = async (event) => {
  162. const tools = JSON.parse(event.target.result);
  163. console.log(tools);
  164. };
  165. reader.readAsText(importFiles[0]);
  166. }}
  167. />
  168. <button
  169. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  170. on:click={() => {
  171. toolsImportInputElement.click();
  172. }}
  173. >
  174. <div class=" self-center mr-2 font-medium">{$i18n.t('Import Tools')}</div>
  175. <div class=" self-center">
  176. <svg
  177. xmlns="http://www.w3.org/2000/svg"
  178. viewBox="0 0 16 16"
  179. fill="currentColor"
  180. class="w-4 h-4"
  181. >
  182. <path
  183. fill-rule="evenodd"
  184. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 9.5a.75.75 0 0 1-.75-.75V8.06l-.72.72a.75.75 0 0 1-1.06-1.06l2-2a.75.75 0 0 1 1.06 0l2 2a.75.75 0 1 1-1.06 1.06l-.72-.72v2.69a.75.75 0 0 1-.75.75Z"
  185. clip-rule="evenodd"
  186. />
  187. </svg>
  188. </div>
  189. </button>
  190. <button
  191. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  192. on:click={async () => {
  193. let blob = new Blob([JSON.stringify($tools)], {
  194. type: 'application/json'
  195. });
  196. saveAs(blob, `tools-export-${Date.now()}.json`);
  197. }}
  198. >
  199. <div class=" self-center mr-2 font-medium">{$i18n.t('Export Tools')}</div>
  200. <div class=" self-center">
  201. <svg
  202. xmlns="http://www.w3.org/2000/svg"
  203. viewBox="0 0 16 16"
  204. fill="currentColor"
  205. class="w-4 h-4"
  206. >
  207. <path
  208. fill-rule="evenodd"
  209. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 3.5a.75.75 0 0 1 .75.75v2.69l.72-.72a.75.75 0 1 1 1.06 1.06l-2 2a.75.75 0 0 1-1.06 0l-2-2a.75.75 0 0 1 1.06-1.06l.72.72V6.25A.75.75 0 0 1 8 5.5Z"
  210. clip-rule="evenodd"
  211. />
  212. </svg>
  213. </div>
  214. </button>
  215. </div>
  216. </div>