Skip to content

Commit

Permalink
Format ai agent messages as markdown, also auto scroll when new messa…
Browse files Browse the repository at this point in the history
…ges come in.
  • Loading branch information
chirino committed Oct 4, 2024
1 parent 4cbbf04 commit bc64c03
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"devDependencies": {
"@hey-api/openapi-ts": "^0.53.5",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.10",
"@tailwindcss/typography": "^0.5.15",
"@testing-library/jest-dom": "^6.4.6",
"@testing-library/react": "^16.0.0",
"@testing-library/user-event": "^14.4.3",
Expand Down
26 changes: 20 additions & 6 deletions client/src/app/components/ai-assistant.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState } from "react";
import React, { useContext, useEffect, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import {
Card,
Expand All @@ -12,6 +12,7 @@ import { ScrollArea } from "@/components/ui/scroll-area";
import { MessageCircle, Send, X } from "lucide-react";
import { ChatMessage, ChatState } from "../client";
import { useCompletionMutation } from "../queries/ai";
import ReactMarkdown from "react-markdown";

interface Conversation {
isEnabled: boolean;
Expand Down Expand Up @@ -106,16 +107,29 @@ export function AIAssistant({ viewing }: { viewing?: string }) {
});
};

const scrollAreaRef = useRef(null as HTMLDivElement | null);
const scrollToBottom = () => {
setTimeout(() => {
if (scrollAreaRef?.current) {
const scrollArea = scrollAreaRef.current;
scrollArea.scrollTop = scrollArea.scrollHeight;
}
}, 0);
};

useEffect(() => {
scrollToBottom();
}, [completionMutation.isPending]);

if (!isEnabled) {
return null;
}

return (
<>
{/* Floating button */}
{!isOpen && (
<Button
className="fixed bottom-20 right-4 rounded-full p-4"
className="fixed bottom-20 right-4 rounded-full p-4 z-10"
onClick={() => setIsOpen(true)}
>
<MessageCircle className="h-6 w-6" />
Expand All @@ -139,7 +153,7 @@ export function AIAssistant({ viewing }: { viewing?: string }) {
</Button>
</CardHeader>
<CardContent className="flex-grow overflow-hidden">
<ScrollArea className="h-full w-full pr-4">
<ScrollArea className="h-full w-full pr-4" ref={scrollAreaRef}>
<div className={`mb-4 text-left`}>
<span className={`inline-block p-2 rounded-lg bg-muted`}>
Hello there! How can I help you today?
Expand All @@ -153,13 +167,13 @@ export function AIAssistant({ viewing }: { viewing?: string }) {
}`}
>
<span
className={`inline-block p-2 rounded-lg max-w-[90%] ${
className={`inline-block p-2 rounded-lg max-w-[90%] prose prose-sm ${
m.message_type === "ai"
? "bg-muted"
: "bg-primary text-primary-foreground"
}`}
>
{m.content}
<ReactMarkdown>{m.content}</ReactMarkdown>
</span>
</div>
))}
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/ui/scroll-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ const ScrollArea = React.forwardRef<
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
>(({ className, children, ...props }, ref) => (
<ScrollAreaPrimitive.Root
ref={ref}
className={cn("relative overflow-hidden", className)}
{...props}
>
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]">
<ScrollAreaPrimitive.Viewport
className="h-full w-full rounded-[inherit]"
ref={ref}
>
{children}
</ScrollAreaPrimitive.Viewport>
<ScrollBar />
Expand Down
2 changes: 1 addition & 1 deletion client/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ module.exports = {
},
},
},
plugins: [require("tailwindcss-animate")],
plugins: [require("tailwindcss-animate"), require("@tailwindcss/typography")],
};
46 changes: 46 additions & 0 deletions package-lock.json

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

0 comments on commit bc64c03

Please sign in to comment.