Files
chatwoot/enterprise/lib/captain/tool.rb
Pranav d070743383 feat(ee): Add Captain features (#10665)
Migration Guide: https://chwt.app/v4/migration

This PR imports all the work related to Captain into the EE codebase. Captain represents the AI-based features in Chatwoot and includes the following key components:

- Assistant: An assistant has a persona, the product it would be trained on. At the moment, the data at which it is trained is from websites. Future integrations on Notion documents, PDF etc. This PR enables connecting an assistant to an inbox. The assistant would run the conversation every time before transferring it to an agent.
- Copilot for Agents: When an agent is supporting a customer, we will be able to offer additional help to lookup some data or fetch information from integrations etc via copilot.
- Conversation FAQ generator: When a conversation is resolved, the Captain integration would identify questions which were not in the knowledge base.
- CRM memory: Learns from the conversations and identifies important information about the contact.

---------

Co-authored-by: Vishnu Narayanan <vishnu@chatwoot.com>
Co-authored-by: Sojan <sojan@pepalo.com>
Co-authored-by: iamsivin <iamsivin@gmail.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
2025-01-14 16:15:47 -08:00

67 lines
1.9 KiB
Ruby

class Captain::Tool
class InvalidImplementationError < StandardError; end
class InvalidSecretsError < StandardError; end
class ExecutionError < StandardError; end
REQUIRED_PROPERTIES = %w[name description properties secrets].freeze
attr_reader :name, :description, :properties, :secrets, :implementation, :memory
def initialize(name:, config:)
@name = name
@description = config[:description]
@properties = config[:properties]
@secrets = config[:secrets] || []
@implementation = config[:implementation]
@memory = config[:memory] || {}
end
def register_method(&block)
@implementation = block
end
def execute(input, provided_secrets = {})
validate_secrets!(provided_secrets)
validate_input!(input)
raise ExecutionError, 'No implementation registered' unless @implementation
instance_exec(input, provided_secrets, memory, &@implementation)
rescue StandardError => e
raise ExecutionError, "Execution failed: #{e.message}"
end
private
def validate_config!(config)
missing_keys = REQUIRED_PROPERTIES - config.keys
return if missing_keys.empty?
raise InvalidImplementationError,
"Missing required properties: #{missing_keys.join(', ')}"
end
def validate_secrets!(provided_secrets)
required_secrets = secrets.map!(&:to_sym)
missing_secrets = required_secrets - provided_secrets.keys
return if missing_secrets.empty?
raise InvalidSecretsError, "Missing required secrets: #{missing_secrets.join(', ')}"
end
def validate_input!(input)
properties.each do |property, constraints|
validate_property!(input, property, constraints)
end
end
def validate_property!(input, property, constraints)
value = input[property.to_sym]
raise ArgumentError, "Missing required property: #{property}" if constraints['required'] && value.nil?
true
end
end