agent-sdk 0.9.2

Rust Agent SDK for building LLM agents
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
use crate::authority::EventAuthority;
use crate::context::{CompactionConfig, ContextCompactor};
use crate::hooks::ToolAuditSink;
use crate::llm::StopReason;
use crate::stores::{EventStore, ToolExecutionStore};
use crate::tools::{ToolContext, ToolRegistry};
#[cfg(feature = "otel")]
use crate::types::RunOptions;
use crate::types::{
    AgentConfig, AgentContinuation, AgentError, AgentInput, AgentState, ListenExecutionContext,
    PendingToolCallInfo, ThreadId, TokenUsage, ToolResult, TurnOptions,
};
use agent_sdk_foundation::audit::AuditProvenance;
use std::sync::Arc;
use std::time::{Duration, Instant};
use time::OffsetDateTime;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

/// Internal result of executing a single turn.
///
/// This is used internally by both `run_loop` and `run_single_turn`.
pub(super) enum InternalTurnResult {
    /// Turn completed, more turns needed (tools were executed)
    Continue { turn_usage: TokenUsage },
    /// Done - no more tool calls
    Done,
    /// Model refused the request (safety/policy)
    Refusal,
    /// Run was cancelled via the cancellation token while the turn was
    /// in flight — during the LLM call (streaming or non-streaming) or
    /// during context compaction. Carries the partial usage accrued in
    /// the turn before the cancel was honored.
    Cancelled { turn_usage: TokenUsage },
    /// Awaiting confirmation (yields)
    AwaitingConfirmation {
        tool_call_id: String,
        tool_name: String,
        display_name: String,
        input: serde_json::Value,
        description: String,
        continuation: Box<AgentContinuation>,
    },
    /// Tool calls ready for external execution (server mode)
    PendingToolCalls {
        turn_usage: TokenUsage,
        pending_tool_calls: Vec<PendingToolCallInfo>,
        continuation: Box<AgentContinuation>,
    },
    /// Error
    Error(AgentError),
}

/// Maximum number of compaction retries before giving up.
pub(super) const MAX_COMPACTION_RETRIES: usize = 3;

/// Mutable context for turn execution.
///
/// This holds all the state that's modified during execution.
pub(super) struct TurnContext {
    pub(super) thread_id: ThreadId,
    pub(super) turn: usize,
    pub(super) total_usage: TokenUsage,
    pub(super) state: AgentState,
    pub(super) start_time: Instant,
    /// Number of consecutive compaction retries for context overflow.
    pub(super) compaction_retries: usize,
    /// Optional system reminder to inject into the next LLM call.
    ///
    /// Set by `begin_turn` when approaching the turn limit, then consumed
    /// by `execute_turn_inner` to append a user message to the conversation.
    pub(super) pending_reminder: Option<String>,
    // ── Turn summary accumulators ───────────────────────────────────
    //
    // These mirror fields on `agent_sdk_foundation::TurnSummary` and are
    // populated incrementally as the turn progresses, then promoted to
    // a full `TurnSummary` by `build_turn_summary` when the outcome is
    // produced. Keeping the accumulators on `TurnContext` lets the
    // existing flow populate them at the same spots where the legacy
    // usage accounting already happens, without adding another
    // parameter to every function along the path.
    /// Provider response ID from the most recent LLM call in this turn.
    pub(super) response_id: Option<String>,
    /// Stop reason from the most recent LLM call in this turn.
    pub(super) stop_reason: Option<StopReason>,
    /// Number of tool calls the LLM requested in this turn.
    pub(super) tool_call_count: usize,
    /// Static input-kind label captured from the run's [`AgentInput`].
    ///
    /// Threaded onto every per-turn metric so dashboards can group
    /// turn timings by whether the run was kicked off with `text`,
    /// `message`, `continue`, `resume`, or `submit_tool_results`. The
    /// value is constant across turns because every continuation
    /// turn shares the same originating run.
    #[cfg(feature = "otel")]
    pub(super) input_kind: &'static str,
}

