import { jsx } from "https://deno.land/x/hono@v3.11.7/middleware.ts";
import { Hono } from "https://deno.land/x/hono@v3.11.7/mod.ts";
import { ModelProvider, modelProvider } from "https://esm.town/v/yawnxyz/ai";
const app = new Hono();
const Layout = ({ children, title = "Dynamic Form Example" }) => (
<html lang="en">
<head>
<title>{title}</title>
<script src="https://unpkg.com/htmx.org@1.9.9"></script>
</head>
<body>
{children}
</body>
</html>
);
const Home: FC = () => (
<main>
<form hx-post="/generate-form" hx-swap="outerHTML" hx-target="#dynamicFormSection">
<label for="prompt">Form Prompt:</label>
<input type="text" id="prompt" name="prompt" />
<button type="submit">Generate Form</button>
</form>
<div id="dynamicFormSection"></div>
</main>
);
app.get("/", (c) => {
return c.html(
<Layout>
<Home />
</Layout>,
);
});
const generateForm = async (prompt: string) => {
const response = await modelProvider.gen({
prompt: `Generate a form based on the following prompt: ${prompt}`,
provider: 'google',
});
const generatedText = response.text;
const fields = generatedText
.split('\n')
.filter((line) => line.startsWith('**') && line.endsWith(':**'))
.map((field) => field.slice(2, -2).trim());
return (
<form>
{fields.map((field) => (
<div key={field}>
<label htmlFor={field}>{field}:</label>
<input type="text" id={field} name={field} />
</div>
))}
<button type="submit">Submit</button>
</form>
);
};
app.post("/generate-form", async (c) => {
const body = await c.req.parseBody();
const { prompt } = body;
const form = await generateForm(prompt);
return c.html(form);
});
export default app.fetch;