fix config
This commit is contained in:
@@ -233,11 +233,9 @@ fn save_state(state: &BotState) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load admin DID from config.json
|
/// Load admin DID from config.json ($cfg first, then fallback)
|
||||||
fn load_admin_did(config_path: &str) -> Result<String> {
|
fn load_admin_did() -> Result<String> {
|
||||||
let content = fs::read_to_string(config_path)
|
let config = token::load_config()?;
|
||||||
.with_context(|| format!("Config file not found: {}", config_path))?;
|
|
||||||
let config: Value = serde_json::from_str(&content)?;
|
|
||||||
config["did"]
|
config["did"]
|
||||||
.as_str()
|
.as_str()
|
||||||
.map(|s| s.to_string())
|
.map(|s| s.to_string())
|
||||||
@@ -342,8 +340,8 @@ async fn post_reply(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Main bot entry point
|
/// Main bot entry point
|
||||||
pub async fn start(interval_secs: u64, config_path: &str) -> Result<()> {
|
pub async fn start(interval_secs: u64) -> Result<()> {
|
||||||
let admin_did = load_admin_did(config_path)?;
|
let admin_did = load_admin_did()?;
|
||||||
eprintln!("bot: admin DID = {}", admin_did);
|
eprintln!("bot: admin DID = {}", admin_did);
|
||||||
eprintln!("bot: polling interval = {}s", interval_secs);
|
eprintln!("bot: polling interval = {}s", interval_secs);
|
||||||
|
|
||||||
@@ -564,10 +562,8 @@ fn save_chat_state(state: &ChatBotState) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Load chat proxy from config.json network field
|
/// Load chat proxy from config.json network field
|
||||||
fn load_chat_proxy(config_path: &str) -> Result<String> {
|
fn load_chat_proxy() -> Result<String> {
|
||||||
let content = fs::read_to_string(config_path)
|
let config = token::load_config()?;
|
||||||
.with_context(|| format!("Config file not found: {}", config_path))?;
|
|
||||||
let config: Value = serde_json::from_str(&content)?;
|
|
||||||
if let Some(network) = config["network"].as_str() {
|
if let Some(network) = config["network"].as_str() {
|
||||||
Ok(format!("did:web:bsky.{}#bsky_chat", network))
|
Ok(format!("did:web:bsky.{}#bsky_chat", network))
|
||||||
} else {
|
} else {
|
||||||
@@ -578,9 +574,9 @@ fn load_chat_proxy(config_path: &str) -> Result<String> {
|
|||||||
// Chat API uses XrpcClient with atproto-proxy header via query_auth_proxy / call_proxy
|
// Chat API uses XrpcClient with atproto-proxy header via query_auth_proxy / call_proxy
|
||||||
|
|
||||||
/// Main chat bot entry point
|
/// Main chat bot entry point
|
||||||
pub async fn start_chat(interval_secs: u64, config_path: &str) -> Result<()> {
|
pub async fn start_chat(interval_secs: u64) -> Result<()> {
|
||||||
let admin_did = load_admin_did(config_path)?;
|
let admin_did = load_admin_did()?;
|
||||||
let proxy_did = load_chat_proxy(config_path)?;
|
let proxy_did = load_chat_proxy()?;
|
||||||
eprintln!("chat-bot: admin DID = {}", admin_did);
|
eprintln!("chat-bot: admin DID = {}", admin_did);
|
||||||
eprintln!("chat-bot: proxy = {}", proxy_did);
|
eprintln!("chat-bot: proxy = {}", proxy_did);
|
||||||
eprintln!("chat-bot: polling interval = {}s", interval_secs);
|
eprintln!("chat-bot: polling interval = {}s", interval_secs);
|
||||||
|
|||||||
@@ -8,38 +8,12 @@ use std::io::{self, Write};
|
|||||||
|
|
||||||
use super::token::{self, Session, BUNDLE_ID};
|
use super::token::{self, Session, BUNDLE_ID};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct SiteConfig {
|
|
||||||
#[serde(rename = "siteUrl")]
|
|
||||||
site_url: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_site_url() -> Result<String> {
|
fn load_site_url() -> Result<String> {
|
||||||
// 1. Try public/config.json in current directory
|
let config = super::token::load_config()?;
|
||||||
let local_path = std::path::Path::new("public/config.json");
|
config["siteUrl"]
|
||||||
if local_path.exists() {
|
.as_str()
|
||||||
let content = std::fs::read_to_string(local_path)?;
|
.map(|s| s.trim_end_matches('/').to_string())
|
||||||
let config: SiteConfig = serde_json::from_str(&content)?;
|
.context("No siteUrl found in config.json. Run 'ailog setup' first.")
|
||||||
if let Some(url) = config.site_url {
|
|
||||||
return Ok(url.trim_end_matches('/').to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Fallback to ~/.config/ai.syui.log/config.json
|
|
||||||
if let Some(cfg_dir) = dirs::config_dir() {
|
|
||||||
let cfg_path = cfg_dir.join(BUNDLE_ID).join("config.json");
|
|
||||||
if cfg_path.exists() {
|
|
||||||
let content = std::fs::read_to_string(&cfg_path)?;
|
|
||||||
let config: SiteConfig = serde_json::from_str(&content)?;
|
|
||||||
if let Some(url) = config.site_url {
|
|
||||||
return Ok(url.trim_end_matches('/').to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
anyhow::bail!(
|
|
||||||
"No siteUrl found. Create public/config.json or run ailog oauth with --client-id"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,10 +29,9 @@ pub async fn sync_to_local(
|
|||||||
collection.to_string(),
|
collection.to_string(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
// User mode: use config.json
|
// User mode: use config.json ($cfg first, then public/config.json)
|
||||||
let config_content =
|
let config_value = super::token::load_config()?;
|
||||||
fs::read_to_string("public/config.json").context("config.json not found")?;
|
let config: Config = serde_json::from_value(config_value)?;
|
||||||
let config: Config = serde_json::from_str(&config_content)?;
|
|
||||||
|
|
||||||
println!("Syncing data for {}", config.handle);
|
println!("Syncing data for {}", config.handle);
|
||||||
|
|
||||||
|
|||||||
@@ -70,3 +70,33 @@ pub fn save_bot_session(session: &Session) -> Result<()> {
|
|||||||
fs::write(&path, content)?;
|
fs::write(&path, content)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get config.json path: $cfg/ai.syui.log/config.json
|
||||||
|
pub fn config_path() -> Result<PathBuf> {
|
||||||
|
let cfg_dir = dirs::config_dir()
|
||||||
|
.context("Could not find config directory")?
|
||||||
|
.join(BUNDLE_ID);
|
||||||
|
Ok(cfg_dir.join("config.json"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Load config.json: $cfg/ai.syui.log/config.json (primary), public/config.json (fallback)
|
||||||
|
pub fn load_config() -> Result<serde_json::Value> {
|
||||||
|
// 1. $cfg/ai.syui.log/config.json
|
||||||
|
if let Ok(cfg_path) = config_path() {
|
||||||
|
if cfg_path.exists() {
|
||||||
|
let content = fs::read_to_string(&cfg_path)?;
|
||||||
|
return Ok(serde_json::from_str(&content)?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Fallback: public/config.json (for backward compat)
|
||||||
|
let local_path = std::path::Path::new("public/config.json");
|
||||||
|
if local_path.exists() {
|
||||||
|
let content = fs::read_to_string(local_path)?;
|
||||||
|
return Ok(serde_json::from_str(&content)?);
|
||||||
|
}
|
||||||
|
|
||||||
|
anyhow::bail!(
|
||||||
|
"config.json not found. Run 'ailog setup' first."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
14
src/main.rs
14
src/main.rs
@@ -287,18 +287,12 @@ enum BotCommands {
|
|||||||
/// Poll interval in seconds
|
/// Poll interval in seconds
|
||||||
#[arg(short, long, default_value = "30")]
|
#[arg(short, long, default_value = "30")]
|
||||||
interval: u64,
|
interval: u64,
|
||||||
/// Path to config.json
|
|
||||||
#[arg(short, long, default_value = "public/config.json")]
|
|
||||||
config: String,
|
|
||||||
},
|
},
|
||||||
/// Start the DM chat bot (poll chat messages and reply)
|
/// Start the DM chat bot (poll chat messages and reply)
|
||||||
Chat {
|
Chat {
|
||||||
/// Poll interval in seconds
|
/// Poll interval in seconds
|
||||||
#[arg(short, long, default_value = "30")]
|
#[arg(short, long, default_value = "30")]
|
||||||
interval: u64,
|
interval: u64,
|
||||||
/// Path to config.json
|
|
||||||
#[arg(short, long, default_value = "public/config.json")]
|
|
||||||
config: String,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -418,11 +412,11 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
Commands::Bot { command } => {
|
Commands::Bot { command } => {
|
||||||
match command {
|
match command {
|
||||||
BotCommands::Start { interval, config } => {
|
BotCommands::Start { interval } => {
|
||||||
commands::bot::start(interval, &config).await?;
|
commands::bot::start(interval).await?;
|
||||||
}
|
}
|
||||||
BotCommands::Chat { interval, config } => {
|
BotCommands::Chat { interval } => {
|
||||||
commands::bot::start_chat(interval, &config).await?;
|
commands::bot::start_chat(interval).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user