deepgram 0.13.0

Community Rust SDK for Deepgram's automated speech recognition APIs.
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! Voice Agent Think (LLM) settings.
//!
//! Mirrors `asyncapi/schemas/agent/think-settings.v1.yml` and the five
//! provider sub-schemas under `asyncapi/schemas/agent/think-providers/`.

use core::fmt;
use std::collections::HashMap;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::agent::endpoint::{RedactedHeaders, RedactedUrl};
use crate::agent::Endpoint;

pub mod anthropic;
pub mod aws_bedrock;
pub mod google;
pub mod groq;
pub mod open_ai;

pub use anthropic::{AnthropicModel, AnthropicThinkProvider, AnthropicVersion};
pub use aws_bedrock::{AwsBedrockModel, AwsBedrockThinkProvider};
pub use google::{GoogleModel, GoogleThinkProvider, GoogleVersion};
pub use groq::{GroqModel, GroqThinkProvider, GroqVersion};
pub use open_ai::{OpenAiModel, OpenAiReasoningMode, OpenAiThinkProvider, OpenAiVersion};

/// Top-level Think configuration on `agent.think` in the Voice Agent
/// `Settings` message.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ThinkSettings {
    /// LLM provider configuration.
    pub provider: ThinkProvider,

    /// Custom LLM endpoint. Optional for non-Deepgram providers; ignored otherwise.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub endpoint: Option<Endpoint>,

    /// Function definitions that the agent may call during a conversation.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub functions: Vec<ThinkFunction>,

    /// System prompt.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prompt: Option<String>,

    /// Context retention setting. Only configurable when a custom think
    /// endpoint is in use.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub context_length: Option<ContextLength>,
}

impl ThinkSettings {
    /// Construct with just a provider and defaults for all other fields.
    pub fn new(provider: ThinkProvider) -> Self {
        Self {
            provider,
            endpoint: None,
            functions: Vec::new(),
            prompt: None,
            context_length: None,
        }
    }

    /// Set a custom LLM endpoint.
    pub fn with_endpoint(mut self, endpoint: Endpoint) -> Self {
        self.endpoint = Some(endpoint);
        self
    }

    /// Replace the function list.
    pub fn with_functions(mut self, functions: impl IntoIterator<Item = ThinkFunction>) -> Self {
        self.functions = functions.into_iter().collect();
        self
    }

    /// Append a single function definition.
    pub fn with_function(mut self, function: ThinkFunction) -> Self {
        self.functions.push(function);
        self
    }

    /// Set the system prompt.
    pub fn with_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    /// Set the context length policy.
    pub fn with_context_length(mut self, context_length: ContextLength) -> Self {
        self.context_length = Some(context_length);
        self
    }
}

/// LLM provider variants supported by the Voice Agent.
///
/// Wire format is internally tagged on `type` with snake_case discriminator
/// values: `open_ai`, `anthropic`, `aws_bedrock`, `google`, `groq`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum ThinkProvider {
    /// OpenAI chat completions.
    OpenAi(OpenAiThinkProvider),
    /// Anthropic Messages API.
    Anthropic(AnthropicThinkProvider),
    /// AWS Bedrock.
    AwsBedrock(AwsBedrockThinkProvider),
    /// Google generative language API (Gemini).
    Google(GoogleThinkProvider),
    /// Groq chat completions (OpenAI-compatible).
    Groq(GroqThinkProvider),
}

/// A function the agent may call during a conversation.
///
/// When `endpoint` is `None`, the function executes client-side: the
/// server emits a `FunctionCallRequest` and waits for a
/// `FunctionCallResponse` from the client.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ThinkFunction {
    /// Function name.
    pub name: String,

    /// Function description.
    pub description: String,

    /// JSON Schema describing the function's parameters. Stored as opaque JSON.
    pub parameters: serde_json::Value,

    /// HTTP endpoint to call for server-side function execution.
    /// When omitted, the function is executed client-side.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub endpoint: Option<FunctionEndpoint>,

    /// Hold this function call until speech-to-text confirms the end of
    /// the user's turn. Server default `false`.
    ///
    /// By default the agent may dispatch function calls during the
    /// speculative window between an eager end-of-turn signal and the
    /// confirmed one; if the user keeps talking, an already-sent
    /// client-side call is revoked with a `FunctionCallCancelled` event.
    /// Set this to `true` for irreversible actions (hanging up, charging a
    /// card) so the call is only sent once the turn is confirmed — a
    /// deferred call whose turn resumes is dropped silently.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub defer_until_eot: Option<bool>,
}

