oino-extension-api 1.0.1

Contracts between oino-core and extension runtimes: instances, host services, manifests, permissions
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
//! `registry.wit`: event kinds and registration records.

use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

use crate::json::RawJson;

macro_rules! event_kinds {
    ($($(#[$doc:meta])* $variant:ident => $wire:literal,)*) => {
        /// `event-kind`: extension events plus Oino host events (`oino_*`).
        /// [`EventKind::wire`] is the `type` string (kebab → snake).
        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
        #[non_exhaustive]
        pub enum EventKind {
            $($(#[$doc])* #[doc = concat!("`", $wire, "`")] $variant,)*
        }

        impl EventKind {
            /// Every event kind in WIT order.
            pub const ALL: &'static [EventKind] = &[$(EventKind::$variant,)*];

            /// Wire name.
            #[must_use]
            pub fn wire(self) -> &'static str {
                match self { $(EventKind::$variant => $wire,)* }
            }
        }

        impl FromStr for EventKind {
            type Err = UnknownEventKind;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                match s { $($wire => Ok(EventKind::$variant),)* _ => Err(UnknownEventKind(s.to_owned())) }
            }
        }
    };
}

event_kinds! {
    ProjectTrust => "project_trust",
    ResourcesDiscover => "resources_discover",
    McpServersChange => "mcp_servers_change",
    SessionStart => "session_start",
    SessionInfoChanged => "session_info_changed",
    SessionBeforeSwitch => "session_before_switch",
    SessionBeforeFork => "session_before_fork",
    SessionBeforeCompact => "session_before_compact",
    SessionCompact => "session_compact",
    SessionCompactFailed => "session_compact_failed",
    SessionShutdown => "session_shutdown",
    SessionBeforeTree => "session_before_tree",
    SessionTree => "session_tree",
    Context => "context",
    ContextWithSystem => "context_with_system",
    CacheWarmingDecision => "cache_warming_decision",
    BeforeProviderHeaders => "before_provider_headers",
    BeforeProviderRequest => "before_provider_request",
    AfterProviderResponse => "after_provider_response",
    ProviderStreamEvent => "provider_stream_event",
    BeforeAgentStart => "before_agent_start",
    AgentStart => "agent_start",
    AgentEnd => "agent_end",
    AgentBeforeSettle => "agent_before_settle",
    AgentSettled => "agent_settled",
    UiPromptStart => "ui_prompt_start",
    UiPromptEnd => "ui_prompt_end",
    TurnStart => "turn_start",
    TurnEnd => "turn_end",
    MessageStart => "message_start",
    MessageUpdate => "message_update",
    MessageEnd => "message_end",
    ToolExecutionStart => "tool_execution_start",
    ToolExecutionUpdate => "tool_execution_update",
    ToolExecutionEnd => "tool_execution_end",
    ModelSelect => "model_select",
    ThinkingLevelSelect => "thinking_level_select",
    ToolCall => "tool_call",
    ToolResult => "tool_result",
    UserBash => "user_bash",
    Input => "input",
    OinoTimer => "oino_timer",
    OinoBus => "oino_bus",
    OinoUiAction => "oino_ui_action",
    OinoEditor => "oino_editor",
    OinoSettingsChanged => "oino_settings_changed",
    OinoProcess => "oino_process",
    OinoAgent => "oino_agent",
    OinoTask => "oino_task",
}

/// An event name that is not part of `event-kind`.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("unknown event kind `{0}`")]
pub struct UnknownEventKind(pub String);

impl EventKind {
    /// Oino host events (`oino_*`), produced by the host rather than by the extension runner.
    #[must_use]
    pub fn is_oino(self) -> bool {
        self.wire().starts_with("oino_")
    }
}

impl fmt::Display for EventKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.wire())
    }
}

impl Serialize for EventKind {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.wire())
    }
}

impl<'de> Deserialize<'de> for EventKind {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let text = String::deserialize(deserializer)?;
        text.parse().map_err(serde::de::Error::custom)
    }
}

/// `exposure`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Exposure {
    /// Declared to the model and callable by tools.
    #[default]
    Direct,
    /// Declared to the model only.
    ModelOnly,
    /// Declared through tool search.
    Deferred,
    /// Not declared.
    Hidden,
}

/// `execution-mode`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ExecutionMode {
    /// May run in parallel batches.
    #[default]
    Parallel,
    /// Forces sequential batches.
    Sequential,
}

/// `replay`: may the call be replayed after recovery.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Replay {
    /// Never replay.
    Never,
    /// Safe to replay.
    Safe,
}

/// `tool-kind` (ACP tool kind).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ToolKind {
    /// Read.
    Read,
    /// Edit.
    Edit,
    /// Delete.
    Delete,
    /// Move.
    Move,
    /// Search.
    Search,
    /// Execute.
    Execute,
    /// Think.
    Think,
    /// Fetch.
    Fetch,
    /// Switch mode.
    SwitchMode,
    /// Other.
    #[default]
    Other,
}

/// `tool-namespace`.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolNamespace {
    /// Namespace name.
    pub name: String,
    /// Aliases.
    #[serde(default)]
    pub aliases: Vec<String>,
    /// Description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Instructions.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,
}

/// `tool-annotations` (MCP-style hints).
#[allow(clippy::struct_field_names, reason = "mirrors the WIT record")]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolAnnotations {
    /// Read-only hint.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub read_only: Option<bool>,
    /// Destructive hint.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub destructive: Option<bool>,
    /// Idempotent hint.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub idempotent: Option<bool>,
    /// Open-world hint.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub open_world: Option<bool>,
}

