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.

feat(editor): answer the agent from the keyboard, and see the values it wants to write

esengine0d5c03e8ef8b
diff --git a/desktop/src/components/AgentDrawer.tsx b/desktop/src/components/AgentDrawer.tsx
index 55c431fd3..d0b805859 100644
--- a/desktop/src/components/AgentDrawer.tsx
+++ b/desktop/src/components/AgentDrawer.tsx
@@ -54,6 +54,31 @@ import type { SceneOp } from '@/engine/sceneOps';
import { ProjectStore } from '@/project/ProjectStore';
import { t } from '@/i18n';
+/**
+ * Bring a passage that needs an answer into view AND under the keyboard.
+ *
+ * Taking focus is the one place in this drawer where that is right: the run is
+ * blocked until this is answered, so the thing the keyboard should be aimed at
+ * is this and not the box for the message after it. It also makes the buttons a
+ * Tab away instead of a mouse trip, which is what the gesture costs today.
+ */
+function showAsk(el: HTMLElement | null): void {
+ el?.scrollIntoView({ block: 'nearest' });
+ el?.focus({ preventScroll: true });
+}
+
+/** Enter on the passage itself is its primary action; Shift+Enter is "for the
+ * rest of this run". Ignored once focus has moved onto a button — the browser
+ * is already turning Enter into that button's click. */
+function askKeys(
+ e: React.KeyboardEvent<HTMLElement>,
+ run: (answer: ConfirmAnswer) => void,
+): void {
+ if (e.target !== e.currentTarget || e.key !== 'Enter') return;
+ e.preventDefault();
+ run(e.shiftKey ? 'turn' : 'once');
+}
+
/**
* Both passages that are WAITING ON THE PERSON carry this, and everything that
* hunts for one looks for it rather than for a particular passage.
@@ -144,7 +169,7 @@ function PreviewPassage({ entry, onConfirm }: {
}) {
const ref = useRef<HTMLDivElement>(null);
const [struck, setStruck] = useState<ReadonlySet<number>>(new Set());
- useEffect(() => { ref.current?.scrollIntoView({ block: 'nearest' }); }, []);
+ useEffect(() => { showAsk(ref.current); }, []);
// A batch big enough to be written to a file is the one most worth reading
// before it lands, so the file form is loaded rather than waved through. An
@@ -179,12 +204,27 @@ function PreviewPassage({ entry, onConfirm }: {
});
return (
- <div ref={ref} className={`ag-ask ag-ask--preview ${ASKING}`}>
+ <div
+ ref={ref}
+ className={`ag-ask ag-ask--preview ${ASKING}`}
+ tabIndex={-1}
+ onKeyDown={(e) => keeping > 0 && askKeys(e, (answer) =>
+ onConfirm(answer, answer === 'once' && struck.size ? [...struck] : undefined))}
+ >
<span className="ag-ask-g"><Pencil size={16} strokeWidth={1.8} /></span>
<span className="ag-ask-hd">
{loading ? t('agent.preview.loading')
: unreadable ? t('agent.preview.unreadable', { path: opsPath ?? '' })
: t('agent.preview.title', { count: preview.length })}
+ {preview.length > 1 && (
+ <button
+ type="button"
+ className="ag-pv-all"
+ onClick={() => setStruck(struck.size ? new Set() : new Set(preview.map((l) => l.index)))}
+ >
+ {struck.size ? t('agent.preview.restoreAll') : t('agent.preview.strikeAll')}
+ </button>
+ )}
</span>
<div className="ag-ask-d">
{unreadable ? t('agent.preview.unreadable.why') : t('agent.preview.why')}
@@ -205,7 +245,31 @@ function PreviewPassage({ entry, onConfirm }: {
{line.detail && <span className="ag-pv-d"> {line.detail}</span>}
{line.components?.length ? <span className="ag-pv-d"> {line.components.join(', ')}</span> : null}
</span>
- {line.fields?.length ? <span className="ag-pv-n">{line.fields.length}</span> : null}
+ {/* The values, not their count. "3 fields" is exactly the part of a
+ write that a preview exists to show, and the change set already
+ speaks this before→after — these are the same facts asked in the
+ other direction, so they read the same way. */}
+ {line.fields?.length ? (
+ <span className="ag-pv-fs">
+ {line.fields.slice(0, MAX_PREVIEW_FIELDS).map((f) => (
+ <span className="ag-pv-f" key={f.path}>
+ <span className="ag-pv-fp">{f.path}</span>
+ {f.before !== undefined && (
+ <>
+ <span className="ag-del">{brief(f.before)}</span>
+ <span className="ag-chg-arrow">→</span>
+ </>
+ )}
+ <span className="ag-add">{brief(f.after)}</span>
+ </span>
+ ))}
+ {line.fields.length > MAX_PREVIEW_FIELDS && (
+ <span className="ag-pv-f ag-pv-more">
+ {t('agent.preview.moreFields', { count: line.fields.length - MAX_PREVIEW_FIELDS })}
+ </span>
+ )}
+ </span>
+ ) : null}
</button>
))}
</div>
@@ -213,9 +277,14 @@ function PreviewPassage({ entry, onConfirm }: {
<button
type="button"
className="ag-go"
+ // Nothing left to apply. The kernel treats an emptied batch as a
+ // decline anyway, so an enabled "Apply 0" would be a button whose
+ // real name is Skip.
+ disabled={keeping === 0}
onClick={() => onConfirm('once', struck.size ? [...struck] : undefined)}
>
{struck.size ? t('agent.preview.applyKept', { count: keeping }) : t('agent.preview.apply')}
+ <span className="ag-kb">⏎</span>
</button>
{/* Trusting the batch once is different from not wanting to be asked
again this run — the second is what makes a long build bearable. */}
@@ -224,7 +293,7 @@ function PreviewPassage({ entry, onConfirm }: {
onClick={() => onConfirm('turn')}
title={t('agent.confirm.allowTurn.why', { tool: entry.name })}
>
- {t('agent.confirm.allowTurn')}
+ {t('agent.confirm.allowTurn')}<span className="ag-kb">⇧⏎</span>
</button>
<button type="button" onClick={() => onConfirm('no')}>{t('agent.confirm.deny')}</button>
</div>
@@ -232,6 +301,9 @@ function PreviewPassage({ entry, onConfirm }: {
);
}
+/** Past this a line is a wall of its own; the count says what was left out. */
+const MAX_PREVIEW_FIELDS = 6;
+
/** The preview reads names and current values straight from the live scene. */
const editorScene: PreviewScene = {
entityName: (id) => EditorControlSurface.getEntity(id)?.name ?? null,
@@ -241,11 +313,14 @@ const editorScene: PreviewScene = {
function ConfirmPassage({ entry, onConfirm }: { entry: AgentToolEntry; onConfirm: (answer: ConfirmAnswer) => void }) {
const ref = useRef<HTMLDivElement>(null);
const target = describeTarget(entry.input);
- useEffect(() => {
- ref.current?.scrollIntoView({ block: 'nearest' });
- }, []);
+ useEffect(() => { showAsk(ref.current); }, []);
return (
- <div ref={ref} className={`ag-ask ag-ask--confirm ${ASKING}`}>
+ <div
+ ref={ref}
+ className={`ag-ask ag-ask--confirm ${ASKING}`}
+ tabIndex={-1}
+ onKeyDown={(e) => askKeys(e, onConfirm)}
+ >
<span className="ag-ask-g"><TriangleAlert size={16} strokeWidth={1.8} /></span>
<span className="ag-ask-hd">{t('agent.confirm.title', { tool: entry.name })}</span>
<div className="ag-ask-d">
@@ -258,7 +333,9 @@ function ConfirmPassage({ entry, onConfirm }: { entry: AgentToolEntry; onConfirm
echoing the tool name back is noise, and the heading already said it. */}
{target && <div className="ag-ask-tgt">{target}</div>}
<div className="ag-ask-acts">
- <button type="button" className="ag-go" onClick={() => onConfirm('once')}>{t('agent.confirm.allow')}</button>
+ <button type="button" className="ag-go" onClick={() => onConfirm('once')}>
+ {t('agent.confirm.allow')}<span className="ag-kb">⏎</span>
+ </button>
{/* A task that saves eleven files should be one decision, not eleven
identical ones — a gate that interrupts that often is one people
learn to click through. Scoped to this run, and says so. */}
@@ -267,7 +344,7 @@ function ConfirmPassage({ entry, onConfirm }: { entry: AgentToolEntry; onConfirm
onClick={() => onConfirm('turn')}
title={t('agent.confirm.allowTurn.why', { tool: entry.name })}
>
- {t('agent.confirm.allowTurn')}
+ {t('agent.confirm.allowTurn')}<span className="ag-kb">⇧⏎</span>
</button>
<button type="button" onClick={() => onConfirm('no')}>{t('agent.confirm.deny')}</button>
</div>
diff --git a/desktop/src/engine/sceneOpsPreview.ts b/desktop/src/engine/sceneOpsPreview.ts
index b823aa63d..ffc1bdff4 100644
--- a/desktop/src/engine/sceneOpsPreview.ts
+++ b/desktop/src/engine/sceneOpsPreview.ts
@@ -151,7 +151,10 @@ export function previewSceneOps(ops: readonly SceneOp[], scene: PreviewScene): P
case 'remove_component':
return { ...base, target, entity: id, components: [op.component] };
case 'rename':
- return { ...base, target, entity: id, detail: op.name };
+ // Carries its own arrow the way `parent` carries "under": the row reads
+ // `target detail`, and a bare new name beside the old one is two names
+ // side by side saying nothing about which way it goes.
+ return { ...base, target, entity: id, detail: `→ ${op.name}` };
case 'parent': {
noteDependency(op.parent, index);
return { ...base, target, entity: id, detail: `under ${describe(op.parent)}` };
diff --git a/desktop/src/i18n/messages/agent.ts b/desktop/src/i18n/messages/agent.ts
index 01c9b1ad6..df2e0c2f7 100644
--- a/desktop/src/i18n/messages/agent.ts
+++ b/desktop/src/i18n/messages/agent.ts
@@ -131,6 +131,9 @@ export const agentMessages = defineMessages({
en: 'Nothing is previewed below. Applying runs the file as the agent wrote it.',
zh: '下面没有可预览的内容。应用将按 Agent 写好的文件原样执行。',
},
+ 'agent.preview.strikeAll': { en: 'Strike all', zh: '全部划掉' },
+ 'agent.preview.restoreAll': { en: 'Restore all', zh: '全部恢复' },
+ 'agent.preview.moreFields': { en: '…and {count} more', zh: '…还有 {count} 项' },
'agent.preview.apply': { en: 'Apply all', zh: '全部应用' },
'agent.preview.applyKept': { en: 'Apply {count}', zh: '应用 {count} 项' },
'agent.confirm.allow': { en: 'Run it', zh: '执行' },
diff --git a/desktop/src/theme/agent.css b/desktop/src/theme/agent.css
index 491e20d61..73f726d22 100644
--- a/desktop/src/theme/agent.css
+++ b/desktop/src/theme/agent.css
@@ -283,8 +283,8 @@
}
.ag-pv {
display: grid;
- grid-template-columns: 14px minmax(0, 1fr) auto;
- gap: 9px;
+ grid-template-columns: 14px minmax(0, 1fr);
+ gap: 0 9px;
align-items: baseline;
width: 100%;
padding: 3px 6px;
@@ -301,8 +301,18 @@
.ag-pv--remove .ag-pv-s { color: var(--error); }
.ag-pv-t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ag-pv-d { color: var(--text-faint); }
-/* How many fields this line writes — the row is a summary, not the diff. */
-.ag-pv-n { color: var(--text-faint); font-size: var(--fs-2xs); }
+/* What the line actually writes, under it and indented to the label column —
+ the same before→after the change set shows, since it is the same fact asked
+ before rather than after. */
+.ag-pv-fs { grid-column: 2 / -1; display: block; margin-top: 2px; }
+.ag-pv-f {
+ display: flex; align-items: baseline; gap: 6px;
+ font-size: var(--fs-2xs);
+ line-height: 1.6;
+ white-space: nowrap;
+}
+.ag-pv-fp { color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; }
+.ag-pv-more { color: var(--text-faint); }
/* Struck, not removed: the line stays where it was so the consequence of
striking a create — its dependents going with it — is visible in place. */
.ag-pv.struck { opacity: 0.4; }
@@ -315,8 +325,35 @@
.ag-asking { animation: ag-flash 1.1s var(--e-out); }
}
@keyframes ag-flash { 0%, 100% { background: transparent; } 40% { background: var(--hover); } }
+/* It takes focus when it arrives — the run is blocked on it, so the keyboard
+ belongs to it — but a focus ring drawn around a passage that already has a
+ rule above, a rule below and a coloured edge is a box on top of structure. */
+.ag-ask:focus { outline: none; }
.ag-ask-g { color: var(--ag-tone); margin-top: 1px; }
-.ag-ask-hd { font-size: var(--fs-md); font-weight: 500; color: var(--text-hi); letter-spacing: -0.01em; line-height: 1.4; }
+.ag-ask-hd {
+ display: flex; align-items: baseline; gap: 10px;
+ font-size: var(--fs-md); font-weight: 500; color: var(--text-hi);
+ letter-spacing: -0.01em; line-height: 1.4;
+}
+/* Strike the lot, or put it back. Only worth showing past one line. */
+.ag-pv-all {
+ margin-left: auto;
+ flex: none;
+ padding: 2px 7px;
+ border-radius: var(--r-sm);
+ font: var(--fs-2xs) var(--font-ui);
+ font-weight: 400;
+ color: var(--text-faint);
+ transition: color var(--t-fast), background var(--t-fast);
+}
+.ag-pv-all:hover { color: var(--text-hi); background: var(--hover); }
+/* The key that does what the button does. Quiet — it labels the button, it is
+ not a second thing on it. */
+.ag-kb {
+ margin-left: 7px;
+ font: var(--fs-2xs) var(--font-mono);
+ opacity: 0.6;
+}
.ag-ask-d { grid-column: 2; font-size: var(--fs-sm); color: var(--text-dim); margin-top: 5px; line-height: 1.65; }
/* Wraps rather than scrolls or truncates: this is the whole basis on which
someone says yes, and a hidden filename is the one thing to never ship. */
@@ -344,6 +381,8 @@
.ag-ask-acts button:hover { background: var(--pressed); }
.ag-ask-acts .ag-go { background: var(--star); color: var(--on-accent); font-weight: 500; }
.ag-ask-acts .ag-go:hover { background: var(--star-hi); }
+.ag-ask-acts .ag-go:disabled { opacity: 0.4; cursor: default; }
+.ag-ask-acts .ag-go:disabled:hover { background: var(--star); }
/* ── Waiting on the first token ──────────────────────────────────────────── */
.ag-wait { display: flex; flex-direction: column; gap: 8px; }
diff --git a/desktop/tests/scene-ops-preview.test.ts b/desktop/tests/scene-ops-preview.test.ts
index 62e222f2e..0c27cd502 100644
--- a/desktop/tests/scene-ops-preview.test.ts
+++ b/desktop/tests/scene-ops-preview.test.ts
@@ -36,7 +36,7 @@ describe('previewing a batch of scene ops', () => {
it('names an existing entity the way the scene does', () => {
const ops: SceneOp[] = [{ op: 'rename', entity: 1, name: 'Backdrop' }];
expect(previewSceneOps(ops, scene)[0]).toMatchObject({
- kind: 'modify', target: 'Panel', entity: 1, detail: 'Backdrop',
+ kind: 'modify', target: 'Panel', entity: 1, detail: '→ Backdrop',
});
});