/// Data extracted from `AgentInput::Resume` after validation.
pub(super) struct ResumeData {
    pub(super) continuation: Box<AgentContinuation>,
    pub(super) tool_call_id: String,
    pub(super) confirmed: bool,
    pub(super) rejection_reason: Option<String>,
}

/// Result of initializing state from agent input.
pub(super) struct InitializedState {
    pub(super) turn: usize,
    pub(super) total_usage: TokenUsage,
    pub(super) state: AgentState,
    pub(super) resume_data: Option<ResumeData>,
}

/// Outcome of executing a single tool call.
pub(super) enum ToolExecutionOutcome {
    /// Tool executed successfully (or failed), result captured
    Completed { tool_id: String, result: ToolResult },
    /// Tool requires user confirmation before execution
    RequiresConfirmation {
        tool_id: String,
        tool_name: String,
        display_name: String,
        input: serde_json::Value,
        description: String,
        listen_context: Option<ListenExecutionContext>,
    },
    /// Event persistence or other infrastructure failure aborted the tool step.
    Error(AgentError),
}

pub(super) const MAX_LISTEN_UPDATES: usize = 240;
pub(super) const LISTEN_UPDATE_TIMEOUT: Duration = Duration::from_secs(30);
pub(super) const LISTEN_TOTAL_TIMEOUT: Duration = Duration::from_mins(5);

pub(super) struct ListenReady {
    pub(super) operation_id: String,
    pub(super) revision: u64,
    pub(super) snapshot: serde_json::Value,
    pub(super) expires_at: Option<OffsetDateTime>,
}

#[derive(Clone, Copy)]
pub(super) enum ListenProgressStage {
    Update,
    Ready,
    Invalidated,
}

impl ListenProgressStage {
    pub(super) const fn as_str(self) -> &'static str {
        match self {
            Self::Update => "listen_update",
            Self::Ready => "listen_ready",
            Self::Invalidated => "listen_invalidated",
        }
    }
}

pub(super) enum ListenUpdateHandling {
    Continue,
    Ready(ListenReady),
}

pub(super) struct ListenUpdateContext<'a, H> {
    pub(super) pending: &'a PendingToolCallInfo,
    pub(super) hooks: &'a Arc<H>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) thread_id: &'a ThreadId,
    pub(super) turn: usize,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
}

pub(super) struct ListenWaitParams<'a, Ctx, H> {
    pub(super) pending: &'a PendingToolCallInfo,
    pub(super) tool: &'a Arc<dyn crate::tools::ErasedListenTool<Ctx>>,
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) update_ctx: ListenUpdateContext<'a, H>,
}

pub(super) struct ToolCallExecutionContext<'a, Ctx, H> {
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) thread_id: &'a ThreadId,
    pub(super) tools: &'a ToolRegistry<Ctx>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) turn: usize,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) execution_store: Option<&'a Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: &'a Arc<dyn ToolAuditSink>,
    pub(super) provenance: &'a AuditProvenance,
}

pub(super) struct ConfirmedToolExecutionContext<'a, Ctx, H> {
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) thread_id: &'a ThreadId,
    pub(super) tools: &'a ToolRegistry<Ctx>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) turn: usize,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) execution_store: Option<&'a Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: &'a Arc<dyn ToolAuditSink>,
    pub(super) provenance: &'a AuditProvenance,
}

/// Error type for stream processing.
pub(super) enum StreamError {
    Recoverable(String),
    Fatal(String),
    /// The run's cancellation token fired while the stream was still
    /// being consumed. Terminal — not retried.
    Cancelled,
}

/// Three-state outcome of an LLM call (streaming or non-streaming).
///
/// Distinguishes a clean cancellation from an error so the turn loop
/// can return [`InternalTurnResult::Cancelled`] (balanced history, a
/// `Cancelled` terminal event) instead of folding the cancel into a
/// generic [`InternalTurnResult::Error`].
pub(super) enum LlmOutcome {
    Response(crate::llm::ChatResponse),
    Cancelled,
    Error(AgentError),
}

