1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Message component for chat-style interfaces
//!
//! Provides styled message components for different roles (user, assistant, system, tool).
use crate::components::{Box, Text};
use crate::core::{Color, Element, FlexDirection};
/// Message role in a chat interface
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageRole {
/// User message (typically with > prefix, yellow)
User,
/// Assistant/AI message (typically with ● prefix, white)
Assistant,
/// System message (typically with ● prefix, cyan)
System,
/// Tool call message (typically with ● prefix, magenta)
Tool,
/// Tool result message (typically with ⎿ prefix, gray)
ToolResult,
/// Error message (typically with ● prefix, red)
Error,
}
/// A styled message component for chat interfaces
///
/// # Example
///
/// ```ignore
/// use rnk::components::{Message, MessageRole};
///
/// // User message
/// let msg = Message::new(MessageRole::User, "Hello, world!");
/// rnk::println(msg.into_element());
///
/// // Assistant message
/// let msg = Message::assistant("Hi! How can I help?");
/// rnk::println(msg.into_element());
/// ```
pub struct Message {
role: MessageRole,
content: String,
prefix: Option<String>,
}
impl Message {
/// Create a new message with the specified role and content
pub fn new(role: MessageRole, content: impl Into<String>) -> Self {
Self {
role,
content: content.into(),
prefix: None,
}
}
/// Create a user message
pub fn user(content: impl Into<String>) -> Self {
Self::new(MessageRole::User, content)
}
/// Create an assistant message
pub fn assistant(content: impl Into<String>) -> Self {
Self::new(MessageRole::Assistant, content)
}
/// Create a system message
pub fn system(content: impl Into<String>) -> Self {
Self::new(MessageRole::System, content)
}
/// Create a tool call message
pub fn tool(content: impl Into<String>) -> Self {
Self::new(MessageRole::Tool, content)
}
/// Create a tool result message
pub fn tool_result(content: impl Into<String>) -> Self {
Self::new(MessageRole::ToolResult, content)
}
/// Create an error message
pub fn error(content: impl Into<String>) -> Self {
Self::new(MessageRole::Error, content)
}
/// Set a custom prefix (overrides default role prefix)
pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
self.prefix = Some(prefix.into());
self
}
/// Get the default prefix for this role
fn default_prefix(&self) -> &str {
match self.role {
MessageRole::User => "> ",
MessageRole::Assistant => "● ",
MessageRole::System => "● ",
MessageRole::Tool => "● ",
MessageRole::ToolResult => " ⎿ ",
MessageRole::Error => "● ",
}
}
/// Get the color for this role
fn color(&self) -> Color {
match self.role {
MessageRole::User => Color::Yellow,
MessageRole::Assistant => Color::BrightWhite,
MessageRole::System => Color::Cyan,
MessageRole::Tool => Color::Magenta,
MessageRole::ToolResult => Color::Ansi256(245),
MessageRole::Error => Color::Red,
}
}
/// Get the prefix color for this role
fn prefix_color(&self) -> Color {
match self.role {
MessageRole::User => Color::Yellow,
MessageRole::Assistant => Color::BrightWhite,
MessageRole::System => Color::Cyan,
MessageRole::Tool => Color::Magenta,
MessageRole::ToolResult => Color::Ansi256(245),
MessageRole::Error => Color::Red,
}
}
/// Convert to an Element
pub fn into_element(self) -> Element {
let prefix = self
.prefix
.as_deref()
.unwrap_or_else(|| self.default_prefix());
let prefix_color = self.prefix_color();
let content_color = self.color();
let mut container = Box::new().flex_direction(FlexDirection::Row);
// Add prefix
if !prefix.is_empty() {
container =
container.child(Text::new(prefix).color(prefix_color).bold().into_element());
}
// Add content
container = container.child(Text::new(&self.content).color(content_color).into_element());
container.into_element()
}
}
/// Tool call message with name and arguments
///
/// # Example
///
/// ```ignore
/// use rnk::components::ToolCall;
///
/// let tool = ToolCall::new("read_file", "path=/tmp/test.txt");
/// rnk::println(tool.into_element());
/// ```
pub struct ToolCall {
name: String,
args: String,
}
impl ToolCall {
/// Create a new tool call message
pub fn new(name: impl Into<String>, args: impl Into<String>) -> Self {
Self {
name: name.into(),
args: args.into(),
}
}
/// Convert to an Element
pub fn into_element(self) -> Element {
Box::new()
.flex_direction(FlexDirection::Row)
.child(Text::new("● ").color(Color::Magenta).into_element())
.child(
Text::new(&self.name)
.color(Color::Magenta)
.bold()
.into_element(),
)
.child(
Text::new(format!("({})", self.args))
.color(Color::Magenta)
.into_element(),
)
.into_element()
}
}
/// Thinking block for displaying AI reasoning
///
/// # Example
///
/// ```ignore
/// use rnk::components::ThinkingBlock;
///
/// let thinking = ThinkingBlock::new("Analyzing the problem...\nConsidering options...");
/// rnk::println(thinking.into_element());
/// ```
pub struct ThinkingBlock {
content: String,
max_lines: usize,
}
impl ThinkingBlock {
/// Create a new thinking block
pub fn new(content: impl Into<String>) -> Self {
Self {
content: content.into(),
max_lines: 5,
}
}
/// Set the maximum number of lines to display
pub fn max_lines(mut self, max_lines: usize) -> Self {
self.max_lines = max_lines;
self
}
/// Convert to an Element
pub fn into_element(self) -> Element {
let lines: Vec<&str> = self.content.lines().take(self.max_lines).collect();
let has_more = self.content.lines().count() > self.max_lines;
let mut container = Box::new().flex_direction(FlexDirection::Column).child(
Text::new("● Thinking...")
.color(Color::Magenta)
.into_element(),
);
for line in lines {
container = container.child(
Box::new()
.flex_direction(FlexDirection::Row)
.child(Text::new(" ").into_element())
.child(Text::new(line).color(Color::Magenta).dim().into_element())
.into_element(),
);
}
if has_more {
container = container.child(
Text::new(" ...")
.color(Color::Ansi256(245))
.dim()
.into_element(),
);
}
container.into_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_message_creation() {
let msg = Message::user("Hello");
let element = msg.into_element();
assert!(!element.children.is_empty());
}
#[test]
fn test_message_roles() {
let _user = Message::user("test");
let _assistant = Message::assistant("test");
let _system = Message::system("test");
let _tool = Message::tool("test");
let _error = Message::error("test");
}
#[test]
fn test_tool_call() {
let tool = ToolCall::new("read_file", "path=/tmp/test.txt");
let element = tool.into_element();
assert!(!element.children.is_empty());
}
#[test]
fn test_thinking_block() {
let thinking = ThinkingBlock::new("Line 1\nLine 2\nLine 3");
let element = thinking.into_element();
assert!(!element.children.is_empty());
}
}