Thomas G. Lopes
commited on
Commit
·
0bd4051
1
Parent(s):
6376043
fix model picker bug
Browse files- package.json +1 -1
- src/lib/state/models.svelte.ts +18 -27
- src/lib/state/session.svelte.ts +4 -5
- src/lib/utils/array.ts +5 -0
- src/routes/+layout.svelte +2 -0
package.json
CHANGED
@@ -38,7 +38,7 @@
|
|
38 |
"globals": "^16.0.0",
|
39 |
"highlight.js": "^11.10.0",
|
40 |
"jiti": "^2.4.2",
|
41 |
-
"melt": "^0.29.
|
42 |
"openai": "^4.90.0",
|
43 |
"postcss": "^8.4.38",
|
44 |
"prettier": "^3.1.1",
|
|
|
38 |
"globals": "^16.0.0",
|
39 |
"highlight.js": "^11.10.0",
|
40 |
"jiti": "^2.4.2",
|
41 |
+
"melt": "^0.29.3",
|
42 |
"openai": "^4.90.0",
|
43 |
"postcss": "^8.4.38",
|
44 |
"prettier": "^3.1.1",
|
src/lib/state/models.svelte.ts
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
import { page } from "$app/state";
|
2 |
-
import { createInit } from "$lib/spells/create-init.svelte";
|
3 |
import type { CustomModel, Model } from "$lib/types.js";
|
|
|
4 |
import { safeParse } from "$lib/utils/json.js";
|
5 |
import typia from "typia";
|
6 |
import { session } from "./session.svelte";
|
7 |
-
import { randomPick } from "$lib/utils/array.js";
|
8 |
|
9 |
const LOCAL_STORAGE_KEY = "hf_inference_playground_custom_models";
|
10 |
|
@@ -12,11 +11,12 @@ class Models {
|
|
12 |
remote = $derived(page.data.models as Model[]);
|
13 |
trending = $derived(this.remote.toSorted((a, b) => b.trendingScore - a.trendingScore).slice(0, 5));
|
14 |
nonTrending = $derived(this.remote.filter(m => !this.trending.includes(m)));
|
|
|
15 |
|
16 |
-
|
17 |
-
#initCustom = createInit(() => {
|
18 |
const savedData = localStorage.getItem(LOCAL_STORAGE_KEY);
|
19 |
if (!savedData) return;
|
|
|
20 |
const parsed = safeParse(savedData);
|
21 |
const res = typia.validate<CustomModel[]>(parsed);
|
22 |
if (res.success) {
|
@@ -24,36 +24,27 @@ class Models {
|
|
24 |
} else {
|
25 |
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify([]));
|
26 |
}
|
27 |
-
});
|
28 |
-
|
29 |
-
all = $derived([...this.remote, ...this.custom]);
|
30 |
-
|
31 |
-
constructor() {
|
32 |
-
$effect.root(() => {
|
33 |
-
$effect(() => {
|
34 |
-
if (!this.#initCustom.called) return;
|
35 |
-
const v = $state.snapshot(this.#custom);
|
36 |
-
try {
|
37 |
-
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(v));
|
38 |
-
} catch (e) {
|
39 |
-
console.error("Failed to save session to localStorage:", e);
|
40 |
-
}
|
41 |
-
});
|
42 |
-
});
|
43 |
}
|
44 |
|
|
|
|
|
45 |
get custom() {
|
46 |
-
this.#initCustom.fn();
|
47 |
return this.#custom;
|
48 |
}
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
addCustom(model: CustomModel) {
|
55 |
if (this.#custom.find(m => m.id === model.id)) return null;
|
56 |
-
this
|
57 |
return model;
|
58 |
}
|
59 |
|
@@ -62,12 +53,12 @@ class Models {
|
|
62 |
if (index === -1) {
|
63 |
this.addCustom(model);
|
64 |
} else {
|
65 |
-
this
|
66 |
}
|
67 |
}
|
68 |
|
69 |
removeCustom(uuid: CustomModel["_id"]) {
|
70 |
-
this
|
71 |
session.project.conversations.forEach((c, i) => {
|
72 |
if (c.model._id !== uuid) return;
|
73 |
session.project.conversations[i]!.model = randomPick(models.trending)!;
|
|
|
1 |
import { page } from "$app/state";
|
|
|
2 |
import type { CustomModel, Model } from "$lib/types.js";
|
3 |
+
import { edit, randomPick } from "$lib/utils/array.js";
|
4 |
import { safeParse } from "$lib/utils/json.js";
|
5 |
import typia from "typia";
|
6 |
import { session } from "./session.svelte";
|
|
|
7 |
|
8 |
const LOCAL_STORAGE_KEY = "hf_inference_playground_custom_models";
|
9 |
|
|
|
11 |
remote = $derived(page.data.models as Model[]);
|
12 |
trending = $derived(this.remote.toSorted((a, b) => b.trendingScore - a.trendingScore).slice(0, 5));
|
13 |
nonTrending = $derived(this.remote.filter(m => !this.trending.includes(m)));
|
14 |
+
all = $derived([...this.remote, ...this.custom]);
|
15 |
|
16 |
+
constructor() {
|
|
|
17 |
const savedData = localStorage.getItem(LOCAL_STORAGE_KEY);
|
18 |
if (!savedData) return;
|
19 |
+
|
20 |
const parsed = safeParse(savedData);
|
21 |
const res = typia.validate<CustomModel[]>(parsed);
|
22 |
if (res.success) {
|
|
|
24 |
} else {
|
25 |
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify([]));
|
26 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
}
|
28 |
|
29 |
+
#custom = $state.raw<CustomModel[]>([]);
|
30 |
+
|
31 |
get custom() {
|
|
|
32 |
return this.#custom;
|
33 |
}
|
34 |
|
35 |
+
set custom(models: CustomModel[]) {
|
36 |
+
this.#custom = models;
|
37 |
+
|
38 |
+
try {
|
39 |
+
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(models));
|
40 |
+
} catch (e) {
|
41 |
+
console.error("Failed to save session to localStorage:", e);
|
42 |
+
}
|
43 |
+
}
|
44 |
|
45 |
addCustom(model: CustomModel) {
|
46 |
if (this.#custom.find(m => m.id === model.id)) return null;
|
47 |
+
this.custom = [...this.custom, model];
|
48 |
return model;
|
49 |
}
|
50 |
|
|
|
53 |
if (index === -1) {
|
54 |
this.addCustom(model);
|
55 |
} else {
|
56 |
+
this.custom = edit(this.custom, index, model);
|
57 |
}
|
58 |
}
|
59 |
|
60 |
removeCustom(uuid: CustomModel["_id"]) {
|
61 |
+
this.custom = this.custom.filter(m => m._id !== uuid);
|
62 |
session.project.conversations.forEach((c, i) => {
|
63 |
if (c.model._id !== uuid) return;
|
64 |
session.project.conversations[i]!.model = randomPick(models.trending)!;
|
src/lib/state/session.svelte.ts
CHANGED
@@ -58,9 +58,9 @@ function getDefaults() {
|
|
58 |
|
59 |
class SessionState {
|
60 |
#value = $state<Session>({} as Session);
|
61 |
-
|
62 |
-
//
|
63 |
-
|
64 |
const { defaultConversation, defaultProject } = getDefaults();
|
65 |
|
66 |
// Get saved session from localStorage if available
|
@@ -116,7 +116,7 @@ class SessionState {
|
|
116 |
constructor() {
|
117 |
$effect.root(() => {
|
118 |
$effect(() => {
|
119 |
-
if (!this
|
120 |
const v = $state.snapshot(this.#value);
|
121 |
try {
|
122 |
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(v));
|
@@ -128,7 +128,6 @@ class SessionState {
|
|
128 |
}
|
129 |
|
130 |
get $() {
|
131 |
-
this.#init.fn();
|
132 |
return this.#value;
|
133 |
}
|
134 |
|
|
|
58 |
|
59 |
class SessionState {
|
60 |
#value = $state<Session>({} as Session);
|
61 |
+
|
62 |
+
// Call once in layout
|
63 |
+
init = createInit(() => {
|
64 |
const { defaultConversation, defaultProject } = getDefaults();
|
65 |
|
66 |
// Get saved session from localStorage if available
|
|
|
116 |
constructor() {
|
117 |
$effect.root(() => {
|
118 |
$effect(() => {
|
119 |
+
if (!this.init.called) return;
|
120 |
const v = $state.snapshot(this.#value);
|
121 |
try {
|
122 |
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(v));
|
|
|
128 |
}
|
129 |
|
130 |
get $() {
|
|
|
131 |
return this.#value;
|
132 |
}
|
133 |
|
src/lib/utils/array.ts
CHANGED
@@ -5,3 +5,8 @@ export function last<T>(arr: T[]): T | undefined {
|
|
5 |
export function randomPick<T>(arr: T[]): T | undefined {
|
6 |
return arr[Math.floor(Math.random() * arr.length)];
|
7 |
}
|
|
|
|
|
|
|
|
|
|
|
|
5 |
export function randomPick<T>(arr: T[]): T | undefined {
|
6 |
return arr[Math.floor(Math.random() * arr.length)];
|
7 |
}
|
8 |
+
|
9 |
+
/** Return an array with an edited element at the given index. */
|
10 |
+
export function edit<T>(arr: T[], index: number, newValue: T): T[] {
|
11 |
+
return arr.map((value, i) => (i === index ? newValue : value));
|
12 |
+
}
|
src/routes/+layout.svelte
CHANGED
@@ -5,12 +5,14 @@
|
|
5 |
import QuotaModal from "$lib/components/quota-modal.svelte";
|
6 |
import ShareModal from "$lib/components/share-modal.svelte";
|
7 |
import "../app.css";
|
|
|
8 |
|
9 |
interface Props {
|
10 |
children?: import("svelte").Snippet;
|
11 |
}
|
12 |
|
13 |
let { children }: Props = $props();
|
|
|
14 |
</script>
|
15 |
|
16 |
{@render children?.()}
|
|
|
5 |
import QuotaModal from "$lib/components/quota-modal.svelte";
|
6 |
import ShareModal from "$lib/components/share-modal.svelte";
|
7 |
import "../app.css";
|
8 |
+
import { session } from "$lib/state/session.svelte";
|
9 |
|
10 |
interface Props {
|
11 |
children?: import("svelte").Snippet;
|
12 |
}
|
13 |
|
14 |
let { children }: Props = $props();
|
15 |
+
session.init.fn();
|
16 |
</script>
|
17 |
|
18 |
{@render children?.()}
|