Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add authorized external agents to Voice calls (#43649)

Gabriel Grubbaeeee16d8fb44
diff --git a/plugins/voice/app/controllers/voice/admin_rooms_controller.rb b/plugins/voice/app/controllers/voice/admin_rooms_controller.rb
index 568ba0f7e87..04b1ae93bfa 100644
--- a/plugins/voice/app/controllers/voice/admin_rooms_controller.rb
+++ b/plugins/voice/app/controllers/voice/admin_rooms_controller.rb
@@ -39,8 +39,7 @@ module Voice
def destroy
room = Voice::Room.find(params[:id])
room.destroy!
- Voice::Livekit::RoomServiceClient.delete_room(room)
- Voice::ParticipantTracker.clear_transport_pin(room.id)
+ Voice::AgentManager.evict_agents_in_room!(room)
head :no_content
end
@@ -53,8 +52,7 @@ module Voice
room = Voice::Room.find(params[:id])
participant_ids = Voice::ParticipantTracker.user_ids(room.id)
- Voice::Livekit::RoomServiceClient.delete_room(room)
- Voice::ParticipantTracker.clear_transport_pin(room.id)
+ Voice::AgentManager.evict_agents_in_room!(room)
participant_ids.each { |user_id| Voice::RoomBroadcaster.publish_kick(room, user_id) }
head :no_content
diff --git a/plugins/voice/app/controllers/voice/agents_controller.rb b/plugins/voice/app/controllers/voice/agents_controller.rb
new file mode 100644
index 00000000000..937524b6897
--- /dev/null
+++ b/plugins/voice/app/controllers/voice/agents_controller.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+module Voice
+ class AgentsController < ApplicationController
+ def index
+ unless guardian.can_invite_voice_agents? && Voice::AgentBot.available?
+ raise Discourse::InvalidAccess
+ end
+
+ refresh = params[:refresh].to_s == "true"
+ RateLimiter.new(current_user, "voice-agent-list-refresh", 6, 1.minute).performed! if refresh
+
+ result = Voice::Livekit::CloudAgentClient.list(refresh:)
+ if result[:ok]
+ render json: { agents: result[:agents] }
+ else
+ render_json_error(I18n.t("voice.errors.agent_list_failed"), status: 503)
+ end
+ end
+ end
+end
diff --git a/plugins/voice/app/controllers/voice/livekit_webhooks_controller.rb b/plugins/voice/app/controllers/voice/livekit_webhooks_controller.rb
index 01ba227856b..236a0af178d 100644
--- a/plugins/voice/app/controllers/voice/livekit_webhooks_controller.rb
+++ b/plugins/voice/app/controllers/voice/livekit_webhooks_controller.rb
@@ -1,12 +1,8 @@
# frozen_string_literal: true
module Voice
- # Receives LiveKit server webhooks. Strictly a reconcile-only backstop:
- # events may early-expire presence LiveKit knows is gone, or clear the
- # transport pin of a finished room — but they never create presence and
- # never touch Session rows. Heartbeats and CloseOrphanedSessions remain the
- # source of truth on both transports, so undelivered webhooks cost nothing
- # but reaction time.
+ # Human events only reconcile existing presence; agents require an authorized
+ # session verified against the current provider connection.
class LivekitWebhooksController < ApplicationController
skip_before_action :ensure_logged_in,
:verify_authenticity_token,
@@ -35,7 +31,7 @@ module Voice
case event["event"]
when "participant_joined"
- record_participant_sid(event)
+ record_participant_join(event)
when "participant_left", "participant_connection_aborted"
expire_participant(event)
when "room_finished"
@@ -56,25 +52,34 @@ module Voice
nil
end
- # Not presence — only which media session (SID) is the user's current one,
- # so a superseded session's late departure can be told apart from the live
- # session's.
- def record_participant_sid(event)
+ def record_participant_join(event)
room = event_room(event)
return if room.nil?
- user_id = event.dig("participant", "identity").to_i
- return if user_id <= 0
+ identity = event.dig("participant", "identity").to_s
+ if AgentDispatcher.pending?(room.id) && !/\A[1-9]\d*\z/.match?(identity)
+ return AgentManager.reconcile(room)
+ end
+ return unless /\A-?[1-9]\d*\z/.match?(identity)
+
+ user_id = identity.to_i
+ return AgentManager.reconcile(room) if user_id.negative?
- Voice::ParticipantTracker.set_livekit_sid(room.id, user_id, event.dig("participant", "sid"))
+ ParticipantTracker.set_livekit_sid(room.id, user_id, event.dig("participant", "sid"))
end
def expire_participant(event)
room = event_room(event)
return if room.nil?
- user_id = event.dig("participant", "identity").to_i
- return if user_id <= 0
+ identity = event.dig("participant", "identity").to_s
+ if AgentDispatcher.pending?(room.id) && !/\A[1-9]\d*\z/.match?(identity)
+ return AgentManager.reconcile(room)
+ end
+ return unless /\A-?[1-9]\d*\z/.match?(identity)
+
+ user_id = identity.to_i
+ return AgentManager.reconcile(room) if user_id.negative?
# A quick disconnect/rejoin can deliver the old session's departure
# after the new session is up; `gone_at` can't distinguish them (the
@@ -90,14 +95,19 @@ module Voice
user_id,
gone_at: event_created_at(event),
)
- Voice::RoomBroadcaster.publish_participants_if_changed(room) if expired
+ if expired
+ if ParticipantTracker.human_user_ids(room.id).empty? && AgentManager.provider_room?(room.id)
+ AgentManager.evict_agents_in_room!(room)
+ end
+ RoomBroadcaster.publish_participants_if_changed(room)
+ end
end
def finish_room(event)
room = event_room(event)
return if room.nil?
- Voice::ParticipantTracker.clear_transport_pin(room.id)
+ AgentManager.evict_agents_in_room!(room, delete_room: false)
Voice::ParticipantTracker.clear_livekit_sids(room.id)
end
diff --git a/plugins/voice/app/controllers/voice/room_memberships_controller.rb b/plugins/voice/app/controllers/voice/room_memberships_controller.rb
index a38e6d03334..31ac4ad6be9 100644
--- a/plugins/voice/app/controllers/voice/room_memberships_controller.rb
+++ b/plugins/voice/app/controllers/voice/room_memberships_controller.rb
@@ -13,6 +13,7 @@ module Voice
guardian.ensure_can_manage_voice_room!(@room)
user = fetch_user
role = Voice::RoomMembership.role_value(params[:role])
+ ensure_agent_role_allowed!(user)
membership = @room.room_memberships.find_or_initialize_by(user: user)
membership.role = role
membership.save!
@@ -34,7 +35,9 @@ module Voice
guardian.ensure_can_manage_voice_room!(@room)
membership = @room.room_memberships.find(params[:id])
new_role = params.require(:role)
- membership.update!(role: Voice::RoomMembership.role_value(new_role))
+ role = Voice::RoomMembership.role_value(new_role)
+ ensure_agent_role_allowed!(membership.user)
+ membership.update!(role: role)
if Voice::ParticipantTracker.user_ids(@room.id).include?(membership.user_id)
metadata = Voice::ParticipantTracker.get_metadata(@room.id, membership.user_id)
@@ -73,6 +76,10 @@ module Voice
private
+ def ensure_agent_role_allowed!(user)
+ raise Discourse::InvalidParameters.new(:user_id) if user.bot?
+ end
+
def fetch_user
if params[:user_id]
User.find(params[:user_id])
diff --git a/plugins/voice/app/controllers/voice/rooms_controller.rb b/plugins/voice/app/controllers/voice/rooms_controller.rb
index 5cd734b9894..006c4214c9e 100644
--- a/plugins/voice/app/controllers/voice/rooms_controller.rb
+++ b/plugins/voice/app/controllers/voice/rooms_controller.rb
@@ -30,6 +30,7 @@ module Voice
ensure_chat_session
chat_message
kick
+ invite_agent
flag
heartbeat
toggle_mute
@@ -180,8 +181,8 @@ module Voice
# empty — an occupied LiveKit room must never be split, and silent
# degradation hides outages from ops.
if SiteSetting.voice_livekit_mesh_fallback &&
- Voice::ParticipantTracker.user_ids(@room.id).empty?
- Voice::ParticipantTracker.clear_transport_pin(@room.id)
+ Voice::ParticipantTracker.human_user_ids(@room.id).empty?
+ Voice::AgentManager.evict_agents_in_room!(@room)
transport = "mesh"
else
return(render_json_error(I18n.t("voice.errors.livekit_unavailable"), status: 503))
@@ -365,9 +366,8 @@ module Voice
session = close_session_for(@room.id, current_user.id)
Voice::ParticipantTracker.mark_left(@room.id, current_user.id)
Voice::ParticipantTracker.remove(@room.id, current_user.id)
- if Voice::ParticipantTracker.user_ids(@room.id).empty?
- Voice::Livekit::RoomServiceClient.delete_room(@room)
- Voice::ParticipantTracker.clear_transport_pin(@room.id)
+ if Voice::ParticipantTracker.human_user_ids(@room.id).empty?
+ Voice::AgentManager.evict_agents_in_room!(@room)
end
Voice::UserStatusManager.clear_voice_status(current_user)
Voice::RoomBroadcaster.publish_participants(@room)
@@ -485,6 +485,17 @@ module Voice
alias toggle_mute state
+ def invite_agent
+ raise Discourse::InvalidAccess unless guardian.can_invite_voice_agent?(@room)
+ dispatch_id =
+ Voice::AgentDispatcher.dispatch!(room: @room, agent_name: params.require(:agent_name))
+ render json: { dispatch_id: }, status: :created
+ rescue Voice::AgentManager::AuthorizationError
+ render_json_error(I18n.t("voice.errors.agent_dispatch_forbidden"), status: 403)
+ rescue Voice::AgentDispatcher::DispatchError
+ render_json_error(I18n.t("voice.errors.agent_dispatch_failed"), status: 503)
+ end
+
def kick
guardian.ensure_can_manage_voice_room!(@room)
@@ -498,6 +509,8 @@ module Voice
raise Discourse::InvalidParameters.new(I18n.t("voice.errors.cannot_kick_creator"))
end
+ Voice::AgentManager.evict!(room: @room, user_id: user_id) if user_id.negative?
+
session = close_session_for(@room.id, user_id)
Voice::ParticipantTracker.mark_left(@room.id, user_id)
Voice::ParticipantTracker.remove(@room.id, user_id)
@@ -511,7 +524,13 @@ module Voice
# The client-side kicked handler already forces a clean leave; this
# additionally evicts the media session from the SFU.
- Voice::Livekit::RoomServiceClient.remove_participant(@room, user_id)
+ Voice::Livekit::RoomServiceClient.remove_participant(@room, user_id) unless user_id.negative?
+
+ if Voice::ParticipantTracker.human_user_ids(@room.id).empty? &&
+ Voice::AgentManager.provider_room?(@room.id)
+ Voice::AgentManager.evict_agents_in_room!(@room)
+ Voice::RoomBroadcaster.publish_participants(@room)
+ end
head :no_content
end
diff --git a/plugins/voice/app/jobs/scheduled/publish_room_participants.rb b/plugins/voice/app/jobs/scheduled/publish_room_participants.rb
index 006a8210d4b..f54ac65a2be 100644
--- a/plugins/voice/app/jobs/scheduled/publish_room_participants.rb
+++ b/plugins/voice/app/jobs/scheduled/publish_room_participants.rb
@@ -15,7 +15,8 @@ module Jobs
def execute(args)
return unless ::Voice.enabled?
- room_ids = ::Voice::ParticipantTracker.recently_active_room_ids
+ provider_room_ids = ::Voice::AgentManager.provider_room_ids
+ room_ids = ::Voice::ParticipantTracker.recently_active_room_ids | provider_room_ids
# Auto voice statuses have no ends_at, so a lapsed heartbeat must drop
# the status the same way it drops the roster entry. Live-anywhere is
@@ -26,20 +27,27 @@ module Jobs
return if room_ids.empty?
+ remaining_provider_room_ids = provider_room_ids.to_set
::Voice::Room
.where(id: room_ids)
.find_each do |room|
+ remaining_provider_room_ids.delete(room.id)
# Backstop for the pin-clear on last leave: a room that emptied
# without one (crashed clients, missed leave) must not hold its
# transport for the next call.
- if ::Voice::ParticipantTracker.user_ids(room.id).empty?
- # No-op once the pin is gone, so an emptied room is deleted from
- # the SFU at most once, on the sweep that clears its pin.
- ::Voice::Livekit::RoomServiceClient.delete_room(room)
- ::Voice::ParticipantTracker.clear_transport_pin(room.id)
+ if ::Voice::ParticipantTracker.human_user_ids(room.id).empty?
+ # Agent cleanup survives expired pins and retries provider failures.
+ ::Voice::AgentManager.evict_agents_in_room!(room)
+ else
+ ::Voice::AgentManager.reconcile(room)
end
::Voice::RoomBroadcaster.publish_participants(room)
end
+
+ # Provider cleanup must outlive the database row when deletion fails remotely.
+ remaining_provider_room_ids.each do |room_id|
+ ::Voice::AgentManager.evict_agents_in_room!(::Voice::Room.new(id: room_id))
+ end
end
end
end
diff --git a/plugins/voice/app/jobs/scheduled/voice/close_orphaned_sessions.rb b/plugins/voice/app/jobs/scheduled/voice/close_orphaned_sessions.rb
index 4fe2f1a1999..b59a56d4b9c 100644
--- a/plugins/voice/app/jobs/scheduled/voice/close_orphaned_sessions.rb
+++ b/plugins/voice/app/jobs/scheduled/voice/close_orphaned_sessions.rb
@@ -24,6 +24,11 @@ module Jobs
session.close!(at: left_at)
::Voice::ParticipantTracker.remove(session.room_id, session.user_id)
+ room = ::Voice::Room.find_by(id: session.room_id)
+ if room && ::Voice::ParticipantTracker.human_user_ids(room.id).empty?
+ ::Voice::AgentManager.evict_agents_in_room!(room)
+ end
+
user = User.find_by(id: session.user_id)
::Voice::BadgeGranterHooks.on_leave(user, session) if user
end
diff --git a/plugins/voice/app/jobs/scheduled/voice/update_co_presence.rb b/plugins/voice/app/jobs/scheduled/voice/update_co_presence.rb
index c7b9e64d86c..876f7d3d1e5 100644
--- a/plugins/voice/app/jobs/scheduled/voice/update_co_presence.rb
+++ b/plugins/voice/app/jobs/scheduled/voice/update_co_presence.rb
@@ -17,6 +17,7 @@ module Jobs
user_ids =
::Voice::ParticipantTracker
.user_ids(room_id)
+ .select(&:positive?)
.sort
.first(SiteSetting.voice_max_room_participants)
next if user_ids.size < 2
diff --git a/plugins/voice/app/services/voice/agent_bot.rb b/plugins/voice/app/services/voice/agent_bot.rb
new file mode 100644
index 00000000000..b8a00cdb0d2
--- /dev/null
+++ b/plugins/voice/app/services/voice/agent_bot.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+module Voice
+ class AgentBot
+ USERNAME = "livekit_agent_bot"
+ STORE_KEY = "livekit_agent_bot_id"
+
+ def self.user
+ id = PluginStore.get("voice", STORE_KEY).to_i
+ User.find_by(id:) if id < -1
+ end
+
+ def self.ensure!
+ return unless SiteSetting.voice_livekit_agent_enabled
+
+ DistributedMutex.synchronize("voice:agent_bot") do
+ user ||
+ User.transaction do
+ bot =
+ User.create!(
+ id: [User.minimum(:id).to_i, -1].min - 1,
+ username: USERNAME,
+ name: "LiveKit agent",
+ email: "#{SecureRandom.hex(16)}@invalid.invalid",
+ active: true,
+ approved: true,
+ trust_level: TrustLevel[1],
+ )
+ PluginStore.set("voice", STORE_KEY, bot.id)
+ bot
+ end
+ end
+ end
+
+ def self.available?
+ bot = user
+ SiteSetting.voice_livekit_agent_enabled && Livekit.cloud? && Livekit.configured? &&
+ bot&.active? && !bot.suspended?
+ end
+ end
+end
diff --git a/plugins/voice/app/services/voice/agent_dispatcher.rb b/plugins/voice/app/services/voice/agent_dispatcher.rb
new file mode 100644
index 00000000000..80f4d3b94c2
--- /dev/null
+++ b/plugins/voice/app/services/voice/agent_dispatcher.rb
@@ -0,0 +1,151 @@
+# frozen_string_literal: true
+
+module Voice
+ class AgentDispatcher
+ class DispatchError < StandardError
+ end
+
+ def self.dispatch!(room:, agent_name:)
+ unless agent_name.is_a?(String) && agent_name.strip.present? && agent_name.length <= 256
+ raise Discourse::InvalidParameters.new(:agent_name)
+ end
+ agent_name = agent_name.strip
+ DistributedMutex.synchronize("voice:dispatch:#{room.id}") do
+ bot = AgentManager.authorize!(room:)
+ AgentManager.revoke_session(room.id, bot.id)
+ session_id = AgentManager.authorize_session!(room:, agent_name:)
+ record = {
+ "bot_user_id" => bot.id,
+ "session_id" => session_id,
+ "agent_name" => agent_name,
+ "metadata" => { voice_session_id: session_id }.to_json,
+ "created_at" => Time.now.to_i,
+ }
+ save(room.id, record)
+
+ result =
+ Livekit::AgentDispatchClient.create(room, agent_name:, metadata: record["metadata"])
+ if result[:ok] && result.dig(:data, "id").present?
+ record["dispatch_id"] = result[:data]["id"]
+ save(room.id, record)
+ else
+ AgentManager.revoke_session(room.id, bot.id)
+ raise DispatchError
+ end
+
+ unless authorized_session(room, record)
+ cancel(room.id, bot.id)
+ raise AgentManager::AuthorizationError
+ end
+ record["dispatch_id"]
+ end
+ end
+
+ # Keep cleanup records separate from admission proofs so revocation still
+ # takes effect when the provider is unavailable, and deletion can be retried.
+ def self.cancel(room_id, user_id = nil)
+ room = Room.find_by(id: room_id) || Room.new(id: room_id)
+
+ records(room_id).each do |record|
+ next if user_id && record["bot_user_id"] != user_id
+ record["revoked"] = true
+ save(room_id, record)
+ delete_dispatch(room, record) if record["dispatch_id"]
+ end
+ end
+
+ def self.bot_user_ids(room_id)
+ records(room_id).map { |record| record["bot_user_id"] }.uniq
+ end
+
+ def self.pending?(room_id)
+ Discourse.redis.hlen(key(room_id)).positive?
+ end
+
+ def self.sessions(room)
+ pending = records(room.id)
+ return {} if pending.empty?
+
+ result = Livekit::AgentDispatchClient.list(room)
+ return {} unless result[:ok]
+
+ dispatches = result[:data]["agentDispatches"] || result[:data]["agent_dispatches"] || []
+ pending.each_with_object({}) do |record, sessions|
+ dispatch =
+ dispatches.find do |candidate|
+ candidate["room"] == Livekit.room_name(room) &&
+ (candidate["agentName"] || candidate["agent_name"]) == record["agent_name"] &&
+ candidate["metadata"] == record["metadata"] &&
+ (!record["dispatch_id"] || candidate["id"] == record["dispatch_id"])
+ end
+ unless dispatch
+ # A timed-out create may still complete remotely. Retain its nonce
+ # while the request settles so a later sweep can find and delete it.
+ if record["created_at"] < 2.minutes.ago.to_i
+ Discourse.redis.hdel(key(room.id), record["session_id"])
+ end
+ next
+ end
+
+ record["dispatch_id"] = dispatch["id"]
+ identities =
+ Array(dispatch.dig("state", "jobs")).filter_map do |job|
+ state = job["state"] || {}
+ identity = state["participantIdentity"] || state["participant_identity"]
+ next if [1, "JS_RUNNING"].exclude?(state["status"])
+ next unless (job["dispatchId"] || job["dispatch_id"]) == dispatch["id"]
+ next if identity.blank? || /\A-?\d+\z/.match?(identity)
+
+ identity
+ end
+ record["participant_identity"] = identities.first if identities.one?
+ save(room.id, record)
+ session = record["revoked"] ? nil : authorized_session(room, record)
+ unless session
+ delete_dispatch(room, record)
+ next
+ end
+
+ sessions[identities.first] = session if identities.one?
+ end
+ end
+
+ def self.authorized_session(room, record)
+ AgentManager.authorized_session(
+ room:,
+ user_id: record["bot_user_id"],
+ metadata: record["session_id"],
+ )
+ end
+ private_class_method :authorized_session
+
+ def self.delete_dispatch(room, record)
+ if Livekit::AgentDispatchClient.delete(room, record["dispatch_id"])[:ok]
+ Discourse.redis.hdel(key(room.id), record["session_id"])
+ elsif record["participant_identity"]
+ Livekit::RoomServiceClient.remove_participant(
+ room,
+ record["bot_user_id"],
+ identity: record["participant_identity"],
+ )
+ end
+ end
+ private_class_method :delete_dispatch
+
+ def self.records(room_id)
+ Discourse.redis.hvals(key(room_id)).map { |raw| JSON.parse(raw) }
+ end
+ private_class_method :records
+
+ def self.save(room_id, record)
+ Discourse.redis.hset(key(room_id), record["session_id"], record.to_json)
+ Discourse.redis.sadd(AgentManager::PROVIDER_ROOMS_KEY, room_id)
+ end
+ private_class_method :save
+
+ def self.key(room_id)
+ "voice:agent:dispatches:#{room_id}"
+ end
+ private_class_method :key
+ end
+end
diff --git a/plugins/voice/app/services/voice/agent_manager.rb b/plugins/voice/app/services/voice/agent_manager.rb
new file mode 100644
index 00000000000..55ca651e829
--- /dev/null
+++ b/plugins/voice/app/services/voice/agent_manager.rb
@@ -0,0 +1,159 @@
+# frozen_string_literal: true
+
+module Voice
+ class AgentManager
+ SESSION_TTL = 15.minutes
+ PROVIDER_ROOMS_KEY = "voice:agent:provider_rooms"
+
+ class AuthorizationError < StandardError
+ end
+
+ class << self
+ def authorize!(room:)
+ bot = AgentBot.user
+ unless Voice.enabled? && AgentBot.available? && room.public? &&
+ ParticipantTracker.pinned_transport(room.id) == "livekit" &&
+ ParticipantTracker.human_user_ids(room.id).any?
+ raise AuthorizationError
+ end
+ bot
+ end
+
+ def authorize_session!(room:, agent_name:)
+ bot = authorize!(room:)
+ Discourse.redis.sadd(PROVIDER_ROOMS_KEY, room.id)
+ session_id = SecureRandom.hex(16)
+ payload = { bot_user_id: bot.id, session_id:, agent_name: }
+ Discourse.redis.setex(session_key(room.id, bot.id), SESSION_TTL.to_i, payload.to_json)
+ session_id
+ end
+
+ def authorized_session(room:, user_id:, metadata:)
+ raw = Discourse.redis.get(session_key(room.id, user_id))
+ return if raw.blank?
+ session = JSON.parse(raw, symbolize_names: true)
+ return unless metadata == session[:session_id]
+ bot = authorize!(room:)
+ return unless bot.id == user_id && session[:bot_user_id] == bot.id
+
+ session.merge(bot:, proof: raw)
+ rescue JSON::ParserError, AuthorizationError
+ nil
+ end
+
+ def revoke_session(room_id, user_id)
+ ParticipantTracker.revoke_agent(
+ room_id,
+ user_id,
+ session_key: session_key(room_id, user_id),
+ )
+ AgentDispatcher.cancel(room_id, user_id)
+ end
+
+ def evict!(room:, user_id:)
+ dispatched = AgentDispatcher.pending?(room.id)
+ identity = ParticipantTracker.get_metadata(room.id, user_id)[:livekit_identity]
+ revoke_session(room.id, user_id)
+ ParticipantTracker.mark_left(room.id, user_id)
+ ParticipantTracker.remove(room.id, user_id)
+ unless dispatched
+ Livekit::RoomServiceClient.remove_participant(
+ room,
+ user_id,
+ **(identity ? { identity: } : {}),
+ )
+ end
+ end
+
+ def stop_all!
+ Room
+ .where(id: provider_room_ids)
+ .find_each do |room|
+ revoke_room_sessions(room)
+ AgentDispatcher.cancel(room.id)
+ RoomBroadcaster.publish_participants(room)
+ end
+ end
+
+ def evict_agents_in_room!(room, delete_room: true)
+ agent_user_ids = ParticipantTracker.agent_user_ids(room.id)
+ revoke_room_sessions(room)
+ AgentDispatcher.cancel(room.id)
+ agent_user_ids.each { |user_id| evict!(room:, user_id:) }
+ AgentDispatcher.sessions(room) if AgentDispatcher.pending?(room.id)
+ room_deleted = !delete_room || Livekit::RoomServiceClient.delete_room(room)
+ if room_deleted && !AgentDispatcher.pending?(room.id)
+ Discourse.redis.srem(PROVIDER_ROOMS_KEY, room.id)
+ end
+ ParticipantTracker.clear_transport_pin(room.id)
+ end
+
+ def provider_room_ids
+ Discourse.redis.smembers(PROVIDER_ROOMS_KEY).map(&:to_i)
+ end
+
+ def provider_room?(room_id)
+ Discourse.redis.sismember(PROVIDER_ROOMS_KEY, room_id)
+ end
+
+ def reconcile(room)
+ return unless provider_room?(room.id) || ParticipantTracker.agent_user_ids(room.id).any?
+ return evict_agents_in_room!(room) if ParticipantTracker.human_user_ids(room.id).empty?
+
+ sessions = AgentDispatcher.sessions(room)
+ participants = Livekit::RoomServiceClient.list_participants(Livekit.room_name(room))
+ return unless participants[:ok]
+
+ live_agents = Set.new
+ Array(participants.dig(:data, "participants")).each do |participant|
+ identity = participant["identity"].to_s
+ session = sessions[identity]
+ next unless session
+ next if [4, "AGENT"].exclude?(participant["kind"])
+
+ bot = session[:bot]
+ next unless Livekit::RoomServiceClient.update_participant(room, bot, identity:)
+ metadata = {
+ external_agent: true,
+ livekit_identity: identity,
+ role: "speaker",
+ last_heartbeat_at: Time.now.to_f,
+ }
+ unless ParticipantTracker.commit_agent(
+ room.id,
+ bot.id,
+ session_key: session_key(room.id, bot.id),
+ proof: session[:proof],
+ ttl: SESSION_TTL.to_i,
+ metadata:,
+ sid: participant["sid"],
+ )
+ Livekit::RoomServiceClient.remove_participant(room, bot.id, identity:)
+ next
+ end
+ live_agents << bot.id
+ end
+
+ ParticipantTracker
+ .agent_user_ids(room.id)
+ .each do |user_id|
+ ParticipantTracker.expire_presence(room.id, user_id) if live_agents.exclude?(user_id)
+ end
+ RoomBroadcaster.publish_participants_if_changed(room)
+ end
+
+ private
+
+ def revoke_room_sessions(room)
+ return unless provider_room?(room.id)
+
+ user_ids = AgentDispatcher.bot_user_ids(room.id) | [AgentBot.user&.id].compact
+ user_ids.each { |user_id| revoke_session(room.id, user_id) }
+ end
+
+ def session_key(room_id, user_id)
+ "voice:agent:session:#{room_id}:#{user_id}"
+ end
+ end
+ end
+end
diff --git a/plugins/voice/app/services/voice/badge_granter_hooks.rb b/plugins/voice/app/services/voice/badge_granter_hooks.rb
index 80bb3f2994c..3466014df03 100644
--- a/plugins/voice/app/services/voice/badge_granter_hooks.rb
+++ b/plugins/voice/app/services/voice/badge_granter_hooks.rb
@@ -15,6 +15,8 @@ module Voice
def self.on_join(user, room, participants)
return unless badges_enabled?
+ participants = participants.reject(&:bot?)
+
grant("Packed House", user) if room_full?(room, participants)
grant("Icebreaker", user) if icebreaker?(user, participants)
end
@@ -55,6 +57,8 @@ module Voice
private
def grant(badge_name, user)
+ return if user.bot?
+
badge = Badge.find_by(name: badge_name)
BadgeGranter.grant(badge, user) if badge&.enabled?
end
diff --git a/plugins/voice/app/services/voice/ephemeral_room_manager.rb b/plugins/voice/app/services/voice/ephemeral_room_manager.rb
index 9e3f9b1acf9..1dc42a76d20 100644
--- a/plugins/voice/app/services/voice/ephemeral_room_manager.rb
+++ b/plugins/voice/app/services/voice/ephemeral_room_manager.rb
@@ -63,7 +63,7 @@ module Voice
ttl = SiteSetting.voice_ephemeral_room_ttl_minutes.minutes
Voice::Room.ephemeral.find_each do |room|
- if Voice::ParticipantTracker.user_ids(room.id).any?
+ if Voice::ParticipantTracker.human_user_ids(room.id).any?
room.update_column(:last_occupied_at, Time.current)
elsif (room.last_occupied_at || room.created_at) < ttl.ago
destroy!(room)
diff --git a/plugins/voice/app/services/voice/participant_tracker.rb b/plugins/voice/app/services/voice/participant_tracker.rb
index 8b464e66fe1..3d3a785002c 100644
--- a/plugins/voice/app/services/voice/participant_tracker.rb
+++ b/plugins/voice/app/services/voice/participant_tracker.rb
@@ -5,6 +5,7 @@ module Voice
KEY_NAMESPACE = "voice:room"
RECENTLY_ACTIVE_ROOMS_KEY = "voice:recently_active_rooms"
SAFETY_TTL = 30.minutes.to_i
+ AGENT_PRESENCE_TTL = 3.minutes.to_i
RoomState =
Data.define(:participant_ids, :participant_metadata, :pinned_transport, :recording_info)
# Must outlive one client heartbeat interval (10s) plus request latency,
@@ -30,7 +31,11 @@ module Voice
return 1
end
- if redis.call('ZCARD', KEYS[1]) >= capacity then
+ local humans = 0
+ for _, member in ipairs(redis.call('ZRANGE', KEYS[1], 0, -1)) do
+ if tonumber(member) > 0 then humans = humans + 1 end
+ end
+ if humans >= capacity then
return -1
end
@@ -39,11 +44,61 @@ module Voice
return 2
LUA
+ COMMIT_AGENT = DiscourseRedis::EvalHelper.new <<~LUA
+ if redis.call('GET', KEYS[1]) ~= ARGV[1] then return 0 end
+ redis.call('EXPIRE', KEYS[1], ARGV[2])
+ redis.call('ZADD', KEYS[2], ARGV[3], ARGV[4])
+ redis.call('HSET', KEYS[3], ARGV[4], ARGV[5])
+ redis.call('HSET', KEYS[4], ARGV[4], ARGV[6])
+ for index = 2, 4 do redis.call('EXPIRE', KEYS[index], ARGV[7]) end
+ return 1
+ LUA
+
+ REVOKE_AGENT = DiscourseRedis::EvalHelper.new <<~LUA
+ redis.call('DEL', KEYS[1])
+ redis.call('ZREM', KEYS[2], ARGV[1])
+ redis.call('HDEL', KEYS[3], ARGV[1])
+ redis.call('HDEL', KEYS[4], ARGV[1])
+ return 1
+ LUA
+
class << self
- def add(room_id, user_id, migrated: false)
- return if user_id.to_i <= 0
+ # Admission and invalidation share an atomic boundary so a stale provider
+ # snapshot cannot restore presence after a moderator has revoked it.
+ def commit_agent(room_id, user_id, session_key:, proof:, ttl:, metadata:, sid:)
+ score =
+ Time.now.to_f + [AGENT_PRESENCE_TTL - SiteSetting.voice_participant_ttl_seconds, 0].max
+ keys = [session_key, key(room_id), metadata_key(room_id), livekit_sid_key(room_id)]
+ committed =
+ COMMIT_AGENT.eval(
+ redis.without_namespace,
+ keys.map { |name| redis.namespace_key(name) },
+ [proof, ttl, score, user_id, metadata.to_json, sid.to_s, SAFETY_TTL],
+ ) == 1
+ touch_recently_active(room_id) if committed
+ committed
+ end
+
+ def revoke_agent(room_id, user_id, session_key:)
+ keys = [session_key, key(room_id), metadata_key(room_id), livekit_sid_key(room_id)]
+ REVOKE_AGENT.eval(
+ redis.without_namespace,
+ keys.map { |name| redis.namespace_key(name) },
+ [user_id],
+ )
+ touch_recently_active(room_id)
+ end
- redis.zadd(key(room_id), Time.now.to_f, user_id)
+ def add(room_id, user_id, migrated: false)
+ return if user_id.to_i.zero?
+
+ score = Time.now.to_f
+ # Agents are verified by the minute sweep, not browser heartbeats.
+ score += [
+ AGENT_PRESENCE_TTL - SiteSetting.voice_participant_ttl_seconds,
+ 0,
+ ].max if user_id.to_i.negative?
+ redis.zadd(key(room_id), score, user_id)
redis.expire(key(room_id), SAFETY_TTL)
redis.expire(metadata_key(room_id), SAFETY_TTL)
touch_recently_active(room_id)
@@ -173,7 +228,7 @@ module Voice
# small (bounded by max_participants).
score = redis.zrange(key(room_id), 0, -1, withscores: true).to_h[user_id.to_s]
return false if score.nil?
- return false if gone_at && score > gone_at.to_f
+ return false if user_id.to_i.positive? && gone_at && score > gone_at.to_f
expired_score = Time.now.to_f - SiteSetting.voice_participant_ttl_seconds - 1
redis.zadd(key(room_id), [score, expired_score].min, user_id, xx: true)
@@ -216,7 +271,7 @@ module Voice
[
room_id,
RoomState.new(
- participant_ids: participant_futures[room_id].value.map(&:to_i).select(&:positive?),
+ participant_ids: participant_futures[room_id].value.map(&:to_i).reject(&:zero?),
participant_metadata: deserialize_all_metadata(metadata_futures[room_id].value),
pinned_transport: transport,
recording_info: deserialize_recording(raw_recording),
@@ -231,13 +286,21 @@ module Voice
def user_ids(room_id, migrated: false)
cutoff = Time.now.to_f - SiteSetting.voice_participant_ttl_seconds
- redis.zrangebyscore(key(room_id), cutoff, "+inf").map(&:to_i).select(&:positive?)
+ redis.zrangebyscore(key(room_id), cutoff, "+inf").map(&:to_i).reject(&:zero?)
rescue Redis::CommandError => e
raise if e.message.exclude?("WRONGTYPE") || migrated
redis.del(key(room_id))
user_ids(room_id, migrated: true)
end
+ def human_user_ids(room_id)
+ user_ids(room_id).select(&:positive?)
+ end
+
+ def agent_user_ids(room_id)
+ user_ids(room_id).select(&:negative?)
+ end
+
def last_heartbeat_at(room_id, user_id)
metadata = get_metadata(room_id, user_id)
ts = metadata[:last_heartbeat_at]
diff --git a/plugins/voice/app/validators/voice_livekit_agent_validator.rb b/plugins/voice/app/validators/voice_livekit_agent_validator.rb
new file mode 100644
index 00000000000..71fbaa518f3
--- /dev/null
+++ b/plugins/voice/app/validators/voice_livekit_agent_validator.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+class VoiceLivekitAgentValidator
+ def initialize(opts = {})
+ end
+
+ def valid_value?(value)
+ return true unless value.to_s == "t" || value == true
+
+ @error_key =
+ if !Voice::Livekit.cloud? || !Voice::Livekit.configured?
+ "voice_livekit_agent_requires_cloud"
+ elsif User
+ .where(username_lower: Voice::AgentBot::USERNAME)
+ .where.not(id: Voice::AgentBot.user&.id)
+ .exists?
+ "voice_livekit_agent_username_taken"
+ end
+ @error_key.nil?
+ end
+
+ def error_message
+ I18n.t("site_settings.errors.#{@error_key}") if @error_key
+ end
+end
diff --git a/plugins/voice/assets/javascripts/discourse/app/services/voice-webrtc.js b/plugins/voice/assets/javascripts/discourse/app/services/voice-webrtc.js
index 83b5c4777a1..7108de0b7a4 100644
--- a/plugins/voice/assets/javascripts/discourse/app/services/voice-webrtc.js
+++ b/plugins/voice/assets/javascripts/discourse/app/services/voice-webrtc.js
@@ -341,8 +341,13 @@ export default class VoiceWebrtcService extends Service {
camera: this.effectiveCameraQuality(roomId),
screen: this.effectiveScreenQuality(roomId),
}),
- onTrack: (roomId, userId, track, streams) =>
- this.#remoteStreamRegistry.register(roomId, userId, track, streams),
+ onTrack: (roomId, userId, track, streams) => {
+ const room = this.voiceRooms.roomById(roomId);
+ if (userId < 0 && (!room || !participantCanSpeak(room, userId))) {
+ return;
+ }
+ this.#remoteStreamRegistry.register(roomId, userId, track, streams);
+ },
removeRemoteStream: (roomId, userId) =>
this.#removeRemoteStream(roomId, userId),
getRemoteUserIds: (roomId) =>
diff --git a/plugins/voice/assets/javascripts/discourse/components/modal/voice-invite-agent.gjs b/plugins/voice/assets/javascripts/discourse/components/modal/voice-invite-agent.gjs
new file mode 100644
index 00000000000..74e4dcf8b44
--- /dev/null
+++ b/plugins/voice/assets/javascripts/discourse/components/modal/voice-invite-agent.gjs
@@ -0,0 +1,160 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { hash } from "@ember/helper";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import Form from "discourse/components/form";
+import { ajax } from "discourse/lib/ajax";
+import { popupAjaxError } from "discourse/lib/ajax-error";
+import DButton from "discourse/ui-kit/d-button";
+import DConditionalLoadingSpinner from "discourse/ui-kit/d-conditional-loading-spinner";
+import DModal from "discourse/ui-kit/d-modal";
+import { i18n } from "discourse-i18n";
+
+export default class VoiceInviteAgentModal extends Component {
+ @service toasts;
+
+ @tracked agents = [];
+ @tracked loading = true;
+ @tracked refreshing = false;
+ @tracked typing = false;
+
+ constructor() {
+ super(...arguments);
+ this.#loadAgents();
+ }
+
+ get showPicker() {
+ return this.agents.length > 0 && !this.typing;
+ }
+
+ @action
+ toggleTyping() {
+ this.typing = !this.typing;
+ }
+
+ @action
+ async refresh() {
+ this.refreshing = true;
+ try {
+ await this.#loadAgents({ refresh: true });
+ } finally {
+ this.refreshing = false;
+ }
+ }
+
+ @action
+ async invite(data) {
+ try {
+ await ajax(`/voice/rooms/${this.args.model.room.id}/invite_agent`, {
+ type: "POST",
+ data: { agent_name: data.agent_name.trim() },
+ });
+ this.toasts.success({ data: { message: i18n("voice.agent.invited") } });
+ this.args.closeModal();
+ } catch (error) {
+ popupAjaxError(error);
+ }
+ }
+
+ // An empty or unavailable catalogue falls back to the typed name, which is
+ // also the only path for agents running outside LiveKit's hosting. Only an
+ // explicit refresh surfaces the failure.
+ async #loadAgents({ refresh = false } = {}) {
+ try {
+ const result = await ajax("/voice/agents", {
+ data: refresh ? { refresh: true } : {},
+ });
+ this.agents = result.agents ?? [];
+ } catch (error) {
+ this.agents = [];
+ if (refresh) {
+ popupAjaxError(error);
+ }
+ } finally {
+ this.loading = false;
+ }
+ }
+
+ <template>
+ <DModal
+ class="voice-invite-agent-modal"
+ @closeModal={{@closeModal}}
+ @inline={{@inline}}
+ @title={{i18n "voice.agent.invite"}}
+ >
+ <:body>
+ <DConditionalLoadingSpinner @condition={{this.loading}}>
+ <Form
+ @data={{hash agent_name=""}}
+ @onSubmit={{this.invite}}
+ as |form|
+ >
+ {{#if this.showPicker}}
+ <form.Field
+ @format="large"
+ @name="agent_name"
+ @title={{i18n "voice.agent.name"}}
+ @type="select"
+ @validation="required"
+ as |field|
+ >
+ <div class="voice-invite-agent-modal__picker">
+ <field.Control as |select|>
+ {{#each this.agents as |agent|}}
+ <select.Option @value={{agent.name}}>
+ {{agent.name}}
+ </select.Option>
+ {{/each}}
+ </field.Control>
+ <DButton
+ class="btn-flat voice-invite-agent-modal__toggle"
+ @action={{this.toggleTyping}}
+ @icon="pencil"
+ @title="voice.agent.type_name"
+ />
+ <DButton
+ class="btn-flat voice-invite-agent-modal__refresh"
+ @action={{this.refresh}}
+ @icon="arrows-rotate"
+ @isLoading={{this.refreshing}}
+ @title="voice.agent.refresh"
+ />
+ </div>
+ </form.Field>
+ {{else}}
+ <form.Field
+ @format="large"
+ @name="agent_name"
+ @title={{i18n "voice.agent.name"}}
+ @type="input"
+ @validation="required"
+ as |field|
+ >
+ <div class="voice-invite-agent-modal__picker">
+ <field.Control maxlength="256" />
+ {{#if this.agents.length}}
+ <DButton
+ class="btn-flat voice-invite-agent-modal__toggle"
+ @action={{this.toggleTyping}}
+ @icon="list"
+ @title="voice.agent.pick_name"
+ />
+ {{/if}}
+ <DButton
+ class="btn-flat voice-invite-agent-modal__refresh"
+ @action={{this.refresh}}
+ @icon="arrows-rotate"
+ @isLoading={{this.refreshing}}
+ @title="voice.agent.refresh"
+ />
+ </div>
+ </form.Field>
+ {{/if}}
+ <form.Submit @label="voice.agent.invite" />
+ </Form>
+ </DConditionalLoadingSpinner>
+ </:body>
+ </DModal>
+ </template>
+}
diff --git a/plugins/voice/assets/javascripts/discourse/components/voice-invite-agent-button.gjs b/plugins/voice/assets/javascripts/discourse/components/voice-invite-agent-button.gjs
new file mode 100644
index 00000000000..7a0d4bee2d3
--- /dev/null
+++ b/plugins/voice/assets/javascripts/discourse/components/voice-invite-agent-button.gjs
@@ -0,0 +1,42 @@
+import Component from "@glimmer/component";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import DButton from "discourse/ui-kit/d-button";
+import VoiceInviteAgentModal from "./modal/voice-invite-agent";
+
+export default class VoiceInviteAgentButton extends Component {
+ @service site;
+ @service modal;
+
+ get available() {
+ const room = this.args.room;
+ const botId = this.site.voice_livekit_agent_bot_id;
+ const participants = room.active_participants ?? [];
+ return (
+ botId &&
+ room.public &&
+ room.expected_transport === "livekit" &&
+ participants.some((participant) => participant.id > 0) &&
+ !participants.some((participant) => participant.id === botId)
+ );
+ }
+
+ @action
+ invite() {
+ this.args.closeMenu?.();
+ this.modal.show(VoiceInviteAgentModal, { model: { room: this.args.room } });
+ }
+
+ <template>
+ {{#if this.available}}
+ <@item>
+ <DButton
+ class="btn-transparent voice-invite-agent"
+ @action={{this.invite}}
+ @icon="robot"
+ @label="voice.agent.invite"
+ />
+ </@item>
+ {{/if}}
+ </template>
+}
diff --git a/plugins/voice/assets/javascripts/discourse/components/voice-participant-sidebar-context-menu.gjs b/plugins/voice/assets/javascripts/discourse/components/voice-participant-sidebar-context-menu.gjs
index 97ca6c82eff..dcbabb95cbc 100644
--- a/plugins/voice/assets/javascripts/discourse/components/voice-participant-sidebar-context-menu.gjs
+++ b/plugins/voice/assets/javascripts/discourse/components/voice-participant-sidebar-context-menu.gjs
@@ -93,6 +93,7 @@ export default class VoiceParticipantSidebarContextMenu extends Component {
return (
this.canManageRoom &&
this.isStageRoom &&
+ !this.participant.external_agent &&
!this.isCurrentUser &&
!this.participantIsSpeakerOrMod
);
@@ -102,6 +103,7 @@ export default class VoiceParticipantSidebarContextMenu extends Component {
return (
this.canManageRoom &&
this.isStageRoom &&
+ !this.participant.external_agent &&
!this.isCurrentUser &&
this.participant.role === "speaker"
);
diff --git a/plugins/voice/assets/javascripts/discourse/components/voice-room-sidebar-context-menu.gjs b/plugins/voice/assets/javascripts/discourse/components/voice-room-sidebar-context-menu.gjs
index 0c260c27a08..518437dc943 100644
--- a/plugins/voice/assets/javascripts/discourse/components/voice-room-sidebar-context-menu.gjs
+++ b/plugins/voice/assets/javascripts/discourse/components/voice-room-sidebar-context-menu.gjs
@@ -7,6 +7,7 @@ import DDropdownMenu from "discourse/ui-kit/d-dropdown-menu";
import { i18n } from "discourse-i18n";
import VoiceInviteUsersModal from "./modal/voice-invite-users";
import VoiceRoomInfoModal from "./modal/voice-room-info";
+import VoiceInviteAgentButton from "./voice-invite-agent-button";
export default class VoiceRoomSidebarContextMenu extends Component {
@service modal;
@@ -73,6 +74,11 @@ export default class VoiceRoomSidebarContextMenu extends Component {
<template>
<DDropdownMenu class="voice-room-sidebar-context-menu" as |dropdown|>
+ <VoiceInviteAgentButton
+ @closeMenu={{@close}}
+ @item={{dropdown.item}}
+ @room={{this.room}}
+ />
<dropdown.item>
<DButton
class="voice-room-sidebar-context-menu__open-page"
diff --git a/plugins/voice/assets/javascripts/discourse/components/voice/call-widget.gjs b/plugins/voice/assets/javascripts/discourse/components/voice/call-widget.gjs
index f0a9b015778..e087d38bd05 100644
--- a/plugins/voice/assets/javascripts/discourse/components/voice/call-widget.gjs
+++ b/plugins/voice/assets/javascripts/discourse/components/voice/call-widget.gjs
@@ -20,6 +20,7 @@ import {
} from "../../lib/voice/video-grid-layout";
import VoiceInviteUsersModal from "../modal/voice-invite-users";
import VoiceRoomInfoModal from "../modal/voice-room-info";
+import VoiceInviteAgentButton from "../voice-invite-agent-button";
import VoiceCallControls from "./call-controls";
import VoiceRecordingBadge from "./recording-badge";
import VoiceRingingTile from "./ringing-tile";
@@ -786,6 +787,11 @@ export default class VoiceCallWidget extends Component {
>
<:content as |roomMenu|>
<DDropdownMenu as |dropdown|>
+ <VoiceInviteAgentButton
+ @closeMenu={{roomMenu.close}}
+ @item={{dropdown.item}}
+ @room={{this.room}}
+ />
{{#if this.chatAvailable}}
<dropdown.item>
<DButton
diff --git a/plugins/voice/assets/javascripts/discourse/components/voice/room-page.gjs b/plugins/voice/assets/javascripts/discourse/components/voice/room-page.gjs
index d80f0f60e5e..8345895d632 100644
--- a/plugins/voice/assets/javascripts/discourse/components/voice/room-page.gjs
+++ b/plugins/voice/assets/javascripts/discourse/components/voice/room-page.gjs
@@ -28,6 +28,7 @@ import {
} from "../../lib/voice/video-grid-layout";
import VoiceInviteUsersModal from "../modal/voice-invite-users";
import VoiceRoomInfoModal from "../modal/voice-room-info";
+import VoiceInviteAgentButton from "../voice-invite-agent-button";
import VoiceCallControls from "./call-controls";
import VoiceCallSubmenu from "./call-submenu";
import VoiceCaptionOverlay from "./caption-overlay";
@@ -698,6 +699,11 @@ export default class VoiceRoomPage extends Component {
>
<:content as |roomMenu|>
<DDropdownMenu as |dropdown|>
+ <VoiceInviteAgentButton
+ @closeMenu={{roomMenu.close}}
+ @item={{dropdown.item}}
+ @room={{this.room}}
+ />
{{#if this.chatAvailable}}
<dropdown.item>
<DButton
diff --git a/plugins/voice/assets/javascripts/discourse/lib/voice/livekit-coordinator.js b/plugins/voice/assets/javascripts/discourse/lib/voice/livekit-coordinator.js
index 46bca3b73ba..3bbf205da30 100644
--- a/plugins/voice/assets/javascripts/discourse/lib/voice/livekit-coordinator.js
+++ b/plugins/voice/assets/javascripts/discourse/lib/voice/livekit-coordinator.js
@@ -174,11 +174,7 @@ export default class LivekitCoordinator {
for (const participant of participants) {
const participantId = Number(participant?.id);
- if (
- !participantId ||
- participantId <= 0 ||
- participantId === currentUserId
- ) {
+ if (!participantId || participantId === currentUserId) {
continue;
}
@@ -203,6 +199,19 @@ export default class LivekitCoordinator {
this.#rosterIds.set(roomId, next);
const session = this.#sessions.get(roomId);
+ session?.syncAgentIdentities(participants);
+ for (const participant of participants) {
+ const userId = Number(participant.id);
+ if (userId >= 0) {
+ continue;
+ }
+ if (participant.role !== "speaker") {
+ session?.dropParticipant(userId);
+ this.#removeRemoteStream(roomId, userId);
+ } else if (!this.#getRemoteUserIds(roomId).includes(userId)) {
+ session?.restoreParticipant(userId);
+ }
+ }
for (const entryUserId of this.#getRemoteUserIds(roomId)) {
if (
entryUserId &&
diff --git a/plugins/voice/assets/javascripts/discourse/lib/voice/livekit-session.js b/plugins/voice/assets/javascripts/discourse/lib/voice/livekit-session.js
index 6cf9830030d..d4dd0709ac4 100644
--- a/plugins/voice/assets/javascripts/discourse/lib/voice/livekit-session.js
+++ b/plugins/voice/assets/javascripts/discourse/lib/voice/livekit-session.js
@@ -102,6 +102,7 @@ export default class LivekitRoomSession {
#watchingVideo = false;
#closed = false;
#reconnecting = false;
+ #agentIdentities = new Map();
constructor({
roomId,
@@ -293,7 +294,9 @@ export default class LivekitRoomSession {
// The SFU doesn't consult the plugin's roster, so a participant expelled
// from it can still hold live subscriptions; drop them explicitly.
dropParticipant(userId) {
- const participant = this.#room?.remoteParticipants?.get(String(userId));
+ const participant = this.#room?.remoteParticipants?.get(
+ this.#agentIdentities.get(userId) ?? String(userId)
+ );
participant?.trackPublications?.forEach((publication) => {
try {
@@ -304,6 +307,55 @@ export default class LivekitRoomSession {
});
}
+ restoreParticipant(userId) {
+ const participant = this.#room?.remoteParticipants?.get(
+ this.#agentIdentities.get(userId) ?? String(userId)
+ );
+ participant?.trackPublications?.forEach((publication) => {
+ if (this.#isWatchGatedSource(publication.source)) {
+ this.#applyDesiredSubscription(publication);
+ } else {
+ try {
+ publication.setSubscribed(true);
+ } catch {
+ // The publication is tearing down.
+ }
+ }
+ if (
+ !publication.track ||
+ (this.#isWatchGatedSource(publication.source) && !this.#watchingVideo)
+ ) {
+ return;
+ }
+ const track = publication.track;
+ const streams =
+ publication.source === this.#sdk.Track.Source.ScreenShareAudio
+ ? []
+ : [track.mediaStream ?? new MediaStream()];
+ this.#onTrack(this.#roomId, userId, track.mediaStreamTrack, streams);
+ });
+ }
+
+ syncAgentIdentities(participants) {
+ const next = new Map(
+ participants
+ .filter(
+ (participant) => participant.id < 0 && participant.livekit_identity
+ )
+ .map((participant) => [
+ Number(participant.id),
+ participant.livekit_identity,
+ ])
+ );
+ for (const [userId, identity] of this.#agentIdentities) {
+ if (next.get(userId) !== identity) {
+ this.dropParticipant(userId);
+ this.#onParticipantGone(this.#roomId, userId);
+ }
+ }
+ this.#agentIdentities = next;
+ }
+
// Terminal-disconnect recovery: up to three attempts, each awaiting a
// freshly minted token (the old one is likely past its 10-minute TTL).
// Resolves "reconnected", "gone" (server says the room instance ended),
@@ -515,10 +567,17 @@ export default class LivekitRoomSession {
}
#userIdFrom(participant) {
- // LiveKit identity is String(user.id); registry keys must be numeric so
- // remoteStreamFor(roomId, userId) matches roster participant ids.
- const userId = Number(participant?.identity);
- if (!Number.isFinite(userId) || userId <= 0) {
+ const identity = String(participant?.identity ?? "");
+ for (const [botId, agentIdentity] of this.#agentIdentities) {
+ if (identity === agentIdentity) {
+ return botId;
+ }
+ }
+ const userId = Number(identity);
+ if (!/^-?[1-9]\d*$/.test(identity) || !Number.isSafeInteger(userId)) {
+ return null;
+ }
+ if (this.#agentIdentities.has(userId)) {
return null;
}
return userId === this.#currentUserId ? null : userId;
diff --git a/plugins/voice/assets/javascripts/discourse/lib/voice/stage-roles.js b/plugins/voice/assets/javascripts/discourse/lib/voice/stage-roles.js
index d0fa69daf87..943f849c36b 100644
--- a/plugins/voice/assets/javascripts/discourse/lib/voice/stage-roles.js
+++ b/plugins/voice/assets/javascripts/discourse/lib/voice/stage-roles.js
@@ -1,7 +1,5 @@
-// Stage rooms gate speaking on the participant's role; every other room type
-// lets anyone speak.
export function participantCanSpeak(room, userId) {
- if (room.room_type !== "stage") {
+ if (room.room_type !== "stage" && Number(userId) > 0) {
return true;
}
diff --git a/plugins/voice/assets/stylesheets/common/voice-admin.scss b/plugins/voice/assets/stylesheets/common/voice-admin.scss
index 4484a52e895..c1cb6ab7641 100644
--- a/plugins/voice/assets/stylesheets/common/voice-admin.scss
+++ b/plugins/voice/assets/stylesheets/common/voice-admin.scss
@@ -175,20 +175,3 @@
margin-top: 0.5em;
}
}
-
-.voice-livekit-status__webhook-config {
- margin: 0.5em 0 1em;
-
- p {
- margin-bottom: 0.25em;
- color: var(--primary-medium);
- }
-
- pre {
- margin: 0;
- padding: 0.5em 0.75em;
- background: var(--primary-very-low);
- font-size: var(--font-down-1);
- overflow-x: auto;
- }
-}
diff --git a/plugins/voice/assets/stylesheets/common/voice.scss b/plugins/voice/assets/stylesheets/common/voice.scss
index 53f68505e32..25c6182c78d 100644
--- a/plugins/voice/assets/stylesheets/common/voice.scss
+++ b/plugins/voice/assets/stylesheets/common/voice.scss
@@ -1297,3 +1297,25 @@
opacity: 0;
}
}
+
+.voice-invite-agent-modal {
+ // The field content wrapper is a column flex box aligned to flex-start, so
+ // the row must stretch to the field width before its controls can share it.
+ &__picker {
+ display: flex;
+ align-items: center;
+ gap: 0.5em;
+ align-self: stretch;
+ min-width: 0;
+
+ .form-kit__control-input-wrapper,
+ .form-kit__control-select {
+ flex: 1 1 0;
+ min-width: 12em;
+ }
+
+ .form-kit__control-input {
+ width: 100%;
+ }
+ }
+}
diff --git a/plugins/voice/config/locales/client.en.yml b/plugins/voice/config/locales/client.en.yml
index 49e51861c09..a177af0e6d4 100644
--- a/plugins/voice/config/locales/client.en.yml
+++ b/plugins/voice/config/locales/client.en.yml
@@ -294,6 +294,13 @@ en:
memberships:
add_user: "Add participant"
remove_user: "Remove participant"
+ agent:
+ invite: "Invite LiveKit agent"
+ name: "Agent name"
+ type_name: "Type a name instead"
+ pick_name: "Choose from the list instead"
+ refresh: "Refresh the agent list"
+ invited: "Agent invited. It will appear when LiveKit connects it. This can take about a minute."
admin:
title: "Voice"
dashboard_title: "Dashboard"
diff --git a/plugins/voice/config/locales/server.en.yml b/plugins/voice/config/locales/server.en.yml
index e071b4d5285..10bdeb3a177 100644
--- a/plugins/voice/config/locales/server.en.yml
+++ b/plugins/voice/config/locales/server.en.yml
@@ -33,6 +33,8 @@ en:
voice_turn_credential: "Static password for the TURN servers in {{setting:voice_turn_servers}}."
voice_turn_secret: "Shared secret used to mint short-lived TURN credentials for the servers in {{setting:voice_turn_secret_servers}} (TURN REST API, coturn's use-auth-secret mode). Must match the secret configured on those TURN servers."
voice_turn_secret_servers: "TURN server URIs (turn:host:port or turns:host:port), separated by |, that authenticate with short-lived per-user credentials derived from {{setting:voice_turn_secret}} instead of a static username and password, so credentials cannot be reused by third parties."
+ voice_livekit_agent_enabled: "Allow members of {{setting:voice_livekit_agent_invite_allowed_groups}} to invite a LiveKit Cloud agent to public voice rooms. Creates the livekit_agent_bot account. Requires configured LiveKit Cloud credentials."
+ voice_livekit_agent_invite_allowed_groups: "Groups that can invite a LiveKit Cloud agent to a public voice room they can join. Admins are always included. Members also need to be in {{setting:voice_allowed_groups}}; room managers can kick an agent regardless of this setting."
voice_livekit_url: "WebSocket URL (wss://, or ws:// outside production) of a self-hosted LiveKit server. Together with the API key and secret, lets voice rooms use it as their media server (SFU) instead of the peer-to-peer mesh. The server must be reachable at a public address."
voice_livekit_api_key: "API key of the LiveKit server."
voice_livekit_api_secret: "API secret of the LiveKit server, used to sign short-lived room access tokens."
@@ -47,6 +49,8 @@ en:
voice_livekit_recording_s3_secret_access_key: "S3 secret key for the recording bucket. Sent only to the configured LiveKit server over HTTPS, never to participants."
voice_livekit_recording_s3_endpoint: "Optional HTTPS endpoint for S3-compatible recording storage, without a path or trailing slash. Leave empty for AWS S3. Custom endpoints use path-style bucket addressing."
errors:
+ voice_livekit_agent_requires_cloud: "Configure a LiveKit Cloud URL, API key, and API secret before enabling the agent."
+ voice_livekit_agent_username_taken: "The username livekit_agent_bot is already in use. Rename that account before enabling the agent."
voice_livekit_recording_s3_requires_credentials: "Set the recording S3 access key and secret key before setting a recording bucket."
voice_livekit_recording_s3_requires_region: "Set the recording S3 region or a custom endpoint before setting a recording bucket."
voice_livekit_recording_s3_requires_wss: "Use a wss:// LiveKit URL before configuring S3 recording storage so storage credentials are sent over HTTPS."
@@ -89,6 +93,9 @@ en:
flag_already_handled: "Thanks, we have already been notified about this user and are looking into it."
chat_threading_disabled: "The linked chat channel no longer has threading enabled, so a chat session can't be started."
livekit_unavailable: "The voice server is unavailable. Contact your site administrator."
+ agent_dispatch_forbidden: "The agent can only join an ongoing LiveKit call in a public room. Check the agent settings and make sure a human is present."
+ agent_dispatch_failed: "LiveKit could not confirm the agent invitation. Check the agent's deployment and the server logs before trying again."
+ agent_list_failed: "LiveKit could not list the deployed agents. Enter the agent's dispatch name instead."
livekit_room_instance_ended: "This call has ended. Rejoin the room to start a new one."
livekit_no_signaling: "This room's call uses a media server; peer-to-peer signaling is disabled."
recording_unavailable: "Recording is not available for this call."
diff --git a/plugins/voice/config/routes.rb b/plugins/voice/config/routes.rb
index 2cbabbc082c..c0d7f5a80f3 100644
--- a/plugins/voice/config/routes.rb
+++ b/plugins/voice/config/routes.rb
@@ -3,6 +3,7 @@
Voice::Engine.routes.draw do
resources :rooms do
member do
+ post :invite_agent
post :join
post :heartbeat
delete :leave
@@ -31,6 +32,8 @@ Voice::Engine.routes.draw do
resources :calls, only: %i[create]
+ get "agents" => "agents#index"
+
# LiveKit server webhooks — machine-to-machine, authenticated by the
# signature on the request body, not by a user session.
post "livekit/webhook" => "livekit_webhooks#create"
diff --git a/plugins/voice/config/settings.yml b/plugins/voice/config/settings.yml
index f7b1281c3b8..ddb360e285e 100644
--- a/plugins/voice/config/settings.yml
+++ b/plugins/voice/config/settings.yml
@@ -93,6 +93,20 @@ plugins:
default: ""
voice_livekit_mesh_fallback:
default: false
+ voice_livekit_agent_enabled:
+ default: false
+ validator: "VoiceLivekitAgentValidator"
+ refresh: true
+ voice_livekit_agent_invite_allowed_groups:
+ type: group_list
+ list_type: compact
+ default: "1" # auto group admins
+ allow_any: false
+ refresh: true
+ mandatory_values: "1" # auto group admins
+ disallowed_groups: "0|4"
+ depends_on:
+ - voice_livekit_agent_enabled
voice_livekit_recording_enabled:
default: false
client: true
diff --git a/plugins/voice/docs/livekit.md b/plugins/voice/docs/livekit.md
index 783379ef676..22c4bf926db 100644
--- a/plugins/voice/docs/livekit.md
+++ b/plugins/voice/docs/livekit.md
@@ -10,7 +10,7 @@ Sites that need bigger calls can deploy their own [LiveKit](https://livekit.io)
server and point the plugin at it. Rooms routed through LiveKit publish each
track **once** to the SFU, which fans it out to subscribers — publisher
upstream stays constant no matter how many people are in the room. Everything
-else (presence, sessions, badges, admin stats, the roster UI, mute/deafen,
+else for human participants (presence, sessions, badges, admin stats, the roster UI, mute/deafen,
push-to-talk, noise suppression, background blur) works identically on both
transports.
@@ -199,12 +199,18 @@ webhook deliveries at `POST /voice/livekit/webhook` and uses them as a
- `room_finished` clears the room's transport pin, so the next call
re-resolves against current settings right away.
-Webhooks never *create* presence and never touch session analytics — those
-ride Discourse heartbeats on both transports. If webhooks are undelivered
+For human participants, webhooks never *create* presence and never touch session
+analytics — those ride Discourse heartbeats on both transports. If webhooks are undelivered
(firewall, misconfigured URL), nothing breaks; the built-in TTLs just take a
little longer to converge, so treat a stale delivery marker as a warning,
never an outage.
+Authorized external agents have a separate lifecycle: provider connection events
+and reconciliation maintain their presence, while Discourse controls admission,
+invitations and removal. They do not create human session analytics. See
+[External agent participation](./roadmap/agent-participation.md) for the contract
+and its implementation checklist.
+
Deliveries are authenticated by the `Authorization` JWT LiveKit signs with the
API secret, which includes a hash of the request body — no extra shared
secret to configure. Rejected deliveries are logged to `/logs` prefixed
@@ -258,6 +264,89 @@ SDK never calls `getUserMedia`).
The [local fake participants harness](./local-fake-participants.md) works
against LiveKit-routed rooms unchanged, for the same reason.
+## LiveKit Agent Builder
+
+Voice supports manually inviting a chosen LiveKit Cloud agent into ongoing
+public Voice calls. Deploy the agent with Agent Builder in the same Cloud project
+configured for Voice. The agent does not need `VOICE_AGENT_CREDENTIAL` or a
+Discourse HTTP tool.
+
+### Setup and testing
+
+1. In **Admin → Settings**, enable `voice_livekit_agent_enabled`. This creates the
+ `livekit_agent_bot` account automatically. Voice uses its existing LiveKit URL
+ and API credentials.
+2. Make a public Voice room use LiveKit through the existing room policy. Join it
+ as a human and confirm that the call uses LiveKit. Mesh calls cannot invite an
+ agent and do not switch transport automatically.
+3. As a member of `voice_livekit_agent_invite_allowed_groups` (admins by default),
+ open the room's **…** menu (on its page or in the sidebar) and
+ select **Invite LiveKit agent**. The same action is available in the widget’s
+ **…** menu. The modal lists the agents deployed to the LiveKit Cloud project by
+ dispatch name; pick one and invite it. Agents without a dispatch name yet are not
+ listed, and workers running outside LiveKit's hosting (for example a local
+ `pnpm dev` worker) never are — use **Type a name instead** to enter such a dispatch
+ name by hand; when the list is empty or unavailable the modal shows the text field
+ directly. The list is cached for 30 seconds; use the refresh button after deploying
+ an agent while the modal is open. Do not use the `CA_` deployment ID.
+4. Wait for `livekit_agent_bot` to appear, then speak. With webhooks configured,
+ presence can appear promptly; otherwise allow the next one-minute scheduled
+ sweep. The agent's initial greeting may occur before roster admission.
+5. Open the bot's participant menu and select **Kick**. Verify that its roster
+ entry and audio disappear. Invite it again immediately from the room menu.
+ Each invitation creates a fresh session; no expulsion duration is stored.
+6. Leave as the last human and verify the bot disconnects. Repeat after closing
+ the human browser abruptly, allowing presence expiry and reconciliation.
+7. Disable the feature while the bot is present and verify that it is removed.
+ Enabling again reuses the same bot account. Private rooms, empty calls, users outside the
+ invite groups, and a missing or inactive bot cannot initiate an invitation.
+
+Set the LiveKit webhook URL to the site's public HTTPS origin followed by
+`/voice/livekit/webhook`. Keep scheduled jobs running even
+when webhooks are configured so missed events and cleanup failures are retried.
+
+Agents always join as speakers. They do not count toward human capacity,
+attendance, badges, or participation statistics. Room managers can kick them;
+only members of `voice_livekit_agent_invite_allowed_groups` (admins by default)
+can invite them. Nothing automatically dispatches an agent when humans join a
+room.
+
+### Hosting and provider behavior
+
+The LiveKit Agents framework and dispatch mechanism support both Cloud and
+self-hosted servers. Agent Builder and managed agent hosting are Cloud services.
+This UI intentionally supports only a configured `wss://*.livekit.cloud` project.
+See [LiveKit Cloud](https://docs.livekit.io/intro/cloud/) and
+[Agent Builder](https://docs.livekit.io/agents/start/builder/).
+
+An accepted invitation means LiveKit accepted the dispatch, not that a worker has
+connected. Check LiveKit's session logs if the bot does not appear, particularly
+the dispatch name and deployment status. Discourse associates the bot only with
+the running job's identity returned by LiveKit's authenticated dispatch API;
+participant metadata or matching display names cannot authorize roster access.
+
+LiveKit controls a managed agent's initial connection permissions. Discourse
+updates them before roster admission, disabling data publishing and applying the
+room's permitted media sources. Use trusted agents: roster gating does not block
+provider access before reconciliation. This flow supports audio conversation;
+it does not enable Agent Console transcription or data features.
+
+Cleanup records outlive revoked admission proofs, allowing failed deletion and
+ambiguous dispatch creation responses to be reconciled. Discourse clients stop
+agent playback on roster removal. Provider disconnection may lag during outages.
+See the [dispatch API](https://docs.livekit.io/reference/agents/agent-dispatch-service-api/)
+and [implementation contract](./roadmap/agent-participation.md).
+
+Automated checks:
+
+```bash
+LOAD_PLUGINS=1 bin/rspec plugins/voice/spec --exclude-pattern 'plugins/voice/spec/system/**/*_spec.rb'
+CI=1 bin/qunit --standalone --target voice --filter '/VoiceInviteAgentButton|VoiceParticipantSidebarContextMenu|voice-webrtc-livekit/i'
+```
+
+The real-SFU system spec above tests human media. A real dashboard-agent
+conversation still requires the manual checks in this section.
+
## Manual browser checklist
`livekit-client` has real platform nuances the automated Chromium-only spec
diff --git a/plugins/voice/docs/roadmap/agent-participation.md b/plugins/voice/docs/roadmap/agent-participation.md
new file mode 100644
index 00000000000..62f53cd3250
--- /dev/null
+++ b/plugins/voice/docs/roadmap/agent-participation.md
@@ -0,0 +1,61 @@
+# LiveKit agent participation
+
+## Product contract
+
+- `voice_livekit_agent_enabled` creates one negative-ID bot, `livekit_agent_bot`.
+ Disabling preserves the account; enabling again reuses it. An existing unrelated
+ account with that username is never adopted.
+- The invite modal lists the dispatch names of agents deployed to the LiveKit Cloud
+ project (read through the Cloud agents catalogue with the project credentials, cached
+ briefly) and falls back to a typed dispatch name when nothing is listed. The feature
+ uses the existing LiveKit Cloud project credentials and dashboard deployment.
+- Members of `voice_livekit_agent_invite_allowed_groups` (admins by default, admins always
+ included) explicitly invite the bot through a public Voice room's menu, including the widget
+ menu. A human must already be present, and the call must already use LiveKit.
+- The bot speaks and listens. Room managers can kick it without a timed exclusion;
+ inviters can immediately invite it again. Private rooms and mesh calls are outside
+ this feature's scope.
+- Bots leave when the last human leaves and do not contribute to human capacity,
+ attendance, co-presence, badges, or statistics.
+- No integration records, custom worker credentials, room allowlists, role fields,
+ or exclusion management are needed. The single bot ID uses PluginStore.
+
+## Authorization and lifecycle
+
+The invite endpoint uses normal session authentication, CSRF protection, and
+Guardian invite-group/public-room checks. The dispatcher independently requires the
+feature, a configured Cloud project and a valid invitation name, an active bot, human presence, and a
+LiveKit transport pin.
+
+Every invitation writes a new opaque Redis admission proof. An authenticated
+provider snapshot must match the dispatch ID, room, agent name, and metadata and
+report exactly one running job identity. The participant must have the provider's
+agent kind. Neither a participant's claimed metadata nor its display name is
+sufficient. Clients receive the verified identity mapping in roster metadata.
+
+Provider permission updates precede roster admission. A Redis compare-and-set
+checks that the authorization proof remains current when committing presence;
+a concurrent kick or changed setting cannot restore a stale session. Signed
+webhooks trigger reconciliation rather than importing event payloads as members.
+The scheduled sweep reconciles missed events and expires absent agents.
+
+Kicking revokes local authorization before cancelling the provider dispatch.
+Cleanup records remain separate from admission proofs, allowing deletion retries
+and recovery of dispatch creation whose response was lost. Room cleanup resolves
+session keys from the room's dispatch records and the shared bot, without scanning
+Redis. The scheduled sweep also retries provider cleanup for deleted rooms using
+their retained IDs; successful cleanup removes them from the provider-room index. A new invitation
+supersedes earlier sessions. Disabling the feature revokes current sessions. Last-human cleanup also handles pending dispatches.
+
+LiveKit supplies the initial permissions for managed agents. Discourse updates
+these before admitting playback, but this does not prevent provider access before
+reconciliation. Use trusted dashboard deployments. Audio conversation is supported;
+provider data publishing is disabled. Outages can delay remote disconnection.
+
+## Hosting and testing
+
+LiveKit's Agents framework supports Cloud and self-hosted servers, while Agent
+Builder and managed hosting belong to Cloud. This implementation targets Cloud
+only. The [operations guide](../livekit.md#livekit-agent-builder) documents setup,
+manual testing, and automated test commands. Backend tests stub provider responses;
+real agent audio, deployment readiness, and provider logs require manual validation.
diff --git a/plugins/voice/docs/roadmap/livekit-sfu.md b/plugins/voice/docs/roadmap/livekit-sfu.md
index 1d5991fe650..4d8183f82ac 100644
--- a/plugins/voice/docs/roadmap/livekit-sfu.md
+++ b/plugins/voice/docs/roadmap/livekit-sfu.md
@@ -1,5 +1,10 @@
# Optional LiveKit SFU support
+For the scoped extension that lets administrators authorize external bot accounts,
+see [External agent participation](./agent-participation.md). That design preserves
+the human presence path described here and adds provider-maintained presence only
+for authorized agent sessions.
+
This is the "SFU integration (future, separate effort)" scoped out of
[video-screenshare.md](./video-screenshare.md). It is written to be executed by
a coding agent, PR by PR (§9). Line references are against plugin commit
diff --git a/plugins/voice/lib/voice.rb b/plugins/voice/lib/voice.rb
index d0a042d71bd..bddc032b93a 100644
--- a/plugins/voice/lib/voice.rb
+++ b/plugins/voice/lib/voice.rb
@@ -54,6 +54,9 @@ require_relative "voice/livekit/twirp"
require_relative "voice/livekit/egress_client"
require_relative "voice/livekit/health_check"
require_relative "voice/livekit/room_service_client"
+require_relative "voice/livekit/agent_dispatch_client"
+require_relative "voice/livekit/cloud_agent_client"
require_relative "voice/livekit/webhook_verifier"
require_relative "voice/room_hashtag_data_source"
require_relative "voice/user_status_manager"
+require_relative "../app/services/voice/agent_manager"
diff --git a/plugins/voice/lib/voice/guardian_extension.rb b/plugins/voice/lib/voice/guardian_extension.rb
index d28b4dd6ee7..d96f115d5f1 100644
--- a/plugins/voice/lib/voice/guardian_extension.rb
+++ b/plugins/voice/lib/voice/guardian_extension.rb
@@ -91,6 +91,18 @@ module Voice
room.public? || can_manage_voice_room?(room)
end
+ # Room-independent half of can_invite_voice_agent?, so the client can learn
+ # whether to offer the action before any room is chosen.
+ def can_invite_voice_agents?
+ return false unless can_access_voice?
+ user.in_any_groups?(SiteSetting.voice_livekit_agent_invite_allowed_groups_map)
+ end
+
+ def can_invite_voice_agent?(room)
+ can_invite_voice_agents? && can_join_voice_room?(room) && room.public? &&
+ Voice::AgentBot.available?
+ end
+
def ensure_can_invite_to_voice_room!(room)
unless can_invite_to_voice_room?(room)
raise Discourse::InvalidAccess.new(I18n.t("voice.errors.not_authorized"))
diff --git a/plugins/voice/lib/voice/livekit.rb b/plugins/voice/lib/voice/livekit.rb
index d0bc72423ce..4a873959329 100644
--- a/plugins/voice/lib/voice/livekit.rb
+++ b/plugins/voice/lib/voice/livekit.rb
@@ -16,6 +16,13 @@ module Voice
SiteSetting.voice_livekit_api_secret.present?
end
+ def self.cloud?
+ uri = URI.parse(SiteSetting.voice_livekit_url)
+ uri.scheme == "wss" && uri.host&.end_with?(".livekit.cloud")
+ rescue URI::InvalidURIError
+ false
+ end
+
def self.available_for?(room)
return false unless configured?
@@ -71,9 +78,6 @@ module Voice
sources
end
- # Least-privilege HS256 JWT: a leaked token can only join this one room,
- # as this one user, for TOKEN_TTL. Guardian remains the sole authority —
- # callers only mint for users who passed `ensure_can_join_voice_room!`.
def self.mint_token(user:, room:, guardian:)
raise MintError, "LiveKit is not fully configured" unless configured?
diff --git a/plugins/voice/lib/voice/livekit/agent_dispatch_client.rb b/plugins/voice/lib/voice/livekit/agent_dispatch_client.rb
new file mode 100644
index 00000000000..c4c9b639485
--- /dev/null
+++ b/plugins/voice/lib/voice/livekit/agent_dispatch_client.rb
@@ -0,0 +1,45 @@
+# frozen_string_literal: true
+
+module Voice
+ module Livekit
+ class AgentDispatchClient
+ def self.create(room, agent_name:, metadata:)
+ request(room, "CreateDispatch", agent_name:, metadata:)
+ end
+
+ def self.list(room)
+ request(room, "ListDispatch")
+ end
+
+ def self.delete(room, dispatch_id)
+ request(room, "DeleteDispatch", dispatch_id:)
+ end
+
+ def self.request(room, method, **body)
+ return { ok: false } unless Livekit.configured?
+
+ name = Livekit.room_name(room)
+ response =
+ Twirp.post(
+ service: "AgentDispatchService",
+ method:,
+ body: body.merge(room: name),
+ grants: {
+ roomAdmin: true,
+ room: name,
+ },
+ timeout: 5,
+ )
+ return { ok: true, data: {} } if method == "DeleteDispatch" && response.status == 404
+ return { ok: true, data: JSON.parse(response.body) } if response.status == 200
+
+ Rails.logger.warn("[voice-livekit] #{method} failed: HTTP #{response.status}")
+ { ok: false }
+ rescue StandardError => error
+ Rails.logger.warn("[voice-livekit] #{method} failed: #{error.class}")
+ { ok: false }
+ end
+ private_class_method :request
+ end
+ end
+end
diff --git a/plugins/voice/lib/voice/livekit/cloud_agent_client.rb b/plugins/voice/lib/voice/livekit/cloud_agent_client.rb
new file mode 100644
index 00000000000..8fde9d0671c
--- /dev/null
+++ b/plugins/voice/lib/voice/livekit/cloud_agent_client.rb
@@ -0,0 +1,89 @@
+# frozen_string_literal: true
+
+module Voice
+ module Livekit
+ # Read-only catalogue of the agents deployed to the LiveKit Cloud project,
+ # so inviters can pick a dispatch name instead of typing one. Cloud-only:
+ # self-hosted servers have no catalogue, and workers running outside
+ # LiveKit's hosting never appear in it, so callers must keep a typed-name
+ # path.
+ class CloudAgentClient
+ class RequestError < StandardError
+ end
+
+ CACHE_KEY = "voice:livekit:cloud_agents"
+ CACHE_TTL = 30.seconds
+ TIMEOUT_SECONDS = 5
+
+ # The catalogue API refuses requests that do not identify a CLI version.
+ # This is the version whose response shape `agent_names` follows.
+ CLIENT_VERSION = "2.18.6"
+
+ def self.list(refresh: false)
+ return { ok: false } unless Livekit.cloud? && Livekit.configured?
+
+ cached = refresh ? nil : Discourse.redis.get(CACHE_KEY)
+ return { ok: true, agents: JSON.parse(cached, symbolize_names: true) } if cached
+
+ # The unfiltered listing is a summary whose dispatch name is blank for
+ # agents built in the Cloud dashboard; only the per-agent lookup
+ # carries it, so every agent is looked up by id.
+ ids = fetch({}).filter_map { |agent| (agent["agent_id"] || agent["agentId"]).presence }
+ agents = agent_names(ids.flat_map { |id| fetch(agent_id: id) })
+
+ Discourse.redis.setex(CACHE_KEY, CACHE_TTL.to_i, agents.to_json)
+ { ok: true, agents: }
+ rescue StandardError => error
+ Rails.logger.warn("[voice-livekit] ListAgents failed: #{error.message}")
+ { ok: false }
+ end
+
+ def self.clear_cache!
+ Discourse.redis.del(CACHE_KEY)
+ end
+
+ # Deployed agents are catalogued on LiveKit's shared agents host, not on
+ # the project's own subdomain that serves the SFU and dispatch APIs.
+ def self.base_url
+ Twirp.api_base_url.sub(%r{\Ahttps://[a-zA-Z0-9\-]+\.}, "https://agents.")
+ end
+
+ def self.fetch(body)
+ response =
+ Twirp.post(
+ service: "CloudAgent",
+ method: "ListAgents",
+ body:,
+ claims: {
+ agent: {
+ admin: true,
+ },
+ },
+ headers: {
+ "X-LIVEKIT-CLI-VERSION" => CLIENT_VERSION,
+ },
+ base_url:,
+ timeout: TIMEOUT_SECONDS,
+ )
+ raise RequestError, "HTTP #{response.status}" unless response.status == 200
+
+ JSON.parse(response.body).fetch("agents", [])
+ end
+ private_class_method :fetch
+
+ # Agents still being created have no dispatch name yet and cannot be
+ # invited, so they are left out. LiveKit reports such names either blank
+ # or as a bracketed placeholder like "<Pending>".
+ def self.agent_names(agents)
+ agents
+ .filter_map do |agent|
+ name = (agent["agent_name"] || agent["agentName"]).to_s.strip
+ { name: } if name.present? && !name.start_with?("<")
+ end
+ .uniq
+ .sort_by { |agent| agent[:name] }
+ end
+ private_class_method :agent_names
+ end
+ end
+end
diff --git a/plugins/voice/lib/voice/livekit/health_check.rb b/plugins/voice/lib/voice/livekit/health_check.rb
index bfba12b10fb..05c3daa3f53 100644
--- a/plugins/voice/lib/voice/livekit/health_check.rb
+++ b/plugins/voice/lib/voice/livekit/health_check.rb
@@ -113,8 +113,10 @@ module Voice
result[:data]["participants"]
.to_a
.filter_map do |participant|
- id = participant["identity"].to_i
- id if id.positive?
+ identity = participant["identity"].to_s
+ next unless /\A-?[1-9]\d*\z/.match?(identity)
+
+ identity.to_i
end
.sort
diff --git a/plugins/voice/lib/voice/livekit/room_service_client.rb b/plugins/voice/lib/voice/livekit/room_service_client.rb
index 00b2b4786a6..fa54b78e41f 100644
--- a/plugins/voice/lib/voice/livekit/room_service_client.rb
+++ b/plugins/voice/lib/voice/livekit/room_service_client.rb
@@ -4,28 +4,37 @@ module Voice
module Livekit
# Minimal Twirp-JSON client for the three RoomService calls that keep the
# SFU in sync with Discourse-side moderation and lifecycle events. Every
- # call is best-effort and no-ops for rooms not pinned to LiveKit: the SFU
+ # call is best-effort and requires a transport pin or pending agent cleanup: the SFU
# being slow or down must never fail the Discourse request that triggered
# the call, and mesh rooms must make zero HTTP requests.
class RoomServiceClient
TIMEOUT_SECONDS = 2
class << self
- def remove_participant(room, user_id)
- call(room, "RemoveParticipant", body: { identity: user_id.to_s })
+ def remove_participant(room, user_id, identity: nil)
+ call(
+ room,
+ "RemoveParticipant",
+ body: {
+ identity: identity || participant_identity(room, user_id),
+ },
+ agent_cleanup: user_id.to_i.negative?,
+ )
end
# UpdateParticipant replaces the participant's whole permission
# object, so this sends the full set mirroring the access-token
# grants — sending only `canPublish` would silently revoke
# `canSubscribe` and deafen the participant.
- def update_participant(room, user)
- can_publish = user.guardian.can_speak_in_voice_room?(room)
+ def update_participant(room, user, identity: nil)
+ can_publish =
+ user.bot? ? user.id == AgentBot.user&.id : user.guardian.can_speak_in_voice_room?(room)
call(
room,
"UpdateParticipant",
+ agent_cleanup: user.id.negative?,
body: {
- identity: user.id.to_s,
+ identity: identity || participant_identity(room, user.id),
permission: {
canSubscribe: true,
canPublish: can_publish,
@@ -41,7 +50,7 @@ module Voice
def delete_room(room)
# LiveKit guards DeleteRoom behind the roomCreate grant; a
# room-scoped roomAdmin token gets "permissions denied".
- call(room, "DeleteRoom", grants: { roomCreate: true })
+ call(room, "DeleteRoom", grants: { roomCreate: true }, agent_cleanup: true)
end
# Diagnostic probes for the admin status panel. Unlike the sync calls
@@ -68,12 +77,24 @@ module Voice
private
- def sync?(room)
- Livekit.configured? && Voice::ParticipantTracker.pinned_transport(room.id) == "livekit"
+ def participant_identity(room, user_id)
+ if user_id.to_i.negative?
+ ParticipantTracker.get_metadata(room.id, user_id)[:livekit_identity] || user_id.to_s
+ else
+ user_id.to_s
+ end
+ end
+
+ def sync?(room, agent_cleanup:)
+ Livekit.configured? &&
+ (
+ Voice::ParticipantTracker.pinned_transport(room.id) == "livekit" ||
+ (agent_cleanup && AgentManager.provider_room?(room.id))
+ )
end
- def call(room, method, body: {}, grants: { roomAdmin: true })
- return unless sync?(room)
+ def call(room, method, body: {}, grants: { roomAdmin: true }, agent_cleanup: false)
+ return unless sync?(room, agent_cleanup:)
livekit_room = Livekit.room_name(room)
response = post(method, body.merge(room: livekit_room), grants.merge(room: livekit_room))
diff --git a/plugins/voice/lib/voice/livekit/twirp.rb b/plugins/voice/lib/voice/livekit/twirp.rb
index 9cb3bde79c9..652f4773946 100644
--- a/plugins/voice/lib/voice/livekit/twirp.rb
+++ b/plugins/voice/lib/voice/livekit/twirp.rb
@@ -16,8 +16,17 @@ module Voice
Response = Struct.new(:status, :body)
- def self.post(service:, method:, body:, grants:, timeout:)
- uri = URI.parse("#{api_base_url}/twirp/livekit.#{service}/#{method}")
+ def self.post(
+ service:,
+ method:,
+ body:,
+ timeout:,
+ grants: nil,
+ claims: {},
+ headers: {},
+ base_url: api_base_url
+ )
+ uri = URI.parse("#{base_url}/twirp/livekit.#{service}/#{method}")
response =
FinalDestination::HTTP.start(
@@ -31,10 +40,10 @@ module Voice
http.post(
uri.request_uri,
body.to_json,
- {
+ headers.merge(
"Content-Type" => "application/json",
- "Authorization" => "Bearer #{admin_token(grants)}",
- },
+ "Authorization" => "Bearer #{admin_token(grants, claims)}",
+ ),
)
end
@@ -47,12 +56,12 @@ module Voice
SiteSetting.voice_livekit_url.sub(/\Awss:/, "https:").sub(/\Aws:/, "http:")
end
- def self.admin_token(grants)
+ def self.admin_token(grants, claims = {})
payload = {
iss: SiteSetting.voice_livekit_api_key,
exp: TOKEN_TTL.from_now.to_i,
video: grants,
- }
+ }.compact.merge(claims)
JWT.encode(payload, SiteSetting.voice_livekit_api_secret, "HS256")
end
end
diff --git a/plugins/voice/lib/voice/user_status_manager.rb b/plugins/voice/lib/voice/user_status_manager.rb
index dea933ebc40..000bf7c8dc7 100644
--- a/plugins/voice/lib/voice/user_status_manager.rb
+++ b/plugins/voice/lib/voice/user_status_manager.rb
@@ -18,6 +18,8 @@ module Voice
# the tooltip shows no "until" line. Leave/kick clear them directly;
# crashed or lapsed clients are reaped by clear_stale_statuses.
def self.set_voice_status(user, room)
+ return if user.bot?
+
return unless SiteSetting.enable_user_status
return unless SiteSetting.voice_auto_status_enabled
return if user_has_non_voice_status?(user)
@@ -26,6 +28,8 @@ module Voice
end
def self.set_afk_status(user, room)
+ return if user.bot?
+
return unless SiteSetting.enable_user_status
return unless voice_status_active?(user)
diff --git a/plugins/voice/plugin.rb b/plugins/voice/plugin.rb
index 5091cc19134..a715e9364e0 100644
--- a/plugins/voice/plugin.rb
+++ b/plugins/voice/plugin.rb
@@ -159,12 +159,18 @@ after_initialize do
end
Voice::DefaultRoomSeeder.ensure! if SiteSetting.voice_enabled?
+ Voice::AgentBot.ensure! if SiteSetting.voice_enabled?
+
+ add_to_serializer(:site, :voice_livekit_agent_bot_id) do
+ Voice::AgentBot.user&.id if scope.can_invite_voice_agents? && Voice::AgentBot.available?
+ end
# This can't live in the on(:site_setting_changed) handler below: plugin
# event handlers are skipped while the plugin is disabled, which silently
# covers the disabling transition itself.
on_enabled_change do |_old_value, new_value|
Voice::DefaultRoomSeeder.ensure! if new_value
+ new_value ? Voice::AgentBot.ensure! : Voice::AgentManager.stop_all!
clear_all_voice_statuses unless new_value
refresh_chat_hashtag_configurations
end
@@ -180,6 +186,9 @@ after_initialize do
end
on(:site_setting_changed) do |name, _old_value, new_value|
+ if name.to_sym == :voice_livekit_agent_enabled
+ new_value ? Voice::AgentBot.ensure! : Voice::AgentManager.stop_all!
+ end
if name.to_sym == :voice_badges_enabled
if new_value
Voice::BadgeGranterHooks.enable_all!
diff --git a/plugins/voice/spec/jobs/scheduled/publish_room_participants_spec.rb b/plugins/voice/spec/jobs/scheduled/publish_room_participants_spec.rb
index a5d9db7a03f..ec3aaeeb3d4 100644
--- a/plugins/voice/spec/jobs/scheduled/publish_room_participants_spec.rb
+++ b/plugins/voice/spec/jobs/scheduled/publish_room_participants_spec.rb
@@ -103,6 +103,60 @@ RSpec.describe Jobs::PublishRoomParticipants do
expect(stub).to have_been_requested.once
end
+ describe "deleted room agent cleanup" do
+ before do
+ SiteSetting.voice_livekit_url = "wss://test.livekit.cloud"
+ SiteSetting.voice_livekit_api_key = "key"
+ SiteSetting.voice_livekit_api_secret = "secret"
+ SiteSetting.voice_livekit_agent_enabled = true
+ room.update!(public: true)
+ Voice::ParticipantTracker.pin_transport!(room.id, "livekit")
+ Voice::ParticipantTracker.add(room.id, user1.id)
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/CreateDispatch",
+ ).to_return(status: 200, body: { id: "AD_test" }.to_json)
+ Voice::AgentDispatcher.dispatch!(room:, agent_name: "assistant")
+ end
+
+ it "retries failed cleanup after the room row and transport pin are gone" do
+ room.destroy!
+ delete_dispatch =
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/DeleteDispatch",
+ ).to_return(status: 503)
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/ListDispatch",
+ ).to_return(status: 503)
+ delete_room =
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/DeleteRoom",
+ ).to_return(status: 503)
+
+ Voice::AgentManager.evict_agents_in_room!(room)
+
+ expect(Voice::ParticipantTracker.pinned_transport(room.id)).to be_nil
+ expect(Voice::AgentDispatcher.pending?(room.id)).to eq(true)
+ expect(Voice::AgentManager.provider_room_ids).to contain_exactly(room.id)
+
+ delete_dispatch.to_return(status: 200, body: "{}")
+ delete_room.to_return(status: 200, body: "{}")
+ messages = MessageBus.track_publish { job.execute({}) }
+
+ expect(Voice::AgentDispatcher.pending?(room.id)).to eq(false)
+ expect(Voice::AgentManager.provider_room_ids).to be_empty
+ expect(messages.map(&:channel)).not_to include(Voice.room_channel(room.id))
+ expect(delete_room).to have_been_requested.twice
+
+ job.execute({})
+
+ expect(delete_room).to have_been_requested.twice
+ end
+ end
+
describe "stale status sweep" do
before do
SiteSetting.enable_user_status = true
diff --git a/plugins/voice/spec/jobs/scheduled/voice/close_orphaned_sessions_spec.rb b/plugins/voice/spec/jobs/scheduled/voice/close_orphaned_sessions_spec.rb
index ecaa670be0a..9b93d0dc797 100644
--- a/plugins/voice/spec/jobs/scheduled/voice/close_orphaned_sessions_spec.rb
+++ b/plugins/voice/spec/jobs/scheduled/voice/close_orphaned_sessions_spec.rb
@@ -71,6 +71,21 @@ RSpec.describe Jobs::Voice::CloseOrphanedSessions do
expect(Voice::ParticipantTracker.get_metadata(room.id, user1.id)).to eq({})
end
+ it "ends agent participation when the final human session expires" do
+ bot = Fabricate(:user, id: -1400)
+ session =
+ Fabricate(:voice_session, user: user1, room: room, joined_at: 50.minutes.ago, left_at: nil)
+ set_stale_participant(room, user1, last_heartbeat: 2.minutes.ago)
+ Voice::ParticipantTracker.add(room.id, bot.id)
+ Voice::ParticipantTracker.pin_transport!(room.id, "mesh")
+
+ job.execute({})
+
+ expect(session.reload.left_at).to be_present
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ expect(Voice::ParticipantTracker.pinned_transport(room.id)).to be_nil
+ end
+
it "closes stale user sessions while keeping active users untouched" do
stale_session =
Fabricate(:voice_session, user: user1, room: room, joined_at: 50.minutes.ago, left_at: nil)
diff --git a/plugins/voice/spec/jobs/voice/update_co_presence_spec.rb b/plugins/voice/spec/jobs/voice/update_co_presence_spec.rb
new file mode 100644
index 00000000000..ede38421ddf
--- /dev/null
+++ b/plugins/voice/spec/jobs/voice/update_co_presence_spec.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Jobs::Voice::UpdateCoPresence do
+ fab!(:room, :voice_room)
+ fab!(:user)
+ fab!(:other_user, :user)
+ fab!(:bot) { Fabricate(:user, id: -1400) }
+
+ before do
+ SiteSetting.voice_enabled = true
+ SiteSetting.voice_analytics_enabled = true
+ SiteSetting.voice_max_room_participants = 2
+ end
+
+ describe "#execute" do
+ it "counts human pairs at capacity without counting agents" do
+ [bot, user, other_user].each do |participant|
+ Voice::ParticipantTracker.add(room.id, participant.id)
+ end
+
+ described_class.new.execute({})
+
+ expect(Voice::CoPresence.pluck(:user_id_1, :user_id_2)).to contain_exactly(
+ [user.id, other_user.id].sort,
+ )
+ expect(Voice::CoPresence.sole.total_seconds).to eq(300)
+ end
+ end
+end
diff --git a/plugins/voice/spec/lib/voice/guardian_extension_spec.rb b/plugins/voice/spec/lib/voice/guardian_extension_spec.rb
index d54d018b9a4..43ca0041eb8 100644
--- a/plugins/voice/spec/lib/voice/guardian_extension_spec.rb
+++ b/plugins/voice/spec/lib/voice/guardian_extension_spec.rb
@@ -108,6 +108,35 @@ RSpec.describe Voice::GuardianExtension do
end
end
+ describe "#can_invite_voice_agents?" do
+ it "is true for admins by default" do
+ expect(staff.guardian.can_invite_voice_agents?).to eq(true)
+ end
+
+ it "is false for users outside the invite groups" do
+ expect(outsider.guardian.can_invite_voice_agents?).to eq(false)
+ end
+
+ it "is true for users in the invite groups" do
+ SiteSetting.voice_livekit_agent_invite_allowed_groups =
+ "#{Group::AUTO_GROUPS[:admins]}|#{Group::AUTO_GROUPS[:trust_level_2]}"
+
+ expect(outsider.guardian.can_invite_voice_agents?).to eq(true)
+ end
+
+ it "is false for anonymous visitors" do
+ expect(anonymous_guardian.can_invite_voice_agents?).to eq(false)
+ end
+
+ it "is false for users outside the voice allowed groups" do
+ SiteSetting.voice_livekit_agent_invite_allowed_groups =
+ "#{Group::AUTO_GROUPS[:admins]}|#{Group::AUTO_GROUPS[:trust_level_2]}"
+ SiteSetting.voice_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
+
+ expect(outsider.guardian.can_invite_voice_agents?).to eq(false)
+ end
+ end
+
describe "#can_manage_voice_room?" do
it "is false for a create-room-group user with no tie to the room" do
expect(outsider.guardian.can_manage_voice_room?(private_room)).to eq(false)
diff --git a/plugins/voice/spec/lib/voice/livekit/cloud_agent_client_spec.rb b/plugins/voice/spec/lib/voice/livekit/cloud_agent_client_spec.rb
new file mode 100644
index 00000000000..a6aea3f59cd
--- /dev/null
+++ b/plugins/voice/spec/lib/voice/livekit/cloud_agent_client_spec.rb
@@ -0,0 +1,97 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Voice::Livekit::CloudAgentClient do
+ let(:endpoint) { "https://agents.livekit.cloud/twirp/livekit.CloudAgent/ListAgents" }
+
+ before do
+ SiteSetting.voice_livekit_url = "wss://my-project-abc123.livekit.cloud"
+ SiteSetting.voice_livekit_api_key = "lk_api_key"
+ SiteSetting.voice_livekit_api_secret = "lk_api_secret"
+ described_class.clear_cache!
+ end
+
+ # Mirrors LiveKit Cloud: the unfiltered listing carries ids but blank names,
+ # while a lookup by id returns the dispatch name.
+ def stub_catalogue(names_by_id)
+ stub_request(:post, endpoint).to_return do |request|
+ id = JSON.parse(request.body)["agent_id"]
+ agents =
+ if id
+ [{ agent_id: id, agent_name: names_by_id.fetch(id) }]
+ else
+ names_by_id.keys.map { |agent_id| { agent_id:, agent_name: "" } }
+ end
+ { status: 200, body: { agents: }.to_json }
+ end
+ end
+
+ describe ".list" do
+ it "POSTs to the shared agents host with an agent admin token and a client version" do
+ stub = stub_catalogue({})
+
+ expect(described_class.list).to eq(ok: true, agents: [])
+
+ expect(
+ stub.with do |req|
+ token = req.headers["Authorization"].delete_prefix("Bearer ")
+ claims = JWT.decode(token, "lk_api_secret", true, algorithm: "HS256").first
+
+ req.body == "{}" && req.headers["X-Livekit-Cli-Version"].present? &&
+ claims["iss"] == "lk_api_key" && claims["agent"] == { "admin" => true } &&
+ !claims.key?("video")
+ end,
+ ).to have_been_requested.once
+ end
+
+ it "resolves each agent's dispatch name by id, sorted, without blanks, placeholders or duplicates" do
+ stub =
+ stub_catalogue(
+ "CA_1" => "support",
+ "CA_2" => "assistant",
+ "CA_3" => "",
+ "CA_4" => "support",
+ "CA_5" => "<Pending>",
+ )
+
+ expect(described_class.list[:agents]).to eq([{ name: "assistant" }, { name: "support" }])
+
+ expect(stub).to have_been_requested.times(6)
+ expect(
+ a_request(:post, endpoint).with(body: { agent_id: "CA_2" }.to_json),
+ ).to have_been_made.once
+ end
+
+ it "reuses the cached list on later calls" do
+ stub = stub_catalogue("CA_1" => "assistant")
+
+ 2.times { expect(described_class.list[:agents]).to eq([{ name: "assistant" }]) }
+
+ expect(stub).to have_been_requested.twice
+ end
+
+ it "bypasses the cached list when refreshing" do
+ stub = stub_catalogue("CA_1" => "assistant")
+ described_class.list
+
+ expect(described_class.list(refresh: true)[:agents]).to eq([{ name: "assistant" }])
+
+ expect(stub).to have_been_requested.times(4)
+ end
+
+ it "is unavailable outside LiveKit Cloud" do
+ SiteSetting.voice_livekit_url = "wss://livekit.example.com"
+ stub = stub_request(:post, /livekit.example.com|agents\./)
+
+ expect(described_class.list).to eq(ok: false)
+ expect(stub).not_to have_been_requested
+ end
+
+ it "is unavailable when the provider fails" do
+ stub_request(:post, endpoint).to_return(status: 503)
+
+ expect(described_class.list).to eq(ok: false)
+ end
+ end
+end
diff --git a/plugins/voice/spec/lib/voice/user_status_manager_spec.rb b/plugins/voice/spec/lib/voice/user_status_manager_spec.rb
index 80a9b665aed..ed1d02305f8 100644
--- a/plugins/voice/spec/lib/voice/user_status_manager_spec.rb
+++ b/plugins/voice/spec/lib/voice/user_status_manager_spec.rb
@@ -14,6 +14,14 @@ RSpec.describe Voice::UserStatusManager do
end
describe ".set_voice_status" do
+ it "leaves agent status unchanged" do
+ bot = Fabricate(:user, id: -1400)
+
+ described_class.set_voice_status(bot, room)
+
+ expect(bot.reload.user_status).to be_nil
+ end
+
it "sets the user's status with room name and no expiry" do
described_class.set_voice_status(user, room)
diff --git a/plugins/voice/spec/requests/voice/admin_livekit_controller_spec.rb b/plugins/voice/spec/requests/voice/admin_livekit_controller_spec.rb
index b796049892c..dd8d4c8369e 100644
--- a/plugins/voice/spec/requests/voice/admin_livekit_controller_spec.rb
+++ b/plugins/voice/spec/requests/voice/admin_livekit_controller_spec.rb
@@ -269,6 +269,26 @@ RSpec.describe Voice::AdminLivekitController do
expect(room_status["presence_user_ids"]).to eq([user.id, other_user.id].sort)
end
+ it "includes bot identities without interpreting malformed identities as users" do
+ bot = Fabricate(:user, id: -1400)
+ Voice::ParticipantTracker.add(room.id, bot.id)
+ list_participants_stub.to_return(
+ status: 200,
+ body: {
+ participants:
+ [user, other_user, bot].map { |participant| { identity: participant.id.to_s } } +
+ [{ identity: "#{user.id}-agent" }, { identity: "0" }],
+ }.to_json,
+ )
+
+ get_status
+
+ room_status = response.parsed_body["rooms"].first
+ expect(room_status["livekit_user_ids"]).to eq([user.id, other_user.id, bot.id].sort)
+ expect(room_status["missing_on_livekit"]).to be_empty
+ expect(room_status["missing_in_presence"]).to be_empty
+ end
+
it "skips per-room probes when the server is already unreachable" do
list_rooms_stub.to_timeout
participants_stub = list_participants_stub
diff --git a/plugins/voice/spec/requests/voice/agents_controller_spec.rb b/plugins/voice/spec/requests/voice/agents_controller_spec.rb
new file mode 100644
index 00000000000..7b2e827d8cb
--- /dev/null
+++ b/plugins/voice/spec/requests/voice/agents_controller_spec.rb
@@ -0,0 +1,108 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Voice::AgentsController do
+ fab!(:admin)
+ fab!(:moderator)
+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
+
+ before do
+ SiteSetting.voice_enabled = true
+ SiteSetting.voice_livekit_url = "wss://test.livekit.cloud"
+ SiteSetting.voice_livekit_api_key = "key"
+ SiteSetting.voice_livekit_api_secret = "secret"
+ SiteSetting.voice_livekit_agent_enabled = true
+ Voice::Livekit::CloudAgentClient.clear_cache!
+ @catalogue = { "CA_2" => "support", "CA_1" => "assistant", "CA_3" => "" }
+ @list =
+ stub_request(
+ :post,
+ "https://agents.livekit.cloud/twirp/livekit.CloudAgent/ListAgents",
+ ).to_return do |request|
+ id = JSON.parse(request.body)["agent_id"]
+ agents =
+ if id
+ [{ agent_id: id, agent_name: @catalogue.fetch(id) }]
+ else
+ @catalogue.keys.map { |agent_id| { agent_id:, agent_name: "" } }
+ end
+ { status: 200, body: { agents: }.to_json }
+ end
+ end
+
+ describe "#index" do
+ it "lists the named deployed agents for a member of the invite groups" do
+ SiteSetting.voice_livekit_agent_invite_allowed_groups =
+ "#{Group::AUTO_GROUPS[:admins]}|#{Group::AUTO_GROUPS[:moderators]}"
+ sign_in(moderator)
+
+ get "/voice/agents.json"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["agents"]).to eq(
+ [{ "name" => "assistant" }, { "name" => "support" }],
+ )
+ end
+
+ it "rejects users outside the invite groups" do
+ sign_in(moderator)
+
+ get "/voice/agents.json"
+
+ expect(response.status).to eq(403)
+ expect(@list).not_to have_been_requested
+ end
+
+ it "rejects requests while the agent feature is disabled" do
+ SiteSetting.voice_livekit_agent_enabled = false
+ sign_in(admin)
+
+ get "/voice/agents.json"
+
+ expect(response.status).to eq(403)
+ expect(@list).not_to have_been_requested
+ end
+
+ it "re-reads the catalogue when asked to refresh" do
+ sign_in(admin)
+ get "/voice/agents.json"
+ @catalogue["CA_4"] = "trainer"
+
+ get "/voice/agents.json"
+ expect(response.parsed_body["agents"].map { |a| a["name"] }).to eq(%w[assistant support])
+
+ get "/voice/agents.json", params: { refresh: true }
+ expect(response.parsed_body["agents"].map { |a| a["name"] }).to eq(
+ %w[assistant support trainer],
+ )
+ end
+
+ it "rate limits refreshes for non-staff inviters" do
+ RateLimiter.enable
+ SiteSetting.voice_livekit_agent_invite_allowed_groups =
+ "#{Group::AUTO_GROUPS[:admins]}|#{Group::AUTO_GROUPS[:trust_level_1]}"
+ sign_in(user)
+ 6.times { RateLimiter.new(user, "voice-agent-list-refresh", 6, 1.minute).performed! }
+
+ get "/voice/agents.json", params: { refresh: true }
+ expect(response.status).to eq(429)
+
+ get "/voice/agents.json"
+ expect(response.status).to eq(200)
+ end
+
+ it "reports a provider failure" do
+ stub_request(
+ :post,
+ "https://agents.livekit.cloud/twirp/livekit.CloudAgent/ListAgents",
+ ).to_return(status: 503)
+ sign_in(admin)
+
+ get "/voice/agents.json"
+
+ expect(response.status).to eq(503)
+ expect(response.parsed_body["errors"]).to eq([I18n.t("voice.errors.agent_list_failed")])
+ end
+ end
+end
diff --git a/plugins/voice/spec/requests/voice/livekit_webhooks_controller_spec.rb b/plugins/voice/spec/requests/voice/livekit_webhooks_controller_spec.rb
index 080c58ef431..6873445b504 100644
--- a/plugins/voice/spec/requests/voice/livekit_webhooks_controller_spec.rb
+++ b/plugins/voice/spec/requests/voice/livekit_webhooks_controller_spec.rb
@@ -71,6 +71,7 @@ RSpec.describe Voice::LivekitWebhooksController do
room_name: Voice::Livekit.room_name(room),
identity: user.id.to_s,
sid: "PA_test",
+ metadata: nil,
created_at: 1.minute.from_now
)
{
@@ -84,6 +85,7 @@ RSpec.describe Voice::LivekitWebhooksController do
"participant" => {
"sid" => sid,
"identity" => identity,
+ "metadata" => metadata,
},
}
end
@@ -94,6 +96,69 @@ RSpec.describe Voice::LivekitWebhooksController do
end
describe "#create" do
+ it "reconciles a dashboard agent without letting stale events restore a kicked bot" do
+ SiteSetting.voice_livekit_url = "wss://test.livekit.cloud"
+ SiteSetting.voice_livekit_agent_enabled = true
+ Voice::ParticipantTracker.pin_transport!(room.id, "livekit")
+ Voice::ParticipantTracker.add(room.id, user.id)
+ dispatch = nil
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/CreateDispatch",
+ ).to_return do |request|
+ dispatch =
+ JSON.parse(request.body).merge(
+ "id" => "AD_test",
+ "state" => {
+ "jobs" => [
+ {
+ "dispatchId" => "AD_test",
+ "state" => {
+ "status" => "JS_RUNNING",
+ "participantIdentity" => "agent-test",
+ },
+ },
+ ],
+ },
+ )
+ { status: 200, body: dispatch.to_json }
+ end
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/ListDispatch",
+ ).to_return { { status: 200, body: { agentDispatches: [dispatch] }.to_json } }
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/ListParticipants",
+ ).to_return(
+ status: 200,
+ body: {
+ participants: [{ identity: "agent-test", kind: "AGENT", sid: "PA_current" }],
+ }.to_json,
+ )
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/UpdateParticipant",
+ ).to_return(status: 200, body: "{}")
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/DeleteDispatch",
+ ).to_return(status: 200, body: "{}")
+ Voice::AgentDispatcher.dispatch!(room:, agent_name: "assistant")
+ bot = Voice::AgentBot.user
+
+ post_webhook(event_for(event: "participant_joined", identity: "agent-test"))
+ expect(response.status).to eq(200)
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to eq([bot.id])
+
+ post_webhook(event_for(identity: "agent-test", sid: "PA_old"))
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to eq([bot.id])
+
+ Voice::AgentManager.evict!(room:, user_id: bot.id)
+ post_webhook(event_for(event: "participant_joined", identity: "agent-test"))
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ end
+
context "with an invalid delivery" do
it "rejects a request with no Authorization token and records no delivery" do
body = event_for.to_json
@@ -246,6 +311,10 @@ RSpec.describe Voice::LivekitWebhooksController do
end
it "leaves the session row open for CloseOrphanedSessions to close" do
+ stub_request(
+ :post,
+ "https://livekit.example.com/twirp/livekit.RoomService/DeleteRoom",
+ ).to_return(status: 200, body: "{}")
sign_in(user)
post "/voice/rooms/#{room.id}/join.json"
expect(response.status).to eq(200)
diff --git a/plugins/voice/spec/requests/voice/room_agent_invitations_spec.rb b/plugins/voice/spec/requests/voice/room_agent_invitations_spec.rb
new file mode 100644
index 00000000000..8442c7d22b9
--- /dev/null
+++ b/plugins/voice/spec/requests/voice/room_agent_invitations_spec.rb
@@ -0,0 +1,126 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Voice::RoomsController do
+ fab!(:admin)
+ fab!(:moderator)
+ fab!(:user)
+ fab!(:room) { Fabricate(:voice_room, public: true) }
+
+ before do
+ SiteSetting.voice_enabled = true
+ SiteSetting.voice_livekit_url = "wss://test.livekit.cloud"
+ SiteSetting.voice_livekit_api_key = "key"
+ SiteSetting.voice_livekit_api_secret = "secret"
+ SiteSetting.voice_livekit_agent_enabled = true
+ Voice::ParticipantTracker.pin_transport!(room.id, "livekit")
+ Voice::ParticipantTracker.add(room.id, user.id)
+ @dispatch =
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/CreateDispatch",
+ ).to_return(status: 200, body: { id: "AD_test" }.to_json)
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/DeleteDispatch",
+ ).to_return(status: 200, body: "{}")
+ end
+
+ describe "#invite_agent" do
+ it "dispatches the chosen agent for an admin" do
+ sign_in(admin)
+
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+
+ expect(response.status).to eq(201)
+ expect(response.parsed_body).to eq("dispatch_id" => "AD_test")
+ expect(
+ @dispatch.with(body: hash_including("agent_name" => "assistant")),
+ ).to have_been_requested.once
+ end
+
+ it "rejects missing, blank, oversized, and non-string names" do
+ sign_in(admin)
+ [nil, " ", "a" * 257, ["assistant"]].each do |name|
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: name }
+ expect(response.status).to eq(400)
+ end
+ expect(@dispatch).not_to have_been_requested
+ end
+
+ it "rejects users outside the invite groups" do
+ [user, moderator].each do |actor|
+ sign_in(actor)
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+ expect(response.status).to eq(403)
+ end
+ expect(@dispatch).not_to have_been_requested
+ end
+
+ it "dispatches for a non-admin in the invite groups" do
+ SiteSetting.voice_livekit_agent_invite_allowed_groups =
+ "#{Group::AUTO_GROUPS[:admins]}|#{Group::AUTO_GROUPS[:moderators]}"
+ sign_in(moderator)
+
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+
+ expect(response.status).to eq(201)
+ expect(@dispatch).to have_been_requested.once
+ end
+
+ it "rejects invitations when disabled or the bot no longer exists" do
+ sign_in(admin)
+ SiteSetting.voice_livekit_agent_enabled = false
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+ expect(response.status).to eq(403)
+
+ SiteSetting.voice_livekit_agent_enabled = true
+ Voice::AgentBot.user.delete
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+ expect(response.status).to eq(403)
+ expect(@dispatch).not_to have_been_requested
+ end
+
+ it "rejects a private room even for admins" do
+ sign_in(admin)
+ room.update!(public: false)
+
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+
+ expect(response.status).to eq(403)
+ expect(@dispatch).not_to have_been_requested
+ end
+
+ it "reports a provider failure" do
+ sign_in(admin)
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/CreateDispatch",
+ ).to_return(status: 503)
+
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+
+ expect(response.status).to eq(503)
+ end
+ end
+
+ describe "#kick" do
+ it "allows the admin to immediately invite the bot again" do
+ sign_in(admin)
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+ bot = Voice::AgentBot.user
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/RemoveParticipant",
+ ).to_return(status: 200, body: "{}")
+
+ delete "/voice/rooms/#{room.id}/kick.json", params: { user_id: bot.id }
+
+ expect(response.status).to eq(204)
+ post "/voice/rooms/#{room.id}/invite_agent.json", params: { agent_name: "assistant" }
+ expect(response.status).to eq(201)
+ expect(@dispatch).to have_been_requested.twice
+ end
+ end
+end
diff --git a/plugins/voice/spec/requests/voice/rooms_controller_livekit_spec.rb b/plugins/voice/spec/requests/voice/rooms_controller_livekit_spec.rb
index 88ab07801a9..b7532ffcfa9 100644
--- a/plugins/voice/spec/requests/voice/rooms_controller_livekit_spec.rb
+++ b/plugins/voice/spec/requests/voice/rooms_controller_livekit_spec.rb
@@ -156,6 +156,24 @@ RSpec.describe Voice::RoomsController do
expect(Voice::ParticipantTracker.pinned_transport(room.id)).to eq("livekit")
end
+ it "cleans up an agent-only call before falling back to mesh" do
+ configure_livekit!
+ SiteSetting.voice_livekit_mesh_fallback = true
+ bot_id = Fabricate(:user, id: -1400).id
+ Voice::ParticipantTracker.pin_transport!(room.id, "livekit")
+ Voice::ParticipantTracker.add(room.id, bot_id)
+ Voice::Livekit.stubs(:mint_token).raises(Voice::Livekit::MintError.new("boom"))
+ Voice::Livekit::RoomServiceClient.expects(:remove_participant).with(room, bot_id)
+ Voice::Livekit::RoomServiceClient.expects(:delete_room).with(room)
+
+ post "/voice/rooms/#{room.id}/join.json"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["transport"]).to eq("mesh")
+ expect(Voice::ParticipantTracker.user_ids(room.id)).to contain_exactly(user.id)
+ expect(Voice::ParticipantTracker.pinned_transport(room.id)).to eq("mesh")
+ end
+
it "fails a livekit-pinned join when the config was half-deleted" do
configure_livekit!
Voice::ParticipantTracker.pin_transport!(room.id, "livekit")
diff --git a/plugins/voice/spec/services/voice/agent_bot_spec.rb b/plugins/voice/spec/services/voice/agent_bot_spec.rb
new file mode 100644
index 00000000000..6862e7b5fd2
--- /dev/null
+++ b/plugins/voice/spec/services/voice/agent_bot_spec.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Voice::AgentBot do
+ before do
+ SiteSetting.voice_enabled = true
+ SiteSetting.voice_livekit_url = "wss://test.livekit.cloud"
+ SiteSetting.voice_livekit_api_key = "key"
+ SiteSetting.voice_livekit_api_secret = "secret"
+ end
+
+ describe ".ensure!" do
+ it "creates a bot when enabled and reuses it when enabled again" do
+ SiteSetting.voice_livekit_agent_enabled = true
+ bot = described_class.user
+
+ expect(bot.username).to eq("livekit_agent_bot")
+ expect(bot).to be_bot
+ expect(bot).to be_active
+ expect(bot).not_to be_staff
+ expect(described_class).to be_available
+ SiteSetting.voice_livekit_agent_enabled = false
+ expect(described_class).not_to be_available
+ SiteSetting.voice_livekit_agent_enabled = true
+ expect(described_class.user.id).to eq(bot.id)
+ end
+
+ it "refuses to take over an existing username" do
+ user = Fabricate(:user, username: "livekit_agent_bot")
+
+ expect { SiteSetting.voice_livekit_agent_enabled = true }.to raise_error(
+ Discourse::InvalidParameters,
+ )
+ expect(described_class.user).to be_nil
+ expect(user.reload).not_to be_bot
+ end
+
+ it "requires a configured Cloud project" do
+ SiteSetting.voice_livekit_url = "wss://livekit.example.com"
+ expect { SiteSetting.voice_livekit_agent_enabled = true }.to raise_error(
+ Discourse::InvalidParameters,
+ )
+ expect(described_class.user).to be_nil
+ end
+ end
+end
diff --git a/plugins/voice/spec/services/voice/agent_dispatcher_spec.rb b/plugins/voice/spec/services/voice/agent_dispatcher_spec.rb
new file mode 100644
index 00000000000..986e1b672ae
--- /dev/null
+++ b/plugins/voice/spec/services/voice/agent_dispatcher_spec.rb
@@ -0,0 +1,279 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Voice::AgentDispatcher do
+ fab!(:user)
+ fab!(:room) { Fabricate(:voice_room, public: true) }
+ let(:bot) { Voice::AgentBot.user }
+
+ before do
+ SiteSetting.voice_enabled = true
+ SiteSetting.voice_livekit_url = "wss://test.livekit.cloud"
+ SiteSetting.voice_livekit_api_key = "key"
+ SiteSetting.voice_livekit_api_secret = "secret"
+ SiteSetting.voice_livekit_agent_enabled = true
+ Voice::ParticipantTracker.pin_transport!(room.id, "livekit")
+ Voice::ParticipantTracker.add(room.id, user.id)
+ @dispatch = nil
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/CreateDispatch",
+ ).to_return do |request|
+ @dispatch =
+ JSON.parse(request.body).merge(
+ "id" => "AD_test",
+ "state" => {
+ "jobs" => [
+ {
+ "dispatchId" => "AD_test",
+ "state" => {
+ "status" => "JS_RUNNING",
+ "participantIdentity" => "agent-test",
+ },
+ },
+ ],
+ },
+ )
+ { status: 200, body: @dispatch.to_json }
+ end
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/ListDispatch",
+ ).to_return { { status: 200, body: { agentDispatches: [@dispatch].compact }.to_json } }
+ @delete =
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/DeleteDispatch",
+ ).to_return(status: 200, body: "{}")
+ @update =
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/UpdateParticipant",
+ ).to_return(status: 200, body: "{}")
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/RemoveParticipant",
+ ).to_return(status: 200, body: "{}")
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/DeleteRoom",
+ ).to_return(status: 200, body: "{}")
+ @participants = [{ identity: "agent-test", kind: "AGENT", sid: "PA_test" }]
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/ListParticipants",
+ ).to_return { { status: 200, body: { participants: @participants }.to_json } }
+ end
+
+ describe ".dispatch!" do
+ it "maps the provider-attested job identity to the authorized bot and applies its permissions" do
+ expect(described_class.dispatch!(room:, agent_name: "assistant")).to eq("AD_test")
+
+ Voice::AgentManager.reconcile(room)
+
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to eq([bot.id])
+ expect(Voice::ParticipantTracker.get_metadata(room.id, bot.id)).to include(
+ livekit_identity: "agent-test",
+ external_agent: true,
+ role: "speaker",
+ )
+ expect(
+ @update.with(
+ body:
+ hash_including(
+ "identity" => "agent-test",
+ "permission" => hash_including("canPublish" => true, "canPublishData" => false),
+ ),
+ ),
+ ).to have_been_requested
+ expect(Voice::Session.where(user_id: bot.id)).to be_empty
+ end
+
+ it "rejects invitations to empty, mesh, and private rooms" do
+ other_room = Fabricate(:voice_room, public: false)
+ expect {
+ described_class.dispatch!(room: other_room, agent_name: "assistant")
+ }.to raise_error(Voice::AgentManager::AuthorizationError)
+ Voice::ParticipantTracker.remove(room.id, user.id)
+ expect { described_class.dispatch!(room:, agent_name: "assistant") }.to raise_error(
+ Voice::AgentManager::AuthorizationError,
+ )
+ Voice::ParticipantTracker.add(room.id, user.id)
+ Voice::ParticipantTracker.clear_transport_pin(room.id)
+ Voice::ParticipantTracker.pin_transport!(room.id, "mesh")
+ expect { described_class.dispatch!(room:, agent_name: "assistant") }.to raise_error(
+ Voice::AgentManager::AuthorizationError,
+ )
+ Voice::ParticipantTracker.clear_transport_pin(room.id)
+ Voice::ParticipantTracker.pin_transport!(room.id, "livekit")
+ expect(@dispatch).to be_nil
+ end
+
+ it "rejects participant-supplied identity claims and unrelated dispatches" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ @participants = [{ identity: "impostor", kind: "AGENT", metadata: @dispatch["metadata"] }]
+ Voice::AgentManager.reconcile(room)
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+
+ @participants = [{ identity: "agent-test", kind: "AGENT" }]
+ @dispatch["id"] = "AD_unrelated"
+ Voice::AgentManager.reconcile(room)
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ end
+
+ it "requires an active agent job and an agent participant before admitting media" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ @dispatch["state"]["jobs"][0]["state"]["status"] = "JS_FAILED"
+ Voice::AgentManager.reconcile(room)
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+
+ @dispatch["state"]["jobs"][0]["state"]["status"] = "JS_RUNNING"
+ @participants[0][:kind] = "STANDARD"
+ Voice::AgentManager.reconcile(room)
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ end
+
+ it "withholds roster admission if provider permission enforcement fails" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/UpdateParticipant",
+ ).to_return(status: 503)
+
+ Voice::AgentManager.reconcile(room)
+
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ end
+
+ it "rejects a snapshot revoked while provider permissions are being updated" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/UpdateParticipant",
+ ).to_return do
+ Voice::AgentManager.evict!(room:, user_id: bot.id)
+ { status: 200, body: "{}" }
+ end
+
+ Voice::AgentManager.reconcile(room)
+
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ expect(
+ a_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/RemoveParticipant",
+ ).with(body: hash_including("identity" => "agent-test")),
+ ).to have_been_made.once
+ end
+
+ it "uses only the new dispatch when an invitation supersedes the previous job" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ previous = @dispatch
+ described_class.dispatch!(room:, agent_name: "another-agent")
+ expect(@dispatch["agent_name"]).to eq("another-agent")
+ expect(@delete).to have_been_requested.once
+ @dispatch = previous
+
+ Voice::AgentManager.reconcile(room)
+
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ end
+
+ it "deletes an in-flight dispatch if authorization is revoked during creation" do
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/CreateDispatch",
+ ).to_return do
+ Voice::AgentManager.evict!(room:, user_id: bot.id)
+ { status: 200, body: { id: "AD_late" }.to_json }
+ end
+
+ expect { described_class.dispatch!(room:, agent_name: "assistant") }.to raise_error(
+ Voice::AgentManager::AuthorizationError,
+ )
+
+ expect(@delete.with(body: hash_including("dispatch_id" => "AD_late"))).to have_been_requested
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ end
+
+ it "recovers and deletes a remotely created dispatch after its response times out" do
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/CreateDispatch",
+ ).to_return do |request|
+ @dispatch = JSON.parse(request.body).merge("id" => "AD_timeout")
+ raise Net::ReadTimeout
+ end
+ expect { described_class.dispatch!(room:, agent_name: "assistant") }.to raise_error(
+ described_class::DispatchError,
+ )
+
+ Voice::AgentManager.reconcile(room)
+
+ expect(
+ @delete.with(body: hash_including("dispatch_id" => "AD_timeout")),
+ ).to have_been_requested
+ expect(described_class.pending?(room.id)).to eq(false)
+ end
+ end
+
+ describe ".cancel" do
+ it "cancels a dispatch after its room has been deleted" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ room.destroy!
+
+ described_class.cancel(room.id)
+
+ expect(@delete).to have_been_requested.once
+ expect(described_class.pending?(room.id)).to eq(false)
+ end
+
+ it "removes local admission immediately and retries failed dispatch deletion after a kick" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ Voice::AgentManager.reconcile(room)
+ @delete =
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.AgentDispatchService/DeleteDispatch",
+ ).to_return(status: 503).then.to_return(status: 200, body: "{}")
+
+ Voice::AgentManager.evict!(room:, user_id: bot.id)
+
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ expect(described_class.pending?(room.id)).to eq(true)
+ expect(
+ a_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/RemoveParticipant",
+ ).with(body: hash_including("identity" => "agent-test")),
+ ).to have_been_made.once
+ Voice::AgentManager.reconcile(room)
+ expect(described_class.pending?(room.id)).to eq(false)
+ expect(@delete).to have_been_requested.twice
+ end
+
+ it "revokes active admission when the feature is disabled" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ Voice::AgentManager.reconcile(room)
+
+ SiteSetting.voice_livekit_agent_enabled = false
+ Voice::AgentManager.reconcile(room)
+
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ expect(@delete).to have_been_requested.once
+ end
+
+ it "stops the dispatch when the last human leaves" do
+ described_class.dispatch!(room:, agent_name: "assistant")
+ Voice::AgentManager.reconcile(room)
+ Voice::ParticipantTracker.remove(room.id, user.id)
+
+ Voice::AgentManager.evict_agents_in_room!(room)
+
+ expect(@delete).to have_been_requested.once
+ expect(described_class.pending?(room.id)).to eq(false)
+ expect(Voice::ParticipantTracker.agent_user_ids(room.id)).to be_empty
+ end
+ end
+end
diff --git a/plugins/voice/spec/services/voice/agent_manager_spec.rb b/plugins/voice/spec/services/voice/agent_manager_spec.rb
new file mode 100644
index 00000000000..10ba6ee6108
--- /dev/null
+++ b/plugins/voice/spec/services/voice/agent_manager_spec.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+RSpec.describe Voice::AgentManager do
+ fab!(:user)
+ fab!(:room) { Fabricate(:voice_room, public: true) }
+ fab!(:other_room) { Fabricate(:voice_room, public: true) }
+
+ before do
+ SiteSetting.voice_enabled = true
+ SiteSetting.voice_livekit_url = "wss://test.livekit.cloud"
+ SiteSetting.voice_livekit_api_key = "key"
+ SiteSetting.voice_livekit_api_secret = "secret"
+ SiteSetting.voice_livekit_agent_enabled = true
+ [room, other_room].each do |voice_room|
+ Voice::ParticipantTracker.pin_transport!(voice_room.id, "livekit")
+ Voice::ParticipantTracker.add(voice_room.id, user.id)
+ end
+ stub_request(
+ :post,
+ "https://test.livekit.cloud/twirp/livekit.RoomService/DeleteRoom",
+ ).to_return(status: 503)
+ end
+
+ describe ".evict_agents_in_room!" do
+ it "revokes an admission proof before a dispatch is saved without revoking other rooms" do
+ bot_id = Voice::AgentBot.user.id
+ session_id = described_class.authorize_session!(room:, agent_name: "assistant")
+ other_session_id =
+ described_class.authorize_session!(room: other_room, agent_name: "assistant")
+
+ described_class.evict_agents_in_room!(room)
+ Voice::ParticipantTracker.pin_transport!(room.id, "livekit")
+
+ expect(
+ described_class.authorized_session(room:, user_id: bot_id, metadata: session_id),
+ ).to be_nil
+ expect(
+ described_class.authorized_session(
+ room: other_room,
+ user_id: bot_id,
+ metadata: other_session_id,
+ ),
+ ).to be_present
+ end
+ end
+end
diff --git a/plugins/voice/spec/services/voice/badge_granter_hooks_spec.rb b/plugins/voice/spec/services/voice/badge_granter_hooks_spec.rb
index f4251ec49b7..3fb9e264887 100644
--- a/plugins/voice/spec/services/voice/badge_granter_hooks_spec.rb
+++ b/plugins/voice/spec/services/voice/badge_granter_hooks_spec.rb
@@ -131,6 +131,17 @@ RSpec.describe Voice::BadgeGranterHooks do
end
describe ".on_join" do
+ it "ignores agents when evaluating room capacity and new companions" do
+ bot = Fabricate(:user, id: -1400)
+ room.update!(max_participants: 2)
+
+ described_class.on_join(user, room, User.where(id: [user.id, bot.id]))
+ described_class.on_join(bot, room, User.where(id: [user.id, bot.id]))
+
+ expect(user.badges).to be_empty
+ expect(bot.badges).to be_empty
+ end
+
describe "Packed House" do
it "grants when room reaches max capacity" do
other1 = Fabricate(:user)
diff --git a/plugins/voice/spec/services/voice/participant_tracker_spec.rb b/plugins/voice/spec/services/voice/participant_tracker_spec.rb
index 9f85be11396..632e237c074 100644
--- a/plugins/voice/spec/services/voice/participant_tracker_spec.rb
+++ b/plugins/voice/spec/services/voice/participant_tracker_spec.rb
@@ -17,9 +17,17 @@ RSpec.describe Voice::ParticipantTracker do
it "ignores invalid user ids" do
described_class.add(room.id, 0)
- described_class.add(room.id, -1)
+ described_class.add(room.id, nil)
expect(described_class.user_ids(room.id)).to be_empty
end
+
+ it "includes bot identities in both individual and batched rosters" do
+ bot = Fabricate(:user, id: -1400)
+ described_class.add(room.id, bot.id)
+ expect(described_class.list(room.id)).to contain_exactly(bot)
+ expect(described_class.room_states([room.id])[room.id].participant_ids).to eq([bot.id])
+ expect(described_class.human_user_ids(room.id)).to be_empty
+ end
end
describe ".remove" do
diff --git a/plugins/voice/spec/system/configure_livekit_agent_spec.rb b/plugins/voice/spec/system/configure_livekit_agent_spec.rb
new file mode 100644
index 00000000000..4ee346eb716
--- /dev/null
+++ b/plugins/voice/spec/system/configure_livekit_agent_spec.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+describe "Configure a LiveKit agent" do
+ fab!(:admin)
+ let(:settings_page) { PageObjects::Pages::AdminSiteSettings.new }
+
+ before do
+ SiteSetting.voice_enabled = true
+ SiteSetting.voice_livekit_url = "wss://test.livekit.cloud"
+ SiteSetting.voice_livekit_api_key = "key"
+ SiteSetting.voice_livekit_api_secret = "secret"
+ sign_in(admin)
+ end
+
+ it "lets an admin enable the bot without configuring an agent name" do
+ settings_page.visit("voice_livekit_agent")
+ expect(settings_page).to have_setting("voice_livekit_agent_enabled")
+ expect(settings_page).to have_setting("voice_livekit_agent_invite_allowed_groups")
+ expect(page).to have_content("Voice livekit agent enabled")
+ # The description embeds a {{setting:...}} token that renders as a link,
+ # so only a token-free fragment can be matched literally.
+ expect(page).to have_content("to invite a LiveKit Cloud agent to public voice rooms")
+
+ settings_page.toggle_bool_setting("voice_livekit_agent_enabled")
+
+ page.refresh
+ expect(settings_page.bool_setting_checkbox("voice_livekit_agent_enabled")).to be_checked
+ end
+end
diff --git a/plugins/voice/test/javascripts/integration/components/voice-invite-agent-button-test.gjs b/plugins/voice/test/javascripts/integration/components/voice-invite-agent-button-test.gjs
new file mode 100644
index 00000000000..9d89c48b2c2
--- /dev/null
+++ b/plugins/voice/test/javascripts/integration/components/voice-invite-agent-button-test.gjs
@@ -0,0 +1,210 @@
+import Service from "@ember/service";
+import { click, fillIn, render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import formKit from "discourse/tests/helpers/form-kit-helper";
+import { logIn } from "discourse/tests/helpers/qunit-helpers";
+import DDropdownMenu from "discourse/ui-kit/d-dropdown-menu";
+import VoiceInviteAgentModal from "discourse/plugins/voice/discourse/components/modal/voice-invite-agent";
+import VoiceInviteAgentButton from "discourse/plugins/voice/discourse/components/voice-invite-agent-button";
+
+class ModalStub extends Service {
+ show(component, options) {
+ this.component = component;
+ this.model = options.model;
+ }
+}
+
+module("Integration | Component | VoiceInviteAgentButton", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ logIn(this.owner);
+ this.owner.unregister("service:modal");
+ this.owner.register("service:modal", ModalStub);
+ this.owner.lookup("service:site").set("voice_livekit_agent_bot_id", -2);
+ this.room = {
+ id: 1,
+ public: true,
+ expected_transport: "livekit",
+ active_participants: [{ id: 42 }],
+ };
+ });
+
+ test("opens the agent chooser for this room", async function (assert) {
+ await render(
+ <template>
+ <DDropdownMenu as |dropdown|>
+ <VoiceInviteAgentButton @item={{dropdown.item}} @room={{this.room}} />
+ </DDropdownMenu>
+ </template>
+ );
+ await click(".voice-invite-agent");
+ const modal = this.owner.lookup("service:modal");
+ assert.strictEqual(modal.component, VoiceInviteAgentModal);
+ assert.strictEqual(modal.model.room, this.room);
+ });
+
+ test("submits an agent picked from the deployed list", async function (assert) {
+ let invited = false;
+ this.model = { room: this.room };
+ this.closeModal = () => {
+ this.closed = true;
+ };
+ pretender.get("/voice/agents", () =>
+ response(200, { agents: [{ name: "assistant" }, { name: "support" }] })
+ );
+ pretender.post("/voice/rooms/1/invite_agent", (request) => {
+ invited = true;
+ assert.strictEqual(request.requestBody, "agent_name=assistant");
+ return response(201, { dispatch_id: "AD_test" });
+ });
+ await render(
+ <template>
+ <VoiceInviteAgentModal
+ @closeModal={{this.closeModal}}
+ @inline={{true}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ assert.dom("[data-name='agent_name'] input").doesNotExist();
+ await formKit().field("agent_name").select("assistant");
+ await click("button[type='submit']");
+ assert.true(invited);
+ assert.true(this.closed);
+ });
+
+ test("lets the inviter type a name instead of picking one", async function (assert) {
+ let invited = false;
+ this.model = { room: this.room };
+ this.closeModal = () => {};
+ pretender.get("/voice/agents", () =>
+ response(200, { agents: [{ name: "assistant" }] })
+ );
+ pretender.post("/voice/rooms/1/invite_agent", (request) => {
+ invited = true;
+ assert.strictEqual(request.requestBody, "agent_name=custom-agent");
+ return response(201, { dispatch_id: "AD_test" });
+ });
+ await render(
+ <template>
+ <VoiceInviteAgentModal
+ @closeModal={{this.closeModal}}
+ @inline={{true}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ await click(".voice-invite-agent-modal__toggle");
+ assert.dom("[data-name='agent_name'] select").doesNotExist();
+ await fillIn("[data-name='agent_name'] input", "custom-agent");
+ await click("button[type='submit']");
+ assert.true(invited);
+ });
+
+ test("refreshes the list and switches to the picker when agents appear", async function (assert) {
+ this.model = { room: this.room };
+ let agents = [];
+ const queries = [];
+ pretender.get("/voice/agents", (request) => {
+ queries.push(request.queryParams.refresh);
+ return response(200, { agents });
+ });
+ await render(
+ <template>
+ <VoiceInviteAgentModal
+ @closeModal={{this.closeModal}}
+ @inline={{true}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ assert.dom("[data-name='agent_name'] input").exists();
+
+ agents = [{ name: "assistant" }];
+ await click(".voice-invite-agent-modal__refresh");
+
+ assert.deepEqual(queries, [undefined, "true"]);
+ assert.dom("[data-name='agent_name'] select").exists();
+ assert.dom("[data-name='agent_name'] option[value='assistant']").exists();
+ });
+
+ test("falls back to a typed name when the list is unavailable", async function (assert) {
+ this.model = { room: this.room };
+ pretender.get("/voice/agents", () => response(503, { errors: ["down"] }));
+ await render(
+ <template>
+ <VoiceInviteAgentModal
+ @closeModal={{this.closeModal}}
+ @inline={{true}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ assert.dom("[data-name='agent_name'] input").exists();
+ assert.dom("[data-name='agent_name'] select").doesNotExist();
+ });
+
+ test("submits a typed agent name when none are listed", async function (assert) {
+ let invited = false;
+ this.model = { room: this.room };
+ this.closeModal = () => {
+ this.closed = true;
+ };
+ pretender.get("/voice/agents", () => response(200, { agents: [] }));
+ pretender.post("/voice/rooms/1/invite_agent", (request) => {
+ invited = true;
+ assert.strictEqual(request.requestBody, "agent_name=assistant");
+ return response(201, { dispatch_id: "AD_test" });
+ });
+ await render(
+ <template>
+ <VoiceInviteAgentModal
+ @closeModal={{this.closeModal}}
+ @inline={{true}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ await fillIn("[data-name='agent_name'] input", "assistant");
+ await click("button[type='submit']");
+ assert.true(invited);
+ assert.true(this.closed);
+ });
+
+ test("hides the action outside eligible calls", async function (assert) {
+ for (const changes of [
+ { public: false },
+ { expected_transport: "mesh" },
+ { active_participants: [] },
+ { active_participants: [{ id: 42 }, { id: -2 }] },
+ ]) {
+ this.set("testRoom", { ...this.room, ...changes });
+ await render(
+ <template>
+ <DDropdownMenu as |dropdown|>
+ <VoiceInviteAgentButton
+ @item={{dropdown.item}}
+ @room={{this.testRoom}}
+ />
+ </DDropdownMenu>
+ </template>
+ );
+ assert.dom(".voice-invite-agent").doesNotExist();
+ }
+ });
+
+ test("requires an available bot the user may invite", async function (assert) {
+ this.owner.lookup("service:site").set("voice_livekit_agent_bot_id", null);
+ await render(
+ <template>
+ <DDropdownMenu as |dropdown|>
+ <VoiceInviteAgentButton @item={{dropdown.item}} @room={{this.room}} />
+ </DDropdownMenu>
+ </template>
+ );
+ assert.dom(".voice-invite-agent").doesNotExist();
+ });
+});
diff --git a/plugins/voice/test/javascripts/integration/components/voice-participant-sidebar-context-menu-test.gjs b/plugins/voice/test/javascripts/integration/components/voice-participant-sidebar-context-menu-test.gjs
index 8270e8cd6be..888f59400ae 100644
--- a/plugins/voice/test/javascripts/integration/components/voice-participant-sidebar-context-menu-test.gjs
+++ b/plugins/voice/test/javascripts/integration/components/voice-participant-sidebar-context-menu-test.gjs
@@ -2,6 +2,7 @@ import Service from "@ember/service";
import { click, render } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
import { logIn } from "discourse/tests/helpers/qunit-helpers";
import VoiceParticipantSidebarContextMenu from "discourse/plugins/voice/discourse/components/voice-participant-sidebar-context-menu";
@@ -40,6 +41,39 @@ module(
};
});
+ test("kicks a bot without an expulsion dialog", async function (assert) {
+ this.menuData.participant = {
+ id: -2,
+ username: "livekit_agent_bot",
+ external_agent: true,
+ };
+ this.menuData.canManageRoom = true;
+ let kicked = false;
+ pretender.delete("/voice/rooms/1/kick", (request) => {
+ kicked = true;
+ assert.strictEqual(
+ request.requestBody,
+ "user_id=-2",
+ "sends only the bot ID"
+ );
+ return response(204);
+ });
+ await render(
+ <template>
+ <VoiceParticipantSidebarContextMenu
+ @close={{this.closeMenu}}
+ @data={{this.menuData}}
+ />
+ </template>
+ );
+
+ await click(".voice-participant-sidebar-context-menu__kick-btn");
+
+ assert.true(kicked);
+ assert.true(this.closed);
+ assert.dom(".d-modal").doesNotExist();
+ });
+
test("spotlights the participant for the viewer", async function (assert) {
await render(
<template>
diff --git a/plugins/voice/test/javascripts/integration/components/voice/call-widget-test.gjs b/plugins/voice/test/javascripts/integration/components/voice/call-widget-test.gjs
index 2b96e36357d..59daadb1d71 100644
--- a/plugins/voice/test/javascripts/integration/components/voice/call-widget-test.gjs
+++ b/plugins/voice/test/javascripts/integration/components/voice/call-widget-test.gjs
@@ -129,6 +129,17 @@ module("Integration | Component | voice/call-widget", function (hooks) {
];
});
+ test("offers the agent invitation in the widget room menu", async function (assert) {
+ this.owner.lookup("service:site").set("voice_livekit_agent_bot_id", -2);
+ Object.assign(this.voiceRooms.rooms[0], {
+ public: true,
+ expected_transport: "livekit",
+ });
+ await render(<template><VoiceCallWidget /></template>);
+ await click("button[data-identifier='voice-widget-room-menu']");
+ assert.dom(".voice-invite-agent").hasText("Invite LiveKit agent");
+ });
+
test("keeps video watching and participant tiles live in widget mode", async function (assert) {
this.set("renderWidget", true);
await render(
diff --git a/plugins/voice/test/javascripts/unit/services/voice-webrtc-livekit-test.js b/plugins/voice/test/javascripts/unit/services/voice-webrtc-livekit-test.js
index 1807b1b8c65..e8d986a895a 100644
--- a/plugins/voice/test/javascripts/unit/services/voice-webrtc-livekit-test.js
+++ b/plugins/voice/test/javascripts/unit/services/voice-webrtc-livekit-test.js
@@ -790,6 +790,234 @@ module("Voice | Unit | Service | voice-webrtc-livekit", function (hooks) {
);
});
+ test("agent audio waits for authorization and follows roster role changes", async function (assert) {
+ await this.subject.join(this.room);
+ await wait(50);
+
+ const lkRoom = this.FakeLivekitRoom.instances[0];
+ const track = {
+ kind: "audio",
+ mediaStreamTrack: createFakeTrack("agent-mic"),
+ mediaStream: null,
+ };
+ const publication = {
+ source: "microphone",
+ track,
+ setSubscribed(value) {
+ if (!value) {
+ this.track = null;
+ lkRoom.emit("trackUnsubscribed", track, this, { identity: "-1400" });
+ } else if (!this.track) {
+ Promise.resolve().then(() => {
+ this.track = track;
+ lkRoom.emit("trackSubscribed", track, this, { identity: "-1400" });
+ });
+ }
+ },
+ };
+ lkRoom.remoteParticipants.set("-1400", {
+ identity: "-1400",
+ trackPublications: new Map([["mic", publication]]),
+ });
+ lkRoom.emit("trackSubscribed", track, publication, { identity: "-1400" });
+ assert.false(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "unknown agents cannot play audio"
+ );
+
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [
+ { id: this.currentUser.id },
+ { id: -1400, role: "speaker", external_agent: true },
+ ],
+ });
+ await wait(10);
+ assert.true(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "roster admission recovers an already subscribed track"
+ );
+
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [
+ { id: this.currentUser.id },
+ { id: -1400, role: "participant", external_agent: true },
+ ],
+ });
+ await wait(10);
+ assert.false(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "listener agents cannot play audio even in open rooms"
+ );
+ assert.strictEqual(
+ publication.track,
+ null,
+ "demotion unsubscribes the microphone"
+ );
+
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [
+ { id: this.currentUser.id },
+ { id: -1400, role: "speaker", external_agent: true },
+ ],
+ });
+ await wait(10);
+ assert.true(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "promotion restores the agent track"
+ );
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [{ id: this.currentUser.id }],
+ });
+ await wait(10);
+ assert.false(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "exclusion removes agent audio"
+ );
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [
+ { id: this.currentUser.id },
+ { id: -1400, role: "speaker", external_agent: true },
+ ],
+ });
+ await wait(10);
+ assert.true(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "readmission subscribes to the retained microphone publication"
+ );
+ });
+
+ test("dashboard agent audio follows the server identity mapping and moderation", async function (assert) {
+ await this.subject.join(this.room);
+ await wait(50);
+
+ const lkRoom = this.FakeLivekitRoom.instances[0];
+ const track = {
+ kind: "audio",
+ mediaStreamTrack: createFakeTrack("agent-mic"),
+ mediaStream: null,
+ };
+ const publication = {
+ source: "microphone",
+ track,
+ setSubscribed(value) {
+ if (!value) {
+ this.track = null;
+ lkRoom.emit("trackUnsubscribed", track, this, {
+ identity: "agent-dashboard",
+ });
+ } else if (!this.track) {
+ Promise.resolve().then(() => {
+ this.track = track;
+ lkRoom.emit("trackSubscribed", track, this, {
+ identity: "agent-dashboard",
+ });
+ });
+ }
+ },
+ };
+ lkRoom.remoteParticipants.set("agent-dashboard", {
+ identity: "agent-dashboard",
+ trackPublications: new Map([["mic", publication]]),
+ });
+ lkRoom.emit("trackSubscribed", track, publication, {
+ identity: "agent-dashboard",
+ });
+ assert.false(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "unknown agents cannot play audio"
+ );
+
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [
+ { id: this.currentUser.id },
+ {
+ id: -1400,
+ role: "speaker",
+ external_agent: true,
+ livekit_identity: "agent-dashboard",
+ },
+ ],
+ });
+ await wait(10);
+ assert.true(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "roster admission recovers an already subscribed track"
+ );
+
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [
+ { id: this.currentUser.id },
+ {
+ id: -1400,
+ role: "participant",
+ external_agent: true,
+ livekit_identity: "agent-dashboard",
+ },
+ ],
+ });
+ await wait(10);
+ assert.false(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "listener agents cannot play audio even in open rooms"
+ );
+ assert.strictEqual(
+ publication.track,
+ null,
+ "demotion unsubscribes the microphone"
+ );
+
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [
+ { id: this.currentUser.id },
+ {
+ id: -1400,
+ role: "speaker",
+ external_agent: true,
+ livekit_identity: "agent-dashboard",
+ },
+ ],
+ });
+ await wait(10);
+ assert.true(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "promotion restores the agent track"
+ );
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [{ id: this.currentUser.id }],
+ });
+ await wait(10);
+ assert.false(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "exclusion removes agent audio"
+ );
+ this.rooms.emit(1, {
+ type: "participants",
+ participants: [
+ { id: this.currentUser.id },
+ {
+ id: -1400,
+ role: "speaker",
+ external_agent: true,
+ livekit_identity: "agent-dashboard",
+ },
+ ],
+ });
+ await wait(10);
+ assert.true(
+ !!this.subject.remoteStreamFor(1, -1400),
+ "readmission subscribes to the retained microphone publication"
+ );
+ });
+
test("a participant expelled from the roster loses their media", async function (assert) {
await this.subject.join(this.room);
await wait(50);