Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
4b65e9e45This file is pinned to the selected release. Download the original to open it in your design tool.
class ChatAudioIO {
constructor(options) {
this.options = options
ChatAudioIO.lastOptions = options
ChatAudioIO.instances.push(this)
this.state = ChatAudioIO.DISCONNECTED
this.error = ''
this.lastText = null
this.lastFunctionResult = null
this.microphone = true
this.inputSuspended = false
this.suspendInputCalls = 0
this.resumeInputCalls = 0
this.volume = 1
}
connect() {
this.state = ChatAudioIO.CONNECTING
this.options.onStateChanged?.(this.state)
}
disconnect() {
this.state = ChatAudioIO.DISCONNECTING
this.options.onStateChanged?.(this.state)
this.state = ChatAudioIO.DISCONNECTED
this.options.onStateChanged?.(this.state)
}
close() {}
sendText(text) {
if (this.state < ChatAudioIO.CONNECTED) {
throw new Error('not connected')
}
this.lastText = text
}
sendFunctionResult(call, name, result) {
this.lastFunctionResult = { call, name, result }
}
changeMicrophone(enabled) {
this.microphone = enabled
}
suspendInput() {
this.suspendInputCalls += 1
this.inputSuspended = true
this.microphone = false
}
resumeInput(microphoneEnabled) {
this.resumeInputCalls += 1
this.inputSuspended = false
this.microphone = microphoneEnabled
}
changeVolume(volume) {
this.volume = volume
}
emitState(state, error) {
this.state = state
if (error) this.error = error
this.options.onStateChanged?.(state)
}
emitInputLevel(level) {
this.options.onInputLevelChanged?.(level)
}
emitOutputLevel(level) {
this.options.onOutputLevelChanged?.(level)
}
emitInputTranscript(text, more = false) {
this.options.onInputTranscript?.(text, more)
}
emitOutputTranscript(text, more = false) {
this.options.onOutputTranscript?.(text, more)
}
emitFunctionCall(call, name, parameters) {
this.options.onFunctionCall?.(call, name, parameters)
}
}
ChatAudioIO.FAILED = -1
ChatAudioIO.DISCONNECTED = 0
ChatAudioIO.DISCONNECTING = 1
ChatAudioIO.CONNECTING = 2
ChatAudioIO.CONNECTED = 3
ChatAudioIO.SPEAKING = 4
ChatAudioIO.LISTENING = 5
ChatAudioIO.WAITING = 6
ChatAudioIO.instances = []
ChatAudioIO.lastOptions = null
export default ChatAudioIO