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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(editor): re-ask a run in different words
esengine6690385e1c2b
diff --git a/desktop/src/components/AgentDrawer.tsx b/desktop/src/components/AgentDrawer.tsxindex b26702b7c..c21b3eebb 100644--- a/desktop/src/components/AgentDrawer.tsx+++ b/desktop/src/components/AgentDrawer.tsx@@ -619,6 +619,8 @@ const Turn = memo(function Turn({ turn, isLast, running, until }: {// that the fold is the user's, and re-opening one stays open.const [folded, setFolded] = useState(!isLast);useEffect(() => { setFolded(!isLast); }, [isLast]);+ /** The question, open for a better wording. Null unless re-asking. */+ const [editing, setEditing] = useState<string | null>(null);useSyncExternalStore(EditorHistory.subscribe, EditorHistory.getVersion);const tokens = turn.inputTokens + turn.outputTokens;@@ -626,20 +628,33 @@ const Turn = memo(function Turn({ turn, isLast, running, until }: {const empty = turn.entries.length === 0;return (<div className={`ag-turn${folded ? ' folded' : ''}${isLast ? ' current' : ''}`}>+ {editing !== null ? (+ // Not a button while editing: a header you fold by clicking cannot also+ // hold a field you click into.+ <div className="ag-th ag-th--editing">+ <AskAgain+ initial={editing}+ onCancel={() => setEditing(null)}+ onSubmit={(text) => { setEditing(null); void retryAgentTurn(turn.id, text); }}+ />+ </div>+ ) : (<button type="button" className="ag-th" onClick={() => setFolded((f) => !f)}><span className="ag-hrow"><ChevronRight size={12} strokeWidth={2} className="ag-fold" /><span className="ag-req">{turn.prompt}</span>{/* A bad answer three turns in should cost you that answer, not the- whole conversation that led to it. */}+ whole conversation that led to it — and the reason to re-ask is+ usually that the question could have been put better, so this+ opens it for editing rather than firing the same words again. */}{!running && (<spanrole="button"tabIndex={0}className="ag-rerun"title={t('agent.rerun')}- onClick={(e) => { e.stopPropagation(); void retryAgentTurn(turn.id); }}- onKeyDown={(e) => { if (e.key === 'Enter') { e.stopPropagation(); void retryAgentTurn(turn.id); } }}+ onClick={(e) => { e.stopPropagation(); setEditing(turn.prompt); }}+ onKeyDown={(e) => { if (e.key === 'Enter') { e.stopPropagation(); setEditing(turn.prompt); } }}><RotateCcw size={12} strokeWidth={1.9} /></span>@@ -666,13 +681,14 @@ const Turn = memo(function Turn({ turn, isLast, running, until }: {)}</span></button>+ )}<Fold open={!folded}><div className="ag-tb">{empty && running && isLast ? <Skeleton /> : (<Entriesentries={turn.entries}streaming={running && isLast}- onRerun={running ? undefined : () => void retryAgentTurn(turn.id)}+ onRerun={running ? undefined : () => setEditing(turn.prompt)}/>)}{/* Between rounds: the last thing that happened was a tool result and@@ -777,6 +793,60 @@ function relativeDay(at: number): string {return new Date(at).toLocaleDateString();}+/**+ * The question, open for a better wording.+ *+ * Re-asking and re-wording are the same gesture: the usual reason to run a turn+ * again is that it could have been put better, and submitting unchanged does+ * exactly what the plain re-ask always did. So there is no second button —+ * editing is a step on the way, not a separate feature.+ */+function AskAgain({ initial, onSubmit, onCancel }: {+ initial: string;+ onSubmit: (text: string) => void;+ onCancel: () => void;+}) {+ const [text, setText] = useState(initial);+ const ref = useRef<HTMLTextAreaElement>(null);++ useLayoutEffect(() => {+ const el = ref.current;+ if (!el) return;+ el.style.height = 'auto';+ el.style.height = `${Math.min(160, el.scrollHeight)}px`;+ }, [text]);++ // Selected, not just focused: re-asking often means saying it differently+ // rather than amending a word, and a caret at the end makes that a chore.+ useEffect(() => { ref.current?.select(); }, []);++ return (+ <div className="ag-reask">+ <textarea+ ref={ref}+ className="ag-reask-in"+ rows={1}+ value={text}+ autoFocus+ aria-label={t('agent.reask.label')}+ onChange={(e) => setText(e.target.value)}+ onKeyDown={(e) => {+ if (e.key === 'Escape') { e.stopPropagation(); onCancel(); return; }+ if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); onSubmit(text); }+ }}+ />+ <div className="ag-reask-acts">+ <button type="button" className="ag-go" onClick={() => onSubmit(text)}>+ {t('agent.reask.go')}<span className="ag-kb">⏎</span>+ </button>+ <button type="button" onClick={onCancel}>{t('agent.reask.cancel')}</button>+ {/* What it costs, where the decision is made. */}+ <span className="ag-reask-note">{t('agent.reask.note')}</span>+ </div>+ </div>+ );+}+function EmptyState() {const ready = useAgent((s) => s.status.ready);const openSettings = useEditorStore((s) => s.openSettings);diff --git a/desktop/src/i18n/messages/agent.ts b/desktop/src/i18n/messages/agent.tsindex 87fd7e011..3c3d70e97 100644--- a/desktop/src/i18n/messages/agent.ts+++ b/desktop/src/i18n/messages/agent.ts@@ -124,6 +124,13 @@ export const agentMessages = defineMessages({'agent.turn.queuedMessage': { en: 'Queued: {text}', zh: '已排队:{text}' },'agent.copy': { en: 'Copy', zh: '复制' },'agent.changes': { en: 'What changed', zh: '改动集' },+ 'agent.reask.label': { en: 'Ask this again', zh: '重新提问' },+ 'agent.reask.go': { en: 'Ask again', zh: '重新提问' },+ 'agent.reask.cancel': { en: 'Cancel', zh: '取消' },+ 'agent.reask.note': {+ en: 'this run and everything after it is discarded',+ zh: '这一轮及之后都会被丢弃',+ },'agent.rerun': { en: 'Ask this again, discarding what followed', zh: '从这里重跑(丢弃之后的内容)' },// — The decision it needs from you —diff --git a/desktop/src/store/AgentStore.ts b/desktop/src/store/AgentStore.tsindex b76178b4d..a11085b75 100644--- a/desktop/src/store/AgentStore.ts+++ b/desktop/src/store/AgentStore.ts@@ -595,12 +595,17 @@ export const clearAgentQueue = (): void => useAgent.setState({ queued: [] });* store truncates first so the transcript never shows runs the session has* already forgotten.*/-export async function retryAgentTurn(turnId: number): Promise<void> {+export async function retryAgentTurn(turnId: number, prompt?: string): Promise<void> {const turn = useAgent.getState().turns.find((t) => t.id === turnId);if (!turn) return;+ // Asking the same question again is a special case of asking a different one,+ // which is why the host has always taken the text rather than looking it up:+ // usually the reason to re-ask is that the question could have been better.+ const text = (prompt ?? turn.prompt).trim();+ if (!text) return;// By id, not by position: this window may not hold every run the session does.useAgent.setState((s) => ({ turns: s.turns.filter((t) => t.id < turnId), checkpointDone: null }));- const status = await window.estella?.agent?.retry(turnId, turn.prompt);+ const status = await window.estella?.agent?.retry(turnId, text);if (status) adoptStatus(status);}diff --git a/desktop/src/theme/agent.css b/desktop/src/theme/agent.cssindex cdd2d5c14..9a516eacd 100644--- a/desktop/src/theme/agent.css+++ b/desktop/src/theme/agent.css@@ -800,6 +800,39 @@font-size: var(--fs-sm);font-weight: 500;}+/* The question, open for a better wording. Sits exactly where the header was,+ so nothing moves and it is plainly the same question being changed. */+.ag-th--editing { display: block; width: 100%; padding: 13px 14px 11px; background: var(--panel); }+.ag-reask { display: flex; flex-direction: column; gap: 9px; }+.ag-reask-in {+ width: 100%;+ padding: 8px 10px;+ border-radius: var(--r-sm);+ background: var(--inset);+ border: 1px solid var(--line-strong);+ outline: 0;+ color: var(--text-hi);+ font: var(--fs-md) / 1.5 var(--font-ui);+ letter-spacing: -0.008em;+ resize: none;+ overflow-y: auto;+}+.ag-reask-in:focus { border-color: var(--acc); }+.ag-reask-acts { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; }+.ag-reask-acts button {+ padding: 5px 13px;+ border-radius: var(--r-sm);+ font-size: var(--fs-sm);+ color: var(--text);+ background: var(--hover);+ transition: background var(--t-fast);+}+.ag-reask-acts button:hover { background: var(--pressed); }+.ag-reask-acts .ag-go { background: var(--star); color: var(--on-accent); font-weight: 500; }+.ag-reask-acts .ag-go:hover { background: var(--star-hi); }+/* What it costs, where the decision is made. */+.ag-reask-note { font-size: var(--fs-2xs); color: var(--text-faint); }+/* Re-ask this turn. Quiet until the header is hovered — it discards work. */.ag-rerun {flex: none;diff --git a/desktop/tests/agent-queue.test.ts b/desktop/tests/agent-queue.test.tsindex edb7cc703..5099e541e 100644--- a/desktop/tests/agent-queue.test.ts+++ b/desktop/tests/agent-queue.test.ts@@ -10,11 +10,14 @@* regression tests for holding it instead.*/import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';-import { useAgent, sendAgentMessage, stopAgentTurn, applyAgentMessage } from '@/store/AgentStore';+import {+ useAgent, sendAgentMessage, stopAgentTurn, applyAgentMessage, retryAgentTurn,+} from '@/store/AgentStore';import type { AgentStatus } from '../electron/agent/host';const send = vi.fn(async (_text: string, _images?: unknown) => undefined);const stop = vi.fn();+const retry = vi.fn(async (_n: number, _text: string) => undefined);const status = (phase: AgentStatus['phase']): AgentStatus =>({ ready: true, conversation: true, phase, model: 'opus-5', error: null });@@ -25,7 +28,8 @@ const pushStatus = (phase: AgentStatus['phase']) => applyAgentMessage({ kind: 'sbeforeEach(() => {send.mockClear();stop.mockClear();- (globalThis as { window?: unknown }).window = { estella: { agent: { send, stop } } };+ retry.mockClear();+ (globalThis as { window?: unknown }).window = { estella: { agent: { send, stop, retry } } };useAgent.setState({ status: status('idle'), turns: [], queued: [], driving: false });});@@ -99,3 +103,50 @@ describe('messages typed while a turn runs', () => {expect(send).not.toHaveBeenCalled();});});++/**+ * Re-asking a run.+ *+ * The host has always taken the text rather than looking it up, because the+ * usual reason to run a turn again is that the question could have been put+ * better. Same words is just the case where it could not.+ */+describe('asking a run again', () => {+ const turn = (id: number, prompt: string) => ({+ id, prompt, model: 'm', entries: [], inputTokens: 0, outputTokens: 0,+ steps: 0, mark: null, reason: 'end_turn' as const, startedAt: 0, endedAt: 1,+ });++ beforeEach(() => {+ useAgent.setState({ turns: [turn(0, 'first'), turn(1, 'second')] });+ });++ it('sends the words it was given, not the ones that were asked', async () => {+ await retryAgentTurn(0, 'put it better');+ expect(retry).toHaveBeenCalledWith(0, 'put it better');+ });++ it('falls back to the original when nothing was changed', async () => {+ await retryAgentTurn(0);+ expect(retry).toHaveBeenCalledWith(0, 'first');+ });++ // The run being re-asked and everything after it goes, on this side too —+ // the transcript must not show runs the session has already forgotten.+ it('drops that run and the ones after it', async () => {+ await retryAgentTurn(0, 'again');+ expect(useAgent.getState().turns).toEqual([]);+ });++ it('keeps the runs before it', async () => {+ await retryAgentTurn(1, 'again');+ expect(useAgent.getState().turns.map((t) => t.id)).toEqual([0]);+ });++ // Clearing the box and pressing Enter is not a request to ask nothing.+ it('refuses an emptied question, leaving the run alone', async () => {+ await retryAgentTurn(0, ' ');+ expect(retry).not.toHaveBeenCalled();+ expect(useAgent.getState().turns).toHaveLength(2);+ });+});