seacloud-sdk 0.12.4 → 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 +56 -50
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.js +239 -196
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -57,19 +57,19 @@ async function getTokenFromParent(timeout = 5e3) {
|
|
|
57
57
|
}
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
|
-
async function
|
|
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:
|
|
66
|
+
if (event.data && event.data.type === "seaverse:env") {
|
|
67
67
|
cleanup();
|
|
68
|
-
const
|
|
69
|
-
resolve(
|
|
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
|
|
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:
|
|
87
|
+
{ type: "seaverse:get_env" },
|
|
88
88
|
"*"
|
|
89
89
|
// 允许任何源,支持跨域场景
|
|
90
90
|
);
|
|
@@ -122,22 +122,34 @@ function checkIsDevelopmentHost(host) {
|
|
|
122
122
|
const devHostPatterns = ["localhost", "127.0.0.1", ":3000", ":8080", "seaverse.dev"];
|
|
123
123
|
return devHostPatterns.some((pattern) => host.includes(pattern));
|
|
124
124
|
}
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
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
137
|
} else {
|
|
138
|
-
|
|
138
|
+
console.log("[SeaCloud SDK] iframe \u73AF\u5883\uFF1A\u4F7F\u7528\u7EBF\u4E0A baseUrl");
|
|
139
|
+
return "https://proxy-rs.seaverse.ai";
|
|
139
140
|
}
|
|
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 = {}) {
|
|
151
|
+
const apiKey = options.apiKey;
|
|
152
|
+
const baseUrl = await getBaseUrl(options.baseUrl);
|
|
141
153
|
const fetchImpl = options.fetch || (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
|
|
142
154
|
if (!fetchImpl) {
|
|
143
155
|
throw new Error("fetch is not available. Please provide a fetch implementation in config or upgrade to Node.js 18+");
|
|
@@ -161,14 +173,14 @@ function validateConfig(config) {
|
|
|
161
173
|
}
|
|
162
174
|
|
|
163
175
|
// src/core/version.ts
|
|
164
|
-
var VERSION = "0.
|
|
176
|
+
var VERSION = "0.12.5";
|
|
165
177
|
|
|
166
178
|
// src/core/client.ts
|
|
167
|
-
var SeacloudClient = class {
|
|
179
|
+
var SeacloudClient = class _SeacloudClient {
|
|
168
180
|
// 保存用户提供的 apiKey
|
|
169
|
-
constructor(config
|
|
170
|
-
this.config =
|
|
171
|
-
this.providedApiKey =
|
|
181
|
+
constructor(config, providedApiKey) {
|
|
182
|
+
this.config = config;
|
|
183
|
+
this.providedApiKey = providedApiKey;
|
|
172
184
|
validateConfig(this.config);
|
|
173
185
|
const isBrowser = typeof globalThis.window !== "undefined" && typeof globalThis.localStorage !== "undefined";
|
|
174
186
|
if (isBrowser) {
|
|
@@ -182,6 +194,15 @@ var SeacloudClient = class {
|
|
|
182
194
|
});
|
|
183
195
|
}
|
|
184
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
|
+
}
|
|
185
206
|
/**
|
|
186
207
|
* 创建一个新任务
|
|
187
208
|
* @param endpoint API 端点路径(例如:/model/tasks)
|
|
@@ -306,24 +327,9 @@ async function initSeacloud(apiKeyOrConfig, options) {
|
|
|
306
327
|
apiKey = void 0;
|
|
307
328
|
}
|
|
308
329
|
if (!config.baseUrl) {
|
|
309
|
-
|
|
310
|
-
const parentHost = await getHostFromParent(3e3);
|
|
311
|
-
if (parentHost) {
|
|
312
|
-
const currentHost = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" ? globalThis.window.location.host : "";
|
|
313
|
-
const isDevelopment = checkIsDevelopmentHost(currentHost) || checkIsDevelopmentHost(parentHost);
|
|
314
|
-
if (isDevelopment) {
|
|
315
|
-
config.baseUrl = "https://proxy-rs.sg.seaverse.dev";
|
|
316
|
-
console.log("[SeaCloud SDK] \u68C0\u6D4B\u5230\u5F00\u53D1\u73AF\u5883\uFF0C\u4F7F\u7528\u5F00\u53D1 baseUrl:", config.baseUrl);
|
|
317
|
-
} else {
|
|
318
|
-
config.baseUrl = "https://proxy-rs.seaverse.ai";
|
|
319
|
-
console.log("[SeaCloud SDK] \u68C0\u6D4B\u5230\u6B63\u5F0F\u73AF\u5883\uFF0C\u4F7F\u7528\u6B63\u5F0F baseUrl:", config.baseUrl);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
} catch (error) {
|
|
323
|
-
console.warn("[SeaCloud SDK] \u83B7\u53D6\u7236\u9875\u9762 host \u5931\u8D25\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u914D\u7F6E", error);
|
|
324
|
-
}
|
|
330
|
+
config.baseUrl = await getBaseUrl();
|
|
325
331
|
}
|
|
326
|
-
globalConfig.client =
|
|
332
|
+
globalConfig.client = await SeacloudClient.create({
|
|
327
333
|
apiKey: apiKey || "",
|
|
328
334
|
baseUrl: config.baseUrl,
|
|
329
335
|
timeout: config.timeout,
|
|
@@ -337,11 +343,11 @@ async function initSeacloud(apiKeyOrConfig, options) {
|
|
|
337
343
|
}
|
|
338
344
|
return globalConfig.client;
|
|
339
345
|
}
|
|
340
|
-
function getClient() {
|
|
346
|
+
async function getClient() {
|
|
341
347
|
if (globalConfig.client) {
|
|
342
348
|
return globalConfig.client;
|
|
343
349
|
}
|
|
344
|
-
return
|
|
350
|
+
return await SeacloudClient.create({
|
|
345
351
|
// 不传 apiKey - 让 createConfig() 自动从环境变量/localStorage 读取
|
|
346
352
|
// 不传 baseUrl - 让 createConfig() 自动从环境变量读取或使用默认值
|
|
347
353
|
// 不传 timeout - 使用默认 30000ms
|
|
@@ -350,7 +356,7 @@ function getClient() {
|
|
|
350
356
|
|
|
351
357
|
// src/api/llm_chat_completions.ts
|
|
352
358
|
async function llmChatCompletions(params) {
|
|
353
|
-
const client = getClient();
|
|
359
|
+
const client = await getClient();
|
|
354
360
|
const config = client.getConfig();
|
|
355
361
|
const url = `${config.baseUrl}/llm/chat/completions`;
|
|
356
362
|
const token = await getApiToken(config.apiKey);
|
|
@@ -434,7 +440,7 @@ async function* parseStreamingResponse(response) {
|
|
|
434
440
|
|
|
435
441
|
// src/api/agent_chat_completions.ts
|
|
436
442
|
async function agentChatCompletions(params) {
|
|
437
|
-
const client = getClient();
|
|
443
|
+
const client = await getClient();
|
|
438
444
|
const config = client.getConfig();
|
|
439
445
|
const url = `${config.baseUrl}/agent/api/v1/chat/completions`;
|
|
440
446
|
const model = params.model || "custom_openai/vertex-ai-claude-sonnet-4.5";
|
|
@@ -636,7 +642,7 @@ function createTextMessage(role, text) {
|
|
|
636
642
|
|
|
637
643
|
// src/api/app_search.ts
|
|
638
644
|
async function appSearch(params) {
|
|
639
|
-
const client = getClient();
|
|
645
|
+
const client = await getClient();
|
|
640
646
|
const config = client.getConfig();
|
|
641
647
|
const url = `${config.baseUrl}/model/v1/template/specs`;
|
|
642
648
|
const controller = new AbortController();
|
|
@@ -681,7 +687,7 @@ async function scan(params) {
|
|
|
681
687
|
if (!params.risk_types || params.risk_types.length === 0) {
|
|
682
688
|
throw new SeacloudError("\u5FC5\u987B\u63D0\u4F9B\u81F3\u5C11\u4E00\u4E2A\u98CE\u9669\u7C7B\u578B");
|
|
683
689
|
}
|
|
684
|
-
const client = getClient();
|
|
690
|
+
const client = await getClient();
|
|
685
691
|
const config = client.getConfig();
|
|
686
692
|
const url = `${config.baseUrl}/scan`;
|
|
687
693
|
const token = await getApiToken(config.apiKey);
|
|
@@ -874,7 +880,7 @@ async function testModel(model, options) {
|
|
|
874
880
|
console.log(`Base URL: ${baseUrl}`);
|
|
875
881
|
console.log(`Parameters:`, JSON.stringify(options.params, null, 2));
|
|
876
882
|
console.log("");
|
|
877
|
-
const client =
|
|
883
|
+
const client = await SeacloudClient.create({ apiKey, baseUrl });
|
|
878
884
|
try {
|
|
879
885
|
console.log("Creating task...");
|
|
880
886
|
const task = await client.createTask("/model/v1/generation", {
|
|
@@ -1064,7 +1070,7 @@ async function runAppGeneration(args) {
|
|
|
1064
1070
|
}
|
|
1065
1071
|
const apiKey = options.apiKey || process.env.API_SERVICE_TOKEN || "";
|
|
1066
1072
|
const baseUrl = options.baseUrl || process.env.SEACLOUD_BASE_URL || "http://proxy.sg.seaverse.dev";
|
|
1067
|
-
const client =
|
|
1073
|
+
const client = await SeacloudClient.create({ apiKey, baseUrl });
|
|
1068
1074
|
try {
|
|
1069
1075
|
console.log("Creating task...");
|
|
1070
1076
|
const task = await client.createTask("/model/v1/generation", {
|
|
@@ -1232,7 +1238,7 @@ async function runTaskStatus(taskId, args) {
|
|
|
1232
1238
|
}
|
|
1233
1239
|
const apiKey = options.apiKey || process.env.API_SERVICE_TOKEN || "";
|
|
1234
1240
|
const baseUrl = options.baseUrl || process.env.SEACLOUD_BASE_URL || "http://proxy.sg.seaverse.dev";
|
|
1235
|
-
const client =
|
|
1241
|
+
const client = await SeacloudClient.create({ apiKey, baseUrl });
|
|
1236
1242
|
console.log(`Querying task status...`);
|
|
1237
1243
|
console.log(`Task ID: ${taskId}`);
|
|
1238
1244
|
console.log(`Base URL: ${baseUrl}
|