Skip to content

Commit b7a83c4

Browse files
committed
feat(agent): add REST CRUD for configurations, variables, think models
Phase 5 of the spec-coverage rollout — the Agent product's REST surface, pairing with the WebSocket landed in Phase 1. Eleven endpoints across three sub-clients reachable from `dg.agent()`: agent.think_models() (1 endpoint) agent.configurations() (5 endpoints) agent.variables() (5 endpoints) Highlights: - AgentConfigurations::create accepts a JSON-encoded `config` string per spec. CreateAgentConfigurationRequest::from_inline serializes a typed InlineAgentConfig (from Phase 1) for you, bridging the typed WS Settings types and the REST CRUD. - AgentConfigurations::update_metadata is documented as the only way to change a saved config; the config itself is immutable server-side per spec. - AgentVariables::update takes serde_json::Value so callers can pass any JSON type. The DG_<NAME> key convention is documented but not enforced — the server validates. - AgentThinkModels::list hits agent.deepgram.com (not the API host) to match the spec's server declaration. Internal: Agent<'a>'s inner field is now `pub` (matching Transcription / Speak), so sibling files in src/agent/ can borrow &Deepgram directly. The rest of the WS surface is unchanged. Two examples under examples/agent/rest/: - configurations.rs: full lifecycle from typed inline config to create -> list -> update metadata -> delete. - variables.rs: CRUD on a DG_ variable, plus think-models catalog. 11 new tests pass; 140 total. All feature combinations build clean.
1 parent 70baa52 commit b7a83c4

8 files changed

Lines changed: 846 additions & 4 deletions

File tree

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,13 @@ required-features = ["agent"]
185185
name = "agent_microphone"
186186
path = "examples/agent/websocket/microphone_agent.rs"
187187
required-features = ["agent"]
188+
189+
[[example]]
190+
name = "agent_configurations"
191+
path = "examples/agent/rest/configurations.rs"
192+
required-features = ["agent"]
193+
194+
[[example]]
195+
name = "agent_variables"
196+
path = "examples/agent/rest/variables.rs"
197+
required-features = ["agent"]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Expected result from running this example program.
2+
Created agent: <uuid> (config_keys: ["language", "listen", "speak", "think"])
3+
Listed 1 agents on the project.
4+
Updated metadata: {"env": "staging"}
5+
Deleted agent <uuid>.
6+
*/
7+
8+
//! Saved-configuration CRUD walkthrough for the Agent REST API.
9+
//!
10+
//! Creates a configuration from an [`InlineAgentConfig`], lists the
11+
//! project's agents, updates the new entry's metadata, then deletes
12+
//! it.
13+
//!
14+
//! Run with:
15+
//!
16+
//! ```bash
17+
//! DEEPGRAM_API_KEY=<your-key> \
18+
//! DEEPGRAM_PROJECT_ID=<project-uuid> \
19+
//! cargo run --features agent --example agent_configurations
20+
//! ```
21+
22+
use std::env;
23+
24+
use deepgram::agent::configurations::{
25+
CreateAgentConfigurationRequest, UpdateAgentMetadataRequest,
26+
};
27+
use deepgram::agent::settings::InlineAgentConfig;
28+
use deepgram::agent::{
29+
listen::{AgentListenProvider, AgentListenSettings, DeepgramListenV2Provider},
30+
speak::{DeepgramSpeakModel, DeepgramSpeakProvider, SpeakProvider, SpeakSettings},
31+
think::{OpenAiModel, OpenAiThinkProvider, ThinkProvider, ThinkSettings},
32+
};
33+
use deepgram::{Deepgram, DeepgramError};
34+
35+
#[tokio::main]
36+
async fn main() -> Result<(), DeepgramError> {
37+
let api_key = env::var("DEEPGRAM_API_KEY").expect("DEEPGRAM_API_KEY environment variable");
38+
let project_id =
39+
env::var("DEEPGRAM_PROJECT_ID").expect("DEEPGRAM_PROJECT_ID environment variable");
40+
41+
let dg = Deepgram::new(&api_key)?;
42+
let configs = dg.agent().configurations();
43+
44+
// Build a typed inline config and let the SDK serialize it to the
45+
// string the API expects.
46+
let inline = InlineAgentConfig::from_parts(
47+
AgentListenSettings::new(AgentListenProvider::DeepgramV2(
48+
DeepgramListenV2Provider::new("flux-general-en"),
49+
)),
50+
ThinkSettings::new(ThinkProvider::OpenAi(OpenAiThinkProvider::new(
51+
OpenAiModel::Gpt4oMini,
52+
))),
53+
SpeakSettings::new(SpeakProvider::Deepgram(DeepgramSpeakProvider::new(
54+
DeepgramSpeakModel::Aura2ThaliaEn,
55+
))),
56+
)
57+
.with_greeting("Hello! Configured by the Rust SDK example.");
58+
59+
let create_request = CreateAgentConfigurationRequest::from_inline(&inline)
60+
.map_err(deepgram::DeepgramError::JsonError)?
61+
.with_metadata([("env", "demo"), ("source", "rust-sdk-example")]);
62+
63+
let created = configs.create(&project_id, &create_request).await?;
64+
let agent_id = created.agent_id;
65+
let mut config_keys: Vec<String> = created
66+
.config
67+
.as_object()
68+
.map(|m| m.keys().cloned().collect())
69+
.unwrap_or_default();
70+
config_keys.sort();
71+
println!("Created agent: {agent_id} (config_keys: {:?})", config_keys);
72+
73+
let listed = configs.list(&project_id).await?;
74+
println!("Listed {} agents on the project.", listed.agents.len());
75+
76+
let updated = configs
77+
.update_metadata(
78+
&project_id,
79+
&agent_id,
80+
&UpdateAgentMetadataRequest::new([("env", "staging")]),
81+
)
82+
.await?;
83+
println!("Updated metadata: {:?}", updated.metadata);
84+
85+
configs.delete(&project_id, &agent_id).await?;
86+
println!("Deleted agent {agent_id}.");
87+
88+
Ok(())
89+
}

