ModelItem.svelte 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <script lang="ts">
  2. import { marked } from 'marked';
  3. import { getContext, tick } from 'svelte';
  4. import dayjs from '$lib/dayjs';
  5. import { mobile, settings, user } from '$lib/stores';
  6. import { WEBUI_BASE_URL } from '$lib/constants';
  7. import Tooltip from '$lib/components/common/Tooltip.svelte';
  8. import { copyToClipboard, sanitizeResponseContent } from '$lib/utils';
  9. import ArrowUpTray from '$lib/components/icons/ArrowUpTray.svelte';
  10. import Check from '$lib/components/icons/Check.svelte';
  11. import ModelItemMenu from './ModelItemMenu.svelte';
  12. import EllipsisHorizontal from '$lib/components/icons/EllipsisHorizontal.svelte';
  13. import { toast } from 'svelte-sonner';
  14. import Tag from '$lib/components/icons/Tag.svelte';
  15. import Label from '$lib/components/icons/Label.svelte';
  16. const i18n = getContext('i18n');
  17. export let selectedModelIdx: number = -1;
  18. export let item: any = {};
  19. export let index: number = -1;
  20. export let value: string = '';
  21. export let unloadModelHandler: (modelValue: string) => void = () => {};
  22. export let pinModelHandler: (modelId: string) => void = () => {};
  23. export let onClick: () => void = () => {};
  24. const copyLinkHandler = async (model) => {
  25. const baseUrl = window.location.origin;
  26. const res = await copyToClipboard(`${baseUrl}/?model=${encodeURIComponent(model.id)}`);
  27. if (res) {
  28. toast.success($i18n.t('Copied link to clipboard'));
  29. } else {
  30. toast.error($i18n.t('Failed to copy link'));
  31. }
  32. };
  33. let showMenu = false;
  34. </script>
  35. <button
  36. aria-roledescription="model-item"
  37. aria-label={item.label}
  38. class="flex group/item w-full text-left font-medium line-clamp-1 select-none items-center rounded-button py-2 pl-3 pr-1.5 text-sm text-gray-700 dark:text-gray-100 outline-hidden transition-all duration-75 hover:bg-gray-100 dark:hover:bg-gray-800 rounded-xl cursor-pointer data-highlighted:bg-muted {index ===
  39. selectedModelIdx
  40. ? 'bg-gray-100 dark:bg-gray-800 group-hover:bg-transparent'
  41. : ''}"
  42. data-arrow-selected={index === selectedModelIdx}
  43. data-value={item.value}
  44. on:click={() => {
  45. onClick();
  46. }}
  47. >
  48. <div class="flex flex-col flex-1 gap-1.5">
  49. <!-- {#if (item?.model?.tags ?? []).length > 0}
  50. <div
  51. class="flex gap-0.5 self-center items-start h-full w-full translate-y-[0.5px] overflow-x-auto scrollbar-none"
  52. >
  53. {#each item.model?.tags.sort((a, b) => a.name.localeCompare(b.name)) as tag}
  54. <Tooltip content={tag.name} className="flex-shrink-0">
  55. <div
  56. class=" text-xs font-semibold px-1 rounded-sm uppercase bg-gray-500/20 text-gray-700 dark:text-gray-200"
  57. >
  58. {tag.name}
  59. </div>
  60. </Tooltip>
  61. {/each}
  62. </div>
  63. {/if} -->
  64. <div class="flex items-center gap-2">
  65. <div class="flex items-center min-w-fit">
  66. <Tooltip content={$user?.role === 'admin' ? (item?.value ?? '') : ''} placement="top-start">
  67. <img
  68. src={item.model?.info?.meta?.profile_image_url ??
  69. `${WEBUI_BASE_URL}/static/favicon.png`}
  70. alt="Model"
  71. class="rounded-full size-5 flex items-center"
  72. />
  73. </Tooltip>
  74. </div>
  75. <div class="flex items-center">
  76. <Tooltip content={`${item.label} (${item.value})`} placement="top-start">
  77. <div class="line-clamp-1">
  78. {item.label}
  79. </div>
  80. </Tooltip>
  81. </div>
  82. <div class=" shrink-0 flex items-center gap-2">
  83. {#if item.model.owned_by === 'ollama'}
  84. {#if (item.model.ollama?.details?.parameter_size ?? '') !== ''}
  85. <div class="flex items-center translate-y-[0.5px]">
  86. <Tooltip
  87. content={`${
  88. item.model.ollama?.details?.quantization_level
  89. ? item.model.ollama?.details?.quantization_level + ' '
  90. : ''
  91. }${
  92. item.model.ollama?.size
  93. ? `(${(item.model.ollama?.size / 1024 ** 3).toFixed(1)}GB)`
  94. : ''
  95. }`}
  96. className="self-end"
  97. >
  98. <span class=" text-xs font-medium text-gray-600 dark:text-gray-400 line-clamp-1"
  99. >{item.model.ollama?.details?.parameter_size ?? ''}</span
  100. >
  101. </Tooltip>
  102. </div>
  103. {/if}
  104. {#if item.model.ollama?.expires_at && new Date(item.model.ollama?.expires_at * 1000) > new Date()}
  105. <div class="flex items-center translate-y-[0.5px] px-0.5">
  106. <Tooltip
  107. content={`${$i18n.t('Unloads {{FROM_NOW}}', {
  108. FROM_NOW: dayjs(item.model.ollama?.expires_at * 1000).fromNow()
  109. })}`}
  110. className="self-end"
  111. >
  112. <div class=" flex items-center">
  113. <span class="relative flex size-2">
  114. <span
  115. class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"
  116. />
  117. <span class="relative inline-flex rounded-full size-2 bg-green-500" />
  118. </span>
  119. </div>
  120. </Tooltip>
  121. </div>
  122. {/if}
  123. {/if}
  124. <!-- {JSON.stringify(item.info)} -->
  125. {#if (item?.model?.tags ?? []).length > 0}
  126. {#key item.model.id}
  127. <Tooltip elementId="tags-{item.model.id}">
  128. <div slot="tooltip" id="tags-{item.model.id}">
  129. {#each item.model?.tags.sort((a, b) => a.name.localeCompare(b.name)) as tag}
  130. <Tooltip content={tag.name} className="flex-shrink-0">
  131. <div class=" text-xs font-medium rounded-sm uppercase text-white">
  132. {tag.name}
  133. </div>
  134. </Tooltip>
  135. {/each}
  136. </div>
  137. <div class="translate-y-[1px]">
  138. <Tag />
  139. </div>
  140. </Tooltip>
  141. {/key}
  142. {/if}
  143. {#if item.model?.direct}
  144. <Tooltip content={`${$i18n.t('Direct')}`}>
  145. <div class="translate-y-[1px]">
  146. <svg
  147. xmlns="http://www.w3.org/2000/svg"
  148. viewBox="0 0 16 16"
  149. fill="currentColor"
  150. class="size-3"
  151. >
  152. <path
  153. fill-rule="evenodd"
  154. d="M2 2.75A.75.75 0 0 1 2.75 2C8.963 2 14 7.037 14 13.25a.75.75 0 0 1-1.5 0c0-5.385-4.365-9.75-9.75-9.75A.75.75 0 0 1 2 2.75Zm0 4.5a.75.75 0 0 1 .75-.75 6.75 6.75 0 0 1 6.75 6.75.75.75 0 0 1-1.5 0C8 10.35 5.65 8 2.75 8A.75.75 0 0 1 2 7.25ZM3.5 11a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3Z"
  155. clip-rule="evenodd"
  156. />
  157. </svg>
  158. </div>
  159. </Tooltip>
  160. {:else if item.model.connection_type === 'external'}
  161. <Tooltip content={`${$i18n.t('External')}`}>
  162. <div class="translate-y-[1px]">
  163. <svg
  164. xmlns="http://www.w3.org/2000/svg"
  165. viewBox="0 0 16 16"
  166. fill="currentColor"
  167. class="size-3"
  168. >
  169. <path
  170. fill-rule="evenodd"
  171. d="M8.914 6.025a.75.75 0 0 1 1.06 0 3.5 3.5 0 0 1 0 4.95l-2 2a3.5 3.5 0 0 1-5.396-4.402.75.75 0 0 1 1.251.827 2 2 0 0 0 3.085 2.514l2-2a2 2 0 0 0 0-2.828.75.75 0 0 1 0-1.06Z"
  172. clip-rule="evenodd"
  173. />
  174. <path
  175. fill-rule="evenodd"
  176. d="M7.086 9.975a.75.75 0 0 1-1.06 0 3.5 3.5 0 0 1 0-4.95l2-2a3.5 3.5 0 0 1 5.396 4.402.75.75 0 0 1-1.251-.827 2 2 0 0 0-3.085-2.514l-2 2a2 2 0 0 0 0 2.828.75.75 0 0 1 0 1.06Z"
  177. clip-rule="evenodd"
  178. />
  179. </svg>
  180. </div>
  181. </Tooltip>
  182. {/if}
  183. {#if item.model?.info?.meta?.description}
  184. <Tooltip
  185. content={`${marked.parse(
  186. sanitizeResponseContent(item.model?.info?.meta?.description).replaceAll('\n', '<br>')
  187. )}`}
  188. >
  189. <div class=" translate-y-[1px]">
  190. <svg
  191. xmlns="http://www.w3.org/2000/svg"
  192. fill="none"
  193. viewBox="0 0 24 24"
  194. stroke-width="1.5"
  195. stroke="currentColor"
  196. class="w-4 h-4"
  197. >
  198. <path
  199. stroke-linecap="round"
  200. stroke-linejoin="round"
  201. d="m11.25 11.25.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9-3.75h.008v.008H12V8.25Z"
  202. />
  203. </svg>
  204. </div>
  205. </Tooltip>
  206. {/if}
  207. </div>
  208. </div>
  209. </div>
  210. <div class="ml-auto pl-2 pr-1 flex items-center gap-1.5 shrink-0">
  211. {#if $user?.role === 'admin' && item.model.owned_by === 'ollama' && item.model.ollama?.expires_at && new Date(item.model.ollama?.expires_at * 1000) > new Date()}
  212. <Tooltip
  213. content={`${$i18n.t('Eject')}`}
  214. className="flex-shrink-0 group-hover/item:opacity-100 opacity-0 "
  215. >
  216. <button
  217. class="flex"
  218. on:click={(e) => {
  219. e.preventDefault();
  220. e.stopPropagation();
  221. unloadModelHandler(item.value);
  222. }}
  223. >
  224. <ArrowUpTray className="size-3" />
  225. </button>
  226. </Tooltip>
  227. {/if}
  228. <ModelItemMenu
  229. bind:show={showMenu}
  230. model={item.model}
  231. {pinModelHandler}
  232. copyLinkHandler={() => {
  233. copyLinkHandler(item.model);
  234. }}
  235. >
  236. <button
  237. aria-label={`${$i18n.t('More Options')}`}
  238. class="flex"
  239. on:click={(e) => {
  240. e.preventDefault();
  241. e.stopPropagation();
  242. showMenu = !showMenu;
  243. }}
  244. >
  245. <EllipsisHorizontal />
  246. </button>
  247. </ModelItemMenu>
  248. {#if value === item.value}
  249. <div>
  250. <Check className="size-3" />
  251. </div>
  252. {/if}
  253. </div>
  254. </button>