AI Agent
Updated
Which AI model answers for the agent; every agent needs one.
- Type
ent-ai-agent- Script class
AIAgentEntity- Family
- Logic and AI
Overview
Section titled “Overview”An AI Agent is a character in your world that visitors talk to: a museum guide, a shopkeeper, a tutor, a quest giver. It answers through a language model that the application serves. On its own it is invisible, an anchor with a position in the scene; you usually place it next to, or inside, the model that represents the character.
An agent is assembled from components, one per capability, so it carries only what it uses:
- The AI Model (
AIModelComponent) is required. It names the model that answers, and it holds the agent’s conversation: the history of messages and replies that the dialog shows and that scripts use. It also sends everyAIAgent::…event to the agent’s Signal Emitters. - The AI Flow is there when the agent is created. It is an editor view that draws how the agent’s AI components connect; it does nothing in play.
- You add the rest as needed: an AI Persona for a name and standing instructions, an AI Dialog for the chat panel, an AI Proximity box to notice when the player walks up, AI Text To Speech for a voice, AI Speech To Text for the microphone, an AI Caption for subtitles, an AI Translator, an AI Memory, an AI Summarizer, and AI Tool and AI Skill components for functions and instruction packs the model can use.
Editor and play. In play the agent is live: its dialog opens, its microphone works and its tools are ready. In the editor it is inert: the dialog shows a static preview with the agent’s name, nothing is sent to a model, and getConversation() returns undefined. Every capability is also idle until it is used: adding a voice does not load it, and adding speech recognition does not open the microphone.
When to use it. Use an AI Agent whenever a character should answer in its own words. For fixed lines that never change, a Text entity or a UI label is simpler and costs nothing. The AI Dialog entity is not an alternative: it only exists in older scenes, and new dialogs are AI Dialog components on the agent itself.
The props that matter most are on the AI Model:
modelis the id of the model that answers. A new agent uses the application’s default model,qwen35-2b-chat-v1, withproviderset to'Remote', the model service the application provides.reasoningEffortis how much the model thinks before it answers, from'none'up to'max'. More effort gives more careful answers, more slowly. A new agent uses'none', which answers fastest.
Most of what makes an agent feel like a character lives on the other components: the persona’s name and instructions, and the dialog’s presentation, autoSpeak and positioning. Changes to the model or persona apply from the next turn.
Set it up
Section titled “Set it up”- Add an AI Agent entity to the scene where visitors will meet it, for example beside a statue (see Scenes and hierarchy). Name it, for example
Guide. - Leave the AI Model on its default model, or choose another model in its properties.
- Add an AI Persona. Set
nameto what visitors call the agent andinstructionsto how it behaves:You are the friendly guide of this gallery. Answer in two or three sentences. - Add an AI Dialog so visitors can type to it. It is always visible by default; to show it only when the player is near, add an AI Proximity and set the dialog’s
presentationto'proximity'. - To give it a voice, add AI Text To Speech and turn on the dialog’s
autoSpeak. - To let scripts hear it, add a Signal Emitter with a
signalsuch asguide:ai. - Enter play, walk up to the agent and type a message. The reply streams into the panel.
Examples
Section titled “Examples”Ask the agent from a button
Section titled “Ask the agent from a button”Send a ready-made question when the visitor presses a button, as if they had typed it.
#pragma lifecycle(startup)
// Scene: an AI Agent named "Guide" with an AI Dialog, and a UI Button// with a Signal Emitter on 'guide/ask'.let conversation;
async function ask(question) { if (conversation.getSnapshot().activeTurnId) return; try { await conversation.send(question); } catch (error) { log('The guide could not answer:', error.message); }}
function startup() { conversation = findEntity('Guide').getComponent(AIModelComponent).getConversation(); if (!conversation) return log('The guide is not available here.');
on('guide/ask', (event) => { if (event.eventType === EventType.UIButton.Press) ask('What is the oldest piece in this room?'); });}How it works
getConversation()returns the agent’s shared conversation, the same one its dialog shows. It isundefinedin the editor, while the agent or its AI Model is off, or when the application offers no AI, so the script checks it first.send(text)sends an ordinary visitor message: it appears in the dialog, and the reply streams in below it. The promise resolves when the turn ends.- An agent answers one turn at a time, and
sendthrows “Conversation is busy.” while a turn runs. CheckinggetSnapshot().activeTurnIdfirst avoids that; thetry/catchcovers every other failure.
Show when the agent is thinking
Section titled “Show when the agent is thinking”Switch an indicator on while the agent works on a reply, and off when it is done.
#pragma lifecycle(startup)
// Scene: an AI Agent named "Guide" with a Signal Emitter on 'guide:ai', and an// entity named "Thinking Bubble" (for example an image above the agent's head).function startup() { const bubble = findEntity('Thinking Bubble'); bubble.setEnabled(false);
on('guide:ai', (event) => { switch (event.eventType) { case EventType.AIAgent.Thinking: bubble.setEnabled(true); break; case EventType.AIAgent.Writing: case EventType.AIAgent.Idle: bubble.setEnabled(false); break; case EventType.AIAgent.Error: log('Guide error:', event.data.text); break; } });}How it works
- The agent sends its events to the Signal Emitters on its own entity; this one puts them on
guide:ai. Each event is{ entityId, eventType, data }. AIAgent::thinkingarrives when the agent is working and has no text yet. The firstAIAgent::writing, a chunk of the reply, means text is on its way.AIAgent::idlearrives after every turn, whether it succeeded, was cancelled or failed, so the bubble never stays on by mistake.AIAgent::errorcarries indata.texta message that is safe to show the visitor.
Tell the agent what the visitor is looking at
Section titled “Tell the agent what the visitor is looking at”When the visitor clicks an exhibit, ask the agent to comment on it without showing the prompt.
#pragma lifecycle(startup)
// Scene: an AI Agent named "Guide" with an AI Dialog. Each exhibit has a Selectable// and a Signal Emitter on 'exhibit/select'; its entity name is the work's title.let conversation;
async function describe(title) { if (!conversation || conversation.getSnapshot().activeTurnId) return; try { await conversation.sendCommand( `The visitor is looking at "${title}". Tell them one surprising fact about it, in two sentences.`, { persist: false }, ); } catch (error) { log('The guide missed it:', error.message); }}
function startup() { conversation = findEntity('Guide').getComponent(AIModelComponent).getConversation();
on('exhibit/select', (event) => { if (event.eventType !== EventType.Selectable.Select) return; findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again describe(findEntity(event.entityId).name); });}How it works
- A command, sent with
sendCommand, is hidden input from your application. It creates no chat entry and noAIAgent::promptevent: the visitor sees only the agent’s reply, which is shown and spoken like any other. persist: falsekeeps the command itself out of the agent’s stored Context, so a passing remark does not clutter later turns. It is the default for commands; it is written here to make the choice visible. Usepersist: truefor facts the agent should remember, such as a finished quest.- All exhibits share one signal;
event.entityIdtells which one was clicked, andfindEntityturns it back into the entity.
Switch to a simpler voice for children
Section titled “Switch to a simpler voice for children”Let a checkbox change how the agent talks, from the next answer on.
#pragma lifecycle(startup)
// Scene: an AI Agent named "Guide" with an AI Persona, and a UI Checkbox// named "Kids Mode" with a Signal Emitter on 'guide/kids'.const BASE = 'You are Ada, the guide of the east wing. Only talk about the works in this wing.';const ADULT = BASE + ' Answer in at most three sentences.';const KIDS = BASE + ' The visitor is a child: use short, simple words and one fun comparison.';
function startup() { const guide = findEntity('Guide'); const persona = guide.getComponent(AIPersonaComponent); persona.setProps({ name: 'Ada', instructions: ADULT });
on('guide/kids', (event) => { if (event.eventType !== EventType.UICheckbox.Change) return; persona.setProps({ instructions: event.data.checked ? KIDS : ADULT }); log(AIPersonaComponent.displayName(guide), 'now talks', event.data.checked ? 'to children' : 'to adults'); });}How it works
- The persona’s
instructionsgo to the model with every turn, so changing them changes how the agent answers, from the next turn on. A reply being written finishes with the old instructions. UICheckbox::changecarries the new state indata.checked.AIPersonaComponent.displayName(entity)returns the name the dialog shows: the persona’s name, else the entity’s name, elseAgent.
- Browsers keep a page silent until the visitor first clicks, taps or presses a key. Spoken replies wait for that first interaction; until then the dialog asks the visitor to press Play.
- Without a Signal Emitter on the agent,
onnever hears anyAIAgent::…event. And eachAIAgent::writingevent carries only its own chunk of the reply: append chunks, or wait forAIAgent::response, which carries the whole reply.
Components
Section titled “Components”What a new AI Agent is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| AI Model | comp-ai-model |
Required | One | Which AI model answers for the agent; every agent needs one. |
| AI Flow | comp-ai-flow |
Starts with | One | Visualize how your Agent’s AI components connect and work together. |
You can also add 18 optional components, such as AI Caption, AI Dialog, AI Memory, AI Persona, AI Proximity, AI Skill.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
provider |
"Remote" |
|
model |
"qwen35- |
|
reasoning |
"none" |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
AIAgent::promptAIAgent::thinkingAIAgent::responseAIAgent::talkingAIAgent::writingAIAgent::errorAIAgent::idleAIAgent::cancelledAIAgent::translation-requestedAIAgent::translation-completedAIAgent::translation-failedAIAgent::translation-cancelledAIAgent::turn-startedAIAgent::turn-completedAIAgent::turn-failedAIAgent::turn-cancelledAIAgent::instructions-usedAIAgent::instructions-updatedAIAgent::skill-updatedAIAgent::tool-updatedAIAgent::skill-requestedAIAgent::skill-loadedAIAgent::skill-load-failedAIAgent::tool-definition-loadedAIAgent::tool-definition-failedAIAgent::tool-requestedAIAgent::tool-completedAIAgent::tool-failedAIAgent::tool-cancelledAIAgent::memory-read-requestedAIAgent::memory-readAIAgent::memory-read-failedAIAgent::memory-write-requestedAIAgent::memory-writtenAIAgent::memory-write-failedAIAgent::context-updated
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
An AI Proximity box round the guide shows her dialog only while the camera is near, and a Behavior script hides the "walk up" hint on the proximity's enter event.
Hold W to walk the camera inside the blue ring: the hint goes and the guide's dialog appears. Hold S to walk back out. The greeting itself is sent to the AI service, which this preview does not have, so the panel shows a short error instead of a reply.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My AI Agent.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My AI Agent'); if (!(entity instanceof AIAgentEntity)) return;
// Its AI Model component holds the settings listed under Props. const component = entity.getComponent(AIModelComponent); log(component.props);}