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