Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

⚡ app.js

nirholas5c6fbb676cb5
diff --git a/src/app.js b/src/app.js
new file mode 100644
index 000000000..aa375d2f1
--- /dev/null
+++ b/src/app.js
@@ -0,0 +1,161 @@
+import * as THREE from 'three';
+import WebGL from 'three/addons/capabilities/WebGL.js';
+import { Viewer } from './viewer.js';
+import { SimpleDropzone } from 'simple-dropzone';
+import { Validator } from './validator.js';
+import { Footer } from './components/footer';
+import queryString from 'query-string';
+
+window.THREE = THREE;
+window.VIEWER = {};
+
+if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
+ console.error('The File APIs are not fully supported in this browser.');
+} else if (!WebGL.isWebGL2Available()) {
+ console.error('WebGL is not supported in this browser.');
+}
+
+class App {
+ /**
+ * @param {Element} el
+ * @param {Location} location
+ */
+ constructor(el, location) {
+ const hash = location.hash ? queryString.parse(location.hash) : {};
+ this.options = {
+ kiosk: Boolean(hash.kiosk),
+ model: hash.model || '',
+ preset: hash.preset || '',
+ cameraPosition: hash.cameraPosition ? hash.cameraPosition.split(',').map(Number) : null,
+ };
+
+ this.el = el;
+ this.viewer = null;
+ this.viewerEl = null;
+ this.spinnerEl = el.querySelector('.spinner');
+ this.dropEl = el.querySelector('.wrap');
+ this.inputEl = el.querySelector('#file-input');
+ this.viewerContainerEl = el.querySelector('#viewer-container');
+ this.validator = new Validator(el);
+
+ this.createDropzone();
+ this.hideSpinner();
+
+ const options = this.options;
+
+ if (options.kiosk) {
+ const headerEl = document.querySelector('header');
+ headerEl.style.display = 'none';
+ }
+
+ // Load specified model or default CZ avatar
+ const model = options.model || '/avatars/cz.glb';
+ this.view(model, '', new Map());
+ }
+
+ /**
+ * Sets up the drag-and-drop controller.
+ */
+ createDropzone() {
+ const dropCtrl = new SimpleDropzone(this.dropEl, this.inputEl);
+ dropCtrl.on('drop', ({ files }) => this.load(files));
+ dropCtrl.on('dropstart', () => this.showSpinner());
+ dropCtrl.on('droperror', () => this.hideSpinner());
+ }
+
+ /**
+ * Sets up the view manager.
+ * @return {Viewer}
+ */
+ createViewer() {
+ this.viewerEl = this.viewerContainerEl;
+ this.viewer = new Viewer(this.viewerEl, this.options);
+ return this.viewer;
+ }
+
+ /**
+ * Loads a fileset provided by user action.
+ * @param {Map<string, File>} fileMap
+ */
+ load(fileMap) {
+ let rootFile;
+ let rootPath;
+ Array.from(fileMap).forEach(([path, file]) => {
+ if (file.name.match(/\.(gltf|glb)$/)) {
+ rootFile = file;
+ rootPath = path.replace(file.name, '');
+ }
+ });
+
+ if (!rootFile) {
+ this.onError('No .gltf or .glb asset found.');
+ }
+
+ this.view(rootFile, rootPath, fileMap);
+ }
+
+ /**
+ * Passes a model to the viewer, given file and resources.
+ * @param {File|string} rootFile
+ * @param {string} rootPath
+ * @param {Map<string, File>} fileMap
+ */
+ view(rootFile, rootPath, fileMap) {
+ if (this.viewer) this.viewer.clear();
+
+ const viewer = this.viewer || this.createViewer();
+
+ const fileURL = typeof rootFile === 'string' ? rootFile : URL.createObjectURL(rootFile);
+
+ const cleanup = () => {
+ this.hideSpinner();
+ if (typeof rootFile === 'object') URL.revokeObjectURL(fileURL);
+ };
+
+ viewer
+ .load(fileURL, rootPath, fileMap)
+ .catch((e) => this.onError(e))
+ .then((gltf) => {
+ // TODO: GLTFLoader parsing can fail on invalid files. Ideally,
+ // we could run the validator either way.
+ if (!this.options.kiosk) {
+ this.validator.validate(fileURL, rootPath, fileMap, gltf);
+ }
+ cleanup();
+ });
+ }
+
+ /**
+ * @param {Error} error
+ */
+ onError(error) {
+ let message = (error || {}).message || error.toString();
+ if (message.match(/ProgressEvent/)) {
+ message = 'Unable to retrieve this file. Check JS console and browser network tab.';
+ } else if (message.match(/Unexpected token/)) {
+ message = `Unable to parse file content. Verify that this file is valid. Error: "${message}"`;
+ } else if (error && error.target && error.target instanceof Image) {
+ message = 'Missing texture: ' + error.target.src.split('/').pop();
+ }
+ window.alert(message);
+ console.error(error);
+ }
+
+ showSpinner() {
+ this.spinnerEl.style.display = '';
+ }
+
+ hideSpinner() {
+ this.spinnerEl.style.display = 'none';
+ }
+}
+
+document.body.innerHTML += Footer();
+
+document.addEventListener('DOMContentLoaded', () => {
+ const app = new App(document.body, location);
+
+ window.VIEWER.app = app;
+
+ console.info('[3D Agent] Debugging data exported as `window.VIEWER`.');
+});