import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import * as ai from "@earendil-works/pi-ai"; import * as pi from "@earendil-works/pi-coding-agent"; import * as tui from "@earendil-works/pi-tui"; import * as typebox from "typebox"; import { boundedText } from "./store.ts"; import type { EphemletFactory } from "./types.ts"; /** In-memory source evaluation: no generated file or reload is needed. Not a sandbox. */ export async function loadFactory(source: string): Promise { boundedText(source, "Source"); const { createJiti } = await import("jiti"); const jiti = createJiti(import.meta.url, { fsCache: false, moduleCache: false, debug: false, interopDefault: false, virtualModules: { "@earendil-works/pi-coding-agent": pi, "@earendil-works/pi-tui": tui, "@earendil-works/pi-ai": ai, typebox, }, }); const module = (await jiti.evalModule(source, { filename: join( dirname(fileURLToPath(import.meta.url)), "ephemlet.generated.ts", ), async: true, forceTranspile: true, })) as { default?: unknown }; if (typeof module?.default !== "function") throw new Error("Ephemlet must export a default component factory"); return module.default as EphemletFactory; }