feat: multiple named conversations for the cooking assistant
The general assistant had exactly one conversation per user forever (recipeId null on chat_messages) — no way to start fresh or organize by topic. Adds an ai_conversations table (title, timestamps) and a nullable conversationId FK on chat_messages; the assistant panel gets a conversations menu to create/switch/rename/delete, auto-titling a new conversation from its first question. Per-recipe chat is untouched — each recipe already has one natural thread, so multi-conversation support only applies to the homepage assistant. Pre-existing general messages (no conversationId) aren't migrated into the new model and won't appear in the conversation list. Requires migration 0042 to run against a live DB — not applied in this sandbox (no Docker here); run `pnpm db:migrate`. v0.43.0
This commit is contained in:
@@ -9,7 +9,7 @@ import { MessageCircle, Send, Bot, User, Maximize2, Minimize2 } from "lucide-rea
|
||||
import ReactMarkdown from "react-markdown";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { ChatHistorySearch } from "./chat-history-search";
|
||||
import { ClearChatHistoryButton } from "./clear-chat-history-button";
|
||||
import { ConversationMenu, type Conversation } from "./conversation-menu";
|
||||
|
||||
type Message = {
|
||||
role: "user" | "assistant";
|
||||
@@ -26,18 +26,59 @@ export function CookingAssistantPanel() {
|
||||
const [messages, setMessages] = useState<Message[]>([]);
|
||||
const [input, setInput] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [conversationId, setConversationId] = useState<string | null>(null);
|
||||
const bottomRef = useRef<HTMLDivElement>(null);
|
||||
const historyLoadedRef = useRef(false);
|
||||
|
||||
async function loadMessages(id: string) {
|
||||
try {
|
||||
const res = await fetch(`/api/v1/ai/chat-history?conversationId=${id}&limit=50`);
|
||||
const json = res.ok ? (await res.json()) as { data?: HistoryEntry[] } : null;
|
||||
setMessages(json?.data?.length ? json.data.map((m) => ({ role: m.role, content: m.content })) : []);
|
||||
} catch {
|
||||
setMessages([]);
|
||||
}
|
||||
}
|
||||
|
||||
function handleSelectConversation(id: string) {
|
||||
setConversationId(id);
|
||||
void loadMessages(id);
|
||||
}
|
||||
|
||||
function handleConversationCreated(conversation: Conversation) {
|
||||
setConversationId(conversation.id);
|
||||
setMessages([]);
|
||||
}
|
||||
|
||||
function handleActiveConversationDeleted() {
|
||||
// Fall back to a fresh conversation rather than leaving the panel
|
||||
// pointed at an id that no longer exists.
|
||||
fetch("/api/v1/ai/conversations", { method: "POST" })
|
||||
.then((res) => (res.ok ? res.json() : null))
|
||||
.then((created: Conversation | null) => {
|
||||
if (created) {
|
||||
setConversationId(created.id);
|
||||
setMessages([]);
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || historyLoadedRef.current) return;
|
||||
historyLoadedRef.current = true;
|
||||
fetch("/api/v1/ai/chat-history?scope=general&limit=50")
|
||||
.then((res) => (res.ok ? res.json() : null))
|
||||
.then((json: { data?: HistoryEntry[] } | null) => {
|
||||
if (json?.data?.length) setMessages(json.data.map((m) => ({ role: m.role, content: m.content })));
|
||||
})
|
||||
.catch(() => {});
|
||||
(async () => {
|
||||
const listRes = await fetch("/api/v1/ai/conversations");
|
||||
const list = listRes.ok ? ((await listRes.json()) as { data: Conversation[] }).data : [];
|
||||
const id = list.length > 0
|
||||
? list[0]!.id
|
||||
: await fetch("/api/v1/ai/conversations", { method: "POST" })
|
||||
.then((res) => (res.ok ? res.json() : null))
|
||||
.then((created: Conversation | null) => created?.id ?? null);
|
||||
if (!id) return;
|
||||
setConversationId(id);
|
||||
await loadMessages(id);
|
||||
})().catch(() => {});
|
||||
}, [open]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -59,7 +100,7 @@ export function CookingAssistantPanel() {
|
||||
const res = await fetch("/api/v1/ai/cooking-chat", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ question }),
|
||||
body: JSON.stringify({ question, conversationId: conversationId ?? undefined }),
|
||||
});
|
||||
const data = await res.json() as { answer?: string; error?: string };
|
||||
setMessages((prev) => [
|
||||
@@ -122,7 +163,12 @@ export function CookingAssistantPanel() {
|
||||
{fullscreen ? <Minimize2 className="h-4 w-4" /> : <Maximize2 className="h-4 w-4" />}
|
||||
</button>
|
||||
<ChatHistorySearch />
|
||||
<ClearChatHistoryButton disabled={messages.length === 0} onCleared={() => setMessages([])} />
|
||||
<ConversationMenu
|
||||
activeId={conversationId}
|
||||
onSelect={handleSelectConversation}
|
||||
onCreated={handleConversationCreated}
|
||||
onDeletedActive={handleActiveConversationDeleted}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground text-left">{t("subtitle")}</p>
|
||||
|
||||
Reference in New Issue
Block a user