Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

7.6 KB
Raw Blame
# Installs a toolset from chatwoot/tools onto an assistant.
#
# The toolset is pinned to a commit: the given revision, or the latest commit on the
# default branch. Installing the same commit again is a no-op once every tool is
# present. Otherwise the existing tools are updated in place, matched by manifest
# tool id so slugs and the user-owned enabled flag and settings stay, and missing ones are added.
class Captain::ToolsManifest::InstallService
  class InstallError < StandardError; end

  SOURCE = 'github'.freeze
  CONFIGURATION_SECTIONS = %w[inputs secrets].freeze
  NUMBER_PATTERN = /\A-?\d+(\.\d+)?\z/
  # Install-time values the manifest fills into a tool, like ${{ inputs.shop_domain }}
  INSTALL_PLACEHOLDER_PATTERN = /\$\{\{\s*(inputs|secrets)\.([a-z][a-z0-9_]*)\s*\}\}/i
  # Inputs are filled into URL and request templates after those were checked, so new Liquid would only fail at
  # call time. Secrets are only used in auth_config, which is never rendered, so they may contain these characters.
  LIQUID_DELIMITER_PATTERN = /\{\{|\{%/

  # Installed at this commit with every manifest tool present, so there is nothing to add or update
  def self.complete?(tools, manifest, revision)
    tools.any? &&
      tools.all? { |tool| tool.source_metadata['revision'] == revision } &&
      (manifest['tools'].pluck('id') - tools.map { |tool| tool.source_metadata['tool_id'] }).empty?
  end

  # Fields filled into auth_config must have a value even when optional, or the tool would send blank credentials
  def self.auth_field_names(manifest)
    placeholders = manifest['tools'].flat_map { |tool| tool['auth_config'].to_json.scan(INSTALL_PLACEHOLDER_PATTERN) }
    CONFIGURATION_SECTIONS.index_with { |section| placeholders.filter_map { |kind, name| name if kind.downcase == section } }
  end

  def initialize(assistant:, source:, configuration:, revision: nil)
    @assistant = assistant
    @source = source
    @configuration = configuration
    @revision = revision
  end

  def perform
    @github_source = Captain::ToolsManifest::GithubSource.new(@source)
    if @revision && !Captain::ToolsManifest::GithubSource::REVISION_PATTERN.match?(@revision)
      raise InstallError, 'Revision must be a full 40-character commit SHA'
    end

    revision = @revision || @github_source.latest_revision
    manifest_source = @github_source.manifest(revision)
    manifest = Captain::ToolsManifest::Manifest.parse(manifest_source)
    return installed_tools if self.class.complete?(installed_tools, manifest, revision)

    values = configuration_values!(manifest)

    install!(manifest, values, revision, manifest_source)
  end

  private

  def installed_tools
    @installed_tools ||= @assistant.custom_tools.from_github(@github_source.repository, @github_source.path).to_a
  end

  # The controller checks the configuration's shape; this checks the values against what the manifest declares
  def configuration_values!(manifest)
    auth_fields = self.class.auth_field_names(manifest)
    CONFIGURATION_SECTIONS.index_with do |section|
      section_values!(manifest[section], @configuration.fetch(section, {}), section, auth_fields[section])
    end
  end

  def section_values!(definitions, values, section, auth_fields)
    validate_values!(definitions, values, section)

    # blank? would treat false as missing, which is a valid value for a boolean
    missing = definitions.find do |name, definition|
      (definition['required'] || auth_fields.include?(name)) && values[name].to_s.strip.empty?
    end
    raise InstallError, "#{missing.last['label']} is required" if missing

    values
  end

  def validate_values!(definitions, values, section)
    unknown_names = values.keys - definitions.keys
    raise InstallError, "Unknown #{section}: #{unknown_names.join(', ')}" if unknown_names.any?

    invalid = values.find do |name, value|
      (section == 'inputs' && value.is_a?(String) && value.match?(LIQUID_DELIMITER_PATTERN)) || !valid_value?(definitions[name], value)
    end
    raise InstallError, "#{definitions[invalid.first]['label']} has an invalid value" if invalid
  end

  # Values are interpolated into URLs, auth and request bodies, so they must match the declared type.
  # Numbers may arrive as strings from form inputs; empty optional values are handled by the required check.
  def valid_value?(definition, value)
    return true if value.nil? || value == ''

    case definition['type']
    when 'boolean' then [true, false].include?(value)
    when 'number' then value.is_a?(Numeric) || value.to_s.match?(NUMBER_PATTERN)
    when 'select' then Array(definition['options']).include?(value)
    else value.is_a?(String)
    end
  end

  def source_metadata(manifest, revision, manifest_source, current_tools)
    {
      'source' => SOURCE,
      'repository' => @github_source.repository,
      'path' => @github_source.path,
      'revision' => revision,
      'version' => manifest['version'],
      'manifest_digest' => "sha256:#{Digest::SHA256.hexdigest(manifest_source)}",
      'installation_id' => current_tools.first&.source_metadata&.fetch('installation_id') || SecureRandom.uuid
    }
  end

  def install!(manifest, values, revision, manifest_source)
    ApplicationRecord.transaction do
      # Serializes installs on the assistant; tools are re-read under the lock so a concurrent install is seen
      Captain::Assistant.lock.find(@assistant.id)
      current_tools = @assistant.custom_tools.from_github(@github_source.repository, @github_source.path).to_a
      next current_tools if self.class.complete?(current_tools, manifest, revision)

      save_tools!(manifest, values, current_tools, source_metadata(manifest, revision, manifest_source, current_tools))
    end
  rescue ActiveRecord::RecordInvalid => e
    raise InstallError, "#{e.record.title}: #{e.record.errors.full_messages.to_sentence}"
  end

  def save_tools!(manifest, values, current_tools, metadata)
    existing_tools = current_tools.index_by { |tool| tool.source_metadata['tool_id'] }
    existing_tools.except(*manifest['tools'].pluck('id')).each_value(&:destroy!)
    manifest['tools'].map { |tool| save_tool!(existing_tools[tool['id']], tool, tool_attributes(tool, manifest, values, metadata)) }
  end

  def save_tool!(existing_tool, tool, attributes)
    return existing_tool.tap { |record| record.update!(attributes) } if existing_tool

    @assistant.custom_tools.create!(
      attributes.merge(account: @assistant.account, enabled: tool['enabled'],
                       settings: { 'requires_email_verification' => tool['requires_email_verification'] })
    )
  end

  # Everything except enabled and settings comes from the manifest. Those belong to the user once a tool exists.
  # The manifest keeps tool options flat. They are stored together in settings.
  def tool_attributes(tool, manifest, values, metadata)
    {
      title: tool['title'],
      description: tool['description'],
      http_method: tool['http_method'],
      endpoint_url: interpolate(tool['endpoint_url'], values),
      auth_type: tool['auth_type'],
      auth_config: interpolate(tool['auth_config'], values),
      param_schema: tool['param_schema'],
      request_template: interpolate(tool['request_template'], values),
      response_template: tool['response_template'],
      headers: manifest['headers'],
      source_metadata: metadata.merge('tool_id' => tool['id'])
    }
  end

  def interpolate(value, values)
    case value
    when String
      value.gsub(INSTALL_PLACEHOLDER_PATTERN) do
        values[Regexp.last_match(1).downcase].fetch(Regexp.last_match(2), '').to_s
      end
    when Hash
      value.transform_values { |item| interpolate(item, values) }
    else
      value
    end
  end
end