Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(editor): pressing Esc no longer costs you the message you were writing

esenginef896d0a92fcc
diff --git a/desktop/src/components/AgentDrawer.tsx b/desktop/src/components/AgentDrawer.tsx
index 8d528dcbe..896998910 100644
--- a/desktop/src/components/AgentDrawer.tsx
+++ b/desktop/src/components/AgentDrawer.tsx
@@ -32,7 +32,7 @@ import {
import { useEditorStore } from '@/store/editorStore';
import {
useAgent, sendAgentMessage, stopAgentTurn, confirmAgentCall, resetAgentSession,
- peekEntities, entitiesInInput, effectiveSelection, selectAgentModel, retryAgentTurn,
+ peekEntities, entitiesInInput, effectiveSelection, selectAgentModel, retryAgentTurn, setAgentDraft,
type AgentTurn, type AgentEntry, type AgentToolEntry, type AgentProseEntry,
} from '@/store/AgentStore';
import type { ConfirmAnswer } from '../../electron/agent/types';
@@ -753,9 +753,10 @@ function mentionMatches(query: string): Mention[] {
return [...entities, ...assets];
}
-function Compose() {
+function Compose({ autoFocus }: { autoFocus?: boolean }) {
const status = useAgent((s) => s.status);
- const [draft, setDraft] = useState('');
+ const draft = useAgent((s) => s.draft);
+ const setDraft = setAgentDraft;
const ref = useRef<HTMLTextAreaElement>(null);
const busy = status.phase !== 'idle';
// `@` opens a picker over the scene tree. Read at open time rather than
@@ -784,22 +785,23 @@ function Compose() {
el?.focus();
const pos = at + name.length + 2;
el?.setSelectionRange(pos, pos);
- autosize();
});
};
- const autosize = () => {
+ // Driven by the text rather than by the keystroke, because the text now
+ // arrives from elsewhere too: a draft restored when the drawer reopens has to
+ // come back the height it was, not one line tall with the rest scrolled away.
+ useLayoutEffect(() => {
const el = ref.current;
if (!el) return;
el.style.height = 'auto';
el.style.height = `${Math.min(112, el.scrollHeight)}px`;
- };
+ }, [draft]);
const submit = () => {
const text = draft.trim();
if (!text) return;
setDraft('');
- requestAnimationFrame(autosize);
void sendAgentMessage(text);
};
@@ -841,11 +843,14 @@ function Compose() {
ref={ref}
rows={1}
value={draft}
+ // Summoned, it is summoned to be typed into. Docked, it is a panel
+ // that happens to be open and stealing focus from the viewport would
+ // be the wrong read of why it is on screen.
+ autoFocus={autoFocus}
placeholder={busy ? t('agent.compose.busy') : t('agent.compose')}
aria-label={t('agent.compose')}
onChange={(e) => {
setDraft(e.target.value);
- autosize();
openMention(e.target.value, e.target.selectionStart ?? 0);
}}
onBlur={() => setMention(null)}
@@ -995,7 +1000,7 @@ export function AgentPanel({ docked }: { docked?: boolean }) {
</button>
)}
{status.error && <div className="ag-banner">{status.error}</div>}
- <Compose />
+ <Compose autoFocus={!docked} />
</div>
);
}
diff --git a/desktop/src/store/AgentStore.ts b/desktop/src/store/AgentStore.ts
index e3038d3eb..b72f6ce0f 100644
--- a/desktop/src/store/AgentStore.ts
+++ b/desktop/src/store/AgentStore.ts
@@ -124,6 +124,15 @@ interface AgentState {
peeked: readonly number[];
/** Typed while a turn was running, waiting for it to end. See sendAgentMessage. */
queued: readonly string[];
+ /**
+ * What is typed and not yet sent.
+ *
+ * Here rather than in the composer because there are TWO composers — the
+ * drawer's and the docked panel's — and the drawer's unmounts every time it
+ * closes. Half a message should not be the price of pressing Esc, and the two
+ * should not disagree about what you were writing.
+ */
+ draft: string;
/** The turn whose checkpoint bar has been answered — see AgentCheckpoint. */
checkpointDone: number | null;
/** What the user picked, or null for "whichever provider has a key". */
@@ -132,11 +141,13 @@ interface AgentState {
export const useAgent = create<AgentState>(() => ({
status: IDLE, turns: [], driving: false, peeked: [], queued: [], checkpointDone: null,
- selection: loadSelection(),
+ draft: '', selection: loadSelection(),
}));
export const peekEntities = (ids: readonly number[]): void => useAgent.setState({ peeked: ids });
+export const setAgentDraft = (draft: string): void => useAgent.setState({ draft });
+
// ── Which provider and model the next conversation runs on ──────────────────
// Persisted here rather than as a registered setting: it is picked from the
// composer, and a settings row for it would be a second control for one choice.