@asiyst/sdk 0.1.33 → 0.1.35

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
@@ -107,7 +107,7 @@ var ALL_ACTION_KINDS = [
107
107
  // src/core/constants.ts
108
108
  function resolveSdkVersion() {
109
109
  {
110
- return "0.1.33";
110
+ return "0.1.35";
111
111
  }
112
112
  }
113
113
  var SDK_VERSION = resolveSdkVersion();
@@ -377,23 +377,50 @@ function fallbackConfig() {
377
377
  };
378
378
  }
379
379
  function parseAnimations(value) {
380
- if (!Array.isArray(value)) return void 0;
380
+ if (!value) return void 0;
381
381
  const list = [];
382
- for (const item of value) {
383
- if (isRecord(item) && typeof item.name === "string") {
384
- list.push({
385
- id: typeof item.id === "string" ? sanitizeText(item.id, 64) : void 0,
386
- name: sanitizeText(item.name, 64),
387
- intent: typeof item.intent === "string" ? sanitizeText(item.intent, 64) : void 0,
388
- clipName: typeof item.clipName === "string" ? sanitizeText(item.clipName, 64) : void 0,
389
- loopMode: typeof item.loopMode === "string" ? sanitizeText(item.loopMode, 32) : void 0,
390
- durationMs: typeof item.durationMs === "number" ? item.durationMs : void 0,
391
- assetUrl: typeof item.assetUrl === "string" ? item.assetUrl : null,
392
- integrity: typeof item.integrity === "string" ? item.integrity : void 0
393
- });
382
+ if (Array.isArray(value)) {
383
+ for (const item of value) {
384
+ if (isRecord(item) && (typeof item.name === "string" || typeof item.id === "string" || typeof item.intent === "string")) {
385
+ const resolvedUrl = typeof item.assetUrl === "string" && item.assetUrl.trim() ? item.assetUrl.trim() : typeof item.downloadUrl === "string" && item.downloadUrl.trim() ? item.downloadUrl.trim() : typeof item.url === "string" && item.url.trim() ? item.url.trim() : null;
386
+ const name = typeof item.name === "string" && item.name.trim() ? sanitizeText(item.name, 64) : typeof item.intent === "string" && item.intent.trim() ? sanitizeText(item.intent, 64) : sanitizeText(String(item.id ?? "animation"), 64);
387
+ list.push({
388
+ id: typeof item.id === "string" ? sanitizeText(item.id, 64) : typeof item.animationId === "string" ? sanitizeText(item.animationId, 64) : void 0,
389
+ animationId: typeof item.animationId === "string" ? sanitizeText(item.animationId, 64) : void 0,
390
+ name,
391
+ normalizedName: typeof item.normalizedName === "string" ? sanitizeText(item.normalizedName, 64) : void 0,
392
+ category: typeof item.category === "string" ? sanitizeText(item.category, 64) : void 0,
393
+ intent: typeof item.intent === "string" ? sanitizeText(item.intent, 64) : void 0,
394
+ clipName: typeof item.clipName === "string" ? sanitizeText(item.clipName, 64) : void 0,
395
+ loopMode: typeof item.loopMode === "string" ? sanitizeText(item.loopMode, 32) : typeof item.loop === "boolean" ? item.loop ? "loop" : "once" : void 0,
396
+ loop: typeof item.loop === "boolean" ? item.loop : void 0,
397
+ durationMs: typeof item.durationMs === "number" ? item.durationMs : void 0,
398
+ duration: typeof item.duration === "number" ? item.duration : null,
399
+ status: typeof item.status === "string" ? sanitizeText(item.status, 32) : void 0,
400
+ format: typeof item.format === "string" ? sanitizeText(item.format, 16) : void 0,
401
+ assetUrl: resolvedUrl,
402
+ downloadUrl: typeof item.downloadUrl === "string" ? item.downloadUrl.trim() : resolvedUrl ?? void 0,
403
+ integrity: typeof item.integrity === "string" ? item.integrity : typeof item.sha256 === "string" ? `sha256-${item.sha256}` : void 0
404
+ });
405
+ }
406
+ }
407
+ return list;
408
+ }
409
+ if (isRecord(value)) {
410
+ for (const [key, urlVal] of Object.entries(value)) {
411
+ if (typeof urlVal === "string" && urlVal.trim()) {
412
+ list.push({
413
+ id: sanitizeText(key, 64),
414
+ name: sanitizeText(key, 64),
415
+ intent: sanitizeText(key, 64),
416
+ assetUrl: urlVal.trim(),
417
+ downloadUrl: urlVal.trim()
418
+ });
419
+ }
394
420
  }
421
+ return list.length > 0 ? list : void 0;
395
422
  }
396
- return list;
423
+ return void 0;
397
424
  }
398
425
  function parseBranding(value) {
399
426
  if (!isRecord(value)) return void 0;
@@ -414,13 +441,16 @@ function normalizeProjectConfig(raw) {
414
441
  "",
415
442
  128
416
443
  ) || void 0;
417
- 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;
444
+ const rawAvatarCharacter = isRecord(rawAvatarRecord?.character) ? rawAvatarRecord.character : void 0;
445
+ const rawManifestCharacter = isRecord(rawManifestRecord?.character) ? rawManifestRecord.character : void 0;
446
+ const rawCharacter = isRecord(raw.character) ? raw.character : void 0;
447
+ const assetUrl = typeof raw.assetUrl === "string" && raw.assetUrl ? raw.assetUrl : typeof raw.modelUrl === "string" && raw.modelUrl ? raw.modelUrl : typeof rawCharacter?.downloadUrl === "string" && rawCharacter.downloadUrl ? rawCharacter.downloadUrl : typeof rawCharacter?.assetUrl === "string" && rawCharacter.assetUrl ? rawCharacter.assetUrl : typeof rawAvatarRecord?.assetUrl === "string" && rawAvatarRecord.assetUrl ? rawAvatarRecord.assetUrl : typeof rawAvatarRecord?.modelUrl === "string" && rawAvatarRecord.modelUrl ? rawAvatarRecord.modelUrl : typeof rawAvatarCharacter?.downloadUrl === "string" && rawAvatarCharacter.downloadUrl ? rawAvatarCharacter.downloadUrl : typeof rawAvatarCharacter?.assetUrl === "string" && rawAvatarCharacter.assetUrl ? rawAvatarCharacter.assetUrl : typeof rawConfigRecord?.assetUrl === "string" && rawConfigRecord.assetUrl ? rawConfigRecord.assetUrl : typeof rawManifestCharacter?.downloadUrl === "string" && rawManifestCharacter.downloadUrl ? rawManifestCharacter.downloadUrl : typeof rawManifestCharacter?.assetUrl === "string" && rawManifestCharacter.assetUrl ? rawManifestCharacter.assetUrl : typeof rawManifestRecord?.assetUrl === "string" && rawManifestRecord.assetUrl ? rawManifestRecord.assetUrl : typeof rawManifestRecord?.modelUrl === "string" && rawManifestRecord.modelUrl ? rawManifestRecord.modelUrl : void 0;
418
448
  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;
419
- const rawModelFormat = raw.modelFormat ?? rawAvatarRecord?.modelFormat ?? rawConfigRecord?.modelFormat ?? rawManifestRecord?.modelFormat;
449
+ const rawModelFormat = raw.modelFormat ?? raw.format ?? rawCharacter?.format ?? rawAvatarRecord?.modelFormat ?? rawAvatarRecord?.format ?? rawAvatarCharacter?.format ?? rawConfigRecord?.modelFormat ?? rawManifestCharacter?.format ?? rawManifestRecord?.modelFormat ?? rawManifestRecord?.characterFormat;
420
450
  const modelFormat = rawModelFormat === "fbx" || rawModelFormat === "glb" ? rawModelFormat : void 0;
421
451
  const publishedVersion = typeof raw.publishedVersion === "string" ? raw.publishedVersion : typeof rawAvatarRecord?.version === "string" ? rawAvatarRecord.version : typeof rawManifestRecord?.version === "string" ? rawManifestRecord.version : void 0;
422
452
  const animations = parseAnimations(
423
- raw.animations ?? rawAvatarRecord?.animations ?? rawConfigRecord?.animations
453
+ raw.animations ?? raw.separateAnimations ?? rawAvatarRecord?.separateAnimations ?? rawAvatarRecord?.animations ?? rawConfigRecord?.animations ?? rawManifestRecord?.separateAnimations ?? rawManifestRecord?.animations
424
454
  );
425
455
  const branding = parseBranding(raw.branding ?? rawAvatarRecord?.branding);
426
456
  const avatarName = asString(
@@ -430,9 +460,11 @@ function normalizeProjectConfig(raw) {
430
460
  );
431
461
  const avatar = typeof raw.avatar === "string" ? asString(raw.avatar, base.avatar, 64) : avatarId ?? base.avatar;
432
462
  const position = ANCHORS.includes(raw.position) ? raw.position : base.position;
463
+ const projectId = typeof raw.projectId === "string" && raw.projectId.trim() ? sanitizeText(raw.projectId, 128) : typeof rawConfigRecord?.projectId === "string" && rawConfigRecord.projectId.trim() ? sanitizeText(rawConfigRecord.projectId, 128) : typeof rawAvatarRecord?.projectId === "string" && rawAvatarRecord.projectId.trim() ? sanitizeText(rawAvatarRecord.projectId, 128) : base.projectId;
433
464
  return {
434
465
  schemaVersion: CONFIG_SCHEMA_VERSION,
435
466
  version: asNumber(raw.version, base.version, 0, Number.MAX_SAFE_INTEGER),
467
+ projectId,
436
468
  avatarName,
437
469
  avatar,
438
470
  avatarId,
@@ -541,8 +573,26 @@ var HttpTransport = class {
541
573
  getAvatarId() {
542
574
  return this.options.avatarId;
543
575
  }
576
+ buildRequestUrl(rawPath) {
577
+ const base = /^https?:\/\//i.test(rawPath) ? rawPath : `${this.options.apiBaseUrl.replace(/\/$/, "")}${rawPath.startsWith("/") ? rawPath : `/${rawPath}`}`;
578
+ try {
579
+ const parsed = new URL(base, typeof location !== "undefined" ? location.href : "https://asiyst.com");
580
+ if (parsed.pathname.startsWith("/api/v1/") && this.options.projectId && !parsed.searchParams.has("projectId")) {
581
+ parsed.searchParams.set("projectId", this.options.projectId);
582
+ }
583
+ if (parsed.pathname.startsWith("/api/v1/") && this.options.publicKey && !parsed.searchParams.has("publicKey") && (parsed.pathname.includes("/runtime/avatar") || parsed.pathname.includes("/runtime/register") || parsed.pathname.includes("/installations/register") || parsed.pathname.includes("/runtime/token") || parsed.pathname.includes("/manifest"))) {
584
+ parsed.searchParams.set("publicKey", this.options.publicKey);
585
+ }
586
+ if (parsed.pathname.includes("/runtime/avatar") && this.options.avatarId && !parsed.searchParams.has("avatarId")) {
587
+ parsed.searchParams.set("avatarId", this.options.avatarId);
588
+ }
589
+ return parsed.toString();
590
+ } catch {
591
+ return base;
592
+ }
593
+ }
544
594
  async request(req) {
545
- const url = /^https?:\/\//i.test(req.path) ? req.path : `${this.options.apiBaseUrl.replace(/\/$/, "")}${req.path}`;
595
+ const url = this.buildRequestUrl(req.path);
546
596
  const requestId = req.requestId || generateRequestId();
547
597
  const timeoutMs = req.timeoutMs ?? this.options.defaultTimeoutMs ?? DEFAULT_TIMEOUT_MS;
548
598
  const isSafeOrIdempotent = req.method === "GET" || Boolean(req.idempotent);
@@ -569,21 +619,45 @@ var HttpTransport = class {
569
619
  }
570
620
  }
571
621
  try {
572
- let response;
573
- response = await fetch(url, {
622
+ const defaultHeaders = {
623
+ Accept: "application/json",
624
+ "X-Asiyst-Project-Id": this.options.projectId,
625
+ "X-Asiyst-Public-Key": this.options.publicKey,
626
+ "X-Asiyst-Project-Key": this.options.publicKey,
627
+ ...this.options.avatarId ? { "X-Asiyst-Avatar-Id": this.options.avatarId } : {},
628
+ "X-Asiyst-SDK-Version": SDK_VERSION,
629
+ "X-Asiyst-Request-Id": requestId,
630
+ ...req.sessionId ? { "X-Asiyst-Session-Id": req.sessionId } : {},
631
+ ...req.conversationId ? { "X-Asiyst-Conversation-Id": req.conversationId } : {},
632
+ ...req.authorizationToken ? { Authorization: `Bearer ${req.authorizationToken}` } : {}
633
+ };
634
+ if (req.body !== void 0) {
635
+ defaultHeaders["Content-Type"] = "application/json";
636
+ }
637
+ const mergedHeaders = new Headers();
638
+ for (const [key, value] of Object.entries(defaultHeaders)) {
639
+ mergedHeaders.set(key, value);
640
+ }
641
+ if (req.headers) {
642
+ if (typeof req.headers.forEach === "function" && !Array.isArray(req.headers)) {
643
+ req.headers.forEach((value, key) => {
644
+ mergedHeaders.set(key, value);
645
+ });
646
+ } else if (Array.isArray(req.headers)) {
647
+ for (const [key, value] of req.headers) {
648
+ mergedHeaders.set(key, value);
649
+ }
650
+ } else if (typeof req.headers === "object") {
651
+ for (const [key, value] of Object.entries(req.headers)) {
652
+ if (value !== void 0 && value !== null) {
653
+ mergedHeaders.set(key, String(value));
654
+ }
655
+ }
656
+ }
657
+ }
658
+ const response = await fetch(url, {
574
659
  method: req.method,
575
- headers: req.headers ?? {
576
- Accept: "application/json",
577
- "Content-Type": "application/json",
578
- "X-Asiyst-Project-Id": this.options.projectId,
579
- "X-Asiyst-Public-Key": this.options.publicKey,
580
- ...this.options.avatarId ? { "X-Asiyst-Avatar-Id": this.options.avatarId } : {},
581
- "X-Asiyst-SDK-Version": SDK_VERSION,
582
- "X-Asiyst-Request-Id": requestId,
583
- ...req.sessionId ? { "X-Asiyst-Session-Id": req.sessionId } : {},
584
- ...req.conversationId ? { "X-Asiyst-Conversation-Id": req.conversationId } : {},
585
- ...req.authorizationToken ? { Authorization: `Bearer ${req.authorizationToken}` } : {}
586
- },
660
+ headers: mergedHeaders,
587
661
  body: req.body === void 0 ? void 0 : JSON.stringify(req.body),
588
662
  signal: controller.signal
589
663
  });
@@ -659,11 +733,21 @@ var API_ROUTES = {
659
733
  deviceVerify: "/cli/device/verify",
660
734
  deviceApprove: "/cli/device/approve",
661
735
  deviceDeny: "/cli/device/deny",
736
+ cliConnectStart: "/cli/connect/start",
737
+ cliConnectExchange: "/cli/connect/exchange",
738
+ cliWhoami: "/cli/whoami",
739
+ cliProject: (projectId) => `/cli/projects/${encodeURIComponent(projectId)}`,
740
+ cliProjectStatus: (projectId) => `/cli/projects/${encodeURIComponent(projectId)}/status`,
741
+ cliAvatarImport: (projectId) => `/cli/projects/${encodeURIComponent(projectId)}/avatars/import`,
742
+ cliAvatarVerify: (projectId, avatarId) => `/cli/projects/${encodeURIComponent(projectId)}/avatars/${encodeURIComponent(avatarId)}/verify`,
662
743
  authDeviceAuthorize: "/auth/device/authorize",
663
744
  authDeviceToken: "/auth/device/token",
664
745
  projectApiKeyVerify: (projectId) => `/projects/${encodeURIComponent(projectId)}/api-key/verify`,
665
746
  sdkInstallationVerify: "/sdk/installations/verify",
666
747
  avatarManifest: (projectId, avatarId) => `/projects/${encodeURIComponent(projectId)}/avatars/${encodeURIComponent(avatarId)}/manifest`,
748
+ projectAvatarManifest: (projectId, avatarId) => `/projects/${encodeURIComponent(projectId)}/avatars/${encodeURIComponent(avatarId)}/manifest`,
749
+ projectAvatarAsset: (projectId, avatarId) => `/projects/${encodeURIComponent(projectId)}/avatars/${encodeURIComponent(avatarId)}/asset`,
750
+ projectAvatarAnimationAsset: (projectId, avatarId, animationId) => `/projects/${encodeURIComponent(projectId)}/avatars/${encodeURIComponent(avatarId)}/animations/${encodeURIComponent(animationId)}/asset`,
667
751
  avatarRuntimeConfig: (projectId) => `/projects/${encodeURIComponent(projectId)}/avatar/runtime-config`
668
752
  };
669
753
  var API_LIMITS = {
@@ -752,63 +836,287 @@ var CloudClient = class {
752
836
  getConfigMetadata() {
753
837
  return this.configMetadata;
754
838
  }
839
+ withProjectQuery(rawUrl, extra) {
840
+ try {
841
+ const baseOrigin = contractApiBaseUrl(this.getApiBaseUrl());
842
+ const parsed = new URL(rawUrl, baseOrigin);
843
+ if (this.projectId && !parsed.searchParams.has("projectId")) {
844
+ parsed.searchParams.set("projectId", this.projectId);
845
+ }
846
+ if (extra) {
847
+ for (const [k, v] of Object.entries(extra)) {
848
+ if (v !== void 0 && v !== "" && !parsed.searchParams.has(k)) {
849
+ parsed.searchParams.set(k, v);
850
+ }
851
+ }
852
+ }
853
+ return parsed.toString();
854
+ } catch {
855
+ return rawUrl;
856
+ }
857
+ }
858
+ resolveAssetUrl(rawUrl) {
859
+ if (typeof rawUrl !== "string") return void 0;
860
+ const trimmed = rawUrl.trim();
861
+ if (!trimmed) return void 0;
862
+ if (/^https?:\/\//i.test(trimmed)) return trimmed;
863
+ try {
864
+ const baseOrigin = contractApiBaseUrl(this.getApiBaseUrl()).replace(/\/+$/, "");
865
+ return `${baseOrigin}${trimmed.startsWith("/") ? trimmed : `/${trimmed}`}`;
866
+ } catch {
867
+ return trimmed;
868
+ }
869
+ }
755
870
  async fetchConfig(etag, cachedConfig) {
756
- const response = await this.transport.request({
757
- path: contractApiUrl(this.getApiBaseUrl(), API_ROUTES.runtimeAvatar),
758
- method: "GET",
759
- idempotent: true,
760
- retries: 2,
761
- headers: {
762
- Accept: "application/json",
763
- "X-Asiyst-Public-Key": this.publicKey,
764
- "X-Asiyst-Project-Id": this.projectId,
765
- ...this.avatarId ? { "X-Asiyst-Avatar-Id": this.avatarId } : {},
766
- ...etag ? { "If-None-Match": etag } : {}
871
+ const runtimeAvatarUrl = this.withProjectQuery(
872
+ contractApiUrl(this.getApiBaseUrl(), API_ROUTES.runtimeAvatar),
873
+ {
874
+ publicKey: this.publicKey || void 0,
875
+ avatarId: this.avatarId || void 0
767
876
  }
768
- });
877
+ );
878
+ let response;
879
+ try {
880
+ response = await this.transport.request({
881
+ path: runtimeAvatarUrl,
882
+ method: "GET",
883
+ idempotent: true,
884
+ retries: 2,
885
+ headers: {
886
+ Accept: "application/json",
887
+ "X-Asiyst-Public-Key": this.publicKey,
888
+ "X-Asiyst-Project-Key": this.publicKey,
889
+ "X-Asiyst-Project-Id": this.projectId,
890
+ ...this.avatarId ? { "X-Asiyst-Avatar-Id": this.avatarId } : {},
891
+ ...etag ? { "If-None-Match": etag } : {}
892
+ }
893
+ });
894
+ } catch (err) {
895
+ throw new AsiystError({
896
+ code: "CORS_OR_NETWORK_FAILURE",
897
+ status: 0,
898
+ message: err instanceof Error ? `CORS or network failure while fetching avatar runtime configuration: ${err.message}` : "CORS or network failure while fetching avatar runtime configuration.",
899
+ hint: "Verify that your website origin is approved in the Asiyst project settings and that the API endpoint is reachable.",
900
+ cause: err
901
+ });
902
+ }
769
903
  if (response.status === 304 && cachedConfig) {
770
- this.configMetadata = {
771
- etag: response.headers?.get("etag") ?? etag,
772
- maxAgeMs: 0,
773
- staleIfErrorMs: 0
774
- };
775
- return cachedConfig;
904
+ if (!this.avatarId || cachedConfig.avatarId === this.avatarId) {
905
+ this.configMetadata = {
906
+ etag: response.headers?.get("etag") ?? etag,
907
+ maxAgeMs: 0,
908
+ staleIfErrorMs: 0
909
+ };
910
+ return cachedConfig;
911
+ }
776
912
  }
777
913
  if (!response.ok || !response.data) {
778
- const body = response.data;
779
- const code = body?.error?.code ?? `HTTP_${response.status}`;
914
+ const rawBody = response.data;
915
+ const errObj = rawBody?.error && typeof rawBody.error === "object" ? rawBody.error : void 0;
916
+ const code = (errObj?.code ?? (typeof rawBody?.code === "string" ? rawBody.code : void 0) ?? `HTTP_${response.status}`).toUpperCase();
917
+ const message = errObj?.message ?? (typeof rawBody?.message === "string" ? rawBody.message : void 0);
918
+ if (response.status === 0) {
919
+ throw new AsiystError({
920
+ code: "CORS_OR_NETWORK_FAILURE",
921
+ status: 0,
922
+ message: message ?? "Browser CORS or network failure blocked the runtime avatar configuration request.",
923
+ hint: "Ensure this website origin is approved in your Asiyst project domain settings."
924
+ });
925
+ }
926
+ if (code === "AVATAR_NOT_IMPORTED" || code === "AVATAR_UNPUBLISHED" || code === "UNPUBLISHED_AVATAR") {
927
+ throw new AsiystError({
928
+ code: "AVATAR_NOT_IMPORTED",
929
+ status: response.status,
930
+ message: message ?? `Avatar${this.avatarId ? ` "${this.avatarId}"` : ""} is not imported or published for project "${this.projectId}".`,
931
+ hint: "Run `npx asiyst avatar import <avatarId>` and confirm the backend completes the import."
932
+ });
933
+ }
934
+ if (response.status === 404 || code === "AVATAR_NOT_FOUND" || code === "NOT_FOUND") {
935
+ throw new AsiystError({
936
+ code: "AVATAR_NOT_FOUND",
937
+ status: 404,
938
+ message: message ?? `Avatar${this.avatarId ? ` "${this.avatarId}"` : ""} was not found for project "${this.projectId}".`,
939
+ hint: "Verify the avatarId in Asiyst.init() and in your Asiyst project."
940
+ });
941
+ }
942
+ if (code === "PROJECT_MISMATCH" || code === "API_KEY_PROJECT_MISMATCH" || code === "WRONG_PROJECT") {
943
+ throw new AsiystError({
944
+ code: "PROJECT_MISMATCH",
945
+ status: response.status || 403,
946
+ message: message ?? `Avatar or public key does not belong to project "${this.projectId}".`,
947
+ hint: "Verify that projectId, publicKey, and avatarId belong to the same Asiyst project."
948
+ });
949
+ }
950
+ if (code === "ORIGIN_NOT_APPROVED" || code === "DOMAIN_NOT_APPROVED" || code === "DOMAIN_MISMATCH") {
951
+ throw new AsiystError({
952
+ code: "DOMAIN_NOT_APPROVED",
953
+ status: response.status || 403,
954
+ message: message ?? `Origin is not approved for project "${this.projectId}" (${code}).`,
955
+ hint: "Add your website domain/origin to the approved domains in Asiyst Dashboard."
956
+ });
957
+ }
958
+ if (code === "MISSING_CHARACTER_FILE" || code === "ASSET_UNAVAILABLE") {
959
+ throw new AsiystError({
960
+ code: "MISSING_CHARACTER_FILE",
961
+ status: response.status,
962
+ message: message ?? "Avatar character FBX/GLB file is missing or unavailable."
963
+ });
964
+ }
780
965
  if (response.status === 401 || response.status === 403) {
781
- throw new AuthenticationError(body?.error?.message ?? `Asiyst runtime avatar configuration was rejected (${code}).`);
966
+ const authErr = new AuthenticationError(
967
+ message ?? `Asiyst runtime avatar configuration was rejected (${code}).`
968
+ );
969
+ throw authErr;
782
970
  }
783
- throw new NetworkError(body?.error?.message ?? `Asiyst runtime avatar configuration is unavailable (${code}).`);
971
+ throw new NetworkError(message ?? `Asiyst runtime avatar configuration is unavailable (${code}).`);
784
972
  }
785
- const data = response.data;
786
- if (data.success !== true || data.projectId !== this.projectId || typeof data.avatarId !== "string" || !data.config || typeof data.config !== "object" || Array.isArray(data.config)) {
973
+ const rawRoot = response.data;
974
+ const data = rawRoot.data && typeof rawRoot.data === "object" && !Array.isArray(rawRoot.data) ? rawRoot.data : rawRoot;
975
+ const rawConfig = data.config && typeof data.config === "object" && !Array.isArray(data.config) ? data.config : void 0;
976
+ const rawAvatar = data.avatar && typeof data.avatar === "object" && !Array.isArray(data.avatar) ? data.avatar : {};
977
+ let manifest = data.manifest && typeof data.manifest === "object" && !Array.isArray(data.manifest) ? data.manifest : {};
978
+ const returnedProjectId = typeof data.projectId === "string" ? data.projectId : typeof rawConfig?.projectId === "string" ? rawConfig.projectId : typeof rawAvatar.projectId === "string" ? rawAvatar.projectId : void 0;
979
+ if (rawRoot.success === false || data.success === false) {
787
980
  throw new ConfigurationError("Asiyst returned an invalid runtime configuration response.");
788
981
  }
789
- const rawConfig = data.config;
790
- const rawAvatar = data.avatar && typeof data.avatar === "object" ? data.avatar : {};
791
- const manifest = data.manifest && typeof data.manifest === "object" ? data.manifest : {};
792
- const modelFormat = [data.format, data.modelFormat, rawConfig.modelFormat, rawAvatar.modelFormat].find((format) => typeof format === "string");
982
+ if (!returnedProjectId || returnedProjectId !== this.projectId) {
983
+ if (!returnedProjectId) {
984
+ throw new ConfigurationError("Asiyst returned an invalid runtime configuration response.");
985
+ }
986
+ throw new AsiystError({
987
+ code: "PROJECT_MISMATCH",
988
+ status: 403,
989
+ message: `Project mismatch: expected "${this.projectId}", received "${returnedProjectId}".`,
990
+ hint: "Verify that the avatar and publicKey belong to the configured projectId."
991
+ });
992
+ }
993
+ if (data.imported === false || rawAvatar.imported === false || rawAvatar.isPublished === false || rawAvatar.status === "unpublished") {
994
+ throw new AsiystError({
995
+ code: "AVATAR_NOT_IMPORTED",
996
+ status: 404,
997
+ message: `Avatar "${this.avatarId ?? String(data.avatarId ?? "unknown")}" is not imported or published for project "${this.projectId}".`,
998
+ hint: "Publish the avatar in Avatar Studio and import it with `npx asiyst avatar import <avatarId>`."
999
+ });
1000
+ }
1001
+ const returnedAvatarId = typeof data.avatarId === "string" && data.avatarId.trim() ? data.avatarId.trim() : typeof rawAvatar.avatarId === "string" && rawAvatar.avatarId.trim() ? rawAvatar.avatarId.trim() : typeof rawAvatar.id === "string" && rawAvatar.id.trim() ? rawAvatar.id.trim() : typeof rawConfig?.avatarId === "string" && rawConfig.avatarId.trim() ? rawConfig.avatarId.trim() : void 0;
1002
+ if (!returnedAvatarId) {
1003
+ throw new ConfigurationError("Asiyst returned an invalid runtime configuration response.");
1004
+ }
1005
+ if (this.avatarId && returnedAvatarId !== this.avatarId && rawAvatar.id !== this.avatarId) {
1006
+ throw new AsiystError({
1007
+ code: "AVATAR_NOT_IMPORTED",
1008
+ status: 409,
1009
+ message: `Requested avatar "${this.avatarId}" does not match the project's imported avatar ("${returnedAvatarId}").`,
1010
+ hint: `Run \`npx asiyst avatar import ${this.avatarId}\` to import this avatar into project "${this.projectId}".`
1011
+ });
1012
+ }
1013
+ const cfgObj = rawConfig ?? {};
1014
+ const extractCharacterUrl = (m) => {
1015
+ const dChar = data.character && typeof data.character === "object" && !Array.isArray(data.character) ? data.character : void 0;
1016
+ const mChar2 = m.character && typeof m.character === "object" && !Array.isArray(m.character) ? m.character : void 0;
1017
+ const aChar = rawAvatar.character && typeof rawAvatar.character === "object" && !Array.isArray(rawAvatar.character) ? rawAvatar.character : void 0;
1018
+ const candidate = [
1019
+ cfgObj.assetUrl,
1020
+ cfgObj.modelUrl,
1021
+ rawAvatar.assetUrl,
1022
+ rawAvatar.modelUrl,
1023
+ dChar?.downloadUrl,
1024
+ dChar?.assetUrl,
1025
+ aChar?.downloadUrl,
1026
+ aChar?.assetUrl,
1027
+ mChar2?.downloadUrl,
1028
+ mChar2?.assetUrl,
1029
+ m.assetUrl,
1030
+ m.modelUrl,
1031
+ data.assetUrl,
1032
+ data.modelUrl
1033
+ ].find((val) => typeof val === "string" && val.trim().length > 0);
1034
+ return this.resolveAssetUrl(candidate);
1035
+ };
1036
+ let assetUrl = extractCharacterUrl(manifest);
1037
+ if (!assetUrl && typeof data.manifestUrl === "string" && data.manifestUrl.trim()) {
1038
+ try {
1039
+ const fetchedManifest = await this.fetchManifest(this.resolveAssetUrl(data.manifestUrl));
1040
+ if (fetchedManifest && typeof fetchedManifest === "object") {
1041
+ manifest = { ...manifest, ...fetchedManifest };
1042
+ assetUrl = extractCharacterUrl(manifest);
1043
+ }
1044
+ } catch {
1045
+ }
1046
+ }
1047
+ const mChar = manifest.character && typeof manifest.character === "object" && !Array.isArray(manifest.character) ? manifest.character : void 0;
1048
+ const modelFormat = [
1049
+ data.format,
1050
+ data.modelFormat,
1051
+ cfgObj.modelFormat,
1052
+ rawAvatar.modelFormat,
1053
+ rawAvatar.format,
1054
+ mChar?.format,
1055
+ manifest.modelFormat,
1056
+ manifest.characterFormat
1057
+ ].find((format) => typeof format === "string" && format.trim().length > 0);
793
1058
  if (modelFormat && modelFormat !== "fbx" && modelFormat !== "glb") {
794
1059
  throw new ConfigurationError(`Asiyst returned an unsupported avatar format: ${modelFormat}.`);
795
1060
  }
796
- const assetUrl = typeof rawConfig.assetUrl === "string" ? rawConfig.assetUrl : typeof rawAvatar.assetUrl === "string" ? rawAvatar.assetUrl : typeof rawAvatar.modelUrl === "string" ? rawAvatar.modelUrl : typeof manifest.assetUrl === "string" ? manifest.assetUrl : void 0;
797
- const animations = Array.isArray(rawConfig.animations) ? rawConfig.animations : Array.isArray(data.animations) ? data.animations : Array.isArray(rawAvatar.animations) ? rawAvatar.animations : [];
1061
+ const rawAnimsSource = Array.isArray(cfgObj.animations) && cfgObj.animations.length > 0 ? cfgObj.animations : Array.isArray(data.separateAnimations) && data.separateAnimations.length > 0 ? data.separateAnimations : Array.isArray(data.animations) && data.animations.length > 0 ? data.animations : Array.isArray(rawAvatar.separateAnimations) && rawAvatar.separateAnimations.length > 0 ? rawAvatar.separateAnimations : Array.isArray(rawAvatar.animations) && rawAvatar.animations.length > 0 ? rawAvatar.animations : Array.isArray(manifest.separateAnimations) && manifest.separateAnimations.length > 0 ? manifest.separateAnimations : manifest.animations;
1062
+ const normalizeAnimationEntries = (source) => {
1063
+ if (Array.isArray(source)) {
1064
+ return source.filter((item) => Boolean(item && typeof item === "object" && !Array.isArray(item))).map((item) => {
1065
+ const rawAnimUrl = typeof item.assetUrl === "string" && item.assetUrl.trim() ? item.assetUrl.trim() : typeof item.downloadUrl === "string" && item.downloadUrl.trim() ? item.downloadUrl.trim() : typeof item.url === "string" && item.url.trim() ? item.url.trim() : void 0;
1066
+ const resolvedAnimUrl = this.resolveAssetUrl(rawAnimUrl);
1067
+ return {
1068
+ ...item,
1069
+ assetUrl: resolvedAnimUrl ?? (item.assetUrl === null ? null : void 0),
1070
+ downloadUrl: resolvedAnimUrl ?? (typeof item.downloadUrl === "string" ? item.downloadUrl : void 0)
1071
+ };
1072
+ });
1073
+ }
1074
+ if (source && typeof source === "object" && !Array.isArray(source)) {
1075
+ const entries = [];
1076
+ for (const [intentOrName, urlVal] of Object.entries(source)) {
1077
+ if (typeof urlVal === "string" && urlVal.trim()) {
1078
+ const resolved = this.resolveAssetUrl(urlVal);
1079
+ entries.push({
1080
+ id: intentOrName,
1081
+ name: intentOrName,
1082
+ intent: intentOrName,
1083
+ assetUrl: resolved,
1084
+ downloadUrl: resolved
1085
+ });
1086
+ }
1087
+ }
1088
+ return entries;
1089
+ }
1090
+ return [];
1091
+ };
1092
+ const animations = normalizeAnimationEntries(rawAnimsSource);
798
1093
  const revision = typeof data.configRevision === "string" ? data.configRevision : typeof data.publishedVersionId === "string" ? data.publishedVersionId : "0";
799
1094
  const version = Number.parseInt(revision, 10);
1095
+ const thumbnailUrl = this.resolveAssetUrl(
1096
+ typeof cfgObj.thumbnailUrl === "string" ? cfgObj.thumbnailUrl : typeof rawAvatar.thumbnailUrl === "string" ? rawAvatar.thumbnailUrl : typeof manifest.thumbnailUrl === "string" ? manifest.thumbnailUrl : void 0
1097
+ );
1098
+ if ((cfgObj.assetUrl === null || rawAvatar.assetUrl === null || mChar?.downloadUrl === null || data.missingCharacterFile === true) && !assetUrl && !thumbnailUrl) {
1099
+ throw new AsiystError({
1100
+ code: "MISSING_CHARACTER_FILE",
1101
+ status: 404,
1102
+ message: `Avatar "${returnedAvatarId}" is missing its character FBX/GLB file.`,
1103
+ hint: "Upload a valid character FBX or GLB file in Avatar Studio and republish the avatar."
1104
+ });
1105
+ }
800
1106
  const config = normalizeProjectConfig({
801
- ...rawConfig,
802
- avatarId: data.avatarId,
803
- avatarName: typeof rawAvatar.name === "string" ? rawAvatar.name : rawConfig.avatarName,
1107
+ ...cfgObj,
1108
+ projectId: returnedProjectId,
1109
+ avatarId: returnedAvatarId,
1110
+ avatarName: typeof rawAvatar.name === "string" ? rawAvatar.name : typeof data.name === "string" ? data.name : cfgObj.avatarName,
804
1111
  assetUrl,
1112
+ thumbnailUrl: thumbnailUrl ?? (rawAvatar.thumbnailUrl === null ? null : void 0),
805
1113
  modelFormat,
806
1114
  publishedVersion: typeof data.publishedVersionId === "string" ? data.publishedVersionId : revision,
807
1115
  version: Number.isFinite(version) ? version : 0,
808
1116
  animations,
809
- theme: rawConfig.theme ?? rawAvatar.theme,
810
- personality: rawConfig.personality,
811
- behavior: rawConfig.behavior ?? rawAvatar.behavior,
1117
+ theme: cfgObj.theme ?? rawAvatar.theme,
1118
+ personality: cfgObj.personality,
1119
+ behavior: cfgObj.behavior ?? rawAvatar.behavior,
812
1120
  maxMessageChars: API_LIMITS.maxMessageChars
813
1121
  });
814
1122
  this.configMetadata = {
@@ -839,12 +1147,18 @@ var CloudClient = class {
839
1147
  }
840
1148
  async registerInstallation(registration) {
841
1149
  const response = await this.transport.request({
842
- path: contractApiUrl(this.getApiBaseUrl(), API_ROUTES.installationRegister),
1150
+ path: this.withProjectQuery(
1151
+ contractApiUrl(this.getApiBaseUrl(), API_ROUTES.installationRegister),
1152
+ { publicKey: registration.publicKey }
1153
+ ),
843
1154
  method: "POST",
844
1155
  headers: {
845
1156
  Accept: "application/json",
846
1157
  "Content-Type": "application/json",
847
- "X-Asiyst-SDK-Version": registration.sdkVersion
1158
+ "X-Asiyst-SDK-Version": registration.sdkVersion,
1159
+ "X-Asiyst-Project-Id": registration.projectId,
1160
+ "X-Asiyst-Public-Key": registration.publicKey,
1161
+ "X-Asiyst-Project-Key": registration.publicKey
848
1162
  },
849
1163
  body: {
850
1164
  projectId: registration.projectId,
@@ -867,14 +1181,18 @@ var CloudClient = class {
867
1181
  }
868
1182
  async registerRuntime(registration) {
869
1183
  const response = await this.transport.request({
870
- path: contractApiUrl(this.getApiBaseUrl(), API_ROUTES.runtimeRegister),
1184
+ path: this.withProjectQuery(
1185
+ contractApiUrl(this.getApiBaseUrl(), API_ROUTES.runtimeRegister),
1186
+ { publicKey: registration.publicKey }
1187
+ ),
871
1188
  method: "POST",
872
1189
  headers: {
873
1190
  Accept: "application/json",
874
1191
  "Content-Type": "application/json",
875
1192
  "X-Asiyst-SDK-Version": registration.sdkVersion || "",
876
1193
  "X-Asiyst-Project-Id": registration.projectId,
877
- "X-Asiyst-Public-Key": registration.publicKey
1194
+ "X-Asiyst-Public-Key": registration.publicKey,
1195
+ "X-Asiyst-Project-Key": registration.publicKey
878
1196
  },
879
1197
  body: {
880
1198
  projectId: registration.projectId,
@@ -902,22 +1220,31 @@ var CloudClient = class {
902
1220
  return body;
903
1221
  }
904
1222
  async fetchManifest(manifestUrl) {
905
- const url = manifestUrl || (this.avatarId ? contractApiUrl(this.getApiBaseUrl(), API_ROUTES.avatarManifest(this.projectId, this.avatarId)) : void 0);
906
- if (!url) {
1223
+ const rawUrl = manifestUrl || (this.avatarId ? contractApiUrl(this.getApiBaseUrl(), API_ROUTES.avatarManifest(this.projectId, this.avatarId)) : void 0);
1224
+ if (!rawUrl) {
907
1225
  throw new ConfigurationError("Manifest URL is required to fetch avatar manifest.");
908
1226
  }
1227
+ const url = this.withProjectQuery(rawUrl, { publicKey: this.publicKey || void 0 });
909
1228
  const response = await this.runtimeRequest({
910
1229
  path: url,
911
1230
  method: "GET",
912
1231
  headers: {
913
1232
  Accept: "application/json",
914
1233
  "X-Asiyst-Public-Key": this.publicKey,
1234
+ "X-Asiyst-Project-Key": this.publicKey,
915
1235
  "X-Asiyst-Project-Id": this.projectId
916
1236
  }
917
1237
  }, "assistant:config");
918
1238
  if (!response.ok || !response.data) {
919
1239
  throw new NetworkError(`Failed to fetch avatar manifest: HTTP ${response.status}`);
920
1240
  }
1241
+ const rawData = response.data;
1242
+ if (rawData.data && typeof rawData.data === "object" && !Array.isArray(rawData.data)) {
1243
+ return rawData.data;
1244
+ }
1245
+ if (rawData.manifest && typeof rawData.manifest === "object" && !Array.isArray(rawData.manifest)) {
1246
+ return rawData.manifest;
1247
+ }
921
1248
  return response.data;
922
1249
  }
923
1250
  async submitContext(request) {
@@ -1135,6 +1462,7 @@ var CloudClient = class {
1135
1462
  async runtimeRequest(request, capability = "assistant:config") {
1136
1463
  const fullRequest = {
1137
1464
  ...request,
1465
+ path: this.withProjectQuery(request.path),
1138
1466
  sessionId: request.sessionId ?? this.sessionId,
1139
1467
  conversationId: request.conversationId ?? this.conversationId
1140
1468
  };
@@ -1300,7 +1628,8 @@ var ConfigManager = class {
1300
1628
  new NamespacedStorage(credentials.projectId, browserLocalStorage())
1301
1629
  );
1302
1630
  const cached = this.cache.read(credentials.projectId);
1303
- this.current = cached ?? this.localFallback();
1631
+ const canUseCached = cached && (!credentials.avatarId || cached.avatarId === credentials.avatarId);
1632
+ this.current = canUseCached ? cached : this.localFallback();
1304
1633
  }
1305
1634
  get() {
1306
1635
  return this.current;
@@ -1316,11 +1645,14 @@ var ConfigManager = class {
1316
1645
  this.inFlightRefresh = null;
1317
1646
  }
1318
1647
  }
1319
- async executeRefresh(_options) {
1648
+ async executeRefresh(options) {
1320
1649
  const prev = this.current;
1321
1650
  try {
1322
1651
  const cached = this.cache.getEnvelope(this.options.projectId);
1323
- const normalized = normalizeProjectConfig(await this.cloud.fetchConfig(cached?.etag, cached?.config));
1652
+ const validCachedConfig = cached?.config && (!this.options.avatarId || cached.config.avatarId === this.options.avatarId) ? cached.config : void 0;
1653
+ const normalized = normalizeProjectConfig(
1654
+ await this.cloud.fetchConfig(validCachedConfig ? cached?.etag : void 0, validCachedConfig)
1655
+ );
1324
1656
  const receivedMetadata = this.cloud.getConfigMetadata?.() ?? {};
1325
1657
  const metadata = {
1326
1658
  ...receivedMetadata,
@@ -1335,16 +1667,20 @@ var ConfigManager = class {
1335
1667
  }
1336
1668
  return this.current;
1337
1669
  } catch (error) {
1338
- const cached = this.cache.getEnvelope(this.options.projectId);
1339
- const staleIfErrorMs = cached?.staleIfErrorMs ?? API_LIMITS.configStaleIfErrorMs;
1340
- if (cached && staleIfErrorMs > 0 && Date.now() - cached.fetchedAt <= staleIfErrorMs) {
1341
- this.current = normalizeProjectConfig(cached.config);
1342
- }
1670
+ const errCode = error && typeof error === "object" && "code" in error ? String(error.code) : "config_refresh_failed";
1343
1671
  this.events.emit("asiyst:error", {
1344
- code: "config_refresh_failed",
1672
+ code: errCode,
1345
1673
  message: error instanceof Error ? error.message : "Failed to refresh configuration"
1346
1674
  });
1347
- return this.current;
1675
+ if (options?.silent) {
1676
+ const cached = this.cache.getEnvelope(this.options.projectId);
1677
+ const staleIfErrorMs = cached?.staleIfErrorMs ?? API_LIMITS.configStaleIfErrorMs;
1678
+ if (cached && staleIfErrorMs > 0 && Date.now() - cached.fetchedAt <= staleIfErrorMs && (!this.options.avatarId || cached.config.avatarId === this.options.avatarId)) {
1679
+ this.current = normalizeProjectConfig(cached.config);
1680
+ }
1681
+ return this.current;
1682
+ }
1683
+ throw error;
1348
1684
  }
1349
1685
  }
1350
1686
  applyLocalUpdate(partial) {
@@ -2393,8 +2729,9 @@ var AssetLoader = class {
2393
2729
  continue;
2394
2730
  }
2395
2731
  throw new AsiystError({
2396
- code: "NETWORK_ERROR",
2732
+ code: "CORS_OR_NETWORK_FAILURE",
2397
2733
  message: `CORS or network policy blocked avatar asset retrieval from ${sanitizedUrl}`,
2734
+ hint: "Check that the asset server allows cross-origin requests from your website origin and that the URL is reachable.",
2398
2735
  cause: err
2399
2736
  });
2400
2737
  } finally {
@@ -2409,24 +2746,32 @@ var AssetLoader = class {
2409
2746
  break;
2410
2747
  }
2411
2748
  if (!response.ok) {
2749
+ if (response.status === 401) {
2750
+ throw new AsiystError({
2751
+ code: "UNAUTHORIZED",
2752
+ status: 401,
2753
+ message: `Unauthorized access (HTTP 401) when retrieving avatar asset from ${sanitizedUrl}`,
2754
+ hint: "Verify your publicKey and project authentication settings."
2755
+ });
2756
+ }
2412
2757
  if (response.status === 403) {
2413
2758
  throw new AsiystError({
2414
2759
  code: "DOMAIN_NOT_APPROVED",
2415
2760
  status: 403,
2416
2761
  message: `Avatar asset request forbidden (HTTP 403) from ${sanitizedUrl}`,
2417
- hint: "Check that the current website domain or origin is approved in the project settings."
2762
+ hint: "Check that the current website domain or origin is approved in the project settings, or refresh an expired signed asset URL."
2418
2763
  });
2419
2764
  }
2420
2765
  if (response.status === 404) {
2421
2766
  throw new AsiystError({
2422
- code: "AVATAR_NOT_FOUND",
2767
+ code: options?.isAnimation ? "MISSING_ANIMATION_FILE" : "MISSING_CHARACTER_FILE",
2423
2768
  status: 404,
2424
- message: `Avatar asset not found (HTTP 404) at ${sanitizedUrl}`,
2425
- hint: "Verify the avatarId and ensure a 3D model asset is published for this avatar."
2769
+ message: options?.isAnimation ? `Animation FBX/GLB file not found (HTTP 404) at ${sanitizedUrl}` : `Character FBX/GLB file not found (HTTP 404) at ${sanitizedUrl}`,
2770
+ hint: options?.isAnimation ? "Verify that the separate animation FBX/GLB file is uploaded and published for this avatar." : "Verify the avatarId and ensure a character 3D model asset is uploaded and published for this avatar."
2426
2771
  });
2427
2772
  }
2428
2773
  throw new AsiystError({
2429
- code: "NETWORK_ERROR",
2774
+ code: "INVALID_ASSET_RESPONSE",
2430
2775
  status: response.status,
2431
2776
  message: `Avatar asset download failed: HTTP ${response.status} from ${sanitizedUrl}`
2432
2777
  });
@@ -2434,7 +2779,7 @@ var AssetLoader = class {
2434
2779
  const contentType = (response.headers.get("content-type") || "").toLowerCase();
2435
2780
  if (contentType.includes("text/html")) {
2436
2781
  throw new AsiystError({
2437
- code: "ASSET_PARSE_FAILED",
2782
+ code: "INVALID_ASSET_RESPONSE",
2438
2783
  message: `Avatar asset request returned HTML instead of 3D asset from ${sanitizedUrl}`,
2439
2784
  hint: "The server returned a web page instead of 3D asset bytes. Check authentication or routing."
2440
2785
  });
@@ -2446,16 +2791,22 @@ var AssetLoader = class {
2446
2791
  manifest = JSON.parse(text);
2447
2792
  } catch {
2448
2793
  throw new AsiystError({
2449
- code: "ASSET_PARSE_FAILED",
2794
+ code: "INVALID_ASSET_RESPONSE",
2450
2795
  message: "Invalid avatar manifest: JSON decoding failed"
2451
2796
  });
2452
2797
  }
2453
2798
  if (typeof manifest !== "object" || manifest === null || Array.isArray(manifest)) {
2454
2799
  throw new AsiystError({
2455
- code: "ASSET_PARSE_FAILED",
2800
+ code: "INVALID_ASSET_RESPONSE",
2456
2801
  message: "Invalid avatar manifest schema"
2457
2802
  });
2458
2803
  }
2804
+ if (options?.expectedFormat === "fbx" || options?.expectedFormat === "glb") {
2805
+ throw new AsiystError({
2806
+ code: "INVALID_ASSET_RESPONSE",
2807
+ message: `Avatar asset request returned JSON instead of ${options.expectedFormat.toUpperCase()} binary from ${sanitizedUrl}`
2808
+ });
2809
+ }
2459
2810
  return {
2460
2811
  format: "manifest",
2461
2812
  url: sanitizedUrl,
@@ -2569,7 +2920,7 @@ var AssetLoader = class {
2569
2920
  }
2570
2921
  if (arrayBuffer.byteLength < 12) {
2571
2922
  throw new AsiystError({
2572
- code: "ASSET_PARSE_FAILED",
2923
+ code: "INVALID_ASSET_RESPONSE",
2573
2924
  message: "Avatar asset payload is too small to be a valid 3D container"
2574
2925
  });
2575
2926
  }
@@ -2580,7 +2931,7 @@ var AssetLoader = class {
2580
2931
  const headerStr = new TextDecoder("utf-8", { fatal: false }).decode(headerBytes).trim();
2581
2932
  if (/^<!DOCTYPE\s+html/i.test(headerStr) || /^<html/i.test(headerStr)) {
2582
2933
  throw new AsiystError({
2583
- code: "ASSET_PARSE_FAILED",
2934
+ code: "INVALID_ASSET_RESPONSE",
2584
2935
  message: `Avatar asset payload contains HTML error page instead of binary 3D container from ${sanitizedUrl}`
2585
2936
  });
2586
2937
  }
@@ -2590,14 +2941,14 @@ var AssetLoader = class {
2590
2941
  const isGlb = hasGlbSignature || !hasFbxSignature && isGlbExtension;
2591
2942
  if (isFbx && isGlb) {
2592
2943
  throw new AsiystError({
2593
- code: "ASSET_PARSE_FAILED",
2944
+ code: "INVALID_ASSET_RESPONSE",
2594
2945
  message: "Avatar asset format is ambiguous"
2595
2946
  });
2596
2947
  }
2597
2948
  if (isFbx) {
2598
2949
  if (options?.expectedFormat && options.expectedFormat !== "fbx") {
2599
2950
  throw new AsiystError({
2600
- code: "ASSET_PARSE_FAILED",
2951
+ code: "INVALID_ASSET_RESPONSE",
2601
2952
  message: `Avatar asset format mismatch: expected ${options.expectedFormat}, received fbx`
2602
2953
  });
2603
2954
  }
@@ -2607,7 +2958,16 @@ var AssetLoader = class {
2607
2958
  console.warn(`[asiyst] Large FBX avatar asset detected (${mb} MB). Serving raw FBX over HTTP causes slow download and render delays. Recommendation: Optimize and convert to Draco-compressed GLB (< 3 MB).`);
2608
2959
  }
2609
2960
  }
2610
- const fbxHeader = this.parseFbxHeader(arrayBuffer);
2961
+ let fbxHeader;
2962
+ try {
2963
+ fbxHeader = this.parseFbxHeader(arrayBuffer);
2964
+ } catch (fbxErr) {
2965
+ throw new AsiystError({
2966
+ code: "INCOMPATIBLE_FBX",
2967
+ message: fbxErr instanceof Error ? fbxErr.message : "Unsupported or incompatible FBX header",
2968
+ cause: fbxErr
2969
+ });
2970
+ }
2611
2971
  const resourcePath = sanitizedUrl.includes("/") ? sanitizedUrl.substring(0, sanitizedUrl.lastIndexOf("/") + 1) : "";
2612
2972
  return {
2613
2973
  format: "fbx",
@@ -2621,13 +2981,13 @@ var AssetLoader = class {
2621
2981
  }
2622
2982
  if (!isGlb) {
2623
2983
  throw new AsiystError({
2624
- code: "ASSET_PARSE_FAILED",
2625
- message: "Avatar asset format is unsupported; expected FBX or GLB"
2984
+ code: "INCOMPATIBLE_FBX",
2985
+ message: "Avatar asset format is unsupported or incompatible; expected valid FBX or GLB container"
2626
2986
  });
2627
2987
  }
2628
2988
  if (options?.expectedFormat && options.expectedFormat !== "glb") {
2629
2989
  throw new AsiystError({
2630
- code: "ASSET_PARSE_FAILED",
2990
+ code: "INVALID_ASSET_RESPONSE",
2631
2991
  message: `Avatar asset format mismatch: expected ${options.expectedFormat}, received glb`
2632
2992
  });
2633
2993
  }
@@ -2986,27 +3346,57 @@ var RigDetector = class {
2986
3346
  return rawName.trim().replace(/^.*[:|/]/, "").replace(/^(mixamorig\d*|bip01|armature|root)[:_ ]?/i, "").replace(/[^a-zA-Z0-9._-]/g, "").toLowerCase();
2987
3347
  }
2988
3348
  /**
2989
- * Analyzes an imported 3D model root, identifies all bones, maps them to canonical AUBR,
2990
- * validates hierarchy, and reports capability profile.
3349
+ * Analyzes an imported 3D model root, identifies all bones and skinned meshes,
3350
+ * maps them to canonical AUBR, validates hierarchy, and reports capability profile.
2991
3351
  */
2992
- static detect(modelRoot) {
3352
+ static detect(modelRoot, options) {
2993
3353
  const allBones = [];
3354
+ const seenBones = /* @__PURE__ */ new Set();
3355
+ const skinnedMeshes = [];
2994
3356
  modelRoot.traverse((node) => {
2995
- if (node.isBone) {
3357
+ if (node.isSkinnedMesh) {
3358
+ const sm = node;
3359
+ skinnedMeshes.push(sm);
3360
+ if (sm.skeleton?.bones) {
3361
+ for (const b of sm.skeleton.bones) {
3362
+ if (b && !seenBones.has(b)) {
3363
+ seenBones.add(b);
3364
+ allBones.push(b);
3365
+ }
3366
+ }
3367
+ }
3368
+ }
3369
+ if (node.isBone && !seenBones.has(node)) {
3370
+ seenBones.add(node);
2996
3371
  allBones.push(node);
2997
3372
  }
2998
3373
  });
2999
3374
  if (allBones.length === 0) {
3000
3375
  modelRoot.traverse((node) => {
3376
+ if (node.isMesh) return;
3001
3377
  if (node.name && (node.type === "Bone" || /bone|joint|limb|hips|spine|arm|leg|head/i.test(node.name))) {
3002
- if (!allBones.includes(node)) {
3378
+ if (!seenBones.has(node)) {
3379
+ seenBones.add(node);
3003
3380
  allBones.push(node);
3004
3381
  }
3005
3382
  }
3006
3383
  });
3007
3384
  }
3385
+ const embeddedAnimationCount = typeof options?.embeddedAnimationCount === "number" && Number.isFinite(options.embeddedAnimationCount) ? Math.max(0, Math.floor(options.embeddedAnimationCount)) : Array.isArray(modelRoot.animations) ? modelRoot.animations.length : 0;
3386
+ const hasEmbeddedAnimations = embeddedAnimationCount > 0;
3387
+ const skinnedMeshCount = skinnedMeshes.length;
3388
+ const hasSkinnedMesh = skinnedMeshCount > 0;
3389
+ const boneCount = allBones.length;
3390
+ const hasSkeleton = boneCount > 0;
3008
3391
  if (allBones.length === 0) {
3009
- const caps = this.createUnsupportedCapabilities("unsupported", ["hips", "spine", "head"]);
3392
+ const caps = this.createUnsupportedCapabilities("unsupported", ["hips", "spine", "head"], {
3393
+ hasSkeleton,
3394
+ hasSkinnedMesh,
3395
+ boneCount,
3396
+ skinnedMeshCount,
3397
+ embeddedAnimationCount,
3398
+ hasEmbeddedAnimations
3399
+ });
3010
3400
  return {
3011
3401
  rigType: "unsupported",
3012
3402
  capabilities: caps,
@@ -3047,26 +3437,43 @@ var RigDetector = class {
3047
3437
  const hasArms = matchedBones.has("leftUpperArm") && matchedBones.has("leftLowerArm") && matchedBones.has("rightUpperArm") && matchedBones.has("rightLowerArm");
3048
3438
  const isHumanoid = hasHipsAndSpine && (hasLegs || hasArms);
3049
3439
  let finalRigType = rigFlavor;
3440
+ const detectedRigFlavor = isHumanoid ? rigFlavor : "unsupported";
3050
3441
  if (!isHumanoid) {
3051
3442
  finalRigType = "unsupported";
3052
3443
  } else if (missingRequired.length > 0) {
3053
3444
  finalRigType = "partial_humanoid";
3054
3445
  }
3446
+ const proceduralIdle = isHumanoid && hasHipsAndSpine && hasHead;
3447
+ const proceduralLocomotion = isHumanoid && hasLegs;
3448
+ const proceduralRunning = isHumanoid && hasLegs;
3449
+ const upperBodyGestures = isHumanoid && hasArms && hasHipsAndSpine;
3450
+ const proceduralAnimationAvailable = proceduralIdle || proceduralLocomotion || upperBodyGestures;
3451
+ const clipPlayback = hasSkeleton && allBones.length > 5 || hasEmbeddedAnimations;
3452
+ const animationSourceMode = proceduralAnimationAvailable && hasEmbeddedAnimations ? "embedded_and_procedural" : proceduralAnimationAvailable ? "procedural_only" : hasEmbeddedAnimations ? "embedded_only" : "static";
3055
3453
  const capabilities = {
3056
3454
  rigType: finalRigType,
3455
+ detectedRigFlavor,
3057
3456
  isHumanoid,
3457
+ hasSkeleton,
3458
+ hasSkinnedMesh,
3459
+ boneCount,
3460
+ skinnedMeshCount,
3058
3461
  confidence: Math.round(confidence * 100) / 100,
3059
- proceduralIdle: hasHipsAndSpine && hasHead,
3060
- proceduralLocomotion: isHumanoid && hasLegs,
3061
- proceduralRunning: isHumanoid && hasLegs,
3462
+ proceduralAnimationAvailable,
3463
+ proceduralIdle,
3464
+ proceduralLocomotion,
3465
+ proceduralRunning,
3062
3466
  turning: isHumanoid,
3063
- legIK: hasLegs,
3064
- armIK: hasArms,
3065
- footPlacement: hasLegs,
3066
- headLookAt: hasHead,
3067
- upperBodyGestures: hasArms && hasHipsAndSpine,
3467
+ legIK: isHumanoid && hasLegs,
3468
+ armIK: isHumanoid && hasArms,
3469
+ footPlacement: isHumanoid && hasLegs,
3470
+ headLookAt: isHumanoid && hasHead,
3471
+ upperBodyGestures,
3068
3472
  facialAnimation: this.detectFacialCapabilities(modelRoot),
3069
- clipPlayback: allBones.length > 5,
3473
+ hasEmbeddedAnimations,
3474
+ embeddedAnimationCount,
3475
+ clipPlayback,
3476
+ animationSourceMode,
3070
3477
  missingRequiredBones: missingRequired,
3071
3478
  missingOptionalBones: missingOptional
3072
3479
  };
@@ -3081,15 +3488,15 @@ var RigDetector = class {
3081
3488
  /**
3082
3489
  * Alias for detect()
3083
3490
  */
3084
- static detectRig(modelRoot) {
3085
- return this.detect(modelRoot);
3491
+ static detectRig(modelRoot, options) {
3492
+ return this.detect(modelRoot, options);
3086
3493
  }
3087
3494
  /**
3088
3495
  * Assesses avatar rig capabilities from detection result or model.
3089
3496
  */
3090
- static assessCapabilities(detection, modelRoot) {
3497
+ static assessCapabilities(detection, modelRoot, options) {
3091
3498
  if (modelRoot) {
3092
- return this.detect(modelRoot).capabilities;
3499
+ return this.detect(modelRoot, options).capabilities;
3093
3500
  }
3094
3501
  return detection.capabilities;
3095
3502
  }
@@ -3154,6 +3561,24 @@ var RigDetector = class {
3154
3561
  if (rightLow && rightFoot && !this.isDescendantOf(rightFoot, rightLow)) {
3155
3562
  matched.delete("rightFoot");
3156
3563
  }
3564
+ const leftUpArm = matched.get("leftUpperArm");
3565
+ const leftLowArm = matched.get("leftLowerArm");
3566
+ const leftHand = matched.get("leftHand");
3567
+ if (leftUpArm && leftLowArm && !this.isDescendantOf(leftLowArm, leftUpArm)) {
3568
+ matched.delete("leftLowerArm");
3569
+ }
3570
+ if (leftLowArm && leftHand && !this.isDescendantOf(leftHand, leftLowArm)) {
3571
+ matched.delete("leftHand");
3572
+ }
3573
+ const rightUpArm = matched.get("rightUpperArm");
3574
+ const rightLowArm = matched.get("rightLowerArm");
3575
+ const rightHand = matched.get("rightHand");
3576
+ if (rightUpArm && rightLowArm && !this.isDescendantOf(rightLowArm, rightUpArm)) {
3577
+ matched.delete("rightLowerArm");
3578
+ }
3579
+ if (rightLowArm && rightHand && !this.isDescendantOf(rightHand, rightLowArm)) {
3580
+ matched.delete("rightHand");
3581
+ }
3157
3582
  const hips = matched.get("hips");
3158
3583
  const spine = matched.get("spine");
3159
3584
  const head = matched.get("head");
@@ -3182,11 +3607,19 @@ var RigDetector = class {
3182
3607
  });
3183
3608
  return hasMorphTargets;
3184
3609
  }
3185
- static createUnsupportedCapabilities(rigType, missingReq) {
3610
+ static createUnsupportedCapabilities(rigType, missingReq, meta) {
3611
+ const hasEmbeddedAnimations = meta?.hasEmbeddedAnimations ?? false;
3612
+ const embeddedAnimationCount = meta?.embeddedAnimationCount ?? 0;
3186
3613
  return {
3187
3614
  rigType,
3615
+ detectedRigFlavor: rigType,
3188
3616
  isHumanoid: false,
3617
+ hasSkeleton: meta?.hasSkeleton ?? false,
3618
+ hasSkinnedMesh: meta?.hasSkinnedMesh ?? false,
3619
+ boneCount: meta?.boneCount ?? 0,
3620
+ skinnedMeshCount: meta?.skinnedMeshCount ?? 0,
3189
3621
  confidence: 0,
3622
+ proceduralAnimationAvailable: false,
3190
3623
  proceduralIdle: false,
3191
3624
  proceduralLocomotion: false,
3192
3625
  proceduralRunning: false,
@@ -3197,7 +3630,10 @@ var RigDetector = class {
3197
3630
  headLookAt: false,
3198
3631
  upperBodyGestures: false,
3199
3632
  facialAnimation: false,
3200
- clipPlayback: false,
3633
+ hasEmbeddedAnimations,
3634
+ embeddedAnimationCount,
3635
+ clipPlayback: hasEmbeddedAnimations,
3636
+ animationSourceMode: hasEmbeddedAnimations ? "embedded_only" : "static",
3201
3637
  missingRequiredBones: missingReq,
3202
3638
  missingOptionalBones: [...OPTIONAL_CANONICAL_BONES]
3203
3639
  };
@@ -3258,12 +3694,16 @@ ScratchMath.v4 = new THREE__namespace.Vector3();
3258
3694
  ScratchMath.v5 = new THREE__namespace.Vector3();
3259
3695
  ScratchMath.v6 = new THREE__namespace.Vector3();
3260
3696
  ScratchMath.v7 = new THREE__namespace.Vector3();
3697
+ ScratchMath.v8 = new THREE__namespace.Vector3();
3698
+ ScratchMath.v9 = new THREE__namespace.Vector3();
3261
3699
  // Quaternion scratch pool
3262
3700
  ScratchMath.q0 = new THREE__namespace.Quaternion();
3263
3701
  ScratchMath.q1 = new THREE__namespace.Quaternion();
3264
3702
  ScratchMath.q2 = new THREE__namespace.Quaternion();
3265
3703
  ScratchMath.q3 = new THREE__namespace.Quaternion();
3266
3704
  ScratchMath.q4 = new THREE__namespace.Quaternion();
3705
+ ScratchMath.q5 = new THREE__namespace.Quaternion();
3706
+ ScratchMath.q6 = new THREE__namespace.Quaternion();
3267
3707
  // Matrix4 scratch pool
3268
3708
  ScratchMath.m0 = new THREE__namespace.Matrix4();
3269
3709
  ScratchMath.m1 = new THREE__namespace.Matrix4();
@@ -3280,16 +3720,29 @@ ScratchMath.LEFT = new THREE__namespace.Vector3(-1, 0, 0);
3280
3720
 
3281
3721
  // src/avatar/procedural/AUBRSkeleton.ts
3282
3722
  var AUBRSkeleton = class {
3283
- constructor(modelRoot) {
3723
+ constructor(modelRoot, options) {
3284
3724
  this.joints = /* @__PURE__ */ new Map();
3725
+ /** Rest foot positions in modelRoot local space */
3726
+ this.restLeftFootModelPos = new THREE__namespace.Vector3();
3727
+ this.restRightFootModelPos = new THREE__namespace.Vector3();
3285
3728
  this.boneToCanonical = /* @__PURE__ */ new Map();
3729
+ this.restModelQuat = /* @__PURE__ */ new Map();
3730
+ this.restModelQuatInv = /* @__PURE__ */ new Map();
3286
3731
  this.modelRoot = modelRoot;
3287
- const detection = RigDetector.detect(modelRoot);
3732
+ this.modelRoot.updateMatrixWorld(true);
3733
+ const detection = RigDetector.detect(modelRoot, options);
3288
3734
  this.capabilities = detection.capabilities;
3735
+ const rootWorldQuatInv = new THREE__namespace.Quaternion();
3736
+ this.modelRoot.getWorldQuaternion(rootWorldQuatInv).invert();
3289
3737
  for (const [canonicalName, bone] of detection.matchedBones.entries()) {
3290
3738
  const restPos = bone.position.clone();
3291
3739
  const restQuat = bone.quaternion.clone();
3292
3740
  const restScale = bone.scale.clone();
3741
+ const boneWorldQuat = new THREE__namespace.Quaternion();
3742
+ bone.getWorldQuaternion(boneWorldQuat);
3743
+ const boneModelQuat = rootWorldQuatInv.clone().multiply(boneWorldQuat).normalize();
3744
+ this.restModelQuat.set(canonicalName, boneModelQuat);
3745
+ this.restModelQuatInv.set(canonicalName, boneModelQuat.clone().invert());
3293
3746
  let restDir = new THREE__namespace.Vector3(0, 1, 0);
3294
3747
  let boneLen = 0.2;
3295
3748
  if (bone.children.length > 0) {
@@ -3318,6 +3771,79 @@ var AUBRSkeleton = class {
3318
3771
  this.boneToCanonical.set(bone, canonicalName);
3319
3772
  }
3320
3773
  this.proportions = this.calculateProportions(detection.matchedBones);
3774
+ this.worldUnitsPerMeter = this.proportions.totalHeight / 1.8;
3775
+ const hipsBone = detection.matchedBones.get("hips");
3776
+ let hipsParentScale = 1;
3777
+ if (hipsBone?.parent) {
3778
+ const parentScaleVec = new THREE__namespace.Vector3();
3779
+ hipsBone.parent.getWorldScale(parentScaleVec);
3780
+ const avg = (Math.abs(parentScaleVec.x) + Math.abs(parentScaleVec.y) + Math.abs(parentScaleVec.z)) / 3;
3781
+ if (Number.isFinite(avg) && avg > 1e-5) {
3782
+ hipsParentScale = avg;
3783
+ }
3784
+ }
3785
+ this.hipsLocalPerMeter = this.worldUnitsPerMeter / hipsParentScale;
3786
+ const leftArmRest = this.measureArmRest("leftUpperArm", "leftLowerArm", 1);
3787
+ const rightArmRest = this.measureArmRest("rightUpperArm", "rightLowerArm", -1);
3788
+ this.leftArmRestAngleFromDown = leftArmRest.angleFromDown;
3789
+ this.leftArmLowerSign = leftArmRest.lowerSign;
3790
+ this.rightArmRestAngleFromDown = rightArmRest.angleFromDown;
3791
+ this.rightArmLowerSign = rightArmRest.lowerSign;
3792
+ const leftFoot = detection.matchedBones.get("leftFoot");
3793
+ const rightFoot = detection.matchedBones.get("rightFoot");
3794
+ if (leftFoot && rightFoot) {
3795
+ leftFoot.getWorldPosition(this.restLeftFootModelPos);
3796
+ this.modelRoot.worldToLocal(this.restLeftFootModelPos);
3797
+ rightFoot.getWorldPosition(this.restRightFootModelPos);
3798
+ this.modelRoot.worldToLocal(this.restRightFootModelPos);
3799
+ this.hasRestFootPositions = true;
3800
+ } else {
3801
+ this.hasRestFootPositions = false;
3802
+ }
3803
+ }
3804
+ measureArmRest(upperName, lowerName, defaultLateralX) {
3805
+ const upper = this.joints.get(upperName)?.bone;
3806
+ const lower = this.joints.get(lowerName)?.bone;
3807
+ if (!upper || !lower) {
3808
+ return { angleFromDown: 0, lowerSign: defaultLateralX >= 0 ? -1 : 1 };
3809
+ }
3810
+ upper.getWorldPosition(ScratchMath.v0);
3811
+ lower.getWorldPosition(ScratchMath.v1);
3812
+ this.modelRoot.worldToLocal(ScratchMath.v0);
3813
+ this.modelRoot.worldToLocal(ScratchMath.v1);
3814
+ const dir = ScratchMath.v2.subVectors(ScratchMath.v1, ScratchMath.v0);
3815
+ if (dir.lengthSq() < 1e-8) {
3816
+ return { angleFromDown: 0, lowerSign: defaultLateralX >= 0 ? -1 : 1 };
3817
+ }
3818
+ dir.normalize();
3819
+ const dotDown = Math.max(-1, Math.min(1, dir.dot(ScratchMath.DOWN)));
3820
+ const lateralX = Math.abs(dir.x) > 0.05 ? dir.x : defaultLateralX;
3821
+ return {
3822
+ angleFromDown: Math.acos(dotDown),
3823
+ lowerSign: lateralX >= 0 ? -1 : 1
3824
+ };
3825
+ }
3826
+ /**
3827
+ * Applies a rotation delta defined in character model space (+Y up, +Z forward, +X right/left)
3828
+ * relative to the joint's rest pose, converted cleanly into the bone's local coordinate frame.
3829
+ * Allocation-free and rig-agnostic across Mixamo, Rigify, Unreal, VRM, and generic humanoids.
3830
+ */
3831
+ applyModelDeltaRotation(name, modelDeltaQuat, weight = 1, additive = false) {
3832
+ const joint = this.joints.get(name);
3833
+ const restModel = this.restModelQuat.get(name);
3834
+ const restModelInv = this.restModelQuatInv.get(name);
3835
+ if (!joint || !restModel || !restModelInv || weight <= 1e-3) return;
3836
+ ScratchMath.q5.copy(restModelInv).multiply(modelDeltaQuat).multiply(restModel);
3837
+ if (additive) {
3838
+ ScratchMath.q6.copy(joint.targetQuaternion).multiply(ScratchMath.q5);
3839
+ } else {
3840
+ ScratchMath.q6.copy(joint.restQuaternion).multiply(ScratchMath.q5);
3841
+ }
3842
+ if (weight >= 0.999) {
3843
+ joint.targetQuaternion.copy(ScratchMath.q6);
3844
+ } else {
3845
+ joint.targetQuaternion.slerp(ScratchMath.q6, Math.max(0, Math.min(1, weight)));
3846
+ }
3321
3847
  }
3322
3848
  getJoint(name) {
3323
3849
  return this.joints.get(name);
@@ -3384,67 +3910,102 @@ var AUBRSkeleton = class {
3384
3910
  }
3385
3911
  }
3386
3912
  calculateProportions(bones) {
3387
- const box = new THREE__namespace.Box3().setFromObject(this.modelRoot);
3388
- const size = box.getSize(new THREE__namespace.Vector3());
3389
- const totalHeight = size.y > 0 ? size.y : 1.78;
3913
+ const box = new THREE__namespace.Box3();
3914
+ try {
3915
+ box.setFromObject(this.modelRoot);
3916
+ } catch {
3917
+ box.makeEmpty();
3918
+ }
3919
+ const size = box.isEmpty() ? new THREE__namespace.Vector3(0, 0, 0) : box.getSize(new THREE__namespace.Vector3());
3920
+ let totalHeight = size.y > 0.01 ? size.y : 0;
3921
+ const head = bones.get("head");
3922
+ const leftFoot = bones.get("leftFoot");
3923
+ const rightFoot = bones.get("rightFoot");
3924
+ const hips = bones.get("hips");
3925
+ if (totalHeight <= 0.01 && head && (leftFoot || rightFoot)) {
3926
+ head.getWorldPosition(ScratchMath.v0);
3927
+ (leftFoot || rightFoot).getWorldPosition(ScratchMath.v1);
3928
+ const headToFoot = Math.abs(ScratchMath.v0.y - ScratchMath.v1.y);
3929
+ if (headToFoot > 0.01) {
3930
+ totalHeight = headToFoot * 1.15;
3931
+ }
3932
+ }
3933
+ if (totalHeight <= 0.01 && head && hips) {
3934
+ head.getWorldPosition(ScratchMath.v0);
3935
+ hips.getWorldPosition(ScratchMath.v1);
3936
+ const headToHips = ScratchMath.v0.distanceTo(ScratchMath.v1);
3937
+ if (headToHips > 0.01) {
3938
+ totalHeight = headToHips * 2.2;
3939
+ }
3940
+ }
3941
+ if (totalHeight <= 0.01) {
3942
+ totalHeight = 1.78;
3943
+ }
3390
3944
  const leftUp = bones.get("leftUpperLeg");
3391
3945
  const leftLow = bones.get("leftLowerLeg");
3392
- const leftFoot = bones.get("leftFoot");
3393
- let thighLength = 0.45;
3394
- let shinLength = 0.43;
3395
- let footHeight = 0.08;
3946
+ let thighLength = totalHeight * 0.25;
3947
+ let shinLength = totalHeight * 0.24;
3948
+ let footHeight = totalHeight * 0.045;
3396
3949
  if (leftUp && leftLow) {
3397
3950
  leftUp.getWorldPosition(ScratchMath.v0);
3398
3951
  leftLow.getWorldPosition(ScratchMath.v1);
3399
- thighLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3952
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3953
+ if (d > 1e-4) thighLength = d;
3400
3954
  }
3401
3955
  if (leftLow && leftFoot) {
3402
3956
  leftLow.getWorldPosition(ScratchMath.v0);
3403
3957
  leftFoot.getWorldPosition(ScratchMath.v1);
3404
- shinLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3405
- footHeight = Math.max(leftFoot.position.y, 0.08);
3958
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3959
+ if (d > 1e-4) shinLength = d;
3960
+ if (!box.isEmpty()) {
3961
+ const rawFootY = ScratchMath.v1.y - box.min.y;
3962
+ footHeight = Math.max(totalHeight * 0.03, Math.min(totalHeight * 0.12, rawFootY));
3963
+ }
3406
3964
  }
3407
3965
  const legLength = thighLength + shinLength + footHeight;
3408
3966
  const spine = bones.get("spine");
3409
- const head = bones.get("head");
3410
- let spineLength = 0.52;
3967
+ let spineLength = totalHeight * 0.29;
3411
3968
  if (spine && head) {
3412
3969
  spine.getWorldPosition(ScratchMath.v0);
3413
3970
  head.getWorldPosition(ScratchMath.v1);
3414
- spineLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3971
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3972
+ if (d > 1e-4) spineLength = d;
3415
3973
  }
3416
3974
  const leftArm = bones.get("leftUpperArm");
3417
3975
  const rightArm = bones.get("rightUpperArm");
3418
- let shoulderWidth = 0.42;
3976
+ let shoulderWidth = totalHeight * 0.23;
3419
3977
  if (leftArm && rightArm) {
3420
3978
  leftArm.getWorldPosition(ScratchMath.v0);
3421
3979
  rightArm.getWorldPosition(ScratchMath.v1);
3422
- shoulderWidth = ScratchMath.v0.distanceTo(ScratchMath.v1);
3980
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3981
+ if (d > 1e-4) shoulderWidth = d;
3423
3982
  }
3424
3983
  const rightUp = bones.get("rightUpperLeg");
3425
- let hipWidth = 0.28;
3984
+ let hipWidth = totalHeight * 0.15;
3426
3985
  if (leftUp && rightUp) {
3427
3986
  leftUp.getWorldPosition(ScratchMath.v0);
3428
3987
  rightUp.getWorldPosition(ScratchMath.v1);
3429
- hipWidth = ScratchMath.v0.distanceTo(ScratchMath.v1);
3988
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3989
+ if (d > 1e-4) hipWidth = d;
3430
3990
  }
3431
3991
  const leftHand = bones.get("leftHand");
3432
- let armLength = 0.65;
3992
+ let armLength = totalHeight * 0.36;
3433
3993
  if (leftArm && leftHand) {
3434
3994
  leftArm.getWorldPosition(ScratchMath.v0);
3435
3995
  leftHand.getWorldPosition(ScratchMath.v1);
3436
- armLength = ScratchMath.v0.distanceTo(ScratchMath.v1);
3996
+ const d = ScratchMath.v0.distanceTo(ScratchMath.v1);
3997
+ if (d > 1e-4) armLength = d;
3437
3998
  }
3438
3999
  return {
3439
4000
  totalHeight,
3440
- legLength: Math.max(legLength, 0.4),
3441
- thighLength: Math.max(thighLength, 0.2),
3442
- shinLength: Math.max(shinLength, 0.2),
3443
- footHeight: Math.max(footHeight, 0.05),
3444
- spineLength: Math.max(spineLength, 0.3),
3445
- shoulderWidth: Math.max(shoulderWidth, 0.25),
3446
- hipWidth: Math.max(hipWidth, 0.15),
3447
- armLength: Math.max(armLength, 0.35)
4001
+ legLength: Math.max(legLength, totalHeight * 0.25),
4002
+ thighLength: Math.max(thighLength, totalHeight * 0.12),
4003
+ shinLength: Math.max(shinLength, totalHeight * 0.12),
4004
+ footHeight: Math.max(footHeight, totalHeight * 0.025),
4005
+ spineLength: Math.max(spineLength, totalHeight * 0.15),
4006
+ shoulderWidth: Math.max(shoulderWidth, totalHeight * 0.12),
4007
+ hipWidth: Math.max(hipWidth, totalHeight * 0.08),
4008
+ armLength: Math.max(armLength, totalHeight * 0.2)
3448
4009
  };
3449
4010
  }
3450
4011
  };
@@ -3538,35 +4099,91 @@ var TwoBoneIK = class {
3538
4099
  };
3539
4100
  }
3540
4101
  /**
3541
- * Applies solved two-bone IK rotations to the target AUBR joints in-place.
4102
+ * Applies solved two-bone IK rotations to the target AUBR leg joints in-place
4103
+ * using rest-pose-preserving world-space delta rotations so Mixamo, Rigify, Unreal,
4104
+ * VRM, and generic humanoid bone orientations are never distorted.
4105
+ *
4106
+ * `targetFootPosWorld` and `poleDirWorld` are in world space (or model space when
4107
+ * `modelRoot` is omitted and bones are evaluated in local space).
3542
4108
  */
3543
- static applyLegIK(thighBone, shinBone, footBone, thighLength, shinLength, targetFootPosModel, poleVector, weight = 1, modelRoot) {
4109
+ static applyLegIK(thighBone, shinBone, footBone, thighLength, shinLength, targetFootPosWorld, poleDirWorld, weight = 1, _modelRoot) {
3544
4110
  if (weight <= 1e-3) return;
3545
- const rootPos = ScratchMath.v3;
3546
- thighBone.getWorldPosition(rootPos);
3547
- if (modelRoot) {
3548
- modelRoot.worldToLocal(rootPos);
3549
- } else if (thighBone.parent) {
3550
- thighBone.parent.worldToLocal(rootPos);
3551
- }
3552
- const chain = {
3553
- rootBone: thighBone,
3554
- midBone: shinBone,
3555
- endBone: footBone,
3556
- poleVector,
3557
- length1: thighLength,
3558
- length2: shinLength
3559
- };
3560
- const result = this.solveAnalytical(chain, targetFootPosModel, rootPos, poleVector);
3561
- if (weight >= 0.999) {
3562
- thighBone.quaternion.copy(result.rootRotation);
3563
- shinBone.quaternion.copy(result.midRotation);
4111
+ const clampedWeight = Math.max(0, Math.min(1, weight));
4112
+ const hipPos = ScratchMath.v0;
4113
+ const kneePos = ScratchMath.v1;
4114
+ const anklePos = ScratchMath.v2;
4115
+ thighBone.getWorldPosition(hipPos);
4116
+ shinBone.getWorldPosition(kneePos);
4117
+ footBone.getWorldPosition(anklePos);
4118
+ let l1 = hipPos.distanceTo(kneePos);
4119
+ let l2 = kneePos.distanceTo(anklePos);
4120
+ if (l1 < 1e-5) l1 = Math.max(thighLength, 0.05);
4121
+ if (l2 < 1e-5) l2 = Math.max(shinLength, 0.05);
4122
+ const maxReach = l1 + l2;
4123
+ const minReach = Math.abs(l1 - l2);
4124
+ const toTarget = ScratchMath.v3.subVectors(targetFootPosWorld, hipPos);
4125
+ const dist = toTarget.length();
4126
+ const clampedDist = Math.max(minReach + 1e-4, Math.min(dist, maxReach * 0.99999));
4127
+ if (dist < 1e-6) {
4128
+ toTarget.copy(ScratchMath.DOWN);
3564
4129
  } else {
3565
- thighBone.quaternion.slerp(result.rootRotation, weight);
3566
- shinBone.quaternion.slerp(result.midRotation, weight);
4130
+ toTarget.divideScalar(dist);
4131
+ }
4132
+ const bendDir = ScratchMath.v4.copy(poleDirWorld);
4133
+ const poleProj = bendDir.dot(toTarget);
4134
+ bendDir.addScaledVector(toTarget, -poleProj);
4135
+ if (bendDir.lengthSq() < 1e-6) {
4136
+ bendDir.crossVectors(toTarget, ScratchMath.RIGHT);
4137
+ if (bendDir.lengthSq() < 1e-6) {
4138
+ bendDir.crossVectors(toTarget, ScratchMath.FORWARD);
4139
+ }
4140
+ }
4141
+ bendDir.normalize();
4142
+ const cosAlpha = Math.max(
4143
+ -1,
4144
+ Math.min(1, (l1 * l1 + clampedDist * clampedDist - l2 * l2) / (2 * l1 * clampedDist))
4145
+ );
4146
+ const sinAlpha = Math.sqrt(Math.max(0, 1 - cosAlpha * cosAlpha));
4147
+ const desiredThighDir = ScratchMath.v5.copy(toTarget).multiplyScalar(cosAlpha).addScaledVector(bendDir, sinAlpha).normalize();
4148
+ const desiredKneePos = ScratchMath.v6.copy(hipPos).addScaledVector(desiredThighDir, l1);
4149
+ const desiredShinDir = ScratchMath.v7.subVectors(targetFootPosWorld, desiredKneePos);
4150
+ if (desiredShinDir.lengthSq() < 1e-8) {
4151
+ desiredShinDir.copy(toTarget);
4152
+ } else {
4153
+ desiredShinDir.normalize();
4154
+ }
4155
+ const currentThighDir = ScratchMath.v8.subVectors(kneePos, hipPos);
4156
+ if (currentThighDir.lengthSq() > 1e-10) {
4157
+ currentThighDir.normalize();
4158
+ ScratchMath.q0.setFromUnitVectors(currentThighDir, desiredThighDir);
4159
+ thighBone.getWorldQuaternion(ScratchMath.q1);
4160
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4161
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4162
+ ScratchMath.q4.copy(thighBone.quaternion).multiply(ScratchMath.q3);
4163
+ if (clampedWeight >= 0.999) {
4164
+ thighBone.quaternion.copy(ScratchMath.q4);
4165
+ } else {
4166
+ thighBone.quaternion.slerp(ScratchMath.q4, clampedWeight);
4167
+ }
4168
+ thighBone.updateMatrixWorld(true);
4169
+ }
4170
+ shinBone.getWorldPosition(kneePos);
4171
+ footBone.getWorldPosition(anklePos);
4172
+ const currentShinDir = ScratchMath.v8.subVectors(anklePos, kneePos);
4173
+ if (currentShinDir.lengthSq() > 1e-10) {
4174
+ currentShinDir.normalize();
4175
+ ScratchMath.q0.setFromUnitVectors(currentShinDir, desiredShinDir);
4176
+ shinBone.getWorldQuaternion(ScratchMath.q1);
4177
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4178
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4179
+ ScratchMath.q4.copy(shinBone.quaternion).multiply(ScratchMath.q3);
4180
+ if (clampedWeight >= 0.999) {
4181
+ shinBone.quaternion.copy(ScratchMath.q4);
4182
+ } else {
4183
+ shinBone.quaternion.slerp(ScratchMath.q4, clampedWeight);
4184
+ }
4185
+ shinBone.updateMatrixWorld(true);
3567
4186
  }
3568
- ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, 0);
3569
- footBone.quaternion.slerp(ScratchMath.q2, weight * 0.85);
3570
4187
  }
3571
4188
  };
3572
4189
  var _FABRIKSolver = class _FABRIKSolver {
@@ -3719,43 +4336,52 @@ var _FABRIKSolver = class _FABRIKSolver {
3719
4336
  };
3720
4337
  }
3721
4338
  /**
3722
- * Projects middle hinge joint onto plane defined by pole vector.
4339
+ * Projects middle hinge joint onto the half-plane defined by the pole direction vector.
3723
4340
  */
3724
4341
  static applyPoleVector(root, hinge, end, pole) {
3725
- const line = ScratchMath.v1.subVectors(end, root).normalize();
4342
+ const line = ScratchMath.v1.subVectors(end, root);
4343
+ if (line.lengthSq() < 1e-10) return;
4344
+ line.normalize();
3726
4345
  const toHinge = ScratchMath.v2.subVectors(hinge, root);
3727
4346
  const projDist = toHinge.dot(line);
3728
4347
  const projPoint = ScratchMath.v3.copy(root).addScaledVector(line, projDist);
3729
- const currentNormal = ScratchMath.v4.subVectors(hinge, projPoint).normalize();
3730
- const desiredNormal = ScratchMath.v5.subVectors(pole, projPoint).normalize();
3731
- if (currentNormal.lengthSq() > 1e-3 && desiredNormal.lengthSq() > 1e-3) {
4348
+ const desiredNormal = ScratchMath.v5.copy(pole).addScaledVector(line, -pole.dot(line));
4349
+ if (desiredNormal.lengthSq() > 1e-6) {
4350
+ desiredNormal.normalize();
3732
4351
  const hingeDist = hinge.distanceTo(projPoint);
3733
- hinge.copy(projPoint).addScaledVector(desiredNormal, Math.max(hingeDist, 0.05));
4352
+ hinge.copy(projPoint).addScaledVector(desiredNormal, Math.max(hingeDist, 0.02));
3734
4353
  }
3735
4354
  }
3736
4355
  /**
3737
4356
  * Converts solved world positions back into local bone rotations for the Three.js hierarchy.
4357
+ * Calculates delta rotation in world space from current bone direction to solved target direction,
4358
+ * transforming into local bone space to guarantee zero deformation when positions match rest pose.
3738
4359
  */
3739
4360
  static applyPositionsToBones(chain, solvedPositions, weight = 1) {
3740
4361
  if (solvedPositions.length < 2 || weight <= 1e-3) return;
3741
4362
  for (let i = 0; i < chain.bones.length - 1; i++) {
3742
4363
  const bone = chain.bones[i];
4364
+ const nextBone = chain.bones[i + 1];
3743
4365
  const nextPos = solvedPositions[i + 1];
3744
- if (!bone || !nextPos) continue;
4366
+ if (!bone || !nextBone || !nextPos) continue;
3745
4367
  bone.getWorldPosition(ScratchMath.v0);
4368
+ nextBone.getWorldPosition(ScratchMath.v2);
4369
+ const currentDirWorld = ScratchMath.v3.subVectors(ScratchMath.v2, ScratchMath.v0);
4370
+ if (currentDirWorld.lengthSq() < 1e-10) continue;
4371
+ currentDirWorld.normalize();
3746
4372
  const targetDirWorld = ScratchMath.v1.subVectors(nextPos, ScratchMath.v0);
3747
4373
  if (targetDirWorld.lengthSq() < 1e-10) continue;
3748
4374
  targetDirWorld.normalize();
3749
- if (bone.parent) {
3750
- ScratchMath.m0.copy(bone.parent.matrixWorld).invert();
3751
- targetDirWorld.transformDirection(ScratchMath.m0).normalize();
3752
- }
3753
- const restDir = ScratchMath.RIGHT;
3754
- ScratchMath.q0.setFromUnitVectors(restDir, targetDirWorld);
4375
+ ScratchMath.q0.setFromUnitVectors(currentDirWorld, targetDirWorld);
4376
+ bone.getWorldQuaternion(ScratchMath.q1);
4377
+ ScratchMath.q2.copy(ScratchMath.q1).invert();
4378
+ ScratchMath.q3.copy(ScratchMath.q2).multiply(ScratchMath.q0).multiply(ScratchMath.q1);
4379
+ const currentLocalQuat = ScratchMath.q4.copy(bone.quaternion);
4380
+ const targetLocalQuat = ScratchMath.q3.premultiply(currentLocalQuat);
3755
4381
  if (weight >= 0.999) {
3756
- bone.quaternion.copy(ScratchMath.q0);
4382
+ bone.quaternion.copy(targetLocalQuat);
3757
4383
  } else {
3758
- bone.quaternion.slerp(ScratchMath.q0, weight);
4384
+ bone.quaternion.copy(currentLocalQuat).slerp(targetLocalQuat, weight);
3759
4385
  }
3760
4386
  bone.updateMatrixWorld(true);
3761
4387
  }
@@ -3787,21 +4413,25 @@ var IKCoordinator = class {
3787
4413
  }
3788
4414
  /**
3789
4415
  * Solves and applies left leg Two-Bone IK.
4416
+ * `targetFootPosModel` is in `skeleton.modelRoot` local space.
3790
4417
  */
3791
4418
  solveLeftLeg(targetFootPosModel, weight = 1) {
3792
4419
  const thigh = this.skeleton.getJoint("leftUpperLeg");
3793
4420
  const shin = this.skeleton.getJoint("leftLowerLeg");
3794
4421
  const foot = this.skeleton.getJoint("leftFoot");
3795
- if (!thigh || !shin || !foot) return;
3796
- const pole = ScratchMath.v6.copy(ScratchMath.FORWARD);
4422
+ if (!thigh || !shin || !foot || weight <= 1e-3) return;
4423
+ const targetWorld = ScratchMath.v9.copy(targetFootPosModel);
4424
+ this.skeleton.modelRoot.localToWorld(targetWorld);
4425
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4426
+ const poleWorld = ScratchMath.v8.copy(ScratchMath.FORWARD).applyQuaternion(ScratchMath.q5).normalize();
3797
4427
  TwoBoneIK.applyLegIK(
3798
4428
  thigh.bone,
3799
4429
  shin.bone,
3800
4430
  foot.bone,
3801
4431
  this.skeleton.proportions.thighLength,
3802
4432
  this.skeleton.proportions.shinLength,
3803
- targetFootPosModel,
3804
- pole,
4433
+ targetWorld,
4434
+ poleWorld,
3805
4435
  weight,
3806
4436
  this.skeleton.modelRoot
3807
4437
  );
@@ -3811,21 +4441,25 @@ var IKCoordinator = class {
3811
4441
  }
3812
4442
  /**
3813
4443
  * Solves and applies right leg Two-Bone IK.
4444
+ * `targetFootPosModel` is in `skeleton.modelRoot` local space.
3814
4445
  */
3815
4446
  solveRightLeg(targetFootPosModel, weight = 1) {
3816
4447
  const thigh = this.skeleton.getJoint("rightUpperLeg");
3817
4448
  const shin = this.skeleton.getJoint("rightLowerLeg");
3818
4449
  const foot = this.skeleton.getJoint("rightFoot");
3819
- if (!thigh || !shin || !foot) return;
3820
- const pole = ScratchMath.v6.copy(ScratchMath.FORWARD);
4450
+ if (!thigh || !shin || !foot || weight <= 1e-3) return;
4451
+ const targetWorld = ScratchMath.v9.copy(targetFootPosModel);
4452
+ this.skeleton.modelRoot.localToWorld(targetWorld);
4453
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4454
+ const poleWorld = ScratchMath.v8.copy(ScratchMath.FORWARD).applyQuaternion(ScratchMath.q5).normalize();
3821
4455
  TwoBoneIK.applyLegIK(
3822
4456
  thigh.bone,
3823
4457
  shin.bone,
3824
4458
  foot.bone,
3825
4459
  this.skeleton.proportions.thighLength,
3826
4460
  this.skeleton.proportions.shinLength,
3827
- targetFootPosModel,
3828
- pole,
4461
+ targetWorld,
4462
+ poleWorld,
3829
4463
  weight,
3830
4464
  this.skeleton.modelRoot
3831
4465
  );
@@ -3839,7 +4473,8 @@ var IKCoordinator = class {
3839
4473
  solveArmReach(side, targetPosWorld, weight = 1) {
3840
4474
  const chain = side === "left" ? this.leftArmChain : this.rightArmChain;
3841
4475
  if (!chain || weight <= 1e-3) return;
3842
- const pole = side === "left" ? ScratchMath.v7.set(-0.5, 0, -1).normalize() : ScratchMath.v7.set(0.5, 0, -1).normalize();
4476
+ this.skeleton.modelRoot.getWorldQuaternion(ScratchMath.q5);
4477
+ const pole = (side === "left" ? ScratchMath.v7.set(-0.5, 0, -1).normalize() : ScratchMath.v7.set(0.5, 0, -1).normalize()).applyQuaternion(ScratchMath.q5);
3843
4478
  const result = FABRIKSolver.solve(chain, targetPosWorld, pole);
3844
4479
  FABRIKSolver.applyPositionsToBones(chain, result.solvedPositions, weight);
3845
4480
  const upper = this.skeleton.getJoint(side === "left" ? "leftUpperArm" : "rightUpperArm");
@@ -3863,63 +4498,86 @@ var IdleEngine = class {
3863
4498
  this.targetWeightShift = 0;
3864
4499
  this.nextWeightShiftTime = 3.5;
3865
4500
  }
4501
+ reset() {
4502
+ this.clock = 0;
4503
+ this.breathPhase = 0;
4504
+ this.swayPhase = 0;
4505
+ this.weightShift = 0;
4506
+ this.targetWeightShift = 0;
4507
+ this.nextWeightShiftTime = 3.5;
4508
+ }
3866
4509
  update(delta, weight = 1) {
3867
4510
  if (weight <= 1e-3) return;
4511
+ const w = Math.max(0, Math.min(1, weight));
3868
4512
  this.clock += delta;
3869
4513
  this.breathPhase += delta * 0.28;
3870
- this.swayPhase += delta * 0.12;
4514
+ this.swayPhase += delta * 0.14;
3871
4515
  if (this.clock > this.nextWeightShiftTime) {
3872
- this.targetWeightShift = (Math.random() - 0.5) * 1.6;
4516
+ this.targetWeightShift = (Math.random() - 0.5) * 1.4;
3873
4517
  this.nextWeightShiftTime = this.clock + 4 + Math.random() * 4;
3874
4518
  }
3875
- this.weightShift = ScratchMath.damp(this.weightShift, this.targetWeightShift, 1.2, delta);
4519
+ this.weightShift = ScratchMath.damp(this.weightShift, this.targetWeightShift, 1.4, delta);
4520
+ const localScale = this.skeleton.hipsLocalPerMeter;
3876
4521
  const breathSin = Math.sin(this.breathPhase * Math.PI * 2);
3877
- const spinePitch = breathSin * 0.015;
3878
- const chestPitch = breathSin * 0.022;
4522
+ const spinePitch = breathSin * 0.022 * w;
4523
+ const chestPitch = breathSin * 0.028 * w;
3879
4524
  const swaySin = Math.sin(this.swayPhase * Math.PI * 2);
3880
- const pelvicX = (swaySin * 6e-3 + this.weightShift * 0.012) * weight;
3881
- const pelvicY = breathSin * 4e-3 * weight;
3882
- const pelvicRoll = -this.weightShift * 0.018 * weight;
4525
+ const pelvicX = (swaySin * 8e-3 + this.weightShift * 0.014) * localScale * w;
4526
+ const pelvicY = breathSin * 5e-3 * localScale * w;
4527
+ const pelvicRoll = -this.weightShift * 0.022 * w;
3883
4528
  const hips = this.skeleton.getJoint("hips");
3884
4529
  if (hips) {
3885
4530
  hips.targetPosition.x = hips.restPosition.x + pelvicX;
3886
4531
  hips.targetPosition.y = hips.restPosition.y + pelvicY;
3887
4532
  ScratchMath.e0.set(0, 0, pelvicRoll, "YXZ");
3888
4533
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
3889
- hips.targetQuaternion.copy(hips.restQuaternion).multiply(ScratchMath.q0);
4534
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, w);
3890
4535
  }
3891
- const spine = this.skeleton.getJoint("spine");
3892
- if (spine) {
3893
- ScratchMath.e0.set(spinePitch * weight, 0, 0, "YXZ");
4536
+ if (this.skeleton.hasJoint("spine")) {
4537
+ ScratchMath.e0.set(spinePitch, swaySin * 0.012 * w, -pelvicRoll * 0.4, "YXZ");
3894
4538
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
3895
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
4539
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, w);
3896
4540
  }
3897
- const chest = this.skeleton.getJoint("chest") || this.skeleton.getJoint("upperChest");
3898
- if (chest) {
3899
- ScratchMath.e0.set(chestPitch * weight, 0, 0, "YXZ");
4541
+ const chestName = this.skeleton.hasJoint("chest") ? "chest" : this.skeleton.hasJoint("upperChest") ? "upperChest" : null;
4542
+ if (chestName) {
4543
+ ScratchMath.e0.set(chestPitch, 0, 0, "YXZ");
3900
4544
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
3901
- chest.targetQuaternion.copy(chest.restQuaternion).multiply(ScratchMath.q0);
4545
+ this.skeleton.applyModelDeltaRotation(chestName, ScratchMath.q0, w);
3902
4546
  }
3903
- const head = this.skeleton.getJoint("head");
3904
- if (head) {
3905
- const headCounterRoll = -pelvicRoll * 0.6;
3906
- const headCounterPitch = -spinePitch * 0.5;
4547
+ if (this.skeleton.hasJoint("head")) {
4548
+ const headCounterRoll = -pelvicRoll * 0.5;
4549
+ const headCounterPitch = -spinePitch * 0.45;
3907
4550
  ScratchMath.e0.set(headCounterPitch, 0, headCounterRoll, "YXZ");
3908
4551
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
3909
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
3910
- }
3911
- const lArm = this.skeleton.getJoint("leftUpperArm");
3912
- const rArm = this.skeleton.getJoint("rightUpperArm");
3913
- const armSway = breathSin * 0.012 * weight;
3914
- if (lArm) {
3915
- ScratchMath.e0.set(armSway, 0, armSway * 0.5, "YXZ");
3916
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
3917
- lArm.targetQuaternion.copy(lArm.restQuaternion).multiply(ScratchMath.q0);
3918
- }
3919
- if (rArm) {
3920
- ScratchMath.e0.set(armSway, 0, -armSway * 0.5, "YXZ");
3921
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
3922
- rArm.targetQuaternion.copy(rArm.restQuaternion).multiply(ScratchMath.q0);
4552
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, w);
4553
+ }
4554
+ const targetStandingArmAngle = 0.28;
4555
+ const armBreathSway = breathSin * 0.025;
4556
+ const armOutSway = breathSin * 0.018;
4557
+ if (this.skeleton.hasJoint("leftUpperArm")) {
4558
+ const relaxAngle = Math.max(0, this.skeleton.leftArmRestAngleFromDown - targetStandingArmAngle);
4559
+ const lowerZ = this.skeleton.leftArmLowerSign * (relaxAngle - armOutSway);
4560
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerZ);
4561
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -armBreathSway);
4562
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4563
+ this.skeleton.applyModelDeltaRotation("leftUpperArm", ScratchMath.q0, w);
4564
+ }
4565
+ if (this.skeleton.hasJoint("rightUpperArm")) {
4566
+ const relaxAngle = Math.max(0, this.skeleton.rightArmRestAngleFromDown - targetStandingArmAngle);
4567
+ const lowerZ = this.skeleton.rightArmLowerSign * (relaxAngle - armOutSway);
4568
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerZ);
4569
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -armBreathSway);
4570
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4571
+ this.skeleton.applyModelDeltaRotation("rightUpperArm", ScratchMath.q0, w);
4572
+ }
4573
+ const elbowRelaxFlex = 0.16 + breathSin * 0.015;
4574
+ if (this.skeleton.hasJoint("leftLowerArm")) {
4575
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -elbowRelaxFlex);
4576
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, w);
4577
+ }
4578
+ if (this.skeleton.hasJoint("rightLowerArm")) {
4579
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -elbowRelaxFlex);
4580
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, w);
3923
4581
  }
3924
4582
  }
3925
4583
  };
@@ -3962,9 +4620,10 @@ var LocomotionEngine = class {
3962
4620
  return this.phase;
3963
4621
  }
3964
4622
  calibrateStride() {
3965
- const legLen = this.skeleton.proportions.legLength;
3966
- this.strideLength = Math.max(0.35, legLen * 0.78);
3967
- this.stepHeight = Math.max(0.04, legLen * 0.095);
4623
+ const unitScale = this.skeleton.hipsLocalPerMeter;
4624
+ const legLenLocal = Math.max(0.4 * unitScale, this.skeleton.proportions.legLength / Math.max(this.skeleton.worldUnitsPerMeter, 1e-4) * unitScale);
4625
+ this.strideLength = Math.max(0.35 * unitScale, legLenLocal * 0.72);
4626
+ this.stepHeight = Math.max(0.04 * unitScale, legLenLocal * 0.11);
3968
4627
  }
3969
4628
  setMotion(params) {
3970
4629
  this.targetSpeed = Math.max(0, params.speed);
@@ -3974,113 +4633,128 @@ var LocomotionEngine = class {
3974
4633
  const stridesPerSecond = params.screenVelocityPx / 180;
3975
4634
  this.baseFrequency = Math.max(0.8, Math.min(3.2, stridesPerSecond * 2));
3976
4635
  } else {
3977
- this.baseFrequency = this.isRunning ? 2.6 : 1.5;
4636
+ this.baseFrequency = this.isRunning ? 2.4 : 1.35;
3978
4637
  }
3979
4638
  }
3980
4639
  stop() {
3981
4640
  this.targetSpeed = 0;
3982
4641
  this.moving = false;
3983
4642
  }
4643
+ cancel() {
4644
+ this.targetSpeed = 0;
4645
+ this.currentSpeed = 0;
4646
+ this.moving = false;
4647
+ this.isRunning = false;
4648
+ }
3984
4649
  update(delta, weight = 1) {
3985
- this.currentSpeed = ScratchMath.damp(this.currentSpeed, this.targetSpeed, 4, delta);
4650
+ const speedDamp = this.moving ? 6.5 : 8;
4651
+ this.currentSpeed = ScratchMath.damp(this.currentSpeed, this.targetSpeed, speedDamp, delta);
3986
4652
  if (this.currentSpeed <= 0.01 && weight <= 1e-3) return;
3987
- const effectiveFreq = this.baseFrequency * Math.max(0.3, this.currentSpeed);
4653
+ const w = Math.max(0, Math.min(1, weight));
4654
+ const speedRatio = Math.min(this.currentSpeed, 2.2);
4655
+ const activeAmplitude = Math.min(1, this.currentSpeed / 0.35) * w;
4656
+ if (activeAmplitude <= 1e-3) return;
4657
+ const effectiveFreq = this.baseFrequency * Math.max(0.35, Math.min(1.6, this.currentSpeed));
3988
4658
  this.phase = (this.phase + effectiveFreq * delta) % 1;
3989
4659
  const phi = this.phase;
3990
4660
  const twoPiPhi = phi * Math.PI * 2;
3991
- const speedRatio = Math.min(this.currentSpeed, 2);
3992
- const bobAmplitude = (this.isRunning ? 0.045 : 0.024) * speedRatio;
4661
+ const localScale = this.skeleton.hipsLocalPerMeter;
4662
+ const bobAmplitude = (this.isRunning ? 0.05 : 0.028) * speedRatio * localScale;
3993
4663
  const pelvicY = -bobAmplitude * Math.abs(Math.sin(twoPiPhi));
3994
- const swayAmplitude = (this.isRunning ? 0.018 : 0.028) * speedRatio;
4664
+ const swayAmplitude = (this.isRunning ? 0.02 : 0.03) * speedRatio * localScale;
3995
4665
  const pelvicX = swayAmplitude * Math.sin(twoPiPhi);
3996
4666
  const yawAmplitude = (this.isRunning ? 0.14 : 0.09) * speedRatio;
3997
4667
  const pelvicYaw = yawAmplitude * Math.cos(twoPiPhi);
3998
- const rollAmplitude = 0.035 * speedRatio;
4668
+ const rollAmplitude = 0.04 * speedRatio;
3999
4669
  const pelvicRoll = -rollAmplitude * Math.sin(twoPiPhi);
4000
4670
  const hips = this.skeleton.getJoint("hips");
4001
4671
  if (hips) {
4002
- hips.targetPosition.x = hips.restPosition.x + pelvicX * weight;
4003
- hips.targetPosition.y = hips.restPosition.y + pelvicY * weight;
4004
- ScratchMath.e0.set(0.04 * speedRatio, pelvicYaw * weight, pelvicRoll * weight, "YXZ");
4672
+ hips.targetPosition.x = hips.restPosition.x + pelvicX * w;
4673
+ hips.targetPosition.y = hips.restPosition.y + pelvicY * w;
4674
+ ScratchMath.e0.set((this.isRunning ? 0.08 : 0.04) * speedRatio, pelvicYaw, pelvicRoll, "YXZ");
4005
4675
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4006
- hips.targetQuaternion.copy(hips.restQuaternion).multiply(ScratchMath.q0);
4676
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, activeAmplitude);
4007
4677
  }
4008
- const spine = this.skeleton.getJoint("spine");
4009
- if (spine) {
4010
- const spineCounterYaw = -pelvicYaw * 0.65 * weight;
4011
- ScratchMath.e0.set(-0.02 * speedRatio, spineCounterYaw, 0, "YXZ");
4678
+ if (this.skeleton.hasJoint("spine")) {
4679
+ const spineCounterYaw = -pelvicYaw * 0.65;
4680
+ ScratchMath.e0.set((this.isRunning ? 0.08 : 0.03) * speedRatio, spineCounterYaw, -pelvicRoll * 0.35, "YXZ");
4012
4681
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4013
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
4682
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, activeAmplitude);
4014
4683
  }
4015
- const chest = this.skeleton.getJoint("chest") || this.skeleton.getJoint("upperChest");
4016
- if (chest) {
4017
- const chestCounterYaw = -pelvicYaw * 0.35 * weight;
4018
- ScratchMath.e0.set(0.01, chestCounterYaw, 0, "YXZ");
4684
+ const chestName = this.skeleton.hasJoint("chest") ? "chest" : this.skeleton.hasJoint("upperChest") ? "upperChest" : null;
4685
+ if (chestName) {
4686
+ const chestCounterYaw = -pelvicYaw * 0.35;
4687
+ ScratchMath.e0.set(0.02 * speedRatio, chestCounterYaw, 0, "YXZ");
4019
4688
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4020
- chest.targetQuaternion.copy(chest.restQuaternion).multiply(ScratchMath.q0);
4021
- }
4022
- const hipWidthHalf = this.skeleton.proportions.hipWidth * 0.5;
4023
- const groundY = 0;
4024
- const halfStride = this.strideLength * 0.5 * speedRatio;
4025
- let leftX = -hipWidthHalf;
4026
- let leftY = groundY;
4027
- let leftZ = 0;
4689
+ this.skeleton.applyModelDeltaRotation(chestName, ScratchMath.q0, activeAmplitude);
4690
+ }
4691
+ const halfStride = this.strideLength * 0.5 * Math.min(speedRatio, 1.45);
4692
+ const liftHeight = this.stepHeight * Math.min(speedRatio, 1.5);
4693
+ const baseLeftX = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.x : -this.skeleton.proportions.hipWidth * 0.5;
4694
+ const baseLeftY = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.y : 0;
4695
+ const baseLeftZ = this.skeleton.hasRestFootPositions ? this.skeleton.restLeftFootModelPos.z : 0;
4696
+ const baseRightX = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.x : this.skeleton.proportions.hipWidth * 0.5;
4697
+ const baseRightY = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.y : 0;
4698
+ const baseRightZ = this.skeleton.hasRestFootPositions ? this.skeleton.restRightFootModelPos.z : 0;
4699
+ let leftY = baseLeftY;
4700
+ let leftZ = baseLeftZ;
4028
4701
  if (phi < 0.5) {
4029
4702
  const s = phi / 0.5;
4030
- leftZ = halfStride * (1 - 2 * s);
4031
- leftY = groundY;
4703
+ leftZ = baseLeftZ + halfStride * (1 - 2 * s);
4032
4704
  } else {
4033
4705
  const s = (phi - 0.5) / 0.5;
4034
- leftZ = halfStride * (2 * s - 1);
4035
- leftY = groundY + this.stepHeight * speedRatio * (4 * s * (1 - s));
4706
+ leftZ = baseLeftZ + halfStride * (2 * s - 1);
4707
+ leftY = baseLeftY + liftHeight * (4 * s * (1 - s));
4036
4708
  }
4037
- let rightX = hipWidthHalf;
4038
- let rightY = groundY;
4039
- let rightZ = 0;
4709
+ let rightY = baseRightY;
4710
+ let rightZ = baseRightZ;
4040
4711
  if (phi < 0.5) {
4041
4712
  const s = phi / 0.5;
4042
- rightZ = halfStride * (2 * s - 1);
4043
- rightY = groundY + this.stepHeight * speedRatio * (4 * s * (1 - s));
4713
+ rightZ = baseRightZ + halfStride * (2 * s - 1);
4714
+ rightY = baseRightY + liftHeight * (4 * s * (1 - s));
4044
4715
  } else {
4045
4716
  const s = (phi - 0.5) / 0.5;
4046
- rightZ = halfStride * (1 - 2 * s);
4047
- rightY = groundY;
4717
+ rightZ = baseRightZ + halfStride * (1 - 2 * s);
4048
4718
  }
4049
- this.leftFootTarget.set(leftX, leftY, leftZ);
4050
- this.rightFootTarget.set(rightX, rightY, rightZ);
4719
+ this.leftFootTarget.set(baseLeftX, leftY, leftZ);
4720
+ this.rightFootTarget.set(baseRightX, rightY, rightZ);
4051
4721
  if (this.ik) {
4052
- this.ik.solveLeftLeg(this.leftFootTarget, weight);
4053
- this.ik.solveRightLeg(this.rightFootTarget, weight);
4054
- }
4055
- const armSwingAngle = (this.isRunning ? 0.65 : 0.38) * speedRatio * weight;
4056
- const lArmAngle = -Math.sin(twoPiPhi) * armSwingAngle;
4057
- const rArmAngle = Math.sin(twoPiPhi) * armSwingAngle;
4058
- const lArm = this.skeleton.getJoint("leftUpperArm");
4059
- const rArm = this.skeleton.getJoint("rightUpperArm");
4060
- const lForeArm = this.skeleton.getJoint("leftLowerArm");
4061
- const rForeArm = this.skeleton.getJoint("rightLowerArm");
4062
- if (lArm) {
4063
- ScratchMath.e0.set(lArmAngle, 0, 0.05, "YXZ");
4064
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4065
- lArm.targetQuaternion.copy(lArm.restQuaternion).multiply(ScratchMath.q0);
4066
- }
4067
- if (rArm) {
4068
- ScratchMath.e0.set(rArmAngle, 0, -0.05, "YXZ");
4069
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4070
- rArm.targetQuaternion.copy(rArm.restQuaternion).multiply(ScratchMath.q0);
4071
- }
4072
- const elbowFlexBase = this.isRunning ? 0.55 : 0.25;
4073
- if (lForeArm) {
4074
- const flex = elbowFlexBase + Math.max(0, -Math.sin(twoPiPhi)) * 0.35;
4075
- ScratchMath.e0.set(flex * weight, 0, 0, "YXZ");
4076
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4077
- lForeArm.targetQuaternion.copy(lForeArm.restQuaternion).multiply(ScratchMath.q0);
4078
- }
4079
- if (rForeArm) {
4080
- const flex = elbowFlexBase + Math.max(0, Math.sin(twoPiPhi)) * 0.35;
4081
- ScratchMath.e0.set(flex * weight, 0, 0, "YXZ");
4082
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4083
- rForeArm.targetQuaternion.copy(rForeArm.restQuaternion).multiply(ScratchMath.q0);
4722
+ if (hips) {
4723
+ hips.bone.position.copy(hips.targetPosition);
4724
+ hips.bone.quaternion.copy(hips.targetQuaternion);
4725
+ hips.bone.updateMatrixWorld(true);
4726
+ }
4727
+ this.ik.solveLeftLeg(this.leftFootTarget, activeAmplitude);
4728
+ this.ik.solveRightLeg(this.rightFootTarget, activeAmplitude);
4729
+ }
4730
+ const targetStandingArmAngle = 0.25;
4731
+ const armSwingAngle = (this.isRunning ? 0.72 : 0.42) * Math.min(speedRatio, 1.5);
4732
+ const lArmForward = -Math.sin(twoPiPhi) * armSwingAngle;
4733
+ const rArmForward = Math.sin(twoPiPhi) * armSwingAngle;
4734
+ if (this.skeleton.hasJoint("leftUpperArm")) {
4735
+ const relaxAngle = Math.max(0, this.skeleton.leftArmRestAngleFromDown - targetStandingArmAngle);
4736
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, this.skeleton.leftArmLowerSign * relaxAngle);
4737
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -lArmForward);
4738
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4739
+ this.skeleton.applyModelDeltaRotation("leftUpperArm", ScratchMath.q0, activeAmplitude);
4740
+ }
4741
+ if (this.skeleton.hasJoint("rightUpperArm")) {
4742
+ const relaxAngle = Math.max(0, this.skeleton.rightArmRestAngleFromDown - targetStandingArmAngle);
4743
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, this.skeleton.rightArmLowerSign * relaxAngle);
4744
+ ScratchMath.q2.setFromAxisAngle(ScratchMath.RIGHT, -rArmForward);
4745
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
4746
+ this.skeleton.applyModelDeltaRotation("rightUpperArm", ScratchMath.q0, activeAmplitude);
4747
+ }
4748
+ const elbowFlexBase = this.isRunning ? 0.85 : 0.28;
4749
+ if (this.skeleton.hasJoint("leftLowerArm")) {
4750
+ const flex = elbowFlexBase + Math.max(0, lArmForward) * 0.45;
4751
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -flex);
4752
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, activeAmplitude);
4753
+ }
4754
+ if (this.skeleton.hasJoint("rightLowerArm")) {
4755
+ const flex = elbowFlexBase + Math.max(0, rArmForward) * 0.45;
4756
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -flex);
4757
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, activeAmplitude);
4084
4758
  }
4085
4759
  }
4086
4760
  getPhase() {
@@ -4093,18 +4767,26 @@ var LocomotionEngine = class {
4093
4767
 
4094
4768
  // src/avatar/procedural/TurnEngine.ts
4095
4769
  var TurnEngine = class {
4096
- constructor(skeleton) {
4770
+ constructor(skeleton, turnTarget) {
4097
4771
  this.skeleton = skeleton;
4098
4772
  this.currentYaw = 0;
4099
- // Current character yaw in radians
4773
+ // Current character yaw offset in radians
4100
4774
  this.targetYaw = 0;
4101
- // Target desired yaw in radians
4775
+ // Target desired yaw offset in radians
4102
4776
  this.isTurning = false;
4103
4777
  this.turnAngularVelocity = 0;
4778
+ this.turnTarget = turnTarget ?? skeleton.modelRoot;
4779
+ this.baseYaw = this.turnTarget.rotation.y;
4104
4780
  }
4105
4781
  setTargetHeading(targetYawRad) {
4106
4782
  this.targetYaw = targetYawRad;
4107
4783
  }
4784
+ turnLeft(stepRad = Math.PI / 4) {
4785
+ this.targetYaw -= Math.abs(stepRad);
4786
+ }
4787
+ turnRight(stepRad = Math.PI / 4) {
4788
+ this.targetYaw += Math.abs(stepRad);
4789
+ }
4108
4790
  setFacingDirection(direction) {
4109
4791
  switch (direction) {
4110
4792
  case "left":
@@ -4115,32 +4797,45 @@ var TurnEngine = class {
4115
4797
  break;
4116
4798
  case "center":
4117
4799
  case "forward":
4800
+ case "front":
4118
4801
  default:
4119
4802
  this.targetYaw = 0;
4120
4803
  break;
4121
4804
  }
4122
4805
  }
4806
+ reset() {
4807
+ this.currentYaw = 0;
4808
+ this.targetYaw = 0;
4809
+ this.isTurning = false;
4810
+ this.turnAngularVelocity = 0;
4811
+ this.turnTarget.rotation.y = this.baseYaw;
4812
+ }
4123
4813
  update(delta, isLocomoting = false) {
4124
4814
  const diff = ((this.targetYaw - this.currentYaw) % (Math.PI * 2) + Math.PI * 3) % (Math.PI * 2) - Math.PI;
4125
4815
  this.isTurning = Math.abs(diff) > 0.02;
4126
- const damping = isLocomoting ? 6.5 : 4;
4816
+ const damping = isLocomoting ? 6.5 : 5;
4127
4817
  const prevYaw = this.currentYaw;
4128
4818
  this.currentYaw = ScratchMath.dampAngle(this.currentYaw, this.targetYaw, damping, delta);
4129
4819
  this.turnAngularVelocity = (this.currentYaw - prevYaw) / Math.max(delta, 1e-3);
4130
- this.skeleton.modelRoot.rotation.y = this.currentYaw;
4131
- if (isLocomoting && Math.abs(this.turnAngularVelocity) > 0.2) {
4132
- const bankRoll = -Math.max(-0.06, Math.min(0.06, this.turnAngularVelocity * 0.04));
4133
- const hips = this.skeleton.getJoint("hips");
4134
- if (hips) {
4820
+ this.turnTarget.rotation.y = this.baseYaw + this.currentYaw;
4821
+ if (Math.abs(this.turnAngularVelocity) > 0.15) {
4822
+ const bankRoll = -Math.max(-0.06, Math.min(0.06, this.turnAngularVelocity * (isLocomoting ? 0.04 : 0.02)));
4823
+ if (this.skeleton.hasJoint("hips")) {
4135
4824
  ScratchMath.e0.set(0, 0, bankRoll, "YXZ");
4136
4825
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4137
- hips.targetQuaternion.multiply(ScratchMath.q0);
4826
+ this.skeleton.applyModelDeltaRotation("hips", ScratchMath.q0, 1, true);
4138
4827
  }
4139
4828
  }
4140
4829
  }
4141
4830
  getYaw() {
4142
4831
  return this.currentYaw;
4143
4832
  }
4833
+ getTargetYaw() {
4834
+ return this.targetYaw;
4835
+ }
4836
+ getAngularVelocity() {
4837
+ return this.turnAngularVelocity;
4838
+ }
4144
4839
  isTurnActive() {
4145
4840
  return this.isTurning;
4146
4841
  }
@@ -4156,23 +4851,24 @@ var BalanceEngine = class {
4156
4851
  */
4157
4852
  applyCompensation(reachOffset, weight = 1) {
4158
4853
  if (weight <= 1e-3) return;
4854
+ const localScale = this.skeleton.hipsLocalPerMeter;
4159
4855
  this.targetOffset.set(
4160
- -reachOffset.x * 0.22,
4161
- -Math.abs(reachOffset.z) * 0.05,
4162
- -reachOffset.z * 0.25
4856
+ -reachOffset.x * 0.18 * localScale,
4857
+ -Math.abs(reachOffset.z) * 0.03 * localScale,
4858
+ -reachOffset.z * 0.18 * localScale
4163
4859
  );
4164
4860
  this.centerOfMassOffset.lerp(this.targetOffset, Math.min(1, weight));
4165
4861
  const hips = this.skeleton.getJoint("hips");
4166
4862
  if (hips) {
4167
4863
  hips.targetPosition.add(this.centerOfMassOffset);
4168
4864
  }
4169
- const spine = this.skeleton.getJoint("spine");
4170
- if (spine && this.centerOfMassOffset.lengthSq() > 1e-4) {
4171
- const counterPitch = this.centerOfMassOffset.z * 0.35;
4172
- const counterRoll = -this.centerOfMassOffset.x * 0.35;
4865
+ if (this.skeleton.hasJoint("spine") && this.centerOfMassOffset.lengthSq() > 1e-6) {
4866
+ const invScale = localScale > 1e-4 ? 1 / localScale : 1;
4867
+ const counterPitch = Math.max(-0.12, Math.min(0.12, this.centerOfMassOffset.z * invScale * 0.35));
4868
+ const counterRoll = Math.max(-0.12, Math.min(0.12, -this.centerOfMassOffset.x * invScale * 0.35));
4173
4869
  ScratchMath.e0.set(counterPitch, 0, counterRoll, "YXZ");
4174
4870
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4175
- spine.targetQuaternion.multiply(ScratchMath.q0);
4871
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, 1, true);
4176
4872
  }
4177
4873
  }
4178
4874
  reset() {
@@ -4392,177 +5088,146 @@ var GestureEngine = class {
4392
5088
  return "right";
4393
5089
  }
4394
5090
  }
5091
+ /**
5092
+ * Helper: rotates an upper arm from its rest orientation to a target standing-relative Euler pose in model space.
5093
+ */
5094
+ applyStandingArmDelta(side, pitchForwardRad, yawRad, abductOutRad, weight) {
5095
+ const boneName = side === "left" ? "leftUpperArm" : "rightUpperArm";
5096
+ if (!this.skeleton.hasJoint(boneName)) return;
5097
+ const restAngle = side === "left" ? this.skeleton.leftArmRestAngleFromDown : this.skeleton.rightArmRestAngleFromDown;
5098
+ const lowerSign = side === "left" ? this.skeleton.leftArmLowerSign : this.skeleton.rightArmLowerSign;
5099
+ const relaxToDown = Math.max(0, restAngle - abductOutRad);
5100
+ ScratchMath.q1.setFromAxisAngle(ScratchMath.FORWARD, lowerSign * relaxToDown);
5101
+ ScratchMath.e0.set(-pitchForwardRad, yawRad, 0, "YXZ");
5102
+ ScratchMath.q2.setFromEuler(ScratchMath.e0);
5103
+ ScratchMath.q0.copy(ScratchMath.q2).multiply(ScratchMath.q1);
5104
+ this.skeleton.applyModelDeltaRotation(boneName, ScratchMath.q0, weight);
5105
+ }
4395
5106
  // 1. Wave (right hand oscillates back and forth at shoulder height)
4396
5107
  synthesizeWave(progress, weight) {
4397
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4398
- const rLower = this.skeleton.getJoint("rightLowerArm");
4399
- const rHand = this.skeleton.getJoint("rightHand");
4400
- if (rUpper && rLower) {
4401
- ScratchMath.e0.set(-0.6, 0.4, -1.35 * weight, "YXZ");
5108
+ this.applyStandingArmDelta("right", 0.35, -0.25, 1.35, weight);
5109
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5110
+ const waveOsc = Math.sin(progress * Math.PI * 8) * 0.35;
5111
+ ScratchMath.e0.set(-1.35, 0, waveOsc, "YXZ");
4402
5112
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4403
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4404
- ScratchMath.e0.set(1.4 * weight, 0, 0, "YXZ");
5113
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
5114
+ }
5115
+ if (this.skeleton.hasJoint("rightHand")) {
5116
+ const handWave = Math.sin(progress * Math.PI * 8) * 0.3;
5117
+ ScratchMath.e0.set(0, 0, handWave, "YXZ");
4405
5118
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4406
- rLower.targetQuaternion.slerp(ScratchMath.q0, weight);
4407
- if (rHand) {
4408
- const waveAngle = Math.sin(progress * Math.PI * 8) * 0.45 * weight;
4409
- ScratchMath.e0.set(0, 0, waveAngle, "YXZ");
4410
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4411
- rHand.targetQuaternion.slerp(ScratchMath.q0, weight);
4412
- }
5119
+ this.skeleton.applyModelDeltaRotation("rightHand", ScratchMath.q0, weight);
4413
5120
  }
4414
5121
  }
4415
5122
  // 2. Pointing (arm extends toward target using IK or forward angle)
4416
5123
  synthesizePoint(_progress, weight, targetPos) {
4417
5124
  const isLeft = this.activeGesture?.type === "point_left";
4418
- const armName = isLeft ? "leftUpperArm" : "rightUpperArm";
4419
- const foreName = isLeft ? "leftLowerArm" : "rightLowerArm";
5125
+ const side = isLeft ? "left" : "right";
4420
5126
  if (targetPos && this.ik) {
4421
- this.ik.solveArmReach(isLeft ? "left" : "right", targetPos, weight);
5127
+ this.ik.solveArmReach(side, targetPos, weight);
4422
5128
  } else {
4423
- const upper = this.skeleton.getJoint(armName);
4424
- const fore = this.skeleton.getJoint(foreName);
4425
5129
  const sign = isLeft ? 1 : -1;
4426
- if (upper && fore) {
4427
- ScratchMath.e0.set(-1.45 * weight, sign * 0.25 * weight, sign * 0.15 * weight, "YXZ");
4428
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4429
- upper.targetQuaternion.slerp(ScratchMath.q0, weight);
4430
- ScratchMath.e0.set(0.15 * weight, 0, 0, "YXZ");
4431
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4432
- fore.targetQuaternion.slerp(ScratchMath.q0, weight);
5130
+ this.applyStandingArmDelta(side, 1.35, sign * 0.2, 0.25, weight);
5131
+ const foreName = isLeft ? "leftLowerArm" : "rightLowerArm";
5132
+ if (this.skeleton.hasJoint(foreName)) {
5133
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.15);
5134
+ this.skeleton.applyModelDeltaRotation(foreName, ScratchMath.q0, weight);
4433
5135
  }
4434
5136
  }
4435
5137
  }
4436
5138
  // 3. Agree (nod head twice)
4437
5139
  synthesizeAgree(progress, weight) {
4438
- const head = this.skeleton.getJoint("head");
4439
- if (head) {
5140
+ if (this.skeleton.hasJoint("head")) {
4440
5141
  const nodPitch = Math.sin(progress * Math.PI * 4) * 0.24 * weight;
4441
5142
  ScratchMath.e0.set(nodPitch, 0, 0, "YXZ");
4442
5143
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4443
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5144
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4444
5145
  }
4445
5146
  }
4446
5147
  // 4. Disagree (shake head side to side)
4447
5148
  synthesizeDisagree(progress, weight) {
4448
- const head = this.skeleton.getJoint("head");
4449
- if (head) {
5149
+ if (this.skeleton.hasJoint("head")) {
4450
5150
  const shakeYaw = Math.sin(progress * Math.PI * 4) * 0.32 * weight;
4451
5151
  ScratchMath.e0.set(0, shakeYaw, 0, "YXZ");
4452
5152
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4453
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5153
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4454
5154
  }
4455
5155
  }
4456
5156
  // 5. Shrug (raise shoulders, turn palms up, tilt head)
4457
5157
  synthesizeShrug(_progress, weight) {
4458
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4459
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4460
- const lFore = this.skeleton.getJoint("leftLowerArm");
4461
- const rFore = this.skeleton.getJoint("rightLowerArm");
4462
- const head = this.skeleton.getJoint("head");
4463
- const lift = 0.28 * weight;
4464
- if (lUpper && rUpper) {
4465
- ScratchMath.e0.set(-0.25 * weight, 0, lift, "YXZ");
4466
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4467
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4468
- ScratchMath.e0.set(-0.25 * weight, 0, -lift, "YXZ");
5158
+ this.applyStandingArmDelta("left", 0.2, 0, 0.48, weight);
5159
+ this.applyStandingArmDelta("right", 0.2, 0, 0.48, weight);
5160
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5161
+ ScratchMath.e0.set(-0.95, 0.35, 0, "YXZ");
4469
5162
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4470
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5163
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4471
5164
  }
4472
- if (lFore && rFore) {
4473
- ScratchMath.e0.set(0.85 * weight, 0.4 * weight, 0, "YXZ");
5165
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5166
+ ScratchMath.e0.set(-0.95, -0.35, 0, "YXZ");
4474
5167
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4475
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4476
- ScratchMath.e0.set(0.85 * weight, -0.4 * weight, 0, "YXZ");
4477
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4478
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5168
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4479
5169
  }
4480
- if (head) {
5170
+ if (this.skeleton.hasJoint("head")) {
4481
5171
  ScratchMath.e0.set(0.08 * weight, 0, 0.12 * weight, "YXZ");
4482
5172
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4483
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5173
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4484
5174
  }
4485
5175
  }
4486
5176
  // 6. Think (hand to chin / temple, look upward slightly)
4487
5177
  synthesizeThink(_progress, weight) {
4488
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4489
- const rFore = this.skeleton.getJoint("rightLowerArm");
4490
- const head = this.skeleton.getJoint("head");
4491
- if (rUpper && rFore) {
4492
- ScratchMath.e0.set(-1.1 * weight, -0.4 * weight, -0.5 * weight, "YXZ");
4493
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4494
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4495
- ScratchMath.e0.set(1.6 * weight, 0, 0, "YXZ");
5178
+ this.applyStandingArmDelta("right", 0.95, 0.35, 0.35, weight);
5179
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5180
+ ScratchMath.e0.set(-1.55, 0, 0, "YXZ");
4496
5181
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4497
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5182
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4498
5183
  }
4499
- if (head) {
5184
+ if (this.skeleton.hasJoint("head")) {
4500
5185
  ScratchMath.e0.set(-0.15 * weight, 0.18 * weight, 0.08 * weight, "YXZ");
4501
5186
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4502
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5187
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4503
5188
  }
4504
5189
  }
4505
5190
  // 7. Explain / Conversational gestures (gentle rhythmic hand gesturing)
4506
5191
  synthesizeExplain(progress, weight) {
4507
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4508
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4509
- const lFore = this.skeleton.getJoint("leftLowerArm");
4510
- const rFore = this.skeleton.getJoint("rightLowerArm");
4511
- const osc = Math.sin(progress * Math.PI * 3) * 0.15 * weight;
4512
- if (lUpper && rUpper) {
4513
- ScratchMath.e0.set(-0.35 * weight + osc, 0.25 * weight, 0.2 * weight, "YXZ");
4514
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4515
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4516
- ScratchMath.e0.set(-0.35 * weight - osc, -0.25 * weight, -0.2 * weight, "YXZ");
4517
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4518
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5192
+ const osc = Math.sin(progress * Math.PI * 3) * 0.18;
5193
+ this.applyStandingArmDelta("left", 0.35 + osc, 0.2, 0.35, weight);
5194
+ this.applyStandingArmDelta("right", 0.35 - osc, -0.2, 0.35, weight);
5195
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5196
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.9);
5197
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4519
5198
  }
4520
- if (lFore && rFore) {
4521
- ScratchMath.e0.set(0.9 * weight, 0, 0, "YXZ");
4522
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4523
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4524
- ScratchMath.e0.set(0.9 * weight, 0, 0, "YXZ");
4525
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4526
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5199
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5200
+ ScratchMath.q0.setFromAxisAngle(ScratchMath.RIGHT, -0.9);
5201
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4527
5202
  }
4528
5203
  }
4529
5204
  // 8. Open Hands (Welcome / Present / Invite)
4530
5205
  synthesizeOpenHands(_progress, weight) {
4531
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4532
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4533
- const lFore = this.skeleton.getJoint("leftLowerArm");
4534
- const rFore = this.skeleton.getJoint("rightLowerArm");
4535
- if (lUpper && rUpper) {
4536
- ScratchMath.e0.set(-0.45 * weight, 0.35 * weight, 0.45 * weight, "YXZ");
4537
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4538
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4539
- ScratchMath.e0.set(-0.45 * weight, -0.35 * weight, -0.45 * weight, "YXZ");
5206
+ this.applyStandingArmDelta("left", 0.45, 0.35, 0.55, weight);
5207
+ this.applyStandingArmDelta("right", 0.45, -0.35, 0.55, weight);
5208
+ if (this.skeleton.hasJoint("leftLowerArm")) {
5209
+ ScratchMath.e0.set(-0.75, 0.35, 0, "YXZ");
4540
5210
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4541
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
5211
+ this.skeleton.applyModelDeltaRotation("leftLowerArm", ScratchMath.q0, weight);
4542
5212
  }
4543
- if (lFore && rFore) {
4544
- ScratchMath.e0.set(0.75 * weight, 0.45 * weight, 0, "YXZ");
4545
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4546
- lFore.targetQuaternion.slerp(ScratchMath.q0, weight);
4547
- ScratchMath.e0.set(0.75 * weight, -0.45 * weight, 0, "YXZ");
5213
+ if (this.skeleton.hasJoint("rightLowerArm")) {
5214
+ ScratchMath.e0.set(-0.75, -0.35, 0, "YXZ");
4548
5215
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4549
- rFore.targetQuaternion.slerp(ScratchMath.q0, weight);
5216
+ this.skeleton.applyModelDeltaRotation("rightLowerArm", ScratchMath.q0, weight);
4550
5217
  }
4551
5218
  }
4552
5219
  // 9. Acknowledge (slight bow of head / chest)
4553
5220
  synthesizeAcknowledge(progress, weight) {
4554
- const spine = this.skeleton.getJoint("spine");
4555
- const head = this.skeleton.getJoint("head");
4556
5221
  const bow = Math.sin(progress * Math.PI) * 0.16 * weight;
4557
- if (spine) {
5222
+ if (this.skeleton.hasJoint("spine")) {
4558
5223
  ScratchMath.e0.set(bow, 0, 0, "YXZ");
4559
5224
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4560
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
5225
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, weight);
4561
5226
  }
4562
- if (head) {
5227
+ if (this.skeleton.hasJoint("head")) {
4563
5228
  ScratchMath.e0.set(bow * 0.5, 0, 0, "YXZ");
4564
5229
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4565
- head.targetQuaternion.copy(head.restQuaternion).multiply(ScratchMath.q0);
5230
+ this.skeleton.applyModelDeltaRotation("head", ScratchMath.q0, weight);
4566
5231
  }
4567
5232
  }
4568
5233
  // 10. Reach
@@ -4573,21 +5238,12 @@ var GestureEngine = class {
4573
5238
  }
4574
5239
  // 11. Celebrate (arms raised in victory, chest open)
4575
5240
  synthesizeCelebrate(_progress, weight) {
4576
- const lUpper = this.skeleton.getJoint("leftUpperArm");
4577
- const rUpper = this.skeleton.getJoint("rightUpperArm");
4578
- const spine = this.skeleton.getJoint("spine");
4579
- if (lUpper && rUpper) {
4580
- ScratchMath.e0.set(-1.8 * weight, 0, 1.2 * weight, "YXZ");
4581
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4582
- lUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4583
- ScratchMath.e0.set(-1.8 * weight, 0, -1.2 * weight, "YXZ");
4584
- ScratchMath.q0.setFromEuler(ScratchMath.e0);
4585
- rUpper.targetQuaternion.slerp(ScratchMath.q0, weight);
4586
- }
4587
- if (spine) {
5241
+ this.applyStandingArmDelta("left", 0.35, 0, 2.45, weight);
5242
+ this.applyStandingArmDelta("right", 0.35, 0, 2.45, weight);
5243
+ if (this.skeleton.hasJoint("spine")) {
4588
5244
  ScratchMath.e0.set(-0.12 * weight, 0, 0, "YXZ");
4589
5245
  ScratchMath.q0.setFromEuler(ScratchMath.e0);
4590
- spine.targetQuaternion.copy(spine.restQuaternion).multiply(ScratchMath.q0);
5246
+ this.skeleton.applyModelDeltaRotation("spine", ScratchMath.q0, weight);
4591
5247
  }
4592
5248
  }
4593
5249
  };
@@ -4885,13 +5541,17 @@ var MotionCoordinator = _MotionCoordinator;
4885
5541
 
4886
5542
  // src/avatar/procedural/ProceduralMovementEngine.ts
4887
5543
  var ProceduralMovementEngine = class {
4888
- constructor(modelRoot) {
5544
+ constructor(modelRoot, options) {
4889
5545
  this.isEnabled = true;
4890
- this.skeleton = new AUBRSkeleton(modelRoot);
5546
+ this.paused = false;
5547
+ this.locomotionBlendWeight = 0;
5548
+ this.startingTimer = 0;
5549
+ this.balanceReachScratch = new THREE__namespace.Vector3();
5550
+ this.skeleton = new AUBRSkeleton(modelRoot, options);
4891
5551
  this.ik = new IKCoordinator(this.skeleton);
4892
5552
  this.idle = new IdleEngine(this.skeleton);
4893
5553
  this.locomotion = new LocomotionEngine(this.skeleton, this.ik);
4894
- this.turn = new TurnEngine(this.skeleton);
5554
+ this.turn = new TurnEngine(this.skeleton, options?.turnTarget);
4895
5555
  this.balance = new BalanceEngine(this.skeleton);
4896
5556
  this.lookAtEngine = new LookAtEngine(this.skeleton);
4897
5557
  this.gesture = new GestureEngine(this.skeleton, this.ik);
@@ -4902,42 +5562,110 @@ var ProceduralMovementEngine = class {
4902
5562
  getCapabilities() {
4903
5563
  return this.skeleton.capabilities;
4904
5564
  }
5565
+ getState() {
5566
+ return this.stateMachine.getState();
5567
+ }
4905
5568
  setEnabled(enabled) {
4906
5569
  this.isEnabled = enabled;
4907
5570
  }
5571
+ pause() {
5572
+ this.paused = true;
5573
+ }
5574
+ resume() {
5575
+ this.paused = false;
5576
+ }
5577
+ isPaused() {
5578
+ return this.paused;
5579
+ }
5580
+ resetToRestPose() {
5581
+ this.startingTimer = 0;
5582
+ this.locomotionBlendWeight = 0;
5583
+ this.locomotion.cancel();
5584
+ this.gesture.stopGesture();
5585
+ this.idle.reset();
5586
+ this.turn.reset();
5587
+ this.balance.reset();
5588
+ this.lookAtEngine.clearTarget();
5589
+ this.facial.setSpeaking(false);
5590
+ this.facial.setExpression("neutral");
5591
+ this.stateMachine.transitionTo("idle", 0.05);
5592
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1, gestureWeight: 0 });
5593
+ this.skeleton.resetToRestPose();
5594
+ this.skeleton.updateWorldMatrices();
5595
+ }
4908
5596
  // --- High-Level Movement API ---
5597
+ startIdle() {
5598
+ const current = this.stateMachine.getState();
5599
+ if (current === "walking" || current === "running" || current === "starting") {
5600
+ this.stopLocomotion();
5601
+ return;
5602
+ }
5603
+ this.startingTimer = 0;
5604
+ this.gesture.stopGesture();
5605
+ this.stateMachine.transitionTo("idle", 0.2);
5606
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1, gestureWeight: 0 });
5607
+ }
5608
+ startWalking(speed = 1, screenVelocityPx) {
5609
+ if (!this.skeleton.capabilities.proceduralLocomotion) return;
5610
+ this.startingTimer = 0.18;
5611
+ this.stateMachine.transitionTo("starting", 0.18);
5612
+ this.locomotion.setMotion({ speed, isRunning: false, screenVelocityPx });
5613
+ }
4909
5614
  walk(speed = 1, screenVelocityPx) {
4910
- this.clearStopTimer();
4911
- this.stateMachine.transitionTo("walking");
5615
+ if (!this.skeleton.capabilities.proceduralLocomotion) return;
5616
+ this.startingTimer = 0;
5617
+ this.stateMachine.transitionTo("walking", 0.2);
4912
5618
  this.locomotion.setMotion({ speed, isRunning: false, screenVelocityPx });
4913
- this.coordinator.setWeights({ locomotionWeight: 1, idleWeight: 0 });
4914
5619
  }
4915
5620
  run(speed = 1.8, screenVelocityPx) {
4916
- this.clearStopTimer();
4917
- this.stateMachine.transitionTo("running");
5621
+ if (!this.skeleton.capabilities.proceduralRunning) return;
5622
+ this.startingTimer = 0;
5623
+ this.stateMachine.transitionTo("running", 0.2);
4918
5624
  this.locomotion.setMotion({ speed, isRunning: true, screenVelocityPx });
4919
- this.coordinator.setWeights({ locomotionWeight: 1, idleWeight: 0 });
5625
+ }
5626
+ stop() {
5627
+ this.stopLocomotion();
4920
5628
  }
4921
5629
  stopLocomotion() {
4922
- this.clearStopTimer();
5630
+ this.startingTimer = 0;
4923
5631
  this.locomotion.stop();
4924
- this.stateMachine.transitionTo("stopping", 0.2);
4925
- this.stopTimer = setTimeout(() => {
4926
- this.stopTimer = void 0;
4927
- if (this.stateMachine.getState() === "stopping") {
4928
- this.stateMachine.transitionTo("idle", 0.25);
4929
- this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1 });
4930
- }
4931
- }, 200);
4932
- }
4933
- dispose() {
4934
- this.clearStopTimer();
4935
- this.isEnabled = false;
4936
- }
4937
- clearStopTimer() {
4938
- if (this.stopTimer) {
4939
- clearTimeout(this.stopTimer);
4940
- this.stopTimer = void 0;
5632
+ const state = this.stateMachine.getState();
5633
+ if (state === "walking" || state === "running" || state === "starting") {
5634
+ this.stateMachine.transitionTo("stopping", 0.22);
5635
+ } else if (state !== "stopping") {
5636
+ this.stateMachine.transitionTo("idle", 0.2);
5637
+ }
5638
+ }
5639
+ cancelLocomotion() {
5640
+ this.startingTimer = 0;
5641
+ this.locomotionBlendWeight = 0;
5642
+ this.locomotion.cancel();
5643
+ this.coordinator.setWeights({ locomotionWeight: 0, idleWeight: 1 });
5644
+ this.stateMachine.transitionTo("idle", 0.05);
5645
+ }
5646
+ turnLeft(stepRad = Math.PI / 4) {
5647
+ if (!this.skeleton.capabilities.turning) return;
5648
+ this.turn.turnLeft(stepRad);
5649
+ if (this.stateMachine.getState() === "idle") {
5650
+ this.stateMachine.transitionTo("turning", 0.15);
5651
+ }
5652
+ }
5653
+ turnRight(stepRad = Math.PI / 4) {
5654
+ if (!this.skeleton.capabilities.turning) return;
5655
+ this.turn.turnRight(stepRad);
5656
+ if (this.stateMachine.getState() === "idle") {
5657
+ this.stateMachine.transitionTo("turning", 0.15);
5658
+ }
5659
+ }
5660
+ turnTo(directionOrYaw) {
5661
+ if (!this.skeleton.capabilities.turning) return;
5662
+ if (typeof directionOrYaw === "number") {
5663
+ this.turn.setTargetHeading(directionOrYaw);
5664
+ } else {
5665
+ this.turn.setFacingDirection(directionOrYaw);
5666
+ }
5667
+ if (this.stateMachine.getState() === "idle") {
5668
+ this.stateMachine.transitionTo("turning", 0.15);
4941
5669
  }
4942
5670
  }
4943
5671
  setFacing(direction) {
@@ -4946,10 +5674,26 @@ var ProceduralMovementEngine = class {
4946
5674
  setTargetHeading(yawRad) {
4947
5675
  this.turn.setTargetHeading(yawRad);
4948
5676
  }
5677
+ dispose() {
5678
+ this.isEnabled = false;
5679
+ this.paused = false;
5680
+ this.startingTimer = 0;
5681
+ this.locomotionBlendWeight = 0;
5682
+ this.locomotion.cancel();
5683
+ this.gesture.stopGesture();
5684
+ this.balance.reset();
5685
+ this.lookAtEngine.clearTarget();
5686
+ }
4949
5687
  playGesture(type, options) {
5688
+ if (!this.skeleton.capabilities.upperBodyGestures && !this.skeleton.capabilities.headLookAt) {
5689
+ return false;
5690
+ }
4950
5691
  const success = this.gesture.playGesture(type, options);
4951
5692
  if (success) {
4952
- this.stateMachine.transitionTo("gesturing");
5693
+ const current = this.stateMachine.getState();
5694
+ if (current === "idle" || current === "speaking" || current === "listening" || current === "thinking") {
5695
+ this.stateMachine.transitionTo("gesturing");
5696
+ }
4953
5697
  this.coordinator.setWeights({ gestureWeight: 1 });
4954
5698
  }
4955
5699
  return success;
@@ -4987,41 +5731,83 @@ var ProceduralMovementEngine = class {
4987
5731
  * Restores bones to reference bind pose to prevent cumulative transform drift.
4988
5732
  */
4989
5733
  preMixerUpdate() {
4990
- if (!this.isEnabled) return;
5734
+ if (!this.isEnabled || this.paused) return;
4991
5735
  this.coordinator.preMixerRestore();
4992
5736
  }
4993
5737
  /**
4994
5738
  * Main procedural pipeline tick.
4995
- * Invoked in FbxRenderer.startLoop() after mixer.update(delta) and before renderer.render().
5739
+ * Invoked in FbxRenderer.startLoop() or Avatar Studio after mixer.update(delta) and before renderer.render().
4996
5740
  */
4997
- update(delta, hasActiveMixerClip) {
4998
- if (!this.isEnabled) return;
5741
+ update(delta, hasActiveMixerClip = false) {
5742
+ if (!this.isEnabled || this.paused) return;
5743
+ const dt = Math.min(Math.max(delta, 0), 0.1);
4999
5744
  if (hasActiveMixerClip) {
5000
5745
  this.coordinator.snapshotMixerOutput();
5001
5746
  this.coordinator.setWeights({ clipWeight: 1 });
5002
5747
  } else {
5003
5748
  this.coordinator.setWeights({ clipWeight: 0 });
5004
5749
  }
5005
- const currentState = this.stateMachine.getState();
5750
+ this.stateMachine.update(dt);
5751
+ let currentState = this.stateMachine.getState();
5752
+ if (currentState === "starting") {
5753
+ this.startingTimer -= dt;
5754
+ if (this.startingTimer <= 0) {
5755
+ this.stateMachine.transitionTo("walking", 0.2);
5756
+ currentState = this.stateMachine.getState();
5757
+ }
5758
+ }
5006
5759
  const isLocomoting = currentState === "walking" || currentState === "running" || currentState === "starting";
5007
- this.stateMachine.update(delta);
5008
- this.turn.update(delta, isLocomoting);
5009
- const locoWeight = isLocomoting ? 1 : this.coordinator.getWeights().locomotionWeight;
5010
- if (locoWeight > 0.01) {
5011
- this.locomotion.update(delta, locoWeight);
5760
+ const targetLocoWeight = isLocomoting ? 1 : 0;
5761
+ const blendRate = isLocomoting ? 7.5 : 8.5;
5762
+ this.locomotionBlendWeight = ScratchMath.damp(
5763
+ this.locomotionBlendWeight,
5764
+ targetLocoWeight,
5765
+ blendRate,
5766
+ dt
5767
+ );
5768
+ if (this.locomotionBlendWeight < 5e-3 && !isLocomoting) {
5769
+ this.locomotionBlendWeight = 0;
5770
+ }
5771
+ if (currentState === "stopping" && this.locomotionBlendWeight < 0.02 && this.locomotion.getSpeed() < 0.05) {
5772
+ this.locomotionBlendWeight = 0;
5773
+ this.stateMachine.transitionTo("idle", 0.15);
5774
+ currentState = "idle";
5775
+ }
5776
+ const locoWeight = this.locomotionBlendWeight;
5777
+ const idleWeight = Math.max(0, 1 - locoWeight);
5778
+ this.coordinator.setWeights({
5779
+ locomotionWeight: locoWeight,
5780
+ idleWeight
5781
+ });
5782
+ if (idleWeight > 5e-3 && this.skeleton.capabilities.proceduralIdle) {
5783
+ this.idle.update(dt, idleWeight);
5012
5784
  }
5013
- const idleWeight = !isLocomoting ? 1 : this.coordinator.getWeights().idleWeight;
5014
- if (idleWeight > 0.01) {
5015
- this.idle.update(delta, idleWeight);
5785
+ if (locoWeight > 5e-3 && this.skeleton.capabilities.proceduralLocomotion) {
5786
+ this.locomotion.update(dt, locoWeight);
5787
+ }
5788
+ if (this.skeleton.capabilities.turning) {
5789
+ this.turn.update(dt, isLocomoting);
5790
+ if (currentState === "turning" && !this.turn.isTurnActive()) {
5791
+ this.stateMachine.transitionTo("idle", 0.15);
5792
+ }
5016
5793
  }
5017
5794
  if (this.gesture.isGestureActive()) {
5018
- this.gesture.update(delta);
5795
+ this.gesture.update(dt);
5019
5796
  this.coordinator.setWeights({ gestureWeight: 1 });
5797
+ this.balanceReachScratch.set(0, 0, 0.15);
5798
+ this.balance.applyCompensation(this.balanceReachScratch, Math.min(1, dt * 6));
5020
5799
  } else {
5021
5800
  this.coordinator.setWeights({ gestureWeight: 0 });
5801
+ if (this.stateMachine.getState() === "gesturing") {
5802
+ this.stateMachine.transitionTo("idle", 0.2);
5803
+ }
5804
+ this.balanceReachScratch.set(0, 0, 0);
5805
+ this.balance.applyCompensation(this.balanceReachScratch, Math.min(1, dt * 6));
5022
5806
  }
5023
- this.lookAtEngine.update(delta, 1);
5024
- this.facial.update(delta);
5807
+ if (this.skeleton.capabilities.headLookAt) {
5808
+ this.lookAtEngine.update(dt, 1);
5809
+ }
5810
+ this.facial.update(dt);
5025
5811
  this.coordinator.blendLayers(hasActiveMixerClip);
5026
5812
  this.skeleton.apply();
5027
5813
  this.skeleton.updateWorldMatrices();
@@ -5059,16 +5845,21 @@ var FbxRenderer = class {
5059
5845
  if (!doc) {
5060
5846
  throw new Error("Unable to access document DOM for 3D avatar viewport");
5061
5847
  }
5848
+ const probeCanvas = doc.createElement("canvas");
5849
+ const glContext = probeCanvas.getContext("webgl2") || probeCanvas.getContext("webgl");
5850
+ if (!glContext) {
5851
+ throw new AsiystError({
5852
+ code: "WEBGL_UNAVAILABLE",
5853
+ message: "WebGLRenderingContext is not available or supported in this browser environment",
5854
+ hint: "Enable hardware acceleration in your browser or use a WebGL-capable device."
5855
+ });
5856
+ }
5062
5857
  this.canvas = doc.createElement("canvas");
5063
5858
  this.canvas.className = "asiyst-avatar-canvas";
5064
5859
  this.canvas.setAttribute("role", "presentation");
5065
5860
  this.canvas.style.opacity = "0";
5066
5861
  this.canvas.style.transition = "opacity 0.25s ease";
5067
5862
  this.canvas.style.background = "transparent";
5068
- const glContext = this.canvas.getContext("webgl2") || this.canvas.getContext("webgl");
5069
- if (!glContext) {
5070
- throw new Error("WebGLRenderingContext is not available or supported in this browser environment");
5071
- }
5072
5863
  const isMobile = typeof window !== "undefined" && window.innerWidth <= 640;
5073
5864
  const width = options?.width || (isMobile ? 140 : 190);
5074
5865
  const height = options?.height || (isMobile ? 280 : 360);
@@ -5153,7 +5944,12 @@ var FbxRenderer = class {
5153
5944
  modelAnimations = parsedGroup.animations || [];
5154
5945
  } catch (parseErr) {
5155
5946
  const msg = parseErr instanceof Error ? parseErr.message : String(parseErr);
5156
- throw new Error(`FBX decoding and parsing failed: ${msg}`);
5947
+ throw new AsiystError({
5948
+ code: "INCOMPATIBLE_FBX",
5949
+ message: `FBX decoding and parsing failed: ${msg}`,
5950
+ hint: "Ensure the character FBX uses a supported binary (7.0+) or ASCII format and valid humanoid mesh data.",
5951
+ cause: parseErr
5952
+ });
5157
5953
  }
5158
5954
  } else {
5159
5955
  const loader = new GLTFLoader_js.GLTFLoader();
@@ -5166,13 +5962,21 @@ var FbxRenderer = class {
5166
5962
  modelAnimations = gltf.animations || [];
5167
5963
  } catch (parseErr) {
5168
5964
  const msg = parseErr instanceof Error ? parseErr.message : String(parseErr);
5169
- throw new Error(`GLB decoding and parsing failed: ${msg}`);
5965
+ throw new AsiystError({
5966
+ code: "INVALID_ASSET_RESPONSE",
5967
+ message: `GLB decoding and parsing failed: ${msg}`,
5968
+ cause: parseErr
5969
+ });
5170
5970
  }
5171
5971
  }
5172
5972
  const { meshCount, vertexCount: totalVertices } = this.validateGeometry(rawObject);
5173
5973
  const rawBox = new THREE__namespace.Box3().setFromObject(rawObject);
5174
5974
  if (rawBox.isEmpty()) {
5175
- throw new Error("FBX asset geometry bounding box is empty");
5975
+ throw new AsiystError({
5976
+ code: "INCOMPATIBLE_FBX",
5977
+ message: "FBX asset geometry bounding box is empty",
5978
+ hint: "Export the character mesh along with the skeleton armature in your FBX file."
5979
+ });
5176
5980
  }
5177
5981
  const rawSize = rawBox.getSize(new THREE__namespace.Vector3());
5178
5982
  const rawCenter = rawBox.getCenter(new THREE__namespace.Vector3());
@@ -5208,7 +6012,10 @@ var FbxRenderer = class {
5208
6012
  });
5209
6013
  this.allAnimations = [...modelAnimations];
5210
6014
  try {
5211
- this.proceduralEngine = new ProceduralMovementEngine(rawObject);
6015
+ this.proceduralEngine = new ProceduralMovementEngine(rawObject, {
6016
+ embeddedAnimationCount: modelAnimations.length,
6017
+ turnTarget: this.modelRoot
6018
+ });
5212
6019
  } catch (procErr) {
5213
6020
  if (typeof console !== "undefined" && typeof console.warn === "function") {
5214
6021
  console.warn("[asiyst] Procedural movement engine initialization notice:", procErr);
@@ -5360,6 +6167,16 @@ var FbxRenderer = class {
5360
6167
  }
5361
6168
  async loadExternalAnimations(animations, targetAnimationName) {
5362
6169
  if (!this.rawObject || !this.mixer) return;
6170
+ for (const anim of animations) {
6171
+ const url = anim.assetUrl || anim.downloadUrl || anim.url;
6172
+ if (!url) {
6173
+ this.failedAnimations.set(anim.id || anim.name, {
6174
+ error: `Animation "${anim.name}" is missing an animation file URL`,
6175
+ code: "MISSING_ANIMATION_FILE",
6176
+ id: anim.id
6177
+ });
6178
+ }
6179
+ }
5363
6180
  const pending = animations.filter(
5364
6181
  (anim) => {
5365
6182
  const url = anim.assetUrl || anim.downloadUrl || anim.url;
@@ -5375,7 +6192,11 @@ var FbxRenderer = class {
5375
6192
  await Promise.allSettled(
5376
6193
  pending.map(async (anim) => {
5377
6194
  try {
5378
- const loaded = await this.assetLoader.load(anim.assetUrl, { integrity: anim.integrity });
6195
+ const loaded = await this.assetLoader.load(anim.assetUrl, {
6196
+ integrity: anim.integrity,
6197
+ isAnimation: true,
6198
+ assetName: anim.name
6199
+ });
5379
6200
  let extractedClips = [];
5380
6201
  if (loaded.format === "fbx") {
5381
6202
  const loader = new FBXLoader_js.FBXLoader();
@@ -5413,18 +6234,25 @@ var FbxRenderer = class {
5413
6234
  clip: retargeted
5414
6235
  });
5415
6236
  }
5416
- if (!anyExtracted && extractedClips.length > 0) {
6237
+ if (extractedClips.length === 0) {
6238
+ this.failedAnimations.set(anim.id || anim.name, {
6239
+ error: `Animation file "${anim.name}" contains no animation clips`,
6240
+ code: "INCOMPATIBLE_FBX",
6241
+ id: anim.id
6242
+ });
6243
+ } else if (!anyExtracted) {
5417
6244
  this.failedAnimations.set(anim.id || anim.name, {
5418
6245
  error: `Animation "${anim.name}" contains no compatible skeleton bone tracks`,
5419
- code: "INCOMPATIBLE_SKELETON",
6246
+ code: "INCOMPATIBLE_FBX",
5420
6247
  id: anim.id
5421
6248
  });
5422
6249
  }
5423
6250
  } catch (err) {
5424
6251
  const msg = err instanceof Error ? err.message : String(err);
6252
+ const errCode = err instanceof AsiystError ? String(err.code) : "MISSING_ANIMATION_FILE";
5425
6253
  this.failedAnimations.set(anim.id || anim.name, {
5426
6254
  error: msg,
5427
- code: "LOAD_FAILED",
6255
+ code: errCode,
5428
6256
  id: anim.id
5429
6257
  });
5430
6258
  if (typeof console !== "undefined" && typeof console.warn === "function") {
@@ -5726,7 +6554,11 @@ var FbxRenderer = class {
5726
6554
  }
5727
6555
  });
5728
6556
  if (meshCount === 0 || totalVertices === 0) {
5729
- throw new Error("FBX asset contains no drawable 3D geometry");
6557
+ throw new AsiystError({
6558
+ code: "INCOMPATIBLE_FBX",
6559
+ message: "FBX asset contains no drawable 3D geometry",
6560
+ hint: "Ensure the FBX file exports the character mesh geometry, not just animation bones."
6561
+ });
5730
6562
  }
5731
6563
  return { meshCount, vertexCount: totalVertices };
5732
6564
  }
@@ -5774,6 +6606,34 @@ var FbxRenderer = class {
5774
6606
  this.mixer = null;
5775
6607
  }
5776
6608
  if (this.scene) {
6609
+ const disposedTextures = /* @__PURE__ */ new Set();
6610
+ const TEXTURE_SLOTS = [
6611
+ "map",
6612
+ "normalMap",
6613
+ "roughnessMap",
6614
+ "metalnessMap",
6615
+ "aoMap",
6616
+ "emissiveMap",
6617
+ "alphaMap",
6618
+ "bumpMap",
6619
+ "displacementMap",
6620
+ "lightMap",
6621
+ "envMap"
6622
+ ];
6623
+ const disposeMat = (m) => {
6624
+ const matRecord = m;
6625
+ for (const slot of TEXTURE_SLOTS) {
6626
+ const texCandidate = matRecord[slot];
6627
+ if (texCandidate && typeof texCandidate === "object" && texCandidate.isTexture) {
6628
+ const tex = texCandidate;
6629
+ if (!disposedTextures.has(tex)) {
6630
+ disposedTextures.add(tex);
6631
+ tex.dispose();
6632
+ }
6633
+ }
6634
+ }
6635
+ m.dispose();
6636
+ };
5777
6637
  this.scene.traverse((obj) => {
5778
6638
  const mesh = obj;
5779
6639
  if (mesh.isMesh) {
@@ -5783,14 +6643,12 @@ var FbxRenderer = class {
5783
6643
  if (mesh.material) {
5784
6644
  if (Array.isArray(mesh.material)) {
5785
6645
  mesh.material.forEach((m) => {
5786
- const mat = m;
5787
- mat.map?.dispose();
5788
- mat.dispose();
6646
+ if (m && typeof m.dispose === "function") {
6647
+ disposeMat(m);
6648
+ }
5789
6649
  });
5790
- } else {
5791
- const mat = mesh.material;
5792
- mat.map?.dispose();
5793
- mat.dispose();
6650
+ } else if (mesh.material && typeof mesh.material.dispose === "function") {
6651
+ disposeMat(mesh.material);
5794
6652
  }
5795
6653
  }
5796
6654
  }
@@ -6423,6 +7281,7 @@ var CssAvatarRenderer = class {
6423
7281
  } catch (err) {
6424
7282
  const errorObj = err instanceof Error ? err : new Error(String(err));
6425
7283
  this.lastErrorMessage = errorObj.message;
7284
+ this.lastErrorObj = errorObj;
6426
7285
  this.renderState = "error";
6427
7286
  this.isAssetLoadedFlag = false;
6428
7287
  this.isFallbackFlag = true;
@@ -6785,6 +7644,12 @@ var CssAvatarRenderer = class {
6785
7644
  getLastError() {
6786
7645
  return this.lastErrorMessage;
6787
7646
  }
7647
+ getLastErrorObject() {
7648
+ return this.lastErrorObj;
7649
+ }
7650
+ getFailedAnimations() {
7651
+ return this.fbxRenderer?.getFailedAnimations() ?? [];
7652
+ }
6788
7653
  getMetrics() {
6789
7654
  return this.renderMetrics;
6790
7655
  }
@@ -7099,6 +7964,8 @@ var MovementEngine = class {
7099
7964
  faceDirection(direction) {
7100
7965
  if (direction === "left" || direction === "right") {
7101
7966
  this.renderer.setFacing(direction);
7967
+ } else if (typeof this.renderer.setFacing === "function") {
7968
+ this.renderer.setFacing(direction);
7102
7969
  }
7103
7970
  }
7104
7971
  viewport() {
@@ -7874,13 +8741,6 @@ var ConversationPanel = class {
7874
8741
  };
7875
8742
 
7876
8743
  // src/core/isolate.ts
7877
- async function isolateAsync(fn, fallback) {
7878
- try {
7879
- return await fn();
7880
- } catch {
7881
- return fallback;
7882
- }
7883
- }
7884
8744
  function withIsolation(fn) {
7885
8745
  try {
7886
8746
  fn();
@@ -7971,6 +8831,13 @@ var RuntimeAuth = class {
7971
8831
  let response;
7972
8832
  try {
7973
8833
  const baseUrl = this.options.apiBaseUrl.replace(/\/$/, "");
8834
+ const tokenUrl = new URL(contractApiUrl(baseUrl, API_ROUTES.runtimeToken));
8835
+ if (this.options.projectId) {
8836
+ tokenUrl.searchParams.set("projectId", this.options.projectId);
8837
+ }
8838
+ if (this.options.publicKey) {
8839
+ tokenUrl.searchParams.set("publicKey", this.options.publicKey);
8840
+ }
7974
8841
  const payload = JSON.stringify({
7975
8842
  projectId: this.options.projectId,
7976
8843
  publicKey: this.options.publicKey,
@@ -7987,11 +8854,14 @@ var RuntimeAuth = class {
7987
8854
  "assistant:events"
7988
8855
  ]
7989
8856
  });
7990
- response = await fetch(contractApiUrl(baseUrl, API_ROUTES.runtimeToken), {
8857
+ response = await fetch(tokenUrl.toString(), {
7991
8858
  method: "POST",
7992
8859
  headers: {
7993
8860
  Accept: "application/json",
7994
- "Content-Type": "application/json"
8861
+ "Content-Type": "application/json",
8862
+ "X-Asiyst-Project-Id": this.options.projectId,
8863
+ "X-Asiyst-Public-Key": this.options.publicKey,
8864
+ "X-Asiyst-Project-Key": this.options.publicKey
7995
8865
  },
7996
8866
  body: payload,
7997
8867
  signal: controller.signal
@@ -9712,12 +10582,20 @@ var ActionScheduler = class {
9712
10582
  ctx.events.emit("action_started", { actionId: action.id, actionType: action.type });
9713
10583
  try {
9714
10584
  const timeoutMs = typeof action.expirationMs === "number" && action.expirationMs > 0 ? action.expirationMs : 15e3;
9715
- const result = await Promise.race([
9716
- handler(ctx),
9717
- new Promise(
9718
- (_, reject) => setTimeout(() => reject(new Error(`Action ${action.type} timed out after ${timeoutMs}ms`)), timeoutMs)
9719
- )
9720
- ]);
10585
+ let timerId;
10586
+ const timeoutPromise = new Promise((_, reject) => {
10587
+ timerId = setTimeout(() => {
10588
+ reject(new Error(`Action ${action.type} timed out after ${timeoutMs}ms`));
10589
+ }, timeoutMs);
10590
+ });
10591
+ let result;
10592
+ try {
10593
+ result = await Promise.race([handler(ctx), timeoutPromise]);
10594
+ } finally {
10595
+ if (timerId !== void 0) {
10596
+ clearTimeout(timerId);
10597
+ }
10598
+ }
9721
10599
  const durationMs = Date.now() - startTime;
9722
10600
  const ackResult = {
9723
10601
  executionDurationMs: durationMs,
@@ -10365,7 +11243,7 @@ var AsiystRuntime = class {
10365
11243
  apiBaseUrl: options.apiBaseUrl ?? DEFAULT_API_BASE_URL,
10366
11244
  projectId: options.projectId,
10367
11245
  publicKey: options.publicKey,
10368
- installationId: void 0,
11246
+ installationId: this.installationId,
10369
11247
  origin: win.location.origin,
10370
11248
  environment: this.environment
10371
11249
  }) : void 0;
@@ -10447,36 +11325,47 @@ var AsiystRuntime = class {
10447
11325
  (event) => this.cloud.sendRuntimeEvent(event),
10448
11326
  this.events
10449
11327
  );
10450
- this.tourEngine = new TourEngine(
10451
- async (route) => {
11328
+ this.tourEngine = new TourEngine({
11329
+ navigateToRoute: async (route) => {
10452
11330
  const nav = resolveNavigationAdapter(win);
10453
11331
  await nav.navigateToRoute(route);
10454
11332
  },
10455
- async (elId) => {
11333
+ moveToElement: async (elId) => {
10456
11334
  await this.movement.moveTo(elId, "developer");
10457
11335
  },
10458
- async (elId) => {
11336
+ pointAtElement: async (elId) => {
10459
11337
  await this.movement.pointAt(elId, "developer");
10460
11338
  },
10461
- async (elId, style) => {
10462
- const el = this.targetRegistry.get(elId);
10463
- if (el) {
10464
- this.highlights.show(el, style || "spotlight");
11339
+ highlightElement: async (elId, style) => {
11340
+ const resolved = this.resolver.tryResolve(elId, "developer");
11341
+ if (resolved) {
11342
+ this.highlights.show(resolved.element, style || "spotlight");
11343
+ } else {
11344
+ const el = this.targetRegistry.get(elId);
11345
+ if (el) {
11346
+ this.highlights.show(el, style || "spotlight");
11347
+ }
10465
11348
  }
10466
11349
  },
10467
- async (text) => {
11350
+ clearHighlight: () => {
11351
+ this.highlights.hide();
11352
+ },
11353
+ speak: async (text) => {
10468
11354
  await this.speechPlayer.speak(text);
10469
11355
  },
10470
- (event) => {
11356
+ playGesture: (gesture) => {
11357
+ return this.avatar.playGesture(gesture);
11358
+ },
11359
+ onStepChanged: (event) => {
10471
11360
  this.events.emit("asiyst:tour:step_changed", event);
10472
11361
  },
10473
- (tourId) => {
11362
+ onTourCompleted: (tourId) => {
10474
11363
  this.events.emit("asiyst:tour:completed", { tourId });
10475
11364
  },
10476
- (tourId, error) => {
11365
+ onTourError: (tourId, error) => {
10477
11366
  this.events.emit("asiyst:tour:error", { tourId, error });
10478
11367
  }
10479
- );
11368
+ });
10480
11369
  this.actionScheduler = new ActionScheduler(
10481
11370
  (action) => this.createActionContext(action, doc, win)
10482
11371
  );
@@ -10612,19 +11501,48 @@ var AsiystRuntime = class {
10612
11501
  }
10613
11502
  this.setState("fetching-config");
10614
11503
  let cfg;
11504
+ let configFailedWithError;
10615
11505
  try {
10616
- cfg = await isolateAsync(() => this.config.refresh(), this.config.get());
11506
+ cfg = await this.config.refresh();
10617
11507
  this.metricsTracker.record("configRetrieved");
10618
11508
  } catch (configErr) {
10619
- const asiystErr = new AsiystError({
10620
- code: "CONFIG_INVALID",
11509
+ const preservedCode = configErr instanceof AsiystError && configErr.code && configErr.code !== "configuration_error" && configErr.code !== "network_error" && configErr.code !== "authentication_error" ? configErr.code : configErr instanceof AuthenticationError ? "UNAUTHORIZED" : "CONFIG_INVALID";
11510
+ const asiystErr = configErr instanceof AsiystError && preservedCode === configErr.code ? configErr : new AsiystError({
11511
+ code: preservedCode,
10621
11512
  message: configErr instanceof Error ? configErr.message : "Failed to load project configuration.",
11513
+ hint: configErr instanceof AsiystError ? configErr.hint : void 0,
11514
+ status: configErr instanceof AsiystError ? configErr.status : void 0,
10622
11515
  cause: configErr
10623
11516
  });
11517
+ configFailedWithError = asiystErr;
10624
11518
  this.recordError(asiystErr);
11519
+ this.events.emit("asiyst:avatar:error", {
11520
+ avatarId: this.options.avatarId,
11521
+ error: asiystErr.message,
11522
+ stage: "metadata_retrieval"
11523
+ });
10625
11524
  this.events.emit("error", asiystErr);
10626
11525
  cfg = this.config.get();
10627
11526
  }
11527
+ if (configFailedWithError) {
11528
+ const fatalConfigCodes = /* @__PURE__ */ new Set([
11529
+ "AVATAR_NOT_FOUND",
11530
+ "AVATAR_NOT_IMPORTED",
11531
+ "PROJECT_MISMATCH",
11532
+ "UNAUTHORIZED",
11533
+ "DOMAIN_NOT_APPROVED",
11534
+ "ORIGIN_NOT_APPROVED",
11535
+ "MISSING_CHARACTER_FILE"
11536
+ ]);
11537
+ if (this.options.avatarId || fatalConfigCodes.has(String(configFailedWithError.code))) {
11538
+ this.renderer.renderErrorState(configFailedWithError, this.options.fallback);
11539
+ this.avatar.show();
11540
+ this.movement.goToAnchor();
11541
+ this.setAvatarLifecycleState("error");
11542
+ this.setState("error", { reason: "avatar_config_failed", code: configFailedWithError.code });
11543
+ return;
11544
+ }
11545
+ }
10628
11546
  const domainCheck = validateWebsiteDomain(cfg, window.location.href);
10629
11547
  if (!domainCheck.allowed) {
10630
11548
  this.connectionStatus = "disconnected";
@@ -10663,6 +11581,23 @@ var AsiystRuntime = class {
10663
11581
  this.avatar.show();
10664
11582
  this.movement.goToAnchor();
10665
11583
  }
11584
+ } else if (this.options.avatarId || cfg.avatarId) {
11585
+ const missingCharErr = new AsiystError({
11586
+ code: "MISSING_CHARACTER_FILE",
11587
+ message: `Avatar "${this.options.avatarId ?? cfg.avatarId}" is missing a character 3D model file (FBX/GLB).`,
11588
+ hint: "Upload and publish a valid character FBX or GLB in Avatar Studio."
11589
+ });
11590
+ this.recordError(missingCharErr);
11591
+ this.renderer.renderErrorState(missingCharErr, this.options.fallback);
11592
+ this.avatar.show();
11593
+ this.movement.goToAnchor();
11594
+ this.events.emit("asiyst:avatar:error", {
11595
+ avatarId: this.options.avatarId ?? cfg.avatarId,
11596
+ error: missingCharErr.message,
11597
+ stage: "asset_url_resolution"
11598
+ });
11599
+ this.events.emit("error", missingCharErr);
11600
+ avatarLoadedSuccessfully = false;
10666
11601
  } else {
10667
11602
  this.avatar.show();
10668
11603
  this.movement.goToAnchor();
@@ -10717,6 +11652,17 @@ var AsiystRuntime = class {
10717
11652
  const format = this.renderer.getLoadedFormat();
10718
11653
  const assetUrl = this.renderer.getAssetUrl();
10719
11654
  const isRendered = this.renderer.isRendered();
11655
+ const failedAnims = this.renderer.getFailedAnimations();
11656
+ for (const failedAnim of failedAnims) {
11657
+ this.recordError({
11658
+ code: failedAnim.code ?? "MISSING_ANIMATION_FILE",
11659
+ message: failedAnim.error
11660
+ });
11661
+ this.events.emit("asiyst:animation:error", {
11662
+ name: failedAnim.name,
11663
+ error: failedAnim.error
11664
+ });
11665
+ }
10720
11666
  this.events.emit("asiyst:avatar:loaded", {
10721
11667
  avatarId: this.options.avatarId ?? cfg.avatarId,
10722
11668
  assetUrl,
@@ -10733,16 +11679,25 @@ var AsiystRuntime = class {
10733
11679
  }
10734
11680
  return true;
10735
11681
  } else {
11682
+ const lastErrObj = this.renderer.getLastErrorObject();
10736
11683
  const err = this.renderer.getLastError() ?? "Avatar asset loading failed";
11684
+ const errCode = lastErrObj instanceof AsiystError && lastErrObj.code ? String(lastErrObj.code) : "AVATAR_LOAD_FAILED";
11685
+ const errHint = lastErrObj instanceof AsiystError ? lastErrObj.hint : void 0;
10737
11686
  this.recordError({
10738
- code: "AVATAR_LOAD_FAILED",
10739
- message: err
11687
+ code: errCode,
11688
+ message: err,
11689
+ hint: errHint
10740
11690
  });
10741
11691
  this.events.emit("asiyst:avatar:error", {
10742
11692
  avatarId: this.options.avatarId ?? cfg.avatarId,
10743
11693
  error: err,
10744
11694
  stage: "renderer_init"
10745
11695
  });
11696
+ this.events.emit("error", {
11697
+ code: errCode,
11698
+ message: err,
11699
+ hint: errHint
11700
+ });
10746
11701
  return false;
10747
11702
  }
10748
11703
  } catch (error) {
@@ -10762,7 +11717,7 @@ var AsiystRuntime = class {
10762
11717
  }
10763
11718
  this.configRevalidationTimer = setInterval(() => {
10764
11719
  if (!this.destroyed) {
10765
- void this.config.refresh();
11720
+ void this.config.refresh({ silent: true });
10766
11721
  }
10767
11722
  }, CONFIG_REVALIDATION_INTERVAL_MS);
10768
11723
  }
@@ -10896,12 +11851,28 @@ var AsiystRuntime = class {
10896
11851
  };
10897
11852
  }
10898
11853
  const cfg = this.config.get();
10899
- const runtimeConfig = {
11854
+ const configError = this.errorsObserved.find(
11855
+ (e) => [
11856
+ "CONFIG_INVALID",
11857
+ "AVATAR_NOT_FOUND",
11858
+ "AVATAR_NOT_IMPORTED",
11859
+ "PROJECT_MISMATCH",
11860
+ "CORS_OR_NETWORK_FAILURE"
11861
+ ].includes(e.code)
11862
+ );
11863
+ const runtimeConfig = configError ? {
11864
+ ok: false,
11865
+ code: configError.code,
11866
+ message: configError.message
11867
+ } : {
10900
11868
  ok: Boolean(cfg && cfg.version !== void 0),
10901
11869
  status: cfg ? 200 : void 0,
10902
11870
  message: cfg ? "Runtime configuration loaded" : "Runtime configuration unavailable"
10903
11871
  };
10904
11872
  const avatarId = this.options.avatarId ?? cfg.avatarId ?? "default";
11873
+ const failedAnimSet = new Set(
11874
+ this.renderer.getFailedAnimations().map((f) => (f.id || f.name).toLowerCase())
11875
+ );
10905
11876
  const assets = [];
10906
11877
  if (cfg.assetUrl) {
10907
11878
  assets.push({
@@ -10912,10 +11883,12 @@ var AsiystRuntime = class {
10912
11883
  }
10913
11884
  if (cfg.animations && Array.isArray(cfg.animations)) {
10914
11885
  for (const anim of cfg.animations) {
11886
+ const animKey = (anim.id || anim.name || "").toLowerCase();
11887
+ const animFailed = failedAnimSet.has(animKey) || !(anim.assetUrl || anim.downloadUrl);
10915
11888
  assets.push({
10916
11889
  name: anim.name || anim.assetUrl || "animation",
10917
- format: "fbx",
10918
- loaded: this.renderer.isAssetLoaded()
11890
+ format: anim.format || "fbx",
11891
+ loaded: this.renderer.isAssetLoaded() && !animFailed
10919
11892
  });
10920
11893
  }
10921
11894
  }
@@ -11168,8 +12141,8 @@ var AsiystRuntime = class {
11168
12141
  origin
11169
12142
  });
11170
12143
  if (reg?.runtimeSessionId) {
11171
- this.installationId = reg.runtimeSessionId;
11172
- this.runtimeAuth?.setInstallationId(reg.runtimeSessionId);
12144
+ this.visitorSessionId = reg.runtimeSessionId;
12145
+ this.cloud.setSessionId(reg.runtimeSessionId);
11173
12146
  this.registrationSucceeded = true;
11174
12147
  this.connectionStatus = "connected";
11175
12148
  this.verificationStatus = "connected";
@@ -11353,17 +12326,11 @@ var AsiystRuntime = class {
11353
12326
  async avatarSubmitContext() {
11354
12327
  await this.submitWebsiteContext();
11355
12328
  }
11356
- async avatarHighlight(targetId, style) {
11357
- const el = this.targetRegistry.get(targetId);
11358
- if (el) {
11359
- this.highlights.show(el, style ?? "outline");
11360
- }
12329
+ async avatarHighlight(target, style) {
12330
+ await this.avatar.highlight(target, style ?? "outline", "developer");
11361
12331
  }
11362
- async avatarPointAt(targetId) {
11363
- const el = this.targetRegistry.get(targetId);
11364
- if (el) {
11365
- await this.avatar.pointAt(targetId, "developer");
11366
- }
12332
+ async avatarPointAt(target) {
12333
+ await this.avatar.pointAt(target, "developer");
11367
12334
  }
11368
12335
  async avatarNavigateTo(route) {
11369
12336
  const win = this.host?.chrome?.ownerDocument?.defaultView || (typeof window !== "undefined" ? window : void 0);
@@ -11500,15 +12467,10 @@ var AsiystInstanceImpl = class {
11500
12467
  interact: {
11501
12468
  highlight: async (targetId, options) => {
11502
12469
  const style = typeof options === "string" ? options : options?.style;
11503
- await this.requireInstanceRuntime().avatarHighlight(
11504
- typeof targetId === "string" ? targetId : targetId.id || "",
11505
- style
11506
- );
12470
+ await this.requireInstanceRuntime().avatarHighlight(targetId, style);
11507
12471
  },
11508
12472
  pointAt: async (targetId) => {
11509
- await this.requireInstanceRuntime().avatarPointAt(
11510
- typeof targetId === "string" ? targetId : targetId.id || ""
11511
- );
12473
+ await this.requireInstanceRuntime().avatarPointAt(targetId);
11512
12474
  }
11513
12475
  },
11514
12476
  navigation: {
@@ -11814,10 +12776,10 @@ var Asiyst = {
11814
12776
  interact: {
11815
12777
  async highlight(targetId, options) {
11816
12778
  const style = typeof options === "string" ? options : options?.style;
11817
- await requireRuntime().avatarHighlight(typeof targetId === "string" ? targetId : targetId.id || "", style);
12779
+ await requireRuntime().avatarHighlight(targetId, style);
11818
12780
  },
11819
12781
  async pointAt(targetId) {
11820
- await requireRuntime().avatarPointAt(typeof targetId === "string" ? targetId : targetId.id || "");
12782
+ await requireRuntime().avatarPointAt(targetId);
11821
12783
  }
11822
12784
  },
11823
12785
  navigation: {
@@ -11964,6 +12926,7 @@ exports.AssetLoader = AssetLoader;
11964
12926
  exports.AuthenticationError = AuthenticationError;
11965
12927
  exports.AvatarController = AvatarController;
11966
12928
  exports.AvatarMetricsTracker = AvatarMetricsTracker;
12929
+ exports.BalanceEngine = BalanceEngine;
11967
12930
  exports.CANONICAL_BONE_NAMES = CANONICAL_BONE_NAMES;
11968
12931
  exports.ConfigurationError = ConfigurationError;
11969
12932
  exports.ConfirmationManager = ConfirmationManager;
@@ -11995,6 +12958,7 @@ exports.TargetRegistry = TargetRegistry;
11995
12958
  exports.TaskExecutionError = TaskExecutionError;
11996
12959
  exports.TaskStatus = TaskStatus;
11997
12960
  exports.TourEngine = TourEngine;
12961
+ exports.TurnEngine = TurnEngine;
11998
12962
  exports.TwoBoneIK = TwoBoneIK;
11999
12963
  exports.anchorToViewport = anchorToViewport;
12000
12964
  exports.asiystInit = asiystInit;