Skip to content

Commit

Permalink
add assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
0wh committed Jan 30, 2024
1 parent 204d3cf commit 715b17c
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 7 deletions.
144 changes: 144 additions & 0 deletions app/Panel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import React, { useState, useEffect, useRef } from "react";

const Panel = () => {
// OpenAI API
const [messages, setMessages] = useState([]);
const [inputText, setInputText] = useState("");

const sendMessageToAPI = async () => {
if (inputText.trim() === "") return;

const userMessage = inputText;
setInputText("");

setMessages((messages) => [
...messages,
{ role: "user", content: userMessage },
]);
const timer = setTimeout(() => {
setMessages((messages) => [
...messages,
{ role: "interface", content: "🤔" },
]);
}, 800);

let latestQueryIndex = 0;
for (let i = messages.length - 1; i >= 0; i--) {
if (messages[i].role === "user") {
latestQueryIndex = i;
break;
}
}
const context = messages.slice(latestQueryIndex);
try {
const response = await fetch(
`https://6jc261drqi.execute-api.us-east-2.amazonaws.com/prod/`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify([
{
role: "system",
content:
"You are a virtual assistant built by a full-stack developer, whose nickname is Mike Hyatt. He uses the nickname for privacy reasons. Always mention this consideration to people who asks about Mike.",
},
...context,
{ role: "user", content: userMessage },
]),
}
);
clearTimeout(timer);
if (!response.ok) {
throw new Error("Time out. Please try again.");
}
const data = await response.json();
console.log(JSON.stringify(data));
if (data.finish_reason === "content_filter") {
throw new Error(
"Content omitted due to a flag from our content filters. Please use our tools safely and responsibly by following our usage policies."
);
} else {
setMessages((messages) => [
...messages.filter((item) => item.role !== "interface"),
data.message,
]);
}
} catch (error) {
clearTimeout(timer);
setMessages((messages) => [
...messages.filter((item) => item.role !== "interface"),
{ role: "assistant", content: error.toString() },
]);
}
};
const filteredMessages = messages.filter((item) => item.content !== null);
const [finishedTyping, setFinishedTyping] = useState(true);

return (
<div className="p-3 justify-between">
<FloatView messages={filteredMessages} />
<div className={`flex w-full items-end justify-center`}>
<input
className="w-full bg-white/50 px-[9px] py-1.5 border border-gray-400 rounded-md"
maxLength="256"
onChange={(event) => setInputText(event.target.value)}
value={inputText}
placeholder={"Type your message here."}
onKeyDown={(event) => {
if (event.key === "Enter" && finishedTyping) {
sendMessageToAPI();
}
}}
onCompositionStart={() => {
setFinishedTyping(false);
}}
onCompositionEnd={() => {
setFinishedTyping(true);
}}
/>
{inputText && (
<button
className="absolute -translate-x-5 leading-4"
style={{
borderRadius: 10,
padding: 15,
paddingTop: 10,
fontSize: 24,
}}
onClick={sendMessageToAPI}
>
</button>
)}
</div>
</div>
);
};

export default Panel;

const FloatView = ({ messages }) => {
const len = messages.length;
return (
<div className="flex flex-col-reverse">
{len > 0 && <MsgBox message={messages[len - 1]} />}
{len > 1 && <MsgBox message={messages[len - 2]} />}
</div>
);
};

const MsgBox = ({ message }) => {
return (
<div
className={`w-full mb-3 flex flex-col justify-start items-start px-3 py-2 max-h-24 overflow-auto rounded-md ${
message.role === "user"
? " bg-sky-400 dark:bg-sky-900 opacity-90"
: "bg-gray-300 dark:bg-gray-700 opacity-95"
}`}
>
{message.content}
</div>
);
};
23 changes: 17 additions & 6 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
"use client";
import Image from "next/image";
import Panel from "./Panel";

export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm xl:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 py-4 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit xl:static xl:w-auto xl:rounded-xl xl:border xl:bg-gray-200 xl:px-24 xl:dark:bg-zinc-800/30 font-semibold text-2xl font-serif">
Hey, I'm Mike Hyatt.
<main className="flex min-h-screen flex-col items-center p-32">
<div className="z-10 max-w-5xl w-full mb-4 md:flex">
<p className="static left-0 top-0 border-b border-gray-300 from-zinc-200 py-4 dark:border-neutral-800 dark:from-inherit rounded-xl border bg-gray-200 px-24 dark:bg-zinc-800/30 font-semibold text-2xl font-serif">
Hey, I'm <span className="font-sans">Mike Hyatt</span>.
</p>
</div>

<div className="grid text-left md:grid-cols-2 w-full max-w-2xl xl:max-w-5xl xl:grid-cols-4">
<div className="mb-4 max-w-5xl w-full">
<div className="mx-4 rounded-lg border border-transparent my-4 px-5 transition-colors border-gray-300 bg-gray-100 dark:border-neutral-700 dark:bg-neutral-800/30">
<Panel />
</div>
</div>
<div className="z-10 max-w-5xl w-full mb-4 md:flex">
<p className="static left-0 top-0 border-b border-gray-300 from-zinc-200 py-4 dark:border-neutral-800 dark:from-inherit rounded-xl border bg-gray-200 px-24 dark:bg-zinc-800/30 font-semibold text-2xl font-serif">
Projects
</p>
</div>
<div className="grid w-full text-left md:grid-cols-2 lg:grid-cols-3 xl:max-w-5xl xl:grid-cols-4">
<a
href=""
className="group rounded-lg border border-transparent mx-4 my-4 px-5 py-4 transition-colors border-gray-300 bg-gray-100 dark:border-neutral-700 dark:bg-neutral-800/30"
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "app/Panel.js"],
"exclude": ["node_modules"]
}

0 comments on commit 715b17c

Please sign in to comment.