PromptEditor.svelte 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <script lang="ts">
  2. import { onMount, tick, getContext } from 'svelte';
  3. import Textarea from '$lib/components/common/Textarea.svelte';
  4. import { toast } from 'svelte-sonner';
  5. import Tooltip from '$lib/components/common/Tooltip.svelte';
  6. import AccessControl from '../common/AccessControl.svelte';
  7. export let onSubmit: Function;
  8. export let edit = false;
  9. export let prompt = null;
  10. const i18n = getContext('i18n');
  11. let loading = false;
  12. let title = '';
  13. let command = '';
  14. let content = '';
  15. let accessControl = null;
  16. $: if (!edit) {
  17. command = title !== '' ? `${title.replace(/\s+/g, '-').toLowerCase()}` : '';
  18. }
  19. const submitHandler = async () => {
  20. loading = true;
  21. if (validateCommandString(command)) {
  22. await onSubmit({
  23. title,
  24. command,
  25. content,
  26. access_control: accessControl
  27. });
  28. } else {
  29. toast.error(
  30. $i18n.t('Only alphanumeric characters and hyphens are allowed in the command string.')
  31. );
  32. }
  33. loading = false;
  34. };
  35. const validateCommandString = (inputString) => {
  36. // Regular expression to match only alphanumeric characters and hyphen
  37. const regex = /^[a-zA-Z0-9-]+$/;
  38. // Test the input string against the regular expression
  39. return regex.test(inputString);
  40. };
  41. onMount(async () => {
  42. if (prompt) {
  43. title = prompt.title;
  44. await tick();
  45. command = prompt.command.at(0) === '/' ? prompt.command.slice(1) : prompt.command;
  46. content = prompt.content;
  47. accessControl = prompt?.access_control ?? null;
  48. }
  49. });
  50. </script>
  51. <div class="w-full max-h-full flex justify-center">
  52. <form
  53. class="flex flex-col w-full mb-10"
  54. on:submit|preventDefault={() => {
  55. submitHandler();
  56. }}
  57. >
  58. <div class="my-2">
  59. <Tooltip
  60. content={`${$i18n.t('Only alphanumeric characters and hyphens are allowed')} - ${$i18n.t(
  61. 'Activate this command by typing "/{{COMMAND}}" to chat input.',
  62. {
  63. COMMAND: command
  64. }
  65. )}`}
  66. placement="bottom-start"
  67. >
  68. <div class="flex flex-col w-full">
  69. <div>
  70. <input
  71. class="text-2xl font-semibold w-full bg-transparent outline-none"
  72. placeholder={$i18n.t('Title')}
  73. bind:value={title}
  74. required
  75. />
  76. </div>
  77. <div class="flex gap-0.5 items-center text-xs text-gray-500">
  78. <div class="">/</div>
  79. <input
  80. class=" w-full bg-transparent outline-none"
  81. placeholder={$i18n.t('Command')}
  82. bind:value={command}
  83. required
  84. disabled={edit}
  85. />
  86. </div>
  87. </div>
  88. </Tooltip>
  89. </div>
  90. <div class="my-2">
  91. <div class="flex w-full justify-between">
  92. <div class=" self-center text-sm font-semibold">{$i18n.t('Prompt Content')}</div>
  93. </div>
  94. <div class="mt-2">
  95. <div>
  96. <Textarea
  97. className="text-sm w-full bg-transparent outline-none overflow-y-hidden resize-none"
  98. placeholder={$i18n.t('Write a summary in 50 words that summarizes [topic or keyword].')}
  99. bind:value={content}
  100. rows={6}
  101. required
  102. />
  103. </div>
  104. <div class="text-xs text-gray-400 dark:text-gray-500">
  105. ⓘ {$i18n.t('Format your variables using brackets like this:')}&nbsp;<span
  106. class=" text-gray-600 dark:text-gray-300 font-medium"
  107. >{'{{'}{$i18n.t('variable')}{'}}'}</span
  108. >.
  109. {$i18n.t('Make sure to enclose them with')}
  110. <span class=" text-gray-600 dark:text-gray-300 font-medium">{'{{'}</span>
  111. {$i18n.t('and')}
  112. <span class=" text-gray-600 dark:text-gray-300 font-medium">{'}}'}</span>.
  113. </div>
  114. <div class="text-xs text-gray-400 dark:text-gray-500">
  115. {$i18n.t('Utilize')}<span class=" text-gray-600 dark:text-gray-300 font-medium">
  116. {` {{CLIPBOARD}}`}</span
  117. >
  118. {$i18n.t('variable to have them replaced with clipboard content.')}
  119. </div>
  120. </div>
  121. </div>
  122. <div class="mt-2">
  123. <div class="px-3 py-2 bg-gray-50 dark:bg-gray-950 rounded-lg">
  124. <AccessControl bind:accessControl />
  125. </div>
  126. </div>
  127. <div class="my-4 flex justify-end pb-20">
  128. <button
  129. class=" text-sm w-full lg:w-fit px-4 py-2 transition rounded-lg {loading
  130. ? ' cursor-not-allowed bg-black hover:bg-gray-900 text-white dark:bg-white dark:hover:bg-gray-100 dark:text-black'
  131. : 'bg-black hover:bg-gray-900 text-white dark:bg-white dark:hover:bg-gray-100 dark:text-black'} flex w-full justify-center"
  132. type="submit"
  133. disabled={loading}
  134. >
  135. <div class=" self-center font-medium">{$i18n.t('Save & Create')}</div>
  136. {#if loading}
  137. <div class="ml-1.5 self-center">
  138. <svg
  139. class=" w-4 h-4"
  140. viewBox="0 0 24 24"
  141. fill="currentColor"
  142. xmlns="http://www.w3.org/2000/svg"
  143. ><style>
  144. .spinner_ajPY {
  145. transform-origin: center;
  146. animation: spinner_AtaB 0.75s infinite linear;
  147. }
  148. @keyframes spinner_AtaB {
  149. 100% {
  150. transform: rotate(360deg);
  151. }
  152. }
  153. </style><path
  154. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  155. opacity=".25"
  156. /><path
  157. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  158. class="spinner_ajPY"
  159. /></svg
  160. >
  161. </div>
  162. {/if}
  163. </button>
  164. </div>
  165. </form>
  166. </div>