Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions ui/src/components/ai-chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -529,14 +529,16 @@ function chatMessage(chat?: any, problem?: string, re_chat?: boolean, other_para
* @param row
*/
function getSourceDetail(row: any) {
logApi.getRecordDetail(id || props.appId, row.chat_id, row.record_id, loading).then((res) => {
const exclude_keys = ['answer_text', 'id', 'answer_text_list']
Object.keys(res.data).forEach((key) => {
if (!exclude_keys.includes(key)) {
row[key] = res.data[key]
}
if (row.record_id) {
logApi.getRecordDetail(id || props.appId, row.chat_id, row.record_id, loading).then((res) => {
const exclude_keys = ['answer_text', 'id', 'answer_text_list']
Object.keys(res.data).forEach((key) => {
if (!exclude_keys.includes(key)) {
row[key] = res.data[key]
}
})
})
})
}
return true
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code looks generally clean, but there are a few improvements that can be made:

  1. Check for loading: Before making network requests, always ensure that the loading indicator is false to avoid unnecessary API calls.

  2. Early Return on Null/Undefined Row: The function should check if the row object exists before proceeding with source detail retrieval to prevent errors.

  3. Simplify Code Structure: Combine or refactor repetitive parts of the code where possible to make it more concise and readable.

Here's an optimized version of the code:

function chatMessage(chat?: any, problem?: string, re_chat?: boolean, other_para: Record<string, unknown>): void {
  // Early return if no record ID provided or row is null/undefined
  if (!row?.record_id) {
    console.warn('No valid record for source detail');
    return;
  }

  logApi.getRecordDetail(id || props.appId, row.chat_id, row.record_id, loading)
    .then((res) => {
      const exclude_keys = ['answer_text', 'id', 'answer_text_list'];
      
      // Avoid potential side effects by using Object.assign or creating a new object
      const updatedRow = { ...row };
      
      Object.keys(res.data).forEach((key) => {
        if (!exclude_keys.includes(key)) {
          updatedRow[key] = res.data[key];
        }
      });

      // Update the original row
      Object.assign(row, updatedRow);
    })
    .catch((error) => {
      console.error("Error fetching Source Detail:", error.message);
      // You might want to handle this error accordingly
    });
}

Key Improvements:

  • Loading Check and Early Returns: Ensures that networking operations only proceed when necessary.
  • Null/Undefined Checks: Prevents runtime errors by checking for null or undefined objects.
  • Updated Assignment: Uses Object.assign() to safely update the original row object without mutating it directly.
  • Exception Handling: Simplifies handling of promise rejections.

These changes improve the reliability, efficiency, and readability of the code while maintaining functionality.

Expand Down