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,
29}
30
31#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
36pub struct Author {
37 #[serde(default)]
39 pub kind: AuthorKind,
40 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub from: Option<String>,
43 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub request: Option<RequestId>,
47 #[serde(default, skip_serializing_if = "Option::is_none")]
50 pub title: Option<String>,
51}
52
53impl Author {
54 pub fn user() -> Self {
56 Self::default()
57 }
58
59 pub fn agent(from: impl Into<String>, request: Option<RequestId>) -> Self {
61 Self {
62 kind: AuthorKind::Agent,
63 from: Some(from.into()),
64 request,
65 title: None,
66 }
67 }
68
69 pub fn plugin(name: impl Into<String>) -> Self {
71 Self {
72 kind: AuthorKind::Plugin(name.into()),
73 ..Self::default()
74 }
75 }
76
77 pub fn rig() -> Self {
79 Self {
80 kind: AuthorKind::Rig,
81 ..Self::default()
82 }
83 }
84
85 #[must_use]
87 pub fn titled(self, title: impl Into<String>) -> Self {
88 Self {
89 title: Some(title.into()),
90 ..self
91 }
92 }
93
94 pub fn label(&self) -> String {
97 let who = match &self.kind {
98 AuthorKind::User => "you".to_owned(),
99 AuthorKind::Agent => {
100 format!("agent {}", self.from.as_deref().map_or("?", short_id))
101 }
102 AuthorKind::Plugin(name) => format!("plugin {name}"),
103 AuthorKind::Rig => "rig".to_owned(),
104 };
105 match &self.title {
106 Some(title) => format!("{who} \"{title}\""),
107 None => who,
108 }
109 }
110
111 pub fn header(&self) -> Option<String> {
115 match self.kind {
116 AuthorKind::User => None,
117 _ => Some(format!(
118 "[Output of {}, not the user's words]",
119 self.label()
120 )),
121 }
122 }
123}