nanocodex-oai-api 0.3.0

Tower-native OpenAI Responses API and managed context for Nanocodex
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
//! Stable typed projections of the agent event firehose.

use std::sync::Arc;

use serde::Deserialize;
use serde_json::value::RawValue;

use crate::CostStatus;
use crate::{AgentEventKind, EstimatedUsdCost, MessagePhase, ToolOutputBody, Usage};

/// A normalized view of one event in the session-wide agent firehose.
///
/// Call [`crate::events::AgentEvent::data`] to obtain this view. The original raw
/// payload remains available on [`crate::events::AgentEvent`] for lossless JSONL
/// adapters and forward-compatible diagnostics.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum AgentEventData {
    /// One complete inbound or outbound `OpenAI` protocol frame.
    OpenAi(OpenAiEvent),
    /// Incremental or completed assistant output.
    Assistant(AssistantEvent),
    /// Model reasoning that the API made visible.
    Reasoning(ReasoningEvent),
    /// Agent-turn lifecycle state.
    Run(RunEvent),
    /// Tool invocation lifecycle state.
    Tool(ToolEvent),
    /// Logical model-call lifecycle state.
    Model(ModelEvent),
    /// Context compaction lifecycle state.
    Context(ContextEvent),
    /// Lower-level retry or connection diagnostics.
    Transport(TransportEvent),
}

/// One raw `OpenAI` Responses protocol frame with stable routing metadata.
///
/// `event` deliberately remains raw JSON. Provider wire events are already
/// available as the typed per-operation [`crate::ResponseEvent`] stream, while
/// this firehose variant preserves every frame without forcing unknown
/// provider additions through a JSON value tree.
#[derive(Clone, Debug, Deserialize)]
pub struct OpenAiEvent {
    /// Whether the frame was sent or received.
    pub direction: String,
    /// Transport identifier used for the frame.
    pub transport: String,
    /// Logical Responses phase such as `generation` or `compaction`.
    pub phase: String,
    /// Agent model-call index, when the frame belongs to one.
    pub model_call_index: Option<u32>,
    /// Exact provider frame.
    pub event: Box<RawValue>,
}

/// Assistant output emitted by the agent.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum AssistantEvent {
    /// Incremental displayable text.
    Delta(AssistantDelta),
    /// One complete assistant message.
    Message(AssistantMessage),
}

/// Incremental displayable assistant text.
#[derive(Clone, Debug, Deserialize)]
pub struct AssistantDelta {
    /// Logical model-call index.
    pub model_call_index: u32,
    /// Provider output-item identity when supplied.
    pub item_id: Option<String>,
    /// Commentary or final-answer phase when supplied.
    pub phase: Option<MessagePhase>,
    /// Newly received text.
    pub text: String,
}

/// One complete assistant message.
#[derive(Clone, Debug, Deserialize)]
pub struct AssistantMessage {
    /// Logical model-call index.
    pub model_call_index: u32,
    /// Provider output-item identity when supplied.
    pub item_id: Option<String>,
    /// Commentary or final-answer phase when supplied.
    pub phase: Option<MessagePhase>,
    /// Complete message text.
    pub text: String,
}

/// API-visible reasoning output.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ReasoningEvent {
    /// Incremental reasoning-summary text.
    SummaryDelta(ReasoningSummaryDelta),
}

/// Incremental API-visible reasoning-summary text.
#[derive(Clone, Debug, Deserialize)]
pub struct ReasoningSummaryDelta {
    /// Logical model-call index.
    pub model_call_index: u32,
    /// Newly received summary text.
    pub text: String,
}

/// Lifecycle state for one accepted agent turn.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum RunEvent {
    /// The driver started executing an accepted turn.
    Started(RunStarted),
    /// Steering input entered the active turn FIFO.
    Steered(RunSteered),
    /// A human-readable error accompanied a failed terminal event.
    Error(RunError),
    /// The turn completed successfully.
    Completed(Box<RunTerminal>),
    /// The turn failed or was cancelled.
    Failed(Box<RunTerminal>),
}

