Back

Version 0

6/14/2024
/** @jsxImportSource https://esm.sh/react */
import OpenAI from "npm:openai";
import { renderToString } from "npm:react-dom/server";
const openai = new OpenAI();
import { Hono } from "npm:hono@3";

const jsxResponse = (jsx) => {
return new Response(renderToString(jsx), { headers: { "Content-Type": "text/html" } });
};

const clientCode = () => {
document.getElementById("input").addEventListener("submit", function(event) {
event.preventDefault();

const msgParent = document.createElement("div");
msgParent.className = "flex justify-end";
document.querySelector(".messages").appendChild(msgParent);
const msgDiv = document.createElement("div");
msgDiv.className = "rounded-lg p-2 m-2 max-w-xs bg-blue-500 text-white";
msgDiv.textContent = event.target.message.value;
msgParent.appendChild(msgDiv);
// Clear the form input
this.reset();

const responseDiv = document.createElement("pre");
responseDiv.className = "p-2 m-2 max-w-xl bg-gray-300 text-black rounded-lg whitespace-pre-wrap font-sans";
document.querySelector(".messages").appendChild(responseDiv);

// Setup the SSE connection
const input = document.getElementById("input");
const eventSource = new EventSource(
`/sse-response?message=${msgDiv.textContent}`
+ `&threadId=${input.getAttribute("data-thread-id")}`
+ `&assistantId=${input.getAttribute("data-assistant-id")}`,
);
eventSource.onmessage = function(event) {