skill-runtime 0.3.0

Core execution engine for Skill - WASM sandbox, Docker runtime, and native skill execution
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
//! SSE streaming event types for AI generation
//!
//! Provides typed events for real-time streaming of generation progress,
//! compatible with Server-Sent Events (SSE) protocol.

use serde::{Deserialize, Serialize};
use std::time::Duration;

// =============================================================================
// Generation Events
// =============================================================================

/// Events emitted during AI-powered example generation
///
/// These events follow the SSE (Server-Sent Events) format and can be
/// streamed to CLI or MCP clients in real-time.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum GenerationEvent {
    /// Generation process has started for a tool
    Started {
        /// Name of the tool being processed
        tool_name: String,
        /// Total number of tools to process
        total_tools: usize,
        /// Current tool index (1-based)
        current_index: usize,
    },

    /// LLM is processing/thinking
    Thinking {
        /// Current thought/reasoning step
        thought: String,
    },

    /// Performing an intermediate search
    Searching {
        /// Search query being executed
        query: String,
    },

    /// Search results received
    SearchResult {
        /// Tool names found
        tools: Vec<String>,
        /// Number of results
        count: usize,
    },

    /// An example has been generated
    Example {
        /// The generated example
        example: GeneratedExample,
    },

    /// Validation result for an example
    Validation {
        /// Whether the example passed validation
        valid: bool,
        /// Validation errors (if any)
        errors: Vec<String>,
        /// Index of the example being validated
        example_index: usize,
    },

    /// Progress update
    Progress {
        /// Current item being processed
        current: usize,
        /// Total items to process
        total: usize,
        /// Completion percentage (0.0 - 100.0)
        percent: f32,
        /// Optional message
        message: Option<String>,
    },

    /// Tool generation completed
    ToolCompleted {
        /// Tool name
        tool_name: String,
        /// Number of examples generated
        examples_generated: usize,
        /// Number of valid examples
        valid_examples: usize,
        /// Duration in milliseconds
        duration_ms: u64,
    },

    /// All generation completed
    Completed {
        /// Total examples generated across all tools
        total_examples: usize,
        /// Total valid examples
        total_valid: usize,
        /// Total tools processed
        total_tools: usize,
        /// Total duration in milliseconds
        duration_ms: u64,
    },

    /// An error occurred
    Error {
        /// Error message
        message: String,
        /// Whether the error is recoverable
        recoverable: bool,
        /// Optional tool name context
        tool_name: Option<String>,
    },

    /// Agent reasoning step (for self-ask patterns)
    AgentStep {
        /// The reasoning step
        step: AgentStep,
    },
}

impl GenerationEvent {
    /// Create a started event
    pub fn started(tool_name: impl Into<String>, total_tools: usize, current_index: usize) -> Self {
        Self::Started {
            tool_name: tool_name.into(),
            total_tools,
            current_index,
        }
    }

    /// Create a thinking event
    pub fn thinking(thought: impl Into<String>) -> Self {
        Self::Thinking {
            thought: thought.into(),
        }
    }

    /// Create a progress event
    pub fn progress(current: usize, total: usize, message: Option<String>) -> Self {
        let percent = if total > 0 {
            (current as f32 / total as f32) * 100.0
        } else {
            0.0
        };
        Self::Progress {
            current,
            total,
            percent,
            message,
        }
    }

    /// Create an error event
    pub fn error(message: impl Into<String>, recoverable: bool) -> Self {
        Self::Error {
            message: message.into(),
            recoverable,
            tool_name: None,
        }
    }

    /// Create an error event with tool context
    pub fn tool_error(message: impl Into<String>, tool_name: impl Into<String>, recoverable: bool) -> Self {
        Self::Error {
            message: message.into(),
            recoverable,
            tool_name: Some(tool_name.into()),
        }
    }

    /// Create a completed event
    pub fn completed(total_examples: usize, total_valid: usize, total_tools: usize, duration: Duration) -> Self {
        Self::Completed {
            total_examples,
            total_valid,
            total_tools,
            duration_ms: duration.as_millis() as u64,
        }
    }

    /// Format as SSE data line
    pub fn to_sse_data(&self) -> String {
        format!("data: {}\n\n", serde_json::to_string(self).unwrap_or_default())
    }

    /// Format as SSE with event type
    pub fn to_sse(&self) -> String {
        let event_type = match self {
            Self::Started { .. } => "started",
            Self::Thinking { .. } => "thinking",
            Self::Searching { .. } => "searching",
            Self::SearchResult { .. } => "search_result",
            Self::Example { .. } => "example",
            Self::Validation { .. } => "validation",
            Self::Progress { .. } => "progress",
            Self::ToolCompleted { .. } => "tool_completed",
            Self::Completed { .. } => "completed",
            Self::Error { .. } => "error",
            Self::AgentStep { .. } => "agent_step",
        };
        format!(
            "event: {}\ndata: {}\n\n",
            event_type,
            serde_json::to_string(self).unwrap_or_default()
        )
    }
}

