Luồng hoạt động
Tìm hiểu chi tiết về cách Galaxy CLI xử lý các tình huống khác nhau từ input đến output.
Tổng quan Workflow
Phần tiêu đề “Tổng quan Workflow”Galaxy CLI có 3 workflows chính:
- Create Project - Tạo dự án mới từ đầu
- Update Project - Thêm feature vào dự án hiện có
- Q&A Mode - Trả lời câu hỏi trực tiếp
1. Create Project Workflow
Phần tiêu đề “1. Create Project Workflow”Flow Chart
Phần tiêu đề “Flow Chart”User: "Tạo app Next.js e-commerce" ↓┌─────────────────────────────────────┐│ Orchestrator: Phân tích yêu cầu ││ → Gọi ba_it_analyze() │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ BA Agent: Analyze Requirements ││ - Xác định type: create_project ││ - Define tech stack ││ - List core features ││ - Create data models │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Orchestrator: Present Analysis (VI) ││ "📊 Phân tích dự án..." ││ "Bạn đồng ý?" │└─────────────────────────────────────┘ ↓User: "yes, làm đi" ↓┌─────────────────────────────────────┐│ Orchestrator: Gọi plan_task() │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Planning Agent: Create Plan ││ - Map features → steps ││ - Select tools ││ - Add test/review steps if enabled │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Orchestrator: Present Plan ││ "📋 Implementation Plan:" ││ "☐ Step 1: Initialize..." ││ "☐ Step 2: Implement Feature A" │└─────────────────────────────────────┘ ↓User: "yes" (or auto-execute) ↓┌─────────────────────────────────────┐│ Orchestrator: Execute Steps ││ Loop through each step: │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Step 1: command_run ││ → npx create-next-app... ││ ☒ Completed │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Step 2: install_dependencies ││ → Auto-detect & install ││ ☒ Completed │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Step 3: code_generate ││ Feature: Product Catalog ││ → Code Gen Agent executes ││ → Generates files ││ ☒ Completed (created 4 files) │└─────────────────────────────────────┘ ↓... (repeat for each feature) ↓┌─────────────────────────────────────┐│ Orchestrator: Summary ││ "✅ All steps completed!" ││ "Created 42 files" │└─────────────────────────────────────┘Detailed Steps
Phần tiêu đề “Detailed Steps”-
User Input Processing
orchestrator.handleUserInput("Tạo app Next.js e-commerce"); -
BA Analysis
const analysis = await ba_it_analyze({userRequest: "Tạo app Next.js e-commerce",currentContext: { projectPath: process.cwd() }});// Returns:{projectName: "Next.js E-commerce",type: "create_project",coreFeatures: [...],technicalStack: {...},dataModels: [...],apiEndpoints: [...]} -
User Confirmation
Orchestrator presents analysis và chờ user confirm.
-
Planning
const plan = await plan_task({userContext: "Tạo app Next.js e-commerce",baAnalysis: analysis,systemContext: {testEnabled: false,reviewEnabled: false}});// Returns:{steps: [{ step: 1, tool: 'command_run', action: 'Initialize' },{ step: 2, tool: 'install_dependencies' },{ step: 3, tool: 'code_generate', featureName: 'Feature A' },...]} -
Execution Loop
for (const step of plan.steps) {await orchestrator.executeStep(step);updateProgress(step.step, 'completed');}
2. Update Project Workflow
Phần tiêu đề “2. Update Project Workflow”Flow Chart
Phần tiêu đề “Flow Chart”User: "Thêm authentication vào dự án" ↓┌─────────────────────────────────────┐│ Orchestrator: Phân tích context ││ → Scan project files ││ → Gọi ba_it_analyze() │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ BA Agent: Analyze Feature ││ - type: update_project ││ - Analyze existing structure ││ - Plan integration points │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Orchestrator: Present Analysis │└─────────────────────────────────────┘ ↓User confirms ↓┌─────────────────────────────────────┐│ Planning Agent: Create Plan ││ - Skip project initialization ││ - Focus on feature implementation ││ - Add integration steps │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Execute Steps ││ - Update existing files ││ - Create new files ││ - Integrate with existing code │└─────────────────────────────────────┘Key Differences from Create Project
Phần tiêu đề “Key Differences from Create Project”steps = [ { step: 1, tool: 'command_run', action: 'Initialize project' }, { step: 2, tool: 'install_dependencies' }, { step: 3, tool: 'code_generate', featureName: 'Feature A' }, ...]steps = [ // ❌ NO initialization steps { step: 1, tool: 'code_generate', featureName: 'Authentication' }, { step: 2, tool: 'file_write', path: 'middleware.ts' }, { step: 3, tool: 'git_commit' }]3. Q&A Mode Workflow
Phần tiêu đề “3. Q&A Mode Workflow”Simple Questions
Phần tiêu đề “Simple Questions”User: "React hooks là gì?" ↓┌─────────────────────────────────────┐│ Orchestrator: Direct Answer ││ - No BA analysis needed ││ - No planning needed ││ - Use knowledge to answer │└─────────────────────────────────────┘ ↓Response: [Detailed explanation]Code Questions
Phần tiêu đề “Code Questions”User: "Giải thích code này: [paste code]" ↓┌─────────────────────────────────────┐│ Orchestrator: Analyze Code ││ - Parse code structure ││ - Identify patterns ││ - Explain functionality │└─────────────────────────────────────┘ ↓Response: [Code explanation]Decision Tree
Phần tiêu đề “Decision Tree”Orchestrator quyết định workflow dựa trên input:
function decideWorkflow(userInput: string) { // Q&A Mode if (isQuestion(userInput)) { return 'qa_mode'; }
// Create Project if (isNewProjectRequest(userInput)) { return 'create_project'; }
// Update Project if (isFeatureRequest(userInput)) { return 'update_project'; }
// Analysis if (isAnalysisRequest(userInput)) { return 'analyze_project'; }}
function isNewProjectRequest(input: string): boolean { const patterns = [ /tạo.*app/i, /create.*app/i, /build.*project/i, /khởi tạo.*dự án/i ]; return patterns.some(p => p.test(input));}
function isFeatureRequest(input: string): boolean { const patterns = [ /thêm.*feature/i, /add.*feature/i, /implement/i, /tích hợp/i ]; return patterns.some(p => p.test(input));}State Management
Phần tiêu đề “State Management”Progress State
Phần tiêu đề “Progress State”interface ProgressState { projectName: string; currentStep: number; totalSteps: number; steps: Array<{ step: number; action: string; status: 'pending' | 'in-progress' | 'completed' | 'failed'; }>;}Conversation State
Phần tiêu đề “Conversation State”interface ConversationState { messages: Message[]; currentPhase: 'analysis' | 'planning' | 'execution' | 'complete'; pendingConfirmation?: { type: 'ba_analysis' | 'plan'; data: any; };}Error Handling
Phần tiêu đề “Error Handling”Retry Strategy
Phần tiêu đề “Retry Strategy”async function executeStepWithRetry( step: Step, maxRetries: number = 3): Promise<Result> { let attempt = 0;
while (attempt < maxRetries) { try { const result = await executeStep(step); return result; } catch (error) { attempt++;
if (attempt >= maxRetries) { return { status: 'failed', error: error.message }; }
// Exponential backoff await sleep(1000 * Math.pow(2, attempt)); } }}Fallback Workflow
Phần tiêu đề “Fallback Workflow”Step fails ↓Retry (max 3 times) ↓Still fails? ↓┌─────────────────────────────────────┐│ Orchestrator: Ask user ││ "Step 3 failed. Options:" ││ 1. Retry ││ 2. Skip ││ 3. Abort │└─────────────────────────────────────┘Parallel Execution
Phần tiêu đề “Parallel Execution”Một số steps có thể chạy song song:
// Sequential (default)await executeStep(step1);await executeStep(step2);await executeStep(step3);
// Parallel (when safe)await Promise.all([ executeStep(step1), // Independent executeStep(step2), // Independent executeStep(step3) // Independent]);Tool Execution Flow
Phần tiêu đề “Tool Execution Flow”File Write Example
Phần tiêu đề “File Write Example”code_generate tool called ↓┌─────────────────────────────────────┐│ Code Gen Agent ││ 1. Generate code ││ 2. Call file_write tool │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ file_write tool ││ 1. Check if file exists ││ 2. Create directories if needed ││ 3. Write content ││ 4. Calculate diff │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Return result to Code Gen Agent ││ { isNewFile, linesAdded, ... } │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Code Gen Agent aggregates results ││ { filesCreated: [...] } │└─────────────────────────────────────┘ ↓┌─────────────────────────────────────┐│ Return to Orchestrator ││ Update progress: ☒ Step N completed │└─────────────────────────────────────┘Real-time Updates
Phần tiêu đề “Real-time Updates”Galaxy CLI cập nhật UI real-time:
Progress Indicators
Phần tiêu đề “Progress Indicators”// Update todosupdateTodoStatus(stepNumber, 'in-progress');// ☐ → ⚙
// Tool executionshowToolExecution('file_write', 'app.tsx');// 📝 APPLY PATCH (app.tsx)
// CompletionupdateTodoStatus(stepNumber, 'completed');// ⚙ → ☒Streaming Responses
Phần tiêu đề “Streaming Responses”// LLM responses stream token-by-tokenfor await (const chunk of llmStream) { process.stdout.write(chunk);}Context Management
Phần tiêu đề “Context Management”Context Window
Phần tiêu đề “Context Window”interface Context { conversation: Message[]; // Recent messages project: ProjectInfo; // Current project progress: ProgressState; // Execution state config: UserConfig; // Settings}
// Context pruning (keep recent + important)function pruneContext(context: Context) { return { conversation: context.conversation.slice(-10), project: context.project, progress: context.progress, config: context.config };}Performance Optimization
Phần tiêu đề “Performance Optimization”Caching Strategy
Phần tiêu đề “Caching Strategy”// Cache BA analysis resultsconst cacheKey = hash(userRequest);const cached = cache.get(cacheKey);
if (cached && !expired(cached)) { return cached.result;}
const result = await ba_it_analyze(userRequest);cache.set(cacheKey, result, ttl: 3600);Lazy Loading
Phần tiêu đề “Lazy Loading”// Tools loaded on-demandasync function getTool(name: string) { if (!loadedTools[name]) { loadedTools[name] = await import(`./tools/${name}`); } return loadedTools[name];}Các bước tiếp theo
Phần tiêu đề “Các bước tiếp theo”- Architecture - Kiến trúc tổng thể
- AI Models - Chi tiết về AI agents
- Tools - Danh sách tools