@@ -21,7 +21,7 @@ class CallContainerModel: ObservableObject {
2121 @Published var liveBotMessage : LiveMessage ?
2222 @Published var liveUserMessage : LiveMessage ?
2323
24- var rtviClientIOS : RTVIClient ?
24+ var pipecatClientIOS : PipecatClient ?
2525
2626 @Published var selectedMic : MediaDeviceId ? = nil {
2727 didSet {
@@ -40,7 +40,7 @@ class CallContainerModel: ObservableObject {
4040 }
4141
4242 @MainActor
43- func connect( backendURL: String ) {
43+ func connect( backendURL: String , apiKey : String ) {
4444 self . resetLiveMessages ( )
4545
4646 let baseUrl = backendURL. trimmingCharacters ( in: . whitespacesAndNewlines)
@@ -50,52 +50,48 @@ class CallContainerModel: ObservableObject {
5050 }
5151
5252 let currentSettings = SettingsManager . getSettings ( )
53- let rtviClientOptions = RTVIClientOptions . init (
53+ let pipecatClientOptions = PipecatClientOptions . init (
54+ transport: SmallWebRTCTransport . init ( ) ,
5455 enableMic: currentSettings. enableMic,
5556 enableCam: currentSettings. enableCam,
56- params: RTVIClientParams (
57- config: [
58- . init(
59- service: SmallWebRTCTransport . SERVICE_NAME,
60- options: [
61- . init( name: " server_url " , value: . string( baseUrl) )
62- ]
63- )
64- ]
65- )
6657 )
67- self . rtviClientIOS = RTVIClient . init (
68- transport: SmallWebRTCTransport . init ( options: rtviClientOptions) ,
69- options: rtviClientOptions
58+ self . pipecatClientIOS = PipecatClient . init (
59+ options: pipecatClientOptions
7060 )
71- self . rtviClientIOS ? . delegate = self
61+ self . pipecatClientIOS ? . delegate = self
7262
73- // Registering the llm helper, we will need this to handle the function calling
74- let llmHelper = try ? self . rtviClientIOS? . registerHelper ( service: " llm " , helper: LLMHelper . self)
75- llmHelper? . delegate = self
76-
77- self . rtviClientIOS? . start ( ) { result in
63+ let authorizationToken = apiKey. trimmingCharacters ( in: . whitespacesAndNewlines)
64+ print ( " authorizationToken: \( authorizationToken) " )
65+ let headers = [ [ " Authorization " : " Bearer \( authorizationToken) " ] ]
66+ let startParams = APIRequest . init (
67+ endpoint: URL ( string: baseUrl + " /start " ) !,
68+ headers: headers,
69+ requestData: Value . object ( [
70+ " enableDefaultIceServers " : . boolean( true )
71+ ] )
72+ )
73+ self . pipecatClientIOS? . startBotAndConnect ( startBotParams: startParams) { ( result: Result < SmallWebRTCStartBotResult , AsyncExecutionError > ) in
7874 switch result {
7975 case . failure( let error) :
8076 self . showError ( message: error. localizedDescription)
81- self . rtviClientIOS = nil
82- case . success( ) :
77+ self . pipecatClientIOS = nil
78+ case . success( _ ) :
8379 // Apply initial mic preference
8480 if let selectedMic = SettingsManager . getSettings ( ) . selectedMic {
8581 self . selectMic ( MediaDeviceId ( id: selectedMic) )
8682 }
8783 // Populate available devices list
88- self . availableMics = self . rtviClientIOS ? . getAllMics ( ) ?? [ ]
84+ self . availableMics = self . pipecatClientIOS ? . getAllMics ( ) ?? [ ]
8985 }
9086 }
91- self . saveCredentials ( backendURL: backendURL )
87+ self . saveCredentials ( backendURL: baseUrl , apiKey : authorizationToken )
9288 }
9389
9490 @MainActor
9591 func disconnect( ) {
96- self . rtviClientIOS ? . disconnect ( completion: nil )
97- self . rtviClientIOS ? . release ( )
98- self . rtviClientIOS = nil
92+ self . pipecatClientIOS ? . disconnect ( completion: nil )
93+ self . pipecatClientIOS ? . release ( )
94+ self . pipecatClientIOS = nil
9995 }
10096
10197 func showError( message: String ) {
@@ -110,10 +106,10 @@ class CallContainerModel: ObservableObject {
110106
111107 @MainActor
112108 func toggleMicInput( ) {
113- self . rtviClientIOS ? . enableMic ( enable: !self . isMicEnabled) { result in
109+ self . pipecatClientIOS ? . enableMic ( enable: !self . isMicEnabled) { result in
114110 switch result {
115111 case . success ( ) :
116- self . isMicEnabled = self . rtviClientIOS ? . isMicEnabled ?? false
112+ self . isMicEnabled = self . pipecatClientIOS ? . isMicEnabled ?? false
117113 case . failure ( let error) :
118114 self . showError ( message: error. localizedDescription)
119115 }
@@ -122,27 +118,29 @@ class CallContainerModel: ObservableObject {
122118
123119 @MainActor
124120 func toggleCamInput( ) {
125- self . rtviClientIOS? . enableCam ( enable: !self . isCamEnabled) { result in
121+ print ( " Is cam enabled: \( self . isCamEnabled) " )
122+ self . pipecatClientIOS? . enableCam ( enable: !self . isCamEnabled) { result in
126123 switch result {
127124 case . success ( ) :
128- self . isCamEnabled = self . rtviClientIOS ? . isCamEnabled ?? false
125+ self . isCamEnabled = self . pipecatClientIOS ? . isCamEnabled ?? false
129126 case . failure ( let error) :
130127 self . showError ( message: error. localizedDescription)
131128 }
132129 }
133130 }
134131
135- func saveCredentials( backendURL: String ) {
132+ func saveCredentials( backendURL: String , apiKey : String ) {
136133 var currentSettings = SettingsManager . getSettings ( )
137134 currentSettings. backendURL = backendURL
135+ currentSettings. apiKey = apiKey
138136 // Saving the settings
139137 SettingsManager . updateSettings ( settings: currentSettings)
140138 }
141139
142140 @MainActor
143141 func selectMic( _ mic: MediaDeviceId ) {
144142 self . selectedMic = mic
145- self . rtviClientIOS ? . updateMic ( micId: mic, completion: nil )
143+ self . pipecatClientIOS ? . updateMic ( micId: mic, completion: nil )
146144 }
147145
148146 private func createLiveMessage( content: String = " " , type: MessageType ) {
@@ -176,7 +174,7 @@ class CallContainerModel: ObservableObject {
176174 }
177175}
178176
179- extension CallContainerModel : RTVIClientDelegate , LLMHelperDelegate {
177+ extension CallContainerModel : PipecatClientDelegate {
180178
181179 private func handleEvent( eventName: String , eventValue: Any ? = nil ) {
182180 if let value = eventValue {
@@ -205,8 +203,8 @@ extension CallContainerModel:RTVIClientDelegate, LLMHelperDelegate {
205203 func onConnected( ) {
206204 Task { @MainActor in
207205 self . handleEvent ( eventName: " onConnected " )
208- self . isMicEnabled = self . rtviClientIOS ? . isMicEnabled ?? false
209- self . isCamEnabled = self . rtviClientIOS ? . isCamEnabled ?? false
206+ self . isMicEnabled = self . pipecatClientIOS ? . isMicEnabled ?? false
207+ self . isCamEnabled = self . pipecatClientIOS ? . isCamEnabled ?? false
210208 }
211209 }
212210
@@ -217,10 +215,10 @@ extension CallContainerModel:RTVIClientDelegate, LLMHelperDelegate {
217215 }
218216 }
219217
220- func onError( message: String ) {
218+ func onError( message: RTVIMessageInbound ) {
221219 Task { @MainActor in
222220 self . handleEvent ( eventName: " onError " , eventValue: message)
223- self . showError ( message: message)
221+ self . showError ( message: message. data ?? " " )
224222 }
225223 }
226224
@@ -236,15 +234,33 @@ extension CallContainerModel:RTVIClientDelegate, LLMHelperDelegate {
236234 }
237235 }
238236
239- func onBotTranscript( data: String ) {
240- self . handleEvent ( eventName: " onBotTranscript " , eventValue: data)
237+ func onTrackStarted( track: MediaStreamTrack , participant: Participant ? ) {
238+ Task { @MainActor in
239+ self . handleEvent ( eventName: " onTrackStarted " , eventValue: track)
240+
241+ guard track. kind == . video else { return }
242+
243+ // Use optional binding to simplify the check for local participant
244+ if participant? . local ?? true {
245+ self . localCamId = track. id
246+ } else {
247+ self . botCamId = track. id
248+ }
249+ }
241250 }
242-
243- func onTracksUpdated( tracks: Tracks ) {
244- self . handleEvent ( eventName: " onTracksUpdated " , eventValue: tracks)
251+
252+ func onTrackStopped( track: MediaStreamTrack , participant: Participant ? ) {
245253 Task { @MainActor in
246- self . localCamId = tracks. local. video
247- self . botCamId = tracks. bot? . video ?? nil
254+ self . handleEvent ( eventName: " onTrackStopped " , eventValue: track)
255+
256+ guard track. kind == . video else { return }
257+
258+ // Use optional binding to simplify the check for local participant
259+ if participant? . local ?? true {
260+ self . localCamId = nil
261+ } else {
262+ self . botCamId = nil
263+ }
248264 }
249265 }
250266
@@ -277,7 +293,8 @@ extension CallContainerModel:RTVIClientDelegate, LLMHelperDelegate {
277293 }
278294 }
279295
280- func onBotTTSText( data: BotTTSText ) {
296+ func onBotTranscript( data: BotLLMText ) {
297+ self . handleEvent ( eventName: " onBotTranscript " , eventValue: data)
281298 self . appendTextToLiveMessage ( fromBot: true , content: data. text)
282299 }
283300
0 commit comments