Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion openplanter-desktop/crates/op-core/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ static OPENAI_RE: LazyLock<Regex> =
static CEREBRAS_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?i)^(llama.*cerebras|qwen-3|gpt-oss|zai-glm)").unwrap());

static UPSTAGE_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?i)^solar").unwrap());

// Ollama regex: `qwen` without lookahead — Cerebras check runs first, so
// `qwen-3*` is already caught before we reach this regex.
static OLLAMA_RE: LazyLock<Regex> = LazyLock::new(|| {
Expand All @@ -49,6 +52,9 @@ pub fn infer_provider_for_model(model: &str) -> Option<&'static str> {
if CEREBRAS_RE.is_match(model) {
return Some("cerebras");
}
if UPSTAGE_RE.is_match(model) {
return Some("upstage");
}
if OPENAI_RE.is_match(model) {
return Some("openai");
}
Expand Down Expand Up @@ -117,6 +123,7 @@ pub fn resolve_provider(cfg: &AgentConfig) -> Result<String, ModelError> {
("openai", &cfg.openai_api_key),
("openrouter", &cfg.openrouter_api_key),
("cerebras", &cfg.cerebras_api_key),
("upstage", &cfg.upstage_api_key),
("ollama", &None), // ollama is always last — no key needed
];

Expand Down Expand Up @@ -190,6 +197,19 @@ pub fn resolve_endpoint(
})?;
Ok((cfg.cerebras_base_url.clone(), key.to_string()))
}
"upstage" => {
let key = cfg
.upstage_api_key
.as_deref()
.or(cfg.api_key.as_deref())
.filter(|k| !k.is_empty())
.ok_or_else(|| {
ModelError::Message(
"No Upstage API key. Set UPSTAGE_API_KEY or OPENPLANTER_UPSTAGE_API_KEY.".into(),
)
})?;
Ok((cfg.upstage_base_url.clone(), key.to_string()))
}
"ollama" => {
// Ollama doesn't need a real key — use a dummy
Ok((cfg.ollama_base_url.clone(), "ollama".to_string()))
Expand All @@ -213,7 +233,7 @@ pub fn build_model(cfg: &AgentConfig) -> Result<Box<dyn BaseModel>, ModelError>
cfg.reasoning_effort.clone(),
))),
_ => {
// OpenAI-compatible: openai, openrouter, cerebras, ollama
// OpenAI-compatible: openai, openrouter, cerebras, upstage, ollama
let mut extra_headers = HashMap::new();
if provider == "openrouter" {
extra_headers.insert(
Expand Down Expand Up @@ -282,6 +302,13 @@ mod tests {
);
}

#[test]
fn test_infer_upstage() {
assert_eq!(infer_provider_for_model("solar-pro3"), Some("upstage"));
assert_eq!(infer_provider_for_model("solar-pro2"), Some("upstage"));
assert_eq!(infer_provider_for_model("solar-mini"), Some("upstage"));
}

#[test]
fn test_infer_ollama() {
assert_eq!(infer_provider_for_model("llama3.2"), Some("ollama"));
Expand Down
14 changes: 14 additions & 0 deletions openplanter-desktop/crates/op-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub static PROVIDER_DEFAULT_MODELS: LazyLock<HashMap<&'static str, &'static str>
("anthropic", "claude-opus-4-6"),
("openrouter", "anthropic/claude-sonnet-4-5"),
("cerebras", "qwen-3-235b-a22b-instruct-2507"),
("upstage", "solar-pro3"),
("ollama", "llama3.2"),
])
});
Expand Down Expand Up @@ -55,6 +56,7 @@ pub struct AgentConfig {
pub anthropic_base_url: String,
pub openrouter_base_url: String,
pub cerebras_base_url: String,
pub upstage_base_url: String,
pub ollama_base_url: String,
pub exa_base_url: String,

Expand All @@ -64,6 +66,7 @@ pub struct AgentConfig {
pub anthropic_api_key: Option<String>,
pub openrouter_api_key: Option<String>,
pub cerebras_api_key: Option<String>,
pub upstage_api_key: Option<String>,
pub exa_api_key: Option<String>,
pub voyage_api_key: Option<String>,

Expand Down Expand Up @@ -100,13 +103,15 @@ impl Default for AgentConfig {
anthropic_base_url: "https://api.anthropic.com/v1".into(),
openrouter_base_url: "https://openrouter.ai/api/v1".into(),
cerebras_base_url: "https://api.cerebras.ai/v1".into(),
upstage_base_url: "https://api.upstage.ai/v1".into(),
ollama_base_url: "http://localhost:11434/v1".into(),
exa_base_url: "https://api.exa.ai".into(),
api_key: None,
openai_api_key: None,
anthropic_api_key: None,
openrouter_api_key: None,
cerebras_api_key: None,
upstage_api_key: None,
exa_api_key: None,
voyage_api_key: None,
max_depth: 4,
Expand Down Expand Up @@ -148,6 +153,9 @@ impl AgentConfig {
let cerebras_api_key = env_opt("OPENPLANTER_CEREBRAS_API_KEY")
.or_else(|| env_opt("CEREBRAS_API_KEY"));

let upstage_api_key = env_opt("OPENPLANTER_UPSTAGE_API_KEY")
.or_else(|| env_opt("UPSTAGE_API_KEY"));

let exa_api_key = env_opt("OPENPLANTER_EXA_API_KEY")
.or_else(|| env_opt("EXA_API_KEY"));

Expand Down Expand Up @@ -196,6 +204,10 @@ impl AgentConfig {
"OPENPLANTER_CEREBRAS_BASE_URL",
"https://api.cerebras.ai/v1",
),
upstage_base_url: env_or(
"OPENPLANTER_UPSTAGE_BASE_URL",
"https://api.upstage.ai/v1",
),
ollama_base_url: env_or(
"OPENPLANTER_OLLAMA_BASE_URL",
"http://localhost:11434/v1",
Expand All @@ -205,6 +217,7 @@ impl AgentConfig {
anthropic_api_key,
openrouter_api_key,
cerebras_api_key,
upstage_api_key,
exa_api_key,
voyage_api_key,
max_depth: env_int("OPENPLANTER_MAX_DEPTH", 4),
Expand Down Expand Up @@ -283,6 +296,7 @@ mod tests {
PROVIDER_DEFAULT_MODELS.get("cerebras"),
Some(&"qwen-3-235b-a22b-instruct-2507")
);
assert_eq!(PROVIDER_DEFAULT_MODELS.get("upstage"), Some(&"solar-pro3"));
assert_eq!(PROVIDER_DEFAULT_MODELS.get("ollama"), Some(&"llama3.2"));
}

Expand Down
13 changes: 12 additions & 1 deletion openplanter-desktop/crates/op-core/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ pub struct CredentialBundle {
pub anthropic_api_key: Option<String>,
pub openrouter_api_key: Option<String>,
pub cerebras_api_key: Option<String>,
pub upstage_api_key: Option<String>,
pub exa_api_key: Option<String>,
pub voyage_api_key: Option<String>,
}

impl CredentialBundle {
/// Returns `true` if any key has a non-empty value.
pub fn has_any(&self) -> bool {
let keys: [&Option<String>; 6] = [
let keys: [&Option<String>; 7] = [
&self.openai_api_key,
&self.anthropic_api_key,
&self.openrouter_api_key,
&self.cerebras_api_key,
&self.upstage_api_key,
&self.exa_api_key,
&self.voyage_api_key,
];
Expand All @@ -51,6 +53,7 @@ impl CredentialBundle {
fill!(anthropic_api_key);
fill!(openrouter_api_key);
fill!(cerebras_api_key);
fill!(upstage_api_key);
fill!(exa_api_key);
fill!(voyage_api_key);
}
Expand All @@ -69,6 +72,7 @@ impl CredentialBundle {
add!(anthropic_api_key, "anthropic_api_key");
add!(openrouter_api_key, "openrouter_api_key");
add!(cerebras_api_key, "cerebras_api_key");
add!(upstage_api_key, "upstage_api_key");
add!(exa_api_key, "exa_api_key");
add!(voyage_api_key, "voyage_api_key");
out
Expand All @@ -87,6 +91,7 @@ impl CredentialBundle {
anthropic_api_key: get_str(payload, "anthropic_api_key"),
openrouter_api_key: get_str(payload, "openrouter_api_key"),
cerebras_api_key: get_str(payload, "cerebras_api_key"),
upstage_api_key: get_str(payload, "upstage_api_key"),
exa_api_key: get_str(payload, "exa_api_key"),
voyage_api_key: get_str(payload, "voyage_api_key"),
}
Expand Down Expand Up @@ -151,6 +156,11 @@ pub fn parse_env_file(path: &Path) -> CredentialBundle {
"CEREBRAS_API_KEY",
"OPENPLANTER_CEREBRAS_API_KEY",
),
upstage_api_key: get_key(
&env_map,
"UPSTAGE_API_KEY",
"OPENPLANTER_UPSTAGE_API_KEY",
),
exa_api_key: get_key(&env_map, "EXA_API_KEY", "OPENPLANTER_EXA_API_KEY"),
voyage_api_key: get_key(&env_map, "VOYAGE_API_KEY", "OPENPLANTER_VOYAGE_API_KEY"),
}
Expand All @@ -171,6 +181,7 @@ pub fn credentials_from_env() -> CredentialBundle {
anthropic_api_key: env_key("OPENPLANTER_ANTHROPIC_API_KEY", "ANTHROPIC_API_KEY"),
openrouter_api_key: env_key("OPENPLANTER_OPENROUTER_API_KEY", "OPENROUTER_API_KEY"),
cerebras_api_key: env_key("OPENPLANTER_CEREBRAS_API_KEY", "CEREBRAS_API_KEY"),
upstage_api_key: env_key("OPENPLANTER_UPSTAGE_API_KEY", "UPSTAGE_API_KEY"),
exa_api_key: env_key("OPENPLANTER_EXA_API_KEY", "EXA_API_KEY"),
voyage_api_key: env_key("OPENPLANTER_VOYAGE_API_KEY", "VOYAGE_API_KEY"),
}
Expand Down
2 changes: 1 addition & 1 deletion openplanter-desktop/crates/op-core/src/model/openai.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// OpenAI-compatible model implementation.
//
// Handles openai, openrouter, cerebras, and ollama — all use /chat/completions.
// Handles openai, openrouter, cerebras, upstage, and ollama — all use /chat/completions.

use std::collections::HashMap;

Expand Down
5 changes: 5 additions & 0 deletions openplanter-desktop/crates/op-core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct PersistentSettings {
pub default_model_anthropic: Option<String>,
pub default_model_openrouter: Option<String>,
pub default_model_cerebras: Option<String>,
pub default_model_upstage: Option<String>,
pub default_model_ollama: Option<String>,
}

Expand All @@ -49,6 +50,7 @@ impl PersistentSettings {
"anthropic" => self.default_model_anthropic.as_deref(),
"openrouter" => self.default_model_openrouter.as_deref(),
"cerebras" => self.default_model_cerebras.as_deref(),
"upstage" => self.default_model_upstage.as_deref(),
"ollama" => self.default_model_ollama.as_deref(),
_ => None,
};
Expand Down Expand Up @@ -84,6 +86,7 @@ impl PersistentSettings {
default_model_anthropic: trim_opt(&self.default_model_anthropic),
default_model_openrouter: trim_opt(&self.default_model_openrouter),
default_model_cerebras: trim_opt(&self.default_model_cerebras),
default_model_upstage: trim_opt(&self.default_model_upstage),
default_model_ollama: trim_opt(&self.default_model_ollama),
})
}
Expand All @@ -104,6 +107,7 @@ impl PersistentSettings {
add!(default_model_anthropic, "default_model_anthropic");
add!(default_model_openrouter, "default_model_openrouter");
add!(default_model_cerebras, "default_model_cerebras");
add!(default_model_upstage, "default_model_upstage");
add!(default_model_ollama, "default_model_ollama");
payload
}
Expand All @@ -129,6 +133,7 @@ impl PersistentSettings {
default_model_anthropic: get_str(obj, "default_model_anthropic"),
default_model_openrouter: get_str(obj, "default_model_openrouter"),
default_model_cerebras: get_str(obj, "default_model_cerebras"),
default_model_upstage: get_str(obj, "default_model_upstage"),
default_model_ollama: get_str(obj, "default_model_ollama"),
};
settings.normalized()
Expand Down
56 changes: 29 additions & 27 deletions openplanter-desktop/crates/op-core/src/tools/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,26 +365,32 @@ fn strict_fixup(schema: &mut Value) {
}
}

/// Convert to OpenAI tools format: `[{ type: "function", function: { name, description, parameters, strict } }]`
pub fn to_openai_tools() -> Vec<Value> {
mvp_tool_defs()
.into_iter()
fn openai_tools_from(defs: Vec<ToolDef>, strict: bool) -> Vec<Value> {
defs.into_iter()
.map(|def| {
let mut params = def.parameters;
strict_fixup(&mut params);
let mut function = json!({
"name": def.name,
"description": def.description,
});
if strict {
strict_fixup(&mut params);
function["strict"] = json!(true);
}
function["parameters"] = params;
json!({
"type": "function",
"function": {
"name": def.name,
"description": def.description,
"parameters": params,
"strict": true
}
"function": function
})
})
.collect()
}

/// Convert to OpenAI tools format: `[{ type: "function", function: { name, description, parameters, strict } }]`
pub fn to_openai_tools() -> Vec<Value> {
openai_tools_from(mvp_tool_defs(), true)
}

/// Convert to Anthropic tools format: `[{ name, description, input_schema }]`
pub fn to_anthropic_tools() -> Vec<Value> {
mvp_tool_defs()
Expand All @@ -403,6 +409,7 @@ pub fn to_anthropic_tools() -> Vec<Value> {
pub fn build_tool_defs(provider: &str) -> Vec<Value> {
match provider {
"anthropic" => to_anthropic_tools(),
"upstage" => openai_tools_from(mvp_tool_defs(), false),
_ => to_openai_tools(),
}
}
Expand Down Expand Up @@ -434,22 +441,7 @@ pub fn build_curator_tool_defs(provider: &str) -> Vec<Value> {
})
})
.collect(),
_ => filtered
.into_iter()
.map(|def| {
let mut params = def.parameters;
strict_fixup(&mut params);
json!({
"type": "function",
"function": {
"name": def.name,
"description": def.description,
"parameters": params,
"strict": true
}
})
})
.collect(),
_ => openai_tools_from(filtered, provider != "upstage"),
}
}

Expand Down Expand Up @@ -520,6 +512,16 @@ mod tests {
assert_eq!(tools[0]["type"], "function");
}

#[test]
fn test_build_tool_defs_upstage_no_strict() {
// Upstage rejects function.strict=true with HTTP 400
let tools = build_tool_defs("upstage");
assert_eq!(tools[0]["type"], "function");
assert!(tools[0]["function"].get("strict").is_none());
let curator = build_curator_tool_defs("upstage");
assert!(curator[0]["function"].get("strict").is_none());
}

#[test]
fn test_strict_fixup_wraps_optional_with_anyof() {
// list_files has only optional "glob" parameter
Expand Down
Loading