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): the last of the agent prototype's transcript, minus one on purpose
esenginec36a6b9c0707
diff --git a/desktop/src/components/AgentDrawer.tsx b/desktop/src/components/AgentDrawer.tsxindex cc764ddc4..3df878885 100644--- a/desktop/src/components/AgentDrawer.tsx+++ b/desktop/src/components/AgentDrawer.tsx@@ -27,12 +27,13 @@ import { useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore } frimport {X, Plus, PanelRight, ArrowUp, Square, ChevronRight, ChevronDown, Check, TriangleAlert,Loader, Copy, Pencil, Eye, KeyRound, Boxes, Stethoscope, Image as ImageIcon, RotateCcw,+ File as FileIcon,} from 'lucide-react';import { useEditorStore } from '@/store/editorStore';import {useAgent, sendAgentMessage, stopAgentTurn, confirmAgentCall, resetAgentSession,peekEntities, entitiesInInput, effectiveSelection, selectAgentModel, retryAgentTurn,- type AgentTurn, type AgentEntry, type AgentToolEntry,+ type AgentTurn, type AgentEntry, type AgentToolEntry, type AgentProseEntry,} from '@/store/AgentStore';import type { ConfirmAnswer } from '../../electron/agent/types';import {@@ -46,6 +47,7 @@ import { dockApi } from '@/layout/dockApi';import { EditorHistory, type HistoryMark } from '@/engine/EditorHistory';import { useSelection } from '@/store/selectionStore';import { EditorControlSurface } from '@/engine/EditorSession';+import { ProjectStore } from '@/project/ProjectStore';import { t } from '@/i18n';const TOOL_ICON: Record<string, typeof Eye> = {@@ -152,47 +154,60 @@ function Fold({ open, children }: { open: boolean; children: React.ReactNode }));}-function Thinking({ text }: { text: string }) {+function Thinking({ entry }: { entry: AgentProseEntry }) {const [open, setOpen] = useState(false);return (<div className={`ag-think${open ? ' open' : ''}`}><button type="button" className="ag-think-t" onClick={() => setOpen((o) => !o)}><ChevronRight size={10} strokeWidth={2} className="ag-car" /><span>{t('agent.thinking')}</span>+ {/* How long it thought. The reader is already measuring this silence;+ saying it is the difference between "working" and "stuck". */}+ <Elapsed from={entry.startedAt} to={entry.endedAt} className="ag-think-el" /></button>- <Fold open={open}><div className="ag-think-body">{text}</div></Fold>+ <Fold open={open}><div className="ag-think-body">{entry.text}</div></Fold></div>);}-function Prose({ text, streaming }: { text: string; streaming?: boolean }) {+function Prose({ text, streaming, onRerun }: { text: string; streaming?: boolean; onRerun?: () => void }) {const [copied, setCopied] = useState(false);return (<div className="ag-say">{/* The caret rides the tail of the text itself — see MarkdownView. */}<MarkdownView text={text} entity={entityByName} caret={streaming} />- <button- type="button"- className="ag-say-copy"- title={t('agent.copy')}- onClick={() => {- void navigator.clipboard?.writeText(text);- setCopied(true);- setTimeout(() => setCopied(false), 900);- }}- >- {copied ? <Check size={12} /> : <Copy size={12} />}- </button>+ {/* On the answer rather than only on the run header: a reply you disagree+ with is the thing you are looking at when you decide to ask again. */}+ <span className="ag-say-acts">+ <button+ type="button"+ title={t('agent.copy')}+ onClick={() => {+ void navigator.clipboard?.writeText(text);+ setCopied(true);+ setTimeout(() => setCopied(false), 900);+ }}+ >+ {copied ? <Check size={12} /> : <Copy size={12} />}+ </button>+ {onRerun && (+ <button type="button" title={t('agent.rerun')} onClick={onRerun}>+ <RotateCcw size={12} strokeWidth={1.9} />+ </button>+ )}+ </span></div>);}/** Consecutive tool rows are one hairline-divided block; prose breaks the run. */-function Entries({ entries, onConfirm, streaming }: {+function Entries({ entries, onConfirm, streaming, onRerun }: {entries: readonly AgentEntry[];onConfirm: (callId: string, answer: ConfirmAnswer) => void;/** The run is still taking events, so the last entry is mid-write. */streaming?: boolean;+ /** Ask this run again. Absent while one is running — see Turn. */+ onRerun?: () => void;}) {const out: React.ReactNode[] = [];let run: AgentToolEntry[] = [];@@ -221,8 +236,8 @@ function Entries({ entries, onConfirm, streaming }: {<span className="ag-ask-hd">{entry.message}</span></div>,);- } else if (entry.kind === 'thinking') out.push(<Thinking key={i} text={entry.text} />);- else out.push(<Prose key={i} text={entry.text} streaming={streaming && i === entries.length - 1} />);+ } else if (entry.kind === 'thinking') out.push(<Thinking key={i} entry={entry} />);+ else out.push(<Prose key={i} text={entry.text} streaming={streaming && i === entries.length - 1} onRerun={onRerun} />);});flush();return <>{out}</>;@@ -332,18 +347,18 @@ function brief(value: unknown): string {return text.length > 22 ? `${text.slice(0, 21)}…` : text;}-/** Ticks while the turn runs, then freezes at what it took. A turn that takes- * two minutes should say so as it happens, not only afterwards. */-function Elapsed({ turn }: { turn: AgentTurn }) {+/** Ticks while it runs, then freezes at what it took. A turn that takes two+ * minutes should say so as it happens, not only afterwards. */+function Elapsed({ from, to, className }: { from: number; to: number | null; className?: string }) {const [now, setNow] = useState(() => Date.now());- const live = turn.endedAt === null;+ const live = to === null;useEffect(() => {if (!live) return;const id = setInterval(() => setNow(Date.now()), 100);return () => clearInterval(id);}, [live]);- const ms = (turn.endedAt ?? now) - turn.startedAt;- return <span className={live ? 'ag-live' : undefined}>{formatElapsed(ms)}</span>;+ const ms = (to ?? now) - from;+ return <span className={`${className ?? ''}${live ? ' ag-live' : ''}`.trim()}>{formatElapsed(ms)}</span>;}function Turn({ turn, isLast, running }: { turn: AgentTurn; isLast: boolean; running: boolean }) {@@ -384,7 +399,7 @@ function Turn({ turn, isLast, running }: { turn: AgentTurn; isLast: boolean; runthem, and the composer's picker only ever says what is next. */}{turn.model && <span className="ag-stat-model">{turn.model}</span>}<span className="ag-sp" />- <Elapsed turn={turn} />+ <Elapsed from={turn.startedAt} to={turn.endedAt} />{tokens > 0 && <span>↑{compact(turn.inputTokens)} ↓{compact(turn.outputTokens)}</span>}{turn.reason === 'aborted' && <span className="ag-warn">{t('agent.turn.aborted')}</span>}{turn.reason === 'refusal' && <span className="ag-warn">{t('agent.turn.refusal')}</span>}@@ -402,12 +417,23 @@ function Turn({ turn, isLast, running }: { turn: AgentTurn; isLast: boolean; run<Fold open={!folded}><div className="ag-tb">{empty && running && isLast ? <Skeleton /> : (- <Entries entries={turn.entries} onConfirm={confirmAgentCall} streaming={running && isLast} />+ <Entries+ entries={turn.entries}+ onConfirm={confirmAgentCall}+ streaming={running && isLast}+ onRerun={running ? undefined : () => void retryAgentTurn(turn.id)}+ />)}{/* Between rounds: the last thing that happened was a tool result andthe model is being asked again. Without this the transcript looksfinished while a request is in flight. */}{running && isLast && turn.entries.length > 0 && lastIsSettled(turn.entries) && <Waiting />}+ {/* How the run ENDED, where the run is — the header's badge survives+ folding, and this says the part that needs a sentence: a stopped+ turn keeps whatever it already did, which is the question anyone+ who just pressed Stop is asking. */}+ {turn.reason === 'aborted' && <div className="ag-sys">{t('agent.turn.aborted.note')}</div>}+ {turn.reason === 'refusal' && <div className="ag-sys">{t('agent.turn.refusal.note')}</div>}{turn.reason !== null && <ChangeSet turn={turn} />}</div></Fold>@@ -540,13 +566,37 @@ function resolveProviderModels(id: string): { label: string; models: readonly streturn { label: def.label, models: def.models };}-/** Entities the `@` picker offers for `query`, best-first. */-function mentionMatches(query: string): { id: number; name: string }[] {+/** One thing `@` can name: something in the scene, or something on disk. */+interface Mention {+ key: string;+ /** What gets typed. An asset goes in as its path — that is what the tools take. */+ insert: string;+ label: string;+ /** Entities light up in the Outliner and the viewport when hovered. */+ entity: number | null;+ detail: string;+}++/**+ * What `@` offers for `query`, entities first.+ *+ * Assets belong here as much as entities do: half of what gets asked for names+ * a texture or a prefab ("use @panel_9slice.png for the buttons"), and without+ * them the alternative is typing a path from memory and hoping it is right.+ */+function mentionMatches(query: string): Mention[] {const q = query.toLowerCase();- return EditorControlSurface.getSceneTree()+ const entities: Mention[] = EditorControlSurface.getSceneTree().filter((n) => !q || n.name.toLowerCase().includes(q))- .slice(0, 40)- .map((n) => ({ id: n.id, name: n.name }));+ .slice(0, 24)+ .map((n) => ({ key: `e${n.id}`, insert: n.name, label: n.name, entity: n.id, detail: String(n.id) }));++ const assets: Mention[] = ProjectStore.listAssets()+ .filter((a) => !q || a.name.toLowerCase().includes(q) || a.path.toLowerCase().includes(q))+ .slice(0, 16)+ .map((a) => ({ key: `a${a.ref}`, insert: a.path, label: a.name, entity: null, detail: a.type }));++ return [...entities, ...assets];}function Compose() {@@ -557,7 +607,7 @@ function Compose() {// `@` opens a picker over the scene tree. Read at open time rather than// subscribed: it is a menu, and a list reordering under the arrow keys is a// menu that picks something other than what was highlighted.- const [mention, setMention] = useState<{ query: string; items: { id: number; name: string }[]; at: number } | null>(null);+ const [mention, setMention] = useState<{ query: string; items: Mention[]; at: number } | null>(null);const [mentionIdx, setMentionIdx] = useState(0);const selected = useSelection((s) => s.selectedId);const selectedName = selected == null ? null : EditorControlSurface.getEntity(selected)?.name ?? null;@@ -608,15 +658,17 @@ function Compose() {{mention.items.map((item, i) => (<buttontype="button"- key={item.id}+ key={item.key}className={`ag-mention-i${i === mentionIdx ? ' on' : ''}`}- onMouseEnter={() => { setMentionIdx(i); peekEntities([item.id]); }}+ onMouseEnter={() => { setMentionIdx(i); peekEntities(item.entity === null ? [] : [item.entity]); }}onMouseLeave={() => peekEntities([])}- onMouseDown={(e) => { e.preventDefault(); insertMention(item.name); }}+ onMouseDown={(e) => { e.preventDefault(); insertMention(item.insert); }}>- <Boxes size={12} strokeWidth={1.8} />- {item.name}- <span className="ag-mention-id">{item.id}</span>+ {item.entity === null+ ? <FileIcon size={12} strokeWidth={1.8} />+ : <Boxes size={12} strokeWidth={1.8} />}+ {item.label}+ <span className="ag-mention-id">{item.detail}</span></button>))}</div>@@ -647,7 +699,7 @@ function Compose() {if (mention && mention.items.length > 0) {if (e.key === 'ArrowDown') { e.preventDefault(); setMentionIdx((i) => (i + 1) % mention.items.length); return; }if (e.key === 'ArrowUp') { e.preventDefault(); setMentionIdx((i) => (i - 1 + mention.items.length) % mention.items.length); return; }- if (e.key === 'Enter' || e.key === 'Tab') { e.preventDefault(); insertMention(mention.items[mentionIdx].name); return; }+ if (e.key === 'Enter' || e.key === 'Tab') { e.preventDefault(); insertMention(mention.items[mentionIdx].insert); return; }// Esc dismisses the picker without closing the drawer behind it.if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); setMention(null); return; }}diff --git a/desktop/src/i18n/messages/agent.ts b/desktop/src/i18n/messages/agent.tsindex 1e33d9cbd..3ecab174f 100644--- a/desktop/src/i18n/messages/agent.ts+++ b/desktop/src/i18n/messages/agent.ts@@ -62,7 +62,15 @@ export const agentMessages = defineMessages({'agent.stopped': { en: 'stopped', zh: '已中止' },'agent.declined': { en: 'skipped', zh: '已跳过' },'agent.turn.aborted': { en: 'Stopped part-way.', zh: '中途停止。' },+ 'agent.turn.aborted.note': {+ en: 'You stopped this run — whatever it had already done is still there.',+ zh: '你停止了这一轮——做到一半的改动都留着。',+ },'agent.turn.refusal': { en: 'The model declined this one.', zh: '模型拒绝了这个请求。' },+ 'agent.turn.refusal.note': {+ en: 'Nothing was changed by this run, so there is nothing to undo.',+ zh: '这一轮什么都没有改动,不需要撤销。',+ },'agent.turn.queuedMessage': { en: 'Queued: {text}', zh: '已排队:{text}' },'agent.copy': { en: 'Copy', zh: '复制' },'agent.changes': { en: 'What changed', zh: '改动集' },diff --git a/desktop/src/store/AgentStore.ts b/desktop/src/store/AgentStore.tsindex 049808deb..b131cacb6 100644--- a/desktop/src/store/AgentStore.ts+++ b/desktop/src/store/AgentStore.ts@@ -63,6 +63,11 @@ export interface AgentToolEntry {export interface AgentProseEntry {kind: 'text' | 'thinking';text: string;+ /** When the first delta landed, and when the run moved on to something else.+ * Reasoning shows how long it took: a long silence is the thing the reader is+ * already measuring, and guessing at it is what makes a turn feel hung. */+ startedAt: number;+ endedAt: number | null;}export interface AgentErrorEntry {@@ -231,9 +236,19 @@ const withEntries = (turns: readonly AgentTurn[], entries: AgentEntry[]): AgentTfunction appendProse(entries: readonly AgentEntry[], kind: 'text' | 'thinking', delta: string): AgentEntry[] {const last = entries[entries.length - 1];if (last?.kind === kind) {- return [...entries.slice(0, -1), { kind, text: last.text + delta }];+ return [...entries.slice(0, -1), { ...last, text: last.text + delta }];+ }+ return [...closeProse(entries), { kind, text: delta, startedAt: Date.now(), endedAt: null }];+}++/** A prose block is over the moment anything else begins — another kind of+ * block, a tool call, the end of the run. Idempotent. */+function closeProse(entries: readonly AgentEntry[]): AgentEntry[] {+ const last = entries[entries.length - 1];+ if (!last || last.kind === 'tool' || last.kind === 'error' || last.endedAt !== null) {+ return entries as AgentEntry[];}- return [...entries, { kind, text: delta }];+ return [...entries.slice(0, -1), { ...last, endedAt: Date.now() }];}/** Rewrite the tool row `id` — a no-op if the turn never saw it asked for. */@@ -291,7 +306,7 @@ export function applyAgentEvent(turns: readonly AgentTurn[], event: AgentEvent):if (open.entries.some((e) => e.kind === 'tool' && e.id === id)) {return withEntries(turns, patchTool(open.entries, id, { input }));}- return withEntries(turns, [...open.entries, {+ return withEntries(turns, [...closeProse(open.entries), {kind: 'tool',id,name,@@ -339,7 +354,7 @@ export function applyAgentEvent(turns: readonly AgentTurn[], event: AgentEvent):});case 'error':- return withEntries(turns, [...open.entries, { kind: 'error', message: event.message }]);+ return withEntries(turns, [...closeProse(open.entries), { kind: 'error', message: event.message }]);case 'turn_end':return patchLast(turns, {@@ -348,7 +363,7 @@ export function applyAgentEvent(turns: readonly AgentTurn[], event: AgentEvent):mark: event.mark,endedAt: Date.now(),// Nothing will report on these now. Neither failed nor succeeded.- entries: open.entries.map((e) =>+ entries: closeProse(open.entries).map((e) =>(e.kind === 'tool' && !TERMINAL.has(e.state) ? { ...e, state: 'stopped' as const } : e)),});diff --git a/desktop/src/theme/agent.css b/desktop/src/theme/agent.cssindex 1b30d0600..19b49e794 100644--- a/desktop/src/theme/agent.css+++ b/desktop/src/theme/agent.css@@ -171,19 +171,27 @@white-space: pre-wrap;overflow-wrap: anywhere;}-.ag-say-copy {- position: absolute; top: -10px; right: 0;+/* Quiet until the answer is hovered: they act on what you are reading, and a+ row of buttons always on would compete with the text for the eye. */+.ag-say-acts {+ position: absolute; top: -11px; right: 0;+ display: flex; gap: 1px;+ padding: 2px;+ border-radius: var(--r-md);+ background: var(--raised);+ border: 1px solid var(--line);+ opacity: 0;+ transition: opacity var(--t-fast);+}+.ag-say:hover .ag-say-acts, .ag-say-acts:focus-within { opacity: 1; }+.ag-say-acts button {width: 22px; height: 22px;display: grid; place-items: center;border-radius: var(--r-sm);- background: var(--raised);- border: 1px solid var(--line);color: var(--text-faint);- opacity: 0;- transition: opacity var(--t-fast), color var(--t-fast);+ transition: color var(--t-fast), background var(--t-fast);}-.ag-say:hover .ag-say-copy, .ag-say-copy:focus-visible { opacity: 1; }-.ag-say-copy:hover { color: var(--text-hi); }+.ag-say-acts button:hover { color: var(--text-hi); background: var(--hover); }.ag-think { padding-left: 12px; border-left: 1px solid var(--line); }.ag-think-t {@@ -193,6 +201,7 @@transition: color var(--t-fast);}.ag-think-t:hover { color: var(--text-dim); }+.ag-think-el { margin-left: 2px; font: var(--fs-2xs) var(--font-mono); color: var(--text-faint); }.ag-car { transition: transform var(--t) var(--e-out); }.ag-think.open .ag-car { transform: rotate(90deg); }.ag-think-body {diff --git a/desktop/tests/agent-transcript.test.ts b/desktop/tests/agent-transcript.test.tsindex 9ae23a868..77c4239a2 100644--- a/desktop/tests/agent-transcript.test.ts+++ b/desktop/tests/agent-transcript.test.ts@@ -10,7 +10,7 @@import { describe, it, expect } from 'vitest';import {applyAgentEvent, entitiesInInput, touchedEntities, briefResult,- type AgentTurn, type AgentToolEntry,+ type AgentTurn, type AgentToolEntry, type AgentProseEntry,} from '../src/store/AgentStore';import type { AgentEvent, ToolCall } from '../electron/agent/types';@@ -61,7 +61,27 @@ describe('the transcript projection', () => {{ type: 'text', delta: 'The scene ' },{ type: 'text', delta: 'has 58 entities.' },);- expect(turn.entries).toEqual([{ kind: 'text', text: 'The scene has 58 entities.' }]);+ expect(turn.entries).toMatchObject([{ kind: 'text', text: 'The scene has 58 entities.' }]);+ });++ // Reasoning is timed: the reader is already measuring that silence, and a+ // block that never closes would keep counting after the run moved on.+ it('stamps a prose block\'s start, and closes it when something else begins', () => {+ const [turn] = fold(+ started(),+ { type: 'thinking', delta: 'look at the UI layer' },+ { type: 'text', delta: 'Building it now.' },+ );+ const [thinking, answer] = turn.entries as [AgentProseEntry, AgentProseEntry];+ expect(thinking.startedAt).toBeGreaterThan(0);+ expect(thinking.endedAt).toBeGreaterThanOrEqual(thinking.startedAt);+ // The one still being written has not ended.+ expect(answer.endedAt).toBeNull();+ });++ it('closes an open prose block when the run ends', () => {+ const [turn] = fold(started(), { type: 'text', delta: 'Done.' }, ended());+ expect((turn.entries[0] as AgentProseEntry).endedAt).not.toBeNull();});// Reasoning and answer are different rows; concatenating them would put the