Class: Insika::Tools::ScheduleFollowup

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/insika/tools/schedule_followup.rb

Overview

schedule — the agent books a follow-up with a customer at a future time. The tool ONLY validates shape + the dedup rule (D6/D7): everything that can block (contact state, frequency, quiet hours, a malformed policy) is decided at FIRE time by the policy in force then — the schedule is a promise made in-conversation. The consent record IS the tool call itself: the customer agreeing in-conversation writes :granted.

System builtin (like remember): require "ruby_llm" stays in THIS file, loaded lazily by the Executor in create_chat. Never enveloped.

Instance Method Summary collapse

Constructor Details

#initialize(contact_store:, followup_store:, state:, event_stream: nil) ⇒ ScheduleFollowup

Returns a new instance of ScheduleFollowup.



29
30
31
32
33
34
35
# File 'lib/insika/tools/schedule_followup.rb', line 29

def initialize(contact_store:, followup_store:, state:, event_stream: nil, **)
  @contact_store = contact_store
  @followup_store = followup_store
  @state = state
  @event_stream = event_stream
  super()
end

Instance Method Details

#execute(at:, reason:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/insika/tools/schedule_followup.rb', line 37

def execute(at:, reason:)
  followup = @state.profile.followup
  # D9: a malformed policy is a tool error naming the rule (the
  # ValidationError rescue below).
  Insika::FollowupPolicy.parse!(followup)

  resolved = resolve_at(at)
  return { error: resolved } if resolved.is_a?(String)

  reason = reason.to_s.strip
  return { error: "reason is required (1..200 chars)" } if reason.empty? || reason.length > 200

  at_time = resolved
  return { error: "the follow-up must be at least 5 minutes in the future" } if at_time < Time.now.utc + 300

  tenant = @state.respond_to?(:tenant) ? @state.tenant : nil
  # D7: the tool call IS the consent — recorded WITHOUT un-silencing a
  # silent customer (only a customer message reopens, D2). A revoked
  # customer raises (the error names the opt-out).
  @contact_store.consent(tenant: tenant, customer: customer)
  record = @followup_store.create(
    tenant: tenant, agent: @state.profile.id, customer: customer,
    session_id: @state.task&.session_id, at: at_time, reason: reason,
    arm: arm(followup), transport: transport
  )
  emit(record.id, record.at)
  { scheduled: record.id, at: record.at, reason: record.reason }
rescue Insika::ValidationError => e
  { error: e.message }
end

#nameObject



27
# File 'lib/insika/tools/schedule_followup.rb', line 27

def name = "schedule"