Agent Mode - Copilot Tự Động
1. Agent Mode là gì?
Agent Mode (còn gọi là Copilot Edits) là tính năng nâng cao cho phép Copilot:
- ✅ Edit nhiều files cùng lúc
- ✅ Tạo files mới trong project
- ✅ Chạy terminal commands
- ✅ Tự động fix khi có lỗi
- ✅ Thực hiện complex tasks end-to-end
So sánh các modes
| Feature | Autocomplete | Chat | Agent |
|---|---|---|---|
| Single file edit | ✅ | ✅ | ✅ |
| Multi-file edit | ❌ | ⚠️ Limited | ✅ |
| Create new files | ❌ | ❌ | ✅ |
| Run commands | ❌ | ❌ | ✅ |
| Auto-fix errors | ❌ | ❌ | ✅ |
| Complex refactoring | ❌ | ⚠️ | ✅ |
2. Kích hoạt Agent Mode
Cách 1: Từ Chat Panel
- Mở Copilot Chat (
Ctrl+Alt+I) - Ở góc trên, chọn dropdown "Chat"
- Chuyển sang "Edits" hoặc "Agent"
Cách 2: Command Palette
Ctrl+Shift+P- Tìm "Copilot: Start Agent Session"
Cách 3: Keyboard Shortcut
Ctrl+Shift+Alt+I- Mở trực tiếp Agent mode
3. Agent Mode Workflow
Agent Mode Flow
4. Use Cases cho Agent Mode
4.1 Scaffold New Features
1Create a new REST API endpoint for user profile:2- File: src/routes/profile.py3- GET /profile - Get current user profile4- PUT /profile - Update profile5- Add proper validation6- Add tests in tests/test_profile.pyAgent sẽ:
- Tạo file
profile.py - Implement endpoints
- Tạo file test
- Update
routes/__init__.pyđể register
4.2 Refactor Across Files
1Refactor the authentication system:2- Move auth logic from routes/users.py to services/auth_service.py3- Create AuthService class with login(), logout(), refresh_token() methods4- Update all files that import from routes/users.py5- Update tests accordingly4.3 Add Feature to Existing Codebase
1Add dark mode support to the React app:2- Create ThemeContext with light/dark modes3- Add toggle component4- Update all components to use theme colors5- Persist preference in localStorage4.4 Fix Bugs Across Multiple Files
1Fix the session timeout bug:2- The session expires after 5 minutes instead of 303- Check config files4- Check middleware5- Check frontend timeout settings6- Fix all related files5. Working with Agent Mode
Adding Context
Trong Agent mode, bạn có thể add files vào context:
1Add these files to context:2- src/models/user.py3- src/services/email_service.py4- src/routes/auth.py5 6Now, add email verification to the registration flowReviewing Changes
Agent sẽ show diff cho mỗi file:
1// src/models/user.py2 3class User:4 def __init__(self, name, email):5 self.name = name6 self.email = email7+ self.email_verified = False8+ self.verification_token = NoneOptions:
- Accept - Apply changes
- Reject - Discard changes
- Edit - Modify before accepting
Iterating
Sau khi review, bạn có thể yêu cầu thêm:
1Good, but also:2- Add expiration time for verification token3- Send welcome email after verification6. Terminal Integration
Agent có thể chạy terminal commands:
Example: Setup Project
1Set up a new Python project:2- Create virtual environment3- Install dependencies: flask, pytest, black4- Create requirements.txt5- Run initial testsAgent sẽ chạy:
1python -m venv venv2source venv/bin/activate # or venv\Scripts\activate on Windows3pip install flask pytest black4pip freeze > requirements.txt5pytestAuto-fix After Commands
Nếu command fail, Agent có thể:
- Analyze error
- Suggest fix
- Re-run command
7. Best Practices
DO ✅
-
Be specific about scope
Text1Update ONLY the UserService class, don't touch other files -
Provide context
Text1This is a Flask app using SQLAlchemy.2Database models are in src/models/ -
Review carefully
- Agent có thể miss edge cases
- Check logic, not just syntax
-
Iterate in small steps
Text1First: Add the model2Then: Add the route3Finally: Add tests
DON'T ❌
-
Don't blindly accept all
- Luôn review diff trước khi accept
-
Don't give conflicting instructions
Text1# Bad2Create a simple function, also make it handle all edge cases3and be production-ready with full logging -
Don't skip the plan review
- Agent shows plan trước khi execute
- Review plan để catch issues sớm
8. Agent Mode Limitations
Current Limitations
- ⚠️ May not understand complex business logic
- ⚠️ Can make mistakes with imports
- ⚠️ Sometimes creates circular dependencies
- ⚠️ May not follow project conventions perfectly
How to Handle
-
Provide conventions explicitly
Text1Follow these conventions:2- Use snake_case for functions3- Use PascalCase for classes4- Put all imports at top -
Review generated code
- Run linter
- Run tests
- Manual review
9. Hands-on Exercise
Challenge: Build Todo API với Agent Mode
Sử dụng Agent Mode để tạo một Todo API hoàn chỉnh:
Prompt:
1Create a complete Todo API with Flask:2 3Structure:4- src/5 - models/6 - todo.py (Todo model with SQLAlchemy)7 - routes/8 - todos.py (CRUD endpoints)9 - services/10 - todo_service.py (Business logic)11 - app.py (Main Flask app)12- tests/13 - test_todos.py14 15Requirements:16- Todo fields: id, title, description, completed, created_at, due_date17- Endpoints: GET /todos, POST /todos, GET /todos/<id>, PUT /todos/<id>, DELETE /todos/<id>18- Filter by completed status: GET /todos?completed=true19- Proper error handling with 404 for not found20- Input validation21 22Create all files with working code.Steps
- Open Agent Mode (
Ctrl+Shift+Alt+I) - Paste the prompt above
- Review the plan Agent provides
- Accept to execute
- Review each file's changes
- Test the API:
Bash1pip install flask flask-sqlalchemy2python src/app.py
- Iterate nếu cần fix bugs
Expected Files
1project/2├── src/3│ ├── __init__.py4│ ├── app.py5│ ├── models/6│ │ ├── __init__.py7│ │ └── todo.py8│ ├── routes/9│ │ ├── __init__.py10│ │ └── todos.py11│ └── services/12│ ├── __init__.py13│ └── todo_service.py14└── tests/15 ├── __init__.py16 └── test_todos.py10. What's New in Agent Mode
Recent Updates (2024-2025)
- Workspace awareness: Better understanding of project structure
- Improved planning: Shows detailed plan before execution
- Better error recovery: Auto-retry with fixes
- Terminal integration: Run commands and check output
- Multi-turn improvements: Better memory of previous changes
Coming Soon
- Image understanding (Copilot Vision)
- Voice commands
- Integration with GitHub Issues/PRs
Tiếp theo
Bài tiếp theo: Code Review với Copilot - sử dụng Copilot để review code quality, security, và best practices!
