1use std::collections::HashMap;
2use std::ops::{Deref, DerefMut};
3
4use serde_json::{Map, Value};
5
6use super::graph::Stimuli;
7use crate::error::Result;
8use crate::parrot::format::{CategoricalFormat, Format};
9
10const X_INSTRUCTION: &str = "You are a viewer on X For You. Phoenix predicts the probability of each action below. Pick the one action you would take. Favorite is Like. Copy link, reply, quote, and share via DM are the strong positive heads. Report, mute, block, and not interested are the negative heads. Did not dwell means you skip the card.";
14
15pub const X_ACTIONS: [&str; 22] = [
16 "Did not dwell",
17 "Dwell",
18 "Like",
19 "Reply",
20 "Repost",
21 "Quote",
22 "Photo expand",
23 "Video open",
24 "Video quality view",
25 "Click",
26 "Open link",
27 "Quoted click",
28 "Quoted video view",
29 "Profile",
30 "Follow",
31 "Share",
32 "Share via DM",
33 "Copy link",
34 "Not interested",
35 "Mute",
36 "Block",
37 "Report",
38];
39
40pub const X_WEIGHTS: [(&str, f64); 22] = [
42 ("Did not dwell", -0.02),
43 ("Dwell", 0.05),
44 ("Like", 0.5),
45 ("Reply", 5.0),
46 ("Repost", 1.0),
47 ("Quote", 5.0),
48 ("Photo expand", 0.05),
49 ("Video open", 0.07),
50 ("Video quality view", 0.0),
51 ("Click", 0.4),
52 ("Open link", 0.2),
53 ("Quoted click", 0.05),
54 ("Quoted video view", 0.0),
55 ("Profile", 0.0),
56 ("Follow", 4.0),
57 ("Share", 2.0),
58 ("Share via DM", 5.0),
59 ("Copy link", 20.0),
60 ("Not interested", -43.2),
61 ("Mute", -58.8),
62 ("Block", -31.2),
63 ("Report", -234.0),
64];
65
66const LINKEDIN_INSTRUCTION: &str = "You are a member in the LinkedIn feed. Feed SR predicts skip, click, and long dwell on one tower and like, comment, and share on the other. Those scores are blended to rank the post. Pick the one action you would take. Long dwell means you stay past the type threshold. Skip means you scroll past. Weights below are ours, not LinkedIn production numbers.";
67
68pub const LINKEDIN_ACTIONS: [&str; 6] = ["Skip", "Click", "Long dwell", "Like", "Comment", "Share"];
69
70pub const LINKEDIN_WEIGHTS: [(&str, f64); 6] = [
72 ("Skip", 0.0),
73 ("Click", 1.0),
74 ("Like", 1.0),
75 ("Long dwell", 2.0),
76 ("Comment", 2.0),
77 ("Share", 3.0),
78];
79
80fn max_weight(weights: &[(&str, f64)]) -> f64 {
81 weights
82 .iter()
83 .map(|(_, weight)| *weight)
84 .fold(0.0, f64::max)
85}
86
87fn score(weights: &[(&str, f64)], counts: &HashMap<String, f64>) -> f64 {
89 let mut n = 0.0;
90 let mut sum = 0.0;
91 for (label, weight) in weights {
92 let count = counts.get(*label).copied().unwrap_or(0.0);
93 n += count;
94 sum += weight * count;
95 }
96 if n == 0.0 { f64::NAN } else { sum / n }
97}
98
99fn with_type(json: Value, kind: &str, weights: &[(&str, f64)]) -> Value {
100 let Value::Object(mut fields) = json else {
101 return json;
102 };
103 fields.insert("type".to_string(), Value::from(kind));
104 fields.insert(
105 "weights".to_string(),
106 Value::Object(
107 weights
108 .iter()
109 .map(|(label, weight)| (label.to_string(), Value::from(*weight)))
110 .collect::<Map<String, Value>>(),
111 ),
112 );
113 Value::Object(fields)
114}
115
116pub enum AntFormat {
118 X(XFormat),
119 LinkedIn(LinkedInFormat),
120}
121
122pub enum AntFormatOrFormat {
125 Ant(AntFormat),
126 Format(Format),
127}
128
129impl AntFormatOrFormat {
130 pub fn to_json(&self) -> Value {
131 match self {
132 AntFormatOrFormat::Ant(AntFormat::X(format)) => format.to_json(),
133 AntFormatOrFormat::Ant(AntFormat::LinkedIn(format)) => format.to_json(),
134 AntFormatOrFormat::Format(format) => format.to_json(),
135 }
136 }
137}
138
139impl Clone for AntFormatOrFormat {
140 fn clone(&self) -> Self {
141 AntFormat::from_json(&self.to_json()).expect("a format reads its own JSON")
142 }
143}
144
145impl AntFormat {
146 pub fn weights(&self) -> &'static [(&'static str, f64)] {
147 match self {
148 AntFormat::X(_) => &X_WEIGHTS,
149 AntFormat::LinkedIn(_) => &LINKEDIN_WEIGHTS,
150 }
151 }
152
153 pub fn max_weight(&self) -> f64 {
154 max_weight(self.weights())
155 }
156
157 pub fn score(&self, counts: &HashMap<String, f64>) -> f64 {
159 score(self.weights(), counts)
160 }
161
162 pub fn from_json(json: &Value) -> Result<AntFormatOrFormat> {
163 let answer = json["answer"].as_str().map(str::to_string);
164 match json["type"].as_str() {
165 Some("x") => {
166 let mut format = XFormat::new();
167 if let Some(answer) = answer {
168 format.answer(answer);
169 }
170 Ok(AntFormatOrFormat::Ant(AntFormat::X(format)))
171 }
172 Some("linkedin") => {
173 let mut format = LinkedInFormat::new();
174 if let Some(answer) = answer {
175 format.answer(answer);
176 }
177 Ok(AntFormatOrFormat::Ant(AntFormat::LinkedIn(format)))
178 }
179 _ => Ok(AntFormatOrFormat::Format(Format::from_json(json)?)),
180 }
181 }
182
183 pub fn for_kind(kind: Stimuli) -> Vec<AntFormat> {
184 match kind {
185 Stimuli::X => vec![AntFormat::X(XFormat::new())],
186 Stimuli::Linkedin => vec![AntFormat::LinkedIn(LinkedInFormat::new())],
187 _ => Vec::new(),
188 }
189 }
190}
191
192pub struct XFormat(CategoricalFormat);
193
194impl Deref for XFormat {
195 type Target = CategoricalFormat;
196 fn deref(&self) -> &CategoricalFormat {
197 &self.0
198 }
199}
200
201impl DerefMut for XFormat {
202 fn deref_mut(&mut self) -> &mut CategoricalFormat {
203 &mut self.0
204 }
205}
206
207impl Default for XFormat {
208 fn default() -> Self {
209 XFormat::new()
210 }
211}
212
213impl XFormat {
214 pub fn new() -> XFormat {
215 XFormat(CategoricalFormat::new(
216 X_INSTRUCTION,
217 X_ACTIONS.iter().map(|action| action.to_string()).collect(),
218 ))
219 }
220
221 pub fn weights(&self) -> &'static [(&'static str, f64)] {
222 &X_WEIGHTS
223 }
224
225 pub fn max_weight(&self) -> f64 {
226 max_weight(&X_WEIGHTS)
227 }
228
229 pub fn score(&self, counts: &HashMap<String, f64>) -> f64 {
230 score(&X_WEIGHTS, counts)
231 }
232
233 #[allow(clippy::should_implement_trait)]
234 pub fn clone(&self) -> XFormat {
235 XFormat::new()
236 }
237
238 pub fn to_json(&self) -> Value {
239 with_type(self.0.to_json(), "x", &X_WEIGHTS)
240 }
241}
242
243pub struct LinkedInFormat(CategoricalFormat);
244
245impl Deref for LinkedInFormat {
246 type Target = CategoricalFormat;
247 fn deref(&self) -> &CategoricalFormat {
248 &self.0
249 }
250}
251
252impl DerefMut for LinkedInFormat {
253 fn deref_mut(&mut self) -> &mut CategoricalFormat {
254 &mut self.0
255 }
256}
257
258impl Default for LinkedInFormat {
259 fn default() -> Self {
260 LinkedInFormat::new()
261 }
262}
263
264impl LinkedInFormat {
265 pub fn new() -> LinkedInFormat {
266 LinkedInFormat(CategoricalFormat::new(
267 LINKEDIN_INSTRUCTION,
268 LINKEDIN_ACTIONS
269 .iter()
270 .map(|action| action.to_string())
271 .collect(),
272 ))
273 }
274
275 pub fn weights(&self) -> &'static [(&'static str, f64)] {
276 &LINKEDIN_WEIGHTS
277 }
278
279 pub fn max_weight(&self) -> f64 {
280 max_weight(&LINKEDIN_WEIGHTS)
281 }
282
283 pub fn score(&self, counts: &HashMap<String, f64>) -> f64 {
284 score(&LINKEDIN_WEIGHTS, counts)
285 }
286
287 #[allow(clippy::should_implement_trait)]
288 pub fn clone(&self) -> LinkedInFormat {
289 LinkedInFormat::new()
290 }
291
292 pub fn to_json(&self) -> Value {
293 with_type(self.0.to_json(), "linkedin", &LINKEDIN_WEIGHTS)
294 }
295}