fastskill-core 0.9.112

FastSkill core library - AI Skills management toolkit
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
//! Manifest (skill-project.toml) endpoint handlers

use crate::core::manifest::{
    DependenciesSection, DependencySource, DependencySpec, SkillProjectToml,
};
use crate::core::repository::RepositoryManager;
use crate::core::sources::{MarketplaceSkill, SourcesManager};
use crate::http::errors::{HttpError, HttpResult};
use crate::http::handlers::AppState;
use crate::http::models::*;
use axum::{
    extract::{Path, State},
    Json,
};
use std::collections::HashMap;
use std::path::PathBuf;

/// Get repositories from skill-project.toml
pub(crate) fn get_repositories(
    project: &SkillProjectToml,
) -> Vec<crate::core::repository::RepositoryDefinition> {
    use crate::core::repository::{
        RepositoryAuth, RepositoryConfig, RepositoryDefinition, RepositoryType,
    };

    if let Some(tool) = &project.tool {
        if let Some(fastskill_config) = &tool.fastskill {
            if let Some(repos) = &fastskill_config.repositories {
                return repos
                    .iter()
                    .map(|r| {
                        let repo_type = match r.r#type {
                            crate::core::manifest::RepositoryType::HttpRegistry => {
                                RepositoryType::HttpRegistry
                            }
                            crate::core::manifest::RepositoryType::GitMarketplace => {
                                RepositoryType::GitMarketplace
                            }
                            crate::core::manifest::RepositoryType::ZipUrl => RepositoryType::ZipUrl,
                            crate::core::manifest::RepositoryType::Local => RepositoryType::Local,
                        };

                        let config = match &r.connection {
                            crate::core::manifest::RepositoryConnection::HttpRegistry {
                                index_url,
                            } => RepositoryConfig::HttpRegistry {
                                index_url: index_url.clone(),
                            },
                            crate::core::manifest::RepositoryConnection::GitMarketplace {
                                url,
                                branch,
                            } => RepositoryConfig::GitMarketplace {
                                url: url.clone(),
                                branch: branch.clone(),
                                tag: None,
                            },
                            crate::core::manifest::RepositoryConnection::ZipUrl { zip_url } => {
                                RepositoryConfig::ZipUrl {
                                    base_url: zip_url.clone(),
                                }
                            }
                            crate::core::manifest::RepositoryConnection::Local { path } => {
                                RepositoryConfig::Local {
                                    path: std::path::PathBuf::from(path),
                                }
                            }
                        };

                        let auth = r.auth.as_ref().map(|a| match a.r#type {
                            crate::core::manifest::AuthType::Pat => RepositoryAuth::Pat {
                                env_var: a
                                    .env_var
                                    .clone()
                                    .unwrap_or_else(|| "PAT_TOKEN".to_string()),
                            },
                        });

                        RepositoryDefinition {
                            name: r.name.clone(),
                            repo_type,
                            priority: r.priority,
                            config,
                            auth,
                            storage: None,
                        }
                    })
                    .collect();
            }
        }
    }
    Vec::new()
}

/// Get lock file path from project path
/// Returns the canonicalized lock path to prevent path traversal
fn get_lock_path(project_path: &std::path::Path) -> Result<PathBuf, HttpError> {
    let lock_path = if let Some(parent) = project_path.parent() {
        parent.join("skills.lock")
    } else {
        PathBuf::from("skills.lock")
    };

    // Canonicalize if it exists, otherwise return the path as-is for creation
    if lock_path.exists() {
        lock_path.canonicalize().map_err(|e| {
            HttpError::InternalServerError(format!("Failed to resolve lock path: {}", e))
        })
    } else {
        Ok(lock_path)
    }
}

