Skip to content

Commit

Permalink
Merge pull request #556 from coasys/launcher-state-cleanup
Browse files Browse the repository at this point in the history
Agent list handling imrovements and other cleanup
  • Loading branch information
lucksus authored Jan 23, 2025
2 parents ce6d979 + b6d652a commit 4c3591d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This project _loosely_ adheres to [Semantic Versioning](https://semver.org/spec/
- Fix for missing links: race-condition fixed in useSubjects [PR#540](https://github.com/coasys/ad4m/pull/540) and [PR#554](https://github.com/coasys/ad4m/pull/554)
- Fix for missing links: detect and rerun failed commits in p-diff-sync [PR#551](https://github.com/coasys/ad4m/pull/551)
- Fix for syncing broken after some time: Prevent p-diff-sync diff entries from exceeding HC entry size limit of 4MB (which would happen at some point through snapshot creation) [PR#553](https://github.com/coasys/ad4m/pull/553)
- Fix for crash when removing .ad4m directory after using multiple agent feature [PR#556](https://github.com/coasys/ad4m/pull/556)


### Added
Expand Down
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@ members = [
"rust-executor",
"ui/src-tauri"
]


[patch.crates-io]
notify-rust = { version = "4.6.0", git = "https://github.com/coasys/notify-rust.git" }
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub fn pull<Retriever: PerspectiveDiffRetreiver>(
// First check if we are actually ahead of them -> we don't have to do anything
// they will have to merge with / or fast-forward to our current
if workspace.all_ancestors(&current.hash)?.contains(&theirs) {
debug!("===PerspectiveDiffSync.pull(): We are ahead of them. They will have to pull/fast-forward. Exiting without change...");
return Ok(PullResult {
diff: PerspectiveDiff::default(),
current_revision: Some(current.hash),
Expand Down
2 changes: 1 addition & 1 deletion cli/mainnet_seed.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"did:key:z6MkvPpWxwXAnLtMcoc9sX7GEoJ96oNnQ3VcQJRLspNJfpE7"
],
"knownLinkLanguages": [
"QmzSYwdZYYgYGWSiRZqMGs5YrJu8gQzCQCxJFkK9Ar28vaki9UN"
"QmzSYwdbWZ8Y7w8ZtAb47xN8AEaTUveoUnxgnT6pxvrWdtBDMVy"
],
"directMessageLanguage": "QmzSYwdmrcq32PAsQFD95D6oTGUK7odPDCmXvoYgDcV6SPRcA3u",
"agentLanguage": "QmzSYwdZDdgxiyE8crozqbxoBP52h6ocMdDq2S2mg4ScjzVLWKQ",
Expand Down
2 changes: 1 addition & 1 deletion rust-executor/src/mainnet_seed.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"did:key:z6MkvPpWxwXAnLtMcoc9sX7GEoJ96oNnQ3VcQJRLspNJfpE7"
],
"knownLinkLanguages": [
"QmzSYwdZYYgYGWSiRZqMGs5YrJu8gQzCQCxJFkK9Ar28vaki9UN"
"QmzSYwdbWZ8Y7w8ZtAb47xN8AEaTUveoUnxgnT6pxvrWdtBDMVy"
],
"directMessageLanguage": "QmzSYwdmrcq32PAsQFD95D6oTGUK7odPDCmXvoYgDcV6SPRcA3u",
"agentLanguage": "QmzSYwdZDdgxiyE8crozqbxoBP52h6ocMdDq2S2mg4ScjzVLWKQ",
Expand Down
33 changes: 18 additions & 15 deletions ui/src-tauri/src/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
use dirs::home_dir;
use serde::{Deserialize, Serialize};
use std::fs::{File, OpenOptions};
use std::fs::{create_dir_all, File, OpenOptions};
use std::io::prelude::*;
use std::path::PathBuf;

pub static FILE_NAME: &str = "launcher-state.json";

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct AgentList {
pub struct AgentConfigDir {
pub name: String,
pub path: PathBuf,
pub bootstrap: Option<PathBuf>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct LauncherState {
pub agent_list: Vec<AgentList>,
pub selected_agent: Option<AgentList>,
pub agent_list: Vec<AgentConfigDir>,
pub selected_agent: Option<AgentConfigDir>,
}

fn file_path() -> PathBuf {
let path = home_dir().expect("Could not get home dir").join(".ad4m");
// Create directories if they don't exist
create_dir_all(&path).expect("Failed to create directory");
path.join(FILE_NAME)
}

impl LauncherState {
pub fn save(&mut self) -> std::io::Result<()> {
let path = home_dir()
.expect("Could not get home dir")
.join("ad4m-state.json");
let mut file = File::create(path)?;
let mut file = File::create(file_path())?;
let data = serde_json::to_string(&self).unwrap();
file.write_all(data.as_bytes())?;
Ok(())
}

pub fn load() -> std::io::Result<LauncherState> {
let path = home_dir()
.expect("Could not get home dir")
.join("ad4m-state.json");
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)?;
.open(file_path())?;
let mut data = String::new();
file.read_to_string(&mut data)?;

let state = match serde_json::from_str(&data) {
Ok(state) => state,
Err(_) => {
let agent = AgentList {
let agent = AgentConfigDir {
name: "Main Net".to_string(),
path: home_dir().expect("Could not get home dir").join(".ad4m"),
bootstrap: None,
Expand All @@ -60,13 +63,13 @@ impl LauncherState {
Ok(state)
}

pub fn add_agent(&mut self, agent: AgentList) {
pub fn add_agent(&mut self, agent: AgentConfigDir) {
if !self.is_agent_taken(&agent.name, &agent.path) {
self.agent_list.push(agent);
}
}

pub fn remove_agent(&mut self, agent: AgentList) {
pub fn remove_agent(&mut self, agent: AgentConfigDir) {
self.agent_list
.retain(|a| a.name != agent.name && a.path != agent.path);
}
Expand Down
8 changes: 4 additions & 4 deletions ui/src-tauri/src/commands/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate remove_dir_all;
use crate::app_state::{AgentList, LauncherState};
use crate::app_state::{AgentConfigDir, LauncherState};
use crate::util::create_tray_message_windows;
use crate::{config::data_path, get_main_window};

Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn open_tray(app_handle: tauri::AppHandle) {
}

#[tauri::command]
pub fn add_app_agent_state(agent: AgentList) {
pub fn add_app_agent_state(agent: AgentConfigDir) {
let mut state = LauncherState::load().unwrap();

let mut new_agent = agent.clone();
Expand All @@ -62,7 +62,7 @@ pub fn add_app_agent_state(agent: AgentList) {
}

#[tauri::command]
pub fn remove_app_agent_state(agent: AgentList) {
pub fn remove_app_agent_state(agent: AgentConfigDir) {
let mut state = LauncherState::load().unwrap();

state.remove_agent(agent.clone());
Expand All @@ -71,7 +71,7 @@ pub fn remove_app_agent_state(agent: AgentList) {
}

#[tauri::command]
pub fn set_selected_agent(agent: AgentList) {
pub fn set_selected_agent(agent: AgentConfigDir) {
let mut state = LauncherState::load().unwrap();

state.selected_agent = Some(agent);
Expand Down

0 comments on commit 4c3591d

Please sign in to comment.