Refactor tool execution pipeline
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
//! weather — 和风天气
|
||||
|
||||
const HOST: &str = "https://devapi.qweather.com/v7";
|
||||
|
||||
fn main() {
|
||||
let input: serde_json::Value = serde_json::from_reader(std::io::stdin()).unwrap();
|
||||
let mode = std::env::args().nth(2).unwrap_or_else(|| "now".to_string());
|
||||
let loc = input["params"]["location"].as_str().unwrap_or("北京");
|
||||
let key = match std::env::var("QWEATHER_KEY") {
|
||||
Ok(k) if !k.is_empty() => k,
|
||||
_ => {
|
||||
println!(r#"{{"type":"result","content":"错误: 缺少环境变量 QWEATHER_KEY(和风天气 API Key),请在 .env 中设置"}}"#);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if input.get("response").is_none() && input.get("db_response").is_none() {
|
||||
let (path, desc) = match mode.as_str() {
|
||||
"now" => ("weather/now", "实时天气"),
|
||||
"forecast" => ("weather/7d", "7天预报"),
|
||||
"hourly" => ("weather/24h", "24h逐时"),
|
||||
_ => { println!(r#"{{"type":"result","content":"未知模式"}}"#); return; }
|
||||
};
|
||||
println!("{}", serde_json::json!({
|
||||
"type":"http","method":"GET",
|
||||
"url":format!("{HOST}/{path}?location={loc}&key={key}"),
|
||||
"desc":desc
|
||||
}));
|
||||
} else {
|
||||
let body = &input["response"]["body"];
|
||||
let ok = body["code"].as_str() == Some("200");
|
||||
let content = if !ok { "查询失败".into() } else { match mode.as_str() {
|
||||
"now" => format!("{} {}°C(体感{}°C) {}风{}级 湿度{}%",
|
||||
body["now"]["text"].as_str().unwrap_or("?"), body["now"]["temp"].as_str().unwrap_or("?"),
|
||||
body["now"]["feelsLike"].as_str().unwrap_or("?"), body["now"]["windDir"].as_str().unwrap_or("?"),
|
||||
body["now"]["windScale"].as_str().unwrap_or("?"), body["now"]["humidity"].as_str().unwrap_or("?")),
|
||||
"forecast" => body["daily"].as_array().unwrap_or(&vec![]).iter().take(7)
|
||||
.map(|d| format!("{}: {}~{}°C", d["fxDate"].as_str().unwrap_or(""),
|
||||
d["tempMin"].as_str().unwrap_or(""), d["tempMax"].as_str().unwrap_or("")))
|
||||
.collect::<Vec<_>>().join("\n"),
|
||||
"hourly" => body["hourly"].as_array().unwrap_or(&vec![]).iter().take(12)
|
||||
.map(|h| format!("{}: {} {}°C", h["fxTime"].as_str().unwrap_or(""),
|
||||
h["text"].as_str().unwrap_or(""), h["temp"].as_str().unwrap_or("")))
|
||||
.collect::<Vec<_>>().join("\n"),
|
||||
_ => String::new(),
|
||||
}};
|
||||
println!("{}", serde_json::json!({"type":"result","content":content}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user