@henryqw/pi-footer 0.2.1 → 0.2.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/extensions/footer.ts +37 -22
- package/package.json +1 -1
package/extensions/footer.ts
CHANGED
|
@@ -53,34 +53,49 @@ export default function footerExtension(pi: ExtensionAPI): void {
|
|
|
53
53
|
const commonName = commonDir && basename(commonDir) === ".git" ? basename(dirname(commonDir)) : undefined;
|
|
54
54
|
const repo = git.code === 0 ? commonName && commonName !== rootName ? commonName : rootName : basename(ctx.cwd);
|
|
55
55
|
|
|
56
|
+
// ponytail: keyed on length + last entry (sessions are append-only); revisit if entries ever mutate in place.
|
|
57
|
+
let usageKey: string | undefined;
|
|
58
|
+
let input = 0;
|
|
59
|
+
let output = 0;
|
|
60
|
+
let cost = 0;
|
|
61
|
+
let cacheRate: number | undefined;
|
|
62
|
+
const computeUsage = () => {
|
|
63
|
+
input = 0;
|
|
64
|
+
output = 0;
|
|
65
|
+
cost = 0;
|
|
66
|
+
cacheRate = undefined;
|
|
67
|
+
const add = (usage: Usage | undefined) => {
|
|
68
|
+
if (!usage) return;
|
|
69
|
+
input += usage.input;
|
|
70
|
+
output += usage.output;
|
|
71
|
+
cost += usage.cost.total;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
for (const entry of ctx.sessionManager.getEntries()) {
|
|
75
|
+
if (entry.type === "message" && entry.message.role === "assistant") {
|
|
76
|
+
const usage = entry.message.usage;
|
|
77
|
+
const prompt = usage.input + usage.cacheRead + usage.cacheWrite;
|
|
78
|
+
cacheRate = prompt ? usage.cacheRead / prompt * 100 : 0;
|
|
79
|
+
add(usage);
|
|
80
|
+
} else if (entry.type === "message" && entry.message.role === "toolResult") {
|
|
81
|
+
add(entry.message.usage);
|
|
82
|
+
} else if (entry.type === "branch_summary" || entry.type === "compaction") {
|
|
83
|
+
add(entry.usage);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
56
88
|
ctx.ui.setFooter((tui, theme, data) => {
|
|
57
89
|
const unsubscribe = data.onBranchChange(() => tui.requestRender());
|
|
58
90
|
return {
|
|
59
91
|
dispose: unsubscribe,
|
|
60
92
|
invalidate() { },
|
|
61
93
|
render(width: number): string[] {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!usage) return;
|
|
68
|
-
input += usage.input;
|
|
69
|
-
output += usage.output;
|
|
70
|
-
cost += usage.cost.total;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
for (const entry of ctx.sessionManager.getEntries()) {
|
|
74
|
-
if (entry.type === "message" && entry.message.role === "assistant") {
|
|
75
|
-
const usage = entry.message.usage;
|
|
76
|
-
const prompt = usage.input + usage.cacheRead + usage.cacheWrite;
|
|
77
|
-
cacheRate = prompt ? usage.cacheRead / prompt * 100 : 0;
|
|
78
|
-
add(usage);
|
|
79
|
-
} else if (entry.type === "message" && entry.message.role === "toolResult") {
|
|
80
|
-
add(entry.message.usage);
|
|
81
|
-
} else if (entry.type === "branch_summary" || entry.type === "compaction") {
|
|
82
|
-
add(entry.usage);
|
|
83
|
-
}
|
|
94
|
+
const entries = ctx.sessionManager.getEntries();
|
|
95
|
+
const key = `${entries.length}:${entries.at(-1)?.type}`;
|
|
96
|
+
if (key !== usageKey) {
|
|
97
|
+
usageKey = key;
|
|
98
|
+
computeUsage();
|
|
84
99
|
}
|
|
85
100
|
|
|
86
101
|
const branch = data.getGitBranch()?.replace(/^worktree\//, "");
|