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): refer to what you are pointing at

esengine142ad8492373
diff --git a/desktop/src/components/AgentDrawer.tsx b/desktop/src/components/AgentDrawer.tsx
index ed42e46ba..347637621 100644
--- a/desktop/src/components/AgentDrawer.tsx
+++ b/desktop/src/components/AgentDrawer.tsx
@@ -492,11 +492,49 @@ function resolveProviderModels(id: string): { label: string; models: readonly st
return { label: def.label, models: def.models };
}
+/** Entities the `@` picker offers for `query`, best-first. */
+function mentionMatches(query: string): { id: number; name: string }[] {
+ const q = query.toLowerCase();
+ return EditorControlSurface.getSceneTree()
+ .filter((n) => !q || n.name.toLowerCase().includes(q))
+ .slice(0, 40)
+ .map((n) => ({ id: n.id, name: n.name }));
+}
+
function Compose() {
const status = useAgent((s) => s.status);
const [draft, setDraft] = useState('');
const ref = useRef<HTMLTextAreaElement>(null);
const busy = status.phase !== 'idle';
+ // `@` 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 [mentionIdx, setMentionIdx] = useState(0);
+ const selected = useSelection((s) => s.selectedId);
+ const selectedName = selected == null ? null : EditorControlSurface.getEntity(selected)?.name ?? null;
+
+ const openMention = (value: string, caret: number) => {
+ const m = /@([^\s@]*)$/.exec(value.slice(0, caret));
+ if (!m) { setMention(null); return; }
+ setMention({ query: m[1], items: mentionMatches(m[1]), at: caret - m[0].length });
+ setMentionIdx(0);
+ };
+
+ const insertMention = (name: string) => {
+ const el = ref.current;
+ const at = mention?.at ?? draft.length;
+ const caret = el?.selectionStart ?? draft.length;
+ const next = `${draft.slice(0, at)}@${name} ${draft.slice(caret)}`;
+ setMention(null);
+ setDraft(next);
+ requestAnimationFrame(() => {
+ el?.focus();
+ const pos = at + name.length + 2;
+ el?.setSelectionRange(pos, pos);
+ autosize();
+ });
+ };
const autosize = () => {
const el = ref.current;
@@ -515,6 +553,35 @@ function Compose() {
return (
<div className={`ag-compose${busy ? ' busy' : ''}`}>
+ {mention && mention.items.length > 0 && (
+ <div className="ag-mention">
+ <div className="ag-mention-h">{t('agent.mention')}</div>
+ <div className="ag-mention-l">
+ {mention.items.map((item, i) => (
+ <button
+ type="button"
+ key={item.id}
+ className={`ag-mention-i${i === mentionIdx ? ' on' : ''}`}
+ onMouseEnter={() => { setMentionIdx(i); peekEntities([item.id]); }}
+ onMouseLeave={() => peekEntities([])}
+ onMouseDown={(e) => { e.preventDefault(); insertMention(item.name); }}
+ >
+ <Boxes size={12} strokeWidth={1.8} />
+ {item.name}
+ <span className="ag-mention-id">{item.id}</span>
+ </button>
+ ))}
+ </div>
+ </div>
+ )}
+ {/* What you have selected, offered rather than assumed: the editor knows
+ what you are looking at, and typing its name again is the editor
+ making you repeat yourself. */}
+ {selectedName && !draft.includes(`@${selectedName}`) && (
+ <button type="button" className="ag-selchip" onClick={() => insertMention(selectedName)}>
+ <Boxes size={12} strokeWidth={1.8} />@{selectedName}
+ </button>
+ )}
<div className="ag-cbox">
<textarea
ref={ref}
@@ -522,8 +589,20 @@ function Compose() {
value={draft}
placeholder={busy ? t('agent.compose.busy') : t('agent.compose')}
aria-label={t('agent.compose')}
- onChange={(e) => { setDraft(e.target.value); autosize(); }}
+ onChange={(e) => {
+ setDraft(e.target.value);
+ autosize();
+ openMention(e.target.value, e.target.selectionStart ?? 0);
+ }}
+ onBlur={() => setMention(null)}
onKeyDown={(e) => {
+ 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; }
+ // Esc dismisses the picker without closing the drawer behind it.
+ if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); setMention(null); return; }
+ }
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
submit();
@@ -544,6 +623,7 @@ function Compose() {
<div className="ag-chint">
<span><b>⏎</b> {t('agent.hint.send')}</span>
<span><b>⇧⏎</b> {t('agent.hint.newline')}</span>
+ <span><b>@</b> {t('agent.hint.mention')}</span>
<span><b>Esc</b> {t('agent.hint.close')}</span>
</div>
</div>
diff --git a/desktop/src/i18n/messages/agent.ts b/desktop/src/i18n/messages/agent.ts
index 9d9dbbb18..fbd5f890b 100644
--- a/desktop/src/i18n/messages/agent.ts
+++ b/desktop/src/i18n/messages/agent.ts
@@ -19,13 +19,15 @@ export const agentMessages = defineMessages({
'agent.newConversation': { en: 'New conversation', zh: '新对话' },
'agent.dock': { en: 'Move it into the layout', zh: '放进布局里' },
'agent.close': { en: 'Close (Esc)', zh: '关闭 (Esc)' },
- 'agent.compose': { en: 'Say something…', zh: '说点什么…' },
+ 'agent.compose': { en: 'Say something… @ to reference an entity', zh: '说点什么… 用 @ 引用实体' },
'agent.compose.busy': { en: 'It is working — this will be the next message…', zh: '正在干活,这条会排到下一轮…' },
'agent.send': { en: 'Send', zh: '发送' },
'agent.stop': { en: 'Stop', zh: '停止' },
'agent.hint.send': { en: 'send', zh: '发送' },
'agent.hint.newline': { en: 'new line', zh: '换行' },
'agent.hint.close': { en: 'close', zh: '关闭' },
+ 'agent.hint.mention': { en: 'reference', zh: '引用' },
+ 'agent.mention': { en: 'Reference', zh: '引用' },
// — Which model the next message runs on —
'agent.picker.none': { en: 'Pick a model', zh: '选择模型' },
diff --git a/desktop/src/theme/agent.css b/desktop/src/theme/agent.css
index ed1de52fd..f27e722a5 100644
--- a/desktop/src/theme/agent.css
+++ b/desktop/src/theme/agent.css
@@ -665,3 +665,53 @@
letter-spacing: -0.01em;
}
.md-ent:hover { border-bottom-color: var(--nebula); background: var(--hover); }
+
+/* ── Referencing something in the scene ─────────────────────────────────────
+ Above the box rather than inside it: the text you are writing stays the
+ subject, and the picker is a thing that happens to it. */
+.ag-mention {
+ position: absolute;
+ left: 13px; right: 13px;
+ bottom: calc(100% - 8px);
+ z-index: 6;
+ border-radius: var(--r-md, 6px);
+ background: var(--raised);
+ border: 1px solid var(--line-strong);
+ box-shadow: 0 18px 44px -10px rgba(0, 0, 0, 0.85);
+ overflow: hidden;
+}
+.ag-compose { position: relative; }
+.ag-mention-h {
+ padding: 6px 11px;
+ font: 500 var(--fs-2xs) var(--font-ui);
+ letter-spacing: 0.1em;
+ text-transform: uppercase;
+ color: var(--text-faint);
+ border-bottom: 1px solid var(--line);
+}
+.ag-mention-l { max-height: 176px; overflow: auto; padding: 4px; }
+.ag-mention-i {
+ display: flex; align-items: center; gap: 9px;
+ width: 100%;
+ padding: 6px 8px;
+ border-radius: var(--r-sm);
+ font: var(--fs-sm) var(--font-ui);
+ color: var(--text);
+ text-align: left;
+}
+.ag-mention-i svg { color: var(--text-faint); flex: none; }
+.ag-mention-i.on { background: var(--hover); color: var(--text-hi); }
+.ag-mention-id { margin-left: auto; font: var(--fs-2xs) var(--font-mono); color: var(--text-faint); }
+
+.ag-selchip {
+ display: flex; align-items: center; gap: 6px;
+ margin: 0 0 7px;
+ padding: 4px 9px;
+ border-radius: var(--r-sm);
+ background: var(--hover);
+ color: var(--text);
+ font: var(--fs-xs) var(--font-mono);
+ width: fit-content;
+}
+.ag-selchip:hover { background: var(--pressed); color: var(--text-hi); }
+.ag-selchip svg { color: var(--text-faint); }