from tealtiger import TealTiger, PolicyMode, DecisionAction
from openai import OpenAI
from typing import Dict, Any
import os
# Initialize TealTiger with refund policies
teal = TealTiger({
"policies": {
"tools": {
"issue_refund": {
"allowed": True,
"conditions": {
"allowedEnvironments": ["production"],
"requireAuth": True,
"maxAmount": 50.00,
"requireApprovalAbove": 50.00,
"denyAbove": 1000.00,
"maxRefundsPerDay": 5,
"maxRefundsPerCustomer": 2,
"requireOrderId": True,
"requirePurchaseDate": True,
"maxDaysSincePurchase": 30
}
},
"check_order_status": {"allowed": True},
"search_customer_history": {"allowed": True}
}
},
"audit": {
"enabled": True,
"redactPII": True,
"outputs": ["file", "http"]
},
"mode": {
"defaultMode": PolicyMode.MONITOR,
"policyModes": {
"tools.issue_refund": PolicyMode.ENFORCE
}
}
})
# Customer support agent
class SupportAgent:
def __init__(self):
self.openai = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
async def handle_refund_request(
self,
customer_id: str,
order_id: str,
amount: float,
reason: str
) -> Dict[str, Any]:
# Create execution context
context = teal.create_context({
"customerId": customer_id,
"orderId": order_id,
"environment": "production",
"agentId": "support-agent-001"
})
# Evaluate refund request
decision = await teal.evaluate({
"action": "tool.execute",
"tool": "issue_refund",
"arguments": {
"customerId": customer_id,
"orderId": order_id,
"amount": amount,
"reason": reason
},
"context": context
})
# Handle decision
if decision["action"] == DecisionAction.DENY:
print(f"Refund denied: {', '.join(decision['reason_codes'])}")
await teal.log_event({
"type": "refund_denied",
"reason": decision["reason_codes"],
"context": context,
"correlationId": decision["correlation_id"],
"metadata": {
"amount": amount,
"orderId": order_id,
"customerId": customer_id
}
})
return {
"success": False,
"message": self.get_deny_message(decision["reason_codes"]),
"correlationId": decision["correlation_id"]
}
elif decision["action"] == DecisionAction.REQUIRE_APPROVAL:
print(f"Refund requires approval: {amount}")
approval_id = await self.queue_for_approval({
"customerId": customer_id,
"orderId": order_id,
"amount": amount,
"reason": reason,
"correlationId": decision["correlation_id"]
})
return {
"success": False,
"message": f"Refund request submitted for approval. Reference: {approval_id}",
"approvalId": approval_id,
"correlationId": decision["correlation_id"]
}
elif decision["action"] == DecisionAction.ALLOW:
print(f"Refund approved: {amount}")
refund_result = await self.process_refund({
"customerId": customer_id,
"orderId": order_id,
"amount": amount,
"reason": reason
})
await teal.log_event({
"type": "refund_processed",
"context": context,
"correlationId": decision["correlation_id"],
"metadata": {
"amount": amount,
"orderId": order_id,
"customerId": customer_id,
"refundId": refund_result["refundId"]
}
})
return {
"success": True,
"message": f"Refund of ${amount} processed successfully",
"refundId": refund_result["refundId"],
"correlationId": decision["correlation_id"]
}
def get_deny_message(self, reason_codes: list) -> str:
if "AMOUNT_EXCEEDS_LIMIT" in reason_codes:
return "Refund amount exceeds automatic approval limit. Please contact a supervisor."
if "MAX_REFUNDS_EXCEEDED" in reason_codes:
return "Maximum refunds per day exceeded. Please try again tomorrow."
if "ORDER_TOO_OLD" in reason_codes:
return "Order is outside the 30-day refund window."
return "Refund request denied by policy."
async def queue_for_approval(self, request: Dict) -> str:
# Implementation of approval queue
return f"approval-{int(time.time())}"
async def process_refund(self, request: Dict) -> Dict:
# Implementation of actual refund processing
return {"refundId": f"refund-{int(time.time())}"}
# Usage examples
agent = SupportAgent()
# Example 1: Small refund (auto-approved)
result1 = await agent.handle_refund_request(
"customer-123",
"order-456",
25.00,
"Product defective"
)
# Result: {"success": True, "refundId": "refund-..."}
# Example 2: Large refund (requires approval)
result2 = await agent.handle_refund_request(
"customer-789",
"order-012",
500.00,
"Not as described"
)
# Result: {"success": False, "message": "Refund request submitted for approval"}
# Example 3: Excessive refund (denied)
result3 = await agent.handle_refund_request(
"customer-345",
"order-678",
5000.00,
"Changed my mind"
)
# Result: {"success": False, "message": "Refund amount exceeds automatic approval limit"}