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
//! Conversation context management
use super::{Message, Tool};
use serde::{Deserialize, Serialize};
/// Conversation context for LLM interactions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Context {
/// System prompt sent with each request
#[serde(skip_serializing_if = "Option::is_none")]
pub system_prompt: Option<String>,
/// Conversation history
pub messages: Vec<Message>,
/// Available tools for this context
#[serde(default)]
pub tools: Vec<Tool>,
}
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
impl Context {
/// Create a new empty context
///
/// # Examples
///
/// ```
/// use oxi_ai::Context;
/// let mut ctx = Context::new();
/// ctx.set_system_prompt("You are a helpful assistant.");
/// ```
pub fn new() -> Self {
Self {
system_prompt: None,
messages: Vec::new(),
tools: Vec::new(),
}
}
/// Create a context with a system prompt
pub fn with_system_prompt(mut self, prompt: impl Into<String>) -> Self {
self.system_prompt = Some(prompt.into());
self
}
/// Add a message to the context
///
/// # Examples
///
/// ```
/// use oxi_ai::{Context, Message, UserMessage};
/// let mut ctx = Context::new();
/// ctx.add_message(Message::User(UserMessage::new("Hello!")));
/// assert_eq!(ctx.len(), 1);
/// ```
pub fn add_message(&mut self, message: Message) {
self.messages.push(message);
}
/// Get a message by index
pub fn message(&self, index: usize) -> Option<&Message> {
self.messages.get(index)
}
/// Get the last message
///
/// # Examples
///
/// ```
/// use oxi_ai::{Context, Message, UserMessage};
/// let mut ctx = Context::new();
/// ctx.add_message(Message::User(UserMessage::new("First")));
/// ctx.add_message(Message::User(UserMessage::new("Second")));
/// assert!(ctx.last_message().is_some());
/// ```
pub fn last_message(&self) -> Option<&Message> {
self.messages.last()
}
/// Check if context has any messages
pub fn is_empty(&self) -> bool {
self.messages.is_empty()
}
/// Get number of messages
pub fn len(&self) -> usize {
self.messages.len()
}
/// Set the system prompt
pub fn set_system_prompt(&mut self, prompt: impl Into<String>) {
self.system_prompt = Some(prompt.into());
}
/// Clear the system prompt
pub fn clear_system_prompt(&mut self) {
self.system_prompt = None;
}
/// Set available tools
///
/// # Examples
///
/// ```
/// use oxi_ai::{Context, Tool};
/// let mut ctx = Context::new();
/// let tool = Tool::new(
/// "search",
/// "Search the web",
/// serde_json::json!({"type": "object", "properties": {}}),
/// );
/// ctx.set_tools(vec![tool]);
/// assert_eq!(ctx.tools.len(), 1);
/// ```
pub fn set_tools(&mut self, tools: Vec<Tool>) {
self.tools = tools;
}
/// Add a tool
pub fn add_tool(&mut self, tool: Tool) {
self.tools.push(tool);
}
/// Get the system prompt for this context.
pub fn system_prompt(&self) -> Option<&str> {
self.system_prompt.as_deref()
}
/// Serialize context to a JSON string.
pub fn to_json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}
/// Deserialize a context from a JSON string.
pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(json)
}
/// Clone the context
pub fn clone(&self) -> Self {
Self {
system_prompt: self.system_prompt.clone(),
messages: self.messages.clone(),
tools: self.tools.clone(),
}
}
}
impl From<Vec<Message>> for Context {
fn from(messages: Vec<Message>) -> Self {
Self {
system_prompt: None,
messages,
tools: Vec::new(),
}
}
}
impl From<Message> for Context {
fn from(message: Message) -> Self {
Self {
system_prompt: None,
messages: vec![message],
tools: Vec::new(),
}
}
}