rig-ecs 0.44.0

rig inside a Bevy World: effects as entities, handlers as entities, the driver as a system.
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Shared by the `run_*` suites: a request-capturing model, a tool handler
//! that is never called, an app with both plugins, and the tick guard.

#![allow(dead_code, reason = "each suite uses the part of the support it needs")]

use rig_core::serve::Dispatch;
use std::{
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};

use bevy_app::App;
use bevy_ecs::{prelude::*, schedule::LogLevel};
use rig_core::{
    completion::{CompletionRequest, CompletionResponse, ModelRef, ProviderCapabilities, Usage},
    effect::{EffectKind, FamilyDescriptor, HandlerDescriptor, HandlerKey, Outcome},
    error::{ErrorKind, ErrorReport},
    message::{AssistantContent, Message},
    serve::{Serve, ServingPolicy},
};
use rig_ecs::{
    agent::{
        AdditionalParams, DefaultMaxTurns, Failed, InvalidCalls, MaxTokens, MaxTurns, Output,
        Owner, Preamble, Settled, Temperature, ToolChoiceSpec, UsesModel, Utterance,
        content::parts::read_message,
    },
    bus::{BusPlugin, Handlers, PendingEffect},
    systems::AgentPlugin,
};

pub const GUARD: Duration = Duration::from_secs(10);

/// A model that keeps every request it is asked and answers a fixed text.
pub struct Capturing {
    pub label: String,
    pub requests: Arc<Mutex<Vec<CompletionRequest>>>,
    pub answer: String,
}

impl Capturing {
    pub fn new(label: &str, answer: &str) -> (Self, Arc<Mutex<Vec<CompletionRequest>>>) {
        let requests = Arc::new(Mutex::new(Vec::new()));
        (
            Self {
                label: label.to_owned(),
                requests: Arc::clone(&requests),
                answer: answer.to_owned(),
            },
            requests,
        )
    }
}

impl Serve for Capturing {
    type Family = rig_core::effect::family::Completion;

    fn descriptor(&self) -> HandlerDescriptor {
        HandlerDescriptor {
            key: HandlerKey::from(self.label.as_str()),
            family: FamilyDescriptor::Completion {
                model: ModelRef::new(self.label.as_str()),
                capabilities: ProviderCapabilities::default(),
            },
            layers: Vec::new(),
        }
    }

    async fn serve(&self, kind: EffectKind, _dispatch: Dispatch) -> rig_core::serve::Reply {
        match kind {
            EffectKind::Completion { request, .. } => {
                self.requests.lock().expect("requests").push(request);
                let response = CompletionResponse::new(
                    vec![AssistantContent::text(&self.answer)],
                    Usage::default(),
                    rig_core::message::Origin::new("test.api", "capturing", ""),
                    serde_json::json!({ "provider": "capturing" }),
                );
                rig_core::serve::Reply::Outcome(Ok(Outcome::Completion(response)))
            }
            other => rig_core::serve::Reply::Outcome(Err(ErrorReport::new(
                ErrorKind::HandlerUnavailable,
                format!("a model cannot serve {}", other.name()),
            ))),
        }
    }
}

/// A tool handler that is advertised and never called.
pub struct NeverCalled {
    pub name: String,
}

impl Serve for NeverCalled {
    type Family = rig_core::effect::family::Tool;

    fn descriptor(&self) -> HandlerDescriptor {
        HandlerDescriptor {
            key: HandlerKey::from(self.name.as_str()),
            family: FamilyDescriptor::Tool {
                name: self.name.clone(),
                description: format!("the {} tool", self.name),
                parameters: serde_json::json!({"type": "object"}),
                embedding: None,
            },
            layers: Vec::new(),
        }
    }

    async fn serve(&self, _kind: EffectKind, _dispatch: Dispatch) -> rig_core::serve::Reply {
        rig_core::serve::Reply::Outcome(Err(ErrorReport::new(
            ErrorKind::Internal,
            "a tool advertised and never called was called",
        )))
    }
}

