|
| 1 | +/* |
| 2 | + * Copyright 2025 LiveKit |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +/// Represents a LiveKit Agent. |
| 20 | +/// |
| 21 | +/// The ``Agent`` struct represents the state of a LiveKit agent within a ``Session``. |
| 22 | +/// It provides information about the agent's connection status, its current state |
| 23 | +/// (e.g., listening, thinking, speaking), and its media tracks. |
| 24 | +/// |
| 25 | +/// The ``Agent``'s properties are updated automatically by the ``Session`` as the agent's |
| 26 | +/// state changes. This allows the application to react to the agent's |
| 27 | +/// behavior, such as displaying its avatar video or indicating when it is speaking. |
| 28 | +/// The ``agentState`` property is particularly useful for building UIs that reflect |
| 29 | +/// the agent's current activity. |
| 30 | +/// |
| 31 | +/// - SeeAlso: [LiveKit SwiftUI Agent Starter](https://github.com/livekit-examples/agent-starter-swift). |
| 32 | +/// - SeeAlso: [LiveKit Agents documentation](https://docs.livekit.io/agents/). |
| 33 | +public struct Agent: Loggable { |
| 34 | + // MARK: - Error |
| 35 | + |
| 36 | + public enum Error: LocalizedError { |
| 37 | + case timeout |
| 38 | + |
| 39 | + public var errorDescription: String? { |
| 40 | + switch self { |
| 41 | + case .timeout: |
| 42 | + "Agent did not connect" |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // MARK: - State |
| 48 | + |
| 49 | + private enum State { |
| 50 | + case disconnected |
| 51 | + case connecting(buffering: Bool) |
| 52 | + case connected(agentState: AgentState, audioTrack: (any AudioTrack)?, avatarVideoTrack: (any VideoTrack)?) |
| 53 | + case failed(error: Error) |
| 54 | + } |
| 55 | + |
| 56 | + private var state: State = .disconnected |
| 57 | + |
| 58 | + // MARK: - Transitions |
| 59 | + |
| 60 | + mutating func disconnected() { |
| 61 | + log("Agent disconnected from \(state)") |
| 62 | + // From any state |
| 63 | + state = .disconnected |
| 64 | + } |
| 65 | + |
| 66 | + mutating func failed(error: Error) { |
| 67 | + log("Agent failed with error \(error) from \(state)") |
| 68 | + // From any state |
| 69 | + state = .failed(error: error) |
| 70 | + } |
| 71 | + |
| 72 | + mutating func connecting(buffering: Bool) { |
| 73 | + log("Agent connecting from \(state)") |
| 74 | + switch state { |
| 75 | + case .disconnected, .connecting: |
| 76 | + state = .connecting(buffering: buffering) |
| 77 | + default: |
| 78 | + log("Invalid transition from \(state) to connecting", .warning) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + mutating func connected(participant: Participant) { |
| 83 | + log("Agent connected to \(participant) from \(state)") |
| 84 | + switch state { |
| 85 | + case .connecting, .connected: |
| 86 | + state = .connected(agentState: participant.agentState, |
| 87 | + audioTrack: participant.agentAudioTrack, |
| 88 | + avatarVideoTrack: participant.avatarVideoTrack) |
| 89 | + default: |
| 90 | + log("Invalid transition from \(state) to connected", .warning) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // MARK: - Public |
| 95 | + |
| 96 | + /// A boolean value indicating whether the agent is connected. |
| 97 | + public var isConnected: Bool { |
| 98 | + switch state { |
| 99 | + case .connected: true |
| 100 | + default: false |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// The current conversational state of the agent. |
| 105 | + public var agentState: AgentState? { |
| 106 | + switch state { |
| 107 | + case let .connected(agentState, _, _): agentState |
| 108 | + default: nil |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// The agent's audio track. |
| 113 | + public var audioTrack: (any AudioTrack)? { |
| 114 | + switch state { |
| 115 | + case let .connected(_, audioTrack, _): audioTrack |
| 116 | + default: nil |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// The agent's avatar video track. |
| 121 | + public var avatarVideoTrack: (any VideoTrack)? { |
| 122 | + switch state { |
| 123 | + case let .connected(_, _, avatarVideoTrack): avatarVideoTrack |
| 124 | + default: nil |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /// The last error that occurred. |
| 129 | + public var error: Error? { |
| 130 | + switch state { |
| 131 | + case let .failed(error): error |
| 132 | + default: nil |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +private extension Participant { |
| 138 | + var agentAudioTrack: (any AudioTrack)? { |
| 139 | + audioTracks.first(where: { $0.source == .microphone })?.track as? AudioTrack |
| 140 | + } |
| 141 | + |
| 142 | + var avatarVideoTrack: (any VideoTrack)? { |
| 143 | + avatarWorker?.firstCameraVideoTrack |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +extension AgentState: CustomStringConvertible { |
| 148 | + public var description: String { |
| 149 | + rawValue.capitalized |
| 150 | + } |
| 151 | +} |
0 commit comments