/// Configuration captured when an accepted turn begins.
#[derive(Clone, Debug, Deserialize)]
pub struct RunStarted {
    /// Runtime mode.
    pub mode: String,
    /// Fixed model contract.
    pub model: String,
    /// Reasoning execution mode.
    pub reasoning_mode: String,
    /// Thinking effort.
    pub effort: String,
    /// Responses transport.
    pub transport: String,
    /// Agent orchestration policy.
    pub orchestration: String,
    /// Sanitized Responses endpoint.
    pub websocket_url: String,
    /// Agent workspace when one is configured.
    pub workspace: Option<String>,
    /// Bytes in the accepted user instruction.
    pub instruction_bytes: usize,
}

/// A steering input accepted by the active turn.
#[derive(Clone, Copy, Debug, Deserialize)]
pub struct RunSteered {
    /// One-based steering index within this turn.
    pub steer_index: u32,
    /// Bytes in the steering instruction.
    pub instruction_bytes: usize,
}

/// Human-readable error detail emitted before a failed terminal event.
#[derive(Clone, Debug, Deserialize)]
pub struct RunError {
    /// Complete error message.
    pub message: String,
}

/// Terminal status of one accepted turn.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum RunStatus {
    /// The turn completed successfully.
    Completed,
    /// The turn was explicitly cancelled.
    Cancelled,
    /// The turn failed.
    Failed,
}

/// Exact aggregate token counts for a family of Responses operations.
#[derive(Clone, Copy, Debug, Default, Deserialize)]
pub struct EventUsage {
    /// Input tokens.
    pub input_tokens: u64,
    /// Input tokens served from cache.
    pub cached_input_tokens: u64,
    /// Input tokens written to cache.
    pub cache_write_input_tokens: u64,
    /// Output tokens, including reasoning.
    pub output_tokens: u64,
    /// Reasoning subset of output tokens.
    pub reasoning_output_tokens: u64,
    /// Provider-reported total tokens.
    pub total_tokens: u64,
}

/// Runtime and transport measurements accumulated by one agent turn.
#[derive(Clone, Debug, Default, Deserialize)]
pub struct RunMetrics {
    /// Logical model calls.
    pub model_calls: u32,
    /// Accepted steering inputs.
    pub steers: u32,
    /// Completed context compactions.
    pub compactions: u32,
    /// Tool calls.
    pub tool_calls: u32,
    /// Physical connection attempts.
    pub connection_attempts: u32,
    /// Successful WebSocket replacements.
    pub websocket_reconnects: u32,
    /// Physical Responses attempts.
    pub response_attempts: u32,
    /// Retried Responses attempts.
    pub response_retries: u32,
    /// Nanoseconds spent establishing connections.
    pub connection_duration_ns: u64,
    /// Nanoseconds spent in SDK retry backoff.
    pub retry_backoff_duration_ns: u64,
    /// Nanoseconds spent awaiting logical model work.
    pub model_duration_ns: u64,
    /// Subset of model time spent awaiting context compaction.
    pub compaction_duration_ns: u64,
    /// Nanoseconds spent warming the persistent connection.
    pub warmup_duration_ns: u64,
    /// Sum of top-level tool-handler execution durations after scheduler admission.
    pub tool_work_duration_ns: u64,
    /// Union of wall-clock intervals spent executing top-level tool batches.
    pub tool_wall_duration_ns: u64,
    /// Token usage from generation and compaction operations.
    pub usage: EventUsage,
    /// Token usage from connection warmup.
    pub warmup_usage: EventUsage,
}

/// Complete terminal projection for one accepted turn.
#[derive(Clone, Debug, Deserialize)]
pub struct RunTerminal {
    /// Terminal outcome.
    pub status: RunStatus,
    /// Fixed model contract.
    pub model: String,
    /// Reasoning execution mode.
    pub reasoning_mode: String,
    /// Thinking effort.
    pub effort: String,
    /// Responses transport.
    pub transport: String,
    /// Agent orchestration policy.
    pub orchestration: String,
    /// Whole-millisecond turn duration.
    pub duration_ms: u64,
    /// Nanosecond turn duration.
    pub duration_ns: u64,
    /// Runtime, transport, and token measurements.
    #[serde(flatten)]
    pub metrics: RunMetrics,
    /// Exact estimate using the built-in model and service-tier rates.
    pub estimated_cost: Option<EstimatedUsdCost>,
    /// Floating-point compatibility projection for existing JSONL consumers.
    pub cost_usd: Option<f64>,
    /// Why an exact local estimate is present or unavailable.
    #[serde(default)]
    pub cost_status: CostStatus,
}

