fix: hide form answer messages from chat, stream progress during generation
This commit is contained in:
1 parent
26fefd623a
commit
c76eda9109
2 files changed
+13
-10
No files matched your search
@@ -60,7 +60,7 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
|
||||
const body = await req.json();
|
||||
const { projectId, message, questionAnswers, markFormSubmitted } = body;
|
||||
const { projectId, message, questionAnswers, markFormSubmitted, hideUserMessage } = body;
|
||||
|
||||
// Handle marking a form as submitted (no streaming needed)
|
||||
if (markFormSubmitted && projectId) {
|
||||
@@ -141,6 +141,7 @@ export async function POST(req: NextRequest) {
|
||||
role: "user",
|
||||
content: userMessage,
|
||||
timestamp: new Date(),
|
||||
...(hideUserMessage ? { hidden: true } : {}),
|
||||
};
|
||||
|
||||
await db.collection("projects").updateOne(
|
||||
|
||||
@@ -37,6 +37,7 @@ interface ChatMsg {
|
||||
formSubmitted?: boolean;
|
||||
steps?: ProgressStep[];
|
||||
timestamp: string;
|
||||
hidden?: boolean;
|
||||
}
|
||||
|
||||
interface SnapshotItem {
|
||||
@@ -366,7 +367,7 @@ export default function ChatBuilderPage() {
|
||||
}, [loading, searchParams, messages.length, projectId, router]);
|
||||
useEffect(() => { setTimeout(() => chatEndRef.current?.scrollIntoView({ behavior: "smooth" }), 100); }, [messages, streamingSteps]);
|
||||
|
||||
async function sendMessage(e?: React.FormEvent, overrideMsg?: string) {
|
||||
async function sendMessage(e?: React.FormEvent, overrideMsg?: string, hideUserMsg?: boolean) {
|
||||
e?.preventDefault();
|
||||
const rawMsg = overrideMsg || input;
|
||||
if (!rawMsg.trim() || sending) return;
|
||||
@@ -379,9 +380,11 @@ export default function ChatBuilderPage() {
|
||||
setStreamingBuildReady(false);
|
||||
setStreamingFiles([]);
|
||||
|
||||
// Add user message to chat immediately
|
||||
const tempMsg: ChatMsg = { id: Date.now().toString(), role: "user", content: userMsg, timestamp: new Date().toISOString() };
|
||||
setMessages((prev) => [...prev, tempMsg]);
|
||||
// Add user message to chat immediately (unless hidden, e.g. form answers)
|
||||
if (!hideUserMsg) {
|
||||
const tempMsg: ChatMsg = { id: Date.now().toString(), role: "user", content: userMsg, timestamp: new Date().toISOString() };
|
||||
setMessages((prev) => [...prev, tempMsg]);
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
abortRef.current = controller;
|
||||
@@ -390,7 +393,7 @@ export default function ChatBuilderPage() {
|
||||
const res = await fetch("/api/chat", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ projectId, message: userMsg }),
|
||||
body: JSON.stringify({ projectId, message: userMsg, ...(hideUserMsg ? { hideUserMessage: true } : {}) }),
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
@@ -646,11 +649,10 @@ export default function ChatBuilderPage() {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ projectId, markFormSubmitted: msgId }),
|
||||
});
|
||||
// Send answers as SSE streaming message
|
||||
setAnsweredMessages((prev) => new Set(prev).add(msgId));
|
||||
setCollapsedForms((prev) => new Set(prev).add(msgId));
|
||||
// Use sendMessage flow for the answers
|
||||
await sendMessage(undefined, summary);
|
||||
// Send answers but mark as hidden (don't show as user bubble)
|
||||
await sendMessage(undefined, summary, true);
|
||||
} catch { /* ignore */ }
|
||||
finally { setSubmittingAnswers(null); }
|
||||
}
|
||||
@@ -862,7 +864,7 @@ export default function ChatBuilderPage() {
|
||||
<p className="text-sm text-zinc-500 max-w-md mx-auto">Tell the AI what Android app you want to build.</p>
|
||||
</div>
|
||||
)}
|
||||
{messages.map((msg) => (
|
||||
{messages.filter(m => !m.hidden).map((msg) => (
|
||||
<div key={msg.id} className={`flex ${msg.role === "user" ? "justify-end" : "justify-start"}`}>
|
||||
<div className={`max-w-[90%] md:max-w-[80%] rounded-xl px-4 py-3 text-sm ${msg.role === "user" ? "bg-blue-600 text-white" : "bg-zinc-900 text-zinc-300 border border-zinc-800"}`}>
|
||||
{/* Completed steps (persisted) */}
|
||||
|
||||
Reference in new issue
Block a user