/// GET /api/project - Full skill-project.toml view (metadata, skills_directory, skills with type/location)
pub async fn get_project(
    State(state): State<AppState>,
) -> HttpResult<axum::Json<ApiResponse<serde_json::Value>>> {
    let project_path = &state.project_file_path;

    if !project_path.exists() {
        return Ok(Json(ApiResponse::success(serde_json::json!({
            "metadata": null,
            "skills_directory": null,
            "skills": []
        }))));
    }

    let project = SkillProjectToml::load_from_file(project_path).map_err(|e| {
        HttpError::InternalServerError(format!("Failed to load skill-project.toml: {}", e))
    })?;

    let metadata = project.metadata.as_ref().map(|m| {
        serde_json::json!({
            "id": m.id,
            "version": m.version,
            "description": m.description,
            "author": m.author,
            "name": m.name
        })
    });

    // Use skills_directory from validated config loaded at server startup
    let skills_directory = state.skills_directory.to_string_lossy().to_string();

    let skills: Vec<serde_json::Value> = project
        .dependencies
        .as_ref()
        .map(|deps| {
            deps.dependencies
                .iter()
                .map(|(id, spec)| {
                    let (typ, location) = match spec {
                        DependencySpec::Version(v) => {
                            ("source".to_string(), format!("version {}", v))
                        }
                        DependencySpec::Inline {
                            source,
                            source_specific,
                            ..
                        } => {
                            let typ = match source {
                                DependencySource::Git => "git",
                                DependencySource::Local => "local",
                                DependencySource::ZipUrl => "zip-url",
                                DependencySource::Source => "source",
                            }
                            .to_string();
                            let location = match source {
                                DependencySource::Git => source_specific
                                    .url
                                    .clone()
                                    .map(|u| {
                                        source_specific
                                            .branch
                                            .as_ref()
                                            .map(|b| format!("{} (branch: {})", u, b))
                                            .unwrap_or(u)
                                    })
                                    .unwrap_or_else(|| "—".to_string()),
                                DependencySource::Local => source_specific
                                    .path
                                    .clone()
                                    .unwrap_or_else(|| "—".to_string()),
                                DependencySource::ZipUrl => source_specific
                                    .zip_url
                                    .clone()
                                    .unwrap_or_else(|| "—".to_string()),
                                DependencySource::Source => source_specific
                                    .name
                                    .as_ref()
                                    .zip(source_specific.skill.as_ref())
                                    .map(|(n, s)| format!("{} / {}", n, s))
                                    .unwrap_or_else(|| "—".to_string()),
                            };
                            (typ, location)
                        }
                    };
                    serde_json::json!({ "id": id, "type": typ, "location": location })
                })
                .collect()
        })
        .unwrap_or_default();

    let data = serde_json::json!({
        "metadata": metadata,
        "skills_directory": skills_directory,
        "skills": skills
    });

    Ok(Json(ApiResponse::success(data)))
}

/// GET /api/manifest/skills - List all skills from skill-project.toml
pub async fn list_manifest_skills(
    State(state): State<AppState>,
) -> HttpResult<axum::Json<ApiResponse<Vec<ManifestSkillResponse>>>> {
    let project_path = &state.project_file_path;

    // Load project
    let project = if project_path.exists() {
        SkillProjectToml::load_from_file(project_path).map_err(|e| {
            HttpError::InternalServerError(format!("Failed to load skill-project.toml: {}", e))
        })?
    } else {
        // Return empty list if project doesn't exist
        return Ok(Json(ApiResponse::success(Vec::new())));
    };

    let skills: Vec<ManifestSkillResponse> = project
        .dependencies
        .map(|deps| {
            deps.dependencies
                .iter()
                .map(|(id, spec)| {
                    let (version, source_type) = match spec {
                        DependencySpec::Version(v) => (Some(v.clone()), "source"),
                        DependencySpec::Inline {
                            source,
                            source_specific,
                            ..
                        } => {
                            let stype = match source {
                                DependencySource::Git => "git",
                                DependencySource::Local => "local",
                                DependencySource::ZipUrl => "zip-url",
                                DependencySource::Source => "source",
                            };
                            (source_specific.version.clone(), stype)
                        }
                    };

                    ManifestSkillResponse {
                        id: id.clone(),
                        version,
                        groups: Vec::new(), // TODO: extract from inline spec
                        editable: false,    // TODO: extract from inline spec
                        source_type: source_type.to_string(),
                    }
                })
                .collect()
        })
        .unwrap_or_default();

    Ok(Json(ApiResponse::success(skills)))
}

/// POST /api/manifest/skills - Add skill to skill-project.toml
pub async fn add_skill_to_manifest(
    State(state): State<AppState>,
    Json(request): Json<AddSkillRequest>,
) -> HttpResult<axum::Json<ApiResponse<ManifestSkillResponse>>> {
    let project_path = &state.project_file_path;
    let _lock_path = get_lock_path(project_path)?;

    // Load project or create new
    let mut project = if project_path.exists() {
        SkillProjectToml::load_from_file(project_path).map_err(|e| {
            HttpError::InternalServerError(format!("Failed to load skill-project.toml: {}", e))
        })?
    } else {
        // Ensure parent directory exists
        if let Some(parent) = project_path.parent() {
            // Validate parent path to prevent traversal
            let safe_parent = if parent.exists() {
                parent.canonicalize().map_err(|e| {
                    HttpError::InternalServerError(format!("Failed to resolve parent path: {}", e))
                })?
            } else {
                parent.to_path_buf()
            };
            std::fs::create_dir_all(&safe_parent).map_err(|e| {
                HttpError::InternalServerError(format!("Failed to create directory: {}", e))
            })?;
        }

        SkillProjectToml {
            metadata: None,
            dependencies: Some(DependenciesSection {
                dependencies: HashMap::new(),
            }),
            tool: None,
        }
    };

    // Ensure dependencies section exists
    if project.dependencies.is_none() {
        project.dependencies = Some(DependenciesSection {
            dependencies: HashMap::new(),
        });
    }

    // Get repositories and create manager
    let repositories = get_repositories(&project);
    let repo_manager = RepositoryManager::from_definitions(repositories);

    // Get sources manager for marketplace-based repositories
    let sources_manager = create_sources_manager_from_repositories(&repo_manager)?;

    // Find the skill in sources
    let marketplace_skill = if let Some(sources_mgr) = &sources_manager {
        find_skill_in_sources(sources_mgr, &request.skill_id, &request.source_name)
            .await
            .ok_or_else(|| {
                HttpError::NotFound(format!(
                    "Skill '{}' not found in source '{}'",
                    request.skill_id, request.source_name
                ))
            })?
    } else {
        return Err(HttpError::NotFound(
            "No marketplace sources configured".to_string(),
        ));
    };

    // Create dependency spec - simple version for now
    let dep_spec = DependencySpec::Version(marketplace_skill.version.clone());

    // Add to dependencies
    if let Some(ref mut deps) = project.dependencies {
        deps.dependencies.insert(request.skill_id.clone(), dep_spec);
    }

    // Save project
    project.save_to_file(project_path).map_err(|e| {
        HttpError::InternalServerError(format!("Failed to save skill-project.toml: {}", e))
    })?;

    let response = ManifestSkillResponse {
        id: request.skill_id.clone(),
        version: Some(marketplace_skill.version),
        groups: request.groups.unwrap_or_default(),
        editable: request.editable.unwrap_or(false),
        source_type: "source".to_string(),
    };

    Ok(Json(ApiResponse::success(response)))
}

