Skip to content

Commit 0a3983d

Browse files
authored
Merge pull request #61 from pragmatrix/elevenlabs-billing
Add billing records in elevenlabs transcribe
2 parents 70bee67 + 3beac5b commit 0a3983d

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

services/elevenlabs/src/transcribe.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{Context, Result, anyhow, bail};
1+
use anyhow::{Context, Result, bail};
22
use async_trait::async_trait;
33
use base64::Engine;
44
use futures::{SinkExt, StreamExt};
@@ -19,8 +19,8 @@ use tracing::{debug, error, warn};
1919
use url::Url;
2020

2121
use context_switch_core::{
22-
AudioFormat, Service,
23-
conversation::{Conversation, ConversationInput, ConversationOutput, Input},
22+
AudioFormat, AudioFrame, BillingRecord, Service,
23+
conversation::{BillingSchedule, Conversation, ConversationInput, ConversationOutput, Input},
2424
language::{bcp47_to_iso639_3, iso639_to_bcp47},
2525
};
2626

@@ -199,10 +199,12 @@ where
199199
}
200200

201201
let previous_text = previous_text_for_next_chunk.take();
202-
let msg = build_audio_chunk_message(frame, false, previous_text)?;
203-
outbound_tx
204-
.send(msg)
205-
.map_err(|_| anyhow!("ElevenLabs websocket writer task stopped unexpectedly"))?;
202+
enqueue_audio_chunk_and_emit_billing(
203+
outbound_tx,
204+
output,
205+
frame,
206+
previous_text,
207+
)?;
206208
}
207209
Some(_) => {}
208210
None => {
@@ -229,6 +231,28 @@ where
229231
}
230232
}
231233

234+
fn enqueue_audio_chunk_and_emit_billing(
235+
outbound_tx: &mpsc::UnboundedSender<OutboundMessage>,
236+
output: &ConversationOutput,
237+
frame: AudioFrame,
238+
previous_text: Option<&str>,
239+
) -> Result<()> {
240+
let duration = frame.duration();
241+
let msg = build_audio_chunk_message(frame, false, previous_text)?;
242+
outbound_tx
243+
.send(msg)
244+
.context("ElevenLabs websocket writer task stopped unexpectedly")?;
245+
246+
output
247+
.billing_records(
248+
None,
249+
None,
250+
[BillingRecord::duration("input:audio", duration)],
251+
BillingSchedule::Now,
252+
)
253+
.context("Failed to output billing records")
254+
}
255+
232256
async fn shutdown_writer_task(mut writer_task: tokio::task::JoinHandle<Result<()>>) -> Result<()> {
233257
select! {
234258
join_result = &mut writer_task => {
@@ -292,9 +316,8 @@ fn build_endpoint(
292316
q.append_pair("commit_strategy", "vad");
293317

294318
if let Some(language) = params.language.as_deref() {
295-
let language_code = bcp47_to_iso639_3(language).map_err(|error| {
296-
anyhow!("Invalid ElevenLabs params.language '{language}': {error}")
297-
})?;
319+
let language_code = bcp47_to_iso639_3(language)
320+
.with_context(|| format!("Invalid ElevenLabs params.language '{language}'"))?;
298321
q.append_pair("language_code", language_code);
299322
}
300323
if let Some(vad_silence_threshold_secs) = params.vad_silence_threshold_secs {

0 commit comments

Comments
 (0)