Add terminal code snippets feature #341
Reference in New Issue
Block a user
Delete Branch "feature/terminal-snippets"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Add code snippets management for terminal sessions with sidebar UI and database persistence.
Changes
/snippets)snippetstable with user-scoped storageFeatures
Technical Details
Summary of Changes
Hello @ZacharyZcR, 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 delivers a significant new feature: terminal code snippets. Users can now save, organize, and quickly execute frequently used commands directly from a dedicated sidebar within their terminal sessions. This enhancement is backed by a new database table and a robust API for managing snippets. Furthermore, the PR improves SSH host configuration by introducing a 'none' authentication option and adding support for Two-Factor Authentication (TOTP/2FA) during connection, making the terminal more versatile and secure.
Highlights
snippetstable to the database schema, designed to store user-specific code snippets with fields for name, content, description, and timestamps.Using Gemini Code Assist
The 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
/gemini <command>or@gemini-code-assist <command>. Below is a summary of the supported commands on the current page./gemini review/gemini summary/gemini helpCustomization
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
.gemini/folder in the base of the repository. Detailed instructions can be found here.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.
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩︎
Code Review
This pull request introduces a new feature for managing and executing terminal code snippets, complete with a backend API, database persistence, and a sidebar UI in the terminal. It also adds support for TOTP-based two-factor authentication for SSH connections and allows for SSH connections without credentials. The changes are well-structured, touching the backend, database, frontend UI, and type definitions. My review focuses on improving the security and efficiency of the new snippets API, enhancing type safety, and aligning the new UI code with React best practices.
@@ -0,0 +23,4 @@authenticateJWT,requireDataAccess,async (req: Request, res: Response) => {const userId = (req as any).userId;The
userIdis accessed via(req as any).userId, which bypasses TypeScript's type checking. To improve type safety and maintainability across the application, consider extending Express'sRequestinterface to include theuserIdproperty. This is typically done in a type definition file (e.g.,src/types/express.d.ts).Example:
With this in place, you can safely access
req.userIdwithout type casting toany.@@ -0,0 +77,4 @@authLogger.error("Failed to fetch snippet", err);res.status(500).json({error: err instanceof Error ? err.message : "Failed to fetch snippet",});The error handling in the
catchblock exposes internal error details (err.message) to the client. This can be a security risk, as it might leak information about the database or application structure. It's a best practice to return a generic error message for 500-level errors. This applies to the other handlers in this file as well.@@ -0,0 +195,4 @@);res.json(updated[0]);} catch (err) {The current implementation for updating a snippet is inefficient and has a potential security issue. It involves three separate database queries: one to check existence, one to update, and a final one to fetch the updated record. The final
selectquery on line 182 lacks auserIdfilter, which could potentially leak data under specific race conditions. Additionally,updateFieldsis typed asany, reducing type safety.This can be simplified, secured, and made more type-safe by using Drizzle's
returning()method on the update query. This performs the update and returns the modified record in a single, atomic operation.To align with React best practices, this form should be converted to use a controlled component. This improves state management and predictability.
You'll need to add a new state variable at the top of your component:
const [totpCode, setTotpCode] = useState("");Then, you can update the form as suggested. This change also ensures the input field is cleared upon submission or cancellation.