/// DELETE /api/manifest/skills/:id - Remove skill from skill-project.toml
pub async fn remove_skill_from_manifest(
    Path(skill_id): Path<String>,
    State(state): State<AppState>,
) -> HttpResult<axum::Json<ApiResponse<()>>> {
    let project_path = &state.project_file_path;
    let lock_path = get_lock_path(project_path)?;

    if !project_path.exists() {
        return Err(HttpError::NotFound(
            "skill-project.toml not found".to_string(),
        ));
    }

    // Remove from project
    let mut project = SkillProjectToml::load_from_file(project_path).map_err(|e| {
        HttpError::InternalServerError(format!("Failed to load skill-project.toml: {}", e))
    })?;

    if let Some(ref mut deps) = project.dependencies {
        deps.dependencies.remove(&skill_id);
    }

    project.save_to_file(project_path).map_err(|e| {
        HttpError::InternalServerError(format!("Failed to save skill-project.toml: {}", e))
    })?;

    // Remove from lock file if it exists
    if lock_path.exists() {
        use crate::core::lock::SkillsLock;
        let mut lock = SkillsLock::load_from_file(&lock_path).map_err(|e| {
            HttpError::InternalServerError(format!("Failed to load lock file: {}", e))
        })?;
        lock.remove_skill(&skill_id);
        lock.save_to_file(&lock_path).map_err(|e| {
            HttpError::InternalServerError(format!("Failed to save lock file: {}", e))
        })?;
    }

    Ok(Json(ApiResponse::success(())))
}

/// PUT /api/manifest/skills/:id - Update skill in skill-project.toml
pub async fn update_skill_in_manifest(
    Path(skill_id): Path<String>,
    State(state): State<AppState>,
    Json(request): Json<UpdateSkillRequest>,
) -> HttpResult<axum::Json<ApiResponse<ManifestSkillResponse>>> {
    let project_path = &state.project_file_path;

    if !project_path.exists() {
        return Err(HttpError::NotFound(
            "skill-project.toml not found".to_string(),
        ));
    }

    let mut project = SkillProjectToml::load_from_file(project_path).map_err(|e| {
        HttpError::InternalServerError(format!("Failed to load skill-project.toml: {}", e))
    })?;

    // Find and update dependency
    let updated_version = if let Some(ref mut deps) = project.dependencies {
        if let Some(dep_spec) = deps.dependencies.get_mut(&skill_id) {
            // Update version if provided
            if let Some(ref version) = request.version {
                *dep_spec = DependencySpec::Version(version.clone());
            }
            match dep_spec {
                DependencySpec::Version(v) => Some(v.clone()),
                _ => None,
            }
        } else {
            return Err(HttpError::NotFound(format!(
                "Skill '{}' not found in project",
                skill_id
            )));
        }
    } else {
        return Err(HttpError::NotFound(format!(
            "Skill '{}' not found in project",
            skill_id
        )));
    };

    // Save project
    project.save_to_file(project_path).map_err(|e| {
        HttpError::InternalServerError(format!("Failed to save skill-project.toml: {}", e))
    })?;

    let response = ManifestSkillResponse {
        id: skill_id,
        version: request.version.or(updated_version),
        groups: request.groups.unwrap_or_default(),
        editable: request.editable.unwrap_or(false),
        source_type: "source".to_string(),
    };

    Ok(Json(ApiResponse::success(response)))
}

/// Helper function to create sources manager from repository manager
fn create_sources_manager_from_repositories(
    _repo_manager: &RepositoryManager,
) -> Result<Option<SourcesManager>, HttpError> {
    // For now, create an empty sources manager - in future can be enhanced to convert repositories
    // This is a simplified implementation
    Ok(None)
}

/// Helper function to find skill in sources
async fn find_skill_in_sources(
    sources_manager: &SourcesManager,
    skill_id: &str,
    source_name: &str,
) -> Option<MarketplaceSkill> {
    let marketplace = sources_manager
        .get_marketplace_json(source_name)
        .await
        .ok()?;
    marketplace.skills.into_iter().find(|s| s.id == skill_id)
}