From 5db0c0bf25a8f10cc0ade8abe63c8c4dfc9f2df9 Mon Sep 17 00:00:00 2001 From: Archer Date: Sat, 7 Jun 2025 13:19:15 +0800 Subject: [PATCH 1/2] update chat session --- amico-mods/Cargo.toml | 5 ++- amico-mods/src/std/ai/mod.rs | 1 + amico-mods/src/std/ai/services/mod.rs | 5 --- .../ai/{services/in_memory.rs => session.rs} | 33 ++++++++++++------- amico/src/engine/systems.rs | 8 ++--- amico/src/main.rs | 12 +++---- 6 files changed, 34 insertions(+), 30 deletions(-) rename amico-mods/src/std/ai/{services/in_memory.rs => session.rs} (82%) diff --git a/amico-mods/Cargo.toml b/amico-mods/Cargo.toml index 222770b..c492b13 100644 --- a/amico-mods/Cargo.toml +++ b/amico-mods/Cargo.toml @@ -13,11 +13,10 @@ license = "MIT OR Apache-2.0" default = ["full"] # The standard set of plugins -std-full = ["std-providers", "std-services", "std-tasks"] +std-full = ["std-providers", "std-chat-session", "std-tasks"] std-providers = ["std-providers-rig"] std-providers-rig = ["rig-core"] -std-services = ["std-services-in-memory"] -std-services-in-memory = [] +std-chat-session = [] std-tasks = ["std-cli-chatbot", "std-audio-chatbot"] std-cli-chatbot = [] std-audio-chatbot = ["dep:amico-hal", "amico-hal/os-common"] diff --git a/amico-mods/src/std/ai/mod.rs b/amico-mods/src/std/ai/mod.rs index 11ead12..52cb070 100644 --- a/amico-mods/src/std/ai/mod.rs +++ b/amico-mods/src/std/ai/mod.rs @@ -1,2 +1,3 @@ pub mod providers; pub mod services; +pub mod session; diff --git a/amico-mods/src/std/ai/services/mod.rs b/amico-mods/src/std/ai/services/mod.rs index 9b838c9..de2b420 100644 --- a/amico-mods/src/std/ai/services/mod.rs +++ b/amico-mods/src/std/ai/services/mod.rs @@ -1,6 +1 @@ -#[cfg(feature = "std-services-in-memory")] -mod in_memory; -#[cfg(feature = "std-services-in-memory")] -pub use in_memory::*; - pub mod speech; diff --git a/amico-mods/src/std/ai/services/in_memory.rs b/amico-mods/src/std/ai/session.rs similarity index 82% rename from amico-mods/src/std/ai/services/in_memory.rs rename to amico-mods/src/std/ai/session.rs index b5ae025..cf78288 100644 --- a/amico-mods/src/std/ai/services/in_memory.rs +++ b/amico-mods/src/std/ai/session.rs @@ -13,13 +13,13 @@ fn debug_history(history: &[Message]) -> String { // Convert message to a prettier shorter string for m in history.iter() { match m { - Message::Assistant(text) => messages.push_str(&format!("a: {}\n", text)), - Message::User(text) => messages.push_str(&format!("u: {}\n", text)), + Message::Assistant(text) => messages.push_str(&format!("- a: {}\n", text)), + Message::User(text) => messages.push_str(&format!("- u: {}\n", text)), Message::ToolCall(name, id, params) => { - messages.push_str(&format!("tc: {}[{}] ({})\n", name, id, params)) + messages.push_str(&format!("- tc: {}[{}] ({})\n", name, id, params)) } Message::ToolResult(name, id, params) => { - messages.push_str(&format!("tr: {}[{}] => {}\n", name, id, params)) + messages.push_str(&format!("- tr: {}[{}] => {}\n", name, id, params)) } } } @@ -27,8 +27,12 @@ fn debug_history(history: &[Message]) -> String { messages } +/// The standard implementation of a chat session. +/// +/// This session manages chat history in a `Vec`, and executes tool call +/// functions. #[derive(Debug)] -pub struct InMemoryService { +pub struct ChatSession { /// The context config for the service pub ctx: SessionContext, @@ -36,12 +40,19 @@ pub struct InMemoryService { pub history: Vec, } -impl Session for InMemoryService { +impl ChatSession { + /// Create a new in-memory session from an existing history of messages. + pub fn from_history(ctx: SessionContext, history: Vec) -> Self { + Self { ctx, history } + } +} + +impl Session for ChatSession { type Model = M; - fn from_ctx(context: SessionContext) -> Self { + fn from_ctx(ctx: SessionContext) -> Self { Self { - ctx: context, + ctx, history: Vec::new(), } } @@ -123,8 +134,8 @@ impl Session for InMemoryService { } } -impl IntoResourceMut> for InMemoryService { - fn into_resource_mut(self) -> ResourceMut> { - ResourceMut::new("InMemoryService", self) +impl IntoResourceMut> for ChatSession { + fn into_resource_mut(self) -> ResourceMut> { + ResourceMut::new("chat-session", self) } } diff --git a/amico/src/engine/systems.rs b/amico/src/engine/systems.rs index 074370b..b203a6a 100644 --- a/amico/src/engine/systems.rs +++ b/amico/src/engine/systems.rs @@ -3,12 +3,10 @@ use std::sync::mpsc::channel; use amico::resource::Resource; use amico::{ai::completion::SessionDyn, resource::ResourceMut}; use amico_core::{traits::System, world::HandlerRegistry}; +use amico_mods::std::ai::session::ChatSession; use amico_mods::std::ai::{ providers::rig::RigProvider, - services::{ - InMemoryService, - speech::{speech_to_text, text_to_speech}, - }, + services::speech::{speech_to_text, text_to_speech}, }; use evenio::prelude::*; @@ -173,7 +171,7 @@ impl System for SpeechSystem { } pub struct CompletionSystem { - pub service_resource: ResourceMut>, + pub service_resource: ResourceMut>, } impl System for CompletionSystem { diff --git a/amico/src/main.rs b/amico/src/main.rs index 80a4e14..c4873ae 100644 --- a/amico/src/main.rs +++ b/amico/src/main.rs @@ -5,7 +5,7 @@ use amico::resource::{IntoResource, IntoResourceMut}; use amico_core::{Agent, OnFinish}; use amico_mods::runtime::storage::fs::FsStorage; use amico_mods::std::ai::providers::rig::{RigProvider, providers}; -use amico_mods::std::ai::services::InMemoryService; +use amico_mods::std::ai::session::ChatSession; use amico_mods::web3::solana::balance::BalanceSensor; use amico_mods::web3::solana::client::SolanaClient; use amico_mods::web3::solana::trade::TradeEffector; @@ -113,8 +113,8 @@ async fn main() { &base_url, )); - // Create the Service - let service = SessionBuilder::new(provider) + // Create the global chat session + let global_session = SessionBuilder::new(provider) .model("gpt-4o".to_string()) .system_prompt(AMICO_SYSTEM_PROMPT.to_string()) .temperature(0.2) @@ -124,16 +124,16 @@ async fn main() { .tool(trade_effector.get().tool()) .tool(a2a.send_message_tool()) .tool(a2a.contact_list_tool()) - .build::>(); + .build::>(); println!(); println!("Agent wallet addresses:"); println!("{}", wallet.get().pubkey_list()); println!(); - println!("Tools enabled:\n{}", service.ctx.tools.describe()); + println!("Tools enabled:\n{}", global_session.ctx.tools.describe()); - let service_resource = service.into_resource_mut(); + let service_resource = global_session.into_resource_mut(); // Initialize ECS let mut agent = Agent::new(DispatchStrategy); From 7a93c6ad50bfb2449fa67bef4df49599f297b495 Mon Sep 17 00:00:00 2001 From: Archer Date: Sat, 7 Jun 2025 13:19:52 +0800 Subject: [PATCH 2/2] update `amico-core` version --- Cargo.lock | 2 +- amico-mods/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 252db9a..69720a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1122,7 +1122,7 @@ dependencies = [ [[package]] name = "amico-mods" -version = "0.0.3" +version = "0.0.4" dependencies = [ "alloy", "amico-core 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/amico-mods/Cargo.toml b/amico-mods/Cargo.toml index c492b13..bb3cca3 100644 --- a/amico-mods/Cargo.toml +++ b/amico-mods/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "amico-mods" -version = "0.0.3" +version = "0.0.4" edition = "2024" description = "The plugins of the Amico AI Agent Framework" repository = "https://github.com/AIMOverse/amico"