Skip to content

Commit d5f9468

Browse files
committed
feat: add AI Code Generator mode with comprehensive guidelines and patterns
1 parent 38d8edf commit d5f9468

File tree

6 files changed

+1221
-0
lines changed

6 files changed

+1221
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<ai_code_generation_workflow>
2+
<overview>
3+
This workflow guides AI-assisted code generation following Google GenAI best practices,
4+
ensuring high-quality, maintainable, and well-tested code output.
5+
</overview>
6+
7+
<initialization_steps>
8+
<step number="1">
9+
<title>Understand Requirements</title>
10+
<description>
11+
Thoroughly analyze the user's request to understand the complete scope
12+
</description>
13+
<actions>
14+
<action>Parse the user's input to identify the core functionality needed</action>
15+
<action>Identify any constraints, performance requirements, or dependencies</action>
16+
<action>Determine the target programming language and framework</action>
17+
<action>Understand the integration points with existing code</action>
18+
</actions>
19+
<validation>
20+
<criterion>All functional requirements are clearly understood</criterion>
21+
<criterion>Non-functional requirements (performance, security) are identified</criterion>
22+
<criterion>Integration points and dependencies are mapped</criterion>
23+
</validation>
24+
</step>
25+
26+
<step number="2">
27+
<title>Analyze Existing Codebase</title>
28+
<description>
29+
Examine the current codebase to understand patterns and conventions
30+
</description>
31+
<tools>
32+
<tool>search_files - Find similar implementations and patterns</tool>
33+
<tool>list_code_definition_names - Understand project structure</tool>
34+
<tool>read_file - Examine existing code for patterns and conventions</tool>
35+
</tools>
36+
<analysis_points>
37+
<point>Coding style and naming conventions</point>
38+
<point>Error handling patterns</point>
39+
<point>Testing approaches and frameworks</point>
40+
<point>Documentation standards</point>
41+
<point>Architectural patterns in use</point>
42+
</analysis_points>
43+
</step>
44+
</initialization_steps>
45+
46+
<main_workflow>
47+
<phase name="planning">
48+
<description>Plan the implementation approach</description>
49+
<steps>
50+
<step>Design the API and interface contracts</step>
51+
<step>Identify reusable components and utilities</step>
52+
<step>Plan the testing strategy</step>
53+
<step>Consider error handling and edge cases</step>
54+
<step>Design for maintainability and extensibility</step>
55+
</steps>
56+
</phase>
57+
58+
<phase name="implementation">
59+
<description>Generate the code following best practices</description>
60+
<steps>
61+
<step>Create well-structured, modular code</step>
62+
<step>Implement comprehensive error handling</step>
63+
<step>Add meaningful documentation and comments</step>
64+
<step>Follow established patterns and conventions</step>
65+
<step>Ensure proper input validation and sanitization</step>
66+
</steps>
67+
</phase>
68+
69+
<phase name="testing">
70+
<description>Generate comprehensive test coverage</description>
71+
<steps>
72+
<step>Create unit tests for all public methods</step>
73+
<step>Add integration tests for complex workflows</step>
74+
<step>Test error conditions and edge cases</step>
75+
<step>Verify performance requirements are met</step>
76+
<step>Ensure security requirements are validated</step>
77+
</steps>
78+
</phase>
79+
80+
<phase name="validation">
81+
<description>Verify the implementation meets all requirements</description>
82+
<steps>
83+
<step>Run all tests to ensure functionality</step>
84+
<step>Verify code follows project conventions</step>
85+
<step>Check for potential security vulnerabilities</step>
86+
<step>Validate performance characteristics</step>
87+
<step>Ensure proper documentation is in place</step>
88+
</steps>
89+
</phase>
90+
</main_workflow>
91+
92+
<completion_criteria>
93+
<criterion>All functional requirements are implemented and tested</criterion>
94+
<criterion>Code follows established patterns and conventions</criterion>
95+
<criterion>Comprehensive test coverage is provided</criterion>
96+
<criterion>Error handling covers all identified edge cases</criterion>
97+
<criterion>Documentation is clear and complete</criterion>
98+
<criterion>Security best practices are followed</criterion>
99+
<criterion>Performance requirements are met</criterion>
100+
</completion_criteria>
101+
102+
<quality_gates>
103+
<gate name="code_review">
104+
<description>Self-review generated code for quality</description>
105+
<checklist>
106+
<item>Code is readable and well-structured</item>
107+
<item>Variable and function names are meaningful</item>
108+
<item>Complex logic is properly documented</item>
109+
<item>Error handling is comprehensive</item>
110+
<item>Security considerations are addressed</item>
111+
</checklist>
112+
</gate>
113+
114+
<gate name="testing">
115+
<description>Ensure comprehensive test coverage</description>
116+
<checklist>
117+
<item>All public methods have unit tests</item>
118+
<item>Edge cases and error conditions are tested</item>
119+
<item>Integration points are validated</item>
120+
<item>Performance tests are included where relevant</item>
121+
<item>Tests are maintainable and well-documented</item>
122+
</checklist>
123+
</gate>
124+
</quality_gates>
125+
</ai_code_generation_workflow>
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<ai_code_generation_best_practices>
2+
<overview>
3+
Best practices for AI-assisted code generation based on Google GenAI guidelines
4+
and industry standards for producing high-quality, maintainable code.
5+
</overview>
6+
7+
<general_principles>
8+
<principle priority="critical">
9+
<name>Code Clarity and Readability</name>
10+
<description>Generate code that is self-documenting and easy to understand</description>
11+
<rationale>Clear code reduces maintenance burden and improves team productivity</rationale>
12+
<guidelines>
13+
<guideline>Use descriptive variable and function names</guideline>
14+
<guideline>Keep functions focused on a single responsibility</guideline>
15+
<guideline>Add comments for complex business logic</guideline>
16+
<guideline>Follow consistent formatting and style conventions</guideline>
17+
</guidelines>
18+
</principle>
19+
20+
<principle priority="critical">
21+
<name>Defensive Programming</name>
22+
<description>Generate code that handles errors gracefully and validates inputs</description>
23+
<rationale>Robust error handling prevents runtime failures and improves user experience</rationale>
24+
<guidelines>
25+
<guideline>Validate all inputs at function boundaries</guideline>
26+
<guideline>Use appropriate error handling mechanisms for the language</guideline>
27+
<guideline>Provide meaningful error messages</guideline>
28+
<guideline>Handle edge cases explicitly</guideline>
29+
</guidelines>
30+
</principle>
31+
32+
<principle priority="high">
33+
<name>Test-Driven Development</name>
34+
<description>Generate comprehensive tests alongside implementation code</description>
35+
<rationale>Tests ensure correctness and enable safe refactoring</rationale>
36+
<guidelines>
37+
<guideline>Write tests for all public interfaces</guideline>
38+
<guideline>Test both success and failure scenarios</guideline>
39+
<guideline>Use meaningful test descriptions</guideline>
40+
<guideline>Ensure tests are deterministic and isolated</guideline>
41+
</guidelines>
42+
</principle>
43+
</general_principles>
44+
45+
<code_quality_standards>
46+
<standard category="naming">
47+
<rule>Use clear, descriptive names that express intent</rule>
48+
<examples>
49+
<good>calculateTotalPrice(items: Item[])</good>
50+
<good>isValidEmailAddress(email: string)</good>
51+
<bad>calc(x: any[])</bad>
52+
<bad>check(s: string)</bad>
53+
</examples>
54+
</standard>
55+
56+
<standard category="functions">
57+
<rule>Keep functions small and focused on a single responsibility</rule>
58+
<guidelines>
59+
<guideline>Aim for functions under 20-30 lines</guideline>
60+
<guideline>Extract complex logic into helper functions</guideline>
61+
<guideline>Use pure functions when possible</guideline>
62+
<guideline>Minimize side effects</guideline>
63+
</guidelines>
64+
</standard>
65+
66+
<standard category="error_handling">
67+
<rule>Implement comprehensive error handling</rule>
68+
<patterns>
69+
<pattern language="typescript">
70+
<description>Use Result types for operations that can fail</description>
71+
<example><![CDATA[
72+
type Result<T, E> = { success: true; data: T } | { success: false; error: E };
73+
74+
function parseJson<T>(json: string): Result<T, string> {
75+
try {
76+
const data = JSON.parse(json) as T;
77+
return { success: true, data };
78+
} catch (error) {
79+
return { success: false, error: error.message };
80+
}
81+
}
82+
]]></example>
83+
</pattern>
84+
</patterns>
85+
</standard>
86+
87+
<standard category="documentation">
88+
<rule>Provide clear documentation for public APIs</rule>
89+
<requirements>
90+
<requirement>Document function parameters and return values</requirement>
91+
<requirement>Explain complex algorithms or business logic</requirement>
92+
<requirement>Provide usage examples for non-trivial functions</requirement>
93+
<requirement>Document any side effects or preconditions</requirement>
94+
</requirements>
95+
</standard>
96+
</code_quality_standards>
97+
98+
<security_guidelines>
99+
<guideline priority="critical">
100+
<rule>Validate and sanitize all user inputs</rule>
101+
<rationale>Prevents injection attacks and data corruption</rationale>
102+
<implementation>
103+
<step>Use type-safe validation libraries</step>
104+
<step>Sanitize inputs before processing</step>
105+
<step>Use parameterized queries for database operations</step>
106+
<step>Escape output when rendering to prevent XSS</step>
107+
</implementation>
108+
</guideline>
109+
110+
<guideline priority="high">
111+
<rule>Follow principle of least privilege</rule>
112+
<rationale>Minimizes potential damage from security breaches</rationale>
113+
<implementation>
114+
<step>Grant minimal necessary permissions</step>
115+
<step>Use role-based access control</step>
116+
<step>Validate authorization at each access point</step>
117+
<step>Log security-relevant events</step>
118+
</implementation>
119+
</guideline>
120+
121+
<guideline priority="high">
122+
<rule>Protect sensitive data</rule>
123+
<rationale>Prevents data breaches and maintains user privacy</rationale>
124+
<implementation>
125+
<step>Encrypt sensitive data at rest and in transit</step>
126+
<step>Use secure random number generation</step>
127+
<step>Implement proper session management</step>
128+
<step>Avoid logging sensitive information</step>
129+
</implementation>
130+
</guideline>
131+
</security_guidelines>
132+
133+
<performance_considerations>
134+
<consideration category="algorithmic_efficiency">
135+
<rule>Choose appropriate algorithms and data structures</rule>
136+
<guidelines>
137+
<guideline>Understand time and space complexity</guideline>
138+
<guideline>Use efficient data structures for the use case</guideline>
139+
<guideline>Avoid premature optimization</guideline>
140+
<guideline>Profile before optimizing</guideline>
141+
</guidelines>
142+
</consideration>
143+
144+
<consideration category="resource_management">
145+
<rule>Manage resources efficiently</rule>
146+
<guidelines>
147+
<guideline>Close resources properly (files, connections, etc.)</guideline>
148+
<guideline>Use connection pooling for database access</guideline>
149+
<guideline>Implement proper caching strategies</guideline>
150+
<guideline>Avoid memory leaks</guideline>
151+
</guidelines>
152+
</consideration>
153+
</performance_considerations>
154+
155+
<common_pitfalls>
156+
<pitfall>
157+
<description>Generating overly complex solutions</description>
158+
<why_problematic>Complex code is harder to understand, test, and maintain</why_problematic>
159+
<correct_approach>Start with simple solutions and refactor when complexity is justified</correct_approach>
160+
</pitfall>
161+
162+
<pitfall>
163+
<description>Ignoring existing codebase patterns</description>
164+
<why_problematic>Inconsistent patterns make the codebase harder to navigate</why_problematic>
165+
<correct_approach>Analyze existing code to understand and follow established patterns</correct_approach>
166+
</pitfall>
167+
168+
<pitfall>
169+
<description>Insufficient error handling</description>
170+
<why_problematic>Unhandled errors lead to poor user experience and debugging difficulties</why_problematic>
171+
<correct_approach>Implement comprehensive error handling for all failure modes</correct_approach>
172+
</pitfall>
173+
174+
<pitfall>
175+
<description>Missing or inadequate tests</description>
176+
<why_problematic>Untested code is prone to bugs and difficult to refactor safely</why_problematic>
177+
<correct_approach>Generate comprehensive test coverage alongside implementation</correct_approach>
178+
</pitfall>
179+
</common_pitfalls>
180+
181+
<quality_checklist>
182+
<category name="before_implementation">
183+
<item>Requirements are clearly understood</item>
184+
<item>Existing patterns and conventions are identified</item>
185+
<item>API design is planned and reviewed</item>
186+
<item>Testing strategy is defined</item>
187+
</category>
188+
189+
<category name="during_implementation">
190+
<item>Code follows established patterns</item>
191+
<item>Error handling is comprehensive</item>
192+
<item>Security considerations are addressed</item>
193+
<item>Performance implications are considered</item>
194+
</category>
195+
196+
<category name="before_completion">
197+
<item>All tests pass</item>
198+
<item>Code is properly documented</item>
199+
<item>Security review is completed</item>
200+
<item>Performance requirements are met</item>
201+
</category>
202+
</quality_checklist>
203+
</ai_code_generation_best_practices>

0 commit comments

Comments
 (0)