yoagent 0.26.0

The agent loop for Rust: stream from 7 LLM protocols, run tools, loop until done. Native or wasm32 (Cloudflare Workers).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Advisory integration: an extension's `before_model` note (installed by
//! `with_decision_model`) that adds at most a skill hint and a tool hint to a
//! turn's latest user message. The advisor is also a [`TurnHook`]. Advisory only — it never
//! blocks, never removes a tool, and on any failure adds nothing.

use super::question::{Question, QuestionKind};
use super::{DecisionError, DecisionModel};
use crate::types::{TurnContext, TurnHook};
use serde_json::json;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

/// Most characters of the user's request sent as state.
const MAX_REQUEST_CHARS: usize = 8_000;
/// Most characters of one skill or tool description sent as a criterion.
const MAX_DESCRIPTION_CHARS: usize = 300;

/// Panic unless `p` is a probability — a threshold outside `[0, 1]` is a
/// programmer error, and failing at setup beats silently never (or always)
/// firing.
pub(crate) fn assert_threshold(name: &str, p: f64) {
    assert!(
        (0.0..=1.0).contains(&p),
        "{name} must be a probability in [0, 1], got {p}"
    );
}

/// Settings for the advisory features
/// [`Agent::with_decision_model`](crate::Agent::with_decision_model)
/// enables. Every setting has a default; override with the `with_*` methods
/// and install with
/// [`Agent::with_decision_advisory`](crate::Agent::with_decision_advisory).
///
/// Defaults: skill hint on (needs skills), shown when the "is a skill
/// needed" Noul is at least 0.3 and the skill Choice's confidence at least
/// 0.5; tool hint on from 40 tools, naming at most 3 tools with probability
/// at least 0.1; 2 s per request. These are starting points for Jev 1.13,
/// not calibrated constants — thresholds are per model.
#[derive(Debug, Clone)]
pub struct Advisory {
    pub(crate) model: DecisionModel,
    skill_hint: bool,
    skill_need_threshold: f64,
    skill_confidence_threshold: f64,
    tool_hint: bool,
    tool_hint_min_tools: usize,
    max_tool_hints: usize,
    tool_hint_min_probability: f64,
    timeout: Duration,
}

impl Advisory {
    /// Defaults for `model`.
    pub fn new(model: DecisionModel) -> Self {
        Self {
            model,
            skill_hint: true,
            skill_need_threshold: 0.3,
            skill_confidence_threshold: 0.5,
            tool_hint: true,
            tool_hint_min_tools: 40,
            max_tool_hints: 3,
            tool_hint_min_probability: 0.1,
            timeout: Duration::from_secs(2),
        }
    }

    /// Suggest at most one skill per request (default on; needs skills).
    pub fn with_skill_hint(mut self, on: bool) -> Self {
        self.skill_hint = on;
        self
    }

    /// Minimum probability that the request needs a skill at all (0.3 —
    /// adapted from TypeSafe's skill-suggestion cookbook, where 0.3 applies
    /// to the mean of three gate questions; here to one). Panics outside
    /// `[0, 1]`.
    pub fn with_skill_need_threshold(mut self, p: f64) -> Self {
        assert_threshold("skill need threshold", p);
        self.skill_need_threshold = p;
        self
    }

    /// Minimum confidence of the skill Choice (0.5, yoagent's own default).
    /// Panics outside `[0, 1]`.
    pub fn with_skill_confidence_threshold(mut self, p: f64) -> Self {
        assert_threshold("skill confidence threshold", p);
        self.skill_confidence_threshold = p;
        self
    }

    /// Name the most relevant tools when the agent has many (default on).
    pub fn with_tool_hint(mut self, on: bool) -> Self {
        self.tool_hint = on;
        self
    }

    /// Tool count from which the tool hint runs (40).
    pub fn with_tool_hint_min_tools(mut self, n: usize) -> Self {
        self.tool_hint_min_tools = n;
        self
    }

    /// Most tools one hint names (3).
    pub fn with_max_tool_hints(mut self, n: usize) -> Self {
        self.max_tool_hints = n;
        self
    }

    /// Minimum probability for a tool to be named (0.1). Panics outside
    /// `[0, 1]`.
    pub fn with_tool_hint_min_probability(mut self, p: f64) -> Self {
        assert_threshold("tool hint minimum probability", p);
        self.tool_hint_min_probability = p;
        self
    }

    /// Time limit for the one request per user message (2 s); on expiry
    /// nothing is added.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }
}