/// Turn-summary metrics captured from the LLM call that preceded the
/// pause, surfaced by [`process_resume`] so the resume handler can
/// build a [`TurnSummary`] from real data instead of fabricating a
/// [`TurnContext`].
///
/// These fields describe the **turn-closing** LLM call for the turn
/// being summarised — the same call whose tool-use blocks produced the
/// pending tool calls that the resume is now finishing. They are
/// threaded through [`AgentContinuation`] so they survive the pause /
/// resume boundary without needing a separate side-channel.
pub(super) struct ResumeSummaryMetrics {
    pub(super) response_id: Option<String>,
    pub(super) stop_reason: Option<StopReason>,
    pub(super) tool_call_count: usize,
}

pub(super) enum ResumeProcessingResult {
    Completed {
        turn_usage: TokenUsage,
        metrics: ResumeSummaryMetrics,
    },
    AwaitingConfirmation {
        tool_call_id: String,
        tool_name: String,
        display_name: String,
        input: serde_json::Value,
        description: String,
        continuation: Box<AgentContinuation>,
    },
}

pub(super) struct RunLoopParameters<Ctx, P, H, M, S> {
    pub(super) event_store: Arc<dyn EventStore>,
    pub(super) authority: Arc<dyn EventAuthority>,
    pub(super) thread_id: ThreadId,
    pub(super) input: AgentInput,
    pub(super) tool_context: ToolContext<Ctx>,
    pub(super) provider: Arc<P>,
    pub(super) tools: Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: Arc<H>,
    pub(super) message_store: Arc<M>,
    pub(super) state_store: Arc<S>,
    pub(super) config: AgentConfig,
    pub(super) compaction_config: Option<CompactionConfig>,
    pub(super) compactor: Option<Arc<dyn ContextCompactor>>,
    pub(super) execution_store: Option<Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: Arc<dyn ToolAuditSink>,
    pub(super) cancel_token: CancellationToken,
    /// Optional channel for receiving new messages in persistent mode.
    pub(super) input_rx: Option<mpsc::Receiver<AgentInput>>,
    /// Trace metadata applied to the root span (and threaded through
    /// every event emission as `langfuse.trace.output`).
    ///
    /// Always present on the otel build path; defaults to
    /// [`RunOptions::default`] when the caller used the historical
    /// `run` / `run_persistent` entry points. Stripped from non-otel
    /// builds because nothing consumes it there.
    #[cfg(feature = "otel")]
    pub(super) run_options: RunOptions,
    #[cfg(feature = "otel")]
    pub(super) observability_store: Option<Arc<dyn crate::observability::ObservabilityStore>>,
}

pub(super) struct ResumeProcessingParameters<'a, Ctx, H, M> {
    pub(super) resume_data: ResumeData,
    pub(super) turn: usize,
    pub(super) total_usage: &'a TokenUsage,
    pub(super) state: &'a AgentState,
    pub(super) thread_id: &'a ThreadId,
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) tools: &'a Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) execution_store: Option<&'a Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: &'a Arc<dyn ToolAuditSink>,
    pub(super) provenance: &'a AuditProvenance,
}

pub(super) struct RunLoopResumeParams<'a, Ctx, H, M> {
    pub(super) resume_data: ResumeData,
    pub(super) turn: usize,
    pub(super) total_usage: &'a TokenUsage,
    pub(super) state: &'a AgentState,
    pub(super) thread_id: &'a ThreadId,
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) tools: &'a Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) execution_store: Option<&'a Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: &'a Arc<dyn ToolAuditSink>,
    pub(super) provenance: &'a AuditProvenance,
}

