Pipelines.svelte 16 KB

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