skill-http 0.3.0

HTTP streaming server for Skill - REST API and web interface support
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
//! Database implementation for execution history persistence

use std::path::Path;
use anyhow::{Context, Result};
use sqlx::{SqlitePool, sqlite::SqlitePoolOptions, Row};
use chrono::{DateTime, Utc};

use crate::types::{ExecutionHistoryEntry, ExecutionStatus};

/// SQLite-based execution history database
pub struct ExecutionHistoryDb {
    pool: SqlitePool,
}

impl ExecutionHistoryDb {
    /// Create a new execution history database
    ///
    /// # Arguments
    /// * `db_path` - Path to the SQLite database file
    ///
    /// # Example
    /// ```no_run
    /// # use skill_http::execution_history::ExecutionHistoryDb;
    /// # async fn example() -> anyhow::Result<()> {
    /// let db = ExecutionHistoryDb::new("~/.skill-engine/execution-history.db").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new(db_path: &str) -> Result<Self> {
        // Expand home directory
        let db_path = shellexpand::tilde(db_path).to_string();

        // Ensure parent directory exists
        if let Some(parent) = Path::new(&db_path).parent() {
            std::fs::create_dir_all(parent)
                .context("Failed to create execution history database directory")?;
        }

        // Build connection URL
        let url = if db_path == ":memory:" {
            "sqlite::memory:".to_string()
        } else {
            format!("sqlite:{}?mode=rwc", db_path)
        };

        // Create connection pool
        let pool = SqlitePoolOptions::new()
            .max_connections(10)
            .connect(&url)
            .await
            .context("Failed to connect to execution history database")?;

        let db = Self { pool };

        // Initialize database schema
        db.setup().await?;

        Ok(db)
    }

    /// Get the connection pool (for advanced usage)
    pub fn pool(&self) -> &SqlitePool {
        &self.pool
    }

    /// Initialize database schema with tables and indexes
    async fn setup(&self) -> Result<()> {
        // Create execution_history table
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS execution_history (
                id TEXT PRIMARY KEY,
                skill TEXT NOT NULL,
                tool TEXT NOT NULL,
                instance TEXT NOT NULL,
                status TEXT NOT NULL,
                duration_ms INTEGER NOT NULL,
                started_at TEXT NOT NULL,
                error TEXT,
                output TEXT
            )
            "#,
        )
        .execute(&self.pool)
        .await
        .context("Failed to create execution_history table")?;

        // Create indexes for common queries
        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_execution_history_started_at
            ON execution_history(started_at DESC);
            "#,
        )
        .execute(&self.pool)
        .await
        .context("Failed to create started_at index")?;

        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_execution_history_skill
            ON execution_history(skill);
            "#,
        )
        .execute(&self.pool)
        .await
        .context("Failed to create skill index")?;

        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_execution_history_status
            ON execution_history(status);
            "#,
        )
        .execute(&self.pool)
        .await
        .context("Failed to create status index")?;

        Ok(())
    }

    /// Add an execution to history
    pub async fn add_execution(&self, entry: &ExecutionHistoryEntry) -> Result<()> {
        let status_str = match entry.status {
            ExecutionStatus::Pending => "pending",
            ExecutionStatus::Running => "running",
            ExecutionStatus::Success => "success",
            ExecutionStatus::Failed => "failed",
            ExecutionStatus::Timeout => "timeout",
            ExecutionStatus::Cancelled => "cancelled",
        };

        sqlx::query(
            r#"
            INSERT INTO execution_history (
                id, skill, tool, instance, status, duration_ms, started_at, error, output
            )
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
            "#,
        )
        .bind(&entry.id)
        .bind(&entry.skill)
        .bind(&entry.tool)
        .bind(&entry.instance)
        .bind(status_str)
        .bind(entry.duration_ms as i64)
        .bind(entry.started_at.to_rfc3339())
        .bind(&entry.error)
        .bind(&entry.output)
        .execute(&self.pool)
        .await
        .context("Failed to insert execution history entry")?;

        Ok(())
    }

    /// Get execution by ID
    pub async fn get_execution(&self, id: &str) -> Result<Option<ExecutionHistoryEntry>> {
        let row = sqlx::query(
            r#"
            SELECT id, skill, tool, instance, status, duration_ms, started_at, error, output
            FROM execution_history
            WHERE id = ?
            "#,
        )
        .bind(id)
        .fetch_optional(&self.pool)
        .await
        .context("Failed to query execution history")?;

        match row {
            Some(row) => Ok(Some(row_to_entry(row)?)),
            None => Ok(None),
        }
    }

    /// List executions with pagination
    pub async fn list_executions(
        &self,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<ExecutionHistoryEntry>> {
        let rows = sqlx::query(
            r#"
            SELECT id, skill, tool, instance, status, duration_ms, started_at, error, output
            FROM execution_history
            ORDER BY started_at DESC
            LIMIT ? OFFSET ?
            "#,
        )
        .bind(limit as i64)
        .bind(offset as i64)
        .fetch_all(&self.pool)
        .await
        .context("Failed to list execution history")?;

        rows.into_iter()
            .map(row_to_entry)
            .collect::<Result<Vec<_>>>()
    }

    /// List executions for a specific skill
    pub async fn list_by_skill(
        &self,
        skill: &str,
        limit: usize,
    ) -> Result<Vec<ExecutionHistoryEntry>> {
        let rows = sqlx::query(
            r#"
            SELECT id, skill, tool, instance, status, duration_ms, started_at, error, output
            FROM execution_history
            WHERE skill = ?
            ORDER BY started_at DESC
            LIMIT ?
            "#,
        )
        .bind(skill)
        .bind(limit as i64)
        .fetch_all(&self.pool)
        .await
        .context("Failed to list execution history by skill")?;

        rows.into_iter()
            .map(row_to_entry)
            .collect::<Result<Vec<_>>>()
    }

    /// List executions by status
    pub async fn list_by_status(
        &self,
        status: &ExecutionStatus,
        limit: usize,
    ) -> Result<Vec<ExecutionHistoryEntry>> {
        let status_str = match status {
            ExecutionStatus::Pending => "pending",
            ExecutionStatus::Running => "running",
            ExecutionStatus::Success => "success",
            ExecutionStatus::Failed => "failed",
            ExecutionStatus::Timeout => "timeout",
            ExecutionStatus::Cancelled => "cancelled",
        };

        let rows = sqlx::query(
            r#"
            SELECT id, skill, tool, instance, status, duration_ms, started_at, error, output
            FROM execution_history
            WHERE status = ?
            ORDER BY started_at DESC
            LIMIT ?
            "#,
        )
        .bind(status_str)
        .bind(limit as i64)
        .fetch_all(&self.pool)
        .await
        .context("Failed to list execution history by status")?;

        rows.into_iter()
            .map(row_to_entry)
            .collect::<Result<Vec<_>>>()
    }

    /// Get total count of executions
    pub async fn count(&self) -> Result<i64> {
        let row = sqlx::query("SELECT COUNT(*) as count FROM execution_history")
            .fetch_one(&self.pool)
            .await
            .context("Failed to count execution history")?;

        Ok(row.get("count"))
    }

    /// Delete execution by ID
    pub async fn delete_execution(&self, id: &str) -> Result<()> {
        sqlx::query("DELETE FROM execution_history WHERE id = ?")
            .bind(id)
            .execute(&self.pool)
            .await
            .context("Failed to delete execution history entry")?;

        Ok(())
    }

    /// Clear all execution history
    pub async fn clear_all(&self) -> Result<()> {
        sqlx::query("DELETE FROM execution_history")
            .execute(&self.pool)
            .await
            .context("Failed to clear execution history")?;

        Ok(())
    }

    /// Delete old executions, keeping only the most recent N entries
    pub async fn prune(&self, keep_count: usize) -> Result<usize> {
        let result = sqlx::query(
            r#"
            DELETE FROM execution_history
            WHERE id NOT IN (
                SELECT id FROM execution_history
                ORDER BY started_at DESC
                LIMIT ?
            )
            "#,
        )
        .bind(keep_count as i64)
        .execute(&self.pool)
        .await
        .context("Failed to prune execution history")?;

        Ok(result.rows_affected() as usize)
    }

    /// Get statistics
    pub async fn get_stats(&self) -> Result<ExecutionStats> {
        let row = sqlx::query(
            r#"
            SELECT
                COUNT(*) as total,
                SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) as success_count,
                SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as failed_count,
                AVG(duration_ms) as avg_duration_ms
            FROM execution_history
            "#,
        )
        .fetch_one(&self.pool)
        .await
        .context("Failed to get execution stats")?;

        Ok(ExecutionStats {
            total: row.get("total"),
            success_count: row.get("success_count"),
            failed_count: row.get("failed_count"),
            avg_duration_ms: row.get::<Option<f64>, _>("avg_duration_ms").unwrap_or(0.0),
        })
    }
}

