@giladbarnea/pi-custom-footer 0.1.8 → 0.1.10
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 +2 -2
- package/index.ts +31 -5
- package/package.json +1 -1
- package/skill-tracker.ts +63 -2
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
last_updated: 2026-11-
|
|
2
|
+
last_updated: 2026-11-07T17-30
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
<div align="center">
|
|
@@ -22,7 +22,7 @@ pi install npm:@giladbarnea/pi-custom-footer
|
|
|
22
22
|
|
|
23
23
|
Supports Pi 1.4.x and 1.5.x, the latest stable minor and its predecessor.
|
|
24
24
|
|
|
25
|
-
Above the editor, see your model, thinking level, session name and ID. Loaded skills get their own row, with token ages. Compaction and context edits remove skills whose loaded text is no longer present verbatim. Restoring that text restores the skill.
|
|
25
|
+
Above the editor, see your model, thinking level, session name and ID. Loaded skills get their own row, with token ages. Compaction and context edits remove skills whose loaded text is no longer present verbatim. Restoring that text restores the skill. A skill read inside a codemode script counts when the script output carries that read verbatim. Pi does not keep nested read results, so the footer records the read text in a custom session entry that is never sent to the model.
|
|
26
26
|
|
|
27
27
|
Below the editor, see:
|
|
28
28
|
|
package/index.ts
CHANGED
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
*
|
|
20
20
|
* Known gaps reviewed September 30, 2026, against Pi 0.99.0:
|
|
21
21
|
* - Costs omit codemode and nested-model usage recorded on tool results.
|
|
22
|
-
* - Loaded skills omit successful reads recorded in tool-result nestedCalls
|
|
22
|
+
* - Loaded skills omit successful reads recorded in tool-result nestedCalls
|
|
23
|
+
* that ran without this extension, which records their returned text.
|
|
23
24
|
* - The header omits the routed physical model and thinking level.
|
|
24
25
|
* - After compaction, unknown usage falls back to the selected model's window
|
|
25
26
|
* or 200k, ignoring the known physical window from getContextUsage().
|
|
@@ -28,7 +29,10 @@ import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
|
28
29
|
import type { ExtensionAPI, ExtensionContext, SessionEntry, Theme } from "@earendil-works/pi-coding-agent";
|
|
29
30
|
import { truncateToWidth, visibleWidth, type TUI } from "@earendil-works/pi-tui";
|
|
30
31
|
|
|
31
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
getLoadedSkills, isNestedSkillRead, NESTED_SKILL_READ_ENTRY, nestedReadText, nestedSkillReadTexts,
|
|
34
|
+
type NestedSkillRead, type SkillLocation,
|
|
35
|
+
} from "./skill-tracker.ts";
|
|
32
36
|
|
|
33
37
|
// ── Layout constants ────────────────────────────────────────────────────────
|
|
34
38
|
|
|
@@ -492,6 +496,7 @@ interface SessionStats {
|
|
|
492
496
|
function getSessionStats(branch: readonly SessionEntry[]): SessionStats {
|
|
493
497
|
let turnCount = 0;
|
|
494
498
|
let totalCost = 0;
|
|
499
|
+
let turnCost = 0;
|
|
495
500
|
let totalReused = 0;
|
|
496
501
|
let totalFresh = 0;
|
|
497
502
|
let lastContextTokens = 0;
|
|
@@ -505,12 +510,21 @@ function getSessionStats(branch: readonly SessionEntry[]): SessionStats {
|
|
|
505
510
|
cacheWasReported = false;
|
|
506
511
|
continue;
|
|
507
512
|
}
|
|
513
|
+
// Like Pi's footer, count usage entries (cache warms) in the totals only: they are not turns.
|
|
514
|
+
if (entry.type === "usage") {
|
|
515
|
+
const { input, cacheRead, cacheWrite, cost } = entry.usage;
|
|
516
|
+
totalCost += cost.total;
|
|
517
|
+
totalReused += cacheRead;
|
|
518
|
+
totalFresh += input + cacheWrite;
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
508
521
|
if (entry.type !== "message" || entry.message?.role !== "assistant") continue;
|
|
509
522
|
const message = entry.message as AssistantMessage;
|
|
510
523
|
if (message.stopReason === "error" || message.stopReason === "aborted") continue;
|
|
511
524
|
const { input, output, cacheRead, cacheWrite, cost } = message.usage;
|
|
512
525
|
const promptTokens = input + cacheRead + cacheWrite;
|
|
513
526
|
turnCount += 1;
|
|
527
|
+
turnCost += cost.total;
|
|
514
528
|
totalCost += cost.total;
|
|
515
529
|
totalReused += cacheRead;
|
|
516
530
|
totalFresh += input + cacheWrite;
|
|
@@ -536,7 +550,7 @@ function getSessionStats(branch: readonly SessionEntry[]): SessionStats {
|
|
|
536
550
|
sessionCacheHitRate: totalPrompt > 0 ? (totalReused / totalPrompt) * 100 : null,
|
|
537
551
|
latestCacheHitRate,
|
|
538
552
|
latestCacheMissTokens,
|
|
539
|
-
averageCostPerTurn: turnCount > 0 ?
|
|
553
|
+
averageCostPerTurn: turnCount > 0 ? turnCost / turnCount : 0,
|
|
540
554
|
lastContextTokens,
|
|
541
555
|
};
|
|
542
556
|
}
|
|
@@ -586,17 +600,19 @@ export default function (pi: ExtensionAPI) {
|
|
|
586
600
|
let gitDirty = false;
|
|
587
601
|
let thinkingLevel = "off";
|
|
588
602
|
let loadedSkills: Array<{ name: string; tokenAge: number | null }> = [];
|
|
603
|
+
const runningNestedSkillReads = new Set<string>();
|
|
589
604
|
let skillsDirty = true;
|
|
590
605
|
let skillContextChangeId: string | undefined;
|
|
591
606
|
let snapshot: FooterSnapshot | undefined;
|
|
592
607
|
|
|
593
608
|
// ── Rendering ────────────────────────────────────────────────────────
|
|
594
609
|
|
|
595
|
-
function refreshLoadedSkills(ctx: ExtensionContext, currentContextTokens: number | null): void {
|
|
610
|
+
function refreshLoadedSkills(ctx: ExtensionContext, branch: readonly SessionEntry[], currentContextTokens: number | null): void {
|
|
596
611
|
loadedSkills = getLoadedSkills(
|
|
597
612
|
ctx.sessionManager.buildSessionProjection().entries,
|
|
598
613
|
getSkillLocations(pi),
|
|
599
614
|
ctx.cwd,
|
|
615
|
+
nestedSkillReadTexts(branch),
|
|
600
616
|
).map(({ name, loadedAtTokens }) => ({
|
|
601
617
|
name,
|
|
602
618
|
tokenAge:
|
|
@@ -618,7 +634,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
618
634
|
// Boundary edits commit after turn_end, which may already have rendered the old projection.
|
|
619
635
|
const contextChangeId = branch.reduce<string | undefined>((latest, entry) =>
|
|
620
636
|
entry.type === "context_edit" || entry.type === "compaction" ? entry.id : latest, undefined);
|
|
621
|
-
if (skillsDirty || contextChangeId !== skillContextChangeId) refreshLoadedSkills(currentCtx, usage?.tokens ?? null);
|
|
637
|
+
if (skillsDirty || contextChangeId !== skillContextChangeId) refreshLoadedSkills(currentCtx, branch, usage?.tokens ?? null);
|
|
622
638
|
skillContextChangeId = contextChangeId;
|
|
623
639
|
skillsDirty = false;
|
|
624
640
|
snapshot = {
|
|
@@ -804,10 +820,20 @@ export default function (pi: ExtensionAPI) {
|
|
|
804
820
|
gitBranch = null;
|
|
805
821
|
gitDirty = false;
|
|
806
822
|
loadedSkills = [];
|
|
823
|
+
runningNestedSkillReads.clear();
|
|
807
824
|
snapshot = undefined;
|
|
808
825
|
skillsDirty = true;
|
|
809
826
|
});
|
|
810
827
|
|
|
828
|
+
pi.on("tool_execution_start", async (event, ctx) => {
|
|
829
|
+
if (isNestedSkillRead(event, getSkillLocations(pi), ctx.cwd)) runningNestedSkillReads.add(event.toolCallId);
|
|
830
|
+
});
|
|
831
|
+
pi.on("tool_execution_end", async (event) => {
|
|
832
|
+
if (!runningNestedSkillReads.delete(event.toolCallId)) return;
|
|
833
|
+
const readText = nestedReadText(event);
|
|
834
|
+
if (readText !== null) pi.appendEntry<NestedSkillRead>(NESTED_SKILL_READ_ENTRY, { toolCallId: event.toolCallId, text: readText });
|
|
835
|
+
});
|
|
836
|
+
|
|
811
837
|
pi.on("model_select", async (_event: any, ctx: any) => updateCtx(ctx));
|
|
812
838
|
pi.on("thinking_level_select", async (event: any) => {
|
|
813
839
|
thinkingLevel = event.level;
|
package/package.json
CHANGED
package/skill-tracker.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ProjectedSessionEntry } from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import type { ProjectedSessionEntry, SessionEntry, ToolExecutionEndEvent, ToolExecutionStartEvent } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
|
|
@@ -12,6 +12,14 @@ export type LoadedSkill = {
|
|
|
12
12
|
name: string;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
+
/** A successful nested skill read's returned text, kept as a custom session entry because Pi does not persist nested results. */
|
|
16
|
+
export type NestedSkillRead = {
|
|
17
|
+
toolCallId: string;
|
|
18
|
+
text: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const NESTED_SKILL_READ_ENTRY = "custom-footer-nested-skill-read";
|
|
22
|
+
|
|
15
23
|
type UnknownRecord = Record<string, unknown>;
|
|
16
24
|
|
|
17
25
|
const MAX_SKILL_NAME_LENGTH = 64;
|
|
@@ -59,6 +67,57 @@ function skillNamesForReadPath(rawPath: string, skills: readonly SkillLocation[]
|
|
|
59
67
|
return skills.flatMap((skill) => path === resolvePath(cwd, skill.filePath) ? [skill.name] : []);
|
|
60
68
|
}
|
|
61
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Whether a starting tool call is a nested skill read, such as one from a codemode script.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* isNestedSkillRead({ type: "tool_execution_start", toolCallId: "c/1", toolName: "read", args: { path: "/s/SKILL.md" }, parentToolCallId: "c" }, [{ name: "s", filePath: "/s/SKILL.md" }], "/") // true
|
|
75
|
+
*/
|
|
76
|
+
export function isNestedSkillRead(event: ToolExecutionStartEvent, skills: readonly SkillLocation[], cwd: string): boolean {
|
|
77
|
+
return event.parentToolCallId !== undefined && readPaths({ name: event.toolName, arguments: event.args })
|
|
78
|
+
.some((path) => skillNamesForReadPath(path, skills, cwd).length > 0);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The text a successful nested read returned to its caller, to match against the parent result later.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* nestedReadText({ type: "tool_execution_end", toolCallId: "c/1", toolName: "read", result: { content: [{ type: "text", text: "Body." }] }, isError: false }) // "Body."
|
|
86
|
+
*/
|
|
87
|
+
export function nestedReadText(event: ToolExecutionEndEvent): string | null {
|
|
88
|
+
return event.isError ? null : textFromContent(event.result?.content);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Nested read texts recorded on a session branch, by nested tool call id.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* nestedSkillReadTexts([]) // Map(0) {}
|
|
96
|
+
*/
|
|
97
|
+
export function nestedSkillReadTexts(branch: readonly SessionEntry[]): Map<string, string> {
|
|
98
|
+
return new Map(branch.flatMap((entry) => {
|
|
99
|
+
if (entry.type !== "custom" || entry.customType !== NESTED_SKILL_READ_ENTRY) return [];
|
|
100
|
+
const { toolCallId, text } = entry.data as NestedSkillRead;
|
|
101
|
+
return [[toolCallId, text]];
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Skills whose nested read text, captured when the read succeeded, survives verbatim in the projected parent result. */
|
|
106
|
+
function deliveredNestedSkillNames(
|
|
107
|
+
message: UnknownRecord,
|
|
108
|
+
skills: readonly SkillLocation[],
|
|
109
|
+
cwd: string,
|
|
110
|
+
nestedReadTexts: ReadonlyMap<string, string>,
|
|
111
|
+
): string[] {
|
|
112
|
+
if (!isRecord(message.nestedCalls) || !Array.isArray(message.nestedCalls.calls)) return [];
|
|
113
|
+
const deliveredText = textFromContent(message.content);
|
|
114
|
+
return message.nestedCalls.calls.filter(isRecord).flatMap((call) => {
|
|
115
|
+
const readText = typeof call.id === "string" ? nestedReadTexts.get(call.id) : undefined;
|
|
116
|
+
if (readText === undefined || !deliveredText.includes(readText)) return [];
|
|
117
|
+
return readPaths(call).flatMap((path) => skillNamesForReadPath(path, skills, cwd));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
62
121
|
function assistantPromptTokens(message: UnknownRecord, compactionTimestamp: number | null): number | null {
|
|
63
122
|
if (message.role !== "assistant" || message.stopReason === "error" || message.stopReason === "aborted") return null;
|
|
64
123
|
if (compactionTimestamp !== null && (typeof message.timestamp !== "number" || message.timestamp <= compactionTimestamp)) return null;
|
|
@@ -80,12 +139,13 @@ function latestCompactionTimestamp(history: readonly unknown[]): number | null {
|
|
|
80
139
|
* Return skills whose loaded text survives verbatim in Pi's projection, with their first valid post-load prompt size.
|
|
81
140
|
*
|
|
82
141
|
* @example
|
|
83
|
-
* getLoadedSkills([], [], "/workspace") // []
|
|
142
|
+
* getLoadedSkills([], [], "/workspace", new Map()) // []
|
|
84
143
|
*/
|
|
85
144
|
export function getLoadedSkills(
|
|
86
145
|
entries: readonly ProjectedSessionEntry[],
|
|
87
146
|
skills: readonly SkillLocation[],
|
|
88
147
|
cwd: string,
|
|
148
|
+
nestedReadTexts: ReadonlyMap<string, string>,
|
|
89
149
|
): LoadedSkill[] {
|
|
90
150
|
const history: Array<{ message: unknown; originalContent: unknown }> = entries.flatMap(({ sourceEntry, messages }) => messages.map((message) => ({
|
|
91
151
|
message,
|
|
@@ -110,6 +170,7 @@ export function getLoadedSkills(
|
|
|
110
170
|
}
|
|
111
171
|
|
|
112
172
|
if (message.role === "toolResult") {
|
|
173
|
+
for (const skillName of deliveredNestedSkillNames(message, skills, cwd, nestedReadTexts)) markLoaded(skillName);
|
|
113
174
|
if (message.isError !== false || typeof message.toolCallId !== "string") continue;
|
|
114
175
|
if (!textFromContent(message.content).includes(textFromContent(originalContent))) continue;
|
|
115
176
|
for (const skillName of skillNamesByToolCall.get(message.toolCallId) ?? []) markLoaded(skillName);
|