Skip to content

Conversation

@AmberJc1
Copy link
Contributor

@AmberJc1 AmberJc1 commented Oct 9, 2025

Summary by CodeRabbit

  • 新功能
    • 接入外部AI服务,后端可生成更丰富的聊天回复
    • 聊天支持图片链接输入
    • 新增网页前端,提供输入框、发送按钮与消息列表,实时展示回复
  • 样式
    • 新增聊天界面样式,区分用户/AI气泡,优化布局与交互
  • 杂务
    • 完善前后端依赖与本地开发代理,支持跨域访问与联调
    • 引入基于环境的启动与数据库初始化流程,提升稳定性

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 9, 2025

Walkthrough

后端在 Chat 处理器中接入火山引擎接口,支持可选图片消息,解析并返回回答,同时持久化历史。主程序改为基于环境变量加载、初始化数据库与增加 CORS。新增基于 Vite+React 的前端与代理配置以对接后端 /api/v1。

Changes

Cohort / File(s) Summary
Chat 控制器集成 Volc
AmberJc1/controllers/chat.go
读取 VOLC 环境变量,构造 messages(含可选 image_url),POST 调用火山引擎接口,解析 choices→message→content 文本,保存问答历史并返回。保留鉴权与历史接口注释重排。
后端启动与依赖
AmberJc1/main.go, AmberJc1/go.mod
启动流程改为加载 .env 与系统环境、读取 PORT/DATABASE_URL、初始化数据库、注册 CORS 中间件与路由后启动;go.mod 将 Gin/JWT/GORM/godotenv 标为直接依赖。
前端应用骨架
AmberJc1/frontend/index.html, AmberJc1/frontend/src/main.jsx, AmberJc1/frontend/src/App.jsx, AmberJc1/frontend/src/components/Chat.jsx, AmberJc1/frontend/src/index.css
新增 React 应用:基础挂载、全局样式、简单聊天 UI 组件与发送逻辑,调用后端 POST /api/v1/chat 并显示回复。
前端构建与样式配置
AmberJc1/frontend/package.json, AmberJc1/frontend/vite.config.mjs, AmberJc1/frontend/tailwind.config.js, AmberJc1/frontend/postcss.config.js
配置 Vite+React 开发与构建,开发代理 /api→http://localhost:8080/api/v1;加入 Tailwind 与 PostCSS(autoprefixer)基础配置与扫描路径。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant U as 用户浏览器
  participant FE as 前端(React/Vite)
  participant BE as 后端(Gin)
  participant VK as 火山引擎API
  participant DB as 数据库

  U->>FE: 输入问题/图片URL
  FE->>BE: POST /api/v1/chat {question, image_url?}
  BE->>VK: POST /v1/chat/completions {messages}
  VK-->>BE: 200 {choices[0].message.content}
  BE->>DB: 保存{question, answer}
  DB-->>BE: OK
  BE-->>FE: 200 {reply: answer}
  FE-->>U: 显示 AI 回复

  rect rgba(230,240,255,0.5)
  note right of FE: 开发模式代理\n /api → /api/v1
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • whxxxxxxxxxx

Poem

小兔敲键轻轻跳,🌙
前端新芽随风俏;
代理指路到后端,
云上回声把话照。
图片一并送问候,🖼️
答复落袋史中靠。

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed 该标题简洁概括了本次PR的核心内容:接入火山引擎API并完善前端功能,与实际改动高度匹配且无冗余词汇,能够让团队成员快速理解主要更新。
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (6)
AmberJc1/frontend/package.json (1)

11-12: 考虑升级 React 到最新稳定版本

当前使用的 React 18.2.0 已过时。React 19.2.0 是最新稳定版本(2025年10月),提供了新的 UI 原语、性能改进和更好的 SSR 支持。建议升级以获得最新功能和性能优化。

基于学习记录

AmberJc1/frontend/src/index.css (1)

1-63: 样式定义功能完整

CSS 样式提供了清晰的聊天界面布局和视觉区分。考虑到项目已配置 Tailwind CSS,可以选择将这些自定义样式重构为 Tailwind 工具类,以提高可维护性和一致性。

AmberJc1/frontend/src/App.jsx (2)

9-27: 建议增强用户体验

当前实现功能正常,但可以通过以下改进提升用户体验:

  1. 添加加载状态: 在请求期间显示加载指示器
  2. 输入验证: 防止发送空消息
  3. 详细的错误反馈: 向用户显示更具体的错误信息