pub(super) struct RunLoopTurnsParams<'a, Ctx, P, H, M, S> {
    pub(super) ctx: &'a mut TurnContext,
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) provider: &'a Arc<P>,
    pub(super) tools: &'a Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) state_store: &'a Arc<S>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) config: &'a AgentConfig,
    pub(super) compaction_config: Option<&'a CompactionConfig>,
    pub(super) compactor: Option<&'a Arc<dyn ContextCompactor>>,
    pub(super) execution_store: Option<&'a Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: &'a Arc<dyn ToolAuditSink>,
    pub(super) provenance: &'a AuditProvenance,
    pub(super) cancel_token: &'a CancellationToken,
    /// Optional channel for receiving new messages in persistent mode.
    pub(super) input_rx: Option<&'a mut mpsc::Receiver<AgentInput>>,
    pub(super) turn_options: &'a TurnOptions,
    #[cfg(feature = "otel")]
    pub(super) observability_store: Option<&'a Arc<dyn crate::observability::ObservabilityStore>>,
}

pub(super) struct PersistentDoneParams<'a, H, M> {
    pub(super) ctx: &'a TurnContext,
    pub(super) rx: &'a mut mpsc::Receiver<AgentInput>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) current_turn: usize,
    pub(super) cancel_token: &'a CancellationToken,
}

pub(super) struct RunLoopTurnResultParams<'a, H, M, S> {
    pub(super) result: InternalTurnResult,
    pub(super) ctx: &'a TurnContext,
    pub(super) input_rx: Option<&'a mut mpsc::Receiver<AgentInput>>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) state_store: &'a Arc<S>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) cancel_token: &'a CancellationToken,
    pub(super) current_turn: usize,
}

pub(super) struct SingleTurnResumeParams<Ctx, H, M, S> {
    pub(super) resume_data: ResumeData,
    pub(super) turn: usize,
    pub(super) total_usage: TokenUsage,
    pub(super) state: AgentState,
    pub(super) thread_id: ThreadId,
    pub(super) tool_context: ToolContext<Ctx>,
    pub(super) tools: Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: Arc<H>,
    pub(super) event_store: Arc<dyn EventStore>,
    pub(super) authority: Arc<dyn EventAuthority>,
    pub(super) message_store: Arc<M>,
    pub(super) state_store: Arc<S>,
    pub(super) execution_store: Option<Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: Arc<dyn ToolAuditSink>,
    pub(super) provenance: AuditProvenance,
    /// Execution options selected by the caller, needed so the resume
    /// path can carry [`TurnOptions`] through into the emitted
    /// [`agent_sdk_foundation::TurnSummary`].
    pub(super) turn_options: TurnOptions,
    /// Wall-clock instant when the enclosing `run_turn` invocation
    /// started — used to measure `duration_ms` for the summary.
    pub(super) start_time: Instant,
}

pub(super) struct TurnParameters<Ctx, P, H, M, S> {
    pub(super) event_store: Arc<dyn EventStore>,
    pub(super) authority: Arc<dyn EventAuthority>,
    pub(super) thread_id: ThreadId,
    pub(super) input: AgentInput,
    pub(super) tool_context: ToolContext<Ctx>,
    pub(super) provider: Arc<P>,
    pub(super) tools: Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: Arc<H>,
    pub(super) message_store: Arc<M>,
    pub(super) state_store: Arc<S>,
    pub(super) config: AgentConfig,
    pub(super) compaction_config: Option<CompactionConfig>,
    pub(super) compactor: Option<Arc<dyn ContextCompactor>>,
    pub(super) execution_store: Option<Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: Arc<dyn ToolAuditSink>,
    pub(super) cancel_token: CancellationToken,
    pub(super) turn_options: TurnOptions,
    /// Trace metadata applied to the root span. See
    /// [`RunLoopParameters::run_options`] for the full contract.
    /// Only consumed on the otel build path — see the gating note
    /// on [`RunLoopParameters::run_options`].
    #[cfg(feature = "otel")]
    pub(super) run_options: RunOptions,
    #[cfg(feature = "otel")]
    pub(super) observability_store: Option<Arc<dyn crate::observability::ObservabilityStore>>,
}

