Skip to content

Commit 6ecc362

Browse files
committed
More abstract credentials
1 parent 075b67b commit 6ecc362

File tree

4 files changed

+41
-58
lines changed

4 files changed

+41
-58
lines changed

VoiceAgent/Agent/AgentSession+Types.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,4 @@ extension AgentSession {
1717
// }
1818
// }
1919
}
20-
21-
struct Options {
22-
let room: Room
23-
24-
init(room: Room = .init()) {
25-
self.room = room
26-
}
27-
}
28-
29-
enum Environment {
30-
// .envfile?
31-
case sandbox(id: String, room: String = "room-\(Int.random(in: 1000 ... 9999))", participant: String = "participant-\(Int.random(in: 1000 ... 9999))")
32-
case cloud(server: String, token: String)
33-
}
3420
}

VoiceAgent/Agent/AgentSession.swift

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class AgentSession: ObservableObject {
4343

4444
// MARK: - Dependencies
4545

46-
private let environment: Environment
46+
private let credentials: any CredentialsProvider
4747
private let room: Room
4848
private let senders: [any MessageSender]
4949
private let receivers: [any MessageReceiver]
@@ -54,9 +54,9 @@ final class AgentSession: ObservableObject {
5454

5555
// MARK: - Init
5656

57-
init(environment: Environment, options: Options = .init(), senders: [any MessageSender]? = nil, receivers: [any MessageReceiver]? = nil) {
58-
self.environment = environment
59-
room = options.room
57+
init(credentials: CredentialsProvider, room: Room = .init(), senders: [any MessageSender]? = nil, receivers: [any MessageReceiver]? = nil) {
58+
self.credentials = credentials
59+
self.room = room
6060

6161
let textMessageSender = TextMessageSender(room: room)
6262
self.senders = senders ?? [textMessageSender]
@@ -113,20 +113,15 @@ final class AgentSession: ObservableObject {
113113
}
114114
}
115115

116-
let connection = { @Sendable in
117-
let (server, token) = try await self.credentials()
118-
try await self.room.connect(url: server, token: token, connectOptions: options, roomOptions: roomOptions)
119-
}
120-
121116
do {
122117
if preConnectAudio {
123118
try await room.withPreConnectAudio(timeout: waitForAgent) {
124119
await MainActor.run { self.isListening = true }
125-
try await connection()
120+
try await self.room.connect(credentialsProvider: self.credentials, connectOptions: options, roomOptions: roomOptions)
126121
await MainActor.run { self.isListening = false }
127122
}
128123
} else {
129-
try await connection()
124+
try await room.connect(credentialsProvider: credentials, connectOptions: options, roomOptions: roomOptions)
130125
}
131126
} catch {
132127
self.error = .failedToConnect(error)
@@ -198,16 +193,4 @@ final class AgentSession: ObservableObject {
198193
self.error = .mediaDevice(error)
199194
}
200195
}
201-
202-
// MARK: - Private
203-
204-
private func credentials() async throws -> (server: String, token: String) {
205-
switch environment {
206-
case let .sandbox(id, room, participant):
207-
let sandboxConnection = try await Sandbox.getConnection(id: id, roomName: room, participantName: participant)
208-
return (sandboxConnection.serverUrl, sandboxConnection.participantToken)
209-
case let .cloud(server, token):
210-
return (server, token)
211-
}
212-
}
213196
}

VoiceAgent/Auth/Sandbox.swift

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
import Foundation
2+
import LiveKit
3+
4+
public struct Credentials: Decodable {
5+
let serverUrl: URL
6+
let participantToken: String
7+
}
8+
9+
public protocol CredentialsProvider: Sendable {
10+
func credentials() async throws -> Credentials
11+
}
12+
13+
public extension Room {
14+
func connect(credentialsProvider: CredentialsProvider,
15+
connectOptions: ConnectOptions? = nil,
16+
roomOptions: RoomOptions? = nil) async throws
17+
{
18+
let credentials = try await credentialsProvider.credentials()
19+
try await connect(url: credentials.serverUrl.absoluteString, token: credentials.participantToken, connectOptions: connectOptions, roomOptions: roomOptions)
20+
}
21+
}
222

323
/// A service for fetching LiveKit authentication tokens.
4-
/// See [docs](https://docs.livekit.io/home/get-started/authentication) for more information.
5-
enum Sandbox {
24+
/// See [docs](https://CredentialsProvider.livekit.io/home/get-started/authentication) for more information.
25+
public struct Sandbox: CredentialsProvider {
26+
private static let url: URL = .init(string: "https://cloud-api.livekit.io/api/sandbox/connection-details")!
27+
628
enum Error: Swift.Error {
729
case noResponse
830
case unsuccessfulStatusCode(Int)
931
case decoding(Swift.Error)
1032
}
1133

12-
struct Connection: Decodable {
13-
let serverUrl: String
14-
let participantToken: String
15-
let roomName: String
16-
let participantName: String
17-
}
18-
19-
private static let url: String = "https://cloud-api.livekit.io/api/sandbox/connection-details"
20-
21-
// TODO: These are not respected anyway (names)
22-
static func getConnection(id: String, roomName: String, participantName: String) async throws -> Connection {
23-
var urlComponents = URLComponents(string: url)!
24-
urlComponents.queryItems = [
25-
URLQueryItem(name: "roomName", value: roomName),
26-
URLQueryItem(name: "participantName", value: participantName),
27-
]
34+
let id: String
2835

29-
var request = URLRequest(url: urlComponents.url!)
36+
public func credentials() async throws -> Credentials {
37+
var request = URLRequest(url: Self.url)
3038
request.httpMethod = "POST"
3139
request.addValue(id.trimmingCharacters(in: CharacterSet(charactersIn: "\"")), forHTTPHeaderField: "X-Sandbox-ID")
3240

@@ -41,9 +49,15 @@ enum Sandbox {
4149
}
4250

4351
do {
44-
return try JSONDecoder().decode(Connection.self, from: data)
52+
return try JSONDecoder().decode(Credentials.self, from: data)
4553
} catch {
4654
throw Error.decoding(error)
4755
}
4856
}
4957
}
58+
59+
extension Credentials: CredentialsProvider {
60+
public func credentials() async throws -> Credentials {
61+
self
62+
}
63+
}

VoiceAgent/VoiceAgentApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct VoiceAgentApp: App {
1313
WindowGroup {
1414
AppView()
1515
.environmentObject(DeviceSwitcher(room: room))
16-
.environmentObject(AgentSession(environment: .sandbox(id: sandboxID), options: .init(room: room)))
16+
.environmentObject(AgentSession(credentials: Sandbox(id: sandboxID), room: room))
1717
}
1818
#if os(macOS)
1919
.defaultSize(width: 900, height: 900)

0 commit comments

Comments
 (0)