diff --git a/src/lib/a2a/routingLogger.ts b/src/lib/a2a/routingLogger.ts index de1b701ec940..45468bce5823 100644 --- a/src/lib/a2a/routingLogger.ts +++ b/src/lib/a2a/routingLogger.ts @@ -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(); - 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, - }; -} diff --git a/tests/unit/a2a-routing-logger.test.ts b/tests/unit/a2a-routing-logger.test.ts new file mode 100644 index 000000000000..03b83e192787 --- /dev/null +++ b/tests/unit/a2a-routing-logger.test.ts @@ -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"); +});