@blueking/ai-blueking 0.0.5 → 0.1.0-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/README.md CHANGED
@@ -26,6 +26,7 @@ npm i @blueking/ai-blueking
26
26
  @clear="handleClear"
27
27
  @close="handleClose"
28
28
  @send="handleSend"
29
+ @scroll="handleScroll"
29
30
  />
30
31
  </div>
31
32
  </template>
@@ -114,6 +115,10 @@ npm i @blueking/ai-blueking
114
115
  console.log('trigger send', val);
115
116
  };
116
117
 
118
+ const handleScroll = (event: Event) => {
119
+ console.log('trigger scroll', event);
120
+ };
121
+
117
122
  const handleClose = () => {
118
123
  console.log('trigger close');
119
124
  };
@@ -148,6 +153,7 @@ npm i @blueking/bkui-library
148
153
  @clear="handleClear"
149
154
  @close="handleClose"
150
155
  @send="handleSend"
156
+ @scroll="handleScroll"
151
157
  />
152
158
  </div>
153
159
  </template>
@@ -236,6 +242,9 @@ npm i @blueking/bkui-library
236
242
  handleChoosePrompt(prompt) {
237
243
  console.log('choose prompt', prompt);
238
244
  },
245
+ handleScroll(event: Event) {
246
+ console.log('trigger scroll', event);
247
+ },
239
248
  },
240
249
  };
241
250
  </script>
@@ -15,6 +15,7 @@ declare const _default: import("vue").DefineComponent<__VLS_TypePropsToOption<{
15
15
  clear: () => void;
16
16
  send: (val: string) => void;
17
17
  "choose-prompt": (prompt: IPrompt) => void;
18
+ scroll: (data: Event) => void;
18
19
  }, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<__VLS_TypePropsToOption<{
19
20
  messages: IMessage[];
20
21
  prompts?: IPrompt[] | undefined;
@@ -27,6 +28,7 @@ declare const _default: import("vue").DefineComponent<__VLS_TypePropsToOption<{
27
28
  userPhoto?: string | undefined;
28
29
  logo?: string | undefined;
29
30
  }>>> & {
31
+ onScroll?: ((data: Event) => any) | undefined;
30
32
  "onChoose-prompt"?: ((prompt: IPrompt) => any) | undefined;
31
33
  onClear?: (() => any) | undefined;
32
34
  onSend?: ((val: string) => any) | undefined;
package/dist/ai.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import type { IChatHistory } from './types/index';
2
+ type HandleStart = (id: number | string) => unknown;
3
+ type HandleReceiveMessage = (message: string, id: number | string) => void;
4
+ type HandleEnd = (id: number | string) => void;
5
+ type HandleError = (message: string, code: string, id: number | string) => unknown;
6
+ export declare class ChatHelper {
7
+ controllerMap: Record<number | string, AbortController>;
8
+ handleEnd: HandleEnd;
9
+ handleError: HandleError;
10
+ handleReceiveMessage: HandleReceiveMessage;
11
+ handleStart: HandleStart;
12
+ url: string;
13
+ constructor(url: string, handleStart: HandleStart, handleReceiveMessage: HandleReceiveMessage, handleEnd: HandleEnd, handleError: HandleError);
14
+ stop(id: number | string): void;
15
+ stream({ id, input, chatHistory }: {
16
+ id: number | string;
17
+ input?: string;
18
+ chatHistory: IChatHistory[];
19
+ }): Promise<void>;
20
+ }
21
+ export {};
@@ -0,0 +1,70 @@
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);
4
+ const E = (a) => {
5
+ try {
6
+ return JSON.parse(a), !0;
7
+ } catch {
8
+ return !1;
9
+ }
10
+ };
11
+ class m {
12
+ constructor(e, t, n, h, o) {
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 = e, this.handleStart = t, this.handleReceiveMessage = n, this.handleEnd = h, this.handleError = o, this.controllerMap = {};
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);
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, {
29
+ method: "post",
30
+ signal: h.signal,
31
+ headers: {
32
+ "Content-Type": "application/json"
33
+ },
34
+ mode: "cors",
35
+ credentials: "include",
36
+ body: JSON.stringify({ inputs: { input: t, chat_history: n } })
37
+ }).then(async (o) => {
38
+ const c = o.body.pipeThrough(new window.TextDecoderStream()).getReader();
39
+ for (; ; )
40
+ try {
41
+ const { value: r, done: i } = await c.read();
42
+ if (i)
43
+ break;
44
+ r.toString().split(`
45
+ `).forEach((d) => {
46
+ const l = d.trim();
47
+ if (E(l)) {
48
+ const { event: p, content: u } = JSON.parse(l);
49
+ switch (p) {
50
+ case "text":
51
+ this.handleReceiveMessage(u, e);
52
+ break;
53
+ case "done":
54
+ this.handleEnd(e);
55
+ break;
56
+ }
57
+ }
58
+ });
59
+ } catch (r) {
60
+ (r == null ? void 0 : r.code) !== 20 && this.handleError(r.message, r.code, e);
61
+ break;
62
+ }
63
+ }).catch((o) => {
64
+ this.handleError(o.message, o.code, e);
65
+ });
66
+ }
67
+ }
68
+ export {
69
+ m as ChatHelper
70
+ };
@@ -10,6 +10,7 @@ declare const _default: import("vue").DefineComponent<__VLS_WithDefaults<__VLS_T
10
10
  }>, {}, unknown, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
