@blueking/ai-ui-sdk 0.0.14 → 0.0.15-beta.1
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/common/chart-helper.d.ts +1 -1
- package/dist/common/chart-helper.js +173 -0
- package/dist/common/util.d.ts +21 -0
- package/dist/common/util.js +143 -0
- package/dist/css/fonts/iconcool.eot +0 -0
- package/dist/css/fonts/iconcool.svg +53 -0
- package/dist/css/fonts/iconcool.ttf +0 -0
- package/dist/css/fonts/iconcool.woff +0 -0
- package/dist/css/iconcool.js +10 -0
- package/dist/css/style.css +43 -0
- package/dist/hooks/use-chat.d.ts +843 -18
- package/dist/hooks/use-chat.js +533 -0
- package/dist/hooks/use-click-proxy.js +77 -0
- package/dist/hooks/use-http.d.ts +15 -0
- package/dist/hooks/use-http.js +48 -0
- package/dist/hooks/use-reference-doc.d.ts +1 -1
- package/dist/hooks/use-reference-doc.js +185 -0
- package/dist/hooks/use-style.js +11 -0
- package/dist/hooks/use-summary.d.ts +1 -1
- package/dist/hooks/use-summary.js +55 -0
- package/dist/hooks/use-think.js +135 -0
- package/dist/http/fetch/error-interceptor.d.ts +3 -0
- package/dist/http/fetch/error-interceptor.js +6 -0
- package/dist/http/fetch/index.d.ts +25 -0
- package/dist/http/fetch/request-error.d.ts +6 -0
- package/dist/http/fetch/request-error.js +25 -0
- package/dist/http/fetch/success-interceptor.d.ts +3 -0
- package/dist/http/fetch/success-interceptor.js +92 -0
- package/dist/http/helper/knowledge.d.ts +43 -0
- package/dist/http/helper/knowledge.js +227 -0
- package/dist/http/helper/permission.d.ts +7 -0
- package/dist/http/helper/permission.js +28 -0
- package/dist/http/helper/session.d.ts +31 -0
- package/dist/http/helper/session.js +147 -0
- package/dist/http/helper/tool.d.ts +13 -0
- package/dist/http/helper/tool.js +49 -0
- package/dist/main.d.ts +12 -2
- package/dist/main.js +22 -239
- package/dist/types/enum.d.ts +36 -0
- package/dist/types/enum.js +101 -0
- package/dist/types/file.d.ts +13 -0
- package/dist/types/file.js +1 -0
- package/dist/types/knowledge.d.ts +133 -0
- package/dist/types/knowledge.js +1 -0
- package/dist/types/permission.d.ts +48 -0
- package/dist/types/permission.js +1 -0
- package/dist/types/{type.d.ts → session.d.ts} +84 -23
- package/dist/types/session.js +1 -0
- package/dist/types/tool.d.ts +60 -0
- package/dist/types/tool.js +1 -0
- package/package.json +3 -3
- package/dist/common/type-transform.d.ts +0 -7
@@ -0,0 +1,185 @@
|
|
1
|
+
import { onBeforeMount, onBeforeUnmount } from 'vue';
|
2
|
+
/**
|
3
|
+
* 获取引用资料的 HTML 内容
|
4
|
+
* @param documents
|
5
|
+
* @returns
|
6
|
+
*/ export const getHtmlContentFromDocuments = (documents)=>{
|
7
|
+
let htmlContent = `<section class="knowledge-head click-close"><svg class="ai-ui-sdk-wenzhang"><use href="#ai-ui-sdk-wenzhang"></use></svg>找到 ${documents.length} 篇资料参考<i class="ai-ui-sdk-icon ai-ui-sdk-angle-up"></i></section><ul class="knowledge-body">`;
|
8
|
+
documents.forEach((document1)=>{
|
9
|
+
const { path, file_path: filePath, display_name: displayName, preview_path: previewPath } = document1.metadata;
|
10
|
+
const title = displayName || filePath.split('/').pop();
|
11
|
+
htmlContent += `<li
|
12
|
+
class="knowledge-item"
|
13
|
+
>
|
14
|
+
<i class="ai-ui-sdk-icon ai-ui-sdk-zhishiku"></i>
|
15
|
+
<a href="${previewPath}" title="${title} (${previewPath})" target="_blank" class="knowledge-link g-flex-truncate">
|
16
|
+
${title}
|
17
|
+
</a>
|
18
|
+
<a href="${path}" title="预览原文" target="_blank" class="knowledge-link hover-show">
|
19
|
+
<i class="ai-ui-sdk-icon ai-ui-sdk-yanjing-kejian"></i>
|
20
|
+
</a>
|
21
|
+
</li>`;
|
22
|
+
});
|
23
|
+
htmlContent += '</ul>';
|
24
|
+
return htmlContent;
|
25
|
+
};
|
26
|
+
/**
|
27
|
+
* 移除引用资料的 HTML 内容
|
28
|
+
* @param content
|
29
|
+
* @returns
|
30
|
+
*/ export const removeReferenceDoc = (content)=>{
|
31
|
+
return content.replace(/<section class="knowledge-head click-close">[\s\S]*?<\/section>/, '').replace(/<ul class="knowledge-body">[\s\S]*?<\/ul>/, '').replace(/<section class="knowledge-tips">[\s\S]*?<\/section>/, '');
|
32
|
+
};
|
33
|
+
export const useReferenceDoc = ()=>{
|
34
|
+
let styleEle = null;
|
35
|
+
const addReferenceDocStyle = ()=>{
|
36
|
+
// 旧版知识样式
|
37
|
+
const oldStyle = `.knowledge-tips {
|
38
|
+
position: relative;
|
39
|
+
padding: 6px 8px;
|
40
|
+
margin-bottom: 14px;
|
41
|
+
line-height: 22px;
|
42
|
+
background: #f5f7fa;
|
43
|
+
border-radius: 4px;
|
44
|
+
|
45
|
+
&.closed {
|
46
|
+
.bkaidev-angle-up {
|
47
|
+
transform: rotate(180deg);
|
48
|
+
}
|
49
|
+
|
50
|
+
.knowledge-summary {
|
51
|
+
margin-bottom: 0;
|
52
|
+
}
|
53
|
+
|
54
|
+
.knowledge-link {
|
55
|
+
display: none;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
.bkaidev-angle-up {
|
60
|
+
position: absolute;
|
61
|
+
top: 4px;
|
62
|
+
right: 0px;
|
63
|
+
font-size: 22px;
|
64
|
+
color: #979ba5;
|
65
|
+
cursor: pointer;
|
66
|
+
}
|
67
|
+
|
68
|
+
.bkaidev-help-document {
|
69
|
+
margin-right: 4px;
|
70
|
+
font-size: 19px;
|
71
|
+
color: #3a84ff;
|
72
|
+
}
|
73
|
+
|
74
|
+
.knowledge-summary {
|
75
|
+
display: block;
|
76
|
+
margin-bottom: 4px;
|
77
|
+
color: #313238;
|
78
|
+
}
|
79
|
+
|
80
|
+
.knowledge-link {
|
81
|
+
display: block;
|
82
|
+
color: #3a84ff;
|
83
|
+
text-decoration: none;
|
84
|
+
cursor: pointer;
|
85
|
+
|
86
|
+
.bkaidev-cc-jump-link {
|
87
|
+
font-size: 14px;
|
88
|
+
}
|
89
|
+
|
90
|
+
&:last-child {
|
91
|
+
margin-bottom: 2px;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}`;
|
95
|
+
// 新版知识样式
|
96
|
+
const newStyle = `.knowledge-head {
|
97
|
+
display: flex;
|
98
|
+
align-items: center;
|
99
|
+
padding: 0 8px 0 6px;
|
100
|
+
margin-bottom: 8px;
|
101
|
+
line-height: 28px;
|
102
|
+
background: #F0F1F5;
|
103
|
+
border-radius: 4px;
|
104
|
+
font-size: 12px;
|
105
|
+
color: #313238;
|
106
|
+
cursor: pointer;
|
107
|
+
width: fit-content;
|
108
|
+
|
109
|
+
.ai-ui-sdk-wenzhang {
|
110
|
+
width: 14px;
|
111
|
+
height: 14px;
|
112
|
+
margin-right: 6px;
|
113
|
+
}
|
114
|
+
|
115
|
+
.ai-ui-sdk-angle-up {
|
116
|
+
font-size: 22px;
|
117
|
+
color: #4D4F56;
|
118
|
+
margin-left: 4px;
|
119
|
+
}
|
120
|
+
|
121
|
+
&.closed {
|
122
|
+
margin-bottom: 16px;
|
123
|
+
|
124
|
+
.ai-ui-sdk-angle-up {
|
125
|
+
transform: rotate(180deg);
|
126
|
+
}
|
127
|
+
|
128
|
+
&~ .knowledge-body {
|
129
|
+
display: none;
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
&:hover {
|
134
|
+
background: #EAEBF0;
|
135
|
+
}
|
136
|
+
}
|
137
|
+
.knowledge-body {
|
138
|
+
background: #F5F7FA;
|
139
|
+
padding: 16px 0 !important;
|
140
|
+
.knowledge-item {
|
141
|
+
padding-left: 10px;
|
142
|
+
display: flex;
|
143
|
+
align-items: center;
|
144
|
+
line-height: 28px;
|
145
|
+
.hover-show {
|
146
|
+
display: none;
|
147
|
+
}
|
148
|
+
a {
|
149
|
+
margin-right: 10px;
|
150
|
+
&:has(i) {
|
151
|
+
padding: 0 10px;
|
152
|
+
margin-right: 0;
|
153
|
+
text-decoration: none !important;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
.ai-ui-sdk-zhishiku {
|
157
|
+
color: #D66F6B;
|
158
|
+
font-size: 14px;
|
159
|
+
margin-right: 6px;
|
160
|
+
}
|
161
|
+
&:hover {
|
162
|
+
background: #EAEBF0;
|
163
|
+
.hover-show {
|
164
|
+
display: inline;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
}`;
|
169
|
+
styleEle = document.createElement('style');
|
170
|
+
styleEle.textContent = oldStyle + newStyle;
|
171
|
+
document.head.appendChild(styleEle);
|
172
|
+
};
|
173
|
+
const removeReferenceDocStyle = ()=>{
|
174
|
+
if (styleEle) {
|
175
|
+
styleEle.remove();
|
176
|
+
styleEle = null;
|
177
|
+
}
|
178
|
+
};
|
179
|
+
onBeforeMount(()=>{
|
180
|
+
addReferenceDocStyle();
|
181
|
+
});
|
182
|
+
onBeforeUnmount(()=>{
|
183
|
+
removeReferenceDocStyle();
|
184
|
+
});
|
185
|
+
};
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { useReferenceDoc } from './use-reference-doc.js';
|
2
|
+
import { useThink } from './use-think.js';
|
3
|
+
import '../css/style.css';
|
4
|
+
import '../css/iconcool.js';
|
5
|
+
// ai 样式。注意:全局引入一次即可
|
6
|
+
export const useStyle = ()=>{
|
7
|
+
// 引用资料
|
8
|
+
useReferenceDoc();
|
9
|
+
// 思考
|
10
|
+
useThink();
|
11
|
+
};
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { SummaryCallbacks } from '../types/
|
1
|
+
import type { SummaryCallbacks } from '../types/session';
|
2
2
|
export declare const useSummary: ({ handleStart, handleEnd, handleError, }?: SummaryCallbacks) => {
|
3
3
|
summary: ({ content, url, headers, model, }: {
|
4
4
|
content: string;
|
@@ -0,0 +1,55 @@
|
|
1
|
+
import { SessionPromptRole } from '../types/enum.js';
|
2
|
+
import { removeReferenceDoc } from './use-reference-doc.js';
|
3
|
+
import { removeThink } from './use-think.js';
|
4
|
+
import { ChatHelper } from '../common/chart-helper.js';
|
5
|
+
// ai 总结
|
6
|
+
export const useSummary = ({ handleStart, handleEnd, handleError } = {})=>{
|
7
|
+
let summaryText = '';
|
8
|
+
const handleStartChat = ()=>{
|
9
|
+
summaryText = '';
|
10
|
+
return handleStart === null || handleStart === void 0 ? void 0 : handleStart();
|
11
|
+
};
|
12
|
+
const handleTextChat = (sessionCode, message, cover)=>{
|
13
|
+
if (cover || summaryText === '正在思考...') {
|
14
|
+
summaryText = message;
|
15
|
+
} else {
|
16
|
+
summaryText += message;
|
17
|
+
}
|
18
|
+
};
|
19
|
+
const handleEndChat = ()=>{
|
20
|
+
if (summaryText === '无话可说') {
|
21
|
+
summaryText = '';
|
22
|
+
}
|
23
|
+
return handleEnd === null || handleEnd === void 0 ? void 0 : handleEnd(summaryText);
|
24
|
+
};
|
25
|
+
const handleErrorChat = (sessionCode, message, code)=>{
|
26
|
+
return handleError === null || handleError === void 0 ? void 0 : handleError(message, code);
|
27
|
+
};
|
28
|
+
const chatHelper = new ChatHelper({
|
29
|
+
handleStart: handleStartChat,
|
30
|
+
handleText: handleTextChat,
|
31
|
+
handleEnd: handleEndChat,
|
32
|
+
handleError: handleErrorChat
|
33
|
+
});
|
34
|
+
const summary = ({ content, url, headers, model })=>{
|
35
|
+
const filterContent = removeThink(removeReferenceDoc(content));
|
36
|
+
const prompts = [
|
37
|
+
{
|
38
|
+
role: SessionPromptRole.User,
|
39
|
+
content: `你是一个总结大师,请帮助我对下面这段话进行总结。要求总结精炼,不超过 15 个字!且末尾没有标点符号!请注意:如果你无法总结,请回复“无话可说”!\n文字如下:\n${filterContent}`
|
40
|
+
}
|
41
|
+
];
|
42
|
+
chatHelper.stream({
|
43
|
+
sessionCode: 'summary',
|
44
|
+
url,
|
45
|
+
data: {
|
46
|
+
prompts,
|
47
|
+
model
|
48
|
+
},
|
49
|
+
headers
|
50
|
+
});
|
51
|
+
};
|
52
|
+
return {
|
53
|
+
summary
|
54
|
+
};
|
55
|
+
};
|
@@ -0,0 +1,135 @@
|
|
1
|
+
import { onBeforeMount, onBeforeUnmount } from 'vue';
|
2
|
+
import { durationFormatter } from '../common/util.js';
|
3
|
+
/**
|
4
|
+
* 获取思考的 HTML 内容
|
5
|
+
* @param chatContent 聊天内容
|
6
|
+
* @param content 思考内容
|
7
|
+
* @param elapsedTime 思考时间
|
8
|
+
* @returns 完整的 chatContent
|
9
|
+
*/ export const getHtmlContentFromThink = (chatContent, content, cover, elapsedTime)=>{
|
10
|
+
let htmlContent = chatContent === '内容正在生成中...' || cover ? '' : chatContent;
|
11
|
+
// 思考开始
|
12
|
+
if (!htmlContent.includes('<section class="think-head click-close">')) {
|
13
|
+
htmlContent += `<section class="think-head click-close">
|
14
|
+
<i class="ai-ui-sdk-icon ai-ui-sdk-sikao"></i>思考中...<i class="ai-ui-sdk-icon ai-ui-sdk-angle-up"></i>
|
15
|
+
</section>
|
16
|
+
<section class="think-body">
|
17
|
+
${content}
|
18
|
+
</section>`;
|
19
|
+
} else {
|
20
|
+
// 补充思考内容
|
21
|
+
const thinkBodyMatch = htmlContent.match(/<section class="think-body">([\s\S]*?)<\/section>/);
|
22
|
+
if (thinkBodyMatch) {
|
23
|
+
// 原有思考内容
|
24
|
+
const thinkContent = thinkBodyMatch[1];
|
25
|
+
// 在最后的换行符前面添加新的思考内容
|
26
|
+
const newThinkContent = thinkContent.replace(/\n$/g, `${content}\n`);
|
27
|
+
htmlContent = htmlContent.replace(thinkContent, newThinkContent);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
// 思考结束
|
31
|
+
if (elapsedTime) {
|
32
|
+
htmlContent = htmlContent.replace('思考中...', `已完成思考 (耗时:${durationFormatter(elapsedTime)})`);
|
33
|
+
}
|
34
|
+
return htmlContent;
|
35
|
+
};
|
36
|
+
/**
|
37
|
+
* 移除思考的 HTML 内容
|
38
|
+
* @param content
|
39
|
+
* @returns
|
40
|
+
*/ export const removeThink = (content)=>{
|
41
|
+
// 移除思考头部
|
42
|
+
const afterRemoveThinkHead = content.replace(/<section class="think-head click-close">[\s\S]*<\/section>/, '');
|
43
|
+
// 移除思考内容
|
44
|
+
let afterRemoveThinkBody = afterRemoveThinkHead.replace(/<section class="think-body">[\s\S]*<\/section>/, '');
|
45
|
+
// 如果内容只有思考,那就给ai传递思考内容
|
46
|
+
if (afterRemoveThinkBody.trim() === '') {
|
47
|
+
const thinkBodyMatch = content.match(/<section class="think-body">([\s\S]*?)<\/section>/);
|
48
|
+
if (thinkBodyMatch) {
|
49
|
+
[, afterRemoveThinkBody] = thinkBodyMatch;
|
50
|
+
}
|
51
|
+
}
|
52
|
+
return afterRemoveThinkBody;
|
53
|
+
};
|
54
|
+
/**
|
55
|
+
* 判断是否是思考中
|
56
|
+
* @param content
|
57
|
+
* @returns
|
58
|
+
*/ export const isThinking = (content)=>{
|
59
|
+
// 是否开始思考
|
60
|
+
const isStartThinking = content === '正在思考...';
|
61
|
+
// 判断是否空思考
|
62
|
+
const thinkBodyMatch = content.match(/<section class="think-body">([\s\S]*?)<\/section>$/);
|
63
|
+
const isEmptyThinking = thinkBodyMatch && thinkBodyMatch[1].trim() === '';
|
64
|
+
return isStartThinking || isEmptyThinking;
|
65
|
+
};
|
66
|
+
// 思考
|
67
|
+
export const useThink = ()=>{
|
68
|
+
let styleEle = null;
|
69
|
+
const addThinkStyle = ()=>{
|
70
|
+
const style = `.think-head {
|
71
|
+
display: flex;
|
72
|
+
align-items: center;
|
73
|
+
padding: 0 8px 0 6px;
|
74
|
+
margin-bottom: 8px;
|
75
|
+
line-height: 28px;
|
76
|
+
background: #F0F1F5;
|
77
|
+
border-radius: 4px;
|
78
|
+
font-size: 12px;
|
79
|
+
color: #313238;
|
80
|
+
cursor: pointer;
|
81
|
+
width: fit-content;
|
82
|
+
.ai-ui-sdk-sikao {
|
83
|
+
font-size: 14px;
|
84
|
+
color: #979ba5;
|
85
|
+
margin-right: 4px;
|
86
|
+
}
|
87
|
+
|
88
|
+
.ai-ui-sdk-angle-up {
|
89
|
+
font-size: 22px;
|
90
|
+
color: #4D4F56;
|
91
|
+
margin-left: 4px;
|
92
|
+
}
|
93
|
+
|
94
|
+
&.closed {
|
95
|
+
margin-bottom: 16px;
|
96
|
+
|
97
|
+
.ai-ui-sdk-angle-up {
|
98
|
+
transform: rotate(180deg);
|
99
|
+
}
|
100
|
+
|
101
|
+
&~ .think-body {
|
102
|
+
display: none;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
&:hover {
|
107
|
+
background: #EAEBF0;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
.think-body {
|
111
|
+
background: #F5F7FA;
|
112
|
+
color: #979BA5;
|
113
|
+
padding: 16px;
|
114
|
+
margin-bottom: 16px;
|
115
|
+
&~ .knowledge-head {
|
116
|
+
margin-bottom: 8px;
|
117
|
+
}
|
118
|
+
}`;
|
119
|
+
styleEle = document.createElement('style');
|
120
|
+
styleEle.textContent = style;
|
121
|
+
document.head.appendChild(styleEle);
|
122
|
+
};
|
123
|
+
const removeThinkStyle = ()=>{
|
124
|
+
if (styleEle) {
|
125
|
+
styleEle.remove();
|
126
|
+
styleEle = null;
|
127
|
+
}
|
128
|
+
};
|
129
|
+
onBeforeMount(()=>{
|
130
|
+
addThinkStyle();
|
131
|
+
});
|
132
|
+
onBeforeUnmount(()=>{
|
133
|
+
removeThinkStyle();
|
134
|
+
});
|
135
|
+
};
|
@@ -0,0 +1,25 @@
|
|
1
|
+
export interface IFetchConfig extends RequestInit {
|
2
|
+
requestType?: 'json' | 'formData';
|
3
|
+
responseType?: 'json' | 'text' | 'formData' | 'file' | 'blob';
|
4
|
+
globalError?: Boolean;
|
5
|
+
signal?: AbortSignal;
|
6
|
+
withoutSpace?: boolean;
|
7
|
+
noCheckPermission?: boolean;
|
8
|
+
}
|
9
|
+
type HttpMethod = <T>(url: string, payload?: any, config?: IFetchConfig) => Promise<T>;
|
10
|
+
interface IHttp {
|
11
|
+
get: HttpMethod;
|
12
|
+
post: HttpMethod;
|
13
|
+
put: HttpMethod;
|
14
|
+
delete: HttpMethod;
|
15
|
+
head: HttpMethod;
|
16
|
+
options: HttpMethod;
|
17
|
+
patch: HttpMethod;
|
18
|
+
}
|
19
|
+
declare const http: IHttp;
|
20
|
+
declare const withAbort: <R, T extends any[]>(fn: (signal: AbortSignal, ...args: T) => Promise<R>) => {
|
21
|
+
(...args: T): Promise<R>;
|
22
|
+
abort(): void | undefined;
|
23
|
+
};
|
24
|
+
export default http;
|
25
|
+
export { withAbort };
|
@@ -0,0 +1,25 @@
|
|
1
|
+
function _define_property(obj, key, value) {
|
2
|
+
if (key in obj) {
|
3
|
+
Object.defineProperty(obj, key, {
|
4
|
+
value: value,
|
5
|
+
enumerable: true,
|
6
|
+
configurable: true,
|
7
|
+
writable: true
|
8
|
+
});
|
9
|
+
} else {
|
10
|
+
obj[key] = value;
|
11
|
+
}
|
12
|
+
return obj;
|
13
|
+
}
|
14
|
+
class RequestError extends Error {
|
15
|
+
constructor(code, message, response){
|
16
|
+
super();
|
17
|
+
_define_property(this, "code", void 0);
|
18
|
+
_define_property(this, "message", void 0);
|
19
|
+
_define_property(this, "response", void 0);
|
20
|
+
this.code = code;
|
21
|
+
this.message = message;
|
22
|
+
this.response = response;
|
23
|
+
}
|
24
|
+
}
|
25
|
+
export { RequestError as default };
|
@@ -0,0 +1,92 @@
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
2
|
+
try {
|
3
|
+
var info = gen[key](arg);
|
4
|
+
var value = info.value;
|
5
|
+
} catch (error) {
|
6
|
+
reject(error);
|
7
|
+
return;
|
8
|
+
}
|
9
|
+
if (info.done) {
|
10
|
+
resolve(value);
|
11
|
+
} else {
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
13
|
+
}
|
14
|
+
}
|
15
|
+
function _async_to_generator(fn) {
|
16
|
+
return function() {
|
17
|
+
var self = this, args = arguments;
|
18
|
+
return new Promise(function(resolve, reject) {
|
19
|
+
var gen = fn.apply(self, args);
|
20
|
+
function _next(value) {
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
22
|
+
}
|
23
|
+
function _throw(err) {
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
25
|
+
}
|
26
|
+
_next(undefined);
|
27
|
+
});
|
28
|
+
};
|
29
|
+
}
|
30
|
+
import RequestError from './request-error.js';
|
31
|
+
// 处理数据
|
32
|
+
const handleData = function() {
|
33
|
+
var _ref = _async_to_generator(function*(response, config) {
|
34
|
+
const { code = response.code, data, message = response.statusText } = yield response[config.responseType]();
|
35
|
+
if (response.ok) {
|
36
|
+
// 对应 HTTP 请求的状态码 200 到 299
|
37
|
+
// 校验接口返回的数据,status 为 success 表示业务成功
|
38
|
+
switch(code){
|
39
|
+
// 接口请求成功
|
40
|
+
case 'success':
|
41
|
+
case 0:
|
42
|
+
return Promise.resolve(data);
|
43
|
+
// 后端业务处理报错
|
44
|
+
default:
|
45
|
+
throw new RequestError(code, message || '系统错误', data);
|
46
|
+
}
|
47
|
+
} else {
|
48
|
+
// 处理 http 非 200 异常
|
49
|
+
throw new RequestError(code || -1, message || '系统错误', data);
|
50
|
+
}
|
51
|
+
});
|
52
|
+
return function handleData(response, config) {
|
53
|
+
return _ref.apply(this, arguments);
|
54
|
+
};
|
55
|
+
}();
|
56
|
+
// 处理文件;
|
57
|
+
const handleFile = function() {
|
58
|
+
var _ref = _async_to_generator(function*(response) {
|
59
|
+
if (!response.ok) {
|
60
|
+
throw new RequestError(response.status || -1, response.statusText || '系统错误');
|
61
|
+
}
|
62
|
+
const reader = response.body.getReader();
|
63
|
+
const stream = new ReadableStream({
|
64
|
+
start (controller) {
|
65
|
+
return pump();
|
66
|
+
function pump() {
|
67
|
+
return reader.read().then(({ done, value })=>{
|
68
|
+
// When no more data needs to be consumed, close the stream
|
69
|
+
if (done) {
|
70
|
+
controller.close();
|
71
|
+
return;
|
72
|
+
}
|
73
|
+
// Enqueue the next data chunk into our target stream
|
74
|
+
controller.enqueue(value);
|
75
|
+
return pump();
|
76
|
+
});
|
77
|
+
}
|
78
|
+
}
|
79
|
+
});
|
80
|
+
return new Response(stream).blob();
|
81
|
+
});
|
82
|
+
return function handleFile(response) {
|
83
|
+
return _ref.apply(this, arguments);
|
84
|
+
};
|
85
|
+
}();
|
86
|
+
// 请求成功执行拦截器
|
87
|
+
export default ((response, config)=>{
|
88
|
+
if (config.responseType === 'file') {
|
89
|
+
return handleFile(response);
|
90
|
+
}
|
91
|
+
return handleData(response, config);
|
92
|
+
});
|
@@ -0,0 +1,43 @@
|
|
1
|
+
import type { IKnowledge, IKnowledgeApi, IKnowledgebase, IKnowledgebaseApi } from '@/types/knowledge';
|
2
|
+
/**
|
3
|
+
* 从路径中获取文件信息
|
4
|
+
* @param path 路径
|
5
|
+
* @returns 文件路径和文件名
|
6
|
+
*/
|
7
|
+
export declare const getFileInfoFromPath: (path: string) => {
|
8
|
+
fileName: string | undefined;
|
9
|
+
filePath: string | undefined;
|
10
|
+
};
|
11
|
+
/**
|
12
|
+
* ui 转换为接口使用
|
13
|
+
* @param data IKnowledge
|
14
|
+
* @returns IKnowledgeApi
|
15
|
+
*/
|
16
|
+
export declare const transferKnowledge: (data: IKnowledge) => IKnowledgeApi;
|
17
|
+
/**
|
18
|
+
* 接口转换为ui使用
|
19
|
+
* @param data IKnowledgeApi
|
20
|
+
* @returns IKnowledge
|
21
|
+
*/
|
22
|
+
export declare const transferKnowledgeApi: (data: IKnowledgeApi) => IKnowledge;
|
23
|
+
/**
|
24
|
+
* 接口文件夹转换为knowledgbase
|
25
|
+
*/
|
26
|
+
export declare const transferKnowledgeApi2Knowledgebase: (knowledge: IKnowledgeApi, knowledgebase?: IKnowledgebase) => IKnowledgebase;
|
27
|
+
/**
|
28
|
+
* ui 转换为接口使用
|
29
|
+
* @param data IKnowledgebase
|
30
|
+
* @returns IKnowledgebaseApi
|
31
|
+
*/
|
32
|
+
export declare const transferKnowledgebase: (data: IKnowledgebase) => IKnowledgebaseApi;
|
33
|
+
/**
|
34
|
+
* 转换 ui 文件夹为 IKnowledgeApi,接口使用
|
35
|
+
* @param knowledgebase IKnowledgebase
|
36
|
+
*/
|
37
|
+
export declare const transferKnowledgebase2KnowledgeApi: (data: IKnowledgebase) => IKnowledgeApi;
|
38
|
+
/**
|
39
|
+
* 接口转换为ui使用
|
40
|
+
* @param data IKnowledgebaseApi
|
41
|
+
* @returns IKnowledgebase
|
42
|
+
*/
|
43
|
+
export declare const transferKnowledgebaseApi: (data: IKnowledgebaseApi) => IKnowledgebase;
|