feat: add captain assistant overview stats and summary endpoints

This commit is contained in:
Shivam Mishra
2026-06-29 22:04:53 +05:30
parent ef63752075
commit 847bb13aa5
6 changed files with 249 additions and 10 deletions
+64
View File
@@ -0,0 +1,64 @@
# Generates the LLM welcome summary for the Captain Overview page from the
# assistant's stats hash (see Captain::AssistantStatsBuilder). Renders the
# captain_overview_summary.liquid prompt and returns markdown.
class Captain::OverviewSummaryService < Captain::BaseTaskService
pattr_initialize [:account!, :assistant!, :first_name!, :stats!]
def perform
api_response = make_api_call(
feature: 'editor',
messages: [
{ role: 'system', content: system_prompt },
{ role: 'user', content: 'Write the summary.' }
]
)
return api_response if api_response[:error]
{ message: api_response[:message] }
end
private
def system_prompt
Liquid::Template.parse(prompt_from_file('captain_overview_summary')).render(prompt_variables)
end
def prompt_variables
{
'first_name' => first_name.to_s,
'assistant_name' => assistant.name.to_s,
'conversations_handled' => current(:conversations_handled),
'hours_saved' => current(:hours_saved),
'auto_resolution_rate' => current(:auto_resolution_rate),
'auto_resolution_trend' => trend(:auto_resolution_rate),
'handoff_rate' => current(:handoff_rate),
'handoff_trend' => trend(:handoff_rate),
'reopen_rate' => current(:reopen_rate),
'reopen_trend' => trend(:reopen_rate),
'knowledge_coverage' => stats.dig(:knowledge, :coverage).to_s
}
end
def current(key)
stats.dig(key, :current).to_s
end
def trend(key)
stats.dig(key, :trend).to_s
end
def event_name
'captain_overview_summary'
end
def use_account_openai_hook?
true
end
# The overview summary is an internal analytics readout, not a customer-facing
# response, so it should not consume or be blocked by the captain_responses quota.
def counts_toward_usage?
false
end
end
@@ -1,21 +1,24 @@
You are Captain, the AI support assistant built into Chatwoot. Write a short, warm summary of how this assistant performed over the selected period, addressed directly to {{ first_name }}.
You are writing a short, warm summary of how an AI support assistant named "{{ assistant_name }}" performed over the selected period. The assistant is built into Chatwoot. Address the summary directly to {{ first_name }}, the person who manages it.
Always refer to the assistant by its name, {{ assistant_name }}. Do not call it "Captain", "the assistant", or "your assistant". For example: "Hey {{ first_name }}, {{ assistant_name }} handled ...".
This text is displayed as a static, read-only poster at the top of an analytics dashboard. It is not a chat: the reader cannot reply to you, ask follow-up questions, or ask you to take any action. Never offer to help further, never ask a question, never invite a reply, and never say things like "let me know" or "I can dive in". Just state the summary.
Here are the assistant's stats for the period:
<stats>
- Conversations handled: {{ conversations_handled }}
- Hours saved for the team: {{ hours_saved }}
- Auto-resolution rate: {{ auto_resolution_rate }} ({{ auto_resolution_trend }} vs previous period)
- Handoff rate: {{ handoff_rate }} ({{ handoff_trend }} vs previous period)
- Reopen-after-resolve rate: {{ reopen_rate }} ({{ reopen_trend }} vs previous period)
- Knowledge base coverage: {{ knowledge_coverage }}
- Flagged response rate: {{ flagged_rate }}
- Credits used: {{ credits_used_pct }} of the monthly allowance
- Hours saved for the team: {{ hours_saved }} hours
- Auto-resolution rate: {{ auto_resolution_rate }}% ({{ auto_resolution_trend }} percentage points vs previous period)
- Handoff rate: {{ handoff_rate }}% ({{ handoff_trend }} percentage points vs previous period)
- Reopen-after-resolve rate: {{ reopen_rate }}% ({{ reopen_trend }} percentage points vs previous period)
- Knowledge base coverage: {{ knowledge_coverage }}%
</stats>
Rules:
- Write 2 to 4 sentences in one short paragraph. Add a second short paragraph only if there is a genuinely useful heads-up to flag.
- Open with "Hey {{ first_name }},". Be encouraging and conversational, never robotic.
- Dont use emdashes in your responses
- Lead with the most impressive wins (conversations handled, hours saved, auto-resolution). Mention a trend only when it is meaningful.
- Surface exactly one proactive concern if a stat warrants it (for example credits nearly exhausted, a rising reopen rate, or low knowledge coverage). Skip it entirely when everything looks healthy. Phrase it as a friendly nudge, not an alarm.
- Surface exactly one proactive concern if a stat warrants it (for example a rising reopen rate, a high handoff rate, or low knowledge coverage). Skip it entirely when everything looks healthy. Phrase it as a calm observation about the data, not an alarm, and not an offer to help or a suggestion to contact you.
- Wrap every number, percentage, and duration in **double asterisks** so the interface can highlight it. Bold only the figures, never whole phrases.
- Output plain markdown only. No headings, no lists, no preamble, no closing sign-off.