aidaemon-core 0.23.1

The aidaemon agent kernel: a small, hook-driven agent loop with tools, providers, receipts and resumable state
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
use std::time::Duration;

use async_trait::async_trait;
use futures::stream::{self, BoxStream, StreamExt};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::message::{Message, ToolCall};

/// What the model sees about a tool: the OpenAI-style function object.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolSpec {
    pub name: String,
    pub description: String,
    /// JSON Schema for the arguments object.
    pub parameters: Value,
}

/// How the model may use tools on this call.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub enum ToolChoice {
    #[default]
    Auto,
    None,
    Required,
    Specific(String),
}

/// The shape a reply must take.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseFormat {
    #[default]
    Text,
    /// Any JSON object.
    JsonObject,
    /// JSON matching `schema`.
    JsonSchema {
        name: String,
        schema: Value,
        strict: bool,
    },
}

/// How a provider should answer, beyond the messages and tools. Providers
/// map what their API supports and ignore the rest.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct RequestOptions {
    pub response_format: ResponseFormat,
    /// Provider-neutral reasoning effort: `"low"`, `"medium"` or `"high"`;
    /// `"off"` turns reasoning off. `None` keeps the provider's default.
    pub reasoning_effort: Option<String>,
    /// Exactly one attempt: providers skip their own internal retries, and
    /// [`Retry`] and [`crate::Failover`] give up at the first error. For a
    /// call that must not run twice.
    pub single_attempt: bool,
    /// With `single_attempt`, lets a [`crate::Failover`] chain move on to
    /// its next step after an error that proves the attempt used nothing
    /// ([`ProviderError::proves_nothing_spent`]) or after the policy
    /// refused the attempt before sending it. Any other error still ends
    /// the chain, and no step is retried. For a call charged against a
    /// fixed budget, where unknown spend must not be repeated.
    pub fallback_when_unspent: bool,
    /// Identifies the conversation for providers that route prompt-cache
    /// lookups by key, so its calls reach the same cache. Only a routing
    /// hint: providers that do not route by key ignore it.
    pub cache_key: Option<String>,
}

/// Deferred tools as a provider that can surface tools mid-conversation
/// sends them, so loading one never changes the request's tool block (and
/// with it the whole cached prefix). Providers without that ignore it:
/// [`ModelRequest::tools`] already holds every tool the model may call.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ToolSurfacing {
    /// Every deferred tool not loaded when the run segment started,
    /// name-sorted: declared hidden on every call of the segment.
    pub deferred: Vec<ToolSpec>,
    /// Those loaded since, in load order.
    pub loads: Vec<ToolLoad>,
}

/// Deferred tools a run loaded, surfaced after the result of the call
/// that answers `after_tool_call` (the batch whose call loaded them).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolLoad {
    pub after_tool_call: String,
    pub names: Vec<String>,
}

/// One provider call. Hooks may edit it in [`crate::Hook::before_model`].
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ModelRequest {
    pub model: String,
    /// Instructions first (stable, cacheable), then the transcript.
    pub messages: Vec<Message>,
    pub tools: Vec<ToolSpec>,
    pub tool_choice: ToolChoice,
    pub max_output_tokens: Option<u32>,
    pub options: RequestOptions,
    /// The run's deferred tools, for providers that keep a fixed tool block
    /// and surface loaded tools in the transcript; `None` for a run with no
    /// deferred tools.
    pub tool_surfacing: Option<ToolSurfacing>,
}

/// Token accounting for one call or a whole run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct Usage {
    pub input_tokens: u64,
    pub output_tokens: u64,
    /// Input tokens served from the provider's prompt cache.
    pub cached_input_tokens: u64,
    pub cache_creation_tokens: u64,
}

impl Usage {
    pub fn add(&mut self, other: Usage) {
        self.input_tokens += other.input_tokens;
        self.output_tokens += other.output_tokens;
        self.cached_input_tokens += other.cached_input_tokens;
        self.cache_creation_tokens += other.cache_creation_tokens;
    }

    /// Tokens that count toward a budget: cached input is excluded.
    pub fn billable_tokens(&self) -> u64 {
        self.input_tokens.saturating_sub(self.cached_input_tokens) + self.output_tokens
    }
}

/// A complete (non-streamed) model reply.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ModelResponse {
    pub text: String,
    pub tool_calls: Vec<ToolCall>,
    pub usage: Usage,
    /// The model's visible reasoning, when the provider returns it apart
    /// from the answer.
    pub reasoning: Option<String>,
    /// Why the reply may be incomplete (cut off at the token limit, held
    /// back by a safety filter), in the provider's words.
    pub note: Option<String>,
    /// The reply stopped at the output-token limit, not where the model
    /// ended it: its text is cut short.
    pub cut_off: bool,
}

