Skip to content

Commit 9969500

Browse files
roboclaw-botVACInc
andcommitted
fix(ui): keep steady composer edits local
Co-authored-by: VACInc <[email protected]> Worked on by: - @VACInc
1 parent 1c9770a commit 9969500

5 files changed

Lines changed: 165 additions & 2 deletions

File tree

‎ui/src/e2e/chat-stream-runtime-budgets.e2e.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const MAX_BURST_HOST_UPDATES = 180;
4040
// frame callback. The queue guarantees this for every stream-driven update;
4141
// only rare timer-driven strays (poll controllers) fall outside frames.
4242
const FRAME_SCHEDULED_MIN_RATIO = 0.9;
43+
// Unrelated page timers can contribute one host update after the probe resets;
44+
// ordinary characters must not invalidate the pane themselves.
45+
const MAX_STEADY_COMPOSER_HOST_UPDATES = 1;
4346

4447
// Shipped live-tool ceiling: ui/src/pages/chat/tool-stream.ts TOOL_STREAM_LIMIT.
4548
const TOOL_STREAM_LIMIT_CONTRACT = 50;
@@ -610,6 +613,47 @@ suite.define(() => {
610613
);
611614
});
612615

616+
it("keeps steady-state composer edits local to a long transcript", async () => {
617+
await suite.withPage(
618+
{
619+
locale: "en-US",
620+
serviceWorkers: "block",
621+
viewport: { height: 900, width: 1280 },
622+
},
623+
async ({ page }) => {
624+
const gateway = await installMockGateway(page, {
625+
historyMessages: buildLongTranscriptFixture(LONG_TRANSCRIPT_MESSAGE_COUNT),
626+
});
627+
await page.goto(`${suite.server.baseUrl}chat`);
628+
await gateway.waitForRequest("chat.startup");
629+
await page.locator(".chat-thread-inner").getByText("LONG-TAIL-SENTINEL").waitFor();
630+
631+
const composer = page.locator(".agent-chat__composer-combobox textarea");
632+
await composer.fill("seed");
633+
await page.getByRole("button", { name: "Send message" }).waitFor();
634+
await installRenderProbe(page);
635+
await resetRenderProbe(page);
636+
637+
const suffix = " ordinary typing without commands";
638+
await composer.pressSequentially(suffix);
639+
expect(await composer.inputValue()).toBe(`seed${suffix}`);
640+
const probe = await readRenderProbe(page);
641+
642+
expect(probe.hostUpdates).toBeLessThanOrEqual(MAX_STEADY_COMPOSER_HOST_UPDATES);
643+
644+
const send = page.locator(".chat-send-btn--send");
645+
await composer.fill("");
646+
await expect.poll(() => send.isDisabled()).toBe(true);
647+
648+
await composer.fill("new draft");
649+
await expect.poll(() => send.isDisabled()).toBe(false);
650+
651+
await composer.fill("مرحبا");
652+
await expect.poll(() => composer.getAttribute("dir")).toBe("rtl");
653+
},
654+
);
655+
});
656+
613657
it("stays quiet while idle after a settled stream", async () => {
614658
await suite.withPage(
615659
{

‎ui/src/pages/chat/chat-composer.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ describe("renderChatComposer controls", () => {
195195
expect(textarea.matches(":placeholder-shown")).toBe(true);
196196
});
197197

198+
it("clears a live whitespace draft when the last rendered draft was already empty", () => {
199+
let currentDraft = " \n ";
200+
const onDraftChange = vi.fn((next: string) => {
201+
currentDraft = next;
202+
});
203+
const { container } = renderComposer({
204+
draft: "",
205+
getDraft: () => currentDraft,
206+
onDraftChange,
207+
});
208+
const textarea = container.querySelector<HTMLTextAreaElement>("textarea");
209+
if (!textarea) {
210+
throw new Error("expected composer textarea");
211+
}
212+
213+
textarea.value = currentDraft;
214+
textarea.dispatchEvent(new FocusEvent("blur", { bubbles: true }));
215+
216+
expect(currentDraft).toBe("");
217+
expect(textarea.value).toBe("");
218+
expect(onDraftChange).toHaveBeenLastCalledWith("");
219+
expect(textarea.matches(":placeholder-shown")).toBe(true);
220+
});
221+
198222
it.each([true, false])(
199223
"keeps the unsaved row edit visible and cancellable (source retained: %s)",
200224
(retained) => {

‎ui/src/pages/chat/chat-view.test.ts‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3877,6 +3877,90 @@ describe("chat voice controls", () => {
38773877
});
38783878
});
38793879

3880+
describe("chat composer render invalidation", () => {
3881+
it("keeps steady ordinary edits and direction changes local", () => {
3882+
const container = document.createElement("div");
3883+
let draft = "a";
3884+
const onDraftChange = vi.fn((next: string) => {
3885+
draft = next;
3886+
});
3887+
let props = createChatProps({
3888+
draft,
3889+
getDraft: () => draft,
3890+
onDraftChange,
3891+
});
3892+
const onRequestUpdate = vi.fn(() => {
3893+
props = { ...props, draft, getDraft: () => draft };
3894+
render(renderChat(props), container);
3895+
});
3896+
props = { ...props, onRequestUpdate };
3897+
render(renderChat(props), container);
3898+
3899+
const textarea = getComposerTextarea(container);
3900+
onRequestUpdate.mockClear();
3901+
vi.mocked(chatThread.buildCachedChatItems).mockClear();
3902+
3903+
textarea.value = "ab";
3904+
textarea.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }));
3905+
textarea.value = "abc";
3906+
textarea.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }));
3907+
3908+
expect(draft).toBe("abc");
3909+
expect(onRequestUpdate).not.toHaveBeenCalled();
3910+
expect(chatThread.buildCachedChatItems).not.toHaveBeenCalled();
3911+
3912+
textarea.value = "مرحبا";
3913+
textarea.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }));
3914+
3915+
expect(onRequestUpdate).not.toHaveBeenCalled();
3916+
expect(chatThread.buildCachedChatItems).not.toHaveBeenCalled();
3917+
expect(textarea.dir).toBe("rtl");
3918+
3919+
render(renderChat(props), container);
3920+
expect(getComposerTextarea(container).dir).toBe("rtl");
3921+
});
3922+
3923+
it("invalidates when offline slash eligibility changes", () => {
3924+
const container = document.createElement("div");
3925+
let draft = "queue this offline";
3926+
const onDraftChange = vi.fn((next: string) => {
3927+
draft = next;
3928+
});
3929+
let props = createChatProps({
3930+
connected: false,
3931+
draft,
3932+
getDraft: () => draft,
3933+
onDraftChange,
3934+
});
3935+
const onRequestUpdate = vi.fn(() => {
3936+
props = { ...props, draft, getDraft: () => draft };
3937+
render(renderChat(props), container);
3938+
});
3939+
props = { ...props, onRequestUpdate };
3940+
render(renderChat(props), container);
3941+
3942+
const sendButton = () =>
3943+
container.querySelector<HTMLButtonElement>('button[aria-label="Send message"]');
3944+
expect(sendButton()?.disabled).toBe(false);
3945+
onRequestUpdate.mockClear();
3946+
3947+
let textarea = getComposerTextarea(container);
3948+
textarea.value = "/status";
3949+
textarea.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }));
3950+
3951+
expect(onRequestUpdate).toHaveBeenCalled();
3952+
expect(sendButton()?.disabled).toBe(true);
3953+
onRequestUpdate.mockClear();
3954+
3955+
textarea = getComposerTextarea(container);
3956+
textarea.value = "queue this instead";
3957+
textarea.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }));
3958+
3959+
expect(onRequestUpdate).toHaveBeenCalled();
3960+
expect(sendButton()?.disabled).toBe(false);
3961+
});
3962+
});
3963+
38803964
describe("chat composer IME composition", () => {
38813965
it("switches to send on the first composing character without committing the draft", () => {
38823966
const onDraftChange = vi.fn();

‎ui/src/pages/chat/components/chat-composer-state.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export function composerDraftKey(
8888
}
8989

9090
export function commitComposerDraft(props: ChatComposerProps, value: string): void {
91-
if (props.getDraft?.() === value || props.draft === value) {
91+
const currentDraft = props.getDraft ? props.getDraft() : props.draft;
92+
if (currentDraft === value) {
9293
return;
9394
}
9495
props.onDraftChange(value);

‎ui/src/pages/chat/components/chat-composer.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { t } from "../../../i18n/index.ts";
1111
import type { SlashCommandDef } from "../../../lib/chat/commands.ts";
1212
import { resolveThinkingCommandArgOptionsForSession } from "../../../lib/chat/thinking.ts";
1313
import { areUiSessionKeysEquivalent } from "../../../lib/sessions/session-key.ts";
14+
import { detectTextDirection } from "../../../lib/text-direction.ts";
1415
import { ComposerDictationController, insertComposerDictation } from "../composer-dictation.ts";
1516
import { normalizeChatComposerDraft } from "../composer-draft.ts";
1617
import { ComposerMicrophonePicker } from "../composer-microphone-picker.ts";
@@ -293,6 +294,7 @@ export function renderChatComposer(props: ChatComposerProps) {
293294
!(state.skillMenuOpen && state.skillCommandRefreshPending) &&
294295
(props.getPendingAttachmentReads?.() ?? props.pendingAttachmentReads ?? 0) === 0 &&
295296
(props.connected || !draft.trimStart().startsWith("/"));
297+
const renderedDraftCanSubmit = canSubmitDraft(visibleDraft);
296298

297299
const syncComposerDraftAfterSend = (target: HTMLTextAreaElement | null) => {
298300
const submittedDraft = target?.value ?? props.getDraft?.() ?? props.draft;
@@ -330,12 +332,20 @@ export function renderChatComposer(props: ChatComposerProps) {
330332

331333
const syncComposerValue = (target: HTMLTextAreaElement) => {
332334
adjustTextareaHeight(target);
335+
target.dir = detectTextDirection(target.value);
333336
commitComposerDraft(props, target.value);
334337
if (!goalComposer.active) {
335338
updateSlashMenu(target.value, state, slashMenuHost, requestUpdate);
336339
updateSkillMenu(target.value, target.selectionStart, state, skillMenuHost, requestUpdate);
337340
}
338-
requestUpdate();
341+
// The textarea owns ordinary edits; only redraw the pane when surrounding
342+
// controls change. Slash and skill menus invalidate their own presentation.
343+
if (
344+
Boolean(target.value.trim()) !== Boolean(visibleDraft.trim()) ||
345+
canSubmitDraft(target.value) !== renderedDraftCanSubmit
346+
) {
347+
requestUpdate();
348+
}
339349
};
340350
const handleBeforeInput = (event: InputEvent) => {
341351
const target = event.target;

0 commit comments

Comments
 (0)