This commit is contained in:
2025-05-20 23:06:24 +09:00
parent d1f52373bb
commit 6824266a19
9 changed files with 98 additions and 18 deletions

View File

@@ -1,4 +1,5 @@
//src/model.rs
use rusqlite::{params, Connection, Result as SqlResult};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
@@ -9,6 +10,38 @@ pub struct AiSystem {
pub messaging: Messaging,
}
impl AiSystem {
pub fn save_to_db(&self, conn: &Connection) -> SqlResult<()> {
conn.execute(
"CREATE TABLE IF NOT EXISTS ai_state (id INTEGER PRIMARY KEY, json TEXT)",
[],
)?;
let json_data = serde_json::to_string(self).map_err(|e| {
rusqlite::Error::ToSqlConversionFailure(Box::new(e))
})?;
conn.execute(
"INSERT OR REPLACE INTO ai_state (id, json) VALUES (?1, ?2)",
params![1, json_data],
)?;
Ok(())
}
pub fn load_from_db(conn: &Connection) -> SqlResult<Self> {
let mut stmt = conn.prepare("SELECT json FROM ai_state WHERE id = ?1")?;
let json: String = stmt.query_row(params![1], |row| row.get(0))?;
// ここも serde_json のエラーを map_err で変換
let system: AiSystem = serde_json::from_str(&json).map_err(|e| {
rusqlite::Error::FromSqlConversionFailure(0, rusqlite::types::Type::Text, Box::new(e))
})?;
Ok(system)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Personality {
pub kind: String, // e.g., "positive", "negative", "neutral"
@@ -37,5 +70,3 @@ pub struct Messaging {
pub decay_rate: f32, // how quickly emotion fades (0.0 - 1.0)
pub templates: Vec<String>, // message template variations
}