add memory
This commit is contained in:
parent
52d0efc086
commit
9cbf5da3fd
@ -10,6 +10,7 @@ use crate::git::{git_init, git_status};
|
|||||||
use crate::config::ConfigPaths;
|
use crate::config::ConfigPaths;
|
||||||
use crate::commands::git_repo::read_all_git_files;
|
use crate::commands::git_repo::read_all_git_files;
|
||||||
use crate::metrics::{load_user_data, save_user_data};
|
use crate::metrics::{load_user_data, save_user_data};
|
||||||
|
use crate::memory::{log_message};
|
||||||
|
|
||||||
pub fn mcp_setup() {
|
pub fn mcp_setup() {
|
||||||
let config = ConfigPaths::new();
|
let config = ConfigPaths::new();
|
||||||
@ -197,6 +198,7 @@ fn chat_cmd() -> Command {
|
|||||||
// 通常のチャット処理(repoが指定されていない場合)
|
// 通常のチャット処理(repoが指定されていない場合)
|
||||||
match c.args.get(0) {
|
match c.args.get(0) {
|
||||||
Some(question) => {
|
Some(question) => {
|
||||||
|
log_message(&config.base_dir, "user", question);
|
||||||
let response = ask_chat(c, question);
|
let response = ask_chat(c, question);
|
||||||
|
|
||||||
if let Some(ref text) = response {
|
if let Some(ref text) = response {
|
||||||
@ -207,6 +209,7 @@ fn chat_cmd() -> Command {
|
|||||||
} else if text.contains("hate") || text.contains("bad") {
|
} else if text.contains("hate") || text.contains("bad") {
|
||||||
user.metrics.trust -= 0.05;
|
user.metrics.trust -= 0.05;
|
||||||
}
|
}
|
||||||
|
log_message(&config.base_dir, "ai", &text);
|
||||||
save_user_data(&user_path, &user);
|
save_user_data(&user_path, &user);
|
||||||
} else {
|
} else {
|
||||||
eprintln!("❗ 応答が取得できませんでした");
|
eprintln!("❗ 応答が取得できませんでした");
|
||||||
|
@ -9,6 +9,7 @@ mod config;
|
|||||||
mod git;
|
mod git;
|
||||||
mod chat;
|
mod chat;
|
||||||
mod metrics;
|
mod metrics;
|
||||||
|
mod memory;
|
||||||
|
|
||||||
use cli::cli_app;
|
use cli::cli_app;
|
||||||
use seahorse::App;
|
use seahorse::App;
|
||||||
|
49
src/memory.rs
Normal file
49
src/memory.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// src/memory.rs
|
||||||
|
use chrono::{DateTime, Local, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fs::{self};
|
||||||
|
//use std::fs::{self, OpenOptions};
|
||||||
|
use std::io::{BufReader, BufWriter};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::{fs::File};
|
||||||
|
//use std::{env, fs::File};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct MemoryEntry {
|
||||||
|
pub timestamp: DateTime<Utc>,
|
||||||
|
pub sender: String,
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_message(base_dir: &PathBuf, sender: &str, message: &str) {
|
||||||
|
let now_utc = Utc::now();
|
||||||
|
let date_str = Local::now().format("%Y-%m-%d").to_string();
|
||||||
|
let mut file_path = base_dir.clone();
|
||||||
|
file_path.push("memory");
|
||||||
|
let _ = fs::create_dir_all(&file_path);
|
||||||
|
file_path.push(format!("{}.json", date_str));
|
||||||
|
|
||||||
|
let new_entry = MemoryEntry {
|
||||||
|
timestamp: now_utc,
|
||||||
|
sender: sender.to_string(),
|
||||||
|
message: message.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut entries = if file_path.exists() {
|
||||||
|
let file = File::open(&file_path).expect("💥 メモリファイルの読み込み失敗");
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
serde_json::from_reader(reader).unwrap_or_else(|_| vec![])
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
};
|
||||||
|
|
||||||
|
entries.push(new_entry);
|
||||||
|
|
||||||
|
let file = File::create(&file_path).expect("💥 メモリファイルの書き込み失敗");
|
||||||
|
let writer = BufWriter::new(file);
|
||||||
|
serde_json::to_writer_pretty(writer, &entries).expect("💥 JSONの書き込み失敗");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 利用例(ask_chatの中)
|
||||||
|
// log_message(&config.base_dir, "user", question);
|
||||||
|
// log_message(&config.base_dir, "ai", &response);
|
Loading…
x
Reference in New Issue
Block a user