seacloud-sdk 0.12.3 → 0.12.5

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,19 +57,19 @@ async function getTokenFromParent(timeout = 5e3) {
57
57
  }
58
58
  });
59
59
  }
60
- async function getHostFromParent(timeout = 5e3) {
60
+ async function getEnvFromParent(timeout = 5e3) {
61
61
  if (!isInIframe()) {
62
62
  return null;
63
63
  }
64
64
  return new Promise((resolve) => {
65
65
  const messageHandler = (event) => {
66
- if (event.data && event.data.type === "seaverse:host") {
66
+ if (event.data && event.data.type === "seaverse:env") {
67
67
  cleanup();
68
- const host = event.data.payload?.host;
69
- resolve(host || null);
68
+ const env = event.data.payload?.env;
69
+ resolve(env || null);
70
70
  } else if (event.data && event.data.type === "seaverse:error") {
71
71
  cleanup();
72
- console.warn("[SeaCloud SDK] Error getting host from parent:", event.data.error);
72
+ console.warn("[SeaCloud SDK] Error getting env from parent:", event.data.error);
73
73
  resolve(null);
74
74
  }
75
75
  };
@@ -84,7 +84,7 @@ async function getHostFromParent(timeout = 5e3) {
84
84
  globalThis.window.addEventListener("message", messageHandler);
85
85
  try {
86
86
  globalThis.window.parent.postMessage(
87
- { type: "seaverse:get_host" },
87
+ { type: "seaverse:get_env" },
88
88
  "*"
89
89
  // 允许任何源,支持跨域场景
90
90
  );
@@ -118,9 +118,38 @@ async function getApiToken(providedApiKey) {
118
118
  }
119
119
  return "";
120
120
  }
121
- function createConfig(options = {}) {
121
+ function checkIsDevelopmentHost(host) {
122
+ const devHostPatterns = ["localhost", "127.0.0.1", ":3000", ":8080", "seaverse.dev"];
123
+ return devHostPatterns.some((pattern) => host.includes(pattern));
124
+ }
125
+ async function getBaseUrl(userBaseUrl) {
126
+ if (userBaseUrl) {
127
+ return userBaseUrl;
128
+ }
129
+ if (typeof process !== "undefined" && process.env?.SEACLOUD_BASE_URL) {
130
+ return process.env.SEACLOUD_BASE_URL;
131
+ }
132
+ if (isInIframe()) {
133
+ const parentEnv = await getEnvFromParent(3e3);
134
+ if (parentEnv === "develop") {
135
+ console.log("[SeaCloud SDK] iframe \u73AF\u5883\uFF1A\u7236\u9875\u9762\u8FD4\u56DE\u5F00\u53D1\u73AF\u5883\uFF0C\u4F7F\u7528\u5F00\u53D1 baseUrl");
136
+ return "https://proxy-rs.sg.seaverse.dev";
137
+ } else {
138
+ console.log("[SeaCloud SDK] iframe \u73AF\u5883\uFF1A\u4F7F\u7528\u7EBF\u4E0A baseUrl");
139
+ return "https://proxy-rs.seaverse.ai";
140
+ }
141
+ }
142
+ const currentHost = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" ? globalThis.window.location.host : "";
143
+ if (checkIsDevelopmentHost(currentHost)) {
144
+ console.log("[SeaCloud SDK] \u68C0\u6D4B\u5230\u5F00\u53D1\u73AF\u5883\uFF08currentHost\uFF09\uFF0C\u4F7F\u7528\u5F00\u53D1 baseUrl");
145
+ return "https://proxy-rs.sg.seaverse.dev";
146
+ } else {
147
+ return "https://proxy-rs.seaverse.ai";
148
+ }
149
+ }
150
+ async function createConfig(options = {}) {
122
151
  const apiKey = options.apiKey;
123
- const baseUrl = options.baseUrl || (typeof process !== "undefined" ? process.env?.SEACLOUD_BASE_URL : void 0) || "https://proxy-rs.seaverse.ai";
152
+ const baseUrl = await getBaseUrl(options.baseUrl);
124
153
  const fetchImpl = options.fetch || (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
125
154
  if (!fetchImpl) {
126
155
  throw new Error("fetch is not available. Please provide a fetch implementation in config or upgrade to Node.js 18+");
@@ -144,14 +173,14 @@ function validateConfig(config) {
144
173
  }
145
174
 
146
175
  // src/core/version.ts
147
- var VERSION = "0.9.6";
176
+ var VERSION = "0.12.5";
148
177
 
149
178
  // src/core/client.ts
150
- var SeacloudClient = class {
179
+ var SeacloudClient = class _SeacloudClient {
151
180
  // 保存用户提供的 apiKey
152
- constructor(config = {}) {
153
- this.config = createConfig(config);
154
- this.providedApiKey = config.apiKey;
181
+ constructor(config, providedApiKey) {
182
+ this.config = config;
183
+ this.providedApiKey = providedApiKey;
155
184
  validateConfig(this.config);
156
185
  const isBrowser = typeof globalThis.window !== "undefined" && typeof globalThis.localStorage !== "undefined";
157
186
  if (isBrowser) {
@@ -165,6 +194,15 @@ var SeacloudClient = class {
165
194
  });
166
195
  }
167
196
  }
197
+ /**
198
+ * 创建 SeacloudClient 实例(异步工厂方法)
199
+ * @param config 配置选项
200
+ * @returns SeacloudClient 实例
201
+ */
202
+ static async create(config = {}) {
203
+ const fullConfig = await createConfig(config);
204
+ return new _SeacloudClient(fullConfig, config.apiKey);
205
+ }
168
206
  /**
169
207
  * 创建一个新任务
170
208
  * @param endpoint API 端点路径(例如:/model/tasks)
@@ -289,26 +327,9 @@ async function initSeacloud(apiKeyOrConfig, options) {
289
327
  apiKey = void 0;
290
328
  }
291
329
  if (!config.baseUrl) {
292
- try {
293
- const parentHost = await getHostFromParent(3e3);
294
- if (parentHost) {
295
- const devHostPatterns = ["localhost", "127.0.0.1", ":3000", ":8080", "seaverse.dev"];
296
- const currentHost = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" ? globalThis.window.location.host : "";
297
- const checkIsDev = (host) => devHostPatterns.some((pattern) => host.includes(pattern));
298
- const isDevelopment = checkIsDev(currentHost) || checkIsDev(parentHost);
299
- if (isDevelopment) {
300
- config.baseUrl = "https://proxy-rs.sg.seaverse.dev";
301
- console.log("[SeaCloud SDK] \u68C0\u6D4B\u5230\u5F00\u53D1\u73AF\u5883\uFF0C\u4F7F\u7528\u5F00\u53D1 baseUrl:", config.baseUrl);
302
- } else {
303
- config.baseUrl = "https://proxy-rs.seaverse.ai";
304
- console.log("[SeaCloud SDK] \u68C0\u6D4B\u5230\u6B63\u5F0F\u73AF\u5883\uFF0C\u4F7F\u7528\u6B63\u5F0F baseUrl:", config.baseUrl);
305
- }
306
- }
307
- } catch (error) {
308
- console.warn("[SeaCloud SDK] \u83B7\u53D6\u7236\u9875\u9762 host \u5931\u8D25\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u914D\u7F6E", error);
309
- }
330
+ config.baseUrl = await getBaseUrl();
310
331
  }
311
- globalConfig.client = new SeacloudClient({
332
+ globalConfig.client = await SeacloudClient.create({
312
333
  apiKey: apiKey || "",
313
334
  baseUrl: config.baseUrl,
314
335
  timeout: config.timeout,
@@ -322,11 +343,11 @@ async function initSeacloud(apiKeyOrConfig, options) {
322
343
  }
323
344
  return globalConfig.client;
324
345
  }
325
- function getClient() {
346
+ async function getClient() {
326
347
  if (globalConfig.client) {
327
348
  return globalConfig.client;
328
349
  }
329
- return new SeacloudClient({
350
+ return await SeacloudClient.create({
330
351
  // 不传 apiKey - 让 createConfig() 自动从环境变量/localStorage 读取
331
352
  // 不传 baseUrl - 让 createConfig() 自动从环境变量读取或使用默认值
332
353
  // 不传 timeout - 使用默认 30000ms
@@ -335,7 +356,7 @@ function getClient() {
335
356
 
336
357
  // src/api/llm_chat_completions.ts
337
358
  async function llmChatCompletions(params) {
338
- const client = getClient();
359
+ const client = await getClient();
339
360
  const config = client.getConfig();
340
361
  const url = `${config.baseUrl}/llm/chat/completions`;
341
362
  const token = await getApiToken(config.apiKey);
@@ -419,7 +440,7 @@ async function* parseStreamingResponse(response) {
419
440
 
420
441
  // src/api/agent_chat_completions.ts
421
442
  async function agentChatCompletions(params) {
422
- const client = getClient();
443
+ const client = await getClient();
423
444
  const config = client.getConfig();
424
445
  const url = `${config.baseUrl}/agent/api/v1/chat/completions`;
425
446
  const model = params.model || "custom_openai/vertex-ai-claude-sonnet-4.5";
@@ -621,7 +642,7 @@ function createTextMessage(role, text) {
621
642
 
622
643
  // src/api/app_search.ts
623
644
  async function appSearch(params) {
624
- const client = getClient();
645
+ const client = await getClient();
625
646
  const config = client.getConfig();
626
647
  const url = `${config.baseUrl}/model/v1/template/specs`;
627
648
  const controller = new AbortController();
@@ -666,7 +687,7 @@ async function scan(params) {
666
687
  if (!params.risk_types || params.risk_types.length === 0) {
667
688
  throw new SeacloudError("\u5FC5\u987B\u63D0\u4F9B\u81F3\u5C11\u4E00\u4E2A\u98CE\u9669\u7C7B\u578B");
668
689
  }
669
- const client = getClient();
690
+ const client = await getClient();
670
691
  const config = client.getConfig();
671
692
  const url = `${config.baseUrl}/scan`;
672
693
  const token = await getApiToken(config.apiKey);
@@ -859,7 +880,7 @@ async function testModel(model, options) {
859
880
  console.log(`Base URL: ${baseUrl}`);
860
881
  console.log(`Parameters:`, JSON.stringify(options.params, null, 2));
861
882
  console.log("");
862
- const client = new SeacloudClient({ apiKey, baseUrl });
883
+ const client = await SeacloudClient.create({ apiKey, baseUrl });
863
884
  try {
864
885
  console.log("Creating task...");
865
886
  const task = await client.createTask("/model/v1/generation", {
@@ -1049,7 +1070,7 @@ async function runAppGeneration(args) {
1049
1070
  }
1050
1071
  const apiKey = options.apiKey || process.env.API_SERVICE_TOKEN || "";
1051
1072
  const baseUrl = options.baseUrl || process.env.SEACLOUD_BASE_URL || "http://proxy.sg.seaverse.dev";
1052
- const client = new SeacloudClient({ apiKey, baseUrl });
1073
+ const client = await SeacloudClient.create({ apiKey, baseUrl });
1053
1074
  try {
1054
1075
  console.log("Creating task...");
1055
1076
  const task = await client.createTask("/model/v1/generation", {
@@ -1217,7 +1238,7 @@ async function runTaskStatus(taskId, args) {
1217
1238
  }
1218
1239
  const apiKey = options.apiKey || process.env.API_SERVICE_TOKEN || "";
1219
1240
  const baseUrl = options.baseUrl || process.env.SEACLOUD_BASE_URL || "http://proxy.sg.seaverse.dev";
1220
- const client = new SeacloudClient({ apiKey, baseUrl });
1241
+ const client = await SeacloudClient.create({ apiKey, baseUrl });
1221
1242
  console.log(`Querying task status...`);
1222
1243
  console.log(`Task ID: ${taskId}`);
1223
1244
  console.log(`Base URL: ${baseUrl}