@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,157 @@
|
|
|
1
|
+
import { Hookable } from 'hookable';
|
|
2
|
+
import { AbortSignalObject, AddHookCallback, ChatAgentHooks, ChatAgentHooksWithOrder, ChatAgentPlugin, ComposeHandler, InputBlock, ChatMessageObject, StreamResponseInstance } from './types';
|
|
3
|
+
import ChatSession from './ChatSession';
|
|
4
|
+
import ChatMessage from './ChatMessage';
|
|
5
|
+
type AddHook<T extends keyof ChatAgentHooks> = AddHookCallback<ChatAgentHooks, T>;
|
|
6
|
+
/**
|
|
7
|
+
* ChatAgent
|
|
8
|
+
*
|
|
9
|
+
* @description ChatAgent is the core class of the chat agent, which is responsible for managing the chat session and the lifecycle of the chat agent.
|
|
10
|
+
*/
|
|
11
|
+
export default class ChatAgent<P extends Record<string, unknown> = Record<string, any>> {
|
|
12
|
+
private started;
|
|
13
|
+
readonly hooks: Hookable<ChatAgentHooksWithOrder>;
|
|
14
|
+
/**
|
|
15
|
+
* ChatSession
|
|
16
|
+
* The chat session associated with the chat agent
|
|
17
|
+
*
|
|
18
|
+
* @description ChatSession is used to manage the chat session, including the messages, tiles, etc.
|
|
19
|
+
*/
|
|
20
|
+
readonly session: ChatSession;
|
|
21
|
+
/**
|
|
22
|
+
* onAgentStart hook
|
|
23
|
+
* This hook is called after the chat agent starts, you can use it to initialize the chat agent.
|
|
24
|
+
*
|
|
25
|
+
* @param fn hook callback
|
|
26
|
+
*/
|
|
27
|
+
readonly onAgentStart: AddHook<'onAgentStart'>;
|
|
28
|
+
/**
|
|
29
|
+
* onChatStart hook
|
|
30
|
+
* This hook is called after the chat agent starts a new chat, you can use it to initialize messages.
|
|
31
|
+
*
|
|
32
|
+
* @param fn hook callback
|
|
33
|
+
*/
|
|
34
|
+
readonly onChatStart: AddHook<'onChatStart'>;
|
|
35
|
+
/**
|
|
36
|
+
* onChatResume hook
|
|
37
|
+
* This hook is called after the chat agent resumes a chat, you can use it to resume the messages.
|
|
38
|
+
*
|
|
39
|
+
* @param fn hook callback
|
|
40
|
+
*/
|
|
41
|
+
readonly onChatResume: AddHook<'onChatResume'>;
|
|
42
|
+
/**
|
|
43
|
+
* onMessageListInit hook
|
|
44
|
+
* This hook is called after onChatStart or onChatResume, it is used to initialize the message list, prepare the messages to show.
|
|
45
|
+
*
|
|
46
|
+
* @param fn hook callback
|
|
47
|
+
*/
|
|
48
|
+
readonly onMessageListInit: AddHook<'onMessageListInit'>;
|
|
49
|
+
/**
|
|
50
|
+
* onInputBlocksPush hook
|
|
51
|
+
* This hook is called after the input blocks are pushed to the chat agent, you can use it to handle the input blocks, and send the input blocks to the assistant.
|
|
52
|
+
*
|
|
53
|
+
* @param fn hook callback
|
|
54
|
+
*/
|
|
55
|
+
readonly onInputBlocksPush: AddHook<'onInputBlocksPush'>;
|
|
56
|
+
/**
|
|
57
|
+
* onMessageChange hook
|
|
58
|
+
* This hook is called after the message.show(), update() or remove(), you can use it to handle the message change.
|
|
59
|
+
*
|
|
60
|
+
* @param fn hook callback
|
|
61
|
+
*/
|
|
62
|
+
readonly onMessageChange: AddHook<'onMessageChange'>;
|
|
63
|
+
/**
|
|
64
|
+
* onMessagePersist hook
|
|
65
|
+
* This hook is called after the message.persist(), you can use it to handle the message persist to the assistant.
|
|
66
|
+
*
|
|
67
|
+
* @param fn hook callback
|
|
68
|
+
*/
|
|
69
|
+
readonly onMessagePersist: AddHook<'onMessagePersist'>;
|
|
70
|
+
/**
|
|
71
|
+
* onTileEvent hook
|
|
72
|
+
* This hook is called after the tile event is emitted, agent.emitTileEvent(), you can use it to handle the tile event.
|
|
73
|
+
*
|
|
74
|
+
* @param fn hook callback
|
|
75
|
+
*/
|
|
76
|
+
readonly onTileEvent: AddHook<'onTileEvent'>;
|
|
77
|
+
/**
|
|
78
|
+
* onAgentDispose hook
|
|
79
|
+
* This hook is called before the chat agent is disposed, you can use it to clean up the resources.
|
|
80
|
+
*
|
|
81
|
+
* @param fn hook callback
|
|
82
|
+
*/
|
|
83
|
+
readonly onAgentDispose: AddHook<'onAgentDispose'>;
|
|
84
|
+
/**
|
|
85
|
+
* onUserAbort hook
|
|
86
|
+
* This hook is called after the user aborts the chat, you can use it to handle the user abort event.
|
|
87
|
+
*
|
|
88
|
+
* @param fn hook callback
|
|
89
|
+
*/
|
|
90
|
+
readonly onUserAbort: AddHook<'onUserAbort'>;
|
|
91
|
+
/**
|
|
92
|
+
* onError hook
|
|
93
|
+
* This hook is called after an error occurs, you can use it to handle the error.
|
|
94
|
+
*
|
|
95
|
+
* @param fn hook callback
|
|
96
|
+
*/
|
|
97
|
+
readonly onError: AddHook<'onError'>;
|
|
98
|
+
pluginHooks: Hookable[];
|
|
99
|
+
/**
|
|
100
|
+
* Plugins
|
|
101
|
+
* The plugins applied to the chat agent
|
|
102
|
+
*
|
|
103
|
+
* @description Plugins are used to extend the functionality of the chat agent, you can apply a plugin to the chat agent by calling the applyPlugin method.
|
|
104
|
+
*/
|
|
105
|
+
plugins: P;
|
|
106
|
+
constructor();
|
|
107
|
+
runWithCatchError: <T>(fn: () => T) => T;
|
|
108
|
+
/**
|
|
109
|
+
* Apply a plugin to the chat agent
|
|
110
|
+
*
|
|
111
|
+
* @param plugin ChatAgentPlugin
|
|
112
|
+
*/
|
|
113
|
+
applyPlugin: <T extends Record<string, unknown>>(plugin: ChatAgentPlugin<T>) => void;
|
|
114
|
+
/**
|
|
115
|
+
* Start the chat agent
|
|
116
|
+
*/
|
|
117
|
+
start: () => Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Dispose the chat agent
|
|
120
|
+
*/
|
|
121
|
+
dispose: () => Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Push input blocks to the chat agent, which will trigger the onInputBlocksPush hook
|
|
124
|
+
*
|
|
125
|
+
* @param blocks input blocks, including text, image, video, etc.
|
|
126
|
+
* @param signal abort signal
|
|
127
|
+
*/
|
|
128
|
+
pushInputBlocks: (blocks: InputBlock[], signal?: AbortSignalObject) => Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Create a new message object associated with the chat agent
|
|
131
|
+
*
|
|
132
|
+
* @param data message data, including role, status, etc.
|
|
133
|
+
*/
|
|
134
|
+
createMessage: (data?: Partial<ChatMessageObject>) => ChatMessage;
|
|
135
|
+
/**
|
|
136
|
+
* Persist a message to the chat agent, which will trigger the onMessagePersist hook
|
|
137
|
+
*
|
|
138
|
+
* @param tileId tile id
|
|
139
|
+
* @param payload message payload
|
|
140
|
+
*/
|
|
141
|
+
emitTileEvent: (tileId: string, payload: any) => Promise<void>;
|
|
142
|
+
/**
|
|
143
|
+
* Remove a message to the chat agent, which will trigger the onMessageChange hook
|
|
144
|
+
*
|
|
145
|
+
* @param messageId message id
|
|
146
|
+
*/
|
|
147
|
+
removeMessage: (messageId: string) => Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* Start a stream to show the response from the StreamResponseInstance, which will trigger the onMessageUpdate hook
|
|
150
|
+
*
|
|
151
|
+
* @param message render message instance
|
|
152
|
+
* @param response stream response instance
|
|
153
|
+
* @param composeHandler compose handler
|
|
154
|
+
*/
|
|
155
|
+
flushStreamToShow: (message: ChatMessage, response: StreamResponseInstance, composeHandler: ComposeHandler) => Promise<ChatMessage[]>;
|
|
156
|
+
}
|
|
157
|
+
export {};
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
4
|
+
const _excluded = ["hooks"];
|
|
5
|
+
import "core-js/modules/es.symbol.description.js";
|
|
6
|
+
import "core-js/modules/es.symbol.async-iterator.js";
|
|
7
|
+
import "core-js/modules/es.promise.finally.js";
|
|
8
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
9
|
+
function _asyncIterator(r) { var n, t, o, e = 2; for ("undefined" != typeof Symbol && (t = Symbol.asyncIterator, o = Symbol.iterator); e--;) { if (t && null != (n = r[t])) return n.call(r); if (o && null != (n = r[o])) return new AsyncFromSyncIterator(n.call(r)); t = "@@asyncIterator", o = "@@iterator"; } throw new TypeError("Object is not async iterable"); }
|
|
10
|
+
function AsyncFromSyncIterator(r) { function AsyncFromSyncIteratorContinuation(r) { if (Object(r) !== r) return Promise.reject(new TypeError(r + " is not an object.")); var n = r.done; return Promise.resolve(r.value).then(function (r) { return { value: r, done: n }; }); } return AsyncFromSyncIterator = function (r) { this.s = r, this.n = r.next; }, AsyncFromSyncIterator.prototype = { s: null, n: null, next: function () { return AsyncFromSyncIteratorContinuation(this.n.apply(this.s, arguments)); }, return: function (r) { var n = this.s.return; return void 0 === n ? Promise.resolve({ value: r, done: !0 }) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); }, throw: function (r) { var n = this.s.return; return void 0 === n ? Promise.reject(r) : AsyncFromSyncIteratorContinuation(n.apply(this.s, arguments)); } }, new AsyncFromSyncIterator(r); }
|
|
11
|
+
import { createHooks } from 'hookable';
|
|
12
|
+
import { BubbleTileStatus, ChatMessageStatus } from './types';
|
|
13
|
+
import ChatSession from './ChatSession';
|
|
14
|
+
import ChatMessage from './ChatMessage';
|
|
15
|
+
import { getLogger } from './Logger';
|
|
16
|
+
import { isAbortError } from './utils';
|
|
17
|
+
const logger = getLogger('TAgent/ChatAgent');
|
|
18
|
+
/**
|
|
19
|
+
* ChatAgent
|
|
20
|
+
*
|
|
21
|
+
* @description ChatAgent is the core class of the chat agent, which is responsible for managing the chat session and the lifecycle of the chat agent.
|
|
22
|
+
*/
|
|
23
|
+
export default class ChatAgent {
|
|
24
|
+
constructor() {
|
|
25
|
+
_defineProperty(this, "started", false);
|
|
26
|
+
_defineProperty(this, "pluginHooks", []);
|
|
27
|
+
/**
|
|
28
|
+
* Plugins
|
|
29
|
+
* The plugins applied to the chat agent
|
|
30
|
+
*
|
|
31
|
+
* @description Plugins are used to extend the functionality of the chat agent, you can apply a plugin to the chat agent by calling the applyPlugin method.
|
|
32
|
+
*/
|
|
33
|
+
_defineProperty(this, "plugins", {});
|
|
34
|
+
_defineProperty(this, "runWithCatchError", fn => {
|
|
35
|
+
try {
|
|
36
|
+
const ret = fn();
|
|
37
|
+
if (ret && ret.then) {
|
|
38
|
+
return ret.catch(error => {
|
|
39
|
+
this.hooks.callHook('onError:before', error).finally(() => this.hooks.callHook('onError', error)).finally(() => this.hooks.callHook('onError:after', error));
|
|
40
|
+
return Promise.reject(error);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return ret;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
this.hooks.callHook('onError:before', error).finally(() => this.hooks.callHook('onError', error)).finally(() => this.hooks.callHook('onError:after', error));
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
/**
|
|
50
|
+
* Apply a plugin to the chat agent
|
|
51
|
+
*
|
|
52
|
+
* @param plugin ChatAgentPlugin
|
|
53
|
+
*/
|
|
54
|
+
_defineProperty(this, "applyPlugin", plugin => {
|
|
55
|
+
const handler = plugin(this);
|
|
56
|
+
if (handler) {
|
|
57
|
+
const {
|
|
58
|
+
hooks
|
|
59
|
+
} = handler,
|
|
60
|
+
rest = _objectWithoutProperties(handler, _excluded);
|
|
61
|
+
if (hooks) {
|
|
62
|
+
this.pluginHooks.push(hooks);
|
|
63
|
+
}
|
|
64
|
+
this.plugins = _objectSpread(_objectSpread({}, this.plugins), rest);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
/**
|
|
68
|
+
* Start the chat agent
|
|
69
|
+
*/
|
|
70
|
+
_defineProperty(this, "start", () => {
|
|
71
|
+
return this.runWithCatchError(async () => {
|
|
72
|
+
if (this.started) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
await this.hooks.callHook('onAgentStart:before');
|
|
76
|
+
this.started = true;
|
|
77
|
+
await this.hooks.callHook('onAgentStart');
|
|
78
|
+
const result = {
|
|
79
|
+
messages: []
|
|
80
|
+
};
|
|
81
|
+
if (this.session.isNewChat) {
|
|
82
|
+
await this.hooks.callHook('onChatStart:before', result);
|
|
83
|
+
await this.hooks.callHook('onChatStart', result);
|
|
84
|
+
await this.hooks.callHook('onChatStart:after', result);
|
|
85
|
+
logger.debug('onChatStart', result);
|
|
86
|
+
} else {
|
|
87
|
+
await this.hooks.callHook('onChatResume:before', result);
|
|
88
|
+
await this.hooks.callHook('onChatResume', result);
|
|
89
|
+
await this.hooks.callHook('onChatResume:after', result);
|
|
90
|
+
logger.debug('onChatResume', result);
|
|
91
|
+
}
|
|
92
|
+
await this.hooks.callHook('onMessageListInit:before', result);
|
|
93
|
+
await this.hooks.callHook('onMessageListInit', result);
|
|
94
|
+
this.session.initMessages(result.messages);
|
|
95
|
+
await this.hooks.callHook('onMessageListInit:after', result);
|
|
96
|
+
await this.hooks.callHook('onAgentStart:after');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
/**
|
|
100
|
+
* Dispose the chat agent
|
|
101
|
+
*/
|
|
102
|
+
_defineProperty(this, "dispose", () => {
|
|
103
|
+
return this.runWithCatchError(async () => {
|
|
104
|
+
await this.hooks.callHook('onAgentDispose:before');
|
|
105
|
+
await this.hooks.callHook('onAgentDispose');
|
|
106
|
+
await this.session.dispose();
|
|
107
|
+
this.started = false;
|
|
108
|
+
for (const hook of this.pluginHooks) {
|
|
109
|
+
hook.removeAllHooks();
|
|
110
|
+
}
|
|
111
|
+
await this.hooks.callHook('onAgentDispose:after');
|
|
112
|
+
this.hooks.removeAllHooks();
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
/**
|
|
116
|
+
* Push input blocks to the chat agent, which will trigger the onInputBlocksPush hook
|
|
117
|
+
*
|
|
118
|
+
* @param blocks input blocks, including text, image, video, etc.
|
|
119
|
+
* @param signal abort signal
|
|
120
|
+
*/
|
|
121
|
+
_defineProperty(this, "pushInputBlocks", (blocks, signal) => {
|
|
122
|
+
logger.debug('pushInputBlocks', blocks);
|
|
123
|
+
return this.runWithCatchError(async () => {
|
|
124
|
+
await this.hooks.callHook('onInputBlocksPush:before', blocks, signal);
|
|
125
|
+
await this.hooks.callHook('onInputBlocksPush', blocks, signal);
|
|
126
|
+
await this.hooks.callHook('onInputBlocksPush:after', blocks, signal);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
/**
|
|
130
|
+
* Create a new message object associated with the chat agent
|
|
131
|
+
*
|
|
132
|
+
* @param data message data, including role, status, etc.
|
|
133
|
+
*/
|
|
134
|
+
_defineProperty(this, "createMessage", data => {
|
|
135
|
+
return new ChatMessage(this, data || {});
|
|
136
|
+
});
|
|
137
|
+
/**
|
|
138
|
+
* Persist a message to the chat agent, which will trigger the onMessagePersist hook
|
|
139
|
+
*
|
|
140
|
+
* @param tileId tile id
|
|
141
|
+
* @param payload message payload
|
|
142
|
+
*/
|
|
143
|
+
_defineProperty(this, "emitTileEvent", (tileId, payload) => {
|
|
144
|
+
logger.debug('emitTileEvent', {
|
|
145
|
+
tileId,
|
|
146
|
+
payload
|
|
147
|
+
});
|
|
148
|
+
return this.runWithCatchError(async () => {
|
|
149
|
+
const tile = this.session.getTileById(tileId);
|
|
150
|
+
await this.hooks.callHook('onTileEvent:before', tile, payload);
|
|
151
|
+
await this.hooks.callHook('onTileEvent', tile, payload);
|
|
152
|
+
await this.hooks.callHook('onTileEvent:after', tile, payload);
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
/**
|
|
156
|
+
* Remove a message to the chat agent, which will trigger the onMessageChange hook
|
|
157
|
+
*
|
|
158
|
+
* @param messageId message id
|
|
159
|
+
*/
|
|
160
|
+
_defineProperty(this, "removeMessage", messageId => {
|
|
161
|
+
return this.runWithCatchError(async () => {
|
|
162
|
+
const message = this.session.messages.get(messageId);
|
|
163
|
+
if (!message) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
await message.remove();
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
/**
|
|
170
|
+
* Start a stream to show the response from the StreamResponseInstance, which will trigger the onMessageUpdate hook
|
|
171
|
+
*
|
|
172
|
+
* @param message render message instance
|
|
173
|
+
* @param response stream response instance
|
|
174
|
+
* @param composeHandler compose handler
|
|
175
|
+
*/
|
|
176
|
+
_defineProperty(this, "flushStreamToShow", async (message, response, composeHandler) => {
|
|
177
|
+
const {
|
|
178
|
+
attachmentCompose
|
|
179
|
+
} = composeHandler;
|
|
180
|
+
let {
|
|
181
|
+
textCompose,
|
|
182
|
+
errorCompose
|
|
183
|
+
} = composeHandler;
|
|
184
|
+
message.bubble.setIsMarkdown(true);
|
|
185
|
+
if (!message.isShow) {
|
|
186
|
+
await message.show();
|
|
187
|
+
}
|
|
188
|
+
const messages = [message];
|
|
189
|
+
textCompose = textCompose || ((_, text) => Promise.resolve(text));
|
|
190
|
+
let text = await textCompose(message, '', ChatMessageStatus.START);
|
|
191
|
+
await message.bubble.setText(text).update();
|
|
192
|
+
await this.runWithCatchError(async () => {
|
|
193
|
+
try {
|
|
194
|
+
const parts = response.parts();
|
|
195
|
+
var _iteratorAbruptCompletion = false;
|
|
196
|
+
var _didIteratorError = false;
|
|
197
|
+
var _iteratorError;
|
|
198
|
+
try {
|
|
199
|
+
for (var _iterator = _asyncIterator(parts), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) {
|
|
200
|
+
const part = _step.value;
|
|
201
|
+
{
|
|
202
|
+
message.setMeta(part.meta);
|
|
203
|
+
if (part.type === 'text') {
|
|
204
|
+
text = await textCompose(message, part.text, ChatMessageStatus.UPDATING);
|
|
205
|
+
await message.set({
|
|
206
|
+
status: ChatMessageStatus.UPDATING
|
|
207
|
+
}).bubble.setText(text).update();
|
|
208
|
+
}
|
|
209
|
+
if (part.type === 'attachment' && attachmentCompose) {
|
|
210
|
+
const list = (await attachmentCompose(message, part)) || [];
|
|
211
|
+
for (const m of list) {
|
|
212
|
+
m.set({
|
|
213
|
+
status: ChatMessageStatus.FINISH
|
|
214
|
+
});
|
|
215
|
+
await m.show();
|
|
216
|
+
messages.push(m);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (part.type === 'error') {
|
|
220
|
+
if (part.level === 'error') {
|
|
221
|
+
throw part.error;
|
|
222
|
+
} else {
|
|
223
|
+
await message.bubble.setStatus(BubbleTileStatus.WARNING).setText(part.error.message).update();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} catch (err) {
|
|
229
|
+
_didIteratorError = true;
|
|
230
|
+
_iteratorError = err;
|
|
231
|
+
} finally {
|
|
232
|
+
try {
|
|
233
|
+
if (_iteratorAbruptCompletion && _iterator.return != null) {
|
|
234
|
+
await _iterator.return();
|
|
235
|
+
}
|
|
236
|
+
} finally {
|
|
237
|
+
if (_didIteratorError) {
|
|
238
|
+
throw _iteratorError;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
text = await textCompose(message, text, ChatMessageStatus.FINISH);
|
|
243
|
+
await message.set({
|
|
244
|
+
status: ChatMessageStatus.FINISH
|
|
245
|
+
}).bubble.setText(text).update();
|
|
246
|
+
} catch (error) {
|
|
247
|
+
errorCompose = errorCompose || ((_, text, error) => Promise.resolve((error === null || error === void 0 ? void 0 : error.message) || ''));
|
|
248
|
+
const info = await errorCompose(message, text, error);
|
|
249
|
+
let status = BubbleTileStatus.ERROR;
|
|
250
|
+
if (isAbortError(error)) {
|
|
251
|
+
status = BubbleTileStatus.WARNING;
|
|
252
|
+
}
|
|
253
|
+
await message.set({
|
|
254
|
+
status: ChatMessageStatus.FINISH
|
|
255
|
+
}).bubble.setStatus(status).setText(text).setInfo(info).update();
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
return messages;
|
|
260
|
+
});
|
|
261
|
+
this.hooks = createHooks();
|
|
262
|
+
const _hooks = ['onAgentStart', 'onChatStart', 'onChatResume', 'onMessageListInit', 'onInputBlocksPush', 'onMessageChange', 'onMessagePersist', 'onTileEvent', 'onAgentDispose', 'onUserAbort', 'onError'];
|
|
263
|
+
for (const hook of _hooks) {
|
|
264
|
+
this[hook] = (fn, type) => {
|
|
265
|
+
if (type === 'before') {
|
|
266
|
+
return this.hooks.hook("".concat(hook, ":before"), fn);
|
|
267
|
+
}
|
|
268
|
+
if (type === 'after') {
|
|
269
|
+
return this.hooks.hook("".concat(hook, ":after"), fn);
|
|
270
|
+
}
|
|
271
|
+
return this.hooks.hook(hook, fn);
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
this.session = new ChatSession(this);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import ChatTile from './ChatTile';
|
|
2
|
+
import ChatMessage from './ChatMessage';
|
|
3
|
+
import { BubbleTileData, BubbleTileStatus, InputBlock } from './types';
|
|
4
|
+
export default class ChatBubbleTile extends ChatTile<BubbleTileData> {
|
|
5
|
+
private textTile;
|
|
6
|
+
get isMarkdown(): any;
|
|
7
|
+
setIsMarkdown(value: boolean): this;
|
|
8
|
+
get text(): string;
|
|
9
|
+
setText(text: string): this;
|
|
10
|
+
get status(): BubbleTileStatus;
|
|
11
|
+
setStatus(status: BubbleTileStatus): this;
|
|
12
|
+
get info(): string;
|
|
13
|
+
setInfo(info: string): this;
|
|
14
|
+
constructor(_message: ChatMessage, data?: BubbleTileData, children?: ChatTile[], id?: string);
|
|
15
|
+
private ensureTextTile;
|
|
16
|
+
/**
|
|
17
|
+
* 使用 AIMessageContent[] 初始化气泡,text 会指向第一条 text content
|
|
18
|
+
* @param blocks
|
|
19
|
+
*/
|
|
20
|
+
initWithInputBlocks(blocks: InputBlock[]): this;
|
|
21
|
+
toObject(): {
|
|
22
|
+
data: {
|
|
23
|
+
status: BubbleTileStatus;
|
|
24
|
+
info: string;
|
|
25
|
+
};
|
|
26
|
+
id: string;
|
|
27
|
+
type: string;
|
|
28
|
+
locked: boolean;
|
|
29
|
+
children: import("./types").ChatTileObject<any>[];
|
|
30
|
+
fallback?: import("./types").TipTileData | undefined;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
|
3
|
+
import ChatTile from './ChatTile';
|
|
4
|
+
import { BubbleTileStatus } from './types';
|
|
5
|
+
import { generateId } from './utils';
|
|
6
|
+
export default class ChatBubbleTile extends ChatTile {
|
|
7
|
+
get isMarkdown() {
|
|
8
|
+
var _this$textTile;
|
|
9
|
+
return ((_this$textTile = this.textTile) === null || _this$textTile === void 0 || (_this$textTile = _this$textTile.data) === null || _this$textTile === void 0 ? void 0 : _this$textTile.markdown) || false;
|
|
10
|
+
}
|
|
11
|
+
setIsMarkdown(value) {
|
|
12
|
+
this.ensureTextTile();
|
|
13
|
+
this.textTile.data.markdown = value;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
get text() {
|
|
17
|
+
var _this$textTile2;
|
|
18
|
+
if (!((_this$textTile2 = this.textTile) !== null && _this$textTile2 !== void 0 && _this$textTile2.data)) {
|
|
19
|
+
return '';
|
|
20
|
+
}
|
|
21
|
+
return this.textTile.data.text;
|
|
22
|
+
}
|
|
23
|
+
setText(text) {
|
|
24
|
+
this.ensureTextTile();
|
|
25
|
+
this.textTile.data.text = text;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
get status() {
|
|
29
|
+
return this.data.status || BubbleTileStatus.NORMAL;
|
|
30
|
+
}
|
|
31
|
+
setStatus(status) {
|
|
32
|
+
this.data.status = status;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
get info() {
|
|
36
|
+
return this.data.info || '';
|
|
37
|
+
}
|
|
38
|
+
setInfo(info) {
|
|
39
|
+
this.data.info = info;
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
constructor(_message) {
|
|
43
|
+
let data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
|
|
44
|
+
status: BubbleTileStatus.NORMAL
|
|
45
|
+
};
|
|
46
|
+
let children = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
47
|
+
let id = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : generateId(16);
|
|
48
|
+
super(_message, 'bubble', data, children, id);
|
|
49
|
+
}
|
|
50
|
+
ensureTextTile() {
|
|
51
|
+
if (!this.textTile) {
|
|
52
|
+
for (const child of this.children) {
|
|
53
|
+
if (child.type === 'text') {
|
|
54
|
+
this.textTile = child;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!this.textTile) {
|
|
59
|
+
this.textTile = new ChatTile(this.message, 'text', {
|
|
60
|
+
text: ''
|
|
61
|
+
});
|
|
62
|
+
this.children.push(this.textTile);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 使用 AIMessageContent[] 初始化气泡,text 会指向第一条 text content
|
|
69
|
+
* @param blocks
|
|
70
|
+
*/
|
|
71
|
+
initWithInputBlocks(blocks) {
|
|
72
|
+
this.textTile = null;
|
|
73
|
+
this.children = [];
|
|
74
|
+
for (const block of blocks) {
|
|
75
|
+
if (block.type === 'text') {
|
|
76
|
+
if (!this.textTile) {
|
|
77
|
+
this.textTile = new ChatTile(this.message, 'text', {
|
|
78
|
+
text: block.text
|
|
79
|
+
});
|
|
80
|
+
this.setText(block.text);
|
|
81
|
+
this.children.push(this.textTile);
|
|
82
|
+
}
|
|
83
|
+
} else if (block.type === 'image_url') {
|
|
84
|
+
this.message.addTile('image', {
|
|
85
|
+
src: block.image_url.url
|
|
86
|
+
});
|
|
87
|
+
} else if (block.type === 'video_url') {
|
|
88
|
+
this.message.addTile('video', {
|
|
89
|
+
src: block.video_url.url,
|
|
90
|
+
thumbUrl: block.video_url.thumb_url
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
toObject() {
|
|
97
|
+
return _objectSpread(_objectSpread({}, super.toObject()), {}, {
|
|
98
|
+
data: {
|
|
99
|
+
status: this.status,
|
|
100
|
+
info: this.info
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ChatMessageObject, ChatMessageObjectMeta, ChatMessageStatus } from './types';
|
|
2
|
+
import ChatTile from './ChatTile';
|
|
3
|
+
import ChatBubbleTile from './ChatBubbleTile';
|
|
4
|
+
import ChatAgent from './ChatAgent';
|
|
5
|
+
export default class ChatMessage implements ChatMessageObject {
|
|
6
|
+
private agent;
|
|
7
|
+
id: string;
|
|
8
|
+
private _isShow;
|
|
9
|
+
protected _bubble: ChatBubbleTile;
|
|
10
|
+
role: 'user' | 'assistant' | string;
|
|
11
|
+
tiles: ChatTile[];
|
|
12
|
+
status: ChatMessageStatus;
|
|
13
|
+
meta: ChatMessageObjectMeta;
|
|
14
|
+
get isShow(): boolean;
|
|
15
|
+
get bubble(): ChatBubbleTile;
|
|
16
|
+
constructor(agent: ChatAgent, data: Partial<ChatMessageObject>);
|
|
17
|
+
private initTiles;
|
|
18
|
+
show(): Promise<void>;
|
|
19
|
+
update(): Promise<void>;
|
|
20
|
+
remove(): Promise<void>;
|
|
21
|
+
_setIsShow(): void;
|
|
22
|
+
persist(payload: any): Promise<void>;
|
|
23
|
+
setTilesLocked(locked: boolean): this;
|
|
24
|
+
removeTile(tile: ChatTile): void;
|
|
25
|
+
addTile(type: string, data: any, id?: string): ChatTile<any>;
|
|
26
|
+
set(data: Omit<Partial<ChatMessageObject>, 'id'>): this;
|
|
27
|
+
setMetaValue(key: string, value: any): this;
|
|
28
|
+
setMeta(meta: Record<string, any>, override?: boolean): this;
|
|
29
|
+
deleteMetaValue(key: string): this;
|
|
30
|
+
findTileByType<T>(type: string): ChatTile<T>[];
|
|
31
|
+
toObject(): ChatMessageObject;
|
|
32
|
+
}
|