impl ThinkFunction {
    /// Construct a client-side function (no `endpoint`).
    ///
    /// `parameters` is the JSON Schema describing the function's args.
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        parameters: serde_json::Value,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            parameters,
            endpoint: None,
            defer_until_eot: None,
        }
    }

    /// Attach a server-side execution endpoint.
    pub fn with_endpoint(mut self, endpoint: FunctionEndpoint) -> Self {
        self.endpoint = Some(endpoint);
        self
    }

    /// Set `defer_until_eot` — hold the call until the user's turn is
    /// confirmed. See the field docs for when to use this.
    pub fn with_defer_until_eot(mut self, defer: bool) -> Self {
        self.defer_until_eot = Some(defer);
        self
    }
}

/// HTTP endpoint for server-side function execution.
///
/// The `Debug` output redacts header values (header names are kept) and
/// any `user:pass@` userinfo in the URL, so that logging a
/// `ThinkSettings` never leaks an `Authorization` header or a credential
/// embedded in the endpoint URL.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FunctionEndpoint {
    /// Endpoint URL.
    pub url: String,

    /// HTTP method.
    pub method: String,

    /// Custom headers.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub headers: HashMap<String, String>,
}

impl FunctionEndpoint {
    /// Construct with the given URL and method and no extra headers.
    pub fn new(url: impl Into<String>, method: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            method: method.into(),
            headers: HashMap::new(),
        }
    }

    /// Add a single header. Useful for fluent construction.
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.insert(name.into(), value.into());
        self
    }
}

impl fmt::Debug for FunctionEndpoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FunctionEndpoint")
            .field("url", &RedactedUrl(&self.url))
            .field("method", &self.method)
            .field("headers", &RedactedHeaders(&self.headers))
            .finish()
    }
}

/// Context retention setting for `agent.think.context_length`.
///
/// Wire format is `oneOf [string("max"), number]`:
/// `ContextLength::Max` serializes as the literal string `"max"`;
/// `ContextLength::Tokens(n)` serializes as the number `n`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ContextLength {
    /// Retain the full context regardless of length.
    Max,
    /// Retain at most `n` tokens of context. Spec minimum is 2.
    Tokens(u32),
}

impl Serialize for ContextLength {
    fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Max => ser.serialize_str("max"),
            Self::Tokens(n) => ser.serialize_u32(*n),
        }
    }
}