// =============================================================================
// Generated Example
// =============================================================================

/// A generated usage example for a tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneratedExample {
    /// Full command string (e.g., "skill run k8s:apply --file=deploy.yaml")
    pub command: String,

    /// Human-readable explanation of what the command does
    pub explanation: String,

    /// Model confidence score (0.0 - 1.0)
    #[serde(default)]
    pub confidence: f32,

    /// Whether the example passed schema validation
    #[serde(default)]
    pub validated: bool,

    /// Optional use case category
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<String>,

    /// Parameter values used in this example
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parameters: Option<serde_json::Value>,
}

impl GeneratedExample {
    /// Create a new generated example
    pub fn new(command: impl Into<String>, explanation: impl Into<String>) -> Self {
        Self {
            command: command.into(),
            explanation: explanation.into(),
            confidence: 0.0,
            validated: false,
            category: None,
            parameters: None,
        }
    }

    /// Set confidence score
    pub fn with_confidence(mut self, confidence: f32) -> Self {
        self.confidence = confidence.clamp(0.0, 1.0);
        self
    }

    /// Mark as validated
    pub fn with_validated(mut self, validated: bool) -> Self {
        self.validated = validated;
        self
    }

    /// Set category
    pub fn with_category(mut self, category: impl Into<String>) -> Self {
        self.category = Some(category.into());
        self
    }
}

// =============================================================================
// Agent Reasoning Steps
// =============================================================================

/// A step in the agent's reasoning process (self-ask-with-search pattern)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentStep {
    /// Step number
    pub step_number: usize,

    /// The agent's current thought
    pub thought: String,

    /// Optional follow-up question
    #[serde(skip_serializing_if = "Option::is_none")]
    pub follow_up_question: Option<String>,

    /// Search results for this step
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_results: Option<Vec<SearchResultRef>>,

    /// Whether this is the final answer step
    pub is_final: bool,

    /// Final answer (if is_final = true)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub final_answer: Option<String>,
}

impl AgentStep {
    /// Create a thinking step
    pub fn thinking(step_number: usize, thought: impl Into<String>) -> Self {
        Self {
            step_number,
            thought: thought.into(),
            follow_up_question: None,
            search_results: None,
            is_final: false,
            final_answer: None,
        }
    }

    /// Create a follow-up question step
    pub fn follow_up(step_number: usize, thought: impl Into<String>, question: impl Into<String>) -> Self {
        Self {
            step_number,
            thought: thought.into(),
            follow_up_question: Some(question.into()),
            search_results: None,
            is_final: false,
            final_answer: None,
        }
    }

    /// Create a final answer step
    pub fn final_answer(step_number: usize, thought: impl Into<String>, answer: impl Into<String>) -> Self {
        Self {
            step_number,
            thought: thought.into(),
            follow_up_question: None,
            search_results: None,
            is_final: true,
            final_answer: Some(answer.into()),
        }
    }

    /// Add search results to this step
    pub fn with_search_results(mut self, results: Vec<SearchResultRef>) -> Self {
        self.search_results = Some(results);
        self
    }
}

/// A reference to a search result (lightweight for streaming)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResultRef {
    /// Tool name (e.g., "kubernetes:apply")
    pub tool_name: String,
    /// Brief description
    pub description: String,
    /// Relevance score
    pub score: f32,
}

// =============================================================================
// Stream Builder
// =============================================================================

/// Builder for creating generation event streams
pub struct GenerationStreamBuilder {
    tool_name: String,
    total_tools: usize,
    current_index: usize,
}

impl GenerationStreamBuilder {
    /// Create a new stream builder
    pub fn new(tool_name: impl Into<String>, total_tools: usize, current_index: usize) -> Self {
        Self {
            tool_name: tool_name.into(),
            total_tools,
            current_index,
        }
    }

    /// Get the started event
    pub fn started(&self) -> GenerationEvent {
        GenerationEvent::started(&self.tool_name, self.total_tools, self.current_index)
    }

    /// Create a thinking event
    pub fn thinking(&self, thought: impl Into<String>) -> GenerationEvent {
        GenerationEvent::thinking(thought)
    }

    /// Create an example event
    pub fn example(&self, example: GeneratedExample) -> GenerationEvent {
        GenerationEvent::Example { example }
    }

    /// Create a validation event
    pub fn validation(&self, valid: bool, errors: Vec<String>, example_index: usize) -> GenerationEvent {
        GenerationEvent::Validation {
            valid,
            errors,
            example_index,
        }
    }