11
11
  send: (userInput: string) => void;
12
12
  "choose-prompt": (prompt: IPrompt) => void;
13
+ scroll: (data: Event) => void;
13
14
  }, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToOption<{
14
15
  messages: IMessage[];
15
16
  prompts?: IPrompt[] | undefined;
@@ -19,6 +20,7 @@ declare const _default: import("vue").DefineComponent<__VLS_WithDefaults<__VLS_T
19
20
  }>, {
20
21
  background: string;
21
22
  }>>> & {
23
+ onScroll?: ((data: Event) => any) | undefined;
22
24
  "onChoose-prompt"?: ((prompt: IPrompt) => any) | undefined;
23
25
  onSend?: ((userInput: string) => any) | undefined;
24
26
  }, {
@@ -0,0 +1,70 @@
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);
4
+ const E = (a) => {
5
+ try {
6
+ return JSON.parse(a), !0;
7
+ } catch {
8
+ return !1;
9
+ }
10
+ };
11
+ class m {
12
+ constructor(e, t, n, h, o) {
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 = e, this.handleStart = t, this.handleReceiveMessage = n, this.handleEnd = h, this.handleError = o, this.controllerMap = {};
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);
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, {
29
+ method: "post",
30
+ signal: h.signal,
31
+ headers: {
32
+ "Content-Type": "application/json"
33
+ },
34
+ mode: "cors",
35
+ credentials: "include",
36
+ body: JSON.stringify({ inputs: { input: t, chat_history: n } })
37
+ }).then(async (o) => {
38
+ const c = o.body.pipeThrough(new window.TextDecoderStream()).getReader();
39
+ for (; ; )
40
+ try {
41
+ const { value: r, done: i } = await c.read();
42
+ if (i)
43
+ break;
44
+ r.toString().split(`
45
+ `).forEach((d) => {
46
+ const l = d.trim();
47
+ if (E(l)) {
48
+ const { event: p, content: u } = JSON.parse(l);
49
+ switch (p) {
50
+ case "text":
51
+ this.handleReceiveMessage(u, e);
52
+ break;
53
+ case "done":
54
+ this.handleEnd(e);
55
+ break;
56
+ }
57
+ }
58
+ });
59
+ } catch (r) {
60
+ (r == null ? void 0 : r.code) !== 20 && this.handleError(r.message, r.code, e);
61
+ break;
62
+ }
63
+ }).catch((o) => {
64
+ this.handleError(o.message, o.code, e);
65
+ });
66
+ }
67
+ }
68
+ export {
69
+ m as ChatHelper
70
+ };
@@ -0,0 +1,9 @@
1
+ export declare enum AgentMessageType {
2
+ AgentAction = "AgentAction",
3
+ AgentActionInfo = "AgentActionInfo"
4
+ }
5
+ export declare enum RoleType {
6
+ Assistant = "assistant",
7
+ System = "system",
8
+ User = "user"
9
+ }
@@ -1,6 +1,5 @@
1
- export interface IMessage {
2
- content: string;
3
- type: 'ai' | 'user';
1
+ import { RoleType } from './enum';
2
+ export interface IMessage extends IChatHistory {
4
3
  status?: 'error' | 'loading';
5
4
  }
6
5
  export interface IPrompt {
@@ -23,3 +22,7 @@ export interface IStartPosition {
23
22
  left?: number;
24
23
  right?: number;
25
24
  }
25
+ export interface IChatHistory {
26
+ role: RoleType;
27
+ content: string;
28
+ }
@@ -3,3 +3,9 @@
3
3
  * @param {*} name cookie 的名称
4
4
  */
5
5
  export declare const getCookieByName: (name: string) => string;
6
+ /**
7
+ * 判断是否是json字符串
8
+ * @param str
9
+ * @returns
10
+ */
11
+ export declare const isJSON: (str: string) => boolean;