The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.
Chat: Esc just after Settings closes does not stop the agent (#202)
WEBGRS3f48c888f73f
diff --git a/.changeset/esc-after-settings.md b/.changeset/esc-after-settings.mdnew file mode 100644index 0000000..cbdbf80--- /dev/null+++ b/.changeset/esc-after-settings.md@@ -0,0 +1,5 @@+---+"@openfilm/desktop": patch+---++Chat: Esc pressed again just after it closed Settings, or held down, no longer stops the agent at work. Settings gives the focus back to the chat as it closes, so the second Esc landed in the chat and stopped the turn without a word. An Esc in the chat a moment later still stops it.diff --git a/apps/desktop/chat/src/App.tsx b/apps/desktop/chat/src/App.tsxindex d3ac77d..984ccef 100644--- a/apps/desktop/chat/src/App.tsx+++ b/apps/desktop/chat/src/App.tsx@@ -71,6 +71,7 @@ import { app, type FfmpegState, type Project, type SkillListing, type StudioRef,import { Divider } from './components/Divider';import { draftRefs, pillCard, refFromPill, refPill, sameStudioThing, selectionMoment, selectionRange, selectionRefs, studioReferencesText } from './studio-refs';import { inChat } from './lib/chat-layer';+import { escStopper } from './lib/esc-stop';import { useProjectChat } from './lib/chat-store';import { givenAfter, pagesToSend } from './lib/skill-pages';import { ChatProjectContext, WaitingContext } from './lib/developer';@@ -935,11 +936,12 @@ function ChatPane({ project, banners, selection, assetsOpen }: { project: Projec</div>);- /* Esc stops the turn while it runs (the send button's other half) */+ /* Esc stops the turn while it runs (the send button's other half): only one pressed in the chat, not held, and not+ just after one in Studio (lib/esc-stop.ts) */+ const escStops = React.useRef(escStopper());React.useEffect(() => {if (!active || !asset) return undefined;- /* only an Esc pressed in the chat: one in Studio closes Studio's menu or dialog, it does not stop the agent */- const onKey = (event: KeyboardEvent) => { if (event.key === 'Escape' && !event.defaultPrevented && inChat(event.target)) void cancelLocalAgentTurn(asset.id); };+ const onKey = (event: KeyboardEvent) => { if (escStops.current(event, inChat(event.target))) void cancelLocalAgentTurn(asset.id); };window.addEventListener('keydown', onKey);return () => window.removeEventListener('keydown', onKey);}, [active, asset]);diff --git a/apps/desktop/chat/src/lib/esc-stop.ts b/apps/desktop/chat/src/lib/esc-stop.tsnew file mode 100644index 0000000..23a2aa8--- /dev/null+++ b/apps/desktop/chat/src/lib/esc-stop.ts@@ -0,0 +1,23 @@+/**+ * Which Esc stops the turn while it runs (the send button's other half): one pressed in the chat (chat-layer.ts+ * `inChat`). One in Studio closes Studio's menu or dialog, and one already handled is its handler's; neither stops the+ * agent. Nor does an Esc held down, or pressed again just after one of those: Studio's Settings gives the focus back to+ * the chat as it closes, so the key held, or pressed twice to close it, would land in the chat and stop the agent.+ *+ * Pure: no React, no bridge, so `node --test` runs it (src/esc-stop.test.mjs).+ */++/** An Esc this soon after one that was not the chat's is still closing, not stopping. */+export const ESC_AGAIN_MS = 1000;++type EscKey = Pick<KeyboardEvent, 'key' | 'repeat' | 'defaultPrevented' | 'timeStamp'>;++/** Hears every key pressed (`inChat`: where it was) and says whether it stops the turn. */+export function escStopper(): (event: EscKey, inChat: boolean) => boolean {+ let elsewhereAt = -Infinity;+ return (event, inChat) => {+ if (event.key !== 'Escape') return false;+ if (event.defaultPrevented || !inChat) { elsewhereAt = event.timeStamp; return false; }+ return !event.repeat && event.timeStamp - elsewhereAt >= ESC_AGAIN_MS;+ };+}diff --git a/apps/desktop/src/esc-stop.test.mjs b/apps/desktop/src/esc-stop.test.mjsnew file mode 100644index 0000000..2d86601--- /dev/null+++ b/apps/desktop/src/esc-stop.test.mjs@@ -0,0 +1,33 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { ESC_AGAIN_MS, escStopper } from '../chat/src/lib/esc-stop.ts';++const esc = (timeStamp, more = {}) => ({ key: 'Escape', repeat: false, defaultPrevented: false, timeStamp, ...more });++test('an Esc in the chat stops the turn; one in Studio, or another key, does not', () => {+ const stops = escStopper();+ assert.equal(stops(esc(5000), true), true);+ assert.equal(stops(esc(9000), false), false);+ assert.equal(stops({ ...esc(12000), key: 'Enter' }, true), false);+});++test('an Esc in the chat just after one that closed Settings does not stop the turn; one a while later does', () => {+ const stops = escStopper();+ /* Settings closes on the first and gives the focus back to the chat */+ assert.equal(stops(esc(1000), false), false);+ assert.equal(stops(esc(1300), true), false);+ assert.equal(stops(esc(1000 + ESC_AGAIN_MS - 1), true), false);+ assert.equal(stops(esc(1000 + ESC_AGAIN_MS), true), true);+});++test('an Esc held down, or one already handled in the chat, does not stop the turn', () => {+ const stops = escStopper();+ assert.equal(stops(esc(1000), false), false);+ /* the system repeats a held key, in the chat once Settings has closed */+ assert.equal(stops(esc(1500, { repeat: true }), true), false);+ assert.equal(stops(esc(4000, { repeat: true }), true), false);+ /* a dialog of the chat's closed on it, and the next comes just after */+ assert.equal(stops(esc(8000, { defaultPrevented: true }), true), false);+ assert.equal(stops(esc(8200), true), false);+ assert.equal(stops(esc(10000), true), true);+});