diff --git a/crates/mind-body/src/projection/narrate.rs b/crates/mind-body/src/projection/narrate.rs index fb94872..e42147a 100644 --- a/crates/mind-body/src/projection/narrate.rs +++ b/crates/mind-body/src/projection/narrate.rs @@ -232,9 +232,13 @@ pub(crate) fn lived_stream(narration: &str, heard: &str) -> String { if heard.trim().is_empty() { narration.to_owned() } else { format!("{narration}\n\n{heard}") } } -/// What the Persona reads after the stream: the conversation's speech cues first (`The room: …`, map 16.20.2), so she -/// drafts or stays silent knowing who is waiting, then what was said, verbatim. +/// What the Persona reads after the stream. When someone turned to her, the stream ends on that arrival, so this opens +/// on it: the bridged lines (`Packet::bridged`), then the conversation's speech cues (`The room: …`, map 16.20.2) last, +/// where she drafts or stays silent knowing who is waiting. Otherwise the cues first, then what was said, verbatim. pub(crate) fn read_after(packet: &Packet) -> String { + if !packet.bridged.is_empty() { + return if packet.speaking.is_empty() { packet.bridged.clone() } else { format!("{}\n\nThe room: {}", packet.bridged, packet.speaking) }; + } match (packet.speaking.is_empty(), packet.heard.trim().is_empty()) { (true, _) => packet.heard.clone(), (false, true) => format!("The room: {}", packet.speaking), diff --git a/crates/mind-body/src/projection/select.rs b/crates/mind-body/src/projection/select.rs index 9ac2a15..7593ef8 100644 --- a/crates/mind-body/src/projection/select.rs +++ b/crates/mind-body/src/projection/select.rs @@ -281,6 +281,11 @@ pub(crate) struct Packet { /// What was said to her, verbatim, rendered by code for the Persona stage: a chat turn's messages, a beat's recent /// room lines. pub heard: String, + /// On a chat turn someone turned to her: what she reads after the stream in place of `heard`, the Persona's alone. + /// For each person who turned to her, a bridge that picks up the arrival in its own terms (`bridge`), then their new + /// lines, verbatim; then the others' new lines, then the room's earlier lines. The same lines as `heard`, from the + /// same selection. Empty on every other turn. + pub bridged: String, /// Bytes the messages to her took past the stimulus and heard caps: verbatim beats the cap. pub overflow: Overflow, pub present: Vec, @@ -783,14 +788,26 @@ impl Addressed<'_> { fn lines(&self, obj: &str, render: impl Fn(&RoomLine, &str) -> String) -> Vec { let mut lines: Vec = Vec::new(); if self.left_out > 0 { - lines.push(format!("({} more said to {obj}, not shown)", self.left_out)); + lines.push(self.not_shown(obj)); } - lines.extend(self.shown.iter().map(|(line, cut)| match cut { - None => render(line, &spoken(&line.text)), - Some(cut) => render(line, &format!("{} [… {} bytes more]", spoken(&line.text[..*cut]), line.text.len() - cut)), - })); + lines.extend(self.each(render).into_iter().map(|(_, line)| line)); lines } + + /// Each message shown, chronological, with its rendered line. + fn each(&self, render: impl Fn(&RoomLine, &str) -> String) -> Vec<(&RoomLine, String)> { + self.shown + .iter() + .map(|(line, cut)| match cut { + None => (*line, render(line, &spoken(&line.text))), + Some(cut) => (*line, render(line, &format!("{} [… {} bytes more]", spoken(&line.text[..*cut]), line.text.len() - cut))), + }) + .collect() + } + + fn not_shown(&self, obj: &str) -> String { + format!("({} more said to {obj}, not shown)", self.left_out) + } } fn said_to_her<'a>(to_her: &[&'a RoomLine], render: impl Fn(&RoomLine, &str) -> String, ceiling: usize) -> Addressed<'a> { @@ -820,11 +837,13 @@ fn said_to_her<'a>(to_her: &[&'a RoomLine], render: impl Fn(&RoomLine, &str) -> Addressed { shown, left_out, clipped } } -/// A chat turn's stimulus, what she heard, the overflow, and the bodies of the earlier lines from others it shows her. -fn chat_stimulus(inputs: &Inputs, tuning: &Tuning) -> (String, String, Overflow, Vec) { +/// A chat turn's stimulus, what she heard, the same bridged for the Persona (`Packet::bridged`), the overflow, and the +/// bodies of the earlier lines from others it shows her. `present` and `turned` are the packet's people present and who +/// turned to her (`turned_to_her`): the arrival's who and where, which each bridge renders. +fn chat_stimulus(inputs: &Inputs, tuning: &Tuning, present: &[(String, String)], turned: &[(String, String)]) -> (String, String, String, Overflow, Vec) { let messages: Vec<&RoomLine> = inputs.room.messages.iter().filter(|line| line.sender != inputs.people.own_sender).collect(); if messages.is_empty() { - return ("Nothing new was said.".into(), String::new(), Overflow::default(), Vec::new()); + return ("Nothing new was said.".into(), String::new(), String::new(), Overflow::default(), Vec::new()); } let (to_her, others): (Vec<&RoomLine>, Vec<&RoomLine>) = messages.into_iter().partition(|line| line.to_her); let typed = |line: &RoomLine, body: &str| format!("- {} in {}: {body}", name_of(line, inputs), line.room); @@ -863,12 +882,12 @@ fn chat_stimulus(inputs: &Inputs, tuning: &Tuning) -> (String, String, Overflow, let mut heard_text = heard_to_her.join("\n"); let others_heard: Vec = others.iter().map(|line| heard(line, &spoken(&line.text))).collect(); let heard_left = tuning.select.heard_bytes.saturating_sub(heard_text.len() + 1); - let (kept, _) = newest_that_fit(&others_heard, heard_left); - for line in kept { + let (others_kept, _) = newest_that_fit(&others_heard, heard_left); + for line in &others_kept { if !heard_text.is_empty() { heard_text.push('\n'); } - heard_text.push_str(&line); + heard_text.push_str(line); } // The conversation as it is on screen (map 16.20.3): the room's lines before the new ones, verbatim, newest kept in // what `heard_bytes` leaves, then the new ones as above. The Persona's and the Interpreter's, never the Projector's @@ -876,6 +895,7 @@ fn chat_stimulus(inputs: &Inputs, tuning: &Tuning) -> (String, String, Overflow, // messages (`Packet::bodies`). let earlier = earlier_lines(inputs); let mut earlier_bodies = Vec::new(); + let mut earlier_block = String::new(); if !earlier.is_empty() { let (room, just) = (turn_room(inputs).unwrap_or_default(), "Just now:"); let heading = format!("Earlier in {room}:"); @@ -883,12 +903,48 @@ fn chat_stimulus(inputs: &Inputs, tuning: &Tuning) -> (String, String, Overflow, let rendered: Vec = earlier.iter().map(|(line, _)| line.clone()).collect(); let (kept, dropped) = newest_that_fit(&rendered, left); if !kept.is_empty() { - heard_text = format!("{heading}\n{}\n{just}\n{heard_text}", kept.join("\n")); + earlier_block = format!("{heading}\n{}", kept.join("\n")); + heard_text = format!("{earlier_block}\n{just}\n{heard_text}"); earlier_bodies = earlier[dropped..].iter().filter(|(_, line)| line.sender != inputs.people.own_sender).map(|(_, line)| line.text.clone()).collect(); } } + // The Persona's (map 16.21): the stream ends on the arrival, so what she reads next opens on it. Each person who + // turned to her, in the order their first shown line came, gets a bridge in the arrival's own who and where, then + // their new lines; the others' new lines after; the room's earlier lines last, as she would glance back up the + // screen. The same selection as `heard`, so no line is in one and not the other. + let shown = addressed.each(heard); + let mut people: Vec = Vec::new(); + for (line, _) in &shown { + let person = inputs.people.person_of(&line.sender); + if !people.contains(&person) { + people.push(person); + } + } + let her = forms(inputs.state); + let mut blocks: Vec = Vec::new(); + for person in &people { + let mut block: Vec = Vec::new(); + let name = present.iter().find(|(id, _)| id == person).map(|(_, name)| name.as_str()); + let place = turned.iter().find(|(id, _)| id == person).map(|(_, place)| place.as_str()); + if let (Some(name), Some(place)) = (name, place) { + block.push(bridge(her, name, place)); + } + block.extend(shown.iter().filter(|(line, _)| inputs.people.person_of(&line.sender) == *person).map(|(_, text)| text.clone())); + blocks.push(block.join("\n")); + } + if let Some(last) = blocks.last_mut().filter(|_| left_out > 0) { + last.push('\n'); + last.push_str(&addressed.not_shown(obj)); + } + if !blocks.is_empty() && !others_kept.is_empty() { + blocks.push(format!("Also just now:\n{}", others_kept.join("\n"))); + } + if !blocks.is_empty() && !earlier_block.is_empty() { + blocks.push(earlier_block); + } + let bridged = blocks.join("\n\n"); let overflow = Overflow { stimulus: stimulus.len().saturating_sub(tuning.select.stimulus_bytes), heard: heard_text.len().saturating_sub(tuning.select.heard_bytes), said_to_her_not_shown: left_out, clipped_bytes: clipped }; - (stimulus, heard_text, overflow, earlier_bodies) + (stimulus, heard_text, bridged, overflow, earlier_bodies) } /// The turn room's lines before this turn's new messages, oldest first, as the Persona reads them: who (her own lines by @@ -1073,6 +1129,13 @@ fn bond_line(bond: &Value, name: &str, tuning: &Tuning, scope: Scope, her: Forms line } +/// What opens the Persona's reading of one person's new lines to her (map 16.21): the arrival picked up in its own +/// terms, the person's name and where as the packet's arrival names them (`turned_to_her`, the act on their own line), +/// and "just now", so the lines read as the very thing the narration's arrival brought. In her pronouns (16.19.7). +pub(crate) fn bridge(her: Forms, name: &str, place: &str) -> String { + format!("Then {} read{} what {name} wrote to {}, {place}, just now:", her.subj, her.s, her.obj) +} + impl Packet { /// Who she is, as the Projector is told: her name and pronouns, then her voice. pub(crate) fn identity(&self) -> String { @@ -1590,11 +1653,15 @@ pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet { let mut draw = Draw(inputs.seed); let (name, voice, identity_clipped) = identity_of(inputs.state, tuning); let mut clipped: Vec = if identity_clipped { vec!["(identity)".to_owned()] } else { Vec::new() }; - let (stimulus, heard, overflow, earlier_bodies) = match inputs.kind { - TurnKind::Chat => chat_stimulus(inputs, tuning), - TurnKind::Beat => (beat_stimulus(inputs, tuning), beat_heard(inputs, tuning), Overflow::default(), Vec::new()), - TurnKind::FollowUp => (clip(&format!("{} just found: {}", forms(inputs.state).cap_subj, inputs.observation.unwrap_or("nothing")), tuning.select.stimulus_bytes), String::new(), Overflow::default(), Vec::new()), - TurnKind::Reflection => (reflection_stimulus(inputs, tuning), String::new(), Overflow::default(), Vec::new()), + // Who is present and who turned to her: the arrival on their own lines, and the bridges to their words, from one + // reading, so the two never disagree (map 16.21). + let present = present_people(inputs, tuning); + let turned = turned_to_her(inputs); + let (stimulus, heard, bridged, overflow, earlier_bodies) = match inputs.kind { + TurnKind::Chat => chat_stimulus(inputs, tuning, &present, &turned), + TurnKind::Beat => (beat_stimulus(inputs, tuning), beat_heard(inputs, tuning), String::new(), Overflow::default(), Vec::new()), + TurnKind::FollowUp => (clip(&format!("{} just found: {}", forms(inputs.state).cap_subj, inputs.observation.unwrap_or("nothing")), tuning.select.stimulus_bytes), String::new(), String::new(), Overflow::default(), Vec::new()), + TurnKind::Reflection => (reflection_stimulus(inputs, tuning), String::new(), String::new(), Overflow::default(), Vec::new()), }; // What reaches her on a turn no one spoke to her, as the Projector is shown it: never the words said to her (I16.7). // A follow-up's finding and a reflection's stock-take are no one's speech, and stay. A chat turn's is room state. @@ -1647,7 +1714,6 @@ pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet { unshown = (shown..outcome.len()).map(|at| outcome.line(at, her, usize::MAX, &bodies)).collect(); } } - let present = present_people(inputs, tuning); let present_ids: BTreeSet = present.iter().map(|(id, _)| id.clone()).collect(); let bonds = items(inputs.state, &["affect", "socialBonds"]); context.force(Section::Here, format!("- now: {}", inputs.now.format("%A %-d %B, %H:%M UTC"))); @@ -1665,7 +1731,6 @@ pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet { let mut named = Vec::new(); let mut private = Vec::new(); let mut full = 0; - let turned = turned_to_her(inputs); // Whoever turned to her this turn is placed first, so a bond line's room goes to them before anyone else present. let arrived = |id: &String| turned.iter().any(|(person, _)| person == id); let order = present.iter().filter(|(id, _)| arrived(id)).chain(present.iter().filter(|(id, _)| !arrived(id))); @@ -1775,5 +1840,5 @@ pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet { .map(|(at, (event, room, text))| Amendable { id: format!("a{}", at + 1), event: event.clone(), room: room.clone(), text: text.clone() }) .collect(); let selected = taken.into_iter().map(|candidate| Pick { id: candidate.id, pool: candidate.pool, log_score: candidate.log_score }).collect(); - Packet { kind: inputs.kind, seed: inputs.seed, lens, name, voice, context: context.render(), sections: context.sizes(), stimulus, heard, overflow, present: present_names, ids, selected, forms: her, people: people_in_view(inputs, tuning), affordances: inputs.affordances.to_vec(), room: turn_room(inputs).map(str::to_owned), room_kind: turn_room(inputs).map_or(RoomKind::Public, |room| room_kind(inputs, room)), bodies, private, clipped, amendable, arrival, speaking: conversation.speaking.unwrap_or_default(), unshown } + Packet { kind: inputs.kind, seed: inputs.seed, lens, name, voice, context: context.render(), sections: context.sizes(), stimulus, heard, bridged, overflow, present: present_names, ids, selected, forms: her, people: people_in_view(inputs, tuning), affordances: inputs.affordances.to_vec(), room: turn_room(inputs).map(str::to_owned), room_kind: turn_room(inputs).map_or(RoomKind::Public, |room| room_kind(inputs, room)), bodies, private, clipped, amendable, arrival, speaking: conversation.speaking.unwrap_or_default(), unshown } } diff --git a/crates/mind-body/src/projection/tests.rs b/crates/mind-body/src/projection/tests.rs index 166a481..d082404 100644 --- a/crates/mind-body/src/projection/tests.rs +++ b/crates/mind-body/src/projection/tests.rs @@ -2120,7 +2120,13 @@ fn k15_the_speaking_cues_reach_the_persona_and_the_interpreter_and_never_the_pro } let persona = narrate::persona_prompt(&packet, "The room is warm.", &tuning); let speech = interpret::speech_prompt(&packet, "The room is warm.", "y", &tuning); - assert!(persona.contains(&format!("The room is warm.\n\nThe room: {}", packet.speaking)), "{kind:?}: at the head of what she reads after the stream: {persona}"); + // A beat's cues head what she reads after the stream; on a chat turn someone turned to her, the stream ends on the + // arrival, so the bridged lines come first and the cues last (map 16.21). + let cued = match kind { + TurnKind::Chat => format!("The room is warm.\n\n{}\n\nThe room: {}", packet.bridged, packet.speaking), + _ => format!("The room is warm.\n\nThe room: {}", packet.speaking), + }; + assert!(persona.contains(&cued), "{kind:?}: {persona}"); assert!(speech.contains(&format!("({}).\nThe conversation: {}\nWho is in it", packet.room_kind.words(), packet.speaking)), "{kind:?}: beside the room: {speech}"); } // The room line stays the Projector's: who has been talking with whom. @@ -2304,3 +2310,110 @@ fn the_arrival_survives_a_busy_room() { within_guards(&packet, &tuning); assert!(packet.context.contains("- bond [bond-libby] Libby") && packet.context.contains(" Just now Libby turned to her, here.") && packet.context.contains("\n- the conversation: Lately Libby with her"), "{}", packet.context); } + +/// Map 16.21: what the Persona reads after the stream opens on the arrival the stream ended on. A bonded sender: the +/// bridge names them as their bond line does, their words follow it, the others' new lines and the speech cues after. +#[test] +fn k22_the_persona_reads_a_bonded_senders_words_as_the_arrival() { + let room = Room { + messages: vec![message("#swarm:gamecult.org", OPERATOR, "Meta", "Eureka, are you around?", 2, true), message("#swarm:gamecult.org", "@kiko:gamecult.org", "Kiko", "hi", 1, false)], + here: Some("#swarm:gamecult.org".into()), + ..Room::default() + }; + let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1); + let read = narrate::read_after(&packet); + assert_eq!( + read, + "Then she reads what Meta, the operator wrote to her, here, just now:\nMeta, the operator, in #swarm:gamecult.org: \"Eureka, are you around?\"\n\nAlso just now:\nKiko, in #swarm:gamecult.org: \"hi\"\n\nThe room: Meta, the operator is waiting on her. Meta, the operator last spoke 2 min ago." + ); + let persona = narrate::persona_prompt(&packet, "Meta turned to her, here, just now.", &Tuning::default()); + assert!(persona.contains(&format!("Meta turned to her, here, just now.\n\n{read}")), "{persona}"); + // The Interpreter's stream is as it was: no bridge. + let speech = interpret::speech_prompt(&packet, "Meta turned to her, here, just now.", "y", &Tuning::default()); + assert!(!speech.contains("Then she reads") && speech.contains(&packet.heard), "{speech}"); +} + +/// An unbonded sender: the bridge names them as the also-here line does, display name and id. +#[test] +fn k22_the_persona_reads_an_unbonded_senders_words_as_the_arrival() { + let room = Room { messages: vec![message("#swarm:gamecult.org", "@deru:gamecult.org", "Deru", "Eureka, quick one", 2, true)], here: Some("#swarm:gamecult.org".into()), ..Room::default() }; + let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1); + assert_eq!( + narrate::read_after(&packet), + format!( + "Then she reads what \"Deru\" (@deru:gamecult.org) wrote to her, here, just now:\n\"Deru\" (@deru:gamecult.org), in #swarm:gamecult.org: \"Eureka, quick one\"\n\nThe room: {}", + packet.speaking + ) + ); +} + +/// The bridge's who and where are the arrival's: the same name and place as the act on the person's own line, for a +/// bonded and an unbonded sender, here and in another room. +#[test] +fn k22_the_bridge_names_who_and_where_as_the_arrival_does() { + const SW: &str = "#swarm:gamecult.org"; + const LAB: &str = "#lab:gamecult.org"; + for (sender, display, name, bonded) in [(OPERATOR, "Meta", "Meta, the operator".to_owned(), true), ("@deru:gamecult.org", "Deru", "\"Deru\" (@deru:gamecult.org)".to_owned(), false)] { + for (room, place) in [(SW, "here".to_owned()), (LAB, format!("in {LAB}"))] { + let here = Room { messages: vec![message(room, sender, display, "Eureka, a word", 2, true)], here: Some(SW.into()), ..Room::default() }; + let packet = packet_of(TurnKind::Chat, &state(), &here, &History::default(), 1); + let arrival = match (bonded, place.as_str()) { + (true, _) => format!(" Just now {name} turned to her, {place}."), + (false, "here") => format!("{name}, who turned to her just now"), + (false, _) => format!("{name}, who turned to her just now, {place}"), + }; + assert!(packet.context.contains(&arrival), "{name} {place}: {}", packet.context); + assert!(packet.bridged.starts_with(&format!("Then she reads what {name} wrote to her, {place}, just now:\n")), "{name} {place}: {}", packet.bridged); + } + } +} + +/// Two senders: one bridge each, in the order their first line came, each followed by that person's lines alone. +#[test] +fn k22_each_sender_gets_a_bridge_of_their_own() { + const SW: &str = "#swarm:gamecult.org"; + let room = Room { + messages: vec![ + message(SW, OPERATOR, "Meta", "Eureka, are you around?", 3, true), + message(SW, "@deru:gamecult.org", "Deru", "Eureka, quick one", 2, true), + message(SW, OPERATOR, "Meta", "Also the door.", 1, true), + ], + here: Some(SW.into()), + ..Room::default() + }; + let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1); + assert!( + packet.bridged.starts_with( + "Then she reads what Meta, the operator wrote to her, here, just now:\nMeta, the operator, in #swarm:gamecult.org: \"Eureka, are you around?\"\nMeta, the operator, in #swarm:gamecult.org: \"Also the door.\"\n\nThen she reads what \"Deru\" (@deru:gamecult.org) wrote to her, here, just now:\n\"Deru\" (@deru:gamecult.org), in #swarm:gamecult.org: \"Eureka, quick one\"" + ), + "{}", + packet.bridged + ); + for line in packet.heard.lines() { + assert!(packet.bridged.contains(line), "every heard line is read: {line}"); + } +} + +/// No arrival, no bridge: a beat, and a chat turn no one turned to her, read as they did, the cues first. +#[test] +fn k22_a_turn_without_an_arrival_reads_as_it_did() { + let minds = Room { tail: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "", "lunch?", 90, false), message("#swarm:gamecult.org", "@kiko:gamecult.org", "", "later", 80, false)], ..Room::default() }; + let beat = packet_of(TurnKind::Beat, &state(), &minds, &History::default(), 1); + assert!(beat.bridged.is_empty() && !beat.speaking.is_empty() && !beat.heard.is_empty()); + assert_eq!(narrate::read_after(&beat), format!("The room: {}\n{}", beat.speaking, beat.heard)); + let chatter = Room { messages: vec![message("#swarm:gamecult.org", "@kiko:gamecult.org", "Kiko", "hi all", 1, false)], here: Some("#swarm:gamecult.org".into()), ..Room::default() }; + let chat = packet_of(TurnKind::Chat, &state(), &chatter, &History::default(), 1); + assert!(chat.bridged.is_empty() && !chat.heard.is_empty(), "{}", chat.bridged); +} + +/// The bridge in her pronouns (16.19.7). +#[test] +fn k22_the_bridge_is_in_her_pronouns() { + for (pronouns, bridge) in [ + ("she/her", "Then she reads what Meta wrote to her, here, just now:"), + ("he/him", "Then he reads what Meta wrote to him, here, just now:"), + ("they/them", "Then they read what Meta wrote to them, here, just now:"), + ] { + assert_eq!(select::bridge(select::forms(&json!({ "presentation": { "pronouns": pronouns } })), "Meta", "here"), bridge); + } +}