Module: Legion::Gaia::Proactive

Defined in:
lib/legion/gaia/proactive.rb

Class Method Summary collapse

Class Method Details

.broadcast(content:, channels: nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/legion/gaia/proactive.rb', line 113

def broadcast(content:, channels: nil)
  registry = Legion::Gaia.channel_registry
  return { error: 'channel registry not available' } unless registry

  targets = channels || registry.active_channels
  results = {}
  targets.each do |ch|
    results[ch] = send_message(channel_id: ch, content: content)
  end
  results
end

.send_message(channel_id:, content:, user_id: nil, content_type: :text) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/gaia/proactive.rb', line 7

def send_message(channel_id:, content:, user_id: nil, content_type: :text)
  registry = Legion::Gaia.channel_registry
  return { error: 'channel registry not available' } unless registry

  adapter = registry.adapter_for(channel_id)
  return { error: "no adapter for channel: #{channel_id}" } unless adapter

  output = OutputFrame.new(
    content: content,
    content_type: content_type,
    channel_id: channel_id,
    metadata: { proactive: true, target_user: user_id }
  )

  registry.deliver(output)
  { sent: true, frame_id: output.id, channel: channel_id }
rescue StandardError => e
  if defined?(Legion::Logging)
    Legion::Logging.warn("Proactive.send_message failed channel=#{channel_id}: #{e.message}")
  end
  { error: e.message }
end

.send_notification(content:, priority: :normal, channel_id: nil, user_id: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/gaia/proactive.rb', line 57

def send_notification(content:, priority: :normal, channel_id: nil, user_id: nil)
  registry = Legion::Gaia.channel_registry
  return { error: 'channel registry not available' } unless registry

  output_router = Legion::Gaia.output_router
  return { error: 'output router not available' } unless output_router

  channels = channel_id ? [channel_id] : registry.active_channels
  results = {}

  channels.each do |ch|
    adapter = registry.adapter_for(ch)
    next unless adapter

    frame = OutputFrame.new(
      content: content,
      channel_id: ch,
      metadata: { proactive: true, priority: priority, target_user: user_id }
    )
    results[ch] = output_router.route(frame)
  end

  results
rescue StandardError => e
  Legion::Logging.warn("Proactive.send_notification failed: #{e.message}") if defined?(Legion::Logging)
  { error: e.message }
end

.send_to_user(user_id:, content:, channel_id: nil, content_type: :text) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/gaia/proactive.rb', line 30

def send_to_user(user_id:, content:, channel_id: nil, content_type: :text)
  registry = Legion::Gaia.channel_registry
  return { error: 'channel registry not available' } unless registry

  if channel_id
    deliver_to_user_on_channel(
      registry: registry,
      user_id: user_id,
      channel_id: channel_id,
      content: content,
      content_type: content_type
    )
  else
    deliver_to_user_all_channels(
      registry: registry,
      user_id: user_id,
      content: content,
      content_type: content_type
    )
  end
rescue StandardError => e
  if defined?(Legion::Logging)
    Legion::Logging.warn("Proactive.send_to_user failed user=#{user_id}: #{e.message}")
  end
  { error: e.message }
end

.start_conversation(channel_id:, user_id:, content:) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/legion/gaia/proactive.rb', line 85

def start_conversation(channel_id:, user_id:, content:)
  registry = Legion::Gaia.channel_registry
  return { error: 'channel registry not available' } unless registry

  adapter = registry.adapter_for(channel_id)
  return { error: "no adapter for channel: #{channel_id}" } unless adapter

  frame = OutputFrame.new(
    content: content,
    channel_id: channel_id,
    metadata: { proactive: true, target_user: user_id, start_conversation: true }
  )

  if adapter.respond_to?(:deliver_proactive)
    result = adapter.deliver_proactive(frame)
    return result if result.is_a?(Hash) && result[:error]

  else
    registry.deliver(frame)
  end
  { started: true, channel: channel_id, user_id: user_id }
rescue StandardError => e
  if defined?(Legion::Logging)
    Legion::Logging.warn("Proactive.start_conversation failed ch=#{channel_id} user=#{user_id}: #{e.message}")
  end
  { error: e.message }
end