seedframe 0.1.2

A clean, macro driven Rust library for building LLM apps
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
use async_trait::async_trait;
use dashmap::DashMap;
use schemars::gen::SchemaSettings;
use serde::Serializer;
use serde_json::json;
use std::{
    any::{Any, TypeId},
    sync::Arc,
};
use thiserror::Error;
use tokio::sync::RwLock;
use tracing::{error, info, instrument};

use crate::{
    embeddings::Embedder,
    tools::{ExecutionStrategy, ToolCall, ToolResponse, ToolSet, ToolSetError},
    vector_store::VectorStoreError,
};

// Default top_n context documents to query from the vector store
const DEFAULT_TOP_N: usize = 1;

/// Messages exchanged with the completion model
#[derive(Debug, Clone, PartialEq)]
pub enum Message {
    /// System prompt
    Preamble(String),
    /// Message sent by the user
    User {
        /// Text content of the message
        content: String,
        /// Optional tool execution results
        tool_responses: Option<Vec<ToolResponse>>,
    },
    /// Model-generated response
    Assistant {
        /// Text content of the response
        content: String,
        /// Optional requested tool calls
        tool_calls: Option<Vec<ToolCall>>,
    },
}

/// Tracks token usage statistics for model interactions
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct TokenUsage {
    /// Tokens consumed by the prompt input
    pub prompt_tokens: Option<u64>,
    /// Tokens generated in the completion output
    pub completion_tokens: Option<u64>,
    /// Combined total of prompt and completion tokens
    pub total_tokens: Option<u64>,
}

pub(crate) type MessageHistory = Vec<Message>;

