seacloud-sdk 0.11.4 → 0.11.6

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/cli.js CHANGED
@@ -57,6 +57,43 @@ async function getTokenFromParent(timeout = 5e3) {
57
57
  }
58
58
  });
59
59
  }
60
+ async function getHostFromParent(timeout = 5e3) {
61
+ if (!isInIframe()) {
62
+ return null;
63
+ }
64
+ return new Promise((resolve) => {
65
+ const messageHandler = (event) => {
66
+ if (event.data && event.data.type === "seaverse:host") {
67
+ cleanup();
68
+ const host = event.data.payload?.host;
69
+ resolve(host || null);
70
+ } else if (event.data && event.data.type === "seaverse:error") {
71
+ cleanup();
72
+ console.warn("[SeaCloud SDK] Error getting host from parent:", event.data.error);
73
+ resolve(null);
74
+ }
75
+ };
76
+ const timeoutId = setTimeout(() => {
77
+ cleanup();
78
+ resolve(null);
79
+ }, timeout);
80
+ const cleanup = () => {
81
+ clearTimeout(timeoutId);
82
+ globalThis.window.removeEventListener("message", messageHandler);
83
+ };
84
+ globalThis.window.addEventListener("message", messageHandler);
85
+ try {
86
+ globalThis.window.parent.postMessage(
87
+ { type: "seaverse:get_host" },
88
+ "*"
89
+ // 允许任何源,支持跨域场景
90
+ );
91
+ } catch (e) {
92
+ cleanup();
93
+ resolve(null);
94
+ }
95
+ });
96
+ }
60
97
  async function getApiToken(providedApiKey) {
61
98
  if (providedApiKey) {
62
99
  return providedApiKey;
@@ -238,7 +275,7 @@ var globalConfig = {
238
275
  // 最多尝试100次 (约5分钟)
239
276
  }
240
277
  };
241
- function initSeacloud(apiKeyOrConfig, options) {
278
+ async function initSeacloud(apiKeyOrConfig, options) {
242
279
  let apiKey;
243
280
  let config;
244
281
  if (typeof apiKeyOrConfig === "string") {
@@ -251,6 +288,23 @@ function initSeacloud(apiKeyOrConfig, options) {
251
288
  config = {};
252
289
  apiKey = void 0;
253
290
  }
291
+ if (!config.baseUrl) {
292
+ try {
293
+ const parentHost = await getHostFromParent(3e3);
294
+ if (parentHost) {
295
+ const isDevelopment = parentHost.includes("localhost") || parentHost.includes("127.0.0.1") || parentHost.includes(":3000") || parentHost.includes(":8080") || parentHost.includes("seaverse.dev");
296
+ if (isDevelopment) {
297
+ config.baseUrl = "https://proxy-rs-dev.seaverse.ai";
298
+ console.log("[SeaCloud SDK] \u68C0\u6D4B\u5230\u5F00\u53D1\u73AF\u5883\uFF0C\u4F7F\u7528\u5F00\u53D1 baseUrl:", config.baseUrl);
299
+ } else {
300
+ config.baseUrl = "https://proxy-rs.seaverse.ai";
301
+ console.log("[SeaCloud SDK] \u68C0\u6D4B\u5230\u6B63\u5F0F\u73AF\u5883\uFF0C\u4F7F\u7528\u6B63\u5F0F baseUrl:", config.baseUrl);
302
+ }
303
+ }
304
+ } catch (error) {
305
+ console.warn("[SeaCloud SDK] \u83B7\u53D6\u7236\u9875\u9762 host \u5931\u8D25\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u914D\u7F6E", error);
306
+ }
307
+ }
254
308
  globalConfig.client = new SeacloudClient({
255
309
  apiKey: apiKey || "",
256
310
  baseUrl: config.baseUrl,