/// One item of a streamed reply. A well-formed stream yields any number of
/// deltas and ends with exactly one `Done` carrying the complete response.
#[derive(Debug, Clone, PartialEq)]
pub enum StreamEvent {
    /// Visible text as it is generated.
    TextDelta(String),
    /// The full reply, with tool calls and usage.
    Done(ModelResponse),
    /// A [`crate::Failover`] chain gave up on one provider and is trying
    /// the next. Comes before any text of the reply.
    Switched(crate::ProviderSwitch),
}

#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum ProviderError {
    #[error("rate limited")]
    RateLimited { retry_after: Option<Duration> },
    #[error("transient provider error: {0}")]
    Transient(String),
    #[error("invalid request: {0}")]
    InvalidRequest(String),
    /// The provider refused the request's tool definitions (a schema it
    /// does not compile). Dropping media or options cannot cure it: the
    /// same tools are refused again, so only another model may answer.
    #[error("invalid tool definitions: {0}")]
    InvalidTools(String),
    #[error("authentication failed: {0}")]
    Auth(String),
    /// The account cannot pay for the request. `affordable_tokens` is the
    /// output the provider says it could still pay for, when it says.
    #[error("billing: {message}")]
    Billing {
        message: String,
        affordable_tokens: Option<u64>,
    },
    /// A usage quota (a subscription plan's cap, a daily allowance) is
    /// used up until `resets_at`, when the provider says. Unlike
    /// [`ProviderError::RateLimited`], waiting seconds does not help.
    /// `per_model` is true when the quota covers only the requested model;
    /// otherwise it covers every model on the account. `in_stream` is true
    /// when the provider reported it inside a response it had already
    /// started, so the request was admitted and may have used tokens;
    /// false when the request was refused before any answer.
    #[error("quota exhausted: {message}")]
    QuotaExhausted {
        message: String,
        resets_at: Option<std::time::SystemTime>,
        per_model: bool,
        in_stream: bool,
    },
    /// The model or endpoint does not exist here.
    #[error("not found: {0}")]
    NotFound(String),
    /// The request does not fit the model's context window. `limit` is
    /// the window in tokens, when the provider states it.
    #[error("context window exceeded: {message}")]
    ContextOverflow { message: String, limit: Option<u64> },
    /// The reply could not be read. `parse` is true when it was not valid
    /// at all (often a cut-off stream, worth another attempt), false when it
    /// parsed but had the wrong shape.
    #[error("malformed reply: {message}")]
    Malformed { message: String, parse: bool },
    #[error("provider error: {0}")]
    Other(String),
}

impl ProviderError {
    /// The provider refused the request before doing any work, so the call
    /// used no tokens: bad credentials, an unknown model, no credit, or a
    /// used-up quota reported as a refusal. Anything else (a timeout, a
    /// server error, a cut-off or malformed reply, a quota reported inside
    /// an answer already started) may have been paid for.
    pub fn proves_nothing_spent(&self) -> bool {
        match self {
            Self::Auth(_) | Self::NotFound(_) | Self::Billing { .. } => true,
            Self::QuotaExhausted { in_stream, .. } => !in_stream,
            _ => false,
        }
    }

    /// Worth another attempt at the same provider.
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            Self::RateLimited { .. } | Self::Transient(_) | Self::Malformed { parse: true, .. }
        )
    }
}

/// A model backend. Implementations translate [`ModelRequest`] to their wire
/// format and back. Retries and fallbacks are wrappers ([`Retry`]), not loop
/// logic.
///
/// The loop always calls [`Provider::stream`]. Providers without native
/// streaming implement only `complete`; the default `stream` wraps it in a
/// single `Done` event. Streaming providers implement both, usually with
/// `complete` as a fold over `stream` (see [`collect_stream`]).
#[async_trait]
pub trait Provider: Send + Sync {
    fn name(&self) -> &str;

    async fn complete(&self, request: &ModelRequest) -> Result<ModelResponse, ProviderError>;

