What You’ll Build
A code assistant that can:- Read and analyze source files
- Search for code patterns
- Provide code suggestions and explanations
- Follow coding best practices
Prerequisites
Complete Example
Step-by-Step Walkthrough
Define File Operation Tools
Create tools for reading files, searching code, and analyzing structure:
These tools provide the assistant with access to your codebase. In production, add proper security checks and path validation.
Create the Code Assistant Builder
Set up the assistant’s identity and expertise:The identity establishes the assistant’s area of expertise.
Add Code Analysis Tools
Add tools for file operations and code analysis:Each tool is typed with Zod for parameter validation.
Set Coding Best Practices
Define constraints that ensure quality suggestions:These constraints ensure the assistant follows best practices.
Export to Mastra
Convert the PromptSmith configuration to Mastra format:The
.toMastra() method handles all the conversion automatically.Expected Output
When you run this example, you’ll see output similar to:Key Concepts
File System Tools - This example shows how to give your agent access to the file system safely using tools with proper error handling and validation.
Mastra Integration - Using
.toMastra() automatically converts PromptSmith tools to Mastra’s format, eliminating duplication and ensuring compatibility.Context-Aware Analysis - The constraints ensure the assistant always reads files before making suggestions, providing context-aware recommendations.
Security Considerations
When building code assistants with file access:- Validate and sanitize all file paths
- Restrict access to specific directories
- Never execute code without explicit user confirmation
- Log all file operations for audit trails
- Consider rate limiting to prevent abuse
Customization Ideas
- Add a
write_filetool for code modifications - Include a
run_teststool for testing suggestions - Add git integration for viewing diffs and history
- Implement AST parsing for deeper code analysis
- Add linting and formatting tools
Next Steps
Data Analysis Agent
Learn how to build agents with conditional logic
Mastra Integration
Deep dive into using PromptSmith with Mastra