Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CONTEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,17 @@ Right-click on the sprite — character switching, settings, quit.
**Summon**:
The deliberate act that opens the Functional Layer.
_Avoid_: Invoke, activate, wake

### Expression

**Speech bubble**:
A bubble above the sprite showing Frame.dialogue, held for reading time (900ms
+ 55ms per character, clamped to 2–8 s). A new line replaces the old one.
Implemented in #119.
_Avoid_: Chat bubble, message, tooltip

**Thinking ellipsis**:
Three animated dots in a bubble above the sprite, shown while a reactive
Director turn is in flight (Poke, Summon, Throw). Appears after 250ms grace,
held ≥600ms once shown. Ambient wakes stay invisible. #119.
_Avoid_: Loading, spinner, progress
4 changes: 2 additions & 2 deletions crates/core/src/director.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ impl<C: Completer> ModelDirector<C> {
}
}

/// A reply that is not a declared Behavior. Empty name: the Engine speaks
/// and starts nothing. #17 will show this in a bubble.
/// A reply that is not a declared Behavior. Empty name: the Engine plays
/// `talk` and speaks. #119 shows this in a bubble.
fn as_speech(reply: &str) -> Option<BehaviorProposal> {
let text = reply.trim();
let text = text
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ mod tests {

fn window(owner: &str, x: f64, y: f64, width: f64, height: f64) -> WindowRect {
WindowRect {
id: 0,
bounds: Rect {
x,
y,
Expand Down
79 changes: 79 additions & 0 deletions crates/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,9 @@ impl Engine {
//
// #84: Do Not Disturb refuses proposals before they reach the State
// gate, so the Character stops starting things while staying visible.
//
// #119: dialogue with an empty behavior plays `talk`. Duration is
// PRIMITIVE_MS, independent of bubble reading time.
let mut behavior = None;
if let Some(proposal) = &snapshot.proposal {
if !self.do_not_disturb {
Expand All @@ -454,6 +457,11 @@ impl Engine {
started = true;
behavior = Some(proposal.behavior.clone());
}
} else if proposal.behavior.is_empty()
&& proposal.dialogue.is_some()
&& self.play(&[Primitive::Talk])
{
started = true;
}
}
}
Expand Down Expand Up @@ -3918,4 +3926,75 @@ mod tests {
"the sprite settles to sleep without Director proposals waking it"
);
}

/// #119: dialogue with an empty behavior plays `talk` for PRIMITIVE_MS,
/// independent of bubble duration.
#[test]
fn dialogue_with_empty_behavior_plays_talk() {
let mut engine = a_resting_sprite();

let spoken = engine.tick(&WorldSnapshot {
proposal: Some(BehaviorProposal {
behavior: String::new(),
dialogue: Some("hello there".to_string()),
}),
..snapshot(100)
});

assert_eq!(
spoken.animation, "talk",
"dialogue with no Behavior plays talk"
);
assert_eq!(spoken.dialogue.as_deref(), Some("hello there"));

let playing: Vec<&'static str> = (0..10)
.map(|_| engine.tick(&snapshot(100)).animation)
.collect();

let talk_ticks = playing.iter().filter(|&&anim| anim == "talk").count();
assert!(
talk_ticks == 5,
"talk holds for PRIMITIVE_MS (600ms = 5 more ticks after the first): {playing:?}"
);
}

/// #119: dialogue with a playable Behavior plays both — the Behavior and
/// its dialogue are independent.
#[test]
fn dialogue_with_a_playable_behavior_plays_the_behavior_not_talk() {
let mut engine = a_resting_sprite();

let spoken = engine.tick(&WorldSnapshot {
proposal: Some(BehaviorProposal {
behavior: "greet".to_string(),
dialogue: Some("hi".to_string()),
}),
..snapshot(100)
});

assert_eq!(
spoken.animation, "react",
"greet plays its own animation, not talk"
);
assert_eq!(spoken.dialogue.as_deref(), Some("hi"));
assert_eq!(spoken.behavior, Some("greet".to_string()));
}

/// #119: a Behavior with no dialogue does not play talk.
#[test]
fn a_behavior_without_dialogue_does_not_play_talk() {
let mut engine = a_resting_sprite();

let silent = engine.tick(&WorldSnapshot {
proposal: Some(BehaviorProposal {
behavior: "greet".to_string(),
dialogue: None,
}),
..snapshot(100)
});

assert_eq!(silent.animation, "react", "greet plays its own animation");
assert_eq!(silent.dialogue, None);
assert_eq!(silent.behavior, Some("greet".to_string()));
}
}
4 changes: 2 additions & 2 deletions docs/SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ Two implementations ship:
Director is disabled, and as the fallback on any session error or timeout. The Shell
may wake it often: the path is free.
- **Session** — an attached Harness, the same conversation as chat. Proposes a Behavior
identifier plus optional dialogue. A reply that is not a declared Behavior is
spoken (`say:`) and starts nothing; #17 will put that in a bubble. Until a
identifier plus optional dialogue. A reply that is not a declared Behavior plays
`talk` and is spoken (`say:`); #119 put that in a bubble. Until a
Harness exists, an HTTP Completer stands in behind the same trait
([ADR-0008](./adr/0008-one-harness-session.md)).

Expand Down
91 changes: 57 additions & 34 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct Drawn {
///
/// Pushed every tick rather than fetched, so the webview holds no authoritative
/// state — it draws what it was last told and remembers nothing.
#[derive(Clone, Copy, Serialize)]
#[derive(Clone, Serialize)]
struct Placement<'a> {
x: i32,
y: i32,
Expand All @@ -123,6 +123,13 @@ struct Placement<'a> {
fade_ms: u32,
/// -1 to mirror the art (heading left), 1 to draw it as authored.
facing: i8,
/// A line to speak on this tick only. Dialogue is an event, not a state.
/// #119: the webview latches it and owns display duration.
dialogue: Option<String>,
/// Whether to show the thinking ellipsis. Derived from InFlight state and
/// addressed. #119: grace and min-hold are in the webview so the Engine
/// stays tick-pure.
thinking: bool,
}

/// Every Animation's frames as `data:` URLs, in play order, keyed by the
Expand Down Expand Up @@ -638,41 +645,55 @@ fn run_frame_loop(
}

// After the tick so a Throw is already Falling, not still Dragged.
if let (Some(model), Some(activity)) = (&model, last_activity.as_ref()) {
if director::session_due(
addressed,
since_ambient,
&pace,
activity.displays_asleep,
engine.do_not_disturb(),
) && pending.ready()
&& !applied
{
let context = Context {
activity: activity.clone(),
recent: recent.clone(),
personality: character.personality.clone(),
state: frame.state,
happened,
standing: assembler.standing_on(frame.position),
};
if addressed {
pace.after_reactive();
let reactive_wake =
if let (Some(model), Some(activity)) = (&model, last_activity.as_ref()) {
if director::session_due(
addressed,
since_ambient,
&pace,
activity.displays_asleep,
engine.do_not_disturb(),
) && pending.ready()
&& !applied
{
let context = Context {
activity: activity.clone(),
recent: recent.clone(),
personality: character.personality.clone(),
state: frame.state,
happened,
standing: assembler.standing_on(frame.position),
};
let was_addressed = addressed;
if addressed {
pace.after_reactive();
} else {
pace.after_ambient();
}
addressed = false;
happened = Happened::Ambient;
since_ambient = Duration::ZERO;
let payload = model.prompt(&context);
if let Ok(mut inspect) = inspect.lock() {
inspect.last_payload = Some(payload);
inspect.wake_secs = pace.wait().as_secs();
}
pending.start(Arc::clone(model), context.clone());
in_flight = Some(context);
was_addressed
} else {
pace.after_ambient();
false
}
addressed = false;
happened = Happened::Ambient;
since_ambient = Duration::ZERO;
let payload = model.prompt(&context);
if let Ok(mut inspect) = inspect.lock() {
inspect.last_payload = Some(payload);
inspect.wake_secs = pace.wait().as_secs();
}
pending.start(Arc::clone(model), context.clone());
in_flight = Some(context);
}
}
} else {
false
};

let thinking = !pending.ready()
&& (reactive_wake
|| in_flight
.as_ref()
.is_some_and(|ctx| ctx.happened != Happened::Ambient))
&& !engine.do_not_disturb();

// What the user has seen is what the Engine played, not what the
// Director asked for: a proposal the State refuses never reaches
Expand Down Expand Up @@ -832,6 +853,8 @@ fn run_frame_loop(
visible: presence.visible,
fade_ms: presence.fade_ms,
facing: frame.facing as i8,
dialogue: frame.dialogue.clone(),
thinking,
},
);

Expand Down
74 changes: 74 additions & 0 deletions src/bubble.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Bubble rendering logic for speech and thinking indicators.

export const CEILING_CLEARANCE = 128;

const MIN_DURATION_MS = 2000;
const MAX_DURATION_MS = 8000;
const BASE_DURATION_MS = 900;
const MS_PER_CHAR = 55;
const MAX_LINES = 6;

export function bubbleDuration(text) {
const duration = BASE_DURATION_MS + text.length * MS_PER_CHAR;
return Math.max(MIN_DURATION_MS, Math.min(MAX_DURATION_MS, duration));
}

export function wrapText(text, maxWidth, measureFn) {
const lines = [];
const paragraphs = text.split("\n");

for (const paragraph of paragraphs) {
if (lines.length >= MAX_LINES) break;

const words = paragraph.split(" ");
let currentLine = "";

for (const word of words) {
if (lines.length >= MAX_LINES) break;

const testLine = currentLine ? `${currentLine} ${word}` : word;
const metrics = measureFn(testLine);

if (metrics.width > maxWidth && currentLine) {
lines.push(currentLine);
currentLine = word;
} else {
currentLine = testLine;
}
}

if (currentLine && lines.length < MAX_LINES) {
lines.push(currentLine);
}
}

if (lines.length === MAX_LINES && (lines.length < paragraphs.length ||
paragraphs[paragraphs.length - 1].split(" ").length > lines[lines.length - 1].split(" ").length)) {
lines[MAX_LINES - 1] = lines[MAX_LINES - 1].trimEnd() + "…";
}

return lines.slice(0, MAX_LINES);
}

export function placeBubble(spriteRect, bubbleSize, displayBounds, ceilingClearance) {
const spriteHeadY = spriteRect.y;
const spriteBottomY = spriteRect.y + spriteRect.height;
const spriteCenterX = spriteRect.x + spriteRect.width / 2;

let x = spriteCenterX - bubbleSize.width / 2;
let y = spriteHeadY - bubbleSize.height - 10;
let flipped = false;

if (spriteHeadY < ceilingClearance) {
y = spriteBottomY + 10;
flipped = true;
}

const unclamped = x;
x = Math.max(displayBounds.x, Math.min(x, displayBounds.x + displayBounds.width - bubbleSize.width));
y = Math.max(displayBounds.y, Math.min(y, displayBounds.y + displayBounds.height - bubbleSize.height));

const tailOffset = spriteCenterX - (x + bubbleSize.width / 2);

return { x, y, flipped, tailOffset };
}
10 changes: 10 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
<link rel="stylesheet" href="main.css" />
</head>
<body>
<div id="bubble" class="bubble hidden">
<div class="bubble-content"></div>
</div>
<div id="thinking-bubble" class="bubble thinking-bubble hidden">
<div class="thinking-dots">
<span></span>
<span></span>
<span></span>
</div>
</div>
<img id="sprite" alt="" />
<script type="module" src="main.js"></script>
</body>
Expand Down
Loading
Loading