@blueking/ai-blueking 0.1.4 → 0.1.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/README.md +24 -6
- package/dist/chat-helper.js +38 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -64,13 +64,29 @@ npm i @blueking/ai-blueking
|
|
|
64
64
|
|
|
65
65
|
// 错误处理
|
|
66
66
|
const handleError = (message: string, code: string, id: number | string) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
if (message.includes('user authentication failed')) {
|
|
68
|
+
// 未登录,跳转登录
|
|
69
|
+
const loginUrl = new URL(process.env.BK_LOGIN_URL);
|
|
70
|
+
loginUrl.searchParams.append('c_url', location.origin);
|
|
71
|
+
window.location.href = loginUrl.href;
|
|
72
|
+
} else {
|
|
73
|
+
// 处理错误消息
|
|
74
|
+
const currentMessage = messages.value.at(-1);
|
|
75
|
+
currentMessage.status = 'fail';
|
|
76
|
+
currentMessage.content = message;
|
|
77
|
+
loading.value = false;
|
|
78
|
+
}
|
|
71
79
|
};
|
|
72
80
|
|
|
73
|
-
|
|
81
|
+
// 需要将 <网关名> 替换成插件部署后生成的网关名
|
|
82
|
+
const prefix = process.env.BK_API_URL_TMPL.replace('{api_name}', '<网关名>').replace('http', 'https');
|
|
83
|
+
const chatHelper = new ChatHelper(
|
|
84
|
+
`${prefix}/prod/bk_plugin/plugin_api/assistant/`,
|
|
85
|
+
handleStart,
|
|
86
|
+
handleReceiveMessage,
|
|
87
|
+
handleEnd,
|
|
88
|
+
handleError,
|
|
89
|
+
);
|
|
74
90
|
|
|
75
91
|
// 清空消息
|
|
76
92
|
const handleClear = () => {
|
|
@@ -79,6 +95,8 @@ npm i @blueking/ai-blueking
|
|
|
79
95
|
|
|
80
96
|
// 发送消息
|
|
81
97
|
const handleSend = (message: string) => {
|
|
98
|
+
// 记录当前消息记录
|
|
99
|
+
const chatHistory = [...messages.value];
|
|
82
100
|
// 添加一条消息
|
|
83
101
|
messages.value.push({
|
|
84
102
|
role: 'user',
|
|
@@ -89,7 +107,7 @@ npm i @blueking/ai-blueking
|
|
|
89
107
|
{
|
|
90
108
|
inputs: {
|
|
91
109
|
input: message,
|
|
92
|
-
chat_history:
|
|
110
|
+
chat_history: chatHistory,
|
|
93
111
|
},
|
|
94
112
|
},
|
|
95
113
|
1,
|
package/dist/chat-helper.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
1
|
+
var b = Object.defineProperty;
|
|
2
|
+
var f = (r, t, e) => t in r ? b(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
|
|
3
|
+
var n = (r, t, e) => (f(r, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
const m = (r) => {
|
|
5
5
|
try {
|
|
6
|
-
return JSON.parse(
|
|
6
|
+
return JSON.parse(r), !0;
|
|
7
7
|
} catch {
|
|
8
8
|
return !1;
|
|
9
9
|
}
|
|
10
10
|
};
|
|
11
|
-
class
|
|
12
|
-
constructor(t, e,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.url = t, this.handleStart = e, this.handleReceiveMessage =
|
|
11
|
+
class y {
|
|
12
|
+
constructor(t, e, a, o, h) {
|
|
13
|
+
n(this, "controllerMap");
|
|
14
|
+
n(this, "handleEnd");
|
|
15
|
+
n(this, "handleError");
|
|
16
|
+
n(this, "handleReceiveMessage");
|
|
17
|
+
n(this, "handleStart");
|
|
18
|
+
n(this, "url");
|
|
19
|
+
this.url = t, this.handleStart = e, this.handleReceiveMessage = a, this.handleEnd = o, this.handleError = h, this.controllerMap = {};
|
|
20
20
|
}
|
|
21
21
|
stop(t) {
|
|
22
|
-
var e,
|
|
23
|
-
return (
|
|
22
|
+
var e, a;
|
|
23
|
+
return (a = (e = this.controllerMap[t]) == null ? void 0 : e.abort) == null || a.call(e), this.handleEnd(t);
|
|
24
24
|
}
|
|
25
25
|
async stream(t, e) {
|
|
26
|
-
const
|
|
27
|
-
this.controllerMap[e] =
|
|
26
|
+
const a = new AbortController();
|
|
27
|
+
this.controllerMap[e] = a, fetch(this.url, {
|
|
28
28
|
method: "post",
|
|
29
|
-
signal:
|
|
29
|
+
signal: a.signal,
|
|
30
30
|
headers: {
|
|
31
31
|
"Content-Type": "application/json"
|
|
32
32
|
},
|
|
@@ -37,26 +37,33 @@ class m {
|
|
|
37
37
|
const h = o.body.pipeThrough(new window.TextDecoderStream()).getReader();
|
|
38
38
|
for (; ; )
|
|
39
39
|
try {
|
|
40
|
-
const { value:
|
|
41
|
-
if (
|
|
40
|
+
const { value: s, done: d } = await h.read();
|
|
41
|
+
if (d)
|
|
42
42
|
break;
|
|
43
|
-
|
|
44
|
-
`).forEach((
|
|
45
|
-
const l =
|
|
46
|
-
if (
|
|
47
|
-
const { event:
|
|
48
|
-
|
|
43
|
+
s.toString().split(`
|
|
44
|
+
`).forEach((p) => {
|
|
45
|
+
const l = p.trim();
|
|
46
|
+
if (m(l)) {
|
|
47
|
+
const { event: u, content: g, result: E, code: c, message: i } = JSON.parse(l);
|
|
48
|
+
if (E === !1) {
|
|
49
|
+
this.handleError(i, c, e);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
switch (u) {
|
|
49
53
|
case "text":
|
|
50
|
-
this.handleReceiveMessage(
|
|
54
|
+
this.handleReceiveMessage(g, e);
|
|
51
55
|
break;
|
|
52
56
|
case "done":
|
|
53
57
|
this.handleEnd(e);
|
|
54
58
|
break;
|
|
59
|
+
case "error":
|
|
60
|
+
this.handleError(i, c, e);
|
|
61
|
+
break;
|
|
55
62
|
}
|
|
56
63
|
}
|
|
57
64
|
});
|
|
58
|
-
} catch (
|
|
59
|
-
(
|
|
65
|
+
} catch (s) {
|
|
66
|
+
(s == null ? void 0 : s.code) !== 20 && this.handleError(s.message, s.code, e);
|
|
60
67
|
break;
|
|
61
68
|
}
|
|
62
69
|
}).catch((o) => {
|
|
@@ -65,5 +72,5 @@ class m {
|
|
|
65
72
|
}
|
|
66
73
|
}
|
|
67
74
|
export {
|
|
68
|
-
|
|
75
|
+
y as ChatHelper
|
|
69
76
|
};
|