This commit is contained in:
Aurélien Tollard 2025-08-25 12:19:14 -04:00 committed by GitHub
commit c571fbd443
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 51 additions and 2 deletions

1
Cargo.lock generated
View file

@ -11192,6 +11192,7 @@ dependencies = [
"schemars",
"serde",
"serde_json",
"util",
"workspace-hack",
]

View file

@ -4,7 +4,8 @@ use credentials_provider::CredentialsProvider;
use editor::{Editor, EditorElement, EditorStyle};
use futures::{FutureExt, Stream, StreamExt, future::BoxFuture};
use gpui::{
AnyView, App, AsyncApp, Context, Entity, FontStyle, Subscription, Task, TextStyle, WhiteSpace,
AnyView, App, AppContext, AsyncApp, Context, Entity, FontStyle, Subscription, Task, TextStyle,
WhiteSpace,
};
use http_client::HttpClient;
use language_model::{
@ -15,7 +16,8 @@ use language_model::{
LanguageModelToolUse, MessageContent, RateLimiter, Role, StopReason, TokenUsage,
};
use open_router::{
Model, ModelMode as OpenRouterModelMode, ResponseStreamEvent, list_models, stream_completion,
Model, ModelMode as OpenRouterModelMode, Provider, ResponseStreamEvent, list_models,
stream_completion,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
@ -48,6 +50,7 @@ pub struct AvailableModel {
pub supports_tools: Option<bool>,
pub supports_images: Option<bool>,
pub mode: Option<ModelMode>,
pub provider: Option<Provider>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
@ -278,6 +281,7 @@ impl LanguageModelProvider for OpenRouterLanguageModelProvider {
supports_tools: model.supports_tools,
supports_images: model.supports_images,
mode: model.mode.clone().unwrap_or_default().into(),
provider: model.provider.clone(),
});
}
@ -556,6 +560,7 @@ pub fn into_open_router(
LanguageModelToolChoice::Any => open_router::ToolChoice::Required,
LanguageModelToolChoice::None => open_router::ToolChoice::None,
}),
provider: model.provider.clone(),
}
}

View file

@ -23,3 +23,4 @@ schemars = { workspace = true, optional = true }
serde.workspace = true
serde_json.workspace = true
workspace-hack.workspace = true
util.workspace = true

View file

@ -4,6 +4,7 @@ use http_client::{AsyncBody, HttpClient, Method, Request as HttpRequest};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::convert::TryFrom;
use util::serde::default_true;
pub const OPEN_ROUTER_API_URL: &str = "https://openrouter.ai/api/v1";
@ -11,6 +12,41 @@ fn is_none_or_empty<T: AsRef<[U]>, U>(opt: &Option<T>) -> bool {
opt.as_ref().is_none_or(|v| v.as_ref().is_empty())
}
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DataCollection {
Allow,
Disallow,
}
impl Default for DataCollection {
fn default() -> Self {
Self::Allow
}
}
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Provider {
#[serde(skip_serializing_if = "Option::is_none")]
order: Option<Vec<String>>,
#[serde(default = "default_true")]
allow_fallbacks: bool,
#[serde(default)]
require_parameters: bool,
#[serde(default)]
data_collection: DataCollection,
#[serde(skip_serializing_if = "Option::is_none")]
only: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
ignore: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
quantizations: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
sort: Option<String>,
}
#[derive(Clone, Copy, Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Role {
@ -55,6 +91,7 @@ pub struct Model {
pub supports_images: Option<bool>,
#[serde(default)]
pub mode: ModelMode,
pub provider: Option<Provider>,
}
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
@ -76,6 +113,7 @@ impl Model {
Some(true),
Some(false),
Some(ModelMode::Default),
None,
)
}
@ -90,6 +128,7 @@ impl Model {
supports_tools: Option<bool>,
supports_images: Option<bool>,
mode: Option<ModelMode>,
provider: Option<Provider>,
) -> Self {
Self {
name: name.to_owned(),
@ -98,6 +137,7 @@ impl Model {
supports_tools,
supports_images,
mode: mode.unwrap_or(ModelMode::Default),
provider,
}
}
@ -145,6 +185,7 @@ pub struct Request {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reasoning: Option<Reasoning>,
pub usage: RequestUsage,
pub provider: Option<Provider>,
}
#[derive(Debug, Default, Serialize, Deserialize)]
@ -632,6 +673,7 @@ pub async fn list_models(client: &dyn HttpClient, api_url: &str) -> Result<Vec<M
} else {
ModelMode::Default
},
provider: None,
})
.collect();