Skip to content

Commit

Permalink
misc: refactored invocation loop
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Jun 22, 2024
1 parent 0fd68dd commit 315a547
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ async-trait = "0.1.80"
clap = { version = "4.5.6", features = ["derive"] }
colored = "2.1.0"
indexmap = "2.2.6"
itertools = "0.13.0"
lazy_static = "1.4.0"
ollama-rs = { version = "0.1.9", features = [
"rustls",
Expand Down
21 changes: 9 additions & 12 deletions src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ mod serialization;
pub mod state;
pub mod task;

#[derive(Debug, Default, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Invocation {
pub action: String,
pub attributes: Option<HashMap<String, String>>,
pub payload: Option<String>,
}

impl std::hash::Hash for Invocation {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.action.hash(state);
state.write(format!("{:?}", &self.attributes).as_bytes());
self.payload.hash(state);
}
}

impl Invocation {
pub fn new(
action: String,
Expand Down Expand Up @@ -114,7 +122,6 @@ impl Agent {

// parse the model response into invocations
let invocations = serialization::xml::parsing::try_parse(&response)?;
let mut prev: Option<Invocation> = None;

// nothing parsed, report the problem to the model
if invocations.is_empty() {
Expand Down Expand Up @@ -143,16 +150,6 @@ impl Agent {

// for each parsed invocation
for inv in invocations {
// avoid running the same command twince in a row
if let Some(p) = prev.as_ref() {
if inv.eq(p) {
println!(".");
continue;
}
}

prev = Some(inv.clone());

// see if valid action and execute
if let Err(e) = self.state.execute(inv.clone()).await {
println!("ERROR: {}", e);
Expand Down
4 changes: 3 additions & 1 deletion src/agent/serialization/xml/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use anyhow::Result;
use itertools::Itertools;
use xml::{reader::XmlEvent, EventReader};

use crate::agent::Invocation;
Expand Down Expand Up @@ -137,7 +138,8 @@ pub(crate) fn try_parse(raw: &str) -> Result<Vec<Invocation>> {
}
}

Ok(parsed)
// avoid running the same command twince in a row
Ok(parsed.into_iter().unique().collect())
}

// TODO: add waaaaay more tests
Expand Down

0 comments on commit 315a547

Please sign in to comment.