import { TealTiger, PolicyMode, DecisionAction } from 'tealtiger';
import OpenAI from 'openai';
// Initialize TealTiger with refund policies
const teal = new TealTiger({
policies: {
tools: {
// Refund tool with strict conditions
issue_refund: {
allowed: true,
conditions: {
// Only allow in specific environments
allowedEnvironments: ['production'],
// Require authentication
requireAuth: true,
// Amount-based rules
maxAmount: 50.00, // Auto-approve up to $50
requireApprovalAbove: 50.00, // Human review for $50+
denyAbove: 1000.00, // Always deny $1000+
// Time-based rules
maxRefundsPerDay: 5,
maxRefundsPerCustomer: 2,
// Order validation
requireOrderId: true,
requirePurchaseDate: true,
maxDaysSincePurchase: 30 // Only refund within 30 days
}
},
// Read-only tools are always allowed
check_order_status: { allowed: true },
search_customer_history: { allowed: true }
}
},
audit: {
enabled: true,
redactPII: true,
outputs: ['file', 'http']
},
// Start in MONITOR mode to measure violations
mode: {
defaultMode: PolicyMode.MONITOR,
policyModes: {
'tools.issue_refund': PolicyMode.ENFORCE // Enforce refund policies
}
}
});
// Customer support agent
class SupportAgent {
private openai: OpenAI;
constructor() {
this.openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
}
async handleRefundRequest(
customerId: string,
orderId: string,
amount: number,
reason: string
) {
// Create execution context
const context = teal.createContext({
customerId: customerId,
orderId: orderId,
environment: 'production',
agentId: 'support-agent-001'
});
// Evaluate refund request
const decision = await teal.evaluate({
action: 'tool.execute',
tool: 'issue_refund',
arguments: {
customerId,
orderId,
amount,
reason
},
context
});
// Handle decision
switch (decision.action) {
case DecisionAction.DENY:
console.log(`Refund denied: ${decision.reason_codes.join(', ')}`);
await teal.logEvent({
type: 'refund_denied',
reason: decision.reason_codes,
context,
correlationId: decision.correlation_id,
metadata: {
amount,
orderId,
customerId
}
});
return {
success: false,
message: this.getDenyMessage(decision.reason_codes),
correlationId: decision.correlation_id
};
case DecisionAction.REQUIRE_APPROVAL:
console.log(`Refund requires approval: ${amount}`);
// Queue for human review
const approvalId = await this.queueForApproval({
customerId,
orderId,
amount,
reason,
correlationId: decision.correlation_id
});
return {
success: false,
message: `Refund request submitted for approval. Reference: ${approvalId}`,
approvalId,
correlationId: decision.correlation_id
};
case DecisionAction.ALLOW:
console.log(`Refund approved: ${amount}`);
// Process refund
const refundResult = await this.processRefund({
customerId,
orderId,
amount,
reason
});
// Log successful refund
await teal.logEvent({
type: 'refund_processed',
context,
correlationId: decision.correlation_id,
metadata: {
amount,
orderId,
customerId,
refundId: refundResult.refundId
}
});
return {
success: true,
message: `Refund of $${amount} processed successfully`,
refundId: refundResult.refundId,
correlationId: decision.correlation_id
};
}
}
private getDenyMessage(reasonCodes: string[]): string {
if (reasonCodes.includes('AMOUNT_EXCEEDS_LIMIT')) {
return 'Refund amount exceeds automatic approval limit. Please contact a supervisor.';
}
if (reasonCodes.includes('MAX_REFUNDS_EXCEEDED')) {
return 'Maximum refunds per day exceeded. Please try again tomorrow.';
}
if (reasonCodes.includes('ORDER_TOO_OLD')) {
return 'Order is outside the 30-day refund window.';
}
return 'Refund request denied by policy.';
}
private async queueForApproval(request: any): Promise<string> {
// Implementation of approval queue
return `approval-${Date.now()}`;
}
private async processRefund(request: any): Promise<any> {
// Implementation of actual refund processing
return { refundId: `refund-${Date.now()}` };
}
}
// Usage examples
const agent = new SupportAgent();
// Example 1: Small refund (auto-approved)
const result1 = await agent.handleRefundRequest(
'customer-123',
'order-456',
25.00,
'Product defective'
);
// Result: { success: true, refundId: 'refund-...' }
// Example 2: Large refund (requires approval)
const result2 = await agent.handleRefundRequest(
'customer-789',
'order-012',
500.00,
'Not as described'
);
// Result: { success: false, message: 'Refund request submitted for approval' }
// Example 3: Excessive refund (denied)
const result3 = await agent.handleRefundRequest(
'customer-345',
'order-678',
5000.00,
'Changed my mind'
);
// Result: { success: false, message: 'Refund amount exceeds automatic approval limit' }