@howone/sdk 0.2.6 → 0.2.8

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.mjs CHANGED
@@ -516,7 +516,7 @@ var UnifiedAuthService = class {
516
516
  }
517
517
  generateAppId() {
518
518
  const urlParams = new URLSearchParams(window.location.search);
519
- let appId = urlParams.get("appId");
519
+ let appId = urlParams.get("appId") || urlParams.get("project_id");
520
520
  return appId || "app";
521
521
  }
522
522
  /**
@@ -894,7 +894,7 @@ var LoginForm = ({
894
894
 
895
895
  // src/components/auth/HowoneProvider.tsx
896
896
  init_auth();
897
- import { createContext as createContext2, useContext as useContext2, useState as useState7, useEffect as useEffect6 } from "react";
897
+ import { createContext as createContext2, useContext as useContext2, useState as useState7, useEffect as useEffect6, useMemo } from "react";
898
898
 
899
899
  // src/components/theme/ThemeProvider.tsx
900
900
  import { createContext, useContext, useEffect as useEffect3, useState as useState3 } from "react";
@@ -3086,6 +3086,7 @@ var ErrorTracking = class {
3086
3086
  this.setupGlobalErrorListeners();
3087
3087
  this.setupConsoleInterception();
3088
3088
  this.setupNetworkMonitoring();
3089
+ this.setupResourceLoadMonitoring();
3089
3090
  this.initialized = true;
3090
3091
  setTimeout(() => {
3091
3092
  this.fullyInitialized = true;
@@ -3096,7 +3097,7 @@ var ErrorTracking = class {
3096
3097
  */