#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone, Error)]
/// Errors that can happen during completion
pub enum CompletionError {
    /// Errors returned from the provider
    #[error("Provider error -> HTTP Status {0}: {1}")]
    ProviderError(u16, String),
    /// Error within the completion request
    #[error("RequestError: {0}")]
    RequestError(String),
    /// Error while parsing compleition reponse
    #[error("ParseError: {0}")]
    ParseError(String),
    /// Error while fetching the context from the vector store
    #[error("Failed to fetch context: {0}")]
    FailedContextFetch(#[from] VectorStoreError),
    /// Error while extracting
    #[error(transparent)]
    ExtractorError(#[from] ExtractionError),
    /// Error with tool call states
    #[error(transparent)]
    StateError(#[from] StateError),
}

/// Types that can be deserialized from model completion responses.
///
/// Requires JSON Schema generation and owned deserialization capabilities.
/// Implement this trait for types that should be extractable from model outputs.
pub trait Extractor: schemars::JsonSchema + serde::de::DeserializeOwned {}

/// Errors related to response extraction from completions
#[derive(Debug, Clone, Error)]
pub enum ExtractionError {
    #[error("Model does not support extraction")]
    ExtractionNotSupported,
}

/// Errors related to state management in the [`Client`]
#[derive(Debug, Clone, Error)]
pub enum StateError {
    /// Attempted to register state when state of this type already exists
    #[error("State with type {0} already exists on client")]
    AlreadyExists(String),
    /// Requested state type was not found in the client
    #[error("State not found")]
    NotFound,
}

/// Core trait defining the interface for completion models
#[allow(clippy::module_name_repetitions)]
#[async_trait]
pub trait CompletionModel: Send {
    /// Constructs a new [`Client`] with this model
    ///
    /// # Arguments
    /// * `preamble` - System prompt/instructions for the model
    /// * `embedder_instances` - Embedding models for context retrieval
    /// * `tools` - Collection of available tools
    fn build_client(
        self,
        preamble: impl AsRef<str>,
        embedder_instances: Vec<crate::embeddings::Embedder>,
        tools: ToolSet,
    ) -> Client<impl CompletionModel>;

    /// Sends a message to the model and returns its response
    ///
    /// # Arguments
    /// * `message` - The message to process
    /// * `history` - Conversation context
    /// * `tools` - Optional toolset for function calling
    /// * `temperature` - Sampling temperature (0.0-1.0)
    /// * `max_tokens` - Maximum tokens to be used
    async fn send(
        &mut self,
        message: Message,
        history: &MessageHistory,
        tools: Option<&ToolSet>,
        temperature: f64,
        max_tokens: usize,
    ) -> Result<(Message, TokenUsage), CompletionError>;

    #[allow(unused)]
    /// Extracts structured data from a model response
    ///
    /// Default implementation returns [`ExtractionError::ExtractionNotSupported`]
    /// unless overridden by the model implementation.
    async fn extract<T: Extractor>(
        &mut self,
        message: Message,
        history: &MessageHistory,
        temperature: f64,
        max_tokens: usize,
    ) -> Result<T, CompletionError> {
        Err(CompletionError::ExtractorError(
            ExtractionError::ExtractionNotSupported,
        ))
    }
}

/// Extractor for state
pub struct State<T: Send + Sync + 'static>(pub Arc<T>);

/// A client for managing interactions with a completion model, including conversation history,
/// tooling, state management, and token tracking.
///
/// The client is generic over any type implementing [`CompletionModel`] and provides:
/// - Thread-safe access to the underlying model
/// - Conversation history management
/// - Completion parameters configuration
/// - Tool integration through [`ToolSet`]
/// - Embeddings
/// - State management for tools
/// - Token usage tracking
pub struct Client<M: CompletionModel> {
    completion_model: Arc<tokio::sync::RwLock<M>>,
    /// Conversation history maintaining message context
    history: MessageHistory,
    /// Collection of available tools for the model to use
    tools: Box<ToolSet>,
    /// Embedding models for text vectorization
    embedders: Vec<Embedder>,
    /// Tracking of token usage statistics
    token_usage: TokenUsage,
    /// Type-mapped state storage for arbitrary values
    states: DashMap<TypeId, Box<dyn Any + Send + Sync>>,

    // common prompt parameters
    temperature: f64,
    max_tokens: usize,
}

#[allow(clippy::struct_excessive_bools)]
/// Builder for constructing and executing completion prompts
pub struct PromptBuilder<'a, M: CompletionModel> {
    prompt: String,
    client: &'a mut Client<M>,
    execute_tools: bool,
    with_tools: bool,
    append_tool_response: bool,
    one_shot: (bool, Option<MessageHistory>),
    with_context: bool,
}

impl<'a, M: CompletionModel> PromptBuilder<'a, M> {
    fn new(client: &'a mut Client<M>, prompt: impl Into<String>) -> Self {
        Self {
            prompt: prompt.into(),
            client,
            execute_tools: true,
            with_tools: true,
            append_tool_response: false,
            one_shot: (false, None),
            with_context: true,
        }
    }

    /// Execute the tool calls if the LLM responds with a Tool call request, `true` by default
    #[must_use]
    pub fn execute_tools(mut self, execute: bool) -> Self {
        self.execute_tools = execute;
        self
    }

    /// Wether to send any tool definitions with the prompt, `true` by default
    #[must_use]
    pub fn with_tools(mut self, no_tools: bool) -> Self {
        self.with_tools = no_tools;
        self
    }

    /// Create a `Message::User` with the tool reponses and append it to the client history, `false` by default
    #[must_use]
    pub fn append_tool_response(mut self, append: bool) -> Self {
        self.append_tool_response = append;
        self
    }

    /// Wether to retrieve and append the context to the prompt, true by default.
    /// If true, the client's vector store will get looked up for the top matches to the prompt and
    /// the context will get appended to the prompt before being sent.
    #[must_use]
    pub fn with_context(mut self, append_context: bool) -> Self {
        self.with_context = append_context;
        self
    }

    /// Prompt the LLM with a custom history, and get a response.
    /// Response won't be stored in the client's history
    #[must_use]
    pub fn one_shot(mut self, one_shot: bool, history: Option<MessageHistory>) -> Self {
        self.one_shot = (one_shot, history);
        self
    }

    /// Extracts structured data from the model's response
    ///
    /// Handles context retrieval and message construction automatically.
    ///
    /// # Errors
    /// Returns errors from:
    /// - Context retrieval ([`VectorStoreError`])
    /// - Model execution ([`CompletionError`])
    /// - Extraction ([`ExtractionError`])
    pub async fn extract<T: Extractor>(self) -> Result<T, crate::error::Error> {
        let history = if self.one_shot.0 {
            &self.one_shot.1.unwrap_or_default()
        } else {
            &self.client.history
        };

        let retrieved_context = self.client.get_context(&self.prompt).await?;
        let context = if self.with_context {
            retrieved_context
                .map_or_else(String::new, |c| format!("\n\n<context>\n{c}\n</context>\n"))
        } else {
            String::new()
        };

        let message = Message::User {
            content: format!("{}{}", self.prompt, context),
            tool_responses: None,
        };

        let model = self.client.completion_model.clone();
        let mut guard = model.write().await;

        guard
            .extract::<T>(
                message,
                history,
                self.client.temperature,
                self.client.max_tokens,
            )
            .await
            .map_err(Into::into)
    }

    /// Builds the prompt and sends it to the completion model
    ///
    /// # Errors
    /// This method will error if it fails to send the prompt or tool calls fail
    pub async fn send(self) -> Result<Message, crate::error::Error> {
        let tools = if self.with_tools && !self.client.tools.0.is_empty() {
            Some(&*self.client.tools)
        } else {
            None
        };
        let history = if self.one_shot.0 {
            &self.one_shot.1.unwrap_or_default()
        } else {
            &self.client.history
        };
        let (mut response, token_usage) = self
            .client
            .send_prompt(
                &self.prompt,
                history,
                tools,
                self.client.temperature,
                self.client.max_tokens,
                self.with_context,
            )
            .await?;

        if !self.one_shot.0 {
            self.client.history.push(Message::User {
                content: self.prompt.clone(),
                tool_responses: None,
            });
            self.client.history.push(response.clone());
        }

        self.client.update_token_usage(&token_usage);
        if token_usage.total_tokens.is_some() {
            info!(
                "Prompt used up: {:?} tokens, Total tokens used: {:?}",
                token_usage.total_tokens, self.client.token_usage.total_tokens
            );
        }

        if self.execute_tools {
            if let Message::Assistant {
                content: _,
                tool_calls: Some(calls),
            } = response.clone()
            {
                if self.one_shot.0 {
                    self.client.history.push(response);
                }
                let values = self.client.run_tools(Some(&calls)).await?;
                if self.one_shot.0 {
                    self.client.history.pop();
                }
                response = Message::User {
                    content: String::new(),
                    tool_responses: Some(values.clone()),
                };
                if self.append_tool_response && !self.one_shot.0 {
                    self.client.append_history(&[response.clone()]);
                }
            }
        }

        Ok(response)
    }
}

impl<M: CompletionModel + Send> Client<M> {
    /// Creates a new client with the specified configuration
    ///
    /// # Arguments
    /// * `completion_model` - The underlying model implementation
    /// * `preamble` - System instructions for the model
    /// * `temperature` - Sampling temperature (0.0-1.0)
    /// * `max_tokens` - Maximum response length
    /// * `embedders` - Embedding models for context retrieval
    /// * `tools` - Available tools for function calling
    pub fn new(
        completion_model: M,
        preamble: impl AsRef<str>,
        temperature: f64,
        max_tokens: usize,
        embedders: Vec<Embedder>,
        tools: ToolSet,
    ) -> Self {
        Self {
            completion_model: Arc::new(RwLock::new(completion_model)),
            history: vec![Message::Preamble(String::from(preamble.as_ref()))],
            embedders,
            tools: Box::new(tools),
            temperature,
            max_tokens,
            token_usage: TokenUsage::default(),
            states: DashMap::new(),
        }
    }

    /// Clear conversation history while maintaining premble
    pub fn clear_history(&mut self) {
        self.history.retain(|m| matches!(m, Message::Preamble(_)));
    }

    /// Replaces the current message history with the provided history
    pub fn load_history(&mut self, history: MessageHistory) {
        self.history = history;
    }

    /// Returns a reference to the current message history
    #[must_use]
    pub fn export_history(&self) -> &MessageHistory {
        &self.history
    }

    /// Appends messages to the conversation history
    pub fn append_history(&mut self, messages: &[Message]) {
        messages.iter().for_each(|m| self.history.push(m.clone()));
    }

    #[instrument(skip(self, state), fields())]
    /// Registers new state with the client
    ///
    /// # Errors
    /// Returns [`StateError::AlreadyExists`] if state of this type is already registered
    pub fn with_state<T: Send + Sync + 'static>(self, state: T) -> Result<Self, CompletionError> {
        let type_id = state.type_id();
        if self.states.contains_key(&type_id) {
            error!(
                state_type = std::any::type_name::<T>(),
                "Failed to add state to client because state of a similar type already exists"
            );
            return Err(CompletionError::StateError(StateError::AlreadyExists(
                format!("{:?}", std::any::type_name::<T>()),
            )));
        }
        self.states.insert(type_id, Box::new(Arc::new(state)));
        info!(
            state_type = std::any::type_name::<T>(),
            "Successfully added state new state to client!"
        );
        Ok(self)
    }

    #[instrument(skip(self))]
    /// Retrieves previously registered state
    ///
    /// # Errors
    /// Returns [`StateError::NotFound`] if no state of type `T` exists
    pub fn get_state<T: Send + Sync + 'static>(&self) -> Result<State<T>, StateError> {
        let boxed = self.states.get(&TypeId::of::<T>()).ok_or({
            error!(
                state_type = std::any::type_name::<T>(),
                "Failed to find requested state on client"
            );
            StateError::NotFound
        })?;

        let arc = boxed.downcast_ref::<Arc<T>>().ok_or(StateError::NotFound)?;

        Ok(State(arc.clone()))
    }

