Prompts.svelte 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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, config, prompts } from '$lib/stores';
  7. import { createNewPrompt, deletePromptByCommand, getPrompts } from '$lib/apis/prompts';
  8. import { error } from '@sveltejs/kit';
  9. import { goto } from '$app/navigation';
  10. import PromptMenu from './Prompts/PromptMenu.svelte';
  11. import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte';
  12. import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  13. const i18n = getContext('i18n');
  14. let importFiles = '';
  15. let query = '';
  16. let promptsImportInputElement: HTMLInputElement;
  17. let showDeleteConfirm = false;
  18. let deletePrompt = null;
  19. let filteredItems = [];
  20. $: filteredItems = $prompts.filter((p) => query === '' || p.command.includes(query));
  21. const shareHandler = async (prompt) => {
  22. toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
  23. const url = 'https://openwebui.com';
  24. const tab = await window.open(`${url}/prompts/create`, '_blank');
  25. window.addEventListener(
  26. 'message',
  27. (event) => {
  28. if (event.origin !== url) return;
  29. if (event.data === 'loaded') {
  30. tab.postMessage(JSON.stringify(prompt), '*');
  31. }
  32. },
  33. false
  34. );
  35. };
  36. const cloneHandler = async (prompt) => {
  37. sessionStorage.prompt = JSON.stringify(prompt);
  38. goto('/workspace/prompts/create');
  39. };
  40. const exportHandler = async (prompt) => {
  41. let blob = new Blob([JSON.stringify([prompt])], {
  42. type: 'application/json'
  43. });
  44. saveAs(blob, `prompt-export-${Date.now()}.json`);
  45. };
  46. const deleteHandler = async (prompt) => {
  47. const command = prompt.command;
  48. await deletePromptByCommand(localStorage.token, command);
  49. await prompts.set(await getPrompts(localStorage.token));
  50. };
  51. </script>
  52. <svelte:head>
  53. <title>
  54. {$i18n.t('Prompts')} | {$WEBUI_NAME}
  55. </title>
  56. </svelte:head>
  57. <DeleteConfirmDialog
  58. bind:show={showDeleteConfirm}
  59. title={$i18n.t('Delete prompt?')}
  60. on:confirm={() => {
  61. deleteHandler(deletePrompt);
  62. }}
  63. >
  64. <div class=" text-sm text-gray-500">
  65. {$i18n.t('This will delete')} <span class=" font-semibold">{deletePrompt.command}</span>.
  66. </div>
  67. </DeleteConfirmDialog>
  68. <div class=" flex w-full space-x-2 mb-2.5">
  69. <div class="flex flex-1">
  70. <div class=" self-center ml-1 mr-3">
  71. <svg
  72. xmlns="http://www.w3.org/2000/svg"
  73. viewBox="0 0 20 20"
  74. fill="currentColor"
  75. class="w-4 h-4"
  76. >
  77. <path
  78. fill-rule="evenodd"
  79. 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"
  80. clip-rule="evenodd"
  81. />
  82. </svg>
  83. </div>
  84. <input
  85. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-none bg-transparent"
  86. bind:value={query}
  87. placeholder={$i18n.t('Search Prompts')}
  88. />
  89. </div>
  90. <div>
  91. <a
  92. 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"
  93. href="/workspace/prompts/create"
  94. >
  95. <svg
  96. xmlns="http://www.w3.org/2000/svg"
  97. viewBox="0 0 16 16"
  98. fill="currentColor"
  99. class="w-4 h-4"
  100. >
  101. <path
  102. 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"
  103. />
  104. </svg>
  105. </a>
  106. </div>
  107. </div>
  108. <div class="mb-3.5">
  109. <div class="flex justify-between items-center">
  110. <div class="flex md:self-center text-base font-medium px-0.5">
  111. {$i18n.t('Prompts')}
  112. <div class="flex self-center w-[1px] h-6 mx-2.5 bg-gray-200 dark:bg-gray-700" />
  113. <span class="text-base font-medium text-gray-500 dark:text-gray-300"
  114. >{filteredItems.length}</span
  115. >
  116. </div>
  117. </div>
  118. </div>
  119. <div class="my-3 mb-5">
  120. {#each filteredItems as prompt}
  121. <div
  122. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl"
  123. >
  124. <div class=" flex flex-1 space-x-4 cursor-pointer w-full">
  125. <a href={`/workspace/prompts/edit?command=${encodeURIComponent(prompt.command)}`}>
  126. <div class=" flex-1 self-center pl-1.5">
  127. <div class=" font-semibold line-clamp-1">{prompt.command}</div>
  128. <div class=" text-xs overflow-hidden text-ellipsis line-clamp-1">
  129. {prompt.title}
  130. </div>
  131. </div>
  132. </a>
  133. </div>
  134. <div class="flex flex-row gap-0.5 self-center">
  135. <a
  136. 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"
  137. type="button"
  138. href={`/workspace/prompts/edit?command=${encodeURIComponent(prompt.command)}`}
  139. >
  140. <svg
  141. xmlns="http://www.w3.org/2000/svg"
  142. fill="none"
  143. viewBox="0 0 24 24"
  144. stroke-width="1.5"
  145. stroke="currentColor"
  146. class="w-4 h-4"
  147. >
  148. <path
  149. stroke-linecap="round"
  150. stroke-linejoin="round"
  151. 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"
  152. />
  153. </svg>
  154. </a>
  155. <PromptMenu
  156. shareHandler={() => {
  157. shareHandler(prompt);
  158. }}
  159. cloneHandler={() => {
  160. cloneHandler(prompt);
  161. }}
  162. exportHandler={() => {
  163. exportHandler(prompt);
  164. }}
  165. deleteHandler={async () => {
  166. deletePrompt = prompt;
  167. showDeleteConfirm = true;
  168. }}
  169. onClose={() => {}}
  170. >
  171. <button
  172. class="self-center w-fit text-sm p-1.5 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  173. type="button"
  174. >
  175. <EllipsisHorizontal className="size-5" />
  176. </button>
  177. </PromptMenu>
  178. </div>
  179. </div>
  180. {/each}
  181. </div>
  182. <div class=" flex justify-end w-full mb-3">
  183. <div class="flex space-x-2">
  184. <input
  185. id="prompts-import-input"
  186. bind:this={promptsImportInputElement}
  187. bind:files={importFiles}
  188. type="file"
  189. accept=".json"
  190. hidden
  191. on:change={() => {
  192. console.log(importFiles);
  193. const reader = new FileReader();
  194. reader.onload = async (event) => {
  195. const savedPrompts = JSON.parse(event.target.result);
  196. console.log(savedPrompts);
  197. for (const prompt of savedPrompts) {
  198. await createNewPrompt(
  199. localStorage.token,
  200. prompt.command.charAt(0) === '/' ? prompt.command.slice(1) : prompt.command,
  201. prompt.title,
  202. prompt.content
  203. ).catch((error) => {
  204. toast.error(error);
  205. return null;
  206. });
  207. }
  208. await prompts.set(await getPrompts(localStorage.token));
  209. };
  210. reader.readAsText(importFiles[0]);
  211. }}
  212. />
  213. <button
  214. 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"
  215. on:click={() => {
  216. promptsImportInputElement.click();
  217. }}
  218. >
  219. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Import Prompts')}</div>
  220. <div class=" self-center">
  221. <svg
  222. xmlns="http://www.w3.org/2000/svg"
  223. viewBox="0 0 16 16"
  224. fill="currentColor"
  225. class="w-4 h-4"
  226. >
  227. <path
  228. fill-rule="evenodd"
  229. 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"
  230. clip-rule="evenodd"
  231. />
  232. </svg>
  233. </div>
  234. </button>
  235. <button
  236. 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"
  237. on:click={async () => {
  238. // promptsImportInputElement.click();
  239. let blob = new Blob([JSON.stringify($prompts)], {
  240. type: 'application/json'
  241. });
  242. saveAs(blob, `prompts-export-${Date.now()}.json`);
  243. }}
  244. >
  245. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Export Prompts')}</div>
  246. <div class=" self-center">
  247. <svg
  248. xmlns="http://www.w3.org/2000/svg"
  249. viewBox="0 0 16 16"
  250. fill="currentColor"
  251. class="w-4 h-4"
  252. >
  253. <path
  254. fill-rule="evenodd"
  255. 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"
  256. clip-rule="evenodd"
  257. />
  258. </svg>
  259. </div>
  260. </button>
  261. <!-- <button
  262. on:click={() => {
  263. loadDefaultPrompts();
  264. }}
  265. >
  266. dd
  267. </button> -->
  268. </div>
  269. </div>
  270. {#if $config?.features.enable_community_sharing}
  271. <div class=" my-16">
  272. <div class=" text-lg font-semibold mb-3 line-clamp-1">
  273. {$i18n.t('Made by OpenWebUI Community')}
  274. </div>
  275. <a
  276. class=" flex space-x-4 cursor-pointer w-full mb-2 px-3 py-2"
  277. href="https://openwebui.com/#open-webui-community"
  278. target="_blank"
  279. >
  280. <div class=" self-center w-10 flex-shrink-0">
  281. <div
  282. class="w-full h-10 flex justify-center rounded-full bg-transparent dark:bg-gray-700 border border-dashed border-gray-200"
  283. >
  284. <svg
  285. xmlns="http://www.w3.org/2000/svg"
  286. viewBox="0 0 24 24"
  287. fill="currentColor"
  288. class="w-6"
  289. >
  290. <path
  291. fill-rule="evenodd"
  292. d="M12 3.75a.75.75 0 01.75.75v6.75h6.75a.75.75 0 010 1.5h-6.75v6.75a.75.75 0 01-1.5 0v-6.75H4.5a.75.75 0 010-1.5h6.75V4.5a.75.75 0 01.75-.75z"
  293. clip-rule="evenodd"
  294. />
  295. </svg>
  296. </div>
  297. </div>
  298. <div class=" self-center">
  299. <div class=" font-semibold line-clamp-1">{$i18n.t('Discover a prompt')}</div>
  300. <div class=" text-sm line-clamp-1">
  301. {$i18n.t('Discover, download, and explore custom prompts')}
  302. </div>
  303. </div>
  304. </a>
  305. </div>
  306. {/if}