iachat/enterprise/app/models/enterprise/inbox.rb
Shivam Mishra 3b366f43e6
feat: setup captain limits (#10713)
This pull request introduces several changes to implement and manage
usage limits for the Captain AI service. The key changes include adding
configuration for plan limits, updating error messages, modifying
controllers and models to handle usage limits, and updating tests to
ensure the new functionality works correctly.

## Implementation Checklist

- [x] Ability to configure captain limits per check
- [x] Update response for `usage_limits` to include captain limits
- [x] Methods to increment or reset captain responses limits in the
`limits` column for the `Account` model
- [x] Check documents limit using a count query
- [x] Ensure Captain hand-off if a limit is reached
- [x] Ensure limits are enforced for Copilot Chat
- [x] Ensure limits are reset when stripe webhook comes in 
- [x] Increment usage for FAQ generation and Contact notes
- [x] Ensure documents limit is enforced

These changes ensure that the Captain AI service operates within the defined usage limits for different subscription plans, providing appropriate error messages and handling when limits are exceeded.
2025-01-23 01:23:18 +05:30

33 lines
1017 B
Ruby

module Enterprise::Inbox
def member_ids_with_assignment_capacity
max_assignment_limit = auto_assignment_config['max_assignment_limit']
overloaded_agent_ids = max_assignment_limit.present? ? get_agent_ids_over_assignment_limit(max_assignment_limit) : []
super - overloaded_agent_ids
end
def active_bot?
super || captain_active?
end
def captain_active?
captain_assistant.present? && more_responses?
end
private
def more_responses?
account.usage_limits[:captain][:responses][:current_available].positive?
end
def get_agent_ids_over_assignment_limit(limit)
conversations.open.select(:assignee_id).group(:assignee_id).having("count(*) >= #{limit.to_i}").filter_map(&:assignee_id)
end
def ensure_valid_max_assignment_limit
return if auto_assignment_config['max_assignment_limit'].blank?
return if auto_assignment_config['max_assignment_limit'].to_i.positive?
errors.add(:auto_assignment_config, 'max_assignment_limit must be greater than 0')
end
end