    /// Create a tool completed event
    pub fn tool_completed(&self, examples_generated: usize, valid_examples: usize, duration: Duration) -> GenerationEvent {
        GenerationEvent::ToolCompleted {
            tool_name: self.tool_name.clone(),
            examples_generated,
            valid_examples,
            duration_ms: duration.as_millis() as u64,
        }
    }

    /// Create an error event
    pub fn error(&self, message: impl Into<String>, recoverable: bool) -> GenerationEvent {
        GenerationEvent::tool_error(message, &self.tool_name, recoverable)
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_generation_event_serialization() {
        let event = GenerationEvent::started("kubernetes:apply", 10, 1);
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("\"type\":\"started\""));
        assert!(json.contains("\"tool_name\":\"kubernetes:apply\""));
        assert!(json.contains("\"total_tools\":10"));

        // Deserialize back
        let parsed: GenerationEvent = serde_json::from_str(&json).unwrap();
        if let GenerationEvent::Started { tool_name, total_tools, current_index } = parsed {
            assert_eq!(tool_name, "kubernetes:apply");
            assert_eq!(total_tools, 10);
            assert_eq!(current_index, 1);
        } else {
            panic!("Expected Started event");
        }
    }

    #[test]
    fn test_thinking_event() {
        let event = GenerationEvent::thinking("Analyzing parameter schema...");
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("\"type\":\"thinking\""));
        assert!(json.contains("Analyzing parameter schema"));
    }

    #[test]
    fn test_progress_event() {
        let event = GenerationEvent::progress(5, 10, Some("Processing tools".to_string()));
        if let GenerationEvent::Progress { current, total, percent, message } = event {
            assert_eq!(current, 5);
            assert_eq!(total, 10);
            assert!((percent - 50.0).abs() < 0.01);
            assert_eq!(message, Some("Processing tools".to_string()));
        } else {
            panic!("Expected Progress event");
        }
    }

    #[test]
    fn test_error_event() {
        let event = GenerationEvent::tool_error("Connection timeout", "k8s:apply", true);
        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("\"type\":\"error\""));
        assert!(json.contains("\"recoverable\":true"));
        assert!(json.contains("\"tool_name\":\"k8s:apply\""));
    }

    #[test]
    fn test_generated_example() {
        let example = GeneratedExample::new(
            "skill run k8s:apply --file=deploy.yaml",
            "Apply a Kubernetes deployment manifest"
        )
        .with_confidence(0.95)
        .with_validated(true)
        .with_category("deployment");

        assert_eq!(example.command, "skill run k8s:apply --file=deploy.yaml");
        assert!((example.confidence - 0.95).abs() < 0.01);
        assert!(example.validated);
        assert_eq!(example.category, Some("deployment".to_string()));
    }

    #[test]
    fn test_agent_step() {
        let step = AgentStep::follow_up(
            1,
            "I need to find tools for container deployment",
            "What tools handle Kubernetes deployments?"
        );

        assert_eq!(step.step_number, 1);
        assert!(!step.is_final);
        assert!(step.follow_up_question.is_some());
        assert!(step.final_answer.is_none());

        let final_step = AgentStep::final_answer(
            3,
            "Based on my search, I recommend using kubernetes:apply",
            "Use kubernetes:apply with --file flag to deploy your manifest"
        );

        assert!(final_step.is_final);
        assert!(final_step.final_answer.is_some());
    }

    #[test]
    fn test_sse_format() {
        let event = GenerationEvent::thinking("Processing...");
        let sse = event.to_sse();

        assert!(sse.starts_with("event: thinking\n"));
        assert!(sse.contains("data: "));
        assert!(sse.ends_with("\n\n"));
    }

    #[test]
    fn test_stream_builder() {
        let builder = GenerationStreamBuilder::new("docker:build", 5, 2);

        let started = builder.started();
        if let GenerationEvent::Started { tool_name, total_tools, current_index } = started {
            assert_eq!(tool_name, "docker:build");
            assert_eq!(total_tools, 5);
            assert_eq!(current_index, 2);
        }

        let example = GeneratedExample::new("skill run docker:build .", "Build Docker image");
        let event = builder.example(example);
        assert!(matches!(event, GenerationEvent::Example { .. }));
    }

    #[test]
    fn test_completed_event() {
        let event = GenerationEvent::completed(50, 45, 10, Duration::from_secs(30));
        if let GenerationEvent::Completed { total_examples, total_valid, total_tools, duration_ms } = event {
            assert_eq!(total_examples, 50);
            assert_eq!(total_valid, 45);
            assert_eq!(total_tools, 10);
            assert_eq!(duration_ms, 30000);
        } else {
            panic!("Expected Completed event");
        }
    }
}