Skip to content
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

Multiple utterances #1114

Merged
merged 6 commits into from
Jan 10, 2024

Conversation

maheshsattala
Copy link
Contributor

@maheshsattala maheshsattala commented Jan 9, 2024

Added code for sending multiple utterances from pyscript action.

Summary by CodeRabbit

  • Refactor

    • Improved bot response handling with enhanced text and JSON support.
  • Tests

    • Added integration tests for Python script actions with various response types.

Copy link

coderabbitai bot commented Jan 9, 2024

Walkthrough

Walkthrough

The update involves refactoring the logic for bot responses in a Python project, streamlining the response handling into two distinct functions for text and JSON. Additionally, the system's response dispatching mechanism has been enhanced. New integration tests have been added to ensure the updated Python script actions work correctly with multiple utterances and integer responses.

Changes

File Path Change Summary
.../actions/utils.py Refactored bot response handling and added new dispatcher function.
.../integration_test/... Added tests for script action execution with multiple utterances.

🐇✨
To code anew, we hop with glee,
Refactoring's done, more bugs to flee.
Tests twirl in, a dance of check,
Our bot responds, without a speck. 🌟🤖

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?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 695d084 and 5b2b5b5.
Files selected for processing (2)
  • kairon/shared/actions/utils.py (1 hunks)
  • tests/integration_test/action_service_test.py (1 hunks)
Additional comments: 4
kairon/shared/actions/utils.py (1)
  • 558-562: The changes to the handle_utter_bot_response method correctly handle the case where bot_response is a list by combining the list items into a single string separated by newlines. This aligns with the PR's objective to allow sending multiple utterances from a Python script action.
tests/integration_test/action_service_test.py (3)
  • 130-130: The assertion for the custom response structure seems correct, but ensure that the actual implementation of the handle_utter_bot_response function supports this format.

  • 131-207: The test case test_pyscript_action_execution_with_multiple_utterances is well-structured. However, there are a few points to consider:

  • Ensure that the Actions and PyscriptActionConfig classes handle the creation and saving of actions correctly.
  • The responses.add method is mocked correctly, but verify that the endpoint and the expected response match the actual service behavior.
  • The assertion on line 206 checks if the response is a single string with newline characters. Confirm that this is the expected format for the bot's response.
  • 209-285: The test case test_pyscript_action_execution_with_multiple_integer_utterances is similar to the previous one, with the following observations:
  • The setup and mocking are consistent with the previous test.
  • The assertion on line 284 checks if the response is a single string with newline characters separating the numbers. Verify that this is the intended behavior, as it may not be the most user-friendly format for numeric responses.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5b2b5b5 and 05da24e.
