@blueking/chat-helper 0.0.1-beta.9 → 0.0.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.
- package/README.md +631 -141
- package/dist/agent/type.d.ts +27 -2
- package/dist/agent/type.ts.js +1 -1
- package/dist/agent/use-agent.d.ts +464 -484
- package/dist/agent/use-agent.ts.js +191 -42
- package/dist/event/ag-ui.d.ts +37 -7
- package/dist/event/ag-ui.ts.js +225 -173
- package/dist/event/type.d.ts +99 -107
- package/dist/event/type.ts.js +34 -11
- package/dist/http/fetch/fetch.d.ts +2 -1
- package/dist/http/fetch/fetch.ts.js +38 -8
- package/dist/http/fetch/index.d.ts +1 -0
- package/dist/http/fetch/index.ts.js +17 -1
- package/dist/http/index.d.ts +14 -5
- package/dist/http/index.ts.js +59 -3
- package/dist/http/module/index.d.ts +13 -5
- package/dist/http/module/index.ts.js +2 -1
- package/dist/http/module/message.d.ts +22 -5
- package/dist/http/module/message.ts.js +51 -4
- package/dist/http/module/session.d.ts +4 -1
- package/dist/http/module/session.ts.js +66 -4
- package/dist/http/transform/agent.ts.js +11 -8
- package/dist/http/transform/message.d.ts +6 -6
- package/dist/http/transform/message.ts.js +566 -118
- package/dist/http/transform/session.ts.js +9 -1
- package/dist/index.d.ts +2983 -3314
- package/dist/index.ts.js +27 -5
- package/dist/mediator/index.d.ts +2 -0
- package/dist/mediator/index.ts.js +26 -0
- package/dist/mediator/type.d.ts +50 -0
- package/dist/mediator/type.ts.js +28 -0
- package/dist/mediator/use-mediator.d.ts +7 -0
- package/dist/mediator/use-mediator.ts.js +47 -0
- package/dist/message/type.d.ts +280 -146
- package/dist/message/type.ts.js +16 -15
- package/dist/message/use-message.d.ts +847 -963
- package/dist/message/use-message.ts.js +230 -37
- package/dist/session/type.d.ts +10 -0
- package/dist/session/use-session.d.ts +2006 -2214
- package/dist/session/use-session.ts.js +198 -33
- package/package.json +11 -6
|
@@ -81,75 +81,224 @@ function _object_spread(target) {
|
|
|
81
81
|
}
|
|
82
82
|
import { ref } from 'vue';
|
|
83
83
|
import { AGUIProtocol } from '../event/index.ts.js';
|
|
84
|
-
import {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
import { MessageRole, MessageStatus } from '../message/index.ts.js';
|
|
85
|
+
/**
|
|
86
|
+
* Agent 模块
|
|
87
|
+
* @param options - 配置选项
|
|
88
|
+
* @param mediator - 中介者模块,用于获取其他模块的引用
|
|
89
|
+
*/ export const useAgent = (mediator, protocol)=>{
|
|
88
90
|
const info = ref(null);
|
|
89
91
|
const isInfoLoading = ref(false);
|
|
90
|
-
|
|
92
|
+
const isChatting = ref(false);
|
|
93
|
+
let usedProtocol = protocol || new AGUIProtocol();
|
|
94
|
+
let abortController = null;
|
|
91
95
|
const getAgentInfo = ()=>{
|
|
96
|
+
var _mediator_http;
|
|
92
97
|
isInfoLoading.value = true;
|
|
93
|
-
return http.agent.getAgentInfo().then((res)=>{
|
|
98
|
+
return (_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.agent.getAgentInfo().then((res)=>{
|
|
94
99
|
info.value = res;
|
|
95
|
-
})
|
|
100
|
+
})['finally'](()=>{
|
|
96
101
|
isInfoLoading.value = false;
|
|
97
102
|
});
|
|
98
103
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
yield message.plusMessage({
|
|
104
|
+
// 处理角色消息
|
|
105
|
+
const handleRole = (data, sessionCode)=>{
|
|
106
|
+
var _data_promptSetting_content, _data_promptSetting;
|
|
107
|
+
const lastRoleMessage = (_data_promptSetting = data.promptSetting) === null || _data_promptSetting === void 0 ? void 0 : (_data_promptSetting_content = _data_promptSetting.content) === null || _data_promptSetting_content === void 0 ? void 0 : _data_promptSetting_content.at(-1);
|
|
108
|
+
if ((lastRoleMessage === null || lastRoleMessage === void 0 ? void 0 : lastRoleMessage.role) === MessageRole.Pause) {
|
|
109
|
+
var _mediator_message;
|
|
110
|
+
(_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.createAndPlusMessage({
|
|
111
|
+
role: MessageRole.Assistant,
|
|
112
|
+
content: lastRoleMessage.content,
|
|
113
|
+
status: MessageStatus.Complete,
|
|
110
114
|
sessionCode,
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
type: MessageContentType.Text,
|
|
115
|
-
data: {
|
|
116
|
-
text: userInput
|
|
117
|
-
},
|
|
118
|
-
status: MessageContentStatus.Complete
|
|
115
|
+
property: {
|
|
116
|
+
extra: {
|
|
117
|
+
pause: true
|
|
119
118
|
}
|
|
120
|
-
|
|
119
|
+
}
|
|
121
120
|
});
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const streamRequest = function() {
|
|
124
|
+
var _ref = _async_to_generator(function*(sessionCode, url, config) {
|
|
125
|
+
var // 发起聊天
|
|
126
|
+
_mediator_http;
|
|
127
|
+
// ag-ui 协议需要注入消息模块
|
|
128
|
+
if (usedProtocol instanceof AGUIProtocol) {
|
|
129
|
+
usedProtocol.injectMessageModule(mediator.message);
|
|
130
|
+
}
|
|
131
|
+
// 事件代理
|
|
132
|
+
const onDone = ()=>{
|
|
133
|
+
var _usedProtocol_onDone;
|
|
134
|
+
isChatting.value = false;
|
|
135
|
+
(_usedProtocol_onDone = usedProtocol.onDone) === null || _usedProtocol_onDone === void 0 ? void 0 : _usedProtocol_onDone.call(usedProtocol);
|
|
136
|
+
};
|
|
137
|
+
const onError = (error)=>{
|
|
138
|
+
var _usedProtocol_onError;
|
|
139
|
+
isChatting.value = false;
|
|
140
|
+
(_usedProtocol_onError = usedProtocol.onError) === null || _usedProtocol_onError === void 0 ? void 0 : _usedProtocol_onError.call(usedProtocol, error);
|
|
141
|
+
};
|
|
142
|
+
const onMessage = (event)=>{
|
|
143
|
+
var _usedProtocol_onMessage;
|
|
144
|
+
(_usedProtocol_onMessage = usedProtocol.onMessage) === null || _usedProtocol_onMessage === void 0 ? void 0 : _usedProtocol_onMessage.call(usedProtocol, event);
|
|
145
|
+
};
|
|
146
|
+
const onStart = ()=>{
|
|
147
|
+
var _usedProtocol_onStart;
|
|
148
|
+
isChatting.value = true;
|
|
149
|
+
(_usedProtocol_onStart = usedProtocol.onStart) === null || _usedProtocol_onStart === void 0 ? void 0 : _usedProtocol_onStart.call(usedProtocol);
|
|
150
|
+
};
|
|
151
|
+
// 创建 AbortController
|
|
152
|
+
abortController = new AbortController();
|
|
153
|
+
(_mediator_http = mediator.http) === null || _mediator_http === void 0 ? void 0 : _mediator_http.fetchClient.streamRequest(_object_spread({
|
|
124
154
|
url: url || 'chat_completion/',
|
|
155
|
+
method: 'POST',
|
|
125
156
|
data: {
|
|
126
|
-
session_code: sessionCode
|
|
157
|
+
session_code: sessionCode,
|
|
158
|
+
execute_kwargs: {
|
|
159
|
+
stream: true
|
|
160
|
+
}
|
|
127
161
|
},
|
|
128
|
-
controller:
|
|
129
|
-
onDone
|
|
130
|
-
onError
|
|
131
|
-
onMessage
|
|
132
|
-
onStart
|
|
162
|
+
controller: abortController,
|
|
163
|
+
onDone,
|
|
164
|
+
onError,
|
|
165
|
+
onMessage,
|
|
166
|
+
onStart
|
|
133
167
|
}, config));
|
|
134
168
|
});
|
|
135
|
-
return function
|
|
169
|
+
return function streamRequest(sessionCode, url, config) {
|
|
136
170
|
return _ref.apply(this, arguments);
|
|
137
171
|
};
|
|
138
172
|
}();
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
173
|
+
/**
|
|
174
|
+
* 发送聊天消息
|
|
175
|
+
* @param userInput - 用户输入的消息内容
|
|
176
|
+
* @param sessionCode - 会话代码
|
|
177
|
+
* @param url - 请求 URL(可选)
|
|
178
|
+
* @param config - 请求配置(可选)
|
|
179
|
+
* @param property - 消息属性,用于传递引用内容或快捷键相关信息(可选)
|
|
180
|
+
*/ const chat = function() {
|
|
181
|
+
var _ref = _async_to_generator(function*(userInput, sessionCode, url, config, property) {
|
|
182
|
+
var _mediator_message;
|
|
183
|
+
// 先新增一个 message
|
|
184
|
+
yield (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.createAndPlusMessage(_object_spread({
|
|
185
|
+
role: MessageRole.User,
|
|
186
|
+
content: userInput,
|
|
187
|
+
status: MessageStatus.Complete,
|
|
188
|
+
sessionCode
|
|
189
|
+
}, property && {
|
|
190
|
+
property
|
|
191
|
+
}));
|
|
192
|
+
// 发起聊天
|
|
193
|
+
streamRequest(sessionCode, url, config);
|
|
143
194
|
});
|
|
144
|
-
return function
|
|
195
|
+
return function chat(userInput, sessionCode, url, config, property) {
|
|
145
196
|
return _ref.apply(this, arguments);
|
|
146
197
|
};
|
|
147
198
|
}();
|
|
199
|
+
/**
|
|
200
|
+
* 恢复流式聊天
|
|
201
|
+
* 如果最后一条消息处于流式传输中或是用户消息,重新建立连接
|
|
202
|
+
* @param sessionCode - 会话代码
|
|
203
|
+
* @param url - 请求 URL(可选)
|
|
204
|
+
* @param config - 请求配置(可选)
|
|
205
|
+
*/ const resumeStreamingChat = (sessionCode, url, config)=>{
|
|
206
|
+
var _mediator_message;
|
|
207
|
+
const lastMessage = (_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.list.value.at(-1);
|
|
208
|
+
if ((lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.status) === MessageStatus.Streaming || (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.role) === MessageRole.User) {
|
|
209
|
+
streamRequest(sessionCode, url, config);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* 中止聊天(纯前端中止,后端继续处理)
|
|
214
|
+
*/ const abortChat = ()=>{
|
|
215
|
+
var _abortController_abort;
|
|
216
|
+
abortController === null || abortController === void 0 ? void 0 : (_abortController_abort = abortController.abort) === null || _abortController_abort === void 0 ? void 0 : _abortController_abort.call(abortController);
|
|
217
|
+
abortController = null;
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* 停止会话,后端中止
|
|
221
|
+
* @param sessionCode - 会话代码
|
|
222
|
+
*/ const stopChat = function() {
|
|
223
|
+
var _ref = _async_to_generator(function*(sessionCode) {
|
|
224
|
+
var _mediator_http_message;
|
|
225
|
+
return (_mediator_http_message = mediator.http.message) === null || _mediator_http_message === void 0 ? void 0 : _mediator_http_message.stopChat(sessionCode);
|
|
226
|
+
});
|
|
227
|
+
return function stopChat(sessionCode) {
|
|
228
|
+
return _ref.apply(this, arguments);
|
|
229
|
+
};
|
|
230
|
+
}();
|
|
231
|
+
/**
|
|
232
|
+
* 重新发送消息(乐观更新)
|
|
233
|
+
* 删除指定用户消息及其后续所有消息,同时创建新消息并重新发送
|
|
234
|
+
*
|
|
235
|
+
* @param messageId - 用户消息 ID(id 字段)
|
|
236
|
+
* @param sessionCode - 会话编码
|
|
237
|
+
* @param newContent - 新内容(可选,不传则使用原消息内容;支持多模态)
|
|
238
|
+
* @param url - 请求 URL(可选)
|
|
239
|
+
* @param config - 请求配置(可选)
|
|
240
|
+
*/ const resendMessage = function() {
|
|
241
|
+
var _ref = _async_to_generator(function*(messageId, sessionCode, newContent, url, config) {
|
|
242
|
+
var _mediator_message, _mediator_message1, _mediator_message2;
|
|
243
|
+
const messages = ((_mediator_message = mediator.message) === null || _mediator_message === void 0 ? void 0 : _mediator_message.list.value) || [];
|
|
244
|
+
// 1. 找到目标用户消息
|
|
245
|
+
const messageIndex = messages.findIndex((m)=>String(m.id) === messageId);
|
|
246
|
+
if (messageIndex === -1) {
|
|
247
|
+
throw new Error(`Message not found: ${messageId}`);
|
|
248
|
+
}
|
|
249
|
+
const targetMessage = messages[messageIndex];
|
|
250
|
+
if (targetMessage.role !== MessageRole.User) {
|
|
251
|
+
throw new Error('Can only resend user messages');
|
|
252
|
+
}
|
|
253
|
+
// 2. 获取原消息内容和 property(在删除前保存)
|
|
254
|
+
const originalContent = targetMessage.content;
|
|
255
|
+
const originalProperty = targetMessage.property;
|
|
256
|
+
// 3. 确定最终发送的内容
|
|
257
|
+
const finalContent = newContent !== null && newContent !== void 0 ? newContent : originalContent;
|
|
258
|
+
// 4. 收集需要删除的消息(目标用户消息 + 后续所有消息)
|
|
259
|
+
const messagesToDelete = messages.slice(messageIndex);
|
|
260
|
+
// 5. 并行执行删除和创建(乐观更新:立即更新 UI,API 调用在后台进行)
|
|
261
|
+
// 不等待 API 返回,让 UI 立即响应
|
|
262
|
+
const deletePromise = (_mediator_message1 = mediator.message) === null || _mediator_message1 === void 0 ? void 0 : _mediator_message1.deleteMessages(messagesToDelete);
|
|
263
|
+
const createPromise = (_mediator_message2 = mediator.message) === null || _mediator_message2 === void 0 ? void 0 : _mediator_message2.createAndPlusMessage(_object_spread({
|
|
264
|
+
role: MessageRole.User,
|
|
265
|
+
content: finalContent,
|
|
266
|
+
status: MessageStatus.Complete,
|
|
267
|
+
sessionCode
|
|
268
|
+
}, originalProperty && {
|
|
269
|
+
property: originalProperty
|
|
270
|
+
}));
|
|
271
|
+
// 6. 立即发起流式请求(不等待删除和创建的 API 完成)
|
|
272
|
+
streamRequest(sessionCode, url, config);
|
|
273
|
+
// 7. 在后台等待 API 完成,处理可能的错误
|
|
274
|
+
Promise.all([
|
|
275
|
+
deletePromise,
|
|
276
|
+
createPromise
|
|
277
|
+
])['catch']((error)=>{
|
|
278
|
+
console.error('[resendMessage] API error:', error);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
return function resendMessage(messageId, sessionCode, newContent, url, config) {
|
|
282
|
+
return _ref.apply(this, arguments);
|
|
283
|
+
};
|
|
284
|
+
}();
|
|
285
|
+
const reset = (protocol)=>{
|
|
286
|
+
// 重置状态
|
|
287
|
+
usedProtocol = protocol || new AGUIProtocol();
|
|
288
|
+
info.value = null;
|
|
289
|
+
isInfoLoading.value = false;
|
|
290
|
+
};
|
|
148
291
|
return {
|
|
149
292
|
info,
|
|
150
293
|
isInfoLoading,
|
|
294
|
+
isChatting,
|
|
151
295
|
chat,
|
|
296
|
+
handleRole,
|
|
297
|
+
resendMessage,
|
|
298
|
+
resumeStreamingChat,
|
|
299
|
+
abortChat,
|
|
152
300
|
stopChat,
|
|
153
|
-
getAgentInfo
|
|
301
|
+
getAgentInfo,
|
|
302
|
+
reset
|
|
154
303
|
};
|
|
155
304
|
};
|
package/dist/event/ag-ui.d.ts
CHANGED
|
@@ -11,7 +11,6 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
11
11
|
private onErrorCallback?;
|
|
12
12
|
private onMessageCallback?;
|
|
13
13
|
private onStartCallback?;
|
|
14
|
-
private sessionCode;
|
|
15
14
|
constructor(params?: {
|
|
16
15
|
onDone?: () => void;
|
|
17
16
|
onError?: (error: unknown) => void;
|
|
@@ -32,6 +31,34 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
32
31
|
* 处理自定义事件
|
|
33
32
|
*/
|
|
34
33
|
handleCustomEvent(event: ICustomEvent): void;
|
|
34
|
+
/**
|
|
35
|
+
* 自定义事件 处理流程编排任务结束
|
|
36
|
+
*/
|
|
37
|
+
handleFlowAgentEndCustomEvent(event: ICustomEvent): void;
|
|
38
|
+
/**
|
|
39
|
+
* 自定义事件 处理流程编排任务结果(增量更新节点状态)
|
|
40
|
+
*/
|
|
41
|
+
handleFlowAgentResultCustomEvent(event: ICustomEvent): void;
|
|
42
|
+
/**
|
|
43
|
+
* 自定义事件 处理流程编排任务开始
|
|
44
|
+
*/
|
|
45
|
+
handleFlowAgentStartCustomEvent(event: ICustomEvent): void;
|
|
46
|
+
/**
|
|
47
|
+
* 自定义事件 处理意图识别结束
|
|
48
|
+
*/
|
|
49
|
+
handleKnowledgeRagEndCustomEvent(_event: ICustomEvent): void;
|
|
50
|
+
/**
|
|
51
|
+
* 自定义事件 处理意图识别结果
|
|
52
|
+
*/
|
|
53
|
+
handleKnowledgeRagResultCustomEvent(event: ICustomEvent): void;
|
|
54
|
+
/**
|
|
55
|
+
* 自定义事件 处理意图识别开始
|
|
56
|
+
*/
|
|
57
|
+
handleKnowledgeRagStartCustomEvent(_event: ICustomEvent): void;
|
|
58
|
+
/**
|
|
59
|
+
* 自定义事件 处理意图识别文本
|
|
60
|
+
*/
|
|
61
|
+
handleKnowledgeRagTextContentCustomEvent(event: ICustomEvent): void;
|
|
35
62
|
/**
|
|
36
63
|
* 处理消息快照事件
|
|
37
64
|
* 用于同步多端消息状态
|
|
@@ -53,7 +80,7 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
53
80
|
/**
|
|
54
81
|
* 处理运行完成事件
|
|
55
82
|
*/
|
|
56
|
-
handleRunFinishedEvent(
|
|
83
|
+
handleRunFinishedEvent(_event: IRunFinishedEvent): void;
|
|
57
84
|
/**
|
|
58
85
|
* 处理运行开始事件
|
|
59
86
|
*/
|
|
@@ -75,11 +102,15 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
75
102
|
*/
|
|
76
103
|
handleStepStartedEvent(_event: IStepStartedEvent): void;
|
|
77
104
|
/**
|
|
78
|
-
*
|
|
105
|
+
* 自定义事件 处理临时消息,转换为 AI 文本消息
|
|
106
|
+
*/
|
|
107
|
+
handleTempMessageCustomEvent(event: ICustomEvent): void;
|
|
108
|
+
/**
|
|
109
|
+
* 处理文本消息块事件
|
|
79
110
|
*/
|
|
80
111
|
handleTextMessageChunkEvent(event: ITextMessageChunkEvent): void;
|
|
81
112
|
/**
|
|
82
|
-
*
|
|
113
|
+
* 处理文本消息内容事件(流式输出)
|
|
83
114
|
*/
|
|
84
115
|
handleTextMessageContentEvent(event: ITextMessageContentEvent): void;
|
|
85
116
|
/**
|
|
@@ -97,7 +128,7 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
97
128
|
/**
|
|
98
129
|
* 处理思考开始事件
|
|
99
130
|
*/
|
|
100
|
-
handleThinkingStartEvent(
|
|
131
|
+
handleThinkingStartEvent(_event: IThinkingStartEvent): void;
|
|
101
132
|
/**
|
|
102
133
|
* 处理思考文本消息内容事件
|
|
103
134
|
*/
|
|
@@ -117,7 +148,7 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
117
148
|
/**
|
|
118
149
|
* 处理工具调用块事件
|
|
119
150
|
*/
|
|
120
|
-
handleToolCallChunkEvent(
|
|
151
|
+
handleToolCallChunkEvent(_event: IToolCallChunkEvent): void;
|
|
121
152
|
/**
|
|
122
153
|
* 处理工具调用结束事件
|
|
123
154
|
*/
|
|
@@ -131,7 +162,6 @@ export declare class AGUIProtocol implements ISSEProtocol {
|
|
|
131
162
|
*/
|
|
132
163
|
handleToolCallStartEvent(event: IToolCallStartEvent): void;
|
|
133
164
|
injectMessageModule(message: IMessageModule): void;
|
|
134
|
-
injectSessionCode(sessionCode: string): void;
|
|
135
165
|
onDone(): void;
|
|
136
166
|
onError(error: Error): void;
|
|
137
167
|
onMessage(message: unknown): void;
|