@0xkahi/pi-qol 0.1.0 → 1.0.0

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.
Files changed (32) hide show
  1. package/README.md +13 -0
  2. package/assets/config.schema.json +23 -0
  3. package/package.json +4 -4
  4. package/src/codemap.md +3 -2
  5. package/src/config-loader.ts +4 -0
  6. package/src/constants.ts +1 -0
  7. package/src/extensions/auto-session-name/title-generator.ts +1 -1
  8. package/src/extensions/codemap.md +9 -2
  9. package/src/extensions/context-view/capture.ts +394 -0
  10. package/src/extensions/context-view/codemap.md +28 -0
  11. package/src/extensions/context-view/constants.ts +4 -0
  12. package/src/extensions/context-view/context-view-controller.ts +67 -0
  13. package/src/extensions/context-view/index.ts +120 -0
  14. package/src/extensions/context-view/measure.ts +307 -0
  15. package/src/extensions/context-view/model.ts +207 -0
  16. package/src/extensions/context-view/ui/context-view-dialog.ts +81 -0
  17. package/src/extensions/context-view/ui/injections-model.ts +257 -0
  18. package/src/extensions/context-view/ui/injections-view.ts +396 -0
  19. package/src/extensions/context-view/ui/layout.ts +88 -0
  20. package/src/extensions/context-view/ui/navigation.ts +37 -0
  21. package/src/extensions/context-view/ui/skill-preview.ts +82 -0
  22. package/src/extensions/context-view/ui/usage-map.ts +133 -0
  23. package/src/extensions/context-view/ui/usage-view.ts +734 -0
  24. package/src/extensions/context-view/usage.ts +432 -0
  25. package/src/extensions/model-select/index.ts +12 -6
  26. package/src/extensions/model-select/model-select-dialog.ts +2 -1
  27. package/src/extensions/model-select/types.ts +2 -0
  28. package/src/index.ts +2 -0
  29. package/src/schemas/codemap.md +2 -2
  30. package/src/schemas/config.schema.ts +3 -0
  31. package/src/schemas/context-view.config.schema.ts +13 -0
  32. package/src/schemas/model-select.config.schema.ts +3 -1
