Thomas G. Lopes commited on
Commit
53e0cfa
·
1 Parent(s): b2170a7

create custom prompt api

Browse files
src/app.css CHANGED
@@ -47,6 +47,15 @@
47
  @apply -translate-x-1/2;
48
  }
49
 
 
 
 
 
 
 
 
 
 
50
  @utility btn {
51
  @apply flex h-[39px] items-center justify-center gap-2 rounded-lg border border-gray-200 bg-white px-3 py-2.5 text-sm font-medium text-gray-900 hover:bg-gray-100 hover:text-blue-700 focus:ring-4 focus:ring-gray-100 focus:outline-hidden dark:border-gray-600 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700;
52
  }
 
47
  @apply -translate-x-1/2;
48
  }
49
 
50
+ @utility abs-y-center {
51
+ top: 50%;
52
+ @apply -translate-y-1/2;
53
+ }
54
+
55
+ @utility abs-center {
56
+ @apply abs-x-center abs-y-center;
57
+ }
58
+
59
  @utility btn {
60
  @apply flex h-[39px] items-center justify-center gap-2 rounded-lg border border-gray-200 bg-white px-3 py-2.5 text-sm font-medium text-gray-900 hover:bg-gray-100 hover:text-blue-700 focus:ring-4 focus:ring-gray-100 focus:outline-hidden dark:border-gray-600 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700;
61
  }
src/lib/actions/click-outside.ts ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Action } from "svelte/action";
2
+
3
+ export const clickOutside: Action<HTMLElement, () => void> = (node, callback) => {
4
+ let _callback = callback;
5
+
6
+ function update(callback: () => void) {
7
+ _callback = callback;
8
+ }
9
+
10
+ function handleClick(event: MouseEvent) {
11
+ if (node && !node.contains(event.target as Node) && !event.defaultPrevented) {
12
+ _callback();
13
+ }
14
+ }
15
+
16
+ document.addEventListener("click", handleClick, true);
17
+
18
+ return {
19
+ update,
20
+ destroy() {
21
+ document.removeEventListener("click", handleClick, true);
22
+ },
23
+ };
24
+ };
src/lib/components/DebugMenu.svelte CHANGED
@@ -2,6 +2,8 @@
2
  import { dev } from "$app/environment";
3
  import { session } from "$lib/stores/session";
4
  import { createPopover } from "@melt-ui/svelte";
 
 
5
 
6
  let innerWidth: number;
7
  let innerHeight: number;
@@ -51,6 +53,23 @@
51
  >
52
  Log session to console
53
  </button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  </div>
55
  </div>
56
  </div>
 
2
  import { dev } from "$app/environment";
3
  import { session } from "$lib/stores/session";
4
  import { createPopover } from "@melt-ui/svelte";
5
+ import { prompt } from "./Prompts.svelte";
6
+ import { token } from "$lib/stores/token";
7
 
8
  let innerWidth: number;
9
  let innerHeight: number;
 
53
  >
54
  Log session to console
55
  </button>
56
+ <button
57
+ class="rounded-md bg-gray-200 px-3 py-1 text-sm hover:bg-gray-300 dark:bg-gray-700 dark:text-white dark:hover:bg-gray-600"
58
+ on:click={async () => {
59
+ console.log(await prompt("Test prompt"));
60
+ }}
61
+ >
62
+ Test prompt
63
+ </button>
64
+
65
+ <button
66
+ class="rounded-md bg-gray-200 px-3 py-1 text-sm hover:bg-gray-300 dark:bg-gray-700 dark:text-white dark:hover:bg-gray-600"
67
+ on:click={async () => {
68
+ $token.showModal = true;
69
+ }}
70
+ >
71
+ Show token modal
72
+ </button>
73
  </div>
74
  </div>
75
  </div>
src/lib/components/InferencePlayground/InferencePlaygroundHFTokenModal.svelte CHANGED
@@ -1,4 +1,5 @@
1
  <script lang="ts">
 
2
  import { createEventDispatcher, onDestroy, onMount } from "svelte";
3
 
4
  import IconCross from "~icons/carbon/close";
@@ -18,15 +19,6 @@
18
  }
19
  }
20
 
