@lark-apaas/client-toolkit 1.2.9-beta.3 → 1.2.9-beta.31
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/lib/antd-table.d.ts +2 -2
- package/lib/apis/aily-chat.d.ts +2 -0
- package/lib/apis/aily-chat.js +2 -0
- package/lib/apis/hooks/useScrollReveal.d.ts +1 -0
- package/lib/apis/hooks/useScrollReveal.js +1 -0
- package/lib/apis/utils/resolveAppUrl.d.ts +1 -0
- package/lib/apis/utils/resolveAppUrl.js +2 -0
- package/lib/apis/utils/scopedStorage.d.ts +1 -0
- package/lib/apis/utils/scopedStorage.js +2 -0
- package/lib/components/AppContainer/api-proxy/core.js +2 -1
- package/lib/components/AppContainer/index.d.ts +5 -1
- package/lib/components/AppContainer/index.js +14 -27
- package/lib/components/AppContainer/safety.js +5 -2
- package/lib/components/AppContainer/utils/childApi.js +1 -0
- package/lib/components/AppContainer/utils/getLarkUser.js +4 -2
- package/lib/components/AppContainer/utils/observable.js +7 -1
- package/lib/components/AppContainer/utils/tea.js +1 -1
- package/lib/components/ErrorRender/index.js +5 -11
- package/lib/components/User/UserSelect.js +1 -13
- package/lib/components/theme/index.d.ts +0 -1
- package/lib/components/theme/index.js +0 -1
- package/lib/components/theme/util.d.ts +0 -2
- package/lib/components/theme/util.js +0 -108
- package/lib/components/ui/toast.d.ts +2 -0
- package/lib/components/ui/toast.js +53 -0
- package/lib/hooks/index.d.ts +1 -0
- package/lib/hooks/index.js +1 -0
- package/lib/hooks/useCurrentUserProfile.js +22 -29
- package/lib/hooks/useLogout.js +2 -17
- package/lib/hooks/useScrollReveal.d.ts +61 -0
- package/lib/hooks/useScrollReveal.js +37 -0
- package/lib/index.js +12 -1
- package/lib/integrations/dataloom.d.ts +3 -1
- package/lib/integrations/dataloom.js +18 -10
- package/lib/integrations/services/DepartmentService.js +3 -2
- package/lib/integrations/services/UserProfileService.js +3 -2
- package/lib/integrations/services/UserService.js +4 -3
- package/lib/integrations/services/types.d.ts +1 -0
- package/lib/logger/intercept-global-error.js +16 -14
- package/lib/logger/log-types.d.ts +4 -4
- package/lib/logger/log-types.js +1 -1
- package/lib/runtime/index.d.ts +1 -0
- package/lib/runtime/index.js +1 -0
- package/lib/runtime/react-devtools-hook.d.ts +19 -0
- package/lib/runtime/react-devtools-hook.js +20 -0
- package/lib/theme-layer.css +2 -1
- package/lib/utils/apiPath.d.ts +5 -0
- package/lib/utils/apiPath.js +5 -0
- package/lib/utils/axiosConfig.js +6 -5
- package/lib/utils/getAppId.d.ts +2 -4
- package/lib/utils/getAppId.js +2 -9
- package/lib/utils/getInitialInfo.d.ts +4 -3
- package/lib/utils/getInitialInfo.js +17 -8
- package/lib/utils/getUserProfile.js +4 -12
- package/lib/utils/hmr-api.d.ts +39 -0
- package/lib/utils/hmr-api.js +36 -0
- package/lib/utils/module-hot.d.ts +9 -5
- package/lib/utils/module-hot.js +9 -10
- package/lib/utils/postMessage.js +18 -0
- package/lib/utils/requestManager.js +1 -3
- package/lib/utils/resolveAppUrl.d.ts +27 -0
- package/lib/utils/resolveAppUrl.js +19 -0
- package/lib/utils/scopedStorage.d.ts +5 -0
- package/lib/utils/scopedStorage.js +46 -0
- package/package.json +12 -6
- package/lib/apis/tools/generateImage.d.ts +0 -1
- package/lib/apis/tools/generateImage.js +0 -1
- package/lib/apis/tools/generateTextStream.d.ts +0 -1
- package/lib/apis/tools/generateTextStream.js +0 -1
- package/lib/components/theme/ui-config.d.ts +0 -1
- package/lib/components/theme/ui-config.js +0 -2
- package/lib/integrations/generateImage.d.ts +0 -1
- package/lib/integrations/generateImage.js +0 -47
- package/lib/integrations/generateTextStream.d.ts +0 -21
- package/lib/integrations/generateTextStream.js +0 -98
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
function createWebpackHmrApi(hot) {
|
|
2
|
+
return {
|
|
3
|
+
onSuccess (callback) {
|
|
4
|
+
let lastStatus = null;
|
|
5
|
+
const handler = (status)=>{
|
|
6
|
+
if ('idle' === status && 'apply' === lastStatus) try {
|
|
7
|
+
callback();
|
|
8
|
+
} catch (e) {
|
|
9
|
+
console.error('[HMR] Success callback error:', e);
|
|
10
|
+
}
|
|
11
|
+
lastStatus = status;
|
|
12
|
+
};
|
|
13
|
+
hot.addStatusHandler(handler);
|
|
14
|
+
return ()=>hot.removeStatusHandler(handler);
|
|
15
|
+
},
|
|
16
|
+
onError (callback) {
|
|
17
|
+
const handler = (status)=>{
|
|
18
|
+
if ('fail' === status || 'abort' === status) try {
|
|
19
|
+
callback(new Error(`HMR ${status}`));
|
|
20
|
+
} catch (e) {
|
|
21
|
+
console.error('[HMR] Error callback error:', e);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
hot.addStatusHandler(handler);
|
|
25
|
+
return ()=>hot.removeStatusHandler(handler);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function getHmrApi() {
|
|
30
|
+
if ('production' === process.env.NODE_ENV) return null;
|
|
31
|
+
if (import.meta.webpackHot) return createWebpackHmrApi(import.meta.webpackHot);
|
|
32
|
+
'undefined' != typeof module && module.hot;
|
|
33
|
+
if (window.__VITE_HMR__) return window.__VITE_HMR__;
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
export { getHmrApi };
|
|
@@ -10,16 +10,20 @@
|
|
|
10
10
|
* - abort: An update was aborted, but the system is still in its previous state
|
|
11
11
|
* - fail: An update has thrown an exception and the system's state has been compromised
|
|
12
12
|
*/
|
|
13
|
-
type ModuleHotType = 'idle' | 'check' | 'prepare' | 'ready' | 'dispose' | 'apply' | 'abort' | 'fail';
|
|
14
|
-
interface ModuleHotInstance {
|
|
13
|
+
export type ModuleHotType = 'idle' | 'check' | 'prepare' | 'ready' | 'dispose' | 'apply' | 'abort' | 'fail';
|
|
14
|
+
export interface ModuleHotInstance {
|
|
15
15
|
addStatusHandler: (handler: (status: ModuleHotType) => void) => void;
|
|
16
16
|
removeStatusHandler: (handler: (status: ModuleHotType) => void) => void;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* 获取 Webpack HMR 实例
|
|
20
|
+
* 仅支持 Webpack/Rspack 的 HMR API
|
|
21
|
+
*/
|
|
18
22
|
export declare function getModuleHot(): ModuleHotInstance | null;
|
|
19
23
|
/**
|
|
20
24
|
* 创建模块热更成功处理函数
|
|
21
|
-
*
|
|
22
|
-
*
|
|
25
|
+
* 监听模块热更状态,检测 apply -> idle 转换表示热更成功
|
|
26
|
+
*
|
|
27
|
+
* @param callback 热更回调函数,参数为是否成功和当前状态
|
|
23
28
|
*/
|
|
24
29
|
export declare function createApplyHandle(callback: (success: boolean, status: ModuleHotType) => void): (status: ModuleHotType) => void;
|
|
25
|
-
export {};
|
package/lib/utils/module-hot.js
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
function getModuleHot() {
|
|
2
2
|
if ('production' === process.env.NODE_ENV) return null;
|
|
3
|
-
|
|
3
|
+
if (import.meta.webpackHot) return import.meta.webpackHot;
|
|
4
|
+
'undefined' != typeof module && module.hot;
|
|
5
|
+
return null;
|
|
4
6
|
}
|
|
5
7
|
function createApplyHandle(callback) {
|
|
6
|
-
let
|
|
8
|
+
let lastStatus = null;
|
|
7
9
|
return (status)=>{
|
|
8
10
|
if ('fail' === status || 'abort' === status) {
|
|
9
|
-
|
|
11
|
+
lastStatus = status;
|
|
10
12
|
return callback(false, status);
|
|
11
13
|
}
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
callback(true, status);
|
|
16
|
-
}
|
|
17
|
-
return;
|
|
14
|
+
if ('idle' === status && 'apply' === lastStatus) {
|
|
15
|
+
lastStatus = status;
|
|
16
|
+
return callback(true, status);
|
|
18
17
|
}
|
|
19
|
-
|
|
18
|
+
lastStatus = status;
|
|
20
19
|
};
|
|
21
20
|
}
|
|
22
21
|
export { createApplyHandle, getModuleHot };
|
package/lib/utils/postMessage.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
const PARENT_ORIGIN_KEY = '__parentOrigin';
|
|
2
|
+
function getParentOriginFromParams() {
|
|
3
|
+
try {
|
|
4
|
+
const params = new URLSearchParams(window.location.search);
|
|
5
|
+
const origin = params.get(PARENT_ORIGIN_KEY);
|
|
6
|
+
if (origin) {
|
|
7
|
+
sessionStorage.setItem(PARENT_ORIGIN_KEY, origin);
|
|
8
|
+
return origin;
|
|
9
|
+
}
|
|
10
|
+
} catch {}
|
|
11
|
+
try {
|
|
12
|
+
return sessionStorage.getItem(PARENT_ORIGIN_KEY) || void 0;
|
|
13
|
+
} catch {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
1
17
|
function getLegacyParentOrigin() {
|
|
2
18
|
const { origin } = window.location;
|
|
3
19
|
if (origin.includes('force.feishuapp.net')) return 'https://force.feishu.cn';
|
|
@@ -8,6 +24,8 @@ function getLegacyParentOrigin() {
|
|
|
8
24
|
return 'https://miaoda.feishu-boe.cn';
|
|
9
25
|
}
|
|
10
26
|
function resolveParentOrigin() {
|
|
27
|
+
const paramOrigin = getParentOriginFromParams();
|
|
28
|
+
if (paramOrigin) return paramOrigin;
|
|
11
29
|
return process.env?.FORCE_FRAMEWORK_DOMAIN_MAIN ?? getLegacyParentOrigin();
|
|
12
30
|
}
|
|
13
31
|
function submitPostMessage(message, targetOrigin) {
|
|
@@ -4,9 +4,7 @@ async function fetchUserProfilesByIds(ids) {
|
|
|
4
4
|
try {
|
|
5
5
|
const dataloom = await getDataloom();
|
|
6
6
|
if (!dataloom) throw new Error('Dataloom client is unavailable');
|
|
7
|
-
const
|
|
8
|
-
if (0 === numericIds.length) return [];
|
|
9
|
-
const response = await dataloom.service.user.getByIds(numericIds);
|
|
7
|
+
const response = await dataloom.service.user.getByIds(ids);
|
|
10
8
|
return Array.isArray(response?.data) ? response.data : response?.data?.user_list || [];
|
|
11
9
|
} catch (error) {
|
|
12
10
|
console.error(`Failed to fetch profiles for user IDs ${ids.join(', ')}:`, error);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 解析应用内路径,自动补全 CLIENT_BASE_PATH 前缀,返回完整 URL
|
|
3
|
+
* 适用于生成分享链接、二维码等需要完整 URL 的场景
|
|
4
|
+
*
|
|
5
|
+
* - 相对路径:补全前缀 + 当前域名
|
|
6
|
+
* - 当前域名的完整 URL:修正路径部分,补全前缀
|
|
7
|
+
* - 其他域名的 URL:原样返回,不做处理
|
|
8
|
+
*
|
|
9
|
+
* @param path - 应用内相对路径或完整 URL
|
|
10
|
+
* @returns 完整的 URL 字符串
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // 假设 CLIENT_BASE_PATH = '/app/abc',当前域名为 https://example.com
|
|
14
|
+
*
|
|
15
|
+
* resolveAppUrl('/detail/123')
|
|
16
|
+
* // => 'https://example.com/app/abc/detail/123'
|
|
17
|
+
*
|
|
18
|
+
* resolveAppUrl('https://example.com/detail/123')
|
|
19
|
+
* // => 'https://example.com/app/abc/detail/123'
|
|
20
|
+
*
|
|
21
|
+
* resolveAppUrl('https://example.com/app/abc/detail/123')
|
|
22
|
+
* // => 'https://example.com/app/abc/detail/123' (已有前缀,不重复添加)
|
|
23
|
+
*
|
|
24
|
+
* resolveAppUrl('https://other-site.com/page')
|
|
25
|
+
* // => 'https://other-site.com/page' (非当前域名,原样返回)
|
|
26
|
+
*/
|
|
27
|
+
export declare function resolveAppUrl(path: string): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { normalizeBasePath } from "./utils.js";
|
|
2
|
+
function ensureBasePath(pathname, basePath) {
|
|
3
|
+
if (!basePath) return pathname;
|
|
4
|
+
if (pathname.startsWith(basePath)) return pathname;
|
|
5
|
+
return `${basePath}${pathname.startsWith('/') ? '' : '/'}${pathname}`;
|
|
6
|
+
}
|
|
7
|
+
function resolveAppUrl(path) {
|
|
8
|
+
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
|
9
|
+
try {
|
|
10
|
+
const url = new URL(path);
|
|
11
|
+
if (url.origin !== window.location.origin) return path;
|
|
12
|
+
url.pathname = ensureBasePath(url.pathname, basePath);
|
|
13
|
+
return url.toString();
|
|
14
|
+
} catch {}
|
|
15
|
+
const normalizedPath = path.startsWith('/') ? path : `/${path}`;
|
|
16
|
+
const resolvedPath = ensureBasePath(normalizedPath, basePath);
|
|
17
|
+
return `${window.location.origin}${resolvedPath}`;
|
|
18
|
+
}
|
|
19
|
+
export { resolveAppUrl };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getAppId } from "./getAppId.js";
|
|
2
|
+
function getPrefix() {
|
|
3
|
+
const appId = getAppId();
|
|
4
|
+
const namespace = appId || '__global__';
|
|
5
|
+
return `__miaoda_${namespace}__:`;
|
|
6
|
+
}
|
|
7
|
+
function getScopedKeys() {
|
|
8
|
+
const prefix = getPrefix();
|
|
9
|
+
const keys = [];
|
|
10
|
+
for(let i = 0; i < localStorage.length; i++){
|
|
11
|
+
const key = localStorage.key(i);
|
|
12
|
+
if (key && key.startsWith(prefix)) keys.push(key.substring(prefix.length));
|
|
13
|
+
}
|
|
14
|
+
return keys;
|
|
15
|
+
}
|
|
16
|
+
const scopedStorage = {
|
|
17
|
+
getItem (key) {
|
|
18
|
+
const prefixedKey = getPrefix() + key;
|
|
19
|
+
return localStorage.getItem(prefixedKey);
|
|
20
|
+
},
|
|
21
|
+
setItem (key, value) {
|
|
22
|
+
const prefixedKey = getPrefix() + key;
|
|
23
|
+
localStorage.setItem(prefixedKey, value);
|
|
24
|
+
},
|
|
25
|
+
removeItem (key) {
|
|
26
|
+
const prefixedKey = getPrefix() + key;
|
|
27
|
+
localStorage.removeItem(prefixedKey);
|
|
28
|
+
},
|
|
29
|
+
clear () {
|
|
30
|
+
const prefix = getPrefix();
|
|
31
|
+
const keysToRemove = [];
|
|
32
|
+
for(let i = 0; i < localStorage.length; i++){
|
|
33
|
+
const key = localStorage.key(i);
|
|
34
|
+
if (key && key.startsWith(prefix)) keysToRemove.push(key);
|
|
35
|
+
}
|
|
36
|
+
keysToRemove.forEach((key)=>localStorage.removeItem(key));
|
|
37
|
+
},
|
|
38
|
+
key (index) {
|
|
39
|
+
const keys = getScopedKeys();
|
|
40
|
+
return keys[index] || null;
|
|
41
|
+
},
|
|
42
|
+
get length () {
|
|
43
|
+
return getScopedKeys().length;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
export { scopedStorage };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/client-toolkit",
|
|
3
|
-
"version": "1.2.9-beta.
|
|
3
|
+
"version": "1.2.9-beta.31",
|
|
4
4
|
"types": "./lib/index.d.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -71,6 +71,11 @@
|
|
|
71
71
|
"require": "./lib/apis/utils/*.js",
|
|
72
72
|
"types": "./lib/apis/utils/*.d.ts"
|
|
73
73
|
},
|
|
74
|
+
"./aily-chat": {
|
|
75
|
+
"import": "./lib/apis/aily-chat.js",
|
|
76
|
+
"require": "./lib/apis/aily-chat.js",
|
|
77
|
+
"types": "./lib/apis/aily-chat.d.ts"
|
|
78
|
+
},
|
|
74
79
|
"./auth": {
|
|
75
80
|
"import": "./lib/auth.js",
|
|
76
81
|
"require": "./lib/auth.js",
|
|
@@ -93,11 +98,12 @@
|
|
|
93
98
|
"dependencies": {
|
|
94
99
|
"@ant-design/colors": "^7.2.1",
|
|
95
100
|
"@ant-design/cssinjs": "^1.24.0",
|
|
96
|
-
"@data-loom/js": "0.4.
|
|
97
|
-
"@lark-apaas/
|
|
98
|
-
"@lark-apaas/
|
|
99
|
-
"@lark-apaas/
|
|
100
|
-
"@lark-apaas/
|
|
101
|
+
"@data-loom/js": "0.4.9",
|
|
102
|
+
"@lark-apaas/aily-web-sdk": "0.0.2-beta.8",
|
|
103
|
+
"@lark-apaas/auth-sdk": "0.1.0-beta.2",
|
|
104
|
+
"@lark-apaas/client-capability": "0.1.3-beta.7",
|
|
105
|
+
"@lark-apaas/miaoda-inspector": "^1.0.16",
|
|
106
|
+
"@lark-apaas/observable-web": "1.0.1-beta.3",
|
|
101
107
|
"@radix-ui/react-avatar": "^1.1.10",
|
|
102
108
|
"@radix-ui/react-popover": "^1.1.15",
|
|
103
109
|
"@radix-ui/react-slot": "^1.2.3",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '../../integrations/generateImage';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "../../integrations/generateImage.js";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '../../integrations/generateTextStream';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "../../integrations/generateTextStream.js";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { defaultUIConfig, type UIComponentConfig, } from '@lark-apaas/miaoda-inspector';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function generateImage(prompt: string, size?: string, headers?: Record<string, string>): Promise<any>;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { getAppId } from "../utils/getAppId.js";
|
|
2
|
-
import { getCsrfToken } from "../utils/getCsrfToken.js";
|
|
3
|
-
import { getEnvPath } from "../utils/getEnvPath.js";
|
|
4
|
-
import { isSparkRuntime } from "../utils/utils.js";
|
|
5
|
-
async function generateImage(prompt, size = '1024x1024', headers = {}) {
|
|
6
|
-
const appId = getAppId(window.location.pathname);
|
|
7
|
-
if (!appId) return {
|
|
8
|
-
code: 1,
|
|
9
|
-
msg: 'appId is required',
|
|
10
|
-
data: {}
|
|
11
|
-
};
|
|
12
|
-
const defaultHeaders = {
|
|
13
|
-
'Content-Type': 'application/json'
|
|
14
|
-
};
|
|
15
|
-
const mergedHeaders = {
|
|
16
|
-
...defaultHeaders,
|
|
17
|
-
...headers,
|
|
18
|
-
'X-Kunlun-Token': window.token,
|
|
19
|
-
'x-miaoda-token': window.MIAODA_BUILTIN_TTT,
|
|
20
|
-
'x-lgw-csrf-token': window.lgw_csrf_token,
|
|
21
|
-
...window.CSRF_HEADERS || {}
|
|
22
|
-
};
|
|
23
|
-
if (isSparkRuntime()) {
|
|
24
|
-
mergedHeaders['X-Suda-Csrf-Token'] = getCsrfToken();
|
|
25
|
-
const response = await fetch(`${window.location.origin}/spark/b/${appId}/text2image`, {
|
|
26
|
-
method: 'POST',
|
|
27
|
-
headers: mergedHeaders,
|
|
28
|
-
credentials: 'include',
|
|
29
|
-
body: JSON.stringify({
|
|
30
|
-
prompt,
|
|
31
|
-
size
|
|
32
|
-
})
|
|
33
|
-
});
|
|
34
|
-
return await response.json();
|
|
35
|
-
}
|
|
36
|
-
const response = await fetch(`${window.location.origin}/ai/api/${getEnvPath()}/v1/apps/${appId}/text2image`, {
|
|
37
|
-
method: 'POST',
|
|
38
|
-
headers: mergedHeaders,
|
|
39
|
-
credentials: 'include',
|
|
40
|
-
body: JSON.stringify({
|
|
41
|
-
prompt,
|
|
42
|
-
size
|
|
43
|
-
})
|
|
44
|
-
});
|
|
45
|
-
return await response.json();
|
|
46
|
-
}
|
|
47
|
-
export { generateImage };
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
interface GenerateTextOptions {
|
|
2
|
-
text: string;
|
|
3
|
-
thinking_type?: 'enabled' | 'disabled';
|
|
4
|
-
headers?: Record<string, string>;
|
|
5
|
-
}
|
|
6
|
-
interface GenerateTextResult {
|
|
7
|
-
content: string;
|
|
8
|
-
reasoning_content: string;
|
|
9
|
-
success: boolean;
|
|
10
|
-
error?: string;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* 文生文 - 流式版本
|
|
14
|
-
* 支持实时接收生成内容的回调
|
|
15
|
-
*/
|
|
16
|
-
export declare function generateTextStream(options: GenerateTextOptions, onChunk?: (chunk: {
|
|
17
|
-
content: string;
|
|
18
|
-
reasoning_content: string;
|
|
19
|
-
finished: boolean;
|
|
20
|
-
}) => void): Promise<GenerateTextResult>;
|
|
21
|
-
export {};
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { getAppId } from "../utils/getAppId.js";
|
|
2
|
-
import { getCsrfToken } from "../utils/getCsrfToken.js";
|
|
3
|
-
import { getEnvPath } from "../utils/getEnvPath.js";
|
|
4
|
-
import { isSparkRuntime } from "../utils/utils.js";
|
|
5
|
-
async function generateTextStream(options, onChunk) {
|
|
6
|
-
const { text, thinking_type = 'disabled', headers = {} } = options;
|
|
7
|
-
const appId = getAppId(window.location.pathname);
|
|
8
|
-
if (!appId) return {
|
|
9
|
-
content: '',
|
|
10
|
-
reasoning_content: '',
|
|
11
|
-
success: false,
|
|
12
|
-
error: 'appId is required'
|
|
13
|
-
};
|
|
14
|
-
try {
|
|
15
|
-
const mergedHeaders = {
|
|
16
|
-
'Content-Type': 'application/json',
|
|
17
|
-
...headers,
|
|
18
|
-
'X-Kunlun-Token': window.token,
|
|
19
|
-
'x-miaoda-token': window.MIAODA_BUILTIN_TTT,
|
|
20
|
-
'x-lgw-csrf-token': window.lgw_csrf_token,
|
|
21
|
-
...window.CSRF_HEADERS || {}
|
|
22
|
-
};
|
|
23
|
-
let response;
|
|
24
|
-
if (isSparkRuntime()) {
|
|
25
|
-
mergedHeaders['X-Suda-Csrf-Token'] = getCsrfToken();
|
|
26
|
-
response = await fetch(`${window.location.origin}/spark/b/${appId}/text/generate`, {
|
|
27
|
-
method: 'POST',
|
|
28
|
-
headers: mergedHeaders,
|
|
29
|
-
credentials: 'include',
|
|
30
|
-
body: JSON.stringify({
|
|
31
|
-
text,
|
|
32
|
-
thinking_type
|
|
33
|
-
})
|
|
34
|
-
});
|
|
35
|
-
} else response = await fetch(`${window.location.origin}/ai/api/${getEnvPath()}/v1/apps/${appId}/text/generate`, {
|
|
36
|
-
method: 'POST',
|
|
37
|
-
headers: mergedHeaders,
|
|
38
|
-
credentials: 'include',
|
|
39
|
-
body: JSON.stringify({
|
|
40
|
-
text,
|
|
41
|
-
thinking_type
|
|
42
|
-
})
|
|
43
|
-
});
|
|
44
|
-
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
|
45
|
-
const reader = response.body?.getReader();
|
|
46
|
-
if (!reader) throw new Error('无法获取响应流');
|
|
47
|
-
let fullContent = '';
|
|
48
|
-
let fullReasoningContent = '';
|
|
49
|
-
const decoder = new TextDecoder();
|
|
50
|
-
while(true){
|
|
51
|
-
const { done, value } = await reader.read();
|
|
52
|
-
if (done) break;
|
|
53
|
-
const chunk = decoder.decode(value, {
|
|
54
|
-
stream: true
|
|
55
|
-
});
|
|
56
|
-
const lines = chunk.split('\n').filter((line)=>line.trim());
|
|
57
|
-
for (const line of lines)if (line.startsWith('data: ')) {
|
|
58
|
-
const jsonStr = line.slice(6);
|
|
59
|
-
try {
|
|
60
|
-
const data = JSON.parse(jsonStr);
|
|
61
|
-
if (0 !== data.code) return {
|
|
62
|
-
content: '',
|
|
63
|
-
reasoning_content: '',
|
|
64
|
-
success: false,
|
|
65
|
-
error: data.msg || '生成失败'
|
|
66
|
-
};
|
|
67
|
-
if (data.data.content) fullContent += data.data.content;
|
|
68
|
-
if (data.data.reasoning_content) fullReasoningContent += data.data.reasoning_content;
|
|
69
|
-
if (onChunk) onChunk({
|
|
70
|
-
content: data.data.content || '',
|
|
71
|
-
reasoning_content: data.data.reasoning_content || '',
|
|
72
|
-
finished: data.finished
|
|
73
|
-
});
|
|
74
|
-
if (data.finished) return {
|
|
75
|
-
content: fullContent,
|
|
76
|
-
reasoning_content: fullReasoningContent,
|
|
77
|
-
success: true
|
|
78
|
-
};
|
|
79
|
-
} catch (parseError) {
|
|
80
|
-
console.error(`解析JSON失败: ${jsonStr}`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return {
|
|
85
|
-
content: fullContent,
|
|
86
|
-
reasoning_content: fullReasoningContent,
|
|
87
|
-
success: true
|
|
88
|
-
};
|
|
89
|
-
} catch (error) {
|
|
90
|
-
return {
|
|
91
|
-
content: '',
|
|
92
|
-
reasoning_content: '',
|
|
93
|
-
success: false,
|
|
94
|
-
error: error instanceof Error ? error.message : '未知错误'
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
export { generateTextStream };
|