/// The [`TurnHook`] behind the advisory features.
///
/// Per user message it sends at most **one** request, batching a skill
/// Choice (the skills plus a "none" option), a Noul "does this request need
/// a skill", and — only with at least `tool_hint_min_tools` tools — a tool
/// Choice. The state is the user's request (see
/// [`TurnContext::user_request`]); the result is memoized on it, so the
/// tool-calling turns of one request send nothing more and see the same
/// note.
pub(crate) struct Advisor {
    advisory: Advisory,
    /// The model with the advisory timeout applied, so a timeout is reported
    /// (and counted) as one.
    model: DecisionModel,
    /// `(name, description)`.
    skills: Vec<(String, String)>,
    memo: Mutex<Option<(u64, Option<String>)>>,
    said_idle: AtomicBool,
    said_missing_key: AtomicBool,
    said_too_many_skills: AtomicBool,
}

impl Advisor {
    /// An advisor over `skills` (may be empty).
    pub(crate) fn new(advisory: Advisory, skills: &crate::skills::SkillSet) -> Self {
        let mut seen = std::collections::HashSet::new();
        let skills = skills
            .skills()
            .iter()
            .filter(|s| seen.insert(s.name.clone()))
            .map(|s| {
                (
                    s.name.clone(),
                    truncate_head(&s.description, MAX_DESCRIPTION_CHARS),
                )
            })
            .collect();
        let model = advisory.model.clone().with_timeout(advisory.timeout);
        Self {
            advisory,
            model,
            skills,
            memo: Mutex::new(None),
            said_idle: AtomicBool::new(false),
            said_missing_key: AtomicBool::new(false),
            said_too_many_skills: AtomicBool::new(false),
        }
    }

