Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Sources/LiveKit/Agent/AgentState+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

extension AgentState: CustomStringConvertible {
public var description: String {
rawValue.capitalized
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to remove this one, looks like leftover that could be handled by the frontend.

}
}
54 changes: 0 additions & 54 deletions Sources/LiveKit/Agent/AgentState.swift

This file was deleted.

22 changes: 16 additions & 6 deletions Sources/LiveKit/Agent/Participant+Agent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,29 @@ public extension Participant {
}
}

@objc
internal var agentAttributes: AgentAttributes? {
guard isAgent else { return nil }

guard let encoded = try? JSONEncoder().encode(attributes),
let decoded = try? JSONDecoder().decode(AgentAttributes.self, from: encoded) else { return nil }

return decoded
}

var agentState: AgentState {
guard isAgent else { return .unknown }
guard let attrString = attributes[agentStateAttributeKey] else { return .unknown }
guard let state = AgentState.fromString(attrString) else { return .unknown }
return state
agentAttributes?.lkAgentState ?? .idle
}

@objc
var agentStateString: String {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sort of breaks Obj-C contract, but IMO it's better than changing the schema for every single language, just for the sake of having Int raw value here...

agentState.rawValue
}
}

public extension Participant {
private var publishingOnBehalf: [Participant.Identity: Participant] {
guard let _room else { return [:] }
return _room.allParticipants.filter { $0.value.attributes[publishOnBehalfAttributeKey] == identity?.stringValue }
return _room.allParticipants.filter { $0.value.agentAttributes?.lkPublishOnBehalf == identity?.stringValue }
}

/// The avatar worker participant associated with the agent.
Expand Down
4 changes: 1 addition & 3 deletions Sources/LiveKit/Agent/Room+Agent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

import Foundation

let publishOnBehalfAttributeKey = "lk.publish_on_behalf"

public extension Room {
/// A dictionary containing all agent participants.
///
/// - Note: This will not include participants that are publishing on behalf of another participant
/// e.g. avatar workers. To access them directly use ``Participant/avatarWorker`` property of `agentParticipant`.
@objc
var agentParticipants: [Participant.Identity: Participant] {
allParticipants.filter { $0.value.isAgent && $0.value.attributes[publishOnBehalfAttributeKey] == nil }
allParticipants.filter { $0.value.isAgent && $0.value.agentAttributes?.lkPublishOnBehalf == nil }
}

/// The first agent participant.
Expand Down
71 changes: 71 additions & 0 deletions Sources/LiveKit/Types/Attributes/AttributeTypings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation

// MARK: - AgentAttributes

struct AgentAttributes: Codable, Sendable {
let lkAgentInputs: [AgentInput]?
let lkAgentOutputs: [AgentOutput]?
let lkAgentState: AgentState?
let lkPublishOnBehalf: String?

enum CodingKeys: String, CodingKey {
case lkAgentInputs = "lk.agent.inputs"
case lkAgentOutputs = "lk.agent.outputs"
case lkAgentState = "lk.agent.state"
case lkPublishOnBehalf = "lk.publish_on_behalf"
}
}

enum AgentInput: String, Codable, Sendable {
case audio
case text
case video
}

enum AgentOutput: String, Codable, Sendable {
case audio
case transcription
}

public enum AgentState: String, Codable, Sendable {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This public is a manual change, I don't think it's possible to expose a single one vs global "access-level": "public".

case idle
case initializing
case listening
case speaking
case thinking
}

/// Schema for transcription-related attributes

// MARK: - TranscriptionAttributes

struct TranscriptionAttributes: Codable, Sendable {
/// The segment id of the transcription
let lkSegmentID: String?
/// The associated track id of the transcription
let lkTranscribedTrackID: String?
/// Whether the transcription is final
let lkTranscriptionFinal: Bool?

enum CodingKeys: String, CodingKey {
case lkSegmentID = "lk.segment_id"
case lkTranscribedTrackID = "lk.transcribed_track_id"
case lkTranscriptionFinal = "lk.transcription_final"
}
}
Loading