nenjo-tool-api 0.31.0

Shared tool API contracts for Nenjo agents, providers, and runtimes
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! Shared contracts for model-visible async operation tools.
//!
//! Runtime crates own operation scheduling, cancellation, polling, and event
//! delivery. This module only defines stable tool names, argument DTOs, JSON
//! schemas, and operation lifecycle enums.

use serde::{Deserialize, Deserializer, Serialize};
use serde_json::json;

pub const INSPECT_TOOL_NAME: &str = "inspect";
pub const SEND_INPUT_TOOL_NAME: &str = "send_input";
pub const STOP_TOOL_NAME: &str = "stop";
pub const WAIT_TOOL_NAME: &str = "wait";

/// A model-facing action supported by an async operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AsyncControl {
    Inspect,
    SendInput,
    Stop,
    Wait,
}

/// Compact set of controls declared when an async operation starts.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct AsyncControls(u8);

impl AsyncControls {
    pub const NONE: Self = Self(0);

    pub const fn new(control: AsyncControl) -> Self {
        Self(control.mask())
    }

    pub const fn with(self, control: AsyncControl) -> Self {
        Self(self.0 | control.mask())
    }

    pub const fn contains(self, control: AsyncControl) -> bool {
        self.0 & control.mask() != 0
    }

    pub fn iter(self) -> impl Iterator<Item = AsyncControl> {
        [
            AsyncControl::Inspect,
            AsyncControl::SendInput,
            AsyncControl::Stop,
            AsyncControl::Wait,
        ]
        .into_iter()
        .filter(move |control| self.contains(*control))
    }
}

impl AsyncControl {
    pub const fn tool_name(self) -> &'static str {
        match self {
            Self::Inspect => INSPECT_TOOL_NAME,
            Self::SendInput => SEND_INPUT_TOOL_NAME,
            Self::Stop => STOP_TOOL_NAME,
            Self::Wait => WAIT_TOOL_NAME,
        }
    }

    const fn mask(self) -> u8 {
        match self {
            Self::Inspect => 1 << 0,
            Self::SendInput => 1 << 1,
            Self::Stop => 1 << 2,
            Self::Wait => 1 << 3,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AsyncOperationKind {
    Ability,
    Delegation,
    SubAgent,
    Shell,
    Media,
    TaskExecution,
}

impl AsyncOperationKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Ability => "ability",
            Self::Delegation => "delegation",
            Self::SubAgent => "sub_agent",
            Self::Shell => "shell",
            Self::Media => "media",
            Self::TaskExecution => "task_execution",
        }
    }
}

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

impl AsyncOperationStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Running => "running",
            Self::WaitingForInput => "waiting_for_input",
            Self::Completed => "completed",
            Self::Failed => "failed",
            Self::Stopped => "stopped",
        }
    }

    pub fn can_receive_input(self) -> bool {
        matches!(self, Self::Running | Self::WaitingForInput)
    }
}

/// Canonical model-facing receipt returned after an async operation starts.
#[derive(Debug, Clone, Serialize)]
pub struct AsyncOperationStartReceipt {
    operation_id: String,
    kind: AsyncOperationKind,
    status: AsyncOperationStatus,
    control_tools: Vec<AsyncControlGuide>,
}

#[derive(Debug, Clone, Serialize)]
struct AsyncControlGuide {
    control: AsyncControl,
    tool: &'static str,
    instruction: &'static str,
    suggested_arguments: serde_json::Value,
}

impl AsyncOperationStartReceipt {
    pub fn new(
        operation_id: impl Into<String>,
        kind: AsyncOperationKind,
        controls: AsyncControls,
    ) -> Self {
        let operation_id = operation_id.into();
        let control_tools = controls
            .iter()
            .map(|control| AsyncControlGuide::new(control, &operation_id, kind))
            .collect();
        Self {
            operation_id,
            kind,
            status: AsyncOperationStatus::Running,
            control_tools,
        }
    }
}