@@ -0,0 +1,133 @@
1
+ /** Forked from dimk90/pi-context-view at f6f007b867212bcf81a61519c8e40ce209cdd608 (MIT). */
2
+ /**
3
+ * Pure proportional-cell model for the Usage view's 14×14 context map. The
4
+ * map uses estimated category totals against the selected Window/Fit scale.
5
+ * Pi's separately reported occupied tokens may differ because of tokenizer,
6
+ * serialization, caching, and last-response timing.
7
+ */
8
+ import type { ContextUsageSnapshot } from '../model';
9
+
10
+ export const DEFAULT_MAP_COLUMNS = 14;
11
+ export const DEFAULT_MAP_ROWS = 14;
12
+
13
+ const FIT_SCALE_PERCENT = 115;
14
+ const PERCENT_DENOMINATOR = 100;
15
+ const MINIMUM_FIT_SCALE_TOKENS = 10_000;
16
+ const FIT_SCALE_SIGNIFICANT_DIGITS = 2;
17
+
18
+ /** One visual map cell assigned to a category, the auto-compact buffer, or remaining free space. */
19
+ export interface UsageMapCell {
20
+ readonly categoryId?: string;
21
+ readonly fill: 'full' | 'partial' | 'buffer' | 'free';
22
+ }
23
+
24
+ /** Rectangular context-usage map in row-major order. */
25
+ export interface UsageMap {
26
+ readonly columns: number;
27
+ readonly rows: number;
28
+ readonly cells: readonly UsageMapCell[];
29
+ }
30
+
31
+ interface MapSegment {
32
+ readonly categoryId: string;
33
+ readonly start: number;
34
+ readonly end: number;
35
+ }
36
+
37
+ /**
38
+ * Calculate a Fit denominator from estimated occupancy: 15% headroom,
39
+ * rounded upward to two significant digits, with the documented floor/cap.
40
+ */
41
+ export function calculateFitMapScale(usage: ContextUsageSnapshot): number | undefined {
42
+ const contextWindow = usage.reported?.contextWindow;
43
+ if (contextWindow === undefined || !Number.isFinite(contextWindow) || contextWindow <= 0) return undefined;
44
+
45
+ const estimatedTotal = usage.categories.reduce((sum, category) => sum + category.tokens, 0);
46
+ const withHeadroom = (Math.max(0, estimatedTotal) * FIT_SCALE_PERCENT) / PERCENT_DENOMINATOR;
47
+ const rounded = roundUpToSignificantDigits(withHeadroom, FIT_SCALE_SIGNIFICANT_DIGITS);
48
+ return Math.min(contextWindow, Math.max(MINIMUM_FIT_SCALE_TOKENS, rounded));
49
+ }
50
+
51
+ /**
52
+ * Build a proportional map from estimated categories. `scaleTokens` changes
53
+ * only the mapped denominator; buffer placement remains anchored to the true
54
+ * context window. Returns undefined without a usable denominator.
55
+ */
56
+ export function buildUsageMap(
57
+ usage: ContextUsageSnapshot,
58
+ columns = DEFAULT_MAP_COLUMNS,
59
+ rows = DEFAULT_MAP_ROWS,
60
+ scaleTokens?: number,
61
+ ): UsageMap | undefined {
62
+ const contextWindow = usage.reported?.contextWindow;
63
+ if (contextWindow === undefined || !Number.isFinite(contextWindow) || contextWindow <= 0 || columns <= 0 || rows <= 0) return undefined;
64
+
65
+ const requestedScale = scaleTokens ?? contextWindow;
66
+ if (!Number.isFinite(requestedScale) || requestedScale <= 0) return undefined;
67
+ const mapScale = Math.min(contextWindow, requestedScale);
68
+ const cellCount = Math.floor(columns) * Math.floor(rows);
69
+ const estimatedTotal = usage.categories.reduce((sum, category) => sum + category.tokens, 0);
70
+ const occupiedTokens = clamp(estimatedTotal, 0, mapScale);
71
+ const occupiedCells = (occupiedTokens / mapScale) * cellCount;
72
+ // Fit reserves its headroom as visible free space; the true-window buffer remains outside the mapped range.
73
+ const windowOccupancy = clamp(estimatedTotal, 0, contextWindow);
74
+ const bufferTokens = mapScale < contextWindow ? 0 : clamp(usage.autoCompactReserveTokens ?? 0, 0, contextWindow - windowOccupancy);
75
+ const bufferStart = ((contextWindow - bufferTokens) / mapScale) * cellCount;
76
+ const segments = createSegments(usage, estimatedTotal, occupiedCells);
77
+ const cells = Array.from({ length: cellCount }, (_, index) => createCell(index, occupiedCells, bufferStart, segments));
78
+ return { columns: Math.floor(columns), rows: Math.floor(rows), cells };
79
+ }
80
+
81
+ /** Scale estimated category shares into the occupied map range. */
82
+ function createSegments(usage: ContextUsageSnapshot, estimatedTotal: number, occupiedCells: number): MapSegment[] {
83
+ if (estimatedTotal <= 0 || occupiedCells <= 0) return [];
84
+ const segments: MapSegment[] = [];
85
+ let cursor = 0;
86
+ for (const category of usage.categories) {
87
+ const size = (category.tokens / estimatedTotal) * occupiedCells;
88
+ segments.push({ categoryId: category.id, start: cursor, end: cursor + size });
89
+ cursor += size;
90
+ }
91
+ return segments;
92
+ }
93
+
94
+ /** Assign one map cell to its largest category overlap and classify its fill. */
95
+ function createCell(index: number, occupiedCells: number, bufferStart: number, segments: readonly MapSegment[]): UsageMapCell {
96
+ const occupiedOverlap = overlap(index, index + 1, 0, occupiedCells);
97
+ if (occupiedOverlap <= 0) {
98
+ // An unoccupied cell belongs to the buffer when at least half of it lies past the trigger point.
99
+ return overlap(index, index + 1, bufferStart, index + 1) >= 0.5 ? { fill: 'buffer' } : { fill: 'free' };
100
+ }
101
+
102
+ let categoryId: string | undefined;
103
+ let categoryOverlap = 0;
104
+ for (const segment of segments) {
105
+ const currentOverlap = overlap(index, index + 1, segment.start, segment.end);
106
+ if (currentOverlap > categoryOverlap) {
107
+ categoryId = segment.categoryId;
108
+ categoryOverlap = currentOverlap;
109
+ }
110
+ }
111
+ return {
112
+ categoryId,
113
+ fill: categoryOverlap >= 0.7 ? 'full' : 'partial',
114
+ };
115
+ }
116
+
117
+ /** Length shared by two half-open numeric ranges. */
118
+ function overlap(aStart: number, aEnd: number, bStart: number, bEnd: number): number {
119
+ return Math.max(0, Math.min(aEnd, bEnd) - Math.max(aStart, bStart));
120
+ }
121
+
122
+ /** Round a positive value upward at the requested significant-digit boundary. */
123
+ function roundUpToSignificantDigits(value: number, significantDigits: number): number {
124
+ if (!Number.isFinite(value) || value <= 0) return 0;
125
+ const boundary = 10 ** (Math.floor(Math.log10(value)) - significantDigits + 1);
126
+ return Math.ceil(value / boundary) * boundary;
127
+ }
128
+
129
+ /** Restrict a finite value to an inclusive range. */
130
+ function clamp(value: number, minimum: number, maximum: number): number {
131
+ if (!Number.isFinite(value)) return minimum;
132
+ return Math.min(maximum, Math.max(minimum, value));
133
+ }