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

Feature request: Add LexResolver for simplified intent routing #5684

Open
2 tasks done
fakhtar opened this issue Dec 4, 2024 · 1 comment
Open
2 tasks done

Feature request: Add LexResolver for simplified intent routing #5684

fakhtar opened this issue Dec 4, 2024 · 1 comment
Labels
feature-request feature request triage Pending triage from maintainers

Comments

@fakhtar
Copy link

fakhtar commented Dec 4, 2024

Use case

Feature Request: Add LexResolver for simplified intent routing

Description

Add a LexResolver class similar to APIGatewayRestResolver that enables decorator-based routing for Amazon Lex V2 Lambda functions. This simplifies handling different intents and event types in Lex bot fulfillment/dialog code hooks.

Use Case

from aws_lambda_powertools.utilities.lex import LexResolver

app = LexResolver()

@app.fulfillment("OrderPizza")
def handle_order_pizza():
    # Handle pizza order fulfillment
    return {
        "sessionAttributes": session_attributes,
        "messages": [{
            "contentType": "PlainText",
            "content": "Your pizza order is confirmed"
        }]
    }

@app.dialog("CheckOrder")
def handle_check_order():
    # Handle order status dialog
    pass

Proposed Features

  • Intent-based routing using decorators
  • Separate decorators for DialogCodeHook and FulfillmentCodeHook
  • Access to session attributes via resolver context
  • Type hints and response validation
  • Error handling with appropriate Lex response formats
  • Middleware support similar to APIGatewayRestResolver

Benefits

  • Reduces boilerplate code
  • Cleaner separation of concerns
  • Type-safe handling of Lex events
  • Consistent with existing Powertools patterns

Implementation Notes

  • Follow similar pattern to APIGatewayRestResolver
  • Support Lambda Powertools logger and tracer integration
  • Maintain compatibility with Lex V2 response formats

Solution/User Experience

Solution/User Experience

The LexResolver would provide a clean, intuitive API for routing Lex events:

from aws_lambda_powertools.utilities.lex import LexResolver

app = LexResolver()

@app.fulfillment("OrderPizza")
def handle_pizza_order(session_attributes: dict):
    return {
        "sessionAttributes": {**session_attributes, "order_status": "confirmed"},
        "messages": [{
            "contentType": "PlainText",
            "content": "Your pizza order is confirmed"
        }]
    }

@app.dialog("CheckDelivery", required_slots=["order_id"])
def handle_delivery_check(session_attributes: dict):
    return {
        "sessionAttributes": session_attributes,
        "messages": [{
            "contentType": "PlainText",
            "content": f"Your delivery is on its way"
        }]
    }

def lambda_handler(event, context):
    return app.resolve(event, context)

Alternative solutions

Acknowledgment

@anafalcao
Copy link
Collaborator

Hey @fakhtar ! Thank you for your feature request for a LexResolver. We appreciate your detailed suggestion and will evaluate it carefully in our next iterations. We'll get back to you soon or with any follow-up questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request feature request triage Pending triage from maintainers
Projects
Development

No branches or pull requests

2 participants