Prompts.svelte 10 KB

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