@auipage/core 0.1.0-alpha.4 → 0.1.0
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/AUTHORS.txt +6 -6
- package/CHANGELOG +4 -4
- package/README.md +54 -54
- package/dist/index.js +584 -584
- package/package.json +2 -2
- package/src/AuiPage.js +428 -428
- package/src/ChatContentRuntime.js +18 -18
- package/src/ChatTransportAdapter.js +17 -17
- package/src/MessageHistoryAdapter.js +17 -17
- package/src/MessageStream.js +7 -7
- package/src/ThreadListAdapter.js +20 -20
- package/src/ThreadListRuntime.js +22 -22
- package/src/defineTool.js +13 -13
- package/src/generator.js +64 -64
- package/src/index.js +11 -11
- package/src/tools/toHistoryMessages.js +15 -15
- package/types/adapters/chat-transport.d.ts +57 -57
- package/types/adapters/index.d.ts +26 -26
- package/types/adapters/message-history.d.ts +35 -35
- package/types/adapters/thread-list.d.ts +45 -45
- package/types/auipage.d.ts +116 -116
- package/types/define-tool.d.ts +18 -18
- package/types/generator.d.ts +27 -27
- package/types/index.d.ts +96 -96
- package/types/message-stream.d.ts +22 -22
- package/types/message.d.ts +60 -60
- package/types/runtimes/chat-content.d.ts +65 -65
- package/types/runtimes/index.d.ts +22 -22
- package/types/runtimes/thread-list.d.ts +40 -40
- package/types/thread.d.ts +14 -14
- package/types/tool.d.ts +51 -51
package/src/AuiPage.js
CHANGED
|
@@ -1,429 +1,429 @@
|
|
|
1
|
-
import toHistoryMessages from "./tools/toHistoryMessages.js";
|
|
2
|
-
|
|
3
|
-
function AuiPage(options) {
|
|
4
|
-
|
|
5
|
-
// 连接适配器和运行时视图
|
|
6
|
-
this.adapter = {};
|
|
7
|
-
this.runtime = {};
|
|
8
|
-
for (let instance of Object.values(options.adapter)) {
|
|
9
|
-
instance.aui = this;
|
|
10
|
-
this.adapter[instance.name.replace("Adapter", "")] = instance;
|
|
11
|
-
}
|
|
12
|
-
for (let instance of Object.values(options.runtime)) {
|
|
13
|
-
instance.aui = this;
|
|
14
|
-
this.runtime[instance.name.replace("Runtime", "")] = instance;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// 工具注册
|
|
18
|
-
this.tools_execute = {};
|
|
19
|
-
this.tools_render = {};
|
|
20
|
-
this.tools_description = [];
|
|
21
|
-
for (let tool of options.tools || []) {
|
|
22
|
-
this.tools_execute[tool.description.function.name] = tool.execute;
|
|
23
|
-
this.tools_render[tool.description.function.name] = tool.render;
|
|
24
|
-
this.tools_description.push({ description: tool.description });
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// 当前是否在执行中
|
|
28
|
-
this.isExecuting = false;
|
|
29
|
-
|
|
30
|
-
// 当前激活的会话ID
|
|
31
|
-
this.currentThreadId = void 0;
|
|
32
|
-
|
|
33
|
-
// 当前会话内容
|
|
34
|
-
this.currentMessages = [];
|
|
35
|
-
|
|
36
|
-
// 任务生命周期事件:task:start / task:end
|
|
37
|
-
this._events = { "task:start": [], "task:end": [] };
|
|
38
|
-
|
|
39
|
-
// 任务自增序号,用于生成 taskId
|
|
40
|
-
this._taskSeq = 0;
|
|
41
|
-
|
|
42
|
-
// 当前正在执行的任务句柄(sendMessage 进入执行期时写入)
|
|
43
|
-
this._currentTask = null;
|
|
44
|
-
|
|
45
|
-
// 请求对话列表
|
|
46
|
-
this.adapter.ThreadList.list()?.then((threads) => {
|
|
47
|
-
|
|
48
|
-
// 在页面中显示对话列表
|
|
49
|
-
this.runtime.ThreadList.init(threads);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// 触发生命周期事件;handler 抛错被吞掉,避免影响 sendMessage 主流程
|
|
55
|
-
AuiPage.prototype._emit = function (event, payload) {
|
|
56
|
-
let handlers = this._events[event];
|
|
57
|
-
if (!handlers) return;
|
|
58
|
-
// 复制一份,防止 off() 在回调中修改原数组
|
|
59
|
-
handlers.slice().forEach(function (fn) {
|
|
60
|
-
try { fn(payload); } catch (e) { /* 吞掉 handler 异常 */ }
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// 订阅生命周期事件
|
|
65
|
-
AuiPage.prototype.on = function (event, handler) {
|
|
66
|
-
if (!this._events[event]) this._events[event] = [];
|
|
67
|
-
this._events[event].push(handler);
|
|
68
|
-
return this;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// 取消订阅
|
|
72
|
-
AuiPage.prototype.off = function (event, handler) {
|
|
73
|
-
let list = this._events[event];
|
|
74
|
-
if (!list) return this;
|
|
75
|
-
let idx = list.indexOf(handler);
|
|
76
|
-
if (idx >= 0) list.splice(idx, 1);
|
|
77
|
-
return this;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
AuiPage.prototype.sendMessage = function (message) {
|
|
81
|
-
let _this = this;
|
|
82
|
-
|
|
83
|
-
return new Promise((resolve, reject) => {
|
|
84
|
-
|
|
85
|
-
if (this.isExecuting) return reject("当前正在执行中");
|
|
86
|
-
this.isExecuting = true;
|
|
87
|
-
|
|
88
|
-
let generator = this.adapter.ChatTransport.run({
|
|
89
|
-
messages: [message],
|
|
90
|
-
context: {
|
|
91
|
-
history: this.currentMessages,
|
|
92
|
-
tools: this.tools_description
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
if (!generator) {
|
|
97
|
-
this.isExecuting = false;
|
|
98
|
-
reject("ChatTransport.run() 返回空值");
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 写入任务上下文,供后续协同使用
|
|
103
|
-
let taskId = String(++this._taskSeq);
|
|
104
|
-
this._currentTask = {
|
|
105
|
-
taskId,
|
|
106
|
-
message,
|
|
107
|
-
generator,
|
|
108
|
-
cacheMessages: [message],
|
|
109
|
-
preType: "",
|
|
110
|
-
preText: "",
|
|
111
|
-
textTypes: ["loading", "thinking", "text"],
|
|
112
|
-
streamFun: void 0,
|
|
113
|
-
tool_calls: {},
|
|
114
|
-
resolve,
|
|
115
|
-
reject,
|
|
116
|
-
ended: false
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
// 任务开始生命周期
|
|
120
|
-
this._emit("task:start", {
|
|
121
|
-
taskId,
|
|
122
|
-
threadId: this.currentThreadId,
|
|
123
|
-
message
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
let cacheMessages = this._currentTask.cacheMessages;
|
|
127
|
-
let preType = this._currentTask.preType;
|
|
128
|
-
let preText = this._currentTask.preText;
|
|
129
|
-
let textTypes = this._currentTask.textTypes;
|
|
130
|
-
let streamFun = this._currentTask.streamFun;
|
|
131
|
-
let tool_calls = this._currentTask.tool_calls;
|
|
132
|
-
|
|
133
|
-
for (let i = 0; i < message.content.length; i++) {
|
|
134
|
-
this.runtime.ChatContent.append(message.role, message.content[i]);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// 同步写入 _currentTask 的小工具
|
|
138
|
-
let sync = function () {
|
|
139
|
-
_this._currentTask.preType = preType;
|
|
140
|
-
_this._currentTask.preText = preText;
|
|
141
|
-
_this._currentTask.streamFun = streamFun;
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
(function doit() {
|
|
145
|
-
generator?.next().then(async function (res) {
|
|
146
|
-
if (!res.done) {
|
|
147
|
-
|
|
148
|
-
// 类型变了,或者是一种特殊类型
|
|
149
|
-
if (res.value.type != preType || !textTypes.includes(res.value.type)) {
|
|
150
|
-
streamFun = void 0;
|
|
151
|
-
|
|
152
|
-
if (preText) {
|
|
153
|
-
cacheMessages.push({
|
|
154
|
-
role: "assistant", content: [{
|
|
155
|
-
type: preType,
|
|
156
|
-
text: preText
|
|
157
|
-
}]
|
|
158
|
-
});
|
|
159
|
-
preText = "";
|
|
160
|
-
preType = "";
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (!textTypes.includes(res.value.type)) {
|
|
164
|
-
|
|
165
|
-
// 工具调用
|
|
166
|
-
if (res.value.type === "tool-call") {
|
|
167
|
-
|
|
168
|
-
// 工具开始调用(前端工具+后端工具)
|
|
169
|
-
if (res.value.status === "pending") {
|
|
170
|
-
tool_calls[res.value.id] = _this.runtime.ChatContent.append("assistant", res.value);
|
|
171
|
-
|
|
172
|
-
// 对于前端工具,需要自己执行工具调用
|
|
173
|
-
if (res.value.caller === "frontend") {
|
|
174
|
-
let result, status;
|
|
175
|
-
|
|
176
|
-
try {
|
|
177
|
-
result = await _this.tools_execute[res.value.name](res.value.args);
|
|
178
|
-
status = "success"; // 调用成功
|
|
179
|
-
} catch (error) {
|
|
180
|
-
result = error;
|
|
181
|
-
status = "error"; // 调用失败
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
let render = _this.tools_render[res.value.name];
|
|
185
|
-
|
|
186
|
-
// 需要视图交互获取结果
|
|
187
|
-
if (render) {
|
|
188
|
-
let result_ui, status_ui;
|
|
189
|
-
|
|
190
|
-
try {
|
|
191
|
-
result_ui = await tool_calls[res.value.id]({
|
|
192
|
-
...res.value,
|
|
193
|
-
result,
|
|
194
|
-
status,
|
|
195
|
-
render
|
|
196
|
-
});
|
|
197
|
-
status_ui = "success";
|
|
198
|
-
} catch (error) {
|
|
199
|
-
result_ui = error;
|
|
200
|
-
status_ui = "error";
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
tool_calls[res.value.id]({
|
|
204
|
-
...res.value,
|
|
205
|
-
result: result_ui,
|
|
206
|
-
status: status_ui
|
|
207
|
-
});
|
|
208
|
-
delete tool_calls[res.value.id];
|
|
209
|
-
|
|
210
|
-
cacheMessages.push({
|
|
211
|
-
role: "assistant", content: [{
|
|
212
|
-
type: "tool-call",
|
|
213
|
-
name: res.value.name,
|
|
214
|
-
args: res.value.args,
|
|
215
|
-
result,
|
|
216
|
-
status,
|
|
217
|
-
ui: {
|
|
218
|
-
status: status_ui,
|
|
219
|
-
result: result_ui
|
|
220
|
-
}
|
|
221
|
-
}]
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
_this.adapter.ChatTransport.response("tool-result", {
|
|
225
|
-
id: res.value.id,
|
|
226
|
-
status: status_ui,
|
|
227
|
-
result: result_ui
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// 没有render,就相当于纯粹的工具调用
|
|
232
|
-
else {
|
|
233
|
-
tool_calls[res.value.id]({
|
|
234
|
-
...res.value,
|
|
235
|
-
result,
|
|
236
|
-
status
|
|
237
|
-
});
|
|
238
|
-
delete tool_calls[res.value.id];
|
|
239
|
-
|
|
240
|
-
cacheMessages.push({
|
|
241
|
-
role: "assistant", content: [{
|
|
242
|
-
type: "tool-call",
|
|
243
|
-
name: res.value.name,
|
|
244
|
-
args: res.value.args,
|
|
245
|
-
result,
|
|
246
|
-
status
|
|
247
|
-
}]
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
_this.adapter.ChatTransport.response("tool-result", {
|
|
251
|
-
id: res.value.id,
|
|
252
|
-
result,
|
|
253
|
-
status
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// 工具调用结束(后端工具)
|
|
262
|
-
else if (res.value.status === "success" || res.value.status === "error") {
|
|
263
|
-
if (tool_calls[res.value.id]) {
|
|
264
|
-
tool_calls[res.value.id](res.value);
|
|
265
|
-
delete tool_calls[res.value.id];
|
|
266
|
-
|
|
267
|
-
cacheMessages.push({
|
|
268
|
-
role: "assistant", content: [{
|
|
269
|
-
type: "tool-call",
|
|
270
|
-
name: res.value.name,
|
|
271
|
-
args: res.value.args,
|
|
272
|
-
result: res.value.result,
|
|
273
|
-
status: res.value.status
|
|
274
|
-
}]
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
} else {
|
|
279
|
-
// console.log("【其他消息】", res.value);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
} else {
|
|
283
|
-
preText = res.value.text;
|
|
284
|
-
preType = res.value.type;
|
|
285
|
-
streamFun = _this.runtime.ChatContent.append("assistant", res.value);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
} else {
|
|
289
|
-
preText += res.value.text;
|
|
290
|
-
if (streamFun) streamFun(res.value.text);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
sync();
|
|
294
|
-
doit();
|
|
295
|
-
} else {
|
|
296
|
-
sync();
|
|
297
|
-
_this._finalize("complete", true);
|
|
298
|
-
}
|
|
299
|
-
}, err => {
|
|
300
|
-
_this._finalize("error", false, err);
|
|
301
|
-
});
|
|
302
|
-
})();
|
|
303
|
-
|
|
304
|
-
});
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* 收尾当前 sendMessage 任务。统一处理:残留文本归并、是否入库、isExecuting 复位、
|
|
309
|
-
* 发出 task:end 事件、释放 Promise。
|
|
310
|
-
*
|
|
311
|
-
* @param {"complete"|"error"} status 任务结束原因
|
|
312
|
-
* @param {boolean} isHistory 是否走入库分支(complete 始终 true;error 始终 false)
|
|
313
|
-
* @param {Error|string} [err] status === "error" 时附带的错误对象
|
|
314
|
-
*/
|
|
315
|
-
AuiPage.prototype._finalize = async function (status, isHistory, err) {
|
|
316
|
-
let task = this._currentTask;
|
|
317
|
-
if (!task || task.ended) return;
|
|
318
|
-
task.ended = true;
|
|
319
|
-
|
|
320
|
-
let { taskId, message, cacheMessages, preType, preText, resolve, reject } = task;
|
|
321
|
-
|
|
322
|
-
// 1. 残留文本归并到 cacheMessages
|
|
323
|
-
if (preText) {
|
|
324
|
-
cacheMessages.push({
|
|
325
|
-
role: "assistant",
|
|
326
|
-
content: [{ type: preType, text: preText }]
|
|
327
|
-
});
|
|
328
|
-
task.preText = "";
|
|
329
|
-
task.preType = "";
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// 2. 入库分支
|
|
333
|
-
if (isHistory) {
|
|
334
|
-
try {
|
|
335
|
-
let historyMessages = toHistoryMessages({ messages: cacheMessages });
|
|
336
|
-
|
|
337
|
-
if (this.currentThreadId === void 0) {
|
|
338
|
-
let newThread = await this.adapter.ThreadList.new(historyMessages);
|
|
339
|
-
|
|
340
|
-
if (!newThread) {
|
|
341
|
-
// 新建会话失败 → 退化为 error,不入库、不 resolve
|
|
342
|
-
this.isExecuting = false;
|
|
343
|
-
this._currentTask = null;
|
|
344
|
-
this._emit("task:end", { taskId, threadId: this.currentThreadId, message, status: "error" });
|
|
345
|
-
reject(new Error("新建会话失败"));
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
await this.runtime.ThreadList.append(newThread);
|
|
350
|
-
this.activeThread(newThread.threadId, true);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
this.currentMessages.push(historyMessages);
|
|
354
|
-
this.adapter.MessageHistory.append(this.currentThreadId, historyMessages);
|
|
355
|
-
} catch (e) {
|
|
356
|
-
// 入库链路本身抛错 → 退化为 error
|
|
357
|
-
this.isExecuting = false;
|
|
358
|
-
this._currentTask = null;
|
|
359
|
-
this._emit("task:end", { taskId, threadId: this.currentThreadId, message, status: "error" });
|
|
360
|
-
reject(e);
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
// 4. 复位、emit task:end、释放 Promise
|
|
366
|
-
this.isExecuting = false;
|
|
367
|
-
this._currentTask = null;
|
|
368
|
-
|
|
369
|
-
this._emit("task:end", { taskId, threadId: this.currentThreadId, message, status });
|
|
370
|
-
|
|
371
|
-
if (status === "error") reject(err);
|
|
372
|
-
else resolve();
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
AuiPage.prototype.activeThread = function (threadId, __noLoad__) {
|
|
376
|
-
if (this.currentThreadId === threadId) return;
|
|
377
|
-
|
|
378
|
-
// 已经有执行的,不允许切换
|
|
379
|
-
if (this.isExecuting && !__noLoad__) return;
|
|
380
|
-
|
|
381
|
-
this.currentThreadId = threadId;
|
|
382
|
-
this.runtime.ThreadList.active(threadId);
|
|
383
|
-
|
|
384
|
-
if (threadId === void 0) {
|
|
385
|
-
this.currentMessages = [];
|
|
386
|
-
this.runtime.ChatContent.init([]);
|
|
387
|
-
} else if (!__noLoad__) {
|
|
388
|
-
|
|
389
|
-
// 加载历史消息
|
|
390
|
-
this.adapter.MessageHistory.load(threadId)?.then(messages => {
|
|
391
|
-
this.currentMessages = messages;
|
|
392
|
-
|
|
393
|
-
let plainMessages = [];
|
|
394
|
-
messages.forEach(item => {
|
|
395
|
-
plainMessages.push(...item.messages);
|
|
396
|
-
});
|
|
397
|
-
this.runtime.ChatContent.init(plainMessages);
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
};
|
|
401
|
-
|
|
402
|
-
AuiPage.prototype.deleteThread = function (threadId) {
|
|
403
|
-
this.adapter.ThreadList.delete(threadId)?.then(() => {
|
|
404
|
-
this.runtime.ThreadList.delete(threadId);
|
|
405
|
-
|
|
406
|
-
if (threadId === this.currentThreadId) this.newThread();
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
};
|
|
410
|
-
|
|
411
|
-
AuiPage.prototype.archiveThread = function (threadId) {
|
|
412
|
-
this.adapter.ThreadList.archive(threadId)?.then(() => {
|
|
413
|
-
this.runtime.ThreadList.archive(threadId);
|
|
414
|
-
|
|
415
|
-
if (threadId === this.currentThreadId) this.newThread();
|
|
416
|
-
});
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
AuiPage.prototype.unarchiveThread = function (threadId) {
|
|
420
|
-
this.adapter.ThreadList.unarchive(threadId).then(() => {
|
|
421
|
-
this.runtime.ThreadList.unarchive(threadId);
|
|
422
|
-
});
|
|
423
|
-
};
|
|
424
|
-
|
|
425
|
-
AuiPage.prototype.newThread = function () {
|
|
426
|
-
this.activeThread(void 0);
|
|
427
|
-
};
|
|
428
|
-
|
|
1
|
+
import toHistoryMessages from "./tools/toHistoryMessages.js";
|
|
2
|
+
|
|
3
|
+
function AuiPage(options) {
|
|
4
|
+
|
|
5
|
+
// 连接适配器和运行时视图
|
|
6
|
+
this.adapter = {};
|
|
7
|
+
this.runtime = {};
|
|
8
|
+
for (let instance of Object.values(options.adapter)) {
|
|
9
|
+
instance.aui = this;
|
|
10
|
+
this.adapter[instance.name.replace("Adapter", "")] = instance;
|
|
11
|
+
}
|
|
12
|
+
for (let instance of Object.values(options.runtime)) {
|
|
13
|
+
instance.aui = this;
|
|
14
|
+
this.runtime[instance.name.replace("Runtime", "")] = instance;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 工具注册
|
|
18
|
+
this.tools_execute = {};
|
|
19
|
+
this.tools_render = {};
|
|
20
|
+
this.tools_description = [];
|
|
21
|
+
for (let tool of options.tools || []) {
|
|
22
|
+
this.tools_execute[tool.description.function.name] = tool.execute;
|
|
23
|
+
this.tools_render[tool.description.function.name] = tool.render;
|
|
24
|
+
this.tools_description.push({ description: tool.description });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 当前是否在执行中
|
|
28
|
+
this.isExecuting = false;
|
|
29
|
+
|
|
30
|
+
// 当前激活的会话ID
|
|
31
|
+
this.currentThreadId = void 0;
|
|
32
|
+
|
|
33
|
+
// 当前会话内容
|
|
34
|
+
this.currentMessages = [];
|
|
35
|
+
|
|
36
|
+
// 任务生命周期事件:task:start / task:end
|
|
37
|
+
this._events = { "task:start": [], "task:end": [] };
|
|
38
|
+
|
|
39
|
+
// 任务自增序号,用于生成 taskId
|
|
40
|
+
this._taskSeq = 0;
|
|
41
|
+
|
|
42
|
+
// 当前正在执行的任务句柄(sendMessage 进入执行期时写入)
|
|
43
|
+
this._currentTask = null;
|
|
44
|
+
|
|
45
|
+
// 请求对话列表
|
|
46
|
+
this.adapter.ThreadList.list()?.then((threads) => {
|
|
47
|
+
|
|
48
|
+
// 在页面中显示对话列表
|
|
49
|
+
this.runtime.ThreadList.init(threads);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 触发生命周期事件;handler 抛错被吞掉,避免影响 sendMessage 主流程
|
|
55
|
+
AuiPage.prototype._emit = function (event, payload) {
|
|
56
|
+
let handlers = this._events[event];
|
|
57
|
+
if (!handlers) return;
|
|
58
|
+
// 复制一份,防止 off() 在回调中修改原数组
|
|
59
|
+
handlers.slice().forEach(function (fn) {
|
|
60
|
+
try { fn(payload); } catch (e) { /* 吞掉 handler 异常 */ }
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// 订阅生命周期事件
|
|
65
|
+
AuiPage.prototype.on = function (event, handler) {
|
|
66
|
+
if (!this._events[event]) this._events[event] = [];
|
|
67
|
+
this._events[event].push(handler);
|
|
68
|
+
return this;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// 取消订阅
|
|
72
|
+
AuiPage.prototype.off = function (event, handler) {
|
|
73
|
+
let list = this._events[event];
|
|
74
|
+
if (!list) return this;
|
|
75
|
+
let idx = list.indexOf(handler);
|
|
76
|
+
if (idx >= 0) list.splice(idx, 1);
|
|
77
|
+
return this;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
AuiPage.prototype.sendMessage = function (message) {
|
|
81
|
+
let _this = this;
|
|
82
|
+
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
|
|
85
|
+
if (this.isExecuting) return reject("当前正在执行中");
|
|
86
|
+
this.isExecuting = true;
|
|
87
|
+
|
|
88
|
+
let generator = this.adapter.ChatTransport.run({
|
|
89
|
+
messages: [message],
|
|
90
|
+
context: {
|
|
91
|
+
history: this.currentMessages,
|
|
92
|
+
tools: this.tools_description
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (!generator) {
|
|
97
|
+
this.isExecuting = false;
|
|
98
|
+
reject("ChatTransport.run() 返回空值");
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 写入任务上下文,供后续协同使用
|
|
103
|
+
let taskId = String(++this._taskSeq);
|
|
104
|
+
this._currentTask = {
|
|
105
|
+
taskId,
|
|
106
|
+
message,
|
|
107
|
+
generator,
|
|
108
|
+
cacheMessages: [message],
|
|
109
|
+
preType: "",
|
|
110
|
+
preText: "",
|
|
111
|
+
textTypes: ["loading", "thinking", "text"],
|
|
112
|
+
streamFun: void 0,
|
|
113
|
+
tool_calls: {},
|
|
114
|
+
resolve,
|
|
115
|
+
reject,
|
|
116
|
+
ended: false
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// 任务开始生命周期
|
|
120
|
+
this._emit("task:start", {
|
|
121
|
+
taskId,
|
|
122
|
+
threadId: this.currentThreadId,
|
|
123
|
+
message
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
let cacheMessages = this._currentTask.cacheMessages;
|
|
127
|
+
let preType = this._currentTask.preType;
|
|
128
|
+
let preText = this._currentTask.preText;
|
|
129
|
+
let textTypes = this._currentTask.textTypes;
|
|
130
|
+
let streamFun = this._currentTask.streamFun;
|
|
131
|
+
let tool_calls = this._currentTask.tool_calls;
|
|
132
|
+
|
|
133
|
+
for (let i = 0; i < message.content.length; i++) {
|
|
134
|
+
this.runtime.ChatContent.append(message.role, message.content[i]);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 同步写入 _currentTask 的小工具
|
|
138
|
+
let sync = function () {
|
|
139
|
+
_this._currentTask.preType = preType;
|
|
140
|
+
_this._currentTask.preText = preText;
|
|
141
|
+
_this._currentTask.streamFun = streamFun;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
(function doit() {
|
|
145
|
+
generator?.next().then(async function (res) {
|
|
146
|
+
if (!res.done) {
|
|
147
|
+
|
|
148
|
+
// 类型变了,或者是一种特殊类型
|
|
149
|
+
if (res.value.type != preType || !textTypes.includes(res.value.type)) {
|
|
150
|
+
streamFun = void 0;
|
|
151
|
+
|
|
152
|
+
if (preText) {
|
|
153
|
+
cacheMessages.push({
|
|
154
|
+
role: "assistant", content: [{
|
|
155
|
+
type: preType,
|
|
156
|
+
text: preText
|
|
157
|
+
}]
|
|
158
|
+
});
|
|
159
|
+
preText = "";
|
|
160
|
+
preType = "";
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!textTypes.includes(res.value.type)) {
|
|
164
|
+
|
|
165
|
+
// 工具调用
|
|
166
|
+
if (res.value.type === "tool-call") {
|
|
167
|
+
|
|
168
|
+
// 工具开始调用(前端工具+后端工具)
|
|
169
|
+
if (res.value.status === "pending") {
|
|
170
|
+
tool_calls[res.value.id] = _this.runtime.ChatContent.append("assistant", res.value);
|
|
171
|
+
|
|
172
|
+
// 对于前端工具,需要自己执行工具调用
|
|
173
|
+
if (res.value.caller === "frontend") {
|
|
174
|
+
let result, status;
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
result = await _this.tools_execute[res.value.name](res.value.args);
|
|
178
|
+
status = "success"; // 调用成功
|
|
179
|
+
} catch (error) {
|
|
180
|
+
result = error;
|
|
181
|
+
status = "error"; // 调用失败
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let render = _this.tools_render[res.value.name];
|
|
185
|
+
|
|
186
|
+
// 需要视图交互获取结果
|
|
187
|
+
if (render) {
|
|
188
|
+
let result_ui, status_ui;
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
result_ui = await tool_calls[res.value.id]({
|
|
192
|
+
...res.value,
|
|
193
|
+
result,
|
|
194
|
+
status,
|
|
195
|
+
render
|
|
196
|
+
});
|
|
197
|
+
status_ui = "success";
|
|
198
|
+
} catch (error) {
|
|
199
|
+
result_ui = error;
|
|
200
|
+
status_ui = "error";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
tool_calls[res.value.id]({
|
|
204
|
+
...res.value,
|
|
205
|
+
result: result_ui,
|
|
206
|
+
status: status_ui
|
|
207
|
+
});
|
|
208
|
+
delete tool_calls[res.value.id];
|
|
209
|
+
|
|
210
|
+
cacheMessages.push({
|
|
211
|
+
role: "assistant", content: [{
|
|
212
|
+
type: "tool-call",
|
|
213
|
+
name: res.value.name,
|
|
214
|
+
args: res.value.args,
|
|
215
|
+
result,
|
|
216
|
+
status,
|
|
217
|
+
ui: {
|
|
218
|
+
status: status_ui,
|
|
219
|
+
result: result_ui
|
|
220
|
+
}
|
|
221
|
+
}]
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
_this.adapter.ChatTransport.response("tool-result", {
|
|
225
|
+
id: res.value.id,
|
|
226
|
+
status: status_ui,
|
|
227
|
+
result: result_ui
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 没有render,就相当于纯粹的工具调用
|
|
232
|
+
else {
|
|
233
|
+
tool_calls[res.value.id]({
|
|
234
|
+
...res.value,
|
|
235
|
+
result,
|
|
236
|
+
status
|
|
237
|
+
});
|
|
238
|
+
delete tool_calls[res.value.id];
|
|
239
|
+
|
|
240
|
+
cacheMessages.push({
|
|
241
|
+
role: "assistant", content: [{
|
|
242
|
+
type: "tool-call",
|
|
243
|
+
name: res.value.name,
|
|
244
|
+
args: res.value.args,
|
|
245
|
+
result,
|
|
246
|
+
status
|
|
247
|
+
}]
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
_this.adapter.ChatTransport.response("tool-result", {
|
|
251
|
+
id: res.value.id,
|
|
252
|
+
result,
|
|
253
|
+
status
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// 工具调用结束(后端工具)
|
|
262
|
+
else if (res.value.status === "success" || res.value.status === "error") {
|
|
263
|
+
if (tool_calls[res.value.id]) {
|
|
264
|
+
tool_calls[res.value.id](res.value);
|
|
265
|
+
delete tool_calls[res.value.id];
|
|
266
|
+
|
|
267
|
+
cacheMessages.push({
|
|
268
|
+
role: "assistant", content: [{
|
|
269
|
+
type: "tool-call",
|
|
270
|
+
name: res.value.name,
|
|
271
|
+
args: res.value.args,
|
|
272
|
+
result: res.value.result,
|
|
273
|
+
status: res.value.status
|
|
274
|
+
}]
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
// console.log("【其他消息】", res.value);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
} else {
|
|
283
|
+
preText = res.value.text;
|
|
284
|
+
preType = res.value.type;
|
|
285
|
+
streamFun = _this.runtime.ChatContent.append("assistant", res.value);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
} else {
|
|
289
|
+
preText += res.value.text;
|
|
290
|
+
if (streamFun) streamFun(res.value.text);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
sync();
|
|
294
|
+
doit();
|
|
295
|
+
} else {
|
|
296
|
+
sync();
|
|
297
|
+
_this._finalize("complete", true);
|
|
298
|
+
}
|
|
299
|
+
}, err => {
|
|
300
|
+
_this._finalize("error", false, err);
|
|
301
|
+
});
|
|
302
|
+
})();
|
|
303
|
+
|
|
304
|
+
});
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 收尾当前 sendMessage 任务。统一处理:残留文本归并、是否入库、isExecuting 复位、
|
|
309
|
+
* 发出 task:end 事件、释放 Promise。
|
|
310
|
+
*
|
|
311
|
+
* @param {"complete"|"error"} status 任务结束原因
|
|
312
|
+
* @param {boolean} isHistory 是否走入库分支(complete 始终 true;error 始终 false)
|
|
313
|
+
* @param {Error|string} [err] status === "error" 时附带的错误对象
|
|
314
|
+
*/
|
|
315
|
+
AuiPage.prototype._finalize = async function (status, isHistory, err) {
|
|
316
|
+
let task = this._currentTask;
|
|
317
|
+
if (!task || task.ended) return;
|
|
318
|
+
task.ended = true;
|
|
319
|
+
|
|
320
|
+
let { taskId, message, cacheMessages, preType, preText, resolve, reject } = task;
|
|
321
|
+
|
|
322
|
+
// 1. 残留文本归并到 cacheMessages
|
|
323
|
+
if (preText) {
|
|
324
|
+
cacheMessages.push({
|
|
325
|
+
role: "assistant",
|
|
326
|
+
content: [{ type: preType, text: preText }]
|
|
327
|
+
});
|
|
328
|
+
task.preText = "";
|
|
329
|
+
task.preType = "";
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// 2. 入库分支
|
|
333
|
+
if (isHistory) {
|
|
334
|
+
try {
|
|
335
|
+
let historyMessages = toHistoryMessages({ messages: cacheMessages });
|
|
336
|
+
|
|
337
|
+
if (this.currentThreadId === void 0) {
|
|
338
|
+
let newThread = await this.adapter.ThreadList.new(historyMessages);
|
|
339
|
+
|
|
340
|
+
if (!newThread) {
|
|
341
|
+
// 新建会话失败 → 退化为 error,不入库、不 resolve
|
|
342
|
+
this.isExecuting = false;
|
|
343
|
+
this._currentTask = null;
|
|
344
|
+
this._emit("task:end", { taskId, threadId: this.currentThreadId, message, status: "error" });
|
|
345
|
+
reject(new Error("新建会话失败"));
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
await this.runtime.ThreadList.append(newThread);
|
|
350
|
+
this.activeThread(newThread.threadId, true);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
this.currentMessages.push(historyMessages);
|
|
354
|
+
this.adapter.MessageHistory.append(this.currentThreadId, historyMessages);
|
|
355
|
+
} catch (e) {
|
|
356
|
+
// 入库链路本身抛错 → 退化为 error
|
|
357
|
+
this.isExecuting = false;
|
|
358
|
+
this._currentTask = null;
|
|
359
|
+
this._emit("task:end", { taskId, threadId: this.currentThreadId, message, status: "error" });
|
|
360
|
+
reject(e);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// 4. 复位、emit task:end、释放 Promise
|
|
366
|
+
this.isExecuting = false;
|
|
367
|
+
this._currentTask = null;
|
|
368
|
+
|
|
369
|
+
this._emit("task:end", { taskId, threadId: this.currentThreadId, message, status });
|
|
370
|
+
|
|
371
|
+
if (status === "error") reject(err);
|
|
372
|
+
else resolve();
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
AuiPage.prototype.activeThread = function (threadId, __noLoad__) {
|
|
376
|
+
if (this.currentThreadId === threadId) return;
|
|
377
|
+
|
|
378
|
+
// 已经有执行的,不允许切换
|
|
379
|
+
if (this.isExecuting && !__noLoad__) return;
|
|
380
|
+
|
|
381
|
+
this.currentThreadId = threadId;
|
|
382
|
+
this.runtime.ThreadList.active(threadId);
|
|
383
|
+
|
|
384
|
+
if (threadId === void 0) {
|
|
385
|
+
this.currentMessages = [];
|
|
386
|
+
this.runtime.ChatContent.init([]);
|
|
387
|
+
} else if (!__noLoad__) {
|
|
388
|
+
|
|
389
|
+
// 加载历史消息
|
|
390
|
+
this.adapter.MessageHistory.load(threadId)?.then(messages => {
|
|
391
|
+
this.currentMessages = messages;
|
|
392
|
+
|
|
393
|
+
let plainMessages = [];
|
|
394
|
+
messages.forEach(item => {
|
|
395
|
+
plainMessages.push(...item.messages);
|
|
396
|
+
});
|
|
397
|
+
this.runtime.ChatContent.init(plainMessages);
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
AuiPage.prototype.deleteThread = function (threadId) {
|
|
403
|
+
this.adapter.ThreadList.delete(threadId)?.then(() => {
|
|
404
|
+
this.runtime.ThreadList.delete(threadId);
|
|
405
|
+
|
|
406
|
+
if (threadId === this.currentThreadId) this.newThread();
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
AuiPage.prototype.archiveThread = function (threadId) {
|
|
412
|
+
this.adapter.ThreadList.archive(threadId)?.then(() => {
|
|
413
|
+
this.runtime.ThreadList.archive(threadId);
|
|
414
|
+
|
|
415
|
+
if (threadId === this.currentThreadId) this.newThread();
|
|
416
|
+
});
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
AuiPage.prototype.unarchiveThread = function (threadId) {
|
|
420
|
+
this.adapter.ThreadList.unarchive(threadId).then(() => {
|
|
421
|
+
this.runtime.ThreadList.unarchive(threadId);
|
|
422
|
+
});
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
AuiPage.prototype.newThread = function () {
|
|
426
|
+
this.activeThread(void 0);
|
|
427
|
+
};
|
|
428
|
+
|
|
429
429
|
export default AuiPage;
|