Q138: a refused send keeps the draft and says why
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d5bc987e0f
commit
c5b011ce80
6 files changed
+62
-18
No files matched your search
@@ -36,6 +36,7 @@ export const zh: Record<string, string> = {
|
|||||||
"Resumes on its own at {time}.": "将于 {time} 自动恢复。",
|
"Resumes on its own at {time}.": "将于 {time} 自动恢复。",
|
||||||
"Waiting on you, or resumes on its own at {time}.": "等待你处理,或于 {time} 自动恢复。",
|
"Waiting on you, or resumes on its own at {time}.": "等待你处理,或于 {time} 自动恢复。",
|
||||||
"Waiting on you.": "等待你处理。",
|
"Waiting on you.": "等待你处理。",
|
||||||
|
"Not sent: {reason}": "未发送:{reason}",
|
||||||
"Resume": "恢复",
|
"Resume": "恢复",
|
||||||
"Missing this run": "本次运行缺少的能力",
|
"Missing this run": "本次运行缺少的能力",
|
||||||
"Disconnect": "断开连接",
|
"Disconnect": "断开连接",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { Dialog } from "../shared/Dialog";
|
|||||||
import { t } from "../i18n/i18n";
|
import { t } from "../i18n/i18n";
|
||||||
import { ChatSidebar } from "./chat/ChatSidebar";
|
import { ChatSidebar } from "./chat/ChatSidebar";
|
||||||
import { ChatConversation } from "./chat/ChatConversation";
|
import { ChatConversation } from "./chat/ChatConversation";
|
||||||
|
import type { SendState } from "./chat/ChatComposer";
|
||||||
import {
|
import {
|
||||||
buildConversations,
|
buildConversations,
|
||||||
collectParticipants,
|
collectParticipants,
|
||||||
@@ -20,6 +21,8 @@ function value(form: FormData, key: string): string {
|
|||||||
return String(form.get(key) ?? "").trim();
|
return String(form.get(key) ?? "").trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const IDLE_SEND: SendState = { pending: false, error: null };
|
||||||
|
|
||||||
export function ChatPage({ snapshot, actions }: PageProps) {
|
export function ChatPage({ snapshot, actions }: PageProps) {
|
||||||
const orgId = activeOrgId(snapshot);
|
const orgId = activeOrgId(snapshot);
|
||||||
const employee = requireActiveEmployee(snapshot);
|
const employee = requireActiveEmployee(snapshot);
|
||||||
@@ -67,6 +70,8 @@ export function ChatPage({ snapshot, actions }: PageProps) {
|
|||||||
const [selectedSpaceId, setSelectedSpaceId] = useState<string | null>(null);
|
const [selectedSpaceId, setSelectedSpaceId] = useState<string | null>(null);
|
||||||
const [createOpen, setCreateOpen] = useState(false);
|
const [createOpen, setCreateOpen] = useState(false);
|
||||||
const [attachmentFileId, setAttachmentFileId] = useState<string>("");
|
const [attachmentFileId, setAttachmentFileId] = useState<string>("");
|
||||||
|
// Sends are tracked per conversation, so a late answer lands in the conversation that sent it.
|
||||||
|
const [sends, setSends] = useState<Record<string, SendState>>({});
|
||||||
const composerRef = useRef<HTMLTextAreaElement | null>(null);
|
const composerRef = useRef<HTMLTextAreaElement | null>(null);
|
||||||
|
|
||||||
const firstAvailableSpaceId = conversations[0]?.space.id ?? null;
|
const firstAvailableSpaceId = conversations[0]?.space.id ?? null;
|
||||||
@@ -107,21 +112,31 @@ export function ChatPage({ snapshot, actions }: PageProps) {
|
|||||||
|
|
||||||
const sendInChannel = (event: FormEvent<HTMLFormElement>) => {
|
const sendInChannel = (event: FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (!activeSpace) return;
|
if (!activeSpace || sends[activeSpace.id]?.pending) return;
|
||||||
const form = new FormData(event.currentTarget);
|
const spaceId = activeSpace.id;
|
||||||
const body = value(form, "message");
|
const sentAttachment = attachmentFileId;
|
||||||
|
const formElement = event.currentTarget;
|
||||||
|
const body = value(new FormData(formElement), "message");
|
||||||
if (!body) return;
|
if (!body) return;
|
||||||
event.currentTarget.reset();
|
setSends((current) => ({ ...current, [spaceId]: { pending: true, error: null } }));
|
||||||
setAttachmentFileId("");
|
// The draft and attachment stay until the message is saved, so a refused send loses nothing.
|
||||||
void actions.run(() => window.agentCompany.sendMessage({
|
actions.run(() => window.agentCompany.sendMessage({
|
||||||
orgId,
|
orgId,
|
||||||
spaceId: activeSpace.id,
|
spaceId,
|
||||||
threadId: null,
|
threadId: null,
|
||||||
senderType: "employee",
|
senderType: "employee",
|
||||||
senderId: employee.id,
|
senderId: employee.id,
|
||||||
bodyMd: body,
|
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<HTMLFormElement>) => {
|
const createSpace = (event: FormEvent<HTMLFormElement>) => {
|
||||||
@@ -218,6 +233,7 @@ export function ChatPage({ snapshot, actions }: PageProps) {
|
|||||||
onDeleteMessage={deleteChatMessage}
|
onDeleteMessage={deleteChatMessage}
|
||||||
onOpenFile={openFile}
|
onOpenFile={openFile}
|
||||||
onSubmit={sendInChannel}
|
onSubmit={sendInChannel}
|
||||||
|
send={(activeSpaceId ? sends[activeSpaceId] : undefined) ?? IDLE_SEND}
|
||||||
composerRef={composerRef}
|
composerRef={composerRef}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,15 @@ import { SLASH_COMMANDS } from "./chat-helpers";
|
|||||||
import { MentionTextarea } from "./MentionTextarea";
|
import { MentionTextarea } from "./MentionTextarea";
|
||||||
import { t } from "../../i18n/i18n";
|
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({
|
export function ChatComposer({
|
||||||
disabled,
|
disabled,
|
||||||
|
send,
|
||||||
files,
|
files,
|
||||||
agents,
|
agents,
|
||||||
employees,
|
employees,
|
||||||
@@ -17,6 +24,7 @@ export function ChatComposer({
|
|||||||
composerRef,
|
composerRef,
|
||||||
}: {
|
}: {
|
||||||
disabled: boolean;
|
disabled: boolean;
|
||||||
|
send: SendState;
|
||||||
files: FileObjectRecord[];
|
files: FileObjectRecord[];
|
||||||
agents: AgentRecord[];
|
agents: AgentRecord[];
|
||||||
employees: EmployeeRecord[];
|
employees: EmployeeRecord[];
|
||||||
@@ -27,13 +35,15 @@ export function ChatComposer({
|
|||||||
composerRef: MutableRefObject<HTMLTextAreaElement | null>;
|
composerRef: MutableRefObject<HTMLTextAreaElement | null>;
|
||||||
}) {
|
}) {
|
||||||
const [slashOpen, setSlashOpen] = useState(false);
|
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 attachment = files.find((file) => file.id === attachmentFileId) ?? null;
|
||||||
const internalRef = useRef<HTMLTextAreaElement | null>(null);
|
const internalRef = useRef<HTMLTextAreaElement | null>(null);
|
||||||
const ref = composerRef ?? internalRef;
|
const ref = composerRef ?? internalRef;
|
||||||
|
|
||||||
const insert = (text: string) => {
|
const insert = (text: string) => {
|
||||||
const node = ref.current;
|
const node = ref.current;
|
||||||
if (!node) return;
|
if (!node || locked) return;
|
||||||
const start = node.selectionStart;
|
const start = node.selectionStart;
|
||||||
const end = node.selectionEnd;
|
const end = node.selectionEnd;
|
||||||
const before = node.value.slice(0, start);
|
const before = node.value.slice(0, start);
|
||||||
@@ -50,7 +60,7 @@ export function ChatComposer({
|
|||||||
<MentionTextarea
|
<MentionTextarea
|
||||||
name="message"
|
name="message"
|
||||||
rows={2}
|
rows={2}
|
||||||
disabled={disabled}
|
disabled={locked}
|
||||||
ariaLabel={t("Message")}
|
ariaLabel={t("Message")}
|
||||||
agents={agents}
|
agents={agents}
|
||||||
employees={employees}
|
employees={employees}
|
||||||
@@ -58,8 +68,8 @@ export function ChatComposer({
|
|||||||
style={{ minHeight: 38, maxHeight: "40vh", resize: "vertical" }}
|
style={{ minHeight: 38, maxHeight: "40vh", resize: "vertical" }}
|
||||||
/>
|
/>
|
||||||
<div className="row" style={{ paddingTop: 6, borderTop: "1px solid var(--border-soft)", gap: 4 }}>
|
<div className="row" style={{ paddingTop: 6, borderTop: "1px solid var(--border-soft)", gap: 4 }}>
|
||||||
<IconButton icon="attach" label={t("Attach file")} onClick={onAttach} disabled={disabled} />
|
<IconButton icon="attach" label={t("Attach file")} onClick={onAttach} disabled={locked} />
|
||||||
<IconButton icon="sparkle" label={t("Commands")} onClick={() => setSlashOpen((open) => !open)} />
|
<IconButton icon="sparkle" label={t("Commands")} onClick={() => setSlashOpen((open) => !open)} disabled={locked} />
|
||||||
{attachment && (
|
{attachment && (
|
||||||
<span className="pill" style={{ maxWidth: 220 }}>
|
<span className="pill" style={{ maxWidth: 220 }}>
|
||||||
<Icon name="file" size={14} />
|
<Icon name="file" size={14} />
|
||||||
@@ -67,6 +77,7 @@ export function ChatComposer({
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
aria-label={t("Remove attachment")}
|
aria-label={t("Remove attachment")}
|
||||||
|
disabled={locked}
|
||||||
onClick={() => onAttachmentChange("")}
|
onClick={() => onAttachmentChange("")}
|
||||||
style={{ display: "inline-flex", border: 0, background: "transparent", padding: 0, color: "var(--text-3)", cursor: "pointer" }}
|
style={{ display: "inline-flex", border: 0, background: "transparent", padding: 0, color: "var(--text-3)", cursor: "pointer" }}
|
||||||
>
|
>
|
||||||
@@ -76,10 +87,11 @@ export function ChatComposer({
|
|||||||
)}
|
)}
|
||||||
<div style={{ marginLeft: "auto", display: "flex", alignItems: "center", gap: 6 }}>
|
<div style={{ marginLeft: "auto", display: "flex", alignItems: "center", gap: 6 }}>
|
||||||
<span className="mono" style={{ fontSize: 12, color: "var(--text-3)" }}>Ctrl+Enter</span>
|
<span className="mono" style={{ fontSize: 12, color: "var(--text-3)" }}>Ctrl+Enter</span>
|
||||||
<Button tone="primary" size="small" type="submit" disabled={disabled}><Icon name="send" size={12} /> {t("Send")}</Button>
|
<Button tone="primary" size="small" type="submit" disabled={locked}><Icon name="send" size={12} /> {t("Send")}</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{slashOpen && (
|
{send.error && <div className="form-error" role="alert">{t("Not sent: {reason}", { reason: send.error })}</div>}
|
||||||
|
{slashOpen && !locked && (
|
||||||
<div style={{ marginTop: 8, display: "flex", flexWrap: "wrap", gap: 6 }}>
|
<div style={{ marginTop: 8, display: "flex", flexWrap: "wrap", gap: 6 }}>
|
||||||
{SLASH_COMMANDS.map((command) => (
|
{SLASH_COMMANDS.map((command) => (
|
||||||
<button
|
<button
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import type {
|
|||||||
ChatSlashInvocationRecord,
|
ChatSlashInvocationRecord,
|
||||||
FileObjectRecord,
|
FileObjectRecord,
|
||||||
} from "../../../../shared/domain";
|
} from "../../../../shared/domain";
|
||||||
import { ChatComposer } from "./ChatComposer";
|
import { ChatComposer, type SendState } from "./ChatComposer";
|
||||||
import { ChatTimeline } from "./ChatTimeline";
|
import { ChatTimeline } from "./ChatTimeline";
|
||||||
import type { MessageContext } from "./chat-helpers";
|
import type { MessageContext } from "./chat-helpers";
|
||||||
import { t } from "../../i18n/i18n";
|
import { t } from "../../i18n/i18n";
|
||||||
@@ -34,6 +34,7 @@ export function ChatConversation({
|
|||||||
onDeleteMessage,
|
onDeleteMessage,
|
||||||
onOpenFile,
|
onOpenFile,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
|
send,
|
||||||
composerRef,
|
composerRef,
|
||||||
}: {
|
}: {
|
||||||
space: ChatSpaceRecord | null;
|
space: ChatSpaceRecord | null;
|
||||||
@@ -55,6 +56,7 @@ export function ChatConversation({
|
|||||||
onDeleteMessage: (messageId: string) => void;
|
onDeleteMessage: (messageId: string) => void;
|
||||||
onOpenFile: (fileId: string) => void;
|
onOpenFile: (fileId: string) => void;
|
||||||
onSubmit: (event: FormEvent<HTMLFormElement>) => void;
|
onSubmit: (event: FormEvent<HTMLFormElement>) => void;
|
||||||
|
send: SendState;
|
||||||
composerRef: MutableRefObject<HTMLTextAreaElement | null>;
|
composerRef: MutableRefObject<HTMLTextAreaElement | null>;
|
||||||
}) {
|
}) {
|
||||||
const [query, setQuery] = useState("");
|
const [query, setQuery] = useState("");
|
||||||
@@ -135,6 +137,7 @@ export function ChatConversation({
|
|||||||
</div>
|
</div>
|
||||||
<ChatComposer
|
<ChatComposer
|
||||||
disabled={!space}
|
disabled={!space}
|
||||||
|
send={send}
|
||||||
files={files}
|
files={files}
|
||||||
agents={context.agents}
|
agents={context.agents}
|
||||||
employees={context.employees}
|
employees={context.employees}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ export function MentionTextarea({
|
|||||||
|
|
||||||
const insertMention = (handle: string) => {
|
const insertMention = (handle: string) => {
|
||||||
const node = ref.current;
|
const node = ref.current;
|
||||||
if (!node) return;
|
if (!node || disabled) return;
|
||||||
const before = node.value.slice(0, anchor.current);
|
const before = node.value.slice(0, anchor.current);
|
||||||
const after = node.value.slice(node.selectionStart);
|
const after = node.value.slice(node.selectionStart);
|
||||||
node.value = `${before}@${handle} ${after}`;
|
node.value = `${before}@${handle} ${after}`;
|
||||||
@@ -109,7 +109,7 @@ export function MentionTextarea({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ position: "relative" }}>
|
<div style={{ position: "relative" }}>
|
||||||
{query !== null && filtered.length > 0 && (
|
{!disabled && query !== null && filtered.length > 0 && (
|
||||||
<div style={{
|
<div style={{
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
bottom: "100%",
|
bottom: "100%",
|
||||||
|
|||||||
@@ -96,6 +96,18 @@ test("the pinned CEO conversation is the Messages front door", async () => {
|
|||||||
});
|
});
|
||||||
await expect(page.locator(".convo-item").first().locator(".convo-ceo-badge")).toBeVisible();
|
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?");
|
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 {
|
} finally {
|
||||||
await app.close();
|
await app.close();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user