ComposioHQ / composio

Composio equips agents with well-crafted tools empowering them to tackle complex tasks
https://docs.composio.dev
Other
1.3k stars 425 forks source link

feat: added wikipedia local tools #222

Open dhruv1710 opened 1 week ago

dhruv1710 commented 1 week ago

User description

Other problems and make checks mentioned in previous pull request resolved.


PR Type

Enhancement, Other


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
7 files
catch_all_exceptions.py
Code cleanup and import reorganization in exception handling.

composio/core/cls/catch_all_exceptions.py
  • Reorganized imports for better readability.
  • Minor formatting changes for consistency.
  • Adjusted exception handling logic.
  • +14/-12 
    scrape_wikipedia.py
    New action for scraping Wikipedia content.                             

    composio/local_tools/wikipedia/actions/scrape_wikipedia.py
  • Added new action to scrape Wikipedia content.
  • Defined request and response schemas using Pydantic.
  • Implemented Wikipedia content scraping logic.
  • +46/-0   
    __init__.py
    Import reorganization and click group reformatting.           

    composio/cli/__init__.py
  • Reorganized imports for better readability.
  • Reformatted click group definition.
  • +13/-4   
    local_handler.py
    Registered Wikipedia tool in local handler.                           

    composio/client/local_handler.py - Registered new Wikipedia tool.
    +2/-0     
    tool.py
    New Wikipedia tool definition and actions.                             

    composio/local_tools/wikipedia/tool.py
  • Added new Wikipedia tool definition.
  • Defined actions for the Wikipedia tool.
  • +15/-0   
    __init__.py
    Import Wikipedia content action.                                                 

    composio/local_tools/wikipedia/actions/__init__.py - Added import for Wikipedia content action.
    +1/-0     
    __init__.py
    Import Wikipedia tool.                                                                     

    composio/local_tools/wikipedia/__init__.py - Added import for Wikipedia tool.
    +1/-0     
    Formatting
    3 files
    __init__.py
    Code reformatting and consistency improvements.                   

    composio/client/__init__.py
  • Reformatted code for better readability.
  • Adjusted method definitions for consistency.
  • +9/-5     
    actions.py
    Minor formatting improvement.                                                       

    composio/cli/actions.py - Added a newline for better code separation.
    +1/-0     
    did_you_mean.py
    Minor formatting improvement.                                                       

    composio/core/cls/did_you_mean.py - Added a newline for better code separation.
    +1/-0     

    ๐Ÿ’ก PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    codiumai-pr-agent-pro[bot] commented 1 week ago

    PR Reviewer Guide ๐Ÿ”

    โฑ๏ธ Estimated effort to review [1-5] 3
    ๐Ÿงช Relevant tests No
    ๐Ÿ”’ Security concerns No
    โšก Key issues to review Possible Bug:
    The execute method in scrape_wikipedia.py attempts to import wikipediaapi inside the method. This could lead to performance issues and unexpected failures if the module is not available. Consider moving imports to the top of the file.
    Error Handling:
    In scrape_wikipedia.py, the error handling could be improved by providing more specific error messages or handling different types of exceptions differently.
    Code Consistency:
    The reformatting in various files like catch_all_exceptions.py and __init__.py files should be reviewed to ensure it follows the project's coding standards.
    codiumai-pr-agent-pro[bot] commented 1 week ago

    PR Code Suggestions โœจ

    CategorySuggestion                                                                                                                                    Score
    Performance
    Use a dictionary for efficient mapping and lookup in class methods ___ **Consider using a dictionary to map app names to their properties instead of multiple
    if-else or switch-case statements in methods like from_app, from_action, and
    from_app_and_action in the Action class. This will make the code cleaner and more
    efficient.** [composio/client/enums.py [467-472]](https://github.com/ComposioHQ/composio/pull/222/files#diff-1d318c488e77dd7846686531b7da3cc9ec3e076bdd87e2dcf4d060bb77932472R467-R472) ```diff @classmethod def from_app(cls, name: str) -> "Action": """Create Action type enum from app name.""" - for action in cls: - if name == action.app: - return action + action_map = {action.app: action for action in cls} + if name in action_map: + return action_map[name] raise ValueError(f"No action type found for name `{name}`") ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: The suggestion to use a dictionary for mapping is excellent for improving code efficiency and readability. It simplifies the logic and enhances performance.
    9
    Use a set for faster membership checking in is_local method ___ **Refactor the is_local method in the App class to use a set for checking if the app is
    local. This will improve the performance as checking membership in a set is faster than
    checking membership in a list.** [composio/client/enums.py [378-389]](https://github.com/ComposioHQ/composio/pull/222/files#diff-1d318c488e77dd7846686531b7da3cc9ec3e076bdd87e2dcf4d060bb77932472R378-R389) ```diff def is_local(self) -> bool: """If the app is local.""" - return self.value.lower() in [ + local_apps = { "mathematical", "localworkspace", ... "wikipedia", - ] + } + return self.value.lower() in local_apps ```
    Suggestion importance[1-10]: 8 Why: Using a set for membership checking is a good performance optimization. This change is straightforward and improves efficiency without altering the method's functionality.
    8
    Move the import of wikipediaapi to the top of the file to improve code clarity and performance ___ **Replace the dynamic import of wikipediaapi with a top-level import to improve performance
    and avoid potential issues with import errors not being caught where expected.** [composio/local_tools/wikipedia/actions/scrape_wikipedia.py [31-34]](https://github.com/ComposioHQ/composio/pull/222/files#diff-6909ced4a3a649eb7273582c3173622c2390a97bd320bd35c94b747c5d24eefbR31-R34) ```diff -# pylint: disable=import-outside-toplevel import wikipediaapi -# pylint: enable=import-outside-toplevel ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 7 Why: Moving the import to the top of the file improves code clarity and performance by avoiding dynamic imports. However, it is a minor optimization.
    7
    Possible bug
    Add error handling for missing DNS configuration to prevent runtime errors ___ **Add error handling for the case where config.get("dns") returns None, which could cause
    the Sentry SDK initialization to fail.** [composio/core/cls/catch_all_exceptions.py [16]](https://github.com/ComposioHQ/composio/pull/222/files#diff-49b9159152a95133118c4a21999e8e582ded3e96ce12d38bdfbc5238286da179R16-R16) ```diff +dsn_value = config.get("dns") +if dsn_value is None: + raise ValueError("DNS configuration is missing.") sentry_sdk.init( - dsn=config.get("dns"), traces_sample_rate=1.0, profiles_sample_rate=1.0 + dsn=dsn_value, traces_sample_rate=1.0, profiles_sample_rate=1.0 ) ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 9 Why: This suggestion addresses a potential runtime error by adding error handling for a missing DNS configuration, which is crucial for robust code.
    9
    Add error handling to is_local method to manage non-string value attributes ___ **Implement error handling for the is_local method in the App class to handle potential
    issues with the value attribute not being a string, which could lead to an AttributeError
    when calling lower().** [composio/client/enums.py [378-389]](https://github.com/ComposioHQ/composio/pull/222/files#diff-1d318c488e77dd7846686531b7da3cc9ec3e076bdd87e2dcf4d060bb77932472R378-R389) ```diff def is_local(self) -> bool: """If the app is local.""" - return self.value.lower() in [ - "mathematical", - "localworkspace", - ... - "wikipedia", - ] + try: + return self.value.lower() in { + "mathematical", + "localworkspace", + ... + "wikipedia", + } + except AttributeError: + raise ValueError("App value must be a string") ```
    Suggestion importance[1-10]: 6 Why: Adding error handling is a good practice to prevent potential runtime errors. However, the likelihood of `value` not being a string in this context seems low, making this a minor improvement.
    6
    Enhancement
    Simplify the isinstance checks by combining them into a single call ___ **Combine the isinstance checks into a single call to simplify the condition and improve
    readability.** [composio/core/cls/catch_all_exceptions.py [56-58]](https://github.com/ComposioHQ/composio/pull/222/files#diff-49b9159152a95133118c4a21999e8e582ded3e96ce12d38bdfbc5238286da179R56-R58) ```diff -if ( - isinstance(exc, ValueError) or isinstance(exc, SystemExit) -) and sentry_sdk.is_initialized(): +if isinstance(exc, (ValueError, SystemExit)) and sentry_sdk.is_initialized(): ``` - [ ] **Apply this suggestion**
    Suggestion importance[1-10]: 8 Why: This suggestion improves readability and simplifies the code without altering functionality. It is a minor enhancement but beneficial for code clarity.
    8
    Maintainability
    Replace tuple inheritance with dataclass for better structure and maintainability ___ **Replace the tuple inheritance in Tag and Action classes with a more suitable data
    structure like a dataclass for better readability and maintainability. Tuples are
    immutable and should be used when the entity represents a collection of heterogeneous
    (different) data types, but here a more structured approach could be beneficial.** [composio/client/enums.py [10-443]](https://github.com/ComposioHQ/composio/pull/222/files#diff-1d318c488e77dd7846686531b7da3cc9ec3e076bdd87e2dcf4d060bb77932472R10-R443) ```diff -class Tag(tuple, Enum): +from dataclasses import dataclass +@dataclass +class Tag(Enum): """App tags.""" ... -class Action(tuple, Enum): +@dataclass +class Action(Enum): """App action.""" ... ```
    Suggestion importance[1-10]: 7 Why: The suggestion to use dataclasses instead of tuples can improve readability and maintainability. However, it may require significant refactoring and testing to ensure compatibility with existing code.
    7