Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

feat(agy): project per-model options onto the gateway model list

Jason9802d7c0ca0d
diff --git a/src-tauri/src/agy_config.rs b/src-tauri/src/agy_config.rs
index 8695c8589..19445f627 100644
--- a/src-tauri/src/agy_config.rs
+++ b/src-tauri/src/agy_config.rs
@@ -674,8 +674,38 @@ fn read_gateway_settings() -> Result<Option<Value>, AppError> {
.flatten()
.filter_map(|entry| text(entry.get("modelId")).map(Value::String))
.collect();
+ // 条目上 agy 认的模型选项原样读回行的 `modelOptions`(投影的逆向,见 live::project::agy)。
+ let mut model_options = serde_json::Map::new();
+ for entry in models
+ .and_then(|models| models.get("list"))
+ .and_then(Value::as_array)
+ .into_iter()
+ .flatten()
+ {
+ let Some(id) = text(entry.get("modelId")) else {
+ continue;
+ };
+ let mut own = serde_json::Map::new();
+ if let Some(name) = text(entry.get("displayName")) {
+ own.insert("displayName".into(), json!(name));
+ }
+ for key in ["maxTokens", "maxOutputTokens"] {
+ if let Some(value) = entry.get(key).and_then(Value::as_u64).filter(|v| *v > 0) {
+ own.insert(key.into(), json!(value));
+ }
+ }
+ if entry.get("supportsThinking").and_then(Value::as_bool) == Some(true) {
+ own.insert("supportsThinking".into(), json!(true));
+ }
+ if !own.is_empty() {
+ model_options.insert(id, Value::Object(own));
+ }
+ }
if !ids.is_empty() {
options.insert("models".into(), Value::Array(ids));
+ if !model_options.is_empty() {
+ options.insert("modelOptions".into(), Value::Object(model_options));
+ }
if let Some(default) = text(models.and_then(|models| models.get("defaultModelId"))) {
options.insert("defaultModel".into(), json!(default));
}
@@ -1897,7 +1927,7 @@ mod integration_tests {
fs::create_dir_all(customizations_dir()).unwrap();
fs::write(
gateway_config_path(),
- r#"{"gateway":{"enabled":true,"url":"https://relay.test","apiKey":"fixture","wireProtocol":"openai","customHeaders":{"x-goog-api-key":"fixture","X-Team":"dev"}},"models":{"defaultModelId":"b","list":[{"modelId":"a"},{"modelId":"b"}]},"caCertPath":"/ca.pem"}"#,
+ r#"{"gateway":{"enabled":true,"url":"https://relay.test","apiKey":"fixture","wireProtocol":"openai","customHeaders":{"x-goog-api-key":"fixture","X-Team":"dev"}},"models":{"defaultModelId":"b","list":[{"modelId":"a"},{"modelId":"b","displayName":"Bee","maxTokens":200000,"maxOutputTokens":32000,"supportsThinking":true}]},"caCertPath":"/ca.pem"}"#,
)
.unwrap();
let db = Arc::new(Database::memory().unwrap());
@@ -1920,6 +1950,8 @@ mod integration_tests {
"customHeaders": {"x-goog-api-key": "fixture", "X-Team": "dev"},
"models": ["a", "b"],
"defaultModel": "b",
+ "modelOptions": {"b": {"displayName": "Bee", "maxTokens": 200000,
+ "maxOutputTokens": 32000, "supportsThinking": true}},
"caCertPath": "/ca.pem",
},
})
diff --git a/src-tauri/src/live/project/agy.rs b/src-tauri/src/live/project/agy.rs
index 048b3cc24..431ea4aa0 100644
--- a/src-tauri/src/live/project/agy.rs
+++ b/src-tauri/src/live/project/agy.rs
@@ -10,7 +10,10 @@
//! ```json
//! { "env": { "GEMINI_API_KEY": "…", "GOOGLE_GEMINI_BASE_URL": "https://relay.example" },
//! "gateway": { "wireProtocol": "genai", "models": ["gemini-3.8-flash"],
-//! "defaultModel": "gemini-3.8-flash", "customHeaders": {}, "caCertPath": "" } }
+//! "defaultModel": "gemini-3.8-flash",
+//! "modelOptions": { "gemini-3.8-flash": { "displayName": "Flash",
+//! "maxTokens": 1048576, "maxOutputTokens": 65536, "supportsThinking": true } },
+//! "customHeaders": {}, "caCertPath": "" } }
//! ```
//!
//! 旧卡 `config.model.name` 里的是显示名(`Gemini 3.1 Pro (High)`),网关只认模型 ID,不投影。
@@ -189,10 +192,49 @@ fn models_block(options: Option<&Map<String, Value>>) -> Option<Value> {
let default = option_text(options, "defaultModel")
.filter(|id| ids.contains(id))
.unwrap_or(first);
- let list: Vec<Value> = ids.iter().map(|id| json!({ "modelId": id })).collect();
+ let per_model = options.and_then(|options| options.get("modelOptions"));
+ let list: Vec<Value> = ids
+ .iter()
+ .map(|id| model_entry(id, per_model.and_then(|all| all.get(id))))
+ .collect();
Some(json!({ "defaultModelId": default, "list": list }))
}
+/// 一个 `models.list[]` 条目。字段名照抄 agy(实测 1.5.20):
+/// - `displayName`:`agy models` 和选择器里显示的名字。
+/// - `maxTokens` / `maxOutputTokens`:上下文总长和输出上限。agy 不认识的模型族输出只给
+/// 4096;只写输出上限 agy 会直接报错,所以没有上下文时不写它(`agy_direct` 保存时已拦)。
+/// - `supportsThinking`:agy 才给自定义模型开放思考强度(低 / 中 / 高)。认得的模型族
+/// (Gemini 等)不标也有,标了不变。
+fn model_entry(id: &str, options: Option<&Value>) -> Value {
+ let mut entry = Map::new();
+ entry.insert("modelId".into(), json!(id));
+ let options = options.and_then(Value::as_object);
+ if let Some(name) = option_text(options, "displayName") {
+ entry.insert("displayName".into(), json!(name));
+ }
+ let positive = |key: &str| {
+ options
+ .and_then(|options| options.get(key))
+ .and_then(Value::as_u64)
+ .filter(|value| *value > 0)
+ };
+ if let Some(context) = positive("maxTokens") {
+ entry.insert("maxTokens".into(), json!(context));
+ if let Some(output) = positive("maxOutputTokens") {
+ entry.insert("maxOutputTokens".into(), json!(output));
+ }
+ }
+ if options
+ .and_then(|options| options.get("supportsThinking"))
+ .and_then(Value::as_bool)
+ == Some(true)
+ {
+ entry.insert("supportsThinking".into(), json!(true));
+ }
+ Value::Object(entry)
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -252,6 +294,32 @@ mod tests {
assert_eq!(projection.exclusive["caCertPath"], "/etc/ca.pem");
}
+ #[test]
+ fn model_options_project_onto_their_list_entries() {
+ let projection = AgyProjection::of(
+ &relay(json!({
+ "models": ["a", "b", "c"],
+ "modelOptions": {
+ "b": {"displayName": " B ", "maxTokens": 200000, "maxOutputTokens": 32000,
+ "supportsThinking": true},
+ // 没有上下文就不写输出上限:agy 单收输出上限会报错。
+ "c": {"displayName": "", "maxOutputTokens": 8000, "supportsThinking": false},
+ "x": {"displayName": "gone"},
+ },
+ })),
+ false,
+ );
+ assert_eq!(
+ projection.top["models"]["list"],
+ json!([
+ {"modelId": "a"},
+ {"modelId": "b", "displayName": "B", "maxTokens": 200000,
+ "maxOutputTokens": 32000, "supportsThinking": true},
+ {"modelId": "c"},
+ ])
+ );
+ }
+
#[test]
fn default_model_falls_back_to_the_first_listed() {
let projection = AgyProjection::of(
diff --git a/src-tauri/src/services/provider/agy_direct.rs b/src-tauri/src/services/provider/agy_direct.rs
index caf024daf..26e2a3445 100644
--- a/src-tauri/src/services/provider/agy_direct.rs
+++ b/src-tauri/src/services/provider/agy_direct.rs
@@ -410,6 +410,7 @@ fn validate_gateway_options(options: Option<&Value>, has_endpoint: bool) -> Resu
}
Some(_) => return Err(invalid("models must be a list")),
}
+ validate_model_options(options.get("modelOptions"))?;
match options.get("customHeaders") {
None | Some(Value::Null) => {}
Some(Value::Object(headers)) => {
@@ -425,6 +426,92 @@ fn validate_gateway_options(options: Option<&Value>, has_endpoint: bool) -> Resu
Ok(())
}
+/// agy 收到的输出上限超过上下文的一半时,每次对话都报 `maxMessageTokens must be positive`
+/// (实测 agy 1.5.20:64000 / 32000 能用,48000 / 32000 不行);只给输出上限也一样报错。
+const AGY_MIN_CONTEXT_TO_OUTPUT_RATIO: u64 = 2;
+
+/// 行里 `gateway.modelOptions`:模型 ID → agy `models.list[]` 条目上的选项。
+fn validate_model_options(value: Option<&Value>) -> Result<(), AppError> {
+ let invalid = |model: &str, zh: &str, en: &str| {
+ AppError::localized(
+ "agy.gateway.model_options",
+ format!("Antigravity 模型 {model}:{zh}"),
+ format!("Antigravity model {model}: {en}"),
+ )
+ };
+ let Some(all) = value.filter(|value| !value.is_null()) else {
+ return Ok(());
+ };
+ let Some(all) = all.as_object() else {
+ return Err(invalid(
+ "",
+ "模型选项格式不对",
+ "model options must be an object",
+ ));
+ };
+ for (model, options) in all {
+ let Some(options) = options.as_object() else {
+ return Err(invalid(
+ model,
+ "模型选项格式不对",
+ "options must be an object",
+ ));
+ };
+ match options.get("displayName") {
+ None | Some(Value::Null) => {}
+ Some(Value::String(name)) if !name.chars().any(char::is_control) => {}
+ Some(_) => return Err(invalid(model, "显示名无效", "invalid display name")),
+ }
+ let limit = |key: &str, zh: &str, en: &str| match options.get(key) {
+ None | Some(Value::Null) => Ok(None),
+ Some(value) => match value.as_u64().filter(|value| *value > 0) {
+ Some(value) => Ok(Some(value)),
+ None => Err(invalid(model, zh, en)),
+ },
+ };
+ let context = limit(
+ "maxTokens",
+ "上下文窗口要填正整数",
+ "the context window must be a positive integer",
+ )?;
+ let output = limit(
+ "maxOutputTokens",
+ "输出上限要填正整数",
+ "the output limit must be a positive integer",
+ )?;
+ if let Some(output) = output {
+ match context {
+ None => {
+ return Err(invalid(
+ model,
+ "填了输出上限也要填上下文窗口,agy 只收输出上限会报错",
+ "set the context window too; agy fails with an output limit alone",
+ ))
+ }
+ Some(context) if context < output.saturating_mul(AGY_MIN_CONTEXT_TO_OUTPUT_RATIO) => {
+ return Err(invalid(
+ model,
+ "上下文窗口至少要是输出上限的 2 倍,否则 agy 每次对话都会报错",
+ "the context window must be at least twice the output limit, or every agy turn fails",
+ ))
+ }
+ Some(_) => {}
+ }
+ }
+ match options.get("supportsThinking") {
+ None | Some(Value::Null) | Some(Value::Bool(_)) => {}
+ Some(_) => {
+ return Err(invalid(
+ model,
+ "思考强度开关格式不对",
+ "supportsThinking must be a boolean",
+ ))
+ }
+ }
+ }
+ Ok(())
+}
+
/// 在终端里启动这张卡要用的参数;Key 卡会先建好它的私有目录(带它自己的网关)。
///
/// 官方卡跑原生 `agy`:启用的是第三方卡时 `config.json` 开着网关,原生 `agy` 走的是网关,
@@ -537,6 +624,30 @@ mod tests {
assert!(live_settings(&row).is_err());
}
+ #[test]
+ fn agy_model_options_need_a_context_twice_the_output_limit() {
+ let check = |options: Value| {
+ let mut row = provider("Relay");
+ row.settings_config["env"] =
+ json!({"GEMINI_API_KEY": "fixture", "GOOGLE_GEMINI_BASE_URL": "https://r.test"});
+ row.settings_config["gateway"] =
+ json!({"models": ["m"], "modelOptions": {"m": options}});
+ live_settings(&row).is_ok()
+ };
+ assert!(check(json!({"displayName": "M", "supportsThinking": true})));
+ assert!(check(json!({"maxTokens": 64000, "maxOutputTokens": 32000})));
+ assert!(check(json!({"maxTokens": 30000})));
+ assert!(!check(json!({"maxOutputTokens": 32000})));
+ assert!(!check(
+ json!({"maxTokens": 48000, "maxOutputTokens": 32000})
+ ));
+ assert!(!check(json!({"maxTokens": 0})));
+ assert!(!check(json!({"maxTokens": "200000"})));
+ assert!(!check(json!({"displayName": "a\nb"})));
+ assert!(!check(json!({"supportsThinking": "yes"})));
+ assert!(!check(json!("m")));
+ }
+
#[test]
fn agy_invalid_credentials_fail_before_writing() {
let mut row = provider("Relay");