Skip to content

Commit 1e2281f

Browse files
feat(buzz-agent): send User-Agent on LLM requests (#8116)
## Summary `buzz-agent` builds its LLM `reqwest::Client` without a User-Agent, and reqwest sends none by default, so OpenAI-compatible providers that identify clients by product token see buzz-agent as an anonymous caller. This sets `User-Agent: buzz-agent/<crate version>` on the shared LLM client in `crates/buzz-agent/src/llm.rs`. The version is `CARGO_PKG_VERSION`, the same value `agentInfo` reports over ACP. No request body or endpoint changes. Open point: the crate version is the workspace version (`0.1.0`), so every build reports the same version. A per-build version would need the desktop to pass one through at spawn time; this PR does not do that. ### Related issue None found. ### Testing - The test stub now captures the `User-Agent` request header; new `llm_requests_carry_buzz_agent_user_agent` asserts `buzz-agent/<version>` on a Chat Completions POST. - Verified red without the `.user_agent(...)` line (`left: None`), green with it. - `cargo test -p buzz-agent --lib llm::tests`: 194 passed. `cargo fmt` clean; the one clippy warning is pre-existing (`hints.rs:33`). - Not exercised in a built desktop app. Signed-off-by: Tom Bedor <[email protected]> Co-authored-by: Tom Bedor <[email protected]>
1 parent af5bb0a commit 1e2281f

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

‎crates/buzz-agent/src/llm.rs‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ pub struct Llm {
5959
/// network/reachability problem, not a slow generation.
6060
const LLM_CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
6161

62+
/// `User-Agent` sent on every LLM request. reqwest sends no User-Agent by
63+
/// default, so OpenAI-compatible providers that identify clients by product
64+
/// token saw buzz-agent as an anonymous caller. The version is the crate
65+
/// version, matching the `agentInfo` reported over ACP.
66+
const LLM_USER_AGENT: &str = concat!("buzz-agent/", env!("CARGO_PKG_VERSION"));
67+
6268
impl Llm {
6369
pub fn new(cfg: &Config) -> Result<Self, AgentError> {
6470
let http = Client::builder()
71+
.user_agent(LLM_USER_AGENT)
6572
.connect_timeout(LLM_CONNECT_TIMEOUT)
6673
// No client-level read_timeout: we apply a per-request total
6774
// timeout via RequestBuilder::timeout() so that escalated budgets
@@ -2654,6 +2661,7 @@ mod tests {
26542661
struct CapturedHttpRequest {
26552662
method: String,
26562663
path: String,
2664+
user_agent: Option<String>,
26572665
body: Option<Value>,
26582666
}
26592667

@@ -2728,6 +2736,11 @@ mod tests {
27282736
Ok(read) => bytes.extend_from_slice(&chunk[..read]),
27292737
}
27302738
}
2739+
let user_agent = header_text.lines().find_map(|line| {
2740+
let (name, value) = line.split_once(':')?;
2741+
name.eq_ignore_ascii_case("user-agent")
2742+
.then(|| value.trim().to_string())
2743+
});
27312744
let mut request_line = header_text.lines().next().unwrap_or_default().split(' ');
27322745
let method = request_line.next().unwrap_or_default().to_string();
27332746
let path = request_line.next().unwrap_or_default().to_string();
@@ -2736,10 +2749,12 @@ mod tests {
27362749
} else {
27372750
serde_json::from_slice(&bytes[header_end..header_end + content_length]).ok()
27382751
};
2739-
captured_for_server
2740-
.lock()
2741-
.await
2742-
.push(CapturedHttpRequest { method, path, body });
2752+
captured_for_server.lock().await.push(CapturedHttpRequest {
2753+
method,
2754+
path,
2755+
user_agent,
2756+
body,
2757+
});
27432758

27442759
let response = responses.lock().await.pop_front().unwrap_or_else(|| {
27452760
StubHttpResponse::error(500, "stub response sequence exhausted")
@@ -2800,6 +2815,23 @@ mod tests {
28002815
.collect()
28012816
}
28022817

2818+
/// Every LLM request identifies the client to the provider via the
2819+
/// first User-Agent product token.
2820+
#[tokio::test]
2821+
async fn llm_requests_carry_buzz_agent_user_agent() {
2822+
let (base_url, captured) =
2823+
spawn_sequence_stub(vec![StubHttpResponse::ok(chat_response("ok"))]).await;
2824+
let mut config = cfg(Provider::OpenAi);
2825+
config.base_url = base_url;
2826+
let llm = Llm::new(&config).unwrap();
2827+
2828+
complete_model(&llm, &config, "gpt-test").await.unwrap();
2829+
let requests = captured.lock().await;
2830+
let expected = format!("buzz-agent/{}", env!("CARGO_PKG_VERSION"));
2831+
assert_eq!(requests.len(), 1);
2832+
assert_eq!(requests[0].user_agent.as_deref(), Some(expected.as_str()));
2833+
}
2834+
28032835
/// An explicit model is sent verbatim and never rewritten to something
28042836
/// else. A server error is retried under the *same* model (the ordinary
28052837
/// transport retry) and then surfaced -- there is no second model to fall

0 commit comments

Comments
 (0)