fix: AI会话对接

This commit is contained in:
Shu Guang 2025-05-17 21:10:09 +08:00
parent 5249bdcd75
commit eb6eee1294

View File

@ -138,14 +138,29 @@ export default {
this.addMessage("```javascript\n" + this.code + "\n```"); this.addMessage("```javascript\n" + this.code + "\n```");
try { try {
const response = await this.mockAiResponse(this.code); const response = await AiChat({
this.addMessage(response, "ai"); 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) { } catch (error) {
console.error("AI 响应错误:", error); console.error("AI 响应错误:", error);
this.addMessage("发生错误,请稍后重试。", "ai");
} finally { } finally {
this.loading = false; this.loading = false;
} }
}, },
async sendTextMessage() { async sendTextMessage() {
if (!this.inputMessage.trim()) return; if (!this.inputMessage.trim()) return;
@ -153,19 +168,29 @@ export default {
this.addMessage(this.inputMessage); this.addMessage(this.inputMessage);
try { try {
const response = await this.mockAiResponse(this.inputMessage); const response = await AiChat({
this.addMessage(response, "ai"); 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 = ""; this.inputMessage = "";
} catch (error) { } catch (error) {
console.error("AI 响应错误:", error); console.error("AI 响应错误:", error);
this.addMessage("发生错误,请稍后重试。", "ai");
} finally { } finally {
this.loading = false; 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() { clearCode() {
if (this.editor) { if (this.editor) {
this.editor.setValue("// 在这里输入你的代码\n"); this.editor.setValue("// 在这里输入你的代码\n");
@ -177,7 +202,7 @@ export default {
<style scoped> <style scoped>
.ai-assistant { .ai-assistant {
padding: 24px; /* padding: 24px; */
background: #f0f2f5; background: #f0f2f5;
min-height: calc(100vh - 64px); min-height: calc(100vh - 64px);
} }
@ -187,11 +212,43 @@ export default {
height: calc(100vh - 112px); height: calc(100vh - 112px);
display: flex; display: flex;
flex-direction: column; flex-direction: column;
overflow: hidden;
}
.chat-container {
flex: 1;
overflow-y: auto;
padding: 16px;
background: #fff;
border: 1px solid #e8e8e8;
border-radius: 4px;
margin-bottom: 16px;
max-height: calc(100vh - 200px); /* 添加最大高度 */
scrollbar-width: thin;
scrollbar-color: rgba(0, 0, 0, 0.3) transparent;
}
/* 滚动条样式 */
.chat-container::-webkit-scrollbar {
width: 6px;
}
.chat-container::-webkit-scrollbar-track {
background: transparent;
}
.chat-container::-webkit-scrollbar-thumb {
background-color: rgba(0, 0, 0, 0.3);
border-radius: 3px;
}
.chat-container::-webkit-scrollbar-thumb:hover {
background-color: rgba(0, 0, 0, 0.5);
} }
.editor-container { .editor-container {
height: calc(100% - 60px); height: calc(100% - 60px);
min-height: 300px; min-height: 70vh;
} }
.editor-actions { .editor-actions {
@ -232,11 +289,12 @@ export default {
background: #e6f7ff; background: #e6f7ff;
padding: 12px; padding: 12px;
border-radius: 8px; border-radius: 8px;
word-break: break-word;
} }
.message.user .message-content { .message.user .message-content {
background: #1890ff; background: #000; /* 修改用户消息背景色 */
color: white; color: #ffffff; /* 修改用户消息文字颜色 */
} }
.message-time { .message-time {
@ -246,7 +304,7 @@ export default {
} }
.message.user .message-time { .message.user .message-time {
color: #e6f7ff; color: rgba(255, 255, 255, 0.8); /* 修改用户消息时间颜色 */
} }
:deep(.message-text pre) { :deep(.message-text pre) {
@ -254,10 +312,28 @@ export default {
padding: 16px; padding: 16px;
border-radius: 4px; border-radius: 4px;
overflow-x: auto; overflow-x: auto;
margin: 8px 0; /* 添加上下间距 */
} }
/* 修改用户消息中的代码块样式 */
.message.user :deep(.message-text pre) { .message.user :deep(.message-text pre) {
background: rgba(255, 255, 255, 0.1); background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
color: #ffffff; /* 修改代码文字颜色 */
}
/* 修改用户消息中的行内代码样式 */
.message.user :deep(.message-text code) {
background: rgba(255, 255, 255, 0.15);
color: #ffffff;
padding: 2px 6px;
border-radius: 4px;
}
/* 修改用户消息中的链接样式 */
.message.user :deep(.message-text a) {
color: #ffffff;
text-decoration: underline;
} }
.chat-input { .chat-input {