Pipelines.svelte 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <script lang="ts">
  2. import { v4 as uuidv4 } from 'uuid';
  3. import { toast } from 'svelte-sonner';
  4. import { models } from '$lib/stores';
  5. import { getContext, onMount, tick } from 'svelte';
  6. import type { Writable } from 'svelte/store';
  7. import type { i18n as i18nType } from 'i18next';
  8. import {
  9. getPipelineValves,
  10. getPipelineValvesSpec,
  11. updatePipelineValves,
  12. getPipelines,
  13. getModels,
  14. getPipelinesList,
  15. downloadPipeline,
  16. deletePipeline,
  17. uploadPipeline
  18. } from '$lib/apis';
  19. import Spinner from '$lib/components/common/Spinner.svelte';
  20. const i18n: Writable<i18nType> = getContext('i18n');
  21. export let saveHandler: Function;
  22. let downloading = false;
  23. let uploading = false;
  24. let pipelineFiles;
  25. let PIPELINES_LIST = null;
  26. let selectedPipelinesUrlIdx = '';
  27. let pipelines = null;
  28. let valves = null;
  29. let valves_spec = null;
  30. let selectedPipelineIdx = null;
  31. let pipelineDownloadUrl = '';
  32. const updateHandler = async () => {
  33. const pipeline = pipelines[selectedPipelineIdx];
  34. if (pipeline && (pipeline?.valves ?? false)) {
  35. for (const property in valves_spec.properties) {
  36. if (valves_spec.properties[property]?.type === 'array') {
  37. valves[property] = valves[property].split(',').map((v) => v.trim());
  38. }
  39. }
  40. const res = await updatePipelineValves(
  41. localStorage.token,
  42. pipeline.id,
  43. valves,
  44. selectedPipelinesUrlIdx
  45. ).catch((error) => {
  46. toast.error(error);
  47. });
  48. if (res) {
  49. toast.success('Valves updated successfully');
  50. setPipelines();
  51. models.set(await getModels(localStorage.token));
  52. saveHandler();
  53. }
  54. } else {
  55. toast.error('No valves to update');
  56. }
  57. };
  58. const getValves = async (idx) => {
  59. valves = null;
  60. valves_spec = null;
  61. valves_spec = await getPipelineValvesSpec(
  62. localStorage.token,
  63. pipelines[idx].id,
  64. selectedPipelinesUrlIdx
  65. );
  66. valves = await getPipelineValves(
  67. localStorage.token,
  68. pipelines[idx].id,
  69. selectedPipelinesUrlIdx
  70. );
  71. for (const property in valves_spec.properties) {
  72. if (valves_spec.properties[property]?.type === 'array') {
  73. valves[property] = valves[property].join(',');
  74. }
  75. }
  76. };
  77. const setPipelines = async () => {
  78. pipelines = null;
  79. valves = null;
  80. valves_spec = null;
  81. if (PIPELINES_LIST.length > 0) {
  82. console.log(selectedPipelinesUrlIdx);
  83. pipelines = await getPipelines(localStorage.token, selectedPipelinesUrlIdx);
  84. if (pipelines.length > 0) {
  85. selectedPipelineIdx = 0;
  86. await getValves(selectedPipelineIdx);
  87. }
  88. } else {
  89. pipelines = [];
  90. }
  91. };
  92. const addPipelineHandler = async () => {
  93. downloading = true;
  94. const res = await downloadPipeline(
  95. localStorage.token,
  96. pipelineDownloadUrl,
  97. selectedPipelinesUrlIdx
  98. ).catch((error) => {
  99. toast.error(error);
  100. return null;
  101. });
  102. if (res) {
  103. toast.success('Pipeline downloaded successfully');
  104. setPipelines();
  105. models.set(await getModels(localStorage.token));
  106. }
  107. downloading = false;
  108. };
  109. const uploadPipelineHandler = async () => {
  110. uploading = true;
  111. if (pipelineFiles && pipelineFiles.length !== 0) {
  112. const file = pipelineFiles[0];
  113. console.log(file);
  114. const res = await uploadPipeline(localStorage.token, file, selectedPipelinesUrlIdx).catch(
  115. (error) => {
  116. console.log(error);
  117. toast.error('Something went wrong :/');
  118. return null;
  119. }
  120. );
  121. if (res) {
  122. toast.success('Pipeline downloaded successfully');
  123. setPipelines();
  124. models.set(await getModels(localStorage.token));
  125. }
  126. } else {
  127. toast.error('No file selected');
  128. }
  129. pipelineFiles = null;
  130. const pipelineUploadInputElement = document.getElementById('pipeline-upload-input');
  131. if (pipelineUploadInputElement) {
  132. pipelineUploadInputElement.value = null;
  133. }
  134. uploading = false;
  135. };
  136. const deletePipelineHandler = async () => {
  137. const res = await deletePipeline(
  138. localStorage.token,
  139. pipelines[selectedPipelineIdx].id,
  140. selectedPipelinesUrlIdx
  141. ).catch((error) => {
  142. toast.error(error);
  143. return null;
  144. });
  145. if (res) {
  146. toast.success('Pipeline deleted successfully');
  147. setPipelines();
  148. models.set(await getModels(localStorage.token));
  149. }
  150. };
  151. onMount(async () => {
  152. PIPELINES_LIST = await getPipelinesList(localStorage.token);
  153. console.log(PIPELINES_LIST);
  154. if (PIPELINES_LIST.length > 0) {
  155. selectedPipelinesUrlIdx = PIPELINES_LIST[0]['idx'].toString();
  156. }
  157. await setPipelines();
  158. });
  159. </script>
  160. <form
  161. class="flex flex-col h-full justify-between space-y-3 text-sm"
  162. on:submit|preventDefault={async () => {
  163. updateHandler();
  164. }}
  165. >
  166. <div class=" pr-1.5 overflow-y-scroll scrollbar-hidden h-full">
  167. {#if PIPELINES_LIST !== null}
  168. <div class="flex w-full justify-between mb-2">
  169. <div class=" self-center text-sm font-semibold">
  170. {$i18n.t('Manage Pipelines')}
  171. </div>
  172. </div>
  173. {#if PIPELINES_LIST.length > 0}
  174. <div class="space-y-1">
  175. <div class="flex gap-2">
  176. <div class="flex-1">
  177. <select
  178. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  179. bind:value={selectedPipelinesUrlIdx}
  180. placeholder={$i18n.t('Select a pipeline url')}
  181. on:change={async () => {
  182. await tick();
  183. await setPipelines();
  184. }}
  185. >
  186. <option value="" selected disabled class="bg-gray-100 dark:bg-gray-700"
  187. >{$i18n.t('Select a pipeline url')}</option
  188. >
  189. {#each PIPELINES_LIST as pipelines, idx}
  190. <option value={pipelines.idx.toString()} class="bg-gray-100 dark:bg-gray-700"
  191. >{pipelines.url}</option
  192. >
  193. {/each}
  194. </select>
  195. </div>
  196. </div>
  197. </div>
  198. <div class=" my-2">
  199. <div class=" mb-2 text-sm font-medium">
  200. {$i18n.t('Upload Pipeline')}
  201. </div>
  202. <div class="flex w-full">
  203. <div class="flex-1 mr-2">
  204. <input
  205. id="pipelines-upload-input"
  206. bind:files={pipelineFiles}
  207. type="file"
  208. accept=".py"
  209. hidden
  210. />
  211. <button
  212. class="w-full text-sm font-medium py-2 bg-transparent hover:bg-gray-100 border border-dashed dark:border-gray-800 dark:hover:bg-gray-850 text-center rounded-xl"
  213. type="button"
  214. on:click={() => {
  215. document.getElementById('pipelines-upload-input')?.click();
  216. }}
  217. >
  218. {#if pipelineFiles}
  219. {pipelineFiles.length > 0 ? `${pipelineFiles.length}` : ''} pipeline(s) selected.
  220. {:else}
  221. {$i18n.t('Click here to select a py file.')}
  222. {/if}
  223. </button>
  224. </div>
  225. <button
  226. class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
  227. on:click={() => {
  228. uploadPipelineHandler();
  229. }}
  230. disabled={uploading}
  231. type="button"
  232. >
  233. {#if uploading}
  234. <div class="self-center">
  235. <svg
  236. class=" w-4 h-4"
  237. viewBox="0 0 24 24"
  238. fill="currentColor"
  239. xmlns="http://www.w3.org/2000/svg"
  240. >
  241. <style>
  242. .spinner_ajPY {
  243. transform-origin: center;
  244. animation: spinner_AtaB 0.75s infinite linear;
  245. }
  246. @keyframes spinner_AtaB {
  247. 100% {
  248. transform: rotate(360deg);
  249. }
  250. }
  251. </style>
  252. <path
  253. 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"
  254. opacity=".25"
  255. />
  256. <path
  257. 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"
  258. class="spinner_ajPY"
  259. />
  260. </svg>
  261. </div>
  262. {:else}
  263. <svg
  264. xmlns="http://www.w3.org/2000/svg"
  265. viewBox="0 0 16 16"
  266. fill="currentColor"
  267. class="size-4"
  268. >
  269. <path
  270. d="M7.25 10.25a.75.75 0 0 0 1.5 0V4.56l2.22 2.22a.75.75 0 1 0 1.06-1.06l-3.5-3.5a.75.75 0 0 0-1.06 0l-3.5 3.5a.75.75 0 0 0 1.06 1.06l2.22-2.22v5.69Z"
  271. />
  272. <path
  273. d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
  274. />
  275. </svg>
  276. {/if}
  277. </button>
  278. </div>
  279. </div>
  280. <div class=" my-2">
  281. <div class=" mb-2 text-sm font-medium">
  282. {$i18n.t('Install from Github URL')}
  283. </div>
  284. <div class="flex w-full">
  285. <div class="flex-1 mr-2">
  286. <input
  287. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  288. placeholder={$i18n.t('Enter Github Raw URL')}
  289. bind:value={pipelineDownloadUrl}
  290. />
  291. </div>
  292. <button
  293. class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
  294. on:click={() => {
  295. addPipelineHandler();
  296. }}
  297. disabled={downloading}
  298. type="button"
  299. >
  300. {#if downloading}
  301. <div class="self-center">
  302. <svg
  303. class=" w-4 h-4"
  304. viewBox="0 0 24 24"
  305. fill="currentColor"
  306. xmlns="http://www.w3.org/2000/svg"
  307. >
  308. <style>
  309. .spinner_ajPY {
  310. transform-origin: center;
  311. animation: spinner_AtaB 0.75s infinite linear;
  312. }
  313. @keyframes spinner_AtaB {
  314. 100% {
  315. transform: rotate(360deg);
  316. }
  317. }
  318. </style>
  319. <path
  320. 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"
  321. opacity=".25"
  322. />
  323. <path
  324. 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"
  325. class="spinner_ajPY"
  326. />
  327. </svg>
  328. </div>
  329. {:else}
  330. <svg
  331. xmlns="http://www.w3.org/2000/svg"
  332. viewBox="0 0 16 16"
  333. fill="currentColor"
  334. class="w-4 h-4"
  335. >
  336. <path
  337. d="M8.75 2.75a.75.75 0 0 0-1.5 0v5.69L5.03 6.22a.75.75 0 0 0-1.06 1.06l3.5 3.5a.75.75 0 0 0 1.06 0l3.5-3.5a.75.75 0 0 0-1.06-1.06L8.75 8.44V2.75Z"
  338. />
  339. <path
  340. d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
  341. />
  342. </svg>
  343. {/if}
  344. </button>
  345. </div>
  346. <div class="mt-2 text-xs text-gray-500">
  347. <span class=" font-semibold dark:text-gray-200">Warning:</span> Pipelines are a plugin
  348. system with arbitrary code execution —
  349. <span class=" font-medium dark:text-gray-400"
  350. >don't fetch random pipelines from sources you don't trust.</span
  351. >
  352. </div>
  353. </div>
  354. <hr class=" dark:border-gray-800 my-3 w-full" />
  355. {#if pipelines !== null}
  356. {#if pipelines.length > 0}
  357. <div class="flex w-full justify-between mb-2">
  358. <div class=" self-center text-sm font-semibold">
  359. {$i18n.t('Pipelines Valves')}
  360. </div>
  361. </div>
  362. <div class="space-y-1">
  363. {#if pipelines.length > 0}
  364. <div class="flex gap-2">
  365. <div class="flex-1">
  366. <select
  367. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  368. bind:value={selectedPipelineIdx}
  369. placeholder={$i18n.t('Select a pipeline')}
  370. on:change={async () => {
  371. await tick();
  372. await getValves(selectedPipelineIdx);
  373. }}
  374. >
  375. {#each pipelines as pipeline, idx}
  376. <option value={idx} class="bg-gray-100 dark:bg-gray-700"
  377. >{pipeline.name} ({pipeline.type ?? 'pipe'})</option
  378. >
  379. {/each}
  380. </select>
  381. </div>
  382. <button
  383. class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
  384. on:click={() => {
  385. deletePipelineHandler();
  386. }}
  387. type="button"
  388. >
  389. <svg
  390. xmlns="http://www.w3.org/2000/svg"
  391. viewBox="0 0 16 16"
  392. fill="currentColor"
  393. class="w-4 h-4"
  394. >
  395. <path
  396. fill-rule="evenodd"
  397. d="M5 3.25V4H2.75a.75.75 0 0 0 0 1.5h.3l.815 8.15A1.5 1.5 0 0 0 5.357 15h5.285a1.5 1.5 0 0 0 1.493-1.35l.815-8.15h.3a.75.75 0 0 0 0-1.5H11v-.75A2.25 2.25 0 0 0 8.75 1h-1.5A2.25 2.25 0 0 0 5 3.25Zm2.25-.75a.75.75 0 0 0-.75.75V4h3v-.75a.75.75 0 0 0-.75-.75h-1.5ZM6.05 6a.75.75 0 0 1 .787.713l.275 5.5a.75.75 0 0 1-1.498.075l-.275-5.5A.75.75 0 0 1 6.05 6Zm3.9 0a.75.75 0 0 1 .712.787l-.275 5.5a.75.75 0 0 1-1.498-.075l.275-5.5a.75.75 0 0 1 .786-.711Z"
  398. clip-rule="evenodd"
  399. />
  400. </svg>
  401. </button>
  402. </div>
  403. {/if}
  404. <div class="space-y-1">
  405. {#if pipelines[selectedPipelineIdx].valves}
  406. {#if valves}
  407. {#each Object.keys(valves_spec.properties) as property, idx}
  408. <div class=" py-0.5 w-full justify-between">
  409. <div class="flex w-full justify-between">
  410. <div class=" self-center text-xs font-medium">
  411. {valves_spec.properties[property].title}
  412. </div>
  413. <button
  414. class="p-1 px-3 text-xs flex rounded transition"
  415. type="button"
  416. on:click={() => {
  417. valves[property] = (valves[property] ?? null) === null ? '' : null;
  418. }}
  419. >
  420. {#if (valves[property] ?? null) === null}
  421. <span class="ml-2 self-center"> {$i18n.t('None')} </span>
  422. {:else}
  423. <span class="ml-2 self-center"> {$i18n.t('Custom')} </span>
  424. {/if}
  425. </button>
  426. </div>
  427. {#if (valves[property] ?? null) !== null}
  428. <div class="flex mt-0.5 space-x-2">
  429. <div class=" flex-1">
  430. <input
  431. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  432. type="text"
  433. placeholder={valves_spec.properties[property].title}
  434. bind:value={valves[property]}
  435. autocomplete="off"
  436. />
  437. </div>
  438. </div>
  439. {/if}
  440. </div>
  441. {/each}
  442. {:else}
  443. <Spinner className="size-5" />
  444. {/if}
  445. {:else}
  446. <div>No valves</div>
  447. {/if}
  448. </div>
  449. </div>
  450. {:else if pipelines.length === 0}
  451. <div>Pipelines Not Detected</div>
  452. {/if}
  453. {:else}
  454. <div class="flex justify-center">
  455. <div class="my-auto">
  456. <Spinner className="size-4" />
  457. </div>
  458. </div>
  459. {/if}
  460. {:else}
  461. <div>Pipelines Not Detected</div>
  462. {/if}
  463. {:else}
  464. <div class="flex justify-center h-full">
  465. <div class="my-auto">
  466. <Spinner className="size-6" />
  467. </div>
  468. </div>
  469. {/if}
  470. </div>
  471. <div class="flex justify-end pt-3 text-sm font-medium">
  472. <button
  473. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  474. type="submit"
  475. >
  476. Save
  477. </button>
  478. </div>
  479. </form>