21
- function handleBackdropClick(event: MouseEvent) {
22
- if (window?.getSelection()?.toString()) {
23
- return;
24
- }
25
- if (event.target === backdropEl) {
26
- dispatch("close");
27
- }
28
- }
29
-
30
  onMount(() => {
31
  document.getElementById("app")?.setAttribute("inert", "true");
32
  modalEl.focus();
@@ -46,7 +38,6 @@
46
  aria-hidden="true"
47
  class="fixed inset-0 z-50 flex items-center justify-center overflow-hidden bg-black/85"
48
  bind:this={backdropEl}
49
- on:click|stopPropagation={handleBackdropClick}
50
  >
51
  <div
52
  role="dialog"
@@ -54,6 +45,7 @@
54
  class="relative max-h-full w-full max-w-xl p-4 outline-hidden"
55
  bind:this={modalEl}
56
  on:keydown={handleKeydown}
 
57
  >
58
  <form on:submit|preventDefault class="relative rounded-lg bg-white shadow-sm dark:bg-gray-900">
59
  <div class="flex items-center justify-between rounded-t border-b p-4 md:px-5 md:py-4 dark:border-gray-800">
 
1
  <script lang="ts">
2
+ import { clickOutside } from "$lib/actions/click-outside";
3
  import { createEventDispatcher, onDestroy, onMount } from "svelte";
4
 
5
  import IconCross from "~icons/carbon/close";
 
19
  }
20
  }
21
 
 
 
 
 
 
 
 
 
 
22
  onMount(() => {
23
  document.getElementById("app")?.setAttribute("inert", "true");
24
  modalEl.focus();
 
38
  aria-hidden="true"
39
  class="fixed inset-0 z-50 flex items-center justify-center overflow-hidden bg-black/85"
40
  bind:this={backdropEl}
 
41
  >
42
  <div
43
  role="dialog"
 
45
  class="relative max-h-full w-full max-w-xl p-4 outline-hidden"
46
  bind:this={modalEl}
47
  on:keydown={handleKeydown}
48
+ use:clickOutside={() => dispatch("close")}
49
  >
50
  <form on:submit|preventDefault class="relative rounded-lg bg-white shadow-sm dark:bg-gray-900">
51
  <div class="flex items-center justify-between rounded-t border-b p-4 md:px-5 md:py-4 dark:border-gray-800">
src/lib/components/InferencePlayground/InferencePlaygroundProjectSelect.svelte CHANGED
@@ -7,6 +7,7 @@
7
  import IconEdit from "~icons/carbon/edit";
8
  import IconSave from "~icons/carbon/save";
9
  import IconDelete from "~icons/carbon/trash-can";
 
10
 
11
  let classNames: string = "";
12
  export { classNames as class };
@@ -23,8 +24,8 @@
23
  $session.activeProjectId = p?.value;
24
  });
25
 
26
- function saveProject() {
27
- session.saveProject(prompt("Project name") || "Project #" + ($session.projects.length + 1));
28
  }
29
  </script>
30
 
@@ -68,9 +69,9 @@
68
  <div class="ml-auto flex items-center gap-1">
69
  <button
70
  class="grid place-items-center rounded-md p-1 text-xs hover:bg-gray-300 dark:hover:bg-gray-600"
71
- on:click={e => {
72
  e.stopPropagation();
73
- session.updateProject(id, { name: prompt("Project name", name) || name });
74
  }}
75
  >
76
  <IconEdit />
 
7
  import IconEdit from "~icons/carbon/edit";
8
  import IconSave from "~icons/carbon/save";
9
  import IconDelete from "~icons/carbon/trash-can";
10
+ import { prompt } from "../Prompts.svelte";
11
 
12
  let classNames: string = "";
13
  export { classNames as class };
 
24
  $session.activeProjectId = p?.value;
25
  });
26
 
27
+ async function saveProject() {
28
+ session.saveProject((await prompt("Project name")) || "Project #" + ($session.projects.length + 1));
29
  }
30
  </script>
31
 
 
69
  <div class="ml-auto flex items-center gap-1">
70
  <button
71
  class="grid place-items-center rounded-md p-1 text-xs hover:bg-gray-300 dark:hover:bg-gray-600"
72
+ on:click={async e => {
73
  e.stopPropagation();
74
+ session.updateProject(id, { name: (await prompt("Project name", name)) || name });
75
  }}
76
  >
77
  <IconEdit />