impl AsyncControlGuide {
    fn new(control: AsyncControl, operation_id: &str, kind: AsyncOperationKind) -> Self {
        let (instruction, suggested_arguments) = match control {
            AsyncControl::Inspect => (
                "Use inspect to check this operation's current state or final output. Set include_transcript=true when recent activity is needed. A nested tool error while status is running is recoverable; use wait rather than stopping and restarting the operation.",
                json!({"operations": [operation_id]}),
            ),
            AsyncControl::SendInput => (
                "Use send_input only when this operation asks the parent agent for input.",
                json!({
                    "operations": [operation_id],
                    "message": "<response to the operation>"
                }),
            ),
            AsyncControl::Stop => (
                "Use stop to cancel this operation when it should no longer continue. Do not stop solely because a nested tool failed while the operation remains running.",
                json!({"operations": [operation_id]}),
            ),
            AsyncControl::Wait => (
                "Use wait while this operation is running; repeat until it completes, fails, stops, or asks for input. A recoverable_tool_error means the operation is still running and the default next action is wait.",
                json!({"seconds": 10, "kind": kind}),
            ),
        };
        Self {
            control,
            tool: control.tool_name(),
            instruction,
            suggested_arguments,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AsyncOperationSignalKind {
    Started,
    Progress,
    RecoverableToolError,
    NeedsInput,
    Completed,
    Failed,
    Stopped,
}

impl AsyncOperationSignalKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Started => "started",
            Self::Progress => "progress",
            Self::RecoverableToolError => "recoverable_tool_error",
            Self::NeedsInput => "needs_input",
            Self::Completed => "completed",
            Self::Failed => "failed",
            Self::Stopped => "stopped",
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct InspectOperationsArgs {
    #[serde(default, deserialize_with = "deserialize_operation_ids")]
    pub operations: Vec<String>,
    #[serde(default)]
    pub kind: Option<AsyncOperationKind>,
    #[serde(default, deserialize_with = "deserialize_bool_or_string")]
    pub include_transcript: bool,
    #[serde(
        default = "default_inspect_limit",
        deserialize_with = "deserialize_usize_from_json_number"
    )]
    pub limit: usize,
}

#[derive(Debug, Clone, Deserialize)]
pub struct StopOperationsArgs {
    #[serde(default, deserialize_with = "deserialize_operation_ids")]
    pub operations: Vec<String>,
    #[serde(default)]
    pub kind: Option<AsyncOperationKind>,
    pub reason: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct WaitOperationsArgs {
    #[serde(
        default = "default_wait_seconds",
        deserialize_with = "deserialize_u64_from_json_number"
    )]
    pub seconds: u64,
    #[serde(default)]
    pub kind: Option<AsyncOperationKind>,
    pub reason: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct SendOperationInputArgs {
    #[serde(default, deserialize_with = "deserialize_operation_ids")]
    pub operations: Vec<String>,
    pub message: String,
}

pub fn inspect_operations_parameters_schema() -> serde_json::Value {
    json!({
        "type": "object",
        "properties": {
            "operations": {"type": "array", "items": {"type": "string"}},
            "kind": operation_kind_schema(),
            "include_transcript": {"type": "boolean"},
            "limit": {"type": "integer", "minimum": 1, "maximum": 50}
        },
        "additionalProperties": false
    })
}

pub fn stop_operations_parameters_schema() -> serde_json::Value {
    json!({
        "type": "object",
        "properties": {
            "operations": {"type": "array", "items": {"type": "string"}},
            "kind": operation_kind_schema(),
            "reason": {"type": "string"}
        },
        "additionalProperties": false
    })
}

pub fn wait_operations_parameters_schema() -> serde_json::Value {
    json!({
        "type": "object",
        "properties": {
            "seconds": {"type": "integer", "minimum": 1, "maximum": 30},
            "kind": operation_kind_schema(),
            "reason": {"type": "string"}
        },
        "additionalProperties": false
    })
}

pub fn send_operation_input_parameters_schema() -> serde_json::Value {
    json!({
        "type": "object",
        "properties": {
            "operations": {"type": "array", "items": {"type": "string"}},
            "message": {"type": "string"}
        },
        "required": ["operations", "message"],
        "additionalProperties": false
    })
}

pub fn operation_kind_schema() -> serde_json::Value {
    json!({
        "type": "string",
        "enum": ["ability", "delegation", "sub_agent", "shell", "media", "task_execution"]
    })
}

fn default_inspect_limit() -> usize {
    30
}

fn deserialize_operation_ids<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
    D: Deserializer<'de>,
{
    let value = serde_json::Value::deserialize(deserializer)?;
    let array = match value {
        serde_json::Value::Array(values) => serde_json::Value::Array(values),
        serde_json::Value::String(encoded) => {
            let decoded = serde_json::from_str::<serde_json::Value>(&encoded).map_err(|_| {
                serde::de::Error::custom(
                    "expected an array of operation ids or a JSON-encoded array",
                )
            })?;
            if !decoded.is_array() {
                return Err(serde::de::Error::custom(
                    "expected an array of operation ids or a JSON-encoded array",
                ));
            }
            decoded
        }
        _ => {
            return Err(serde::de::Error::custom(
                "expected an array of operation ids or a JSON-encoded array",
            ));
        }
    };

    serde_json::from_value(array).map_err(serde::de::Error::custom)
}

fn deserialize_bool_or_string<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
    D: Deserializer<'de>,
{
    match serde_json::Value::deserialize(deserializer)? {
        serde_json::Value::Bool(value) => Ok(value),
        serde_json::Value::String(value) if value == "true" => Ok(true),
        serde_json::Value::String(value) if value == "false" => Ok(false),
        _ => Err(serde::de::Error::custom(
            "expected a boolean or the string \"true\" or \"false\"",
        )),
    }
}

pub fn deserialize_usize_from_json_number<'de, D>(deserializer: D) -> Result<usize, D::Error>
where
    D: Deserializer<'de>,
{
    let value = serde_json::Value::deserialize(deserializer)?;
    let number = deserialize_non_negative_whole_number(value)?;
    usize::try_from(number).map_err(serde::de::Error::custom)
}

pub fn deserialize_u64_from_json_number<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
    D: Deserializer<'de>,
{
    let value = serde_json::Value::deserialize(deserializer)?;
    deserialize_non_negative_whole_number(value)
}

fn deserialize_non_negative_whole_number<E>(value: serde_json::Value) -> Result<u64, E>
where
    E: serde::de::Error,
{
    match value {
        serde_json::Value::Number(number) => {
            if let Some(raw) = number.as_u64() {
                Ok(raw)
            } else if let Some(raw) = number.as_f64() {
                // `u64::MAX as f64` rounds up to 2^64. A strict comparison is
                // therefore required or the first out-of-range float silently
                // saturates to `u64::MAX` during the cast.
                if raw.is_finite() && raw.fract() == 0.0 && raw >= 0.0 && raw < u64::MAX as f64 {
                    Ok(raw as u64)
                } else {
                    Err(E::custom("expected a non-negative whole number"))
                }
            } else {
                Err(E::custom("expected a non-negative whole number"))
            }
        }
        serde_json::Value::String(raw)
            if !raw.is_empty() && raw.bytes().all(|byte| byte.is_ascii_digit()) =>
        {
            raw.parse::<u64>().map_err(E::custom)
        }
        other => Err(E::custom(format!(
            "expected a non-negative whole number, got {other}"
        ))),
    }
}

fn default_wait_seconds() -> u64 {
    10
}

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

