-
Notifications
You must be signed in to change notification settings - Fork 339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: AI chat request error #1059
Conversation
WalkthroughThe pull request involves two main changes: a modification to the AI response handling logic in Changes
Possibly related PRs
Suggested reviewers
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/plugins/robot/src/Main.vue (3)
234-235
: LGTM! Consider adding defensive programming.The simplified response extraction aligns with the backend changes and fixes the reported issue. However, consider adding defensive checks to handle potential undefined properties or malformed responses.
- const responseMessage = getAiRespMessage(originalResponse.role, originalResponse.content) - const respDisplayMessage = getAiRespMessage(originalResponse.role, replyWithoutCode) + const { role = 'assistant', content = '' } = originalResponse || {} + const responseMessage = getAiRespMessage(role, content) + const respDisplayMessage = getAiRespMessage(role, replyWithoutCode || '')
Line range hint
233-259
: Enhance error handling with specific error cases.The current error handling only covers connection failures. Consider adding specific error handling for different failure scenarios to improve the user experience.
.then((res) => { + if (!res || typeof res !== 'object') { + throw new Error('Invalid response format') + } const { originalResponse, schema, replyWithoutCode } = res const responseMessage = getAiRespMessage(originalResponse.role, originalResponse.content) const respDisplayMessage = getAiRespMessage(originalResponse.role, replyWithoutCode) sessionProcess.messages.push(responseMessage) sessionProcess.displayMessages.push(respDisplayMessage) messages.value[messages.value.length - 1].content = replyWithoutCode setContextSession() if (schema?.schema) { createNewPage(schema.schema) } inProcesing.value = false connectedFailed.value = false }) - .catch(() => { - messages.value[messages.value.length - 1].content = '连接失败' + .catch((error) => { + let errorMessage = '连接失败' + if (error.response) { + switch (error.response.status) { + case 429: + errorMessage = '请求过于频繁,请稍后再试' + break + case 408: + errorMessage = '请求超时,请重试' + break + default: + errorMessage = error.response.data?.message || '服务器错误' + } + } else if (error.message === 'Invalid response format') { + errorMessage = '响应格式错误' + } + messages.value[messages.value.length - 1].content = errorMessage localStorage.removeItem('aiChat') inProcesing.value = false connectedFailed.value = false })
233-233
: Optimize request timeout configuration.The current 10-minute timeout (600000ms) is excessive and could lead to poor user experience. Consider reducing it and implementing a retry mechanism.
+ const MAX_RETRIES = 3 + const TIMEOUT = 60000 // 60 seconds + const retryRequest = async (retries = 0) => { getMetaApi(META_SERVICE.Http) - .post('/app-center/api/ai/chat', getSendSeesionProcess(), { timeout: 600000 }) + .post('/app-center/api/ai/chat', getSendSeesionProcess(), { timeout: TIMEOUT }) .then((res) => { // ... existing success handling }) .catch((error) => { + if (retries < MAX_RETRIES && error.code === 'ECONNABORTED') { + messages.value[messages.value.length - 1].content = `请求超时,正在重试 (${retries + 1}/${MAX_RETRIES})...` + return retryRequest(retries + 1) + } // ... existing error handling }) + } + retryRequest()
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/plugins/robot/src/Main.vue
(1 hunks)packages/plugins/robot/src/js/robotSetting.js
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- packages/plugins/robot/src/js/robotSetting.js
🔇 Additional comments (1)
packages/plugins/robot/src/Main.vue (1)
234-235
: Add test coverage for the response handling changes.The changes fix the immediate issue, but lack test coverage. Consider adding tests for:
- New response structure handling
- Error cases and recovery
- Timeout and retry mechanism
Would you like me to help create a test suite for these scenarios?
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
AI聊天框,第一次对话成功回复后,接着第二次对话会报错。
问题根因:后端返回的数据结构变更 tiny-engine-webservice commit 2993c4a。后端直接取了
choices[0].message
里面的内容解决方法:适配后端返回的新的数据结构。直接去掉前端的
choices[0].message
What is the current behavior?
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
UI Updates
Code Improvements