Module: Legion::Extensions::MicrosoftTeams::Runners::Bot

Includes:
Helpers::Lex, Helpers::Client, Helpers::PromptResolver, Helpers::TraceRetriever
Included in:
Client
Defined in:
lib/legion/extensions/microsoft_teams/runners/bot.rb

Constant Summary

Constants included from Helpers::TraceRetriever

Helpers::TraceRetriever::MAX_TRACES, Helpers::TraceRetriever::MAX_TRACE_TOKENS

Instance Method Summary collapse

Methods included from Helpers::TraceRetriever

#retrieve_context

Methods included from Helpers::PromptResolver

#resolve_llm_config, #resolve_prompt

Methods included from Helpers::Client

#bot_connection, #graph_connection, #oauth_connection, #user_path

Instance Method Details

#create_conversation(service_url:, bot_id:, user_id:, tenant_id: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 58

def create_conversation(service_url:, bot_id:, user_id:, tenant_id: nil, **)
  payload = {
    bot:     { id: bot_id },
    members: [{ id: user_id }],
    isGroup: false
  }
  payload[:tenantId] = tenant_id if tenant_id
  conn = bot_connection(service_url: service_url, **)
  response = conn.post('/v3/conversations', payload)
  { result: response.body }
end

#dispatch_message(mode: :direct, **payload) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 76

def dispatch_message(mode: :direct, **payload)
  case mode.to_s
  when 'observe'
    observe_message(**payload)
  else
    handle_message(**payload)
  end
end

#get_conversation_members(service_url:, conversation_id:) ⇒ Object



70
71
72
73
74
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 70

def get_conversation_members(service_url:, conversation_id:, **)
  conn = bot_connection(service_url: service_url, **)
  response = conn.get("/v3/conversations/#{conversation_id}/members")
  { result: response.body }
end

#handle_command(text:, owner_id:, chat_id:) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 147

def handle_command(text:, owner_id:, chat_id:, **)
  stripped = text.strip

  case stripped
  when /\Awatch\s+(.+)/i
    cmd_watch(name: ::Regexp.last_match(1).strip, owner_id: owner_id, chat_id: chat_id, **)
  when /\A(?:stop\s+watching|unwatch)\s+(.+)/i
    cmd_unwatch(name: ::Regexp.last_match(1).strip, owner_id: owner_id)
  when /\A(?:watching|list|subscriptions)\z/i
    cmd_list(owner_id: owner_id)
  when /\Apause\s+(.+)/i
    cmd_pause(name: ::Regexp.last_match(1).strip, owner_id: owner_id)
  when /\Aresume\s+(.+)/i
    cmd_resume(name: ::Regexp.last_match(1).strip, owner_id: owner_id)
  when /\Areset preferences\z/i
    cmd_reset_preferences(owner_id: owner_id)
  when /\Aprefer\s+(.+)/i
    cmd_prefer(value: ::Regexp.last_match(1).strip, owner_id: owner_id)
  when /\A(?:preferences|my preferences)\z/i
    cmd_preferences(owner_id: owner_id)
  end
end

#handle_message(chat_id:, conversation_id:, text:, owner_id:, mode: :direct, **opts) ⇒ 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
112
113
114
115
116
117
118
119
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 85

def handle_message(chat_id:, conversation_id:, text:, owner_id:, mode: :direct, **opts)
  command_result = handle_command(text: text, owner_id: owner_id, chat_id: chat_id, **opts)
  if command_result
    reply_text = command_result[:message]
    send_reply(
      chat_id: chat_id, conversation_id: conversation_id,
      activity_id: opts[:activity_id], service_url: opts[:service_url],
      text: reply_text, token: opts[:token]
    )
    return { result: command_result }
  end

  session = session_manager.get_or_create(
    conversation_id: conversation_id, owner_id: owner_id, mode: mode
  )
  session_manager.add_message(conversation_id: conversation_id, role: :user, content: text)

  trace_context = retrieve_trace_context(message: text, owner_id: owner_id, chat_id: chat_id)
  response_text = generate_response(text: text, session: session, trace_context: trace_context)

  reply_result = send_reply(
    chat_id:         chat_id,
    conversation_id: conversation_id,
    activity_id:     opts[:activity_id],
    service_url:     opts[:service_url],
    text:            response_text,
    token:           opts[:token]
  )

  session_manager.add_message(conversation_id: conversation_id, role: :assistant, content: response_text)
  session_manager.touch(conversation_id: conversation_id)
  session_manager.persist(conversation_id: conversation_id) if session_manager.should_flush?(conversation_id: conversation_id)

  { result: reply_result }
end

#observe_message(chat_id:, owner_id:, text:, from:, peer_name:, timestamp: nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 121

def observe_message(chat_id:, owner_id:, text:, from:, peer_name:, timestamp: nil, **)
  return { result: :skipped, reason: :observe_disabled } unless observe_enabled?

  extraction = extract_from_message(text: text, from: from, peer_name: peer_name, owner_id: owner_id)

  store_observation(
    chat_id: chat_id, owner_id: owner_id, text: text,
    from: from, peer_name: peer_name, extraction: extraction,
    timestamp: timestamp
  )

  if extraction && extraction[:action_items]&.any? && notify_enabled?(owner_id: owner_id, chat_id: chat_id)
    notify_owner(owner_id: owner_id, extraction: extraction, peer_name: peer_name)
  end

  { result: extraction || { raw_text: text } }
end

#reply_to_activity(service_url:, conversation_id:, activity_id:, text: nil, attachments: [], content_type: 'message') ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 24

def reply_to_activity(service_url:, conversation_id:, activity_id:, text: nil,
                      attachments: [], content_type: 'message', **)
  activity = { type: content_type, text: text }
  activity[:attachments] = attachments unless attachments.empty?
  conn = bot_connection(service_url: service_url, **)
  response = conn.post(
    "/v3/conversations/#{conversation_id}/activities/#{activity_id}", activity
  )
  { result: response.body }
end

#send_activity(service_url:, conversation_id:, activity:) ⇒ Object



18
19
20
21
22
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 18

def send_activity(service_url:, conversation_id:, activity:, **)
  conn = bot_connection(service_url: service_url, **)
  response = conn.post("/v3/conversations/#{conversation_id}/activities", activity)
  { result: response.body }
end

#send_card(service_url:, conversation_id:, card:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 44

def send_card(service_url:, conversation_id:, card:, **)
  attachment = {
    contentType: 'application/vnd.microsoft.card.adaptive',
    contentUrl:  nil,
    content:     card
  }
  send_activity(
    service_url:     service_url,
    conversation_id: conversation_id,
    activity:        { type: 'message', attachments: [attachment] },
    **
  )
end

#send_text(service_url:, conversation_id:, text:) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 35

def send_text(service_url:, conversation_id:, text:, **)
  send_activity(
    service_url:     service_url,
    conversation_id: conversation_id,
    activity:        { type: 'message', text: text },
    **
  )
end

#session_managerObject



139
140
141
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 139

def session_manager
  @session_manager ||= Legion::Extensions::MicrosoftTeams::Helpers::SessionManager.new
end

#subscription_registryObject



143
144
145
# File 'lib/legion/extensions/microsoft_teams/runners/bot.rb', line 143

def subscription_registry
  @subscription_registry ||= Legion::Extensions::MicrosoftTeams::Helpers::SubscriptionRegistry.new
end