    /// Creates a `PromptBuilder` instance .
    pub fn prompt(&mut self, prompt: impl Into<String>) -> PromptBuilder<M> {
        PromptBuilder::new(self, prompt)
    }

    #[instrument(skip(self, calls), fields(fetch_toolcall_from_history = calls.is_some()))]
    /// Executes requested tool calls from the model
    ///
    /// If no calls are provided, attempts to use calls from the last assistant message.
    /// Supports different execution strategies (fail-early vs best-effort).
    ///
    /// # Errors
    /// Returns [`ToolSetError`] for:
    /// - Empty message history when calls not provided
    /// - Last message not containing tool calls
    /// - Individual tool execution failures
    pub async fn run_tools(
        &self,
        calls: Option<&[ToolCall]>,
    ) -> Result<Vec<ToolResponse>, ToolSetError> {
        let calls = calls.unwrap_or({
            let last = self.history.last().ok_or({
                error!("Attempting to extract tool to call from an empty message history!");
                ToolSetError::EmptyMessageHistory
            })?;
            if let Message::Assistant {
                content: _,
                tool_calls: Some(tcs),
            } = last
            {
                tcs
            } else {
                error!("Last message in message history doesnt contain tools to call");
                Err(ToolSetError::LastMessageNotAToolCall)?
            }
        });

        let mut values = vec![];
        match self.tools.1 {
            ExecutionStrategy::FailEarly => {
                for call in calls {
                    info!(
                        tool_name = call.name,
                        "Calling tool with a `FailEarly` execution strategy!"
                    );
                    let call_result = self
                        .tools
                        .call(&call.id, &call.name, &call.arguments, &self.states)
                        .await;
                    if let Err(ref e) = call_result {
                        error!(error = ?e, tool_name = call.name, "Tool call failed");
                    }
                    values.push(call_result?);
                }
            }
            ExecutionStrategy::BestEffort => {
                for call in calls {
                    info!(
                        tool_name = call.name,
                        "Calling tool with a `BestEffort` execution strategy!"
                    );
                    let tr = self
                        .tools
                        .call(&call.id, &call.name, &call.arguments, &self.states)
                        .await;
                    match tr {
                        Ok(v) => {
                            values.push(v);
                        }
                        Err(e) => {
                            error!(error = ?e, tool_name = call.name, "Tool call failed");
                        }
                    }
                }
            }
        }

        Ok(values)
    }

