Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions src/lib/a2a/routingLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,52 +65,3 @@ export function logRoutingDecision(

return decision;
}

/**
* Get a specific routing decision by request ID.
*/
export function getRoutingDecision(requestId: string): RoutingDecision | undefined {
return decisions.find((d) => d.requestId === requestId);
}

/**
* Get recent routing decisions.
*/
export function getRecentDecisions(limit: number = 20): RoutingDecision[] {
return decisions.slice(-limit).reverse();
}

/**
* Get routing decision stats.
*/
export function getDecisionStats(): {
total: number;
successRate: number;
avgLatencyMs: number;
topProviders: Array<{ provider: string; count: number }>;
} {
if (decisions.length === 0) {
return { total: 0, successRate: 1, avgLatencyMs: 0, topProviders: [] };
}

const successful = decisions.filter((d) => d.success);
const providerCounts = new Map<string, number>();
let totalLatency = 0;

for (const d of decisions) {
totalLatency += d.latencyMs;
providerCounts.set(d.providerSelected, (providerCounts.get(d.providerSelected) || 0) + 1);
}

const topProviders = [...providerCounts.entries()]
.sort((a, b) => b[1] - a[1])
.slice(0, 5)
.map(([provider, count]) => ({ provider, count }));

return {
total: decisions.length,
successRate: successful.length / decisions.length,
avgLatencyMs: Math.round(totalLatency / decisions.length),
topProviders,
};
}
32 changes: 32 additions & 0 deletions tests/unit/a2a-routing-logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import test from "node:test";
import assert from "node:assert/strict";

import { logRoutingDecision } from "../../src/lib/a2a/routingLogger.ts";

test("logRoutingDecision records a routing decision with generated metadata", () => {
const decision = logRoutingDecision({
taskType: "chat",
comboId: "combo-1",
providerSelected: "openai",
modelUsed: "gpt-4o-mini",
score: 0.91,
factors: [
{
name: "health",
value: 1,
weight: 0.5,
contribution: 0.5,
},
],
fallbacksTriggered: [],
success: true,
latencyMs: 123,
cost: 0.001,
});

assert.equal(typeof decision.requestId, "string");
assert.match(decision.requestId, /^[0-9a-f-]{36}$/);
assert.match(decision.timestamp, /^\d{4}-\d{2}-\d{2}T/);
assert.equal(decision.providerSelected, "openai");
assert.equal(decision.factors[0].name, "health");
});
Loading