示例增强代码:

 function App() {
   const [input, setInput] = useState("");
   const [response, setResponse] = useState("");
+  const [loading, setLoading] = useState(false);

   const sendMessage = async () => {
+    if (!input.trim()) {
+      setResponse("请输入内容");
+      return;
+    }
+    setLoading(true);
     try {
       const res = await fetch("/api/v1/chat", {
         method: "POST",
         headers: { "Content-Type": "application/json" },
         body: JSON.stringify({ question: input }),
       });

       if (!res.ok) {
         throw new Error("HTTP " + res.status);
       }

       const data = await res.json();
       setResponse(data.reply || "无响应");
     } catch (error) {
       console.error("发送失败:", error);
-      setResponse("出错了");
+      setResponse(`请求失败: ${error.message}`);
+    } finally {
+      setLoading(false);
     }
   };

29-42: UI 渲染功能完整

组件的渲染逻辑正确,用户界面简洁明了。由于项目已配置 Tailwind CSS,可以考虑将内联样式替换为 Tailwind 工具类,以保持项目样式的一致性。

AmberJc1/frontend/src/components/Chat.jsx (1)

6-29: 建议阻止发送空消息

当前 handleSend 没有判空,用户连续敲回车会多次触发空请求,后端要么报错要么返回空响应,体验较差。建议在调用 onSendtrim() 判空(同时禁用按钮)。

-  const handleSend = () => {
-    onSend(input)
+  const handleSend = () => {
+    if (!input.trim()) return
+    onSend(input.trim())

若按钮也需要防抖,可一并处理。 -->

AmberJc1/controllers/chat.go (1)

160-175: 补齐关键错误处理

json.Marshalhttp.NewRequestio.ReadAll 目前直接忽略错误,一旦返回异常(例如内存不足、URL 非法、网络读失败)会继续执行并返回难以诊断的结果。建议显式检查并及时返回 500。

示例:

-	bodyBytes, _ := json.Marshal(reqBody)
+	bodyBytes, err := json.Marshal(reqBody)
+	if err != nil {
+		c.JSON(http.StatusInternalServerError, gin.H{"error": "构造请求失败", "detail": err.Error()})
+		return
+	}

http.NewRequestio.ReadAll 同理处理。 -->

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0275bb9 and 9480487.

⛔ Files ignored due to path filters (2)
  • AmberJc1/frontend/package-lock.json is excluded by !**/package-lock.json
  • AmberJc1/go.sum is excluded by !**/*.sum
📒 Files selected for processing (12)
  • AmberJc1/controllers/chat.go (8 hunks)
  • AmberJc1/frontend/index.html (1 hunks)
  • AmberJc1/frontend/package.json (1 hunks)
  • AmberJc1/frontend/postcss.config.js (1 hunks)
  • AmberJc1/frontend/src/App.jsx (1 hunks)
  • AmberJc1/frontend/src/components/Chat.jsx (1 hunks)
  • AmberJc1/frontend/src/index.css (1 hunks)
  • AmberJc1/frontend/src/main.jsx (1 hunks)
  • AmberJc1/frontend/tailwind.config.js (1 hunks)
  • AmberJc1/frontend/vite.config.mjs (1 hunks)
  • AmberJc1/go.mod (1 hunks)
  • AmberJc1/main.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (5)
AmberJc1/frontend/src/components/Chat.jsx (1)
AmberJc1/controllers/chat.go (1)
  • Chat (113-218)
AmberJc1/frontend/src/App.jsx (1)
AmberJc1/frontend/src/components/Chat.jsx (1)
  • input (4-4)
AmberJc1/go.mod (4)
Yyzfddsdf/MyGo/main.go (1)
  • main (25-151)
Yyzfddsdf/MyGo/models/user.go (2)
  • ID (35-40)
  • Model (10-19)
Yyzfddsdf/MyGo/middleware/auth_middleware.go (1)
  • AuthMiddleware (46-171)
Yyzfddsdf/MyGo/database/setup.go (1)
  • ConnectDB (16-49)
AmberJc1/main.go (2)
AmberJc1/config/config.go (1)
  • InitDB (11-21)
AmberJc1/routes/routes.go (1)
  • InitRoutes (9-22)
AmberJc1/controllers/chat.go (1)
AmberJc1/models/models.go (1)
  • Chat (13-19)
🔇 Additional comments (8)
AmberJc1/frontend/postcss.config.js (1)

1-6: PostCSS 配置正确

配置符合 Tailwind CSS 标准设置,插件配置正确。

AmberJc1/frontend/package.json (2)

14-19: 开发依赖配置合理

Vite、Tailwind CSS、PostCSS 和 Autoprefixer 的版本配置符合当前最佳实践。


10-10: @vitejs/plugin-react 5.0.4 已是最新稳定版本
npm registry 上 dist-tags.latest 指向 5.0.4,beta 和 alpha 分别指向 5.0.0-beta.0 和 3.0.0-alpha.2,无需回退至 4.x 系列。

Likely an incorrect or invalid review comment.

AmberJc1/frontend/index.html (1)

1-12: HTML 入口文件配置正确

HTML 结构符合 Vite + React 应用的标准配置,语言属性设置恰当,模块脚本引用正确。

AmberJc1/frontend/src/main.jsx (1)

1-10: React 应用初始化代码正确

使用 React 18 的 createRoot API 和 StrictMode 包装,符合最佳实践,能够在开发环境中及早发现潜在问题。

AmberJc1/frontend/tailwind.config.js (1)

1-8: Tailwind 配置符合标准

配置的 content 路径涵盖了 HTML 和源文件,确保 Tailwind 能够正确检测和生成所需的样式类。

AmberJc1/frontend/src/App.jsx (1)

1-5: 状态管理设置正确

组件使用 useState 管理输入和响应数据,符合 React 状态管理最佳实践。

AmberJc1/go.mod (1)

5-11: GORM 版本号已验证

gorm.io/gorm v1.31.0 在 GitHub Release 与 Go 代理中均可找到,是最新稳定版本。

Comment on lines +166 to +171
resp, err := http.DefaultClient.Do(req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "调用火山引擎失败", "detail": err.Error()})
return
}
defer resp.Body.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

为外部 HTTP 调用设置超时

http.DefaultClient 默认无超时,若火山接口网络抖动或响应挂起,请求会一直阻塞占用 Gin 处理线程,造成雪崩风险。建议自建 &http.Client{Timeout: ...} 或为请求设置 context.WithTimeout

-	resp, err := http.DefaultClient.Do(req)
+	client := &http.Client{Timeout: 10 * time.Second}
+	resp, err := client.Do(req)

按需调整超时并复用 client。 -->

🤖 Prompt for AI Agents
In AmberJc1/controllers/chat.go around lines 166 to 171, the code uses
http.DefaultClient.Do(req) which has no timeout; change it to use a client with
a timeout or perform the request with a context that has a deadline to avoid
blocking Gin handlers: create or reuse an &http.Client{Timeout: <reasonable
duration>} (or wrap the request with context.WithTimeout and pass
req.WithContext(ctx)), use that client to Do the request, handle the context
timeout error and still defer resp.Body.Close() only after a successful
response; ensure the client is reused (e.g., package-level variable) instead of
creating a new client per request if appropriate.


const sendMessage = async () => {
try {
const res = await fetch("http://localhost:8080/api/v1/chat", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

修复硬编码的 localhost URL

硬编码的 http://localhost:8080 会导致生产环境部署失败。根据 Vite 代理配置,应使用相对路径 /api/v1/chat,这样开发环境会通过代理转发,生产环境会使用实际部署的后端地址。

应用此修改:

-      const res = await fetch("http://localhost:8080/api/v1/chat", {
+      const res = await fetch("/api/v1/chat", {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const res = await fetch("http://localhost:8080/api/v1/chat", {
const res = await fetch("/api/v1/chat", {
🤖 Prompt for AI Agents
In AmberJc1/frontend/src/App.jsx around line 11, the fetch call uses a hardcoded
backend URL "http://localhost:8080/api/v1/chat" which breaks production; change
it to a relative path "/api/v1/chat" so Vite dev proxy handles local forwarding
and production will call the deployed backend (or alternatively read BASE_API
from env and use `${BASE_API}/api/v1/chat`), then test in dev and production
builds.

Comment on lines +57 to +61
log.Printf("🚀 服务已启动,监听端口 %s ...", port)
if err := r.Run(fmt.Sprintf(":%s", port)); err != nil {
log.Fatalf("❌ 启动失败: %v", err)
}
fmt.Println("API Key:", os.Getenv("ARK_API_KEY"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

不要在日志中输出 API 密钥

fmt.Println("API Key:", os.Getenv("ARK_API_KEY")) 会把密钥写入日志,属于敏感信息泄露风险(可能落盘、被采集或出现在监控中)。请删除这行或改成只输出配置是否存在。

-	fmt.Println("API Key:", os.Getenv("ARK_API_KEY"))

如需诊断,可改为记录布尔状态,例如 log.Printf("ARK_API_KEY loaded: %t", os.Getenv("ARK_API_KEY") != "")。 -->

🤖 Prompt for AI Agents
In AmberJc1/main.go around lines 57 to 61, remove the sensitive output
fmt.Println("API Key:", os.Getenv("ARK_API_KEY")) because it logs the secret;
instead log only a non-sensitive existence check (e.g. log whether ARK_API_KEY
is loaded) or delete the line entirely; ensure no other code prints environment
secrets to stdout/stderr.

@whxxxxxxxxxx whxxxxxxxxxx added this pull request to the merge queue Oct 9, 2025
Merged via the queue into hduhelp:main with commit cc0e02d Oct 9, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants