@ray-js/t-agent 0.0.5-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/LICENSE.md +21 -0
- package/README-zh_CN.md +1350 -0
- package/README.md +1345 -0
- package/dist/chat/ChatAgent.d.ts +157 -0
- package/dist/chat/ChatAgent.js +276 -0
- package/dist/chat/ChatBubbleTile.d.ts +32 -0
- package/dist/chat/ChatBubbleTile.js +104 -0
- package/dist/chat/ChatMessage.d.ts +32 -0
- package/dist/chat/ChatMessage.js +173 -0
- package/dist/chat/ChatSession.d.ts +27 -0
- package/dist/chat/ChatSession.js +89 -0
- package/dist/chat/ChatTile.d.ts +26 -0
- package/dist/chat/ChatTile.js +75 -0
- package/dist/chat/Emitter.d.ts +28 -0
- package/dist/chat/Emitter.js +61 -0
- package/dist/chat/Logger.d.ts +16 -0
- package/dist/chat/Logger.js +90 -0
- package/dist/chat/StreamResponse.d.ts +22 -0
- package/dist/chat/StreamResponse.js +47 -0
- package/dist/chat/createChatAgent.d.ts +6 -0
- package/dist/chat/createChatAgent.js +21 -0
- package/dist/chat/index.d.ts +12 -0
- package/dist/chat/index.js +12 -0
- package/dist/chat/types.d.ts +177 -0
- package/dist/chat/types.js +29 -0
- package/dist/chat/utils.d.ts +32 -0
- package/dist/chat/utils.js +207 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/plugins/debug.d.ts +10 -0
- package/dist/plugins/debug.js +33 -0
- package/dist/plugins/ui.d.ts +28 -0
- package/dist/plugins/ui.js +44 -0
- package/package.json +30 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
2
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
3
|
+
const _excluded = ["id", "tiles"];
|
|
4
|
+
import "core-js/modules/esnext.iterator.constructor.js";
|
|
5
|
+
import "core-js/modules/esnext.iterator.for-each.js";
|
|
6
|
+
import "core-js/modules/esnext.iterator.map.js";
|
|
7
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
8
|
+
import { ChatMessageStatus } from './types';
|
|
9
|
+
import ChatTile from './ChatTile';
|
|
10
|
+
import ChatBubbleTile from './ChatBubbleTile';
|
|
11
|
+
import { deepCloneToPlainObject, deepMerge, generateId } from './utils';
|
|
12
|
+
export default class ChatMessage {
|
|
13
|
+
get isShow() {
|
|
14
|
+
return this._isShow;
|
|
15
|
+
}
|
|
16
|
+
get bubble() {
|
|
17
|
+
if (!this._bubble) {
|
|
18
|
+
for (const tile of this.tiles) {
|
|
19
|
+
if (tile.type === 'bubble') {
|
|
20
|
+
this._bubble = tile;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (!this._bubble) {
|
|
25
|
+
this._bubble = new ChatBubbleTile(this);
|
|
26
|
+
this.tiles.push(this._bubble);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return this._bubble;
|
|
30
|
+
}
|
|
31
|
+
constructor(agent, data) {
|
|
32
|
+
_defineProperty(this, "_isShow", false);
|
|
33
|
+
_defineProperty(this, "role", 'user');
|
|
34
|
+
_defineProperty(this, "tiles", []);
|
|
35
|
+
_defineProperty(this, "status", ChatMessageStatus.START);
|
|
36
|
+
_defineProperty(this, "meta", {});
|
|
37
|
+
this.agent = agent;
|
|
38
|
+
const {
|
|
39
|
+
id,
|
|
40
|
+
tiles
|
|
41
|
+
} = data,
|
|
42
|
+
rest = _objectWithoutProperties(data, _excluded);
|
|
43
|
+
Object.assign(this, rest);
|
|
44
|
+
this.id = id || generateId(16);
|
|
45
|
+
this.initTiles(null, tiles || []);
|
|
46
|
+
}
|
|
47
|
+
initTiles(root, tiles) {
|
|
48
|
+
for (const tile of tiles) {
|
|
49
|
+
let r;
|
|
50
|
+
if (root) {
|
|
51
|
+
r = root.addTile(tile.type, tile.data, tile.id);
|
|
52
|
+
} else {
|
|
53
|
+
r = this.addTile(tile.type, tile.data, tile.id);
|
|
54
|
+
}
|
|
55
|
+
if (tile.fallback) {
|
|
56
|
+
r.setFallback(tile.fallback);
|
|
57
|
+
}
|
|
58
|
+
this.initTiles(r, tile.children);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
show() {
|
|
62
|
+
return this.agent.runWithCatchError(async () => {
|
|
63
|
+
if (this._isShow) {
|
|
64
|
+
console.warn('message is already show');
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
await this.agent.hooks.callHook('onMessageChange:before', 'show', this);
|
|
68
|
+
await this.agent.hooks.callHook('onMessageChange', 'show', this);
|
|
69
|
+
this._isShow = true;
|
|
70
|
+
this.agent.session.bindMessage(this);
|
|
71
|
+
await this.agent.hooks.callHook('onMessageChange:after', 'show', this);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
update() {
|
|
75
|
+
return this.agent.runWithCatchError(async () => {
|
|
76
|
+
if (!this._isShow) {
|
|
77
|
+
console.warn('message is not show');
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
await this.agent.hooks.callHook('onMessageChange:before', 'update', this);
|
|
81
|
+
await this.agent.hooks.callHook('onMessageChange', 'update', this);
|
|
82
|
+
for (const tile of this.tiles) {
|
|
83
|
+
this.agent.session.bindTile(tile);
|
|
84
|
+
}
|
|
85
|
+
await this.agent.hooks.callHook('onMessageChange:after', 'update', this);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
remove() {
|
|
89
|
+
return this.agent.runWithCatchError(async () => {
|
|
90
|
+
if (!this._isShow) {
|
|
91
|
+
console.warn('message is not show');
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
await this.agent.hooks.callHook('onMessageChange:before', 'remove', this);
|
|
95
|
+
await this.agent.hooks.callHook('onMessageChange', 'remove', this);
|
|
96
|
+
this.agent.session.unbindMessage(this);
|
|
97
|
+
await this.agent.hooks.callHook('onMessageChange:after', 'remove', this);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
_setIsShow() {
|
|
101
|
+
this._isShow = true;
|
|
102
|
+
}
|
|
103
|
+
persist(payload) {
|
|
104
|
+
return this.agent.runWithCatchError(async () => {
|
|
105
|
+
await this.agent.hooks.callHook('onMessagePersist:before', payload, this);
|
|
106
|
+
await this.agent.hooks.callHook('onMessagePersist', payload, this);
|
|
107
|
+
await this.agent.hooks.callHook('onMessagePersist:after', payload, this);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
setTilesLocked(locked) {
|
|
111
|
+
for (const tile of this.tiles) {
|
|
112
|
+
tile.setLocked(locked);
|
|
113
|
+
}
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
removeTile(tile) {
|
|
117
|
+
const index = this.tiles.findIndex(t => t.id === tile.id);
|
|
118
|
+
if (index > -1) {
|
|
119
|
+
this.tiles.splice(index, 1);
|
|
120
|
+
this.agent.session.unbindTile(tile);
|
|
121
|
+
tile.children.forEach(t => this.agent.session.unbindTile(t));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
addTile(type, data, id) {
|
|
125
|
+
const tile = type === 'bubble' ? new ChatBubbleTile(this, data, [], id) : new ChatTile(this, type, data, [], id);
|
|
126
|
+
this.tiles.push(tile);
|
|
127
|
+
return tile;
|
|
128
|
+
}
|
|
129
|
+
set(data) {
|
|
130
|
+
Object.assign(this, data);
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
setMetaValue(key, value) {
|
|
134
|
+
this.meta[key] = value;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
setMeta(meta) {
|
|
138
|
+
let override = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
139
|
+
if (override) {
|
|
140
|
+
this.meta = meta;
|
|
141
|
+
} else {
|
|
142
|
+
this.meta = deepMerge(this.meta, meta);
|
|
143
|
+
}
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
deleteMetaValue(key) {
|
|
147
|
+
delete this.meta[key];
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
findTileByType(type) {
|
|
151
|
+
const ret = [];
|
|
152
|
+
const tiles = this.tiles;
|
|
153
|
+
for (const tile of tiles) {
|
|
154
|
+
if (tile.type === type) {
|
|
155
|
+
ret.push(tile);
|
|
156
|
+
}
|
|
157
|
+
if (tile.children.length > 0) {
|
|
158
|
+
ret.push(...tile.findByType(type));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return ret;
|
|
162
|
+
}
|
|
163
|
+
toObject() {
|
|
164
|
+
const meta = deepCloneToPlainObject(this.meta);
|
|
165
|
+
return {
|
|
166
|
+
id: this.id,
|
|
167
|
+
role: this.role,
|
|
168
|
+
tiles: this.tiles.map(tile => tile.toObject()),
|
|
169
|
+
status: this.status,
|
|
170
|
+
meta: meta || {}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ChatSessionHooks } from './types';
|
|
2
|
+
import { Hookable } from 'hookable';
|
|
3
|
+
import ChatTile from './ChatTile';
|
|
4
|
+
import ChatMessage from './ChatMessage';
|
|
5
|
+
import ChatAgent from './ChatAgent';
|
|
6
|
+
export default class ChatSession {
|
|
7
|
+
private agent;
|
|
8
|
+
private data;
|
|
9
|
+
private tileIdIndex;
|
|
10
|
+
hooks: Hookable<ChatSessionHooks>;
|
|
11
|
+
isNewChat: boolean;
|
|
12
|
+
sessionId: string;
|
|
13
|
+
readonly messages: Map<string, ChatMessage>;
|
|
14
|
+
constructor(agent: ChatAgent);
|
|
15
|
+
set: (key: string, value: any) => Promise<void>;
|
|
16
|
+
get: <T = any>(key: string, defaultValue?: T | undefined) => any;
|
|
17
|
+
getData: () => Record<string, any>;
|
|
18
|
+
initMessages: (messages: ChatMessage[]) => void;
|
|
19
|
+
getTileById: (id: string) => ChatTile<any> | undefined;
|
|
20
|
+
bindTile: (tile: ChatTile) => void;
|
|
21
|
+
unbindTile: (tile: ChatTile) => void;
|
|
22
|
+
bindMessage: (message: ChatMessage) => void;
|
|
23
|
+
unbindMessage: (message: ChatMessage) => void;
|
|
24
|
+
onChange: (fn: ChatSessionHooks['onChange']) => () => void;
|
|
25
|
+
getLatestMessage: () => ChatMessage | undefined;
|
|
26
|
+
dispose: () => Promise<void>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
+
import "core-js/modules/esnext.iterator.constructor.js";
|
|
3
|
+
import "core-js/modules/esnext.iterator.for-each.js";
|
|
4
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
5
|
+
import { ChatMessageStatus } from './types';
|
|
6
|
+
import { createHooks } from 'hookable';
|
|
7
|
+
export default class ChatSession {
|
|
8
|
+
constructor(agent) {
|
|
9
|
+
_defineProperty(this, "data", new Map());
|
|
10
|
+
_defineProperty(this, "tileIdIndex", new Map());
|
|
11
|
+
_defineProperty(this, "isNewChat", true);
|
|
12
|
+
_defineProperty(this, "sessionId", '');
|
|
13
|
+
_defineProperty(this, "messages", new Map());
|
|
14
|
+
_defineProperty(this, "set", async (key, value) => {
|
|
15
|
+
const oldValue = this.data.get(key);
|
|
16
|
+
if (oldValue === value) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
await this.hooks.callHook('onChange', key, value, oldValue);
|
|
20
|
+
this.data.set(key, value);
|
|
21
|
+
});
|
|
22
|
+
_defineProperty(this, "get", (key, defaultValue) => {
|
|
23
|
+
if (!this.data.has(key)) {
|
|
24
|
+
return defaultValue;
|
|
25
|
+
}
|
|
26
|
+
return this.data.get(key);
|
|
27
|
+
});
|
|
28
|
+
_defineProperty(this, "getData", () => {
|
|
29
|
+
const ret = {};
|
|
30
|
+
for (const [key, value] of this.data.entries()) {
|
|
31
|
+
ret[key] = value;
|
|
32
|
+
}
|
|
33
|
+
return ret;
|
|
34
|
+
});
|
|
35
|
+
_defineProperty(this, "initMessages", messages => {
|
|
36
|
+
for (const message of messages) {
|
|
37
|
+
this.bindMessage(message);
|
|
38
|
+
message.set({
|
|
39
|
+
status: ChatMessageStatus.FINISH
|
|
40
|
+
})._setIsShow();
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
_defineProperty(this, "getTileById", id => {
|
|
44
|
+
return this.tileIdIndex.get(id);
|
|
45
|
+
});
|
|
46
|
+
_defineProperty(this, "bindTile", tile => {
|
|
47
|
+
this.tileIdIndex.set(tile.id, tile);
|
|
48
|
+
for (const child of tile.children) {
|
|
49
|
+
this.bindTile(child);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
_defineProperty(this, "unbindTile", tile => {
|
|
53
|
+
this.tileIdIndex.delete(tile.id);
|
|
54
|
+
for (const child of tile.children) {
|
|
55
|
+
this.unbindTile(child);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
_defineProperty(this, "bindMessage", message => {
|
|
59
|
+
this.messages.set(message.id, message);
|
|
60
|
+
for (const tile of message.tiles) {
|
|
61
|
+
this.bindTile(tile);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
_defineProperty(this, "unbindMessage", message => {
|
|
65
|
+
this.messages.delete(message.id);
|
|
66
|
+
this.tileIdIndex.forEach(tile => {
|
|
67
|
+
if (tile.message.id === message.id) {
|
|
68
|
+
this.tileIdIndex.delete(tile.id);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
_defineProperty(this, "onChange", fn => this.hooks.hook('onChange', fn));
|
|
73
|
+
_defineProperty(this, "getLatestMessage", () => {
|
|
74
|
+
return Array.from(this.messages.values()).pop();
|
|
75
|
+
});
|
|
76
|
+
_defineProperty(this, "dispose", async () => {
|
|
77
|
+
this.hooks.removeAllHooks();
|
|
78
|
+
this.data.clear();
|
|
79
|
+
this.tileIdIndex.clear();
|
|
80
|
+
this.messages.clear();
|
|
81
|
+
});
|
|
82
|
+
this.agent = agent;
|
|
83
|
+
this.hooks = createHooks();
|
|
84
|
+
const hooks = ['onChange'];
|
|
85
|
+
for (const hook of hooks) {
|
|
86
|
+
this[hook] = fn => this.hooks.hook(hook, fn);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ChatTileObject, TipTileData } from './types';
|
|
2
|
+
import ChatMessage from './ChatMessage';
|
|
3
|
+
export default class ChatTile<T = any> implements ChatTileObject<T> {
|
|
4
|
+
readonly message: ChatMessage;
|
|
5
|
+
type: string;
|
|
6
|
+
data: T;
|
|
7
|
+
children: ChatTile[];
|
|
8
|
+
id: string;
|
|
9
|
+
locked: boolean;
|
|
10
|
+
fallback: undefined;
|
|
11
|
+
constructor(message: ChatMessage, type: string, data?: T, children?: ChatTile[], id?: string);
|
|
12
|
+
setLocked(value: boolean): this;
|
|
13
|
+
addTile(type: string, data: any, id?: string): ChatTile<any>;
|
|
14
|
+
/**
|
|
15
|
+
* 同 message 的 show
|
|
16
|
+
*/
|
|
17
|
+
show(): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* 同 message 的 update
|
|
20
|
+
*/
|
|
21
|
+
update(): Promise<void>;
|
|
22
|
+
setData(data: T): this;
|
|
23
|
+
setFallback(fallback: TipTileData): this;
|
|
24
|
+
findByType(type: string): ChatTile[];
|
|
25
|
+
toObject(): ChatTileObject;
|
|
26
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
3
|
+
import "core-js/modules/esnext.iterator.map.js";
|
|
4
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
5
|
+
import { generateId } from './utils';
|
|
6
|
+
export default class ChatTile {
|
|
7
|
+
constructor(message, type) {
|
|
8
|
+
let data = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
9
|
+
let children = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
|
|
10
|
+
let id = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : generateId(16);
|
|
11
|
+
_defineProperty(this, "locked", false);
|
|
12
|
+
_defineProperty(this, "fallback", undefined);
|
|
13
|
+
this.message = message;
|
|
14
|
+
this.type = type;
|
|
15
|
+
this.data = data;
|
|
16
|
+
this.children = children;
|
|
17
|
+
this.id = id;
|
|
18
|
+
}
|
|
19
|
+
setLocked(value) {
|
|
20
|
+
this.locked = value;
|
|
21
|
+
// 递归锁定
|
|
22
|
+
for (const child of this.children) {
|
|
23
|
+
child.setLocked(value);
|
|
24
|
+
}
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
addTile(type, data, id) {
|
|
28
|
+
const tile = new ChatTile(this.message, type, data, [], id);
|
|
29
|
+
this.children.push(tile);
|
|
30
|
+
return tile;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 同 message 的 show
|
|
35
|
+
*/
|
|
36
|
+
show() {
|
|
37
|
+
return this.message.show();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 同 message 的 update
|
|
42
|
+
*/
|
|
43
|
+
update() {
|
|
44
|
+
return this.message.update();
|
|
45
|
+
}
|
|
46
|
+
setData(data) {
|
|
47
|
+
this.data = _objectSpread(_objectSpread({}, this.data), data);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
setFallback(fallback) {
|
|
51
|
+
this.fallback = fallback;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
findByType(type) {
|
|
55
|
+
let ret = [];
|
|
56
|
+
for (const tile of this.children) {
|
|
57
|
+
if (tile.type === type) {
|
|
58
|
+
ret.push(tile);
|
|
59
|
+
}
|
|
60
|
+
const t = tile.findByType(type);
|
|
61
|
+
ret = ret.concat(t);
|
|
62
|
+
}
|
|
63
|
+
return ret;
|
|
64
|
+
}
|
|
65
|
+
toObject() {
|
|
66
|
+
return {
|
|
67
|
+
id: this.id,
|
|
68
|
+
type: this.type,
|
|
69
|
+
locked: this.locked,
|
|
70
|
+
fallback: this.fallback,
|
|
71
|
+
children: this.children.map(child => child.toObject()),
|
|
72
|
+
data: this.data
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare class EmitterEvent<T = any> {
|
|
2
|
+
type: string;
|
|
3
|
+
bubbles: boolean;
|
|
4
|
+
cancelable: boolean;
|
|
5
|
+
detail?: T;
|
|
6
|
+
reason?: any;
|
|
7
|
+
defaultPrevented?: boolean;
|
|
8
|
+
constructor(type: string, eventInitDict?: {
|
|
9
|
+
bubbles?: boolean;
|
|
10
|
+
cancelable?: boolean;
|
|
11
|
+
reason?: any;
|
|
12
|
+
detail?: T;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export type EventCallback = (event: EmitterEvent) => void;
|
|
16
|
+
export type EventOptions = {
|
|
17
|
+
once?: boolean;
|
|
18
|
+
};
|
|
19
|
+
export declare class Emitter {
|
|
20
|
+
readonly listeners: Record<string, {
|
|
21
|
+
callback: EventCallback;
|
|
22
|
+
options?: EventOptions;
|
|
23
|
+
}[]>;
|
|
24
|
+
constructor();
|
|
25
|
+
addEventListener(type: string, callback: EventCallback, options?: EventOptions): void;
|
|
26
|
+
removeEventListener(type: string, callback: EventCallback): void;
|
|
27
|
+
dispatchEvent(event: EmitterEvent): boolean;
|
|
28
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export class EmitterEvent {
|
|
2
|
+
constructor(type, eventInitDict) {
|
|
3
|
+
var _eventInitDict$bubble, _eventInitDict$cancel;
|
|
4
|
+
this.type = type;
|
|
5
|
+
this.bubbles = (_eventInitDict$bubble = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.bubbles) !== null && _eventInitDict$bubble !== void 0 ? _eventInitDict$bubble : false;
|
|
6
|
+
this.cancelable = (_eventInitDict$cancel = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.cancelable) !== null && _eventInitDict$cancel !== void 0 ? _eventInitDict$cancel : false;
|
|
7
|
+
this.reason = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.reason;
|
|
8
|
+
this.detail = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.detail;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class Emitter {
|
|
12
|
+
constructor() {
|
|
13
|
+
Object.defineProperty(this, 'listeners', {
|
|
14
|
+
value: {},
|
|
15
|
+
writable: true,
|
|
16
|
+
configurable: true
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
addEventListener(type, callback, options) {
|
|
20
|
+
if (!(type in this.listeners)) {
|
|
21
|
+
this.listeners[type] = [];
|
|
22
|
+
}
|
|
23
|
+
this.listeners[type].push({
|
|
24
|
+
callback,
|
|
25
|
+
options
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
removeEventListener(type, callback) {
|
|
29
|
+
if (!(type in this.listeners)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const stack = this.listeners[type];
|
|
33
|
+
for (let i = 0, l = stack.length; i < l; i++) {
|
|
34
|
+
if (stack[i].callback === callback) {
|
|
35
|
+
stack.splice(i, 1);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
dispatchEvent(event) {
|
|
41
|
+
if (!(event.type in this.listeners)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const stack = this.listeners[event.type];
|
|
45
|
+
const stackToCall = stack.slice();
|
|
46
|
+
for (let i = 0, l = stackToCall.length; i < l; i++) {
|
|
47
|
+
const listener = stackToCall[i];
|
|
48
|
+
try {
|
|
49
|
+
listener.callback.call(this, event);
|
|
50
|
+
} catch (e) {
|
|
51
|
+
Promise.resolve().then(() => {
|
|
52
|
+
throw e;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (listener.options && listener.options.once) {
|
|
56
|
+
this.removeEventListener(event.type, listener.callback);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return !event.defaultPrevented;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export default class Logger {
|
|
2
|
+
static create(prefix: string): Logger;
|
|
3
|
+
static setLogLevel(level: string): void;
|
|
4
|
+
static mute(value?: boolean): void;
|
|
5
|
+
static setOnErrorHandler(handler: (...args: any[]) => void): void;
|
|
6
|
+
static _callOnErrorHandler(args: any[]): void;
|
|
7
|
+
private readonly prefix;
|
|
8
|
+
constructor(prefix: string);
|
|
9
|
+
debug(...args: any[]): void;
|
|
10
|
+
log(...args: any[]): void;
|
|
11
|
+
info(...args: any[]): void;
|
|
12
|
+
warn(...args: any[]): void;
|
|
13
|
+
error(...args: any[]): void;
|
|
14
|
+
_output(level: string, args: any[]): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function getLogger(prefix: string): Logger;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
2
|
+
const LOG_LEVELS = ['debug', 'log', 'info', 'warn', 'error', 'none'];
|
|
3
|
+
const DEFAULT_LOG_LEVEL = 'log';
|
|
4
|
+
let currentLogLevelPos = LOG_LEVELS.indexOf(DEFAULT_LOG_LEVEL);
|
|
5
|
+
let onErrorHandler;
|
|
6
|
+
let muted = false;
|
|
7
|
+
function passLevel(level) {
|
|
8
|
+
const messageLogLevelPos = LOG_LEVELS.indexOf(level);
|
|
9
|
+
return messageLogLevelPos >= currentLogLevelPos;
|
|
10
|
+
}
|
|
11
|
+
export default class Logger {
|
|
12
|
+
static create(prefix) {
|
|
13
|
+
return new Logger(prefix);
|
|
14
|
+
}
|
|
15
|
+
static setLogLevel(level) {
|
|
16
|
+
const newPos = LOG_LEVELS.indexOf(level);
|
|
17
|
+
if (newPos === -1) {
|
|
18
|
+
throw new Error("Incorrect log level: ".concat(level, ". Possible levels are: ").concat(LOG_LEVELS.join(', ')));
|
|
19
|
+
} else {
|
|
20
|
+
currentLogLevelPos = newPos;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
static mute() {
|
|
24
|
+
let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
25
|
+
muted = value;
|
|
26
|
+
}
|
|
27
|
+
static setOnErrorHandler(handler) {
|
|
28
|
+
onErrorHandler = handler;
|
|
29
|
+
}
|
|
30
|
+
static _callOnErrorHandler(args) {
|
|
31
|
+
if (onErrorHandler) {
|
|
32
|
+
try {
|
|
33
|
+
onErrorHandler(...args);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
// better to nothing here to avoid infinite loop as logger.error() usually handles uncaught exceptions
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
constructor(prefix) {
|
|
40
|
+
this.prefix = prefix ? ["[".concat(prefix, "]")] : [];
|
|
41
|
+
}
|
|
42
|
+
debug() {
|
|
43
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
44
|
+
args[_key] = arguments[_key];
|
|
45
|
+
}
|
|
46
|
+
this._output('debug', args);
|
|
47
|
+
}
|
|
48
|
+
log() {
|
|
49
|
+
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
50
|
+
args[_key2] = arguments[_key2];
|
|
51
|
+
}
|
|
52
|
+
this._output('log', args);
|
|
53
|
+
}
|
|
54
|
+
info() {
|
|
55
|
+
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
56
|
+
args[_key3] = arguments[_key3];
|
|
57
|
+
}
|
|
58
|
+
this._output('info', args);
|
|
59
|
+
}
|
|
60
|
+
warn() {
|
|
61
|
+
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
62
|
+
args[_key4] = arguments[_key4];
|
|
63
|
+
}
|
|
64
|
+
this._output('warn', args);
|
|
65
|
+
}
|
|
66
|
+
error() {
|
|
67
|
+
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
|
|
68
|
+
args[_key5] = arguments[_key5];
|
|
69
|
+
}
|
|
70
|
+
this._output('error', args);
|
|
71
|
+
Logger._callOnErrorHandler(args);
|
|
72
|
+
}
|
|
73
|
+
_output(level, args) {
|
|
74
|
+
if (!muted && passLevel(level)) {
|
|
75
|
+
args = [...this.prefix, ...args];
|
|
76
|
+
if (typeof console[level] === 'function') {
|
|
77
|
+
console[level](...args);
|
|
78
|
+
} else {
|
|
79
|
+
console.log(...args);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const loggers = {};
|
|
85
|
+
export function getLogger(prefix) {
|
|
86
|
+
if (!loggers[prefix]) {
|
|
87
|
+
loggers[prefix] = Logger.create(prefix);
|
|
88
|
+
}
|
|
89
|
+
return loggers[prefix];
|
|
90
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { MessagePart, StreamResponseInstance } from './types';
|
|
2
|
+
export default class StreamResponse implements StreamResponseInstance {
|
|
3
|
+
private partStream;
|
|
4
|
+
/**
|
|
5
|
+
* Create a new StreamResponse instance.
|
|
6
|
+
*
|
|
7
|
+
* @param partStream
|
|
8
|
+
*/
|
|
9
|
+
constructor(partStream: ReadableStream<MessagePart>);
|
|
10
|
+
/**
|
|
11
|
+
* Get all parts of the response as an async iterator.
|
|
12
|
+
*/
|
|
13
|
+
parts(): {
|
|
14
|
+
[Symbol.asyncIterator](): AsyncGenerator<MessagePart, void, unknown>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Cancel the response stream.
|
|
18
|
+
*
|
|
19
|
+
* @param reason
|
|
20
|
+
*/
|
|
21
|
+
cancel(reason: any): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import _awaitAsyncGenerator from "@babel/runtime/helpers/esm/awaitAsyncGenerator";
|
|
2
|
+
import _wrapAsyncGenerator from "@babel/runtime/helpers/esm/wrapAsyncGenerator";
|
|
3
|
+
import "core-js/modules/es.symbol.async-iterator.js";
|
|
4
|
+
export default class StreamResponse {
|
|
5
|
+
/**
|
|
6
|
+
* Create a new StreamResponse instance.
|
|
7
|
+
*
|
|
8
|
+
* @param partStream
|
|
9
|
+
*/
|
|
10
|
+
constructor(partStream) {
|
|
11
|
+
this.partStream = partStream;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Get all parts of the response as an async iterator.
|
|
16
|
+
*/
|
|
17
|
+
parts() {
|
|
18
|
+
const reader = this.partStream.getReader();
|
|
19
|
+
return {
|
|
20
|
+
[Symbol.asyncIterator]() {
|
|
21
|
+
return _wrapAsyncGenerator(function* () {
|
|
22
|
+
try {
|
|
23
|
+
while (true) {
|
|
24
|
+
const {
|
|
25
|
+
done,
|
|
26
|
+
value
|
|
27
|
+
} = yield _awaitAsyncGenerator(reader.read());
|
|
28
|
+
if (done) break;
|
|
29
|
+
yield value;
|
|
30
|
+
}
|
|
31
|
+
} finally {
|
|
32
|
+
reader.releaseLock();
|
|
33
|
+
}
|
|
34
|
+
})();
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Cancel the response stream.
|
|
41
|
+
*
|
|
42
|
+
* @param reason
|
|
43
|
+
*/
|
|
44
|
+
cancel(reason) {
|
|
45
|
+
return this.partStream.cancel(reason);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ChatAgentPlugin } from './types';
|
|
2
|
+
import ChatAgent from './ChatAgent';
|
|
3
|
+
type ExtractPluginReturnType<T extends ChatAgentPlugin<any>[]> = T extends [infer F, ...infer R] ? F extends ChatAgentPlugin<infer U> ? R extends ChatAgentPlugin<any>[] ? U & ExtractPluginReturnType<R> : U : never : Record<string, unknown>;
|
|
4
|
+
export declare function createChatAgent<T extends ChatAgentPlugin<any>[]>(...plugins: T): ChatAgent<Omit<ExtractPluginReturnType<T>, 'hooks'>>;
|
|
5
|
+
export declare function applyChatAgentPlugins<T extends Record<string, unknown>, P extends ChatAgentPlugin<any>[]>(agent: ChatAgent<T>, ...plugins: P): ChatAgent<T & ExtractPluginReturnType<P>>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
2
|
+
import ChatAgent from './ChatAgent';
|
|
3
|
+
export function createChatAgent() {
|
|
4
|
+
const agent = new ChatAgent();
|
|
5
|
+
for (var _len = arguments.length, plugins = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
6
|
+
plugins[_key] = arguments[_key];
|
|
7
|
+
}
|
|
8
|
+
for (const plugin of plugins) {
|
|
9
|
+
agent.applyPlugin(plugin);
|
|
10
|
+
}
|
|
11
|
+
return agent;
|
|
12
|
+
}
|
|
13
|
+
export function applyChatAgentPlugins(agent) {
|
|
14
|
+
for (var _len2 = arguments.length, plugins = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
15
|
+
plugins[_key2 - 1] = arguments[_key2];
|
|
16
|
+
}
|
|
17
|
+
for (const plugin of plugins) {
|
|
18
|
+
agent.applyPlugin(plugin);
|
|
19
|
+
}
|
|
20
|
+
return agent;
|
|
21
|
+
}
|