Skip to content

fix: AI conversation jumps to 404#3118

Merged
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_chat
May 21, 2025
Merged

fix: AI conversation jumps to 404#3118
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_chat

Conversation

@shaohuzhang1
Copy link
Contributor

fix: AI conversation jumps to 404

@shaohuzhang1 shaohuzhang1 merged commit a7e31b9 into main May 21, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_chat branch May 21, 2025 02:58
}
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.

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.

1 participant