/// An app with the bus and the agent installed, ambiguity detection at
/// error level, the runner in `Update`.
pub fn app() -> App {
    let mut app = App::new();
    app.add_plugins(
        rig_ecs::RigPlugin::with_policy(ServingPolicy::default())
            .ambiguity_detection(LogLevel::Error),
    );
    app.add_plugins(rig_cassette::ecs::ReplayPlugin);
    app.finish();
    app.cleanup();
    app
}

#[path = "../bus_support/registration.rs"]
mod registration;
pub use registration::register;

/// An agent entity over `model`, with a preamble and defaults.
pub fn spawn_agent(world: &mut World, owner: &str, model: Entity) -> Entity {
    world
        .spawn((
            Owner(owner.to_owned()),
            Preamble(Some("You are terse.".to_owned())),
            Temperature(None),
            MaxTokens(None),
            AdditionalParams(None),
            ToolChoiceSpec(None),
            Output::default(),
            DefaultMaxTurns(None),
            MaxTurns(1),
            InvalidCalls::default(),
            UsesModel(model),
        ))
        .id()
}

/// Tick the app until `done` holds, or fail after [`GUARD`].
pub fn tick_until(app: &mut App, what: &str, mut done: impl FnMut(&mut World) -> bool) {
    let start = Instant::now();
    loop {
        app.update();
        if done(app.world_mut()) {
            return;
        }
        assert!(start.elapsed() < GUARD, "{what}: not done within {GUARD:?}");
        std::thread::yield_now();
    }
}

/// The text parts of a request's user messages, in order.
pub fn texts(request: &CompletionRequest) -> Vec<String> {
    request
        .chat_history
        .iter()
        .map(|message| match message {
            rig_core::message::Message::System { content } => format!("system:{content}"),
            rig_core::message::Message::User { content } => format!(
                "user:{}",
                content
                    .iter()
                    .filter_map(|part| match part {
                        rig_core::message::UserContent::Text(text) => Some(text.text.clone()),
                        rig_core::message::UserContent::ToolResult(_)
                        | rig_core::message::UserContent::Image(_)
                        | rig_core::message::UserContent::Audio(_)
                        | rig_core::message::UserContent::Video(_)
                        | rig_core::message::UserContent::Document(_) => None,
                    })
                    .collect::<String>()
            ),
            rig_core::message::Message::Assistant(rig_core::message::AssistantMessage {
                content,
                ..
            }) => format!(
                "assistant:{}",
                content
                    .iter()
                    .filter_map(|part| match part {
                        AssistantContent::Text(text) => Some(text.text.clone()),
                        _ => None,
                    })
                    .collect::<String>()
            ),
        })
        .collect()
}

/// A completion model that never answers: the dispatch stays in flight
/// for as long as the world lives.
pub struct NeverAnswers {
    pub label: String,
}

impl Serve for NeverAnswers {
    type Family = rig_core::effect::family::Completion;

    fn descriptor(&self) -> HandlerDescriptor {
        HandlerDescriptor {
            key: HandlerKey::from(self.label.as_str()),
            family: FamilyDescriptor::Completion {
                model: ModelRef::new(self.label.as_str()),
                capabilities: ProviderCapabilities::default(),
            },
            layers: Vec::new(),
        }
    }

    async fn serve(&self, _kind: EffectKind, _dispatch: Dispatch) -> rig_core::serve::Reply {
        std::future::pending().await
    }
}

/// A model that answers a script: one reply per request, in order, then a
/// fixed text.
pub struct Scripted {
    pub label: String,
    pub replies: Mutex<std::collections::VecDeque<CompletionResponse>>,
    pub requests: Arc<Mutex<Vec<CompletionRequest>>>,
}

impl Scripted {
    /// A script of assistant turns, each a reply with no finish reason.
    pub fn new(
        label: &str,
        turns: Vec<Vec<AssistantContent>>,
    ) -> (Self, Arc<Mutex<Vec<CompletionRequest>>>) {
        Self::replies(label, turns.into_iter().map(reply).collect())
    }

    /// A script of whole replies, such as ones that end in an error.
    pub fn replies(
        label: &str,
        replies: Vec<CompletionResponse>,
    ) -> (Self, Arc<Mutex<Vec<CompletionRequest>>>) {
        let requests = Arc::new(Mutex::new(Vec::new()));
        (
            Self {
                label: label.to_owned(),
                replies: Mutex::new(replies.into()),
                requests: Arc::clone(&requests),
            },
            requests,
        )
    }
}

