|
| 1 | +// Copyright 2026 The KitOps Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package generate |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/kitops-ml/kitops/pkg/artifact" |
| 24 | + "github.com/kitops-ml/kitops/pkg/output" |
| 25 | + |
| 26 | + "go.yaml.in/yaml/v3" |
| 27 | +) |
| 28 | + |
| 29 | +type SkillFrontmatter struct { |
| 30 | + Name string `yaml:"name"` |
| 31 | + Description string `yaml:"description"` |
| 32 | + License string `yaml:"license,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +func parseSkillFrontmatter(skillMDPath string) *SkillFrontmatter { |
| 36 | + data, err := os.ReadFile(skillMDPath) |
| 37 | + if err != nil { |
| 38 | + output.Logf(output.LogLevelWarn, "Failed to read %s: %s", skillMDPath, err) |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + content := string(data) |
| 43 | + if !strings.HasPrefix(content, "---\n") { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + end := strings.Index(content[4:], "\n---") |
| 48 | + if end == -1 { |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + frontmatterYAML := content[4 : 4+end] |
| 53 | + if strings.TrimSpace(frontmatterYAML) == "" { |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + var fm SkillFrontmatter |
| 58 | + if err := yaml.Unmarshal([]byte(frontmatterYAML), &fm); err != nil { |
| 59 | + output.Logf(output.LogLevelWarn, "Malformed frontmatter in %s: %s", skillMDPath, err) |
| 60 | + return nil |
| 61 | + } |
| 62 | + return &fm |
| 63 | +} |
| 64 | + |
| 65 | +func dirContainsSkillMD(dir DirectoryListing) (bool, string) { |
| 66 | + for _, file := range dir.Files { |
| 67 | + if strings.EqualFold(file.Name, "skill.md") { |
| 68 | + return true, file.Path |
| 69 | + } |
| 70 | + } |
| 71 | + return false, "" |
| 72 | +} |
| 73 | + |
| 74 | +func buildPromptFromSkill(dir DirectoryListing) artifact.Prompt { |
| 75 | + prompt := artifact.Prompt{ |
| 76 | + Path: dir.Path, |
| 77 | + } |
| 78 | + |
| 79 | + found, skillPath := dirContainsSkillMD(dir) |
| 80 | + if !found { |
| 81 | + return prompt |
| 82 | + } |
| 83 | + |
| 84 | + fm := parseSkillFrontmatter(skillPath) |
| 85 | + if fm != nil { |
| 86 | + prompt.Name = fm.Name |
| 87 | + prompt.Description = fm.Description |
| 88 | + } |
| 89 | + return prompt |
| 90 | +} |
| 91 | + |
| 92 | +func applySkillMetadataToPackage(kitfile *artifact.KitFile, dir DirectoryListing) { |
| 93 | + var skillFrontmatters []*SkillFrontmatter |
| 94 | + for _, subDir := range dir.Subdirs { |
| 95 | + if found, skillPath := dirContainsSkillMD(subDir); found { |
| 96 | + if fm := parseSkillFrontmatter(skillPath); fm != nil { |
| 97 | + skillFrontmatters = append(skillFrontmatters, fm) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if len(skillFrontmatters) == 0 { |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + if len(skillFrontmatters) == 1 { |
| 107 | + fm := skillFrontmatters[0] |
| 108 | + if kitfile.Package.Name == "" { |
| 109 | + kitfile.Package.Name = fm.Name |
| 110 | + } |
| 111 | + if kitfile.Package.Description == "" { |
| 112 | + kitfile.Package.Description = fm.Description |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + first := skillFrontmatters[0] |
| 117 | + if kitfile.Package.License == "" && first.License != "" { |
| 118 | + kitfile.Package.License = first.License |
| 119 | + output.Logf(output.LogLevelWarn, "Using license from skill %q", first.Name) |
| 120 | + } |
| 121 | +} |
0 commit comments