    fn update_token_usage(&mut self, usage: &TokenUsage) {
        self.token_usage.prompt_tokens =
            combine_options(self.token_usage.prompt_tokens, usage.prompt_tokens);
        self.token_usage.completion_tokens =
            combine_options(self.token_usage.completion_tokens, usage.completion_tokens);
        self.token_usage.total_tokens =
            combine_options(self.token_usage.total_tokens, usage.total_tokens);
    }

    async fn send_prompt(
        &self,
        prompt: &str,
        history: &MessageHistory,
        tools: Option<&ToolSet>,
        temperature: f64,
        max_tokens: usize,
        append_context: bool,
    ) -> Result<(Message, TokenUsage), crate::error::Error> {
        let retrieved_context = self.get_context(prompt).await?;
        let context = if append_context {
            retrieved_context
                .map_or_else(String::new, |c| format!("\n\n<context>\n{c}\n</context>\n"))
        } else {
            String::new()
        };

        let message_with_context = Message::User {
            content: format!("{prompt}{context}"),
            tool_responses: None,
        };

        let model = self.completion_model.clone();
        let mut guard = model.write().await;
        guard
            .send(
                message_with_context,
                history,
                tools,
                temperature,
                max_tokens,
            )
            .await
            .map_err(crate::error::Error::from)
    }

