@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 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
- const currentMessage = messages.value.at(-1);
68
- currentMessage.status = 'error';
69
- currentMessage.content = message;
70
- loading.value = false;
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
- const chatHelper = new ChatHelper('<接口地址>', handleStart, handleReceiveMessage, handleEnd, handleError);
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: messages.value,
110
+ chat_history: chatHistory,
93
111
  },
94
112
  },
95
113
  1,
@@ -1,32 +1,32 @@
1
- var u = Object.defineProperty;
2
- var g = (a, t, e) => t in a ? u(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
- var s = (a, t, e) => (g(a, typeof t != "symbol" ? t + "" : t, e), e);
4
- const E = (a) => {
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(a), !0;
6
+ return JSON.parse(r), !0;
7
7
  } catch {
8
8
  return !1;
9
9
  }
10
10
  };
11
- class m {
12
- constructor(t, e, r, o, h) {
13
- s(this, "controllerMap");
14
- s(this, "handleEnd");
15
- s(this, "handleError");
16
- s(this, "handleReceiveMessage");
17
- s(this, "handleStart");
18
- s(this, "url");
19
- this.url = t, this.handleStart = e, this.handleReceiveMessage = r, this.handleEnd = o, this.handleError = h, this.controllerMap = {};
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, r;
23
- return (r = (e = this.controllerMap[t]) == null ? void 0 : e.abort) == null || r.call(e), this.handleEnd(t);
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 r = new AbortController();
27
- this.controllerMap[e] = r, fetch(this.url, {
26
+ const a = new AbortController();
27
+ this.controllerMap[e] = a, fetch(this.url, {
28
28
  method: "post",
29
- signal: r.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: n, done: c } = await h.read();
41
- if (c)
40
+ const { value: s, done: d } = await h.read();
41
+ if (d)
42
42
  break;
43
- n.toString().split(`
44
- `).forEach((i) => {
45
- const l = i.trim();
46
- if (E(l)) {
47
- const { event: d, content: p } = JSON.parse(l);
48
- switch (d) {
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(p, e);
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 (n) {
59
- (n == null ? void 0 : n.code) !== 20 && this.handleError(n.message, n.code, e);
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
- m as ChatHelper
75
+ y as ChatHelper
69
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueking/ai-blueking",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "AI 小鲸",
5
5
  "license": "MIT",
6
6
  "author": "Tencent BlueKing",