src/lib/components/Prompts.svelte ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts" context="module">
2
+ import { clickOutside } from "$lib/actions/click-outside";
3
+ import { writable } from "svelte/store";
4
+ import IconCross from "~icons/carbon/close";
5
+
6
+ type Prompt = {
7
+ label: string;
8
+ value?: string;
9
+ placeholder?: string;
10
+ callback: (value: string) => void;
11
+ };
12
+
13
+ const prompts = writable<Prompt[]>([]);
14
+
15
+ export function resolvePrompt() {
16
+ prompts.update(p => {
17
+ p[0]?.callback(p[0]?.value ?? "");
18
+ return p.slice(1);
19
+ });
20
+ }
21
+
22
+ export async function prompt(label: string, defaultVAlue?: string): Promise<string> {
23
+ return new Promise(res => {
24
+ prompts.update(p => [...p, { label, value: defaultVAlue, callback: res }]);
25
+ });
26
+ }
27
+ </script>
28
+
29
+ <script lang="ts">
30
+ $: current = $prompts?.[0];
31
+
32
+ let dialog: HTMLDialogElement | undefined;
33
+
34
+ $: if (current) {
35
+ dialog?.showModal();
36
+ } else {
37
+ dialog?.close();
38
+ }
39
+
40
+ function onSubmit(e: Event) {
41
+ e.preventDefault();
42
+ resolvePrompt();
43
+ }
44
+ </script>
45
+
46
+ <dialog bind:this={dialog} on:close={resolvePrompt}>
47
+ {#if current}
48
+ <div class="fixed inset-0 z-50 flex items-center justify-center overflow-hidden bg-black/85">
49
+ <form
50
+ on:submit={onSubmit}
51
+ class="relative w-xl rounded-lg bg-white shadow-sm dark:bg-gray-900"
52
+ use:clickOutside={resolvePrompt}
53
+ >
54
+ <div class="flex items-center justify-between rounded-t border-b p-4 md:px-5 md:py-4 dark:border-gray-800">
55
+ <h3 class="flex items-center gap-2.5 text-lg font-semibold text-gray-900 dark:text-white">Prompt</h3>
56
+ <button
57
+ type="button"
58
+ class="ms-auto inline-flex h-8 w-8 items-center justify-center rounded-lg bg-transparent text-sm text-gray-400 hover:bg-gray-200 hover:text-gray-900 dark:hover:bg-gray-600 dark:hover:text-white"
59
+ on:click={resolvePrompt}
60
+ >
61
+ <div class="text-xl">
62
+ <IconCross />
63
+ </div>
64
+ <span class="sr-only">Close modal</span>
65
+ </button>
66
+ </div>
67
+ <!-- Modal body -->
68
+ <div class="p-4 md:p-5">
69
+ <label class="flex flex-col gap-2 font-medium text-gray-900 dark:text-white">
70
+ <p>{current.label}</p>
71
+ <!-- svelte-ignore a11y-autofocus - this is fine in dialogs -->
72
+ <input
73
+ bind:value={current.value}
74
+ placeholder={current.placeholder}
75
+ autofocus
76
+ required
77
+ type="text"
78
+ class="block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-blue-500 dark:focus:ring-blue-500"
79
+ />
80
+ </label>
81
+ </div>
82
+
83
+ <!-- Modal footer -->
84
+ <div class="flex rounded-b border-t border-gray-200 p-4 md:p-5 dark:border-gray-800">
85
+ <button
86
+ type="submit"
87
+ class="ml-auto rounded-lg bg-black px-5 py-2.5 text-sm font-medium text-white hover:bg-gray-900 focus:ring-4 focus:ring-gray-300 focus:outline-hidden dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700 dark:focus:ring-gray-700"
88
+ >Submit</button
89
+ >
90
+ </div>
91
+ </form>
92
+ </div>
93
+ {/if}
94
+ </dialog>
src/routes/+layout.svelte CHANGED
@@ -1,7 +1,9 @@
1
  <script lang="ts">
2
  import "../app.css";
3
  import DebugMenu from "$lib/components/DebugMenu.svelte";
 
4
  </script>
5
 
6
  <slot />
7
  <DebugMenu />
 
 
1
  <script lang="ts">
2
  import "../app.css";
3
  import DebugMenu from "$lib/components/DebugMenu.svelte";
4
+ import Prompts from "$lib/components/Prompts.svelte";
5
  </script>
6
 
7
  <slot />
8
  <DebugMenu />
9
+ <Prompts />