Skip to main content

langfuse/prompts/
types.rs

1//! Unified prompt type wrapping text and chat variants.
2
3use crate::prompts::chat::ChatPromptClient;
4use crate::prompts::text::TextPromptClient;
5
6/// Unified prompt type — either Text or Chat.
7///
8/// Returned by [`PromptManager::get_prompt`](crate::prompts::manager::PromptManager::get_prompt).
9/// Use [`is_text`](Prompt::is_text) / [`is_chat`](Prompt::is_chat) or pattern
10/// matching to determine the variant.
11#[derive(Debug, Clone)]
12pub enum Prompt {
13    /// A text prompt with `{{variable}}` template support.
14    Text(TextPromptClient),
15    /// A chat prompt with a list of messages.
16    Chat(ChatPromptClient),
17}
18
19impl Prompt {
20    /// Returns `true` if this is a text prompt.
21    pub fn is_text(&self) -> bool {
22        matches!(self, Self::Text(_))
23    }
24
25    /// Returns `true` if this is a chat prompt.
26    pub fn is_chat(&self) -> bool {
27        matches!(self, Self::Chat(_))
28    }
29
30    /// Returns a reference to the inner [`TextPromptClient`] if this is a text prompt.
31    pub fn as_text(&self) -> Option<&TextPromptClient> {
32        match self {
33            Self::Text(t) => Some(t),
34            Self::Chat(_) => None,
35        }
36    }
37
38    /// Returns a reference to the inner [`ChatPromptClient`] if this is a chat prompt.
39    pub fn as_chat(&self) -> Option<&ChatPromptClient> {
40        match self {
41            Self::Text(_) => None,
42            Self::Chat(c) => Some(c),
43        }
44    }
45
46    /// Returns the prompt name regardless of variant.
47    pub fn name(&self) -> &str {
48        match self {
49            Self::Text(t) => &t.name,
50            Self::Chat(c) => &c.name,
51        }
52    }
53
54    /// Returns the prompt version regardless of variant.
55    pub fn version(&self) -> i32 {
56        match self {
57            Self::Text(t) => t.version,
58            Self::Chat(c) => c.version,
59        }
60    }
61
62    /// Returns whether this prompt was served from an expired cache entry (fallback).
63    pub fn is_fallback(&self) -> bool {
64        match self {
65            Self::Text(t) => t.is_fallback,
66            Self::Chat(c) => c.is_fallback,
67        }
68    }
69}