token-stats-timer 1.0.8 → 1.0.9
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 +6 -3
- package/notify.ts +4 -30
- package/package.json +1 -1
- package/step-timer.ts +4 -85
package/README.md
CHANGED
|
@@ -57,10 +57,13 @@ Footer 下行:cwd + git 分支 + 其他扩展状态。
|
|
|
57
57
|
## 任务计时(step-timer)
|
|
58
58
|
|
|
59
59
|
- 任务执行中:工作指示器(spinner 文案)显示 `Working... 01:02`(整体已耗时,每秒刷新)
|
|
60
|
-
-
|
|
61
|
-
- 完成后:会话末尾插入一条汇总 `总耗时 01:23`(失败时 ❌,中止时 ⏹),经 `appendEntry` 持久化、不进入 LLM 上下文,`/resume` 后仍在
|
|
60
|
+
- 完成后:会话末尾插入一条汇总,仅显示总耗时:
|
|
62
61
|
|
|
63
|
-
|
|
62
|
+
`总耗时 01:23`
|
|
63
|
+
|
|
64
|
+
经 `appendEntry` 持久化、不进入 LLM 上下文,`/resume` 后仍在;无主题色样式。
|
|
65
|
+
|
|
66
|
+
无独立开关,随包启用;计时口径与 run-timer 一致(一次 run = 首个 agent_start → agent_settled,含重试/压缩/排队提示)。早期版本的每 turn 摘要(timing-turn)、“⏱ 思考”每条消息内联标注与 Thinking 实时标签均已移除,历史条目自动隐藏。
|
|
64
67
|
|
|
65
68
|
## 命令
|
|
66
69
|
|
package/notify.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// enabled 总开关(默认 true)
|
|
10
10
|
// minDurationSec 时长低于该秒数的 run 不通知(默认 0 = 每次都通知)
|
|
11
11
|
// sound 通知声音(默认 "Glass","" 表示静音)
|
|
12
|
-
// onSuccess /
|
|
12
|
+
// onSuccess / onAbort / onSessionEnd 各类通知开关
|
|
13
13
|
//
|
|
14
14
|
// 命令:/notify on | off | status
|
|
15
15
|
|
|
@@ -27,7 +27,6 @@ export interface NotifyConfig {
|
|
|
27
27
|
minDurationSec: number;
|
|
28
28
|
sound: string;
|
|
29
29
|
onSuccess: boolean;
|
|
30
|
-
onFailure: boolean;
|
|
31
30
|
onAbort: boolean;
|
|
32
31
|
onSessionEnd: boolean;
|
|
33
32
|
}
|
|
@@ -37,7 +36,6 @@ const DEFAULT_CONFIG: NotifyConfig = {
|
|
|
37
36
|
minDurationSec: 0,
|
|
38
37
|
sound: "Glass",
|
|
39
38
|
onSuccess: true,
|
|
40
|
-
onFailure: true,
|
|
41
39
|
onAbort: false,
|
|
42
40
|
onSessionEnd: false,
|
|
43
41
|
};
|
|
@@ -80,9 +78,7 @@ export function createNotifier(pi: ExtensionAPI): void {
|
|
|
80
78
|
|
|
81
79
|
// 当前 run 的状态(agent_start 重置,agent_end 收集,agent_settled 消费)
|
|
82
80
|
let runStartMs: number | undefined;
|
|
83
|
-
let runFailed = false;
|
|
84
81
|
let runAborted = false;
|
|
85
|
-
let runErrorMsg: string | undefined;
|
|
86
82
|
|
|
87
83
|
// 进程内去抖计时
|
|
88
84
|
let lastNotifyAt = 0;
|
|
@@ -102,9 +98,7 @@ export function createNotifier(pi: ExtensionAPI): void {
|
|
|
102
98
|
}
|
|
103
99
|
|
|
104
100
|
pi.on("agent_start", () => {
|
|
105
|
-
runFailed = false;
|
|
106
101
|
runAborted = false;
|
|
107
|
-
runErrorMsg = undefined;
|
|
108
102
|
if (runStartMs === undefined) runStartMs = Date.now();
|
|
109
103
|
});
|
|
110
104
|
|
|
@@ -115,17 +109,7 @@ export function createNotifier(pi: ExtensionAPI): void {
|
|
|
115
109
|
if (m.role === "assistant") {
|
|
116
110
|
if (m.stopReason === "aborted") {
|
|
117
111
|
runAborted = true;
|
|
118
|
-
runErrorMsg = runErrorMsg || "被用户中止";
|
|
119
|
-
} else if (m.stopReason === "error" || m.errorMessage) {
|
|
120
|
-
runFailed = true;
|
|
121
|
-
runErrorMsg = runErrorMsg || m.errorMessage || "模型调用出错";
|
|
122
112
|
}
|
|
123
|
-
} else if (m.role === "toolResult" && m.isError) {
|
|
124
|
-
runFailed = true;
|
|
125
|
-
const toolMsg = `工具 ${m.toolName} 执行失败`;
|
|
126
|
-
runErrorMsg = runErrorMsg && !runErrorMsg.startsWith("工具 ")
|
|
127
|
-
? runErrorMsg
|
|
128
|
-
: toolMsg;
|
|
129
113
|
}
|
|
130
114
|
}
|
|
131
115
|
});
|
|
@@ -142,19 +126,11 @@ export function createNotifier(pi: ExtensionAPI): void {
|
|
|
142
126
|
|
|
143
127
|
const dur = startMs !== undefined ? `${formatDuration(durationMs)}` : "-";
|
|
144
128
|
|
|
145
|
-
if (
|
|
146
|
-
if (config.onFailure) {
|
|
147
|
-
notify(
|
|
148
|
-
"❌ pi 执行失败",
|
|
149
|
-
`耗时:${dur}\n${truncate(runErrorMsg || "未知错误")}`,
|
|
150
|
-
config.sound,
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
} else if (runAborted) {
|
|
129
|
+
if (runAborted) {
|
|
154
130
|
if (config.onAbort) {
|
|
155
131
|
notify(
|
|
156
132
|
"⏹ pi 已中止",
|
|
157
|
-
`耗时:${dur}\n${truncate(
|
|
133
|
+
`耗时:${dur}\n${truncate("执行被中止")}`,
|
|
158
134
|
config.sound,
|
|
159
135
|
);
|
|
160
136
|
}
|
|
@@ -167,9 +143,7 @@ export function createNotifier(pi: ExtensionAPI): void {
|
|
|
167
143
|
}
|
|
168
144
|
|
|
169
145
|
// 消费本次结果,避免串扰下一次 run
|
|
170
|
-
runFailed = false;
|
|
171
146
|
runAborted = false;
|
|
172
|
-
runErrorMsg = undefined;
|
|
173
147
|
});
|
|
174
148
|
|
|
175
149
|
// 会话关闭时提示(可选)
|
|
@@ -196,7 +170,7 @@ export function createNotifier(pi: ExtensionAPI): void {
|
|
|
196
170
|
const sound = config.sound ? `, 声音=${config.sound}` : ", 静音";
|
|
197
171
|
ctx.ui.notify(
|
|
198
172
|
`完成通知: ${config.enabled ? "开" : "关"}, 最短时长=${config.minDurationSec}s, ` +
|
|
199
|
-
`成功=${config.onSuccess ? "开" : "关"},
|
|
173
|
+
`成功=${config.onSuccess ? "开" : "关"}, ` +
|
|
200
174
|
`中止=${config.onAbort ? "开" : "关"}, 会话结束=${config.onSessionEnd ? "开" : "关"}${sound}`,
|
|
201
175
|
"info",
|
|
202
176
|
);
|
package/package.json
CHANGED
package/step-timer.ts
CHANGED
|
@@ -2,13 +2,8 @@
|
|
|
2
2
|
// =============================================================================
|
|
3
3
|
// 实时显示(仅交互 TUI):
|
|
4
4
|
// - 任务执行中:工作指示器文案显示 "Working... 01:02"(整体已耗时,每秒刷新)
|
|
5
|
-
// 逐条内联标注(每条 assistant 消息末尾自动追加一行):
|
|
6
|
-
// - 例:`ai 回复内容...` 后另起一行 `> ⏱ 思考 00:05`
|
|
7
|
-
// - 当前正在生成的消息:该行随流式内容实时更新(思考计时);消息提交后永久定格
|
|
8
|
-
// - 历史消息各自保留提交时的耗时,互不影响、不再变化(标注已存在则跳过,resume 回放不显示)
|
|
9
|
-
// (pi 的 hiddenThinkingLabel 文案是全局的、无法逐条保留,故改用内联标注)
|
|
10
5
|
// 完成后汇总(appendEntry 持久化,不进入 LLM 上下文,resume 后可回看):
|
|
11
|
-
// - agent_settled appendEntry("timing-final", …)
|
|
6
|
+
// - agent_settled appendEntry("timing-final", …):仅本次任务总耗时
|
|
12
7
|
//
|
|
13
8
|
// 一次 run = 空闲后的首个 agent_start → agent_settled(与 run-timer 语义一致,
|
|
14
9
|
// 包含重试、压缩恢复和排队的 steering/follow-up 提示)。
|
|
@@ -19,8 +14,6 @@ import { Box, Text } from "@earendil-works/pi-tui";
|
|
|
19
14
|
/** 一次 run 的总耗时汇总(appendEntry "timing-final") */
|
|
20
15
|
export interface FinalTimingData {
|
|
21
16
|
totalMs: number;
|
|
22
|
-
failed: boolean;
|
|
23
|
-
aborted: boolean;
|
|
24
17
|
}
|
|
25
18
|
|
|
26
19
|
const FINAL_TYPE = "timing-final";
|
|
@@ -44,29 +37,6 @@ export function createStepTimer(pi: ExtensionAPI): void {
|
|
|
44
37
|
|
|
45
38
|
let runActive = false;
|
|
46
39
|
let runStartMs = 0;
|
|
47
|
-
let runFailed = false;
|
|
48
|
-
let runAborted = false;
|
|
49
|
-
|
|
50
|
-
// 逐条内联标注:当前消息的思考计时(每条 assistant 消息开始时重置)
|
|
51
|
-
// curThinkingMs:已结算的思考毫秒;thinkingStart:进行中思考块的起表时间
|
|
52
|
-
let curThinkingMs = 0;
|
|
53
|
-
let thinkingStart: number | undefined;
|
|
54
|
-
|
|
55
|
-
/** 结算当前进行中的思考块(若有) */
|
|
56
|
-
function settleThinking(now: number): void {
|
|
57
|
-
if (thinkingStart !== undefined) {
|
|
58
|
-
curThinkingMs += now - thinkingStart;
|
|
59
|
-
thinkingStart = undefined;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** 当前消息的思考总耗时(含进行中块):有思考数据才显示 */
|
|
64
|
-
function currentThinkingMs(now: number): number | undefined {
|
|
65
|
-
if (curThinkingMs === 0 && thinkingStart === undefined) return undefined;
|
|
66
|
-
return curThinkingMs + (thinkingStart !== undefined ? now - thinkingStart : 0);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const MARK_TEXT = "⏱ 思考";
|
|
70
40
|
|
|
71
41
|
function startTicker(): void {
|
|
72
42
|
stopTicker();
|
|
@@ -85,8 +55,6 @@ export function createStepTimer(pi: ExtensionAPI): void {
|
|
|
85
55
|
function appendFinal(): void {
|
|
86
56
|
pi.appendEntry<FinalTimingData>(FINAL_TYPE, {
|
|
87
57
|
totalMs: Date.now() - runStartMs,
|
|
88
|
-
failed: runFailed,
|
|
89
|
-
aborted: runAborted,
|
|
90
58
|
});
|
|
91
59
|
}
|
|
92
60
|
|
|
@@ -101,47 +69,10 @@ export function createStepTimer(pi: ExtensionAPI): void {
|
|
|
101
69
|
if (runActive) return;
|
|
102
70
|
runActive = true;
|
|
103
71
|
runStartMs = Date.now();
|
|
104
|
-
runFailed = false;
|
|
105
|
-
runAborted = false;
|
|
106
|
-
curThinkingMs = 0;
|
|
107
|
-
thinkingStart = undefined;
|
|
108
72
|
startTicker();
|
|
109
73
|
if (lastCtx?.hasUI) lastCtx.ui.setWorkingMessage("Working... 00:00");
|
|
110
74
|
});
|
|
111
75
|
|
|
112
|
-
// 每条 assistant 消息开始时重置思考计时
|
|
113
|
-
pi.on("message_start", (event, ctx) => {
|
|
114
|
-
if (!runActive || event.message.role !== "assistant") return;
|
|
115
|
-
lastCtx = ctx;
|
|
116
|
-
curThinkingMs = 0;
|
|
117
|
-
thinkingStart = undefined;
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
pi.on("message_update", (event, ctx) => {
|
|
121
|
-
if (!runActive) return;
|
|
122
|
-
lastCtx = ctx;
|
|
123
|
-
const ev = event.assistantMessageEvent;
|
|
124
|
-
if (!ev) return;
|
|
125
|
-
const now = Date.now();
|
|
126
|
-
if (ev.type === "thinking_start") {
|
|
127
|
-
if (thinkingStart === undefined) thinkingStart = now;
|
|
128
|
-
} else if (ev.type === "thinking_end" || ev.type === "text_start") {
|
|
129
|
-
settleThinking(now);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
pi.on("tool_execution_start", (_event, ctx) => {
|
|
134
|
-
if (!runActive) return;
|
|
135
|
-
lastCtx = ctx;
|
|
136
|
-
// 工具执行通常在同一消息的 toolCall 内容之后:结算思考计时
|
|
137
|
-
settleThinking(Date.now());
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
pi.on("agent_end", (event, ctx) => {
|
|
141
|
-
if (!runActive) return;
|
|
142
|
-
lastCtx = ctx;
|
|
143
|
-
});
|
|
144
|
-
|
|
145
76
|
pi.on("agent_settled", (_event, ctx) => {
|
|
146
77
|
lastCtx = ctx;
|
|
147
78
|
if (!runActive) return;
|
|
@@ -161,23 +92,11 @@ export function createStepTimer(pi: ExtensionAPI): void {
|
|
|
161
92
|
lastCtx = undefined;
|
|
162
93
|
});
|
|
163
94
|
|
|
164
|
-
|
|
165
|
-
// 只在 assistant 文本上追加一行耗时;已带标注(历史消息重渲染)则跳过保持原值
|
|
166
|
-
pi.registerMarkdownTransformer((markdown, context) => {
|
|
167
|
-
if (context.messageType !== "assistant") return markdown;
|
|
168
|
-
if (markdown.includes(MARK_TEXT)) return markdown;
|
|
169
|
-
const now = Date.now();
|
|
170
|
-
const ms = currentThinkingMs(now);
|
|
171
|
-
if (ms === undefined) return markdown;
|
|
172
|
-
return `${markdown}\n\n> ${MARK_TEXT} ${formatDuration(ms)}`;
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
pi.registerEntryRenderer<FinalTimingData>(FINAL_TYPE, (entry, _opts, theme) => {
|
|
95
|
+
pi.registerEntryRenderer<FinalTimingData>(FINAL_TYPE, (entry, _opts, _theme) => {
|
|
176
96
|
const d = entry.data;
|
|
177
97
|
if (!d) return undefined;
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
box.addChild(new Text(`${title} ${formatDuration(d.totalMs)}`, 0, 0));
|
|
98
|
+
const box = new Box(1, 1);
|
|
99
|
+
box.addChild(new Text(`总耗时 ${formatDuration(d.totalMs)}`, 0, 0));
|
|
181
100
|
return box;
|
|
182
101
|
});
|
|
183
102
|
}
|