(file_or_url: String)
| 3960 | } |
| 3961 | |
| 3962 | async fn import_session_data(file_or_url: String) -> anyhow::Result<()> { |
| 3963 | let raw = if file_or_url.starts_with("http://") || file_or_url.starts_with("https://") { |
| 3964 | let client = reqwest::Client::new(); |
| 3965 | let mut text = client.get(&file_or_url).send().await?.text().await?; |
| 3966 | |
| 3967 | if let Some(slug) = parse_share_slug(&file_or_url) { |
| 3968 | if serde_json::from_str::<serde_json::Value>(&text).is_err() { |
| 3969 | let share_api = format!("https://opencode.ai/api/share/{}/data", slug); |
| 3970 | text = client.get(share_api).send().await?.text().await?; |
| 3971 | } |
| 3972 | } |
| 3973 | text |
| 3974 | } else { |
| 3975 | fs::read_to_string(&file_or_url)? |
| 3976 | }; |
| 3977 | let payload: SessionImportPayload = serde_json::from_str(&raw)?; |
| 3978 | let entries = normalize_import_payload(payload); |
| 3979 | |
| 3980 | if entries.is_empty() { |
| 3981 | anyhow::bail!("No session entries found in {}", file_or_url); |
| 3982 | } |
| 3983 | |
| 3984 | let db = Database::new().await?; |
| 3985 | let session_repo = SessionRepository::new(db.pool().clone()); |
| 3986 | let message_repo = MessageRepository::new(db.pool().clone()); |
| 3987 | |
| 3988 | let mut imported = 0usize; |
| 3989 | for mut entry in entries { |
| 3990 | entry.info.messages.clear(); |
| 3991 | |
| 3992 | if session_repo.get(&entry.info.id).await?.is_some() { |
| 3993 | session_repo.update(&entry.info).await?; |
| 3994 | } else { |
| 3995 | session_repo.create(&entry.info).await?; |
| 3996 | } |
| 3997 | |
| 3998 | for mut message in entry.messages { |
| 3999 | if message.session_id.is_empty() { |
| 4000 | message.session_id = entry.info.id.clone(); |
| 4001 | } |
| 4002 | message_repo.upsert(&message).await?; |
| 4003 | } |
| 4004 | imported += 1; |
| 4005 | } |
| 4006 | |
| 4007 | println!("Imported {} session(s) from {}", imported, file_or_url); |
| 4008 | Ok(()) |
| 4009 | } |
| 4010 | |
| 4011 | fn parse_github_remote(url: &str) -> Option<(String, String)> { |
| 4012 | let normalized = url.trim().trim_end_matches('/').trim_end_matches(".git"); |
no test coverage detected