APTalkmoji Prefetching & Advanced Control
Docs›SDK Introduction

Prefetching & Advanced Control

The AvatarPlatform global namespace exposes methods for preloading configuration, asking questions asynchronously, and controlling the widget state from your own JavaScript.

AvatarPlatform.preload()

Pre-fetch a bot's configuration before the widget renders. Call this as early as possible — on page load or after the user hovers over a "Chat" button — to eliminate the initial config-fetch latency when the panel first opens.

// Warm the browser cache immediately on page load
AvatarPlatform.preload('YOUR_BOT_ID');

// Or await the config if you need to inspect it
const config = await AvatarPlatform.preload('YOUR_BOT_ID');
console.log(config.project.name);       // "My Support Bot"
console.log(config.character.rivePath); // "/assets/character-files//v1.riv"

Signature

AvatarPlatform.preload(botId: string): Promise<{
  project: ProjectConfig;
  character: { id: string; name: string; rivePath: string };
  captureFields: CaptureField[];
  apiKey: string;
  model: string;
}>

preload() calls GET /embed/:publicId/config and caches the response in the browser. Subsequent calls for the same botId return the cached value without a network request.

AvatarPlatform.ask()

Send a question to the agent and get back a text answer with source citations. This uses the async REST endpoint — no voice, no WebSocket, no widget required. Perfect for search bars, FAQ flows, and in-page tooltips.

const result = await AvatarPlatform.ask('YOUR_BOT_ID', 'How do I cancel my subscription?');

console.log(result.answer);
// → "To cancel your subscription, go to Settings → Billing → Cancel plan..."

console.log(result.sources);
// → [{ title: "Billing FAQ", url: null, snippet: "Subscriptions can be cancelled..." }]

console.log(result.sessionId);
// → "550e8400-e29b-41d4-a716-446655440000"

Continuing a conversation

Pass the sessionId back on follow-up questions to maintain context:

const first = await AvatarPlatform.ask('YOUR_BOT_ID', 'What is your refund policy?');
const followUp = await AvatarPlatform.ask('YOUR_BOT_ID', 'How long does it take?', first.sessionId);
// The second call has context from the first answer.

Signature

AvatarPlatform.ask(
  botId: string,
  question: string,
  sessionId?: string
): Promise<{
  answer: string;
  sources: { title: string; url: string | null; snippet: string }[];
  sessionId: string;
}>
ℹ️

ask() calls POST /embed/:publicId/ask. The endpoint embeds the question, performs a pgvector similarity search, calls Gemini REST (gemini-2.5-flash), and returns a complete answer. It is not streaming — the full answer is returned in one response.

Controlling the widget

You can open, close, hide, and show the widget launcher from any JavaScript on the page. The widget listens for custom events dispatched on document.

Open and close

// Open the chat panel
document.dispatchEvent(new CustomEvent('ap:open', { detail: { botId: 'YOUR_BOT_ID' } }));

// Close the chat panel (launcher remains visible)
document.dispatchEvent(new CustomEvent('ap:close', { detail: { botId: 'YOUR_BOT_ID' } }));

Hide and show the launcher

// Completely hide the widget (useful on checkout pages, full-screen modals)
document.dispatchEvent(new CustomEvent('ap:hide', { detail: { botId: 'YOUR_BOT_ID' } }));

// Bring it back
document.dispatchEvent(new CustomEvent('ap:show', { detail: { botId: 'YOUR_BOT_ID' } }));

React hook shorthand

If you're using the React SDK, the useAvatarPlatform hook wraps these events for you:

import { useAvatarPlatform } from '@avatar-platform/react';

function NavBar() {
  const { open, close, hide, show } = useAvatarPlatform('YOUR_BOT_ID');

  return (
    <nav>
      <button onClick={open}>Chat with us</button>
      <button onClick={hide}>Hide widget</button>
    </nav>
  );
}

Listening for events

The widget emits events on document so you can react to user actions:

// Fired when the user opens the panel
document.addEventListener('ap:opened', (e) => {
  console.log('Widget opened for bot:', e.detail.botId);
});

// Fired when a message is sent by the user
document.addEventListener('ap:message', (e) => {
  const { botId, sessionId, role, text } = e.detail;
  // Send to your own analytics
  analytics.track('chat_message', { botId, role });
});

// Fired when the agent responds
document.addEventListener('ap:response', (e) => {
  const { botId, sessionId, answer, sources } = e.detail;
});

Combining preload + ask

A common pattern for FAQ widgets: preload on hover, ask on click, show the answer inline — no widget needed:

const BOT_ID = 'YOUR_BOT_ID';

// Preload on hover so ask() is instant
document.getElementById('faq-btn').addEventListener('mouseenter', () => {
  AvatarPlatform.preload(BOT_ID);
});

// Ask on click, render answer inline
document.getElementById('faq-btn').addEventListener('click', async () => {
  const query = document.getElementById('faq-input').value;
  const result = await AvatarPlatform.ask(BOT_ID, query);
  document.getElementById('faq-answer').textContent = result.answer;
});
✅

See the Troubleshooting guide if AvatarPlatform is undefined — it means the SDK script tag hasn't loaded yet.