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 +59 -9
- package/dist/cli.js.map +1 -1
- package/dist/index.js +59 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -9,7 +9,51 @@ var SeacloudError = class extends Error {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
// src/core/config.ts
|
|
12
|
-
function
|
|
12
|
+
function isInIframe() {
|
|
13
|
+
try {
|
|
14
|
+
return typeof globalThis.window !== "undefined" && globalThis.window.self !== globalThis.window.top;
|
|
15
|
+
} catch (e) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
async function getTokenFromParent(timeout = 5e3) {
|
|
20
|
+
if (!isInIframe()) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return new Promise((resolve) => {
|
|
24
|
+
const messageHandler = (event) => {
|
|
25
|
+
if (event.data && event.data.type === "seaverse:token") {
|
|
26
|
+
cleanup();
|
|
27
|
+
const token = event.data.payload?.accessToken;
|
|
28
|
+
resolve(token || null);
|
|
29
|
+
} else if (event.data && event.data.type === "seaverse:error") {
|
|
30
|
+
cleanup();
|
|
31
|
+
console.warn("[SeaCloud SDK] Error getting token from parent:", event.data.error);
|
|
32
|
+
resolve(null);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const timeoutId = setTimeout(() => {
|
|
36
|
+
cleanup();
|
|
37
|
+
resolve(null);
|
|
38
|
+
}, timeout);
|
|
39
|
+
const cleanup = () => {
|
|
40
|
+
clearTimeout(timeoutId);
|
|
41
|
+
globalThis.window.removeEventListener("message", messageHandler);
|
|
42
|
+
};
|
|
43
|
+
globalThis.window.addEventListener("message", messageHandler);
|
|
44
|
+
try {
|
|
45
|
+
globalThis.window.parent.postMessage(
|
|
46
|
+
{ type: "seaverse:get_token" },
|
|
47
|
+
"*"
|
|
48
|
+
// 允许任何源,支持跨域场景
|
|
49
|
+
);
|
|
50
|
+
} catch (e) {
|
|
51
|
+
cleanup();
|
|
52
|
+
resolve(null);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async function getApiToken(providedApiKey) {
|
|
13
57
|
if (providedApiKey) {
|
|
14
58
|
return providedApiKey;
|
|
15
59
|
}
|
|
@@ -25,19 +69,24 @@ function getApiToken(providedApiKey) {
|
|
|
25
69
|
if (typeof process !== "undefined" && process.env?.API_SERVICE_TOKEN) {
|
|
26
70
|
return process.env.API_SERVICE_TOKEN;
|
|
27
71
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
72
|
+
if (typeof globalThis.window !== "undefined") {
|
|
73
|
+
const parentToken = await getTokenFromParent();
|
|
74
|
+
if (parentToken) {
|
|
75
|
+
return parentToken;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return "";
|
|
31
79
|
}
|
|
32
80
|
function createConfig(options = {}) {
|
|
33
|
-
const apiKey =
|
|
81
|
+
const apiKey = options.apiKey;
|
|
34
82
|
const baseUrl = options.baseUrl || (typeof process !== "undefined" ? process.env?.SEACLOUD_BASE_URL : void 0) || "https://proxy-rs.seaverse.ai";
|
|
35
83
|
const fetchImpl = options.fetch || (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
|
|
36
84
|
if (!fetchImpl) {
|
|
37
85
|
throw new Error("fetch is not available. Please provide a fetch implementation in config or upgrade to Node.js 18+");
|
|
38
86
|
}
|
|
39
87
|
return {
|
|
40
|
-
apiKey,
|
|
88
|
+
apiKey: apiKey || "",
|
|
89
|
+
// 提供默认空字符串,实际请求时会动态获取
|
|
41
90
|
baseUrl,
|
|
42
91
|
fetch: fetchImpl,
|
|
43
92
|
timeout: options.timeout || 3e4
|
|
@@ -82,7 +131,7 @@ var SeacloudClient = class {
|
|
|
82
131
|
*/
|
|
83
132
|
async createTask(endpoint, body) {
|
|
84
133
|
const url = `${this.config.baseUrl}${endpoint}`;
|
|
85
|
-
const currentToken = getApiToken(this.providedApiKey);
|
|
134
|
+
const currentToken = await getApiToken(this.providedApiKey);
|
|
86
135
|
const controller = new AbortController();
|
|
87
136
|
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
88
137
|
try {
|
|
@@ -129,7 +178,7 @@ var SeacloudClient = class {
|
|
|
129
178
|
*/
|
|
130
179
|
async getTaskStatus(endpoint, taskId) {
|
|
131
180
|
const url = `${this.config.baseUrl}${endpoint}/task/${taskId}`;
|
|
132
|
-
const currentToken = getApiToken(this.providedApiKey);
|
|
181
|
+
const currentToken = await getApiToken(this.providedApiKey);
|
|
133
182
|
const controller = new AbortController();
|
|
134
183
|
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
135
184
|
try {
|
|
@@ -5081,6 +5130,7 @@ async function llmChatCompletions(params) {
|
|
|
5081
5130
|
const client = getClient();
|
|
5082
5131
|
const config = client.getConfig();
|
|
5083
5132
|
const url = `${config.baseUrl}/llm/chat/completions`;
|
|
5133
|
+
const token = await getApiToken(config.apiKey);
|
|
5084
5134
|
const controller = new AbortController();
|
|
5085
5135
|
const timeoutId = setTimeout(() => controller.abort(), config.timeout);
|
|
5086
5136
|
try {
|
|
@@ -5088,7 +5138,7 @@ async function llmChatCompletions(params) {
|
|
|
5088
5138
|
method: "POST",
|
|
5089
5139
|
headers: {
|
|
5090
5140
|
"Content-Type": "application/json",
|
|
5091
|
-
"Authorization": `Bearer ${
|
|
5141
|
+
"Authorization": `Bearer ${token}`
|
|
5092
5142
|
},
|
|
5093
5143
|
body: JSON.stringify(params),
|
|
5094
5144
|
signal: controller.signal
|