Repository navigation
agent: copy one API endpoint (the simplest to copy) from the chat codebase to t - #285
sweetmantech wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughA new Next.js API route handler at Changes
Sequence DiagramsequenceDiagram
participant Client
participant RouteHandler as GET /api/ai/models
participant ModelsService as getAvailableModels()
participant Response
Client->>RouteHandler: Request available models
RouteHandler->>ModelsService: Fetch available models
alt Success
ModelsService-->>RouteHandler: Return models array
RouteHandler->>Response: 200 JSON (models)
else Error
ModelsService-->>RouteHandler: Throw error
RouteHandler->>RouteHandler: Log error safely
RouteHandler->>Response: 500 JSON (error message)
end
Response-->>Client: Response
Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ❌ 1❌ Failed checks (1 inconclusive)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
app/api/ai/models/route.ts (1)
10-19: Add endpoint tests for both branches.This new route should be covered with a success test and an error test that mocks
getAvailableModels()failure. That would also pin the intended status code/body once the helper stops swallowing upstream exceptions. Based on learnings, "Applies to app/api/**/route.ts : Write tests for new API endpoints covering all success and error paths".🤖 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 - 19, Add two tests for the GET handler in app/api/ai/models/route.ts: one that stubs getAvailableModels() to resolve with a sample models array and asserts the endpoint returns status 200 and a JSON body containing { models }, and another that stubs getAvailableModels() to throw/reject and asserts the endpoint returns status 500 and a JSON body containing { message } (matching the error.message when an Error is thrown). In your test file (e.g., tests/api/ai/models.test.ts) import or request the route handler, mock the getAvailableModels module (using your test runner/mocking utilities) to control success/failure, and verify both response status and body shape to lock in intended behavior for the GET function and the getAvailableModels() error path.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@app/api/ai/models/route.ts`:
- Around line 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.
- Around line 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.
---
Nitpick comments:
In `@app/api/ai/models/route.ts`:
- Around line 10-19: Add two tests for the GET handler in
app/api/ai/models/route.ts: one that stubs getAvailableModels() to resolve with
a sample models array and asserts the endpoint returns status 200 and a JSON
body containing { models }, and another that stubs getAvailableModels() to
throw/reject and asserts the endpoint returns status 500 and a JSON body
containing { message } (matching the error.message when an Error is thrown). In
your test file (e.g., tests/api/ai/models.test.ts) import or request the route
handler, mock the getAvailableModels module (using your test runner/mocking
utilities) to control success/failure, and verify both response status and body
shape to lock in intended behavior for the GET function and the
getAvailableModels() error path.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 2f9b64eb-9537-41f7-bf76-06c082a5b831
📒 Files selected for processing (1)
app/api/ai/models/route.ts
| 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 }); | ||
| } | ||
| } | ||
|
|
||
| // Disable caching to always serve the latest model list. | ||
| export const dynamic = "force-dynamic"; | ||
| export const fetchCache = "force-no-store"; | ||
| export const revalidate = 0; |
There was a problem hiding this comment.
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.
| 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 }); |
There was a problem hiding this comment.
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.
|
Closing this unmerged PR as superseded in source by #498 ( |
Automated PR from coding agent.
Summary by CodeRabbit
/api/ai/modelsendpoint to retrieve available AI models with improved error handling and real-time data fetching.