@aeondave/pi-skill-optimizer 3.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.
- package/BENCHMARK.md +354 -0
- package/CHANGELOG.md +117 -0
- package/LICENSE +21 -0
- package/README.md +148 -0
- package/assets/banner.png +0 -0
- package/docs/ARCHITECTURE.md +66 -0
- package/docs/CONFIGURATION.md +138 -0
- package/docs/RANKING.md +155 -0
- package/package.json +85 -0
- package/src/activation.ts +88 -0
- package/src/aliases.ts +111 -0
- package/src/config.ts +446 -0
- package/src/corpus.ts +416 -0
- package/src/evaluation.ts +692 -0
- package/src/generate.ts +365 -0
- package/src/history.ts +147 -0
- package/src/index.ts +1362 -0
- package/src/init-profile.ts +96 -0
- package/src/init-prompt.ts +10 -0
- package/src/init-readiness.ts +123 -0
- package/src/optimize.ts +424 -0
- package/src/output-recovery.ts +19 -0
- package/src/output.ts +629 -0
- package/src/persistence.ts +535 -0
- package/src/profile.ts +253 -0
- package/src/prompts/init-profile.md +40 -0
- package/src/request.ts +304 -0
- package/src/skill-loader.ts +138 -0
- package/src/skills.ts +1230 -0
- package/src/stats.ts +178 -0
- package/src/tools.ts +548 -0
- package/src/usage.ts +256 -0
package/src/stats.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure persistent telemetry. Savings are characters removed, never token
|
|
3
|
+
* counts. Extraction guard outcomes are separate event counters.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type SavingsArea = "skills" | "tools" | "output";
|
|
7
|
+
|
|
8
|
+
export interface SavingsByArea {
|
|
9
|
+
skills: number;
|
|
10
|
+
tools: number;
|
|
11
|
+
output: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ExtractionTelemetry {
|
|
15
|
+
attempts: number;
|
|
16
|
+
accepted: number;
|
|
17
|
+
fallbackEvidence: number;
|
|
18
|
+
fallbackSavings: number;
|
|
19
|
+
fallbackError: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Authoritative provider-reported usage. Token counts and cost are never estimated. */
|
|
23
|
+
export interface ProviderCacheTelemetry {
|
|
24
|
+
requests: number;
|
|
25
|
+
input: number;
|
|
26
|
+
output: number;
|
|
27
|
+
cacheRead: number;
|
|
28
|
+
cacheWrite: number;
|
|
29
|
+
totalCost: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface StatsFile {
|
|
33
|
+
version: 3;
|
|
34
|
+
updatedAt: string;
|
|
35
|
+
/** Lifetime characters removed by optimization area. */
|
|
36
|
+
lifetime: SavingsByArea;
|
|
37
|
+
extraction: ExtractionTelemetry;
|
|
38
|
+
/** Lifetime provider-reported request, token, cache, and cost totals. */
|
|
39
|
+
cache: ProviderCacheTelemetry;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const EMPTY_SAVINGS: SavingsByArea = { skills: 0, tools: 0, output: 0 };
|
|
43
|
+
export const EMPTY_EXTRACTION_TELEMETRY: ExtractionTelemetry = {
|
|
44
|
+
attempts: 0,
|
|
45
|
+
accepted: 0,
|
|
46
|
+
fallbackEvidence: 0,
|
|
47
|
+
fallbackSavings: 0,
|
|
48
|
+
fallbackError: 0,
|
|
49
|
+
};
|
|
50
|
+
export const EMPTY_PROVIDER_CACHE_TELEMETRY: ProviderCacheTelemetry = {
|
|
51
|
+
requests: 0,
|
|
52
|
+
input: 0,
|
|
53
|
+
output: 0,
|
|
54
|
+
cacheRead: 0,
|
|
55
|
+
cacheWrite: 0,
|
|
56
|
+
totalCost: 0,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
function nonNeg(value: unknown): number {
|
|
60
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function nonNegCost(value: unknown): number {
|
|
64
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function normalizeSavings(value: unknown): SavingsByArea {
|
|
68
|
+
const source = value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
69
|
+
return { skills: nonNeg(source.skills), tools: nonNeg(source.tools), output: nonNeg(source.output) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Read lifetime character savings from v1/v2 or a bare legacy object. */
|
|
73
|
+
export function normalizeStatsFile(value: unknown): SavingsByArea {
|
|
74
|
+
const source = value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
75
|
+
return normalizeSavings("lifetime" in source ? source.lifetime : source);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Read extraction counters from a complete stats file or a bare counter object. */
|
|
79
|
+
export function normalizeExtractionTelemetry(value: unknown): ExtractionTelemetry {
|
|
80
|
+
const outer = value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
81
|
+
const source = outer.extraction && typeof outer.extraction === "object" && !Array.isArray(outer.extraction)
|
|
82
|
+
? outer.extraction as Record<string, unknown>
|
|
83
|
+
: outer;
|
|
84
|
+
return {
|
|
85
|
+
attempts: nonNeg(source.attempts),
|
|
86
|
+
accepted: nonNeg(source.accepted),
|
|
87
|
+
fallbackEvidence: nonNeg(source.fallbackEvidence),
|
|
88
|
+
fallbackSavings: nonNeg(source.fallbackSavings),
|
|
89
|
+
fallbackError: nonNeg(source.fallbackError),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Read v3 cache telemetry, or return an empty snapshot for v1/v2 files. */
|
|
94
|
+
export function normalizeProviderCacheTelemetry(value: unknown): ProviderCacheTelemetry {
|
|
95
|
+
const outer = value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
96
|
+
const source = outer.cache && typeof outer.cache === "object" && !Array.isArray(outer.cache)
|
|
97
|
+
? outer.cache as Record<string, unknown>
|
|
98
|
+
: outer;
|
|
99
|
+
return {
|
|
100
|
+
requests: nonNeg(source.requests),
|
|
101
|
+
input: nonNeg(source.input),
|
|
102
|
+
output: nonNeg(source.output),
|
|
103
|
+
cacheRead: nonNeg(source.cacheRead),
|
|
104
|
+
cacheWrite: nonNeg(source.cacheWrite),
|
|
105
|
+
totalCost: nonNegCost(source.totalCost),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function addSavings(a: SavingsByArea, b: SavingsByArea): SavingsByArea {
|
|
110
|
+
return { skills: a.skills + b.skills, tools: a.tools + b.tools, output: a.output + b.output };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function subtractSavings(a: SavingsByArea, b: SavingsByArea): SavingsByArea {
|
|
114
|
+
return {
|
|
115
|
+
skills: Math.max(0, a.skills - b.skills),
|
|
116
|
+
tools: Math.max(0, a.tools - b.tools),
|
|
117
|
+
output: Math.max(0, a.output - b.output),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function totalSavings(savings: SavingsByArea): number {
|
|
122
|
+
return savings.skills + savings.tools + savings.output;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function addExtractionTelemetry(a: ExtractionTelemetry, b: ExtractionTelemetry): ExtractionTelemetry {
|
|
126
|
+
return {
|
|
127
|
+
attempts: a.attempts + b.attempts,
|
|
128
|
+
accepted: a.accepted + b.accepted,
|
|
129
|
+
fallbackEvidence: a.fallbackEvidence + b.fallbackEvidence,
|
|
130
|
+
fallbackSavings: a.fallbackSavings + b.fallbackSavings,
|
|
131
|
+
fallbackError: a.fallbackError + b.fallbackError,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function addProviderCacheTelemetry(a: ProviderCacheTelemetry, b: ProviderCacheTelemetry): ProviderCacheTelemetry {
|
|
136
|
+
return {
|
|
137
|
+
requests: a.requests + b.requests,
|
|
138
|
+
input: a.input + b.input,
|
|
139
|
+
output: a.output + b.output,
|
|
140
|
+
cacheRead: a.cacheRead + b.cacheRead,
|
|
141
|
+
cacheWrite: a.cacheWrite + b.cacheWrite,
|
|
142
|
+
totalCost: a.totalCost + b.totalCost,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function subtractExtractionTelemetry(a: ExtractionTelemetry, b: ExtractionTelemetry): ExtractionTelemetry {
|
|
147
|
+
return {
|
|
148
|
+
attempts: Math.max(0, a.attempts - b.attempts),
|
|
149
|
+
accepted: Math.max(0, a.accepted - b.accepted),
|
|
150
|
+
fallbackEvidence: Math.max(0, a.fallbackEvidence - b.fallbackEvidence),
|
|
151
|
+
fallbackSavings: Math.max(0, a.fallbackSavings - b.fallbackSavings),
|
|
152
|
+
fallbackError: Math.max(0, a.fallbackError - b.fallbackError),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function subtractProviderCacheTelemetry(a: ProviderCacheTelemetry, b: ProviderCacheTelemetry): ProviderCacheTelemetry {
|
|
157
|
+
return {
|
|
158
|
+
requests: Math.max(0, a.requests - b.requests),
|
|
159
|
+
input: Math.max(0, a.input - b.input),
|
|
160
|
+
output: Math.max(0, a.output - b.output),
|
|
161
|
+
cacheRead: Math.max(0, a.cacheRead - b.cacheRead),
|
|
162
|
+
cacheWrite: Math.max(0, a.cacheWrite - b.cacheWrite),
|
|
163
|
+
totalCost: Math.max(0, a.totalCost - b.totalCost),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function totalExtractionEvents(telemetry: ExtractionTelemetry): number {
|
|
168
|
+
return Object.values(telemetry).reduce((sum, value) => sum + value, 0);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function toStatsFile(
|
|
172
|
+
lifetime: SavingsByArea,
|
|
173
|
+
now = Date.now(),
|
|
174
|
+
extraction: ExtractionTelemetry = EMPTY_EXTRACTION_TELEMETRY,
|
|
175
|
+
cache: ProviderCacheTelemetry = EMPTY_PROVIDER_CACHE_TELEMETRY,
|
|
176
|
+
): StatsFile {
|
|
177
|
+
return { version: 3, updatedAt: new Date(now).toISOString(), lifetime, extraction, cache };
|
|
178
|
+
}
|