    #[test]
    fn async_controls_track_explicit_capabilities() {
        let controls = AsyncControls::new(AsyncControl::Inspect).with(AsyncControl::Wait);

        assert!(controls.contains(AsyncControl::Inspect));
        assert!(controls.contains(AsyncControl::Wait));
        assert!(!controls.contains(AsyncControl::SendInput));
        assert!(!controls.contains(AsyncControl::Stop));
    }

    #[test]
    fn recoverable_tool_error_has_a_stable_wire_name() {
        assert_eq!(
            AsyncOperationSignalKind::RecoverableToolError.as_str(),
            "recoverable_tool_error"
        );
    }

    #[test]
    fn start_receipt_guides_recovery_without_restarting() {
        let receipt = AsyncOperationStartReceipt::new(
            "ability_build_routine_1",
            AsyncOperationKind::Ability,
            AsyncControls::new(AsyncControl::Inspect)
                .with(AsyncControl::Stop)
                .with(AsyncControl::Wait),
        );
        let value = serde_json::to_value(receipt).unwrap();
        let instructions = value["control_tools"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|control| control["instruction"].as_str())
            .collect::<Vec<_>>()
            .join(" ");

        assert!(instructions.contains("recoverable"));
        assert!(instructions.contains("Do not stop solely"));
        assert!(instructions.contains("default next action is wait"));
    }

