mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-27 19:57:54 -05:00
Add a starting state for chat
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"title": "Frigate Chat",
|
||||
"subtitle": "Your AI assistant for camera management and insights",
|
||||
"placeholder": "Ask anything...",
|
||||
"error": "Something went wrong. Please try again.",
|
||||
"processing": "Processing...",
|
||||
@@ -9,5 +11,14 @@
|
||||
"result": "Result",
|
||||
"arguments": "Arguments:",
|
||||
"response": "Response:",
|
||||
"send": "Send"
|
||||
"send": "Send",
|
||||
"suggested_requests": "Try asking:",
|
||||
"starting_requests": {
|
||||
"show_recent_events": "Show recent events",
|
||||
"show_camera_status": "Show camera status"
|
||||
},
|
||||
"starting_requests_prompts": {
|
||||
"show_recent_events": "Show me the recent events from the last hour",
|
||||
"show_camera_status": "What is the current status of my cameras?"
|
||||
}
|
||||
}
|
||||
|
||||
89
web/src/components/chat/ChatStartingState.tsx
Normal file
89
web/src/components/chat/ChatStartingState.tsx
Normal file
@@ -0,0 +1,89 @@
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { FaArrowUpLong } from "react-icons/fa6";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useState } from "react";
|
||||
import type { StartingRequest } from "@/types/chat";
|
||||
|
||||
type ChatStartingStateProps = {
|
||||
onSendMessage: (message: string) => void;
|
||||
};
|
||||
|
||||
export function ChatStartingState({ onSendMessage }: ChatStartingStateProps) {
|
||||
const { t } = useTranslation(["views/chat"]);
|
||||
const [input, setInput] = useState("");
|
||||
|
||||
const defaultRequests: StartingRequest[] = [
|
||||
{
|
||||
label: t("starting_requests.show_recent_events"),
|
||||
prompt: t("starting_requests_prompts.show_recent_events"),
|
||||
},
|
||||
{
|
||||
label: t("starting_requests.show_camera_status"),
|
||||
prompt: t("starting_requests_prompts.show_camera_status"),
|
||||
},
|
||||
];
|
||||
|
||||
const handleRequestClick = (prompt: string) => {
|
||||
onSendMessage(prompt);
|
||||
};
|
||||
|
||||
const handleSubmit = () => {
|
||||
const text = input.trim();
|
||||
if (!text) return;
|
||||
onSendMessage(text);
|
||||
setInput("");
|
||||
};
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
if (e.key === "Enter" && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleSubmit();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex size-full flex-col items-center justify-center gap-6 p-8">
|
||||
<div className="flex flex-col items-center gap-2">
|
||||
<h1 className="text-4xl font-bold text-foreground">{t("title")}</h1>
|
||||
<p className="text-muted-foreground">{t("subtitle")}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex w-full max-w-2xl flex-col items-center gap-4">
|
||||
<p className="text-center text-sm text-muted-foreground">
|
||||
{t("suggested_requests")}
|
||||
</p>
|
||||
<div className="flex w-full flex-wrap justify-center gap-2">
|
||||
{defaultRequests.map((request, idx) => (
|
||||
<Button
|
||||
key={idx}
|
||||
variant="outline"
|
||||
className="max-w-sm text-sm"
|
||||
onClick={() => handleRequestClick(request.prompt)}
|
||||
>
|
||||
{request.label}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex w-full max-w-2xl flex-row items-center gap-2 rounded-xl bg-secondary p-4">
|
||||
<Input
|
||||
className="h-12 w-full flex-1 border-transparent bg-transparent text-base shadow-none focus-visible:ring-0 dark:bg-transparent"
|
||||
placeholder={t("placeholder")}
|
||||
value={input}
|
||||
onChange={(e) => setInput(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
/>
|
||||
<Button
|
||||
variant="select"
|
||||
className="size-10 shrink-0 rounded-full"
|
||||
disabled={!input.trim()}
|
||||
onClick={handleSubmit}
|
||||
>
|
||||
<FaArrowUpLong size="18" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import axios from "axios";
|
||||
import { ChatEventThumbnailsRow } from "@/components/chat/ChatEventThumbnailsRow";
|
||||
import { MessageBubble } from "@/components/chat/ChatMessage";
|
||||
import { ToolCallBubble } from "@/components/chat/ToolCallBubble";
|
||||
import { ChatStartingState } from "@/components/chat/ChatStartingState";
|
||||
import type { ChatMessage } from "@/types/chat";
|
||||
import {
|
||||
getEventIdsFromSearchObjectsToolCalls,
|
||||
@@ -78,88 +79,101 @@ export default function ChatPage() {
|
||||
return (
|
||||
<div className="flex size-full justify-center p-2">
|
||||
<div className="flex size-full flex-col xl:w-[50%] 3xl:w-[35%]">
|
||||
<div className="scrollbar-container flex min-h-0 w-full flex-1 flex-col gap-2 overflow-y-auto">
|
||||
{messages.map((msg, i) => {
|
||||
const isStreamingPlaceholder =
|
||||
i === messages.length - 1 &&
|
||||
msg.role === "assistant" &&
|
||||
isLoading &&
|
||||
!msg.content?.trim() &&
|
||||
!(msg.toolCalls && msg.toolCalls.length > 0);
|
||||
if (isStreamingPlaceholder) {
|
||||
return <div key={i} />;
|
||||
}
|
||||
return (
|
||||
<div key={i} className="flex flex-col gap-2">
|
||||
{msg.role === "assistant" && msg.toolCalls && (
|
||||
<>
|
||||
{msg.toolCalls.map((tc, tcIdx) => (
|
||||
<div key={tcIdx} className="flex flex-col gap-2">
|
||||
<ToolCallBubble
|
||||
name={tc.name}
|
||||
arguments={tc.arguments}
|
||||
side="left"
|
||||
/>
|
||||
{tc.response && (
|
||||
{messages.length === 0 ? (
|
||||
<ChatStartingState
|
||||
onSendMessage={(message) => {
|
||||
setInput("");
|
||||
submitConversation([{ role: "user", content: message }]);
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<div className="scrollbar-container flex min-h-0 w-full flex-1 flex-col gap-2 overflow-y-auto">
|
||||
{messages.map((msg, i) => {
|
||||
const isStreamingPlaceholder =
|
||||
i === messages.length - 1 &&
|
||||
msg.role === "assistant" &&
|
||||
isLoading &&
|
||||
!msg.content?.trim() &&
|
||||
!(msg.toolCalls && msg.toolCalls.length > 0);
|
||||
if (isStreamingPlaceholder) {
|
||||
return <div key={i} />;
|
||||
}
|
||||
return (
|
||||
<div key={i} className="flex flex-col gap-2">
|
||||
{msg.role === "assistant" && msg.toolCalls && (
|
||||
<>
|
||||
{msg.toolCalls.map((tc, tcIdx) => (
|
||||
<div key={tcIdx} className="flex flex-col gap-2">
|
||||
<ToolCallBubble
|
||||
name={tc.name}
|
||||
response={tc.response}
|
||||
side="right"
|
||||
arguments={tc.arguments}
|
||||
side="left"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
<MessageBubble
|
||||
role={msg.role}
|
||||
content={msg.content}
|
||||
messageIndex={i}
|
||||
onEditSubmit={
|
||||
msg.role === "user" ? handleEditSubmit : undefined
|
||||
}
|
||||
isComplete={
|
||||
msg.role === "user" || !isLoading || i < messages.length - 1
|
||||
}
|
||||
/>
|
||||
{msg.role === "assistant" &&
|
||||
(() => {
|
||||
const isComplete = !isLoading || i < messages.length - 1;
|
||||
if (!isComplete) return null;
|
||||
const events = getEventIdsFromSearchObjectsToolCalls(
|
||||
msg.toolCalls,
|
||||
);
|
||||
return <ChatEventThumbnailsRow events={events} />;
|
||||
})()}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{(() => {
|
||||
const lastMsg = messages[messages.length - 1];
|
||||
const showProcessing =
|
||||
isLoading &&
|
||||
lastMsg?.role === "assistant" &&
|
||||
!lastMsg.content?.trim() &&
|
||||
!(lastMsg.toolCalls && lastMsg.toolCalls.length > 0);
|
||||
return showProcessing ? (
|
||||
<div className="self-start rounded-lg bg-muted px-3 py-2 text-muted-foreground">
|
||||
{t("processing")}
|
||||
</div>
|
||||
) : null;
|
||||
})()}
|
||||
{error && (
|
||||
<p className="self-start text-sm text-destructive" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<ChatEntry
|
||||
input={input}
|
||||
setInput={setInput}
|
||||
sendMessage={sendMessage}
|
||||
isLoading={isLoading}
|
||||
placeholder={t("placeholder")}
|
||||
/>
|
||||
{tc.response && (
|
||||
<ToolCallBubble
|
||||
name={tc.name}
|
||||
response={tc.response}
|
||||
side="right"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
<MessageBubble
|
||||
role={msg.role}
|
||||
content={msg.content}
|
||||
messageIndex={i}
|
||||
onEditSubmit={
|
||||
msg.role === "user" ? handleEditSubmit : undefined
|
||||
}
|
||||
isComplete={
|
||||
msg.role === "user" ||
|
||||
!isLoading ||
|
||||
i < messages.length - 1
|
||||
}
|
||||
/>
|
||||
{msg.role === "assistant" &&
|
||||
(() => {
|
||||
const isComplete = !isLoading || i < messages.length - 1;
|
||||
if (!isComplete) return null;
|
||||
const events = getEventIdsFromSearchObjectsToolCalls(
|
||||
msg.toolCalls,
|
||||
);
|
||||
return <ChatEventThumbnailsRow events={events} />;
|
||||
})()}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{(() => {
|
||||
const lastMsg = messages[messages.length - 1];
|
||||
const showProcessing =
|
||||
isLoading &&
|
||||
lastMsg?.role === "assistant" &&
|
||||
!lastMsg.content?.trim() &&
|
||||
!(lastMsg.toolCalls && lastMsg.toolCalls.length > 0);
|
||||
return showProcessing ? (
|
||||
<div className="self-start rounded-lg bg-muted px-3 py-2 text-muted-foreground">
|
||||
{t("processing")}
|
||||
</div>
|
||||
) : null;
|
||||
})()}
|
||||
{error && (
|
||||
<p className="self-start text-sm text-destructive" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{messages.length > 0 && (
|
||||
<ChatEntry
|
||||
input={input}
|
||||
setInput={setInput}
|
||||
sendMessage={sendMessage}
|
||||
isLoading={isLoading}
|
||||
placeholder={t("placeholder")}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -9,3 +9,8 @@ export type ChatMessage = {
|
||||
content: string;
|
||||
toolCalls?: ToolCall[];
|
||||
};
|
||||
|
||||
export type StartingRequest = {
|
||||
label: string;
|
||||
prompt: string;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user