Skip to content

Commit 4ec7974

Browse files
committed
fix: parse user transcript events
1 parent 21ea04f commit 4ec7974

4 files changed

Lines changed: 28 additions & 26 deletions

File tree

Sources/ElevenLabs/Auth/TokenService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public struct TokenService: Sendable {
132132
components.queryItems = [
133133
URLQueryItem(name: "agent_id", value: agentId),
134134
URLQueryItem(name: "source", value: "swift_sdk"),
135-
URLQueryItem(name: "version", value: SDKVersion.version)
135+
URLQueryItem(name: "version", value: SDKVersion.version),
136136
]
137137

138138
guard let url = components.url else {

Sources/ElevenLabs/Conversation.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ public final class Conversation: ObservableObject {
9595

9696
deps.connectionManager.onAgentReady = { [weak self, auth, options] in
9797
Task { @MainActor in
98-
guard let self else {
99-
return
98+
guard let self else {
99+
return
100100
}
101101
// send conversation init exactly when the agent appears
102102
try? await self.sendConversationInit(config: options.toConversationConfig())
103103
// flip to .active once conversation init is sent
104104
self.state = .active(.init(agentId: self.extractAgentId(from: auth)))
105105
}
106106
}
107-
107+
108108
deps.connectionManager.onAgentDisconnected = { [weak self] in
109109
Task { @MainActor in
110110
guard let self else { return }
@@ -149,7 +149,9 @@ public final class Conversation: ObservableObject {
149149

150150
/// Send a text message to the agent.
151151
public func sendMessage(_ text: String) async throws {
152-
guard state.isActive else { throw ConversationError.notConnected }
152+
guard state.isActive else {
153+
throw ConversationError.notConnected
154+
}
153155
let event = OutgoingEvent.userMessage(UserMessageEvent(text: text))
154156
try await publish(event)
155157
appendLocalMessage(text)
@@ -303,7 +305,6 @@ public final class Conversation: ObservableObject {
303305
case let .tentativeAgentResponse(e):
304306
agentState = .speaking
305307
scheduleBackToListening()
306-
appendTentativeAgent(e.tentativeResponse)
307308

308309
case let .agentResponse(e):
309310
agentState = .speaking
@@ -348,7 +349,6 @@ public final class Conversation: ObservableObject {
348349
case let .tentativeAgentResponse(e):
349350
agentState = .speaking
350351
scheduleBackToListening()
351-
appendTentativeAgent(e.tentativeResponse)
352352

353353
case let .agentResponse(e):
354354
agentState = .speaking

Sources/ElevenLabs/Networking/ConnectionManager.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import LiveKit
55
@MainActor
66
class ConnectionManager {
77
private var _room: Room?
8-
8+
99
private var readyDelegate: ReadyDelegate?
1010

1111
var onAgentReady: (() -> Void)?
@@ -21,7 +21,7 @@ class ConnectionManager {
2121
onReady: { [weak self] in self?.onAgentReady?() },
2222
onDisconnected: { [weak self] in self?.onAgentDisconnected?() }
2323
)
24-
readyDelegate = rd // Keep strong reference
24+
readyDelegate = rd // Keep strong reference
2525
room.add(delegate: rd)
2626

2727
let (_, continuation) = AsyncStream<Data>.makeStream()
@@ -38,7 +38,7 @@ class ConnectionManager {
3838
func disconnect() async {
3939
await _room?.disconnect()
4040
_room = nil
41-
readyDelegate = nil // Clean up delegate reference
41+
readyDelegate = nil // Clean up delegate reference
4242
}
4343

4444
func dataEventsStream() -> AsyncStream<Data> {
@@ -64,32 +64,30 @@ class ConnectionManager {
6464
private var agentConnected = false
6565
private let onReady: () -> Void
6666
private let onDisconnected: () -> Void
67-
68-
init(onReady: @escaping () -> Void, onDisconnected: @escaping () -> Void) {
67+
68+
init(onReady: @escaping () -> Void, onDisconnected: @escaping () -> Void) {
6969
self.onReady = onReady
7070
self.onDisconnected = onDisconnected
7171
}
7272

73-
func room(_ room: Room, participantDidConnect participant: RemoteParticipant) {
74-
guard !agentConnected else {
75-
return
73+
func room(_: Room, participantDidConnect _: RemoteParticipant) {
74+
guard !agentConnected else {
75+
return
7676
}
7777
agentConnected = true
7878
onReady()
7979
}
80-
81-
func room(_ room: Room, participantDidDisconnect participant: RemoteParticipant) {
82-
if agentConnected && room.remoteParticipants.isEmpty {
80+
81+
func room(_ room: Room, participantDidDisconnect _: RemoteParticipant) {
82+
if agentConnected, room.remoteParticipants.isEmpty {
8383
agentConnected = false
8484
onDisconnected()
8585
}
8686
}
87-
88-
func roomDidConnect(_ room: Room) {
89-
}
90-
91-
func room(_ room: Room, didUpdate connectionState: ConnectionState, from previousState: ConnectionState) {
92-
}
87+
88+
func roomDidConnect(_: Room) {}
89+
90+
func room(_: Room, didUpdate _: ConnectionState, from _: ConnectionState) {}
9391
}
9492
}
9593

Sources/ElevenLabs/Protocol/EventParser.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ enum EventParser {
1616

1717
switch type {
1818
case "user_transcript":
19-
if let transcript = json["user_transcript"] as? String {
19+
if let event = json["user_transcription_event"] as? [String: Any],
20+
let transcript = event["user_transcript"] as? String
21+
{
2022
return .userTranscript(UserTranscriptEvent(transcript: transcript))
2123
}
2224

2325
case "agent_response":
24-
if let response = json["agent_response"] as? String {
26+
if let event = json["agent_response_event"] as? [String: Any],
27+
let response = event["agent_response"] as? String
28+
{
2529
return .agentResponse(AgentResponseEvent(response: response))
2630
}
2731

0 commit comments

Comments
 (0)