Skip to content

Commit

Permalink
refact: wide refactoring around events, event handling and naming con…
Browse files Browse the repository at this point in the history
…ventions
  • Loading branch information
evilsocket committed Feb 7, 2025
1 parent 88f7213 commit 54269da
Show file tree
Hide file tree
Showing 15 changed files with 525 additions and 272 deletions.
11 changes: 8 additions & 3 deletions src/agent/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use channel::*;

use super::namespaces::ActionOutput;
use super::task::tasklet::Tasklet;
use super::workflow::Workflow;
use super::{
generator::ChatOptions,
state::{metrics::Metrics, storage::StorageType},
Expand All @@ -23,7 +24,10 @@ pub struct StateUpdate {
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type", content = "data", rename_all = "snake_case")]
pub enum EventType {
WorkflowStarted(Workflow),
WorkflowCompleted(Workflow),
TaskStarted(Tasklet),
MetricsUpdate(Metrics),
StorageUpdate {
Expand All @@ -37,7 +41,7 @@ pub enum EventType {
EmptyResponse,
Thinking(String),
Sleeping(usize),
ChatResponse(String),
TextResponse(String),
InvalidAction {
invocation: Invocation,
error: Option<String>,
Expand All @@ -64,7 +68,8 @@ pub enum EventType {

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Event {
pub timestamp: u64,
pub timestamp: u128,
#[serde(flatten)]
pub event: EventType,
}

Expand All @@ -74,7 +79,7 @@ impl Event {
timestamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs(),
.as_nanos(),
event,
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/agent/generator/anthropic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,21 @@ impl Client for AnthropicClient {
for m in options.history.iter() {
// all messages must have non-empty content except for the optional final assistant messag
match m {
super::Message::Agent(data, _) => {
let trimmed = data.trim();
super::Message::Agent {
content,
tool_call: _,
} => {
let trimmed = content.trim();
if !trimmed.is_empty() {
messages.push(Message::assistant(data.trim()))
messages.push(Message::assistant(trimmed))
} else {
log::warn!("ignoring empty assistant message: {:?}", m);
}
}
super::Message::Feedback(data, _) => match data {
super::Message::Feedback {
result,
tool_call: _,
} => match result {
ActionOutput::Image { data, mime_type } => messages.push(Message::user(
Content::from(ImageContentSource::base64(get_media_type(mime_type), data)),
)),
Expand Down Expand Up @@ -240,7 +246,7 @@ impl Client for AnthropicClient {

if let Some(tool_use) = tool_use {
let mut attributes = HashMap::new();
let mut payload = None;
let mut argument = None;

let object = match tool_use.input.as_object() {
Some(o) => o,
Expand All @@ -260,20 +266,20 @@ impl Client for AnthropicClient {

let str_val = value_content.trim_matches('"').to_string();
if name == "payload" {
payload = Some(str_val);
argument = Some(str_val);
} else {
attributes.insert(name.to_string(), str_val);
}
}

let inv = Invocation {
action: tool_use.name.to_string(),
attributes: if attributes.is_empty() {
tool_name: tool_use.name.to_string(),
named_arguments: if attributes.is_empty() {
None
} else {
Some(attributes)
},
payload,
argument,
};

invocations.push(inv);
Expand Down
28 changes: 14 additions & 14 deletions src/agent/generator/groq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,28 @@ impl Client for GroqClient {

for m in options.history.iter() {
chat_history.push(match m {
Message::Agent(data, invocation) => {
Message::Agent { content, tool_call } => {
let mut tool_call_id = None;
if let Some(inv) = invocation {
tool_call_id = Some(format!("{}-{}", inv.action, call_idx));
if let Some(inv) = tool_call {
tool_call_id = Some(format!("{}-{}", inv.tool_name, call_idx));
call_idx += 1;
}

crate::api::groq::completion::message::Message::AssistantMessage {
role: Some("assistant".to_string()),
content: Some(data.trim().to_string()),
content: Some(content.trim().to_string()),
name: None,
tool_call_id,
tool_calls: None,
}
}
Message::Feedback(data, invocation) => {
Message::Feedback { result, tool_call } => {
let mut tool_call_id: Option<String> = None;
if let Some(inv) = invocation {
tool_call_id = Some(format!("{}-{}", inv.action, call_idx));
if let Some(inv) = tool_call {
tool_call_id = Some(format!("{}-{}", inv.tool_name, call_idx));
}
if tool_call_id.is_some() {
match data {
match result {
ActionOutput::Text(text) => {
crate::api::groq::completion::message::Message::ToolMessage {
role: Some("tool".to_string()),
Expand Down Expand Up @@ -207,7 +207,7 @@ impl Client for GroqClient {
}
}
} else {
match data {
match result {
ActionOutput::Text(text) => {
crate::api::groq::completion::message::Message::UserMessage {
role: Some("user".to_string()),
Expand Down Expand Up @@ -339,7 +339,7 @@ impl Client for GroqClient {
if let Some(calls) = choice.message.tool_calls {
for call in calls {
let mut attributes = HashMap::new();
let mut payload = None;
let mut argument = None;

if let Some(args) = call.function.arguments.as_ref() {
let map: HashMap<String, serde_json::Value> = serde_json::from_str(args)?;
Expand All @@ -352,21 +352,21 @@ impl Client for GroqClient {

let str_val = content.trim_matches('"').to_string();
if name == "payload" {
payload = Some(str_val);
argument = Some(str_val);
} else {
attributes.insert(name.to_string(), str_val);
}
}
}

let inv = Invocation {
action: call.function.name.unwrap_or_default().to_string(),
attributes: if attributes.is_empty() {
tool_name: call.function.name.unwrap_or_default().to_string(),
named_arguments: if attributes.is_empty() {
None
} else {
Some(attributes)
},
payload,
argument,
};

invocations.push(inv);
Expand Down
Loading

0 comments on commit 54269da

Please sign in to comment.