-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
it lists templates, can create new and update it
- Loading branch information
Showing
7 changed files
with
147 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { get } from "svelte/store"; | ||
import { settingsStore, systemTemplatesStore } from "$lib/store"; | ||
|
||
export interface SystemTemplate { | ||
id: string; | ||
name: string; | ||
content: string[]; | ||
updated_at: string; | ||
} | ||
|
||
export const loadSystemTemplate = (id: string): SystemTemplate => { | ||
let systemTemplate: SystemTemplate | null = null; | ||
|
||
// Retrieve the current sessions | ||
const currentSystemTemplates = get(systemTemplatesStore); | ||
|
||
// Find the session with the given id | ||
if (currentSystemTemplates) { | ||
const existingSystemTemplate = currentSystemTemplates.find(s => s.id === id); | ||
existingSystemTemplate && (systemTemplate = existingSystemTemplate); | ||
} | ||
|
||
if (!systemTemplate) { | ||
// Get the current model | ||
const model = get(settingsStore)?.ollamaModel || ""; | ||
|
||
// Create a new session | ||
systemTemplate = { id, name: "", content: [], updated_at: new Date().toISOString() }; | ||
} | ||
|
||
return systemTemplate; | ||
}; | ||
|
||
export const saveSystemTemplate = (systemTemplate: SystemTemplate): void => { | ||
// Retrieve the current sessions | ||
const currentSystemTemplates = get(systemTemplatesStore) || []; | ||
|
||
// Find the index of the session with the same id, if it exists | ||
const existingSystemTemplate = currentSystemTemplates.findIndex(s => s.id === systemTemplate.id); | ||
|
||
if (existingSystemTemplate !== -1) { | ||
// Update the existing session | ||
currentSystemTemplates[existingSystemTemplate] = systemTemplate; | ||
} else { | ||
// Add the new session if it doesn't exist | ||
currentSystemTemplates.push(systemTemplate); | ||
} | ||
|
||
// Update the store, which will trigger the localStorage update | ||
systemTemplatesStore.set(currentSystemTemplates); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,7 @@ | ||
import { type ClassValue, clsx } from "clsx"; | ||
import { twMerge } from "tailwind-merge"; | ||
import { cubicOut } from "svelte/easing"; | ||
import type { TransitionConfig } from "svelte/transition"; | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)); | ||
export function generateStorageId() { | ||
return Math.random().toString(36).substring(2, 8); // E.g. `z7avx9` | ||
} | ||
|
||
type FlyAndScaleParams = { | ||
y?: number; | ||
x?: number; | ||
start?: number; | ||
duration?: number; | ||
}; | ||
|
||
export const flyAndScale = ( | ||
node: Element, | ||
params: FlyAndScaleParams = { y: -8, x: 0, start: 0.95, duration: 150 } | ||
): TransitionConfig => { | ||
const style = getComputedStyle(node); | ||
const transform = style.transform === "none" ? "" : style.transform; | ||
|
||
const scaleConversion = ( | ||
valueA: number, | ||
scaleA: [number, number], | ||
scaleB: [number, number] | ||
) => { | ||
const [minA, maxA] = scaleA; | ||
const [minB, maxB] = scaleB; | ||
|
||
const percentage = (valueA - minA) / (maxA - minA); | ||
const valueB = percentage * (maxB - minB) + minB; | ||
|
||
return valueB; | ||
}; | ||
|
||
const styleToString = ( | ||
style: Record<string, number | string | undefined> | ||
): string => { | ||
return Object.keys(style).reduce((str, key) => { | ||
if (style[key] === undefined) return str; | ||
return str + `${key}:${style[key]};`; | ||
}, ""); | ||
}; | ||
|
||
return { | ||
duration: params.duration ?? 200, | ||
delay: 0, | ||
css: (t) => { | ||
const y = scaleConversion(t, [0, 1], [params.y ?? 5, 0]); | ||
const x = scaleConversion(t, [0, 1], [params.x ?? 0, 0]); | ||
const scale = scaleConversion(t, [0, 1], [params.start ?? 0.95, 1]); | ||
|
||
return styleToString({ | ||
transform: `${transform} translate3d(${x}px, ${y}px, 0) scale(${scale})`, | ||
opacity: t | ||
}); | ||
}, | ||
easing: cubicOut | ||
}; | ||
}; | ||
export function getUpdatedAtDate() { | ||
return new Date().toISOString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<script lang="ts"> | ||
import { onMount } from 'svelte'; | ||
import { systemTemplatesStore } 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); | ||
} | ||
} | ||
let newSessionId: string; | ||
function setSessionId() { | ||
newSessionId = generateStorageId(); | ||
} | ||
onMount(setSessionId); | ||
</script> | ||
|
||
<div class="container"> | ||
<h1>System Templates</h1> | ||
|
||
{#if $systemTemplatesStore} | ||
<ul> | ||
{#each $systemTemplatesStore as template} | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<script lang="ts"> | ||
import type { PageData } from '../$types'; | ||
import { loadSystemTemplate, saveSystemTemplate } from '$lib/systemTemplates'; | ||
import Button from '$lib/components/Button.svelte'; | ||
import { getUpdatedAtDate } from '$lib/utils'; | ||
export let data: PageData; | ||
let systemTemplate = loadSystemTemplate(data.id); | ||
let { name, content, updated_at } = systemTemplate; | ||
function handleSubmit() { | ||
saveSystemTemplate({ id: systemTemplate.id, name, content, updated_at: getUpdatedAtDate() }); | ||
} | ||
</script> | ||
|
||
{#if systemTemplate} | ||
<div class="container"> | ||
<ul> | ||
<li><a href="/templates">/templates</a></li> | ||
<li>id: {systemTemplate.id}</li> | ||
<li>created_at: {systemTemplate.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> | ||
</ul> | ||
|
||
<Button class="w-full" on:click={handleSubmit} disabled={!name && !content}>Save</Button> | ||
</div> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { PageLoad } from './$types'; | ||
|
||
export const load = (async ({ params }) => { | ||
return { id: params.id }; | ||
}) satisfies PageLoad; |