Skip to content

Commit eacbbe8

Browse files
wpfleger96npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw
andauthored
feat(desktop): provider-agnostic model selection + databricks discovery (#1307)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@sprout-oss.stage.blox.sqprod.co>
1 parent 2561cbd commit eacbbe8

19 files changed

Lines changed: 1046 additions & 361 deletions

File tree

‎crates/buzz-agent/README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,17 @@ Everything is environment variables. No flags, no config files. (We are a subpro
129129

130130
| Variable | Default | Notes |
131131
|---|---|---|
132-
| `BUZZ_AGENT_PROVIDER` | — | `anthropic`, `openai`, `databricks`, or `databricks_v2`. If unset, or if `anthropic`/`openai` is selected but its API key is missing, Databricks is auto-selected when `DATABRICKS_HOST` + `DATABRICKS_MODEL` are set. |
133-
| `ANTHROPIC_API_KEY` | — | Required when provider=anthropic unless Databricks fallback is configured. |
132+
| `BUZZ_AGENT_PROVIDER` | — | Required. `anthropic`, `openai`, `databricks`, or `databricks_v2`. No implicit fallback — the agent errors at startup when this is unset. |
133+
| `ANTHROPIC_API_KEY` | — | Required when provider=anthropic. |
134134
| `ANTHROPIC_MODEL` | — | Required when provider=anthropic. |
135135
| `ANTHROPIC_BASE_URL` | `https://api.anthropic.com` | |
136136
| `ANTHROPIC_API_VERSION` | `2023-06-01` | |
137-
| `OPENAI_COMPAT_API_KEY` | — | Required when provider=openai unless Databricks fallback is configured. |
137+
| `OPENAI_COMPAT_API_KEY` | — | Required when provider=openai. |
138138
| `OPENAI_COMPAT_MODEL` | — | Required when provider=openai. |
139139
| `OPENAI_COMPAT_BASE_URL` | `https://api.openai.com/v1` | Point at vLLM, llama.cpp, OpenRouter, Ollama, etc. |
140140
| `OPENAI_COMPAT_API` | `auto` | `auto` \| `chat` \| `responses`. `auto` picks Responses for `*.openai.com`, Chat Completions everywhere else. |
141-
| `DATABRICKS_HOST` | — | Required when provider=databricks or when using Databricks fallback. |
142-
| `DATABRICKS_MODEL` | — | Required when provider=databricks or when using Databricks fallback. |
141+
| `DATABRICKS_HOST` | — | Required when provider=databricks or provider=databricks_v2. |
142+
| `DATABRICKS_MODEL` | — | Required when provider=databricks or provider=databricks_v2. |
143143
| `DATABRICKS_TOKEN` | — | Optional static bearer escape hatch. If unset, Databricks uses browser OAuth + refresh cache. |
144144
| `BUZZ_AGENT_SYSTEM_PROMPT` | built-in | Inline system prompt. |
145145
| `BUZZ_AGENT_SYSTEM_PROMPT_FILE` | — | File path. Mutually exclusive with the above. |
@@ -172,7 +172,7 @@ Everything is environment variables. No flags, no config files. (We are a subpro
172172
| Databricks | `databricks` | `POST {host}/serving-endpoints/{model}/invocations` | goose-claude-4-6-sonnet |
173173
| Databricks AI Gateway v2 | `databricks_v2` | `POST {host}/ai-gateway/{provider}/v1/...` | databricks-gpt-5-5, databricks-claude-opus-4-7 |
174174

175-
If `BUZZ_AGENT_PROVIDER=anthropic` is selected without `ANTHROPIC_API_KEY`, or `BUZZ_AGENT_PROVIDER=openai` is selected without `OPENAI_COMPAT_API_KEY`, the agent automatically falls back to Databricks OAuth when `DATABRICKS_HOST` and `DATABRICKS_MODEL` are set. The same Databricks fallback applies when `BUZZ_AGENT_PROVIDER` is unset. Explicit Anthropic/OpenAI API keys always win.
175+
If `BUZZ_AGENT_PROVIDER=anthropic` is selected without `ANTHROPIC_API_KEY`, or `BUZZ_AGENT_PROVIDER=openai` is selected without `OPENAI_COMPAT_API_KEY`, the agent returns an error — there is no implicit fallback to another provider.
176176

177177
`provider=openai` speaks two HTTP dialects: the [Responses API](https://platform.openai.com/docs/api-reference/responses) (`/v1/responses`, required for GPT-5 / o-series tool-calling on OpenAI's own service) and the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat) (`/chat/completions`, the broadly-supported OpenAI-compatible wire format).
178178

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

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ const BROWSER_AUTH_TIMEOUT: Duration = Duration::from_secs(60);
4444
pub trait TokenSource: Send + Sync {
4545
async fn bearer(&self) -> Result<String, AgentError>;
4646

47+
/// Return a bearer token from cache or refresh, **never** opening a browser.
48+
///
49+
/// The default delegates to [`bearer`](Self::bearer) — correct for token
50+
/// sources (e.g. static API keys) that can never trigger a browser flow.
51+
/// [`PkceOAuthTokenSource`] overrides this to stop before the browser step.
52+
async fn bearer_no_browser(&self) -> Result<String, AgentError> {
53+
self.bearer().await
54+
}
55+
4756
/// Force a fresh bearer after the server rejected the current one (401).
4857
///
4958
/// `rejected` is the exact access token that just got the 401. Unlike
@@ -287,6 +296,10 @@ impl TokenSource for PkceOAuthTokenSource {
287296
Ok(bearer)
288297
}
289298

299+
async fn bearer_no_browser(&self) -> Result<String, AgentError> {
300+
self.try_bearer_no_browser().await
301+
}
302+
290303
/// Force-refresh after a 401, never touching the browser flow.
291304
///
292305
/// `rejected` is the access token the server just 401'd. Coalescing keys
@@ -344,6 +357,68 @@ impl TokenSource for PkceOAuthTokenSource {
344357
}
345358
}
346359

360+
impl PkceOAuthTokenSource {
361+
/// Return a bearer token from cache or refresh, **never** opening a browser.
362+
///
363+
/// Follows the same steps as [`bearer`](TokenSource::bearer) but stops at
364+
/// step 4 — if no usable token is available after cache + refresh attempts,
365+
/// returns `Err(LlmAuth(...))` instead of launching the browser PKCE flow.
366+
/// Used by model-discovery paths that must not block on user interaction.
367+
pub(crate) async fn try_bearer_no_browser(&self) -> Result<String, AgentError> {
368+
let mut state = self.state.lock().await;
369+
370+
// 1. In-memory cache hit, still fresh.
371+
if let Some(tok) = state.as_ref() {
372+
if !is_expired(tok) {
373+
return Ok(tok.access_token.clone());
374+
}
375+
}
376+
377+
// 2. Re-read disk — another process may have refreshed already.
378+
if let Some(disk_tok) = read_cache(&self.cache_path) {
379+
if !is_expired(&disk_tok) {
380+
let bearer = disk_tok.access_token.clone();
381+
*state = Some(disk_tok);
382+
return Ok(bearer);
383+
}
384+
}
385+
386+
// 3. Try refresh if we have a refresh token. Endpoints are discovered
387+
// lazily here — only when a refresh token is actually present — so
388+
// that an unreachable OIDC discovery URL cannot prevent the
389+
// no-token/no-cache path from returning `LlmAuth` (graceful
390+
// fallback) instead of `Llm` (hard error).
391+
let refresh = state.as_ref().and_then(|t| t.refresh_token.clone());
392+
if let Some(rt) = refresh {
393+
let endpoints = self.endpoints().await?;
394+
match self.refresh(&endpoints, &rt).await {
395+
Ok(fresh) => {
396+
let bearer = fresh.access_token.clone();
397+
self.save(&mut state, fresh)?;
398+
return Ok(bearer);
399+
}
400+
Err(e) => {
401+
tracing::warn!(error = %e, "oauth refresh failed during model discovery");
402+
}
403+
}
404+
405+
// 4. Re-read disk after refresh failure.
406+
if let Some(disk_tok) = read_cache(&self.cache_path) {
407+
if !is_expired(&disk_tok) {
408+
let bearer = disk_tok.access_token.clone();
409+
*state = Some(disk_tok);
410+
return Ok(bearer);
411+
}
412+
}
413+
}
414+
415+
// No usable token — return error instead of opening a browser.
416+
Err(AgentError::LlmAuth(
417+
"no cached Databricks token; run `buzz-agent auth databricks` first".into(),
418+
))
419+
}
420+
}
421+
347422
// ---- helpers -------------------------------------------------------------
348423

349424
/// Aborts a spawned task when dropped. Used to guarantee the localhost
@@ -726,4 +801,45 @@ mod tests {
726801
"expected discovery error, got: {err_msg}"
727802
);
728803
}
804+
805+
/// `try_bearer_no_browser` with an empty cache and no refresh token must
806+
/// return `LlmAuth` immediately — it must NOT attempt OIDC discovery even
807+
/// when the `discovery_url` is unreachable/invalid. This guards the
808+
/// regression where `endpoints()` was called unconditionally before the
809+
/// refresh-token check, causing an `Llm` error (hard failure) instead of
810+
/// the intended graceful `LlmAuth` fallback.
811+
#[tokio::test]
812+
async fn test_try_bearer_no_browser_empty_cache_no_refresh_returns_llm_auth_without_discovery()
813+
{
814+
let dir = tempfile::tempdir().unwrap();
815+
// Intentionally invalid/unreachable discovery URL — if endpoints() is
816+
// called, the test will get an `Llm` error and the assertion below fails.
817+
let cfg = PkceOAuthConfig {
818+
discovery_url: "https://invalid.example.test/.well-known/oauth-authorization-server"
819+
.into(),
820+
client_id: "test-client".into(),
821+
scopes: vec!["offline_access".into()],
822+
cache_namespace: "test".into(),
823+
cache_dir_override: Some(dir.path().to_path_buf()),
824+
};
825+
let source = PkceOAuthTokenSource::new(cfg).unwrap();
826+
827+
// Empty in-memory state (no token, no refresh token).
828+
{
829+
let mut state = source.state.lock().await;
830+
*state = None;
831+
}
832+
833+
// No disk cache file either — dir is empty.
834+
835+
let result = source.try_bearer_no_browser().await;
836+
assert!(result.is_err(), "expected Err, got Ok");
837+
match result.unwrap_err() {
838+
AgentError::LlmAuth(_) => {} // correct: graceful fallback
839+
other => panic!(
840+
"expected LlmAuth (no discovery attempted), got: {other:?}\n\
841+
This means endpoints() was called before the refresh-token check."
842+
),
843+
}
844+
}
729845
}

0 commit comments

Comments
 (0)