diff --git a/src/views/user/Agent.vue b/src/views/user/Agent.vue index 6cc39b75..2b2cd29c 100644 --- a/src/views/user/Agent.vue +++ b/src/views/user/Agent.vue @@ -138,14 +138,29 @@ export default { this.addMessage("```javascript\n" + this.code + "\n```"); try { - const response = await this.mockAiResponse(this.code); - this.addMessage(response, "ai"); + const response = await AiChat({ + userId: this.$store.state.user.userId, + question: this.code, + competitionType: "编程", + }); + + if ( + response.code === 200 && + response.data && + response.data.code === 200 + ) { + this.addMessage(response.data.data, "ai"); + } else { + this.addMessage("AI 助手暂时无法回复,请稍后再试。", "ai"); + } } catch (error) { console.error("AI 响应错误:", error); + this.addMessage("发生错误,请稍后重试。", "ai"); } finally { this.loading = false; } }, + async sendTextMessage() { if (!this.inputMessage.trim()) return; @@ -153,19 +168,29 @@ export default { this.addMessage(this.inputMessage); try { - const response = await this.mockAiResponse(this.inputMessage); - this.addMessage(response, "ai"); + const response = await AiChat({ + userId: this.$store.state.user.userId, + question: this.inputMessage, + competitionType: "编程", + }); + + if ( + response.code === 200 && + response.data && + response.data.code === 200 + ) { + this.addMessage(response.data.data, "ai"); + } else { + this.addMessage("AI 助手暂时无法回复,请稍后再试。", "ai"); + } this.inputMessage = ""; } catch (error) { console.error("AI 响应错误:", error); + this.addMessage("发生错误,请稍后重试。", "ai"); } finally { this.loading = false; } }, - async mockAiResponse(input) { - await new Promise((resolve) => setTimeout(resolve, 1000)); - return '这是 AI 助手的回复,实际开发时替换为真实的 AI 接口调用。\n\n```javascript\nconsole.log("Hello from AI");\n```'; - }, clearCode() { if (this.editor) { this.editor.setValue("// 在这里输入你的代码\n"); @@ -177,7 +202,7 @@ export default {