Repository navigation
feat: add REST API for session pool health (dashboard interface) #3404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 5 commits into
cheaper-inference:release/v3.8.16
from
oyi77:feat/session-pool-rest-api
Jun 8, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
781aff1
feat: add web-session pool observability (MCP tool + health-matrix)
oyi77 979b61a
fix: guard success rate check with totalRequests > 0 for idle pools
oyi77 8ed456c
feat: add REST API for session pool health (dashboard interface)
oyi77 6142475
Merge remote-tracking branch 'origin/release/v3.8.16' into feat/sessi…
diegosouzapw 28f9a07
fix(session-pools): sanitize error messages in REST routes (Rule #12)
oyi77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { requireManagementAuth } from "@/lib/api/requireManagementAuth"; | ||
| import { getWebSessionPoolHealth } from "@omniroute/open-sse/services/webSessionPoolHealth"; | ||
| import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| export async function GET( | ||
| request: Request, | ||
| { params }: { params: Promise<{ provider: string }> } | ||
| ): Promise<Response> { | ||
| const authError = await requireManagementAuth(request); | ||
| if (authError) return authError; | ||
|
|
||
| const { provider } = await params; | ||
|
|
||
| try { | ||
| const report = getWebSessionPoolHealth(provider); | ||
| const poolData = report.providers[0]; | ||
|
|
||
| if (!poolData) { | ||
| return NextResponse.json( | ||
| { error: `No session pool found for provider '${provider}'` }, | ||
| { status: 404 } | ||
| ); | ||
| } | ||
|
|
||
| return NextResponse.json({ | ||
| checkedAt: report.checkedAt, | ||
| ...poolData, | ||
| }); | ||
| } catch (err) { | ||
| return NextResponse.json( | ||
| { error: sanitizeErrorMessage(err) || "Failed to get session pool health" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { requireManagementAuth } from "@/lib/api/requireManagementAuth"; | ||
| import { getWebSessionPoolHealth } from "@omniroute/open-sse/services/webSessionPoolHealth"; | ||
| import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| export async function GET(request: Request): Promise<Response> { | ||
| const authError = await requireManagementAuth(request); | ||
| if (authError) return authError; | ||
|
|
||
| try { | ||
| const report = getWebSessionPoolHealth(); | ||
| return NextResponse.json(report); | ||
| } catch (err) { | ||
| return NextResponse.json( | ||
| { error: sanitizeErrorMessage(err) || "Failed to get session pool health" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| /** | ||
| * Session Pool REST API — Unit Tests (PR7 of issue #3368) | ||
| * | ||
| * Run: node --import tsx/esm --test tests/unit/session-pool-rest-api.test.ts | ||
| */ | ||
|
|
||
| import { describe, it } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import type { WebSessionPoolHealthDeps } from "../../open-sse/services/webSessionPoolHealth.ts"; | ||
| import { getWebSessionPoolHealth } from "../../open-sse/services/webSessionPoolHealth.ts"; | ||
|
|
||
| function createMockDeps(overrides: { | ||
| providers?: string[]; | ||
| stats?: Record<string, any>; | ||
| sessionDetails?: Record<string, any[]>; | ||
| breakerCooldown?: Record<string, boolean>; | ||
| breakerRemaining?: Record<string, number | null>; | ||
| breakerStates?: Record<string, any>; | ||
| } = {}): WebSessionPoolHealthDeps { | ||
| const providers = overrides.providers ?? []; | ||
| const stats = overrides.stats ?? {}; | ||
| const sessionDetails = overrides.sessionDetails ?? {}; | ||
| const breakerCooldown = overrides.breakerCooldown ?? {}; | ||
| const breakerRemaining = overrides.breakerRemaining ?? {}; | ||
| const breakerStates = overrides.breakerStates ?? {}; | ||
|
|
||
| return { | ||
| listProviders: () => [...providers], | ||
| getStats: (p: string) => stats[p] ?? null, | ||
| getSessionDetails: (p: string) => sessionDetails[p] ?? null, | ||
| isProviderInCooldown: (p: string) => breakerCooldown[p] ?? false, | ||
| getProviderCooldownRemainingMs: (p: string) => breakerRemaining[p] ?? null, | ||
| getProviderBreakerState: (p: string) => breakerStates[p] ?? null, | ||
| }; | ||
| } | ||
|
|
||
| describe("GET /api/session-pools (list all)", () => { | ||
| it("returns empty report when no pools registered", () => { | ||
| const deps = createMockDeps(); | ||
| const report = getWebSessionPoolHealth(undefined, deps); | ||
| assert.equal(report.providers.length, 0); | ||
| assert.ok(report.checkedAt); | ||
| }); | ||
|
|
||
| it("returns all registered pools", () => { | ||
| const now = Date.now(); | ||
| const deps = createMockDeps({ | ||
| providers: ["pollinations", "longcat"], | ||
| stats: { | ||
| pollinations: { | ||
| provider: "pollinations", | ||
| sessions: { total: 6, active: 5, cooldown: 1, dead: 0 }, | ||
| requests: { total: 100, success: 95, rate429: 3, otherErrors: 2 }, | ||
| throughput: "12.3 req/s", | ||
| successRate: "95.0%", | ||
| elapsed: "2h 15m", | ||
| createdAt: now - 2 * 60 * 60 * 1000, | ||
| }, | ||
| longcat: { | ||
| provider: "longcat", | ||
| sessions: { total: 4, active: 4, cooldown: 0, dead: 0 }, | ||
| requests: { total: 50, success: 50, rate429: 0, otherErrors: 0 }, | ||
| throughput: "8.0 req/s", | ||
| successRate: "100.0%", | ||
| elapsed: "1h", | ||
| createdAt: now - 60 * 60 * 1000, | ||
| }, | ||
| }, | ||
| sessionDetails: { pollinations: [], longcat: [] }, | ||
| breakerStates: { | ||
| pollinations: { state: "CLOSED", failureCount: 0, lastFailureTime: null, retryAfterMs: 0 }, | ||
| longcat: { state: "CLOSED", failureCount: 0, lastFailureTime: null, retryAfterMs: 0 }, | ||
| }, | ||
| }); | ||
|
|
||
| const report = getWebSessionPoolHealth(undefined, deps); | ||
| assert.equal(report.providers.length, 2); | ||
| assert.equal(report.providers[0].provider, "pollinations"); | ||
| assert.equal(report.providers[1].provider, "longcat"); | ||
| }); | ||
|
|
||
| it("includes health summary for each pool", () => { | ||
| const deps = createMockDeps({ | ||
| providers: ["pollinations"], | ||
| stats: { | ||
| pollinations: { | ||
| provider: "pollinations", | ||
| sessions: { total: 6, active: 5, cooldown: 1, dead: 0 }, | ||
| requests: { total: 100, success: 95, rate429: 3, otherErrors: 2 }, | ||
| throughput: "12.3 req/s", | ||
| successRate: "95.0%", | ||
| elapsed: "2h", | ||
| createdAt: Date.now() - 2 * 60 * 60 * 1000, | ||
| }, | ||
| }, | ||
| sessionDetails: { pollinations: [] }, | ||
| breakerStates: { | ||
| pollinations: { state: "CLOSED", failureCount: 0, lastFailureTime: null, retryAfterMs: 0 }, | ||
| }, | ||
| }); | ||
|
|
||
| const report = getWebSessionPoolHealth(undefined, deps); | ||
| const pool = report.providers[0]; | ||
| assert.ok(pool.pool); | ||
| assert.equal(pool.pool.totalSessions, 6); | ||
| assert.equal(pool.pool.activeSessions, 5); | ||
| assert.equal(pool.health, "healthy"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("GET /api/session-pools/[provider] (single pool)", () => { | ||
| it("returns specific pool when provider arg given", () => { | ||
| const deps = createMockDeps({ | ||
| providers: ["pollinations", "longcat"], | ||
| stats: { | ||
| pollinations: { | ||
| provider: "pollinations", | ||
| sessions: { total: 6, active: 5, cooldown: 1, dead: 0 }, | ||
| requests: { total: 100, success: 95, rate429: 3, otherErrors: 2 }, | ||
| throughput: "12.3 req/s", | ||
| successRate: "95.0%", | ||
| elapsed: "2h", | ||
| createdAt: Date.now() - 2 * 60 * 60 * 1000, | ||
| }, | ||
| }, | ||
| sessionDetails: { pollinations: [] }, | ||
| breakerStates: { | ||
| pollinations: { state: "CLOSED", failureCount: 0, lastFailureTime: null, retryAfterMs: 0 }, | ||
| }, | ||
| }); | ||
|
|
||
| const report = getWebSessionPoolHealth("pollinations", deps); | ||
| assert.equal(report.providers.length, 1); | ||
| assert.equal(report.providers[0].provider, "pollinations"); | ||
| }); | ||
|
|
||
| it("returns pool with session details", () => { | ||
| const deps = createMockDeps({ | ||
| providers: ["pollinations"], | ||
| stats: { | ||
| pollinations: { | ||
| provider: "pollinations", | ||
| sessions: { total: 2, active: 2, cooldown: 0, dead: 0 }, | ||
| requests: { total: 10, success: 10, rate429: 0, otherErrors: 0 }, | ||
| throughput: "5.0 req/s", | ||
| successRate: "100.0%", | ||
| elapsed: "30m", | ||
| createdAt: Date.now() - 30 * 60 * 1000, | ||
| }, | ||
| }, | ||
| sessionDetails: { | ||
| pollinations: [ | ||
| { | ||
| id: "s1", | ||
| fingerprint: "fp-1", | ||
| status: "active", | ||
| totalRequests: 5, | ||
| successfulRequests: 5, | ||
| successRate: "100.0%", | ||
| inflight: 0, | ||
| cooldownRemaining: "none", | ||
| age: "30m", | ||
| }, | ||
| { | ||
| id: "s2", | ||
| fingerprint: "fp-2", | ||
| status: "active", | ||
| totalRequests: 5, | ||
| successfulRequests: 5, | ||
| successRate: "100.0%", | ||
| inflight: 1, | ||
| cooldownRemaining: "none", | ||
| age: "30m", | ||
| }, | ||
| ], | ||
| }, | ||
| breakerStates: { | ||
| pollinations: { state: "CLOSED", failureCount: 0, lastFailureTime: null, retryAfterMs: 0 }, | ||
| }, | ||
| }); | ||
|
|
||
| const report = getWebSessionPoolHealth("pollinations", deps); | ||
| const pool = report.providers[0]; | ||
| assert.equal(pool.sessions.length, 2); | ||
| assert.equal(pool.sessions[0].id, "s1"); | ||
| assert.equal(pool.sessions[1].id, "s2"); | ||
| }); | ||
|
|
||
| it("returns pool with breaker state when available", () => { | ||
| const now = Date.now(); | ||
| const deps = createMockDeps({ | ||
| providers: ["pollinations"], | ||
| stats: { | ||
| pollinations: { | ||
| provider: "pollinations", | ||
| sessions: { total: 6, active: 0, cooldown: 2, dead: 4 }, | ||
| requests: { total: 50, success: 10, rate429: 30, otherErrors: 10 }, | ||
| throughput: "0.5 req/s", | ||
| successRate: "20.0%", | ||
| elapsed: "1h", | ||
| createdAt: now - 60 * 60 * 1000, | ||
| }, | ||
| }, | ||
| sessionDetails: { pollinations: [] }, | ||
| breakerCooldown: { pollinations: true }, | ||
| breakerRemaining: { pollinations: 30000 }, | ||
| breakerStates: { | ||
| pollinations: { | ||
| state: "OPEN", | ||
| failureCount: 10, | ||
| lastFailureTime: now - 5000, | ||
| retryAfterMs: 30000, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const report = getWebSessionPoolHealth("pollinations", deps); | ||
| const pool = report.providers[0]; | ||
| assert.ok(pool.breaker); | ||
| assert.equal(pool.breaker.state, "OPEN"); | ||
| assert.equal(pool.breaker.inCooldown, true); | ||
| assert.equal(pool.breaker.cooldownRemainingMs, 30000); | ||
| assert.equal(pool.health, "down"); | ||
| }); | ||
|
|
||
| it("returns provider entry even when no pool registered (for 404 detection)", () => { | ||
| const deps = createMockDeps(); | ||
| const report = getWebSessionPoolHealth("nonexistent", deps); | ||
| assert.equal(report.providers.length, 1); | ||
| assert.equal(report.providers[0].provider, "nonexistent"); | ||
| assert.equal(report.providers[0].pool, null); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current check
if (!poolData)will never evaluate to true becausegetWebSessionPoolHealth(provider)always returns a provider entry (even if the provider has no active session pool, in which casepoolData.poolisnull). This causes the endpoint to return200 OKwith null pool data instead of the expected404 Not Foundfor nonexistent or unregistered providers.Updating the condition to check
poolData.pool === nullensures that a404is correctly returned when no session pool exists for the requested provider.