Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ai": "^4.3.9",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"nuqs": "^2.4.3",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"tailwind-merge": "^3.2.0",
Expand Down
33 changes: 33 additions & 0 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useAgentChat } from "agents/ai-react";
import type { Message } from "@ai-sdk/react";
import { APPROVAL } from "./shared";
import type { tools } from "./tools";
import { useQueryState } from "nuqs";

// Component imports
import { Button } from "@/components/button/Button";
Expand All @@ -21,14 +22,17 @@ import {
Robot,
Sun,
Trash,
FilePlus,
} from "@phosphor-icons/react";
import { generateShortId } from "./lib/utils";

// List of tools that require human confirmation
const toolsRequiringConfirmation: (keyof typeof tools)[] = [
"getWeatherInformation",
];

export default function Chat() {
const [ thread, setThread ] = useQueryState("thread");
const [theme, setTheme] = useState<"dark" | "light">(() => {
// Check localStorage first, default to dark if not found
const savedTheme = localStorage.getItem("theme");
Expand Down Expand Up @@ -60,13 +64,25 @@ export default function Chat() {
scrollToBottom();
}, [scrollToBottom]);

useEffect(() => {
if (thread) return;
createNewThread();
}, [thread]);

const toggleTheme = () => {
const newTheme = theme === "dark" ? "light" : "dark";
setTheme(newTheme);
};

const createNewThread = () => {
const newThread = generateShortId();
setThread(newThread);
setMessages([]);
};

const agent = useAgent({
agent: "chat",
name: thread ?? undefined,
});

const {
Expand All @@ -76,6 +92,7 @@ export default function Chat() {
handleSubmit: handleAgentSubmit,
addToolResult,
clearHistory,
setMessages,
} = useAgentChat({
agent,
maxSteps: 5,
Expand All @@ -101,6 +118,10 @@ export default function Chat() {
return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
};

if (!thread) {
return <></>;
}

return (
<div className="h-[100vh] w-full p-4 flex justify-center items-center bg-fixed overflow-hidden">
<HasOpenAIKey />
Expand Down Expand Up @@ -152,6 +173,18 @@ export default function Chat() {
size="md"
shape="square"
className="rounded-full h-9 w-9"
tooltip={"New Thread"}
onClick={createNewThread}
>
<FilePlus size={20} />
</Button>

<Button
variant="ghost"
size="md"
shape="square"
className="rounded-full h-9 w-9"
tooltip={"Clear History"}
onClick={clearHistory}
>
<Trash size={20} />
Expand Down
5 changes: 4 additions & 1 deletion src/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import "./styles.css";
import { createRoot } from "react-dom/client";
import App from "./app";
import { Providers } from "@/providers";
import { NuqsAdapter } from 'nuqs/adapters/react';

const root = createRoot(document.getElementById("app")!);

root.render(
<Providers>
<div className="bg-neutral-50 text-base text-neutral-900 antialiased transition-colors selection:bg-blue-700 selection:text-white dark:bg-neutral-950 dark:text-neutral-100">
<App />
<NuqsAdapter>
<App />
</NuqsAdapter>
</div>
</Providers>
);
5 changes: 5 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

export function generateShortId(length = 8) {
const bytes = crypto.getRandomValues(new Uint8Array(length));
return btoa(String.fromCharCode(...bytes)).replace(/[^a-zA-Z0-9]/g, '').slice(0, length);
}