rpi-agent 0.1.10

Agent runtime + loop, Rust port of pi-agent-core core layer
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Mirrors `packages/agent/src/agent.ts` — the stateful wrapper around the
//! low-level agent loop. `Agent` owns the current transcript, emits lifecycle
//! events to subscribers, executes tools, and exposes queueing APIs for
//! steering and follow-up messages.
//!
//! The TS `Agent` class drives the loop via `runWithLifecycle` +
//! `processEvents`. Rust models the same shape:
//! - internal `Mutex<MutableAgentState>` for the transcript + runtime flags;
//! - a `broadcast::Sender<AgentEvent>` so multiple subscribers each get their
//!   own copy;
//! - a state-reducing emitter ([`StatefulEmitter`]) that folds each event into
//!   `MutableAgentState` FIRST, then broadcasts — the direct mirror of TS
//!   `processEvents` ("reduce internal state, then await listeners");
//! - a per-run `ActiveRun` (abort handle + completion notify) so `prompt`
//!   blocks until the run settles and `abort`/`wait_for_idle` can act on it.

use crate::agent_loop::{run_agent_loop, run_agent_loop_continue};
use crate::events::{AgentEmitter, AgentEvent, BroadcastEmitter};
use crate::hooks::{default_convert_to_llm_fn, AgentLoopConfig, ConvertToLlm};
use crate::message::AgentMessage;
use crate::queue::PendingMessageQueue;
use crate::stream_fn::{get_default_stream_fn, StreamFn};
use crate::types::{AgentContext, AgentState, QueueMode, ToolExecutionMode};

use rpi_ai::types::{UserContent, UserMessage};
use rpi_ai::Model;
use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use tokio::sync::{broadcast, Notify};
use tokio_util::sync::CancellationToken;

/// Options for constructing an [`Agent`]. Mirrors TS `AgentOptions`. Only
/// `stream_fn` is required (or a default must have been installed process-wide
/// via [`crate::stream_fn::set_default_stream_fn`]).
#[derive(Default)]
pub struct AgentOptions {
    pub initial_state: Option<InitialState>,
    pub convert_to_llm: Option<ConvertToLlm>,
    pub stream_fn: Option<StreamFn>,
    pub thinking_level: Option<rpi_ai::types::ThinkingLevel>,
    pub queue_mode: Option<QueueMode>,
    pub follow_up_mode: Option<QueueMode>,
    pub tool_execution: Option<ToolExecutionMode>,
    pub session_id: Option<String>,
}

/// Subset of `AgentState` settable at construction.
#[derive(Default)]
pub struct InitialState {
    pub system_prompt: Option<String>,
    pub model: Option<Model>,
    pub thinking_level: Option<rpi_ai::types::ThinkingLevel>,
    pub tools: Option<Vec<Arc<dyn crate::agent_tool::AgentTool>>>,
    pub messages: Option<Vec<AgentMessage>>,
}

/// The owned mutable state. Mirrors TS `MutableAgentState`. Guarded by a mutex.
struct MutableAgentState {
    system_prompt: String,
    model: Model,
    thinking_level: rpi_ai::types::ThinkingLevel,
    tools: Vec<Arc<dyn crate::agent_tool::AgentTool>>,
    messages: Vec<AgentMessage>,
    is_streaming: bool,
    streaming_message: Option<AgentMessage>,
    pending_tool_calls: HashSet<String>,
    error_message: Option<String>,
}

impl MutableAgentState {
    fn snapshot(&self) -> AgentState {
        AgentState {
            system_prompt: self.system_prompt.clone(),
            model: self.model.clone(),
            thinking_level: self.thinking_level,
            tools: self.tools.clone(),
            messages: self.messages.clone(),
            is_streaming: self.is_streaming,
            streaming_message: self.streaming_message.clone(),
            pending_tool_calls: self.pending_tool_calls.clone(),
            error_message: self.error_message.clone(),
        }
    }

