-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprogress.js
More file actions
259 lines (228 loc) · 8.32 KB
/
Copy pathprogress.js
File metadata and controls
259 lines (228 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// 实时进度显示模块
// 支持统一适配器事件格式(type: progress/text)
// 工具图标映射(Claude 专用,Codex 用通用图标)
const TOOL_ICONS = {
Read: "📖",
Write: "✍️",
Edit: "✏️",
Bash: "💻",
Glob: "🔍",
Grep: "🔎",
WebFetch: "🌐",
WebSearch: "🔍",
Agent: "🤖",
NotebookEdit: "📓",
TodoWrite: "📝",
TaskCreate: "📋",
TaskUpdate: "📋",
TaskList: "📋",
TaskGet: "📋",
AskUserQuestion: "❓",
take_screenshot: "📸",
"mcp__computer-use__screenshot": "📸",
"mcp__peekaboo__see": "👁️",
"mcp__chrome-devtools__take_screenshot": "📸",
"mcp__chrome-devtools__take_snapshot": "📸",
};
const SILENT_TOOLS = new Set([
"TodoWrite", "TaskCreate", "TaskUpdate", "TaskList", "TaskGet",
]);
const MAX_ENTRIES = 15;
const EDIT_THROTTLE_MS = 2000;
/**
* 生成紧凑 inline diff 预览
* @param {object} input - Edit 工具的 input(含 old_string, new_string, file_path)
* @returns {string|null} 格式化的 diff 行,或 null
*/
function formatInlineDiff(input) {
if (!input?.old_string || !input?.new_string) return null;
const fileName = input.file_path ? input.file_path.split("/").pop() : "";
const oldLines = input.old_string.split("\n");
const newLines = input.new_string.split("\n");
const removed = oldLines.length;
const added = newLines.length;
// 取第一行有实际变化的内容做摘要
const oldSnippet = input.old_string.trim().split("\n")[0].slice(0, 40);
const newSnippet = input.new_string.trim().split("\n")[0].slice(0, 40);
let diff = `📄 ${fileName} −${removed} +${added}`;
if (oldSnippet !== newSnippet) {
diff += `\n − ${oldSnippet}`;
diff += `\n + ${newSnippet}`;
}
return diff;
}
/**
* 格式化 Agent(子 agent)事件
* @param {object} input - Agent 工具的 input
* @returns {string} 显示文本
*/
function formatAgentEntry(input) {
const name = input?.name || input?.subagent_type || "";
const desc = input?.description || "";
const parts = [name, desc].filter(Boolean);
const label = parts.join(" — ").slice(0, 60);
return label || "subagent";
}
export function createProgressTracker(ctx, chatId, verboseLevel = 1, backendLabel = "CC", { replyMarkup = null } = {}) {
let progressMsgId = null;
let typingInterval = null;
let entries = [];
let lastEditTime = 0;
let editTimer = null;
let finished = false;
const startedAt = Date.now();
function elapsedLabel() {
const sec = Math.round((Date.now() - startedAt) / 1000);
if (sec < 60) return `${sec}s`;
const min = Math.floor(sec / 60);
const s = sec % 60;
return `${min}m${s > 0 ? ` ${s}s` : ""}`;
}
const headerBase = `⏳ ${backendLabel} 正在处理`;
function headerText() {
return `${headerBase}... ⏱ ${elapsedLabel()}`;
}
async function start({ visibleMessage = true } = {}) {
if (visibleMessage) {
try {
const opts = replyMarkup ? { reply_markup: replyMarkup } : {};
const msg = await ctx.api.sendMessage(chatId, headerText(), opts);
progressMsgId = msg.message_id;
} catch {
// 发送失败不影响主流程
}
}
// Typing 心跳(每 4 秒发一次,Telegram typing 持续 5 秒)
typingInterval = setInterval(() => {
ctx.api.sendChatAction(chatId, "typing").catch(() => {});
}, 4000);
// 立刻发一次
ctx.api.sendChatAction(chatId, "typing").catch(() => {});
}
// 处理适配器统一事件
function processEvent(event) {
if (finished || verboseLevel === 0) return;
if (event.type === "progress") {
const toolName = event.toolName || "action";
if (SILENT_TOOLS.has(toolName)) return;
const icon = TOOL_ICONS[toolName] || "🔧";
// ── 特殊工具:Edit → inline diff 预览 ──
if (toolName === "Edit" && event.input) {
const diff = formatInlineDiff(event.input);
if (diff) {
entries.push(`${icon} ${diff}`);
scheduleEdit();
return;
}
}
// ── 特殊工具:Agent → 子 agent 身份展示 ──
if (toolName === "Agent" && event.input) {
const label = formatAgentEntry(event.input);
entries.push(`${icon} Agent: ${label}`);
scheduleEdit();
return;
}
if (verboseLevel >= 2 && event.input) {
const input = typeof event.input === "object"
? (event.input.command || event.input.file_path || event.input.description || event.input.pattern || event.input.query || "").slice(0, 60)
: (event.detail || "").slice(0, 60);
entries.push(`${icon} ${toolName}${input ? ": " + input : ""}`);
} else if (verboseLevel >= 2 && event.detail) {
entries.push(`${icon} ${toolName}: ${event.detail.slice(0, 60)}`);
} else {
// verboseLevel 1: 同名工具合并计数,避免一堆 "Bash / Bash / Bash"
const lastEntry = entries[entries.length - 1] || "";
const counterRe = new RegExp(`^${icon.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')} ${toolName}(?: x(\\d+))?$`);
const m = lastEntry.match(counterRe);
if (m) {
const count = (parseInt(m[1]) || 1) + 1;
entries[entries.length - 1] = `${icon} ${toolName} x${count}`;
} else {
entries.push(`${icon} ${toolName}`);
}
}
} else if (event.type === "text" && event.text && verboseLevel >= 2) {
const snippet = event.text.slice(0, 80).replace(/\n/g, " ");
if (snippet.trim()) {
entries.push(`💭 ${snippet}${event.text.length > 80 ? "..." : ""}`);
}
}
// 保留最近 MAX_ENTRIES 条
if (entries.length > MAX_ENTRIES) {
entries = entries.slice(-MAX_ENTRIES);
}
scheduleEdit();
}
function scheduleEdit() {
if (!progressMsgId || finished) return;
const now = Date.now();
const timeSinceLastEdit = now - lastEditTime;
if (timeSinceLastEdit >= EDIT_THROTTLE_MS) {
doEdit();
} else if (!editTimer) {
editTimer = setTimeout(() => {
editTimer = null;
if (!finished) doEdit();
}, EDIT_THROTTLE_MS - timeSinceLastEdit);
}
}
function doEdit() {
if (!progressMsgId || finished) return;
lastEditTime = Date.now();
const text = entries.length > 0
? `${headerText()}\n\n${entries.join("\n")}`
: headerText();
const editOpts = replyMarkup ? { reply_markup: replyMarkup } : {};
ctx.api.editMessageText(chatId, progressMsgId, text, editOpts).catch(() => {});
}
/**
* 交出消息所有权给 streaming preview
* 停止 progress 的编辑,但不删除消息,返回消息 ID
* @returns {number|null}
*/
function surrender() {
finished = true;
if (editTimer) { clearTimeout(editTimer); editTimer = null; }
// 不清 typingInterval — typing 心跳继续跑,对用户有益
const id = progressMsgId;
progressMsgId = null; // 释放所有权
return id;
}
function getMessageId() {
return progressMsgId;
}
async function finish({ keepAsSummary = false, durationMs = 0, skipMessage = false } = {}) {
finished = true;
if (editTimer) {
clearTimeout(editTimer);
editTimer = null;
}
if (typingInterval) {
clearInterval(typingInterval);
typingInterval = null;
}
// skipMessage: 消息已被 streaming preview 接管,只做清理
if (skipMessage || !progressMsgId) return;
if (keepAsSummary && entries.length > 0) {
// 统计各工具调用次数
const toolCounts = {};
for (const entry of entries) {
const match = entry.match(/^\S+\s+(\w+)/);
if (match && match[1]) toolCounts[match[1]] = (toolCounts[match[1]] || 0) + 1;
}
const toolSummary = Object.entries(toolCounts)
.map(([name, count]) => `${name}${count > 1 ? ` x${count}` : ""}`)
.join(", ");
const durLabel = durationMs > 0 ? ` ${Math.round(durationMs / 1000)}s` : "";
const text = `✅ Done${durLabel} — ${toolSummary || "no tools"}`;
await ctx.api.editMessageText(chatId, progressMsgId, text).catch(async () => {
// edit 失败 fallback 到删除
await ctx.api.deleteMessage(chatId, progressMsgId).catch(() => {});
});
} else {
await ctx.api.deleteMessage(chatId, progressMsgId).catch(() => {});
}
progressMsgId = null;
}
return { start, processEvent, finish, surrender, getMessageId };
}