Add Azure AI Foundry multi-LLM provider (#10622)
Co-authored-by: Jack Amadeo <jackamadeo@squareup.com>
This commit is contained in:
@@ -65,10 +65,10 @@ pub fn recommended_models_from_registry(provider: &str) -> Vec<String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Providers that run models locally — their cost is always zero regardless
|
||||
/// of what the canonical registry says for the underlying model architecture.
|
||||
fn is_local_provider(provider: &str) -> bool {
|
||||
matches!(provider, "ollama" | "local")
|
||||
/// Catalog pricing is not valid for local inference or Azure Foundry deployments.
|
||||
/// Azure billing depends on deployment region, SKU, offer, and contract.
|
||||
fn should_clear_catalog_pricing(provider: &str) -> bool {
|
||||
matches!(provider, "ollama" | "local" | "azure_foundry")
|
||||
}
|
||||
|
||||
pub fn maybe_get_canonical_model(provider: &str, model: &str) -> Option<CanonicalModel> {
|
||||
@@ -81,9 +81,7 @@ pub fn maybe_get_canonical_model(provider: &str, model: &str) -> Option<Canonica
|
||||
return None;
|
||||
};
|
||||
|
||||
// Local providers run models on the user's own hardware — zero out cloud
|
||||
// pricing so every consumer (CLI, server, etc.) sees the correct cost.
|
||||
if is_local_provider(provider) {
|
||||
if should_clear_catalog_pricing(provider) {
|
||||
canonical.cost = Pricing::default();
|
||||
}
|
||||
|
||||
@@ -108,6 +106,15 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn azure_foundry_models_retain_limits_without_catalog_pricing() {
|
||||
let canonical = maybe_get_canonical_model("azure_foundry", "gpt-5")
|
||||
.expect("gpt-5 should resolve through the Azure catalog");
|
||||
assert_eq!(canonical.limit.context, 400_000);
|
||||
assert_eq!(canonical.cost.input, None);
|
||||
assert_eq!(canonical.cost.output, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cloud_provider_retains_cost() {
|
||||
let canonical = maybe_get_canonical_model("anthropic", "claude-sonnet-4-5-20250929")
|
||||
|
||||
@@ -37,7 +37,7 @@ pub fn canonical_name(provider: &str, model: &str) -> String {
|
||||
fn is_meta_provider(provider: &str) -> bool {
|
||||
matches!(
|
||||
provider,
|
||||
"databricks" | "databricks_v2" | "tetrate" | "bedrock" | "azure"
|
||||
"databricks" | "databricks_v2" | "tetrate" | "bedrock" | "azure" | "azure_foundry"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ pub fn map_provider_name(provider: &str) -> &str {
|
||||
match provider {
|
||||
// Goose provider names that differ from models.dev names
|
||||
"xai" => "x-ai",
|
||||
"azure_openai" => "azure",
|
||||
"azure_openai" | "azure_foundry" => "azure",
|
||||
"aws_bedrock" => "amazon-bedrock",
|
||||
"gcp_vertex_ai" => "google-vertex",
|
||||
"gemini_oauth" => "google",
|
||||
@@ -420,6 +420,10 @@ mod tests {
|
||||
map_to_canonical_model("azure", "gpt-4o", r),
|
||||
Some("openai/gpt-4o".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
map_to_canonical_model("azure_foundry", "gpt-4o", r),
|
||||
Some("openai/gpt-4o".to_string())
|
||||
);
|
||||
|
||||
// === OpenAI O-series ===
|
||||
assert_eq!(
|
||||
|
||||
@@ -776,6 +776,26 @@ pub fn create_request(
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
options: AnthropicFormatOptions,
|
||||
) -> Result<Value> {
|
||||
create_request_for_model(
|
||||
provider_name,
|
||||
model_config,
|
||||
&model_config.model_name,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
options,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn create_request_for_model(
|
||||
provider_name: &str,
|
||||
model_config: &ModelConfig,
|
||||
wire_model_name: &str,
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
options: AnthropicFormatOptions,
|
||||
) -> Result<Value> {
|
||||
let options = options.for_model(model_config);
|
||||
let anthropic_messages = format_messages_with_options(messages, options);
|
||||
@@ -788,7 +808,7 @@ pub fn create_request(
|
||||
|
||||
let max_tokens = model_config.max_output_tokens();
|
||||
let mut payload = json!({
|
||||
"model": model_config.model_name,
|
||||
"model": wire_model_name,
|
||||
"messages": anthropic_messages,
|
||||
"max_tokens": max_tokens,
|
||||
});
|
||||
|
||||
@@ -1401,6 +1401,32 @@ pub fn create_request_with_options(
|
||||
image_format: &ImageFormat,
|
||||
for_streaming: bool,
|
||||
format_options: OpenAiFormatOptions,
|
||||
) -> anyhow::Result<Value, Error> {
|
||||
let (wire_model_name, _) = extract_reasoning_effort(&model_config.model_name);
|
||||
create_request_for_model_with_options(
|
||||
model_config,
|
||||
&wire_model_name,
|
||||
&model_config.model_name,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
image_format,
|
||||
for_streaming,
|
||||
format_options,
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn create_request_for_model_with_options(
|
||||
model_config: &ModelConfig,
|
||||
wire_model_name: &str,
|
||||
capability_model_name: &str,
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
image_format: &ImageFormat,
|
||||
for_streaming: bool,
|
||||
format_options: OpenAiFormatOptions,
|
||||
) -> anyhow::Result<Value, Error> {
|
||||
if model_config.model_name.starts_with("o1-mini") {
|
||||
return Err(anyhow!(
|
||||
@@ -1408,7 +1434,7 @@ pub fn create_request_with_options(
|
||||
));
|
||||
}
|
||||
|
||||
let (model_name, legacy_reasoning_effort) = extract_reasoning_effort(&model_config.model_name);
|
||||
let (model_name, legacy_reasoning_effort) = extract_reasoning_effort(capability_model_name);
|
||||
let is_reasoning_model = is_openai_responses_model(&model_name);
|
||||
let supports_xai_effort = supports_xai_reasoning_effort(&model_name);
|
||||
let reasoning_effort = if is_reasoning_model {
|
||||
@@ -1439,7 +1465,7 @@ pub fn create_request_with_options(
|
||||
messages_array.extend(messages_spec);
|
||||
|
||||
let mut payload = json!({
|
||||
"model": model_name,
|
||||
"model": wire_model_name,
|
||||
"messages": messages_array
|
||||
});
|
||||
|
||||
|
||||
@@ -388,6 +388,7 @@ fn add_message_items(input_items: &mut Vec<Value>, messages: &[Message]) {
|
||||
MessageContentBlock::ToolRequest(request) if message.role == Role::Assistant => {
|
||||
if !text_items.is_empty() {
|
||||
input_items.push(json!({
|
||||
"type": "message",
|
||||
"role": role,
|
||||
"content": text_items
|
||||
}));
|
||||
@@ -435,6 +436,7 @@ fn add_message_items(input_items: &mut Vec<Value>, messages: &[Message]) {
|
||||
MessageContentBlock::ToolResponse(response) => {
|
||||
if !text_items.is_empty() {
|
||||
input_items.push(json!({
|
||||
"type": "message",
|
||||
"role": role,
|
||||
"content": text_items
|
||||
}));
|
||||
@@ -520,6 +522,7 @@ fn add_message_items(input_items: &mut Vec<Value>, messages: &[Message]) {
|
||||
MessageContentBlock::FrontendToolRequest(request) => {
|
||||
if !text_items.is_empty() {
|
||||
input_items.push(json!({
|
||||
"type": "message",
|
||||
"role": role,
|
||||
"content": text_items
|
||||
}));
|
||||
@@ -559,6 +562,7 @@ fn add_message_items(input_items: &mut Vec<Value>, messages: &[Message]) {
|
||||
|
||||
if !text_items.is_empty() {
|
||||
input_items.push(json!({
|
||||
"type": "message",
|
||||
"role": role,
|
||||
"content": text_items
|
||||
}));
|
||||
@@ -579,11 +583,31 @@ pub fn create_responses_request(
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
) -> anyhow::Result<Value, Error> {
|
||||
let (wire_model_name, _) = extract_reasoning_effort(&model_config.model_name);
|
||||
create_responses_request_for_model(
|
||||
model_config,
|
||||
&wire_model_name,
|
||||
&model_config.model_name,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn create_responses_request_for_model(
|
||||
model_config: &ModelConfig,
|
||||
wire_model_name: &str,
|
||||
capability_model_name: &str,
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
) -> anyhow::Result<Value, Error> {
|
||||
let mut input_items = Vec::new();
|
||||
|
||||
if !system.is_empty() {
|
||||
input_items.push(json!({
|
||||
"type": "message",
|
||||
"role": "system",
|
||||
"content": [{
|
||||
"type": "input_text",
|
||||
@@ -594,7 +618,7 @@ pub fn create_responses_request(
|
||||
|
||||
add_message_items(&mut input_items, messages);
|
||||
|
||||
let (model_name, legacy_reasoning_effort) = extract_reasoning_effort(&model_config.model_name);
|
||||
let (model_name, legacy_reasoning_effort) = extract_reasoning_effort(capability_model_name);
|
||||
// All models routed here are responses-capable; temperature is rejected
|
||||
// by the API for reasoning models regardless of whether an explicit
|
||||
// effort suffix was provided.
|
||||
@@ -639,7 +663,7 @@ pub fn create_responses_request(
|
||||
));
|
||||
}
|
||||
let mut payload = json!({
|
||||
"model": model_name,
|
||||
"model": wire_model_name,
|
||||
"input": input_items,
|
||||
"store": store,
|
||||
});
|
||||
@@ -1358,16 +1382,12 @@ mod tests {
|
||||
|
||||
let types: Vec<&str> = input
|
||||
.iter()
|
||||
.map(|item| {
|
||||
item.get("type")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or_else(|| item["role"].as_str().unwrap())
|
||||
})
|
||||
.map(|item| item["type"].as_str().unwrap())
|
||||
.collect();
|
||||
|
||||
assert_eq!(
|
||||
types,
|
||||
vec!["assistant", "function_call", "assistant", "function_call"]
|
||||
vec!["message", "function_call", "message", "function_call"]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ use tokio_util::io::StreamReader;
|
||||
use super::api_client::ApiClient;
|
||||
use super::base::{ConfigKey, MessageStream, ModelInfo, Provider, ProviderMetadata};
|
||||
use super::formats::anthropic::{
|
||||
create_request, response_to_streaming_message, AnthropicFormatOptions, ANTHROPIC_PROVIDER_NAME,
|
||||
create_request_for_model, response_to_streaming_message, AnthropicFormatOptions,
|
||||
ANTHROPIC_PROVIDER_NAME,
|
||||
};
|
||||
use super::openai_compatible::handle_status;
|
||||
use super::openai_compatible::map_http_error_to_provider_error;
|
||||
@@ -155,6 +156,54 @@ impl AnthropicProviderBuilder {
|
||||
}
|
||||
|
||||
impl AnthropicProvider {
|
||||
pub async fn stream_for_model(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
wire_model: &str,
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
let mut payload = create_request_for_model(
|
||||
ANTHROPIC_PROVIDER_NAME,
|
||||
model_config,
|
||||
wire_model,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
self.format_options,
|
||||
)?;
|
||||
payload["stream"] = Value::Bool(true);
|
||||
let mut log = start_log(model_config, &payload)?;
|
||||
let response = self
|
||||
.with_retry(|| async {
|
||||
handle_status(
|
||||
self.api_client
|
||||
.request("v1/messages")
|
||||
.model_headers(model_config)?
|
||||
.response_post(&payload)
|
||||
.await?,
|
||||
)
|
||||
.await
|
||||
})
|
||||
.await
|
||||
.inspect_err(|e| {
|
||||
let _ = log.error(e);
|
||||
})?;
|
||||
let stream = response.bytes_stream().map_err(io::Error::other);
|
||||
Ok(Box::pin(try_stream! {
|
||||
let reader = StreamReader::new(stream);
|
||||
let framed = tokio_util::codec::FramedRead::new(reader, tokio_util::codec::LinesCodec::new()).map_err(anyhow::Error::from);
|
||||
let messages = response_to_streaming_message(framed);
|
||||
pin!(messages);
|
||||
while let Some(message) = futures::StreamExt::next(&mut messages).await {
|
||||
let (message, usage) = message.map_err(ProviderError::from_stream_error)?;
|
||||
log.write(&message, usage.as_ref().map(|f| f.usage).as_ref())?;
|
||||
yield (message, usage);
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
async fn fetch_models_from_api(&self) -> Result<Vec<String>, ProviderError> {
|
||||
let response = self.api_client.request("v1/models").api_get().await?;
|
||||
|
||||
@@ -233,6 +282,13 @@ impl Provider for AnthropicProvider {
|
||||
&self.name
|
||||
}
|
||||
|
||||
async fn refresh_credentials(&self) -> Result<(), ProviderError> {
|
||||
self.api_client
|
||||
.refresh_credentials()
|
||||
.await
|
||||
.map_err(|error| ProviderError::Authentication(error.to_string()))
|
||||
}
|
||||
|
||||
fn skip_canonical_filtering(&self) -> bool {
|
||||
self.skip_canonical_filtering
|
||||
}
|
||||
@@ -266,49 +322,14 @@ impl Provider for AnthropicProvider {
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
let mut payload = create_request(
|
||||
ANTHROPIC_PROVIDER_NAME,
|
||||
self.stream_for_model(
|
||||
model_config,
|
||||
&model_config.model_name,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
self.format_options,
|
||||
)?;
|
||||
payload
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.insert("stream".to_string(), Value::Bool(true));
|
||||
|
||||
let mut log = start_log(model_config, &payload)?;
|
||||
|
||||
let response = self
|
||||
.with_retry(|| async {
|
||||
let request = self
|
||||
.api_client
|
||||
.request("v1/messages")
|
||||
.model_headers(model_config)?;
|
||||
let resp = request.response_post(&payload).await?;
|
||||
handle_status(resp).await
|
||||
})
|
||||
.await
|
||||
.inspect_err(|e| {
|
||||
let _ = log.error(e);
|
||||
})?;
|
||||
|
||||
let stream = response.bytes_stream().map_err(io::Error::other);
|
||||
|
||||
Ok(Box::pin(try_stream! {
|
||||
let stream_reader = StreamReader::new(stream);
|
||||
let framed = tokio_util::codec::FramedRead::new(stream_reader, tokio_util::codec::LinesCodec::new()).map_err(anyhow::Error::from);
|
||||
|
||||
let message_stream = response_to_streaming_message(framed);
|
||||
pin!(message_stream);
|
||||
while let Some(message) = futures::StreamExt::next(&mut message_stream).await {
|
||||
let (message, usage) = message.map_err(ProviderError::from_stream_error)?;
|
||||
log.write(&message, usage.as_ref().map(|f| f.usage).as_ref())?;
|
||||
yield (message, usage);
|
||||
}
|
||||
}))
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -195,6 +195,10 @@ fn convert_key_to_pkcs8_pem(key_pem_str: &str) -> Result<String> {
|
||||
#[async_trait]
|
||||
pub trait AuthProvider: Send + Sync {
|
||||
async fn get_auth_header(&self) -> Result<(String, String)>;
|
||||
|
||||
async fn refresh_credentials(&self) -> Result<()> {
|
||||
anyhow::bail!("credential refresh not supported")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ApiResponse {
|
||||
@@ -356,6 +360,13 @@ impl ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn refresh_credentials(&self) -> Result<()> {
|
||||
match &self.auth {
|
||||
AuthMethod::Custom(provider) => provider.refresh_credentials().await,
|
||||
_ => anyhow::bail!("credential refresh not supported"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn api_post(&self, path: &str, payload: &Value) -> Result<ApiResponse> {
|
||||
self.request(path).api_post(payload).await
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,6 @@
|
||||
pub mod anthropic;
|
||||
pub mod api_client;
|
||||
pub mod azure_foundry;
|
||||
pub mod databricks;
|
||||
pub mod databricks_auth;
|
||||
pub mod databricks_v2;
|
||||
|
||||
@@ -11,7 +11,8 @@ use crate::formats::openai::{
|
||||
create_request_with_options, get_cost, get_usage, response_to_message, OpenAiFormatOptions,
|
||||
};
|
||||
use crate::formats::openai_responses::{
|
||||
create_responses_request, get_responses_usage, responses_api_to_message, ResponsesApiResponse,
|
||||
create_responses_request_for_model, get_responses_usage, responses_api_to_message,
|
||||
ResponsesApiResponse,
|
||||
};
|
||||
use crate::images::ImageFormat;
|
||||
use crate::openai_compatible::{
|
||||
@@ -272,6 +273,80 @@ impl OpenAiProviderBuilder {
|
||||
}
|
||||
|
||||
impl OpenAiProvider {
|
||||
pub async fn stream_for_model(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
wire_model: &str,
|
||||
capability_model: &str,
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
let mut payload = create_responses_request_for_model(
|
||||
model_config,
|
||||
wire_model,
|
||||
capability_model,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
)?;
|
||||
payload["stream"] = serde_json::Value::Bool(self.supports_streaming);
|
||||
self.stream_responses_payload(model_config, payload).await
|
||||
}
|
||||
|
||||
async fn stream_responses_payload(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
payload: serde_json::Value,
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
let mut log = start_log(model_config, &payload)?;
|
||||
let response = self
|
||||
.with_retry(|| async {
|
||||
handle_status(
|
||||
self.api_client
|
||||
.request(&Self::map_base_path(
|
||||
&self.base_path,
|
||||
"responses",
|
||||
OPEN_AI_DEFAULT_RESPONSES_PATH,
|
||||
))
|
||||
.model_headers(model_config)?
|
||||
.response_post(&payload)
|
||||
.await?,
|
||||
)
|
||||
.await
|
||||
})
|
||||
.await
|
||||
.inspect_err(|e| {
|
||||
let _ = log.error(e);
|
||||
})?;
|
||||
if self.supports_streaming {
|
||||
stream_responses_compat(response, log)
|
||||
} else {
|
||||
let json: serde_json::Value = response.json().await.map_err(|e| {
|
||||
ProviderError::RequestFailed(format!("Failed to parse JSON: {}", e))
|
||||
})?;
|
||||
let parsed: ResponsesApiResponse =
|
||||
serde_json::from_value(json.clone()).map_err(|e| {
|
||||
ProviderError::ExecutionError(format!(
|
||||
"Failed to parse responses API response: {}",
|
||||
e
|
||||
))
|
||||
})?;
|
||||
let message = responses_api_to_message(&parsed)?;
|
||||
let usage_data = get_responses_usage(&parsed);
|
||||
let usage_json = json.get("usage").unwrap_or(&serde_json::Value::Null);
|
||||
let mut usage = ProviderUsage::new(model_config.model_name.clone(), usage_data);
|
||||
if let Some(cost) = get_cost(usage_json) {
|
||||
usage = usage.with_cost(cost, CostSource::ProviderReported);
|
||||
}
|
||||
log.write(
|
||||
&serde_json::to_value(&message).unwrap_or_default(),
|
||||
Some(&usage_data),
|
||||
)?;
|
||||
Ok(super::base::stream_from_single_message(message, usage))
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn new(api_client: ApiClient) -> Self {
|
||||
Self {
|
||||
@@ -584,6 +659,13 @@ impl Provider for OpenAiProvider {
|
||||
&self.name
|
||||
}
|
||||
|
||||
async fn refresh_credentials(&self) -> Result<(), ProviderError> {
|
||||
self.api_client
|
||||
.refresh_credentials()
|
||||
.await
|
||||
.map_err(|error| ProviderError::Authentication(error.to_string()))
|
||||
}
|
||||
|
||||
fn skip_canonical_filtering(&self) -> bool {
|
||||
self.skip_canonical_filtering
|
||||
}
|
||||
@@ -655,61 +737,17 @@ impl Provider for OpenAiProvider {
|
||||
tools: &[Tool],
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
if self.should_use_responses_api_for_provider(&model_config.model_name) {
|
||||
let mut payload = create_responses_request(model_config, system, messages, tools)?;
|
||||
payload["stream"] = serde_json::Value::Bool(self.supports_streaming);
|
||||
|
||||
let mut log = start_log(model_config, &payload)?;
|
||||
|
||||
let response = self
|
||||
.with_retry(|| async {
|
||||
let payload_clone = payload.clone();
|
||||
let resp = self
|
||||
.api_client
|
||||
.request(&Self::map_base_path(
|
||||
&self.base_path,
|
||||
"responses",
|
||||
OPEN_AI_DEFAULT_RESPONSES_PATH,
|
||||
))
|
||||
.model_headers(model_config)?
|
||||
.response_post(&payload_clone)
|
||||
.await?;
|
||||
handle_status(resp).await
|
||||
})
|
||||
.await
|
||||
.inspect_err(|e| {
|
||||
let _ = log.error(e);
|
||||
})?;
|
||||
|
||||
if self.supports_streaming {
|
||||
stream_responses_compat(response, log)
|
||||
} else {
|
||||
let json: serde_json::Value = response.json().await.map_err(|e| {
|
||||
ProviderError::RequestFailed(format!("Failed to parse JSON: {}", e))
|
||||
})?;
|
||||
|
||||
let responses_api_response: ResponsesApiResponse =
|
||||
serde_json::from_value(json.clone()).map_err(|e| {
|
||||
ProviderError::ExecutionError(format!(
|
||||
"Failed to parse responses API response: {}",
|
||||
e
|
||||
))
|
||||
})?;
|
||||
|
||||
let message = responses_api_to_message(&responses_api_response)?;
|
||||
let usage_data = get_responses_usage(&responses_api_response);
|
||||
let usage_json = json.get("usage").unwrap_or(&serde_json::Value::Null);
|
||||
let mut usage = ProviderUsage::new(model_config.model_name.clone(), usage_data);
|
||||
if let Some(cost) = get_cost(usage_json) {
|
||||
usage = usage.with_cost(cost, CostSource::ProviderReported);
|
||||
}
|
||||
|
||||
log.write(
|
||||
&serde_json::to_value(&message).unwrap_or_default(),
|
||||
Some(&usage_data),
|
||||
)?;
|
||||
|
||||
Ok(super::base::stream_from_single_message(message, usage))
|
||||
}
|
||||
let (wire_model, _) =
|
||||
crate::formats::openai::extract_reasoning_effort(&model_config.model_name);
|
||||
self.stream_for_model(
|
||||
model_config,
|
||||
&wire_model,
|
||||
&model_config.model_name,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
let payload = create_request_with_options(
|
||||
model_config,
|
||||
|
||||
@@ -18,7 +18,8 @@ use super::retry::ProviderRetry;
|
||||
use crate::conversation::message::Message;
|
||||
use crate::errors::ProviderError;
|
||||
use crate::formats::openai::{
|
||||
create_request, get_cost, get_usage, response_to_message, response_to_streaming_message,
|
||||
create_request, create_request_for_model_with_options, get_cost, get_usage,
|
||||
response_to_message, response_to_streaming_message, OpenAiFormatOptions,
|
||||
};
|
||||
use crate::formats::openai_responses::responses_api_to_streaming_message;
|
||||
use crate::model::ModelConfig;
|
||||
@@ -49,6 +50,99 @@ impl OpenAiCompatibleProvider {
|
||||
self
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn build_request_for_model(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
wire_model: &str,
|
||||
capability_model: &str,
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
for_streaming: bool,
|
||||
) -> Result<Value, ProviderError> {
|
||||
create_request_for_model_with_options(
|
||||
model_config,
|
||||
wire_model,
|
||||
capability_model,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
&ImageFormat::OpenAi,
|
||||
for_streaming,
|
||||
OpenAiFormatOptions {
|
||||
preserve_thinking_context: true,
|
||||
},
|
||||
)
|
||||
.map_err(|e| ProviderError::RequestFailed(format!("Failed to create request: {}", e)))
|
||||
}
|
||||
|
||||
pub async fn stream_for_model(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
wire_model: &str,
|
||||
capability_model: &str,
|
||||
system: &str,
|
||||
messages: &[Message],
|
||||
tools: &[Tool],
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
let payload = self.build_request_for_model(
|
||||
model_config,
|
||||
wire_model,
|
||||
capability_model,
|
||||
system,
|
||||
messages,
|
||||
tools,
|
||||
self.supports_streaming,
|
||||
)?;
|
||||
self.stream_payload(model_config, payload).await
|
||||
}
|
||||
|
||||
async fn stream_payload(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
payload: Value,
|
||||
) -> Result<MessageStream, ProviderError> {
|
||||
let mut log = start_log(model_config, &payload)?;
|
||||
let path = format!("{}chat/completions", self.completions_prefix);
|
||||
let response = self
|
||||
.with_retry(|| async {
|
||||
handle_status(
|
||||
self.api_client
|
||||
.request(&path)
|
||||
.model_headers(model_config)?
|
||||
.response_post(&payload)
|
||||
.await?,
|
||||
)
|
||||
.await
|
||||
})
|
||||
.await
|
||||
.inspect_err(|e| {
|
||||
let _ = log.error(e);
|
||||
})?;
|
||||
if self.supports_streaming {
|
||||
stream_openai_compat(response, log)
|
||||
} else {
|
||||
let json = response.json().await.map_err(|e| {
|
||||
ProviderError::RequestFailed(format!("Failed to parse JSON: {}", e))
|
||||
})?;
|
||||
let message = response_to_message(&json).map_err(|e| {
|
||||
ProviderError::RequestFailed(format!("Failed to parse message: {}", e))
|
||||
})?;
|
||||
let usage_json = json.get("usage").unwrap_or(&Value::Null);
|
||||
let usage_data = get_usage(usage_json);
|
||||
let mut usage = ProviderUsage::new(model_config.model_name.clone(), usage_data);
|
||||
if let Some(cost) = get_cost(usage_json) {
|
||||
usage = usage.with_cost(cost, CostSource::ProviderReported);
|
||||
}
|
||||
log.write(
|
||||
&serde_json::to_value(&message).unwrap_or_default(),
|
||||
Some(&usage.usage),
|
||||
)?;
|
||||
Ok(stream_from_single_message(message, usage))
|
||||
}
|
||||
}
|
||||
|
||||
fn build_request(
|
||||
&self,
|
||||
model_config: &ModelConfig,
|
||||
@@ -75,6 +169,13 @@ impl Provider for OpenAiCompatibleProvider {
|
||||
&self.name
|
||||
}
|
||||
|
||||
async fn refresh_credentials(&self) -> Result<(), ProviderError> {
|
||||
self.api_client
|
||||
.refresh_credentials()
|
||||
.await
|
||||
.map_err(|error| ProviderError::Authentication(error.to_string()))
|
||||
}
|
||||
|
||||
async fn fetch_supported_models(&self) -> Result<Vec<String>, ProviderError> {
|
||||
let response = self
|
||||
.api_client
|
||||
@@ -116,49 +217,7 @@ impl Provider for OpenAiCompatibleProvider {
|
||||
tools,
|
||||
self.supports_streaming,
|
||||
)?;
|
||||
let mut log = start_log(model_config, &payload)?;
|
||||
|
||||
let completions_path = format!("{}chat/completions", self.completions_prefix);
|
||||
let response = self
|
||||
.with_retry(|| async {
|
||||
let resp = self
|
||||
.api_client
|
||||
.request(&completions_path)
|
||||
.model_headers(model_config)?
|
||||
.response_post(&payload)
|
||||
.await?;
|
||||
handle_status(resp).await
|
||||
})
|
||||
.await
|
||||
.inspect_err(|e| {
|
||||
let _ = log.error(e);
|
||||
})?;
|
||||
|
||||
if self.supports_streaming {
|
||||
stream_openai_compat(response, log)
|
||||
} else {
|
||||
let json: serde_json::Value = response.json().await.map_err(|e| {
|
||||
ProviderError::RequestFailed(format!("Failed to parse JSON: {}", e))
|
||||
})?;
|
||||
|
||||
let message = response_to_message(&json).map_err(|e| {
|
||||
ProviderError::RequestFailed(format!("Failed to parse message: {}", e))
|
||||
})?;
|
||||
|
||||
let usage_json = json.get("usage").unwrap_or(&serde_json::Value::Null);
|
||||
let usage_data = get_usage(usage_json);
|
||||
let mut usage = ProviderUsage::new(model_config.model_name.clone(), usage_data);
|
||||
if let Some(cost) = get_cost(usage_json) {
|
||||
usage = usage.with_cost(cost, CostSource::ProviderReported);
|
||||
}
|
||||
|
||||
log.write(
|
||||
&serde_json::to_value(&message).unwrap_or_default(),
|
||||
Some(&usage.usage),
|
||||
)?;
|
||||
|
||||
Ok(stream_from_single_message(message, usage))
|
||||
}
|
||||
self.stream_payload(model_config, payload).await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ pub fn model_config_from_user_config(
|
||||
provider_name: &str,
|
||||
model_name: impl AsRef<str>,
|
||||
) -> Result<ModelConfig> {
|
||||
let model = base_model_config_from_user_config(model_name.as_ref())?;
|
||||
let model = base_model_config_from_user_config(provider_name, model_name.as_ref())?;
|
||||
materialize_model_config(provider_name, model)
|
||||
}
|
||||
|
||||
@@ -26,18 +26,26 @@ pub fn model_config_from_user_config_with_session_settings(
|
||||
context_limit: Option<usize>,
|
||||
) -> Result<ModelConfig> {
|
||||
let config = Config::global();
|
||||
let model = base_model_config_from_user_config(model_name.as_ref())?;
|
||||
let model = base_model_config_from_user_config(provider_name, model_name.as_ref())?;
|
||||
let model = materialize_model_config_inner(model, provider_name, false)?
|
||||
.with_context_limit(context_limit)
|
||||
.with_inherited_session_settings_from(previous, request_params)
|
||||
.with_default_thinking_effort(config.get_goose_thinking_effort());
|
||||
|
||||
Ok(model.with_canonical_limits(provider_name))
|
||||
Ok(apply_canonical_limits(provider_name, model))
|
||||
}
|
||||
|
||||
pub fn materialize_model_config(provider_name: &str, model: ModelConfig) -> Result<ModelConfig> {
|
||||
let model = materialize_model_config_inner(model, provider_name, true)?;
|
||||
Ok(model.with_canonical_limits(provider_name))
|
||||
Ok(apply_canonical_limits(provider_name, model))
|
||||
}
|
||||
|
||||
fn apply_canonical_limits(provider_name: &str, model: ModelConfig) -> ModelConfig {
|
||||
if provider_name == goose_providers::azure_foundry::AZURE_FOUNDRY_PROVIDER_NAME {
|
||||
model
|
||||
} else {
|
||||
model.with_canonical_limits(provider_name)
|
||||
}
|
||||
}
|
||||
|
||||
fn materialize_model_config_inner(
|
||||
@@ -169,7 +177,10 @@ fn apply_openai_request_params(mut model: ModelConfig) -> ModelConfig {
|
||||
model
|
||||
}
|
||||
|
||||
fn base_model_config_from_user_config(model_name: &str) -> Result<ModelConfig> {
|
||||
fn base_model_config_from_user_config(
|
||||
provider_name: &str,
|
||||
model_name: &str,
|
||||
) -> Result<ModelConfig> {
|
||||
let config = Config::global();
|
||||
let mut model = ModelConfig {
|
||||
model_name: model_name.to_string(),
|
||||
@@ -182,7 +193,9 @@ fn base_model_config_from_user_config(model_name: &str) -> Result<ModelConfig> {
|
||||
reasoning: None,
|
||||
request_headers: None,
|
||||
};
|
||||
model.normalize_effort_suffix();
|
||||
if provider_name != goose_providers::azure_foundry::AZURE_FOUNDRY_PROVIDER_NAME {
|
||||
model.normalize_effort_suffix();
|
||||
}
|
||||
Ok(model)
|
||||
}
|
||||
|
||||
@@ -247,3 +260,27 @@ fn parse_yaml_bool_config(key: &str, value: serde_yaml::Value) -> Result<bool> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod azure_foundry_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn deployment_name_survives_thinking_effort_changes() {
|
||||
let config = base_model_config_from_user_config("azure_foundry", "gpt-5-high")
|
||||
.unwrap()
|
||||
.with_thinking_effort(ThinkingEffort::Off);
|
||||
|
||||
assert_eq!(config.model_name, "gpt-5-high");
|
||||
assert_eq!(config.context_limit, None);
|
||||
assert_eq!(config.thinking_effort(), Some(ThinkingEffort::Off));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn none_suffixed_deployment_name_is_preserved() {
|
||||
let config = base_model_config_from_user_config("azure_foundry", "gpt-5-none").unwrap();
|
||||
|
||||
assert_eq!(config.model_name, "gpt-5-none");
|
||||
assert_eq!(config.thinking_effort(), None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use futures::future::BoxFuture;
|
||||
use goose_providers::api_client::{AuthMethod, AuthProvider, TlsConfig};
|
||||
use goose_providers::azure_foundry::{endpoint_kind, AzureFoundryProvider, EndpointKind};
|
||||
use goose_providers::base::{ProviderDescriptor, ProviderMetadata};
|
||||
|
||||
use crate::config::{Config, ExtensionConfig};
|
||||
use crate::providers::azureauth::{AzureAuth, AzureCredentials};
|
||||
use crate::providers::base::ProviderDef;
|
||||
|
||||
const AZURE_PROJECT_ENTRA_RESOURCE: &str = "https://ai.azure.com";
|
||||
const AZURE_MAAS_ENTRA_RESOURCE: &str = "https://ml.azure.com";
|
||||
|
||||
enum AuthHeader {
|
||||
ApiKey,
|
||||
Bearer,
|
||||
}
|
||||
|
||||
struct AzureFoundryAuthProvider {
|
||||
auth: Arc<AzureAuth>,
|
||||
header: AuthHeader,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AuthProvider for AzureFoundryAuthProvider {
|
||||
async fn get_auth_header(&self) -> Result<(String, String)> {
|
||||
let token = self.auth.get_token().await?;
|
||||
match &self.header {
|
||||
AuthHeader::ApiKey => Ok(("api-key".to_string(), token.token_value)),
|
||||
AuthHeader::Bearer => Ok((
|
||||
"Authorization".to_string(),
|
||||
format!("Bearer {}", token.token_value),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn refresh_credentials(&self) -> Result<()> {
|
||||
self.auth.invalidate_token().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AzureFoundryProviderDef;
|
||||
|
||||
impl ProviderDescriptor for AzureFoundryProviderDef {
|
||||
fn metadata() -> ProviderMetadata {
|
||||
AzureFoundryProvider::metadata()
|
||||
}
|
||||
}
|
||||
|
||||
impl ProviderDef for AzureFoundryProviderDef {
|
||||
type Provider = AzureFoundryProvider;
|
||||
|
||||
fn from_env(
|
||||
_extensions: Vec<ExtensionConfig>,
|
||||
tls_config: Option<TlsConfig>,
|
||||
) -> BoxFuture<'static, Result<Self::Provider>> {
|
||||
Box::pin(from_env(tls_config))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn from_env(tls_config: Option<TlsConfig>) -> Result<AzureFoundryProvider> {
|
||||
let config = Config::global();
|
||||
let endpoint: String = config.get_param("AZURE_FOUNDRY_ENDPOINT")?;
|
||||
let api_version = config.get_param("AZURE_FOUNDRY_API_VERSION").ok();
|
||||
let maas_model = config
|
||||
.get_param::<String>("AZURE_FOUNDRY_MODEL")
|
||||
.ok()
|
||||
.filter(|model| !model.trim().is_empty());
|
||||
let api_key = config
|
||||
.get_secret::<String>("AZURE_FOUNDRY_API_KEY")
|
||||
.ok()
|
||||
.filter(|key| !key.is_empty());
|
||||
let ad_token = config
|
||||
.get_secret::<String>("AZURE_FOUNDRY_AD_TOKEN")
|
||||
.ok()
|
||||
.filter(|token| !token.is_empty());
|
||||
let endpoint_kind = endpoint_kind(&endpoint);
|
||||
let resource = if endpoint_kind == EndpointKind::Maas {
|
||||
AZURE_MAAS_ENTRA_RESOURCE
|
||||
} else {
|
||||
AZURE_PROJECT_ENTRA_RESOURCE
|
||||
};
|
||||
let auth = Arc::new(AzureAuth::new_with_resource(
|
||||
api_key,
|
||||
ad_token,
|
||||
resource.to_string(),
|
||||
)?);
|
||||
let auth_method = |header| {
|
||||
AuthMethod::Custom(Box::new(AzureFoundryAuthProvider {
|
||||
auth: Arc::clone(&auth),
|
||||
header,
|
||||
}))
|
||||
};
|
||||
let anthropic_auth = match auth.credential_type() {
|
||||
AzureCredentials::ApiKey(key) => AuthMethod::ApiKey {
|
||||
header_name: "x-api-key".to_string(),
|
||||
key: key.clone(),
|
||||
},
|
||||
_ => auth_method(AuthHeader::Bearer),
|
||||
};
|
||||
let api_key_auth_header = || match auth.credential_type() {
|
||||
AzureCredentials::ApiKey(_) => AuthHeader::ApiKey,
|
||||
_ => AuthHeader::Bearer,
|
||||
};
|
||||
let chat_auth_header = match auth.credential_type() {
|
||||
AzureCredentials::ApiKey(_) if endpoint_kind == EndpointKind::Maas => AuthHeader::Bearer,
|
||||
_ => api_key_auth_header(),
|
||||
};
|
||||
|
||||
AzureFoundryProvider::create(
|
||||
endpoint,
|
||||
api_version,
|
||||
maas_model,
|
||||
auth_method(chat_auth_header),
|
||||
auth_method(api_key_auth_header()),
|
||||
anthropic_auth,
|
||||
auth_method(api_key_auth_header()),
|
||||
tls_config,
|
||||
Some(crate::session_context::session_id_request_builder()),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
async fn header(
|
||||
api_key: Option<&str>,
|
||||
ad_token: Option<&str>,
|
||||
header: AuthHeader,
|
||||
) -> (String, String) {
|
||||
let auth = Arc::new(
|
||||
AzureAuth::new_with_resource(
|
||||
api_key.map(str::to_string),
|
||||
ad_token.map(str::to_string),
|
||||
AZURE_PROJECT_ENTRA_RESOURCE.to_string(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
AzureFoundryAuthProvider { auth, header }
|
||||
.get_auth_header()
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn project_api_key_uses_api_key_header() {
|
||||
assert_eq!(
|
||||
header(Some("key"), None, AuthHeader::ApiKey).await,
|
||||
("api-key".to_string(), "key".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn maas_api_key_uses_bearer_header() {
|
||||
assert_eq!(
|
||||
header(Some("key"), None, AuthHeader::Bearer).await,
|
||||
("Authorization".to_string(), "Bearer key".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn entra_token_uses_bearer_header() {
|
||||
assert_eq!(
|
||||
header(None, Some("token"), AuthHeader::Bearer).await,
|
||||
("Authorization".to_string(), "Bearer token".to_string())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,7 @@ struct TokenResponse {
|
||||
#[derive(Debug)]
|
||||
pub struct AzureAuth {
|
||||
credentials: AzureCredentials,
|
||||
resource: String,
|
||||
cached_token: Arc<RwLock<Option<CachedToken>>>,
|
||||
}
|
||||
|
||||
@@ -73,6 +74,18 @@ impl AzureAuth {
|
||||
/// # Returns
|
||||
/// * `Result<Self, AuthError>` - A new AzureAuth instance or an error if initialization fails
|
||||
pub fn new(api_key: Option<String>, ad_token: Option<String>) -> Result<Self, AuthError> {
|
||||
Self::new_with_resource(
|
||||
api_key,
|
||||
ad_token,
|
||||
"https://cognitiveservices.azure.com".to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_with_resource(
|
||||
api_key: Option<String>,
|
||||
ad_token: Option<String>,
|
||||
resource: String,
|
||||
) -> Result<Self, AuthError> {
|
||||
let credentials = match (ad_token, api_key) {
|
||||
(Some(token), _) => AzureCredentials::BearerToken(token),
|
||||
(None, Some(key)) => AzureCredentials::ApiKey(key),
|
||||
@@ -81,6 +94,7 @@ impl AzureAuth {
|
||||
|
||||
Ok(Self {
|
||||
credentials,
|
||||
resource,
|
||||
cached_token: Arc::new(RwLock::new(None)),
|
||||
})
|
||||
}
|
||||
@@ -90,6 +104,10 @@ impl AzureAuth {
|
||||
&self.credentials
|
||||
}
|
||||
|
||||
pub async fn invalidate_token(&self) {
|
||||
*self.cached_token.write().await = None;
|
||||
}
|
||||
|
||||
/// Retrieves a valid authentication token.
|
||||
///
|
||||
/// This method implements an efficient token management strategy:
|
||||
@@ -137,12 +155,7 @@ impl AzureAuth {
|
||||
|
||||
let az = if cfg!(windows) { "az.cmd" } else { "az" };
|
||||
let output = tokio::process::Command::new(az)
|
||||
.args([
|
||||
"account",
|
||||
"get-access-token",
|
||||
"--resource",
|
||||
"https://cognitiveservices.azure.com",
|
||||
])
|
||||
.args(["account", "get-access-token", "--resource", &self.resource])
|
||||
.set_no_window()
|
||||
.output()
|
||||
.await
|
||||
|
||||
@@ -37,6 +37,7 @@ use super::{
|
||||
};
|
||||
use crate::config::ExtensionConfig;
|
||||
use crate::providers::anthropic_def::AnthropicProviderDef;
|
||||
use crate::providers::azure_foundry_def::AzureFoundryProviderDef;
|
||||
use crate::providers::base::ProviderType;
|
||||
use crate::providers::databricks_def::{self, DatabricksProviderDef};
|
||||
use crate::providers::databricks_v2_def::{self, DatabricksV2ProviderDef};
|
||||
@@ -69,6 +70,10 @@ async fn init_registry() -> RwLock<ProviderRegistry> {
|
||||
);
|
||||
registry.register::<AvianProvider>(false);
|
||||
registry.register::<AzureProvider>(false);
|
||||
registry.register_with_inventory::<AzureFoundryProviderDef>(
|
||||
true,
|
||||
Some(registrations::azure_foundry_inventory()),
|
||||
);
|
||||
#[cfg(feature = "aws-providers")]
|
||||
registry.register::<BedrockProvider>(false);
|
||||
#[cfg(feature = "local-inference")]
|
||||
|
||||
@@ -20,6 +20,7 @@ use crate::providers::ollama::OLLAMA_PROVIDER_NAME;
|
||||
use crate::providers::openai::{OPEN_AI_DEFAULT_BASE_PATH, OPEN_AI_PROVIDER_NAME};
|
||||
use crate::providers::pi_acp::{PI_ACP_BINARY, PI_ACP_PROVIDER_NAME};
|
||||
use crate::providers::xai_oauth::TokenCache as XaiOAuthTokenCache;
|
||||
use goose_providers::azure_foundry::{endpoint_kind, EndpointKind, AZURE_FOUNDRY_PROVIDER_NAME};
|
||||
|
||||
pub fn openai_inventory() -> InventoryRegistration {
|
||||
InventoryRegistration::new(true, || {
|
||||
@@ -67,6 +68,52 @@ pub fn openai_inventory() -> InventoryRegistration {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn azure_foundry_inventory() -> InventoryRegistration {
|
||||
InventoryRegistration::new(true, || {
|
||||
let config = Config::global();
|
||||
let mut identity =
|
||||
InventoryIdentityInput::new(AZURE_FOUNDRY_PROVIDER_NAME, AZURE_FOUNDRY_PROVIDER_NAME);
|
||||
if let Ok(endpoint) = config.get_param::<String>("AZURE_FOUNDRY_ENDPOINT") {
|
||||
identity = identity.with_public("endpoint", endpoint);
|
||||
}
|
||||
if let Ok(api_version) = config.get_param::<String>("AZURE_FOUNDRY_API_VERSION") {
|
||||
identity = identity.with_public("api_version", api_version);
|
||||
}
|
||||
if let Ok(model) = config.get_param::<String>("AZURE_FOUNDRY_MODEL") {
|
||||
identity = identity.with_public("model", model);
|
||||
}
|
||||
if let Some(api_key) = config_secret_value(config, "AZURE_FOUNDRY_API_KEY") {
|
||||
identity = identity.with_secret("api_key", api_key);
|
||||
}
|
||||
if let Some(ad_token) = config_secret_value(config, "AZURE_FOUNDRY_AD_TOKEN") {
|
||||
identity = identity.with_secret("ad_token", ad_token);
|
||||
}
|
||||
Ok(identity)
|
||||
})
|
||||
.with_configured(|| azure_foundry_configured(Config::global()))
|
||||
}
|
||||
|
||||
fn azure_foundry_configured(config: &Config) -> bool {
|
||||
azure_foundry_configured_values(
|
||||
config
|
||||
.get_param::<String>("AZURE_FOUNDRY_ENDPOINT")
|
||||
.ok()
|
||||
.as_deref(),
|
||||
config
|
||||
.get_param::<String>("AZURE_FOUNDRY_MODEL")
|
||||
.ok()
|
||||
.as_deref(),
|
||||
)
|
||||
}
|
||||
|
||||
fn azure_foundry_configured_values(endpoint: Option<&str>, model: Option<&str>) -> bool {
|
||||
let Some(endpoint) = endpoint.filter(|endpoint| !endpoint.trim().is_empty()) else {
|
||||
return false;
|
||||
};
|
||||
endpoint_kind(endpoint) != EndpointKind::Maas
|
||||
|| model.is_some_and(|model| !model.trim().is_empty())
|
||||
}
|
||||
|
||||
pub fn anthropic_inventory() -> InventoryRegistration {
|
||||
InventoryRegistration::new(true, || {
|
||||
let config = Config::global();
|
||||
@@ -222,6 +269,30 @@ mod tests {
|
||||
use crate::config::paths::Paths;
|
||||
use chrono::Utc;
|
||||
|
||||
#[test]
|
||||
fn azure_foundry_maas_requires_a_model_to_be_configured() {
|
||||
assert!(!azure_foundry_configured_values(
|
||||
Some("https://deployment.models.ai.azure.com"),
|
||||
None,
|
||||
));
|
||||
assert!(!azure_foundry_configured_values(
|
||||
Some("https://deployment.models.ai.azure.com"),
|
||||
Some(" "),
|
||||
));
|
||||
assert!(azure_foundry_configured_values(
|
||||
Some("https://deployment.models.ai.azure.com"),
|
||||
Some("Phi-4"),
|
||||
));
|
||||
assert!(azure_foundry_configured_values(
|
||||
Some("https://hub.services.ai.azure.com/api/projects/project"),
|
||||
None,
|
||||
));
|
||||
assert!(azure_foundry_configured_values(
|
||||
Some("https://hub.services.ai.azure.com"),
|
||||
None,
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial_test::serial]
|
||||
fn gemini_oauth_inventory_configured_uses_token_cache() {
|
||||
|
||||
@@ -9,6 +9,7 @@ pub mod api_client {
|
||||
}
|
||||
pub mod avian;
|
||||
pub mod azure;
|
||||
pub mod azure_foundry_def;
|
||||
pub mod azureauth;
|
||||
pub mod base;
|
||||
#[cfg(feature = "aws-providers")]
|
||||
|
||||
@@ -715,6 +715,27 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_session_model_config(
|
||||
provider_name: Option<&str>,
|
||||
json: &str,
|
||||
) -> Option<ModelConfig> {
|
||||
let mut model_config: ModelConfig = serde_json::from_str(json).ok()?;
|
||||
// TODO: Remove this workaround once ModelConfig guarantees deserialize(serialize(config)) == config.
|
||||
if provider_name == Some(goose_providers::azure_foundry::AZURE_FOUNDRY_PROVIDER_NAME) {
|
||||
#[derive(Deserialize)]
|
||||
struct AzurePersistedFields {
|
||||
model_name: String,
|
||||
#[serde(default)]
|
||||
request_params: Option<HashMap<String, serde_json::Value>>,
|
||||
}
|
||||
|
||||
let persisted: AzurePersistedFields = serde_json::from_str(json).ok()?;
|
||||
model_config.model_name = persisted.model_name;
|
||||
model_config.request_params = persisted.request_params;
|
||||
}
|
||||
Some(model_config)
|
||||
}
|
||||
|
||||
impl sqlx::FromRow<'_, sqlx::sqlite::SqliteRow> for Session {
|
||||
fn from_row(row: &sqlx::sqlite::SqliteRow) -> Result<Self, sqlx::Error> {
|
||||
use sqlx::Row;
|
||||
@@ -726,8 +747,11 @@ impl sqlx::FromRow<'_, sqlx::sqlite::SqliteRow> for Session {
|
||||
let user_recipe_values =
|
||||
user_recipe_values_json.and_then(|json| serde_json::from_str(&json).ok());
|
||||
|
||||
let provider_name: Option<String> = row.try_get("provider_name").ok().flatten();
|
||||
let model_config_json: Option<String> = row.try_get("model_config_json").ok().flatten();
|
||||
let model_config = model_config_json.and_then(|json| serde_json::from_str(&json).ok());
|
||||
let model_config = model_config_json
|
||||
.as_deref()
|
||||
.and_then(|json| deserialize_session_model_config(provider_name.as_deref(), json));
|
||||
|
||||
let name: String = {
|
||||
let name_val: String = row.try_get("name").unwrap_or_default();
|
||||
@@ -788,7 +812,7 @@ impl sqlx::FromRow<'_, sqlx::sqlite::SqliteRow> for Session {
|
||||
conversation: None,
|
||||
message_count: row.try_get("message_count").unwrap_or(0) as usize,
|
||||
last_message_at,
|
||||
provider_name: row.try_get("provider_name").ok().flatten(),
|
||||
provider_name,
|
||||
model_config,
|
||||
goose_mode: row
|
||||
.try_get::<String, _>("goose_mode")
|
||||
@@ -2587,6 +2611,61 @@ mod tests {
|
||||
const NUM_CONCURRENT_SESSIONS: i32 = 10;
|
||||
const GENERATED_SESSION_NAME: &str = "Generated session name";
|
||||
|
||||
#[test]
|
||||
fn azure_session_model_config_preserves_suffixed_deployment_id() {
|
||||
let json = serde_json::to_string(&ModelConfig {
|
||||
model_name: "gpt-5-high".to_string(),
|
||||
context_limit: None,
|
||||
temperature: None,
|
||||
max_tokens: None,
|
||||
toolshim: false,
|
||||
toolshim_model: None,
|
||||
request_params: None,
|
||||
reasoning: None,
|
||||
request_headers: None,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let config = deserialize_session_model_config(
|
||||
Some(goose_providers::azure_foundry::AZURE_FOUNDRY_PROVIDER_NAME),
|
||||
&json,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(config.model_name, "gpt-5-high");
|
||||
assert_eq!(config.thinking_effort(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn azure_session_model_config_preserves_explicit_thinking_effort() {
|
||||
let config = deserialize_session_model_config(
|
||||
Some(goose_providers::azure_foundry::AZURE_FOUNDRY_PROVIDER_NAME),
|
||||
r#"{"model_name":"gpt-5-high","context_limit":null,"temperature":null,"max_tokens":null,"toolshim":false,"toolshim_model":null,"request_params":{"thinking_effort":"low"}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(config.model_name, "gpt-5-high");
|
||||
assert_eq!(
|
||||
config.thinking_effort(),
|
||||
Some(goose_providers::thinking::ThinkingEffort::Low)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_azure_session_model_config_keeps_suffix_normalization() {
|
||||
let config = deserialize_session_model_config(
|
||||
Some(goose_providers::openai::OPEN_AI_PROVIDER_NAME),
|
||||
r#"{"model_name":"gpt-5-high","context_limit":null,"temperature":null,"max_tokens":null,"toolshim":false,"toolshim_model":null}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(config.model_name, "gpt-5");
|
||||
assert_eq!(
|
||||
config.thinking_effort(),
|
||||
Some(goose_providers::thinking::ThinkingEffort::High)
|
||||
);
|
||||
}
|
||||
|
||||
struct NamingTestProvider;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
|
||||
@@ -27,6 +27,7 @@ goose is compatible with a wide range of LLM providers, allowing you to choose a
|
||||
| [Anthropic](https://www.anthropic.com/) | Offers Claude, an advanced AI model for natural language tasks. | `ANTHROPIC_API_KEY`, `ANTHROPIC_HOST` (optional) |
|
||||
| [Atomic Chat](https://github.com/AtomicBot-ai/Atomic-Chat) | Run local models with Atomic Chat's OpenAI-compatible server. **Because this provider runs locally, you must first [download a model](#local-llms).** | None required. Connects to local server at `localhost:1337` by default. |
|
||||
| [Avian](https://avian.io/) | Cost-effective inference API with DeepSeek, Kimi, GLM, and MiniMax models. OpenAI-compatible with streaming and function calling support. | `AVIAN_API_KEY`, `AVIAN_HOST` (optional) |
|
||||
| [Azure AI Foundry](/docs/guides/azure-foundry-provider) | Access OpenAI, Anthropic, Microsoft, Meta, Mistral, DeepSeek, GLM, Kimi, and other models deployed through Azure AI Foundry project or MaaS endpoints. | `AZURE_FOUNDRY_ENDPOINT`, `AZURE_FOUNDRY_API_KEY` (optional), `AZURE_FOUNDRY_AD_TOKEN` (optional), `AZURE_FOUNDRY_API_VERSION` (optional) |
|
||||
| [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/) | Access Azure-hosted OpenAI models, including GPT-4 and GPT-3.5. Supports API key, Entra ID bearer token, and Azure credential chain authentication. | `AZURE_OPENAI_ENDPOINT`, `AZURE_OPENAI_DEPLOYMENT_NAME`, `AZURE_OPENAI_API_KEY` (optional), `AZURE_OPENAI_AD_TOKEN` (optional) |
|
||||
| [ChatGPT Codex](https://chatgpt.com/codex) | Access GPT-5 Codex models optimized for code generation and understanding. **Requires a ChatGPT Plus/Pro subscription.** | No manual key. Uses browser-based OAuth authentication for both CLI and Desktop. |
|
||||
| [Databricks](https://www.databricks.com/) | Unified data analytics and AI platform for building and deploying models. | `DATABRICKS_HOST`, `DATABRICKS_TOKEN` |
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
---
|
||||
title: Azure AI Foundry
|
||||
description: Use OpenAI, Anthropic, and partner model deployments from an Azure AI Foundry project
|
||||
---
|
||||
|
||||
# Azure AI Foundry
|
||||
|
||||
The `azure_foundry` provider connects goose to Azure AI Foundry deployments. It supports two endpoint types:
|
||||
|
||||
| Endpoint | Inference surface |
|
||||
|---|---|
|
||||
| Foundry project: `https://<resource>.services.ai.azure.com/api/projects/<project>` | Deployment discovery plus publisher-aware routing |
|
||||
| Foundry resource: `https://<resource>.services.ai.azure.com` | OpenAI models through Responses, Claude through Anthropic Messages, and partner models through Chat Completions |
|
||||
| MaaS/serverless: `https://<deployment>.<region>.models.ai.azure.com` | Chat Completions for the model bound to the endpoint |
|
||||
|
||||
For project endpoints, goose discovers deployments with `GET /deployments`. Deployment names can be customized; goose uses the returned `modelPublisher` to select the protocol and `modelName` to resolve model metadata such as the context window.
|
||||
|
||||
Resource endpoints do not expose project deployment discovery. goose routes recognizable model or deployment names by family: `gpt-5*` and supported o-series models use Responses, `claude-*` uses Anthropic Messages, and other names use Chat Completions. Use a project endpoint when aliases do not identify their underlying model family.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Required | Description |
|
||||
|---|---:|---|
|
||||
| `AZURE_FOUNDRY_ENDPOINT` | Yes | Full Foundry project or MaaS endpoint |
|
||||
| `AZURE_FOUNDRY_API_KEY` | No | API key; omit it to use Azure CLI credentials |
|
||||
| `AZURE_FOUNDRY_MODEL` | MaaS only | Model bound to the configured MaaS endpoint |
|
||||
| `AZURE_FOUNDRY_AD_TOKEN` | No | Pre-acquired Microsoft Entra access token; takes precedence over the API key |
|
||||
| `AZURE_FOUNDRY_API_VERSION` | No | Deployment discovery API version; project endpoints default to `v1` |
|
||||
|
||||
Run `goose configure`, select **Configure Providers**, and choose **Azure AI Foundry**. You can also set the variables before starting goose:
|
||||
|
||||
```sh
|
||||
export AZURE_FOUNDRY_ENDPOINT="https://my-resource.services.ai.azure.com/api/projects/my-project"
|
||||
export AZURE_FOUNDRY_API_KEY="<key>"
|
||||
goose session
|
||||
```
|
||||
|
||||
For a MaaS endpoint:
|
||||
|
||||
```sh
|
||||
export AZURE_FOUNDRY_ENDPOINT="https://my-deployment.eastus.models.ai.azure.com"
|
||||
export AZURE_FOUNDRY_API_KEY="<key>"
|
||||
export AZURE_FOUNDRY_MODEL="<model-bound-to-this-endpoint>"
|
||||
goose session
|
||||
```
|
||||
|
||||
MaaS endpoints expose a single deployed model. `AZURE_FOUNDRY_MODEL` is required for these endpoints.
|
||||
|
||||
## Authentication
|
||||
|
||||
Authentication is selected in this order:
|
||||
|
||||
1. `AZURE_FOUNDRY_AD_TOKEN`
|
||||
2. `AZURE_FOUNDRY_API_KEY`
|
||||
3. Azure CLI credentials
|
||||
|
||||
When neither token nor key is configured, sign in with Azure CLI before starting goose:
|
||||
|
||||
```sh
|
||||
az login
|
||||
```
|
||||
|
||||
Project and resource endpoints request a token for `https://ai.azure.com`. MaaS endpoints request a token for `https://ml.azure.com`.
|
||||
|
||||
## Protocol routing
|
||||
|
||||
For a project endpoint, goose routes each deployment using metadata returned by Azure:
|
||||
|
||||
- publisher `OpenAI` with a Responses-compatible model (`gpt-5*` and the supported o-series) → `/openai/v1/responses`
|
||||
- publisher `Anthropic` → `/anthropic/v1/messages`
|
||||
- older OpenAI models and all other publishers → `/openai/v1/chat/completions`
|
||||
|
||||
If deployment discovery is temporarily unavailable, recognizable Responses-compatible OpenAI and `claude-*` names use their native surfaces. Other names use Chat Completions.
|
||||
|
||||
Resource endpoints use the same inference surfaces without deployment discovery: recognizable model-family names select the native protocol. This allows a custom agent's `model:` declaration to select deployments such as `gpt-5.6-sol` without rewriting the name through canonical model resolution.
|
||||
|
||||
MaaS endpoints always use `/v1/chat/completions` and the model configured by `AZURE_FOUNDRY_MODEL`.
|
||||
|
||||
## Model metadata and pricing
|
||||
|
||||
The deployments API provides the deployment name and underlying `modelName`, `modelVersion`, and `modelPublisher`. goose uses the underlying model name to look up a context window in its bundled model catalog. An explicit `GOOSE_CONTEXT_LIMIT` or session override still takes precedence.
|
||||
|
||||
Azure pricing depends on region, SKU, offer, deployment type, and contract. The deployments API does not provide a reliable per-token price, so this provider does not attach a price to discovered deployments.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### 401 or 403
|
||||
|
||||
- Ensure the key belongs to the configured endpoint.
|
||||
- For Entra authentication, run `az login` again and verify that your identity has access to the Foundry project.
|
||||
- Do not use a project endpoint key with a MaaS endpoint, or the reverse.
|
||||
|
||||
### No deployments are listed
|
||||
|
||||
- Confirm that the endpoint includes `/api/projects/<project>`.
|
||||
- Confirm that the project contains model deployments.
|
||||
- If your project uses a non-default deployment API version, set `AZURE_FOUNDRY_API_VERSION`.
|
||||
|
||||
### Wrong protocol for a custom deployment name
|
||||
|
||||
Refresh the provider model list so goose can retrieve `modelPublisher`. Without deployment metadata, routing can only use recognizable model-name prefixes.
|
||||
@@ -8,6 +8,7 @@ activities:
|
||||
- Initialize test workspace and logging infrastructure
|
||||
- Test file operations (create, read, update, delete, undo)
|
||||
- Validate shell command execution and error handling
|
||||
- Validate Azure AI Foundry provider routing and deployment aliases
|
||||
- Analyze code structure and parsing capabilities
|
||||
- Test extension discovery and management
|
||||
- Test load tool for knowledge injection and discovery
|
||||
@@ -156,6 +157,14 @@ prompt: |
|
||||
4. Test symbol focus and call graphs
|
||||
5. Verify LOC, function, and class counting
|
||||
|
||||
### Azure AI Foundry Provider Validation
|
||||
When running from a goose source checkout:
|
||||
1. Run the goose-providers Azure Foundry test module.
|
||||
2. Verify project deployments route Responses-compatible OpenAI models to Responses, Anthropic models to Messages, and partner or older OpenAI models to Chat Completions.
|
||||
3. Verify a custom deployment alias remains the wire model while the underlying Azure model controls reasoning capabilities.
|
||||
4. Verify MaaS requires its bound model and uses /v1/chat/completions.
|
||||
If the source checkout or Rust toolchain is unavailable, mark this validation as skipped rather than failed.
|
||||
|
||||
Log results to: {{ workspace_dir }}/phase1_basic_tools.md
|
||||
{% endif %}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import SnowflakeLogo from './icons/snowflake@3x.png';
|
||||
import XaiLogo from './icons/xai@3x.png';
|
||||
import MiniMaxLogo from './icons/minimax@3x.png';
|
||||
import TanzuLogo from './icons/tanzu@3x.png';
|
||||
import AzureFoundryLogo from './icons/azure_foundry@3x.png';
|
||||
import DefaultLogo from './icons/default@3x.png';
|
||||
import { defineMessages, useIntl } from '../../../../../i18n';
|
||||
|
||||
@@ -32,6 +33,7 @@ const providerLogos: Record<string, string> = {
|
||||
xai: XaiLogo,
|
||||
minimax: MiniMaxLogo,
|
||||
tanzu_ai: TanzuLogo,
|
||||
azure_foundry: AzureFoundryLogo,
|
||||
default: DefaultLogo,
|
||||
};
|
||||
|
||||
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
BIN
Binary file not shown.
|
After Width: | Height: | Size: 8.3 KiB |
Reference in New Issue
Block a user