impl<'de> Deserialize<'de> for ContextLength {
    fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
        use serde::de::Error;
        let value = serde_json::Value::deserialize(de)?;
        if let Some(s) = value.as_str() {
            if s == "max" {
                Ok(Self::Max)
            } else {
                Err(D::Error::custom(format!(
                    "expected `\"max\"` or a non-negative integer for context_length, got string {s:?}"
                )))
            }
        } else if let Some(n) = value.as_u64() {
            u32::try_from(n)
                .map(Self::Tokens)
                .map_err(|_| D::Error::custom(format!("context_length {n} does not fit in u32")))
        } else {
            Err(D::Error::custom(
                "expected `\"max\"` or a non-negative integer for context_length",
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent::Endpoint;
    use serde_json::json;

    #[test]
    fn provider_round_trip_openai() {
        let raw = json!({
            "type": "open_ai",
            "model": "gpt-4o",
            "temperature": 0.5,
        });
        let provider: ThinkProvider = serde_json::from_value(raw.clone()).unwrap();
        match &provider {
            ThinkProvider::OpenAi(p) => assert_eq!(p.model, OpenAiModel::Gpt4o),
            _ => panic!("expected OpenAi"),
        }
        assert_eq!(serde_json::to_value(&provider).unwrap(), raw);
    }

    #[test]
    fn provider_round_trip_anthropic() {
        let raw = json!({
            "type": "anthropic",
            "model": "claude-3-5-haiku-latest",
        });
        let provider: ThinkProvider = serde_json::from_value(raw.clone()).unwrap();
        match &provider {
            ThinkProvider::Anthropic(p) => {
                assert_eq!(p.model, AnthropicModel::Claude35HaikuLatest);
            }
            _ => panic!("expected Anthropic"),
        }
        assert_eq!(serde_json::to_value(&provider).unwrap(), raw);
    }

    #[test]
    fn provider_round_trip_aws_bedrock() {
        let raw = json!({
            "type": "aws_bedrock",
            "model": "anthropic/claude-3-5-haiku-20240307-v1:0",
        });
        let provider: ThinkProvider = serde_json::from_value(raw.clone()).unwrap();
        match &provider {
            ThinkProvider::AwsBedrock(p) => {
                assert_eq!(p.model, AwsBedrockModel::AnthropicClaude35Haiku20240307V1);
            }
            _ => panic!("expected AwsBedrock"),
        }
        assert_eq!(serde_json::to_value(&provider).unwrap(), raw);
    }

    #[test]
    fn provider_round_trip_google() {
        let raw = json!({
            "type": "google",
            "model": "gemini-2.0-flash",
        });
        let provider: ThinkProvider = serde_json::from_value(raw.clone()).unwrap();
        match &provider {
            ThinkProvider::Google(p) => assert_eq!(p.model, GoogleModel::Gemini20Flash),
            _ => panic!("expected Google"),
        }
        assert_eq!(serde_json::to_value(&provider).unwrap(), raw);
    }

    #[test]
    fn provider_round_trip_groq() {
        let raw = json!({
            "type": "groq",
            "model": "openai/gpt-oss-20b",
        });
        let provider: ThinkProvider = serde_json::from_value(raw.clone()).unwrap();
        match &provider {
            ThinkProvider::Groq(p) => assert_eq!(p.model, GroqModel::OpenAiGptOss20b),
            _ => panic!("expected Groq"),
        }
        assert_eq!(serde_json::to_value(&provider).unwrap(), raw);
    }

    #[test]
    fn settings_round_trip_full() {
        let raw = json!({
            "provider": {
                "type": "open_ai",
                "model": "gpt-4o-mini",
            },
            "endpoint": {
                "url": "https://llm.internal/v1/chat",
                "headers": { "Authorization": "Bearer abc" }
            },
            "functions": [
                {
                    "name": "get_weather",
                    "description": "Look up current weather",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "city": { "type": "string" }
                        }
                    }
                }
            ],
            "prompt": "You are a helpful assistant.",
            "context_length": 4096,
        });
        let settings: ThinkSettings = serde_json::from_value(raw.clone()).unwrap();
        assert_eq!(settings.context_length, Some(ContextLength::Tokens(4096)));
        assert_eq!(settings.functions.len(), 1);
        assert_eq!(settings.functions[0].name, "get_weather");
        assert_eq!(serde_json::to_value(&settings).unwrap(), raw);
    }

    #[test]
    fn settings_round_trip_minimal() {
        let raw = json!({
            "provider": {
                "type": "anthropic",
                "model": "claude-3-5-haiku-latest"
            }
        });
        let settings: ThinkSettings = serde_json::from_value(raw.clone()).unwrap();
        assert!(settings.endpoint.is_none());
        assert!(settings.functions.is_empty());
        assert_eq!(serde_json::to_value(&settings).unwrap(), raw);
    }

    #[test]
    fn context_length_max_string() {
        let json = json!("max");
        let cl: ContextLength = serde_json::from_value(json.clone()).unwrap();
        assert_eq!(cl, ContextLength::Max);
        assert_eq!(serde_json::to_value(cl).unwrap(), json);
    }

    #[test]
    fn context_length_number() {
        let json = json!(8192);
        let cl: ContextLength = serde_json::from_value(json.clone()).unwrap();
        assert_eq!(cl, ContextLength::Tokens(8192));
        assert_eq!(serde_json::to_value(cl).unwrap(), json);
    }

    #[test]
    fn context_length_rejects_garbage_string() {
        let json = json!("infinite");
        let err = serde_json::from_value::<ContextLength>(json).unwrap_err();
        assert!(err.to_string().contains("max"), "got: {err}");
    }

    #[test]
    fn function_endpoint_round_trip() {
        let raw = json!({
            "url": "https://hooks.internal/fn",
            "method": "POST",
            "headers": { "X-Tenant": "acme" }
        });
        let fe: FunctionEndpoint = serde_json::from_value(raw.clone()).unwrap();
        assert_eq!(fe.method, "POST");
        assert_eq!(serde_json::to_value(&fe).unwrap(), raw);
    }

    #[test]
    fn function_defer_until_eot_serialization() {
        // An irreversible action deferred until the user's turn is confirmed.
        // (Single-key schema so the exact-string assertion is independent
        // of `serde_json::Value`'s sorted map ordering.)
        let end_call =
            ThinkFunction::new("end_call", "Hang up the call.", json!({ "type": "object" }))
                .with_defer_until_eot(true);
        assert_eq!(
            serde_json::to_string(&end_call).unwrap(),
            r#"{"name":"end_call","description":"Hang up the call.","parameters":{"type":"object"},"defer_until_eot":true}"#
        );

        // Unset → key omitted (server default is false).
        let plain = ThinkFunction::new("lookup", "Read-only lookup.", json!({}));
        assert!(serde_json::to_value(&plain)
            .unwrap()
            .get("defer_until_eot")
            .is_none());

        // Explicit false is preserved on the wire.
        let explicit = plain.clone().with_defer_until_eot(false);
        assert_eq!(
            serde_json::to_value(&explicit).unwrap()["defer_until_eot"],
            json!(false)
        );

        // Round trip through ThinkSettings.
        let raw = json!({
            "provider": { "type": "open_ai", "model": "gpt-4o-mini" },
            "functions": [{
                "name": "end_call",
                "description": "Hang up the call.",
                "parameters": { "type": "object" },
                "defer_until_eot": true
            }]
        });
        let settings: ThinkSettings = serde_json::from_value(raw.clone()).unwrap();
        assert_eq!(settings.functions[0].defer_until_eot, Some(true));
        assert_eq!(serde_json::to_value(&settings).unwrap(), raw);
    }

    #[test]
    fn function_endpoint_debug_redacts_header_values() {
        let fe = FunctionEndpoint::new("https://hooks.internal/fn", "POST")
            .with_header("Authorization", "Bearer super-secret-token")
            .with_header("X-Tenant", "acme");
        let debug = format!("{fe:?}");
        assert!(!debug.contains("super-secret-token"), "got: {debug}");
        assert!(!debug.contains("acme"), "got: {debug}");
        assert!(debug.contains("Authorization"), "got: {debug}");
        assert!(debug.contains("X-Tenant"), "got: {debug}");
        assert!(debug.contains("<redacted>"), "got: {debug}");
        assert!(debug.contains("https://hooks.internal/fn"), "got: {debug}");

        // Also redacted when nested in a ThinkSettings.
        let settings = ThinkSettings::new(ThinkProvider::OpenAi(OpenAiThinkProvider::new(
            OpenAiModel::Gpt4oMini,
        )))
        .with_function(ThinkFunction::new("fn", "d", json!({})).with_endpoint(fe))
        .with_endpoint(Endpoint::new("https://llm.internal").with_header("api-key", "llm-secret"));
        let debug = format!("{settings:?}");
        assert!(!debug.contains("super-secret-token"), "got: {debug}");
        assert!(!debug.contains("llm-secret"), "got: {debug}");
    }

    #[test]
    fn function_endpoint_debug_redacts_url_userinfo() {
        let fe = FunctionEndpoint::new("https://carol:t0ken@hooks.internal/fn", "POST");
        let debug = format!("{fe:?}");
        assert!(!debug.contains("t0ken"), "got: {debug}");
        assert!(!debug.contains("carol"), "got: {debug}");
        assert!(debug.contains("hooks.internal/fn"), "got: {debug}");

        // Also redacted when nested in a ThinkSettings, alongside a
        // Think `endpoint` carrying userinfo of its own.
        let settings = ThinkSettings::new(ThinkProvider::OpenAi(OpenAiThinkProvider::new(
            OpenAiModel::Gpt4oMini,
        )))
        .with_function(ThinkFunction::new("fn", "d", json!({})).with_endpoint(fe))
        .with_endpoint(Endpoint::new("https://dave:llm-pass@llm.internal"));
        let debug = format!("{settings:?}");
        assert!(!debug.contains("t0ken"), "got: {debug}");
        assert!(!debug.contains("llm-pass"), "got: {debug}");
        assert!(!debug.contains("dave"), "got: {debug}");
    }

    #[test]
    fn function_endpoint_debug_redacts_schemeless_url_userinfo() {
        // A free-form URL with no scheme must still lose its userinfo.
        let fe = FunctionEndpoint::new("carol:t0ken@hooks.internal/fn", "POST");
        let debug = format!("{fe:?}");
        assert!(!debug.contains("t0ken"), "got: {debug}");
        assert!(!debug.contains("carol"), "got: {debug}");
        assert!(
            debug.contains("<redacted>@hooks.internal/fn"),
            "got: {debug}"
        );
    }

    #[test]
    fn settings_with_context_length_max() {
        let settings = ThinkSettings {
            provider: ThinkProvider::OpenAi(OpenAiThinkProvider::new(OpenAiModel::Gpt5)),
            endpoint: Some(Endpoint::new("https://example.com")),
            functions: Vec::new(),
            prompt: Some("hi".into()),
            context_length: Some(ContextLength::Max),
        };
        let serialized = serde_json::to_value(&settings).unwrap();
        assert_eq!(serialized["context_length"], json!("max"));
    }
}