199 lines
6.0 KiB
Markdown
199 lines
6.0 KiB
Markdown
## Code Context
|
|
|
|
**Query:** captain assistant, captain document, captain scenario, captain inbox, copilot message, copilot thread, LLM service, tool registry, AI suggestion, openai, vector search
|
|
|
|
### Entry Points
|
|
|
|
- **document** (variable) - spec/enterprise/models/captain/document_spec.rb:9
|
|
`= create(:captain_document,
|
|
assistant: assistant,
|
|
acco...`
|
|
- **document** (variable) - spec/enterprise/models/captain/document_spec.rb:127
|
|
`= create(
|
|
:captain_document,
|
|
assistant: assistant,
|
|
account: account,
|
|
...`
|
|
- **Captain::Tools::SearchDocumentationService** (class) - enterprise/app/services/captain/tools/search_documentation_service.rb:1
|
|
|
|
### Related Symbols
|
|
|
|
- enterprise/app/services/captain/tools/base_tool.rb: Captain::Tools::BaseTool:1
|
|
- enterprise/app/services/captain/tools/search_documentation_service.rb: name:2, execute:9, format_response:25
|
|
- enterprise/app/services/captain/copilot/chat_service.rb: build_tools:63
|
|
- enterprise/app/services/captain/llm/assistant_chat_service.rb: build_tools:32
|
|
|
|
### Code
|
|
|
|
#### document (spec/enterprise/models/captain/document_spec.rb:9)
|
|
|
|
```ruby
|
|
document = create(:captain_document,
|
|
assistant: assistant,
|
|
account: account,
|
|
external_link: 'https://example.com/path/')
|
|
```
|
|
|
|
#### document (spec/enterprise/models/captain/document_spec.rb:127)
|
|
|
|
```ruby
|
|
document = create(
|
|
:captain_document,
|
|
assistant: assistant,
|
|
account: account,
|
|
status: :in_progress,
|
|
content: nil
|
|
)
|
|
```
|
|
|
|
#### Captain::Tools::SearchDocumentationService (enterprise/app/services/captain/tools/search_documentation_service.rb:1)
|
|
|
|
```ruby
|
|
class Captain::Tools::SearchDocumentationService < Captain::Tools::BaseTool
|
|
def self.name
|
|
'search_documentation'
|
|
end
|
|
description 'Search and retrieve documentation from knowledge base'
|
|
|
|
param :query, desc: 'Search Query', required: true
|
|
|
|
def execute(query:)
|
|
Rails.logger.info { "#{self.class.name}: #{query}" }
|
|
|
|
translated_query = Captain::Llm::TranslateQueryService
|
|
.new(account: assistant.account)
|
|
.translate(query, target_language: assistant.account.locale_english_name)
|
|
|
|
responses = assistant.responses.approved.search(translated_query)
|
|
|
|
return 'No FAQs found for the given query' if responses.empty?
|
|
|
|
responses.map { |response| format_response(response) }.join
|
|
end
|
|
|
|
private
|
|
|
|
def format_response(response)
|
|
formatted_response = "
|
|
Question: #{response.question}
|
|
Answer: #{response.answer}
|
|
"
|
|
if response.documentable.present? && response.documentable.try(:external_link)
|
|
formatted_response += "
|
|
Source: #{response.documentable.external_link}
|
|
"
|
|
end
|
|
|
|
formatted_response
|
|
end
|
|
end
|
|
```
|
|
|
|
#### name (enterprise/app/services/captain/tools/search_documentation_service.rb:2)
|
|
|
|
```ruby
|
|
def self.name
|
|
'search_documentation'
|
|
end
|
|
```
|
|
|
|
#### execute (enterprise/app/services/captain/tools/search_documentation_service.rb:9)
|
|
|
|
```ruby
|
|
def execute(query:)
|
|
Rails.logger.info { "#{self.class.name}: #{query}" }
|
|
|
|
translated_query = Captain::Llm::TranslateQueryService
|
|
.new(account: assistant.account)
|
|
.translate(query, target_language: assistant.account.locale_english_name)
|
|
|
|
responses = assistant.responses.approved.search(translated_query)
|
|
|
|
return 'No FAQs found for the given query' if responses.empty?
|
|
|
|
responses.map { |response| format_response(response) }.join
|
|
end
|
|
```
|
|
|
|
#### format_response (enterprise/app/services/captain/tools/search_documentation_service.rb:25)
|
|
|
|
```ruby
|
|
def format_response(response)
|
|
formatted_response = "
|
|
Question: #{response.question}
|
|
Answer: #{response.answer}
|
|
"
|
|
if response.documentable.present? && response.documentable.try(:external_link)
|
|
formatted_response += "
|
|
Source: #{response.documentable.external_link}
|
|
"
|
|
end
|
|
|
|
formatted_response
|
|
end
|
|
```
|
|
|
|
#### build_tools (enterprise/app/services/captain/copilot/chat_service.rb:63)
|
|
|
|
```ruby
|
|
def build_tools
|
|
tools = []
|
|
|
|
tools << Captain::Tools::SearchDocumentationService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::GetConversationService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::SearchConversationsService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::GetContactService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::GetArticleService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::SearchArticlesService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::SearchContactsService.new(@assistant, user: @user)
|
|
tools << Captain::Tools::Copilot::SearchLinearIssuesService.new(@assistant, user: @user)
|
|
|
|
tools.select(&:active?)
|
|
end
|
|
```
|
|
|
|
#### build_tools (enterprise/app/services/captain/llm/assistant_chat_service.rb:32)
|
|
|
|
```ruby
|
|
def build_tools
|
|
tools = [Captain::Tools::SearchDocumentationService.new(@assistant, user: nil)]
|
|
return tools unless custom_tools_enabled?
|
|
|
|
tools + @assistant.account.captain_custom_tools.enabled.map do |ct|
|
|
ct.tool(@assistant, base_class: Captain::Tools::CustomHttpTool, conversation: @conversation)
|
|
end
|
|
end
|
|
```
|
|
|
|
#### Captain::Tools::BaseTool (enterprise/app/services/captain/tools/base_tool.rb:1)
|
|
|
|
```ruby
|
|
class Captain::Tools::BaseTool < RubyLLM::Tool
|
|
prepend Captain::Tools::Instrumentation
|
|
|
|
attr_accessor :assistant
|
|
|
|
def initialize(assistant, user: nil)
|
|
@assistant = assistant
|
|
@user = user
|
|
super()
|
|
end
|
|
|
|
def active?
|
|
true
|
|
end
|
|
|
|
private
|
|
|
|
def user_has_permission(permission)
|
|
return false if @user.blank?
|
|
|
|
account_user = AccountUser.find_by(account_id: @assistant.account_id, user_id: @user.id)
|
|
return false if account_user.blank?
|
|
|
|
return account_user.custom_role.permissions.include?(permission) if account_user.custom_role.present?
|
|
|
|
account_user.administrator? || account_user.agent?
|
|
end
|
|
end
|
|
``` |