rig_core/conversation/
author.rs1use serde::{Deserialize, Serialize};
4
5use super::short_id;
6
7#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct RequestId(pub String);
13
14#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
16#[serde(rename_all = "snake_case")]
17pub enum AuthorKind {
18 #[default]
20 User,
21 Agent,
24 Plugin(String),
26 Rig,
28}
29
30#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
35pub struct Author {
36 #[serde(default)]
38 pub kind: AuthorKind,
39 #[serde(default, skip_serializing_if = "Option::is_none")]
41 pub from: Option<String>,
42 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub request: Option<RequestId>,
46 #[serde(default, skip_serializing_if = "Option::is_none")]
49 pub title: Option<String>,
50}
51
52impl Author {
53 pub fn user() -> Self {
55 Self::default()
56 }
57
58 pub fn agent(from: impl Into<String>, request: Option<RequestId>) -> Self {
60 Self {
61 kind: AuthorKind::Agent,
62 from: Some(from.into()),
63 request,
64 title: None,
65 }
66 }
67
68 pub fn plugin(name: impl Into<String>) -> Self {
70 Self {
71 kind: AuthorKind::Plugin(name.into()),
72 ..Self::default()
73 }
74 }
75
76 pub fn rig() -> Self {
78 Self {
79 kind: AuthorKind::Rig,
80 ..Self::default()
81 }
82 }
83
84 #[must_use]
86 pub fn titled(self, title: impl Into<String>) -> Self {
87 Self {
88 title: Some(title.into()),
89 ..self
90 }
91 }
92
93 pub fn label(&self) -> String {
96 let who = match &self.kind {
97 AuthorKind::User => "you".to_owned(),
98 AuthorKind::Agent => {
99 format!("agent {}", self.from.as_deref().map_or("?", short_id))
100 }
101 AuthorKind::Plugin(name) => format!("plugin {name}"),
102 AuthorKind::Rig => "rig".to_owned(),
103 };
104 match &self.title {
105 Some(title) => format!("{who} \"{title}\""),
106 None => who,
107 }
108 }
109
110 pub fn header(&self) -> Option<String> {
114 match self.kind {
115 AuthorKind::User => None,
116 _ => Some(format!(
117 "[Output of {}, not the user's words]",
118 self.label()
119 )),
120 }
121 }
122}