Feedbacks.svelte 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import dayjs from 'dayjs';
  6. import relativeTime from 'dayjs/plugin/relativeTime';
  7. dayjs.extend(relativeTime);
  8. import { onMount, getContext } from 'svelte';
  9. const i18n = getContext('i18n');
  10. import { deleteFeedbackById, exportAllFeedbacks, getAllFeedbacks } from '$lib/apis/evaluations';
  11. import Tooltip from '$lib/components/common/Tooltip.svelte';
  12. import ArrowDownTray from '$lib/components/icons/ArrowDownTray.svelte';
  13. import Badge from '$lib/components/common/Badge.svelte';
  14. import CloudArrowUp from '$lib/components/icons/CloudArrowUp.svelte';
  15. import Pagination from '$lib/components/common/Pagination.svelte';
  16. import FeedbackMenu from './FeedbackMenu.svelte';
  17. import EllipsisHorizontal from '$lib/components/icons/EllipsisHorizontal.svelte';
  18. export let feedbacks = [];
  19. let page = 1;
  20. $: paginatedFeedbacks = feedbacks.slice((page - 1) * 10, page * 10);
  21. type Feedback = {
  22. id: string;
  23. data: {
  24. rating: number;
  25. model_id: string;
  26. sibling_model_ids: string[] | null;
  27. reason: string;
  28. comment: string;
  29. tags: string[];
  30. };
  31. user: {
  32. name: string;
  33. profile_image_url: string;
  34. };
  35. updated_at: number;
  36. };
  37. type ModelStats = {
  38. rating: number;
  39. won: number;
  40. lost: number;
  41. };
  42. //////////////////////
  43. //
  44. // CRUD operations
  45. //
  46. //////////////////////
  47. const deleteFeedbackHandler = async (feedbackId: string) => {
  48. const response = await deleteFeedbackById(localStorage.token, feedbackId).catch((err) => {
  49. toast.error(err);
  50. return null;
  51. });
  52. if (response) {
  53. feedbacks = feedbacks.filter((f) => f.id !== feedbackId);
  54. }
  55. };
  56. const shareHandler = async () => {
  57. toast.success($i18n.t('Redirecting you to Open WebUI Community'));
  58. // remove snapshot from feedbacks
  59. const feedbacksToShare = feedbacks.map((f) => {
  60. const { snapshot, user, ...rest } = f;
  61. return rest;
  62. });
  63. console.log(feedbacksToShare);
  64. const url = 'https://openwebui.com';
  65. const tab = await window.open(`${url}/leaderboard`, '_blank');
  66. // Define the event handler function
  67. const messageHandler = (event) => {
  68. if (event.origin !== url) return;
  69. if (event.data === 'loaded') {
  70. tab.postMessage(JSON.stringify(feedbacksToShare), '*');
  71. // Remove the event listener after handling the message
  72. window.removeEventListener('message', messageHandler);
  73. }
  74. };
  75. window.addEventListener('message', messageHandler, false);
  76. };
  77. const exportHandler = async () => {
  78. if (feedbacks.length === 0) {
  79. toast.info($i18n.t('No feedback available to export.'));
  80. return;
  81. }
  82. const _feedbacks = await exportAllFeedbacks(localStorage.token).catch((err) => {
  83. toast.error(err);
  84. return null;
  85. });
  86. if (_feedbacks) {
  87. let blob = new Blob([JSON.stringify(_feedbacks)], {
  88. type: 'application/json'
  89. });
  90. saveAs(blob, `feedback-history-export-${Date.now()}.json`);
  91. }
  92. };
  93. </script>
  94. <div class="mt-0.5 mb-2 gap-1 flex flex-row justify-between">
  95. <div class="flex md:self-center text-lg font-medium px-0.5">
  96. {$i18n.t('Feedback History')}
  97. <div class="flex self-center w-[1px] h-6 mx-2.5 bg-gray-50 dark:bg-gray-850" />
  98. <span class="text-lg font-medium text-gray-500 dark:text-gray-300">{feedbacks.length}</span>
  99. </div>
  100. {#if feedbacks.length > 0}
  101. <div>
  102. <Tooltip content={$i18n.t('Export')}>
  103. <button
  104. class=" p-2 rounded-xl hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 transition font-medium text-sm flex items-center space-x-1"
  105. on:click={() => {
  106. exportHandler();
  107. }}
  108. >
  109. <ArrowDownTray className="size-3" />
  110. </button>
  111. </Tooltip>
  112. </div>
  113. {/if}
  114. </div>
  115. <div
  116. class="scrollbar-hidden relative whitespace-nowrap overflow-x-auto max-w-full rounded-sm pt-0.5"
  117. >
  118. {#if (feedbacks ?? []).length === 0}
  119. <div class="text-center text-xs text-gray-500 dark:text-gray-400 py-1">
  120. {$i18n.t('No feedbacks found')}
  121. </div>
  122. {:else}
  123. <table
  124. class="w-full text-sm text-left text-gray-500 dark:text-gray-400 table-auto max-w-full rounded-sm"
  125. >
  126. <thead
  127. class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-850 dark:text-gray-400 -translate-y-0.5"
  128. >
  129. <tr class="">
  130. <th scope="col" class="px-3 text-right cursor-pointer select-none w-0">
  131. {$i18n.t('User')}
  132. </th>
  133. <th scope="col" class="px-3 pr-1.5 cursor-pointer select-none">
  134. {$i18n.t('Models')}
  135. </th>
  136. <th scope="col" class="px-3 py-1.5 text-right cursor-pointer select-none w-fit">
  137. {$i18n.t('Result')}
  138. </th>
  139. <th scope="col" class="px-3 py-1.5 text-right cursor-pointer select-none w-0">
  140. {$i18n.t('Updated At')}
  141. </th>
  142. <th scope="col" class="px-3 py-1.5 text-right cursor-pointer select-none w-0"> </th>
  143. </tr>
  144. </thead>
  145. <tbody class="">
  146. {#each paginatedFeedbacks as feedback (feedback.id)}
  147. <tr class="bg-white dark:bg-gray-900 dark:border-gray-850 text-xs">
  148. <td class=" py-0.5 text-right font-semibold">
  149. <div class="flex justify-center">
  150. <Tooltip content={feedback?.user?.name}>
  151. <div class="shrink-0">
  152. <img
  153. src={feedback?.user?.profile_image_url ?? '/user.png'}
  154. alt={feedback?.user?.name}
  155. class="size-5 rounded-full object-cover shrink-0"
  156. />
  157. </div>
  158. </Tooltip>
  159. </div>
  160. </td>
  161. <td class=" py-1 pl-3 flex flex-col">
  162. <div class="flex flex-col items-start gap-0.5 h-full">
  163. <div class="flex flex-col h-full">
  164. {#if feedback.data?.sibling_model_ids}
  165. <div class="font-semibold text-gray-600 dark:text-gray-400 flex-1">
  166. {feedback.data?.model_id}
  167. </div>
  168. <Tooltip content={feedback.data.sibling_model_ids.join(', ')}>
  169. <div class=" text-[0.65rem] text-gray-600 dark:text-gray-400 line-clamp-1">
  170. {#if feedback.data.sibling_model_ids.length > 2}
  171. <!-- {$i18n.t('and {{COUNT}} more')} -->
  172. {feedback.data.sibling_model_ids.slice(0, 2).join(', ')}, {$i18n.t(
  173. 'and {{COUNT}} more',
  174. { COUNT: feedback.data.sibling_model_ids.length - 2 }
  175. )}
  176. {:else}
  177. {feedback.data.sibling_model_ids.join(', ')}
  178. {/if}
  179. </div>
  180. </Tooltip>
  181. {:else}
  182. <div
  183. class=" text-sm font-medium text-gray-600 dark:text-gray-400 flex-1 py-1.5"
  184. >
  185. {feedback.data?.model_id}
  186. </div>
  187. {/if}
  188. </div>
  189. </div>
  190. </td>
  191. <td class="px-3 py-1 text-right font-medium text-gray-900 dark:text-white w-max">
  192. <div class=" flex justify-end">
  193. {#if feedback.data.rating.toString() === '1'}
  194. <Badge type="info" content={$i18n.t('Won')} />
  195. {:else if feedback.data.rating.toString() === '0'}
  196. <Badge type="muted" content={$i18n.t('Draw')} />
  197. {:else if feedback.data.rating.toString() === '-1'}
  198. <Badge type="error" content={$i18n.t('Lost')} />
  199. {/if}
  200. </div>
  201. </td>
  202. <td class=" px-3 py-1 text-right font-medium">
  203. {dayjs(feedback.updated_at * 1000).fromNow()}
  204. </td>
  205. <td class=" px-3 py-1 text-right font-semibold">
  206. <FeedbackMenu
  207. on:delete={(e) => {
  208. deleteFeedbackHandler(feedback.id);
  209. }}
  210. >
  211. <button
  212. 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"
  213. >
  214. <EllipsisHorizontal />
  215. </button>
  216. </FeedbackMenu>
  217. </td>
  218. </tr>
  219. {/each}
  220. </tbody>
  221. </table>
  222. {/if}
  223. </div>
  224. {#if feedbacks.length > 0}
  225. <div class=" flex flex-col justify-end w-full text-right gap-1">
  226. <div class="line-clamp-1 text-gray-500 text-xs">
  227. {$i18n.t('Help us create the best community leaderboard by sharing your feedback history!')}
  228. </div>
  229. <div class="flex space-x-1 ml-auto">
  230. <Tooltip
  231. content={$i18n.t(
  232. 'To protect your privacy, only ratings, model IDs, tags, and metadata are shared from your feedback—your chat logs remain private and are not included.'
  233. )}
  234. >
  235. <button
  236. class="flex text-xs items-center px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-200 transition"
  237. on:click={async () => {
  238. shareHandler();
  239. }}
  240. >
  241. <div class=" self-center mr-2 font-medium line-clamp-1">
  242. {$i18n.t('Share to Open WebUI Community')}
  243. </div>
  244. <div class=" self-center">
  245. <CloudArrowUp className="size-3" strokeWidth="3" />
  246. </div>
  247. </button>
  248. </Tooltip>
  249. </div>
  250. </div>
  251. {/if}
  252. {#if feedbacks.length > 10}
  253. <Pagination bind:page count={feedbacks.length} perPage={10} />
  254. {/if}