Skip to main content
Loading navigation
Back to prompt library

Implement Code Generation Workflow

Linked challenge: Accelerated Code Dev & Review Agent

Prompt source

implementation23 lines3 sectionsNo variables1 code block
Extend your Mastra AI project. Implement a workflow where the 'CodeGenerator' agent receives a feature description (e.g., 'a function to sort a list of dictionaries by a key'). It should then use Claude Sonnet 4 to generate the Python function code and corresponding unit tests. Implement a custom tool that allows the agent to 'save_file(filename, content)' to a mock file system. The agent should invoke this tool twice: once for the function, once for the tests. Ensure it follows PEP8 where possible.

```typescript
// Example of a custom tool
const saveFileTool = createTool({
  id: 'save_file',
  description: 'Saves content to a specified file.',
  schema: {
    type: 'object',
    properties: {
      filename: { type: 'string' },
      content: { type: 'string' },
    },
    required: ['filename', 'content'],
  },
  async execute({ filename, content }) {
    console.log(`Saving ${filename}...`);
    // Simulate file save, e.g., write to a local temp dir or in-memory map
    return `File ${filename} saved successfully.`;
  },
});

codeGenerator.addTool(saveFileTool);
// ... Workflow to use the tool
```