Skip to content

Commit 0d7e6d2

Browse files
Olasunkanmi OyinlolaOlasunkanmi Oyinlola
authored andcommitted
feat(codebase-analysis): Add codebase analysis and recommendation command
- Implements a new command to analyze the codebase and answer user questions. - Adds a new file that handles the logic for codebase analysis. - Integrates to perform comprehensive analysis including frameworks, dependencies, API endpoints, data models, and database schema. - Enhances the to increase the output token limit and reduce aggressive stop sequences for better response completeness. - Includes a debug truncation test guide () to provide manual testing steps to check if the truncation has been resolved.
1 parent c0be2f2 commit 0d7e6d2

File tree

14 files changed

+2153
-45
lines changed

14 files changed

+2153
-45
lines changed

debug-truncation-test.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Response Truncation Fix - Testing Guide
2+
3+
## 🛠️ What Was Implemented
4+
5+
### 1. Enhanced Truncation Detection
6+
- Detects responses ending with `**API`, `**API `, or similar patterns
7+
- Identifies incomplete numbered lists, headers, and markdown formatting
8+
- Catches suspiciously short responses (< 50 chars) or short responses with bold formatting
9+
10+
### 2. Intelligent Retry Mechanism
11+
- Automatically retries when truncation is detected
12+
- Uses fresh chat session to avoid context issues
13+
- Returns the longer response if both are incomplete
14+
- Fallback to original response if retry fails
15+
16+
### 3. Improved Generation Configuration
17+
- Increased `maxOutputTokens` to 8192 (doubled)
18+
- Removed aggressive stop sequences that might cause premature truncation
19+
- Optimized temperature and other parameters for completeness
20+
21+
### 4. Explicit Completion Instructions
22+
- Added clear instructions to AI about completing responses
23+
- Enhanced prompts in both chat interface and command palette
24+
25+
## 🧪 Testing the Fix
26+
27+
### Test Case 1: Chat Interface
28+
Ask a question that previously caused truncation:
29+
```
30+
"Can you explain the API design patterns used in this codebase?"
31+
```
32+
33+
### Test Case 2: Command Palette
34+
1. Press `Cmd+Shift+P` (Mac)
35+
2. Type "CodeBuddy: Analyze Codebase"
36+
3. Ask: "What are the main API endpoints and their structures?"
37+
38+
### Test Case 3: Specific Truncation Pattern
39+
Ask questions that might trigger the `**API` truncation:
40+
```
41+
"Show me all the API endpoints and explain their functionality"
42+
"What are the main API patterns and best practices used?"
43+
```
44+
45+
## 📊 Debug Information
46+
47+
The system now logs detailed debug information in the Developer Console:
48+
49+
1. **Open VS Code Developer Console**:
50+
- Help → Toggle Developer Tools → Console tab
51+
52+
2. **Look for these debug messages**:
53+
- `[DEBUG] Response length: X characters`
54+
- `[DEBUG] Response ends with: "..."`
55+
- `[DEBUG] Response seems incomplete, attempting retry...`
56+
- `[DEBUG] Retry response length: X characters`
57+
58+
## 🎯 Expected Behavior
59+
60+
**Before Fix**: Responses cutting off at `**API`
61+
**After Fix**: Complete responses with automatic retry if truncation detected
62+
63+
## 🔍 If Issues Persist
64+
65+
If you still see truncation:
66+
1. Check the Developer Console for debug logs
67+
2. Note the exact pattern where truncation occurs
68+
3. The system should automatically retry and provide a complete response
69+
70+
## 📝 Additional Notes
71+
72+
- The retry mechanism adds ~2-3 seconds to response time when truncation is detected
73+
- Debug logging can be removed once the issue is confirmed fixed
74+
- The enhanced detection covers multiple truncation patterns beyond just `**API`

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@
9696
"when": "editorHasSelection",
9797
"command": "CodeBuddy.generateMermaidDiagram",
9898
"group": "CodeBuddy"
99+
},
100+
{
101+
"command": "CodeBuddy.codebaseAnalysis",
102+
"group": "CodeBuddy"
99103
}
100104
]
101105
},
@@ -135,6 +139,10 @@
135139
{
136140
"command": "CodeBuddy.generateMermaidDiagram",
137141
"title": "CodeBuddy. Generate Mermaid diagram."
142+
},
143+
{
144+
"command": "CodeBuddy.codebaseAnalysis",
145+
"title": "CodeBuddy. Analyze Codebase & Answer Questions"
138146
}
139147
],
140148
"viewsContainers": {

src/application/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export enum CODEBUDDY_ACTIONS {
1717
inlineChat = "CodeBuddy.inLineChat",
1818
restart = "CodeBuddy.restart",
1919
reviewPR = "CodeBuddy.reviewPR",
20+
codebaseAnalysis = "CodeBuddy.codebaseAnalysis",
2021
}
2122
export enum COMMON {
2223
GROQ_CHAT_HISTORY = "groqChatHistory",
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import * as vscode from "vscode";
2+
import { CodebaseUnderstandingService } from "../services/codebase-understanding.service";
3+
import { GeminiLLM } from "../llms/gemini/gemini";
4+
import { getAPIKeyAndModel } from "../utils/utils";
5+
6+
export const architecturalRecommendationCommand = async () => {
7+
const codebaseUnderstandingService =
8+
CodebaseUnderstandingService.getInstance();
9+
const apiKeyInfo = getAPIKeyAndModel("gemini");
10+
11+
if (!apiKeyInfo.apiKey || !apiKeyInfo.model) {
12+
vscode.window.showErrorMessage("Gemini API key or model not configured.");
13+
return;
14+
}
15+
16+
const gemini = new GeminiLLM({
17+
apiKey: apiKeyInfo.apiKey,
18+
model: apiKeyInfo.model,
19+
});
20+
21+
const question = await vscode.window.showInputBox({
22+
prompt: "What would you like to know about this codebase?",
23+
placeHolder:
24+
"e.g., How is authentication handled? What are the API endpoints? I want to build an admin dashboard, what APIs do I need?",
25+
});
26+
27+
if (!question) {
28+
return;
29+
}
30+
31+
await vscode.window.withProgress(
32+
{
33+
location: vscode.ProgressLocation.Notification,
34+
title: "Analyzing codebase to answer your question...",
35+
cancellable: false,
36+
},
37+
async (progress) => {
38+
progress.report({ increment: 0 });
39+
40+
// Get comprehensive codebase context (not for webview, so use direct file links)
41+
const context =
42+
await codebaseUnderstandingService.getCodebaseContext(false);
43+
progress.report({ increment: 50 });
44+
45+
const prompt = `
46+
You are a senior software architect and full-stack developer with extensive experience analyzing codebases. Based on the comprehensive codebase analysis below, provide detailed, accurate answers to the user's question.
47+
48+
**Comprehensive Codebase Analysis:**
49+
${context}
50+
51+
**User's Question:**
52+
${question}
53+
54+
**Instructions:**
55+
1. **Analyze the question type**:
56+
- If it's about HOW something is implemented (e.g., "How is authentication handled?"), focus on explaining existing patterns, files, and implementations
57+
- If it's about BUILDING something new (e.g., "I want to build an admin dashboard"), provide architectural recommendations and implementation guidance
58+
- If it's about UNDERSTANDING the codebase (e.g., "What are the main components?"), provide clear explanations of the architecture and structure
59+
60+
2. **For implementation questions** ("How is X handled?"):
61+
- Explain the existing patterns found in the codebase
62+
- Reference specific files and code patterns
63+
- Describe the flow and architecture
64+
- Mention any dependencies or frameworks involved
65+
66+
3. **For building/enhancement questions**:
67+
- Provide specific, actionable recommendations
68+
- Suggest exact endpoint paths, data models, and implementation approach
69+
- Consider the existing architecture and recommend consistent patterns
70+
- Include code examples where helpful
71+
72+
4. **For general understanding questions**:
73+
- Provide clear overviews of the codebase structure
74+
- Explain the main architectural patterns
75+
- Describe key components and their relationships
76+
77+
5. **Always include**:
78+
- Specific file references using the clickable links provided (e.g., [[1]], [[2]], etc.)
79+
- Concrete examples from the actual codebase
80+
- Clear, actionable next steps
81+
- When referencing files or components, use the provided clickable references like [[1]](file_path) so users can navigate directly to the source
82+
83+
**Focus on being accurate, specific, and helpful. Use the actual codebase analysis to provide concrete, evidence-based answers.**
84+
85+
**CRITICAL: Provide a complete response. Do not truncate your answer. Ensure your response is fully finished and does not end abruptly mid-sentence or mid-word.**
86+
`.trim();
87+
88+
const result = await gemini.generateText(prompt);
89+
progress.report({ increment: 100 });
90+
91+
const panel = vscode.window.createWebviewPanel(
92+
"codebaseAnalysis",
93+
"Codebase Analysis & Recommendations",
94+
vscode.ViewColumn.One,
95+
{},
96+
);
97+
98+
panel.webview.html = result;
99+
},
100+
);
101+
};

0 commit comments

Comments
 (0)