Skip to content

Commit 14f5373

Browse files
committed
Fix inconsistencies
1 parent a95e8c9 commit 14f5373

File tree

10 files changed

+61
-54
lines changed

10 files changed

+61
-54
lines changed

Sources/LiveKit/Agent/Agent.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import Foundation
18-
import LiveKit
1918

2019
@MainActor
2120
open class Agent: ObservableObject {
@@ -33,7 +32,7 @@ open class Agent: ObservableObject {
3332

3433
private func observe(_ participant: Participant) {
3534
Task { [weak self] in
36-
for await _ in participant.changes {
35+
for try await _ in participant.changes {
3736
guard let self else { return }
3837

3938
state = participant.agentState
@@ -47,3 +46,9 @@ open class Agent: ObservableObject {
4746
avatarVideoTrack = participant.avatarWorker?.firstCameraVideoTrack
4847
}
4948
}
49+
50+
extension AgentState: CustomStringConvertible {
51+
public var description: String {
52+
rawValue.capitalized
53+
}
54+
}

Sources/LiveKit/Agent/AgentState+.swift

Lines changed: 0 additions & 21 deletions
This file was deleted.

Sources/LiveKit/Agent/Chat/Receive/TranscriptionDelegateReceiver.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import Foundation
18-
import LiveKit
1918

2019
/// An actor that receives transcription messages from the room and yields them as messages.
2120
///

Sources/LiveKit/Agent/Chat/Receive/TranscriptionStreamReceiver.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import Foundation
18-
import LiveKit
1918

2019
/// An actor that converts raw text streams from the LiveKit `Room` into `Message` objects.
2120
/// - Note: Streams are supported by `livekit-agents` >= 1.0.0.

Sources/LiveKit/Agent/Chat/Send/TextMessageSender.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import Foundation
18-
import LiveKit
1918

2019
/// An actor that sends local messages to the agent.
2120
/// Currently, it only supports sending text messages.

Sources/LiveKit/Agent/Conversation+Environment.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,60 @@
1616

1717
import SwiftUI
1818

19-
extension EnvironmentValues {
19+
#if swift(>=6.0)
20+
public extension EnvironmentValues {
2021
@Entry var agentName: String? = nil
2122
}
23+
#else
24+
public struct AgentNameKey: EnvironmentKey {
25+
public static let defaultValue: String? = nil
26+
}
27+
28+
public extension EnvironmentValues {
29+
var agentName: String? {
30+
get { self[AgentNameKey.self] }
31+
set { self[AgentNameKey.self] = newValue }
32+
}
33+
}
34+
#endif
2235

2336
@MainActor
2437
@propertyWrapper
25-
struct LKConversation: DynamicProperty {
38+
public struct LKConversation: DynamicProperty {
2639
@EnvironmentObject private var conversation: Conversation
2740

28-
var wrappedValue: Conversation {
41+
public init() {}
42+
43+
public var wrappedValue: Conversation {
2944
conversation
3045
}
3146
}
3247

3348
@MainActor
3449
@propertyWrapper
35-
struct LKLocalMedia: DynamicProperty {
50+
public struct LKLocalMedia: DynamicProperty {
3651
@EnvironmentObject private var localMedia: LocalMedia
3752

38-
var wrappedValue: LocalMedia {
53+
public init() {}
54+
55+
public var wrappedValue: LocalMedia {
3956
localMedia
4057
}
4158
}
4259

4360
@MainActor
4461
@propertyWrapper
45-
struct LKAgent: DynamicProperty {
62+
public struct LKAgent: DynamicProperty {
4663
@EnvironmentObject private var conversation: Conversation
4764
@Environment(\.agentName) private var environmentName
4865

4966
let agentName: String?
5067

51-
init(named agentName: String? = nil) {
68+
public init(named agentName: String? = nil) {
5269
self.agentName = agentName
5370
}
5471

55-
var wrappedValue: Agent? {
72+
public var wrappedValue: Agent? {
5673
if let agentName {
5774
return conversation.agent(named: agentName)
5875
} else if let environmentName {

Sources/LiveKit/Agent/Conversation.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import Collections
1817
import Foundation
19-
import LiveKit
18+
import OrderedCollections
2019

2120
@MainActor
2221
open class Conversation: ObservableObject {
@@ -86,7 +85,7 @@ open class Conversation: ObservableObject {
8685

8786
private func observe(room: Room, agentName _: String?) {
8887
Task { [weak self] in
89-
for await _ in room.changes {
88+
for try await _ in room.changes {
9089
guard let self else { return }
9190

9291
connectionState = room.connectionState
@@ -143,7 +142,7 @@ open class Conversation: ObservableObject {
143142

144143
defer {
145144
waitForAgentTask = Task {
146-
try await Task.sleep(for: .seconds(waitForAgent))
145+
try await Task.sleep(nanoseconds: UInt64(TimeInterval(NSEC_PER_SEC) * waitForAgent))
147146
try Task.checkCancellation()
148147
if connectionState == .connected, agents.isEmpty {
149148
await end()

Sources/LiveKit/Agent/LocalMedia.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
@preconcurrency import AVFoundation
18-
import LiveKit
1918

2019
@MainActor
2120
open class LocalMedia: ObservableObject {
@@ -64,7 +63,7 @@ open class LocalMedia: ObservableObject {
6463

6564
private func observe(room: Room) {
6665
Task { [weak self] in
67-
for await _ in room.changes {
66+
for try await _ in room.changes {
6867
guard let self else { return }
6968

7069
microphoneTrack = room.localParticipant.firstAudioTrack

Sources/LiveKit/Support/ObservableObject+.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
import Combine
17+
@preconcurrency import Combine
1818

19-
@available(iOS 15, *)
2019
extension ObservableObject {
21-
typealias BufferedObjectWillChangePublisher = Publishers.Buffer<ObjectWillChangePublisher>
22-
23-
// This is necessary due to ObservableObjectPublisher not respecting the demand.
24-
// See: https://forums.swift.org/t/asyncpublisher-causes-crash-in-rather-simple-situation
25-
private var bufferedObjectWillChange: BufferedObjectWillChangePublisher {
26-
objectWillChange
27-
.buffer(size: 1, prefetch: .byRequest, whenFull: .dropOldest)
28-
}
29-
30-
/// A publisher that emits the `objectWillChange` events.
31-
var changes: AsyncPublisher<BufferedObjectWillChangePublisher> {
32-
bufferedObjectWillChange.values
20+
/// An async sequence that emits the `objectWillChange` events.
21+
var changes: any AsyncSequence {
22+
if #available(macOS 12.0, iOS 15.0, tvOS 15.0, *) {
23+
// This is necessary due to ObservableObjectPublisher not respecting the demand.
24+
// See: https://forums.swift.org/t/asyncpublisher-causes-crash-in-rather-simple-situation
25+
objectWillChange.buffer(size: 1, prefetch: .byRequest, whenFull: .dropOldest).values
26+
} else {
27+
AsyncStream { continuation in
28+
let cancellable = objectWillChange.sink { _ in
29+
continuation.yield()
30+
}
31+
continuation.onTermination = { _ in
32+
cancellable.cancel()
33+
}
34+
}
35+
}
3336
}
3437
}

Sources/LiveKit/Track/VideoTrack.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,11 @@ extension VideoTrack {
6969
return missingCodecs
7070
}
7171
}
72+
73+
public extension VideoTrack {
74+
/// The aspect ratio of the video track or 1 if the dimensions are not available.
75+
var aspectRatio: CGFloat {
76+
guard let dimensions else { return 1 }
77+
return CGFloat(dimensions.width) / CGFloat(dimensions.height)
78+
}
79+
}

0 commit comments

Comments
 (0)