/// `reaches-tools`: which tools a tool may call through `control.execute-tool`.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "names", rename_all = "lowercase")]
pub enum ReachesTools {
    /// None.
    #[default]
    None,
    /// Every callable tool.
    All,
    /// Named tools.
    Names(Vec<String>),
    /// Decided per input by the guest export `reaches-tools`.
    Dynamic,
}

/// `tool-spec`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolSpec {
    /// Model-facing name.
    pub name: String,
    /// Frontend label.
    pub label: String,
    /// Model-facing description.
    pub description: String,
    /// JSON Schema object (draft 2020-12).
    pub parameters: RawJson,
    /// Output schema.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_schema: Option<RawJson>,
    /// One-line prompt snippet.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prompt_snippet: Option<String>,
    /// Prompt guidelines.
    #[serde(default)]
    pub prompt_guidelines: Vec<String>,
    /// Exposure.
    #[serde(default)]
    pub exposure: Exposure,
    /// Activated on registration.
    pub default_active: bool,
    /// Execution mode.
    #[serde(default)]
    pub execution_mode: ExecutionMode,
    /// Replay policy.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub replay: Option<Replay>,
    /// ACP kind.
    #[serde(default)]
    pub kind: ToolKind,
    /// Namespace.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub namespace: Option<ToolNamespace>,
    /// Annotations.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotations: Option<ToolAnnotations>,
    /// Nested tool reach.
    #[serde(default)]
    pub reaches_tools: ReachesTools,
    /// Guest implements `prepare-arguments` for this tool.
    #[serde(default)]
    pub prepare_arguments: bool,
    /// Guest implements `prepare-loadout` for this tool.
    #[serde(default)]
    pub prepare_loadout: bool,
    /// `ToolRenderHints` JSON (ui-protocol §6.4).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub render: Option<RawJson>,
}

/// `command-spec`.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommandSpec {
    /// Name without `/`.
    pub name: String,
    /// Description.
    pub description: String,
    /// Argument hint.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub argument_hint: Option<String>,
    /// Guest implements `complete-command`.
    #[serde(default)]
    pub completions: bool,
    /// Not listed in `get_commands`.
    #[serde(default)]
    pub hidden: bool,
}

/// `shortcut-spec`.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShortcutSpec {
    /// Stable action id.
    pub id: String,
    /// Default key ids.
    #[serde(default)]
    pub default_keys: Vec<String>,
    /// Description.
    #[serde(default)]
    pub description: String,
}

/// `flag-type`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FlagType {
    /// `--name` / `--no-name`.
    Boolean,
    /// `--name <value>`.
    String,
}

/// `flag-spec`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FlagSpec {
    /// Long name without `--`.
    pub name: String,
    /// Type.
    #[serde(rename = "type")]
    pub kind: FlagType,
    /// Default value.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_value: Option<RawJson>,
    /// Help text.
    #[serde(default)]
    pub description: String,
}

/// `renderer-target`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum RendererTarget {
    /// Custom message (`customType`).
    CustomMessage,
    /// Custom entry (`customType`).
    CustomEntry,
    /// Tool (tool name).
    Tool,
    /// End-of-turn footer (ui-protocol §6.7): core creates the entry
    /// `<extensionId>/turnFooter:<key>`.
    TurnFooter,
}

/// `renderer-spec`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RendererSpec {
    /// Target.
    pub target: RendererTarget,
    /// `customType` or tool name.
    pub key: String,
}

/// `arbiter-roles` (runtime §7.6).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ArbiterRoles {
    /// Implements `propose-nudge`.
    pub nudge_provider: bool,
    /// Implements `wait-reason`.
    pub wait_source: bool,
}

/// `registration`: everything returned by `init`.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Registration {
    /// Subscribed events.
    #[serde(default)]
    pub events: Vec<EventKind>,
    /// Tools.
    #[serde(default)]
    pub tools: Vec<ToolSpec>,
    /// Commands.
    #[serde(default)]
    pub commands: Vec<CommandSpec>,
    /// Shortcuts.
    #[serde(default)]
    pub shortcuts: Vec<ShortcutSpec>,
    /// Flags.
    #[serde(default)]
    pub flags: Vec<FlagSpec>,
    /// Renderers.
    #[serde(default)]
    pub renderers: Vec<RendererSpec>,
    /// Service names.
    #[serde(default)]
    pub services: Vec<String>,
    /// Arbiter roles.
    #[serde(default)]
    pub arbiter: ArbiterRoles,
    /// `SettingsNamespace` JSON values.
    #[serde(default)]
    pub settings: Vec<RawJson>,
}

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

    #[test]
    fn wire_names_round_trip() {
        assert_eq!(EventKind::ALL.len(), 49);
        assert_eq!(EventKind::ALL.iter().filter(|k| !k.is_oino()).count(), 41);
        for kind in EventKind::ALL {
            assert_eq!(kind.wire().parse::<EventKind>().unwrap(), *kind);
            assert!(!kind.wire().contains('-'));
        }
        assert_eq!(
            serde_json::to_string(&EventKind::SessionBeforeCompact).unwrap(),
            "\"session_before_compact\""
        );
        assert!("nope".parse::<EventKind>().is_err());
    }
}