/// Execute a single turn of the agent loop.
///
/// This is the core turn execution logic shared by both `run_loop` (looping mode)
/// and `run_single_turn` (single-turn mode).
pub(super) struct ExecuteTurnParameters<'a, Ctx, P, H, M, S> {
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) ctx: &'a mut TurnContext,
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) provider: &'a Arc<P>,
    pub(super) tools: &'a Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) state_store: &'a Arc<S>,
    pub(super) config: &'a AgentConfig,
    pub(super) compaction_config: Option<&'a CompactionConfig>,
    pub(super) compactor: Option<&'a Arc<dyn ContextCompactor>>,
    pub(super) execution_store: Option<&'a Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: &'a Arc<dyn ToolAuditSink>,
    pub(super) provenance: &'a AuditProvenance,
    pub(super) turn_options: &'a TurnOptions,
    /// Run-level cancellation token, threaded into the LLM call and the
    /// compaction path so both phases are raced against cancel.
    pub(super) cancel_token: &'a CancellationToken,
    #[cfg(feature = "otel")]
    pub(super) observability_store: Option<&'a Arc<dyn crate::observability::ObservabilityStore>>,
}

pub(super) struct TurnMessageLoadParams<'a, P, H, M> {
    pub(super) thread_id: &'a ThreadId,
    pub(super) turn: usize,
    pub(super) provider: &'a Arc<P>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) compaction_config: Option<&'a CompactionConfig>,
    pub(super) compactor: Option<&'a Arc<dyn ContextCompactor>>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    /// Run-level cancellation token; checked before the destructive
    /// `replace_history` write so a cancel during compaction cannot let
    /// a slow summary land after the user asked to stop.
    pub(super) cancel_token: &'a CancellationToken,
}

pub(super) struct LlmCallParams<'a, P, H> {
    pub(super) provider: &'a Arc<P>,
    pub(super) request: crate::llm::ChatRequest,
    pub(super) config: &'a AgentConfig,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) thread_id: &'a ThreadId,
    pub(super) turn: usize,
    pub(super) message_id: &'a str,
    pub(super) thinking_id: &'a str,
    /// Run-level cancellation token, raced against the provider call
    /// (streaming + non-streaming) so a cancel mid-stream or before
    /// the first token stops the LLM phase promptly instead of waiting
    /// for the response to complete.
    pub(super) cancel_token: &'a CancellationToken,
    #[cfg(feature = "otel")]
    pub(super) observability_store: Option<&'a Arc<dyn crate::observability::ObservabilityStore>>,
}

pub(super) struct LlmEventContext<'a, H> {
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) thread_id: &'a ThreadId,
    pub(super) turn: usize,
    /// Run-level cancellation token observed inside the streaming /
    /// retry loops. See [`LlmCallParams::cancel_token`].
    pub(super) cancel_token: &'a CancellationToken,
}

#[derive(Clone, Copy)]
pub(super) struct LlmStreamIds<'a> {
    pub(super) message_id: &'a str,
    pub(super) thinking_id: &'a str,
}

pub(super) struct ProcessedTurnResponse {
    pub(super) stop_reason: Option<StopReason>,
    pub(super) text_content: Option<String>,
    pub(super) pending_tool_calls: Vec<PendingToolCallInfo>,
}

pub(super) struct TurnResponseProcessingParams<'a, Ctx, H, M> {
    pub(super) response: crate::llm::ChatResponse,
    pub(super) message_id: &'a str,
    pub(super) thinking_id: &'a str,
    pub(super) thread_id: &'a ThreadId,
    pub(super) turn: usize,
    pub(super) tools: &'a Arc<ToolRegistry<Ctx>>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
}

