refactor: better delegation of captain tasks

This commit is contained in:
Shivam Mishra
2025-12-17 18:39:15 +05:30
parent 9589050ea8
commit 4bbc2b7abe
12 changed files with 181 additions and 160 deletions
+3 -7
View File
@@ -8,20 +8,16 @@ class Captain::BaseEditorService
TOKEN_LIMIT = 400_000
GPT_MODEL = Llm::Config::DEFAULT_MODEL
pattr_initialize [:account!, :event!]
def perform
send("#{event_name}_message")
end
pattr_initialize [:account!, { conversation_display_id: nil }]
private
def event_name
event['name']
raise NotImplementedError, "#{self.class} must implement #event_name"
end
def conversation
@conversation ||= account.conversations.find_by(display_id: event['data']['conversation_display_id'])
@conversation ||= account.conversations.find_by(display_id: conversation_display_id)
end
def api_base
+7 -1
View File
@@ -1,5 +1,7 @@
class Captain::LabelSuggestionService < Captain::BaseEditorService
def label_suggestion_message
pattr_initialize [:account!, :conversation_display_id!]
def perform
# Check cache first
cached_response = read_from_cache
return cached_response if cached_response.present?
@@ -80,4 +82,8 @@ class Captain::LabelSuggestionService < Captain::BaseEditorService
true
end
def event_name
'label_suggestion'
end
end
+9 -1
View File
@@ -1,5 +1,7 @@
class Captain::ReplySuggestionService < Captain::BaseEditorService
def reply_suggestion_message
pattr_initialize [:account!, :conversation_display_id!]
def perform
make_api_call(
model: GPT_MODEL,
messages: [
@@ -7,4 +9,10 @@ class Captain::ReplySuggestionService < Captain::BaseEditorService
].concat(conversation_messages)
)
end
private
def event_name
'reply_suggestion'
end
end
+43 -33
View File
@@ -1,42 +1,48 @@
class Captain::RewriteService < Captain::BaseEditorService
def fix_spelling_grammar_message
call_llm_with_prompt(prompt_from_file('fix_spelling_grammar'))
end
pattr_initialize [:account!, :content!, :action!, { conversation_display_id: nil }]
def confident_message
call_llm_with_prompt(tone_rewrite_prompt('confident'))
end
def straightforward_message
call_llm_with_prompt(tone_rewrite_prompt('straightforward'))
end
def casual_message
call_llm_with_prompt(tone_rewrite_prompt('casual'))
end
def friendly_message
call_llm_with_prompt(tone_rewrite_prompt('friendly'))
end
def professional_message
call_llm_with_prompt(tone_rewrite_prompt('professional'))
end
def improve_message
template = prompt_from_file('improve')
system_prompt = render_liquid_template(template, {
'conversation_context' => conversation.to_llm_text(include_contact_details: true),
'draft_message' => event['data']['content']
})
call_llm_with_prompt(system_prompt, event['data']['content'])
def perform
send(action)
end
private
def call_llm_with_prompt(system_content, user_content = event['data']['content'])
def fix_spelling_grammar
call_llm_with_prompt(prompt_from_file('fix_spelling_grammar'))
end
def casual
call_llm_with_prompt(tone_rewrite_prompt('casual'))
end
def professional
call_llm_with_prompt(tone_rewrite_prompt('professional'))
end
def friendly
call_llm_with_prompt(tone_rewrite_prompt('friendly'))
end
def confident
call_llm_with_prompt(tone_rewrite_prompt('confident'))
end
def straightforward
call_llm_with_prompt(tone_rewrite_prompt('straightforward'))
end
def improve
template = prompt_from_file('improve')
system_prompt = render_liquid_template(template, {
'conversation_context' => conversation.to_llm_text(include_contact_details: true),
'draft_message' => content
})
call_llm_with_prompt(system_prompt, content)
end
def call_llm_with_prompt(system_content, user_content = content)
make_api_call(
model: GPT_MODEL,
messages: [
@@ -54,4 +60,8 @@ class Captain::RewriteService < Captain::BaseEditorService
template = prompt_from_file('tone_rewrite')
render_liquid_template(template, 'tone' => tone)
end
def event_name
action
end
end
+9 -1
View File
@@ -1,5 +1,7 @@
class Captain::SummaryService < Captain::BaseEditorService
def summarize_message
pattr_initialize [:account!, :conversation_display_id!]
def perform
make_api_call(
model: GPT_MODEL,
messages: [
@@ -8,4 +10,10 @@ class Captain::SummaryService < Captain::BaseEditorService
]
)
end
private
def event_name
'summarize'
end
end