Prompts.svelte 11 KB

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