evolclaw 2.8.3 → 3.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/README.md +21 -12
- package/bin/ec.js +29 -0
- package/dist/agents/baseagent-normalize.js +19 -0
- package/dist/agents/claude-runner.js +108 -46
- package/dist/agents/codex-runner.js +13 -14
- package/dist/agents/gemini-runner.js +15 -17
- package/dist/agents/kit-renderer.js +281 -0
- package/dist/agents/resolve.js +134 -0
- package/dist/aun/aid/agentmd.js +186 -0
- package/dist/aun/aid/client.js +134 -0
- package/dist/aun/aid/identity.js +159 -0
- package/dist/aun/aid/index.js +3 -0
- package/dist/aun/aid/lifecycle-log.js +33 -0
- package/dist/aun/aid/types.js +1 -0
- package/dist/aun/aid/validation.js +21 -0
- package/dist/aun/msg/group.js +293 -0
- package/dist/aun/msg/index.js +4 -0
- package/dist/aun/msg/p2p.js +147 -0
- package/dist/aun/msg/payload-type.js +27 -0
- package/dist/aun/msg/upload.js +98 -0
- package/dist/aun/outbox.js +138 -0
- package/dist/aun/rpc/caller.js +42 -0
- package/dist/aun/rpc/connection.js +34 -0
- package/dist/aun/rpc/index.js +2 -0
- package/dist/aun/storage/download.js +29 -0
- package/dist/aun/storage/index.js +3 -0
- package/dist/aun/storage/manage.js +10 -0
- package/dist/aun/storage/upload.js +35 -0
- package/dist/channels/aun.js +1340 -349
- package/dist/channels/dingtalk.js +59 -5
- package/dist/channels/feishu.js +381 -32
- package/dist/channels/qqbot.js +68 -12
- package/dist/channels/wechat.js +63 -4
- package/dist/channels/wecom.js +59 -5
- package/dist/cli/agent.js +800 -0
- package/dist/cli/bench.js +1219 -0
- package/dist/cli/index.js +4513 -0
- package/dist/{utils → cli}/init-channel.js +211 -621
- package/dist/cli/init.js +178 -0
- package/dist/cli/link-rules.js +245 -0
- package/dist/cli/net-check.js +640 -0
- package/dist/cli/watch-msg.js +589 -0
- package/dist/config-store.js +645 -0
- package/dist/core/{agent-loader.js → baseagent-loader.js} +6 -12
- package/dist/core/channel-loader.js +176 -12
- package/dist/core/command-handler.js +883 -848
- package/dist/core/evolagent-registry.js +191 -371
- package/dist/core/evolagent.js +202 -238
- package/dist/core/interaction-router.js +52 -5
- package/dist/core/message/im-renderer.js +486 -0
- package/dist/core/message/items-formatter.js +68 -0
- package/dist/core/message/message-bridge.js +109 -56
- package/dist/core/message/message-log.js +93 -0
- package/dist/core/message/message-processor.js +430 -212
- package/dist/core/message/message-queue.js +13 -6
- package/dist/core/permission.js +116 -11
- package/dist/core/session/adapters/codex-session-file-adapter.js +24 -2
- package/dist/core/session/session-fs-store.js +230 -0
- package/dist/core/session/session-manager.js +740 -777
- package/dist/core/session/session-mapper.js +87 -0
- package/dist/core/trigger/manager.js +122 -0
- package/dist/core/trigger/parser.js +128 -0
- package/dist/core/trigger/scheduler.js +224 -0
- package/dist/data/error-dict.json +118 -0
- package/dist/eck/baseagent-caps.js +18 -0
- package/dist/eck/detect.js +47 -0
- package/dist/eck/init.js +77 -0
- package/dist/eck/rules-loader.js +28 -0
- package/dist/index.js +560 -283
- package/dist/ipc.js +49 -0
- package/dist/net-check.js +640 -0
- package/dist/paths.js +73 -9
- package/dist/types.js +8 -2
- package/dist/utils/aid-lifecycle-log.js +33 -0
- package/dist/utils/atomic-write.js +89 -0
- package/dist/utils/channel-helpers.js +46 -0
- package/dist/utils/cross-platform.js +17 -26
- package/dist/utils/error-utils.js +10 -2
- package/dist/utils/instance-registry.js +434 -0
- package/dist/utils/log-writer.js +217 -0
- package/dist/utils/logger.js +34 -77
- package/dist/utils/media-cache.js +23 -0
- package/dist/utils/npm-ops.js +163 -0
- package/dist/utils/process-introspect.js +122 -0
- package/dist/utils/stats.js +192 -0
- package/dist/watch-msg.js +544 -0
- package/evolclaw-install-aun.md +127 -47
- package/kits/docs/GUIDE.md +20 -0
- package/kits/docs/INDEX.md +52 -0
- package/kits/docs/aun/CHEATSHEET.md +17 -0
- package/kits/docs/aun/SYNC_PROTOCOL.md +15 -0
- package/kits/docs/channels/aun.md +25 -0
- package/kits/docs/channels/feishu.md +27 -0
- package/kits/docs/eck_templates/GUIDE.template.md +22 -0
- package/kits/docs/eck_templates/INDEX.template.md +28 -0
- package/kits/docs/eck_templates/path-registry.template.md +33 -0
- package/kits/docs/eck_templates/runtime.template.md +19 -0
- package/kits/docs/evolclaw/AGENT_CMD.md +31 -0
- package/kits/docs/evolclaw/MSG_GROUP.md +30 -0
- package/kits/docs/evolclaw/MSG_PRIVATE.md +25 -0
- package/kits/docs/evolclaw/self-summary.md +29 -0
- package/kits/docs/evolclaw/tools.md +25 -0
- package/kits/docs/identity/AID_PROFILE_SPEC.md +27 -0
- package/kits/docs/identity/PATH_OPS.md +16 -0
- package/kits/docs/identity/ROLE_DETAIL.md +20 -0
- package/kits/docs/identity/identity-tools.md +26 -0
- package/kits/docs/path-registry.md +43 -0
- package/kits/eck_manifest.json +95 -0
- package/kits/rules/01-overview.md +120 -0
- package/kits/rules/02-navigation.md +75 -0
- package/kits/rules/03-identity.md +34 -0
- package/kits/rules/04-relation.md +49 -0
- package/kits/rules/05-venue.md +45 -0
- package/kits/rules/06-channel.md +43 -0
- package/kits/templates/system-fragments/baseagent.md +2 -0
- package/kits/templates/system-fragments/channel.md +10 -0
- package/kits/templates/system-fragments/identity.md +12 -0
- package/kits/templates/system-fragments/relation.md +9 -0
- package/kits/templates/system-fragments/runtime.md +19 -0
- package/kits/templates/system-fragments/venue.md +5 -0
- package/package.json +10 -6
- package/data/evolclaw.sample.json +0 -60
- package/dist/agents/templates.js +0 -122
- package/dist/channels/aun-ops.js +0 -275
- package/dist/cli.js +0 -2178
- package/dist/config.js +0 -591
- package/dist/core/agent-registry.js +0 -450
- package/dist/core/evolagent-schema.js +0 -72
- package/dist/core/message/stream-flusher.js +0 -238
- package/dist/core/message/thought-emitter.js +0 -162
- package/dist/core/reload-hooks.js +0 -87
- package/dist/prompts/templates.js +0 -122
- package/dist/templates/prompts.md +0 -104
- package/dist/templates/skills.md +0 -66
- package/dist/utils/channel-fingerprint.js +0 -59
- package/dist/utils/error-dict.js +0 -63
- package/dist/utils/format.js +0 -32
- package/dist/utils/init.js +0 -645
- package/dist/utils/migrate-project.js +0 -122
- package/dist/utils/reload-hooks.js +0 -87
- package/dist/utils/stats-collector.js +0 -99
- package/dist/utils/upgrade.js +0 -100
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import { logger } from '../../utils/logger.js';
|
|
2
|
+
import { summarizeToolInput } from '../permission.js';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { resolvePaths } from '../../paths.js';
|
|
6
|
+
let diagStream = null;
|
|
7
|
+
function getDiagStream() {
|
|
8
|
+
if (!diagStream) {
|
|
9
|
+
const logDir = resolvePaths().logs;
|
|
10
|
+
diagStream = fs.createWriteStream(path.join(logDir, 'im-renderer-diag.log'), { flags: 'a' });
|
|
11
|
+
}
|
|
12
|
+
return diagStream;
|
|
13
|
+
}
|
|
14
|
+
function diag(instanceId, action, meta = {}) {
|
|
15
|
+
const line = JSON.stringify({ ts: new Date().toISOString(), id: instanceId, action, ...meta });
|
|
16
|
+
getDiagStream().write(line + '\n');
|
|
17
|
+
}
|
|
18
|
+
let instanceCounter = 0;
|
|
19
|
+
export class IMRenderer {
|
|
20
|
+
opts;
|
|
21
|
+
itemsQueue = [];
|
|
22
|
+
textBuffer = '';
|
|
23
|
+
timer;
|
|
24
|
+
lastFlush = Date.now();
|
|
25
|
+
allText = '';
|
|
26
|
+
sentContent = false;
|
|
27
|
+
flushCount = 0;
|
|
28
|
+
messageTimestamps = [];
|
|
29
|
+
instanceId;
|
|
30
|
+
diagEnabled;
|
|
31
|
+
/** 串行发送队列:保证消息按序到达 */
|
|
32
|
+
sendChain = Promise.resolve();
|
|
33
|
+
/** proactive:是否已发过 text 文本(用于去重 complete.result) */
|
|
34
|
+
hasEmittedText = false;
|
|
35
|
+
/** 自增 callId 兜底(runner 没提供时用) */
|
|
36
|
+
syntheticCallSeq = 0;
|
|
37
|
+
constructor(opts) {
|
|
38
|
+
this.opts = opts;
|
|
39
|
+
this.diagEnabled = opts.diagEnabled ?? false;
|
|
40
|
+
this.instanceId = `R${++instanceCounter}`;
|
|
41
|
+
if (this.diagEnabled) {
|
|
42
|
+
diag(this.instanceId, 'created', {
|
|
43
|
+
chatmode: opts.envelope.chatmode,
|
|
44
|
+
flushDelay: opts.flushDelay,
|
|
45
|
+
suppress: opts.suppressActivities,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// ── 公开接口 ──
|
|
50
|
+
/** 推入 AgentEvent,按 chatmode 投影 */
|
|
51
|
+
emit(event) {
|
|
52
|
+
try {
|
|
53
|
+
logger.event({ source: 'runner', taskId: this.opts.envelope.taskId, channelId: this.opts.envelope.channelId, event });
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// logger.event 失败不影响业务
|
|
57
|
+
}
|
|
58
|
+
if (this.opts.envelope.chatmode === 'proactive') {
|
|
59
|
+
this.emitProactive(event);
|
|
60
|
+
}
|
|
61
|
+
// interactive 模式由 MessageProcessor 显式调 addText/addToolCall/... 推入 items
|
|
62
|
+
}
|
|
63
|
+
/** 强制刷新所有 pending 事件 */
|
|
64
|
+
async flush(isFinal) {
|
|
65
|
+
if (this.opts.envelope.chatmode === 'proactive') {
|
|
66
|
+
// proactive 是 fire-and-forget,无 pending buffer
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
return this.flushInternal(isFinal);
|
|
70
|
+
}
|
|
71
|
+
/** 仅 flush activities,保留 textBuffer(用于中间 complete 事件) */
|
|
72
|
+
async flushActivitiesOnly() {
|
|
73
|
+
if (this.opts.envelope.chatmode === 'proactive')
|
|
74
|
+
return;
|
|
75
|
+
return this.flushActivitiesInternal();
|
|
76
|
+
}
|
|
77
|
+
/** 是否有 pending 内容 */
|
|
78
|
+
hasContent() {
|
|
79
|
+
return this.textBuffer.length > 0 || this.itemsQueue.some(it => it.kind !== 'text');
|
|
80
|
+
}
|
|
81
|
+
/** 是否有待发送的文本 */
|
|
82
|
+
hasTextPending() {
|
|
83
|
+
return this.textBuffer.length > 0;
|
|
84
|
+
}
|
|
85
|
+
/** flush 当前 textBuffer 作为独立的 result.text(非 final),然后清空 buffer */
|
|
86
|
+
async flushText() {
|
|
87
|
+
if (this.opts.envelope.chatmode === 'proactive')
|
|
88
|
+
return;
|
|
89
|
+
if (this.textBuffer.length === 0)
|
|
90
|
+
return;
|
|
91
|
+
if (this.timer) {
|
|
92
|
+
clearTimeout(this.timer);
|
|
93
|
+
this.timer = undefined;
|
|
94
|
+
}
|
|
95
|
+
const text = this.textBuffer;
|
|
96
|
+
this.textBuffer = '';
|
|
97
|
+
// 清掉 itemsQueue 中的 text items(已发出)
|
|
98
|
+
this.itemsQueue = this.itemsQueue.filter(it => it.kind !== 'text');
|
|
99
|
+
const payload = { kind: 'result.text', text, isFinal: false };
|
|
100
|
+
this.sentContent = true;
|
|
101
|
+
this.sendChain = this.sendChain
|
|
102
|
+
.then(() => this.opts.send(payload))
|
|
103
|
+
.catch(e => logger.warn('[IMRenderer] flushText send failed:', e));
|
|
104
|
+
await this.sendChain;
|
|
105
|
+
this.lastFlush = Date.now();
|
|
106
|
+
this.flushCount++;
|
|
107
|
+
}
|
|
108
|
+
/** 是否已发送过内容(用于决定最终 flush 是否带 isFinal 标题) */
|
|
109
|
+
hasSentContent() {
|
|
110
|
+
return this.sentContent;
|
|
111
|
+
}
|
|
112
|
+
/** 累积的全部文本(流式 + 最终) */
|
|
113
|
+
getFinalText() {
|
|
114
|
+
return this.allText;
|
|
115
|
+
}
|
|
116
|
+
/** 当前 buffer 中尚未 flush 的文本 */
|
|
117
|
+
getRemainingText() {
|
|
118
|
+
return this.textBuffer;
|
|
119
|
+
}
|
|
120
|
+
/** 从 buffer 中移除指定 pattern(用于文件标记预处理) */
|
|
121
|
+
stripFromBuffer(pattern) {
|
|
122
|
+
this.textBuffer = this.textBuffer.replace(pattern, '').trim();
|
|
123
|
+
// itemsQueue 中的 text items 也同步过滤
|
|
124
|
+
for (const item of this.itemsQueue) {
|
|
125
|
+
if (item.kind === 'text') {
|
|
126
|
+
item.text = item.text.replace(pattern, '');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// ── 文本/活动注入(替代 StreamFlusher.addText/addActivity)──
|
|
131
|
+
/** 添加文本片段(流式 text) */
|
|
132
|
+
addText(text) {
|
|
133
|
+
if (this.opts.envelope.chatmode === 'proactive')
|
|
134
|
+
return;
|
|
135
|
+
if (!text)
|
|
136
|
+
return;
|
|
137
|
+
// 同一窗口内连续 text delta 合并到最后一个 text item
|
|
138
|
+
const last = this.itemsQueue[this.itemsQueue.length - 1];
|
|
139
|
+
if (last && last.kind === 'text') {
|
|
140
|
+
last.text += text;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
this.itemsQueue.push({ kind: 'text', text });
|
|
144
|
+
}
|
|
145
|
+
this.textBuffer += text;
|
|
146
|
+
this.allText += text;
|
|
147
|
+
this.messageTimestamps.push(Date.now());
|
|
148
|
+
if (this.diagEnabled) {
|
|
149
|
+
diag(this.instanceId, 'addText', {
|
|
150
|
+
len: text.length,
|
|
151
|
+
preview: text.substring(0, 60),
|
|
152
|
+
bufLen: this.textBuffer.length,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
this.scheduleFlush();
|
|
156
|
+
}
|
|
157
|
+
/** 添加工具调用 */
|
|
158
|
+
addToolCall(name, input, callId, descText) {
|
|
159
|
+
if (this.opts.envelope.chatmode === 'proactive')
|
|
160
|
+
return;
|
|
161
|
+
if (this.opts.suppressActivities)
|
|
162
|
+
return;
|
|
163
|
+
this.itemsQueue.push({
|
|
164
|
+
kind: 'tool_call',
|
|
165
|
+
call_id: callId || this.synthCallId(),
|
|
166
|
+
name,
|
|
167
|
+
arguments: input,
|
|
168
|
+
text: descText,
|
|
169
|
+
});
|
|
170
|
+
this.messageTimestamps.push(Date.now());
|
|
171
|
+
if (this.diagEnabled)
|
|
172
|
+
diag(this.instanceId, 'addToolCall', { name, callId });
|
|
173
|
+
this.scheduleFlush();
|
|
174
|
+
}
|
|
175
|
+
/** 添加工具结果 */
|
|
176
|
+
addToolResult(name, ok, result, error, callId, durationMs, descText) {
|
|
177
|
+
if (this.opts.envelope.chatmode === 'proactive')
|
|
178
|
+
return;
|
|
179
|
+
if (this.opts.suppressActivities)
|
|
180
|
+
return;
|
|
181
|
+
this.itemsQueue.push({
|
|
182
|
+
kind: 'tool_result',
|
|
183
|
+
call_id: callId || this.synthCallId(),
|
|
184
|
+
name,
|
|
185
|
+
ok,
|
|
186
|
+
result,
|
|
187
|
+
error,
|
|
188
|
+
duration_ms: durationMs,
|
|
189
|
+
text: descText,
|
|
190
|
+
});
|
|
191
|
+
this.messageTimestamps.push(Date.now());
|
|
192
|
+
if (this.diagEnabled)
|
|
193
|
+
diag(this.instanceId, 'addToolResult', { name, ok, callId });
|
|
194
|
+
this.scheduleFlush();
|
|
195
|
+
}
|
|
196
|
+
/** 添加进度提示 */
|
|
197
|
+
addProgress(text, opts = {}) {
|
|
198
|
+
if (this.opts.envelope.chatmode === 'proactive')
|
|
199
|
+
return;
|
|
200
|
+
if (this.opts.suppressActivities)
|
|
201
|
+
return;
|
|
202
|
+
this.itemsQueue.push({
|
|
203
|
+
kind: 'progress',
|
|
204
|
+
text,
|
|
205
|
+
state: opts.state,
|
|
206
|
+
tool_uses: opts.toolUses,
|
|
207
|
+
duration_ms: opts.durationMs,
|
|
208
|
+
});
|
|
209
|
+
this.messageTimestamps.push(Date.now());
|
|
210
|
+
this.scheduleFlush();
|
|
211
|
+
}
|
|
212
|
+
/** 添加系统提示 / 通知。force=true 时绕过 suppressActivities(用于 compact/retry/error 等操作反馈) */
|
|
213
|
+
addNotice(text, severity, subtype, force = false) {
|
|
214
|
+
if (this.opts.envelope.chatmode === 'proactive')
|
|
215
|
+
return;
|
|
216
|
+
if (this.opts.suppressActivities && !force)
|
|
217
|
+
return;
|
|
218
|
+
this.itemsQueue.push({ kind: 'notice', text, severity, subtype });
|
|
219
|
+
this.messageTimestamps.push(Date.now());
|
|
220
|
+
this.scheduleFlush();
|
|
221
|
+
}
|
|
222
|
+
// ── 内部:interactive 模式聚合窗口 ──
|
|
223
|
+
synthCallId() {
|
|
224
|
+
return `synth-${this.opts.envelope.taskId}-${++this.syntheticCallSeq}`;
|
|
225
|
+
}
|
|
226
|
+
scheduleFlush() {
|
|
227
|
+
if (this.timer) {
|
|
228
|
+
if (this.diagEnabled)
|
|
229
|
+
diag(this.instanceId, 'scheduleFlush:skip', { reason: 'timer_exists' });
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const interval = this.opts.flushDelay ?? 4000;
|
|
233
|
+
let targetDelay;
|
|
234
|
+
if (this.flushCount === 0) {
|
|
235
|
+
targetDelay = 500;
|
|
236
|
+
}
|
|
237
|
+
else if (this.flushCount <= 3) {
|
|
238
|
+
targetDelay = Math.ceil(interval / 2);
|
|
239
|
+
}
|
|
240
|
+
else if (this.messageTimestamps.length >= 5) {
|
|
241
|
+
targetDelay = this.calculateDynamicDelay(interval);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
targetDelay = interval;
|
|
245
|
+
}
|
|
246
|
+
const elapsed = Date.now() - this.lastFlush;
|
|
247
|
+
const delay = Math.max(0, targetDelay - elapsed);
|
|
248
|
+
if (this.diagEnabled) {
|
|
249
|
+
diag(this.instanceId, 'scheduleFlush:set', {
|
|
250
|
+
flushCount: this.flushCount,
|
|
251
|
+
targetDelay,
|
|
252
|
+
elapsed,
|
|
253
|
+
actualDelay: delay,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
this.timer = setTimeout(() => this.flushInternal(), delay);
|
|
257
|
+
}
|
|
258
|
+
calculateDynamicDelay(interval) {
|
|
259
|
+
const recent = this.messageTimestamps.slice(-10);
|
|
260
|
+
const intervals = [];
|
|
261
|
+
for (let i = 1; i < recent.length; i++) {
|
|
262
|
+
intervals.push(recent[i] - recent[i - 1]);
|
|
263
|
+
}
|
|
264
|
+
if (intervals.length === 0)
|
|
265
|
+
return interval;
|
|
266
|
+
const avgInterval = intervals.reduce((a, b) => a + b) / intervals.length;
|
|
267
|
+
const dynamicDelay = avgInterval * 3;
|
|
268
|
+
const minDelay = interval;
|
|
269
|
+
const maxDelay = interval * 2.5;
|
|
270
|
+
return Math.max(minDelay, Math.min(maxDelay, dynamicDelay));
|
|
271
|
+
}
|
|
272
|
+
/** 仅 flush 非 text items(text items 和 textBuffer 保留,等待下次完整 flush) */
|
|
273
|
+
async flushActivitiesInternal() {
|
|
274
|
+
const nonThinking = this.itemsQueue.filter(it => it.kind !== 'text');
|
|
275
|
+
if (nonThinking.length === 0)
|
|
276
|
+
return;
|
|
277
|
+
if (this.timer) {
|
|
278
|
+
clearTimeout(this.timer);
|
|
279
|
+
this.timer = undefined;
|
|
280
|
+
}
|
|
281
|
+
// 移除已 flush 的 non-text items,保留 text items
|
|
282
|
+
this.itemsQueue = this.itemsQueue.filter(it => it.kind === 'text');
|
|
283
|
+
const payload = { kind: 'activity.batch', items: nonThinking };
|
|
284
|
+
if (this.diagEnabled)
|
|
285
|
+
diag(this.instanceId, 'flushActivitiesOnly', { itemCount: nonThinking.length });
|
|
286
|
+
this.sentContent = true;
|
|
287
|
+
this.sendChain = this.sendChain
|
|
288
|
+
.then(() => this.opts.send(payload))
|
|
289
|
+
.catch(e => logger.warn('[IMRenderer] activity.batch send failed:', e));
|
|
290
|
+
await this.sendChain;
|
|
291
|
+
this.lastFlush = Date.now();
|
|
292
|
+
this.flushCount++;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* 完整 flush:把 itemsQueue 里所有 items 打包成 activity.batch 发送。
|
|
296
|
+
* 如果 isFinal=true,还会在 batch 之后单独发一条 result.text 作为最终回复。
|
|
297
|
+
*/
|
|
298
|
+
async flushInternal(isFinal) {
|
|
299
|
+
if (this.timer) {
|
|
300
|
+
clearTimeout(this.timer);
|
|
301
|
+
this.timer = undefined;
|
|
302
|
+
}
|
|
303
|
+
// 文件标记过滤
|
|
304
|
+
if (this.opts.fileMarkerPattern) {
|
|
305
|
+
this.textBuffer = this.textBuffer.replace(this.opts.fileMarkerPattern, '').trim();
|
|
306
|
+
for (const item of this.itemsQueue) {
|
|
307
|
+
if (item.kind === 'text')
|
|
308
|
+
item.text = item.text.replace(this.opts.fileMarkerPattern, '');
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// 清掉空 text items
|
|
312
|
+
const items = this.itemsQueue.filter(it => {
|
|
313
|
+
if (it.kind === 'text')
|
|
314
|
+
return it.text.length > 0;
|
|
315
|
+
return true;
|
|
316
|
+
});
|
|
317
|
+
this.itemsQueue = [];
|
|
318
|
+
const finalText = isFinal ? this.textBuffer : '';
|
|
319
|
+
if (isFinal)
|
|
320
|
+
this.textBuffer = '';
|
|
321
|
+
if (this.diagEnabled) {
|
|
322
|
+
diag(this.instanceId, 'flush', {
|
|
323
|
+
isFinal,
|
|
324
|
+
itemCount: items.length,
|
|
325
|
+
finalTextLen: finalText.length,
|
|
326
|
+
flushCount: this.flushCount,
|
|
327
|
+
sinceLastFlush: Date.now() - this.lastFlush,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
// 1. interactive 模式下:不发 text items(由 result.text 统一发送最终文本)
|
|
331
|
+
let itemsForBatch = items.filter(it => it.kind !== 'text');
|
|
332
|
+
if (itemsForBatch.length > 0) {
|
|
333
|
+
const payload = { kind: 'activity.batch', items: itemsForBatch };
|
|
334
|
+
this.sentContent = true;
|
|
335
|
+
this.sendChain = this.sendChain
|
|
336
|
+
.then(() => this.opts.send(payload))
|
|
337
|
+
.catch(e => logger.warn('[IMRenderer] activity.batch send failed:', e));
|
|
338
|
+
await this.sendChain;
|
|
339
|
+
this.lastFlush = Date.now();
|
|
340
|
+
this.flushCount++;
|
|
341
|
+
}
|
|
342
|
+
// 2. isFinal=true 时单独发最终回复文本
|
|
343
|
+
if (isFinal && finalText.length > 0) {
|
|
344
|
+
const payload = { kind: 'result.text', text: finalText, isFinal: true };
|
|
345
|
+
this.sentContent = true;
|
|
346
|
+
this.sendChain = this.sendChain
|
|
347
|
+
.then(() => this.opts.send(payload))
|
|
348
|
+
.catch(e => logger.warn('[IMRenderer] result.text send failed:', e));
|
|
349
|
+
await this.sendChain;
|
|
350
|
+
this.lastFlush = Date.now();
|
|
351
|
+
this.flushCount++;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// ── 内部:proactive 模式(逐事件 activity.batch[1 item]) ──
|
|
355
|
+
emitProactive(event) {
|
|
356
|
+
// 对齐 interactive 的 dedup:流式 text 已推过时,complete.result 不再重复发 summary
|
|
357
|
+
if (event.type === 'complete' &&
|
|
358
|
+
!event.isError &&
|
|
359
|
+
event.result &&
|
|
360
|
+
this.hasEmittedText) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const item = this.mapEventToItem(event);
|
|
364
|
+
if (!item)
|
|
365
|
+
return;
|
|
366
|
+
if (item.kind === 'text') {
|
|
367
|
+
this.hasEmittedText = true;
|
|
368
|
+
this.allText += item.text;
|
|
369
|
+
}
|
|
370
|
+
const payload = { kind: 'activity.batch', items: [item] };
|
|
371
|
+
// fire-and-forget
|
|
372
|
+
this.opts.send(payload).catch(err => {
|
|
373
|
+
logger.debug(`[IMRenderer] proactive send failed: ${err.message}`);
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
mapEventToItem(event) {
|
|
377
|
+
switch (event.type) {
|
|
378
|
+
case 'text':
|
|
379
|
+
if (!event.text)
|
|
380
|
+
return null;
|
|
381
|
+
return { kind: 'text', text: event.text };
|
|
382
|
+
case 'tool_use': {
|
|
383
|
+
const desc = summarizeToolInput(event.name, event.input || {});
|
|
384
|
+
return {
|
|
385
|
+
kind: 'tool_call',
|
|
386
|
+
call_id: event.callId || this.synthCallId(),
|
|
387
|
+
name: event.name,
|
|
388
|
+
arguments: event.input,
|
|
389
|
+
text: desc,
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
case 'tool_result':
|
|
393
|
+
if (event.isError) {
|
|
394
|
+
return {
|
|
395
|
+
kind: 'tool_result',
|
|
396
|
+
call_id: event.callId || this.synthCallId(),
|
|
397
|
+
name: event.name,
|
|
398
|
+
ok: false,
|
|
399
|
+
error: event.error || (typeof event.result === 'string' ? event.result : '执行失败'),
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
const resultText = this.truncate(this.stringifyResult(event.result), 200);
|
|
404
|
+
return {
|
|
405
|
+
kind: 'tool_result',
|
|
406
|
+
call_id: event.callId || this.synthCallId(),
|
|
407
|
+
name: event.name,
|
|
408
|
+
ok: true,
|
|
409
|
+
result: event.result,
|
|
410
|
+
text: resultText,
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
case 'compact':
|
|
414
|
+
return {
|
|
415
|
+
kind: 'notice',
|
|
416
|
+
text: `💡 会话压缩完成 (压缩前 tokens: ${event.preTokens})`,
|
|
417
|
+
severity: 'info',
|
|
418
|
+
subtype: 'compact',
|
|
419
|
+
};
|
|
420
|
+
case 'task_progress': {
|
|
421
|
+
const stats = this.formatTaskStats(event);
|
|
422
|
+
const text = event.summary
|
|
423
|
+
? `子任务: ${event.summary}${stats ? ` (${stats})` : ''}`
|
|
424
|
+
: `子任务进行中${stats ? `: ${stats}` : ''}`;
|
|
425
|
+
return {
|
|
426
|
+
kind: 'progress',
|
|
427
|
+
text,
|
|
428
|
+
state: 'processing',
|
|
429
|
+
tool_uses: event.toolUses,
|
|
430
|
+
duration_ms: event.durationMs,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
case 'error':
|
|
434
|
+
return { kind: 'notice', text: event.error, severity: 'warn' };
|
|
435
|
+
case 'complete':
|
|
436
|
+
if (event.isError) {
|
|
437
|
+
const errText = event.errors?.join('; ') || event.result || '任务失败';
|
|
438
|
+
return {
|
|
439
|
+
kind: 'summary',
|
|
440
|
+
text: errText,
|
|
441
|
+
is_error: true,
|
|
442
|
+
subtype: event.subtype,
|
|
443
|
+
duration_ms: event.durationMs,
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
if (event.result) {
|
|
447
|
+
return {
|
|
448
|
+
kind: 'summary',
|
|
449
|
+
text: event.result,
|
|
450
|
+
subtype: event.subtype,
|
|
451
|
+
duration_ms: event.durationMs,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
return null;
|
|
455
|
+
case 'session_id':
|
|
456
|
+
case 'state_changed':
|
|
457
|
+
case 'status':
|
|
458
|
+
return null;
|
|
459
|
+
default:
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
stringifyResult(result) {
|
|
464
|
+
if (result === null || result === undefined)
|
|
465
|
+
return '';
|
|
466
|
+
if (typeof result === 'string')
|
|
467
|
+
return result;
|
|
468
|
+
try {
|
|
469
|
+
return JSON.stringify(result);
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
return String(result);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
truncate(text, maxLen) {
|
|
476
|
+
return text.length > maxLen ? text.substring(0, maxLen) + '...' : text;
|
|
477
|
+
}
|
|
478
|
+
formatTaskStats(event) {
|
|
479
|
+
const parts = [];
|
|
480
|
+
if (event.toolUses)
|
|
481
|
+
parts.push(`${event.toolUses} tools`);
|
|
482
|
+
if (event.durationMs)
|
|
483
|
+
parts.push(`${Math.round(event.durationMs / 1000)}s`);
|
|
484
|
+
return parts.join(', ');
|
|
485
|
+
}
|
|
486
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把结构化 ThoughtItem 数组降级为人类可读的纯文本。
|
|
3
|
+
* 用于不支持 thought 能力的渠道(Feishu/WeChat/DingTalk/QQBot/WeCom),
|
|
4
|
+
* 在 send() 中收到 activity.batch 后调用。
|
|
5
|
+
*/
|
|
6
|
+
export function formatItemsAsText(items) {
|
|
7
|
+
if (!items || items.length === 0)
|
|
8
|
+
return ''; // early exit
|
|
9
|
+
const lines = [];
|
|
10
|
+
for (const item of items) {
|
|
11
|
+
const line = formatItem(item);
|
|
12
|
+
if (line)
|
|
13
|
+
lines.push(line);
|
|
14
|
+
}
|
|
15
|
+
return lines.join('\n');
|
|
16
|
+
}
|
|
17
|
+
function formatItem(item) {
|
|
18
|
+
switch (item.kind) {
|
|
19
|
+
case 'text':
|
|
20
|
+
return item.text;
|
|
21
|
+
case 'reasoning':
|
|
22
|
+
return `💭 ${item.text}`;
|
|
23
|
+
case 'tool_call': {
|
|
24
|
+
const desc = item.text || summarizeArgs(item.arguments);
|
|
25
|
+
if (!desc)
|
|
26
|
+
return `🔧 ${item.name}`;
|
|
27
|
+
// 多行 desc(如 Edit diff):第一行跟工具名同行,代码块从新行开始
|
|
28
|
+
if (desc.includes('\n')) {
|
|
29
|
+
const nlIdx = desc.indexOf('\n');
|
|
30
|
+
return `🔧 ${item.name} ${desc.slice(0, nlIdx)}\n${desc.slice(nlIdx + 1)}`;
|
|
31
|
+
}
|
|
32
|
+
return `🔧 ${item.name}: ${desc}`;
|
|
33
|
+
}
|
|
34
|
+
case 'tool_result': {
|
|
35
|
+
if (!item.ok) {
|
|
36
|
+
const errMsg = item.error || (typeof item.result === 'string' ? item.result : '执行失败');
|
|
37
|
+
return `⚠️ ${item.name}: ${errMsg}`;
|
|
38
|
+
}
|
|
39
|
+
return item.text ? `✓ ${item.name}: ${item.text}` : `✓ ${item.name}`;
|
|
40
|
+
}
|
|
41
|
+
case 'progress':
|
|
42
|
+
return `⏳ ${item.text}`;
|
|
43
|
+
case 'notice':
|
|
44
|
+
return item.severity === 'warn' ? `⚠️ ${item.text}` : item.text;
|
|
45
|
+
case 'summary':
|
|
46
|
+
return item.is_error ? `❌ ${item.text}` : item.text;
|
|
47
|
+
default:
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function summarizeArgs(args) {
|
|
52
|
+
if (!args || typeof args !== 'object')
|
|
53
|
+
return '';
|
|
54
|
+
const a = args;
|
|
55
|
+
if (typeof a.description === 'string')
|
|
56
|
+
return a.description;
|
|
57
|
+
if (typeof a.file_path === 'string')
|
|
58
|
+
return a.file_path;
|
|
59
|
+
if (typeof a.pattern === 'string')
|
|
60
|
+
return a.pattern;
|
|
61
|
+
if (typeof a.command === 'string')
|
|
62
|
+
return a.command.substring(0, 80);
|
|
63
|
+
if (typeof a.prompt === 'string')
|
|
64
|
+
return a.prompt.substring(0, 80);
|
|
65
|
+
if (typeof a.query === 'string')
|
|
66
|
+
return a.query.substring(0, 80);
|
|
67
|
+
return '';
|
|
68
|
+
}
|