refactor(js): replace reflection server express with hono#4851
refactor(js): replace reflection server express with hono#4851Ehesp wants to merge 3 commits intofirebase:mainfrom
Conversation
Summary of ChangesHello @Ehesp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the reflection server by replacing the Express.js framework with Hono. This change was driven by the need for a lighter, more runtime-agnostic solution, as Express.js presented compatibility challenges in non-Node.js environments. The migration involved updating core dependencies, rewriting server-side logic to leverage Hono's features, and introducing a new test suite to ensure the stability and correctness of the refactored server. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a solid refactoring that replaces the Express.js-based reflection server with Hono. This is a commendable move towards a lighter, more performant, and runtime-agnostic implementation. The code changes are well-executed, and the introduction of a test suite for the reflection server significantly improves the robustness of this critical component. I've identified a couple of opportunities to enhance error handling consistency and align with standard HTTP practices, which should further strengthen the API.
| try { | ||
| body = await c.req.json(); | ||
| } catch { | ||
| return c.text('OK', 200); |
There was a problem hiding this comment.
Inconsistent error handling for invalid JSON body. This endpoint returns a 200 OK response when JSON parsing fails, which can hide issues from the client. Other POST endpoints in this file, like /api/runAction and /api/cancelAction, correctly return a 400 error. For consistency and proper error reporting, this should also return a 400 error with an informative message.
| return c.text('OK', 200); | |
| return c.json({ error: 'Invalid JSON body' }, 400); |
| app.onError((err, c) => { | ||
| logger.error(err.stack); | ||
| const error = err as Error; | ||
| const { message, stack } = error; | ||
| const errorResponse: Status = { | ||
| code: StatusCodes.INTERNAL, | ||
| message, | ||
| details: { | ||
| stack, | ||
| }, | ||
| message: err.message, | ||
| details: { stack: err.stack }, | ||
| }; | ||
|
|
||
| // Headers may have been sent already (via onTraceStart), so check before setting status | ||
| res.status(200).end(JSON.stringify({ error: errorResponse })); | ||
| return (c as { json: (body: unknown, status?: number) => Response }).json( | ||
| { error: errorResponse }, | ||
| 200 | ||
| ); | ||
| }); |
There was a problem hiding this comment.
This error handler returns a 200 OK status code for what appears to be an internal server error. This is unconventional and can be misleading for clients. Most other error paths in this file correctly return 4xx or 5xx status codes. It would be better to use a 500 Internal Server Error status code here to align with standard HTTP practices and improve API consistency.
Additionally, the type assertion for c on line 438 seems unnecessary. The Context object from Hono provides a json method that should work without casting, which would improve code clarity.
| app.onError((err, c) => { | |
| logger.error(err.stack); | |
| const error = err as Error; | |
| const { message, stack } = error; | |
| const errorResponse: Status = { | |
| code: StatusCodes.INTERNAL, | |
| message, | |
| details: { | |
| stack, | |
| }, | |
| message: err.message, | |
| details: { stack: err.stack }, | |
| }; | |
| // Headers may have been sent already (via onTraceStart), so check before setting status | |
| res.status(200).end(JSON.stringify({ error: errorResponse })); | |
| return (c as { json: (body: unknown, status?: number) => Response }).json( | |
| { error: errorResponse }, | |
| 200 | |
| ); | |
| }); | |
| app.onError((err, c) => { | |
| logger.error(err.stack); | |
| const errorResponse: Status = { | |
| code: StatusCodes.INTERNAL, | |
| message: err.message, | |
| details: { stack: err.stack }, | |
| }; | |
| return c.json( | |
| { error: errorResponse }, | |
| 500 | |
| ); | |
| }); |
This PR replaces the reflection server using express with hono.
Express is mainly designed for Node, and whilst restricted runtime support exists, express and its deps are still bundled with genkit, causing issues in those environments; e.g.
#4815
Hono is much lighter, less dependencies and works in all JS based runtimes.
Also added a test suite for the reflection server. Need to do more manual testing.