seacloud-sdk 0.9.7 → 0.9.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/cli.js CHANGED
@@ -13,7 +13,51 @@ var SeacloudError = class extends Error {
13
13
  };
14
14
 
15
15
  // src/core/config.ts
16
- function getApiToken(providedApiKey) {
16
+ function isInIframe() {
17
+ try {
18
+ return typeof globalThis.window !== "undefined" && globalThis.window.self !== globalThis.window.top;
19
+ } catch (e) {
20
+ return true;
21
+ }
22
+ }
23
+ async function getTokenFromParent(timeout = 5e3) {
24
+ if (!isInIframe()) {
25
+ return null;
26
+ }
27
+ return new Promise((resolve) => {
28
+ const messageHandler = (event) => {
29
+ if (event.data && event.data.type === "seaverse:token") {
30
+ cleanup();
31
+ const token = event.data.payload?.accessToken;
32
+ resolve(token || null);
33
+ } else if (event.data && event.data.type === "seaverse:error") {
34
+ cleanup();
35
+ console.warn("[SeaCloud SDK] Error getting token from parent:", event.data.error);
36
+ resolve(null);
37
+ }
38
+ };
39
+ const timeoutId = setTimeout(() => {
40
+ cleanup();
41
+ resolve(null);
42
+ }, timeout);
43
+ const cleanup = () => {
44
+ clearTimeout(timeoutId);
45
+ globalThis.window.removeEventListener("message", messageHandler);
46
+ };
47
+ globalThis.window.addEventListener("message", messageHandler);
48
+ try {
49
+ globalThis.window.parent.postMessage(
50
+ { type: "seaverse:get_token" },
51
+ "*"
52
+ // 允许任何源,支持跨域场景
53
+ );
54
+ } catch (e) {
55
+ cleanup();
56
+ resolve(null);
57
+ }
58
+ });
59
+ }
60
+ async function getApiToken(providedApiKey) {
17
61
  if (providedApiKey) {
18
62
  return providedApiKey;
19
63
  }
@@ -29,19 +73,24 @@ function getApiToken(providedApiKey) {
29
73
  if (typeof process !== "undefined" && process.env?.API_SERVICE_TOKEN) {
30
74
  return process.env.API_SERVICE_TOKEN;
31
75
  }
32
- throw new Error(
33
- 'SeaCloud SDK: No API token found. Please ensure token is available in localStorage.getItem("auth_token") (browser) or process.env.API_SERVICE_TOKEN (Node.js), or initialize with initSeacloud({ apiKey: "your-token" }).'
34
- );
76
+ if (typeof globalThis.window !== "undefined") {
77
+ const parentToken = await getTokenFromParent();
78
+ if (parentToken) {
79
+ return parentToken;
80
+ }
81
+ }
82
+ return "";
35
83
  }
36
84
  function createConfig(options = {}) {
37
- const apiKey = getApiToken(options.apiKey);
85
+ const apiKey = options.apiKey;
38
86
  const baseUrl = options.baseUrl || (typeof process !== "undefined" ? process.env?.SEACLOUD_BASE_URL : void 0) || "https://proxy-rs.seaverse.ai";
39
87
  const fetchImpl = options.fetch || (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
40
88
  if (!fetchImpl) {
41
89
  throw new Error("fetch is not available. Please provide a fetch implementation in config or upgrade to Node.js 18+");
42
90
  }
43
91
  return {
44
- apiKey,
92
+ apiKey: apiKey || "",
93
+ // 提供默认空字符串,实际请求时会动态获取
45
94
  baseUrl,
46
95
  fetch: fetchImpl,
47
96
  timeout: options.timeout || 3e4
@@ -86,7 +135,7 @@ var SeacloudClient = class {
86
135
  */
87
136
  async createTask(endpoint, body) {
88
137
  const url = `${this.config.baseUrl}${endpoint}`;
89
- const currentToken = getApiToken(this.providedApiKey);
138
+ const currentToken = await getApiToken(this.providedApiKey);
90
139
  const controller = new AbortController();
91
140
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
92
141
  try {
@@ -133,7 +182,7 @@ var SeacloudClient = class {
133
182
  */
134
183
  async getTaskStatus(endpoint, taskId) {
135
184
  const url = `${this.config.baseUrl}${endpoint}/task/${taskId}`;
136
- const currentToken = getApiToken(this.providedApiKey);
185
+ const currentToken = await getApiToken(this.providedApiKey);
137
186
  const controller = new AbortController();
138
187
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
139
188
  try {
@@ -230,6 +279,7 @@ async function llmChatCompletions(params) {
230
279
  const client = getClient();
231
280
  const config = client.getConfig();
232
281
  const url = `${config.baseUrl}/llm/chat/completions`;
282
+ const token = await getApiToken(config.apiKey);
233
283
  const controller = new AbortController();
234
284
  const timeoutId = setTimeout(() => controller.abort(), config.timeout);
235
285
  try {
@@ -237,7 +287,7 @@ async function llmChatCompletions(params) {
237
287
  method: "POST",
238
288
  headers: {
239
289
  "Content-Type": "application/json",
240
- "Authorization": `Bearer ${config.apiKey}`
290
+ "Authorization": `Bearer ${token}`
241
291
  },
242
292
  body: JSON.stringify(params),
243
293
  signal: controller.signal