@agimon-ai/po-log 0.0.0-alpha.21 → 0.0.0-alpha.210
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/LICENSE +21 -149
- package/README.md +4 -10
- package/dist/apiContracts.cjs +81 -57
- package/dist/apiContracts.cjs.map +1 -1
- package/dist/apiContracts.d.mts +2 -2
- package/dist/apiContracts.mjs +82 -58
- package/dist/apiContracts.mjs.map +1 -1
- package/dist/extensions/native.mjs +403 -0
- package/dist/extensions/native.mjs.map +1 -0
- package/dist/extensions/server.cjs +835 -296
- package/dist/extensions/server.cjs.map +1 -1
- package/dist/extensions/server.d.mts.map +1 -1
- package/dist/extensions/server.mjs +837 -298
- package/dist/extensions/server.mjs.map +1 -1
- package/dist/extensions/web.mjs +735 -300
- package/dist/extensions/web.mjs.map +1 -1
- package/dist/findings-Bo4IHNpr.cjs +165 -0
- package/dist/findings-Bo4IHNpr.cjs.map +1 -0
- package/dist/findings-DAKeRG4O.mjs +160 -0
- package/dist/findings-DAKeRG4O.mjs.map +1 -0
- package/dist/http-D7IHAk6Y.mjs +29 -0
- package/dist/http-D7IHAk6Y.mjs.map +1 -0
- package/dist/http-Dhn2bpMh.cjs +40 -0
- package/dist/http-Dhn2bpMh.cjs.map +1 -0
- package/dist/index.cjs +3 -2
- package/dist/index.d.mts +8 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -1
- package/dist/{metrics-jCZe13Xg.cjs → metrics-Bml7TSZq.cjs} +9 -160
- package/dist/metrics-Bml7TSZq.cjs.map +1 -0
- package/dist/{metrics-DywCpDJ_.mjs → metrics-DwfV84Fy.mjs} +2 -159
- package/dist/metrics-DwfV84Fy.mjs.map +1 -0
- package/dist/metrics.cjs +3 -2
- package/dist/metrics.mjs +3 -1
- package/dist/{metricsSource-Cy8f47Q0.cjs → metricsSource-CF3cp-sf.cjs} +16 -43
- package/dist/metricsSource-CF3cp-sf.cjs.map +1 -0
- package/dist/{metricsSource-BaoBR_Ia.mjs → metricsSource-I5p5_t2L.mjs} +9 -42
- package/dist/metricsSource-I5p5_t2L.mjs.map +1 -0
- package/dist/{metricsSource-DNT8BrTF.d.mts → metricsSource-sO8OpIes.d.mts} +14 -3
- package/dist/metricsSource-sO8OpIes.d.mts.map +1 -0
- package/dist/metricsSource.cjs +1 -1
- package/dist/metricsSource.d.mts +1 -13
- package/dist/metricsSource.d.mts.map +1 -1
- package/dist/metricsSource.mjs +1 -1
- package/dist/metricsStore-BcjSOJGN.mjs +44 -0
- package/dist/metricsStore-BcjSOJGN.mjs.map +1 -0
- package/dist/metricsStore-nGI-c5NH.cjs +79 -0
- package/dist/metricsStore-nGI-c5NH.cjs.map +1 -0
- package/dist/metricsStore.cjs +476 -0
- package/dist/metricsStore.cjs.map +1 -0
- package/dist/metricsStore.d.mts +63 -0
- package/dist/metricsStore.d.mts.map +1 -0
- package/dist/metricsStore.mjs +471 -0
- package/dist/metricsStore.mjs.map +1 -0
- package/dist/po-runtime-identity.json +1 -0
- package/llms.txt +5 -1
- package/locales/vi.json +202 -0
- package/locales/zh-CN.json +202 -0
- package/package.json +81 -18
- package/src/prompts/codemode-how-to-logsink/SKILL.md +80 -0
- package/src/prompts/help-how-to-log/SKILL.md +28 -0
- package/dist/metrics-DywCpDJ_.mjs.map +0 -1
- package/dist/metrics-jCZe13Xg.cjs.map +0 -1
- package/dist/metricsSource-BaoBR_Ia.mjs.map +0 -1
- package/dist/metricsSource-Cy8f47Q0.cjs.map +0 -1
- package/dist/metricsSource-DNT8BrTF.d.mts.map +0 -1
- package/src/prompts/po-debug-agent/SKILL.md +0 -28
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
//#region src/services/findings/index.ts
|
|
2
|
+
/** Below this, the prompt prefix is churning and cache writes are being wasted. */
|
|
3
|
+
const LOW_CACHE_HIT_RATIO = .3;
|
|
4
|
+
/** Cache hit rate is meaningless on a session that has barely sent anything. */
|
|
5
|
+
const MIN_CACHE_SAMPLE_TOKENS = 5e4;
|
|
6
|
+
/** A tool is "expensive" only well clear of the session's typical turn. */
|
|
7
|
+
const EXPENSIVE_TOOL_RATIO = 1.5;
|
|
8
|
+
const MIN_TOOL_SAMPLES = 3;
|
|
9
|
+
const FAILING_TOOL_RATIO = .25;
|
|
10
|
+
const MIN_TOOL_CALLS = 3;
|
|
11
|
+
/** Sustained input this large per turn means context is carried, not scoped. */
|
|
12
|
+
const HEAVY_TURN_INPUT_TOKENS = 12e4;
|
|
13
|
+
const MIN_TURNS_FOR_AVERAGE = 3;
|
|
14
|
+
/** Tool calls per turn above this reads as search thrash rather than progress. */
|
|
15
|
+
const BUSY_TURN_TOOL_CALLS = 8;
|
|
16
|
+
const RATE_LIMITED = "rate_limited";
|
|
17
|
+
/** Kept short because the finding's subject column already says "provider". */
|
|
18
|
+
const SHORT_CATEGORY = {
|
|
19
|
+
[RATE_LIMITED]: "rate limited",
|
|
20
|
+
provider_unavailable: "unavailable"
|
|
21
|
+
};
|
|
22
|
+
const SEVERITY_RANK = {
|
|
23
|
+
critical: 0,
|
|
24
|
+
warning: 1,
|
|
25
|
+
info: 2
|
|
26
|
+
};
|
|
27
|
+
const PERCENT = 100;
|
|
28
|
+
const MILLION = 1e6;
|
|
29
|
+
const THOUSAND = 1e3;
|
|
30
|
+
const COMPACT_DECIMALS = 1;
|
|
31
|
+
const HEALTHY_FINDING = {
|
|
32
|
+
id: "healthy",
|
|
33
|
+
severity: "info",
|
|
34
|
+
subject: "session",
|
|
35
|
+
detail: "no failures, no outliers",
|
|
36
|
+
action: "nothing to act on yet"
|
|
37
|
+
};
|
|
38
|
+
function compact(value) {
|
|
39
|
+
if (value >= MILLION) return `${(value / MILLION).toFixed(COMPACT_DECIMALS)}M`;
|
|
40
|
+
if (value >= THOUSAND) return `${Math.round(value / THOUSAND)}k`;
|
|
41
|
+
return String(Math.round(value));
|
|
42
|
+
}
|
|
43
|
+
function percent(value) {
|
|
44
|
+
return `${Math.round(value * PERCENT)}%`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Median rather than mean: one pathological tool would drag a mean up far
|
|
48
|
+
* enough to hide itself from the comparison that is meant to catch it.
|
|
49
|
+
*/
|
|
50
|
+
function medianTokens(tools) {
|
|
51
|
+
const sampled = tools.map((tool) => tool.p90Tokens).filter((tokens) => tokens !== void 0).sort((left, right) => left - right);
|
|
52
|
+
if (sampled.length === 0) return 0;
|
|
53
|
+
const middle = Math.floor(sampled.length / 2);
|
|
54
|
+
const lower = sampled[middle - 1] ?? 0;
|
|
55
|
+
const upper = sampled[middle] ?? 0;
|
|
56
|
+
return sampled.length % 2 === 0 ? (lower + upper) / 2 : upper;
|
|
57
|
+
}
|
|
58
|
+
function cacheFinding(snapshot) {
|
|
59
|
+
const { cacheRead, cacheWrite, input } = snapshot.tokens;
|
|
60
|
+
const cacheable = cacheRead + input;
|
|
61
|
+
if (cacheable < MIN_CACHE_SAMPLE_TOKENS) return void 0;
|
|
62
|
+
const hitRatio = cacheRead / cacheable;
|
|
63
|
+
if (hitRatio >= LOW_CACHE_HIT_RATIO) return void 0;
|
|
64
|
+
return {
|
|
65
|
+
id: "low-cache-hit",
|
|
66
|
+
severity: "warning",
|
|
67
|
+
subject: "cache",
|
|
68
|
+
detail: `${percent(hitRatio)} hit · ${compact(cacheWrite)} written`,
|
|
69
|
+
action: "pin stable context first"
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function expensiveToolFinding(snapshot) {
|
|
73
|
+
const median = medianTokens(snapshot.toolCost);
|
|
74
|
+
if (median <= 0) return void 0;
|
|
75
|
+
const worst = snapshot.toolCost.find((tool) => tool.samples >= MIN_TOOL_SAMPLES && (tool.p90Tokens ?? 0) >= median * EXPENSIVE_TOOL_RATIO);
|
|
76
|
+
if (!worst?.p90Tokens) return void 0;
|
|
77
|
+
return {
|
|
78
|
+
id: "expensive-tool",
|
|
79
|
+
severity: "warning",
|
|
80
|
+
subject: worst.name,
|
|
81
|
+
detail: `${compact(worst.p90Tokens)} p90 · ${Math.round(worst.p90Tokens / median)}× median`,
|
|
82
|
+
action: "narrow what it pulls in"
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function failingToolFindings(snapshot) {
|
|
86
|
+
return snapshot.toolCost.filter((tool) => tool.calls >= MIN_TOOL_CALLS && tool.failed / tool.calls >= FAILING_TOOL_RATIO).sort((left, right) => right.failed - left.failed).map((tool) => ({
|
|
87
|
+
id: `failing-tool:${tool.name}`,
|
|
88
|
+
severity: "critical",
|
|
89
|
+
subject: tool.name,
|
|
90
|
+
detail: `${tool.failed} of ${tool.calls} failed`,
|
|
91
|
+
action: "retries burn tokens"
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
function abortedTurnFinding(snapshot) {
|
|
95
|
+
if (snapshot.abortedTurns + snapshot.failedTurns === 0) return void 0;
|
|
96
|
+
return {
|
|
97
|
+
id: "redone-turns",
|
|
98
|
+
severity: "critical",
|
|
99
|
+
subject: "turns",
|
|
100
|
+
detail: `${snapshot.abortedTurns} aborted · ${snapshot.failedTurns} failed`,
|
|
101
|
+
action: "paid for, then thrown away"
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function providerFinding(snapshot) {
|
|
105
|
+
const category = snapshot.failureCategories.find((entry) => entry.name in SHORT_CATEGORY);
|
|
106
|
+
if (!category) return void 0;
|
|
107
|
+
return {
|
|
108
|
+
id: `provider:${category.name}`,
|
|
109
|
+
severity: "warning",
|
|
110
|
+
subject: "provider",
|
|
111
|
+
detail: `${category.count} × ${SHORT_CATEGORY[category.name] ?? category.name}`,
|
|
112
|
+
action: category.name === RATE_LIMITED ? "back off or drop concurrency" : "retry or switch provider"
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function contextFinding(snapshot) {
|
|
116
|
+
if (snapshot.turns < MIN_TURNS_FOR_AVERAGE) return void 0;
|
|
117
|
+
const perTurn = snapshot.tokens.input / snapshot.turns;
|
|
118
|
+
if (perTurn < HEAVY_TURN_INPUT_TOKENS) return void 0;
|
|
119
|
+
return {
|
|
120
|
+
id: "heavy-context",
|
|
121
|
+
severity: "warning",
|
|
122
|
+
subject: "context",
|
|
123
|
+
detail: `${compact(perTurn)} input tok/turn`,
|
|
124
|
+
action: "compact or split the task"
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function toolChurnFinding(snapshot) {
|
|
128
|
+
if (snapshot.turns < MIN_TURNS_FOR_AVERAGE) return void 0;
|
|
129
|
+
const perTurn = snapshot.toolCalls / snapshot.turns;
|
|
130
|
+
if (perTurn < BUSY_TURN_TOOL_CALLS) return void 0;
|
|
131
|
+
return {
|
|
132
|
+
id: "tool-churn",
|
|
133
|
+
severity: "info",
|
|
134
|
+
subject: "tools",
|
|
135
|
+
detail: `${perTurn.toFixed(1)} calls/turn`,
|
|
136
|
+
action: "give it a narrower target"
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Ranked most-actionable first. Severity leads because a failing tool costs
|
|
141
|
+
* more than an inefficient one, and the panel is read top-down under a
|
|
142
|
+
* height limit.
|
|
143
|
+
*/
|
|
144
|
+
function deriveFindings(snapshot) {
|
|
145
|
+
const findings = [
|
|
146
|
+
...failingToolFindings(snapshot),
|
|
147
|
+
abortedTurnFinding(snapshot),
|
|
148
|
+
providerFinding(snapshot),
|
|
149
|
+
cacheFinding(snapshot),
|
|
150
|
+
expensiveToolFinding(snapshot),
|
|
151
|
+
contextFinding(snapshot),
|
|
152
|
+
toolChurnFinding(snapshot)
|
|
153
|
+
].filter((finding) => finding !== void 0);
|
|
154
|
+
if (findings.length === 0) return snapshot.events > 0 ? [HEALTHY_FINDING] : [];
|
|
155
|
+
return findings.sort((left, right) => SEVERITY_RANK[left.severity] - SEVERITY_RANK[right.severity]);
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
Object.defineProperty(exports, "deriveFindings", {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
get: function() {
|
|
161
|
+
return deriveFindings;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
//# sourceMappingURL=findings-Bo4IHNpr.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findings-Bo4IHNpr.cjs","names":[],"sources":["../src/services/findings/index.ts"],"sourcesContent":["/**\n * Ranked, actionable findings derived from a session snapshot.\n *\n * Every finding restates numbers the aggregator already holds and pairs them\n * with the one change that moves them, because a counter the user cannot act on\n * is screen space spent for nothing. Nothing here queries the sink: the panel\n * has to stay useful when no sink is connected, which is exactly the situation\n * where a user has no other way to see any of this.\n *\n * Thresholds are deliberately conservative. A finding that fires on a healthy\n * session teaches the user to ignore the panel, which is worse than an empty\n * one.\n */\n\nimport type { LogMetricsSnapshot, LogMetricsToolCost } from '../metrics';\n\nexport type LogMetricsFindingSeverity = 'critical' | 'warning' | 'info';\n\nexport interface LogMetricsFinding {\n /** Stable identifier, so a finding can be asserted without matching prose. */\n id: string;\n severity: LogMetricsFindingSeverity;\n /** What the finding is about: a tool name, or a facet like `cache`. */\n subject: string;\n /** The evidence, already formatted. */\n detail: string;\n /** The change that moves the number. */\n action: string;\n}\n\n/** Below this, the prompt prefix is churning and cache writes are being wasted. */\nconst LOW_CACHE_HIT_RATIO = 0.3;\n/** Cache hit rate is meaningless on a session that has barely sent anything. */\nconst MIN_CACHE_SAMPLE_TOKENS = 50_000;\n/** A tool is \"expensive\" only well clear of the session's typical turn. */\nconst EXPENSIVE_TOOL_RATIO = 1.5;\nconst MIN_TOOL_SAMPLES = 3;\nconst FAILING_TOOL_RATIO = 0.25;\nconst MIN_TOOL_CALLS = 3;\n/** Sustained input this large per turn means context is carried, not scoped. */\nconst HEAVY_TURN_INPUT_TOKENS = 120_000;\nconst MIN_TURNS_FOR_AVERAGE = 3;\n/** Tool calls per turn above this reads as search thrash rather than progress. */\nconst BUSY_TURN_TOOL_CALLS = 8;\n\nconst RATE_LIMITED = 'rate_limited';\n/** Kept short because the finding's subject column already says \"provider\". */\nconst SHORT_CATEGORY: Record<string, string> = {\n [RATE_LIMITED]: 'rate limited',\n provider_unavailable: 'unavailable',\n};\n\nconst SEVERITY_RANK: Record<LogMetricsFindingSeverity, number> = { critical: 0, warning: 1, info: 2 };\nconst PERCENT = 100;\nconst MILLION = 1_000_000;\nconst THOUSAND = 1000;\nconst COMPACT_DECIMALS = 1;\nconst HEALTHY_FINDING: LogMetricsFinding = {\n id: 'healthy',\n severity: 'info',\n subject: 'session',\n detail: 'no failures, no outliers',\n action: 'nothing to act on yet',\n};\n\nfunction compact(value: number): string {\n if (value >= MILLION) return `${(value / MILLION).toFixed(COMPACT_DECIMALS)}M`;\n if (value >= THOUSAND) return `${Math.round(value / THOUSAND)}k`;\n return String(Math.round(value));\n}\n\nfunction percent(value: number): string {\n return `${Math.round(value * PERCENT)}%`;\n}\n\n/**\n * Median rather than mean: one pathological tool would drag a mean up far\n * enough to hide itself from the comparison that is meant to catch it.\n */\nfunction medianTokens(tools: readonly LogMetricsToolCost[]): number {\n const sampled = tools\n .map((tool) => tool.p90Tokens)\n .filter((tokens): tokens is number => tokens !== undefined)\n .sort((left, right) => left - right);\n if (sampled.length === 0) return 0;\n const middle = Math.floor(sampled.length / 2);\n const lower = sampled[middle - 1] ?? 0;\n const upper = sampled[middle] ?? 0;\n return sampled.length % 2 === 0 ? (lower + upper) / 2 : upper;\n}\n\nfunction cacheFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n const { cacheRead, cacheWrite, input } = snapshot.tokens;\n const cacheable = cacheRead + input;\n if (cacheable < MIN_CACHE_SAMPLE_TOKENS) return undefined;\n\n const hitRatio = cacheRead / cacheable;\n if (hitRatio >= LOW_CACHE_HIT_RATIO) return undefined;\n return {\n id: 'low-cache-hit',\n severity: 'warning',\n subject: 'cache',\n detail: `${percent(hitRatio)} hit · ${compact(cacheWrite)} written`,\n action: 'pin stable context first',\n };\n}\n\nfunction expensiveToolFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n const median = medianTokens(snapshot.toolCost);\n if (median <= 0) return undefined;\n\n const worst = snapshot.toolCost.find(\n (tool) => tool.samples >= MIN_TOOL_SAMPLES && (tool.p90Tokens ?? 0) >= median * EXPENSIVE_TOOL_RATIO,\n );\n if (!worst?.p90Tokens) return undefined;\n return {\n id: 'expensive-tool',\n severity: 'warning',\n subject: worst.name,\n detail: `${compact(worst.p90Tokens)} p90 · ${Math.round(worst.p90Tokens / median)}× median`,\n action: 'narrow what it pulls in',\n };\n}\n\nfunction failingToolFindings(snapshot: LogMetricsSnapshot): LogMetricsFinding[] {\n return snapshot.toolCost\n .filter((tool) => tool.calls >= MIN_TOOL_CALLS && tool.failed / tool.calls >= FAILING_TOOL_RATIO)\n .sort((left, right) => right.failed - left.failed)\n .map((tool) => ({\n id: `failing-tool:${tool.name}`,\n severity: 'critical' as const,\n subject: tool.name,\n detail: `${tool.failed} of ${tool.calls} failed`,\n action: 'retries burn tokens',\n }));\n}\n\nfunction abortedTurnFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n const redone = snapshot.abortedTurns + snapshot.failedTurns;\n if (redone === 0) return undefined;\n return {\n id: 'redone-turns',\n severity: 'critical',\n subject: 'turns',\n detail: `${snapshot.abortedTurns} aborted · ${snapshot.failedTurns} failed`,\n action: 'paid for, then thrown away',\n };\n}\n\nfunction providerFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n const category = snapshot.failureCategories.find((entry) => entry.name in SHORT_CATEGORY);\n if (!category) return undefined;\n return {\n id: `provider:${category.name}`,\n severity: 'warning',\n subject: 'provider',\n detail: `${category.count} × ${SHORT_CATEGORY[category.name] ?? category.name}`,\n action: category.name === RATE_LIMITED ? 'back off or drop concurrency' : 'retry or switch provider',\n };\n}\n\nfunction contextFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n if (snapshot.turns < MIN_TURNS_FOR_AVERAGE) return undefined;\n\n const perTurn = snapshot.tokens.input / snapshot.turns;\n if (perTurn < HEAVY_TURN_INPUT_TOKENS) return undefined;\n return {\n id: 'heavy-context',\n severity: 'warning',\n subject: 'context',\n detail: `${compact(perTurn)} input tok/turn`,\n action: 'compact or split the task',\n };\n}\n\nfunction toolChurnFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n if (snapshot.turns < MIN_TURNS_FOR_AVERAGE) return undefined;\n\n const perTurn = snapshot.toolCalls / snapshot.turns;\n if (perTurn < BUSY_TURN_TOOL_CALLS) return undefined;\n return {\n id: 'tool-churn',\n severity: 'info',\n subject: 'tools',\n detail: `${perTurn.toFixed(1)} calls/turn`,\n action: 'give it a narrower target',\n };\n}\n\n/**\n * Ranked most-actionable first. Severity leads because a failing tool costs\n * more than an inefficient one, and the panel is read top-down under a\n * height limit.\n */\nexport function deriveFindings(snapshot: LogMetricsSnapshot): LogMetricsFinding[] {\n const findings = [\n ...failingToolFindings(snapshot),\n abortedTurnFinding(snapshot),\n providerFinding(snapshot),\n cacheFinding(snapshot),\n expensiveToolFinding(snapshot),\n contextFinding(snapshot),\n toolChurnFinding(snapshot),\n ].filter((finding): finding is LogMetricsFinding => finding !== undefined);\n\n if (findings.length === 0) return snapshot.events > 0 ? [HEALTHY_FINDING] : [];\n return findings.sort((left, right) => SEVERITY_RANK[left.severity] - SEVERITY_RANK[right.severity]);\n}\n"],"mappings":";;AA+BA,MAAM,sBAAsB;;AAE5B,MAAM,0BAA0B;;AAEhC,MAAM,uBAAuB;AAC7B,MAAM,mBAAmB;AACzB,MAAM,qBAAqB;AAC3B,MAAM,iBAAiB;;AAEvB,MAAM,0BAA0B;AAChC,MAAM,wBAAwB;;AAE9B,MAAM,uBAAuB;AAE7B,MAAM,eAAe;;AAErB,MAAM,iBAAyC;EAC5C,eAAe;CAChB,sBAAsB;AACxB;AAEA,MAAM,gBAA2D;CAAE,UAAU;CAAG,SAAS;CAAG,MAAM;AAAE;AACpG,MAAM,UAAU;AAChB,MAAM,UAAU;AAChB,MAAM,WAAW;AACjB,MAAM,mBAAmB;A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,yBAAyB,OAAO,KAAA;CAC9C,OAAO;EACL,IAAI;EACJ,UAAU;EACV,SAAS;EACT,QAAQ,GAAG,QAAQ,OAAO,EAAE;EAC5B,QAAQ;CACV;AACF;AAEA,SAAS,iBAAiB,UAA6D;CACrF,IAAI,SAAS,QAAQ,uBAAuB,OAAO,KAAA;CAEnD,MAAM,UAAU,SAAS,YAAY,SAAS;CAC9C,IAAI,UAAU,sBAAsB,OAAO,KAAA;CAC3C,OAAO;EACL,IAAI;EACJ,UAAU;EACV,SAAS;EACT,QAAQ,GAAG,QAAQ,QAAQ,CAAC,EAAE;EAC9B,QAAQ;CACV;AACF;;;;;;AAOA,SAAgB,eAAe,UAAmD;CAChF,MAAM,WAAW;EACf,GAAG,oBAAoB,QAAQ;EAC/B,mBAAmB,QAAQ;EAC3B,gBAAgB,QAAQ;EACxB,aAAa,QAAQ;EACrB,qBAAqB,QAAQ;EAC7B,eAAe,QAAQ;EACvB,iBAAiB,QAAQ;CAC3B,CAAC,CAAC,QAAQ,YAA0C,YAAY,KAAA,CAAS;CAEzE,IAAI,SAAS,WAAW,GAAG,OAAO,SAAS,SAAS,IAAI,CAAC,eAAe,IAAI,CAAC;CAC7E,OAAO,SAAS,MAAM,MAAM,UAAU,cAAc,KAAK,YAAY,cAAc,MAAM,SAAS;AACpG"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
//#region src/services/findings/index.ts
|
|
2
|
+
/** Below this, the prompt prefix is churning and cache writes are being wasted. */
|
|
3
|
+
const LOW_CACHE_HIT_RATIO = .3;
|
|
4
|
+
/** Cache hit rate is meaningless on a session that has barely sent anything. */
|
|
5
|
+
const MIN_CACHE_SAMPLE_TOKENS = 5e4;
|
|
6
|
+
/** A tool is "expensive" only well clear of the session's typical turn. */
|
|
7
|
+
const EXPENSIVE_TOOL_RATIO = 1.5;
|
|
8
|
+
const MIN_TOOL_SAMPLES = 3;
|
|
9
|
+
const FAILING_TOOL_RATIO = .25;
|
|
10
|
+
const MIN_TOOL_CALLS = 3;
|
|
11
|
+
/** Sustained input this large per turn means context is carried, not scoped. */
|
|
12
|
+
const HEAVY_TURN_INPUT_TOKENS = 12e4;
|
|
13
|
+
const MIN_TURNS_FOR_AVERAGE = 3;
|
|
14
|
+
/** Tool calls per turn above this reads as search thrash rather than progress. */
|
|
15
|
+
const BUSY_TURN_TOOL_CALLS = 8;
|
|
16
|
+
const RATE_LIMITED = "rate_limited";
|
|
17
|
+
/** Kept short because the finding's subject column already says "provider". */
|
|
18
|
+
const SHORT_CATEGORY = {
|
|
19
|
+
[RATE_LIMITED]: "rate limited",
|
|
20
|
+
provider_unavailable: "unavailable"
|
|
21
|
+
};
|
|
22
|
+
const SEVERITY_RANK = {
|
|
23
|
+
critical: 0,
|
|
24
|
+
warning: 1,
|
|
25
|
+
info: 2
|
|
26
|
+
};
|
|
27
|
+
const PERCENT = 100;
|
|
28
|
+
const MILLION = 1e6;
|
|
29
|
+
const THOUSAND = 1e3;
|
|
30
|
+
const COMPACT_DECIMALS = 1;
|
|
31
|
+
const HEALTHY_FINDING = {
|
|
32
|
+
id: "healthy",
|
|
33
|
+
severity: "info",
|
|
34
|
+
subject: "session",
|
|
35
|
+
detail: "no failures, no outliers",
|
|
36
|
+
action: "nothing to act on yet"
|
|
37
|
+
};
|
|
38
|
+
function compact(value) {
|
|
39
|
+
if (value >= MILLION) return `${(value / MILLION).toFixed(COMPACT_DECIMALS)}M`;
|
|
40
|
+
if (value >= THOUSAND) return `${Math.round(value / THOUSAND)}k`;
|
|
41
|
+
return String(Math.round(value));
|
|
42
|
+
}
|
|
43
|
+
function percent(value) {
|
|
44
|
+
return `${Math.round(value * PERCENT)}%`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Median rather than mean: one pathological tool would drag a mean up far
|
|
48
|
+
* enough to hide itself from the comparison that is meant to catch it.
|
|
49
|
+
*/
|
|
50
|
+
function medianTokens(tools) {
|
|
51
|
+
const sampled = tools.map((tool) => tool.p90Tokens).filter((tokens) => tokens !== void 0).sort((left, right) => left - right);
|
|
52
|
+
if (sampled.length === 0) return 0;
|
|
53
|
+
const middle = Math.floor(sampled.length / 2);
|
|
54
|
+
const lower = sampled[middle - 1] ?? 0;
|
|
55
|
+
const upper = sampled[middle] ?? 0;
|
|
56
|
+
return sampled.length % 2 === 0 ? (lower + upper) / 2 : upper;
|
|
57
|
+
}
|
|
58
|
+
function cacheFinding(snapshot) {
|
|
59
|
+
const { cacheRead, cacheWrite, input } = snapshot.tokens;
|
|
60
|
+
const cacheable = cacheRead + input;
|
|
61
|
+
if (cacheable < MIN_CACHE_SAMPLE_TOKENS) return void 0;
|
|
62
|
+
const hitRatio = cacheRead / cacheable;
|
|
63
|
+
if (hitRatio >= LOW_CACHE_HIT_RATIO) return void 0;
|
|
64
|
+
return {
|
|
65
|
+
id: "low-cache-hit",
|
|
66
|
+
severity: "warning",
|
|
67
|
+
subject: "cache",
|
|
68
|
+
detail: `${percent(hitRatio)} hit · ${compact(cacheWrite)} written`,
|
|
69
|
+
action: "pin stable context first"
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function expensiveToolFinding(snapshot) {
|
|
73
|
+
const median = medianTokens(snapshot.toolCost);
|
|
74
|
+
if (median <= 0) return void 0;
|
|
75
|
+
const worst = snapshot.toolCost.find((tool) => tool.samples >= MIN_TOOL_SAMPLES && (tool.p90Tokens ?? 0) >= median * EXPENSIVE_TOOL_RATIO);
|
|
76
|
+
if (!worst?.p90Tokens) return void 0;
|
|
77
|
+
return {
|
|
78
|
+
id: "expensive-tool",
|
|
79
|
+
severity: "warning",
|
|
80
|
+
subject: worst.name,
|
|
81
|
+
detail: `${compact(worst.p90Tokens)} p90 · ${Math.round(worst.p90Tokens / median)}× median`,
|
|
82
|
+
action: "narrow what it pulls in"
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function failingToolFindings(snapshot) {
|
|
86
|
+
return snapshot.toolCost.filter((tool) => tool.calls >= MIN_TOOL_CALLS && tool.failed / tool.calls >= FAILING_TOOL_RATIO).sort((left, right) => right.failed - left.failed).map((tool) => ({
|
|
87
|
+
id: `failing-tool:${tool.name}`,
|
|
88
|
+
severity: "critical",
|
|
89
|
+
subject: tool.name,
|
|
90
|
+
detail: `${tool.failed} of ${tool.calls} failed`,
|
|
91
|
+
action: "retries burn tokens"
|
|
92
|
+
}));
|
|
93
|
+
}
|
|
94
|
+
function abortedTurnFinding(snapshot) {
|
|
95
|
+
if (snapshot.abortedTurns + snapshot.failedTurns === 0) return void 0;
|
|
96
|
+
return {
|
|
97
|
+
id: "redone-turns",
|
|
98
|
+
severity: "critical",
|
|
99
|
+
subject: "turns",
|
|
100
|
+
detail: `${snapshot.abortedTurns} aborted · ${snapshot.failedTurns} failed`,
|
|
101
|
+
action: "paid for, then thrown away"
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function providerFinding(snapshot) {
|
|
105
|
+
const category = snapshot.failureCategories.find((entry) => entry.name in SHORT_CATEGORY);
|
|
106
|
+
if (!category) return void 0;
|
|
107
|
+
return {
|
|
108
|
+
id: `provider:${category.name}`,
|
|
109
|
+
severity: "warning",
|
|
110
|
+
subject: "provider",
|
|
111
|
+
detail: `${category.count} × ${SHORT_CATEGORY[category.name] ?? category.name}`,
|
|
112
|
+
action: category.name === RATE_LIMITED ? "back off or drop concurrency" : "retry or switch provider"
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function contextFinding(snapshot) {
|
|
116
|
+
if (snapshot.turns < MIN_TURNS_FOR_AVERAGE) return void 0;
|
|
117
|
+
const perTurn = snapshot.tokens.input / snapshot.turns;
|
|
118
|
+
if (perTurn < HEAVY_TURN_INPUT_TOKENS) return void 0;
|
|
119
|
+
return {
|
|
120
|
+
id: "heavy-context",
|
|
121
|
+
severity: "warning",
|
|
122
|
+
subject: "context",
|
|
123
|
+
detail: `${compact(perTurn)} input tok/turn`,
|
|
124
|
+
action: "compact or split the task"
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function toolChurnFinding(snapshot) {
|
|
128
|
+
if (snapshot.turns < MIN_TURNS_FOR_AVERAGE) return void 0;
|
|
129
|
+
const perTurn = snapshot.toolCalls / snapshot.turns;
|
|
130
|
+
if (perTurn < BUSY_TURN_TOOL_CALLS) return void 0;
|
|
131
|
+
return {
|
|
132
|
+
id: "tool-churn",
|
|
133
|
+
severity: "info",
|
|
134
|
+
subject: "tools",
|
|
135
|
+
detail: `${perTurn.toFixed(1)} calls/turn`,
|
|
136
|
+
action: "give it a narrower target"
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Ranked most-actionable first. Severity leads because a failing tool costs
|
|
141
|
+
* more than an inefficient one, and the panel is read top-down under a
|
|
142
|
+
* height limit.
|
|
143
|
+
*/
|
|
144
|
+
function deriveFindings(snapshot) {
|
|
145
|
+
const findings = [
|
|
146
|
+
...failingToolFindings(snapshot),
|
|
147
|
+
abortedTurnFinding(snapshot),
|
|
148
|
+
providerFinding(snapshot),
|
|
149
|
+
cacheFinding(snapshot),
|
|
150
|
+
expensiveToolFinding(snapshot),
|
|
151
|
+
contextFinding(snapshot),
|
|
152
|
+
toolChurnFinding(snapshot)
|
|
153
|
+
].filter((finding) => finding !== void 0);
|
|
154
|
+
if (findings.length === 0) return snapshot.events > 0 ? [HEALTHY_FINDING] : [];
|
|
155
|
+
return findings.sort((left, right) => SEVERITY_RANK[left.severity] - SEVERITY_RANK[right.severity]);
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
export { deriveFindings as t };
|
|
159
|
+
|
|
160
|
+
//# sourceMappingURL=findings-DAKeRG4O.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findings-DAKeRG4O.mjs","names":[],"sources":["../src/services/findings/index.ts"],"sourcesContent":["/**\n * Ranked, actionable findings derived from a session snapshot.\n *\n * Every finding restates numbers the aggregator already holds and pairs them\n * with the one change that moves them, because a counter the user cannot act on\n * is screen space spent for nothing. Nothing here queries the sink: the panel\n * has to stay useful when no sink is connected, which is exactly the situation\n * where a user has no other way to see any of this.\n *\n * Thresholds are deliberately conservative. A finding that fires on a healthy\n * session teaches the user to ignore the panel, which is worse than an empty\n * one.\n */\n\nimport type { LogMetricsSnapshot, LogMetricsToolCost } from '../metrics';\n\nexport type LogMetricsFindingSeverity = 'critical' | 'warning' | 'info';\n\nexport interface LogMetricsFinding {\n /** Stable identifier, so a finding can be asserted without matching prose. */\n id: string;\n severity: LogMetricsFindingSeverity;\n /** What the finding is about: a tool name, or a facet like `cache`. */\n subject: string;\n /** The evidence, already formatted. */\n detail: string;\n /** The change that moves the number. */\n action: string;\n}\n\n/** Below this, the prompt prefix is churning and cache writes are being wasted. */\nconst LOW_CACHE_HIT_RATIO = 0.3;\n/** Cache hit rate is meaningless on a session that has barely sent anything. */\nconst MIN_CACHE_SAMPLE_TOKENS = 50_000;\n/** A tool is \"expensive\" only well clear of the session's typical turn. */\nconst EXPENSIVE_TOOL_RATIO = 1.5;\nconst MIN_TOOL_SAMPLES = 3;\nconst FAILING_TOOL_RATIO = 0.25;\nconst MIN_TOOL_CALLS = 3;\n/** Sustained input this large per turn means context is carried, not scoped. */\nconst HEAVY_TURN_INPUT_TOKENS = 120_000;\nconst MIN_TURNS_FOR_AVERAGE = 3;\n/** Tool calls per turn above this reads as search thrash rather than progress. */\nconst BUSY_TURN_TOOL_CALLS = 8;\n\nconst RATE_LIMITED = 'rate_limited';\n/** Kept short because the finding's subject column already says \"provider\". */\nconst SHORT_CATEGORY: Record<string, string> = {\n [RATE_LIMITED]: 'rate limited',\n provider_unavailable: 'unavailable',\n};\n\nconst SEVERITY_RANK: Record<LogMetricsFindingSeverity, number> = { critical: 0, warning: 1, info: 2 };\nconst PERCENT = 100;\nconst MILLION = 1_000_000;\nconst THOUSAND = 1000;\nconst COMPACT_DECIMALS = 1;\nconst HEALTHY_FINDING: LogMetricsFinding = {\n id: 'healthy',\n severity: 'info',\n subject: 'session',\n detail: 'no failures, no outliers',\n action: 'nothing to act on yet',\n};\n\nfunction compact(value: number): string {\n if (value >= MILLION) return `${(value / MILLION).toFixed(COMPACT_DECIMALS)}M`;\n if (value >= THOUSAND) return `${Math.round(value / THOUSAND)}k`;\n return String(Math.round(value));\n}\n\nfunction percent(value: number): string {\n return `${Math.round(value * PERCENT)}%`;\n}\n\n/**\n * Median rather than mean: one pathological tool would drag a mean up far\n * enough to hide itself from the comparison that is meant to catch it.\n */\nfunction medianTokens(tools: readonly LogMetricsToolCost[]): number {\n const sampled = tools\n .map((tool) => tool.p90Tokens)\n .filter((tokens): tokens is number => tokens !== undefined)\n .sort((left, right) => left - right);\n if (sampled.length === 0) return 0;\n const middle = Math.floor(sampled.length / 2);\n const lower = sampled[middle - 1] ?? 0;\n const upper = sampled[middle] ?? 0;\n return sampled.length % 2 === 0 ? (lower + upper) / 2 : upper;\n}\n\nfunction cacheFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n const { cacheRead, cacheWrite, input } = snapshot.tokens;\n const cacheable = cacheRead + input;\n if (cacheable < MIN_CACHE_SAMPLE_TOKENS) return undefined;\n\n const hitRatio = cacheRead / cacheable;\n if (hitRatio >= LOW_CACHE_HIT_RATIO) return undefined;\n return {\n id: 'low-cache-hit',\n severity: 'warning',\n subject: 'cache',\n detail: `${percent(hitRatio)} hit · ${compact(cacheWrite)} written`,\n action: 'pin stable context first',\n };\n}\n\nfunction expensiveToolFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n const median = medianTokens(snapshot.toolCost);\n if (median <= 0) return undefined;\n\n const worst = snapshot.toolCost.find(\n (tool) => tool.samples >= MIN_TOOL_SAMPLES && (tool.p90Tokens ?? 0) >= median * EXPENSIVE_TOOL_RATIO,\n );\n if (!worst?.p90Tokens) return undefined;\n return {\n id: 'expensive-tool',\n severity: 'warning',\n subject: worst.name,\n detail: `${compact(worst.p90Tokens)} p90 · ${Math.round(worst.p90Tokens / median)}× median`,\n action: 'narrow what it pulls in',\n };\n}\n\nfunction failingToolFindings(snapshot: LogMetricsSnapshot): LogMetricsFinding[] {\n return snapshot.toolCost\n .filter((tool) => tool.calls >= MIN_TOOL_CALLS && tool.failed / tool.calls >= FAILING_TOOL_RATIO)\n .sort((left, right) => right.failed - left.failed)\n .map((tool) => ({\n id: `failing-tool:${tool.name}`,\n severity: 'critical' as const,\n subject: tool.name,\n detail: `${tool.failed} of ${tool.calls} failed`,\n action: 'retries burn tokens',\n }));\n}\n\nfunction abortedTurnFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n const redone = snapshot.abortedTurns + snapshot.failedTurns;\n if (redone === 0) return undefined;\n return {\n id: 'redone-turns',\n severity: 'critical',\n subject: 'turns',\n detail: `${snapshot.abortedTurns} aborted · ${snapshot.failedTurns} failed`,\n action: 'paid for, then thrown away',\n };\n}\n\nfunction providerFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n const category = snapshot.failureCategories.find((entry) => entry.name in SHORT_CATEGORY);\n if (!category) return undefined;\n return {\n id: `provider:${category.name}`,\n severity: 'warning',\n subject: 'provider',\n detail: `${category.count} × ${SHORT_CATEGORY[category.name] ?? category.name}`,\n action: category.name === RATE_LIMITED ? 'back off or drop concurrency' : 'retry or switch provider',\n };\n}\n\nfunction contextFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n if (snapshot.turns < MIN_TURNS_FOR_AVERAGE) return undefined;\n\n const perTurn = snapshot.tokens.input / snapshot.turns;\n if (perTurn < HEAVY_TURN_INPUT_TOKENS) return undefined;\n return {\n id: 'heavy-context',\n severity: 'warning',\n subject: 'context',\n detail: `${compact(perTurn)} input tok/turn`,\n action: 'compact or split the task',\n };\n}\n\nfunction toolChurnFinding(snapshot: LogMetricsSnapshot): LogMetricsFinding | undefined {\n if (snapshot.turns < MIN_TURNS_FOR_AVERAGE) return undefined;\n\n const perTurn = snapshot.toolCalls / snapshot.turns;\n if (perTurn < BUSY_TURN_TOOL_CALLS) return undefined;\n return {\n id: 'tool-churn',\n severity: 'info',\n subject: 'tools',\n detail: `${perTurn.toFixed(1)} calls/turn`,\n action: 'give it a narrower target',\n };\n}\n\n/**\n * Ranked most-actionable first. Severity leads because a failing tool costs\n * more than an inefficient one, and the panel is read top-down under a\n * height limit.\n */\nexport function deriveFindings(snapshot: LogMetricsSnapshot): LogMetricsFinding[] {\n const findings = [\n ...failingToolFindings(snapshot),\n abortedTurnFinding(snapshot),\n providerFinding(snapshot),\n cacheFinding(snapshot),\n expensiveToolFinding(snapshot),\n contextFinding(snapshot),\n toolChurnFinding(snapshot),\n ].filter((finding): finding is LogMetricsFinding => finding !== undefined);\n\n if (findings.length === 0) return snapshot.events > 0 ? [HEALTHY_FINDING] : [];\n return findings.sort((left, right) => SEVERITY_RANK[left.severity] - SEVERITY_RANK[right.severity]);\n}\n"],"mappings":";;AA+BA,MAAM,sBAAsB;;AAE5B,MAAM,0BAA0B;;AAEhC,MAAM,uBAAuB;AAC7B,MAAM,mBAAmB;AACzB,MAAM,qBAAqB;AAC3B,MAAM,iBAAiB;;AAEvB,MAAM,0BAA0B;AAChC,MAAM,wBAAwB;;AAE9B,MAAM,uBAAuB;AAE7B,MAAM,eAAe;;AAErB,MAAM,iBAAyC;EAC5C,eAAe;CAChB,sBAAsB;AACxB;AAEA,MAAM,gBAA2D;CAAE,UAAU;CAAG,SAAS;CAAG,MAAM;AAAE;AACpG,MAAM,UAAU;AAChB,MAAM,UAAU;AAChB,MAAM,WAAW;AACjB,MAAM,mBAAmB;AACzB,MAAM,kBAAqC;CACzC,IAAI;CACJ,UAAU;CACV,SAAS;CACT,QAAQ;CACR,QAAQ;AACV;AAEA,SAAS,QAAQ,OAAuB;CACtC,IAAI,SAAS,SAAS,OAAO,IAAI,QAAQ,QAAA,CAAS,QAAQ,gBAAgB,EAAE;CAC5E,IAAI,SAAS,UAAU,OAAO,GAAG,KAAK,MAAM,QAAQ,QAAQ,EAAE;CAC9D,OAAO,OAAO,KAAK,MAAM,KAAK,CAAC;A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AI;EACJ,UAAU;EACV,SAAS;EACT,QAAQ,GAAG,QAAQ,QAAQ,CAAC,EAAE;EAC9B,QAAQ;CACV;AACF;;;;;;AAOA,SAAgB,eAAe,UAAmD;CAChF,MAAM,WAAW;EACf,GAAG,oBAAoB,QAAQ;EAC/B,mBAAmB,QAAQ;EAC3B,gBAAgB,QAAQ;EACxB,aAAa,QAAQ;EACrB,qBAAqB,QAAQ;EAC7B,eAAe,QAAQ;EACvB,iBAAiB,QAAQ;CAC3B,CAAC,CAAC,QAAQ,YAA0C,YAAY,KAAA,CAAS;CAEzE,IAAI,SAAS,WAAW,GAAG,OAAO,SAAS,SAAS,IAAI,CAAC,eAAe,IAAI,CAAC;CAC7E,OAAO,SAAS,MAAM,MAAM,UAAU,cAAc,KAAK,YAAY,cAAc,MAAM,SAAS;AACpG"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const TOKEN_SORT = "total-tokens";
|
|
2
|
+
/** HTTP is the common transport for local and remote collectors. */
|
|
3
|
+
async function queryHttp(endpoint, params, fetchImpl) {
|
|
4
|
+
const search = new URLSearchParams({
|
|
5
|
+
groupBy: params.groupBy,
|
|
6
|
+
period: params.period,
|
|
7
|
+
sort: TOKEN_SORT,
|
|
8
|
+
limit: String(params.limit),
|
|
9
|
+
toolLimit: String(params.toolLimit ?? 1)
|
|
10
|
+
});
|
|
11
|
+
for (const key of [
|
|
12
|
+
"sessionId",
|
|
13
|
+
"agentName",
|
|
14
|
+
"model",
|
|
15
|
+
"provider"
|
|
16
|
+
]) {
|
|
17
|
+
const value = params.filter?.[key];
|
|
18
|
+
if (value !== void 0 && value !== "") search.set(key, value);
|
|
19
|
+
}
|
|
20
|
+
const response = await fetchImpl(`${endpoint}/api/metrics?${search.toString()}`, { signal: AbortSignal.timeout(5e3) });
|
|
21
|
+
if (!response.ok) throw new Error(`sink responded ${response.status}`);
|
|
22
|
+
const body = await response.json();
|
|
23
|
+
if (body.error || !Array.isArray(body.timeline)) throw new Error(body.error ?? "sink is an older version");
|
|
24
|
+
return body;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { queryHttp as n, TOKEN_SORT as t };
|
|
28
|
+
|
|
29
|
+
//# sourceMappingURL=http-D7IHAk6Y.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-D7IHAk6Y.mjs","names":[],"sources":["../src/services/metricsSource/http.ts"],"sourcesContent":["import type { LogMetricsReport } from '@agimon-ai/po-logsink';\n\nimport type { MetricsQueryParams } from '../../types/metricsSource';\n\nexport const DEFAULT_TOOL_LIMIT = 1;\nexport const TOKEN_SORT = 'total-tokens';\n\n/** HTTP is the common transport for local and remote collectors. */\nexport async function queryHttp(\n endpoint: string,\n params: MetricsQueryParams,\n fetchImpl: typeof fetch,\n): Promise<LogMetricsReport> {\n const search = new URLSearchParams({\n groupBy: params.groupBy,\n period: params.period,\n sort: TOKEN_SORT,\n limit: String(params.limit),\n toolLimit: String(params.toolLimit ?? DEFAULT_TOOL_LIMIT),\n });\n for (const key of ['sessionId', 'agentName', 'model', 'provider'] as const) {\n const value = params.filter?.[key];\n if (value !== undefined && value !== '') search.set(key, value);\n }\n const response = await fetchImpl(`${endpoint}/api/metrics?${search.toString()}`, {\n signal: AbortSignal.timeout(5000),\n });\n if (!response.ok) throw new Error(`sink responded ${response.status}`);\n const body = (await response.json()) as LogMetricsReport & { error?: string };\n if (body.error || !Array.isArray(body.timeline)) throw new Error(body.error ?? 'sink is an older version');\n return body;\n}\n"],"mappings":"AAKA,MAAa,aAAa;;AAG1B,eAAsB,UACpB,UACA,QACA,WAC2B;CAC3B,MAAM,SAAS,IAAI,gBAAgB;EACjC,SAAS,OAAO;EAChB,QAAQ,OAAO;EACf,MAAM;EACN,OAAO,OAAO,OAAO,KAAK;EAC1B,WAAW,OAAO,OAAO,aAAA,CAA+B;CAC1D,CAAC;CACD,KAAK,MAAM,OAAO;EAAC;EAAa;EAAa;EAAS;CAAU,GAAY;EAC1E,MAAM,QAAQ,OAAO,SAAS;EAC9B,IAAI,UAAU,KAAA,KAAa,UAAU,IAAI,OAAO,IAAI,KAAK,KAAK;CAChE;CACA,MAAM,WAAW,MAAM,UAAU,GAAG,SAAS,eAAe,OAAO,SAAS,KAAK,EAC/E,QAAQ,YAAY,QAAQ,GAAI,EAClC,CAAC;CACD,IAAI,CAAC,SAAS,IAAI,MAAM,IAAI,MAAM,kBAAkB,SAAS,QAAQ;CACrE,MAAM,OAAQ,MAAM,SAAS,KAAK;CAClC,IAAI,KAAK,SAAS,CAAC,MAAM,QAAQ,KAAK,QAAQ,GAAG,MAAM,IAAI,MAAM,KAAK,SAAS,0BAA0B;CACzG,OAAO;AACT"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const TOKEN_SORT = "total-tokens";
|
|
2
|
+
/** HTTP is the common transport for local and remote collectors. */
|
|
3
|
+
async function queryHttp(endpoint, params, fetchImpl) {
|
|
4
|
+
const search = new URLSearchParams({
|
|
5
|
+
groupBy: params.groupBy,
|
|
6
|
+
period: params.period,
|
|
7
|
+
sort: TOKEN_SORT,
|
|
8
|
+
limit: String(params.limit),
|
|
9
|
+
toolLimit: String(params.toolLimit ?? 1)
|
|
10
|
+
});
|
|
11
|
+
for (const key of [
|
|
12
|
+
"sessionId",
|
|
13
|
+
"agentName",
|
|
14
|
+
"model",
|
|
15
|
+
"provider"
|
|
16
|
+
]) {
|
|
17
|
+
const value = params.filter?.[key];
|
|
18
|
+
if (value !== void 0 && value !== "") search.set(key, value);
|
|
19
|
+
}
|
|
20
|
+
const response = await fetchImpl(`${endpoint}/api/metrics?${search.toString()}`, { signal: AbortSignal.timeout(5e3) });
|
|
21
|
+
if (!response.ok) throw new Error(`sink responded ${response.status}`);
|
|
22
|
+
const body = await response.json();
|
|
23
|
+
if (body.error || !Array.isArray(body.timeline)) throw new Error(body.error ?? "sink is an older version");
|
|
24
|
+
return body;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
Object.defineProperty(exports, "TOKEN_SORT", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function() {
|
|
30
|
+
return TOKEN_SORT;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(exports, "queryHttp", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: function() {
|
|
36
|
+
return queryHttp;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=http-Dhn2bpMh.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-Dhn2bpMh.cjs","names":[],"sources":["../src/services/metricsSource/http.ts"],"sourcesContent":["import type { LogMetricsReport } from '@agimon-ai/po-logsink';\n\nimport type { MetricsQueryParams } from '../../types/metricsSource';\n\nexport const DEFAULT_TOOL_LIMIT = 1;\nexport const TOKEN_SORT = 'total-tokens';\n\n/** HTTP is the common transport for local and remote collectors. */\nexport async function queryHttp(\n endpoint: string,\n params: MetricsQueryParams,\n fetchImpl: typeof fetch,\n): Promise<LogMetricsReport> {\n const search = new URLSearchParams({\n groupBy: params.groupBy,\n period: params.period,\n sort: TOKEN_SORT,\n limit: String(params.limit),\n toolLimit: String(params.toolLimit ?? DEFAULT_TOOL_LIMIT),\n });\n for (const key of ['sessionId', 'agentName', 'model', 'provider'] as const) {\n const value = params.filter?.[key];\n if (value !== undefined && value !== '') search.set(key, value);\n }\n const response = await fetchImpl(`${endpoint}/api/metrics?${search.toString()}`, {\n signal: AbortSignal.timeout(5000),\n });\n if (!response.ok) throw new Error(`sink responded ${response.status}`);\n const body = (await response.json()) as LogMetricsReport & { error?: string };\n if (body.error || !Array.isArray(body.timeline)) throw new Error(body.error ?? 'sink is an older version');\n return body;\n}\n"],"mappings":"AAKA,MAAa,aAAa;;AAG1B,eAAsB,UACpB,UACA,QACA,WAC2B;CAC3B,MAAM,SAAS,IAAI,gBAAgB;EACjC,SAAS,OAAO;EAChB,QAAQ,OAAO;EACf,MAAM;EACN,OAAO,OAAO,OAAO,KAAK;EAC1B,WAAW,OAAO,OAAO,aAAA,CAA+B;CAC1D,CAAC;CACD,KAAK,MAAM,OAAO;EAAC;EAAa;EAAa;EAAS;CAAU,GAAY;EAC1E,MAAM,QAAQ,OAAO,SAAS;EAC9B,IAAI,UAAU,KAAA,KAAa,UAAU,IAAI,OAAO,IAAI,KAAK,KAAK;CAChE;CACA,MAAM,WAAW,MAAM,UAAU,GAAG,SAAS,eAAe,OAAO,SAAS,KAAK,EAC/E,QAAQ,YAAY,QAAQ,GAAI,EAClC,CAAC;CACD,IAAI,CAAC,SAAS,IAAI,MAAM,IAAI,MAAM,kBAAkB,SAAS,QAAQ;CACrE,MAAM,OAAQ,MAAM,SAAS,KAAK;CAClC,IAAI,KAAK,SAAS,CAAC,MAAM,QAAQ,KAAK,QAAQ,GAAG,MAAM,IAAI,MAAM,KAAK,SAAS,0BAA0B;CACzG,OAAO;AACT"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_findings = require("./findings-Bo4IHNpr.cjs");
|
|
3
|
+
const require_metrics = require("./metrics-Bml7TSZq.cjs");
|
|
3
4
|
exports.API_ERROR_RECORD = require_metrics.API_ERROR_RECORD;
|
|
4
5
|
exports.LogMetricsAggregator = require_metrics.LogMetricsAggregator;
|
|
5
6
|
exports.MAX_RETAINED_METRIC_NAMES = require_metrics.MAX_RETAINED_METRIC_NAMES;
|
|
@@ -10,4 +11,4 @@ exports.TOOL_RESULT_RECORD = require_metrics.TOOL_RESULT_RECORD;
|
|
|
10
11
|
exports.TOOL_TOKEN_SAMPLE_RECORD = require_metrics.TOOL_TOKEN_SAMPLE_RECORD;
|
|
11
12
|
exports.TURN_FAILED_RECORD = require_metrics.TURN_FAILED_RECORD;
|
|
12
13
|
exports.TURN_FINISHED_RECORD = require_metrics.TURN_FINISHED_RECORD;
|
|
13
|
-
exports.deriveFindings =
|
|
14
|
+
exports.deriveFindings = require_findings.deriveFindings;
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { S as TURN_FINISHED_RECORD, _ as MAX_RETAINED_TOKEN_BUCKETS, a as LogMetricsAggregator, b as TOOL_TOKEN_SAMPLE_RECORD, c as LogMetricsEventCount, d as LogMetricsSnapshot, f as LogMetricsTokenTotals, g as MAX_RETAINED_STATE_PER_TOOL, h as MAX_RETAINED_METRIC_NAMES, i as API_ERROR_RECORD, l as LogMetricsOperationDuration, m as LogMetricsToolLatency, n as LogMetricsFindingSeverity, o as LogMetricsAggregatorOptions, p as LogMetricsToolCost, r as deriveFindings, s as LogMetricsError, t as LogMetricsFinding, u as LogMetricsRecorder, v as METRIC_OVERFLOW_NAME, x as TURN_FAILED_RECORD, y as TOOL_RESULT_RECORD } from "./index-CTZMUfTZ.mjs";
|
|
2
|
-
import { i as MetricsSource } from "./metricsSource-
|
|
3
|
-
import {
|
|
2
|
+
import { i as MetricsSource } from "./metricsSource-sO8OpIes.mjs";
|
|
3
|
+
import { PoChildSessionHookRegistry } from "@agimon-ai/po-core/childSession";
|
|
4
|
+
import { PoWorkflowContextService } from "@agimon-ai/po-core/workflow";
|
|
5
|
+
import { PoTelemetryOptions } from "@agimon-ai/po-core/telemetry";
|
|
4
6
|
//#region src/types/sinkStatus.d.ts
|
|
5
7
|
interface SinkStatus {
|
|
6
8
|
service: string;
|
|
@@ -15,6 +17,10 @@ interface SinkStatus {
|
|
|
15
17
|
//#region src/types/piTelemetry.d.ts
|
|
16
18
|
type TelemetryFactory = NonNullable<PoTelemetryOptions['telemetryFactory']>;
|
|
17
19
|
interface PiTelemetryExtensionOptions {
|
|
20
|
+
/** Trusted optional step-session capability, never serialized. */
|
|
21
|
+
workflowContext?: PoWorkflowContextService;
|
|
22
|
+
/** Optional observer seam, ordinary sessions never wait for it. */
|
|
23
|
+
childSessionHookRegistry?: PoChildSessionHookRegistry;
|
|
18
24
|
allowFileFallback?: boolean;
|
|
19
25
|
/** Overrides the telemetry service identity. */
|
|
20
26
|
serviceName?: string;
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/types/sinkStatus.ts","../src/types/piTelemetry.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/types/sinkStatus.ts","../src/types/piTelemetry.ts"],"mappings":";;;;;;UAAiB;EACf;EACA;EACA;EACA;EACA;EACA;EACA;;;;KCAU,mBAAmB,YAAY;UAI1B;;EAEf,kBAAkB;;EAElB,2BAA2B;EAC3B;;EAEA;;EAEA;EACA,MAAM,OAAO;EACb,mBAAmB;;EAEnB,UAAU;;EAEV,gBAAgB,QAAQ;;EAExB,gBAAgB;;EAEhB,gBAAgB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
import { t as deriveFindings } from "./findings-DAKeRG4O.mjs";
|
|
2
|
+
import { a as MAX_RETAINED_TOKEN_BUCKETS, c as TOOL_TOKEN_SAMPLE_RECORD, i as MAX_RETAINED_STATE_PER_TOOL, l as TURN_FAILED_RECORD, n as LogMetricsAggregator, o as METRIC_OVERFLOW_NAME, r as MAX_RETAINED_METRIC_NAMES, s as TOOL_RESULT_RECORD, t as API_ERROR_RECORD, u as TURN_FINISHED_RECORD } from "./metrics-DwfV84Fy.mjs";
|
|
3
|
+
export { API_ERROR_RECORD, LogMetricsAggregator, MAX_RETAINED_METRIC_NAMES, MAX_RETAINED_STATE_PER_TOOL, MAX_RETAINED_TOKEN_BUCKETS, METRIC_OVERFLOW_NAME, TOOL_RESULT_RECORD, TOOL_TOKEN_SAMPLE_RECORD, TURN_FAILED_RECORD, TURN_FINISHED_RECORD, deriveFindings };
|