Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(automations): Enter that picks an IME candidate no longer sends the new automation request

hadan897749db3dd1a704
diff --git a/src/components/automations/NewAutomation.ime.test.ts b/src/components/automations/NewAutomation.ime.test.ts
new file mode 100644
index 0000000000..b79b16beff
--- /dev/null
+++ b/src/components/automations/NewAutomation.ime.test.ts
@@ -0,0 +1,75 @@
+// @vitest-environment happy-dom
+import { createElement } from "react";
+import { flushSync } from "react-dom";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import type { Bot } from "@/state/store";
+
+const fixture = vi.hoisted(() => ({ dispatch: vi.fn() }));
+vi.mock("@/state/store", async (importOriginal) => {
+ const original = await importOriginal<typeof import("@/state/store")>();
+ return {
+ ...original,
+ useStore: () => ({ state: { ...original.initialState, bots: [bot], selectedId: bot.id }, dispatch: fixture.dispatch }),
+ };
+});
+
+import { NewAutomation } from "./NewAutomation";
+
+const bot: Bot = {
+ id: "ime-bot", threadId: "ime-thread", name: "Atlas", title: "", description: "",
+ color: "blue", notifications: false, unread: false, busy: false, messages: [], section: "",
+ modelSelection: { instanceId: "claude", model: "fake" },
+};
+
+let root: Root;
+let host: HTMLDivElement;
+beforeEach(() => {
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+ vi.clearAllMocks();
+});
+
+function open() {
+ const onClose = vi.fn();
+ flushSync(() => root.render(createElement(NewAutomation, { onClose, onSetUpYourself: vi.fn() })));
+ return { onClose, box: document.querySelector<HTMLTextAreaElement>('[role="dialog"] textarea')! };
+}
+
+function type(box: HTMLTextAreaElement, value: string) {
+ const setter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!;
+ flushSync(() => {
+ setter.call(box, value);
+ box.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+}
+
+function press(box: HTMLTextAreaElement, init: KeyboardEventInit = {}) {
+ flushSync(() => box.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...init })));
+}
+
+describe("New automation box with an input method", () => {
+ it.each([
+ { name: "composition", init: { isComposing: true } },
+ { name: "IME key code", init: { keyCode: 229 } },
+ ])("does not send a half-typed request on the Enter that belongs to the $name", ({ init }) => {
+ const { onClose, box } = open();
+ type(box, "每天早上 jiudian");
+ press(box, init);
+ expect(fixture.dispatch).not.toHaveBeenCalled();
+ expect(onClose).not.toHaveBeenCalled();
+ });
+
+ it("sends on the Enter after the text is committed", () => {
+ const { onClose, box } = open();
+ type(box, "每天早上九点总结邮件");
+ press(box);
+ expect(fixture.dispatch).toHaveBeenCalledWith({ type: "send", botId: bot.id, text: "每天早上九点总结邮件", threadId: bot.threadId });
+ expect(onClose).toHaveBeenCalledOnce();
+ });
+});
diff --git a/src/components/automations/NewAutomation.tsx b/src/components/automations/NewAutomation.tsx
index 4d82aab8a6..35d9d87788 100644
--- a/src/components/automations/NewAutomation.tsx
+++ b/src/components/automations/NewAutomation.tsx
@@ -4,6 +4,7 @@ import { AppWindow, CalendarClock, Send, Share2, X } from "lucide-react";
import { useModalDialog } from "@/hooks/use-modal-dialog";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
+import { imeComposing } from "@/lib/send-key";
import { useStore, type Bot } from "@/state/store";
import { Select } from "../Select";
@@ -71,7 +72,7 @@ export function NewAutomation({ initialText = "", onClose, onSetUpYourself }: {
) : (
<>
<textarea autoFocus value={text} onChange={(event) => setText(event.target.value)} rows={3} aria-label={t("automations.newBox.title")} placeholder={t("automations.newBox.placeholder")}
- onKeyDown={(event) => { if (event.key === "Enter" && !event.shiftKey) { event.preventDefault(); send(); } }}
+ onKeyDown={(event) => { if (event.key === "Enter" && !event.shiftKey && !imeComposing(event.nativeEvent)) { event.preventDefault(); send(); } }}
className="mt-4 w-full resize-none rounded-xl border border-hairline/60 bg-inset px-3.5 py-3 text-[14px] leading-relaxed text-ink outline-none placeholder:text-ink-tertiary focus:border-accent/70" />
{bots.length === 0 && <p className="mt-2 text-[13px] text-warning">{t("automations.newBox.noBots")}</p>}
{bots.length > 1 && (