From c5b011ce8071c4f6eb71fb39dba85c0cee69e189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Thu, 8 Oct 2026 22:15:18 -0400 Subject: [PATCH] Q138: a refused send keeps the draft and says why Co-Authored-By: Claude Opus 5.5 --- src/renderer/src/i18n/zh.ts | 1 + src/renderer/src/pages/ChatPage.tsx | 34 ++++++++++++++----- src/renderer/src/pages/chat/ChatComposer.tsx | 24 +++++++++---- .../src/pages/chat/ChatConversation.tsx | 5 ++- .../src/pages/chat/MentionTextarea.tsx | 4 +-- tests/e2e/ceo-chat.spec.ts | 12 +++++++ 6 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/renderer/src/i18n/zh.ts b/src/renderer/src/i18n/zh.ts index c33877e8..66666db6 100644 --- a/src/renderer/src/i18n/zh.ts +++ b/src/renderer/src/i18n/zh.ts @@ -36,6 +36,7 @@ export const zh: Record = { "Resumes on its own at {time}.": "将于 {time} 自动恢复。", "Waiting on you, or resumes on its own at {time}.": "等待你处理,或于 {time} 自动恢复。", "Waiting on you.": "等待你处理。", + "Not sent: {reason}": "未发送:{reason}", "Resume": "恢复", "Missing this run": "本次运行缺少的能力", "Disconnect": "断开连接", diff --git a/src/renderer/src/pages/ChatPage.tsx b/src/renderer/src/pages/ChatPage.tsx index 897cc7b9..6f2b0acf 100644 --- a/src/renderer/src/pages/ChatPage.tsx +++ b/src/renderer/src/pages/ChatPage.tsx @@ -4,6 +4,7 @@ import { Dialog } from "../shared/Dialog"; import { t } from "../i18n/i18n"; import { ChatSidebar } from "./chat/ChatSidebar"; import { ChatConversation } from "./chat/ChatConversation"; +import type { SendState } from "./chat/ChatComposer"; import { buildConversations, collectParticipants, @@ -20,6 +21,8 @@ function value(form: FormData, key: string): string { return String(form.get(key) ?? "").trim(); } +const IDLE_SEND: SendState = { pending: false, error: null }; + export function ChatPage({ snapshot, actions }: PageProps) { const orgId = activeOrgId(snapshot); const employee = requireActiveEmployee(snapshot); @@ -67,6 +70,8 @@ export function ChatPage({ snapshot, actions }: PageProps) { const [selectedSpaceId, setSelectedSpaceId] = useState(null); const [createOpen, setCreateOpen] = useState(false); const [attachmentFileId, setAttachmentFileId] = useState(""); + // Sends are tracked per conversation, so a late answer lands in the conversation that sent it. + const [sends, setSends] = useState>({}); const composerRef = useRef(null); const firstAvailableSpaceId = conversations[0]?.space.id ?? null; @@ -107,21 +112,31 @@ export function ChatPage({ snapshot, actions }: PageProps) { const sendInChannel = (event: FormEvent) => { event.preventDefault(); - if (!activeSpace) return; - const form = new FormData(event.currentTarget); - const body = value(form, "message"); + if (!activeSpace || sends[activeSpace.id]?.pending) return; + const spaceId = activeSpace.id; + const sentAttachment = attachmentFileId; + const formElement = event.currentTarget; + const body = value(new FormData(formElement), "message"); if (!body) return; - event.currentTarget.reset(); - setAttachmentFileId(""); - void actions.run(() => window.agentCompany.sendMessage({ + setSends((current) => ({ ...current, [spaceId]: { pending: true, error: null } })); + // The draft and attachment stay until the message is saved, so a refused send loses nothing. + actions.run(() => window.agentCompany.sendMessage({ orgId, - spaceId: activeSpace.id, + spaceId, threadId: null, senderType: "employee", senderId: employee.id, bodyMd: body, - attachmentFileIds: attachmentFileId ? [attachmentFileId] : [], - })); + attachmentFileIds: sentAttachment ? [sentAttachment] : [], + })).then(() => { + formElement.reset(); + setAttachmentFileId((current) => (current === sentAttachment ? "" : current)); + setSends((current) => ({ ...current, [spaceId]: { pending: false, error: null } })); + }).catch((error: unknown) => { + const reason = error instanceof Error ? error.message : String(error); + console.error(`[chat] send failed: ${reason}`); + setSends((current) => ({ ...current, [spaceId]: { pending: false, error: reason } })); + }); }; const createSpace = (event: FormEvent) => { @@ -218,6 +233,7 @@ export function ChatPage({ snapshot, actions }: PageProps) { onDeleteMessage={deleteChatMessage} onOpenFile={openFile} onSubmit={sendInChannel} + send={(activeSpaceId ? sends[activeSpaceId] : undefined) ?? IDLE_SEND} composerRef={composerRef} /> diff --git a/src/renderer/src/pages/chat/ChatComposer.tsx b/src/renderer/src/pages/chat/ChatComposer.tsx index 444c2252..c42dce2e 100644 --- a/src/renderer/src/pages/chat/ChatComposer.tsx +++ b/src/renderer/src/pages/chat/ChatComposer.tsx @@ -5,8 +5,15 @@ import { SLASH_COMMANDS } from "./chat-helpers"; import { MentionTextarea } from "./MentionTextarea"; import { t } from "../../i18n/i18n"; +/** Whether a send is in flight, and why the last one was refused. */ +export interface SendState { + pending: boolean; + error: string | null; +} + export function ChatComposer({ disabled, + send, files, agents, employees, @@ -17,6 +24,7 @@ export function ChatComposer({ composerRef, }: { disabled: boolean; + send: SendState; files: FileObjectRecord[]; agents: AgentRecord[]; employees: EmployeeRecord[]; @@ -27,13 +35,15 @@ export function ChatComposer({ composerRef: MutableRefObject; }) { const [slashOpen, setSlashOpen] = useState(false); + // While a send is in flight nothing may change the draft it will clear on success. + const locked = disabled || send.pending; const attachment = files.find((file) => file.id === attachmentFileId) ?? null; const internalRef = useRef(null); const ref = composerRef ?? internalRef; const insert = (text: string) => { const node = ref.current; - if (!node) return; + if (!node || locked) return; const start = node.selectionStart; const end = node.selectionEnd; const before = node.value.slice(0, start); @@ -50,7 +60,7 @@ export function ChatComposer({
- - setSlashOpen((open) => !open)} /> + + setSlashOpen((open) => !open)} disabled={locked} /> {attachment && ( @@ -67,6 +77,7 @@ export function ChatComposer({ +
- {slashOpen && ( + {send.error &&
{t("Not sent: {reason}", { reason: send.error })}
} + {slashOpen && !locked && (
{SLASH_COMMANDS.map((command) => (
{ const node = ref.current; - if (!node) return; + if (!node || disabled) return; const before = node.value.slice(0, anchor.current); const after = node.value.slice(node.selectionStart); node.value = `${before}@${handle} ${after}`; @@ -109,7 +109,7 @@ export function MentionTextarea({ return (
- {query !== null && filtered.length > 0 && ( + {!disabled && query !== null && filtered.length > 0 && (
{ }); await expect(page.locator(".convo-item").first().locator(".convo-ceo-badge")).toBeVisible(); await expect(page.locator(".convo-item").first()).toContainText("How are we tracking against the goal?"); + + // A send the backend refuses keeps the draft and says why, instead of clearing it. + const composer = page.getByRole("textbox", { name: "Message" }); + await composer.fill("/nosuch keep this draft"); + await page.getByRole("button", { name: "Send" }).click(); + await expect(page.getByRole("alert")).toContainText("Not sent: Slash command /nosuch is unavailable."); + await expect(composer).toHaveValue("/nosuch keep this draft"); + await composer.fill("Second thought: what is blocked?"); + await page.getByRole("button", { name: "Send" }).click(); + await expect(chatLog.getByText("Second thought: what is blocked?")).toBeVisible(); + await expect(composer).toHaveValue(""); + await expect(page.getByRole("alert")).toHaveCount(0); } finally { await app.close(); }