Files selected for processing (2)
  • kairon/shared/actions/utils.py (1 hunks)
  • tests/integration_test/action_service_test.py (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • kairon/shared/actions/utils.py
  • tests/integration_test/action_service_test.py

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 05da24e and a4b4c8b.
Files selected for processing (1)
  • kairon/shared/actions/utils.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • kairon/shared/actions/utils.py

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a4b4c8b and 07eb2e9.
Files selected for processing (1)
  • kairon/shared/actions/utils.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • kairon/shared/actions/utils.py

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 07eb2e9 and 7fbce0f.
Files selected for processing (1)
  • tests/integration_test/action_service_test.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/integration_test/action_service_test.py

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7fbce0f and f22ded2.
Files selected for processing (1)
  • kairon/shared/actions/utils.py (1 hunks)
Additional comments: 2
kairon/shared/actions/utils.py (2)
  • 547-582: The handle_utter_bot_response function has been refactored to use handle_text_response and handle_json_response to handle different types of bot responses. The set_dispatcher_response function has been added to set the dispatcher response based on the dispatch type. This refactoring improves the modularity and readability of the code. However, it's important to ensure that the dispatcher's behavior remains consistent with these changes.
Verification successful

The executed scripts have successfully verified the usage of the new set_dispatcher_response function and the refactored handle_text_response and handle_json_response functions within the kairon/shared/actions/utils.py file. The results show that these functions are being called, indicating that the refactoring has been integrated into the codebase as intended.

Based on the script outputs, we can confirm that the changes made to the utils.py file are consistent with the rest of the codebase, and the dispatcher's behavior has been updated to use the new utility functions for handling responses.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the dispatcher's behavior is consistent with the changes made to the response handling functions.
# This script checks for the usage of the dispatcher's utter_message method with json_message and text parameters.
rg --type python 'dispatcher.utter_message' -- '-json_message' '-text'

Length of output: 97



Script:

#!/bin/bash
# Verify that the dispatcher's behavior is consistent with the changes made to the response handling functions.
# This script checks for the usage of the dispatcher's utter_message method with json_message and text parameters.
rg 'dispatcher.utter_message' -- '-json_message' '-text'

Length of output: 152



Script:

#!/bin/bash
# Verify that the dispatcher's behavior is consistent with the changes made to the response handling functions.
# This script checks for the usage of the dispatcher's utter_message method, excluding lines that contain 'json_message' or 'text'.
rg 'dispatcher.utter_message' | rg -v 'json_message|text'

Length of output: 1176



Script:

#!/bin/bash
# Verify the usage of the new set_dispatcher_response function and the refactored handle_text_response and handle_json_response functions.
rg 'ActionUtility.set_dispatcher_response' 'kairon/shared/actions/utils.py'
rg 'ActionUtility.handle_text_response' 'kairon/shared/actions/utils.py'
rg 'ActionUtility.handle_json_response' 'kairon/shared/actions/utils.py'

Length of output: 904

* 560-568: The `handle_text_response` method has been added to handle text responses. It checks if the `bot_response` is a list and iterates over it, dispatching each message appropriately. This method enhances the ability to handle multiple messages in a single response, which aligns with the PR's objective to enable sending multiple utterances from a Python script action.

Comment on lines +571 to +582
def handle_json_response(dispatcher: CollectingDispatcher, bot_response: Any):
from json import JSONDecodeError

message = None
try:
bot_response = json.loads(bot_response) if isinstance(bot_response, str) else bot_response
ActionUtility.set_dispatcher_response(dispatcher, bot_response, DispatchType.json.value)
except JSONDecodeError as e:
message = f'Failed to convert http response to json: {str(e)}'
logger.exception(e)
ActionUtility.set_dispatcher_response(dispatcher, bot_response, DispatchType.text.value)
return message, bot_response
Copy link

Choose a reason for hiding this comment

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

Given the absence of direct references to the handle_json_response method in the test files, it appears that there are no specific test cases for this method, or they do not call the method directly. This could indicate a gap in the test coverage for the handle_json_response method.

Analysis chain

The handle_json_response method has been added to handle JSON responses. It attempts to parse the bot_response as JSON and dispatches it. If parsing fails, it logs the exception and dispatches the response as text. This method should be tested to ensure that it handles various JSON structures and parsing errors gracefully.


I will correct the script to search for the handle_json_response method without specifying the file type, allowing ripgrep to automatically detect Python files.


To further verify the handling of various JSON structures and parsing errors by the handle_json_response method, I will search for test cases that may be invoking this method.


Given the absence of direct references to the handle_json_response method in the test files, I will now search for indirect references or patterns that might indicate testing of JSON response handling, such as tests that check for JSON parsing errors or dispatching logic.


To determine if the handle_json_response method is indirectly tested, I will examine the action_test.py file more closely, looking for tests that might involve JSON response handling.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the handle_json_response method handles various JSON structures and parsing errors gracefully.
# This script checks for the usage of the handle_json_response method and suggests creating test cases for different JSON structures.
rg --type python 'handle_json_response'

Length of output: 70



Script:

#!/bin/bash
# Search for the usage of the handle_json_response method in the codebase.
rg 'handle_json_response'

Length of output: 271



Script:

#!/bin/bash
# Search for test cases that involve the handle_json_response method.
rg 'handle_json_response' -- 'tests/'

Length of output: 35



Script:

#!/bin/bash
# Search for test cases that might indirectly involve the handle_json_response method by looking for JSON parsing errors or dispatching logic.
rg 'JSONDecodeError' -- 'tests/'
rg 'dispatcher' -- 'tests/'

Length of output: 7466



Script:

#!/bin/bash
# Examine the action_test.py file for tests related to JSON response handling.
cat tests/unit_test/action/action_test.py

Length of output: 239700

Copy link
Contributor

@udit-pandey udit-pandey left a comment

Choose a reason for hiding this comment

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

approved

@udit-pandey udit-pandey merged commit 8ffc87b into digiteinfotech:master Jan 10, 2024
8 checks passed
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.

2 participants