Skip to content

AI Agent

Updated

Which AI model answers for the agent; every agent needs one.

Type
ent-ai-agent
Script class
AIAgentEntity

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 every AIAgent::… 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:

  • model is the id of the model that answers. A new agent uses the application’s default model, qwen35-2b-chat-v1, with provider set to 'Remote', the model service the application provides.
  • reasoningEffort is 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.

  1. 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.
  2. Leave the AI Model on its default model, or choose another model in its properties.
  3. Add an AI Persona. Set name to what visitors call the agent and instructions to how it behaves: You are the friendly guide of this gallery. Answer in two or three sentences.
  4. 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 presentation to 'proximity'.
  5. To give it a voice, add AI Text To Speech and turn on the dialog’s autoSpeak.
  6. To let scripts hear it, add a Signal Emitter with a signal such as guide:ai.
  7. Enter play, walk up to the agent and type a message. The reply streams into the panel.

Send a ready-made question when the visitor presses a button, as if they had typed it.

AskButton.jx
#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 is undefined in 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 send throws “Conversation is busy.” while a turn runs. Checking getSnapshot().activeTurnId first avoids that; the try/catch covers every other failure.

Switch an indicator on while the agent works on a reply, and off when it is done.

ThinkingIndicator.jx
#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::thinking arrives when the agent is working and has no text yet. The first AIAgent::writing, a chunk of the reply, means text is on its way.
  • AIAgent::idle arrives after every turn, whether it succeeded, was cancelled or failed, so the bubble never stays on by mistake.
  • AIAgent::error carries in data.text a 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.

ExhibitNarrator.jx
#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 no AIAgent::prompt event: the visitor sees only the agent’s reply, which is shown and spoken like any other.
  • persist: false keeps 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. Use persist: true for facts the agent should remember, such as a finished quest.
  • All exhibits share one signal; event.entityId tells which one was clicked, and findEntity turns it back into the entity.

Let a checkbox change how the agent talks, from the next answer on.

KidsMode.jx
#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 instructions go 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::change carries the new state in data.checked.
  • AIPersonaComponent.displayName(entity) returns the name the dialog shows: the persona’s name, else the entity’s name, else Agent.
  • 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, on never hears any AIAgent::… event. And each AIAgent::writing event carries only its own chunk of the reply: append chunks, or wait for AIAgent::response, which carries the whole reply.

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-transform 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-2b-chat-v1"
reasoningEffort "none"

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • AIAgent::prompt
  • AIAgent::thinking
  • AIAgent::response
  • AIAgent::talking
  • AIAgent::writing
  • AIAgent::error
  • AIAgent::idle
  • AIAgent::cancelled
  • AIAgent::translation-requested
  • AIAgent::translation-completed
  • AIAgent::translation-failed
  • AIAgent::translation-cancelled
  • AIAgent::turn-started
  • AIAgent::turn-completed
  • AIAgent::turn-failed
  • AIAgent::turn-cancelled
  • AIAgent::instructions-used
  • AIAgent::instructions-updated
  • AIAgent::skill-updated
  • AIAgent::tool-updated
  • AIAgent::skill-requested
  • AIAgent::skill-loaded
  • AIAgent::skill-load-failed
  • AIAgent::tool-definition-loaded
  • AIAgent::tool-definition-failed
  • AIAgent::tool-requested
  • AIAgent::tool-completed
  • AIAgent::tool-failed
  • AIAgent::tool-cancelled
  • AIAgent::memory-read-requested
  • AIAgent::memory-read
  • AIAgent::memory-read-failed
  • AIAgent::memory-write-requested
  • AIAgent::memory-written
  • AIAgent::memory-write-failed
  • AIAgent::context-updated

Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

AIAgentEntity.jx
#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);
}