@asiyst/sdk 0.1.22 → 0.1.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -97,7 +97,7 @@ var ALL_ACTION_KINDS = [
97
97
  // src/core/constants.ts
98
98
  function resolveSdkVersion() {
99
99
  {
100
- return "0.1.21";
100
+ return "0.1.24";
101
101
  }
102
102
  }
103
103
  var SDK_VERSION = resolveSdkVersion();
@@ -115,6 +115,7 @@ var AVATAR_VIEWPORT_PADDING = 12;
115
115
  var AVATAR_TARGET_GAP = 16;
116
116
  var WEBSITE_MAP_TEXT_LIMIT = 80;
117
117
  var CONFIG_CACHE_TTL_MS = 1e3 * 60 * 10;
118
+ var HEARTBEAT_INTERVAL_MS = 45e3;
118
119
 
119
120
  // src/security/selectors.ts
120
121
  var DANGEROUS_SELECTOR = /javascript:|expression\(|@import|url\s*\(|behavior:|binding:|<script/i;
@@ -157,11 +158,12 @@ var ANCHORS = [
157
158
  "top-left"
158
159
  ];
159
160
  var PUBLIC_IDENTIFIER_PATTERN = /^(?=.{1,128}$)[A-Za-z0-9_][A-Za-z0-9_-]*$/;
161
+ var UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
160
162
  function isValidPublicIdentifier(value) {
161
163
  if (typeof value !== "string") return false;
162
164
  const trimmed = value.trim();
163
165
  if (!trimmed || trimmed.length > 128) return false;
164
- if (/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(trimmed)) {
166
+ if (UUID_PATTERN.test(trimmed)) {
165
167
  return false;
166
168
  }
167
169
  return PUBLIC_IDENTIFIER_PATTERN.test(trimmed);
@@ -393,18 +395,22 @@ function normalizeProjectConfig(raw) {
393
395
  return base;
394
396
  }
395
397
  const rawAvatarRecord = isRecord(raw.avatar) ? raw.avatar : void 0;
398
+ const rawConfigRecord = isRecord(raw.config) ? raw.config : void 0;
399
+ const rawManifestRecord = isRecord(raw.manifest) ? raw.manifest : void 0;
396
400
  const avatarId = asString(
397
- raw.avatarId ?? rawAvatarRecord?.avatarId ?? rawAvatarRecord?.id,
401
+ raw.avatarId ?? rawAvatarRecord?.avatarId ?? rawAvatarRecord?.id ?? rawConfigRecord?.avatarId,
398
402
  "",
399
403
  128
400
404
  ) || void 0;
401
- const assetUrl = typeof raw.assetUrl === "string" ? raw.assetUrl : typeof rawAvatarRecord?.assetUrl === "string" ? rawAvatarRecord.assetUrl : void 0;
402
- const thumbnailUrl = typeof raw.thumbnailUrl === "string" ? raw.thumbnailUrl : typeof rawAvatarRecord?.thumbnailUrl === "string" ? rawAvatarRecord.thumbnailUrl : rawAvatarRecord?.thumbnailUrl === null ? null : void 0;
403
- const publishedVersion = typeof raw.publishedVersion === "string" ? raw.publishedVersion : typeof rawAvatarRecord?.version === "string" ? rawAvatarRecord.version : void 0;
404
- const animations = parseAnimations(raw.animations ?? rawAvatarRecord?.animations);
405
- const branding = parseBranding(raw.branding);
405
+ const assetUrl = typeof raw.assetUrl === "string" && raw.assetUrl ? raw.assetUrl : typeof rawAvatarRecord?.assetUrl === "string" && rawAvatarRecord.assetUrl ? rawAvatarRecord.assetUrl : typeof rawAvatarRecord?.modelUrl === "string" && rawAvatarRecord.modelUrl ? rawAvatarRecord.modelUrl : typeof rawConfigRecord?.assetUrl === "string" && rawConfigRecord.assetUrl ? rawConfigRecord.assetUrl : typeof rawManifestRecord?.assetUrl === "string" && rawManifestRecord.assetUrl ? rawManifestRecord.assetUrl : typeof rawManifestRecord?.modelUrl === "string" && rawManifestRecord.modelUrl ? rawManifestRecord.modelUrl : void 0;
406
+ const thumbnailUrl = typeof raw.thumbnailUrl === "string" ? raw.thumbnailUrl : typeof rawAvatarRecord?.thumbnailUrl === "string" ? rawAvatarRecord.thumbnailUrl : typeof rawConfigRecord?.thumbnailUrl === "string" ? rawConfigRecord.thumbnailUrl : typeof rawManifestRecord?.thumbnailUrl === "string" ? rawManifestRecord.thumbnailUrl : rawAvatarRecord?.thumbnailUrl === null ? null : void 0;
407
+ const publishedVersion = typeof raw.publishedVersion === "string" ? raw.publishedVersion : typeof rawAvatarRecord?.version === "string" ? rawAvatarRecord.version : typeof rawManifestRecord?.version === "string" ? rawManifestRecord.version : void 0;
408
+ const animations = parseAnimations(
409
+ raw.animations ?? rawAvatarRecord?.animations ?? rawConfigRecord?.animations
410
+ );
411
+ const branding = parseBranding(raw.branding ?? rawAvatarRecord?.branding);
406
412
  const avatarName = asString(
407
- raw.avatarName ?? rawAvatarRecord?.name,
413
+ raw.avatarName ?? rawAvatarRecord?.name ?? rawConfigRecord?.avatarName ?? rawManifestRecord?.name,
408
414
  base.avatarName,
409
415
  40
410
416
  );
@@ -668,39 +674,58 @@ var CloudClient = class {
668
674
  const avatarAndKeyParams = `?projectId=${encodeURIComponent(this.projectId)}&publicKey=${encodeURIComponent(this.publicKey)}${this.avatarId ? `&avatarId=${encodeURIComponent(this.avatarId)}` : ""}`;
669
675
  const baseUrl = this.getApiBaseUrl();
670
676
  const resolveAssetUrls = (raw) => {
671
- const canonicalBase = baseUrl.includes("localhost") || baseUrl.includes("127.0.0.1") ? baseUrl.replace(/\/functions\/v1.*$/, "").replace(/\/api\/v1.*$/, "") : "https://asiyst.com";
672
- if (raw.avatar && typeof raw.avatar === "object" && !Array.isArray(raw.avatar)) {
673
- const av = { ...raw.avatar };
674
- if (typeof av.assetUrl === "string" && av.assetUrl.startsWith("/")) {
675
- av.assetUrl = new URL(av.assetUrl, canonicalBase).href;
677
+ let canonicalBase = "https://asiyst.com";
678
+ try {
679
+ if (baseUrl) {
680
+ const u = new URL(baseUrl);
681
+ canonicalBase = u.origin;
676
682
  }
677
- if (typeof av.thumbnailUrl === "string" && av.thumbnailUrl.startsWith("/")) {
678
- av.thumbnailUrl = new URL(av.thumbnailUrl, canonicalBase).href;
683
+ } catch {
684
+ canonicalBase = "https://asiyst.com";
685
+ }
686
+ const toAbsolute = (url) => {
687
+ if (typeof url === "string" && url.startsWith("/")) {
688
+ return new URL(url, canonicalBase).href;
679
689
  }
690
+ return url;
691
+ };
692
+ if (raw.avatar && typeof raw.avatar === "object" && !Array.isArray(raw.avatar)) {
693
+ const av = { ...raw.avatar };
694
+ av.assetUrl = toAbsolute(av.assetUrl);
695
+ av.thumbnailUrl = toAbsolute(av.thumbnailUrl);
696
+ av.modelUrl = toAbsolute(av.modelUrl);
697
+ av.manifestUrl = toAbsolute(av.manifestUrl);
680
698
  if (Array.isArray(av.animations)) {
681
699
  av.animations = av.animations.map((item) => {
682
700
  if (item && typeof item === "object") {
683
701
  const animObj = item;
684
- if (typeof animObj.assetUrl === "string" && animObj.assetUrl.startsWith("/")) {
685
- return { ...animObj, assetUrl: new URL(animObj.assetUrl, canonicalBase).href };
686
- }
702
+ return { ...animObj, assetUrl: toAbsolute(animObj.assetUrl) };
687
703
  }
688
704
  return item;
689
705
  });
690
706
  }
691
707
  raw.avatar = av;
692
708
  }
693
- if (typeof raw.assetUrl === "string" && raw.assetUrl.startsWith("/")) {
694
- raw.assetUrl = new URL(raw.assetUrl, canonicalBase).href;
709
+ if (raw.config && typeof raw.config === "object" && !Array.isArray(raw.config)) {
710
+ const cfg = { ...raw.config };
711
+ cfg.assetUrl = toAbsolute(cfg.assetUrl);
712
+ cfg.thumbnailUrl = toAbsolute(cfg.thumbnailUrl);
713
+ cfg.manifestUrl = toAbsolute(cfg.manifestUrl);
714
+ raw.config = cfg;
695
715
  }
696
- if (typeof raw.thumbnailUrl === "string" && raw.thumbnailUrl.startsWith("/")) {
697
- raw.thumbnailUrl = new URL(raw.thumbnailUrl, canonicalBase).href;
716
+ if (raw.manifest && typeof raw.manifest === "object" && !Array.isArray(raw.manifest)) {
717
+ const mf = { ...raw.manifest };
718
+ mf.assetUrl = toAbsolute(mf.assetUrl);
719
+ mf.thumbnailUrl = toAbsolute(mf.thumbnailUrl);
720
+ mf.modelUrl = toAbsolute(mf.modelUrl);
721
+ raw.manifest = mf;
698
722
  }
723
+ raw.assetUrl = toAbsolute(raw.assetUrl);
724
+ raw.thumbnailUrl = toAbsolute(raw.thumbnailUrl);
725
+ raw.manifestUrl = toAbsolute(raw.manifestUrl);
699
726
  if (raw.branding && typeof raw.branding === "object" && !Array.isArray(raw.branding)) {
700
727
  const br = { ...raw.branding };
701
- if (typeof br.siteLogoUrl === "string" && br.siteLogoUrl.startsWith("/")) {
702
- br.siteLogoUrl = new URL(br.siteLogoUrl, canonicalBase).href;
703
- }
728
+ br.siteLogoUrl = toAbsolute(br.siteLogoUrl);
704
729
  raw.branding = br;
705
730
  }
706
731
  return raw;
@@ -713,8 +738,11 @@ var CloudClient = class {
713
738
  idempotent: true
714
739
  });
715
740
  if (response.ok && response.data) {
716
- const dataObj = response.data.config ?? response.data;
717
- return normalizeProjectConfig(resolveAssetUrls(dataObj));
741
+ const fullData = response.data && typeof response.data === "object" && !Array.isArray(response.data) ? {
742
+ ...response.data,
743
+ ...response.data.config && typeof response.data.config === "object" ? response.data.config : {}
744
+ } : {};
745
+ return normalizeProjectConfig(resolveAssetUrls(fullData));
718
746
  }
719
747
  if (response.status === 404) {
720
748
  lastError = new ConfigurationError(`Project ${this.projectId} or avatar configuration not found`);
@@ -776,7 +804,7 @@ var CloudClient = class {
776
804
  throw new ConfigurationError(`Failed to resolve avatar configuration for project ${this.projectId}`);
777
805
  }
778
806
  async heartbeat(metadata) {
779
- const response = await this.runtimeRequest({
807
+ let response = await this.runtimeRequest({
780
808
  path: "/sdk/heartbeat",
781
809
  method: "POST",
782
810
  idempotent: true,
@@ -789,6 +817,21 @@ var CloudClient = class {
789
817
  timestamp: metadata.timestamp
790
818
  }
791
819
  }, "assistant:heartbeat");
820
+ if (response.status === 404) {
821
+ response = await this.runtimeRequest({
822
+ path: "/api/v1/sdk/heartbeat",
823
+ method: "POST",
824
+ idempotent: true,
825
+ body: {
826
+ project_id: this.projectId,
827
+ public_key: this.publicKey,
828
+ sdk_version: SDK_VERSION,
829
+ origin: metadata.origin,
830
+ environment: metadata.environment,
831
+ timestamp: metadata.timestamp
832
+ }
833
+ }, "assistant:heartbeat");
834
+ }
792
835
  const body = response.data && typeof response.data === "object" && !Array.isArray(response.data) ? response.data : void 0;
793
836
  const rawStatus = body?.status ?? body?.connectionStatus ?? body?.verificationStatus ?? (body?.verified === true ? "verified" : void 0);
794
837
  const verificationStatus = rawStatus === "connected" || rawStatus === "verified" || rawStatus === "inactive" || rawStatus === "not_detected" || rawStatus === "error" ? rawStatus : void 0;
@@ -801,13 +844,22 @@ var CloudClient = class {
801
844
  };
802
845
  }
803
846
  async registerInstallation(registration) {
804
- const response = await this.transport.request({
847
+ let response = await this.transport.request({
805
848
  path: "/sdk/installations/register",
806
849
  method: "POST",
807
850
  body: registration,
808
851
  idempotent: true,
809
852
  sessionId: this.sessionId
810
853
  });
854
+ if (response.status === 404) {
855
+ response = await this.transport.request({
856
+ path: "/api/v1/sdk/installations/register",
857
+ method: "POST",
858
+ body: registration,
859
+ idempotent: true,
860
+ sessionId: this.sessionId
861
+ });
862
+ }
811
863
  if (!response.ok) {
812
864
  throw new NetworkError(`SDK installation registration failed (${response.status}).`);
813
865
  }
@@ -1735,7 +1787,7 @@ var AssetLoader = class {
1735
1787
  }
1736
1788
  this.inFlightController = new AbortController();
1737
1789
  const abortSignal = options?.signal ? anySignal([this.inFlightController.signal, options.signal]) : this.inFlightController.signal;
1738
- const timeoutMs = options?.timeoutMs ?? 15e3;
1790
+ const timeoutMs = options?.timeoutMs ?? 6e4;
1739
1791
  const timeoutId = setTimeout(() => {
1740
1792
  this.inFlightController?.abort();
1741
1793
  }, timeoutMs);
@@ -2083,6 +2135,8 @@ var FbxRenderer = class {
2083
2135
  this.camera = null;
2084
2136
  this.modelRoot = null;
2085
2137
  this.mixer = null;
2138
+ this.allAnimations = [];
2139
+ this.currentAction = null;
2086
2140
  this.animFrameId = null;
2087
2141
  this.lastTime = 0;
2088
2142
  this.facing = "right";
@@ -2101,35 +2155,53 @@ var FbxRenderer = class {
2101
2155
  if (!glContext) {
2102
2156
  throw new Error("WebGLRenderingContext is not available or supported in this browser environment");
2103
2157
  }
2158
+ const isMobile = typeof window !== "undefined" && window.innerWidth <= 640;
2159
+ const width = options?.width || (isMobile ? 180 : 240);
2160
+ const height = options?.height || (isMobile ? 360 : 460);
2104
2161
  const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
2105
- const size = options?.config?.size || 96;
2106
- this.canvas.width = Math.round(size * dpr);
2107
- this.canvas.height = Math.round(size * dpr);
2162
+ this.canvas.width = Math.round(width * dpr);
2163
+ this.canvas.height = Math.round(height * dpr);
2164
+ this.canvas.style.width = `${width}px`;
2165
+ this.canvas.style.height = `${height}px`;
2108
2166
  try {
2109
2167
  this.renderer = new THREE__namespace.WebGLRenderer({
2110
2168
  canvas: this.canvas,
2111
2169
  alpha: true,
2112
2170
  antialias: true,
2113
- powerPreference: "low-power"
2171
+ powerPreference: "high-performance"
2114
2172
  });
2115
2173
  this.renderer.setPixelRatio(Math.min(dpr, 2));
2116
- this.renderer.setSize(size, size, false);
2174
+ this.renderer.setSize(width, height, false);
2117
2175
  this.renderer.outputColorSpace = THREE__namespace.SRGBColorSpace;
2176
+ this.renderer.shadowMap.enabled = true;
2177
+ this.renderer.shadowMap.type = THREE__namespace.PCFSoftShadowMap;
2118
2178
  } catch (err) {
2119
2179
  const msg = err instanceof Error ? err.message : String(err);
2120
2180
  throw new Error(`Failed to initialize WebGLRenderer: ${msg}`);
2121
2181
  }
2122
2182
  this.scene = new THREE__namespace.Scene();
2123
- this.camera = new THREE__namespace.PerspectiveCamera(40, 1, 0.1, 100);
2124
- this.camera.position.set(0, 0, 3.4);
2183
+ this.camera = new THREE__namespace.PerspectiveCamera(45, width / height, 0.1, 100);
2125
2184
  const ambientLight = new THREE__namespace.AmbientLight(16777215, 1.2);
2126
2185
  this.scene.add(ambientLight);
2127
- const keyLight = new THREE__namespace.DirectionalLight(16777215, 1.2);
2128
- keyLight.position.set(2, 3.5, 3);
2186
+ const keyLight = new THREE__namespace.DirectionalLight(16774634, 2);
2187
+ keyLight.position.set(3, 4, 3);
2188
+ keyLight.castShadow = true;
2189
+ keyLight.shadow.mapSize.width = 512;
2190
+ keyLight.shadow.mapSize.height = 512;
2129
2191
  this.scene.add(keyLight);
2130
- const fillLight = new THREE__namespace.DirectionalLight(10859772, 0.6);
2131
- fillLight.position.set(-2.5, -1, -1.5);
2192
+ const fillLight = new THREE__namespace.DirectionalLight(15459032, 1.2);
2193
+ fillLight.position.set(-3, 2, 2);
2132
2194
  this.scene.add(fillLight);
2195
+ const rimLight = new THREE__namespace.DirectionalLight(16771286, 1.5);
2196
+ rimLight.position.set(0, 4, -3);
2197
+ this.scene.add(rimLight);
2198
+ const shadowGeo = new THREE__namespace.PlaneGeometry(3, 3);
2199
+ const shadowMat = new THREE__namespace.ShadowMaterial({ opacity: 0.15 });
2200
+ const shadowPlane = new THREE__namespace.Mesh(shadowGeo, shadowMat);
2201
+ shadowPlane.rotation.x = -Math.PI / 2;
2202
+ shadowPlane.position.y = 0;
2203
+ shadowPlane.receiveShadow = true;
2204
+ this.scene.add(shadowPlane);
2133
2205
  let rawObject;
2134
2206
  let modelAnimations = [];
2135
2207
  if (asset.format === "fbx") {
@@ -2166,32 +2238,39 @@ var FbxRenderer = class {
2166
2238
  }
2167
2239
  }
2168
2240
  const { meshCount, vertexCount: totalVertices } = this.validateGeometry(rawObject);
2169
- const box = new THREE__namespace.Box3().setFromObject(rawObject);
2170
- if (box.isEmpty()) {
2241
+ const rawBox = new THREE__namespace.Box3().setFromObject(rawObject);
2242
+ if (rawBox.isEmpty()) {
2171
2243
  throw new Error("FBX asset geometry bounding box is empty");
2172
2244
  }
2173
- const center = new THREE__namespace.Vector3();
2174
- box.getCenter(center);
2175
- const boxSize = new THREE__namespace.Vector3();
2176
- box.getSize(boxSize);
2177
- rawObject.position.x -= center.x;
2178
- rawObject.position.y -= center.y;
2179
- rawObject.position.z -= center.z;
2245
+ const rawSize = rawBox.getSize(new THREE__namespace.Vector3());
2246
+ const rawCenter = rawBox.getCenter(new THREE__namespace.Vector3());
2247
+ const rawHeight = rawSize.y > 0 ? rawSize.y : 1.8;
2248
+ rawObject.position.set(-rawCenter.x, -rawBox.min.y, -rawCenter.z);
2249
+ const targetVisualHeight = 1.8;
2250
+ const targetScale = targetVisualHeight / rawHeight;
2180
2251
  this.modelRoot = new THREE__namespace.Group();
2181
2252
  this.modelRoot.add(rawObject);
2253
+ this.modelRoot.scale.setScalar(targetScale);
2182
2254
  this.scene.add(this.modelRoot);
2183
- const maxDim = Math.max(boxSize.x, boxSize.y, boxSize.z);
2184
- if (maxDim > 0) {
2185
- const targetSpan = 1.85;
2186
- const scale = targetSpan / maxDim;
2187
- this.modelRoot.scale.setScalar(scale);
2188
- }
2255
+ const visualCenterY = targetVisualHeight * 0.52;
2256
+ const fovRadians = this.camera.fov * Math.PI / 180;
2257
+ const fitDist = Math.max(2.4, targetVisualHeight / 2 / Math.tan(fovRadians / 2) * 1.25);
2258
+ this.camera.position.set(0, visualCenterY + 0.1, fitDist);
2259
+ this.camera.lookAt(0, visualCenterY, 0);
2260
+ rawObject.traverse((child) => {
2261
+ const mesh = child;
2262
+ if (mesh.isMesh) {
2263
+ mesh.castShadow = true;
2264
+ mesh.receiveShadow = true;
2265
+ }
2266
+ });
2267
+ this.allAnimations = modelAnimations;
2189
2268
  if (modelAnimations.length > 0) {
2190
2269
  this.mixer = new THREE__namespace.AnimationMixer(rawObject);
2191
- const clip = modelAnimations[0];
2192
- if (clip) {
2193
- const action = this.mixer.clipAction(clip);
2194
- action.play();
2270
+ const idleClip = modelAnimations.find((c) => c.name.toLowerCase().includes("idle")) || modelAnimations[0];
2271
+ if (idleClip) {
2272
+ this.currentAction = this.mixer.clipAction(idleClip);
2273
+ this.currentAction.play();
2195
2274
  }
2196
2275
  }
2197
2276
  container.replaceChildren();
@@ -2206,6 +2285,10 @@ var FbxRenderer = class {
2206
2285
  this.startLoop();
2207
2286
  this.setFacing(this.facing);
2208
2287
  this.setPose(this.currentPose);
2288
+ if (typeof window !== "undefined") {
2289
+ this.resizeListener = () => this.handleResize();
2290
+ window.addEventListener("resize", this.resizeListener);
2291
+ }
2209
2292
  return {
2210
2293
  meshCount,
2211
2294
  vertexCount: totalVertices,
@@ -2221,6 +2304,31 @@ var FbxRenderer = class {
2221
2304
  setPose(pose) {
2222
2305
  this.currentPose = pose;
2223
2306
  if (!this.modelRoot) return;
2307
+ if (this.mixer && this.allAnimations.length > 1) {
2308
+ let targetClip;
2309
+ if (pose === "speak") {
2310
+ targetClip = this.allAnimations.find(
2311
+ (c) => c.name.toLowerCase().includes("talk") || c.name.toLowerCase().includes("speak")
2312
+ );
2313
+ } else if (pose === "think") {
2314
+ targetClip = this.allAnimations.find(
2315
+ (c) => c.name.toLowerCase().includes("think")
2316
+ );
2317
+ } else {
2318
+ targetClip = this.allAnimations.find((c) => c.name.toLowerCase().includes("idle")) || this.allAnimations[0];
2319
+ }
2320
+ if (targetClip) {
2321
+ const newAction = this.mixer.clipAction(targetClip);
2322
+ if (newAction !== this.currentAction) {
2323
+ newAction.reset().fadeIn(0.25).play();
2324
+ if (this.currentAction) {
2325
+ this.currentAction.fadeOut(0.25);
2326
+ }
2327
+ this.currentAction = newAction;
2328
+ return;
2329
+ }
2330
+ }
2331
+ }
2224
2332
  switch (pose) {
2225
2333
  case "point":
2226
2334
  this.modelRoot.rotation.z = -0.15;
@@ -2230,6 +2338,10 @@ var FbxRenderer = class {
2230
2338
  this.modelRoot.rotation.z = 0.12;
2231
2339
  this.modelRoot.rotation.x = -0.1;
2232
2340
  break;
2341
+ case "speak":
2342
+ this.modelRoot.rotation.y = this.facing === "left" ? -0.22 : 0.22;
2343
+ this.modelRoot.rotation.x = 0.04;
2344
+ break;
2233
2345
  case "celebrate":
2234
2346
  this.modelRoot.rotation.z = 0;
2235
2347
  this.modelRoot.rotation.x = -0.15;
@@ -2238,6 +2350,7 @@ var FbxRenderer = class {
2238
2350
  default:
2239
2351
  this.modelRoot.rotation.z = 0;
2240
2352
  this.modelRoot.rotation.x = 0;
2353
+ this.modelRoot.rotation.y = this.facing === "left" ? -0.35 : 0.35;
2241
2354
  break;
2242
2355
  }
2243
2356
  }
@@ -2282,6 +2395,10 @@ var FbxRenderer = class {
2282
2395
  this.animFrameId = requestAnimationFrame(tick);
2283
2396
  }
2284
2397
  destroy() {
2398
+ if (typeof window !== "undefined" && this.resizeListener) {
2399
+ window.removeEventListener("resize", this.resizeListener);
2400
+ this.resizeListener = void 0;
2401
+ }
2285
2402
  if (this.animFrameId !== null && typeof cancelAnimationFrame !== "undefined") {
2286
2403
  cancelAnimationFrame(this.animFrameId);
2287
2404
  this.animFrameId = null;
@@ -2327,6 +2444,30 @@ var FbxRenderer = class {
2327
2444
  this.modelRoot = null;
2328
2445
  this.initialRenderCompleted = false;
2329
2446
  }
2447
+ handleResize() {
2448
+ if (!this.canvas || !this.renderer || !this.camera) return;
2449
+ const isMobile = typeof window !== "undefined" && window.innerWidth <= 640;
2450
+ const width = isMobile ? 180 : 240;
2451
+ const height = isMobile ? 360 : 460;
2452
+ const dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;
2453
+ this.canvas.width = Math.round(width * dpr);
2454
+ this.canvas.height = Math.round(height * dpr);
2455
+ this.canvas.style.width = `${width}px`;
2456
+ this.canvas.style.height = `${height}px`;
2457
+ this.camera.aspect = width / height;
2458
+ this.camera.updateProjectionMatrix();
2459
+ this.renderer.setPixelRatio(Math.min(dpr, 2));
2460
+ this.renderer.setSize(width, height, false);
2461
+ }
2462
+ getCamera() {
2463
+ return this.camera;
2464
+ }
2465
+ getModelRoot() {
2466
+ return this.modelRoot;
2467
+ }
2468
+ getMixer() {
2469
+ return this.mixer;
2470
+ }
2330
2471
  };
2331
2472
 
2332
2473
  // src/avatar/CssAvatarRenderer.ts
@@ -2337,7 +2478,7 @@ var AVATAR_CSS = `
2337
2478
  display: flex;
2338
2479
  flex-direction: column;
2339
2480
  align-items: center;
2340
- gap: 6px;
2481
+ gap: 4px;
2341
2482
  pointer-events: auto;
2342
2483
  user-select: none;
2343
2484
  }
@@ -2358,28 +2499,38 @@ var AVATAR_CSS = `
2358
2499
  padding: 0;
2359
2500
  outline: none;
2360
2501
  background: #2563eb;
2502
+ transition: width 0.3s ease, height 0.3s ease, background 0.3s ease;
2503
+ }
2504
+ .asiyst-figure.asiyst-3d-active {
2505
+ width: 240px !important;
2506
+ height: 460px !important;
2507
+ background: transparent !important;
2508
+ border-radius: 0 !important;
2509
+ box-shadow: none !important;
2510
+ overflow: visible !important;
2511
+ }
2512
+ @media (max-width: 640px) {
2513
+ .asiyst-figure.asiyst-3d-active {
2514
+ width: 180px !important;
2515
+ height: 360px !important;
2516
+ }
2361
2517
  }
2362
- .asiyst-figure[data-pose="point"] { transform: rotate(-12deg); }
2363
- .asiyst-figure[data-pose="think"] { opacity: 0.85; }
2364
- .asiyst-figure[data-pose="celebrate"] { transform: scale(1.08); }
2365
2518
  .asiyst-avatar-viewport {
2366
- position: absolute;
2367
- inset: 0;
2519
+ position: relative;
2368
2520
  width: 100%;
2369
2521
  height: 100%;
2370
2522
  display: grid;
2371
2523
  place-items: center;
2372
2524
  border-radius: inherit;
2373
- overflow: hidden;
2374
- pointer-events: none;
2525
+ overflow: visible;
2526
+ pointer-events: auto;
2375
2527
  }
2376
2528
  .asiyst-avatar-canvas {
2377
2529
  width: 100%;
2378
2530
  height: 100%;
2379
2531
  object-fit: contain;
2380
- border-radius: inherit;
2381
2532
  display: block;
2382
- pointer-events: none;
2533
+ pointer-events: auto;
2383
2534
  }
2384
2535
  .asiyst-avatar-graphic {
2385
2536
  width: 100%;
@@ -2433,11 +2584,73 @@ var AVATAR_CSS = `
2433
2584
  padding: 8px 10px;
2434
2585
  font: 13px/1.4 system-ui, sans-serif;
2435
2586
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.2);
2587
+ margin-bottom: 4px;
2588
+ }
2589
+ .asiyst-name-tag {
2590
+ display: inline-flex;
2591
+ align-items: center;
2592
+ gap: 6px;
2593
+ background: rgba(255, 255, 255, 0.95);
2594
+ backdrop-filter: blur(8px);
2595
+ border: 1px solid rgba(226, 232, 240, 0.9);
2596
+ border-radius: 9999px;
2597
+ padding: 4px 12px;
2598
+ font-size: 11px;
2599
+ font-weight: 700;
2600
+ color: #1e293b;
2601
+ box-shadow: 0 4px 14px rgba(15, 23, 42, 0.12);
2602
+ user-select: none;
2603
+ margin-bottom: 2px;
2604
+ pointer-events: none;
2605
+ transition: opacity 0.2s ease;
2606
+ }
2607
+ .asiyst-name-sparkle { color: #f59e0b; font-size: 12px; font-weight: 900; }
2608
+ .asiyst-name-dot {
2609
+ width: 6px;
2610
+ height: 6px;
2611
+ border-radius: 50%;
2612
+ background: #10b981;
2613
+ display: inline-block;
2614
+ animation: asiyst-pulse 2s infinite;
2615
+ }
2616
+ .asiyst-name-status {
2617
+ font-size: 10px;
2618
+ color: #64748b;
2619
+ font-weight: 500;
2620
+ text-transform: uppercase;
2621
+ }
2622
+ @keyframes asiyst-pulse {
2623
+ 0%, 100% { opacity: 1; transform: scale(1); }
2624
+ 50% { opacity: 0.4; transform: scale(1.3); }
2625
+ }
2626
+ .asiyst-open-chat-btn {
2627
+ display: inline-flex;
2628
+ align-items: center;
2629
+ gap: 6px;
2630
+ background: #0f172a;
2631
+ color: #fbbf24;
2632
+ border: 1px solid rgba(245, 158, 11, 0.6);
2633
+ border-radius: 9999px;
2634
+ padding: 6px 14px;
2635
+ font-size: 12px;
2636
+ font-weight: 700;
2637
+ box-shadow: 0 8px 24px rgba(15, 23, 42, 0.25);
2638
+ cursor: pointer;
2639
+ margin-top: 4px;
2640
+ pointer-events: auto;
2641
+ transition: transform 0.15s ease, background-color 0.15s ease;
2642
+ }
2643
+ .asiyst-open-chat-btn:hover {
2644
+ transform: scale(1.05);
2645
+ background: #1e293b;
2646
+ }
2647
+ .asiyst-open-chat-btn svg {
2648
+ stroke: #fbbf24;
2436
2649
  }
2437
- .asiyst-name { font: 600 12px/1 system-ui, sans-serif; color: #0f172a; }
2438
2650
  @media (prefers-reduced-motion: reduce) {
2439
2651
  .asiyst-avatar.asiyst-animate { transition: none; }
2440
2652
  .asiyst-avatar-loader { animation: none; }
2653
+ .asiyst-name-dot { animation: none; }
2441
2654
  }
2442
2655
  `;
2443
2656
  var CssAvatarRenderer = class {
@@ -2448,6 +2661,7 @@ var CssAvatarRenderer = class {
2448
2661
  this.pose = "idle";
2449
2662
  this.accentColor = "#2563eb";
2450
2663
  this.avatarName = "Asiyst";
2664
+ this.isPanelOpen = false;
2451
2665
  // Asset loading & rendering state
2452
2666
  this.assetLoader = new AssetLoader();
2453
2667
  this.fbxRenderer = null;
@@ -2463,6 +2677,20 @@ var CssAvatarRenderer = class {
2463
2677
  this.root = doc.createElement("div");
2464
2678
  this.root.className = "asiyst-avatar asiyst-animate";
2465
2679
  this.root.setAttribute("role", "img");
2680
+ this.nameTagEl = doc.createElement("div");
2681
+ this.nameTagEl.className = "asiyst-name-tag";
2682
+ const sparkle = doc.createElement("span");
2683
+ sparkle.className = "asiyst-name-sparkle";
2684
+ sparkle.textContent = "\u2726";
2685
+ this.nameTextEl = doc.createElement("span");
2686
+ this.nameTextEl.className = "asiyst-name-text";
2687
+ this.nameTextEl.textContent = config.avatarName;
2688
+ const dot = doc.createElement("span");
2689
+ dot.className = "asiyst-name-dot";
2690
+ this.statusTextEl = doc.createElement("span");
2691
+ this.statusTextEl.className = "asiyst-name-status";
2692
+ this.statusTextEl.textContent = "(IDLE)";
2693
+ this.nameTagEl.append(sparkle, this.nameTextEl, dot, this.statusTextEl);
2466
2694
  this.figure = doc.createElement("button");
2467
2695
  this.figure.type = "button";
2468
2696
  this.figure.className = "asiyst-figure";
@@ -2471,12 +2699,26 @@ var CssAvatarRenderer = class {
2471
2699
  this.viewport = doc.createElement("div");
2472
2700
  this.viewport.className = "asiyst-avatar-viewport";
2473
2701
  this.figure.appendChild(this.viewport);
2474
- this.nameEl = doc.createElement("div");
2475
- this.nameEl.className = "asiyst-name";
2476
2702
  this.bubble = doc.createElement("div");
2477
2703
  this.bubble.className = "asiyst-bubble";
2478
2704
  this.bubble.hidden = true;
2479
- this.root.append(this.bubble, this.figure, this.nameEl);
2705
+ this.chatActionBtn = doc.createElement("button");
2706
+ this.chatActionBtn.type = "button";
2707
+ this.chatActionBtn.className = "asiyst-open-chat-btn";
2708
+ this.chatActionBtn.innerHTML = `
2709
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
2710
+ <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"></path>
2711
+ </svg>
2712
+ <span>Chat with ${config.avatarName}</span>
2713
+ `;
2714
+ this.chatActionBtn.addEventListener("click", (e) => {
2715
+ e.stopPropagation();
2716
+ this.onOpenChat?.();
2717
+ });
2718
+ this.figure.addEventListener("click", () => {
2719
+ this.onOpenChat?.();
2720
+ });
2721
+ this.root.append(this.bubble, this.nameTagEl, this.figure, this.chatActionBtn);
2480
2722
  container.appendChild(this.root);
2481
2723
  this.applyConfig(config);
2482
2724
  }
@@ -2490,24 +2732,51 @@ var CssAvatarRenderer = class {
2490
2732
  this.root = void 0;
2491
2733
  this.figure = void 0;
2492
2734
  this.viewport = void 0;
2735
+ this.nameTagEl = void 0;
2736
+ this.chatActionBtn = void 0;
2493
2737
  }
2494
2738
  applyConfig(config) {
2495
2739
  this.size = config.size;
2496
2740
  this.accentColor = config.theme.accent ?? "#2563eb";
2497
2741
  this.avatarName = config.avatarName;
2498
- if (this.figure) {
2499
- this.figure.style.width = `${config.size}px`;
2500
- this.figure.style.height = `${config.size}px`;
2501
- this.figure.style.background = this.accentColor;
2742
+ if (this.nameTextEl) {
2743
+ renderSafeText(this.nameTextEl, config.avatarName);
2502
2744
  }
2503
- if (this.nameEl) {
2504
- renderSafeText(this.nameEl, config.avatarName);
2745
+ if (this.chatActionBtn) {
2746
+ const span = this.chatActionBtn.querySelector("span");
2747
+ if (span) {
2748
+ renderSafeText(span, `Chat with ${config.avatarName}`);
2749
+ }
2505
2750
  }
2506
2751
  if (this.root) {
2507
2752
  this.root.setAttribute("aria-label", config.avatarName);
2508
2753
  }
2509
- if (!this.isAssetLoadedFlag && this.viewport) {
2510
- this.renderFallback();
2754
+ if (!this.isAssetLoadedFlag) {
2755
+ if (this.figure) {
2756
+ this.figure.classList.remove("asiyst-3d-active");
2757
+ this.figure.style.width = `${config.size}px`;
2758
+ this.figure.style.height = `${config.size}px`;
2759
+ this.figure.style.background = this.accentColor;
2760
+ }
2761
+ if (this.chatActionBtn) {
2762
+ this.chatActionBtn.style.display = "none";
2763
+ }
2764
+ if (this.viewport) {
2765
+ this.renderFallback();
2766
+ }
2767
+ } else {
2768
+ if (this.figure) {
2769
+ this.figure.classList.add("asiyst-3d-active");
2770
+ }
2771
+ if (this.chatActionBtn) {
2772
+ this.chatActionBtn.style.display = this.isPanelOpen ? "none" : "inline-flex";
2773
+ }
2774
+ }
2775
+ }
2776
+ setPanelOpen(open) {
2777
+ this.isPanelOpen = open;
2778
+ if (this.chatActionBtn && this.isAssetLoadedFlag) {
2779
+ this.chatActionBtn.style.display = open ? "none" : "inline-flex";
2511
2780
  }
2512
2781
  }
2513
2782
  async loadAsset(config) {
@@ -2528,7 +2797,7 @@ var CssAvatarRenderer = class {
2528
2797
  }
2529
2798
  this.renderLoader();
2530
2799
  try {
2531
- const asset = await this.assetLoader.load(assetUrl);
2800
+ const asset = await this.assetLoader.load(assetUrl, { timeoutMs: 6e4 });
2532
2801
  await this.renderLoadedAsset(asset, config);
2533
2802
  this.renderState = "ready";
2534
2803
  this.isAssetLoadedFlag = true;
@@ -2536,6 +2805,13 @@ var CssAvatarRenderer = class {
2536
2805
  this.loadedFormat = asset.format;
2537
2806
  if (this.figure) {
2538
2807
  this.figure.setAttribute("data-state", "ready");
2808
+ this.figure.classList.add("asiyst-3d-active");
2809
+ this.figure.style.width = "";
2810
+ this.figure.style.height = "";
2811
+ this.figure.style.background = "transparent";
2812
+ }
2813
+ if (this.chatActionBtn) {
2814
+ this.chatActionBtn.style.display = this.isPanelOpen ? "none" : "inline-flex";
2539
2815
  }
2540
2816
  return true;
2541
2817
  } catch (err) {
@@ -2546,6 +2822,13 @@ var CssAvatarRenderer = class {
2546
2822
  this.isFallbackFlag = true;
2547
2823
  if (this.figure) {
2548
2824
  this.figure.setAttribute("data-state", "error");
2825
+ this.figure.classList.remove("asiyst-3d-active");
2826
+ this.figure.style.width = `${this.size}px`;
2827
+ this.figure.style.height = `${this.size}px`;
2828
+ this.figure.style.background = this.accentColor;
2829
+ }
2830
+ if (this.chatActionBtn) {
2831
+ this.chatActionBtn.style.display = "none";
2549
2832
  }
2550
2833
  this.renderFallback(true);
2551
2834
  return false;
@@ -2567,10 +2850,15 @@ var CssAvatarRenderer = class {
2567
2850
  this.fbxRenderer = null;
2568
2851
  }
2569
2852
  if (asset.format === "fbx" || asset.format === "glb") {
2853
+ const isMobile = typeof window !== "undefined" && window.innerWidth <= 640;
2854
+ const width = isMobile ? 180 : 240;
2855
+ const height = isMobile ? 360 : 460;
2570
2856
  this.fbxRenderer = new FbxRenderer();
2571
2857
  this.renderMetrics = await this.fbxRenderer.mount(this.viewport, asset, {
2572
2858
  accentColor: this.accentColor,
2573
- config
2859
+ config,
2860
+ width,
2861
+ height
2574
2862
  });
2575
2863
  this.fbxRenderer.setFacing(this.facing);
2576
2864
  this.fbxRenderer.setPose(this.pose);
@@ -2586,7 +2874,7 @@ var CssAvatarRenderer = class {
2586
2874
  const thumb = typeof asset.manifest.thumbnailUrl === "string" ? asset.manifest.thumbnailUrl : void 0;
2587
2875
  const modelUrl = typeof asset.manifest.assetUrl === "string" ? asset.manifest.assetUrl : typeof asset.manifest.modelUrl === "string" ? asset.manifest.modelUrl : void 0;
2588
2876
  if (modelUrl) {
2589
- const subAsset = await this.assetLoader.load(modelUrl);
2877
+ const subAsset = await this.assetLoader.load(modelUrl, { timeoutMs: 6e4 });
2590
2878
  await this.renderLoadedAsset(subAsset, config);
2591
2879
  } else if (thumb) {
2592
2880
  this.viewport.replaceChildren();
@@ -2618,7 +2906,6 @@ var CssAvatarRenderer = class {
2618
2906
  if (isError) {
2619
2907
  const badge = doc.createElement("span");
2620
2908
  badge.className = "asiyst-avatar-fallback-badge";
2621
- badge.setAttribute("title", "Avatar asset failed to load");
2622
2909
  badge.textContent = "!";
2623
2910
  fallback.appendChild(badge);
2624
2911
  }
@@ -2626,66 +2913,72 @@ var CssAvatarRenderer = class {
2626
2913
  }
2627
2914
  setPosition(x, y, animate) {
2628
2915
  if (!this.root) return;
2629
- this.root.classList.toggle("asiyst-animate", animate);
2630
- this.root.style.left = `${x}px`;
2631
- this.root.style.top = `${y}px`;
2916
+ if (animate) {
2917
+ this.root.classList.add("asiyst-animate");
2918
+ } else {
2919
+ this.root.classList.remove("asiyst-animate");
2920
+ }
2921
+ this.root.style.left = `${Math.round(x)}px`;
2922
+ this.root.style.top = `${Math.round(y)}px`;
2632
2923
  }
2633
2924
  setFacing(direction) {
2634
2925
  this.facing = direction;
2635
2926
  this.fbxRenderer?.setFacing(direction);
2636
- this.syncTransform();
2637
2927
  }
2638
2928
  setPose(pose) {
2639
2929
  this.pose = pose;
2930
+ if (this.figure) {
2931
+ this.figure.setAttribute("data-pose", pose);
2932
+ }
2933
+ if (this.statusTextEl) {
2934
+ const displayStatus = pose === "speak" ? "(SPEAKING)" : pose === "think" ? "(THINKING)" : pose === "celebrate" ? "(CELEBRATING)" : "(IDLE)";
2935
+ this.statusTextEl.textContent = displayStatus;
2936
+ }
2640
2937
  this.fbxRenderer?.setPose(pose);
2641
- this.figure?.setAttribute("data-pose", pose);
2642
- this.syncTransform();
2643
- }
2644
- syncTransform() {
2645
- if (!this.figure) return;
2646
- const face = this.facing === "left" ? "scaleX(-1)" : "";
2647
- const pose = this.pose === "point" ? "rotate(-12deg)" : this.pose === "celebrate" ? "scale(1.08)" : "";
2648
- this.figure.style.transform = `${face} ${pose}`.trim();
2649
2938
  }
2650
2939
  setSpeech(text) {
2651
2940
  if (!this.bubble) return;
2652
2941
  if (!text) {
2653
2942
  this.bubble.hidden = true;
2654
- this.bubble.textContent = "";
2943
+ this.bubble.replaceChildren();
2655
2944
  return;
2656
2945
  }
2657
2946
  this.bubble.hidden = false;
2658
2947
  renderSafeText(this.bubble, text);
2659
2948
  }
2660
2949
  getSize() {
2950
+ if (this.isAssetLoadedFlag && (this.loadedFormat === "fbx" || this.loadedFormat === "glb")) {
2951
+ const isMobile = typeof window !== "undefined" && window.innerWidth <= 640;
2952
+ return { width: isMobile ? 180 : 240, height: isMobile ? 390 : 490 };
2953
+ }
2661
2954
  return { width: this.size, height: this.size + 28 };
2662
2955
  }
2663
2956
  setHidden(hidden) {
2664
2957
  this.hidden = hidden;
2665
2958
  if (this.root) {
2666
- this.root.hidden = hidden;
2959
+ this.root.style.display = hidden ? "none" : "";
2667
2960
  }
2668
2961
  }
2669
2962
  isHidden() {
2670
2963
  return this.hidden;
2671
2964
  }
2672
- getFigure() {
2673
- return this.figure;
2674
- }
2675
- getViewport() {
2676
- return this.viewport;
2965
+ getRenderState() {
2966
+ return this.renderState;
2677
2967
  }
2678
2968
  isAssetLoaded() {
2679
2969
  return this.isAssetLoadedFlag;
2680
2970
  }
2681
- isRendered() {
2682
- return this.isAssetLoadedFlag && (this.fbxRenderer ? this.fbxRenderer.isRendered() : true);
2971
+ getViewport() {
2972
+ return this.viewport;
2973
+ }
2974
+ isFallback() {
2975
+ return this.isFallbackFlag;
2683
2976
  }
2684
2977
  isFallbackActive() {
2685
2978
  return this.isFallbackFlag;
2686
2979
  }
2687
- getRenderState() {
2688
- return this.renderState;
2980
+ isRendered() {
2981
+ return this.isAssetLoadedFlag && (this.fbxRenderer ? this.fbxRenderer.isRendered() : true);
2689
2982
  }
2690
2983
  getLoadedFormat() {
2691
2984
  return this.loadedFormat;
@@ -2699,6 +2992,18 @@ var CssAvatarRenderer = class {
2699
2992
  getMetrics() {
2700
2993
  return this.renderMetrics;
2701
2994
  }
2995
+ getFigure() {
2996
+ return this.figure;
2997
+ }
2998
+ getFbxRenderer() {
2999
+ return this.fbxRenderer;
3000
+ }
3001
+ getChatActionBtn() {
3002
+ return this.chatActionBtn;
3003
+ }
3004
+ getNameTag() {
3005
+ return this.nameTagEl;
3006
+ }
2702
3007
  };
2703
3008
 
2704
3009
  // src/accessibility/a11y.ts
@@ -2882,6 +3187,8 @@ var MovementEngine = class {
2882
3187
  this.onViewport = throttle(() => {
2883
3188
  if (this.followTarget) {
2884
3189
  this.placeBeside(this.followTarget, false);
3190
+ } else {
3191
+ this.goToAnchor();
2885
3192
  }
2886
3193
  }, VIEWPORT_HANDLER_THROTTLE_MS);
2887
3194
  }
@@ -3036,6 +3343,9 @@ function isLikelyJavaScriptUrl(value) {
3036
3343
  const lower = value.trim().toLowerCase();
3037
3344
  return lower.startsWith("javascript:") || lower.startsWith("data:") || lower.startsWith("vbscript:") || lower.includes("eval(") || lower.includes("new function") || lower.includes("document.cookie");
3038
3345
  }
3346
+ function isLocalHost(host) {
3347
+ return host === "localhost" || host === "127.0.0.1" || host === "::1" || host === "[::1]";
3348
+ }
3039
3349
  function validateWebsiteDomain(config, currentUrl) {
3040
3350
  const allowedDomains = config.allowedDomains.map((domain) => normalizeHost(domain));
3041
3351
  if (allowedDomains.length === 0) {
@@ -3044,7 +3354,16 @@ function validateWebsiteDomain(config, currentUrl) {
3044
3354
  try {
3045
3355
  const parsed = new URL(currentUrl);
3046
3356
  const host = normalizeHost(parsed.host);
3047
- const allowed = allowedDomains.some((domain) => host === domain || host.endsWith(`.${domain}`));
3357
+ const hostIsLocal = isLocalHost(host);
3358
+ const allowed = allowedDomains.some((domain) => {
3359
+ if (host === domain || host.endsWith(`.${domain}`)) {
3360
+ return true;
3361
+ }
3362
+ if (hostIsLocal && isLocalHost(domain)) {
3363
+ return true;
3364
+ }
3365
+ return false;
3366
+ });
3048
3367
  if (!allowed) {
3049
3368
  return { allowed: false, reason: "DOMAIN_NOT_AUTHORIZED" };
3050
3369
  }
@@ -3487,27 +3806,39 @@ var ConversationPanel = class {
3487
3806
  this.style.textContent = `
3488
3807
  .asiyst-panel {
3489
3808
  position: fixed;
3490
- right: 16px;
3491
- bottom: 16px;
3492
- width: min(360px, calc(100vw - 32px));
3493
- max-height: min(480px, 70vh);
3494
- background: #fff;
3809
+ right: 256px;
3810
+ bottom: 20px;
3811
+ width: min(380px, calc(100vw - 280px));
3812
+ max-height: min(560px, 75vh);
3813
+ background: #ffffff;
3495
3814
  color: #0f172a;
3496
- border-radius: 16px;
3497
- box-shadow: 0 16px 50px rgba(15, 23, 42, 0.22);
3815
+ border-radius: 18px;
3816
+ box-shadow: 0 20px 48px rgba(15, 23, 42, 0.25), 0 0 0 1px rgba(15, 23, 42, 0.08);
3498
3817
  display: flex;
3499
3818
  flex-direction: column;
3500
- font: 14px/1.45 system-ui, sans-serif;
3819
+ font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
3501
3820
  overflow: hidden;
3821
+ z-index: 100000;
3822
+ transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease;
3823
+ }
3824
+ @media (max-width: 640px) {
3825
+ .asiyst-panel {
3826
+ right: 12px;
3827
+ bottom: 12px;
3828
+ left: 12px;
3829
+ width: auto;
3830
+ max-height: 70vh;
3831
+ }
3502
3832
  }
3503
3833
  .asiyst-panel[hidden] { display: none; }
3504
3834
  .asiyst-panel header {
3505
3835
  display: flex;
3506
3836
  justify-content: space-between;
3507
3837
  align-items: center;
3508
- padding: 12px 14px;
3838
+ padding: 14px 16px;
3509
3839
  background: #0f172a;
3510
3840
  color: #f8fafc;
3841
+ border-bottom: 1px solid rgba(255, 255, 255, 0.1);
3511
3842
  }
3512
3843
  .asiyst-log { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
3513
3844
  .asiyst-msg { padding: 8px 10px; border-radius: 10px; max-width: 90%; }
@@ -3696,23 +4027,36 @@ var RuntimeAuth = class {
3696
4027
  const timeout = setTimeout(() => controller.abort(), 1e4);
3697
4028
  let response;
3698
4029
  try {
3699
- response = await fetch(`${this.options.apiBaseUrl.replace(/\/$/, "")}/sdk/runtime/token`, {
4030
+ const baseUrl = this.options.apiBaseUrl.replace(/\/$/, "");
4031
+ const payload = JSON.stringify({
4032
+ projectId: this.options.projectId,
4033
+ publicKey: this.options.publicKey,
4034
+ installationId: this.options.installationId,
4035
+ origin: this.options.origin,
4036
+ environment: this.options.environment,
4037
+ sdkVersion: SDK_VERSION,
4038
+ capabilities: [...RUNTIME_CAPABILITIES]
4039
+ });
4040
+ response = await fetch(`${baseUrl}/sdk/runtime/token`, {
3700
4041
  method: "POST",
3701
4042
  headers: {
3702
4043
  Accept: "application/json",
3703
4044
  "Content-Type": "application/json"
3704
4045
  },
3705
- body: JSON.stringify({
3706
- projectId: this.options.projectId,
3707
- publicKey: this.options.publicKey,
3708
- installationId: this.options.installationId,
3709
- origin: this.options.origin,
3710
- environment: this.options.environment,
3711
- sdkVersion: SDK_VERSION,
3712
- capabilities: [...RUNTIME_CAPABILITIES]
3713
- }),
4046
+ body: payload,
3714
4047
  signal: controller.signal
3715
4048
  });
4049
+ if (response.status === 404) {
4050
+ response = await fetch(`${baseUrl}/api/v1/sdk/runtime/token`, {
4051
+ method: "POST",
4052
+ headers: {
4053
+ Accept: "application/json",
4054
+ "Content-Type": "application/json"
4055
+ },
4056
+ body: payload,
4057
+ signal: controller.signal
4058
+ });
4059
+ }
3716
4060
  } catch {
3717
4061
  throw new NetworkError("ASIYST runtime authentication is unavailable.");
3718
4062
  } finally {
@@ -3859,6 +4203,9 @@ var AsiystRuntime = class {
3859
4203
  this.connectionStatus = "disconnected";
3860
4204
  this.verificationStatus = "not_detected";
3861
4205
  this.rescanDebounced = debounce(() => this.rescan(), DOM_SCAN_DEBOUNCE_MS);
4206
+ this.isHeartbeatInFlight = false;
4207
+ this.lastHeartbeatTime = 0;
4208
+ this.registrationSucceeded = false;
3862
4209
  if (!doc.body) {
3863
4210
  throw new InitializationError("document.body is not available");
3864
4211
  }
@@ -3866,7 +4213,7 @@ var AsiystRuntime = class {
3866
4213
  this.installationId = loadOrCreateInstallationId(options.projectId);
3867
4214
  this.visitorSessionId = options.visitorSessionId || `vsess_${Math.random().toString(36).slice(2, 10)}_${Date.now().toString(36)}`;
3868
4215
  const isLocalOrigin = /^(localhost|127(?:\.\d{1,3}){3}|\[::1\])$/i.test(win.location.hostname);
3869
- this.environment = options.apiBaseUrl || isLocalOrigin ? "development" : "production";
4216
+ this.environment = options.environment || (isLocalOrigin ? "development" : "production");
3870
4217
  const transport = new HttpTransport({
3871
4218
  apiBaseUrl: options.apiBaseUrl ?? DEFAULT_API_BASE_URL,
3872
4219
  projectId: options.projectId,
@@ -3891,6 +4238,7 @@ var AsiystRuntime = class {
3891
4238
  () => this.map.list()
3892
4239
  );
3893
4240
  this.renderer = new CssAvatarRenderer();
4241
+ this.renderer.onOpenChat = () => this.open();
3894
4242
  this.renderer.mount(this.host.chrome, this.config.get());
3895
4243
  this.highlights = new HighlightLayer(this.host.overlay, win);
3896
4244
  this.movement = new MovementEngine(win, doc, this.renderer, this.resolver, this.events, () => this.config.get());
@@ -3913,6 +4261,7 @@ var AsiystRuntime = class {
3913
4261
  (open) => {
3914
4262
  const convId = this.panel.getConversationId();
3915
4263
  this.cloud.setConversationId(convId);
4264
+ this.renderer.setPanelOpen(open);
3916
4265
  if (open) {
3917
4266
  this.analytics.track("assistant_opened");
3918
4267
  this.events.emit("asiyst:conversation:started", { conversationId: convId });
@@ -3963,82 +4312,9 @@ var AsiystRuntime = class {
3963
4312
  sdkVersion: SDK_VERSION
3964
4313
  });
3965
4314
  await this.registerInstallation();
3966
- const previousConnection = this.connectionStatus === "connected";
3967
- try {
3968
- const heartbeat = await this.cloud.heartbeat({
3969
- origin: window.location.origin,
3970
- environment: this.environment,
3971
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
3972
- });
3973
- this.connectionStatus = heartbeat.ok ? "connected" : "disconnected";
3974
- this.verificationStatus = heartbeat.verificationStatus === "verified" ? "connected" : heartbeat.verificationStatus ?? (heartbeat.ok ? "connected" : "not_detected");
3975
- if (heartbeat.ok) {
3976
- this.lifecycle.emit("sdk.connected", {
3977
- projectId: this.options.projectId,
3978
- origin: window.location.origin,
3979
- environment: this.environment,
3980
- installationId: this.installationId,
3981
- sdkVersion: SDK_VERSION
3982
- });
3983
- if (heartbeat.verificationStatus === "connected" || heartbeat.verificationStatus === "verified") {
3984
- this.lifecycle.emit("sdk.verified", {
3985
- projectId: this.options.projectId,
3986
- origin: window.location.origin,
3987
- environment: this.environment,
3988
- installationId: this.installationId,
3989
- sdkVersion: SDK_VERSION
3990
- });
3991
- }
3992
- if (heartbeat.versionOutdated) {
3993
- this.lifecycle.emit("sdk.version_outdated", {
3994
- projectId: this.options.projectId,
3995
- origin: window.location.origin,
3996
- environment: this.environment,
3997
- installationId: this.installationId,
3998
- sdkVersion: SDK_VERSION
3999
- });
4000
- }
4001
- } else {
4002
- const reason = heartbeat.verificationStatus ?? "unverified";
4003
- this.lifecycle.emit("sdk.verification_failed", {
4004
- projectId: this.options.projectId,
4005
- origin: window.location.origin,
4006
- environment: this.environment,
4007
- status: reason,
4008
- reason: "heartbeat_failed"
4009
- });
4010
- if (previousConnection) {
4011
- this.lifecycle.emit("sdk.disconnected", {
4012
- projectId: this.options.projectId,
4013
- origin: window.location.origin,
4014
- environment: this.environment,
4015
- installationId: this.installationId,
4016
- reason: "heartbeat_failed",
4017
- status: reason
4018
- });
4019
- }
4020
- }
4021
- } catch (error) {
4022
- this.connectionStatus = "offline";
4023
- this.verificationStatus = "error";
4024
- this.lifecycle.emit("sdk.verification_failed", {
4025
- projectId: this.options.projectId,
4026
- origin: window.location.origin,
4027
- environment: this.environment,
4028
- installationId: this.installationId,
4029
- reason: "heartbeat_failed",
4030
- category: error instanceof AuthenticationError ? "authorization" : "network"
4031
- });
4032
- if (previousConnection) {
4033
- this.lifecycle.emit("sdk.disconnected", {
4034
- projectId: this.options.projectId,
4035
- origin: window.location.origin,
4036
- environment: this.environment,
4037
- reason: "heartbeat_failed",
4038
- category: "network"
4039
- });
4040
- }
4041
- }
4315
+ await this.sendHeartbeat();
4316
+ const doc = this.host.host.ownerDocument;
4317
+ this.startHeartbeatScheduler(doc);
4042
4318
  this.events.emit("asiyst:ready", { projectId: this.options.projectId, configVersion: cfg.version });
4043
4319
  void this.cloud.sendTelemetryEvent("avatar_runtime_initialized", {
4044
4320
  avatarId: this.options.avatarId ?? cfg.avatarId ?? cfg.avatarName,
@@ -4058,6 +4334,7 @@ var AsiystRuntime = class {
4058
4334
  try {
4059
4335
  const loaded = await this.renderer.loadAsset(cfg);
4060
4336
  if (loaded) {
4337
+ this.movement.goToAnchor();
4061
4338
  const format = this.renderer.getLoadedFormat();
4062
4339
  const assetUrl = this.renderer.getAssetUrl();
4063
4340
  const isRendered = this.renderer.isRendered();
@@ -4125,9 +4402,14 @@ var AsiystRuntime = class {
4125
4402
  open() {
4126
4403
  this.panel.open();
4127
4404
  this.avatar.show();
4405
+ this.renderer.setPanelOpen(true);
4128
4406
  }
4129
4407
  close() {
4130
4408
  this.panel.close();
4409
+ this.renderer.setPanelOpen(false);
4410
+ }
4411
+ getRenderer() {
4412
+ return this.renderer;
4131
4413
  }
4132
4414
  avatarApi() {
4133
4415
  return this.avatar;
@@ -4163,6 +4445,16 @@ var AsiystRuntime = class {
4163
4445
  return;
4164
4446
  }
4165
4447
  this.destroyed = true;
4448
+ if (this.heartbeatTimer) {
4449
+ clearInterval(this.heartbeatTimer);
4450
+ this.heartbeatTimer = void 0;
4451
+ }
4452
+ const doc = this.host.host.ownerDocument;
4453
+ if (doc && this.visibilityListener) {
4454
+ doc.removeEventListener("visibilitychange", this.visibilityListener);
4455
+ this.visibilityListener = void 0;
4456
+ }
4457
+ this.isHeartbeatInFlight = false;
4166
4458
  if (this.pagehideListener) {
4167
4459
  win.removeEventListener("pagehide", this.pagehideListener);
4168
4460
  }
@@ -4182,6 +4474,135 @@ var AsiystRuntime = class {
4182
4474
  this.events.removeAll();
4183
4475
  await this.analytics.destroy();
4184
4476
  }
4477
+ startHeartbeatScheduler(doc) {
4478
+ if (this.heartbeatTimer) {
4479
+ clearInterval(this.heartbeatTimer);
4480
+ }
4481
+ this.heartbeatTimer = setInterval(() => {
4482
+ if (!this.destroyed) {
4483
+ void this.sendHeartbeat(true);
4484
+ }
4485
+ }, HEARTBEAT_INTERVAL_MS);
4486
+ if (doc && typeof doc.addEventListener === "function") {
4487
+ this.visibilityListener = () => {
4488
+ if (!this.destroyed && doc.visibilityState === "visible" && Date.now() - this.lastHeartbeatTime > 3e4) {
4489
+ void this.sendHeartbeat(true);
4490
+ }
4491
+ };
4492
+ doc.addEventListener("visibilitychange", this.visibilityListener);
4493
+ }
4494
+ }
4495
+ async sendHeartbeat(_isPeriodic = false) {
4496
+ if (this.destroyed || this.isHeartbeatInFlight) {
4497
+ return false;
4498
+ }
4499
+ this.isHeartbeatInFlight = true;
4500
+ const previousConnection = this.connectionStatus === "connected";
4501
+ const origin = window.location.origin;
4502
+ const environment = this.environment;
4503
+ const installationId = this.installationId;
4504
+ try {
4505
+ if (!this.registrationSucceeded && installationId) {
4506
+ await this.registerInstallation();
4507
+ }
4508
+ const heartbeat = await this.cloud.heartbeat({
4509
+ origin,
4510
+ environment,
4511
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
4512
+ });
4513
+ this.lastHeartbeatTime = Date.now();
4514
+ this.connectionStatus = heartbeat.ok ? "connected" : "disconnected";
4515
+ this.verificationStatus = heartbeat.verificationStatus === "verified" ? "connected" : heartbeat.verificationStatus ?? (heartbeat.ok ? "connected" : "not_detected");
4516
+ if (heartbeat.ok) {
4517
+ if (!previousConnection) {
4518
+ this.events.emit("asiyst:runtime:connected", {
4519
+ projectId: this.options.projectId,
4520
+ status: "connected",
4521
+ environment,
4522
+ origin
4523
+ });
4524
+ this.lifecycle.emit("sdk.connected", {
4525
+ projectId: this.options.projectId,
4526
+ origin,
4527
+ environment,
4528
+ installationId,
4529
+ sdkVersion: SDK_VERSION
4530
+ });
4531
+ }
4532
+ if (heartbeat.verificationStatus === "connected" || heartbeat.verificationStatus === "verified") {
4533
+ this.lifecycle.emit("sdk.verified", {
4534
+ projectId: this.options.projectId,
4535
+ origin,
4536
+ environment,
4537
+ installationId,
4538
+ sdkVersion: SDK_VERSION
4539
+ });
4540
+ }
4541
+ if (heartbeat.versionOutdated) {
4542
+ this.lifecycle.emit("sdk.version_outdated", {
4543
+ projectId: this.options.projectId,
4544
+ origin,
4545
+ environment,
4546
+ installationId,
4547
+ sdkVersion: SDK_VERSION
4548
+ });
4549
+ }
4550
+ return true;
4551
+ } else {
4552
+ const reason = heartbeat.verificationStatus ?? "unverified";
4553
+ this.lifecycle.emit("sdk.verification_failed", {
4554
+ projectId: this.options.projectId,
4555
+ origin,
4556
+ environment,
4557
+ status: reason,
4558
+ reason: "heartbeat_failed"
4559
+ });
4560
+ if (previousConnection) {
4561
+ this.events.emit("asiyst:runtime:disconnected", {
4562
+ projectId: this.options.projectId,
4563
+ reason: "heartbeat_failed"
4564
+ });
4565
+ this.lifecycle.emit("sdk.disconnected", {
4566
+ projectId: this.options.projectId,
4567
+ origin,
4568
+ environment,
4569
+ installationId,
4570
+ reason: "heartbeat_failed",
4571
+ status: reason
4572
+ });
4573
+ }
4574
+ return false;
4575
+ }
4576
+ } catch (error) {
4577
+ this.connectionStatus = "offline";
4578
+ this.verificationStatus = "error";
4579
+ this.lifecycle.emit("sdk.verification_failed", {
4580
+ projectId: this.options.projectId,
4581
+ origin,
4582
+ environment,
4583
+ installationId,
4584
+ reason: "heartbeat_failed",
4585
+ category: error instanceof AuthenticationError ? "authorization" : "network"
4586
+ });
4587
+ if (previousConnection) {
4588
+ this.events.emit("asiyst:runtime:disconnected", {
4589
+ projectId: this.options.projectId,
4590
+ reason: "heartbeat_failed"
4591
+ });
4592
+ this.lifecycle.emit("sdk.disconnected", {
4593
+ projectId: this.options.projectId,
4594
+ origin,
4595
+ environment,
4596
+ installationId,
4597
+ reason: "heartbeat_failed",
4598
+ category: "network"
4599
+ });
4600
+ }
4601
+ return false;
4602
+ } finally {
4603
+ this.isHeartbeatInFlight = false;
4604
+ }
4605
+ }
4185
4606
  async registerInstallation() {
4186
4607
  const origin = window.location.origin;
4187
4608
  const environment = this.environment;
@@ -4203,6 +4624,7 @@ var AsiystRuntime = class {
4203
4624
  environment,
4204
4625
  sdk_version: SDK_VERSION
4205
4626
  });
4627
+ this.registrationSucceeded = true;
4206
4628
  return;
4207
4629
  } catch (error) {
4208
4630
  lastError = error;
@@ -4302,6 +4724,9 @@ var Asiyst = {
4302
4724
  getVerificationStatus() {
4303
4725
  return requireRuntime().getVerificationStatus();
4304
4726
  },
4727
+ isRuntimeConnected() {
4728
+ return requireRuntime().getConnectionStatus() === "connected";
4729
+ },
4305
4730
  avatar: {
4306
4731
  show() {
4307
4732
  withIsolation(() => requireRuntime().avatarApi().show());