1
0

change dir public/at src

This commit is contained in:
2026-03-02 15:50:01 +09:00
parent 6a1eb85a53
commit 9f8c8d9f90
10 changed files with 109 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
use anyhow::{Context, Result};
use serde_json::Value;
use std::fs;
use std::path::PathBuf;
use super::auth;
use crate::lexicons::com_atproto_repo;
@@ -9,6 +10,13 @@ use crate::xrpc::XrpcClient;
const COLLECTION_CORE: &str = "ai.syui.gpt.core";
const COLLECTION_MEMORY: &str = "ai.syui.gpt.memory";
/// Get base dir: $cfg/ai.syui.log/content/
fn gpt_base_dir() -> Result<PathBuf> {
Ok(dirs::config_dir()
.context("Could not find config directory")?
.join(super::token::BUNDLE_ID)
.join("at"))
}
/// Get core record (rkey=self)
pub async fn get_core(download: bool) -> Result<()> {
@@ -144,12 +152,12 @@ pub async fn push(collection_name: &str) -> Result<()> {
let did = &session.did;
let client = XrpcClient::new_bot(pds);
let collection_dir = format!("public/content/{}/{}", did, collection);
if !std::path::Path::new(&collection_dir).exists() {
anyhow::bail!("Collection directory not found: {}", collection_dir);
let collection_dir = gpt_base_dir()?.join(did).join(collection);
if !collection_dir.exists() {
anyhow::bail!("Collection directory not found: {}", collection_dir.display());
}
println!("Pushing {} records from {}", collection_name, collection_dir);
println!("Pushing {} records from {}", collection_name, collection_dir.display());
let mut count = 0;
for entry in fs::read_dir(&collection_dir)? {
@@ -205,19 +213,19 @@ pub async fn push(collection_name: &str) -> Result<()> {
Ok(())
}
/// Save a record to local content directory
/// Save a record to $cfg/ai.syui.gpt/{did}/{collection}/{rkey}.json
fn save_record(did: &str, collection: &str, rkey: &str, record: &Record) -> Result<()> {
let dir = format!("public/content/{}/{}", did, collection);
let dir = gpt_base_dir()?.join(did).join(collection);
fs::create_dir_all(&dir)?;
let path = format!("{}/{}.json", dir, rkey);
let path = dir.join(format!("{}.json", rkey));
let json = serde_json::json!({
"uri": record.uri,
"cid": record.cid,
"value": record.value,
});
fs::write(&path, serde_json::to_string_pretty(&json)?)?;
println!("Saved: {}", path);
println!("Saved: {}", path.display());
Ok(())
}

View File

@@ -13,3 +13,4 @@ pub mod bot;
pub mod pds;
pub mod gpt;
pub mod oauth;
pub mod setup;

View File

@@ -15,21 +15,34 @@ struct SiteConfig {
}
fn load_site_url() -> Result<String> {
// Try public/config.json in current directory
let config_path = std::path::Path::new("public/config.json");
if config_path.exists() {
let content = std::fs::read_to_string(config_path)?;
// 1. Try public/config.json in current directory
let local_path = std::path::Path::new("public/config.json");
if local_path.exists() {
let content = std::fs::read_to_string(local_path)?;
let config: SiteConfig = serde_json::from_str(&content)?;
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 in public/config.json. \
Create config.json with {{\"siteUrl\": \"https://example.com\"}}"
"No siteUrl found. Create public/config.json or run ailog oauth with --client-id"
);
}
fn percent_encode(s: &str) -> String {
let mut result = String::with_capacity(s.len() * 2);
for b in s.bytes() {

28
src/commands/setup.rs Normal file
View File

@@ -0,0 +1,28 @@
use anyhow::{Context, Result};
use std::fs;
use super::token::BUNDLE_ID;
const DEFAULT_CONFIG: &str = include_str!("../rules/config.json");
/// Run setup: copy config.json to $cfg/ai.syui.log/
pub fn run() -> Result<()> {
let cfg_dir = dirs::config_dir()
.context("Could not find config directory")?
.join(BUNDLE_ID);
fs::create_dir_all(&cfg_dir)?;
let cfg_file = cfg_dir.join("config.json");
// Prefer local public/config.json
let content = if std::path::Path::new("public/config.json").exists() {
fs::read_to_string("public/config.json")?
} else {
DEFAULT_CONFIG.to_string()
};
fs::write(&cfg_file, &content)?;
println!("ok {}", cfg_file.display());
Ok(())
}