move the send and turn row markers into hive-types
This commit is contained in:
parent
e4a9005468
commit
6084fb6329
9 changed files with 119 additions and 43 deletions
|
|
@ -1,4 +1,5 @@
|
|||
//! Foundational shared types for the hyperhive workspace: [`Ident`] and [`Permission`].
|
||||
//! Foundational shared types for the hyperhive workspace: [`Ident`], [`Permission`]
|
||||
//! and the [`term_msg`] row markers.
|
||||
//!
|
||||
//! A zero-dependency (bar `serde`) leaf crate so every wire-type crate
|
||||
//! (`hive-sh4re`, `hive-host-sock`, `hive-core-agent-sock`) and both binaries
|
||||
|
|
@ -10,6 +11,8 @@ mod permission;
|
|||
|
||||
pub use permission::Permission;
|
||||
|
||||
pub mod term_msg;
|
||||
|
||||
/// The environment variable nix uses to hand this process the blacklist of
|
||||
/// names that already mean something to the message layer.
|
||||
///
|
||||
|
|
|
|||
72
hive-types/src/term_msg.rs
Normal file
72
hive-types/src/term_msg.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
//! The icons and summary prefixes that mark a terminal row as a sent message
|
||||
//! or a message-started turn, and the parsers that read the peer back out.
|
||||
//!
|
||||
//! The classifier in `hive-sh4re` builds the summaries and
|
||||
//! `swarm-controller`'s message stream parses them, so both sides name the
|
||||
//! same constants and the same parsers rather than each spelling the format.
|
||||
|
||||
/// Icon of the row an agent's `send` tool call makes.
|
||||
pub const SEND_ICON: &str = "📤";
|
||||
|
||||
/// What a `send` row's summary starts with; the recipient follows. A
|
||||
/// multi-line body appends ` · <N>L`, which [`send_recipient`] drops.
|
||||
pub const SEND_SUMMARY_PREFIX: &str = "send* → ";
|
||||
|
||||
/// Icon of the row a turn started by a message makes.
|
||||
pub const TURN_ICON: &str = "◆";
|
||||
|
||||
/// What a turn-start row's summary starts with; the sender follows.
|
||||
pub const TURN_SUMMARY_PREFIX: &str = "TURN ← ";
|
||||
|
||||
/// The recipient a `send` row's summary names, without the line-count suffix.
|
||||
#[must_use]
|
||||
pub fn send_recipient(summary: &str) -> Option<&str> {
|
||||
let rest = summary.strip_prefix(SEND_SUMMARY_PREFIX)?;
|
||||
Some(match rest.rsplit_once(" · ") {
|
||||
Some((to, lines))
|
||||
if lines
|
||||
.strip_suffix('L')
|
||||
.is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())) =>
|
||||
{
|
||||
to
|
||||
}
|
||||
_ => rest,
|
||||
})
|
||||
}
|
||||
|
||||
/// The sender a turn-start row's summary names.
|
||||
#[must_use]
|
||||
pub fn turn_sender(summary: &str) -> Option<&str> {
|
||||
summary.strip_prefix(TURN_SUMMARY_PREFIX)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{SEND_SUMMARY_PREFIX, TURN_SUMMARY_PREFIX, send_recipient, turn_sender};
|
||||
|
||||
#[test]
|
||||
fn a_send_summary_names_its_recipient_with_and_without_the_line_count() {
|
||||
assert_eq!(
|
||||
send_recipient(&format!("{SEND_SUMMARY_PREFIX}iris")),
|
||||
Some("iris")
|
||||
);
|
||||
assert_eq!(
|
||||
send_recipient(&format!("{SEND_SUMMARY_PREFIX}iris · 3L")),
|
||||
Some("iris")
|
||||
);
|
||||
assert_eq!(send_recipient("send* →"), None);
|
||||
assert_eq!(send_recipient("Read(x)"), None);
|
||||
// A suffix lookalike in the name is not stripped.
|
||||
assert_eq!(send_recipient("send* → a · xL"), Some("a · xL"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_turn_summary_names_its_sender() {
|
||||
assert_eq!(
|
||||
turn_sender(&format!("{TURN_SUMMARY_PREFIX}atlas")),
|
||||
Some("atlas")
|
||||
);
|
||||
assert_eq!(turn_sender("TURN ←"), None);
|
||||
assert_eq!(turn_sender("send* → iris"), None);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue