Skip to content

Commit

Permalink
feat(ui): add toasts with decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed May 16, 2024
1 parent 11c100f commit dcba57c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ line-length = 120

[tool.pyright]
reportMissingModuleSource = false
pythonVersion = "3.12"
7 changes: 5 additions & 2 deletions src/lib/components/Chat.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
async function refresh() {
refreshing = true;
chain = await reloadChain();
refreshing = false;
try {
chain = await reloadChain();
} finally {
refreshing = false;
}
}
async function start() {
Expand Down
13 changes: 12 additions & 1 deletion src/lib/py/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
from importlib import reload
from os import getenv
from time import perf_counter
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Callable
from zipfile import BadZipFile

from micropip import uninstall
from pyodide.ffi import create_once_callable

if TYPE_CHECKING:
from micropip import install

create_proxy = create_once_callable

def with_toast[T: Callable](message: str) -> Callable[[T], T]: ...

else:
from micropip import install as _install
from pyodide.ffi import create_proxy

@wraps(_install)
async def install(*args, **kwargs):
Expand All @@ -33,6 +40,8 @@ async def patch_promplate():
patch_promplate()


@with_toast("re-installing reasonify")
@create_proxy
async def reload_reasonify_chain():
uninstall("reasonify-headless")
await install(getenv("PACKAGE", "reasonify-headless"))
Expand All @@ -45,6 +54,8 @@ async def reload_reasonify_chain():
return reasonify.chain


@with_toast("installing reasonify")
@create_proxy
async def get_reasonify_chain(patch_promplate=patch_promplate):
await gather(patch_promplate(), install(getenv("PACKAGE", "reasonify-headless")))

Expand Down
2 changes: 2 additions & 0 deletions src/lib/py/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import VERSION from "./version";
import { dev } from "$app/environment";
import * as env from "$env/static/public";
import { cacheSingleton } from "$lib/utils/cache";
import { withToast } from "$lib/utils/toast";
import { version } from "pyodide";

const indexURL = ("PUBLIC_PYODIDE_INDEX_URL" in env) ? (env.PUBLIC_PYODIDE_INDEX_URL as string).replace("{}", version) : `https://cdn.jsdelivr.net/pyodide/v${version}/full/`;
Expand All @@ -12,5 +13,6 @@ export const getPy = cacheSingleton(async () => {
const PACKAGE = dev ? `/whl/reasonify_headless-${VERSION}-py3-none-any.whl` : `reasonify-headless==${VERSION}`;
const py = await loadPyodide({ indexURL, packages: ["micropip", "typing-extensions"], env: { PACKAGE }, args: ["-OO"] });
pyodideReady.set(true);
py.globals.set("with_toast", withToast);
return py;
});
3 changes: 2 additions & 1 deletion src/lib/tools/python/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import schema from "./schema.ts?raw";
import { getPy } from "$lib/py";
import { cacheSingleton } from "$lib/utils/cache";
import { formatSchemaPrompt } from "$lib/utils/format";
import { withToast } from "$lib/utils/toast";

const instruction = `
The "source" field must be a runnable python script.
Expand All @@ -26,5 +27,5 @@ export default {
usage: "Run some python code. Open your mind. If something can be done through code, then don't use other tools.",
schema: formatSchemaPrompt(schema),
instruction,
run: async options => await (await getRun()).callKwargs(options),
run: withToast("running python code")(async options => await (await getRun()).callKwargs(options)),
} as Tool;
20 changes: 20 additions & 0 deletions src/lib/utils/toast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { PythonError } from "pyodide/ffi";

import { toast } from "svelte-sonner";

function displayError(error: any) {
if (error instanceof Error && error.name === "PythonError")
return (error as PythonError).message;
return String(error);
}

export function withToast<T extends (...args: any[]) => any>(message: string) {
return (anyFunction: T) => {
return (async (...args) => {
const promise: Promise<ReturnType<T>> = Promise.resolve(anyFunction(...args));
toast.promise(promise, { loading: message, success: message, error: displayError });
const result = await promise;
return result;
}) as T;
};
}
2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { Toaster } from "svelte-sonner";
</script>

<Toaster theme="dark" richColors />
<Toaster theme="dark" richColors position="bottom-left" toastOptions={{ class: "ws-pre-wrap font-mono" }} />

<slot />

Expand Down
6 changes: 6 additions & 0 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
import { initChain } from "$lib/py";
import { promplateReady, pyodideReady, reasonifyReady } from "$lib/stores";
import { version } from "pyodide";
import { onDestroy } from "svelte";
onDestroy(() => {
$promplateReady = false;
$reasonifyReady = false;
});
</script>

{#if browser}
Expand Down

0 comments on commit dcba57c

Please sign in to comment.