/// Convert database row to ExecutionHistoryEntry
fn row_to_entry(row: sqlx::sqlite::SqliteRow) -> Result<ExecutionHistoryEntry> {
    let status_str: String = row.get("status");
    let status = match status_str.as_str() {
        "pending" => ExecutionStatus::Pending,
        "running" => ExecutionStatus::Running,
        "success" => ExecutionStatus::Success,
        "failed" => ExecutionStatus::Failed,
        "timeout" => ExecutionStatus::Timeout,
        "cancelled" => ExecutionStatus::Cancelled,
        _ => ExecutionStatus::Failed,
    };

    let started_at_str: String = row.get("started_at");
    let started_at = DateTime::parse_from_rfc3339(&started_at_str)
        .context("Failed to parse started_at timestamp")?
        .with_timezone(&Utc);

    Ok(ExecutionHistoryEntry {
        id: row.get("id"),
        skill: row.get("skill"),
        tool: row.get("tool"),
        instance: row.get("instance"),
        status,
        duration_ms: row.get::<i64, _>("duration_ms") as u64,
        started_at,
        error: row.get("error"),
        output: row.get("output"),
    })
}

/// Execution statistics
#[derive(Debug, Clone)]
pub struct ExecutionStats {
    pub total: i64,
    pub success_count: i64,
    pub failed_count: i64,
    pub avg_duration_ms: f64,
}

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

    #[tokio::test]
    async fn test_execution_history_crud() -> Result<()> {
        let db = ExecutionHistoryDb::new(":memory:").await?;

        // Create test entry
        let entry = ExecutionHistoryEntry {
            id: "test-123".to_string(),
            skill: "test-skill".to_string(),
            tool: "test-tool".to_string(),
            instance: "default".to_string(),
            status: ExecutionStatus::Success,
            duration_ms: 100,
            started_at: Utc::now(),
            error: None,
            output: Some("test output".to_string()),
        };

        // Add
        db.add_execution(&entry).await?;

        // Get by ID
        let retrieved = db.get_execution("test-123").await?;
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().id, "test-123");

        // List
        let list = db.list_executions(10, 0).await?;
        assert_eq!(list.len(), 1);

        // Stats
        let stats = db.get_stats().await?;
        assert_eq!(stats.total, 1);
        assert_eq!(stats.success_count, 1);

        Ok(())
    }
}