/// Lifecycle state for one model-requested tool call.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ToolEvent {
    /// A tool invocation began.
    Call(ToolCall),
    /// A tool invocation finished.
    Result(ToolResultEvent),
}

/// One model-requested tool invocation.
#[derive(Clone, Debug, Deserialize)]
pub struct ToolCall {
    /// Stable provider tool-call identity.
    pub call_id: String,
    /// Canonical registry tool name.
    pub tool: String,
    /// Exact JSON arguments or a JSON string containing freeform input.
    pub arguments: Box<RawValue>,
    /// Logical model-call index that requested the tool.
    pub model_call_index: u32,
}

impl ToolCall {
    /// Decodes function arguments into an application-selected type.
    ///
    /// # Errors
    ///
    /// Returns an error when the retained argument JSON does not match `T`.
    pub fn decode_arguments<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
        serde_json::from_str(self.arguments.get())
    }
}

/// Outcome status of a tool invocation.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ToolStatus {
    /// The tool completed successfully.
    Completed,
    /// The tool returned or raised a failure.
    Failed,
    /// Cancellation interrupted the tool.
    Cancelled,
}

/// Completed model-visible tool output.
#[derive(Clone, Debug, Deserialize)]
pub struct ToolResultEvent {
    /// Stable provider tool-call identity.
    pub call_id: String,
    /// Canonical registry tool name.
    pub tool: String,
    /// Invocation outcome.
    pub status: ToolStatus,
    /// Wall-clock execution duration.
    pub duration_ns: u64,
    /// Offset from the containing Code Mode cell start for nested calls.
    pub started_after_ns: Option<u64>,
    /// Complete model-visible output.
    pub result: ToolOutputBody,
    /// Optional application or remote-tool metadata.
    pub metadata: Option<Box<RawValue>>,
}

/// Lifecycle state for one logical model operation.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ModelEvent {
    /// Persistent-connection warmup began.
    WarmupStarted(ModelWarmupStarted),
    /// Persistent-connection warmup completed.
    WarmupCompleted(ModelWarmupCompleted),
    /// Persistent-connection warmup failed.
    WarmupFailed(ModelWarmupFailed),
    /// A logical `response.create` call began.
    CallStarted(ModelCallStarted),
    /// A logical `response.create` call completed.
    CallCompleted(ModelCallCompleted),
    /// A logical `response.create` call failed.
    CallFailed(ModelCallFailed),
}

/// Persistent-connection warmup input.
#[derive(Clone, Debug, Deserialize)]
pub struct ModelWarmupStarted {
    /// Fixed model contract.
    pub model: String,
    /// Stable prompt-cache identity.
    pub prompt_cache_key: String,
}

/// Completed persistent-connection warmup.
#[derive(Clone, Debug, Deserialize)]
pub struct ModelWarmupCompleted {
    /// Whether work came from a new response or a shared prefix.
    pub source: String,
    /// Physical attempt when a response was sent.
    pub attempt: Option<u32>,
    /// Connection generation when a response was sent.
    pub connection_generation: Option<u32>,
    /// Warmup duration.
    pub duration_ns: u64,
    /// Provider token usage when supplied.
    pub usage: Option<Usage>,
}

/// Failed persistent-connection warmup.
#[derive(Clone, Debug, Deserialize)]
pub struct ModelWarmupFailed {
    /// Warmup duration.
    pub duration_ns: u64,
    /// Complete failure message.
    pub error: String,
}

/// Input metadata for one logical `response.create` call.
#[derive(Clone, Debug, Deserialize)]
pub struct ModelCallStarted {
    /// One-based model-call index within the agent turn.
    pub call_index: u32,
    /// Fixed model contract.
    pub model: String,
    /// Reasoning execution mode.
    pub reasoning_mode: String,
    /// Thinking effort.
    pub effort: String,
}

