Pipelines.svelte 17 KB

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