@blueking/ai-blueking 0.0.6 → 0.1.0-beta.2

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.
@@ -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
+ };
@@ -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;