Skip to content

Commit 8d7d20d

Browse files
committed
improve AI context
1 parent e6e8d4a commit 8d7d20d

File tree

3 files changed

+346
-5
lines changed

3 files changed

+346
-5
lines changed

.github/docs-gen-prompts.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,37 @@ These section headers from `copilot-instructions.md` are appended to the system
4242

4343
Given this module documentation from the Compose diamond proxy framework, enhance it by generating developer-grade content that is specific, actionable, and faithful to the provided contract data.
4444

45+
**CRITICAL: Use the EXACT function signatures, import paths, and storage information provided below. Do not invent or modify function names, parameter types, or import paths.**
46+
4547
1. **description**: A concise one-line description (max 100 chars) for the page subtitle. Derive from the module's purpose based on its functions and NatSpec. Do NOT include "module" or "for Compose diamonds" - just describe what it does.
4648
2. **overview**: 2-3 sentence overview of what the module does and why it matters for diamonds (storage reuse, composition, safety).
47-
3. **usageExample**: 10-20 lines of Solidity demonstrating how a facet would import and call this module. Use the real function names and signatures; include pragma and any required imports. Keep it minimal but compilable.
49+
3. **usageExample**: 10-20 lines of Solidity demonstrating how a facet would import and call this module. Use the EXACT import path provided ({{importPath}}), EXACT function signatures from the Function Signatures section below, and pragma version {{pragmaVersion}}. Keep it minimal but compilable.
4850
4. **bestPractices**: 2-3 bullets focused on safe and idiomatic use (access control, storage hygiene, upgrade awareness, error handling).
49-
5. **integrationNotes**: Explain how the module interacts with diamond storage and how changes are visible to facets; note any invariants or ordering requirements.
51+
5. **integrationNotes**: Explain how the module interacts with diamond storage and how changes are visible to facets; note any invariants or ordering requirements. Reference the storage information provided below.
5052
6. **keyFeatures**: 2-4 bullets highlighting unique capabilities, constraints, or guarantees.
5153

5254
Contract Information:
5355
- Name: {{title}}
5456
- Current Description: {{description}}
57+
- Import Path: {{importPath}}
58+
- Pragma Version: {{pragmaVersion}}
5559
- Functions: {{functionNames}}
60+
- Function Signatures:
61+
{{functionSignatures}}
5662
- Events: {{eventNames}}
63+
- Event Signatures:
64+
{{eventSignatures}}
5765
- Errors: {{errorNames}}
66+
- Error Signatures:
67+
{{errorSignatures}}
5868
- Function Details:
5969
{{functionDescriptions}}
70+
- Storage Information:
71+
{{storageContext}}
72+
- Related Contracts:
73+
{{relatedContracts}}
74+
- Struct Definitions:
75+
{{structDefinitions}}
6076