    #[test]
    fn start_receipt_instructs_only_supported_controls() {
        let receipt = AsyncOperationStartReceipt::new(
            "media_generate_video_1",
            AsyncOperationKind::Media,
            AsyncControls::new(AsyncControl::Inspect).with(AsyncControl::Wait),
        );
        let value = serde_json::to_value(receipt).unwrap();

        assert_eq!(value["operation_id"], "media_generate_video_1");
        assert_eq!(value["kind"], "media");
        assert_eq!(value["status"], "running");
        assert_eq!(value["control_tools"].as_array().unwrap().len(), 2);
        assert_eq!(value["control_tools"][0]["tool"], "inspect");
        assert_eq!(
            value["control_tools"][0]["suggested_arguments"]["operations"][0],
            "media_generate_video_1"
        );
        assert_eq!(value["control_tools"][1]["tool"], "wait");
        assert_eq!(
            value["control_tools"][1]["suggested_arguments"]["kind"],
            "media"
        );
    }

    #[test]
    fn async_operation_kind_uses_wire_names() {
        assert_eq!(AsyncOperationKind::Ability.as_str(), "ability");
        assert_eq!(AsyncOperationKind::Delegation.as_str(), "delegation");
        assert_eq!(AsyncOperationKind::SubAgent.as_str(), "sub_agent");
        assert_eq!(AsyncOperationKind::Shell.as_str(), "shell");
        assert_eq!(AsyncOperationKind::Media.as_str(), "media");
        assert_eq!(AsyncOperationKind::TaskExecution.as_str(), "task_execution");
    }

    #[test]
    fn wait_args_deserialize_with_defaults() {
        let args: WaitOperationsArgs = serde_json::from_value(json!({})).unwrap();

        assert_eq!(args.seconds, 10);
        assert_eq!(args.kind, None);
    }

    #[test]
    fn wait_args_accept_whole_float_seconds_from_model_args() {
        let args: WaitOperationsArgs = serde_json::from_value(json!({
            "kind": "ability",
            "seconds": 30.0
        }))
        .unwrap();

        assert_eq!(args.seconds, 30);
        assert_eq!(args.kind, Some(AsyncOperationKind::Ability));
    }

    #[test]
    fn wait_args_accept_digit_string_seconds_from_model_args() {
        let args: WaitOperationsArgs = serde_json::from_value(json!({
            "kind": "ability",
            "seconds": "10"
        }))
        .unwrap();

        assert_eq!(args.seconds, 10);
        assert_eq!(args.kind, Some(AsyncOperationKind::Ability));
    }

