# 🚀 Epic: Merge MCP-SuperAssistant vào n8n AI Agent

## 📌 Context
- **Status:** ✅ Done
- **Feasibility:** 🟢 POSSIBLE (7.5/10)
- **Source:** `D:\cnf\n8n_ai_agent\preference\MCP-SuperAssistant`
- **Target:** `D:\cnf\n8n_ai_agent\src\superassistant\`

## 🎯 Goal
Khi user mở ChatGPT trên Chrome, Extension tự nhận diện → inject sidebar + render MCP tool blocks → user bấm Run → McpService gọi n8n API trực tiếp.

## 📋 Execution Checklist

### Phase 1: Foundation — Logger + Types + Config
- [x] Task 1.1: Tạo `src/superassistant/logger.ts` — wrapper logger thay `@extension/shared/lib/logger`
- [x] Task 1.2: Tạo `src/superassistant/types.ts` — port core types từ render_prescript + adapter types
- [x] Task 1.3: Tạo `src/superassistant/config.ts` — port FunctionCallRendererConfig + per-site selectors

### Phase 2: Parser — Function Call Extraction
- [x] Task 2.1: Port parser → `src/superassistant/parser.ts` (consolidated: JSON + XML detection, parameter extraction, language stripping)
- [x] Task 2.2: Port observer logic → integrated into `renderer/index.ts` (MutationObserver + scanAndRender)
- [x] Task 2.3: Port utils → integrated into parser.ts (extractJSONObjects, extractCleanContent)

### Phase 3: Renderer — UI Components
- [x] Task 3.1: Port `renderer/styles.ts` — premium dark/light theme CSS
- [x] Task 3.2: Port `renderer/index.ts` — addExecuteButton, addRawXmlToggle, renderFunctionBlock
- [x] Task 3.3: DOM observer — scanAndRender(), startObserver(), stopObserver()
- [x] Task 3.4: Result display — success/error panels with timing

### Phase 4: MCP Bridge — Kết nối McpService
- [x] Task 4.1: Tạo `src/superassistant/bridge.ts` — expose McpService via `window.__n8nMcpBridge`
- [x] Task 4.2: chrome.runtime.sendMessage relay (content ↔ background)
- [x] Task 4.3: Update `bg/index.ts` — add "n8n-mcp:call-tool" handler with sendResponse

### Phase 5: Adapter — ChatGPT Detection
- [x] Task 5.1: Tạo `src/superassistant/adapters.ts` — BaseAdapter + ChatGPTAdapter + GeminiAdapter
- [x] Task 5.2: insertText/submitForm methods per platform (DOM selectors, contenteditable handling)
- [x] Task 5.3: detectAdapter() registry — auto-detect by hostname

### Phase 6: Content Script — Injection
- [x] Task 6.1: Tạo `src/superassistant/index.ts` — main entry (init adapter + bridge + observer)
- [x] Task 6.2: Update `manifest.ts` — add content_scripts for 9 AI platforms
- [x] Task 6.3: Build verification — `npm run build` passes ✅

### Phase 7: Polish (Future)
- [ ] Task 7.1: Error handling cho connection lost / MCP server down
- [ ] Task 7.2: Insert result text back into AI chat for continued conversation
- [ ] Task 7.3: Add more adapters (Grok, DeepSeek, Mistral, Kimi)