3097
3098
  setupGlobalErrorListeners() {
3098
3099
  window.addEventListener("error", (event) => {
3099
- event.preventDefault();
3100
+ this.internalLog("warn", "\u{1F3AF}\u{1F3AF}\u{1F3AF} window.error \u4E8B\u4EF6\u89E6\u53D1:", event.message || event.error?.message);
3100
3101
  this.handleError(event.error || new Error(event.message), {
3101
3102
  filename: event.filename,
3102
3103
  lineno: event.lineno,
@@ -3106,8 +3107,8 @@ var ErrorTracking = class {
3106
3107
  });
3107
3108
  });
3108
3109
  window.addEventListener("unhandledrejection", (event) => {
3109
- event.preventDefault();
3110
3110
  const error = event.reason instanceof Error ? event.reason : new Error(String(event.reason));
3111
+ this.internalLog("warn", "\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u{1F680}\u6355\u83B7 unhandledrejection:", error.message);
3111
3112
  this.handleError(error, {
3112
3113
  type: "promise",
3113
3114
  source: "unhandledrejection"
@@ -3142,7 +3143,12 @@ var ErrorTracking = class {
3142
3143
  */
3143
3144
  safeForwardConsoleToParent(method, args) {
3144
3145
  if (window.parent === window) return;
3145
- if (!this.fullyInitialized) return;
3146
+ const argsStr = args.map((arg) => String(arg)).join(" ").toLowerCase();
3147
+ const is504Error = argsStr.includes("504") || argsStr.includes("gateway timeout");
3148
+ if (is504Error) {
3149
+ this.internalLog("warn", "\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}\u{1F697}[ErrorTracking] \u63A7\u5236\u53F0\u6355\u83B7 504 \u9519\u8BEF:", argsStr.substring(0, 100));
3150
+ }
3151
+ if (!is504Error && !this.fullyInitialized) return;
3146
3152
  try {
3147
3153
  let stack = null;
3148
3154
  if (method === "warn" || method === "error") {
@@ -3218,6 +3224,43 @@ var ErrorTracking = class {
3218
3224
  this.internalLog("error", "[ErrorTracking] \u63A7\u5236\u53F0\u8F6C\u53D1\u5931\u8D25:", error);
3219
3225
  }
3220
3226
  }
3227
+ /**
3228
+ * 设置资源加载监控(使用 PerformanceObserver 监控 504 错误)
3229
+ */
3230
+ setupResourceLoadMonitoring() {
3231
+ if (!window.PerformanceObserver) {
3232
+ this.internalLog("warn", "\u274C\u274C\u274CPerformanceObserver \u4E0D\u652F\u6301\u274C\u274C\u274C");
3233
+ return;
3234
+ }
3235
+ try {
3236
+ const observer = new PerformanceObserver((list) => {
3237
+ for (const entry of list.getEntries()) {
3238
+ console.log("\u{1F30D}\u{1F30D}entry---------entry\u{1F30D}\u{1F30D}", entry);
3239
+ if (entry.entryType === "resource") {
3240
+ const resourceEntry = entry;
3241
+ if (resourceEntry.transferSize === 0 && !resourceEntry.duration) {
3242
+ const url = resourceEntry.name;
3243
+ console.log("\u{1F30D}\u{1F30D}url--------------url\u{1F30D}\u{1F30D}", url);
3244
+ if (url.includes(".js") || url.includes(".mjs")) {
3245
+ this.internalLog("error", "\u274C\u274C\u274C\u274C\u274C\u274C\u274C\u274C\u274C\u274C\u68C0\u6D4B\u5230\u8D44\u6E90\u52A0\u8F7D\u5931\u8D25\uFF08\u53EF\u80FD\u662F 504\uFF09:", url);
3246
+ this.handleError(new Error(`Resource load failed (possibly 504): ${url}`), {
3247
+ type: "resource",
3248
+ resourceUrl: url,
3249
+ source: "PerformanceObserver",
3250
+ transferSize: resourceEntry.transferSize,
3251
+ duration: resourceEntry.duration
3252
+ });
3253
+ }
3254
+ }
3255
+ }
3256
+ }
3257
+ });
3258
+ observer.observe({ entryTypes: ["resource"] });
3259
+ this.internalLog("log", "\u2705\u2705\u2705\u2705\u2705\u2705\u2705\u2705\u2705\u2705\u5DF2\u542F\u52A8\uFF0C\u76D1\u63A7\u8D44\u6E90\u52A0\u8F7D");
3260
+ } catch (error) {
3261
+ this.internalLog("error", "\u274C\u274C\u274C\u274C\u274C\u274C\u274C\u274C\u274C\u274CPerformanceObserver \u8BBE\u7F6E\u5931\u8D25:", error);
3262
+ }
3263
+ }
3221
3264
  /**
3222
3265
  * 设置网络监控
3223
3266
  */
@@ -3351,11 +3394,18 @@ var ErrorTracking = class {
3351
3394
  return false;
3352
3395
  }
3353
3396
  /**
3354
- * 判断是否是 504 错误,需要立即发送
3397
+ * 判断是否是 504 错误或资源加载失败,需要立即发送
3355
3398
  */
3356
3399
  isCriticalNetworkError(error, details) {
3357
3400
  const message = error.message?.toLowerCase() || "";
3358
- return message.includes("504") || message.includes("gateway timeout");
3401
+ console.log("\u5F00\u59CB\u68C0\u6D4B\u2705\u5F00\u59CB\u68C0\u6D4B\u2705\u5F00\u59CB\u68C0\u6D4B\u2705\u5F00\u59CB\u68C0\u6D4B\u2705\u5F00\u59CB\u68C0\u6D4B\u2705", message, details);
3402
+ if (message.includes("504") || message.includes("gateway timeout")) {
3403
+ return true;
3404
+ }
3405
+ if (details.type === "resource" && details.source === "PerformanceObserver") {
3406
+ return true;
3407
+ }
3408
+ return false;
3359
3409
  }
3360
3410
  /**
3361
3411
  * 发送统一格式的错误
@@ -3363,6 +3413,11 @@ var ErrorTracking = class {
3363
3413
  sendUnifiedError(error, details = {}) {
3364
3414
  if (window.parent === window) return;
3365
3415
  const isCriticalError = this.isCriticalNetworkError(error, details);
3416
+ console.log("\u53D1\u73B0504\u9519\u8BEF\u4E86\u5417\u2753\u2753\u2753\u2753\u2753\u2753\u2753\u2753", isCriticalError);
3417
+ if (isCriticalError) {
3418
+ this.internalLog("warn", "\u{1F31E}\u{1F31E}\u{1F31E}\u{1F31E}\u{1F31E}\u{1F31E}\u{1F31E}\u{1F31E}\u{1F31E}\u{1F31E}\u{1F31E}\u68C0\u6D4B\u5230 504 \u9519\u8BEF\uFF0C\u7ACB\u5373\u53D1\u9001:", error.message);
3419
+ }
3420
+ console.log("\u7B2C\u4E8C\u4E2A\u5224\u65AD\u662F\u4EC0\u4E48\u2753\u2753\u2753\u2753\u2753\u2753\u2753\u2753", this.fullyInitialized);
3366
3421
  if (!isCriticalError && !this.fullyInitialized) {
3367
3422
  return;
3368
3423
  }
@@ -4737,6 +4792,7 @@ var ErrorHandler = class {
4737
4792
  };
4738
4793
 
4739
4794
  // src/components/auth/HowoneProvider.tsx
4795
+ init_config();
4740
4796
  import { jsx as jsx7, jsxs as jsxs5 } from "react/jsx-runtime";
4741
4797
  var globalErrorHandler = null;
4742
4798
  var initializeErrorHandler = () => {
@@ -4760,7 +4816,6 @@ var HowOneProvider = ({
4760
4816
  defaultTheme = "system",
4761
4817
  themeStorageKey = "howone-theme",
4762
4818
  forceDefaultTheme = false,
4763
- authUrl = "https://howone.dev/auth",
4764
4819
  redirectOnUnauthenticated = true
4765
4820
  }) => {
4766
4821
  const [user, setUser] = useState7(() => parseUserFromToken(getToken()));
@@ -4791,6 +4846,18 @@ var HowOneProvider = ({
4791
4846
  setHasCheckedUrlToken(true);
4792
4847
  }
4793
4848
  }, []);
4849
+ const resolvedAuthUrl = useMemo(() => {
4850
+ const env2 = getGlobalEnvironment() ?? "dev";
4851
+ switch (env2) {
4852
+ case "local":
4853
+ return "http://localhost:3000/auth";
4854
+ case "prod":
4855
+ return "https://howone.ai/auth";
4856
+ case "dev":
4857
+ default:
4858
+ return "https://howone.dev/auth";
4859
+ }
4860
+ }, []);
4794
4861
  useEffect6(() => {
4795
4862
  if (!hasCheckedUrlToken) {
4796
4863
  return;
@@ -4799,7 +4866,7 @@ var HowOneProvider = ({
4799
4866
  const currentUrl = new URL(window.location.href);
4800
4867
  if (!currentUrl.pathname.includes("/auth")) {
4801
4868
  try {
4802
- const authUrlObj = new URL(authUrl);
4869
+ const authUrlObj = new URL(resolvedAuthUrl);
4803
4870
  const redirectUri = window.location.href;
4804
4871
  authUrlObj.searchParams.set("redirect_uri", redirectUri);
4805
4872
  if (projectId) {
@@ -4808,11 +4875,11 @@ var HowOneProvider = ({
4808
4875
  window.location.href = authUrlObj.toString();
4809
4876
  } catch (error) {
4810
4877
  console.error("[HowOneProvider] Failed to build auth URL:", error);
4811
- window.location.href = authUrl;
4878
+ window.location.href = resolvedAuthUrl;
4812
4879
  }
4813
4880
  }
4814
4881
  }
4815
- }, [token, user, redirectOnUnauthenticated, authUrl, projectId, hasCheckedUrlToken]);
4882
+ }, [token, user, redirectOnUnauthenticated, resolvedAuthUrl, projectId, hasCheckedUrlToken]);
4816
4883
  const logout = () => {
4817
4884
  try {
4818
4885
  setToken(null);