Prompts.svelte 8.9 KB

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