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
use std::pin::Pin;
use std::time::Duration;
use async_trait::async_trait;
use futures::Stream;
use futures::StreamExt;
use thiserror::Error;
use crate::ids::AgentId;
use crate::message::{AgentContent, AgentMessage, Role, RunStatus};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AgentSpec {
pub agent_id: AgentId,
pub agent_name: String,
pub instructions: String,
}
#[derive(Clone, Debug)]
pub struct RunRequest {
pub agent: AgentSpec,
/// Conversation thread; stateful tools (code mode REPL) scope by it.
/// Runs on one thread never overlap (the runtime queues them).
pub thread_id: Option<String>,
pub messages: Vec<AgentMessage>,
}
#[derive(Clone, Debug)]
pub struct RunOutcome {
pub status: RunStatus,
pub messages: Vec<AgentMessage>,
pub final_output: Option<AgentContent>,
pub last_error: Option<String>,
}
#[derive(Clone, Debug)]
pub enum BackendEvent {
Message(AgentMessage),
Completed(RunOutcome),
}
#[derive(Clone, Debug, PartialEq, Eq, Error)]
#[error("{0}")]
pub struct BackendError(pub String);
impl BackendError {
pub fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
}
pub type BackendStream = Pin<Box<dyn Stream<Item = Result<BackendEvent, BackendError>> + Send>>;
#[async_trait]
pub trait AgentBackend: Send + Sync {
async fn start_run(&self, request: RunRequest) -> Result<BackendStream, BackendError>;
}
#[derive(Clone, Debug)]
enum MockReply {
Fixed(String),
Echo,
}
/// Deterministic in-process backend used by the CLI and tests.
#[derive(Clone, Debug)]
pub struct MockBackend {
reply: MockReply,
failure: Option<String>,
delay: Option<Duration>,
}
impl MockBackend {
pub fn fixed(reply: impl Into<String>) -> Self {
Self {
reply: MockReply::Fixed(reply.into()),
failure: None,
delay: None,
}
}
pub fn echo() -> Self {
Self {
reply: MockReply::Echo,
failure: None,
delay: None,
}
}
pub fn with_failure(mut self, message: impl Into<String>) -> Self {
self.failure = Some(message.into());
self
}
pub fn with_delay(mut self, delay: Duration) -> Self {
self.delay = Some(delay);
self
}
}
#[async_trait]
impl AgentBackend for MockBackend {
async fn start_run(&self, request: RunRequest) -> Result<BackendStream, BackendError> {
if let Some(failure) = &self.failure {
return Err(BackendError::new(failure.clone()));
}
let reply = match &self.reply {
MockReply::Fixed(reply) => reply.clone(),
MockReply::Echo => request
.messages
.iter()
.rev()
.find_map(|message| message.content.as_text().map(str::to_owned))
.unwrap_or_else(|| "hello from asyncai".to_string()),
};
let assistant = AgentMessage {
role: Role::Assistant,
content: AgentContent::Text(reply.clone()),
};
let mut messages = request.messages;
messages.push(assistant.clone());
let events: Vec<Result<BackendEvent, BackendError>> = vec![
Ok(BackendEvent::Message(assistant)),
Ok(BackendEvent::Completed(RunOutcome {
status: RunStatus::Completed,
messages,
final_output: Some(AgentContent::Text(reply)),
last_error: None,
})),
];
let stream = futures::stream::iter(events);
match self.delay {
Some(delay) => Ok(Box::pin(stream.then(move |event| async move {
tokio::time::sleep(delay).await;
event
}))),
None => Ok(Box::pin(stream)),
}
}
}