Skip to content

Commit

Permalink
rename systemTemplate > knowledge
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaclen committed Jun 29, 2024
1 parent 87061c7 commit 716c599
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 97 deletions.
51 changes: 51 additions & 0 deletions src/lib/knowledge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { get } from "svelte/store";
import { settingsStore, knowledgeStore } from "$lib/store";

export interface Knowledge {
id: string;
name: string;
content: string[];
updated_at: string;
}

export const loadKnowledge = (id: string): Knowledge => {
let knowledge: Knowledge | null = null;

// Retrieve the current sessions
const currentKnowledges = get(knowledgeStore);

// Find the session with the given id
if (currentKnowledges) {
const existingKnowledge = currentKnowledges.find(s => s.id === id);
existingKnowledge && (knowledge = existingKnowledge);
}

if (!knowledge) {
// Get the current model
const model = get(settingsStore)?.ollamaModel || "";

// Create a new session
knowledge = { id, name: "", content: [], updated_at: new Date().toISOString() };
}

return knowledge;
};

export const saveKnowledge = (Knowledge: Knowledge): void => {
// Retrieve the current sessions
const currentKnowledges = get(knowledgeStore) || [];

// Find the index of the session with the same id, if it exists
const existingKnowledge = currentKnowledges.findIndex(s => s.id === Knowledge.id);

if (existingKnowledge !== -1) {
// Update the existing session
currentKnowledges[existingKnowledge] = Knowledge;
} else {
// Add the new session if it doesn't exist
currentKnowledges.push(Knowledge);
}

// Update the store, which will trigger the localStorage update
knowledgeStore.set(currentKnowledges);
};
4 changes: 2 additions & 2 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { browser } from '$app/environment';
import { writable } from 'svelte/store';
import type { Session } from '$lib/sessions';
import type { OllamaModel } from './ollama';
import type { SystemTemplate } from './systemTemplates';
import type { Knowledge } from './knowledge';

function createLocalStorageStore<T>(key: string, initialValue: T | null = null) {
const localStorageValue: string | null = browser ? window.localStorage.getItem(key) : null;
Expand Down Expand Up @@ -35,4 +35,4 @@ export interface Settings {

export const settingsStore = createLocalStorageStore<Settings>(`${LOCAL_STORAGE_PREFIX}-settings`);
export const sessionsStore = createLocalStorageStore<Session[]>(`${LOCAL_STORAGE_PREFIX}-sessions`);
export const systemTemplatesStore = createLocalStorageStore<SystemTemplate[]>(`${LOCAL_STORAGE_PREFIX}-system-templates`);
export const knowledgeStore = createLocalStorageStore<Knowledge[]>(`${LOCAL_STORAGE_PREFIX}-knowledge`);
51 changes: 0 additions & 51 deletions src/lib/systemTemplates.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<nav class="flex w-max flex-col gap-y-6 text-sm font-semibold">
<a href="/sessions">Sessions</a>
<a href="/knowledge">Knowdlege</a>
<a href="/motd">MOTD</a>
<!-- <a href="/motd">MOTD</a> -->
</nav>

<a href="/" class="flex flex-col items-center justify-between py-6 hover:bg-accent">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<script lang="ts">
import { onMount } from 'svelte';
import { systemTemplatesStore } from '$lib/store';
import { knowledgeStore } from '$lib/store';
import { generateStorageId } from '$lib/utils';
import Button from '$lib/components/Button.svelte';
function deleteTemplate(id: string) {
const confirmed = confirm('Are you sure you want to delete this template?');
if (!confirmed) return;
if ($systemTemplatesStore) {
$systemTemplatesStore = $systemTemplatesStore.filter((s) => s.id !== id);
if ($knowledgeStore) {
$knowledgeStore = $knowledgeStore.filter((s) => s.id !== id);
}
}
Expand All @@ -24,18 +24,22 @@
</script>

<div class="container">
<h1>System Templates</h1>
<h1>Knowledge</h1>

{#if $systemTemplatesStore}
<Button href={`/knowledge/${newSessionId}`} on:click={setSessionId}>New knowledge</Button>

{#if $knowledgeStore}
<ul>
{#each $systemTemplatesStore as template}
{#each $knowledgeStore as template}
<li>
<strong><a href="/knowledge/{template.id}">{template.name}</a></strong>
</li>
<li>Lupdated on: {template.updated_at}</li>
<li>
<Button href="/templates/{template.id}">{template.name}</Button> — updated on: {template.updated_at}
<Button variant="secondary" on:click={() => deleteTemplate(template.id)}>Delete</Button>
</li>
{/each}
</ul>
{/if}

<a href={`templates/${newSessionId}`} on:click={setSessionId}>Create New Template</a>
</div>
31 changes: 31 additions & 0 deletions src/routes/knowledge/[id]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script lang="ts">
import type { PageData } from '../$types';
import { loadKnowledge, saveKnowledge } from '$lib/knowledge';
import Button from '$lib/components/Button.svelte';
import { getUpdatedAtDate } from '$lib/utils';
export let data: PageData;
let knowledge = loadKnowledge(data.id);
let { id, name, content, updated_at } = knowledge;
function handleSubmit() {
saveKnowledge({ id, name, content, updated_at: getUpdatedAtDate() });
}
</script>

{#if knowledge}
<ul>
<li>id: {id}</li>
<li>created_at: {updated_at}</li>
<li>name: <input class="border border-neutral-500" bind:value={name} /></li>
<li>
content:
<textarea class="border border-neutral-500" bind:value={content}></textarea>
</li>
<li>
<Button class="w-full" on:click={handleSubmit} disabled={!name && !content}>Save</Button>
</li>
</ul>
{/if}
File renamed without changes.
2 changes: 0 additions & 2 deletions src/routes/sessions/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@
});
</script>



<div class="flex w-full flex-col bg-secondary">
<div class="justify-content-center m-auto max-w-[40ch] p-6">
<div class="fieldset">
Expand Down
33 changes: 0 additions & 33 deletions src/routes/templates/[id]/+page.svelte

This file was deleted.

0 comments on commit 716c599

Please sign in to comment.