    /// Reduce an event into state. Mirrors the switch in TS `processEvents`.
    fn reduce(&mut self, event: &AgentEvent) {
        match event {
            AgentEvent::MessageStart { message } => {
                self.streaming_message = Some(message.clone());
            }
            AgentEvent::MessageUpdate { message, .. } => {
                self.streaming_message = Some(message.clone());
            }
            AgentEvent::MessageEnd { message } => {
                self.streaming_message = None;
                self.messages.push(message.clone());
            }
            AgentEvent::ToolExecutionStart { tool_call_id, .. } => {
                self.pending_tool_calls.insert(tool_call_id.clone());
            }
            AgentEvent::ToolExecutionEnd { tool_call_id, .. } => {
                self.pending_tool_calls.remove(tool_call_id);
            }
            AgentEvent::TurnEnd { message, .. } => {
                if let Some(am) = message.as_assistant() {
                    if am.error_message.is_some() {
                        self.error_message = am.error_message.clone();
                    }
                }
            }
            AgentEvent::AgentEnd { .. } => {
                self.streaming_message = None;
            }
            _ => {}
        }
    }
}

/// Per-run handle. Replaces TS `ActiveRun`.
struct ActiveRun {
    abort: CancellationToken,
    done: Arc<Notify>,
}

/// A state-reducing emitter: folds each event into `MutableAgentState` FIRST,
/// then broadcasts to subscribers. The direct mirror of TS `processEvents`.
struct StatefulEmitter {
    state: Arc<Mutex<MutableAgentState>>,
    broadcast: BroadcastEmitter,
}

impl AgentEmitter for StatefulEmitter {
    fn emit(&self, event: AgentEvent) -> futures::future::BoxFuture<'static, ()> {
        self.state.lock().expect("state lock").reduce(&event);
        self.broadcast.try_emit(event);
        Box::pin(async {})
    }
    fn try_emit(&self, event: AgentEvent) {
        self.state.lock().expect("state lock").reduce(&event);
        self.broadcast.try_emit(event);
    }
}

/// Shared queue storage: `Arc<Mutex<PendingMessageQueue>>` so the
/// `get_steering_messages` / `get_follow_up_messages` hook closures (which must
/// be `'static + Send + Sync`) can capture a clone.
type SharedQueue = Arc<Mutex<PendingMessageQueue>>;

/// A stateful agent. Clone shares the same inner state + event channel (like
/// holding a second reference to the TS `Agent` instance).
#[derive(Clone)]
pub struct Agent {
    inner: Arc<Inner>,
}

struct Inner {
    state: Arc<Mutex<MutableAgentState>>,
    convert_to_llm: ConvertToLlm,
    stream_fn: StreamFn,
    steering_queue: SharedQueue,
    follow_up_queue: SharedQueue,
    session_id: Option<String>,
    tool_execution: ToolExecutionMode,
    event_tx: broadcast::Sender<AgentEvent>,
    active_run: Mutex<Option<ActiveRun>>,
}

/// Builder for [`Agent`] with a fluent API. Mirrors the TS `new Agent(opts)`.
pub struct AgentBuilder {
    opts: AgentOptions,
}

impl AgentBuilder {
    pub fn new() -> Self {
        Self {
            opts: AgentOptions::default(),
        }
    }

    pub fn model(mut self, model: Model) -> Self {
        self.opts
            .initial_state
            .get_or_insert_with(InitialState::default)
            .model = Some(model);
        self
    }

    pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.opts
            .initial_state
            .get_or_insert_with(InitialState::default)
            .system_prompt = Some(prompt.into());
        self
    }

    pub fn thinking_level(mut self, level: rpi_ai::types::ThinkingLevel) -> Self {
        self.opts
            .initial_state
            .get_or_insert_with(InitialState::default)
            .thinking_level = Some(level);
        self
    }

    pub fn tools(mut self, tools: Vec<Arc<dyn crate::agent_tool::AgentTool>>) -> Self {
        self.opts
            .initial_state
            .get_or_insert_with(InitialState::default)
            .tools = Some(tools);
        self
    }

    pub fn messages(mut self, messages: Vec<AgentMessage>) -> Self {
        self.opts
            .initial_state
            .get_or_insert_with(InitialState::default)
            .messages = Some(messages);
        self
    }

    pub fn stream_fn(mut self, stream_fn: StreamFn) -> Self {
        self.opts.stream_fn = Some(stream_fn);
        self
    }

    pub fn convert_to_llm(mut self, f: ConvertToLlm) -> Self {
        self.opts.convert_to_llm = Some(f);
        self
    }

    pub fn queue_mode(mut self, mode: QueueMode) -> Self {
        self.opts.queue_mode = Some(mode);
        self
    }

    pub fn follow_up_mode(mut self, mode: QueueMode) -> Self {
        self.opts.follow_up_mode = Some(mode);
        self
    }

    pub fn tool_execution(mut self, mode: ToolExecutionMode) -> Self {
        self.opts.tool_execution = Some(mode);
        self
    }

    pub fn session_id(mut self, id: impl Into<String>) -> Self {
        self.opts.session_id = Some(id.into());
        self
    }

    /// Build the `Agent`. Errors if no `stream_fn` was supplied and no default
    /// has been installed process-wide.
    pub fn build(self) -> Result<Agent, crate::AgentError> {
        let stream_fn = match self.opts.stream_fn {
            Some(f) => f,
            None => get_default_stream_fn()?,
        };

        let initial = self.opts.initial_state.unwrap_or_default();
        let model = initial.model.unwrap_or_else(default_model);
        let thinking_level = self
            .opts
            .thinking_level
            .or(initial.thinking_level)
            .unwrap_or(rpi_ai::types::ThinkingLevel::Off);
        let convert_to_llm = self
            .opts
            .convert_to_llm
            .unwrap_or_else(default_convert_to_llm_fn);

        let state = MutableAgentState {
            system_prompt: initial.system_prompt.unwrap_or_default(),
            model: model.clone(),
            thinking_level,
            tools: initial.tools.unwrap_or_default(),
            messages: initial.messages.unwrap_or_default(),
            is_streaming: false,
            streaming_message: None,
            pending_tool_calls: HashSet::new(),
            error_message: None,
        };

        let (event_tx, _) = broadcast::channel(256);

        let queue_mode = self.opts.queue_mode.unwrap_or_default();
        let follow_up_mode = self.opts.follow_up_mode.unwrap_or_default();

        let inner = Inner {
            state: Arc::new(Mutex::new(state)),
            convert_to_llm,
            stream_fn,
            steering_queue: Arc::new(Mutex::new(PendingMessageQueue::new(queue_mode))),
            follow_up_queue: Arc::new(Mutex::new(PendingMessageQueue::new(follow_up_mode))),
            session_id: self.opts.session_id,
            tool_execution: self.opts.tool_execution.unwrap_or_default(),
            event_tx,
            active_run: Mutex::new(None),
        };

        Ok(Agent {
            inner: Arc::new(inner),
        })
    }
}

impl Default for AgentBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl Agent {
    /// Subscribe to agent lifecycle events. Returns a `broadcast::Receiver`.
    /// Mirrors TS `subscribe`.
    pub fn subscribe(&self) -> broadcast::Receiver<AgentEvent> {
        self.inner.event_tx.subscribe()
    }

    /// Current agent state snapshot. Clones tools/messages so callers can't
    /// mutate internal state. Mirrors TS `get state()`.
    pub fn state(&self) -> AgentState {
        self.inner.state.lock().expect("state lock").snapshot()
    }

    /// Queue a steering message (injected after the current turn's tool batch).
    pub fn steer(&self, message: AgentMessage) {
        self.inner
            .steering_queue
            .lock()
            .expect("steer lock")
            .enqueue(message);
    }

    /// Queue a follow-up message (injected when the agent would otherwise stop).
    pub fn follow_up(&self, message: AgentMessage) {
        self.inner
            .follow_up_queue
            .lock()
            .expect("followup lock")
            .enqueue(message);
    }

    /// True when either queue has pending messages.
    pub fn has_queued_messages(&self) -> bool {
        let s = self.inner.steering_queue.lock().expect("steer lock");
        if !s.is_empty() {
            return true;
        }
        let f = self.inner.follow_up_queue.lock().expect("followup lock");
        !f.is_empty()
    }

    /// Abort the current run, if one is active. No-op otherwise.
    pub fn abort(&self) {
        if let Some(run) = self.inner.active_run.lock().expect("run lock").as_ref() {
            run.abort.cancel();
        }
    }

    /// Resolve when the current run finishes (or immediately if idle).
    pub async fn wait_for_idle(&self) {
        let notify = {
            let guard = self.inner.active_run.lock().expect("run lock");
            guard.as_ref().map(|r| Arc::clone(&r.done))
        };
        if let Some(n) = notify {
            n.notified().await;
        }
    }

    /// Start a new prompt from text. Convenience for `prompt_message`.
    pub async fn prompt(&self, text: impl Into<String>) -> Result<(), crate::AgentError> {
        let message =
            AgentMessage::User(UserMessage::new(UserContent::Text(text.into()), now_ms()));
        self.prompt_messages(vec![message]).await
    }

    /// Start a new prompt from a single `AgentMessage`.
    pub async fn prompt_message(&self, message: AgentMessage) -> Result<(), crate::AgentError> {
        self.prompt_messages(vec![message]).await
    }