    #[test]
    fn wait_args_reject_fractional_seconds() {
        let err = serde_json::from_value::<WaitOperationsArgs>(json!({
            "seconds": 5.5
        }))
        .unwrap_err();

        assert!(err.to_string().contains("whole number"));
    }

    #[test]
    fn numeric_coercion_accepts_u64_max_without_rounding() {
        #[derive(Deserialize)]
        struct Args {
            #[serde(deserialize_with = "deserialize_u64_from_json_number")]
            value: u64,
        }

        let integer: Args = serde_json::from_str(r#"{"value":18446744073709551615}"#).unwrap();
        let string: Args = serde_json::from_value(json!({
            "value": "18446744073709551615"
        }))
        .unwrap();

        assert_eq!(integer.value, u64::MAX);
        assert_eq!(string.value, u64::MAX);
    }

    #[test]
    fn numeric_coercion_rejects_values_at_or_above_two_to_the_64th() {
        #[derive(Debug, Deserialize)]
        struct Args {
            #[serde(deserialize_with = "deserialize_u64_from_json_number")]
            #[allow(dead_code)]
            value: u64,
        }

        let numeric =
            serde_json::from_str::<Args>(r#"{"value":1.8446744073709552e19}"#).unwrap_err();
        let string = serde_json::from_value::<Args>(json!({
            "value": "18446744073709551616"
        }))
        .unwrap_err();

        assert!(numeric.to_string().contains("whole number"));
        assert!(string.to_string().contains("number too large"));
    }

    #[test]
    fn inspect_args_accept_whole_float_limit_from_model_args() {
        let args: InspectOperationsArgs = serde_json::from_value(json!({
            "operations": ["ability_build_agent_2"],
            "include_transcript": true,
            "limit": 5.0
        }))
        .unwrap();

        assert_eq!(args.limit, 5);
    }

    #[test]
    fn inspect_args_accept_json_encoded_model_values() {
        let args: InspectOperationsArgs = serde_json::from_value(json!({
            "operations": "[\"ability_build_routine_1\"]",
            "include_transcript": "true",
            "limit": "5"
        }))
        .unwrap();

        assert_eq!(args.operations, ["ability_build_routine_1"]);
        assert!(args.include_transcript);
        assert_eq!(args.limit, 5);
    }

    #[test]
    fn operation_controls_share_json_encoded_operation_id_compatibility() {
        let stop: StopOperationsArgs = serde_json::from_value(json!({
            "operations": "[\"ability_build_routine_1\"]"
        }))
        .unwrap();
        let send: SendOperationInputArgs = serde_json::from_value(json!({
            "operations": "[\"ability_build_routine_1\"]",
            "message": "continue"
        }))
        .unwrap();

        assert_eq!(stop.operations, ["ability_build_routine_1"]);
        assert_eq!(send.operations, ["ability_build_routine_1"]);
    }

    #[test]
    fn inspect_args_reject_ambiguous_string_values() {
        let operations_err = serde_json::from_value::<InspectOperationsArgs>(json!({
            "operations": "ability_build_routine_1"
        }))
        .unwrap_err();
        let bool_err = serde_json::from_value::<InspectOperationsArgs>(json!({
            "include_transcript": "yes"
        }))
        .unwrap_err();

        assert!(operations_err.to_string().contains("JSON-encoded array"));
        assert!(bool_err.to_string().contains("expected a boolean"));
    }

    #[test]
    fn inspect_args_reject_fractional_limit() {
        let err = serde_json::from_value::<InspectOperationsArgs>(json!({
            "limit": 5.5
        }))
        .unwrap_err();

        assert!(err.to_string().contains("whole number"));
    }

    #[test]
    fn wait_args_reject_non_digit_string_seconds() {
        for seconds in ["5.5", "-1", "ten"] {
            let err = serde_json::from_value::<WaitOperationsArgs>(json!({
                "seconds": seconds
            }))
            .unwrap_err();

            assert!(err.to_string().contains("whole number"));
        }
    }
}