Module: Assistant::SalesManagement::PipelineTools
- Defined in:
- app/services/assistant/sales_management/pipeline_tools.rb
Overview
Pipeline + workload + performance + recent-call tools. Each one is
rep-centric: lookups are anchored on an employee (or all employees in
a role) and return a snapshot of the deals, activities, or calls
they're working with.
Class Method Summary collapse
-
.build_pipeline_summary_tool ⇒ RubyLLM::Tool
Builds the
get_pipeline_summaryRubyLLM tool. -
.build_recent_calls_tool ⇒ RubyLLM::Tool
Builds the
get_recent_callsRubyLLM tool. -
.build_rep_performance_tool ⇒ RubyLLM::Tool
Builds the
get_rep_performanceRubyLLM tool. -
.build_rep_workload_tool ⇒ RubyLLM::Tool
Builds the
get_rep_workloadRubyLLM tool. -
.pipeline_apply_rep_filter(scope, rep_id:, rep_name:) ⇒ Array<Array(ActiveRecord::Relation, nil)>, Array<Array(nil, String)>
Narrows an Opportunity relation by rep_id or rep_name.
-
.pipeline_grouped_by_state(opportunities) ⇒ Hash{String=>Hash}
Groups opportunities by state with count and total value.
-
.pipeline_summary(rep_id: nil, rep_name: nil, state: nil, open_only: true, since: nil, limit: 25) ⇒ String
Summarises the sales pipeline for a rep, role, or the whole team.
-
.recent_calls(rep_id: nil, rep_name: nil, days: 7, limit: 20) ⇒ String
Returns recent call records for a sales rep.
-
.rep_performance(rep_id: nil, rep_name: nil, period_days: 30) ⇒ String
Returns a performance snapshot for a sales rep over a look-back period.
-
.rep_performance_payload(emp, since:, period_days:) ⇒ Hash{Symbol=>Object}
Builds the raw performance payload for a single employee.
-
.rep_performance_pipeline(open_opps, won_recent, lost_recent) ⇒ Hash{Symbol=>Object}
Computes pipeline counts and values from open, won, and lost relations.
-
.rep_workload(rep_id: nil, rep_name: nil, role: nil, date: nil) ⇒ String
Returns workload and capacity information for one or more reps.
-
.rep_workload_employees(rep_id:, rep_name:, role:) ⇒ Array<Employee>, String
Resolves the Employee list for a workload lookup.
-
.rep_workload_entry(emp, target_date) ⇒ Hash{Symbol=>Object}
Builds the workload payload for a single employee.
-
.rep_workload_today_activities(open_activities, target_date) ⇒ ActiveRecord::Relation
Filters open activities to those scheduled for the target date.
-
.rep_workload_today_detail(today_activities) ⇒ Array<Hash>
Serialises today's activities for the workload payload.
-
.serialize_pipeline_opportunity(opp) ⇒ Hash{Symbol=>Object}
Serialises an opportunity into a compact LLM-facing Hash.
-
.serialize_recent_call(call) ⇒ Hash{Symbol=>Object}
Serialises a call record into a compact LLM-facing Hash.
-
.serialize_rep_order(order) ⇒ Hash{Symbol=>Object}
Compact LLM-facing payload for an order inside a
get_rep_performanceresponse. -
.serialize_rep_quote(quote) ⇒ Hash{Symbol=>Object}
Compact LLM-facing payload for a quote inside a
get_rep_performanceresponse. -
.tools ⇒ Array<RubyLLM::Tool>
Returns all pipeline-focused RubyLLM tools in this module.
Class Method Details
.build_pipeline_summary_tool ⇒ RubyLLM::Tool
Builds the get_pipeline_summary RubyLLM tool.
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 381 def build_pipeline_summary_tool klass = Class.new(RubyLLM::Tool) do description 'Get a summary of the sales pipeline — opportunities grouped by stage, rep, or time period. ' \ 'Shows open deals, values, close dates, and recent state changes. ' \ 'Use this to understand what a rep is working on, pipeline health, and deal progress.' parameters type: 'object', properties: { rep_id: { type: 'integer', description: 'Employee ID of a specific sales rep (filters to their opportunities)' }, rep_name: { type: 'string', description: 'Sales rep name (partial match) — alternative to rep_id' }, state: { type: 'string', description: 'Filter by opportunity state (e.g. "quoting", "follow_up", "promised", "won", "lost")' }, open_only: { type: 'boolean', description: 'Only show open opportunities (default: true)' }, since: { type: 'string', description: 'Only opportunities created after this date (YYYY-MM-DD)' }, limit: { type: 'integer', description: 'Maximum opportunities to return (default: 25, max: 50)' } } define_method(:name) { 'get_pipeline_summary' } define_method(:execute) do |rep_id: nil, rep_name: nil, state: nil, open_only: true, since: nil, limit: 25, **_| Assistant::SalesManagement::PipelineTools.pipeline_summary( rep_id: rep_id, rep_name: rep_name, state: state, open_only: open_only, since: since, limit: limit ) end end klass.new end |
.build_recent_calls_tool ⇒ RubyLLM::Tool
Builds the get_recent_calls RubyLLM tool.
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 489 def build_recent_calls_tool klass = Class.new(RubyLLM::Tool) do description 'Get recent call records for a sales rep or team, with duration, direction, ' \ 'outcome, and key topics from transcripts. Use for understanding call activity ' \ 'and following up on action items from calls.' parameters type: 'object', properties: { rep_id: { type: 'integer', description: 'Employee ID to get calls for' }, rep_name: { type: 'string', description: 'Rep name (partial match) — alternative to rep_id' }, days: { type: 'integer', description: 'Look-back period in days (default: 7)' }, limit: { type: 'integer', description: 'Maximum calls to return (default: 20, max: 50)' } } define_method(:name) { 'get_recent_calls' } define_method(:execute) do |rep_id: nil, rep_name: nil, days: 7, limit: 20, **_| Assistant::SalesManagement::PipelineTools.recent_calls( rep_id: rep_id, rep_name: rep_name, days: days, limit: limit ) end end klass.new end |
.build_rep_performance_tool ⇒ RubyLLM::Tool
Builds the get_rep_performance RubyLLM tool.
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 462 def build_rep_performance_tool klass = Class.new(RubyLLM::Tool) do description 'Get a performance snapshot for a sales rep: recent quotes, orders, ' \ 'won/lost opportunities, and activity completion rates. ' \ 'Useful for sales manager reviews and planning conversations.' parameters type: 'object', properties: { rep_id: { type: 'integer', description: 'Employee ID of the sales rep' }, rep_name: { type: 'string', description: 'Rep name (partial match) — alternative to rep_id' }, period_days: { type: 'integer', description: 'Look-back period in days (default: 30)' } }, required: [] define_method(:name) { 'get_rep_performance' } define_method(:execute) do |rep_id: nil, rep_name: nil, period_days: 30, **_| Assistant::SalesManagement::PipelineTools.rep_performance( rep_id: rep_id, rep_name: rep_name, period_days: period_days ) end end klass.new end |
.build_rep_workload_tool ⇒ RubyLLM::Tool
Builds the get_rep_workload RubyLLM tool.
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 429 def build_rep_workload_tool klass = Class.new(RubyLLM::Tool) do description 'Get the activity workload for one or more sales reps. Shows open activities, ' \ 'overdue activities, today\'s activities, and workload capacity. ' \ 'Use this to understand how busy a rep is and plan their day.' parameters type: 'object', properties: { rep_id: { type: 'integer', description: 'Employee ID of a specific rep' }, rep_name: { type: 'string', description: 'Rep name (partial match) — alternative to rep_id' }, role: { type: 'string', description: 'Filter by role to get workload for all reps of a type (e.g. "sales_rep")' }, date: { type: 'string', description: 'Date to check workload for (YYYY-MM-DD, default: today)' } } define_method(:name) { 'get_rep_workload' } define_method(:execute) do |rep_id: nil, rep_name: nil, role: nil, date: nil, **_| Assistant::SalesManagement::PipelineTools.rep_workload( rep_id: rep_id, rep_name: rep_name, role: role, date: date ) end end klass.new end |
.pipeline_apply_rep_filter(scope, rep_id:, rep_name:) ⇒ Array<Array(ActiveRecord::Relation, nil)>, Array<Array(nil, String)>
Narrows an Opportunity relation by rep_id or rep_name.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 69 def pipeline_apply_rep_filter(scope, rep_id:, rep_name:) return [scope.assigned_to_rep(rep_id), nil] if rep_id.present? return [scope, nil] if rep_name.blank? rep_ids = Employee.active_employees.where('parties.full_name ILIKE ?', "%#{rep_name}%").ids if rep_ids.empty? [nil, { error: "No employee found matching '#{rep_name}'", suggestion: 'Try find_employee first to get the exact name or ID.' }.to_json] else [scope.assigned_to_rep(rep_ids), nil] end end |
.pipeline_grouped_by_state(opportunities) ⇒ Hash{String=>Hash}
Groups opportunities by state with count and total value.
86 87 88 89 90 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 86 def pipeline_grouped_by_state(opportunities) opportunities.group_by(&:state).transform_values do |opps| { count: opps.size, total_value: opps.sum { |opp| opp.value.to_f }.round(2) } end end |
.pipeline_summary(rep_id: nil, rep_name: nil, state: nil, open_only: true, since: nil, limit: 25) ⇒ String
Summarises the sales pipeline for a rep, role, or the whole team.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 40 def pipeline_summary(rep_id: nil, rep_name: nil, state: nil, open_only: true, since: nil, limit: 25) limit = limit.to_i.clamp(1, 50) scope = Opportunity.includes(:customer, :contact, :primary_sales_rep, :secondary_sales_rep, :local_sales_rep, :quotes, :orders) scope, error = pipeline_apply_rep_filter(scope, rep_id: rep_id, rep_name: rep_name) return error if error scope = scope.open_opportunities if open_only scope = scope.where(state: state) if state.present? scope = scope.where(opportunities: { created_at: Date.parse(since).. }) if since.present? opportunities = scope.order(created_at: :desc).limit(limit).to_a Helpers.truncate_json({ total_results: opportunities.size, pipeline_summary: pipeline_grouped_by_state(opportunities), total_pipeline_value: opportunities.sum { |opp| opp.value.to_f }.round(2), opportunities: opportunities.map { |opp| serialize_pipeline_opportunity(opp) } }.to_json) rescue StandardError => e { error: e. }.to_json end |
.recent_calls(rep_id: nil, rep_name: nil, days: 7, limit: 20) ⇒ String
Returns recent call records for a sales rep.
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 332 def recent_calls(rep_id: nil, rep_name: nil, days: 7, limit: 20) days = days.to_i.clamp(1, 90) limit = limit.to_i.clamp(1, 50) since = days.days.ago emp, error = Helpers.resolve_employee(rep_id: rep_id, rep_name: rep_name) return error if error return { error: 'Employee not found. Use find_employee first.' }.to_json unless emp calls = CallRecord.for_party(emp.id) .where(call_records: { created_at: since.. }) .order(created_at: :desc) .limit(limit) results = calls.map { |c| serialize_recent_call(c) } Helpers.truncate_json({ employee: { id: emp.id, name: emp.full_name }, period: "Last #{days} days", total_calls: results.size, total_duration_minutes: (calls.sum { |call| call.duration_secs.to_i } / 60.0).round(1), calls: results }.to_json) rescue StandardError => e { error: e. }.to_json end |
.rep_performance(rep_id: nil, rep_name: nil, period_days: 30) ⇒ String
Returns a performance snapshot for a sales rep over a look-back period.
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 215 def rep_performance(rep_id: nil, rep_name: nil, period_days: 30) period_days = period_days.to_i.clamp(7, 365) since = period_days.days.ago emp, error = Helpers.resolve_employee(rep_id: rep_id, rep_name: rep_name) return error if error return { error: 'Employee not found. Use find_employee first to get the correct name or ID.' }.to_json unless emp Helpers.truncate_json(rep_performance_payload(emp, since: since, period_days: period_days).to_json) rescue StandardError => e { error: e. }.to_json end |
.rep_performance_payload(emp, since:, period_days:) ⇒ Hash{Symbol=>Object}
Builds the raw performance payload for a single employee.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 234 def rep_performance_payload(emp, since:, period_days:) rep_opps = Opportunity.assigned_to_rep(emp.id) open_opps = rep_opps.open_opportunities won_recent = rep_opps.won.where(opportunities: { updated_at: since.. }) lost_recent = rep_opps.lost.where(opportunities: { updated_at: since.. }) recent_quotes = Quote.where(opportunity_id: rep_opps.select(:id)) .where(quotes: { created_at: since.. }) .order(created_at: :desc).limit(10) recent_orders = Order.assigned_to_rep(emp.id) .where(orders: { created_at: since.. }) .order(created_at: :desc).limit(10) completed_activities = emp.activities.completed.where(activities: { completion_datetime: since.. }) open_activities = emp.activities.open_activities recent_calls = CallRecord.for_party(emp.id).where(call_records: { created_at: since.. }) { employee: { id: emp.id, name: emp.full_name, job_title: emp.job_title }, period: "Last #{period_days} days (since #{since.to_date.iso8601})", pipeline: rep_performance_pipeline(open_opps, won_recent, lost_recent), quotes: { count: recent_quotes.count, items: recent_quotes.map { |q| serialize_rep_quote(q) } }, orders: { count: recent_orders.count, items: recent_orders.map { |o| serialize_rep_order(o) } }, activities: { completed_count: completed_activities.count, open_count: open_activities.count, overdue_count: open_activities.overdue_activities.count }, calls: { total_calls: recent_calls.count, total_duration_minutes: (recent_calls.sum(:duration_secs).to_f / 60).round(1) } } end |
.rep_performance_pipeline(open_opps, won_recent, lost_recent) ⇒ Hash{Symbol=>Object}
Computes pipeline counts and values from open, won, and lost relations.
272 273 274 275 276 277 278 279 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 272 def rep_performance_pipeline(open_opps, won_recent, lost_recent) { open_opportunities: open_opps.count, open_pipeline_value: open_opps.sum(:value).to_f.round(2), won_count: won_recent.count, won_value: won_recent.sum(:value).to_f.round(2), lost_count: lost_recent.count, lost_value: lost_recent.sum(:value).to_f.round(2) } end |
.rep_workload(rep_id: nil, rep_name: nil, role: nil, date: nil) ⇒ String
Returns workload and capacity information for one or more reps.
119 120 121 122 123 124 125 126 127 128 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 119 def rep_workload(rep_id: nil, rep_name: nil, role: nil, date: nil) target_date = date.present? ? Date.parse(date) : Date.current employees = rep_workload_employees(rep_id: rep_id, rep_name: rep_name, role: role) return employees if employees.is_a?(String) # error JSON results = employees.map { |emp| rep_workload_entry(emp, target_date) } Helpers.truncate_json({ date: target_date.iso8601, total_reps: results.size, reps: results }.to_json) rescue StandardError => e { error: e. }.to_json end |
.rep_workload_employees(rep_id:, rep_name:, role:) ⇒ Array<Employee>, String
Resolves the Employee list for a workload lookup.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 137 def rep_workload_employees(rep_id:, rep_name:, role:) employees = if rep_id.present? Employee.where(id: rep_id).to_a elsif rep_name.present? Employee.active_employees.where('parties.full_name ILIKE ?', "%#{rep_name}%").to_a elsif role.present? Helpers.apply_role_scope(Employee.active_employees, role).sorted.to_a else return { error: 'Provide rep_id, rep_name, or role to look up workload.' }.to_json end return employees if employees.any? { error: 'No employees found matching the criteria.', suggestion: 'Try find_employee first.' }.to_json end |
.rep_workload_entry(emp, target_date) ⇒ Hash{Symbol=>Object}
Builds the workload payload for a single employee.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 157 def rep_workload_entry(emp, target_date) open_activities = emp.activities.open_activities.non_notes overdue = open_activities.where(Activity.arel_table[:target_datetime].lteq(Time.current)) today_activities = rep_workload_today_activities(open_activities, target_date) max_per_day = emp.maximum_activities_per_day(target_date) current_load = emp.activity_load_on_day(target_date) { id: emp.id, name: emp.full_name, working_today: emp.working_on_day?(target_date), status_today: emp.work_status_on_day(target_date).to_s, capacity: emp.capacity_ratio(target_date), open_activity_count: open_activities.count, overdue_count: overdue.count, today_count: today_activities.count, max_activities_per_day: max_per_day, current_load: current_load, load_percentage: max_per_day.positive? ? ((current_load.to_f / max_per_day) * 100).round(1) : 0, can_take_more: emp.can_take_activities_on_day?(target_date), today_activities: rep_workload_today_detail(today_activities) } end |
.rep_workload_today_activities(open_activities, target_date) ⇒ ActiveRecord::Relation
Filters open activities to those scheduled for the target date.
183 184 185 186 187 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 183 def rep_workload_today_activities(open_activities, target_date) table = Activity.arel_table open_activities.where(table[:target_datetime].gteq(target_date.beginning_of_day)) .where(table[:target_datetime].lteq(target_date.end_of_day)) end |
.rep_workload_today_detail(today_activities) ⇒ Array<Hash>
Serialises today's activities for the workload payload.
193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 193 def rep_workload_today_detail(today_activities) today_activities.includes(:activity_type, :party, :opportunity).limit(20).map do |act| { id: act.id, type: act.activity_type&.task_type || 'Task', priority: act.activity_type&.priority, party: act.party&.full_name, due: act.target_datetime&.strftime('%I:%M %p'), overdue: act.overdue?, opportunity: act.opportunity&.reference_number, opportunity_id: act.opportunity_id, notes_excerpt: act.notes&.truncate(100) }.compact end end |
.serialize_pipeline_opportunity(opp) ⇒ Hash{Symbol=>Object}
Serialises an opportunity into a compact LLM-facing Hash.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 96 def serialize_pipeline_opportunity(opp) { id: opp.id, reference: opp.reference_number, name: opp.name, state: opp.state, value: opp.value&.to_f, customer: opp.customer&.full_name, contact: opp.contact&.full_name, primary_rep: opp.primary_sales_rep&.full_name, secondary_rep: opp.secondary_sales_rep&.full_name, local_rep: opp.local_sales_rep&.full_name, close_date: opp.close_date&.iso8601, created_at: opp.created_at&.iso8601, quote_count: opp.quotes.size, order_count: opp.orders.size, opportunity_type: opp.opportunity_type, crm_url: "#{CRM_URL}/opportunities/#{opp.id}" }.compact end |
.serialize_recent_call(call) ⇒ Hash{Symbol=>Object}
Serialises a call record into a compact LLM-facing Hash.
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 359 def serialize_recent_call(call) base = { id: call.id, date: call.created_at&.strftime('%Y-%m-%d %I:%M %p'), direction: call.respond_to?(:direction) ? call.direction : nil, duration_seconds: call.duration_secs, duration_display: Helpers.format_duration(call.duration_secs), origin: call.origin_party&.full_name || call.origin_name, destination: call.destination_party&.full_name || call.destination_name } base[:outcome] = call.outcome if call.respond_to?(:outcome) && call.outcome.present? base[:key_topics] = call.key_topics if call.respond_to?(:key_topics) && call.key_topics.present? base[:action_items] = call.action_items if call.respond_to?(:action_items) && call.action_items.present? base[:summary] = call.summary&.truncate(300) if call.respond_to?(:summary) base.compact end |
.serialize_rep_order(order) ⇒ Hash{Symbol=>Object}
The bare id field is intentionally omitted. The model is
expected to cite the user-facing reference (e.g. "SO725148")
and link via crm_url. Exposing the primary key here regressed
twice in production daily-focus runs (Apr 15 conv 1352, May 7
conv 2447): with no total field present the model rendered the
id as either a dollar amount ("CO723898 ($1,354,181)" — id was
1354181) or a synthetic reference number ("SO1374291" — id was
1374291). See PR #734.
Compact LLM-facing payload for an order inside a get_rep_performance
response.
316 317 318 319 320 321 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 316 def serialize_rep_order(order) { reference: order.reference_number, state: order.state, type: order.order_type, total: order.try(:total)&.to_f, created: order.created_at&.iso8601, crm_url: UrlHelper.instance.order_url(order, host: CRM_HOSTNAME) } end |
.serialize_rep_quote(quote) ⇒ Hash{Symbol=>Object}
The bare id field is intentionally omitted. The model is
expected to cite the user-facing reference (e.g. "SQ814062")
and link via crm_url. Exposing the primary key here regressed
in production: with no total field present the model grabbed
the id and rendered it as either a dollar amount or a synthetic
reference number. See PR #734.
Compact LLM-facing payload for a quote inside a get_rep_performance
response.
294 295 296 297 298 299 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 294 def serialize_rep_quote(quote) { reference: quote.reference_number, state: quote.state, type: quote.quote_type, total: quote.try(:total)&.to_f, created: quote.created_at&.iso8601, crm_url: UrlHelper.instance.quote_url(quote, host: CRM_HOSTNAME) } end |
.tools ⇒ Array<RubyLLM::Tool>
Returns all pipeline-focused RubyLLM tools in this module.
19 20 21 22 23 24 25 26 |
# File 'app/services/assistant/sales_management/pipeline_tools.rb', line 19 def tools [ build_pipeline_summary_tool, build_rep_workload_tool, build_rep_performance_tool, build_recent_calls_tool ] end |