agent-runtime-http-api 0.1.1

Internal HTTP API contract for Agent Runtime
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
//! Public Open API contract for Runtime executions.
//!
//! This crate contains transport-neutral DTOs only. Axum, persistence, Agent
//! Infra, and execution behavior are deliberately excluded.

mod claude;
mod openai;
mod task;

pub use claude::*;
pub use openai::*;
pub use task::*;

use std::collections::BTreeMap;

use runtime_types::{ConversationId, ExecutionId, OperationId, RuntimeInstanceId, WorkspaceId};
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Versioned OpenAPI document served by the Runtime Outer Shell. Keeping the
/// snapshot in the contract crate makes protocol review independent of Axum.
pub fn openapi_document() -> Value {
    serde_json::from_str(include_str!("../openapi/runtime-v3.json"))
        .expect("embedded Runtime OpenAPI must be valid JSON")
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CreateExecutionRequest {
    pub runtime_instance_id: RuntimeInstanceId,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub conversation_id: Option<ConversationId>,
    pub input: RuntimeInput,
    /// Selects the workspace for this execution. When omitted, the Runtime
    /// binding's default workspace is used. Agent Infra delegation remains the
    /// authorization boundary; this field only selects a scoped resource.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub workspace_id: Option<WorkspaceId>,
    /// Optional downstream model override for the native Task API.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Request-scoped instructions augment the immutable Agent Definition.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,
    /// Bounded string metadata is safe to correlate through traces.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub metadata: BTreeMap<String, String>,
    /// Provider-neutral model parameters shared by the native Task API and
    /// the OpenAI/Claude wire adapters. Unsupported provider-only fields are
    /// rejected at ingress instead of being silently discarded.
    #[serde(default)]
    pub generation: ModelGenerationOptions,
    #[serde(default)]
    pub options: ExecutionOptions,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ModelGenerationOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_output_tokens: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stop_sequences: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub response_format: Option<Value>,
    #[serde(default)]
    pub tool_choice: ModelToolChoice,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
pub enum ModelToolChoice {
    #[default]
    Auto,
    None,
}

impl ModelGenerationOptions {
    pub fn validate(&self) -> Result<(), &'static str> {
        if self.max_output_tokens.is_some_and(|value| value == 0) {
            return Err("maxOutputTokens must be greater than zero");
        }
        if self
            .temperature
            .is_some_and(|value| !value.is_finite() || !(0.0..=2.0).contains(&value))
        {
            return Err("temperature must be finite and within 0..=2");
        }
        if self.stop_sequences.len() > 4
            || self
                .stop_sequences
                .iter()
                .any(|value| value.is_empty() || value.len() > 1024)
        {
            return Err("stopSequences supports at most 4 non-empty values up to 1024 bytes");
        }
        if self
            .response_format
            .as_ref()
            .is_some_and(|value| !value.is_object())
        {
            return Err("responseFormat must be a JSON object");
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(
    tag = "type",
    rename_all = "snake_case",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum RuntimeInput {
    UserMessage { text: String },
    Messages { messages: Vec<RuntimeMessage> },
    ToolApproval { request_id: String, approved: bool },
    ElicitationResponse { request_id: String, text: String },
}

impl RuntimeInput {
    pub fn user_text(&self) -> Option<&str> {
        match self {
            Self::UserMessage { text } => Some(text),
            Self::Messages { messages } => messages
                .iter()
                .rev()
                .find(|message| message.role == RuntimeMessageRole::User)
                .or_else(|| messages.last())
                .map(|message| message.text.as_str()),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct RuntimeMessage {
    pub role: RuntimeMessageRole,
    pub text: String,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RuntimeMessageRole {
    System,
    Developer,
    User,
    Assistant,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExecutionOptions {
    #[serde(default = "default_deadline_seconds")]
    pub deadline_seconds: u64,
    #[serde(default = "default_model_turns")]
    pub max_model_turns: usize,
    #[serde(default = "default_tool_calls")]
    pub max_tool_calls: usize,
}

impl Default for ExecutionOptions {
    fn default() -> Self {
        Self {
            deadline_seconds: default_deadline_seconds(),
            max_model_turns: default_model_turns(),
            max_tool_calls: default_tool_calls(),
        }
    }
}

impl ExecutionOptions {
    pub fn validate(&self) -> Result<(), &'static str> {
        // Long-running Tasks remain bounded. Crash recovery still depends on
        // the host selecting a durable ExecutionJournal implementation.
        if !(1..=604_800).contains(&self.deadline_seconds) {
            return Err("deadlineSeconds must be within 1..=604800");
        }
        if !(1..=256).contains(&self.max_model_turns) {
            return Err("maxModelTurns must be within 1..=256");
        }
        if !(1..=2048).contains(&self.max_tool_calls) {
            return Err("maxToolCalls must be within 1..=2048");
        }
        Ok(())
    }
}

fn default_deadline_seconds() -> u64 {
    900
}
fn default_model_turns() -> usize {
    64
}
fn default_tool_calls() -> usize {
    256
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CreateExecutionResponse {
    pub execution: ExecutionView,
    pub replayed: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExecutionView {
    pub id: ExecutionId,
    pub runtime_instance_id: RuntimeInstanceId,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub conversation_id: Option<ConversationId>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub workspace_id: Option<WorkspaceId>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub metadata: BTreeMap<String, String>,
    pub state: ExecutionState,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub outcome: Option<ExecutionOutcome>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub failure: Option<ExecutionFailure>,
    pub created_at_ms: i64,
    pub updated_at_ms: i64,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ExecutionState {
    Queued,
    Running,
    WaitingForInput,
    Finalizing,
    Completed,
    Failed,
    Canceled,
}

impl ExecutionState {
    pub fn is_terminal(self) -> bool {
        matches!(self, Self::Completed | Self::Failed | Self::Canceled)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExecutionOutcome {
    pub answer: String,
    pub model_turns: usize,
    pub tool_calls: usize,
    /// `None` means the provider omitted usage; it is not equivalent to zero.
    pub input_tokens: Option<u64>,
    pub output_tokens: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExecutionFailure {
    pub code: String,
    pub message: String,
    pub retryable: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SubmitInputRequest {
    pub operation_id: OperationId,
    pub input: RuntimeInput,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExecutionEvent {
    pub execution_id: ExecutionId,
    pub sequence: u64,
    pub created_at_ms: i64,
    pub payload: EventPayload,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", content = "data", rename_all = "snake_case")]
pub enum EventPayload {
    ExecutionQueued,
    ExecutionStarted,
    ModelStarted {
        turn: usize,
        invocation_id: String,
    },
    ModelCompleted {
        turn: usize,
        invocation_id: String,
        finish_reason: String,
        input_tokens: Option<u64>,
        output_tokens: Option<u64>,
    },
    ToolStarted {
        call_id: String,
        name: String,
    },
    ToolCompleted {
        call_id: String,
        name: String,
        failed: bool,
    },
    InteractionRequired {
        request_id: String,
        prompt: String,
    },
    InteractionReceived {
        request_id: String,
    },
    Warning {
        code: String,
        message: String,
    },
    ExecutionCompleted {
        answer: String,
    },
    ExecutionFailed {
        code: String,
        message: String,
    },
    ExecutionCanceled,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct EventPage {
    pub items: Vec<ExecutionEvent>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub next_after: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ApiErrorBody {
    pub code: String,
    pub message: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub details: Option<Value>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn input_is_strict_and_tagged() {
        let input: RuntimeInput = serde_json::from_value(serde_json::json!({
            "type": "user_message", "text": "hello"
        }))
        .unwrap();
        assert_eq!(input.user_text(), Some("hello"));
        assert!(
            serde_json::from_value::<RuntimeInput>(serde_json::json!({
                "type":"user_message", "text":"hello", "secret":"no"
            }))
            .is_err()
        );
        let approval: RuntimeInput = serde_json::from_value(serde_json::json!({
            "type":"tool_approval", "requestId":"call-1", "approved":true
        }))
        .unwrap();
        assert!(matches!(approval, RuntimeInput::ToolApproval { .. }));
        assert!(
            serde_json::from_value::<RuntimeInput>(serde_json::json!({
                "type":"tool_approval", "request_id":"call-1", "approved":true
            }))
            .is_err()
        );
    }

    #[test]
    fn limits_are_bounded() {
        let mut options = ExecutionOptions::default();
        assert!(options.validate().is_ok());
        options.max_tool_calls = usize::MAX;
        assert!(options.validate().is_err());
    }

    #[test]
    fn openapi_snapshot_covers_the_public_execution_surface() {
        let document = openapi_document();
        assert_eq!(document["openapi"], "3.1.0");
        for path in [
            "/v1/agent/tasks",
            "/v1/agent/tasks/{taskId}/stream",
            "/v1/agent/tasks/{taskId}/ws",
            "/v1/responses",
            "/v1/messages",
        ] {
            assert!(document["paths"].get(path).is_some(), "missing {path}");
        }
    }

    #[test]
    fn native_task_preserves_workspace_model_and_long_task_limits() {
        let request: CreateAgentTaskRequest = serde_json::from_value(serde_json::json!({
            "runtimeId": "runtime-1",
            "input": {"text": "inspect it"},
            "workspace": {"id": "workspace-2"},
            "model": {"id": "model-2", "maxOutputTokens": 4096},
            "limits": {"deadlineSeconds": 86400, "maxModelTurns": 8, "maxToolCalls": 32},
            "metadata": {"traceId": "trace-1"}
        }))
        .unwrap();
        let execution = request.into_execution();
        assert_eq!(execution.workspace_id.unwrap().as_str(), "workspace-2");
        assert_eq!(execution.model.as_deref(), Some("model-2"));
        assert_eq!(execution.generation.max_output_tokens, Some(4096));
        assert_eq!(execution.options.deadline_seconds, 86400);
        assert!(execution.options.validate().is_ok());
    }

    #[test]
    fn provider_contracts_accept_their_native_parameter_names() {
        let openai: OpenAiResponseRequest = serde_json::from_value(serde_json::json!({
            "model": "runtime-1",
            "input": "hello",
            "max_output_tokens": 512,
            "background": true,
            "agent": {"workspaceId": "workspace-2"}
        }))
        .unwrap();
        assert!(openai.background);
        assert_eq!(openai.max_output_tokens, Some(512));

        let claude: ClaudeMessageRequest = serde_json::from_value(serde_json::json!({
            "model": "runtime-1",
            "max_tokens": 512,
            "messages": [{"role": "user", "content": "hello"}],
            "agent": {"workspaceId": "workspace-2"}
        }))
        .unwrap();
        assert_eq!(claude.max_tokens, 512);
        assert_eq!(claude.messages[0].role, "user");
    }
}