Skip to content

Commit 82ca7df

Browse files
committed
feat: Enhance context gathering for AI suggestions with project-wide awareness
1 parent da83a3b commit 82ca7df

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Local AI Copilot for VS Code
22

3+
34
A VS Code extension that provides GitHub Copilot-like functionality using locally running Ollama models.
45

6+
Now with **smart project-wide context**: The extension automatically gathers code and documentation from your workspace for every AI request, making suggestions and edits much more intelligent and Copilot-like.
7+
58
## Features
69

710
- 🤖 **Chat Interface** - Interactive AI chat panel similar to GitHub Copilot
@@ -12,6 +15,10 @@ A VS Code extension that provides GitHub Copilot-like functionality using locall
1215
- 🎨 **Beautiful UI** - Modern interface that matches VS Code's theme
1316
- 🚀 **Multiple Models** - Choose between different Ollama models
1417

18+
- 🧠 **Project-Wide Context Awareness** – AI suggestions and edits use key files from your workspace (README, SETUP, main, server, extension, `.py`, `.ts`, `.js`, `.md`).
19+
- 🔍 **Intelligent Suggestions & Edits** – AI can reason about your whole project, not just the current file or cursor.
20+
- 🛠️ **Smart Code Actions** – Insert, replace, and preview code with context-aware suggestions and diff previews.
21+
1522
## Prerequisites
1623

1724
1. **Ollama** - Install from [https://ollama.ai](https://ollama.ai)
@@ -59,6 +66,14 @@ Then press `F5` to launch the extension in a new VS Code window.
5966

6067
### Chat Interface
6168

69+
## Smart Project Context
70+
71+
- The extension automatically gathers context from up to 5 key files in your workspace (README, SETUP, main, server, extension, `.py`, `.ts`, `.js`, `.md`) for every AI request.
72+
- It combines this with the current editor's context and selection.
73+
- The AI will now be able to reason about your whole project, not just the current file or cursor.
74+
75+
This makes code suggestions, edits, and completions much smarter and more Copilot-like!
76+
6277
1. Open Command Palette (`Ctrl+Shift+P` or `Cmd+Shift+P`)
6378
2. Type "Local AI Copilot: Chat"
6479
3. Start chatting with your AI!
@@ -89,6 +104,13 @@ Choose your preferred model from the dropdown in the chat panel:
89104
2. **Python Server**: Handles communication with Ollama
90105
3. **Ollama**: Runs the AI models locally on your machine
91106

107+
**Advanced:**
108+
109+
When you ask for code generation, editing, or explanation, the extension:
110+
1. Collects relevant code and documentation from your workspace.
111+
2. Combines it with your current selection or file context.
112+
3. Sends this rich context to the AI backend for smarter, project-aware results.
113+
92114
```
93115
VS Code Extension (TypeScript)
94116

vscode-extension/src/extension.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,25 @@ async function handleChatMessage(message: any) {
169169
let context = '';
170170
let taskType = 'chat';
171171

172-
// Smart context gathering
172+
// Project-wide smart context gathering
173+
const workspaceFolders = vscode.workspace.workspaceFolders;
174+
let projectContext = '';
175+
if (workspaceFolders && workspaceFolders.length > 0) {
176+
const folder = workspaceFolders[0].uri.fsPath;
177+
// Gather up to 5 key files (README, setup, main, server, extension, etc.)
178+
const keyFiles = fs.readdirSync(folder)
179+
.filter(f => /README|SETUP|main|server|extension|\.py$|\.ts$|\.js$|\.md$/i.test(f))
180+
.slice(0, 5);
181+
for (const file of keyFiles) {
182+
try {
183+
const filePath = path.join(folder, file);
184+
const fileContent = fs.readFileSync(filePath, 'utf8');
185+
projectContext += `\n\n--- File: ${file} ---\n` + fileContent.slice(0, 2000); // Limit size per file
186+
} catch (e) { /* skip unreadable files */ }
187+
}
188+
}
189+
190+
// Smart context from current editor
173191
if (editor) {
174192
const selection = editor.selection;
175193
const selectedText = editor.document.getText(selection);
@@ -189,6 +207,11 @@ async function handleChatMessage(message: any) {
189207
}
190208
}
191209

210+
// Combine project context and editor context
211+
if (projectContext) {
212+
context = (context ? context : '') + '\n\n--- Project Context ---\n' + projectContext;
213+
}
214+
192215
const response = await callAI(message.prompt, taskType, context, message.model);
193216
console.log('AI response from server:', response);
194217

0 commit comments

Comments
 (0)