Thomas G. Lopes commited on
Commit
52803ee
·
1 Parent(s): 2cadf2a

simpler stores

Browse files
Files changed (2) hide show
  1. src/lib/stores/models.ts +5 -30
  2. src/lib/stores/session.ts +43 -59
src/lib/stores/models.ts CHANGED
@@ -1,33 +1,8 @@
1
  import { page } from "$app/stores";
2
  import type { ModelEntryWithTokenizer } from "$lib/components/InferencePlayground/types";
3
- import { effect } from "$lib/utils/effect";
4
- import { get, writable } from "svelte/store";
5
 
6
- function createModelsStore() {
7
- let hasStarted = false;
8
- const store = writable<ModelEntryWithTokenizer[]>([]);
9
-
10
- function init() {
11
- store.set(get(page)?.data.models ?? []);
12
- hasStarted = true;
13
- }
14
-
15
- const subscribe: (typeof store)["subscribe"] = (...args) => {
16
- if (!hasStarted) init();
17
- hasStarted = true;
18
- const unsubs = [
19
- effect(page, $page => {
20
- store.set($page.data.models);
21
- }),
22
- store.subscribe(...args),
23
- ];
24
-
25
- return () => {
26
- unsubs.forEach(unsub => unsub());
27
- };
28
- };
29
-
30
- return { ...store, subscribe };
31
- }
32
-
33
- export const models = createModelsStore();
 
1
  import { page } from "$app/stores";
2
  import type { ModelEntryWithTokenizer } from "$lib/components/InferencePlayground/types";
3
+ import { readable } from "svelte/store";
 
4
 
5
+ export const models = readable<ModelEntryWithTokenizer[]>(undefined, set => {
6
+ const unsub = page.subscribe($p => set($p.data.models));
7
+ return unsub;
8
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/lib/stores/session.ts CHANGED
@@ -6,66 +6,50 @@ import {
6
  import type { Conversation, ConversationMessage, Session } from "$lib/components/InferencePlayground/types";
7
 
8
  import { models } from "$lib/stores/models";
9
- import { partialSet, safePage } from "$lib/utils/store";
10
  import { get, writable } from "svelte/store";
11
 
12
- export function createSessionStore() {
13
- let hasStarted = false;
14
- const store = writable<Session>();
15
-
16
- // Init is needed, otherwise there are stale values coming from page.
17
- function init() {
18
- const startMessageUser: ConversationMessage = { role: "user", content: "" };
19
- const modelIdsFromQueryParam = get(safePage)?.url?.searchParams?.get("modelId")?.split(",");
20
- const modelsFromQueryParam = modelIdsFromQueryParam?.map(id => get(models).find(model => model.id === id));
21
- const systemMessage: ConversationMessage = {
22
- role: "system",
23
- content: modelIdsFromQueryParam?.[0] ? (defaultSystemMessage?.[modelIdsFromQueryParam[0]] ?? "") : "",
24
- };
25
-
26
- store.set({
27
- conversations: [
28
- {
29
- model: get(models).find(m => FEATURED_MODELS_IDS.includes(m.id)) ??
30
- get(models)[0] ?? {
31
- id: "",
32
- downloads: 0,
33
- gated: false,
34
- likes: 0,
35
- name: "",
36
- private: false,
37
- tokenizerConfig: {},
38
- updatedAt: new Date(),
39
- },
40
- config: { ...defaultGenerationConfig },
41
- messages: [{ ...startMessageUser }],
42
- systemMessage,
43
- streaming: true,
44
- },
45
- ],
46
- });
47
-
48
- if (modelsFromQueryParam?.length) {
49
- const conversations = modelsFromQueryParam.map(model => {
50
- return {
51
- model,
52
- config: { ...defaultGenerationConfig },
53
- messages: [{ ...startMessageUser }],
54
- systemMessage,
55
- streaming: true,
56
- };
57
- }) as [Conversation] | [Conversation, Conversation];
58
- partialSet(store, { conversations });
59
- }
60
- }
61
-
62
- const subscribe: (typeof store)["subscribe"] = (...args) => {
63
- if (!hasStarted) init();
64
- hasStarted = true;
65
- return store.subscribe(...args);
66
  };
67
 
68
- return { ...store, subscribe };
69
- }
70
-
71
- export const session = createSessionStore();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import type { Conversation, ConversationMessage, Session } from "$lib/components/InferencePlayground/types";
7
 
8
  import { models } from "$lib/stores/models";
9
+ import { safePage } from "$lib/utils/store";
10
  import { get, writable } from "svelte/store";
11
 
12
+ export const session = writable<Session>(undefined, (set, update) => {
13
+ const startMessageUser: ConversationMessage = { role: "user", content: "" };
14
+ const modelIdsFromQueryParam = get(safePage)?.url?.searchParams?.get("modelId")?.split(",");
15
+ const modelsFromQueryParam = modelIdsFromQueryParam?.map(id => get(models).find(model => model.id === id));
16
+ const systemMessage: ConversationMessage = {
17
+ role: "system",
18
+ content: modelIdsFromQueryParam?.[0] ? (defaultSystemMessage?.[modelIdsFromQueryParam[0]] ?? "") : "",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  };
20
 
21
+ set({
22
+ conversations: [
23
+ {
24
+ model: get(models).find(m => FEATURED_MODELS_IDS.includes(m.id)) ??
25
+ get(models)[0] ?? {
26
+ id: "",
27
+ downloads: 0,
28
+ gated: false,
29
+ likes: 0,
30
+ name: "",
31
+ private: false,
32
+ tokenizerConfig: {},
33
+ updatedAt: new Date(),
34
+ },
35
+ config: { ...defaultGenerationConfig },
36
+ messages: [{ ...startMessageUser }],
37
+ systemMessage,
38
+ streaming: true,
39
+ },
40
+ ],
41
+ });
42
+
43
+ if (modelsFromQueryParam?.length) {
44
+ const conversations = modelsFromQueryParam.map(model => {
45
+ return {
46
+ model,
47
+ config: { ...defaultGenerationConfig },
48
+ messages: [{ ...startMessageUser }],
49
+ systemMessage,
50
+ streaming: true,
51
+ };
52
+ }) as [Conversation] | [Conversation, Conversation];
53
+ update(s => ({ ...s, conversations }));
54
+ }
55
+ });