Files
rhema-together/src/room/message_tool.rs
T
vhaudiquet 2e17e7463c
ci / checks (push) Successful in 56s
ci / test (push) Successful in 1m1s
ci / chart (push) Successful in 6s
ci / web (push) Successful in 29s
ci / docker (push) Successful in 2m37s
ci / publish-edge-image (push) Successful in 11s
ci / publish-edge-chart (push) Successful in 4s
feat: add image attachments for vision models
A chat line can carry up to four images, each capped at 5 MB and
sniffed from its bytes. Uploads go to a room route first, the message
references the ids, and the bytes are served back to members; an
upload no message claims within an hour is swept on the next send.

Images only make sense when the model can look at them, and the
catalog's answer is tri-state, so the gate is a stored fact, not a
guess: the provider listing remembers supports_vision per kind and
model, the upload and send routes refuse when the room's effective
model is not a known vision model, and the composer hides the attach
button on the same fact, which rides the room payload. At turn time
the fact goes to the host agent, which sends the blocks on a vision
model and leaves a note where each image was on the rest.
2026-12-01 21:59:53 +01:00

94 lines
3.0 KiB
Rust

//! The `message` tool: the agent's only way into the room.
use async_trait::async_trait;
use rhema_core::request::ToolSpec;
use rhema_core::tool::{ReplaySafety, ToolCall, ToolContext, ToolResolver, ToolResult};
use serde_json::json;
use crate::error::AppResult;
use crate::models::Message;
use crate::state::Shared;
use crate::util::{mentions_agent, now_ms, random_id};
/// Posts an agent message into the room log.
pub fn post_agent_message(state: &Shared, room: &str, text: &str) -> AppResult<Message> {
let text = text.trim();
let seq = state.hub.next_seq(&state.db, room);
let message = Message {
seq,
id: random_id(16),
author_id: None,
author_name: "rhema".into(),
author_kind: "agent".into(),
text: text.to_owned(),
mentions: mentions_agent(text),
created_at: now_ms(),
attachments: Vec::new(),
};
let data = serde_json::to_value(&message)?;
state.hub.insert(&state.db, room, seq, "message", &data)?;
state.hub.broadcast(
room,
crate::models::Envelope {
seq,
kind: "message".into(),
data,
},
);
Ok(message)
}
/// The resolver handed to the agent as an extra tool.
pub struct MessageTool {
state: Shared,
room: String,
}
impl MessageTool {
/// A tool posting into `room`.
pub fn new(state: Shared, room: String) -> Self {
Self { state, room }
}
}
#[async_trait]
impl ToolResolver for MessageTool {
fn specs(&self) -> Vec<ToolSpec> {
vec![ToolSpec::new(
"message",
"Send a message to the people in the room. This is the only way you \
speak: text outside this tool is not shown. One short message per \
update. Always send one before your last message.",
json!({
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "The message, in plain language. Address people by name."
}
},
"required": ["text"]
}),
)]
}
fn replay_safety(&self, _name: &str) -> ReplaySafety {
// Posting twice would duplicate a line in the room. A resume
// reports the call interrupted instead of re-running it.
ReplaySafety::Unsafe
}
async fn resolve(&self, call: ToolCall, _ctx: &ToolContext) -> ToolResult {
let Some(text) = call.args.get("text").and_then(|value| value.as_str()) else {
return ToolResult::failure(call.id, "`text` must be a string");
};
if text.trim().is_empty() {
return ToolResult::failure(call.id, "the message is empty");
}
match post_agent_message(&self.state, &self.room, text) {
Ok(_) => ToolResult::success(call.id, "sent"),
Err(err) => ToolResult::failure(call.id, format!("could not post: {}", err.message)),
}
}
}