Add a decisions provider crate, with impls for openrouter and jev (#12418)

This commit is contained in:
Jack Amadeo
2026-09-22 18:28:41 +00:00
committed by GitHub
parent 91a5789bce
commit 8a460f964e
6 changed files with 774 additions and 0 deletions
+87
View File
@@ -0,0 +1,87 @@
use crate::errors::ProviderError;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DecisionRequest {
pub model: String,
pub state: Value,
pub questions: HashMap<String, DecisionQuestion>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum DecisionQuestion {
Noul {
instructions: String,
#[serde(skip_serializing_if = "Option::is_none")]
criteria: Option<NoulCriteria>,
},
Choice {
instructions: String,
criteria: HashMap<String, String>,
},
Score {
instructions: String,
criteria: Vec<String>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NoulCriteria {
#[serde(rename = "true")]
pub true_description: String,
#[serde(rename = "false")]
pub false_description: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DecisionResponse {
pub model: String,
#[serde(default)]
pub answers: HashMap<String, DecisionAnswer>,
pub usage: DecisionUsage,
#[serde(default)]
pub id: Option<String>,
#[serde(default)]
pub provider: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum DecisionAnswer {
Noul {
noul: f64,
},
Choice {
choice: String,
confidence: f64,
probabilities: HashMap<String, f64>,
},
Score {
score: f64,
confidence: f64,
legend: HashMap<String, Value>,
probabilities: HashMap<String, f64>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DecisionUsage {
#[serde(default)]
pub input_tokens: Option<u64>,
#[serde(default)]
pub output_tokens: Option<u64>,
#[serde(default)]
pub cost: Option<f64>,
}
#[async_trait]
pub trait DecisionProvider: Send + Sync {
async fn create_decision(
&self,
request: &DecisionRequest,
) -> Result<DecisionResponse, ProviderError>;
}
+2
View File
@@ -10,6 +10,7 @@ pub mod azure_foundry;
pub mod databricks;
pub mod databricks_auth;
pub mod databricks_v2;
pub mod decision;
pub mod google;
pub use goose_provider_types::{
base, cache_semantics, canonical, context_limit, conversation, documents, errors, formats,
@@ -32,6 +33,7 @@ pub mod openai_live;
pub mod openai_live_voice_provider;
pub mod openrouter;
pub mod openrouter_format;
pub mod typesafe;
pub use declarative::declarative_providers::*;
+54
View File
@@ -11,8 +11,10 @@ pub type OpenRouterSessionIdProvider = Box<dyn Fn() -> Option<String> + Send + S
use crate::base::{ConfigKey, MessageStream, Provider, ProviderMetadata};
use crate::cache_semantics::{apply_chat_payload_breakpoints, CacheSemantics};
use crate::conversation::message::Message;
use crate::decision::{DecisionProvider, DecisionRequest, DecisionResponse};
use crate::errors::ProviderError;
use crate::formats::openai::create_request;
use crate::http_status::read_json_response;
use crate::model::ModelConfig;
use crate::openai_compatible::{handle_status, stream_openai_compat};
use crate::openrouter_format;
@@ -23,6 +25,7 @@ use rmcp::model::Tool;
pub const OPENROUTER_PROVIDER_NAME: &str = "openrouter";
const OPENROUTER_PARAMETERS_CONFIG_KEY: &str = "OPENROUTER_PARAMETERS";
pub const OPENROUTER_DEFAULT_MODEL: &str = "anthropic/claude-sonnet-4";
pub const OPENROUTER_DECISION_DEFAULT_MODEL: &str = "typesafe/jev-1.13";
// OpenRouter can run many models, we suggest the default
pub const OPENROUTER_KNOWN_MODELS: &[&str] = &[
@@ -395,6 +398,57 @@ impl crate::base::ProviderDescriptor for OpenRouterProvider {
}
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct OpenRouterDecisionOptions {
#[serde(skip_serializing_if = "Option::is_none")]
pub provider: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub trace: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
}
impl OpenRouterProvider {
pub async fn create_decision_with_options(
&self,
request: &DecisionRequest,
options: &OpenRouterDecisionOptions,
) -> Result<DecisionResponse, ProviderError> {
let mut payload = serde_json::to_value(request).map_err(|error| {
ProviderError::RequestFailed(format!("Failed to serialize Decisions request: {error}"))
})?;
let option_values = serde_json::to_value(options).map_err(|error| {
ProviderError::RequestFailed(format!(
"Failed to serialize OpenRouter Decisions options: {error}"
))
})?;
if let (Some(payload), Some(options)) = (payload.as_object_mut(), option_values.as_object())
{
payload.extend(options.clone());
}
let response = self
.api_client
.request("api/alpha/decisions")
.response_post(&payload)
.await?;
let response = handle_status(response).await?;
read_json_response(response).await
}
}
#[async_trait]
impl DecisionProvider for OpenRouterProvider {
async fn create_decision(
&self,
request: &DecisionRequest,
) -> Result<DecisionResponse, ProviderError> {
self.create_decision_with_options(request, &OpenRouterDecisionOptions::default())
.await
}
}
#[async_trait]
impl Provider for OpenRouterProvider {
fn get_name(&self) -> &str {
+88
View File
@@ -0,0 +1,88 @@
use crate::api_client::ApiClient;
use crate::decision::{DecisionProvider, DecisionRequest, DecisionResponse};
use crate::errors::ProviderError;
use crate::http_status::read_json_response;
use crate::openai_compatible::handle_status;
use async_trait::async_trait;
pub const TYPESAFE_DEFAULT_HOST: &str = "https://api.typesafe.ai";
pub const TYPESAFE_DEFAULT_MODEL: &str = "jev-latest";
pub struct TypeSafeProvider {
api_client: ApiClient,
}
impl TypeSafeProvider {
pub fn new(api_client: ApiClient) -> Self {
Self { api_client }
}
}
#[async_trait]
impl DecisionProvider for TypeSafeProvider {
async fn create_decision(
&self,
request: &DecisionRequest,
) -> Result<DecisionResponse, ProviderError> {
let payload = serde_json::to_value(request).map_err(|error| {
ProviderError::RequestFailed(format!("Failed to serialize decision request: {error}"))
})?;
let response = self
.api_client
.request("v1/systemone")
.response_post(&payload)
.await?;
let response = handle_status(response).await?;
read_json_response(response).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api_client::AuthMethod;
use crate::decision::{DecisionAnswer, DecisionQuestion};
use serde_json::json;
use std::collections::HashMap;
use wiremock::matchers::{body_json, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
#[tokio::test]
async fn creates_system_one_decision() {
let server = MockServer::start().await;
let request = DecisionRequest {
model: TYPESAFE_DEFAULT_MODEL.to_string(),
state: json!("Please help ASAP"),
questions: HashMap::from([(
"urgent".to_string(),
DecisionQuestion::Noul {
instructions: "Does this express urgency?".to_string(),
criteria: None,
},
)]),
};
Mock::given(method("POST"))
.and(path("/v1/systemone"))
.and(body_json(&request))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"model": "jev-latest",
"answers": {"urgent": {"type": "noul", "noul": 0.99}},
"usage": {"input_tokens": 12, "output_tokens": 3}
})))
.expect(1)
.mount(&server)
.await;
let provider = TypeSafeProvider::new(
ApiClient::new_with_tls(server.uri(), AuthMethod::BearerToken("key".into()), None)
.unwrap(),
);
let response = provider.create_decision(&request).await.unwrap();
assert_eq!(
response.answers["urgent"],
DecisionAnswer::Noul { noul: 0.99 }
);
}
}
+230
View File
@@ -19,6 +19,11 @@ use goose_providers::{
databricks::DatabricksProvider as GooseDatabricksProvider,
databricks_auth::DatabricksAuth,
databricks_v2::DatabricksV2Provider as GooseDatabricksV2Provider,
decision::{
DecisionAnswer as GooseDecisionAnswer, DecisionProvider as GooseDecisionProvider,
DecisionQuestion as GooseDecisionQuestion, DecisionRequest as GooseDecisionRequest,
NoulCriteria as GooseNoulCriteria,
},
declarative::{DeclarativeProviderConfig, EnvKeyResolver},
documents::{document_media_type_is_supported, SUPPORTED_DOCUMENT_MEDIA_TYPES},
model::ModelConfig,
@@ -866,6 +871,185 @@ fn convert_tools(tools: Vec<ProviderTool>) -> Result<Vec<Tool>, GooseError> {
tools.iter().map(ProviderTool::to_goose_tool).collect()
}
#[derive(Debug, Clone, uniffi::Enum)]
pub enum DecisionQuestion {
Noul {
instructions: String,
criteria: Option<NoulCriteria>,
},
Choice {
instructions: String,
criteria: HashMap<String, String>,
},
Score {
instructions: String,
criteria: Vec<String>,
},
}
#[derive(Debug, Clone, uniffi::Record)]
pub struct NoulCriteria {
pub true_description: String,
pub false_description: String,
}
#[derive(Debug, Clone, uniffi::Record)]
pub struct DecisionRequest {
pub model: String,
pub state_json: String,
pub questions: HashMap<String, DecisionQuestion>,
}
#[derive(Debug, Clone, uniffi::Enum)]
pub enum DecisionAnswer {
Noul {
noul: f64,
},
Choice {
choice: String,
confidence: f64,
probabilities: HashMap<String, f64>,
},
Score {
score: f64,
confidence: f64,
legend_json: HashMap<String, String>,
probabilities: HashMap<String, f64>,
},
}
#[derive(Debug, Clone, uniffi::Record)]
pub struct DecisionResponse {
pub model: String,
pub answers: HashMap<String, DecisionAnswer>,
pub input_tokens: Option<u64>,
pub output_tokens: Option<u64>,
pub cost: Option<f64>,
pub id: Option<String>,
pub provider: Option<String>,
}
impl TryFrom<DecisionRequest> for GooseDecisionRequest {
type Error = GooseError;
fn try_from(value: DecisionRequest) -> Result<Self, Self::Error> {
Ok(Self {
model: value.model,
state: serde_json::from_str(&value.state_json)?,
questions: value
.questions
.into_iter()
.map(|(name, question)| (name, question.into()))
.collect(),
})
}
}
impl From<DecisionQuestion> for GooseDecisionQuestion {
fn from(value: DecisionQuestion) -> Self {
match value {
DecisionQuestion::Noul {
instructions,
criteria,
} => Self::Noul {
instructions,
criteria: criteria.map(|criteria| GooseNoulCriteria {
true_description: criteria.true_description,
false_description: criteria.false_description,
}),
},
DecisionQuestion::Choice {
instructions,
criteria,
} => Self::Choice {
instructions,
criteria,
},
DecisionQuestion::Score {
instructions,
criteria,
} => Self::Score {
instructions,
criteria,
},
}
}
}
impl From<goose_providers::decision::DecisionResponse> for DecisionResponse {
fn from(value: goose_providers::decision::DecisionResponse) -> Self {
Self {
model: value.model,
answers: value
.answers
.into_iter()
.map(|(name, answer)| (name, answer.into()))
.collect(),
input_tokens: value.usage.input_tokens,
output_tokens: value.usage.output_tokens,
cost: value.usage.cost,
id: value.id,
provider: value.provider,
}
}
}
impl From<GooseDecisionAnswer> for DecisionAnswer {
fn from(value: GooseDecisionAnswer) -> Self {
match value {
GooseDecisionAnswer::Noul { noul } => Self::Noul { noul },
GooseDecisionAnswer::Choice {
choice,
confidence,
probabilities,
} => Self::Choice {
choice,
confidence,
probabilities,
},
GooseDecisionAnswer::Score {
score,
confidence,
legend,
probabilities,
} => Self::Score {
score,
confidence,
legend_json: legend
.into_iter()
.map(|(level, description)| {
let description = match description {
serde_json::Value::String(text) => text,
other => other.to_string(),
};
(level, description)
})
.collect(),
probabilities,
},
}
}
}
#[derive(uniffi::Object)]
pub struct DecisionProvider {
provider: Arc<dyn GooseDecisionProvider>,
}
#[uniffi::export]
impl DecisionProvider {
pub async fn create_decision(
&self,
request: DecisionRequest,
) -> Result<DecisionResponse, GooseError> {
let request = request.try_into()?;
let provider = Arc::clone(&self.provider);
let response =
run_on_runtime(async move { provider.create_decision(&request).await }).await??;
Ok(response.into())
}
}
#[derive(uniffi::Object)]
pub struct Provider {
handle: ProviderHandle,
@@ -1033,6 +1217,52 @@ pub fn declarative_provider_from_json(json: String) -> Result<Arc<Provider>, Goo
Ok(Provider::new(provider))
}
fn decision_provider(provider: impl GooseDecisionProvider + 'static) -> Arc<DecisionProvider> {
Arc::new(DecisionProvider {
provider: Arc::new(provider),
})
}
#[uniffi::export]
pub fn openrouter_decision_provider(
api_key: String,
base_url: Option<String>,
) -> Result<Arc<DecisionProvider>, GooseError> {
let client = ApiClient::new_with_tls(
base_url.unwrap_or_else(|| "https://openrouter.ai".to_string()),
AuthMethod::BearerToken(api_key),
None,
)?;
Ok(decision_provider(
goose_providers::openrouter::OpenRouterProvider::new(client, None, None),
))
}
#[uniffi::export]
pub fn typesafe_decision_provider(
api_key: String,
base_url: Option<String>,
) -> Result<Arc<DecisionProvider>, GooseError> {
let client = ApiClient::new_with_tls(
base_url.unwrap_or_else(|| goose_providers::typesafe::TYPESAFE_DEFAULT_HOST.to_string()),
AuthMethod::BearerToken(api_key),
None,
)?;
Ok(decision_provider(
goose_providers::typesafe::TypeSafeProvider::new(client),
))
}
#[uniffi::export]
pub fn openrouter_decision_default_model() -> String {
goose_providers::openrouter::OPENROUTER_DECISION_DEFAULT_MODEL.to_string()
}
#[uniffi::export]
pub fn typesafe_decision_default_model() -> String {
goose_providers::typesafe::TYPESAFE_DEFAULT_MODEL.to_string()
}
#[uniffi::export]
pub fn openai_default_model() -> String {
goose_providers::openai::OPEN_AI_DEFAULT_MODEL.to_string()
+313
View File
@@ -208,6 +208,64 @@
"returns": "Provider",
"throws": "GooseError",
"isAsync": false
},
{
"name": "openrouter_decision_default_model",
"docs": "",
"params": [],
"returns": "String",
"throws": null,
"isAsync": false
},
{
"name": "openrouter_decision_provider",
"docs": "",
"params": [
{
"name": "api_key",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "base_url",
"type": "Option<String>",
"default": null,
"docs": ""
}
],
"returns": "DecisionProvider",
"throws": "GooseError",
"isAsync": false
},
{
"name": "typesafe_decision_default_model",
"docs": "",
"params": [],
"returns": "String",
"throws": null,
"isAsync": false
},
{
"name": "typesafe_decision_provider",
"docs": "",
"params": [
{
"name": "api_key",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "base_url",
"type": "Option<String>",
"default": null,
"docs": ""
}
],
"returns": "DecisionProvider",
"throws": "GooseError",
"isAsync": false
}
],
"items": [
@@ -929,6 +987,261 @@
],
"methods": []
},
{
"name": "DecisionQuestion",
"kind": "enum",
"docs": "",
"fields": [],
"variants": [
{
"name": "Noul",
"fields": [
{
"name": "instructions",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "criteria",
"type": "Option<NoulCriteria>",
"default": null,
"docs": ""
}
]
},
{
"name": "Choice",
"fields": [
{
"name": "instructions",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "criteria",
"type": "HashMap<String, String>",
"default": null,
"docs": ""
}
]
},
{
"name": "Score",
"fields": [
{
"name": "instructions",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "criteria",
"type": "Vec<String>",
"default": null,
"docs": ""
}
]
}
],
"methods": []
},
{
"name": "NoulCriteria",
"kind": "record",
"docs": "",
"fields": [
{
"name": "true_description",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "false_description",
"type": "String",
"default": null,
"docs": ""
}
],
"variants": [],
"methods": []
},
{
"name": "DecisionRequest",
"kind": "record",
"docs": "",
"fields": [
{
"name": "model",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "state_json",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "questions",
"type": "HashMap<String, DecisionQuestion>",
"default": null,
"docs": ""
}
],
"variants": [],
"methods": []
},
{
"name": "DecisionAnswer",
"kind": "enum",
"docs": "",
"fields": [],
"variants": [
{
"name": "Noul",
"fields": [
{
"name": "noul",
"type": "f64",
"default": null,
"docs": ""
}
]
},
{
"name": "Choice",
"fields": [
{
"name": "choice",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "confidence",
"type": "f64",
"default": null,
"docs": ""
},
{
"name": "probabilities",
"type": "HashMap<String, f64>",
"default": null,
"docs": ""
}
]
},
{
"name": "Score",
"fields": [
{
"name": "score",
"type": "f64",
"default": null,
"docs": ""
},
{
"name": "confidence",
"type": "f64",
"default": null,
"docs": ""
},
{
"name": "legend_json",
"type": "HashMap<String, String>",
"default": null,
"docs": ""
},
{
"name": "probabilities",
"type": "HashMap<String, f64>",
"default": null,
"docs": ""
}
]
}
],
"methods": []
},
{
"name": "DecisionResponse",
"kind": "record",
"docs": "",
"fields": [
{
"name": "model",
"type": "String",
"default": null,
"docs": ""
},
{
"name": "answers",
"type": "HashMap<String, DecisionAnswer>",
"default": null,
"docs": ""
},
{
"name": "input_tokens",
"type": "Option<u64>",
"default": null,
"docs": ""
},
{
"name": "output_tokens",
"type": "Option<u64>",
"default": null,
"docs": ""
},
{
"name": "cost",
"type": "Option<f64>",
"default": null,
"docs": ""
},
{
"name": "id",
"type": "Option<String>",
"default": null,
"docs": ""
},
{
"name": "provider",
"type": "Option<String>",
"default": null,
"docs": ""
}
],
"variants": [],
"methods": []
},
{
"name": "DecisionProvider",
"kind": "object",
"docs": "",
"fields": [],
"variants": [],
"methods": [
{
"name": "create_decision",
"docs": "",
"params": [
{
"name": "request",
"type": "DecisionRequest",
"default": null,
"docs": ""
}
],
"returns": "DecisionResponse",
"throws": "GooseError",
"isAsync": true
}
]
},
{
"name": "Provider",
"kind": "object",