Skip to main content

rig_core/conversation/
author.rs

1//! Who wrote an entry or an aside of a conversation, or made a change to it.
2
3use serde::{Deserialize, Serialize};
4
5use super::short_id;
6
7/// The id of a request to an agent, which the reply to it names, such as
8/// the tool call that asked. Unique per sender, so it doubles as an
9/// idempotency key.
10#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct RequestId(pub String);
13
14/// Who wrote it.
15#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
16#[serde(rename_all = "snake_case")]
17pub enum AuthorKind {
18    /// The user, typing.
19    #[default]
20    User,
21    /// An agent, such as a subagent answering or an agent handing out a
22    /// task.
23    Agent,
24    /// The plugin named.
25    Plugin(String),
26    /// The host running the conversation: the stopped note, restart
27    /// answers, its own edits.
28    Rig,
29}
30
31/// Who wrote an entry or an aside, or made a change: the kind of writer,
32/// the id of the agent it came from, the request it answers or makes, and
33/// what it is about. An entry without one is the user's own text, the
34/// model's reply or a tool result.
35#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
36pub struct Author {
37    /// Who wrote it.
38    #[serde(default)]
39    pub kind: AuthorKind,
40    /// The id of the agent it came from.
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub from: Option<String>,
43    /// The request it answers or makes. Kept for correlation, such as a
44    /// report to the call that asked; not shown in its header.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub request: Option<RequestId>,
47    /// What it is about, such as a subagent's task title, shown in its
48    /// header.
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub title: Option<String>,
51}
52
53impl Author {
54    /// The user, typing.
55    pub fn user() -> Self {
56        Self::default()
57    }
58
59    /// The agent whose id is `from`, answering or making `request`.
60    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    /// The plugin `name`.
70    pub fn plugin(name: impl Into<String>) -> Self {
71        Self {
72            kind: AuthorKind::Plugin(name.into()),
73            ..Self::default()
74        }
75    }
76
77    /// rig itself.
78    pub fn rig() -> Self {
79        Self {
80            kind: AuthorKind::Rig,
81            ..Self::default()
82        }
83    }
84
85    /// This author, writing about `title`, such as a task's.
86    #[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    /// Who wrote it, for views: such as `agent 1a2b3c4d "Fix the parser"`.
95    /// The request id is left out; it stays in [`Author::request`].
96    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    /// The line that heads text by this author for the model, or `None`
112    /// for the user's own: it names the writer and says the text is not
113    /// the user's words.
114    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}