6177
Respond ONLY with valid JSON in this exact format (no markdown code blocks, no extra text):
6278
{
@@ -74,21 +90,37 @@ Respond ONLY with valid JSON in this exact format (no markdown code blocks, no e
7490

7591
Given this facet documentation from the Compose diamond proxy framework, enhance it by generating precise, implementation-ready guidance.
7692

93+
**CRITICAL: Use the EXACT function signatures, import paths, and storage information provided below. Do not invent or modify function names, parameter types, or import paths.**
94+
7795
1. **description**: A concise one-line description (max 100 chars) for the page subtitle. Derive from the facet's purpose based on its functions and NatSpec. Do NOT include "facet" or "for Compose diamonds" - just describe what it does.
7896
2. **overview**: 2-3 sentence summary of the facet's purpose and value inside a diamond (routing, orchestration, surface area).
79-
3. **usageExample**: 10-20 lines showing how this facet is deployed or invoked within a diamond. Include pragma, imports, selector usage, and sample calls that reflect the real function names and signatures.
97+
3. **usageExample**: 10-20 lines showing how this facet is deployed or invoked within a diamond. Use the EXACT import path provided ({{importPath}}), EXACT function signatures from the Function Signatures section below, pragma version {{pragmaVersion}}, and sample calls that reflect the real function names and signatures.
8098
4. **bestPractices**: 2-3 bullets on correct integration patterns (initialization, access control, storage handling, upgrade safety).
8199
5. **securityConsiderations**: Concise notes on access control, reentrancy, input validation, and any state-coupling risks specific to this facet.
82100
6. **keyFeatures**: 2-4 bullets calling out unique abilities, constraints, or guarantees.
83101

84102
Contract Information:
85103
- Name: {{title}}
86104
- Current Description: {{description}}
105+
- Import Path: {{importPath}}
106+
- Pragma Version: {{pragmaVersion}}
87107
- Functions: {{functionNames}}
108+
- Function Signatures:
109+
{{functionSignatures}}
88110
- Events: {{eventNames}}
111+
- Event Signatures:
112+
{{eventSignatures}}
89113
- Errors: {{errorNames}}
114+
- Error Signatures:
115+
{{errorSignatures}}
90116
- Function Details:
91117
{{functionDescriptions}}
118+
- Storage Information:
119+
{{storageContext}}
120+
- Related Contracts:
121+
{{relatedContracts}}
122+
- Struct Definitions:
123+
{{structDefinitions}}
92124

93125
Respond ONLY with valid JSON in this exact format (no markdown code blocks, no extra text):
94126
{

.github/scripts/generate-docs-utils/ai-enhancement.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
const fs = require('fs');
77
const path = require('path');
88
const ai = require('../ai-provider');
9+
const {
10+
extractSourceContext,
11+
computeImportPath,
12+
formatFunctionSignatures,
13+
formatStorageContext,
14+
formatRelatedContracts,
15+
formatStructDefinitions,
16+
formatEventSignatures,
17+
formatErrorSignatures,
18+
} = require('./context-extractor');
19+
const { getContractRegistry } = require('./contract-registry');
920

1021
const AI_PROMPT_PATH = path.join(__dirname, '../../docs-gen-prompts.md');
1122
const REPO_INSTRUCTIONS_PATH = path.join(__dirname, '../../copilot-instructions.md');
@@ -154,6 +165,33 @@ function buildPrompt(data, contractType) {
154165
const eventNames = (data.events || []).map(e => e.name).join(', ');
155166
const errorNames = (data.errors || []).map(e => e.name).join(', ');
156167

168+
// Extract additional context
169+
const sourceContext = extractSourceContext(data.sourceFilePath);
170+
const importPath = computeImportPath(data.sourceFilePath);
171+
const functionSignatures = formatFunctionSignatures(data.functions);
172+
const eventSignatures = formatEventSignatures(data.events);
173+
const errorSignatures = formatErrorSignatures(data.errors);
174+
const structDefinitions = formatStructDefinitions(data.structs);
175+
176+
// Get storage context
177+
const storageContext = formatStorageContext(
178+
data.storageInfo,
179+
data.structs,
180+
data.stateVariables
181+
);
182+
183+
// Get related contracts context
184+
const registry = getContractRegistry();
185+
// Try to get category from registry entry, or use empty string
186+
const registryEntry = registry.byName.get(data.title);
187+
const category = data.category || (registryEntry ? registryEntry.category : '');
188+
const relatedContracts = formatRelatedContracts(
189+
data.title,
190+
contractType,
191+
category,
192+
registry
193+
);
194+
157195
const promptTemplate = contractType === 'module'
158196
? AI_PROMPTS.modulePrompt
159197
: AI_PROMPTS.facetPrompt;
@@ -166,7 +204,15 @@ function buildPrompt(data, contractType) {
166204
.replace(/\{\{functionNames\}\}/g, functionNames || 'None')
167205
.replace(/\{\{functionDescriptions\}\}/g, functionDescriptions || ' None')
168206
.replace(/\{\{eventNames\}\}/g, eventNames || 'None')
169-
.replace(/\{\{errorNames\}\}/g, errorNames || 'None');
207+
.replace(/\{\{errorNames\}\}/g, errorNames || 'None')
208+
.replace(/\{\{functionSignatures\}\}/g, functionSignatures || 'None')
209+
.replace(/\{\{eventSignatures\}\}/g, eventSignatures || 'None')
210+
.replace(/\{\{errorSignatures\}\}/g, errorSignatures || 'None')
211+
.replace(/\{\{importPath\}\}/g, importPath || 'N/A')
212+
.replace(/\{\{pragmaVersion\}\}/g, sourceContext.pragmaVersion || '^0.8.30')
213+
.replace(/\{\{storageContext\}\}/g, storageContext || 'None')
214+
.replace(/\{\{relatedContracts\}\}/g, relatedContracts || 'None')
215+
.replace(/\{\{structDefinitions\}\}/g, structDefinitions || 'None');
170216
}
171217

172218
// Fallback to hardcoded prompt if template not loaded
@@ -176,7 +222,7 @@ function buildPrompt(data, contractType) {
176222
177223
2. **overview**: A clear, concise overview (2-3 sentences) explaining what this ${contractType} does and why it's useful in the context of diamond contracts.
178224
179-
3. **usageExample**: A practical Solidity code example (10-20 lines) showing how to use this ${contractType}. For modules, show importing and calling functions. For facets, show how it would be used in a diamond.
225+
3. **usageExample**: A practical Solidity code example (10-20 lines) showing how to use this ${contractType}. For modules, show importing and calling functions. For facets, show how it would be used in a diamond. Use the EXACT import path and function signatures provided below.
180226
181227
4. **bestPractices**: 2-3 bullet points of best practices for using this ${contractType}.
182228
@@ -189,11 +235,24 @@ ${contractType === 'facet' ? '5. **securityConsiderations**: Important security
189235
Contract Information:
190236
- Name: ${data.title}
191237
- Current Description: ${data.description || 'No description provided'}
238+
- Import Path: ${importPath || 'N/A'}
239+
- Pragma Version: ${sourceContext.pragmaVersion || '^0.8.30'}
192240
- Functions: ${functionNames || 'None'}
241+
- Function Signatures:
242+
${functionSignatures || ' None'}
193243
- Events: ${eventNames || 'None'}
244+
- Event Signatures:
245+
${eventSignatures || ' None'}
194246
- Errors: ${errorNames || 'None'}
247+
- Error Signatures:
248+
${errorSignatures || ' None'}
195249
- Function Details:
196250
${functionDescriptions || ' None'}
251+
${storageContext && storageContext !== 'None' ? `\n- Storage Information:\n${storageContext}` : ''}
252+
${relatedContracts && relatedContracts !== 'None' ? `\n- Related Contracts:\n${relatedContracts}` : ''}
253+
${structDefinitions && structDefinitions !== 'None' ? `\n- Struct Definitions:\n${structDefinitions}` : ''}
254+
255+
IMPORTANT: Use the EXACT function signatures, import paths, and storage information provided above. Do not invent or modify function names, parameter types, or import paths.
197256
198257
Respond ONLY with valid JSON in this exact format (no markdown code blocks, no extra text):
199258
{

0 commit comments

Comments
 (0)