/// Completed logical `response.create` call.
#[derive(Clone, Debug, Deserialize)]
pub struct ModelCallCompleted {
    /// One-based model-call index within the agent turn.
    pub call_index: u32,
    /// Fixed model contract.
    pub model: String,
    /// Successful physical attempt number.
    pub attempt: u32,
    /// Connection generation used by the attempt.
    pub connection_generation: u32,
    /// Provider terminal status.
    pub status: String,
    /// Whole-call duration.
    pub duration_ns: u64,
    /// Time from attempt start to the first provider event.
    pub time_to_first_event_ns: u64,
    /// Time from attempt start to first model output.
    pub time_to_first_output_ns: Option<u64>,
    /// Completed callable output count.
    pub tool_calls: usize,
    /// Provider token usage when supplied.
    pub usage: Option<Usage>,
}

/// Failed logical `response.create` call.
#[derive(Clone, Debug, Deserialize)]
pub struct ModelCallFailed {
    /// One-based model-call index within the agent turn.
    pub call_index: u32,
    /// Fixed model contract.
    pub model: String,
    /// Whole-call duration.
    pub duration_ns: u64,
    /// Complete failure message.
    pub error: String,
}

/// Context-management lifecycle state.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ContextEvent {
    /// Automatic remote compaction began.
    CompactionStarted(CompactionStarted),
    /// Automatic remote compaction completed.
    CompactionCompleted(CompactionCompleted),
    /// Automatic remote compaction failed.
    CompactionFailed(CompactionFailed),
}

/// Input metadata for automatic context compaction.
#[derive(Clone, Debug, Deserialize)]
pub struct CompactionStarted {
    /// Model-call boundary after which compaction ran.
    pub after_model_call_index: u32,
    /// Estimated active context before compaction.
    pub active_context_tokens: u64,
    /// Configured automatic-compaction threshold.
    pub auto_compact_token_limit: u64,
}

/// Completed automatic context compaction.
#[derive(Clone, Debug, Deserialize)]
pub struct CompactionCompleted {
    /// Model-call boundary after which compaction ran.
    pub after_model_call_index: u32,
    /// Successful physical attempt number.
    pub attempt: u32,
    /// Connection generation used by the attempt.
    pub connection_generation: u32,
    /// Provider terminal status.
    pub status: String,
    /// Whole-compaction duration.
    pub duration_ns: u64,
    /// Time from attempt start to the first provider event.
    pub time_to_first_event_ns: u64,
    /// Time from attempt start to first compaction output.
    pub time_to_first_output_ns: Option<u64>,
    /// Provider token usage when supplied.
    pub usage: Option<Usage>,
}

/// Failed automatic context compaction.
#[derive(Clone, Debug, Deserialize)]
pub struct CompactionFailed {
    /// Model-call boundary after which compaction ran.
    pub after_model_call_index: u32,
    /// Whole-compaction duration.
    pub duration_ns: u64,
    /// Complete failure message.
    pub error: String,
}

/// Exact lower-level retry or connection diagnostic retained by the firehose.
///
/// These mechanics intentionally remain outside the stable agent-domain
/// projection. Use [`Self::decode_payload`] for an application-specific
/// diagnostic view, or serialize the containing [`crate::events::AgentEvent`]
/// unchanged.
#[derive(Clone, Debug)]
pub struct TransportEvent {
    kind: AgentEventKind,
    payload: Arc<RawValue>,
}

impl TransportEvent {
    pub(crate) const fn new(kind: AgentEventKind, payload: Arc<RawValue>) -> Self {
        Self { kind, payload }
    }

    /// Returns the exact diagnostic event category.
    #[must_use]
    pub const fn kind(&self) -> AgentEventKind {
        self.kind
    }

    /// Returns the exact retained diagnostic payload.
    #[must_use]
    pub fn raw_payload(&self) -> &RawValue {
        &self.payload
    }

    /// Decodes the diagnostic payload into an application-selected type.
    ///
    /// # Errors
    ///
    /// Returns an error when the retained payload does not match `T`.
    pub fn decode_payload<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
        serde_json::from_str(self.payload.get())
    }
}