an http based roguelike rpg about jelqing
Blame: src/game/oracle.rs
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 1 | //! Where the game's decisions come from. The seeded rng is the default; an |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 2 | //! operator can instead point the server at a System One service and let the |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 3 | //! model answer every roll over HTTP (JELQD_ORACLE=clm). |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 4 | //! |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 5 | //! Identity randomness (run ids, etag salts, nonces) never goes to the model. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 6 | //! The Clm arm carries its own rng for those, seeded like the rng arm's. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 7 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 8 | use rand::{Rng, SeedableRng, rngs::StdRng}; |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 9 | use serde_json::{Value, json}; |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 10 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 11 | /// One named decision. The state prose is what the model reads as context, the |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 12 | /// ask is the question, and the site is a stable name for logs and caching. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 13 | pub(crate) struct Roll { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 14 | pub(crate) state: String, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 15 | pub(crate) site: &'static str, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 16 | pub(crate) ask: String, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 17 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 18 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 19 | pub(crate) enum Oracle { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 20 | Rng(StdRng), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 21 | Clm { clm: Clm, technical: StdRng }, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 22 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 23 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 24 | pub(crate) enum OracleTemplate { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 25 | Rng, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 26 | Clm { url: String }, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 27 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 28 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 29 | pub(crate) struct Clm { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 30 | url: String, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 31 | agent: ureq::Agent, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 32 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 33 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 34 | impl Oracle { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 35 | pub(crate) fn build(template: &OracleTemplate, seed: u64) -> Self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 36 | match template { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 37 | OracleTemplate::Rng => Self::Rng(StdRng::seed_from_u64(seed)), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 38 | OracleTemplate::Clm { url } => Self::Clm { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 39 | clm: Clm::new(url), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 40 | technical: StdRng::seed_from_u64(seed), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 41 | }, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 42 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 43 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 44 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 45 | fn technical(&mut self) -> &mut StdRng { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 46 | match self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 47 | Self::Rng(rng) => rng, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 48 | Self::Clm { technical, .. } => technical, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 49 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 50 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 51 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 52 | /// Identity randomness, never a game decision. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 53 | pub(crate) fn u64(&mut self) -> u64 { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 54 | self.technical().random() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 55 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 56 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 57 | /// A yes/no game roll with stated odds of 1 in `odds`. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 58 | pub(crate) fn chance(&mut self, roll: Roll, odds: u32) -> bool { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 59 | match self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 60 | Self::Rng(rng) => rng.random_ratio(1, odds), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 61 | Self::Clm { clm, .. } => { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 62 | let ask = format!("{}, with odds of 1 in {odds}.", roll.ask); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 63 | clm.decide(&roll.state, roll.site, &ask) > 0.5 |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 64 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 65 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 66 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 67 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 68 | /// A yes/no game roll where the ask describes the calm outcome, not the |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 69 | /// event. The model believes dull statements more readily than rare |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 70 | /// warnings, so the interrupts that read as drama get phrased as calm. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 71 | /// The rng arm still draws the event, never the calm. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 72 | pub(crate) fn chance_not(&mut self, roll: Roll, odds: u32) -> bool { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 73 | match self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 74 | Self::Rng(rng) => rng.random_ratio(1, odds), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 75 | Self::Clm { clm, .. } => { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 76 | let ask = format!("{}, with odds of 1 in {odds}.", roll.ask); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 77 | clm.decide(&roll.state, roll.site, &ask) <= 0.5 |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 78 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 79 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 80 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 81 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 82 | /// A yes/no game roll with general odds of `num` in `den`, put to the |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 83 | /// model as a choice between the calm answer and the event. The noul head |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 84 | /// answers interrupt questions with yes; the choice head, offered the two |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 85 | /// answers side by side, does not. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 86 | pub(crate) fn ratio_choice( |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 87 | &mut self, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 88 | roll: Roll, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 89 | num: u32, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 90 | den: u32, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 91 | calm: &str, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 92 | event: &str, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 93 | ) -> bool { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 94 | match self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 95 | Self::Rng(rng) => rng.random_ratio(num, den), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 96 | Self::Clm { clm, .. } => { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 97 | let i = clm.choose(&roll.state, roll.site, &roll.ask, &[calm, event]); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 98 | i == 1 |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 99 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 100 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 101 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 102 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 103 | /// A yes/no game roll with general odds of `num` in `den`. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 104 | pub(crate) fn ratio(&mut self, roll: Roll, num: u32, den: u32) -> bool { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 105 | match self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 106 | Self::Rng(rng) => rng.random_ratio(num, den), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 107 | Self::Clm { clm, .. } => { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 108 | let ask = format!("{}, with odds of about {num} in {den}.", roll.ask); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 109 | clm.decide(&roll.state, roll.site, &ask) > 0.5 |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 110 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 111 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 112 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 113 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 114 | /// One bucket chosen by weighted draw. The rng arm draws by `weights` and |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 115 | /// ignores the descriptions; the clm arm reads them all and picks the one |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 116 | /// the model believes, weights unused. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 117 | pub(crate) fn pick(&mut self, roll: Roll, weights: &[u32], options: &[&str]) -> usize { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 118 | match self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 119 | Self::Rng(rng) => { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 120 | let total: u32 = weights.iter().sum(); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 121 | let mut draw = rng.random_range(0..total); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 122 | for (i, w) in weights.iter().enumerate() { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 123 | if draw < *w { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 124 | return i; |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 125 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 126 | draw -= *w; |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 127 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 128 | weights.len() - 1 |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 129 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 130 | Self::Clm { clm, .. } => clm.choose(&roll.state, roll.site, &roll.ask, options), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 131 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 132 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 133 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 134 | /// A value in `lo..hi`. The rng arm draws uniformly; the clm arm picks from |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 135 | /// a 0.1-step grid so the model is choosing among described options. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 136 | pub(crate) fn span(&mut self, roll: Roll, lo: f64, hi: f64) -> f64 { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 137 | match self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 138 | Self::Rng(rng) => rng.random_range(lo..hi), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 139 | Self::Clm { clm, .. } => { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 140 | let mut last = format!("{lo:.1}"); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 141 | let mut values = Vec::new(); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 142 | let mut v = lo; |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 143 | while v < hi { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 144 | values.push(last); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 145 | v += 0.1; |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 146 | last = format!("{v:.1}"); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 147 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 148 | let options: Vec<&str> = values.iter().map(String::as_str).collect(); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 149 | let ask = format!("{}. Answer with a value.", roll.ask); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 150 | let i = clm.choose(&roll.state, roll.site, &ask, &options); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 151 | lo + 0.1 * i as f64 |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 152 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 153 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 154 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 155 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 156 | /// An integer in `lo..=hi`. The rng arm draws uniformly; the clm arm picks |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 157 | /// from the integers as described options. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 158 | pub(crate) fn span_int(&mut self, roll: Roll, lo: i32, hi: i32) -> i32 { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 159 | match self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 160 | Self::Rng(rng) => rng.random_range(lo..=hi), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 161 | Self::Clm { clm, .. } => { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 162 | let values: Vec<String> = (lo..=hi).map(|v| v.to_string()).collect(); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 163 | let options: Vec<&str> = values.iter().map(String::as_str).collect(); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 164 | let ask = format!("{}. Answer with a whole number.", roll.ask); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 165 | let i = clm.choose(&roll.state, roll.site, &ask, &options); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 166 | lo + i as i32 |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 167 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 168 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 169 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 170 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 171 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 172 | impl Clm { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 173 | fn new(url: &str) -> Self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 174 | Self { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 175 | url: url.trim_end_matches('/').to_owned(), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 176 | agent: ureq::AgentBuilder::new() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 177 | .timeout(std::time::Duration::from_secs(60)) |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 178 | .build(), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 179 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 180 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 181 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 182 | /// POST /v1/systemone with a single question; panics on any failure, which |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 183 | /// surfaces as a 500 through the catch_panic layer. A dead oracle must not |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 184 | /// quietly fall back to the rng. |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 185 | fn ask(&self, state: &str, site: &str, question: Value) -> Value { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 186 | let body = json!({ |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 187 | "state": state, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 188 | "model": "clm-latest", |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 189 | "questions": { site: question }, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 190 | "temperature": 1.0, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 191 | }); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 192 | let response = self |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 193 | .agent |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 194 | .post(&self.url) |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 195 | .send_json(&body) |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 196 | .unwrap_or_else(|e| panic!("oracle unreachable at {}: {e}", self.url)); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 197 | let answer: Value = response |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 198 | .into_json() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 199 | .unwrap_or_else(|e| panic!("oracle answer unreadable: {e}")); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 200 | answer["answers"][site].clone() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 201 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 202 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 203 | fn decide(&self, state: &str, site: &str, ask: &str) -> f64 { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 204 | self.ask(state, site, json!({ "type": "noul", "instructions": ask }))["noul"] |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 205 | .as_f64() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 206 | .unwrap_or_else(|| panic!("oracle noul answer for {site} missing")) |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 207 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 208 | |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 209 | fn choose(&self, state: &str, site: &str, ask: &str, options: &[&str]) -> usize { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 210 | let criteria: serde_json::Map<String, Value> = options |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 211 | .iter() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 212 | .enumerate() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 213 | .map(|(i, text)| (i.to_string(), json!(text))) |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 214 | .collect(); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 215 | let q = if ask.trim_end().ends_with('?') { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 216 | "" |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 217 | } else { |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 218 | "?" |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 219 | }; |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 220 | let instructions = format!("{ask}{q}"); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 221 | let answer = self.ask( |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 222 | state, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 223 | site, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 224 | json!({ "type": "choice", "instructions": instructions, "criteria": criteria }), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 225 | ); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 226 | let choice = answer["choice"] |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 227 | .as_str() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 228 | .unwrap_or_else(|| panic!("oracle choice answer for {site} missing")); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 229 | choice |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 230 | .parse::<usize>() |
e807266c323b | atagen | 2026-10-12 09:46 UTC | 231 | .ok() |
e807266c323b | atagen | 2026-10-12 09:46 UTC | 232 | .filter(|&i| i < options.len()) |
e807266c323b | atagen | 2026-10-12 09:46 UTC | 233 | .unwrap_or_else(|| panic!("oracle chose {choice:?} for {site}, not an option index")) |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 234 | } |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 235 | } |