    /// Start a new prompt from a batch of `AgentMessage`s.
    pub async fn prompt_messages(
        &self,
        messages: Vec<AgentMessage>,
    ) -> Result<(), crate::AgentError> {
        self.start_active_run()?;
        let run_done = self.current_done();
        let outcome = self.run_prompt(messages).await;
        self.finish_run();
        if let Some(n) = run_done {
            n.notify_waiters();
        }
        outcome
    }

    /// Continue from the current transcript. Errors if the agent is busy or
    /// the last message is an assistant message with no queued steering/follow-up.
    pub async fn continue_run(&self) -> Result<(), crate::AgentError> {
        self.start_active_run()?;
        let run_done = self.current_done();
        let outcome = self.run_continue().await;
        self.finish_run();
        if let Some(n) = run_done {
            n.notify_waiters();
        }
        outcome
    }

    /// Clear transcript + runtime state + queues. Errors if a run is active.
    pub fn reset(&self) -> Result<(), crate::AgentError> {
        let mut state = self.inner.state.lock().expect("state lock");
        if self.inner.active_run.lock().expect("run lock").is_some() {
            return Err(crate::AgentError::State(
                "Agent is already processing. Wait for completion before resetting.".into(),
            ));
        }
        state.messages.clear();
        state.is_streaming = false;
        state.streaming_message = None;
        state.pending_tool_calls.clear();
        state.error_message = None;
        drop(state);
        let _ = self
            .inner
            .steering_queue
            .lock()
            .expect("steer lock")
            .try_drain();
        let _ = self
            .inner
            .follow_up_queue
            .lock()
            .expect("followup lock")
            .try_drain();
        Ok(())
    }

    // ---- internals --------------------------------------------------------

    fn start_active_run(&self) -> Result<(), crate::AgentError> {
        let mut guard = self.inner.active_run.lock().expect("run lock");
        if guard.is_some() {
            return Err(crate::AgentError::State(
                "Agent is already processing a prompt. Use steer() or followUp() to queue messages, or wait for completion.".into(),
            ));
        }
        let abort = CancellationToken::new();
        let done = Arc::new(Notify::new());
        *guard = Some(ActiveRun {
            abort: abort.clone(),
            done: Arc::clone(&done),
        });

        let mut state = self.inner.state.lock().expect("state lock");
        state.is_streaming = true;
        state.streaming_message = None;
        state.error_message = None;
        drop(state);
        Ok(())
    }

    fn finish_run(&self) {
        {
            let mut state = self.inner.state.lock().expect("state lock");
            state.is_streaming = false;
            state.streaming_message = None;
            state.pending_tool_calls.clear();
        }
        let mut guard = self.inner.active_run.lock().expect("run lock");
        *guard = None;
    }

    fn current_done(&self) -> Option<Arc<Notify>> {
        self.inner
            .active_run
            .lock()
            .expect("run lock")
            .as_ref()
            .map(|r| Arc::clone(&r.done))
    }

    fn abort_token(&self) -> CancellationToken {
        self.inner
            .active_run
            .lock()
            .expect("run lock")
            .as_ref()
            .map(|r| r.abort.clone())
            .unwrap_or_else(CancellationToken::new)
    }

    fn context_snapshot(&self) -> AgentContext {
        let state = self.inner.state.lock().expect("state lock");
        AgentContext {
            system_prompt: state.system_prompt.clone(),
            messages: state.messages.clone(),
            tools: state.tools.clone(),
        }
    }

    fn build_config(&self, signal: CancellationToken) -> AgentLoopConfig {
        let state = self.inner.state.lock().expect("state lock");
        let steering = Arc::clone(&self.inner.steering_queue);
        let follow_up = Arc::clone(&self.inner.follow_up_queue);
        AgentLoopConfig {
            model: state.model.clone(),
            convert_to_llm: Arc::clone(&self.inner.convert_to_llm),
            transform_context: None,
            get_api_key: None,
            should_stop_after_turn: None,
            prepare_next_turn: None,
            after_tool_results: None,
            get_steering_messages: Some(Arc::new(move || {
                let q = Arc::clone(&steering);
                Box::pin(async move { q.lock().expect("steer lock").try_drain() })
            })),
            get_follow_up_messages: Some(Arc::new(move || {
                let q = Arc::clone(&follow_up);
                Box::pin(async move { q.lock().expect("followup lock").try_drain() })
            })),
            before_tool_call: None,
            after_tool_call: None,
            tool_execution: self.inner.tool_execution,
            thinking_level: state.thinking_level,
            api_key: None,
            timeout: None,
            max_retries: None,
            max_retry_delay: None,
            cache_retention: rpi_ai::provider::CacheRetention::default(),
            session_id: self.inner.session_id.clone(),
            signal,
        }
    }