    async fn get_context(&self, prompt: &str) -> Result<Option<String>, crate::error::Error> {
        if self.embedders.is_empty() {
            return Ok(None);
        }
        let mut context = String::new();
        for embedder in &self.embedders {
            let query_results = embedder.query(prompt, DEFAULT_TOP_N).await?;
            if query_results.is_empty() {
                return Ok(None);
            }
            for r in query_results {
                context.push_str(&r.raw_data);
            }
        }
        Ok(Some(context))
    }
}

/// Generates a JSON schema serializer for extractor types
///
/// # Errors
/// Returns `serde_json::Error` if schema serialization fails
pub fn default_extractor_serializer<'a, T: schemars::JsonSchema + serde::Deserialize<'a>>(
) -> Result<serde_json::Value, serde_json::error::Error> {
    let settings = SchemaSettings::default().with(|s| {
        s.inline_subschemas = true;
    });
    let generator = settings.into_generator();
    let schema = generator.into_root_schema_for::<T>();
    let mut schema_value = serde_json::to_value(&schema)?;

    let type_name: &str = std::any::type_name::<T>();
    let type_name = type_name.split("::").last().unwrap_or("ExtractorType");
    if let Some(obj) = schema_value.as_object_mut() {
        obj.remove("$schema");
        obj.remove("format");
        obj.remove("title");
    }
    process_json_value(&mut schema_value);
    let schema = json!({
        "name": type_name,
        "strict": true,
        "schema": schema_value
    });
    Ok(json!({
        "type": "json_schema",
        "json_schema": schema
    }))
}

fn process_json_value(value: &mut serde_json::Value) {
    match value {
        serde_json::Value::Object(obj) => {
            let fields_to_remove = ["$schema", "format", "title", "minimum"];
            for &f in &fields_to_remove {
                if obj.get(f).map_or(false, |v| v.is_string() || v.is_number()) {
                    obj.remove(f);
                }
            }
            if let Some(v) = obj.get("oneOf").cloned() {
                obj.remove("oneOf");
                obj.insert("anyOf".to_string(), v);
            };

            if obj.contains_key("properties") {
                obj.insert("additionalProperties".to_string(), json!(false));
            }
            for (_, v) in obj.iter_mut() {
                process_json_value(v);
            }
        }
        serde_json::Value::Array(arr) => {
            for elem in arr.iter_mut() {
                process_json_value(elem);
            }
        }
        _ => {}
    }
}

/// Serializes user messages, ignoring tool call responses
///
/// # Errors
/// If serializer fails
pub fn serialize_user<S>(
    content: &str,
    _tool_calls: &Option<Vec<ToolResponse>>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_newtype_struct("user", &content)
}

/// Serializes assistant messages with integrated tool call information
///
/// # Errors
/// If serializer fails
pub fn serialize_assistant<S>(
    content: &str,
    tool_calls: &Option<Vec<ToolCall>>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let combined_content = match tool_calls {
        Some(calls) => &format!("{content} {calls:?}"),
        None => content,
    };
    serializer.serialize_newtype_struct("assistant", &combined_content)
}

fn combine_options(a: Option<u64>, b: Option<u64>) -> Option<u64> {
    match (a, b) {
        (Some(a_val), Some(b_val)) => Some(a_val + b_val),
        _ => None,
    }
}