Skip to content
Closed
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
24 changes: 24 additions & 0 deletions app/api/ai/models/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getAvailableModels } from "@/lib/ai/getAvailableModels";

/**
* GET /api/ai/models
*
* Server-side endpoint that proxies `getAvailableModels()` so that the
* client can fetch model metadata without requiring server-side imports
* of `@ai-sdk/gateway`.
*/
export async function GET() {
try {
const models = await getAvailableModels();
return Response.json({ models });
} catch (error) {
console.error("/api/ai/models error", error);
const message = error instanceof Error ? error.message : "failed";
return Response.json({ message }, { status: 500 });
Comment on lines +12 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

The catch won't handle the main failure mode.

lib/ai/getAvailableModels.ts:9-16 catches gateway errors and returns [], so upstream outages still produce 200 { models: [] } here and never hit Line 15. Let the helper rethrow, or return a typed failure/result so this route can send a real non-200 response.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/api/ai/models/route.ts` around lines 12 - 17, The route currently treats
any empty array from getAvailableModels() as success because the helper swallows
gateway errors; update getAvailableModels (in lib/ai/getAvailableModels.ts) to
either rethrow on gateway/network errors instead of returning [] or return a
typed Result/Failure object, and then modify this route handler (route.ts) to
detect that failure and return a non-200 Response (e.g., Response.json({ message
}, { status: 500 })) when the helper signals an error; ensure you reference
getAvailableModels() when changing behavior so the catch here actually receives
thrown errors or inspects the Result and returns the appropriate error response.

}
}

// Disable caching to always serve the latest model list.
export const dynamic = "force-dynamic";
export const fetchCache = "force-no-store";
export const revalidate = 0;
Comment on lines +10 to +24

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Require validateAuthContext() before fetching models.

This handler is unauthenticated right now. With Line 12 making a live gateway call and Lines 21-24 disabling caching, anonymous traffic can turn this into a direct upstream fan-out path. Change the signature to accept the request and validate auth before calling getAvailableModels(). As per coding guidelines, "Always use validateAuthContext() for authentication in API routes; it supports both x-api-key header and Authorization: Bearer token authentication".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/api/ai/models/route.ts` around lines 10 - 24, Change the GET handler to
require and validate auth before calling getAvailableModels(): update the
function signature to accept the Request (e.g., export async function GET(req))
and call validateAuthContext(req) at the top, returning an appropriate 401/403
Response.json if validation fails; only after successful validateAuthContext()
call getAvailableModels() and return Response.json({ models }); keep the
existing error handling and keep dynamic/fetchCache/revalidate exports
unchanged.

Loading