-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathIncomingEvents.swift
More file actions
207 lines (178 loc) · 5.87 KB
/
Copy pathIncomingEvents.swift
File metadata and controls
207 lines (178 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import Foundation
// MARK: - Incoming Events (from ElevenLabs)
/// Events that can be received from the ElevenLabs agent
public enum IncomingEvent: Sendable {
case userTranscript(UserTranscriptEvent)
case tentativeUserTranscript(TentativeUserTranscriptEvent)
case agentResponse(AgentResponseEvent)
case agentResponseCorrection(AgentResponseCorrectionEvent)
case agentResponseMetadata(AgentResponseMetadataEvent)
case agentChatResponsePart(AgentChatResponsePartEvent)
case audio(AudioEvent)
case interruption(InterruptionEvent)
case vadScore(VadScoreEvent)
case conversationMetadata(ConversationMetadataEvent)
case ping(PingEvent)
case clientToolCall(ClientToolCallEvent)
case agentToolRequest(AgentToolRequestEvent)
case agentToolResponse(AgentToolResponseEvent)
case mcpToolCall(MCPToolCallEvent)
case mcpConnectionStatus(MCPConnectionStatusEvent)
case error(ErrorEvent)
}
public enum AgentChatResponsePartType: String, Sendable {
case start
case delta
case stop
}
/// User's speech transcription
public struct UserTranscriptEvent: Sendable {
public let transcript: String
public let eventId: Int
}
/// Tentative user's speech transcription (in-progress)
public struct TentativeUserTranscriptEvent: Sendable {
public let transcript: String
public let eventId: Int
}
/// Agent's text response
public struct AgentResponseEvent: Sendable {
public let response: String
public let eventId: Int
public let responseId: String
}
/// Agent's response correction
public struct AgentResponseCorrectionEvent: Sendable {
public let originalAgentResponse: String
public let correctedAgentResponse: String
public let eventId: Int
public let responseId: String
}
/// Agent response metadata
public struct AgentResponseMetadataEvent: Sendable {
public let eventId: Int
public let metadataData: Data
}
public struct AgentChatResponsePartEvent: Sendable {
public let text: String
public let type: AgentChatResponsePartType
public let eventId: Int
public let responseId: String
}
/// Audio alignment data showing character-level timing information
public struct AudioAlignment: Sendable {
public let chars: [String]
public let charStartTimesMs: [Int]
public let charDurationsMs: [Int]
}
/// Audio data from the agent
public struct AudioEvent: Sendable {
public let audioBase64: String
public let eventId: Int
public let alignment: AudioAlignment?
}
/// Interruption detected
public struct InterruptionEvent: Sendable {
public let eventId: Int
}
/// Conversation initialization metadata
public struct ConversationMetadataEvent: Sendable {
public let conversationId: String
public let agentOutputAudioFormat: String
public let userInputAudioFormat: String
}
/// VAD score
public struct VadScoreEvent: Sendable {
public let vadScore: Double
}
/// Ping event for connection health
public struct PingEvent: Sendable {
public let eventId: Int
public let pingMs: Int?
}
/// Client tool call request
public struct ClientToolCallEvent: Sendable {
public let toolName: String
public let toolCallId: String
public let parametersData: Data // Store as JSON data to be Sendable
public let eventId: Int
public let expectsResponse: Bool
public init(
toolName: String,
toolCallId: String,
parametersData: Data,
eventId: Int,
expectsResponse: Bool
) {
self.toolName = toolName
self.toolCallId = toolCallId
self.parametersData = parametersData
self.eventId = eventId
self.expectsResponse = expectsResponse
}
/// Get parameters as dictionary (not Sendable, use carefully)
public func getParameters() throws -> [String: Any] {
try JSONSerialization.jsonObject(with: parametersData) as? [String: Any] ?? [:]
}
}
/// Agent tool request event (request initiated by the agent)
public struct AgentToolRequestEvent: Sendable {
public let toolName: String
public let toolCallId: String
public let toolType: String
public let eventId: Int
}
/// Agent tool response event
public struct AgentToolResponseEvent: Sendable {
public let toolName: String
public let toolCallId: String
public let toolType: String
public let isError: Bool
public let eventId: Int
}
/// MCP tool call event
public struct MCPToolCallEvent: Sendable {
public enum State: String, Sendable {
case loading
case awaitingApproval = "awaiting_approval"
case success
case failure
}
public let serviceId: String
public let toolCallId: String
public let toolName: String
public let toolDescription: String?
public let parametersData: Data
public let timestamp: String
public let state: State
public let approvalTimeoutSecs: Int?
public let resultData: Data?
public let errorMessage: String?
public func getParameters() throws -> [String: Any] {
try JSONSerialization.jsonObject(with: parametersData) as? [String: Any] ?? [:]
}
public func getResult() throws -> [[String: Any]]? {
guard let resultData else { return nil }
return try JSONSerialization.jsonObject(with: resultData) as? [[String: Any]]
}
}
public struct MCPConnectionStatusEvent: Sendable {
public struct Integration: Sendable {
public let integrationId: String
public let integrationType: String
public let isConnected: Bool
public let toolCount: Int
}
public let integrations: [Integration]
}
/// Server error event with code, optional name, and message.
public struct ErrorEvent: Sendable, Equatable {
public let code: Int
public let message: String?
public let errorName: String?
public init(code: Int, message: String? = nil, errorName: String? = nil) {
self.code = code
self.message = message
self.errorName = errorName
}
}