/// The reply a scripted model sends for `choice`, with no finish reason.
pub fn reply(choice: Vec<AssistantContent>) -> CompletionResponse {
    CompletionResponse::new(
        choice,
        Usage::default(),
        rig_core::message::Origin::new("test.api", "scripted", ""),
        serde_json::json!({}),
    )
}

impl Serve for Scripted {
    type Family = rig_core::effect::family::Completion;

    fn descriptor(&self) -> HandlerDescriptor {
        HandlerDescriptor {
            key: HandlerKey::from(self.label.as_str()),
            family: FamilyDescriptor::Completion {
                model: ModelRef::new(self.label.as_str()),
                capabilities: ProviderCapabilities::default(),
            },
            layers: Vec::new(),
        }
    }

    async fn serve(&self, kind: EffectKind, _dispatch: Dispatch) -> rig_core::serve::Reply {
        match kind {
            EffectKind::Completion { request, .. } => {
                self.requests.lock().expect("requests").push(request);
                let response = self
                    .replies
                    .lock()
                    .expect("replies")
                    .pop_front()
                    .unwrap_or_else(|| reply(vec![AssistantContent::text("done")]));
                rig_core::serve::Reply::Outcome(Ok(Outcome::Completion(response)))
            }
            other => rig_core::serve::Reply::Outcome(Err(ErrorReport::new(
                ErrorKind::HandlerUnavailable,
                format!("a model cannot serve {}", other.name()),
            ))),
        }
    }
}

/// A tool call the model makes, as the script's assistant part.
pub fn call(id: &str, name: &str, arguments: serde_json::Value) -> AssistantContent {
    AssistantContent::tool_call(
        id,
        rig_core::message::ToolName::new(name).expect("tool name"),
        arguments,
    )
}

/// A tool that adds `x` and `y`, counting how many calls were in flight
/// at once.
pub struct Adder {
    pub name: String,
    pub in_flight: Arc<std::sync::atomic::AtomicUsize>,
    pub peak: Arc<std::sync::atomic::AtomicUsize>,
    pub hold: Option<Arc<Mutex<Option<futures::channel::oneshot::Receiver<()>>>>>,
}

impl Adder {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_owned(),
            in_flight: Arc::default(),
            peak: Arc::default(),
            hold: None,
        }
    }
}

impl Serve for Adder {
    type Family = rig_core::effect::family::Tool;

    fn descriptor(&self) -> HandlerDescriptor {
        HandlerDescriptor {
            key: HandlerKey::from(self.name.as_str()),
            family: FamilyDescriptor::Tool {
                name: "add".to_owned(),
                description: "adds x and y".to_owned(),
                parameters: serde_json::json!({"type": "object", "properties": {"x": {"type": "integer"}, "y": {"type": "integer"}}}),
                embedding: None,
            },
            layers: Vec::new(),
        }
    }

    async fn serve(&self, kind: EffectKind, _dispatch: Dispatch) -> rig_core::serve::Reply {
        use std::sync::atomic::Ordering;
        let EffectKind::ToolCall { args, .. } = kind else {
            return rig_core::serve::Reply::Outcome(Err(ErrorReport::new(
                ErrorKind::Request,
                "a tool call",
            )));
        };
        let now = self.in_flight.fetch_add(1, Ordering::SeqCst) + 1;
        self.peak.fetch_max(now, Ordering::SeqCst);
        let parsed: serde_json::Value = serde_json::from_str(&args).unwrap_or_default();
        let sum = parsed["x"].as_i64().unwrap_or(0) + parsed["y"].as_i64().unwrap_or(0);
        // Let a sibling start before answering, so concurrency shows.
        for _ in 0..3 {
            bevy_tasks::futures_lite::future::yield_now().await;
        }
        self.in_flight.fetch_sub(1, Ordering::SeqCst);
        rig_core::serve::Reply::Outcome(Ok(Outcome::ToolResult {
            result: rig_core::tool::ToolResult::success(rig_core::tool::ToolOutput::json(
                serde_json::json!(sum),
            )),
        }))
    }
}

