Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent): strip provider prefix from model id sent upstream

thinhlpg5f75425133e4
diff --git a/src-tauri/src/core/agent/loop.rs b/src-tauri/src/core/agent/loop.rs
index 9fc1e893b..7301f3a25 100644
--- a/src-tauri/src/core/agent/loop.rs
+++ b/src-tauri/src/core/agent/loop.rs
@@ -145,6 +145,9 @@ struct HttpModelInvoker {
client: Client,
upstream_url: String,
api_keys: Vec<String>,
+ /// Provider registry, used to strip a `<provider>/` qualifier from the
+ /// request's `model` field (the upstream must receive the bare model id).
+ provider_configs: Arc<Mutex<HashMap<String, ProviderConfig>>>,
/// When the provider fronts a native (non-chat/completions) wire API
/// (Anthropic `/messages`, OpenAI `/responses`, Google `generateContent`),
/// this converter translates the request and decodes the upstream stream
@@ -159,13 +162,25 @@ impl ModelInvoker for HttpModelInvoker {
request: &serde_json::Value,
events: &mpsc::UnboundedSender<StreamEvent>,
) -> Result<serde_json::Value, String> {
+ // `provider/model` is the CLI's explicit selection syntax. The upstream
+ // URL + credential have already been resolved from that qualifier, so
+ // the body must carry the bare model id - providers like OpenCode GO
+ // reject a provider-qualified id with "model not supported".
+ let mut normalized = request.clone();
+ if let Some(model) = normalized.get("model").and_then(|m| m.as_str()) {
+ let pc = self.provider_configs.lock().await;
+ let bare = crate::core::agent::upstream::strip_provider_prefix(model, &pc);
+ if bare != model {
+ normalized["model"] = serde_json::json!(bare);
+ }
+ }
if let Some(converter) = &self.converter {
crate::core::agent::upstream::stream_converted_chat_completions(
&self.client,
&self.upstream_url,
&self.api_keys,
converter.as_ref(),
- request,
+ &normalized,
events,
)
.await
@@ -174,7 +189,7 @@ impl ModelInvoker for HttpModelInvoker {
&self.client,
&self.upstream_url,
&self.api_keys,
- request,
+ &normalized,
events,
)
.await
@@ -1504,6 +1519,7 @@ async fn orchestrate_inner(
client: client.clone(),
upstream_url,
api_keys: session_api_keys,
+ provider_configs: provider_configs.clone(),
converter: resolve_api_type_for_model(&model_id, provider_configs.clone())
.await
.and_then(|(api_type, oauth)| converter_for(Some(&api_type), oauth)),
@@ -1717,6 +1733,7 @@ pub(crate) async fn compact_history(
client: args.client.clone(),
upstream_url,
api_keys,
+ provider_configs: args.provider_configs.clone(),
converter: resolve_api_type_for_model(model_id, args.provider_configs.clone())
.await
.and_then(|(api_type, oauth)| converter_for(Some(&api_type), oauth)),
@@ -1754,6 +1771,7 @@ pub(crate) async fn evaluate_goal(
client: args.client.clone(),
upstream_url,
api_keys,
+ provider_configs: args.provider_configs.clone(),
converter: resolve_api_type_for_model(smol_model_id, args.provider_configs.clone())
.await
.and_then(|(api_type, oauth)| converter_for(Some(&api_type), oauth)),
diff --git a/src-tauri/src/core/agent/upstream.rs b/src-tauri/src/core/agent/upstream.rs
index fc9134fab..b19784f92 100644
--- a/src-tauri/src/core/agent/upstream.rs
+++ b/src-tauri/src/core/agent/upstream.rs
@@ -349,6 +349,27 @@ pub(crate) async fn resolve_upstream_for_model(
Err(format!("No upstream session found for model '{model_id}'"))
}
+/// Strip a leading `<provider>/` qualifier from `model_id` when that prefix
+/// names a configured provider, mirroring the disambiguation in
+/// [`resolve_upstream_for_model`]. `provider/model` is the CLI's explicit
+/// selection syntax; once the upstream URL and credential are resolved the
+/// request body must carry the bare model id, or providers that reject a
+/// provider-qualified id (e.g. OpenCode GO) fail with "model not supported".
+/// A slash inside a real model id (e.g. an org-scoped name) is left alone
+/// unless the leading segment is literally a provider key.
+pub(crate) fn strip_provider_prefix(
+ model_id: &str,
+ provider_configs: &HashMap<String, ProviderConfig>,
+) -> String {
+ if let Some(sep_pos) = model_id.find('/') {
+ let potential_provider: &str = &model_id[..sep_pos];
+ if provider_configs.contains_key(potential_provider) {
+ return model_id[sep_pos + 1..].to_string();
+ }
+ }
+ model_id.to_string()
+}
+
/// Resolve the wire API for `model_id`'s provider, mirroring the
/// model-to-provider lookup in [`resolve_upstream_for_model`]. Returns the
@@ -1393,6 +1414,51 @@ mod tests {
) {
mpsc::unbounded_channel()
}
+ fn provider_configs(map: &[(&str, &str)]) -> HashMap<String, ProviderConfig> {
+ map.iter()
+ .map(|(name, url)| {
+ (
+ name.to_string(),
+ ProviderConfig {
+ provider: name.to_string(),
+ base_url: Some(url.to_string()),
+ models: vec![],
+ ..Default::default()
+ },
+ )
+ })
+ .collect()
+ }
+
+ /// A `<provider>/<model>` id resolves to the bare model once the prefix
+ /// names a configured provider - the body the upstream receives must not
+ /// carry the selection qualifier.
+ #[test]
+ fn provider_prefix_is_stripped_when_it_names_a_provider() {
+ let pc = provider_configs(&[("opencode", "https://opencode.ai/zen/go/v1")]);
+ assert_eq!(
+ strip_provider_prefix("opencode/gpt-5.6-luna", &pc),
+ "gpt-5.6-luna"
+ );
+ }
+
+ /// A slash inside a real model id is preserved when the leading segment is
+ /// not a provider key, so org-scoped ids survive untouched.
+ #[test]
+ fn unknown_slash_prefix_is_left_alone() {
+ let pc = provider_configs(&[("opencode", "https://opencode.ai/zen/go/v1")]);
+ assert_eq!(
+ strip_provider_prefix("mistral-technologies/mixtral", &pc),
+ "mistral-technologies/mixtral"
+ );
+ }
+
+ /// A bare model id (no slash) is returned unchanged.
+ #[test]
+ fn bare_model_id_is_unchanged() {
+ let pc = provider_configs(&[("opencode", "https://opencode.ai/zen/go/v1")]);
+ assert_eq!(strip_provider_prefix("gpt-5.6-luna", &pc), "gpt-5.6-luna");
+ }
/// `reqwest` prints only its own layer, so the cause chain is where the
/// actual failure lives -- the whole point of `describe_request_error`.
diff --git a/src-tauri/src/core/server/proxy.rs b/src-tauri/src/core/server/proxy.rs
index 476628efa..de1643551 100644
--- a/src-tauri/src/core/server/proxy.rs
+++ b/src-tauri/src/core/server/proxy.rs
@@ -1326,6 +1326,13 @@ async fn proxy_request(
return Ok(error_response.body(full(e)).unwrap());
}
};
+ // `provider/model` records the provider qualifier used to resolve
+ // the upstream above; the request body must carry the bare model id.
+ let body_model_id = {
+ let pc = provider_configs.lock().await;
+ crate::core::agent::upstream::strip_provider_prefix(&model_id, &pc)
+ };
+
let max_turns = json_body
.get("max_turns")
@@ -1338,7 +1345,7 @@ async fn proxy_request(
for _turn in 0..max_turns {
// Build upstream request body for each turn so messages are updated.
let mut completion_map = serde_json::Map::new();
- completion_map.insert("model".to_string(), serde_json::json!(model_id));
+ completion_map.insert("model".to_string(), serde_json::json!(body_model_id));
completion_map.insert(
"messages".to_string(),
serde_json::Value::Array(conversation_messages.clone()),