@blueking/ai-blueking 0.1.3 → 0.1.4

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
@@ -85,11 +85,15 @@ npm i @blueking/ai-blueking
85
85
  content: message,
86
86
  });
87
87
  // ai 消息,id是唯一标识当前流,调用 chatHelper.stop 的时候需要传入
88
- chatHelper.stream({
89
- id: 1,
90
- input: message,
91
- chatHistory: messages.value,
92
- });
88
+ chatHelper.stream(
89
+ {
90
+ inputs: {
91
+ input: message,
92
+ chat_history: messages.value,
93
+ },
94
+ },
95
+ 1,
96
+ );
93
97
  };
94
98
  </script>
95
99
  ```
@@ -1,4 +1,3 @@
1
- import type { IChatHistory } from './types/index';
2
1
  type HandleStart = (id: number | string) => unknown;
3
2
  type HandleReceiveMessage = (message: string, id: number | string) => void;
4
3
  type HandleEnd = (id: number | string) => void;
@@ -12,10 +11,6 @@ export declare class ChatHelper {
12
11
  url: string;
13
12
  constructor(url: string, handleStart: HandleStart, handleReceiveMessage: HandleReceiveMessage, handleEnd: HandleEnd, handleError: HandleError);
14
13
  stop(id: number | string): void;
15
- stream({ id, input, chatHistory }: {
16
- id: number | string;
17
- input?: string;
18
- chatHistory: IChatHistory[];
19
- }): Promise<void>;
14
+ stream(param: unknown, id: number | string): Promise<void>;
20
15
  }
21
16
  export {};
@@ -1,6 +1,6 @@
1
- var g = Object.defineProperty;
2
- var y = (a, e, t) => e in a ? g(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
3
- var s = (a, e, t) => (y(a, typeof e != "symbol" ? e + "" : e, t), t);
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
4
  const E = (a) => {
5
5
  try {
6
6
  return JSON.parse(a), !0;
@@ -9,46 +9,45 @@ const E = (a) => {
9
9
  }
10
10
  };
11
11
  class m {
12
- constructor(e, t, n, h, o) {
12
+ constructor(t, e, r, o, h) {
13
13
  s(this, "controllerMap");
14
14
  s(this, "handleEnd");
15
15
  s(this, "handleError");
16
16
  s(this, "handleReceiveMessage");
17
17
  s(this, "handleStart");
18
18
  s(this, "url");
19
- this.url = e, this.handleStart = t, this.handleReceiveMessage = n, this.handleEnd = h, this.handleError = o, this.controllerMap = {};
19
+ this.url = t, this.handleStart = e, this.handleReceiveMessage = r, this.handleEnd = o, this.handleError = h, this.controllerMap = {};
20
20
  }
21
- stop(e) {
22
- var t, n;
23
- return (n = (t = this.controllerMap[e]) == null ? void 0 : t.abort) == null || n.call(t), this.handleEnd(e);
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);
24
24
  }
25
- async stream({ id: e, input: t, chatHistory: n }) {
26
- await this.handleStart(e);
27
- const h = new AbortController();
28
- this.controllerMap[e] = h, fetch(this.url, {
25
+ async stream(t, e) {
26
+ const r = new AbortController();
27
+ this.controllerMap[e] = r, fetch(this.url, {
29
28
  method: "post",
30
- signal: h.signal,
29
+ signal: r.signal,
31
30
  headers: {
32
31
  "Content-Type": "application/json"
33
32
  },
34
33
  mode: "cors",
35
34
  credentials: "include",
36
- body: JSON.stringify({ inputs: { input: t, chat_history: n } })
35
+ body: JSON.stringify(t)
37
36
  }).then(async (o) => {
38
- const c = o.body.pipeThrough(new window.TextDecoderStream()).getReader();
37
+ const h = o.body.pipeThrough(new window.TextDecoderStream()).getReader();
39
38
  for (; ; )
40
39
  try {
41
- const { value: r, done: i } = await c.read();
42
- if (i)
40
+ const { value: n, done: c } = await h.read();
41
+ if (c)
43
42
  break;
44
- r.toString().split(`
45
- `).forEach((d) => {
46
- const l = d.trim();
43
+ n.toString().split(`
44
+ `).forEach((i) => {
45
+ const l = i.trim();
47
46
  if (E(l)) {
48
- const { event: p, content: u } = JSON.parse(l);
49
- switch (p) {
47
+ const { event: d, content: p } = JSON.parse(l);
48
+ switch (d) {
50
49
  case "text":
51
- this.handleReceiveMessage(u, e);
50
+ this.handleReceiveMessage(p, e);
52
51
  break;
53
52
  case "done":
54
53
  this.handleEnd(e);
@@ -56,13 +55,13 @@ class m {
56
55
  }
57
56
  }
58
57
  });
59
- } catch (r) {
60
- (r == null ? void 0 : r.code) !== 20 && this.handleError(r.message, r.code, e);
58
+ } catch (n) {
59
+ (n == null ? void 0 : n.code) !== 20 && this.handleError(n.message, n.code, e);
61
60
  break;
62
61
  }
63
62
  }).catch((o) => {
64
63
  this.handleError(o.message, o.code, e);
65
- });
64
+ }), await this.handleStart(e);
66
65
  }
67
66
  }
68
67
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blueking/ai-blueking",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "AI 小鲸",
5
5
  "license": "MIT",
6
6
  "author": "Tencent BlueKing",