/// The requests a registered model kept, shared with the test.
pub type RequestsSeen = Arc<Mutex<Vec<CompletionRequest>>>;

/// A [`Scripted`] model registered under `key`, and an agent over it.
pub fn scripted_agent(
    app: &mut App,
    key: &str,
    turns: Vec<Vec<AssistantContent>>,
) -> (Entity, RequestsSeen) {
    replied_agent(app, key, turns.into_iter().map(reply).collect())
}

/// [`scripted_agent`] over whole replies ([`Scripted::replies`]).
pub fn replied_agent(
    app: &mut App,
    key: &str,
    replies: Vec<CompletionResponse>,
) -> (Entity, RequestsSeen) {
    let (model, requests) = Scripted::replies(key, replies);
    let model = register(app, key, model);
    (spawn_agent(app.world_mut(), "t", model), requests)
}

/// A [`Capturing`] model registered under `key`, labelled `label` in its
/// own descriptor, and an agent over it.
pub fn capturing_agent(
    app: &mut App,
    key: &str,
    label: &str,
    answer: &str,
) -> (Entity, RequestsSeen) {
    let (model, requests) = Capturing::new(label, answer);
    let model = register(app, key, model);
    (spawn_agent(app.world_mut(), "t", model), requests)
}

/// Tick until `run` settled or failed, or fail after [`GUARD`].
pub fn ended(app: &mut App, run: Entity, what: &str) {
    tick_until(app, what, |world| {
        world.get::<Settled>(run).is_some() || world.get::<Failed>(run).is_some()
    });
}

/// A bare world with the bus and the agent installed, an open completion
/// handler under `model`, and one agent over it.
pub fn open_model_world() -> (World, Entity) {
    let mut world = World::new();
    BusPlugin::with_policy(ServingPolicy::default()).install(&mut world);
    AgentPlugin::install(&mut world);
    rig_ecs::checkpoint::register_types(&mut world);
    let model = Handlers::with(&mut world, |handlers| {
        handlers.register_open(
            "model",
            FamilyDescriptor::Completion {
                model: ModelRef::new("model"),
                capabilities: ProviderCapabilities::default(),
            },
        )
    })
    .unwrap()
    .unwrap();
    let agent = world.spawn((Owner("owner".into()), UsesModel(model))).id();
    (world, agent)
}

/// The one utterance `ChildOf` `run`.
pub fn first_utterance(world: &mut World, run: Entity) -> Entity {
    world
        .query_filtered::<(Entity, &ChildOf), With<Utterance>>()
        .iter(world)
        .find(|(_, parent)| parent.parent() == run)
        .unwrap()
        .0
}

/// The utterances `ChildOf` `run`, in sibling (`Children`) order.
pub fn utterances_of(world: &mut World, run: Entity) -> Vec<Entity> {
    world
        .get::<Children>(run)
        .into_iter()
        .flat_map(|children| children.iter())
        .filter(|child| world.get::<Utterance>(*child).is_some())
        .collect()
}

/// The fresh, uncached render of `run`'s history, as the DTOs the graph
/// reconstructs.
pub fn graph_messages(world: &mut World, run: Entity) -> Vec<Message> {
    utterances_of(world, run)
        .into_iter()
        .map(|entity| read_message(world, entity).unwrap().to_message())
        .collect()
}

/// The completion requests folded under `run`, by turn order (the run's
/// sibling order).
pub fn requests(world: &mut World, run: Entity) -> Vec<CompletionRequest> {
    let turns: Vec<Entity> = world
        .get::<Children>(run)
        .into_iter()
        .flat_map(|children| children.iter())
        .collect();
    let mut found: Vec<_> = world
        .query::<(&PendingEffect, &ChildOf)>()
        .iter(world)
        .filter_map(|(effect, parent)| match &effect.kind {
            EffectKind::Completion { request, .. } => {
                let position = turns.iter().position(|turn| *turn == parent.parent())?;
                Some((position, request.clone()))
            }
            _ => None,
        })
        .collect();
    found.sort_by_key(|(position, _)| *position);
    found.into_iter().map(|(_, request)| request).collect()
}