@hmharness/kernel 0.9.5 → 0.9.6
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/dist/adaptive-router.d.ts +56 -0
- package/dist/adaptive-router.js +87 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/kernel - Adaptive Model Router (P1-05)
|
|
3
|
+
*
|
|
4
|
+
* The audit called for: "基于历史 success/cost/latency 的路由实验"
|
|
5
|
+
*
|
|
6
|
+
* Routes requests to the best provider based on historical performance
|
|
7
|
+
* metrics (success rate, token cost, latency) per task type.
|
|
8
|
+
*/
|
|
9
|
+
export interface RouteHistoryEntry {
|
|
10
|
+
provider: string;
|
|
11
|
+
taskType: string;
|
|
12
|
+
success: boolean;
|
|
13
|
+
tokens: number;
|
|
14
|
+
durationMs: number;
|
|
15
|
+
timestamp: string;
|
|
16
|
+
}
|
|
17
|
+
export interface ProviderStats {
|
|
18
|
+
provider: string;
|
|
19
|
+
taskType: string;
|
|
20
|
+
totalRequests: number;
|
|
21
|
+
successRate: number;
|
|
22
|
+
avgTokens: number;
|
|
23
|
+
avgDurationMs: number;
|
|
24
|
+
/** composite score [0,1] - higher is better */
|
|
25
|
+
score: number;
|
|
26
|
+
}
|
|
27
|
+
export interface RouteDecision {
|
|
28
|
+
provider: string;
|
|
29
|
+
reason: string;
|
|
30
|
+
stats?: ProviderStats;
|
|
31
|
+
/** all candidates considered */
|
|
32
|
+
alternatives: Array<{
|
|
33
|
+
provider: string;
|
|
34
|
+
score: number;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Compute statistics for a provider from history.
|
|
39
|
+
* Pure - testable.
|
|
40
|
+
*/
|
|
41
|
+
export declare function computeStats(history: RouteHistoryEntry[], provider: string, taskType: string): ProviderStats | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Decide which provider to route to based on historical performance.
|
|
44
|
+
* Pure - testable.
|
|
45
|
+
*/
|
|
46
|
+
export declare function computeRoute(history: RouteHistoryEntry[], taskType: string, candidates: string[], opts?: {
|
|
47
|
+
minSamples?: number;
|
|
48
|
+
successWeight?: number;
|
|
49
|
+
costWeight?: number;
|
|
50
|
+
latencyWeight?: number;
|
|
51
|
+
}): RouteDecision;
|
|
52
|
+
/**
|
|
53
|
+
* Format route history as a summary string.
|
|
54
|
+
* Pure - testable.
|
|
55
|
+
*/
|
|
56
|
+
export declare function routeSummary(history: RouteHistoryEntry[]): string;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/kernel - Adaptive Model Router (P1-05)
|
|
3
|
+
*
|
|
4
|
+
* The audit called for: "基于历史 success/cost/latency 的路由实验"
|
|
5
|
+
*
|
|
6
|
+
* Routes requests to the best provider based on historical performance
|
|
7
|
+
* metrics (success rate, token cost, latency) per task type.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Compute statistics for a provider from history.
|
|
11
|
+
* Pure - testable.
|
|
12
|
+
*/
|
|
13
|
+
export function computeStats(history, provider, taskType) {
|
|
14
|
+
const entries = history.filter(h => h.provider === provider && h.taskType === taskType);
|
|
15
|
+
if (entries.length === 0)
|
|
16
|
+
return undefined;
|
|
17
|
+
const success = entries.filter(e => e.success).length;
|
|
18
|
+
const avgTokens = entries.reduce((a, b) => a + b.tokens, 0) / entries.length;
|
|
19
|
+
const avgDuration = entries.reduce((a, b) => a + b.durationMs, 0) / entries.length;
|
|
20
|
+
const successRate = success / entries.length;
|
|
21
|
+
// composite: 60% success + 20% cost efficiency + 20% latency efficiency
|
|
22
|
+
// cost and latency are inverse-normalized against the best in the set
|
|
23
|
+
return {
|
|
24
|
+
provider, taskType,
|
|
25
|
+
totalRequests: entries.length,
|
|
26
|
+
successRate,
|
|
27
|
+
avgTokens,
|
|
28
|
+
avgDurationMs: avgDuration,
|
|
29
|
+
score: successRate, // will be adjusted by computeRoute
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Decide which provider to route to based on historical performance.
|
|
34
|
+
* Pure - testable.
|
|
35
|
+
*/
|
|
36
|
+
export function computeRoute(history, taskType, candidates, opts = {}) {
|
|
37
|
+
const { minSamples = 3, successWeight = 0.6, costWeight = 0.2, latencyWeight = 0.2 } = opts;
|
|
38
|
+
const statsList = [];
|
|
39
|
+
for (const p of candidates) {
|
|
40
|
+
const s = computeStats(history, p, taskType);
|
|
41
|
+
if (s && s.totalRequests >= minSamples)
|
|
42
|
+
statsList.push(s);
|
|
43
|
+
}
|
|
44
|
+
if (statsList.length === 0) {
|
|
45
|
+
return {
|
|
46
|
+
provider: candidates[0] ?? 'default',
|
|
47
|
+
reason: 'insufficient history - using first candidate',
|
|
48
|
+
alternatives: candidates.map(p => ({ provider: p, score: 0 })),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
// normalize cost and latency across candidates
|
|
52
|
+
const minTokens = Math.min(...statsList.map(s => s.avgTokens));
|
|
53
|
+
const minDuration = Math.min(...statsList.map(s => s.avgDurationMs));
|
|
54
|
+
for (const s of statsList) {
|
|
55
|
+
const costEff = minTokens > 0 ? minTokens / s.avgTokens : 1;
|
|
56
|
+
const latencyEff = minDuration > 0 ? minDuration / s.avgDurationMs : 1;
|
|
57
|
+
s.score = successWeight * s.successRate + costWeight * costEff + latencyWeight * latencyEff;
|
|
58
|
+
}
|
|
59
|
+
statsList.sort((a, b) => b.score - a.score);
|
|
60
|
+
const best = statsList[0];
|
|
61
|
+
return {
|
|
62
|
+
provider: best.provider,
|
|
63
|
+
reason: `best composite score ${best.score.toFixed(3)} (success ${(best.successRate * 100).toFixed(0)}%, ${best.totalRequests} samples)`,
|
|
64
|
+
stats: best,
|
|
65
|
+
alternatives: statsList.map(s => ({ provider: s.provider, score: Math.round(s.score * 1000) / 1000 })),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Format route history as a summary string.
|
|
70
|
+
* Pure - testable.
|
|
71
|
+
*/
|
|
72
|
+
export function routeSummary(history) {
|
|
73
|
+
const byProvider = new Map();
|
|
74
|
+
for (const h of history) {
|
|
75
|
+
const cur = byProvider.get(h.provider) ?? { total: 0, success: 0, avgTokens: 0 };
|
|
76
|
+
cur.total++;
|
|
77
|
+
if (h.success)
|
|
78
|
+
cur.success++;
|
|
79
|
+
cur.avgTokens = (cur.avgTokens * (cur.total - 1) + h.tokens) / cur.total;
|
|
80
|
+
byProvider.set(h.provider, cur);
|
|
81
|
+
}
|
|
82
|
+
const lines = [`routing history: ${history.length} entries`];
|
|
83
|
+
for (const [p, s] of byProvider) {
|
|
84
|
+
lines.push(` ${p}: ${s.success}/${s.total} success (${Math.round(s.success / s.total * 100)}%), avg ${Math.round(s.avgTokens)} tokens`);
|
|
85
|
+
}
|
|
86
|
+
return lines.join('\n');
|
|
87
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hmharness/kernel",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.6",
|
|
4
4
|
"description": "hmharness kernel: tool registry, provider adapters, the agent loop, session log, config. Zero runtime dependencies (Node >=22 native fetch).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|