diff --git a/src/commands/token.rs b/src/commands/token.rs index 9bc99ce..6e69b61 100644 --- a/src/commands/token.rs +++ b/src/commands/token.rs @@ -37,13 +37,30 @@ pub fn bot_token_path() -> Result { Ok(config_dir.join("bot.json")) } -/// Load session from token file +/// Load session from token file, fallback to oauth_session.json pub fn load_session() -> Result { let path = token_path()?; - let content = fs::read_to_string(&path) - .with_context(|| format!("Token file not found: {:?}. Run 'ailog login' first.", path))?; - let session: Session = serde_json::from_str(&content)?; - Ok(session) + if let Ok(content) = fs::read_to_string(&path) { + if let Ok(session) = serde_json::from_str::(&content) { + return Ok(session); + } + } + // Fallback: oauth_session.json (different field names) + let oauth_path = path.with_file_name("oauth_session.json"); + if let Ok(content) = fs::read_to_string(&oauth_path) { + if let Ok(oauth) = serde_json::from_str::(&content) { + if let (Some(did), Some(handle)) = (oauth["did"].as_str(), oauth["handle"].as_str()) { + return Ok(Session { + did: did.to_string(), + handle: handle.to_string(), + access_jwt: oauth["access_token"].as_str().unwrap_or("").to_string(), + refresh_jwt: oauth["refresh_token"].as_str().unwrap_or("").to_string(), + pds: oauth["pds"].as_str().map(|s| s.to_string()), + }); + } + } + } + Err(anyhow::anyhow!("Token file not found: {:?}. Run 'ailog login' or 'ailog oauth' first.", path)) } /// Save session to token file @@ -54,13 +71,30 @@ pub fn save_session(session: &Session) -> Result<()> { Ok(()) } -/// Load bot session from bot token file +/// Load bot session from bot token file, fallback to oauth_bot_session.json pub fn load_bot_session() -> Result { let path = bot_token_path()?; - let content = fs::read_to_string(&path) - .with_context(|| format!("Bot token file not found: {:?}. Run 'ailog login --bot' first.", path))?; - let session: Session = serde_json::from_str(&content)?; - Ok(session) + if let Ok(content) = fs::read_to_string(&path) { + if let Ok(session) = serde_json::from_str::(&content) { + return Ok(session); + } + } + // Fallback: oauth_bot_session.json + let oauth_path = path.with_file_name("oauth_bot_session.json"); + if let Ok(content) = fs::read_to_string(&oauth_path) { + if let Ok(oauth) = serde_json::from_str::(&content) { + if let (Some(did), Some(handle)) = (oauth["did"].as_str(), oauth["handle"].as_str()) { + return Ok(Session { + did: did.to_string(), + handle: handle.to_string(), + access_jwt: oauth["access_token"].as_str().unwrap_or("").to_string(), + refresh_jwt: oauth["refresh_token"].as_str().unwrap_or("").to_string(), + pds: oauth["pds"].as_str().map(|s| s.to_string()), + }); + } + } + } + Err(anyhow::anyhow!("Bot token file not found: {:?}. Run 'ailog login --bot' or 'ailog oauth --bot' first.", path)) } /// Save bot session to bot token file diff --git a/src/mcp/mod.rs b/src/mcp/mod.rs index 8ef604b..2109a00 100644 --- a/src/mcp/mod.rs +++ b/src/mcp/mod.rs @@ -419,7 +419,7 @@ fn handle_post_create(text: &str) -> Result { &oauth_session, "POST", &full_url, dpop_nonce.as_deref(), )?; let res = client.post(&url) - .header("Authorization", format!("DPoP {}", bot_session.access_jwt)) + .header("Authorization", format!("DPoP {}", oauth_session.access_token)) .header("DPoP", dpop_proof) .json(&body) .send()