AI Agent Integration — How it works
The
window.postMessagecontract between the portal and an embedded AI. Overview: AI Agent Integration.
Where it lives
The bridge is the SupportAgent component
(components/pages/search/components/SupportAgent/), mounted in the search experience when
the aiAgent flag is on. It reads live search state via useSearch and talks to the agent
iframe over postMessage. Every message carries a generated sessionId.
The message protocol
Portal → Agent (the portal pushes context):
| Message | Payload | When |
|---|---|---|
InitSession | userAgent, userName | After the agent signals AgentReady. |
Listings | count, listings, statistics | On results change, or on RequestListings. |
ListingDetails | listing | When a listing is opened/cached. |
Filters | filters | Reply to ExtractFilters (NLP result). |
WindowVisible / WindowHidden | — | Chat panel open/close. |
CloseListing | — | When the listing dialog closes. |
Agent → Portal (the agent drives the portal):
| Message | Payload | Effect |
|---|---|---|
AgentReady | — | Portal sends InitSession + current Listings. |
RequestListings | — | Portal replies with Listings. |
ExtractFilters | query | Portal runs NLP (APIChat.fetchReply) → replies Filters. |
ApplyFilters | filters | Portal applies them (setFilters). |
ResetFilters | — | Portal clears filters. |
OpenListing | mlsNumber, boardId | Portal opens that listing. |
CloseListing | — | Portal clears the cached listing. |
ResetAgent | — | Portal resets the NLP token. |
Note the natural-language path: the agent sends ExtractFilters{query}, the
portal turns it into MLS filters via the backend NLP endpoint, returns
Filters, and the agent (or the user) can then ApplyFilters. The AI never
touches the MLS API — it goes through the portal.
Modes
- Embedded —
SupportAgentrenders a chat button + panel with the agent iframe (config.iframeUrlfromNEXT_PUBLIC_AI_AGENT_IFRAME_URL, withsessionIdappended). - Headless — when
headlessIframeIdis set,SupportAgentrenders nothing; it locates an external iframe already in the DOM (by id, via aMutationObserver) and posts to that. This is how a host system plugs its own AI in without portal UI.
Configuration
| Config / env / flag | Role |
|---|---|
@configs/features aiAgent | Enables the bridge. |
NEXT_PUBLIC_AI_AGENT_IFRAME_URL | The embedded agent’s iframe URL. |
@configs/ai-agent headlessIframeId | DOM id of an external iframe for headless mode (empty = embedded). |
Gotchas & hints
- The protocol is the integration surface. Any assistant that speaks these messages can be connected — the AI is swappable; the portal side is fixed.
- Session-scoped. The portal stamps
sessionIdon every outbound message and several inbound handlers ignore messages without it. - Headless renders no UI — if you enable
headlessIframeIdbut no matching iframe appears, nothing happens (the observer just keeps waiting). - NLP runs server-side through
APIChat.fetchReply; the returned token is reused across queries and reset byResetAgent.
Verified protocol boundary
Source review: e30dd9b47, 2026-11-02, including SupportAgent.tsx and
packages/ai-agent-client/src/App.tsx. Outbound messages use '*'; receivers do
not check origin/source or enforce session-id equality. Presence checks on some
messages are not session authentication. The reference client emits AgentReset,
while the portal handler is named ResetAgent, so that reset action does not
match. No origin hardening or protocol change was made in this docs task.
F03 traces extraction/apply separately and documents the null-filter failure path. This contract review does not claim an executed end-to-end assistant session.