examples/agent/rest/variables.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* Expected result from running this example program.
2+
Created variable v1 (key=DG_AGENT_NAME)
3+
Listed 1 variables.
4+
Get -> "Alice"
5+
Update -> "Bob"
6+
Deleted variable v1.
7+
Think models available:
8+
open_ai gpt-4o-mini
9+
anthropic claude-3-5-haiku-latest
10+
...
11+
*/
12+
13+
//! Walkthrough for the Agent template-variables REST endpoints, plus a
14+
//! call to the think-models catalog at the end.
15+
//!
16+
//! Run with:
17+
//!
18+
//! ```bash
19+
//! DEEPGRAM_API_KEY=<your-key> \
20+
//! DEEPGRAM_PROJECT_ID=<project-uuid> \
21+
//! cargo run --features agent --example agent_variables
22+
//! ```
23+
24+
use std::env;
25+
26+
use serde_json::json;
27+
28+
use deepgram::agent::variables::CreateAgentVariableRequest;
29+
use deepgram::{Deepgram, DeepgramError};
30+
31+
#[tokio::main]
32+
async fn main() -> Result<(), DeepgramError> {
33+
let api_key = env::var("DEEPGRAM_API_KEY").expect("DEEPGRAM_API_KEY environment variable");
34+
let project_id =
35+
env::var("DEEPGRAM_PROJECT_ID").expect("DEEPGRAM_PROJECT_ID environment variable");
36+
37+
let dg = Deepgram::new(&api_key)?;
38+
let agent = dg.agent();
39+
let variables = agent.variables();
40+
41+
let created = variables
42+
.create(
43+
&project_id,
44+
&CreateAgentVariableRequest::new("DG_AGENT_NAME", json!("Alice")),
45+
)
46+
.await?;
47+
let variable_id = created.variable_id.clone();
48+
println!("Created variable {} (key={})", variable_id, created.key);
49+
50+
let listed = variables.list(&project_id).await?;
51+
println!("Listed {} variables.", listed.variables.len());
52+
53+
let fetched = variables.get(&project_id, &variable_id).await?;
54+
println!("Get -> {}", fetched.value);
55+
56+
let updated = variables
57+
.update(&project_id, &variable_id, json!("Bob"))
58+
.await?;
59+
println!("Update -> {}", updated.value);
60+
61+
variables.delete(&project_id, &variable_id).await?;
62+
println!("Deleted variable {}.", variable_id);
63+
64+
let think_models = agent.think_models().list().await?;
65+
println!("Think models available:");
66+
for model in think_models.models {
67+
println!(
68+
" {:<12} {}",
69+
format!("{:?}", model.provider).to_lowercase(),
70+
model.id
71+
);
72+
}
73+
74+
Ok(())
75+
}

0 commit comments

Comments
 (0)