pub(super) struct ToolBatchExecutionParams<'a, Ctx, H> {
    pub(super) pending_tool_calls: Vec<PendingToolCallInfo>,
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) thread_id: &'a ThreadId,
    pub(super) tools: &'a Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) execution_store: Option<&'a Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: &'a Arc<dyn ToolAuditSink>,
    pub(super) provenance: &'a AuditProvenance,
    pub(super) turn: usize,
    pub(super) total_usage: &'a TokenUsage,
    pub(super) turn_usage: &'a TokenUsage,
    pub(super) state: &'a AgentState,
    /// Response ID from the LLM call that produced `pending_tool_calls`.
    /// Copied into any [`AgentContinuation`] this phase emits so the
    /// resume-side [`TurnSummary`] can report the same value.
    pub(super) response_id: Option<String>,
    /// Stop reason from the LLM call that produced `pending_tool_calls`.
    /// Copied into any [`AgentContinuation`] this phase emits so the
    /// resume-side [`TurnSummary`] can report the same value.
    pub(super) stop_reason: Option<StopReason>,
}

pub(super) struct TurnCompletionParams<'a, H, M> {
    pub(super) tool_results: &'a [(String, ToolResult)],
    pub(super) thread_id: &'a ThreadId,
    pub(super) turn: usize,
    pub(super) turn_usage: &'a TokenUsage,
    pub(super) message_store: &'a Arc<M>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
}

pub(super) struct TurnToolPhaseParams<'a, Ctx, H, M> {
    pub(super) pending_tool_calls: Vec<PendingToolCallInfo>,
    pub(super) tool_context: &'a ToolContext<Ctx>,
    pub(super) thread_id: &'a ThreadId,
    pub(super) tools: &'a Arc<ToolRegistry<Ctx>>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) execution_store: Option<&'a Arc<dyn ToolExecutionStore>>,
    pub(super) audit_sink: &'a Arc<dyn ToolAuditSink>,
    pub(super) provenance: &'a AuditProvenance,
    pub(super) turn: usize,
    pub(super) total_usage: &'a TokenUsage,
    pub(super) turn_usage: &'a TokenUsage,
    pub(super) state: &'a AgentState,
    pub(super) message_store: &'a Arc<M>,
    /// Response ID from the LLM call that produced `pending_tool_calls`.
    /// Forwarded into any [`AgentContinuation`] this phase emits.
    pub(super) response_id: Option<String>,
    /// Stop reason from the LLM call that produced `pending_tool_calls`.
    /// Forwarded into any [`AgentContinuation`] this phase emits.
    pub(super) stop_reason: Option<StopReason>,
}

pub(super) struct TurnStopReasonParams<'a, P, H, M> {
    pub(super) stop_reason: Option<StopReason>,
    pub(super) text_content: Option<String>,
    pub(super) had_tool_calls: bool,
    pub(super) message_id: String,
    pub(super) turn_usage: TokenUsage,
    pub(super) ctx: &'a mut TurnContext,
    pub(super) provider: &'a Arc<P>,
    pub(super) message_store: &'a Arc<M>,
    pub(super) compaction_config: Option<&'a CompactionConfig>,
    pub(super) compactor: Option<&'a Arc<dyn ContextCompactor>>,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    /// Run-level cancellation token, forwarded into the overflow-driven
    /// compaction path triggered by `ModelContextWindowExceeded`.
    pub(super) cancel_token: &'a CancellationToken,
}

pub(super) struct ConvertTurnResultParams<'a, H, S> {
    pub(super) result: InternalTurnResult,
    pub(super) ctx: TurnContext,
    pub(super) event_store: &'a Arc<dyn EventStore>,
    pub(super) hooks: &'a Arc<H>,
    pub(super) authority: &'a Arc<dyn EventAuthority>,
    pub(super) thread_id: ThreadId,
    pub(super) current_turn: usize,
    pub(super) state_store: &'a Arc<S>,
    /// Provider / model provenance, captured once at the start of the
    /// turn and promoted into every [`TurnSummary`] this conversion
    /// produces.
    pub(super) provenance: &'a AuditProvenance,
    /// Execution options selected by the caller for this turn.
    pub(super) turn_options: &'a TurnOptions,
}

/// Extracted content from an LLM response: (thinking, text, `tool_uses`).
pub(super) type ExtractedContent = (
    Option<String>,
    Option<String>,
    Vec<(String, String, serde_json::Value)>,
);