@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 +9 -5
- package/dist/chat-helper.d.ts +1 -6
- package/dist/chat-helper.js +25 -26
- package/package.json +1 -1
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
```
|
package/dist/chat-helper.d.ts
CHANGED
|
@@ -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(
|
|
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 {};
|
package/dist/chat-helper.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var s = (a,
|
|
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,
|
|
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 =
|
|
19
|
+
this.url = t, this.handleStart = e, this.handleReceiveMessage = r, this.handleEnd = o, this.handleError = h, this.controllerMap = {};
|
|
20
20
|
}
|
|
21
|
-
stop(
|
|
22
|
-
var
|
|
23
|
-
return (
|
|
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(
|
|
26
|
-
|
|
27
|
-
|
|
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:
|
|
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(
|
|
35
|
+
body: JSON.stringify(t)
|
|
37
36
|
}).then(async (o) => {
|
|
38
|
-
const
|
|
37
|
+
const h = o.body.pipeThrough(new window.TextDecoderStream()).getReader();
|
|
39
38
|
for (; ; )
|
|
40
39
|
try {
|
|
41
|
-
const { value:
|
|
42
|
-
if (
|
|
40
|
+
const { value: n, done: c } = await h.read();
|
|
41
|
+
if (c)
|
|
43
42
|
break;
|
|
44
|
-
|
|
45
|
-
`).forEach((
|
|
46
|
-
const l =
|
|
43
|
+
n.toString().split(`
|
|
44
|
+
`).forEach((i) => {
|
|
45
|
+
const l = i.trim();
|
|
47
46
|
if (E(l)) {
|
|
48
|
-
const { event:
|
|
49
|
-
switch (
|
|
47
|
+
const { event: d, content: p } = JSON.parse(l);
|
|
48
|
+
switch (d) {
|
|
50
49
|
case "text":
|
|
51
|
-
this.handleReceiveMessage(
|
|
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 (
|
|
60
|
-
(
|
|
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 {
|