    fn none_option(&self) -> &'static str {
        if self.skills.iter().any(|(n, _)| n == "none") {
            "no_skill"
        } else {
            "none"
        }
    }

    async fn advise(&self, turn: &TurnContext<'_>) -> Option<String> {
        let caps = self.model.capabilities();
        let choice_ok = caps.supports(QuestionKind::Choice);
        let too_many_skills = self.skills.len() >= caps.max_choice_options;
        if self.advisory.skill_hint
            && too_many_skills
            && !self.said_too_many_skills.swap(true, Ordering::Relaxed)
        {
            tracing::warn!(
                skills = self.skills.len(),
                limit = caps.max_choice_options,
                "decision advisory skill hint off: more skills than the backend's Choice \
                 option limit (one option is reserved for \"none\")"
            );
        }
        let want_skill = self.advisory.skill_hint
            && !self.skills.is_empty()
            && choice_ok
            && caps.supports(QuestionKind::Noul)
            && !too_many_skills;
        let want_tools = self.advisory.tool_hint
            && choice_ok
            && turn.tools.len() >= self.advisory.tool_hint_min_tools.max(2)
            && turn.tools.len() <= caps.max_choice_options;
        if !want_skill && !want_tools {
            if !self.said_idle.swap(true, Ordering::Relaxed) {
                tracing::debug!(
                    skills = self.skills.len(),
                    tools = turn.tools.len(),
                    "decision advisory has nothing to ask: no skills to suggest and fewer \
                     than {} tools, so no decision request is sent",
                    self.advisory.tool_hint_min_tools
                );
            }
            return None;
        }
        let request = turn.user_request()?;

        let key = {
            let mut h = std::collections::hash_map::DefaultHasher::new();
            request.hash(&mut h);
            want_skill.hash(&mut h);
            if want_tools {
                for t in turn.tools {
                    t.name.hash(&mut h);
                }
            }
            h.finish()
        };
        if let Some((k, line)) = self.memo.lock().unwrap_or_else(|e| e.into_inner()).as_ref() {
            if *k == key {
                return line.clone();
            }
        }

        let mut questions = Vec::new();
        let none = self.none_option();
        if want_skill {
            let mut options: Vec<(String, String)> = self.skills.clone();
            options.push((none.into(), "No listed skill fits the request.".into()));
            questions.push((
                "skill".to_string(),
                Question::choice_with_criteria(
                    "Which of these skills, if any, is the right one to load to help with \
                     the user's latest request in `request`?",
                    options,
                ),
            ));
            questions.push((
                "skill_needed".to_string(),
                Question::noul(
                    "Would a careful expert handling `request` consult a specific documented \
                     procedure or set of commands, rather than answering from general \
                     understanding?",
                ),
            ));
        }
        if want_tools {
            questions.push((
                "tools".to_string(),
                Question::choice_with_criteria(
                    "Which of these tools will the assistant most likely need next to carry \
                     out `request`?",
                    turn.tools.iter().map(|t| {
                        let desc = truncate_head(&t.description, MAX_DESCRIPTION_CHARS);
                        let desc = if desc.trim().is_empty() {
                            t.name.clone()
                        } else {
                            desc
                        };
                        (t.name.clone(), desc)
                    }),
                ),
            ));
        }

        let state = json!({ "request": truncate_middle(&request, MAX_REQUEST_CHARS) });
        let eval = match self.model.evaluate(state, questions).await {
            Ok(eval) => eval,
            Err(DecisionError::MissingApiKey(var)) => {
                if !self.said_missing_key.swap(true, Ordering::Relaxed) {
                    tracing::warn!(
                        "decision advisory disabled: the decision model's API key is missing \
                         (set {var}); the agent continues without hints"
                    );
                }
                self.remember(key, None);
                return None;
            }
            Err(e) => {
                tracing::warn!("decision advisory skipped this request: {e}");
                self.remember(key, None);
                return None;
            }
        };

        let mut lines = Vec::new();
        if want_skill {
            let needed = eval.p_true("skill_needed").unwrap_or(0.0);
            if let Some(choice) = eval.choice("skill") {
                if needed >= self.advisory.skill_need_threshold
                    && choice.choice() != none
                    && choice.confidence() >= self.advisory.skill_confidence_threshold
                {
                    lines.push(format!(
                        "Relevant to the current request: {}. Ignore this if it does not fit \
                         what the user actually asked for.",
                        choice.choice()
                    ));
                }
            }
        }
        if want_tools {
            if let Some(choice) = eval.choice("tools") {
                let named: Vec<&str> = choice
                    .ranked()
                    .into_iter()
                    .filter(|(_, p)| *p >= self.advisory.tool_hint_min_probability)
                    .take(self.advisory.max_tool_hints)
                    .map(|(name, _)| name)
                    .collect();
                if !named.is_empty() {
                    lines.push(format!(
                        "Tools likely relevant to the current request: {}. This is a hint \
                         only; every tool remains available.",
                        named.join(", ")
                    ));
                }
            }
        }
        let line = (!lines.is_empty()).then(|| lines.join("\n"));
        self.remember(key, line.clone());
        line
    }

    fn remember(&self, key: u64, line: Option<String>) {
        *self.memo.lock().unwrap_or_else(|e| e.into_inner()) = Some((key, line));
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl TurnHook for Advisor {
    async fn before_turn(&self, turn: &TurnContext<'_>) -> Option<String> {
        self.advise(turn).await
    }
}

/// The advisor as an [`Extension`](crate::Extension): its hint is a
/// `before_model` note. A fresh advisor is built for each run (by
/// `decision::wire`), so its memo (one request per user request) spans that
/// run's turns.
pub(crate) struct AdvisorExtension(Arc<Advisor>);

impl AdvisorExtension {
    pub(crate) fn new(advisor: Advisor) -> Self {
        Self(Arc::new(advisor))
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl crate::Extension for AdvisorExtension {
    fn name(&self) -> &str {
        "decision-advisor"
    }

    async fn start_run(
        &self,
        _run: &crate::extension::RunContext<'_>,
    ) -> Result<Box<dyn crate::RunHooks>, crate::extension::ExtensionError> {
        Ok(Box::new(AdvisorHooks(self.0.clone())))
    }
}

struct AdvisorHooks(Arc<Advisor>);

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl crate::RunHooks for AdvisorHooks {
    async fn before_model(&mut self, turn: &TurnContext<'_>) -> crate::extension::TurnDecision {
        match self.0.advise(turn).await {
            Some(note) => crate::extension::TurnDecision::Note(note),
            None => crate::extension::TurnDecision::Continue,
        }
    }
}

/// At most `max` characters from the start, on a char boundary.
pub(crate) fn truncate_head(text: &str, max: usize) -> String {
    match text.char_indices().nth(max) {
        Some((end, _)) => format!("{}…", &text[..end]),
        None => text.to_string(),
    }
}

/// At most about `max` characters: the head and the tail, with an explicit
/// `[truncated N chars]` marker between them.
pub(crate) fn truncate_middle(text: &str, max: usize) -> String {
    let len = text.chars().count();
    if len <= max {
        return text.to_string();
    }
    let keep = max / 2;
    let head: String = text.chars().take(keep).collect();
    let tail: String = text.chars().skip(len - keep).collect();
    format!("{head}\n[truncated {} chars]\n{tail}", len - 2 * keep)
}