feat: include chatwoot metadata with each tool call (#12907)

This commit is contained in:
Shivam Mishra
2025-12-10 15:25:18 +05:30
committed by GitHub
parent 20fa5eeaa5
commit 0d8e249fe4
4 changed files with 257 additions and 3 deletions

View File

@@ -66,6 +66,31 @@ module Concerns::Toolable
[auth_config['username'], auth_config['password']]
end
def build_metadata_headers(state)
{}.tap do |headers|
add_base_headers(headers, state)
add_conversation_headers(headers, state[:conversation]) if state[:conversation]
add_contact_headers(headers, state[:contact]) if state[:contact]
end
end
def add_base_headers(headers, state)
headers['X-Chatwoot-Account-Id'] = state[:account_id].to_s if state[:account_id]
headers['X-Chatwoot-Assistant-Id'] = state[:assistant_id].to_s if state[:assistant_id]
headers['X-Chatwoot-Tool-Slug'] = slug if slug.present?
end
def add_conversation_headers(headers, conversation)
headers['X-Chatwoot-Conversation-Id'] = conversation[:id].to_s if conversation[:id]
headers['X-Chatwoot-Conversation-Display-Id'] = conversation[:display_id].to_s if conversation[:display_id]
end
def add_contact_headers(headers, contact)
headers['X-Chatwoot-Contact-Id'] = contact[:id].to_s if contact[:id]
headers['X-Chatwoot-Contact-Email'] = contact[:email].to_s if contact[:email].present?
headers['X-Chatwoot-Contact-Phone'] = contact[:phone_number].to_s if contact[:phone_number].present?
end
def format_response(raw_response_body)
return raw_response_body if response_template.blank?

View File

@@ -11,11 +11,11 @@ class Captain::Tools::HttpTool < Agents::Tool
@custom_tool.enabled?
end
def perform(_tool_context, **params)
def perform(tool_context, **params)
url = @custom_tool.build_request_url(params)
body = @custom_tool.build_request_body(params)
response = execute_http_request(url, body)
response = execute_http_request(url, body, tool_context)
@custom_tool.format_response(response.body)
rescue StandardError => e
Rails.logger.error("HttpTool execution error for #{@custom_tool.slug}: #{e.class} - #{e.message}")
@@ -39,7 +39,7 @@ class Captain::Tools::HttpTool < Agents::Tool
# 1MB of text ≈ 250K tokens, which exceeds most LLM context windows
MAX_RESPONSE_SIZE = 1.megabyte
def execute_http_request(url, body)
def execute_http_request(url, body, tool_context)
uri = URI.parse(url)
# Check if resolved IP is private
@@ -53,6 +53,7 @@ class Captain::Tools::HttpTool < Agents::Tool
request = build_http_request(uri, body)
apply_authentication(request)
apply_metadata_headers(request, tool_context)
response = http.request(request)
@@ -102,4 +103,10 @@ class Captain::Tools::HttpTool < Agents::Tool
credentials = @custom_tool.build_basic_auth_credentials
request.basic_auth(*credentials) if credentials
end
def apply_metadata_headers(request, tool_context)
state = tool_context&.state || {}
metadata_headers = @custom_tool.build_metadata_headers(state)
metadata_headers.each { |key, value| request[key] = value }
end
end