story-kit: merge 155_story_queue_messages_while_agent_is_busy

This commit is contained in:
Dave
2026-02-24 16:29:05 +00:00
parent f0000e0436
commit bb1c3ac97c
2 changed files with 420 additions and 46 deletions

View File

@@ -90,6 +90,11 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
const [agentConfigVersion, setAgentConfigVersion] = useState(0);
const [needsOnboarding, setNeedsOnboarding] = useState(false);
const onboardingTriggeredRef = useRef(false);
const [queuedMessage, setQueuedMessage] = useState<string | null>(null);
// Ref so stale WebSocket callbacks can read the current queued message
const queuedMessageRef = useRef<string | null>(null);
// Trigger state: set to a message string to fire auto-send after loading ends
const [pendingAutoSend, setPendingAutoSend] = useState<string | null>(null);
const wsRef = useRef<ChatWebSocket | null>(null);
const messagesEndRef = useRef<HTMLDivElement>(null);
@@ -204,6 +209,12 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
if (last?.role === "assistant" && !last.tool_calls) {
setLoading(false);
setActivityStatus(null);
if (queuedMessageRef.current) {
const msg = queuedMessageRef.current;
queuedMessageRef.current = null;
setQueuedMessage(null);
setPendingAutoSend(msg);
}
}
},
onSessionId: (sessionId) => {
@@ -213,6 +224,12 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
console.error("WebSocket error:", message);
setLoading(false);
setActivityStatus(null);
if (queuedMessageRef.current) {
const msg = queuedMessageRef.current;
queuedMessageRef.current = null;
setQueuedMessage(null);
setPendingAutoSend(msg);
}
},
onPipelineState: (state) => {
setPipeline(state);
@@ -295,6 +312,15 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
inputRef.current?.focus();
}, []);
// Auto-send queued message when loading ends
useEffect(() => {
if (pendingAutoSend) {
const msg = pendingAutoSend;
setPendingAutoSend(null);
sendMessage(msg);
}
}, [pendingAutoSend]);
useEffect(() => {
const handleResize = () =>
setIsNarrowScreen(window.innerWidth < NARROW_BREAKPOINT);
@@ -303,6 +329,9 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
}, []);
const cancelGeneration = async () => {
// Discard any queued message — do not auto-send after cancel
queuedMessageRef.current = null;
setQueuedMessage(null);
try {
wsRef.current?.cancel();
await api.cancelChat();
@@ -324,7 +353,17 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
const sendMessage = async (messageOverride?: string) => {
const messageToSend = messageOverride ?? input;
if (!messageToSend.trim() || loading) return;
if (!messageToSend.trim()) return;
// Agent is busy — queue the message instead of dropping it
if (loading) {
queuedMessageRef.current = messageToSend;
setQueuedMessage(messageToSend);
if (!messageOverride || messageOverride === input) {
setInput("");
}
return;
}
const isClaudeCode = model === "claude-code-pty";
if (!isClaudeCode && model.startsWith("claude-")) {
@@ -842,59 +881,154 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
maxWidth: "768px",
width: "100%",
display: "flex",
flexDirection: "column",
gap: "8px",
alignItems: "center",
}}
>
<textarea
ref={inputRef}
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
}}
placeholder="Send a message..."
rows={1}
{/* Queued message indicator */}
{queuedMessage && (
<div
data-testid="queued-message-indicator"
style={{
display: "flex",
alignItems: "center",
gap: "8px",
padding: "8px 12px",
background: "#1e1e1e",
border: "1px solid #3a3a3a",
borderRadius: "12px",
fontSize: "0.875rem",
}}
>
<span
style={{
color: "#666",
flexShrink: 0,
fontSize: "0.7rem",
fontWeight: 700,
letterSpacing: "0.05em",
textTransform: "uppercase",
}}
>
Queued
</span>
<span
style={{
color: "#888",
flex: 1,
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
}}
>
{queuedMessage}
</span>
<button
type="button"
title="Edit queued message"
onClick={() => {
setInput(queuedMessage);
queuedMessageRef.current = null;
setQueuedMessage(null);
inputRef.current?.focus();
}}
style={{
background: "none",
border: "none",
color: "#666",
cursor: "pointer",
padding: "2px 6px",
fontSize: "0.8rem",
flexShrink: 0,
borderRadius: "4px",
}}
>
Edit
</button>
<button
type="button"
title="Cancel queued message"
onClick={() => {
queuedMessageRef.current = null;
setQueuedMessage(null);
}}
style={{
background: "none",
border: "none",
color: "#666",
cursor: "pointer",
padding: "2px 4px",
fontSize: "0.875rem",
flexShrink: 0,
borderRadius: "4px",
}}
>
</button>
</div>
)}
{/* Input row */}
<div
style={{
flex: 1,
padding: "14px 20px",
borderRadius: "24px",
border: "1px solid #333",
outline: "none",
fontSize: "1rem",
fontWeight: "500",
background: "#2f2f2f",
color: "#ececec",
boxShadow: "0 2px 6px rgba(0,0,0,0.02)",
resize: "none",
overflowY: "auto",
fontFamily: "inherit",
}}
/>
<button
type="button"
onClick={loading ? cancelGeneration : () => sendMessage()}
disabled={!loading && !input.trim()}
style={{
background: "#ececec",
color: "black",
border: "none",
borderRadius: "50%",
width: "32px",
height: "32px",
display: "flex",
gap: "8px",
alignItems: "center",
justifyContent: "center",
cursor: "pointer",
opacity: !loading && !input.trim() ? 0.5 : 1,
flexShrink: 0,
}}
>
{loading ? "■" : "↑"}
</button>
<textarea
ref={inputRef}
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
}}
placeholder="Send a message..."
rows={1}
style={{
flex: 1,
padding: "14px 20px",
borderRadius: "24px",
border: "1px solid #333",
outline: "none",
fontSize: "1rem",
fontWeight: "500",
background: "#2f2f2f",
color: "#ececec",
boxShadow: "0 2px 6px rgba(0,0,0,0.02)",
resize: "none",
overflowY: "auto",
fontFamily: "inherit",
}}
/>
<button
type="button"
onClick={
loading && !input.trim()
? cancelGeneration
: () => sendMessage()
}
disabled={!loading && !input.trim()}
style={{
background: "#ececec",
color: "black",
border: "none",
borderRadius: "50%",
width: "32px",
height: "32px",
display: "flex",
alignItems: "center",
justifyContent: "center",
cursor: "pointer",
opacity: !loading && !input.trim() ? 0.5 : 1,
flexShrink: 0,
}}
>
{loading && !input.trim() ? "■" : "↑"}
</button>
</div>
</div>
</div>
</div>