Prompts.svelte 9.3 KB

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