Thomas G. Lopes commited on
Commit
e657b46
·
1 Parent(s): af1f386

improve initialization

Browse files
src/lib/spells/create-init.svelte.ts CHANGED
@@ -1,10 +1,10 @@
1
- export function createInit(cb: () => void) {
2
  let called = $state(false);
3
 
4
- function init() {
5
  if (called) return;
6
  called = true;
7
- cb();
8
  }
9
 
10
  return Object.defineProperties(init, {
 
1
+ export function createInit(cb: () => void | Promise<void>) {
2
  let called = $state(false);
3
 
4
+ async function init() {
5
  if (called) return;
6
  called = true;
7
+ await cb();
8
  }
9
 
10
  return Object.defineProperties(init, {
src/lib/state/conversations.svelte.ts CHANGED
@@ -283,7 +283,19 @@ class Conversations {
283
 
284
  #active = $derived.by(() => this.for(projects.activeId));
285
 
286
- init = createInit(() => {
 
 
 
 
 
 
 
 
 
 
 
 
287
  const searchParams = new URLSearchParams(window.location.search);
288
  const searchProvider = searchParams.get("provider") ?? "";
289
  const searchModelId = searchParams.get("modelId") ?? "";
@@ -291,7 +303,7 @@ class Conversations {
291
  const searchModel = models.remote.find(m => m.id === searchModelId);
292
  if (!searchModel) return;
293
 
294
- conversationsRepo
295
  .upsert({
296
  where: { projectId: DEFAULT_PROJECT_ID },
297
  set: {
@@ -338,17 +350,6 @@ class Conversations {
338
  };
339
 
340
  for = (projectId: ProjectEntity["id"]): ConversationClass[] => {
341
- // Async load from db
342
- if (!this.#conversations[projectId]?.length) {
343
- conversationsRepo.find({ where: { projectId } }).then(c => {
344
- if (!c.length) {
345
- const dc = conversationsRepo.create(getDefaultConversation(projectId));
346
- c.push(dc);
347
- }
348
- this.#conversations = { ...this.#conversations, [projectId]: c.map(c => new ConversationClass(c)) };
349
- });
350
- }
351
-
352
  let res = this.#conversations[projectId];
353
  if (res?.length === 0 || !res) {
354
  // We set id to -1 because it is temporary, there should always be a conversation.
 
283
 
284
  #active = $derived.by(() => this.for(projects.activeId));
285
 
286
+ init = createInit(async () => {
287
+ // Load all conversations from all projects
288
+ await Promise.all(
289
+ projects.all.map(async p => {
290
+ const c = await conversationsRepo.find({ where: { projectId: p.id } });
291
+ if (!c.length) {
292
+ const dc = conversationsRepo.create(getDefaultConversation(p.id));
293
+ c.push(dc);
294
+ }
295
+ this.#conversations = { ...this.#conversations, [p.id]: c.map(c => new ConversationClass(c)) };
296
+ }),
297
+ );
298
+
299
  const searchParams = new URLSearchParams(window.location.search);
300
  const searchProvider = searchParams.get("provider") ?? "";
301
  const searchModelId = searchParams.get("modelId") ?? "";
 
303
  const searchModel = models.remote.find(m => m.id === searchModelId);
304
  if (!searchModel) return;
305
 
306
+ await conversationsRepo
307
  .upsert({
308
  where: { projectId: DEFAULT_PROJECT_ID },
309
  set: {
 
350
  };
351
 
352
  for = (projectId: ProjectEntity["id"]): ConversationClass[] => {
 
 
 
 
 
 
 
 
 
 
 
353
  let res = this.#conversations[projectId];
354
  if (res?.length === 0 || !res) {
355
  // We set id to -1 because it is temporary, there should always be a conversation.
src/lib/state/models.svelte.ts CHANGED
@@ -15,13 +15,6 @@ class Models {
15
  all = $derived([...this.remote, ...this.custom]);
16
 
17
  constructor() {
18
- getModels().then(models => {
19
- this.remote = models;
20
- });
21
- getRouterData().then(data => {
22
- this.routerData = data;
23
- });
24
-
25
  const savedData = localStorage.getItem(LOCAL_STORAGE_KEY);
26
  if (!savedData) return;
27
 
@@ -34,6 +27,13 @@ class Models {
34
  }
35
  }
36
 
 
 
 
 
 
 
 
37
  #custom = $state.raw<CustomModel[]>([]);
38
 
39
  get custom() {
 
15
  all = $derived([...this.remote, ...this.custom]);
16
 
17
  constructor() {
 
 
 
 
 
 
 
18
  const savedData = localStorage.getItem(LOCAL_STORAGE_KEY);
19
  if (!savedData) return;
20
 
 
27
  }
28
  }
29
 
30
+ async load() {
31
+ await Promise.all([getModels(), getRouterData()]).then(([models, data]) => {
32
+ this.remote = models;
33
+ this.routerData = data;
34
+ });
35
+ }
36
+
37
  #custom = $state.raw<CustomModel[]>([]);
38
 
39
  get custom() {
src/lib/state/projects.svelte.ts CHANGED
@@ -4,6 +4,7 @@ import { Entity, Fields, repo, type MembersOnly } from "remult";
4
  import { PersistedState } from "runed";
5
  import { checkpoints } from "./checkpoints.svelte";
6
  import { conversations } from "./conversations.svelte";
 
7
 
8
  @Entity("project")
9
  export class ProjectEntity {
@@ -46,16 +47,15 @@ class Projects {
46
  this.#activeId.current = id;
47
  }
48
 
49
- constructor() {
50
- projectsRepo.find().then(res => {
51
- if (!res.some(p => p.id === this.activeId)) this.activeId === DEFAULT_PROJECT_ID;
52
 
53
- res.forEach(p => {
54
- if (dequal(this.#projects[p.id], p)) return;
55
- this.#projects[p.id] = p;
56
- });
57
  });
58
- }
59
 
60
  async create(args: Omit<ProjectEntity, "id">): Promise<string> {
61
  const p = await projectsRepo.save({ ...args });
 
4
  import { PersistedState } from "runed";
5
  import { checkpoints } from "./checkpoints.svelte";
6
  import { conversations } from "./conversations.svelte";
7
+ import { createInit } from "$lib/spells/create-init.svelte";
8
 
9
  @Entity("project")
10
  export class ProjectEntity {
 
47
  this.#activeId.current = id;
48
  }
49
 
50
+ init = createInit(async () => {
51
+ const res = await projectsRepo.find();
52
+ if (!res.some(p => p.id === this.activeId)) this.activeId === DEFAULT_PROJECT_ID;
53
 
54
+ res.forEach(p => {
55
+ if (dequal(this.#projects[p.id], p)) return;
56
+ this.#projects[p.id] = p;
 
57
  });
58
+ });
59
 
60
  async create(args: Omit<ProjectEntity, "id">): Promise<string> {
61
  const p = await projectsRepo.save({ ...args });
src/routes/+layout.svelte CHANGED
@@ -5,7 +5,6 @@
5
  import Prompts from "$lib/components/prompts.svelte";
6
  import QuotaModal from "$lib/components/quota-modal.svelte";
7
  import ShareModal from "$lib/components/share-modal.svelte";
8
- import { conversations } from "$lib/state/conversations.svelte";
9
  import "../app.css";
10
 
11
  interface Props {
@@ -13,13 +12,12 @@
13
  }
14
 
15
  let { children }: Props = $props();
16
- conversations.init();
17
  </script>
18
 
19
  <svelte:boundary>
20
  {@render children?.()}
21
  {#snippet pending()}
22
- <!-- pending -->
23
  {/snippet}
24
  </svelte:boundary>
25
 
 
5
  import Prompts from "$lib/components/prompts.svelte";
6
  import QuotaModal from "$lib/components/quota-modal.svelte";
7
  import ShareModal from "$lib/components/share-modal.svelte";
 
8
  import "../app.css";
9
 
10
  interface Props {
 
12
  }
13
 
14
  let { children }: Props = $props();
 
15
  </script>
16
 
17
  <svelte:boundary>
18
  {@render children?.()}
19
  {#snippet pending()}
20
+ <p class="abs-center absolute dark:text-white">🤗</p>
21
  {/snippet}
22
  </svelte:boundary>
23
 
src/routes/+page.svelte CHANGED
@@ -1,5 +1,12 @@
1
  <script lang="ts">
2
  import Playground from "$lib/components/inference-playground/playground.svelte";
 
 
 
 
 
 
 
3
  </script>
4
 
5
  <Playground />
 
1
  <script lang="ts">
2
  import Playground from "$lib/components/inference-playground/playground.svelte";
3
+ import { conversations } from "$lib/state/conversations.svelte";
4
+ import { models } from "$lib/state/models.svelte";
5
+ import { projects } from "$lib/state/projects.svelte";
6
+
7
+ await models.load();
8
+ await projects.init();
9
+ await conversations.init();
10
  </script>
11
 
12
  <Playground />