Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Jul 1, 2024
1 parent b3830b1 commit 27ac42f
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/lib/components/ErrorExplainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Markdown from "./Markdown.svelte";
import { draggable } from "@neodrag/svelte";
import { renderMarkdown } from "$lib/markdown";
import { explain } from "$lib/pyodide/explain";
import { explain } from "$lib/pyodide/start/chat";
import { onMount } from "svelte";
import { cubicIn, cubicOut } from "svelte/easing";
import { scale } from "svelte/transition";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/console/HeadlessConsole.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<script lang="ts">
import type { ConsoleAPI } from "$py/console/console";
import { getPy } from "$lib/pyodide";
import getPy from "$lib/pyodide";
import { onDestroy, onMount } from "svelte";
export let ready = false;
Expand All @@ -27,7 +27,7 @@
let loading = 0;
onMount(async () => {
const py = await getPy();
const py = await getPy({ console: true });
pyConsole = (py.pyimport("console.console")).ConsoleAPI();
complete = pyConsole.complete;
Expand Down
18 changes: 0 additions & 18 deletions src/lib/pyodide/explain.ts

This file was deleted.

17 changes: 16 additions & 1 deletion src/lib/pyodide/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
export { getPy } from "./get-py";
import { getPyodide } from "./start/init";

export default async function getPy(feature: { console?: boolean; chat?: boolean }) {
const { console = false, chat = false } = feature;

if (console) {
const { loadConsole } = await import("./start/console");
await loadConsole();
}
if (chat) {
const { loadChat } = await import("./start/chat");
await loadChat();
}

return await getPyodide();
}
29 changes: 13 additions & 16 deletions src/lib/pyodide/get-py.ts → src/lib/pyodide/start/chat.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { ClientOptions } from "openai";

import console from "../../python/console";
import { loadChat } from "./explain";
import { getAtomicPy as getAtomicPyodide, getSetupModule } from "./init";
import chat from "../../../python/chat";
import { getPyodide, setupModule } from "./init";
import * as env from "$env/static/public";
import { pyodideReady } from "$lib/stores";
import { cacheSingleton } from "$lib/utils/cache";
import { OpenAI } from "openai";
import * as version from "openai/version";
Expand All @@ -19,17 +17,16 @@ class PatchedOpenAI extends OpenAI {
}
}

export const getPy = cacheSingleton(async () => {
const py = await getAtomicPyodide();
const setupModule = await getSetupModule();

export const loadChat = cacheSingleton(async () => {
const py = await getPyodide();
py.registerJsModule("openai", { OpenAI: PatchedOpenAI, version, __all__: [] });

setupModule(py.toPy(console), "console");

pyodideReady.set(true);

loadChat();

return py;
await setupModule(chat, "chat");
await py.pyimport("chat.install_requirements");
});

export async function* explain(traceback: string, code: string): AsyncGenerator<string> {
const py = await getPyodide();
await loadChat();
const explain = py.pyimport("chat.explain").explain;
yield * explain(traceback, code);
}
11 changes: 11 additions & 0 deletions src/lib/pyodide/start/console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import console from "../../../python/console";
import { getPyodide, setupModule } from "./init";
import { pyodideReady } from "$lib/stores";
import { cacheSingleton } from "$lib/utils/cache";

export const loadConsole = cacheSingleton(async () => {
const py = await getPyodide();
await setupModule(console, "console");
pyodideReady.set(true);
return py;
});
19 changes: 12 additions & 7 deletions src/lib/pyodide/init.ts → src/lib/pyodide/start/init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PyProxy } from "pyodide/ffi";

import patches from "../../python/patches";
import source from "./load-sources.py?raw";
import patches from "../../../python/patches";
import loader from "./loader.py?raw";
import { dev } from "$app/environment";
import * as env from "$env/static/public";
import { cacheSingleton } from "$lib/utils/cache";
Expand All @@ -18,23 +18,28 @@ else if (dev)
else
indexURL = env.PUBLIC_PYODIDE_INDEX_URL ?? "/pyodide/";

export const getMinimalPyodide = cacheSingleton(withToast({ loading: "loading pyodide runtime" })(async () => {
const getMinimalPyodide = cacheSingleton(withToast({ loading: "loading pyodide runtime" })(async () => {
const { loadPyodide } = await import("pyodide");
const py = await loadPyodide({ indexURL, env: getEnv(), packages: ["micropip"], args: dev ? [] : ["-O"] });
py.globals.set("with_toast", withToast);
py.globals.set("toast", toast);
return py;
}));

export const getSetupModule = cacheSingleton(async () => {
const getSetupModule = cacheSingleton(async () => {
const py = await getMinimalPyodide();
return py.runPython(source) as (sources: Record<string, string>, moduleName: string) => PyProxy;
return py.runPython(loader) as (sources: Record<string, string>, moduleName: string) => PyProxy;
});

export const getAtomicPy = cacheSingleton(async () => {
export async function setupModule(sources: Record<string, string>, moduleName: string) {
const setupModule = await getSetupModule();
const py = await getMinimalPyodide();
setupModule(py.toPy(patches), "patches");
setupModule(py.toPy(sources), moduleName);
}

export const getPyodide = cacheSingleton(async () => {
const py = await getMinimalPyodide();
await setupModule(patches, "patches");
py.pyimport("patches.patches");
return py;
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@
from sys import modules
from sys import path as sys_path

root = Path("/runtime_sources/python")
from js import console

root = Path("/runtime_sources/")

sys_path.insert(0, root.as_posix())


def setup_module(sources: dict[str, str], module_name: str):
console.groupCollapsed(module_name)

for path, source in sources.items():
file = root / module_name / path
print(file)
if not file.parent.is_dir():
file.parent.mkdir(parents=True)
file.write_text(source, "utf-8")

console.groupCollapsed(path)
console.log(source)
console.groupEnd()

if __debug__ and (name := path.replace("/", ".")) in modules:
del modules[name] # reload module at dev time

console.groupEnd()


setup_module
File renamed without changes.
8 changes: 7 additions & 1 deletion src/python/chat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
async def asynchronous_bootstrap():
from asyncio import ensure_future
from operator import call


@ensure_future
@call
async def install_requirements():
from micropip import install

await install(["promplate==0.3.4.8", "promplate-pyodide==0.0.3.3"])
Expand Down
2 changes: 0 additions & 2 deletions src/python/console/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@ def js_api(func):
def wrapper(*args, **kwargs):
return _to_js(func(*map(_to_py, args), **{k: _to_py(v) for k, v in kwargs.items()}))

setattr(wrapper, "_is_js_api", True)

return wrapper
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import HeadlessConsole from "$lib/components/console/HeadlessConsole.svelte";
import ConsolePrompt from "$lib/components/ConsolePrompt.svelte";
import Modal from "$lib/components/Modal.svelte";
import { patchSource, reformatInputSource } from "$lib/pyodide/translate";
import { pyodideReady } from "$lib/stores";
import { patchSource, reformatInputSource } from "$lib/utils/formatSource";
import { afterUpdate, beforeUpdate, onMount } from "svelte";
import { cubicIn, cubicOut } from "svelte/easing";
import { scale } from "svelte/transition";
Expand Down

0 comments on commit 27ac42f

Please sign in to comment.