    async fn run_prompt(&self, messages: Vec<AgentMessage>) -> Result<(), crate::AgentError> {
        let signal = self.abort_token();
        let context = self.context_snapshot();
        let config = self.build_config(signal);
        let emit: Arc<dyn AgentEmitter> = Arc::new(StatefulEmitter {
            state: Arc::clone(&self.inner.state),
            broadcast: BroadcastEmitter::from_sender(self.inner.event_tx.clone()),
        });
        let stream_fn = Arc::clone(&self.inner.stream_fn);
        run_agent_loop(messages, context, config, emit, stream_fn).await?;
        Ok(())
    }

    async fn run_continue(&self) -> Result<(), crate::AgentError> {
        let signal = self.abort_token();
        let context = self.context_snapshot();
        if context.messages.is_empty() {
            return Err(crate::AgentError::State(
                "No messages to continue from".into(),
            ));
        }
        if context.messages.last().unwrap().is_assistant() {
            return Err(crate::AgentError::State(
                "Cannot continue from message role: assistant".into(),
            ));
        }
        let config = self.build_config(signal);
        let emit: Arc<dyn AgentEmitter> = Arc::new(StatefulEmitter {
            state: Arc::clone(&self.inner.state),
            broadcast: BroadcastEmitter::from_sender(self.inner.event_tx.clone()),
        });
        let stream_fn = Arc::clone(&self.inner.stream_fn);
        run_agent_loop_continue(context, config, emit, stream_fn).await?;
        Ok(())
    }
}

fn default_model() -> Model {
    rpi_ai::model::Model::new(
        "unknown",
        "unknown",
        rpi_ai::types::Api::Other("unknown".into()),
        "unknown",
        "",
    )
}

fn now_ms() -> i64 {
    use std::sync::atomic::{AtomicI64, Ordering};
    static T: AtomicI64 = AtomicI64::new(1);
    T.fetch_add(1, Ordering::Relaxed)
}

#[cfg(test)]
mod tests {
    use super::*;
    use rpi_ai::event_stream::create_assistant_message_event_stream;
    use rpi_ai::provider::Provider;
    use rpi_ai::providers::faux::{FauxProvider, FauxScript};

    fn faux_stream_fn(provider: Arc<FauxProvider>) -> StreamFn {
        crate::stream_fn::stream_fn(move |model, ctx, opts| {
            // The faux provider's stream_simple is async; StreamFn is sync-return.
            // Bridge by spawning the producer ourselves.
            let (mut prod, stream) = create_assistant_message_event_stream();
            let p = Arc::clone(&provider);
            let model = model.clone();
            let ctx = ctx.clone();
            let opts = opts.clone();
            tokio::spawn(async move {
                let mut s = p.stream_simple(&model, &ctx, &opts).await;
                // Drain the real stream into our producer.
                while let Some(ev) = s.next().await {
                    if !prod.push(ev) {
                        break;
                    }
                }
            });
            stream
        })
    }

    #[tokio::test]
    async fn builder_requires_stream_fn() {
        let res = AgentBuilder::new().build();
        assert!(res.is_err(), "build with no stream_fn should error");
    }

    #[tokio::test]
    async fn prompt_with_faux_text_collects_events() {
        let provider = FauxProvider::new(FauxScript::new().with_text("hello"));
        let sf = faux_stream_fn(provider);
        let agent = AgentBuilder::new().stream_fn(sf).build().unwrap();

        let mut rx = agent.subscribe();
        agent.prompt("hi").await.unwrap();

        // Drain the broadcast until AgentEnd.
        let mut saw_start = false;
        let mut saw_end = false;
        while let Ok(ev) = rx.try_recv() {
            match ev {
                AgentEvent::AgentStart => saw_start = true,
                AgentEvent::AgentEnd { .. } => saw_end = true,
                _ => {}
            }
        }
        assert!(saw_start, "agent_start observed");
        assert!(saw_end, "agent_end observed");
        // State: 1 user prompt + 1 assistant reply.
        assert_eq!(agent.state().messages.len(), 2);
    }
}