@chen_hu_hu/pi-qq 1.0.0 → 1.0.2
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 +70 -104
- package/doc/{features.md → features-v1.md} +55 -92
- package/doc/{plan.md → plan-v1.md} +38 -43
- package/index.ts +111 -159
- package/package.json +2 -2
- package/src/append-stream.ts +21 -20
- package/src/bot.ts +23 -23
- package/src/budget-status.ts +9 -9
- package/src/config.ts +22 -80
- package/src/download.ts +3 -3
- package/src/files.ts +9 -9
- package/src/format.ts +6 -1
- package/src/images.ts +6 -6
- package/src/inbound.ts +12 -19
- package/src/login.ts +19 -20
- package/src/outbound.ts +22 -22
- package/src/qrcode-terminal.d.ts +1 -1
- package/src/reply-budget.ts +15 -15
- package/src/run-feedback.ts +70 -75
- package/src/usage.ts +0 -93
package/src/usage.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import type { ContextUsage } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* F5/F6 用量摘要:本次 agent run 的累计输入/输出/缓存命中率 + 当前上下文占比。
|
|
5
|
-
*
|
|
6
|
-
* pi 的 ExtensionContext 没有现成的 token 总和;只能从消息的 `usage`
|
|
7
|
-
* 字段聚合。这里按 agent_end 的 `event.messages` 统计本次 run:
|
|
8
|
-
* assistant 消息 + toolResult 自带的嵌套 usage(如 codemode/subagent)。
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/** 只依赖 message.usage 的结构化子集,便于单测与跨版本兼容 */
|
|
12
|
-
export interface UsageLike {
|
|
13
|
-
input: number;
|
|
14
|
-
output: number;
|
|
15
|
-
cacheRead: number;
|
|
16
|
-
cacheWrite: number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface UsageTotals {
|
|
20
|
-
input: number;
|
|
21
|
-
output: number;
|
|
22
|
-
cacheRead: number;
|
|
23
|
-
cacheWrite: number;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** 供 sumUsageMessages 使用的最小消息结构(不直接依赖 AgentMessage 导出) */
|
|
27
|
-
export interface UsageMessageLike {
|
|
28
|
-
role: string;
|
|
29
|
-
usage?: UsageLike | null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface UsageSummaryInput {
|
|
33
|
-
/** 本次 agent run 的累计用量 */
|
|
34
|
-
usage: UsageTotals;
|
|
35
|
-
context?: ContextUsage;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function emptyUsageTotals(): UsageTotals {
|
|
39
|
-
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** 累加一次 usage,返回新对象(不修改入参) */
|
|
43
|
-
export function addUsage(totals: UsageTotals, usage: UsageLike | null | undefined): UsageTotals {
|
|
44
|
-
if (!usage) return totals;
|
|
45
|
-
return {
|
|
46
|
-
input: totals.input + usage.input,
|
|
47
|
-
output: totals.output + usage.output,
|
|
48
|
-
cacheRead: totals.cacheRead + usage.cacheRead,
|
|
49
|
-
cacheWrite: totals.cacheWrite + usage.cacheWrite,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** 汇总本次 run 的消息用量:assistant + toolResult 的嵌套 usage */
|
|
54
|
-
export function sumUsageMessages(messages: readonly UsageMessageLike[]): UsageTotals {
|
|
55
|
-
let totals = emptyUsageTotals();
|
|
56
|
-
for (const message of messages) {
|
|
57
|
-
if (message.role === "assistant" || message.role === "toolResult") {
|
|
58
|
-
totals = addUsage(totals, message.usage);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return totals;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function formatTokenCount(n: number): string {
|
|
65
|
-
return n >= 1000 ? `${(n / 1000).toFixed(1).replace(/\.0$/, "")}k` : String(n);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function formatPercent(percent: number): string {
|
|
69
|
-
return `${percent.toFixed(1)}%`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function formatContextPercent(context: ContextUsage | undefined): string {
|
|
73
|
-
if (!context) return "未知";
|
|
74
|
-
if (context.tokens === null) return "重建中";
|
|
75
|
-
|
|
76
|
-
const percent =
|
|
77
|
-
context.percent ?? (context.contextWindow > 0 ? (context.tokens / context.contextWindow) * 100 : null);
|
|
78
|
-
return percent === null ? "未知" : formatPercent(percent);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** 生成严格三行的用量摘要 */
|
|
82
|
-
export function formatUsageSummary(input: UsageSummaryInput): string {
|
|
83
|
-
const { usage, context } = input;
|
|
84
|
-
const totalInput = usage.input + usage.cacheRead + usage.cacheWrite;
|
|
85
|
-
const cacheHitRate = totalInput > 0 ? (usage.cacheRead / totalInput) * 100 : 0;
|
|
86
|
-
|
|
87
|
-
return [
|
|
88
|
-
"📊 用量摘要",
|
|
89
|
-
`• 输入:${formatTokenCount(totalInput)} (${formatPercent(cacheHitRate)})`,
|
|
90
|
-
`• 输出:${formatTokenCount(usage.output)}`,
|
|
91
|
-
`• 上下文:${formatContextPercent(context)}`,
|
|
92
|
-
].join("\n");
|
|
93
|
-
}
|