package main import ( "log" "mrcc/internal/config" "mrcc/internal/httpserver" "mrcc/internal/logger" "mrcc/pkg/response" "github.com/gin-gonic/gin" ) func main() { cfg := config.Load("ai-service", "8087") logger.WithService(cfg.Name).Info("starting AI assistant service", "port", cfg.Port) srv := httpserver.New(cfg) // AI 助搭 API 路由 api := srv.Group("/api/ai") { // 聊天接口 - 流式返回 (SSE) api.POST("/chat", handleChat) // 电路分析 - 分析当前电路并返回诊断报告 api.POST("/analyze", handleAnalyze) // 电路建议 - 根据需求生成搭建方案 api.POST("/suggest", handleSuggest) // 操作执行 - AI 规划的操作序列 (客户端确认后调用) api.POST("/execute", handleExecute) // 聊天历史 api.GET("/history", handleHistory) // 用户反馈 api.POST("/feedback", handleFeedback) } log.Printf("ai-service listening on %s (endpoints: /api/ai/*)", cfg.Address()) if err := srv.Run(cfg.Address()); err != nil { log.Fatal(err) } } // === 请求/响应结构 === type ChatRequest struct { SessionID string `json:"sessionId"` Message string `json:"message"` Context GameContext `json:"context"` } type GameContext struct { Mode string `json:"mode"` CanvasWidth int `json:"canvasWidth"` CanvasHeight int `json:"canvasHeight"` ComponentCount int `json:"componentCount"` SelectedComponent string `json:"selectedComponent"` LevelID *int `json:"levelId"` LevelTitle string `json:"levelTitle"` RedstoneCoins int64 `json:"redstoneCoins"` } type ChatResponse struct { Type string `json:"type"` // text | analysis | action | error Content string `json:"content"` Actions []AIAction `json:"actions"` RequiresConfirmation bool `json:"requiresConfirmation"` } type AIAction struct { Type string `json:"type"` // place | delete | move | rotate | wire Component string `json:"component"` // 元件类型 (place 时) X int `json:"x"` Y int `json:"y"` FromX int `json:"fromX"` // move/wire 时 FromY int `json:"fromY"` // move/wire 时 Rotation int `json:"rotation"` Description string `json:"description"` // 供用户确认时显示 } type AnalysisReport struct { Summary string `json:"summary"` Metrics AnalysisMetrics `json:"metrics"` Issues []AnalysisIssue `json:"issues"` Optimizations []AnalysisOptimization `json:"optimizations"` } type AnalysisMetrics struct { ComponentCount int `json:"componentCount"` TotalCost int `json:"totalCost"` MaxDelay int `json:"maxDelay"` SignalPaths int `json:"signalPaths"` } type AnalysisIssue struct { Severity string `json:"severity"` // error | warning | info Type string `json:"type"` // signal_loss | short_circuit | redundant LocationX int `json:"locationX"` LocationY int `json:"locationY"` Description string `json:"description"` Suggestion string `json:"suggestion"` } type AnalysisOptimization struct { Type string `json:"type"` Description string `json:"description"` EstimatedImprovement string `json:"estimatedImprovement"` } // === 处理函数 === func handleChat(c *gin.Context) { var req ChatRequest if err := c.ShouldBindJSON(&req); err != nil { response.Error(c, 400, "invalid request: "+err.Error()) return } // TODO: 实现 LLM 调用链 // 1. 意图识别 (问答 / 助搭 / 分析 / 操作) // 2. RAG 检索相关知识 // 3. 调用 LLM API (GPT-4o / Claude / 国产模型) // 4. 解析 LLM 输出,组装响应 // 骨架响应 response.OK(c, ChatResponse{ Type: "text", Content: "AI 助搭服务正在开发中。您的消息已收到:" + req.Message, }) } func handleAnalyze(c *gin.Context) { // TODO: 接收电路数据,运行本地电路分析引擎 response.OK(c, AnalysisReport{ Summary: "分析功能开发中", Metrics: AnalysisMetrics{}, }) } func handleSuggest(c *gin.Context) { // TODO: 根据用户需求生成电路搭建方案 response.OK(c, gin.H{ "plans": []gin.H{}, }) } func handleExecute(c *gin.Context) { // TODO: 记录 AI 操作日志,用于审计 response.OK(c, gin.H{"status": "logged"}) } func handleHistory(c *gin.Context) { // TODO: 从 PostgreSQL 查询用户聊天历史 response.OK(c, gin.H{ "messages": []gin.H{}, "total": 0, }) } func handleFeedback(c *gin.Context) { // TODO: 记录用户反馈 (赞/踩),用于改进 AI 质量 response.OK(c, gin.H{"status": "recorded"}) }