package cmd import ( "fmt" "path/filepath" "strings" "time" "github.com/david/knox/internal/db" "github.com/david/knox/internal/skillsrepo" ) // snippetEncodeArgs carries an agent's curation decision for one snippet. type snippetEncodeArgs struct { Fingerprint string Title string Description string Tags string Language string Skill string // skill name, or "auto" to derive from the title Overwrite bool } // encodeSnippet is the single curation path shared by the CLI and the MCP tool. // It optionally materializes the snippet as a ~/.skills SKILL.md (the second // sink), then marks the snippet encoded in the index. func encodeSnippet(kdb *db.KnoxDB, a snippetEncodeArgs) (string, error) { s, err := kdb.SnippetByFingerprint(a.Fingerprint) if err != nil { return "", err } if s == nil { return "", fmt.Errorf("no snippet matching %q", a.Fingerprint) } if s.Status == "rejected" { return "", fmt.Errorf("snippet %s is rejected; un-reject it before encoding", shortFP(s.Fingerprint)) } title := firstNonEmpty(a.Title, s.Title) description := firstNonEmpty(a.Description, s.Description) if description == "" { description = title } skillName := "" skillPath := "" if strings.TrimSpace(a.Skill) != "" { name := a.Skill if name == "auto" { name = title } md := snippetSkillMarkdown(s, name, title, description, a.Tags) skillPath, err = skillsrepo.WriteSkill(name, md, a.Overwrite) if err != nil { return "", err } skillName = filepath.Base(filepath.Dir(skillPath)) } ok, err := kdb.EncodeSnippet(a.Fingerprint, a.Title, a.Description, a.Tags, a.Language, skillName) if err != nil { return "", err } if !ok { return "", fmt.Errorf("snippet %s not encoded (not found or already rejected)", shortFP(s.Fingerprint)) } var b strings.Builder fmt.Fprintf(&b, "Encoded snippet %s (%s, %s)", shortFP(s.Fingerprint), firstNonEmpty(s.Language, "text"), s.Tool) if skillPath != "" { fmt.Fprintf(&b, "\nWrote skill: %s", skillPath) } return b.String(), nil } // snippetSkillMarkdown renders a SKILL.md for an encoded snippet, matching the // frontmatter shape the skills MCP and knox's skills ingester expect. func snippetSkillMarkdown(s *db.Snippet, name, title, description, tags string) string { slug := skillsrepo.Slugify(name) allTags := strings.TrimSpace(tags) if s.Project != "" { allTags = strings.TrimSpace(allTags + " scope:project:" + s.Project) } allTags = strings.TrimSpace(allTags + " scope:machine:specific") fence := "```" if strings.Contains(s.Code, "```") { fence = "````" } lang := s.Language if lang == "" { lang = "text" } var b strings.Builder b.WriteString("---\n") fmt.Fprintf(&b, "name: %s\n", slug) fmt.Fprintf(&b, "description: %s\n", oneLine(description, 400)) b.WriteString("compatibility: opencode\n") b.WriteString("metadata:\n") fmt.Fprintf(&b, " tags: %q\n", allTags) fmt.Fprintf(&b, " created_at: %q\n", time.Now().UTC().Format(time.RFC3339)) b.WriteString(" source_type: \"knox-snippet\"\n") fmt.Fprintf(&b, " kind: %q\n", kindOr(s.Kind)) fmt.Fprintf(&b, " source_session: %q\n", s.SourceSession) fmt.Fprintf(&b, " source_path: %q\n", s.SourcePath) fmt.Fprintf(&b, " snippet_fingerprint: %q\n", s.Fingerprint) b.WriteString("---\n\n") technique := kindOr(s.Kind) == "technique" fmt.Fprintf(&b, "# %s\n\n", title) if description != "" { fmt.Fprintf(&b, "%s\n\n", description) } heading := "## Snippet" if technique { heading = "## Recipe" } fmt.Fprintf(&b, "%s\n\n", heading) fmt.Fprintf(&b, "%s%s\n%s\n%s\n", fence, lang, s.Code, fence) b.WriteString("\n## Provenance\n\n") fmt.Fprintf(&b, "- Extracted from an opencode session where the %s tool completed successfully.\n", s.Tool) if s.SourcePath != "" && s.SourcePath != "(bash)" { fmt.Fprintf(&b, "- File: `%s`\n", s.SourcePath) } if s.SourceSession != "" { fmt.Fprintf(&b, "- Session: `%s`\n", s.SourceSession) } fmt.Fprintf(&b, "- Evidence: `%s`\n", s.Evidence) return b.String() } func firstNonEmpty(vals ...string) string { for _, v := range vals { if strings.TrimSpace(v) != "" { return strings.TrimSpace(v) } } return "" } func oneLine(s string, n int) string { s = strings.Join(strings.Fields(s), " ") return truncateStr(s, n) }