# Untitled Page

> Source: https://parkbox.bg/agentIA/loader.js

## Page Content

/**
* agentIA — Loader
* This is the only file clients embed on their site.
*
* Usage:
* 
*
* The loader:
* 1. Reads the agent token from the script tag
* 2. Fetches agent config from widget-config.php
* 3. Loads the correct widget type JS
* 4. Widget initialises itself
*/

(function () {
'use strict';

// ─── SELF-LOCATE ─────────────────────────────────────────────────────────
var scripts = document.getElementsByTagName('script');
var thisScript = scripts[scripts.length - 1];
var scriptSrc = thisScript.src;
var baseUrl = scriptSrc.substring(0, scriptSrc.indexOf('loader.js'));

// ─── PARSE PARAMS ────────────────────────────────────────────────────────
var params = {};
var qs = scriptSrc.indexOf('?') !== -1 ? scriptSrc.split('?')[1] : '';
qs.split('&').forEach(function (pair) {
var kv = pair.split('=');
if (kv[0]) params[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1] || '');
});

var agentToken = params['agent'] || '';

if (!agentToken) {
console.warn('[agentIA] No agent token specified. Add ?agent=YOUR_TOKEN');
return;
}

// ─── FETCH CONFIG ────────────────────────────────────────────────────────
fetch(baseUrl + 'widget-config.php?agent=' + encodeURIComponent(agentToken))
.then(function (r) {
if (!r.ok) throw new Error('Agent not found');
return r.json();
})
.then(function (data) {
var agentType = (data.config && data.config.agent_type) || 'chat';

// Store config globally so widget.js can access it
window.__agentIA = {
token: agentToken,
baseUrl: baseUrl,
config: data.config || {},
questions: data.questions || [],
faq_tree: data.faq_tree || [],
flow: data.flow || {},
next_agent: data.next_agent || null,
params: params,
};

// Load the correct widget type
// Currently only 'chat' is built — more types will be added
var widgetFile = baseUrl + 'widget.js';

var s = document.createElement('script');
s.src = widgetFile;
s.async = true;
s.onerror = function () {
console.warn('[agentIA] Failed to load widget.');
};
document.head.appendChild(s);
})
.catch(function (err) {
console.warn('[agentIA] ' + err.message);
});

})();

---
*Served by AIble — aible.io*
