-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: simplify patch step improve performance
- Loading branch information
1 parent
3869d34
commit 72f2a7f
Showing
9 changed files
with
58 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/pyodide/* https://promplate.dev/pyodide/:splat 200 | ||
/pyodide/* https://cdn.jsdelivr.net/pyodide/v0.25.0/full/:splat 200 |
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
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,40 +1,50 @@ | ||
import type { ClientOptions } from "openai"; | ||
|
||
import { pyodideReady } from "../stores"; | ||
import * as env from "$env/static/public"; | ||
import { cacheSingleton } from "$lib/utils/cache"; | ||
import { getEnv } from "$lib/utils/env"; | ||
import { withToast } from "$lib/utils/toast"; | ||
import { toast } from "svelte-sonner"; | ||
|
||
const indexURL = typeof window === "undefined" ? undefined : (process.env.NODE_ENV === "production" && "/pyodide/") || "https://promplate.dev/pyodide/"; | ||
const indexURL = typeof window === "undefined" ? undefined : (process.env.NODE_ENV === "production" && "/pyodide/") || "https://cdn.jsdelivr.net/pyodide/v0.25.0/full/"; | ||
|
||
async function initPyodide() { | ||
const { loadPyodide } = await import("pyodide"); | ||
return await loadPyodide({ indexURL, env: { ...env }, packages: ["micropip", "typing-extensions"] }); | ||
const py = await loadPyodide({ indexURL, env: getEnv(), packages: ["micropip", "typing-extensions"] }); | ||
pyodideReady.set(true); | ||
return py; | ||
} | ||
|
||
const getPyodide = cacheSingleton(withToast(initPyodide, { loading: "loading pyodide runtime" })); | ||
|
||
async function initPy() { | ||
const [py, { AsyncClient }, version, { default: initCode }] = await Promise.all([ | ||
initPyodide(), | ||
import("./translate"), | ||
const [py, { OpenAI }, version, { default: initCode }] = await Promise.all([ | ||
getPyodide(), | ||
import("openai"), | ||
import("openai/version"), | ||
import("./init.py?raw"), | ||
]); | ||
const info = toast.loading("installing extra python dependencies"); | ||
|
||
py.registerJsModule("openai", { AsyncClient, Client: () => null, version, __all__: [] }); | ||
py.registerJsModule("httpx", { AsyncClient: () => null, Client: () => null }); | ||
|
||
await py.runPythonAsync(initCode); | ||
class PatchedOpenAI extends OpenAI { | ||
constructor(options: ClientOptions) { | ||
if (!options.apiKey) | ||
(options.apiKey = env.PUBLIC_OPENAI_API_KEY); | ||
if (!options.baseURL) | ||
options.baseURL = env.PUBLIC_OPENAI_BASE_URL; | ||
super(options); | ||
} | ||
} | ||
|
||
toast.dismiss(info); | ||
py.registerJsModule("openai", { OpenAI: PatchedOpenAI, version, __all__: [] }); | ||
|
||
pyodideReady.set(true); | ||
await withToast(py.runPythonAsync.bind(null, initCode), { loading: "installing extra python dependencies", duration: 300 })(); | ||
|
||
return py; | ||
} | ||
|
||
export const getPy = cacheSingleton(withToast(initPy, { loading: "preparing pyodide runtime", success: `successfully initialized pyodide runtime` })); | ||
export const getPy = cacheSingleton(initPy); | ||
|
||
export async function initConsole() { | ||
const [py, { default: initConsoleCode }] = await Promise.all([getPy(), import("./console.py?raw")]); | ||
const [py, { default: initConsoleCode }] = await Promise.all([getPyodide(), import("./console.py?raw")]); | ||
await py.runPythonAsync(initConsoleCode); | ||
} |
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,5 @@ | ||
import * as env from "$env/static/public"; | ||
|
||
export function getEnv() { | ||
return Object.fromEntries(Object.entries(env).map(([k, v]) => [k.replace("PUBLIC_", ""), v])); | ||
} |
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
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