    fn stream<'a>(
        &'a self,
        request: &'a ModelRequest,
    ) -> BoxStream<'a, Result<StreamEvent, ProviderError>> {
        Box::pin(stream::once(async move {
            self.complete(request).await.map(StreamEvent::Done)
        }))
    }

    /// When `model` can next be asked, if this provider already knows it
    /// cannot answer now (a used-up quota, a rejected credential, no
    /// credit; see [`crate::QuotaGuard`]). `None`: it may answer now.
    fn unavailable_until(&self, _model: &str) -> Option<std::time::SystemTime> {
        None
    }

    /// The model to use when the agent names none. `None` (the default)
    /// means the agent must name one with [`crate::AgentBuilder::model`];
    /// a provider that serves one model, or a test double, says which.
    fn default_model(&self) -> Option<&str> {
        None
    }
}

/// Drain a stream into its final response. Useful for implementing
/// `complete` on a streaming provider.
pub async fn collect_stream(
    mut events: BoxStream<'_, Result<StreamEvent, ProviderError>>,
) -> Result<ModelResponse, ProviderError> {
    while let Some(event) = events.next().await {
        if let StreamEvent::Done(response) = event? {
            return Ok(response);
        }
    }
    Err(ProviderError::Transient(
        "stream ended without a final response".into(),
    ))
}

#[async_trait]
impl<P: Provider + ?Sized> Provider for std::sync::Arc<P> {
    fn name(&self) -> &str {
        (**self).name()
    }
    async fn complete(&self, request: &ModelRequest) -> Result<ModelResponse, ProviderError> {
        (**self).complete(request).await
    }
    fn stream<'a>(
        &'a self,
        request: &'a ModelRequest,
    ) -> BoxStream<'a, Result<StreamEvent, ProviderError>> {
        (**self).stream(request)
    }
    // Forwarded like the calls: without it every `Arc<dyn Provider>` (the
    // daemon's model backends) reported a used-up model as available.
    fn unavailable_until(&self, model: &str) -> Option<std::time::SystemTime> {
        (**self).unavailable_until(model)
    }
    fn default_model(&self) -> Option<&str> {
        (**self).default_model()
    }
}

/// Retry layer: exponential backoff on retryable errors.
pub struct Retry<P> {
    inner: P,
    max_attempts: u32,
    base_delay: Duration,
}

impl<P: Provider> Retry<P> {
    fn delay_for(&self, err: &ProviderError, attempt: u32) -> Duration {
        match err {
            ProviderError::RateLimited {
                retry_after: Some(after),
            } => *after,
            _ => self.base_delay * 2u32.saturating_pow(attempt.saturating_sub(1)),
        }
    }

    pub fn new(inner: P) -> Self {
        Self {
            inner,
            max_attempts: 3,
            base_delay: Duration::from_millis(500),
        }
    }

    pub fn max_attempts(mut self, attempts: u32) -> Self {
        self.max_attempts = attempts.max(1);
        self
    }

    pub fn base_delay(mut self, delay: Duration) -> Self {
        self.base_delay = delay;
        self
    }
}

#[async_trait]
impl<P: Provider> Provider for Retry<P> {
    fn name(&self) -> &str {
        self.inner.name()
    }
    fn unavailable_until(&self, model: &str) -> Option<std::time::SystemTime> {
        self.inner.unavailable_until(model)
    }
    fn default_model(&self) -> Option<&str> {
        self.inner.default_model()
    }

    async fn complete(&self, request: &ModelRequest) -> Result<ModelResponse, ProviderError> {
        let mut attempt = 0;
        loop {
            attempt += 1;
            match self.inner.complete(request).await {
                Ok(response) => return Ok(response),
                Err(err)
                    if err.is_retryable()
                        && attempt < self.max_attempts
                        && !request.options.single_attempt =>
                {
                    tokio::time::sleep(self.delay_for(&err, attempt)).await;
                }
                Err(err) => return Err(err),
            }
        }
    }

    /// Retries only while nothing has been streamed yet: once a delta has
    /// reached the caller, a retry would duplicate visible output.
    fn stream<'a>(
        &'a self,
        request: &'a ModelRequest,
    ) -> BoxStream<'a, Result<StreamEvent, ProviderError>> {
        let opened = async move {
            let mut attempt = 0;
            loop {
                attempt += 1;
                let mut events = self.inner.stream(request);
                match events.next().await {
                    Some(Err(err))
                        if err.is_retryable()
                            && attempt < self.max_attempts
                            && !request.options.single_attempt =>
                    {
                        tokio::time::sleep(self.delay_for(&err, attempt)).await;
                    }
                    first => return (first, events),
                }
            }
        };
        Box::pin(stream::once(opened).flat_map(|(first, rest)| stream::iter(first).chain(rest)))
    }
}